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