r12620: Get rid of automatically generated lists of init functions of subsystems.
[sfrench/samba-autobuild/.git] / source4 / torture / gentest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    generic testing tool
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "request.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "librpc/gen_ndr/ndr_security.h"
27 #include "auth/gensec/gensec.h"
28
29 #define NSERVERS 2
30 #define NINSTANCES 2
31
32 /* global options */
33 static struct gentest_options {
34         BOOL showall;
35         BOOL analyze;
36         BOOL analyze_always;
37         BOOL analyze_continuous;
38         uint_t max_open_handles;
39         uint_t seed;
40         uint_t numops;
41         BOOL use_oplocks;
42         char **ignore_patterns;
43         const char *seeds_file;
44         BOOL use_preset_seeds;
45         BOOL fast_reconnect;
46 } options;
47
48 /* mapping between open handles on the server and local handles */
49 static struct {
50         BOOL active;
51         uint_t instance;
52         uint_t server_fnum[NSERVERS];
53         const char *name;
54 } *open_handles;
55 static uint_t num_open_handles;
56
57 /* state information for the servers. We open NINSTANCES connections to
58    each server */
59 static struct {
60         struct smbcli_state *cli[NINSTANCES];
61         char *server_name;
62         char *share_name;
63         struct cli_credentials *credentials;
64 } servers[NSERVERS];
65
66 /* the seeds and flags for each operation */
67 static struct {
68         uint_t seed;
69         BOOL disabled;
70 } *op_parms;
71
72
73 /* oplock break info */
74 static struct {
75         BOOL got_break;
76         uint16_t fnum;
77         uint16_t handle;
78         uint8_t level;
79         BOOL do_close;
80 } oplocks[NSERVERS][NINSTANCES];
81
82 /* change notify reply info */
83 static struct {
84         int notify_count;
85         NTSTATUS status;
86         struct smb_notify notify;
87 } notifies[NSERVERS][NINSTANCES];
88
89 /* info relevant to the current operation */
90 static struct {
91         const char *name;
92         uint_t seed;
93         NTSTATUS status;
94         uint_t opnum;
95         TALLOC_CTX *mem_ctx;
96 } current_op;
97
98
99
100 #define BAD_HANDLE 0xFFFE
101
102 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private);
103 static void idle_func(struct smbcli_transport *transport, void *private);
104
105 /*
106   check if a string should be ignored. This is used as the basis
107   for all error ignore settings
108 */
109 static BOOL ignore_pattern(const char *str)
110 {
111         int i;
112         if (!options.ignore_patterns) return False;
113
114         for (i=0;options.ignore_patterns[i];i++) {
115                 if (strcmp(options.ignore_patterns[i], str) == 0 ||
116                     gen_fnmatch(options.ignore_patterns[i], str) == 0) {
117                         DEBUG(2,("Ignoring '%s'\n", str));
118                         return True;
119                 }
120         }
121         return False;
122 }
123
124 /***************************************************** 
125 connect to the servers
126 *******************************************************/
127 static BOOL connect_servers_fast(void)
128 {
129         int h, i;
130
131         /* close all open files */
132         for (h=0;h<options.max_open_handles;h++) {
133                 if (!open_handles[h].active) continue;
134                 for (i=0;i<NSERVERS;i++) {
135                         if (NT_STATUS_IS_ERR((smbcli_close(servers[i].cli[open_handles[h].instance]->tree,
136                                        open_handles[h].server_fnum[i])))) {
137                                 return False;
138                         }
139                         open_handles[h].active = False;
140                 }
141         }
142
143         return True;
144 }
145
146
147
148
149 /***************************************************** 
150 connect to the servers
151 *******************************************************/
152 static BOOL connect_servers(void)
153 {
154         int i, j;
155
156         if (options.fast_reconnect && servers[0].cli[0]) {
157                 if (connect_servers_fast()) {
158                         return True;
159                 }
160         }
161
162         /* close any existing connections */
163         for (i=0;i<NSERVERS;i++) {
164                 for (j=0;j<NINSTANCES;j++) {
165                         if (servers[i].cli[j]) {
166                                 smbcli_tdis(servers[i].cli[j]);
167                                 talloc_free(servers[i].cli[j]);
168                                 servers[i].cli[j] = NULL;
169                         }
170                 }
171         }
172
173         for (i=0;i<NSERVERS;i++) {
174                 for (j=0;j<NINSTANCES;j++) {
175                         NTSTATUS status;
176                         printf("Connecting to \\\\%s\\%s as %s - instance %d\n",
177                                servers[i].server_name, servers[i].share_name, 
178                                servers[i].credentials->username, j);
179
180                         cli_credentials_set_workstation(servers[i].credentials, 
181                                                         "gentest", CRED_SPECIFIED);
182
183                         status = smbcli_full_connection(NULL, &servers[i].cli[j],
184                                                         servers[i].server_name, 
185                                                         servers[i].share_name, NULL, 
186                                                         servers[i].credentials, NULL);
187                         if (!NT_STATUS_IS_OK(status)) {
188                                 printf("Failed to connect to \\\\%s\\%s - %s\n",
189                                        servers[i].server_name, servers[i].share_name,
190                                        nt_errstr(status));
191                                 return False;
192                         }
193
194                         smbcli_oplock_handler(servers[i].cli[j]->transport, oplock_handler, NULL);
195                         smbcli_transport_idle_handler(servers[i].cli[j]->transport, idle_func, 50000, NULL);
196                 }
197         }
198
199         return True;
200 }
201
202 /*
203   work out the time skew between the servers - be conservative
204 */
205 static uint_t time_skew(void)
206 {
207         uint_t ret;
208         ret = ABS(servers[0].cli[0]->transport->negotiate.server_time -
209                   servers[1].cli[0]->transport->negotiate.server_time);
210         return ret + 300;
211 }
212
213 /*
214   turn an fnum for an instance into a handle
215 */
216 static uint_t fnum_to_handle(int server, int instance, uint16_t fnum)
217 {
218         uint_t i;
219         for (i=0;i<options.max_open_handles;i++) {
220                 if (!open_handles[i].active ||
221                     instance != open_handles[i].instance) continue;
222                 if (open_handles[i].server_fnum[server] == fnum) {
223                         return i;
224                 }
225         }
226         printf("Invalid fnum %d in fnum_to_handle on server %d instance %d\n", 
227                fnum, server, instance);
228         return BAD_HANDLE;
229 }
230
231 /*
232   add some newly opened handles
233 */
234 static void gen_add_handle(int instance, const char *name, uint16_t fnums[NSERVERS])
235 {
236         int i, h;
237         for (h=0;h<options.max_open_handles;h++) {
238                 if (!open_handles[h].active) break;
239         }
240         if (h == options.max_open_handles) {
241                 /* we have to force close a random handle */
242                 h = random() % options.max_open_handles;
243                 for (i=0;i<NSERVERS;i++) {
244                         if (NT_STATUS_IS_ERR((smbcli_close(servers[i].cli[open_handles[h].instance]->tree, 
245                                        open_handles[h].server_fnum[i])))) {
246                                 printf("INTERNAL ERROR: Close failed when recovering handle! - %s\n",
247                                        smbcli_errstr(servers[i].cli[open_handles[h].instance]->tree));
248                         }
249                 }
250                 printf("Recovered handle %d\n", h);
251                 num_open_handles--;
252         }
253         for (i=0;i<NSERVERS;i++) {
254                 open_handles[h].server_fnum[i] = fnums[i];
255                 open_handles[h].instance = instance;
256                 open_handles[h].active = True;
257                 open_handles[h].name = name;
258         }
259         num_open_handles++;
260
261         printf("OPEN num_open_handles=%d h=%d s1=0x%x s2=0x%x (%s)\n", 
262                num_open_handles, h, 
263                open_handles[h].server_fnum[0], open_handles[h].server_fnum[1],
264                name);
265 }
266
267 /*
268   remove a closed handle
269 */
270 static void gen_remove_handle(int instance, uint16_t fnums[NSERVERS])
271 {
272         int h;
273         for (h=0;h<options.max_open_handles;h++) {
274                 if (instance == open_handles[h].instance &&
275                     open_handles[h].server_fnum[0] == fnums[0]) {
276                         open_handles[h].active = False;                 
277                         num_open_handles--;
278                         printf("CLOSE num_open_handles=%d h=%d s1=0x%x s2=0x%x (%s)\n", 
279                                num_open_handles, h, 
280                                open_handles[h].server_fnum[0], open_handles[h].server_fnum[1],
281                                open_handles[h].name);
282                         return;
283                 }
284         }
285         printf("Removing invalid handle!?\n");
286         exit(1);
287 }
288
289 /*
290   return True with 'chance' probability as a percentage
291 */
292 static BOOL gen_chance(uint_t chance)
293 {
294         return ((random() % 100) <= chance);
295 }
296
297 /*
298   map an internal handle number to a server fnum
299 */
300 static uint16_t gen_lookup_fnum(int server, uint16_t handle)
301 {
302         if (handle == BAD_HANDLE) return handle;
303         return open_handles[handle].server_fnum[server];
304 }
305
306 /*
307   return a file handle
308 */
309 static uint16_t gen_fnum(int instance)
310 {
311         uint16_t h;
312         int count = 0;
313
314         if (gen_chance(20)) return BAD_HANDLE;
315
316         while (num_open_handles > 0 && count++ < 10*options.max_open_handles) {
317                 h = random() % options.max_open_handles;
318                 if (open_handles[h].active && 
319                     open_handles[h].instance == instance) {
320                         return h;
321                 }
322         }
323         return BAD_HANDLE;
324 }
325
326 /*
327   return a file handle, but skewed so we don't close the last
328   couple of handles too readily
329 */
330 static uint16_t gen_fnum_close(int instance)
331 {
332         if (num_open_handles < 3) {
333                 if (gen_chance(80)) return BAD_HANDLE;
334         }
335
336         return gen_fnum(instance);
337 }
338
339 /*
340   generate an integer in a specified range
341 */
342 static int gen_int_range(uint_t min, uint_t max)
343 {
344         uint_t r = random();
345         return min + (r % (1+max-min));
346 }
347
348 /*
349   return a fnum for use as a root fid
350   be careful to call GEN_SET_FNUM() when you use this!
351 */
352 static uint16_t gen_root_fid(int instance)
353 {
354         if (gen_chance(5)) return gen_fnum(instance);
355         return 0;
356 }
357
358 /*
359   generate a file offset
360 */
361 static int gen_offset(void)
362 {
363         if (gen_chance(20)) return 0;
364         return gen_int_range(0, 1024*1024);
365 }
366
367 /*
368   generate a io count
369 */
370 static int gen_io_count(void)
371 {
372         if (gen_chance(20)) return 0;
373         return gen_int_range(0, 4096);
374 }
375
376 /*
377   generate a filename
378 */
379 static const char *gen_fname(void)
380 {
381         const char *names[] = {"\\gentest\\gentest.dat", 
382                                "\\gentest\\foo", 
383                                "\\gentest\\foo2.sym", 
384                                "\\gentest\\foo3.dll", 
385                                "\\gentest\\foo4", 
386                                "\\gentest\\foo4:teststream1", 
387                                "\\gentest\\foo4:teststream2", 
388                                "\\gentest\\foo5.exe", 
389                                "\\gentest\\foo5.exe:teststream3", 
390                                "\\gentest\\foo5.exe:teststream4", 
391                                "\\gentest\\foo6.com", 
392                                "\\gentest\\blah", 
393                                "\\gentest\\blah\\blergh.txt", 
394                                "\\gentest\\blah\\blergh2", 
395                                "\\gentest\\blah\\blergh3.txt", 
396                                "\\gentest\\blah\\blergh4", 
397                                "\\gentest\\blah\\blergh5.txt", 
398                                "\\gentest\\blah\\blergh5", 
399                                "\\gentest\\blah\\.", 
400 #if 0
401                                /* this causes problem with w2k3 */
402                                "\\gentest\\blah\\..", 
403 #endif
404                                "\\gentest\\a_very_long_name.bin", 
405                                "\\gentest\\x.y", 
406                                "\\gentest\\blah"};
407         int i;
408
409         do {
410                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
411         } while (ignore_pattern(names[i]));
412
413         return names[i];
414 }
415
416 /*
417   generate a filename with a higher chance of choosing an already 
418   open file
419 */
420 static const char *gen_fname_open(int instance)
421 {
422         uint16_t h;
423         h = gen_fnum(instance);
424         if (h == BAD_HANDLE) {
425                 return gen_fname();
426         }
427         return open_handles[h].name;
428 }
429
430 /*
431   generate a wildcard pattern
432 */
433 static const char *gen_pattern(void)
434 {
435         int i;
436         const char *names[] = {"\\gentest\\*.dat", 
437                                "\\gentest\\*", 
438                                "\\gentest\\*.*", 
439                                "\\gentest\\blah\\*.*", 
440                                "\\gentest\\blah\\*", 
441                                "\\gentest\\?"};
442
443         if (gen_chance(50)) return gen_fname();
444
445         do {
446                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
447         } while (ignore_pattern(names[i]));
448
449         return names[i];
450 }
451
452 /*
453   generate a bitmask
454 */
455 static uint32_t gen_bits_mask(uint_t mask)
456 {
457         uint_t ret = random();
458         return ret & mask;
459 }
460
461 /*
462   generate a bitmask with high probability of the first mask
463   and low of the second
464 */
465 static uint32_t gen_bits_mask2(uint32_t mask1, uint32_t mask2)
466 {
467         if (gen_chance(10)) return gen_bits_mask(mask2);
468         return gen_bits_mask(mask1);
469 }
470
471 /*
472   generate a boolean
473 */
474 static BOOL gen_bool(void)
475 {
476         return gen_bits_mask2(0x1, 0xFF);
477 }
478
479 /*
480   generate ntrename flags
481 */
482 static uint16_t gen_rename_flags(void)
483 {
484         if (gen_chance(30)) return RENAME_FLAG_RENAME;
485         if (gen_chance(30)) return RENAME_FLAG_HARD_LINK;
486         if (gen_chance(30)) return RENAME_FLAG_COPY;
487         return gen_bits_mask(0xFFFF);
488 }
489
490
491 /*
492   return a lockingx lock mode
493 */
494 static uint16_t gen_lock_mode(void)
495 {
496         if (gen_chance(5))  return gen_bits_mask(0xFFFF);
497         if (gen_chance(20)) return gen_bits_mask(0x1F);
498         return gen_bits_mask(LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES);
499 }
500
501 /*
502   generate a pid 
503 */
504 static uint16_t gen_pid(void)
505 {
506         if (gen_chance(10)) return gen_bits_mask(0xFFFF);
507         return getpid();
508 }
509
510 /*
511   generate a lock count
512 */
513 static off_t gen_lock_count(void)
514 {
515         return gen_int_range(0, 3);
516 }
517
518 /*
519   generate a ntcreatex flags field
520 */
521 static uint32_t gen_ntcreatex_flags(void)
522 {
523         if (gen_chance(70)) return NTCREATEX_FLAGS_EXTENDED;
524         return gen_bits_mask2(0x1F, 0xFFFFFFFF);
525 }
526
527 /*
528   generate a NT access mask
529 */
530 static uint32_t gen_access_mask(void)
531 {
532         if (gen_chance(50)) return SEC_FLAG_MAXIMUM_ALLOWED;
533         if (gen_chance(20)) return SEC_FILE_ALL;
534         return gen_bits_mask(0xFFFFFFFF);
535 }
536
537 /*
538   generate a ntcreatex create options bitfield
539 */
540 static uint32_t gen_create_options(void)
541 {
542         if (gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
543         if (gen_chance(50)) return 0;
544         return gen_bits_mask(NTCREATEX_OPTIONS_DELETE_ON_CLOSE | NTCREATEX_OPTIONS_DIRECTORY);
545 }
546
547 /*
548   generate a ntcreatex open disposition
549 */
550 static uint32_t gen_open_disp(void)
551 {
552         if (gen_chance(10)) return gen_bits_mask(0xFFFFFFFF);
553         return gen_int_range(0, 5);
554 }
555
556 /*
557   generate an openx open mode
558 */
559 static uint16_t gen_openx_mode(void)
560 {
561         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
562         if (gen_chance(20)) return gen_bits_mask(0xFF);
563         return OPENX_MODE_DENY_NONE | gen_bits_mask(0x3);
564 }
565
566 /*
567   generate an openx flags field
568 */
569 static uint16_t gen_openx_flags(void)
570 {
571         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
572         return gen_bits_mask(0x7);
573 }
574
575 /*
576   generate an openx open function
577 */
578 static uint16_t gen_openx_func(void)
579 {
580         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
581         return gen_bits_mask(0x13);
582 }
583
584 /*
585   generate a file attrib combination
586 */
587 static uint32_t gen_attrib(void)
588 {
589         if (gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
590         return gen_bits_mask(FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
591 }
592
593 /*
594   generate a unix timestamp
595 */
596 static time_t gen_timet(void)
597 {
598         if (gen_chance(30)) return 0;
599         return (time_t)random();
600 }
601
602 /*
603   generate a unix timestamp
604 */
605 static NTTIME gen_nttime(void)
606 {
607         NTTIME ret;
608         unix_to_nt_time(&ret, gen_timet());
609         return ret;
610 }
611
612 /*
613   generate a milliseconds protocol timeout
614 */
615 static uint32_t gen_timeout(void)
616 {
617         if (gen_chance(98)) return 0;
618         return random() % 50;
619 }
620
621 /*
622   generate a file allocation size
623 */
624 static uint_t gen_alloc_size(void)
625 {
626         uint_t ret;
627
628         if (gen_chance(30)) return 0;
629
630         ret = random() % 4*1024*1024;
631         /* give a high chance of a round number */
632         if (gen_chance(60)) {
633                 ret &= ~(1024*1024 - 1);
634         }
635         return ret;
636 }
637
638 /*
639   generate an ea_struct
640 */
641 static struct ea_struct gen_ea_struct(void)
642 {
643         struct ea_struct ea;
644         const char *names[] = {"EAONE", 
645                                "", 
646                                "FOO!", 
647                                " WITH SPACES ", 
648                                ".", 
649                                "AVERYLONGATTRIBUTENAME"};
650         const char *values[] = {"VALUE1", 
651                                "", 
652                                "NOT MUCH FOO", 
653                                " LEADING SPACES ", 
654                                ":", 
655                                "ASOMEWHATLONGERATTRIBUTEVALUE"};
656         int i;
657
658         do {
659                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
660         } while (ignore_pattern(names[i]));
661
662         ea.name.s = names[i];
663
664         do {
665                 i = gen_int_range(0, ARRAY_SIZE(values)-1);
666         } while (ignore_pattern(values[i]));
667
668         ea.value = data_blob(values[i], strlen(values[i]));
669
670         if (gen_chance(10)) ea.flags = gen_bits_mask(0xFF);
671         ea.flags = 0;
672
673         return ea;
674 }
675
676
677 /*
678   this is called when a change notify reply comes in
679 */
680 static void async_notify(struct smbcli_request *req)
681 {
682         struct smb_notify notify;
683         NTSTATUS status;
684         int i, j;
685         uint16_t tid;
686         struct smbcli_transport *transport = req->transport;
687
688         tid = SVAL(req->in.hdr, HDR_TID);
689
690         status = smb_raw_changenotify_recv(req, current_op.mem_ctx, &notify);
691         if (NT_STATUS_IS_OK(status)) {
692                 printf("notify tid=%d num_changes=%d action=%d name=%s\n", 
693                        tid, 
694                        notify.out.num_changes,
695                        notify.out.changes[0].action,
696                        notify.out.changes[0].name.s);
697         }
698
699         for (i=0;i<NSERVERS;i++) {
700                 for (j=0;j<NINSTANCES;j++) {
701                         if (transport == servers[i].cli[j]->transport &&
702                             tid == servers[i].cli[j]->tree->tid) {
703                                 notifies[i][j].notify_count++;
704                                 notifies[i][j].status = status;
705                                 notifies[i][j].notify = notify;
706                         }
707                 }
708         }
709 }
710
711 static void oplock_handler_close_recv(struct smbcli_request *req)
712 {
713         NTSTATUS status;
714         status = smbcli_request_simple_recv(req);
715         if (!NT_STATUS_IS_OK(status)) {
716                 printf("close failed in oplock_handler\n");
717                 smb_panic("close failed in oplock_handler");
718         }
719 }
720
721 /*
722   the oplock handler will either ack the break or close the file
723 */
724 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private)
725 {
726         union smb_close io;
727         NTSTATUS status;
728         int i, j;
729         BOOL do_close;
730         struct smbcli_tree *tree = NULL;
731         struct smbcli_request *req;
732
733         srandom(current_op.seed);
734         do_close = gen_chance(50);
735
736         for (i=0;i<NSERVERS;i++) {
737                 for (j=0;j<NINSTANCES;j++) {
738                         if (transport == servers[i].cli[j]->transport &&
739                             tid == servers[i].cli[j]->tree->tid) {
740                                 oplocks[i][j].got_break = True;
741                                 oplocks[i][j].fnum = fnum;
742                                 oplocks[i][j].handle = fnum_to_handle(i, j, fnum);
743                                 oplocks[i][j].level = level;
744                                 oplocks[i][j].do_close = do_close;
745                                 tree = servers[i].cli[j]->tree;
746                         }
747                 }
748         }
749
750         if (!tree) {
751                 printf("Oplock break not for one of our trees!?\n");
752                 return False;
753         }
754
755         if (!do_close) {
756                 printf("oplock ack fnum=%d\n", fnum);
757                 return smbcli_oplock_ack(tree, fnum, level);
758         }
759
760         printf("oplock close fnum=%d\n", fnum);
761
762         io.close.level = RAW_CLOSE_CLOSE;
763         io.close.in.fnum = fnum;
764         io.close.in.write_time = 0;
765         req = smb_raw_close_send(tree, &io);
766
767         if (req == NULL) {
768                 printf("WARNING: close failed in oplock_handler_close - %s\n", 
769                        nt_errstr(status));
770                 return False;
771         }
772
773         req->async.fn = oplock_handler_close_recv;
774         req->async.private = NULL;
775
776         return True;
777 }
778
779
780 /*
781   the idle function tries to cope with getting an oplock break on a connection, and
782   an operation on another connection blocking until that break is acked
783   we check for operations on all transports in the idle function
784 */
785 static void idle_func(struct smbcli_transport *transport, void *private)
786 {
787         int i, j;
788         for (i=0;i<NSERVERS;i++) {
789                 for (j=0;j<NINSTANCES;j++) {
790                         if (servers[i].cli[j] &&
791                             transport != servers[i].cli[j]->transport) {
792                                 smbcli_transport_process(servers[i].cli[j]->transport);
793                         }
794                 }
795         }
796
797 }
798
799
800 /*
801   compare NTSTATUS, using checking ignored patterns
802 */
803 static BOOL compare_status(NTSTATUS status1, NTSTATUS status2)
804 {
805         if (NT_STATUS_EQUAL(status1, status2)) return True;
806
807         /* one code being an error and the other OK is always an error */
808         if (NT_STATUS_IS_OK(status1) || NT_STATUS_IS_OK(status2)) return False;
809
810         /* if we are ignoring one of the status codes then consider this a match */
811         if (ignore_pattern(nt_errstr(status1)) ||
812             ignore_pattern(nt_errstr(status2))) {
813                 return True;
814         }
815         return False;
816 }
817
818
819 /*
820   check for pending packets on all connections
821 */
822 static void check_pending(void)
823 {
824         int i, j;
825
826         msleep(20);
827
828         for (j=0;j<NINSTANCES;j++) {
829                 for (i=0;i<NSERVERS;i++) {
830                         smbcli_transport_process(servers[i].cli[j]->transport);
831                 }
832         }       
833 }
834
835 /*
836   check that the same oplock breaks have been received by all instances
837 */
838 static BOOL check_oplocks(const char *call)
839 {
840         int i, j;
841         int tries = 0;
842
843 again:
844         check_pending();
845
846         for (j=0;j<NINSTANCES;j++) {
847                 for (i=1;i<NSERVERS;i++) {
848                         if (oplocks[0][j].got_break != oplocks[i][j].got_break ||
849                             oplocks[0][j].handle != oplocks[i][j].handle ||
850                             oplocks[0][j].level != oplocks[i][j].level) {
851                                 if (tries++ < 10) goto again;
852                                 printf("oplock break inconsistent - %d/%d/%d vs %d/%d/%d\n",
853                                        oplocks[0][j].got_break, 
854                                        oplocks[0][j].handle, 
855                                        oplocks[0][j].level, 
856                                        oplocks[i][j].got_break, 
857                                        oplocks[i][j].handle, 
858                                        oplocks[i][j].level);
859                                 return False;
860                         }
861                 }
862         }
863
864         /* if we got a break and closed then remove the handle */
865         for (j=0;j<NINSTANCES;j++) {
866                 if (oplocks[0][j].got_break &&
867                     oplocks[0][j].do_close) {
868                         uint16_t fnums[NSERVERS];
869                         for (i=0;i<NSERVERS;i++) {
870                                 fnums[i] = oplocks[i][j].fnum;
871                         }
872                         gen_remove_handle(j, fnums);
873                         break;
874                 }
875         }       
876         return True;
877 }
878
879
880 /*
881   check that the same change notify info has been received by all instances
882 */
883 static BOOL check_notifies(const char *call)
884 {
885         int i, j;
886         int tries = 0;
887
888 again:
889         check_pending();
890
891         for (j=0;j<NINSTANCES;j++) {
892                 for (i=1;i<NSERVERS;i++) {
893                         int n;
894                         struct smb_notify not1, not2;
895
896                         if (notifies[0][j].notify_count != notifies[i][j].notify_count) {
897                                 if (tries++ < 10) goto again;
898                                 printf("Notify count inconsistent %d %d\n",
899                                        notifies[0][j].notify_count,
900                                        notifies[i][j].notify_count);
901                                 return False;
902                         }
903
904                         if (notifies[0][j].notify_count == 0) continue;
905
906                         if (!NT_STATUS_EQUAL(notifies[0][j].status,
907                                              notifies[i][j].status)) {
908                                 printf("Notify status mismatch - %s - %s\n",
909                                        nt_errstr(notifies[0][j].status),
910                                        nt_errstr(notifies[i][j].status));
911                                 return False;
912                         }
913
914                         if (!NT_STATUS_IS_OK(notifies[0][j].status)) {
915                                 continue;
916                         }
917
918                         not1 = notifies[0][j].notify;
919                         not2 = notifies[i][j].notify;
920
921                         for (n=0;n<not1.out.num_changes;n++) {
922                                 if (not1.out.changes[n].action != 
923                                     not2.out.changes[n].action) {
924                                         printf("Notify action %d inconsistent %d %d\n", n,
925                                                not1.out.changes[n].action,
926                                                not2.out.changes[n].action);
927                                         return False;
928                                 }
929                                 if (strcmp(not1.out.changes[n].name.s,
930                                            not2.out.changes[n].name.s)) {
931                                         printf("Notify name %d inconsistent %s %s\n", n,
932                                                not1.out.changes[n].name.s,
933                                                not2.out.changes[n].name.s);
934                                         return False;
935                                 }
936                                 if (not1.out.changes[n].name.private_length !=
937                                     not2.out.changes[n].name.private_length) {
938                                         printf("Notify name length %d inconsistent %d %d\n", n,
939                                                not1.out.changes[n].name.private_length,
940                                                not2.out.changes[n].name.private_length);
941                                         return False;
942                                 }
943                         }
944                 }
945         }
946
947         ZERO_STRUCT(notifies);
948
949         return True;
950 }
951
952
953 #define GEN_COPY_PARM do { \
954         int i; \
955         for (i=1;i<NSERVERS;i++) { \
956                 parm[i] = parm[0]; \
957         } \
958 } while (0)
959
960 #define GEN_CALL(call) do { \
961         int i; \
962         ZERO_STRUCT(oplocks); \
963         ZERO_STRUCT(notifies); \
964         for (i=0;i<NSERVERS;i++) { \
965                 struct smbcli_tree *tree = servers[i].cli[instance]->tree; \
966                 status[i] = call; \
967         } \
968         current_op.status = status[0]; \
969         for (i=1;i<NSERVERS;i++) { \
970                 if (!compare_status(status[i], status[0])) { \
971                         printf("status different in %s - %s %s\n", #call, \
972                                nt_errstr(status[0]), nt_errstr(status[i])); \
973                         return False; \
974                 } \
975         } \
976         if (!check_oplocks(#call)) return False; \
977         if (!check_notifies(#call)) return False; \
978         if (!NT_STATUS_IS_OK(status[0])) { \
979                 return True; \
980         } \
981 } while(0)
982
983 #define ADD_HANDLE(name, field) do { \
984         uint16_t fnums[NSERVERS]; \
985         int i; \
986         for (i=0;i<NSERVERS;i++) { \
987                 fnums[i] = parm[i].field; \
988         } \
989         gen_add_handle(instance, name, fnums); \
990 } while(0)
991
992 #define REMOVE_HANDLE(field) do { \
993         uint16_t fnums[NSERVERS]; \
994         int i; \
995         for (i=0;i<NSERVERS;i++) { \
996                 fnums[i] = parm[i].field; \
997         } \
998         gen_remove_handle(instance, fnums); \
999 } while(0)
1000
1001 #define GEN_SET_FNUM(field) do { \
1002         int i; \
1003         for (i=0;i<NSERVERS;i++) { \
1004                 parm[i].field = gen_lookup_fnum(i, parm[i].field); \
1005         } \
1006 } while(0)
1007
1008 #define CHECK_EQUAL(field) do { \
1009         if (parm[0].field != parm[1].field && !ignore_pattern(#field)) { \
1010                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
1011                        (int)parm[0].field, (int)parm[1].field); \
1012                 return False; \
1013         } \
1014 } while(0)
1015
1016 #define CHECK_WSTR_EQUAL(field) do { \
1017         if ((!parm[0].field.s && parm[1].field.s) || (parm[0].field.s && !parm[1].field.s)) { \
1018                 printf("%s is NULL!\n", #field); \
1019                 return False; \
1020         } \
1021         if (parm[0].field.s && strcmp(parm[0].field.s, parm[1].field.s) != 0 && !ignore_pattern(#field)) { \
1022                 printf("Mismatch in %s - %s %s\n", #field, \
1023                        parm[0].field.s, parm[1].field.s); \
1024                 return False; \
1025         } \
1026         CHECK_EQUAL(field.private_length); \
1027 } while(0)
1028
1029 #define CHECK_BLOB_EQUAL(field) do { \
1030         if (memcmp(parm[0].field.data, parm[1].field.data, parm[0].field.length) != 0 && !ignore_pattern(#field)) { \
1031                 printf("Mismatch in %s\n", #field); \
1032                 return False; \
1033         } \
1034         CHECK_EQUAL(field.length); \
1035 } while(0)
1036
1037 #define CHECK_TIMES_EQUAL(field) do { \
1038         if (ABS(parm[0].field - parm[1].field) > time_skew() && \
1039             !ignore_pattern(#field)) { \
1040                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
1041                        (int)parm[0].field, (int)parm[1].field); \
1042                 return False; \
1043         } \
1044 } while(0)
1045
1046 #define CHECK_NTTIMES_EQUAL(field) do { \
1047         if (ABS(nt_time_to_unix(parm[0].field) - \
1048                 nt_time_to_unix(parm[1].field)) > time_skew() && \
1049             !ignore_pattern(#field)) { \
1050                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
1051                        (int)nt_time_to_unix(parm[0].field), \
1052                        (int)nt_time_to_unix(parm[1].field)); \
1053                 return False; \
1054         } \
1055 } while(0)
1056
1057 /*
1058   generate openx operations
1059 */
1060 static BOOL handler_openx(int instance)
1061 {
1062         union smb_open parm[NSERVERS];
1063         NTSTATUS status[NSERVERS];
1064
1065         parm[0].openx.level = RAW_OPEN_OPENX;
1066         parm[0].openx.in.flags = gen_openx_flags();
1067         parm[0].openx.in.open_mode = gen_openx_mode();
1068         parm[0].openx.in.search_attrs = gen_attrib();
1069         parm[0].openx.in.file_attrs = gen_attrib();
1070         parm[0].openx.in.write_time = gen_timet();
1071         parm[0].openx.in.open_func = gen_openx_func();
1072         parm[0].openx.in.size = gen_io_count();
1073         parm[0].openx.in.timeout = gen_timeout();
1074         parm[0].openx.in.fname = gen_fname_open(instance);
1075
1076         if (!options.use_oplocks) {
1077                 /* mask out oplocks */
1078                 parm[0].openx.in.flags &= ~(OPENX_FLAGS_REQUEST_OPLOCK|
1079                                             OPENX_FLAGS_REQUEST_BATCH_OPLOCK);
1080         }
1081         
1082         GEN_COPY_PARM;
1083         GEN_CALL(smb_raw_open(tree, current_op.mem_ctx, &parm[i]));
1084
1085         CHECK_EQUAL(openx.out.attrib);
1086         CHECK_EQUAL(openx.out.size);
1087         CHECK_EQUAL(openx.out.access);
1088         CHECK_EQUAL(openx.out.ftype);
1089         CHECK_EQUAL(openx.out.devstate);
1090         CHECK_EQUAL(openx.out.action);
1091         CHECK_EQUAL(openx.out.access_mask);
1092         CHECK_EQUAL(openx.out.unknown);
1093         CHECK_TIMES_EQUAL(openx.out.write_time);
1094
1095         /* open creates a new file handle */
1096         ADD_HANDLE(parm[0].openx.in.fname, openx.out.fnum);
1097
1098         return True;
1099 }
1100
1101
1102 /*
1103   generate open operations
1104 */
1105 static BOOL handler_open(int instance)
1106 {
1107         union smb_open parm[NSERVERS];
1108         NTSTATUS status[NSERVERS];
1109
1110         parm[0].openold.level = RAW_OPEN_OPEN;
1111         parm[0].openold.in.open_mode = gen_bits_mask2(0xF, 0xFFFF);
1112         parm[0].openold.in.search_attrs = gen_attrib();
1113         parm[0].openold.in.fname = gen_fname_open(instance);
1114
1115         if (!options.use_oplocks) {
1116                 /* mask out oplocks */
1117                 parm[0].openold.in.open_mode &= ~(OPENX_FLAGS_REQUEST_OPLOCK|
1118                                                   OPENX_FLAGS_REQUEST_BATCH_OPLOCK);
1119         }
1120         
1121         GEN_COPY_PARM;
1122         GEN_CALL(smb_raw_open(tree, current_op.mem_ctx, &parm[i]));
1123
1124         CHECK_EQUAL(openold.out.attrib);
1125         CHECK_TIMES_EQUAL(openold.out.write_time);
1126         CHECK_EQUAL(openold.out.size);
1127         CHECK_EQUAL(openold.out.rmode);
1128
1129         /* open creates a new file handle */
1130         ADD_HANDLE(parm[0].openold.in.fname, openold.out.fnum);
1131
1132         return True;
1133 }
1134
1135
1136 /*
1137   generate ntcreatex operations
1138 */
1139 static BOOL handler_ntcreatex(int instance)
1140 {
1141         union smb_open parm[NSERVERS];
1142         NTSTATUS status[NSERVERS];
1143
1144         parm[0].ntcreatex.level = RAW_OPEN_NTCREATEX;
1145         parm[0].ntcreatex.in.flags = gen_ntcreatex_flags();
1146         parm[0].ntcreatex.in.root_fid = gen_root_fid(instance);
1147         parm[0].ntcreatex.in.access_mask = gen_access_mask();
1148         parm[0].ntcreatex.in.alloc_size = gen_alloc_size();
1149         parm[0].ntcreatex.in.file_attr = gen_attrib();
1150         parm[0].ntcreatex.in.share_access = gen_bits_mask2(0x7, 0xFFFFFFFF);
1151         parm[0].ntcreatex.in.open_disposition = gen_open_disp();
1152         parm[0].ntcreatex.in.create_options = gen_create_options();
1153         parm[0].ntcreatex.in.impersonation = gen_bits_mask2(0, 0xFFFFFFFF);
1154         parm[0].ntcreatex.in.security_flags = gen_bits_mask2(0, 0xFF);
1155         parm[0].ntcreatex.in.fname = gen_fname_open(instance);
1156
1157         if (!options.use_oplocks) {
1158                 /* mask out oplocks */
1159                 parm[0].ntcreatex.in.flags &= ~(NTCREATEX_FLAGS_REQUEST_OPLOCK|
1160                                                 NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK);
1161         }
1162         
1163         GEN_COPY_PARM;
1164         if (parm[0].ntcreatex.in.root_fid != 0) {
1165                 GEN_SET_FNUM(ntcreatex.in.root_fid);
1166         }
1167         GEN_CALL(smb_raw_open(tree, current_op.mem_ctx, &parm[i]));
1168
1169         CHECK_EQUAL(ntcreatex.out.oplock_level);
1170         CHECK_EQUAL(ntcreatex.out.create_action);
1171         CHECK_NTTIMES_EQUAL(ntcreatex.out.create_time);
1172         CHECK_NTTIMES_EQUAL(ntcreatex.out.access_time);
1173         CHECK_NTTIMES_EQUAL(ntcreatex.out.write_time);
1174         CHECK_NTTIMES_EQUAL(ntcreatex.out.change_time);
1175         CHECK_EQUAL(ntcreatex.out.attrib);
1176         CHECK_EQUAL(ntcreatex.out.alloc_size);
1177         CHECK_EQUAL(ntcreatex.out.size);
1178         CHECK_EQUAL(ntcreatex.out.file_type);
1179         CHECK_EQUAL(ntcreatex.out.ipc_state);
1180         CHECK_EQUAL(ntcreatex.out.is_directory);
1181
1182         /* ntcreatex creates a new file handle */
1183         ADD_HANDLE(parm[0].ntcreatex.in.fname, ntcreatex.out.fnum);
1184
1185         return True;
1186 }
1187
1188 /*
1189   generate close operations
1190 */
1191 static BOOL handler_close(int instance)
1192 {
1193         union smb_close parm[NSERVERS];
1194         NTSTATUS status[NSERVERS];
1195
1196         parm[0].close.level = RAW_CLOSE_CLOSE;
1197         parm[0].close.in.fnum = gen_fnum_close(instance);
1198         parm[0].close.in.write_time = gen_timet();
1199
1200         GEN_COPY_PARM;
1201         GEN_SET_FNUM(close.in.fnum);
1202         GEN_CALL(smb_raw_close(tree, &parm[i]));
1203
1204         REMOVE_HANDLE(close.in.fnum);
1205
1206         return True;
1207 }
1208
1209 /*
1210   generate unlink operations
1211 */
1212 static BOOL handler_unlink(int instance)
1213 {
1214         struct smb_unlink parm[NSERVERS];
1215         NTSTATUS status[NSERVERS];
1216
1217         parm[0].in.pattern = gen_pattern();
1218         parm[0].in.attrib = gen_attrib();
1219
1220         GEN_COPY_PARM;
1221         GEN_CALL(smb_raw_unlink(tree, &parm[i]));
1222
1223         return True;
1224 }
1225
1226 /*
1227   generate chkpath operations
1228 */
1229 static BOOL handler_chkpath(int instance)
1230 {
1231         struct smb_chkpath parm[NSERVERS];
1232         NTSTATUS status[NSERVERS];
1233
1234         parm[0].in.path = gen_fname_open(instance);
1235
1236         GEN_COPY_PARM;
1237         GEN_CALL(smb_raw_chkpath(tree, &parm[i]));
1238
1239         return True;
1240 }
1241
1242 /*
1243   generate mkdir operations
1244 */
1245 static BOOL handler_mkdir(int instance)
1246 {
1247         union smb_mkdir parm[NSERVERS];
1248         NTSTATUS status[NSERVERS];
1249
1250         parm[0].mkdir.level = RAW_MKDIR_MKDIR;
1251         parm[0].mkdir.in.path = gen_fname_open(instance);
1252
1253         GEN_COPY_PARM;
1254         GEN_CALL(smb_raw_mkdir(tree, &parm[i]));
1255
1256         return True;
1257 }
1258
1259 /*
1260   generate rmdir operations
1261 */
1262 static BOOL handler_rmdir(int instance)
1263 {
1264         struct smb_rmdir parm[NSERVERS];
1265         NTSTATUS status[NSERVERS];
1266
1267         parm[0].in.path = gen_fname_open(instance);
1268
1269         GEN_COPY_PARM;
1270         GEN_CALL(smb_raw_rmdir(tree, &parm[i]));
1271
1272         return True;
1273 }
1274
1275 /*
1276   generate rename operations
1277 */
1278 static BOOL handler_rename(int instance)
1279 {
1280         union smb_rename parm[NSERVERS];
1281         NTSTATUS status[NSERVERS];
1282
1283         parm[0].generic.level = RAW_RENAME_RENAME;
1284         parm[0].rename.in.pattern1 = gen_pattern();
1285         parm[0].rename.in.pattern2 = gen_pattern();
1286         parm[0].rename.in.attrib = gen_attrib();
1287
1288         GEN_COPY_PARM;
1289         GEN_CALL(smb_raw_rename(tree, &parm[i]));
1290
1291         return True;
1292 }
1293
1294 /*
1295   generate ntrename operations
1296 */
1297 static BOOL handler_ntrename(int instance)
1298 {
1299         union smb_rename parm[NSERVERS];
1300         NTSTATUS status[NSERVERS];
1301
1302         parm[0].generic.level = RAW_RENAME_NTRENAME;
1303         parm[0].ntrename.in.old_name = gen_fname();
1304         parm[0].ntrename.in.new_name = gen_fname();
1305         parm[0].ntrename.in.attrib = gen_attrib();
1306         parm[0].ntrename.in.cluster_size = gen_bits_mask2(0, 0xFFFFFFF);
1307         parm[0].ntrename.in.flags = gen_rename_flags();
1308
1309         GEN_COPY_PARM;
1310         GEN_CALL(smb_raw_rename(tree, &parm[i]));
1311
1312         return True;
1313 }
1314
1315
1316 /*
1317   generate seek operations
1318 */
1319 static BOOL handler_seek(int instance)
1320 {
1321         struct smb_seek parm[NSERVERS];
1322         NTSTATUS status[NSERVERS];
1323
1324         parm[0].in.fnum = gen_fnum(instance);
1325         parm[0].in.mode = gen_bits_mask2(0x3, 0xFFFF);
1326         parm[0].in.offset = gen_offset();
1327
1328         GEN_COPY_PARM;
1329         GEN_SET_FNUM(in.fnum);
1330         GEN_CALL(smb_raw_seek(tree, &parm[i]));
1331
1332         CHECK_EQUAL(out.offset);
1333
1334         return True;
1335 }
1336
1337
1338 /*
1339   generate readx operations
1340 */
1341 static BOOL handler_readx(int instance)
1342 {
1343         union smb_read parm[NSERVERS];
1344         NTSTATUS status[NSERVERS];
1345
1346         parm[0].readx.level = RAW_READ_READX;
1347         parm[0].readx.in.fnum = gen_fnum(instance);
1348         parm[0].readx.in.offset = gen_offset();
1349         parm[0].readx.in.mincnt = gen_io_count();
1350         parm[0].readx.in.maxcnt = gen_io_count();
1351         parm[0].readx.in.remaining = gen_io_count();
1352         parm[0].readx.out.data = talloc_size(current_op.mem_ctx,
1353                                              MAX(parm[0].readx.in.mincnt, parm[0].readx.in.maxcnt));
1354
1355         GEN_COPY_PARM;
1356         GEN_SET_FNUM(readx.in.fnum);
1357         GEN_CALL(smb_raw_read(tree, &parm[i]));
1358
1359         CHECK_EQUAL(readx.out.remaining);
1360         CHECK_EQUAL(readx.out.compaction_mode);
1361         CHECK_EQUAL(readx.out.nread);
1362
1363         return True;
1364 }
1365
1366 /*
1367   generate writex operations
1368 */
1369 static BOOL handler_writex(int instance)
1370 {
1371         union smb_write parm[NSERVERS];
1372         NTSTATUS status[NSERVERS];
1373
1374         parm[0].writex.level = RAW_WRITE_WRITEX;
1375         parm[0].writex.in.fnum = gen_fnum(instance);
1376         parm[0].writex.in.offset = gen_offset();
1377         parm[0].writex.in.wmode = gen_bits_mask(0xFFFF);
1378         parm[0].writex.in.remaining = gen_io_count();
1379         parm[0].writex.in.count = gen_io_count();
1380         parm[0].writex.in.data = talloc_zero_size(current_op.mem_ctx, parm[0].writex.in.count);
1381
1382         GEN_COPY_PARM;
1383         GEN_SET_FNUM(writex.in.fnum);
1384         GEN_CALL(smb_raw_write(tree, &parm[i]));
1385
1386         CHECK_EQUAL(writex.out.nwritten);
1387         CHECK_EQUAL(writex.out.remaining);
1388
1389         return True;
1390 }
1391
1392 /*
1393   generate lockingx operations
1394 */
1395 static BOOL handler_lockingx(int instance)
1396 {
1397         union smb_lock parm[NSERVERS];
1398         NTSTATUS status[NSERVERS];
1399         int n, nlocks;
1400
1401         parm[0].lockx.level = RAW_LOCK_LOCKX;
1402         parm[0].lockx.in.fnum = gen_fnum(instance);
1403         parm[0].lockx.in.mode = gen_lock_mode();
1404         parm[0].lockx.in.timeout = gen_timeout();
1405         do {
1406                 /* make sure we don't accidentially generate an oplock
1407                    break ack - otherwise the server can just block forever */
1408                 parm[0].lockx.in.ulock_cnt = gen_lock_count();
1409                 parm[0].lockx.in.lock_cnt = gen_lock_count();
1410                 nlocks = parm[0].lockx.in.ulock_cnt + parm[0].lockx.in.lock_cnt;
1411         } while (nlocks == 0);
1412
1413         if (nlocks > 0) {
1414                 parm[0].lockx.in.locks = talloc_array(current_op.mem_ctx,
1415                                                         struct smb_lock_entry,
1416                                                         nlocks);
1417                 for (n=0;n<nlocks;n++) {
1418                         parm[0].lockx.in.locks[n].pid = gen_pid();
1419                         parm[0].lockx.in.locks[n].offset = gen_offset();
1420                         parm[0].lockx.in.locks[n].count = gen_io_count();
1421                 }
1422         }
1423
1424         GEN_COPY_PARM;
1425         GEN_SET_FNUM(lockx.in.fnum);
1426         GEN_CALL(smb_raw_lock(tree, &parm[i]));
1427
1428         return True;
1429 }
1430
1431 /*
1432   generate a fileinfo query structure
1433 */
1434 static void gen_fileinfo(int instance, union smb_fileinfo *info)
1435 {
1436         int i;
1437         #define LVL(v) {RAW_FILEINFO_ ## v, "RAW_FILEINFO_" #v}
1438         struct {
1439                 enum smb_fileinfo_level level;
1440                 const char *name;
1441         }  levels[] = {
1442                 LVL(GETATTR), LVL(GETATTRE), LVL(STANDARD),
1443                 LVL(EA_SIZE), LVL(ALL_EAS), LVL(IS_NAME_VALID),
1444                 LVL(BASIC_INFO), LVL(STANDARD_INFO), LVL(EA_INFO),
1445                 LVL(NAME_INFO), LVL(ALL_INFO), LVL(ALT_NAME_INFO),
1446                 LVL(STREAM_INFO), LVL(COMPRESSION_INFO), LVL(BASIC_INFORMATION),
1447                 LVL(STANDARD_INFORMATION), LVL(INTERNAL_INFORMATION), LVL(EA_INFORMATION),
1448                 LVL(ACCESS_INFORMATION), LVL(NAME_INFORMATION), LVL(POSITION_INFORMATION),
1449                 LVL(MODE_INFORMATION), LVL(ALIGNMENT_INFORMATION), LVL(ALL_INFORMATION),
1450                 LVL(ALT_NAME_INFORMATION), LVL(STREAM_INFORMATION), LVL(COMPRESSION_INFORMATION),
1451                 LVL(NETWORK_OPEN_INFORMATION), LVL(ATTRIBUTE_TAG_INFORMATION)
1452         };
1453         do {
1454                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1455         } while (ignore_pattern(levels[i].name));
1456
1457         info->generic.level = levels[i].level;
1458 }
1459
1460 /*
1461   compare returned fileinfo structures
1462 */
1463 static BOOL cmp_fileinfo(int instance, 
1464                          union smb_fileinfo parm[NSERVERS],
1465                          NTSTATUS status[NSERVERS])
1466 {
1467         int i;
1468
1469         switch (parm[0].generic.level) {
1470         case RAW_FILEINFO_GENERIC:
1471                 return False;
1472
1473         case RAW_FILEINFO_GETATTR:
1474                 CHECK_EQUAL(getattr.out.attrib);
1475                 CHECK_EQUAL(getattr.out.size);
1476                 CHECK_TIMES_EQUAL(getattr.out.write_time);
1477                 break;
1478
1479         case RAW_FILEINFO_GETATTRE:
1480                 CHECK_TIMES_EQUAL(getattre.out.create_time);
1481                 CHECK_TIMES_EQUAL(getattre.out.access_time);
1482                 CHECK_TIMES_EQUAL(getattre.out.write_time);
1483                 CHECK_EQUAL(getattre.out.size);
1484                 CHECK_EQUAL(getattre.out.alloc_size);
1485                 CHECK_EQUAL(getattre.out.attrib);
1486                 break;
1487
1488         case RAW_FILEINFO_STANDARD:
1489                 CHECK_TIMES_EQUAL(standard.out.create_time);
1490                 CHECK_TIMES_EQUAL(standard.out.access_time);
1491                 CHECK_TIMES_EQUAL(standard.out.write_time);
1492                 CHECK_EQUAL(standard.out.size);
1493                 CHECK_EQUAL(standard.out.alloc_size);
1494                 CHECK_EQUAL(standard.out.attrib);
1495                 break;
1496
1497         case RAW_FILEINFO_EA_SIZE:
1498                 CHECK_TIMES_EQUAL(ea_size.out.create_time);
1499                 CHECK_TIMES_EQUAL(ea_size.out.access_time);
1500                 CHECK_TIMES_EQUAL(ea_size.out.write_time);
1501                 CHECK_EQUAL(ea_size.out.size);
1502                 CHECK_EQUAL(ea_size.out.alloc_size);
1503                 CHECK_EQUAL(ea_size.out.attrib);
1504                 CHECK_EQUAL(ea_size.out.ea_size);
1505                 break;
1506
1507         case RAW_FILEINFO_ALL_EAS:
1508                 CHECK_EQUAL(all_eas.out.num_eas);
1509                 for (i=0;i<parm[0].all_eas.out.num_eas;i++) {
1510                         CHECK_EQUAL(all_eas.out.eas[i].flags);
1511                         CHECK_WSTR_EQUAL(all_eas.out.eas[i].name);
1512                         CHECK_BLOB_EQUAL(all_eas.out.eas[i].value);
1513                 }
1514                 break;
1515
1516         case RAW_FILEINFO_IS_NAME_VALID:
1517                 break;
1518                 
1519         case RAW_FILEINFO_BASIC_INFO:
1520         case RAW_FILEINFO_BASIC_INFORMATION:
1521                 CHECK_NTTIMES_EQUAL(basic_info.out.create_time);
1522                 CHECK_NTTIMES_EQUAL(basic_info.out.access_time);
1523                 CHECK_NTTIMES_EQUAL(basic_info.out.write_time);
1524                 CHECK_NTTIMES_EQUAL(basic_info.out.change_time);
1525                 CHECK_EQUAL(basic_info.out.attrib);
1526                 break;
1527
1528         case RAW_FILEINFO_STANDARD_INFO:
1529         case RAW_FILEINFO_STANDARD_INFORMATION:
1530                 CHECK_EQUAL(standard_info.out.alloc_size);
1531                 CHECK_EQUAL(standard_info.out.size);
1532                 CHECK_EQUAL(standard_info.out.nlink);
1533                 CHECK_EQUAL(standard_info.out.delete_pending);
1534                 CHECK_EQUAL(standard_info.out.directory);
1535                 break;
1536
1537         case RAW_FILEINFO_EA_INFO:
1538         case RAW_FILEINFO_EA_INFORMATION:
1539                 CHECK_EQUAL(ea_info.out.ea_size);
1540                 break;
1541
1542         case RAW_FILEINFO_NAME_INFO:
1543         case RAW_FILEINFO_NAME_INFORMATION:
1544                 CHECK_WSTR_EQUAL(name_info.out.fname);
1545                 break;
1546
1547         case RAW_FILEINFO_ALL_INFO:
1548         case RAW_FILEINFO_ALL_INFORMATION:
1549                 CHECK_NTTIMES_EQUAL(all_info.out.create_time);
1550                 CHECK_NTTIMES_EQUAL(all_info.out.access_time);
1551                 CHECK_NTTIMES_EQUAL(all_info.out.write_time);
1552                 CHECK_NTTIMES_EQUAL(all_info.out.change_time);
1553                 CHECK_EQUAL(all_info.out.attrib);
1554                 CHECK_EQUAL(all_info.out.alloc_size);
1555                 CHECK_EQUAL(all_info.out.size);
1556                 CHECK_EQUAL(all_info.out.nlink);
1557                 CHECK_EQUAL(all_info.out.delete_pending);
1558                 CHECK_EQUAL(all_info.out.directory);
1559                 CHECK_EQUAL(all_info.out.ea_size);
1560                 CHECK_WSTR_EQUAL(all_info.out.fname);
1561                 break;
1562
1563         case RAW_FILEINFO_ALT_NAME_INFO:
1564         case RAW_FILEINFO_ALT_NAME_INFORMATION:
1565                 CHECK_WSTR_EQUAL(alt_name_info.out.fname);
1566                 break;
1567
1568         case RAW_FILEINFO_STREAM_INFO:
1569         case RAW_FILEINFO_STREAM_INFORMATION:
1570                 CHECK_EQUAL(stream_info.out.num_streams);
1571                 for (i=0;i<parm[0].stream_info.out.num_streams;i++) {
1572                         CHECK_EQUAL(stream_info.out.streams[i].size);
1573                         CHECK_EQUAL(stream_info.out.streams[i].alloc_size);
1574                         CHECK_WSTR_EQUAL(stream_info.out.streams[i].stream_name);
1575                 }
1576                 break;
1577
1578         case RAW_FILEINFO_COMPRESSION_INFO:
1579         case RAW_FILEINFO_COMPRESSION_INFORMATION:
1580                 CHECK_EQUAL(compression_info.out.compressed_size);
1581                 CHECK_EQUAL(compression_info.out.format);
1582                 CHECK_EQUAL(compression_info.out.unit_shift);
1583                 CHECK_EQUAL(compression_info.out.chunk_shift);
1584                 CHECK_EQUAL(compression_info.out.cluster_shift);
1585                 break;
1586
1587         case RAW_FILEINFO_INTERNAL_INFORMATION:
1588                 CHECK_EQUAL(internal_information.out.file_id);
1589                 break;
1590
1591         case RAW_FILEINFO_ACCESS_INFORMATION:
1592                 CHECK_EQUAL(access_information.out.access_flags);
1593                 break;
1594
1595         case RAW_FILEINFO_POSITION_INFORMATION:
1596                 CHECK_EQUAL(position_information.out.position);
1597                 break;
1598
1599         case RAW_FILEINFO_MODE_INFORMATION:
1600                 CHECK_EQUAL(mode_information.out.mode);
1601                 break;
1602
1603         case RAW_FILEINFO_ALIGNMENT_INFORMATION:
1604                 CHECK_EQUAL(alignment_information.out.alignment_requirement);
1605                 break;
1606
1607         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1608                 CHECK_NTTIMES_EQUAL(network_open_information.out.create_time);
1609                 CHECK_NTTIMES_EQUAL(network_open_information.out.access_time);
1610                 CHECK_NTTIMES_EQUAL(network_open_information.out.write_time);
1611                 CHECK_NTTIMES_EQUAL(network_open_information.out.change_time);
1612                 CHECK_EQUAL(network_open_information.out.alloc_size);
1613                 CHECK_EQUAL(network_open_information.out.size);
1614                 CHECK_EQUAL(network_open_information.out.attrib);
1615                 break;
1616
1617         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1618                 CHECK_EQUAL(attribute_tag_information.out.attrib);
1619                 CHECK_EQUAL(attribute_tag_information.out.reparse_tag);
1620                 break;
1621
1622                 /* Unhandled levels */
1623
1624         case RAW_FILEINFO_SEC_DESC:
1625         case RAW_FILEINFO_EA_LIST:
1626         case RAW_FILEINFO_UNIX_BASIC:
1627         case RAW_FILEINFO_UNIX_LINK:
1628                 break;
1629         }
1630
1631         return True;
1632 }
1633
1634 /*
1635   generate qpathinfo operations
1636 */
1637 static BOOL handler_qpathinfo(int instance)
1638 {
1639         union smb_fileinfo parm[NSERVERS];
1640         NTSTATUS status[NSERVERS];
1641
1642         parm[0].generic.in.fname = gen_fname_open(instance);
1643
1644         gen_fileinfo(instance, &parm[0]);
1645
1646         GEN_COPY_PARM;
1647         GEN_CALL(smb_raw_pathinfo(tree, current_op.mem_ctx, &parm[i]));
1648
1649         return cmp_fileinfo(instance, parm, status);
1650 }
1651
1652 /*
1653   generate qfileinfo operations
1654 */
1655 static BOOL handler_qfileinfo(int instance)
1656 {
1657         union smb_fileinfo parm[NSERVERS];
1658         NTSTATUS status[NSERVERS];
1659
1660         parm[0].generic.in.fnum = gen_fnum(instance);
1661
1662         gen_fileinfo(instance, &parm[0]);
1663
1664         GEN_COPY_PARM;
1665         GEN_SET_FNUM(generic.in.fnum);
1666         GEN_CALL(smb_raw_fileinfo(tree, current_op.mem_ctx, &parm[i]));
1667
1668         return cmp_fileinfo(instance, parm, status);
1669 }
1670
1671
1672 /*
1673   generate a fileinfo query structure
1674 */
1675 static void gen_setfileinfo(int instance, union smb_setfileinfo *info)
1676 {
1677         int i;
1678         #undef LVL
1679         #define LVL(v) {RAW_SFILEINFO_ ## v, "RAW_SFILEINFO_" #v}
1680         struct {
1681                 enum smb_setfileinfo_level level;
1682                 const char *name;
1683         }  levels[] = {
1684 #if 0
1685                 /* disabled until win2003 can handle them ... */
1686                 LVL(EA_SET), LVL(BASIC_INFO), LVL(DISPOSITION_INFO), 
1687                 LVL(STANDARD), LVL(ALLOCATION_INFO), LVL(END_OF_FILE_INFO), 
1688 #endif
1689                 LVL(SETATTR), LVL(SETATTRE), LVL(BASIC_INFORMATION),
1690                 LVL(RENAME_INFORMATION), LVL(DISPOSITION_INFORMATION), 
1691                 LVL(POSITION_INFORMATION), LVL(MODE_INFORMATION),
1692                 LVL(ALLOCATION_INFORMATION), LVL(END_OF_FILE_INFORMATION), 
1693                 LVL(1023), LVL(1025), LVL(1029), LVL(1032), LVL(1039), LVL(1040)
1694         };
1695         do {
1696                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1697         } while (ignore_pattern(levels[i].name));
1698
1699         info->generic.level = levels[i].level;
1700
1701         switch (info->generic.level) {
1702         case RAW_SFILEINFO_SETATTR:
1703                 info->setattr.in.attrib = gen_attrib();
1704                 info->setattr.in.write_time = gen_timet();
1705                 break;
1706         case RAW_SFILEINFO_SETATTRE:
1707                 info->setattre.in.create_time = gen_timet();
1708                 info->setattre.in.access_time = gen_timet();
1709                 info->setattre.in.write_time = gen_timet();
1710                 break;
1711         case RAW_SFILEINFO_STANDARD:
1712                 info->standard.in.create_time = gen_timet();
1713                 info->standard.in.access_time = gen_timet();
1714                 info->standard.in.write_time = gen_timet();
1715                 break;
1716         case RAW_SFILEINFO_EA_SET: {
1717                 static struct ea_struct ea;
1718                 info->ea_set.in.num_eas = 1;
1719                 info->ea_set.in.eas = &ea;
1720                 info->ea_set.in.eas[0] = gen_ea_struct();
1721         }
1722                 break;
1723         case RAW_SFILEINFO_BASIC_INFO:
1724         case RAW_SFILEINFO_BASIC_INFORMATION:
1725                 info->basic_info.in.create_time = gen_nttime();
1726                 info->basic_info.in.access_time = gen_nttime();
1727                 info->basic_info.in.write_time = gen_nttime();
1728                 info->basic_info.in.change_time = gen_nttime();
1729                 info->basic_info.in.attrib = gen_attrib();
1730                 break;
1731         case RAW_SFILEINFO_DISPOSITION_INFO:
1732         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
1733                 info->disposition_info.in.delete_on_close = gen_bool();
1734                 break;
1735         case RAW_SFILEINFO_ALLOCATION_INFO:
1736         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
1737                 info->allocation_info.in.alloc_size = gen_alloc_size();
1738                 break;
1739         case RAW_SFILEINFO_END_OF_FILE_INFO:
1740         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
1741                 info->end_of_file_info.in.size = gen_offset();
1742                 break;
1743         case RAW_SFILEINFO_RENAME_INFORMATION:
1744                 info->rename_information.in.overwrite = gen_bool();
1745                 info->rename_information.in.root_fid = gen_root_fid(instance);
1746                 info->rename_information.in.new_name = gen_fname_open(instance);
1747                 break;
1748         case RAW_SFILEINFO_POSITION_INFORMATION:
1749                 info->position_information.in.position = gen_offset();
1750                 break;
1751         case RAW_SFILEINFO_MODE_INFORMATION:
1752                 info->mode_information.in.mode = gen_bits_mask(0xFFFFFFFF);
1753                 break;
1754         case RAW_SFILEINFO_GENERIC:
1755         case RAW_SFILEINFO_SEC_DESC:
1756         case RAW_SFILEINFO_UNIX_BASIC:
1757         case RAW_SFILEINFO_UNIX_LINK:
1758         case RAW_SFILEINFO_UNIX_HLINK:
1759         case RAW_SFILEINFO_1023:
1760         case RAW_SFILEINFO_1025:
1761         case RAW_SFILEINFO_1029:
1762         case RAW_SFILEINFO_1032:
1763         case RAW_SFILEINFO_1039:
1764         case RAW_SFILEINFO_1040:
1765                 /* Untested */
1766                 break;
1767         }
1768 }
1769
1770 /*
1771   generate setpathinfo operations
1772 */
1773 static BOOL handler_spathinfo(int instance)
1774 {
1775         union smb_setfileinfo parm[NSERVERS];
1776         NTSTATUS status[NSERVERS];
1777
1778         parm[0].generic.file.fname = gen_fname_open(instance);
1779
1780         gen_setfileinfo(instance, &parm[0]);
1781
1782         GEN_COPY_PARM;
1783
1784         /* a special case for the fid in a RENAME */
1785         if (parm[0].generic.level == RAW_SFILEINFO_RENAME_INFORMATION &&
1786             parm[0].rename_information.in.root_fid != 0) {
1787                 GEN_SET_FNUM(rename_information.in.root_fid);
1788         }
1789
1790         GEN_CALL(smb_raw_setpathinfo(tree, &parm[i]));
1791
1792         return True;
1793 }
1794
1795
1796 /*
1797   generate setfileinfo operations
1798 */
1799 static BOOL handler_sfileinfo(int instance)
1800 {
1801         union smb_setfileinfo parm[NSERVERS];
1802         NTSTATUS status[NSERVERS];
1803
1804         parm[0].generic.file.fnum = gen_fnum(instance);
1805
1806         gen_setfileinfo(instance, &parm[0]);
1807
1808         GEN_COPY_PARM;
1809         GEN_SET_FNUM(generic.file.fnum);
1810         GEN_CALL(smb_raw_setfileinfo(tree, &parm[i]));
1811
1812         return True;
1813 }
1814
1815
1816 /*
1817   generate change notify operations
1818 */
1819 static BOOL handler_notify(int instance)
1820 {
1821         struct smb_notify parm[NSERVERS];
1822         int n;
1823
1824         parm[0].in.buffer_size = gen_io_count();
1825         parm[0].in.completion_filter = gen_bits_mask(0xFF);
1826         parm[0].in.fnum = gen_fnum(instance);
1827         parm[0].in.recursive = gen_bool();
1828
1829         GEN_COPY_PARM;
1830         GEN_SET_FNUM(in.fnum);
1831
1832         for (n=0;n<NSERVERS;n++) {
1833                 struct smbcli_request *req;
1834                 req = smb_raw_changenotify_send(servers[n].cli[instance]->tree, &parm[n]);
1835                 req->async.fn = async_notify;
1836         }
1837
1838         return True;
1839 }
1840
1841 /*
1842   wipe any relevant files
1843 */
1844 static void wipe_files(void)
1845 {
1846         int i;
1847         for (i=0;i<NSERVERS;i++) {
1848                 int n = smbcli_deltree(servers[i].cli[0]->tree, "\\gentest");
1849                 if (n == -1) {
1850                         printf("Failed to wipe tree on server %d\n", i);
1851                         exit(1);
1852                 }
1853                 if (NT_STATUS_IS_ERR(smbcli_mkdir(servers[i].cli[0]->tree, "\\gentest"))) {
1854                         printf("Failed to create \\gentest - %s\n",
1855                                smbcli_errstr(servers[i].cli[0]->tree));
1856                         exit(1);
1857                 }
1858                 if (n > 0) {
1859                         printf("Deleted %d files on server %d\n", n, i);
1860                 }
1861         }
1862 }
1863
1864 /*
1865   dump the current seeds - useful for continuing a backtrack
1866 */
1867 static void dump_seeds(void)
1868 {
1869         int i;
1870         FILE *f;
1871
1872         if (!options.seeds_file) {
1873                 return;
1874         }
1875         f = fopen("seeds.tmp", "w");
1876         if (!f) return;
1877
1878         for (i=0;i<options.numops;i++) {
1879                 fprintf(f, "%u\n", op_parms[i].seed);
1880         }
1881         fclose(f);
1882         rename("seeds.tmp", options.seeds_file);
1883 }
1884
1885
1886
1887 /*
1888   the list of top-level operations that we will generate
1889 */
1890 static struct {
1891         const char *name;
1892         BOOL (*handler)(int instance);
1893         int count, success_count;
1894 } gen_ops[] = {
1895         {"OPEN",       handler_open},
1896         {"OPENX",      handler_openx},
1897         {"NTCREATEX",  handler_ntcreatex},
1898         {"CLOSE",      handler_close},
1899         {"UNLINK",     handler_unlink},
1900         {"MKDIR",      handler_mkdir},
1901         {"RMDIR",      handler_rmdir},
1902         {"RENAME",     handler_rename},
1903         {"NTRENAME",   handler_ntrename},
1904         {"READX",      handler_readx},
1905         {"WRITEX",     handler_writex},
1906         {"CHKPATH",    handler_chkpath},
1907         {"LOCKINGX",   handler_lockingx},
1908         {"QPATHINFO",  handler_qpathinfo},
1909         {"QFILEINFO",  handler_qfileinfo},
1910         {"SPATHINFO",  handler_spathinfo},
1911         {"SFILEINFO",  handler_sfileinfo},
1912         {"NOTIFY",     handler_notify},
1913         {"SEEK",       handler_seek},
1914 };
1915
1916
1917 /*
1918   run the test with the current set of op_parms parameters
1919   return the number of operations that completed successfully
1920 */
1921 static int run_test(void)
1922 {
1923         int op, i;
1924
1925         if (!connect_servers()) {
1926                 printf("Failed to connect to servers\n");
1927                 exit(1);
1928         }
1929
1930         dump_seeds();
1931
1932         /* wipe any leftover files from old runs */
1933         wipe_files();
1934
1935         /* reset the open handles array */
1936         memset(open_handles, 0, options.max_open_handles * sizeof(open_handles[0]));
1937         num_open_handles = 0;
1938
1939         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1940                 gen_ops[i].count = 0;
1941                 gen_ops[i].success_count = 0;
1942         }
1943
1944         for (op=0; op<options.numops; op++) {
1945                 int instance, which_op;
1946                 BOOL ret;
1947
1948                 if (op_parms[op].disabled) continue;
1949
1950                 srandom(op_parms[op].seed);
1951
1952                 instance = gen_int_range(0, NINSTANCES-1);
1953
1954                 /* generate a non-ignored operation */
1955                 do {
1956                         which_op = gen_int_range(0, ARRAY_SIZE(gen_ops)-1);
1957                 } while (ignore_pattern(gen_ops[which_op].name));
1958
1959                 DEBUG(3,("Generating op %s on instance %d\n",
1960                          gen_ops[which_op].name, instance));
1961
1962                 current_op.seed = op_parms[op].seed;
1963                 current_op.opnum = op;
1964                 current_op.name = gen_ops[which_op].name;
1965                 current_op.status = NT_STATUS_OK;
1966                 current_op.mem_ctx = talloc_init("%s", current_op.name);
1967
1968                 ret = gen_ops[which_op].handler(instance);
1969
1970                 talloc_free(current_op.mem_ctx);
1971
1972                 gen_ops[which_op].count++;
1973                 if (NT_STATUS_IS_OK(current_op.status)) {
1974                         gen_ops[which_op].success_count++;                      
1975                 }
1976
1977                 if (!ret) {
1978                         printf("Failed at operation %d - %s\n",
1979                                op, gen_ops[which_op].name);
1980                         return op;
1981                 }
1982
1983                 if (op % 100 == 0) {
1984                         printf("%d\n", op);
1985                 }
1986         }
1987
1988         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1989                 printf("Op %-10s got %d/%d success\n", 
1990                        gen_ops[i].name,
1991                        gen_ops[i].success_count,
1992                        gen_ops[i].count);
1993         }
1994
1995         return op;
1996 }
1997
1998 /* 
1999    perform a backtracking analysis of the minimal set of operations
2000    to generate an error
2001 */
2002 static void backtrack_analyze(void)
2003 {
2004         int chunk, ret;
2005
2006         chunk = options.numops / 2;
2007
2008         do {
2009                 int base;
2010                 for (base=0; 
2011                      chunk > 0 && base+chunk < options.numops && options.numops > 1; ) {
2012                         int i, max;
2013
2014                         chunk = MIN(chunk, options.numops / 2);
2015
2016                         /* mark this range as disabled */
2017                         max = MIN(options.numops, base+chunk);
2018                         for (i=base;i<max; i++) {
2019                                 op_parms[i].disabled = True;
2020                         }
2021                         printf("Testing %d ops with %d-%d disabled\n", 
2022                                options.numops, base, max-1);
2023                         ret = run_test();
2024                         printf("Completed %d of %d ops\n", ret, options.numops);
2025                         for (i=base;i<max; i++) {
2026                                 op_parms[i].disabled = False;
2027                         }
2028                         if (ret == options.numops) {
2029                                 /* this chunk is needed */
2030                                 base += chunk;
2031                         } else if (ret < base) {
2032                                 printf("damn - inconsistent errors! found early error\n");
2033                                 options.numops = ret+1;
2034                                 base = 0;
2035                         } else {
2036                                 /* it failed - this chunk isn't needed for a failure */
2037                                 memmove(&op_parms[base], &op_parms[max], 
2038                                         sizeof(op_parms[0]) * (options.numops - max));
2039                                 options.numops = (ret+1) - (max - base);
2040                         }
2041                 }
2042
2043                 if (chunk == 2) {
2044                         chunk = 1;
2045                 } else {
2046                         chunk *= 0.4;
2047                 }
2048
2049                 if (options.analyze_continuous && chunk == 0 && options.numops != 1) {
2050                         chunk = 1;
2051                 }
2052         } while (chunk > 0);
2053
2054         printf("Reduced to %d ops\n", options.numops);
2055         ret = run_test();
2056         if (ret != options.numops - 1) {
2057                 printf("Inconsistent result? ret=%d numops=%d\n", ret, options.numops);
2058         }
2059 }
2060
2061 /* 
2062    start the main gentest process
2063 */
2064 static BOOL start_gentest(void)
2065 {
2066         int op;
2067         int ret;
2068
2069         /* allocate the open_handles array */
2070         open_handles = calloc(options.max_open_handles, sizeof(open_handles[0]));
2071
2072         srandom(options.seed);
2073         op_parms = calloc(options.numops, sizeof(op_parms[0]));
2074
2075         /* generate the seeds - after this everything is deterministic */
2076         if (options.use_preset_seeds) {
2077                 int numops;
2078                 char **preset = file_lines_load(options.seeds_file, &numops, NULL);
2079                 if (!preset) {
2080                         printf("Failed to load %s - %s\n", options.seeds_file, strerror(errno));
2081                         exit(1);
2082                 }
2083                 if (numops < options.numops) {
2084                         options.numops = numops;
2085                 }
2086                 for (op=0;op<options.numops;op++) {
2087                         if (!preset[op]) {
2088                                 printf("Not enough seeds in %s\n", options.seeds_file);
2089                                 exit(1);
2090                         }
2091                         op_parms[op].seed = atoi(preset[op]);
2092                 }
2093                 printf("Loaded %d seeds from %s\n", options.numops, options.seeds_file);
2094         } else {
2095                 for (op=0; op<options.numops; op++) {
2096                         op_parms[op].seed = random();
2097                 }
2098         }
2099
2100         ret = run_test();
2101
2102         if (ret != options.numops && options.analyze) {
2103                 options.numops = ret+1;
2104                 backtrack_analyze();
2105         } else if (options.analyze_always) {
2106                 backtrack_analyze();
2107         } else if (options.analyze_continuous) {
2108                 while (run_test() == options.numops) ;
2109         }
2110
2111         return ret == options.numops;
2112 }
2113
2114
2115 static void usage(void)
2116 {
2117         printf(
2118 "Usage:\n\
2119   gentest2 //server1/share1 //server2/share2 [options..]\n\
2120   options:\n\
2121         -U user%%pass        (can be specified twice)\n\
2122         -s seed\n\
2123         -o numops\n\
2124         -a            (show all ops)\n\
2125         -A            backtrack to find minimal ops\n\
2126         -i FILE       add a list of wildcard exclusions\n\
2127         -O            enable oplocks\n\
2128         -S FILE       set preset seeds file\n\
2129         -L            use preset seeds\n\
2130         -F            fast reconnect (just close files)\n\
2131         -C            continuous analysis mode\n\
2132         -X            analyse even when test OK\n\
2133 ");
2134 }
2135
2136 /****************************************************************************
2137   main program
2138 ****************************************************************************/
2139  int main(int argc, char *argv[])
2140 {
2141         int opt;
2142         int i, username_count=0;
2143         BOOL ret;
2144
2145         setlinebuf(stdout);
2146
2147         setup_logging("gentest", DEBUG_STDOUT);
2148
2149         if (argc < 3 || argv[1][0] == '-') {
2150                 usage();
2151                 exit(1);
2152         }
2153
2154         setup_logging(argv[0], DEBUG_STDOUT);
2155
2156         for (i=0;i<NSERVERS;i++) {
2157                 const char *share = argv[1+i];
2158                 servers[i].credentials = cli_credentials_init(NULL);
2159                 if (!split_unc_name(share, &servers[i].server_name, &servers[i].share_name)) {
2160                         printf("Invalid share name '%s'\n", share);
2161                         return -1;
2162                 }
2163         }
2164
2165         argc -= NSERVERS;
2166         argv += NSERVERS;
2167
2168         lp_load();
2169
2170         servers[0].credentials = cli_credentials_init(talloc_autofree_context());
2171         servers[1].credentials = cli_credentials_init(talloc_autofree_context());
2172         cli_credentials_guess(servers[0].credentials);
2173         cli_credentials_guess(servers[1].credentials);
2174
2175         options.seed = time(NULL);
2176         options.numops = 1000;
2177         options.max_open_handles = 20;
2178         options.seeds_file = "gentest_seeds.dat";
2179
2180         while ((opt = getopt(argc, argv, "U:s:o:ad:i:AOhS:LFXC")) != EOF) {
2181                 switch (opt) {
2182                 case 'U':
2183                         if (username_count == 2) {
2184                                 usage();
2185                                 exit(1);
2186                         }
2187                         cli_credentials_parse_string(servers[username_count].credentials, 
2188                                                      optarg, CRED_SPECIFIED);
2189                         username_count++;
2190                         break;
2191                 case 'd':
2192                         DEBUGLEVEL = atoi(optarg);
2193                         setup_logging(NULL, DEBUG_STDOUT);
2194                         break;
2195                 case 's':
2196                         options.seed = atoi(optarg);
2197                         break;
2198                 case 'S':
2199                         options.seeds_file = optarg;
2200                         break;
2201                 case 'L':
2202                         options.use_preset_seeds = True;
2203                         break;
2204                 case 'F':
2205                         options.fast_reconnect = True;
2206                         break;
2207                 case 'o':
2208                         options.numops = atoi(optarg);
2209                         break;
2210                 case 'O':
2211                         options.use_oplocks = True;
2212                         break;
2213                 case 'a':
2214                         options.showall = True;
2215                         break;
2216                 case 'A':
2217                         options.analyze = True;
2218                         break;
2219                 case 'X':
2220                         options.analyze_always = True;
2221                         break;
2222                 case 'C':
2223                         options.analyze_continuous = True;
2224                         break;
2225                 case 'i':
2226                         options.ignore_patterns = file_lines_load(optarg, NULL, NULL);
2227                         break;
2228                 case 'h':
2229                         usage();
2230                         exit(1);
2231                 default:
2232                         printf("Unknown option %c (%d)\n", (char)opt, opt);
2233                         exit(1);
2234                 }
2235         }
2236
2237         gensec_init();
2238
2239         if (username_count == 0) {
2240                 usage();
2241                 return -1;
2242         }
2243         if (username_count == 1) {
2244                 servers[1].credentials = servers[0].credentials;
2245         }
2246
2247         printf("seed=%u\n", options.seed);
2248
2249         ret = start_gentest();
2250
2251         if (ret) {
2252                 printf("gentest completed - no errors\n");
2253         } else {
2254                 printf("gentest failed\n");
2255         }
2256
2257         return ret?0:-1;
2258 }