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