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