r14173: change smb interface structures to always use
[jelmer/samba4-debian.git] / source / 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 "libcli/raw/request.h"
25 #include "libcli/libcli.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "auth/gensec/gensec.h"
29 #include "torture/torture.h"
30
31 #define NSERVERS 2
32 #define NINSTANCES 2
33
34 /* global options */
35 static struct gentest_options {
36         BOOL showall;
37         BOOL analyze;
38         BOOL analyze_always;
39         BOOL analyze_continuous;
40         uint_t max_open_handles;
41         uint_t seed;
42         uint_t numops;
43         BOOL use_oplocks;
44         char **ignore_patterns;
45         const char *seeds_file;
46         BOOL use_preset_seeds;
47         BOOL fast_reconnect;
48 } options;
49
50 /* mapping between open handles on the server and local handles */
51 static struct {
52         BOOL active;
53         uint_t instance;
54         uint_t server_fnum[NSERVERS];
55         const char *name;
56 } *open_handles;
57 static uint_t num_open_handles;
58
59 /* state information for the servers. We open NINSTANCES connections to
60    each server */
61 static struct {
62         struct smbcli_state *cli[NINSTANCES];
63         char *server_name;
64         char *share_name;
65         struct cli_credentials *credentials;
66 } servers[NSERVERS];
67
68 /* the seeds and flags for each operation */
69 static struct {
70         uint_t seed;
71         BOOL disabled;
72 } *op_parms;
73
74
75 /* oplock break info */
76 static struct {
77         BOOL got_break;
78         uint16_t fnum;
79         uint16_t handle;
80         uint8_t level;
81         BOOL do_close;
82 } oplocks[NSERVERS][NINSTANCES];
83
84 /* change notify reply info */
85 static struct {
86         int notify_count;
87         NTSTATUS status;
88         union smb_notify notify;
89 } notifies[NSERVERS][NINSTANCES];
90
91 /* info relevant to the current operation */
92 static struct {
93         const char *name;
94         uint_t seed;
95         NTSTATUS status;
96         uint_t opnum;
97         TALLOC_CTX *mem_ctx;
98 } current_op;
99
100
101
102 #define BAD_HANDLE 0xFFFE
103
104 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private);
105 static void idle_func(struct smbcli_transport *transport, void *private);
106
107 /*
108   check if a string should be ignored. This is used as the basis
109   for all error ignore settings
110 */
111 static BOOL ignore_pattern(const char *str)
112 {
113         int i;
114         if (!options.ignore_patterns) return False;
115
116         for (i=0;options.ignore_patterns[i];i++) {
117                 if (strcmp(options.ignore_patterns[i], str) == 0 ||
118                     gen_fnmatch(options.ignore_patterns[i], str) == 0) {
119                         DEBUG(2,("Ignoring '%s'\n", str));
120                         return True;
121                 }
122         }
123         return False;
124 }
125
126 /***************************************************** 
127 connect to the servers
128 *******************************************************/
129 static BOOL connect_servers_fast(void)
130 {
131         int h, i;
132
133         /* close all open files */
134         for (h=0;h<options.max_open_handles;h++) {
135                 if (!open_handles[h].active) continue;
136                 for (i=0;i<NSERVERS;i++) {
137                         if (NT_STATUS_IS_ERR((smbcli_close(servers[i].cli[open_handles[h].instance]->tree,
138                                        open_handles[h].server_fnum[i])))) {
139                                 return False;
140                         }
141                         open_handles[h].active = False;
142                 }
143         }
144
145         return True;
146 }
147
148
149
150
151 /***************************************************** 
152 connect to the servers
153 *******************************************************/
154 static BOOL connect_servers(void)
155 {
156         int i, j;
157
158         if (options.fast_reconnect && servers[0].cli[0]) {
159                 if (connect_servers_fast()) {
160                         return True;
161                 }
162         }
163
164         /* close any existing connections */
165         for (i=0;i<NSERVERS;i++) {
166                 for (j=0;j<NINSTANCES;j++) {
167                         if (servers[i].cli[j]) {
168                                 smbcli_tdis(servers[i].cli[j]);
169                                 talloc_free(servers[i].cli[j]);
170                                 servers[i].cli[j] = NULL;
171                         }
172                 }
173         }
174
175         for (i=0;i<NSERVERS;i++) {
176                 for (j=0;j<NINSTANCES;j++) {
177                         NTSTATUS status;
178                         printf("Connecting to \\\\%s\\%s as %s - instance %d\n",
179                                servers[i].server_name, servers[i].share_name, 
180                                servers[i].credentials->username, j);
181
182                         cli_credentials_set_workstation(servers[i].credentials, 
183                                                         "gentest", CRED_SPECIFIED);
184
185                         status = smbcli_full_connection(NULL, &servers[i].cli[j],
186                                                         servers[i].server_name, 
187                                                         servers[i].share_name, NULL, 
188                                                         servers[i].credentials, NULL);
189                         if (!NT_STATUS_IS_OK(status)) {
190                                 printf("Failed to connect to \\\\%s\\%s - %s\n",
191                                        servers[i].server_name, servers[i].share_name,
192                                        nt_errstr(status));
193                                 return False;
194                         }
195
196                         smbcli_oplock_handler(servers[i].cli[j]->transport, oplock_handler, NULL);
197                         smbcli_transport_idle_handler(servers[i].cli[j]->transport, idle_func, 50000, NULL);
198                 }
199         }
200
201         return True;
202 }
203
204 /*
205   work out the time skew between the servers - be conservative
206 */
207 static uint_t time_skew(void)
208 {
209         uint_t ret;
210         ret = ABS(servers[0].cli[0]->transport->negotiate.server_time -
211                   servers[1].cli[0]->transport->negotiate.server_time);
212         return ret + 300;
213 }
214
215 /*
216   turn an fnum for an instance into a handle
217 */
218 static uint_t fnum_to_handle(int server, int instance, uint16_t fnum)
219 {
220         uint_t i;
221         for (i=0;i<options.max_open_handles;i++) {
222                 if (!open_handles[i].active ||
223                     instance != open_handles[i].instance) continue;
224                 if (open_handles[i].server_fnum[server] == fnum) {
225                         return i;
226                 }
227         }
228         printf("Invalid fnum %d in fnum_to_handle on server %d instance %d\n", 
229                fnum, server, instance);
230         return BAD_HANDLE;
231 }
232
233 /*
234   add some newly opened handles
235 */
236 static void gen_add_handle(int instance, const char *name, uint16_t fnums[NSERVERS])
237 {
238         int i, h;
239         for (h=0;h<options.max_open_handles;h++) {
240                 if (!open_handles[h].active) break;
241         }
242         if (h == options.max_open_handles) {
243                 /* we have to force close a random handle */
244                 h = random() % options.max_open_handles;
245                 for (i=0;i<NSERVERS;i++) {
246                         if (NT_STATUS_IS_ERR((smbcli_close(servers[i].cli[open_handles[h].instance]->tree, 
247                                        open_handles[h].server_fnum[i])))) {
248                                 printf("INTERNAL ERROR: Close failed when recovering handle! - %s\n",
249                                        smbcli_errstr(servers[i].cli[open_handles[h].instance]->tree));
250                         }
251                 }
252                 printf("Recovered handle %d\n", h);
253                 num_open_handles--;
254         }
255         for (i=0;i<NSERVERS;i++) {
256                 open_handles[h].server_fnum[i] = fnums[i];
257                 open_handles[h].instance = instance;
258                 open_handles[h].active = True;
259                 open_handles[h].name = name;
260         }
261         num_open_handles++;
262
263         printf("OPEN num_open_handles=%d h=%d s1=0x%x s2=0x%x (%s)\n", 
264                num_open_handles, h, 
265                open_handles[h].server_fnum[0], open_handles[h].server_fnum[1],
266                name);
267 }
268
269 /*
270   remove a closed handle
271 */
272 static void gen_remove_handle(int instance, uint16_t fnums[NSERVERS])
273 {
274         int h;
275         for (h=0;h<options.max_open_handles;h++) {
276                 if (instance == open_handles[h].instance &&
277                     open_handles[h].server_fnum[0] == fnums[0]) {
278                         open_handles[h].active = False;                 
279                         num_open_handles--;
280                         printf("CLOSE num_open_handles=%d h=%d s1=0x%x s2=0x%x (%s)\n", 
281                                num_open_handles, h, 
282                                open_handles[h].server_fnum[0], open_handles[h].server_fnum[1],
283                                open_handles[h].name);
284                         return;
285                 }
286         }
287         printf("Removing invalid handle!?\n");
288         exit(1);
289 }
290
291 /*
292   return True with 'chance' probability as a percentage
293 */
294 static BOOL gen_chance(uint_t chance)
295 {
296         return ((random() % 100) <= chance);
297 }
298
299 /*
300   map an internal handle number to a server fnum
301 */
302 static uint16_t gen_lookup_fnum(int server, uint16_t handle)
303 {
304         if (handle == BAD_HANDLE) return handle;
305         return open_handles[handle].server_fnum[server];
306 }
307
308 /*
309   return a file handle
310 */
311 static uint16_t gen_fnum(int instance)
312 {
313         uint16_t h;
314         int count = 0;
315
316         if (gen_chance(20)) return BAD_HANDLE;
317
318         while (num_open_handles > 0 && count++ < 10*options.max_open_handles) {
319                 h = random() % options.max_open_handles;
320                 if (open_handles[h].active && 
321                     open_handles[h].instance == instance) {
322                         return h;
323                 }
324         }
325         return BAD_HANDLE;
326 }
327
328 /*
329   return a file handle, but skewed so we don't close the last
330   couple of handles too readily
331 */
332 static uint16_t gen_fnum_close(int instance)
333 {
334         if (num_open_handles < 3) {
335                 if (gen_chance(80)) return BAD_HANDLE;
336         }
337
338         return gen_fnum(instance);
339 }
340
341 /*
342   generate an integer in a specified range
343 */
344 static int gen_int_range(uint_t min, uint_t max)
345 {
346         uint_t r = random();
347         return min + (r % (1+max-min));
348 }
349
350 /*
351   return a fnum for use as a root fid
352   be careful to call GEN_SET_FNUM() when you use this!
353 */
354 static uint16_t gen_root_fid(int instance)
355 {
356         if (gen_chance(5)) return gen_fnum(instance);
357         return 0;
358 }
359
360 /*
361   generate a file offset
362 */
363 static int gen_offset(void)
364 {
365         if (gen_chance(20)) return 0;
366         return gen_int_range(0, 1024*1024);
367 }
368
369 /*
370   generate a io count
371 */
372 static int gen_io_count(void)
373 {
374         if (gen_chance(20)) return 0;
375         return gen_int_range(0, 4096);
376 }
377
378 /*
379   generate a filename
380 */
381 static const char *gen_fname(void)
382 {
383         const char *names[] = {"\\gentest\\gentest.dat", 
384                                "\\gentest\\foo", 
385                                "\\gentest\\foo2.sym", 
386                                "\\gentest\\foo3.dll", 
387                                "\\gentest\\foo4", 
388                                "\\gentest\\foo4:teststream1", 
389                                "\\gentest\\foo4:teststream2", 
390                                "\\gentest\\foo5.exe", 
391                                "\\gentest\\foo5.exe:teststream3", 
392                                "\\gentest\\foo5.exe:teststream4", 
393                                "\\gentest\\foo6.com", 
394                                "\\gentest\\blah", 
395                                "\\gentest\\blah\\blergh.txt", 
396                                "\\gentest\\blah\\blergh2", 
397                                "\\gentest\\blah\\blergh3.txt", 
398                                "\\gentest\\blah\\blergh4", 
399                                "\\gentest\\blah\\blergh5.txt", 
400                                "\\gentest\\blah\\blergh5", 
401                                "\\gentest\\blah\\.", 
402 #if 0
403                                /* this causes problem with w2k3 */
404                                "\\gentest\\blah\\..", 
405 #endif
406                                "\\gentest\\a_very_long_name.bin", 
407                                "\\gentest\\x.y", 
408                                "\\gentest\\blah"};
409         int i;
410
411         do {
412                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
413         } while (ignore_pattern(names[i]));
414
415         return names[i];
416 }
417
418 /*
419   generate a filename with a higher chance of choosing an already 
420   open file
421 */
422 static const char *gen_fname_open(int instance)
423 {
424         uint16_t h;
425         h = gen_fnum(instance);
426         if (h == BAD_HANDLE) {
427                 return gen_fname();
428         }
429         return open_handles[h].name;
430 }
431
432 /*
433   generate a wildcard pattern
434 */
435 static const char *gen_pattern(void)
436 {
437         int i;
438         const char *names[] = {"\\gentest\\*.dat", 
439                                "\\gentest\\*", 
440                                "\\gentest\\*.*", 
441                                "\\gentest\\blah\\*.*", 
442                                "\\gentest\\blah\\*", 
443                                "\\gentest\\?"};
444
445         if (gen_chance(50)) return gen_fname();
446
447         do {
448                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
449         } while (ignore_pattern(names[i]));
450
451         return names[i];
452 }
453
454 /*
455   generate a bitmask
456 */
457 static uint32_t gen_bits_mask(uint_t mask)
458 {
459         uint_t ret = random();
460         return ret & mask;
461 }
462
463 /*
464   generate a bitmask with high probability of the first mask
465   and low of the second
466 */
467 static uint32_t gen_bits_mask2(uint32_t mask1, uint32_t mask2)
468 {
469         if (gen_chance(10)) return gen_bits_mask(mask2);
470         return gen_bits_mask(mask1);
471 }
472
473 /*
474   generate a boolean
475 */
476 static BOOL gen_bool(void)
477 {
478         return gen_bits_mask2(0x1, 0xFF);
479 }
480
481 /*
482   generate ntrename flags
483 */
484 static uint16_t gen_rename_flags(void)
485 {
486         if (gen_chance(30)) return RENAME_FLAG_RENAME;
487         if (gen_chance(30)) return RENAME_FLAG_HARD_LINK;
488         if (gen_chance(30)) return RENAME_FLAG_COPY;
489         return gen_bits_mask(0xFFFF);
490 }
491
492
493 /*
494   return a lockingx lock mode
495 */
496 static uint16_t gen_lock_mode(void)
497 {
498         if (gen_chance(5))  return gen_bits_mask(0xFFFF);
499         if (gen_chance(20)) return gen_bits_mask(0x1F);
500         return gen_bits_mask(LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES);
501 }
502
503 /*
504   generate a pid 
505 */
506 static uint16_t gen_pid(void)
507 {
508         if (gen_chance(10)) return gen_bits_mask(0xFFFF);
509         return getpid();
510 }
511
512 /*
513   generate a lock count
514 */
515 static off_t gen_lock_count(void)
516 {
517         return gen_int_range(0, 3);
518 }
519
520 /*
521   generate a ntcreatex flags field
522 */
523 static uint32_t gen_ntcreatex_flags(void)
524 {
525         if (gen_chance(70)) return NTCREATEX_FLAGS_EXTENDED;
526         return gen_bits_mask2(0x1F, 0xFFFFFFFF);
527 }
528
529 /*
530   generate a NT access mask
531 */
532 static uint32_t gen_access_mask(void)
533 {
534         if (gen_chance(50)) return SEC_FLAG_MAXIMUM_ALLOWED;
535         if (gen_chance(20)) return SEC_FILE_ALL;
536         return gen_bits_mask(0xFFFFFFFF);
537 }
538
539 /*
540   generate a ntcreatex create options bitfield
541 */
542 static uint32_t gen_create_options(void)
543 {
544         if (gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
545         if (gen_chance(50)) return 0;
546         return gen_bits_mask(NTCREATEX_OPTIONS_DELETE_ON_CLOSE | NTCREATEX_OPTIONS_DIRECTORY);
547 }
548
549 /*
550   generate a ntcreatex open disposition
551 */
552 static uint32_t gen_open_disp(void)
553 {
554         if (gen_chance(10)) return gen_bits_mask(0xFFFFFFFF);
555         return gen_int_range(0, 5);
556 }
557
558 /*
559   generate an openx open mode
560 */
561 static uint16_t gen_openx_mode(void)
562 {
563         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
564         if (gen_chance(20)) return gen_bits_mask(0xFF);
565         return OPENX_MODE_DENY_NONE | gen_bits_mask(0x3);
566 }
567
568 /*
569   generate an openx flags field
570 */
571 static uint16_t gen_openx_flags(void)
572 {
573         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
574         return gen_bits_mask(0x7);
575 }
576
577 /*
578   generate an openx open function
579 */
580 static uint16_t gen_openx_func(void)
581 {
582         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
583         return gen_bits_mask(0x13);
584 }
585
586 /*
587   generate a file attrib combination
588 */
589 static uint32_t gen_attrib(void)
590 {
591         if (gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
592         return gen_bits_mask(FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
593 }
594
595 /*
596   generate a unix timestamp
597 */
598 static time_t gen_timet(void)
599 {
600         if (gen_chance(30)) return 0;
601         return (time_t)random();
602 }
603
604 /*
605   generate a unix timestamp
606 */
607 static NTTIME gen_nttime(void)
608 {
609         NTTIME ret;
610         unix_to_nt_time(&ret, gen_timet());
611         return ret;
612 }
613
614 /*
615   generate a milliseconds protocol timeout
616 */
617 static uint32_t gen_timeout(void)
618 {
619         if (gen_chance(98)) return 0;
620         return random() % 50;
621 }
622
623 /*
624   generate a file allocation size
625 */
626 static uint_t gen_alloc_size(void)
627 {
628         uint_t ret;
629
630         if (gen_chance(30)) return 0;
631
632         ret = random() % 4*1024*1024;
633         /* give a high chance of a round number */
634         if (gen_chance(60)) {
635                 ret &= ~(1024*1024 - 1);
636         }
637         return ret;
638 }
639
640 /*
641   generate an ea_struct
642 */
643 static struct ea_struct gen_ea_struct(void)
644 {
645         struct ea_struct ea;
646         const char *names[] = {"EAONE", 
647                                "", 
648                                "FOO!", 
649                                " WITH SPACES ", 
650                                ".", 
651                                "AVERYLONGATTRIBUTENAME"};
652         const char *values[] = {"VALUE1", 
653                                "", 
654                                "NOT MUCH FOO", 
655                                " LEADING SPACES ", 
656                                ":", 
657                                "ASOMEWHATLONGERATTRIBUTEVALUE"};
658         int i;
659
660         do {
661                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
662         } while (ignore_pattern(names[i]));
663
664         ea.name.s = names[i];
665
666         do {
667                 i = gen_int_range(0, ARRAY_SIZE(values)-1);
668         } while (ignore_pattern(values[i]));
669
670         ea.value = data_blob(values[i], strlen(values[i]));
671
672         if (gen_chance(10)) ea.flags = gen_bits_mask(0xFF);
673         ea.flags = 0;
674
675         return ea;
676 }
677
678
679 /*
680   this is called when a change notify reply comes in
681 */
682 static void async_notify(struct smbcli_request *req)
683 {
684         union smb_notify notify;
685         NTSTATUS status;
686         int i, j;
687         uint16_t tid;
688         struct smbcli_transport *transport = req->transport;
689
690         tid = SVAL(req->in.hdr, HDR_TID);
691
692         status = smb_raw_changenotify_recv(req, current_op.mem_ctx, &notify);
693         if (NT_STATUS_IS_OK(status)) {
694                 printf("notify tid=%d num_changes=%d action=%d name=%s\n", 
695                        tid, 
696                        notify.notify.out.num_changes,
697                        notify.notify.out.changes[0].action,
698                        notify.notify.out.changes[0].name.s);
699         }
700
701         for (i=0;i<NSERVERS;i++) {
702                 for (j=0;j<NINSTANCES;j++) {
703                         if (transport == servers[i].cli[j]->transport &&
704                             tid == servers[i].cli[j]->tree->tid) {
705                                 notifies[i][j].notify_count++;
706                                 notifies[i][j].status = status;
707                                 notifies[i][j].notify = notify;
708                         }
709                 }
710         }
711 }
712
713 static void oplock_handler_close_recv(struct smbcli_request *req)
714 {
715         NTSTATUS status;
716         status = smbcli_request_simple_recv(req);
717         if (!NT_STATUS_IS_OK(status)) {
718                 printf("close failed in oplock_handler\n");
719                 smb_panic("close failed in oplock_handler");
720         }
721 }
722
723 /*
724   the oplock handler will either ack the break or close the file
725 */
726 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private)
727 {
728         union smb_close io;
729         NTSTATUS status;
730         int i, j;
731         BOOL do_close;
732         struct smbcli_tree *tree = NULL;
733         struct smbcli_request *req;
734
735         srandom(current_op.seed);
736         do_close = gen_chance(50);
737
738         for (i=0;i<NSERVERS;i++) {
739                 for (j=0;j<NINSTANCES;j++) {
740                         if (transport == servers[i].cli[j]->transport &&
741                             tid == servers[i].cli[j]->tree->tid) {
742                                 oplocks[i][j].got_break = True;
743                                 oplocks[i][j].fnum = fnum;
744                                 oplocks[i][j].handle = fnum_to_handle(i, j, fnum);
745                                 oplocks[i][j].level = level;
746                                 oplocks[i][j].do_close = do_close;
747                                 tree = servers[i].cli[j]->tree;
748                         }
749                 }
750         }
751
752         if (!tree) {
753                 printf("Oplock break not for one of our trees!?\n");
754                 return False;
755         }
756
757         if (!do_close) {
758                 printf("oplock ack fnum=%d\n", fnum);
759                 return smbcli_oplock_ack(tree, fnum, level);
760         }
761
762         printf("oplock close fnum=%d\n", fnum);
763
764         io.close.level = RAW_CLOSE_CLOSE;
765         io.close.file.fnum = fnum;
766         io.close.in.write_time = 0;
767         req = smb_raw_close_send(tree, &io);
768
769         if (req == NULL) {
770                 printf("WARNING: close failed in oplock_handler_close - %s\n", 
771                        nt_errstr(status));
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.notify.out.num_changes;n++) {
924                                 if (not1.notify.out.changes[n].action != 
925                                     not2.notify.out.changes[n].action) {
926                                         printf("Notify action %d inconsistent %d %d\n", n,
927                                                not1.notify.out.changes[n].action,
928                                                not2.notify.out.changes[n].action);
929                                         return False;
930                                 }
931                                 if (strcmp(not1.notify.out.changes[n].name.s,
932                                            not2.notify.out.changes[n].name.s)) {
933                                         printf("Notify name %d inconsistent %s %s\n", n,
934                                                not1.notify.out.changes[n].name.s,
935                                                not2.notify.out.changes[n].name.s);
936                                         return False;
937                                 }
938                                 if (not1.notify.out.changes[n].name.private_length !=
939                                     not2.notify.out.changes[n].name.private_length) {
940                                         printf("Notify name length %d inconsistent %d %d\n", n,
941                                                not1.notify.out.changes[n].name.private_length,
942                                                not2.notify.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 (ABS(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 (ABS(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.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.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.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.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.file.fnum);
1204         GEN_CALL(smb_raw_close(tree, &parm[i]));
1205
1206         REMOVE_HANDLE(close.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.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.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.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.out.data = talloc_size(current_op.mem_ctx,
1355                                              MAX(parm[0].readx.in.mincnt, parm[0].readx.in.maxcnt));
1356
1357         GEN_COPY_PARM;
1358         GEN_SET_FNUM(readx.file.fnum);
1359         GEN_CALL(smb_raw_read(tree, &parm[i]));
1360
1361         CHECK_EQUAL(readx.out.remaining);
1362         CHECK_EQUAL(readx.out.compaction_mode);
1363         CHECK_EQUAL(readx.out.nread);
1364
1365         return True;
1366 }
1367
1368 /*
1369   generate writex operations
1370 */
1371 static BOOL handler_writex(int instance)
1372 {
1373         union smb_write parm[NSERVERS];
1374         NTSTATUS status[NSERVERS];
1375
1376         parm[0].writex.level = RAW_WRITE_WRITEX;
1377         parm[0].writex.file.fnum = gen_fnum(instance);
1378         parm[0].writex.in.offset = gen_offset();
1379         parm[0].writex.in.wmode = gen_bits_mask(0xFFFF);
1380         parm[0].writex.in.remaining = gen_io_count();
1381         parm[0].writex.in.count = gen_io_count();
1382         parm[0].writex.in.data = talloc_zero_size(current_op.mem_ctx, parm[0].writex.in.count);
1383
1384         GEN_COPY_PARM;
1385         GEN_SET_FNUM(writex.file.fnum);
1386         GEN_CALL(smb_raw_write(tree, &parm[i]));
1387
1388         CHECK_EQUAL(writex.out.nwritten);
1389         CHECK_EQUAL(writex.out.remaining);
1390
1391         return True;
1392 }
1393
1394 /*
1395   generate lockingx operations
1396 */
1397 static BOOL handler_lockingx(int instance)
1398 {
1399         union smb_lock parm[NSERVERS];
1400         NTSTATUS status[NSERVERS];
1401         int n, nlocks;
1402
1403         parm[0].lockx.level = RAW_LOCK_LOCKX;
1404         parm[0].lockx.file.fnum = gen_fnum(instance);
1405         parm[0].lockx.in.mode = gen_lock_mode();
1406         parm[0].lockx.in.timeout = gen_timeout();
1407         do {
1408                 /* make sure we don't accidentially generate an oplock
1409                    break ack - otherwise the server can just block forever */
1410                 parm[0].lockx.in.ulock_cnt = gen_lock_count();
1411                 parm[0].lockx.in.lock_cnt = gen_lock_count();
1412                 nlocks = parm[0].lockx.in.ulock_cnt + parm[0].lockx.in.lock_cnt;
1413         } while (nlocks == 0);
1414
1415         if (nlocks > 0) {
1416                 parm[0].lockx.in.locks = talloc_array(current_op.mem_ctx,
1417                                                         struct smb_lock_entry,
1418                                                         nlocks);
1419                 for (n=0;n<nlocks;n++) {
1420                         parm[0].lockx.in.locks[n].pid = gen_pid();
1421                         parm[0].lockx.in.locks[n].offset = gen_offset();
1422                         parm[0].lockx.in.locks[n].count = gen_io_count();
1423                 }
1424         }
1425
1426         GEN_COPY_PARM;
1427         GEN_SET_FNUM(lockx.file.fnum);
1428         GEN_CALL(smb_raw_lock(tree, &parm[i]));
1429
1430         return True;
1431 }
1432
1433 /*
1434   generate a fileinfo query structure
1435 */
1436 static void gen_fileinfo(int instance, union smb_fileinfo *info)
1437 {
1438         int i;
1439         #define LVL(v) {RAW_FILEINFO_ ## v, "RAW_FILEINFO_" #v}
1440         struct {
1441                 enum smb_fileinfo_level level;
1442                 const char *name;
1443         }  levels[] = {
1444                 LVL(GETATTR), LVL(GETATTRE), LVL(STANDARD),
1445                 LVL(EA_SIZE), LVL(ALL_EAS), LVL(IS_NAME_VALID),
1446                 LVL(BASIC_INFO), LVL(STANDARD_INFO), LVL(EA_INFO),
1447                 LVL(NAME_INFO), LVL(ALL_INFO), LVL(ALT_NAME_INFO),
1448                 LVL(STREAM_INFO), LVL(COMPRESSION_INFO), LVL(BASIC_INFORMATION),
1449                 LVL(STANDARD_INFORMATION), LVL(INTERNAL_INFORMATION), LVL(EA_INFORMATION),
1450                 LVL(ACCESS_INFORMATION), LVL(NAME_INFORMATION), LVL(POSITION_INFORMATION),
1451                 LVL(MODE_INFORMATION), LVL(ALIGNMENT_INFORMATION), LVL(ALL_INFORMATION),
1452                 LVL(ALT_NAME_INFORMATION), LVL(STREAM_INFORMATION), LVL(COMPRESSION_INFORMATION),
1453                 LVL(NETWORK_OPEN_INFORMATION), LVL(ATTRIBUTE_TAG_INFORMATION)
1454         };
1455         do {
1456                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1457         } while (ignore_pattern(levels[i].name));
1458
1459         info->generic.level = levels[i].level;
1460 }
1461
1462 /*
1463   compare returned fileinfo structures
1464 */
1465 static BOOL cmp_fileinfo(int instance, 
1466                          union smb_fileinfo parm[NSERVERS],
1467                          NTSTATUS status[NSERVERS])
1468 {
1469         int i;
1470
1471         switch (parm[0].generic.level) {
1472         case RAW_FILEINFO_GENERIC:
1473                 return False;
1474
1475         case RAW_FILEINFO_GETATTR:
1476                 CHECK_EQUAL(getattr.out.attrib);
1477                 CHECK_EQUAL(getattr.out.size);
1478                 CHECK_TIMES_EQUAL(getattr.out.write_time);
1479                 break;
1480
1481         case RAW_FILEINFO_GETATTRE:
1482                 CHECK_TIMES_EQUAL(getattre.out.create_time);
1483                 CHECK_TIMES_EQUAL(getattre.out.access_time);
1484                 CHECK_TIMES_EQUAL(getattre.out.write_time);
1485                 CHECK_EQUAL(getattre.out.size);
1486                 CHECK_EQUAL(getattre.out.alloc_size);
1487                 CHECK_EQUAL(getattre.out.attrib);
1488                 break;
1489
1490         case RAW_FILEINFO_STANDARD:
1491                 CHECK_TIMES_EQUAL(standard.out.create_time);
1492                 CHECK_TIMES_EQUAL(standard.out.access_time);
1493                 CHECK_TIMES_EQUAL(standard.out.write_time);
1494                 CHECK_EQUAL(standard.out.size);
1495                 CHECK_EQUAL(standard.out.alloc_size);
1496                 CHECK_EQUAL(standard.out.attrib);
1497                 break;
1498
1499         case RAW_FILEINFO_EA_SIZE:
1500                 CHECK_TIMES_EQUAL(ea_size.out.create_time);
1501                 CHECK_TIMES_EQUAL(ea_size.out.access_time);
1502                 CHECK_TIMES_EQUAL(ea_size.out.write_time);
1503                 CHECK_EQUAL(ea_size.out.size);
1504                 CHECK_EQUAL(ea_size.out.alloc_size);
1505                 CHECK_EQUAL(ea_size.out.attrib);
1506                 CHECK_EQUAL(ea_size.out.ea_size);
1507                 break;
1508
1509         case RAW_FILEINFO_ALL_EAS:
1510                 CHECK_EQUAL(all_eas.out.num_eas);
1511                 for (i=0;i<parm[0].all_eas.out.num_eas;i++) {
1512                         CHECK_EQUAL(all_eas.out.eas[i].flags);
1513                         CHECK_WSTR_EQUAL(all_eas.out.eas[i].name);
1514                         CHECK_BLOB_EQUAL(all_eas.out.eas[i].value);
1515                 }
1516                 break;
1517
1518         case RAW_FILEINFO_IS_NAME_VALID:
1519                 break;
1520                 
1521         case RAW_FILEINFO_BASIC_INFO:
1522         case RAW_FILEINFO_BASIC_INFORMATION:
1523                 CHECK_NTTIMES_EQUAL(basic_info.out.create_time);
1524                 CHECK_NTTIMES_EQUAL(basic_info.out.access_time);
1525                 CHECK_NTTIMES_EQUAL(basic_info.out.write_time);
1526                 CHECK_NTTIMES_EQUAL(basic_info.out.change_time);
1527                 CHECK_EQUAL(basic_info.out.attrib);
1528                 break;
1529
1530         case RAW_FILEINFO_STANDARD_INFO:
1531         case RAW_FILEINFO_STANDARD_INFORMATION:
1532                 CHECK_EQUAL(standard_info.out.alloc_size);
1533                 CHECK_EQUAL(standard_info.out.size);
1534                 CHECK_EQUAL(standard_info.out.nlink);
1535                 CHECK_EQUAL(standard_info.out.delete_pending);
1536                 CHECK_EQUAL(standard_info.out.directory);
1537                 break;
1538
1539         case RAW_FILEINFO_EA_INFO:
1540         case RAW_FILEINFO_EA_INFORMATION:
1541                 CHECK_EQUAL(ea_info.out.ea_size);
1542                 break;
1543
1544         case RAW_FILEINFO_NAME_INFO:
1545         case RAW_FILEINFO_NAME_INFORMATION:
1546                 CHECK_WSTR_EQUAL(name_info.out.fname);
1547                 break;
1548
1549         case RAW_FILEINFO_ALL_INFO:
1550         case RAW_FILEINFO_ALL_INFORMATION:
1551                 CHECK_NTTIMES_EQUAL(all_info.out.create_time);
1552                 CHECK_NTTIMES_EQUAL(all_info.out.access_time);
1553                 CHECK_NTTIMES_EQUAL(all_info.out.write_time);
1554                 CHECK_NTTIMES_EQUAL(all_info.out.change_time);
1555                 CHECK_EQUAL(all_info.out.attrib);
1556                 CHECK_EQUAL(all_info.out.alloc_size);
1557                 CHECK_EQUAL(all_info.out.size);
1558                 CHECK_EQUAL(all_info.out.nlink);
1559                 CHECK_EQUAL(all_info.out.delete_pending);
1560                 CHECK_EQUAL(all_info.out.directory);
1561                 CHECK_EQUAL(all_info.out.ea_size);
1562                 CHECK_WSTR_EQUAL(all_info.out.fname);
1563                 break;
1564
1565         case RAW_FILEINFO_ALT_NAME_INFO:
1566         case RAW_FILEINFO_ALT_NAME_INFORMATION:
1567                 CHECK_WSTR_EQUAL(alt_name_info.out.fname);
1568                 break;
1569
1570         case RAW_FILEINFO_STREAM_INFO:
1571         case RAW_FILEINFO_STREAM_INFORMATION:
1572                 CHECK_EQUAL(stream_info.out.num_streams);
1573                 for (i=0;i<parm[0].stream_info.out.num_streams;i++) {
1574                         CHECK_EQUAL(stream_info.out.streams[i].size);
1575                         CHECK_EQUAL(stream_info.out.streams[i].alloc_size);
1576                         CHECK_WSTR_EQUAL(stream_info.out.streams[i].stream_name);
1577                 }
1578                 break;
1579
1580         case RAW_FILEINFO_COMPRESSION_INFO:
1581         case RAW_FILEINFO_COMPRESSION_INFORMATION:
1582                 CHECK_EQUAL(compression_info.out.compressed_size);
1583                 CHECK_EQUAL(compression_info.out.format);
1584                 CHECK_EQUAL(compression_info.out.unit_shift);
1585                 CHECK_EQUAL(compression_info.out.chunk_shift);
1586                 CHECK_EQUAL(compression_info.out.cluster_shift);
1587                 break;
1588
1589         case RAW_FILEINFO_INTERNAL_INFORMATION:
1590                 CHECK_EQUAL(internal_information.out.file_id);
1591                 break;
1592
1593         case RAW_FILEINFO_ACCESS_INFORMATION:
1594                 CHECK_EQUAL(access_information.out.access_flags);
1595                 break;
1596
1597         case RAW_FILEINFO_POSITION_INFORMATION:
1598                 CHECK_EQUAL(position_information.out.position);
1599                 break;
1600
1601         case RAW_FILEINFO_MODE_INFORMATION:
1602                 CHECK_EQUAL(mode_information.out.mode);
1603                 break;
1604
1605         case RAW_FILEINFO_ALIGNMENT_INFORMATION:
1606                 CHECK_EQUAL(alignment_information.out.alignment_requirement);
1607                 break;
1608
1609         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1610                 CHECK_NTTIMES_EQUAL(network_open_information.out.create_time);
1611                 CHECK_NTTIMES_EQUAL(network_open_information.out.access_time);
1612                 CHECK_NTTIMES_EQUAL(network_open_information.out.write_time);
1613                 CHECK_NTTIMES_EQUAL(network_open_information.out.change_time);
1614                 CHECK_EQUAL(network_open_information.out.alloc_size);
1615                 CHECK_EQUAL(network_open_information.out.size);
1616                 CHECK_EQUAL(network_open_information.out.attrib);
1617                 break;
1618
1619         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1620                 CHECK_EQUAL(attribute_tag_information.out.attrib);
1621                 CHECK_EQUAL(attribute_tag_information.out.reparse_tag);
1622                 break;
1623
1624                 /* Unhandled levels */
1625
1626         case RAW_FILEINFO_SEC_DESC:
1627         case RAW_FILEINFO_EA_LIST:
1628         case RAW_FILEINFO_UNIX_BASIC:
1629         case RAW_FILEINFO_UNIX_LINK:
1630                 break;
1631         }
1632
1633         return True;
1634 }
1635
1636 /*
1637   generate qpathinfo operations
1638 */
1639 static BOOL handler_qpathinfo(int instance)
1640 {
1641         union smb_fileinfo parm[NSERVERS];
1642         NTSTATUS status[NSERVERS];
1643
1644         parm[0].generic.file.path = gen_fname_open(instance);
1645
1646         gen_fileinfo(instance, &parm[0]);
1647
1648         GEN_COPY_PARM;
1649         GEN_CALL(smb_raw_pathinfo(tree, current_op.mem_ctx, &parm[i]));
1650
1651         return cmp_fileinfo(instance, parm, status);
1652 }
1653
1654 /*
1655   generate qfileinfo operations
1656 */
1657 static BOOL handler_qfileinfo(int instance)
1658 {
1659         union smb_fileinfo parm[NSERVERS];
1660         NTSTATUS status[NSERVERS];
1661
1662         parm[0].generic.file.fnum = gen_fnum(instance);
1663
1664         gen_fileinfo(instance, &parm[0]);
1665
1666         GEN_COPY_PARM;
1667         GEN_SET_FNUM(generic.file.fnum);
1668         GEN_CALL(smb_raw_fileinfo(tree, current_op.mem_ctx, &parm[i]));
1669
1670         return cmp_fileinfo(instance, parm, status);
1671 }
1672
1673
1674 /*
1675   generate a fileinfo query structure
1676 */
1677 static void gen_setfileinfo(int instance, union smb_setfileinfo *info)
1678 {
1679         int i;
1680         #undef LVL
1681         #define LVL(v) {RAW_SFILEINFO_ ## v, "RAW_SFILEINFO_" #v}
1682         struct {
1683                 enum smb_setfileinfo_level level;
1684                 const char *name;
1685         }  levels[] = {
1686 #if 0
1687                 /* disabled until win2003 can handle them ... */
1688                 LVL(EA_SET), LVL(BASIC_INFO), LVL(DISPOSITION_INFO), 
1689                 LVL(STANDARD), LVL(ALLOCATION_INFO), LVL(END_OF_FILE_INFO), 
1690 #endif
1691                 LVL(SETATTR), LVL(SETATTRE), LVL(BASIC_INFORMATION),
1692                 LVL(RENAME_INFORMATION), LVL(DISPOSITION_INFORMATION), 
1693                 LVL(POSITION_INFORMATION), LVL(MODE_INFORMATION),
1694                 LVL(ALLOCATION_INFORMATION), LVL(END_OF_FILE_INFORMATION), 
1695                 LVL(1023), LVL(1025), LVL(1029), LVL(1032), LVL(1039), LVL(1040)
1696         };
1697         do {
1698                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1699         } while (ignore_pattern(levels[i].name));
1700
1701         info->generic.level = levels[i].level;
1702
1703         switch (info->generic.level) {
1704         case RAW_SFILEINFO_SETATTR:
1705                 info->setattr.in.attrib = gen_attrib();
1706                 info->setattr.in.write_time = gen_timet();
1707                 break;
1708         case RAW_SFILEINFO_SETATTRE:
1709                 info->setattre.in.create_time = gen_timet();
1710                 info->setattre.in.access_time = gen_timet();
1711                 info->setattre.in.write_time = gen_timet();
1712                 break;
1713         case RAW_SFILEINFO_STANDARD:
1714                 info->standard.in.create_time = gen_timet();
1715                 info->standard.in.access_time = gen_timet();
1716                 info->standard.in.write_time = gen_timet();
1717                 break;
1718         case RAW_SFILEINFO_EA_SET: {
1719                 static struct ea_struct ea;
1720                 info->ea_set.in.num_eas = 1;
1721                 info->ea_set.in.eas = &ea;
1722                 info->ea_set.in.eas[0] = gen_ea_struct();
1723         }
1724                 break;
1725         case RAW_SFILEINFO_BASIC_INFO:
1726         case RAW_SFILEINFO_BASIC_INFORMATION:
1727                 info->basic_info.in.create_time = gen_nttime();
1728                 info->basic_info.in.access_time = gen_nttime();
1729                 info->basic_info.in.write_time = gen_nttime();
1730                 info->basic_info.in.change_time = gen_nttime();
1731                 info->basic_info.in.attrib = gen_attrib();
1732                 break;
1733         case RAW_SFILEINFO_DISPOSITION_INFO:
1734         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
1735                 info->disposition_info.in.delete_on_close = gen_bool();
1736                 break;
1737         case RAW_SFILEINFO_ALLOCATION_INFO:
1738         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
1739                 info->allocation_info.in.alloc_size = gen_alloc_size();
1740                 break;
1741         case RAW_SFILEINFO_END_OF_FILE_INFO:
1742         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
1743                 info->end_of_file_info.in.size = gen_offset();
1744                 break;
1745         case RAW_SFILEINFO_RENAME_INFORMATION:
1746                 info->rename_information.in.overwrite = gen_bool();
1747                 info->rename_information.in.root_fid = gen_root_fid(instance);
1748                 info->rename_information.in.new_name = gen_fname_open(instance);
1749                 break;
1750         case RAW_SFILEINFO_POSITION_INFORMATION:
1751                 info->position_information.in.position = gen_offset();
1752                 break;
1753         case RAW_SFILEINFO_MODE_INFORMATION:
1754                 info->mode_information.in.mode = gen_bits_mask(0xFFFFFFFF);
1755                 break;
1756         case RAW_SFILEINFO_GENERIC:
1757         case RAW_SFILEINFO_SEC_DESC:
1758         case RAW_SFILEINFO_UNIX_BASIC:
1759         case RAW_SFILEINFO_UNIX_LINK:
1760         case RAW_SFILEINFO_UNIX_HLINK:
1761         case RAW_SFILEINFO_1023:
1762         case RAW_SFILEINFO_1025:
1763         case RAW_SFILEINFO_1029:
1764         case RAW_SFILEINFO_1032:
1765         case RAW_SFILEINFO_1039:
1766         case RAW_SFILEINFO_1040:
1767                 /* Untested */
1768                 break;
1769         }
1770 }
1771
1772 /*
1773   generate setpathinfo operations
1774 */
1775 static BOOL handler_spathinfo(int instance)
1776 {
1777         union smb_setfileinfo parm[NSERVERS];
1778         NTSTATUS status[NSERVERS];
1779
1780         parm[0].generic.file.path = gen_fname_open(instance);
1781
1782         gen_setfileinfo(instance, &parm[0]);
1783
1784         GEN_COPY_PARM;
1785
1786         /* a special case for the fid in a RENAME */
1787         if (parm[0].generic.level == RAW_SFILEINFO_RENAME_INFORMATION &&
1788             parm[0].rename_information.in.root_fid != 0) {
1789                 GEN_SET_FNUM(rename_information.in.root_fid);
1790         }
1791
1792         GEN_CALL(smb_raw_setpathinfo(tree, &parm[i]));
1793
1794         return True;
1795 }
1796
1797
1798 /*
1799   generate setfileinfo operations
1800 */
1801 static BOOL handler_sfileinfo(int instance)
1802 {
1803         union smb_setfileinfo parm[NSERVERS];
1804         NTSTATUS status[NSERVERS];
1805
1806         parm[0].generic.file.fnum = gen_fnum(instance);
1807
1808         gen_setfileinfo(instance, &parm[0]);
1809
1810         GEN_COPY_PARM;
1811         GEN_SET_FNUM(generic.file.fnum);
1812         GEN_CALL(smb_raw_setfileinfo(tree, &parm[i]));
1813
1814         return True;
1815 }
1816
1817
1818 /*
1819   generate change notify operations
1820 */
1821 static BOOL handler_notify(int instance)
1822 {
1823         union smb_notify parm[NSERVERS];
1824         int n;
1825
1826         parm[0].notify.in.buffer_size = gen_io_count();
1827         parm[0].notify.in.completion_filter = gen_bits_mask(0xFF);
1828         parm[0].notify.file.fnum = gen_fnum(instance);
1829         parm[0].notify.in.recursive = gen_bool();
1830
1831         GEN_COPY_PARM;
1832         GEN_SET_FNUM(notify.file.fnum);
1833
1834         for (n=0;n<NSERVERS;n++) {
1835                 struct smbcli_request *req;
1836                 req = smb_raw_changenotify_send(servers[n].cli[instance]->tree, &parm[n]);
1837                 req->async.fn = async_notify;
1838         }
1839
1840         return True;
1841 }
1842
1843 /*
1844   wipe any relevant files
1845 */
1846 static void wipe_files(void)
1847 {
1848         int i;
1849         for (i=0;i<NSERVERS;i++) {
1850                 int n = smbcli_deltree(servers[i].cli[0]->tree, "\\gentest");
1851                 if (n == -1) {
1852                         printf("Failed to wipe tree on server %d\n", i);
1853                         exit(1);
1854                 }
1855                 if (NT_STATUS_IS_ERR(smbcli_mkdir(servers[i].cli[0]->tree, "\\gentest"))) {
1856                         printf("Failed to create \\gentest - %s\n",
1857                                smbcli_errstr(servers[i].cli[0]->tree));
1858                         exit(1);
1859                 }
1860                 if (n > 0) {
1861                         printf("Deleted %d files on server %d\n", n, i);
1862                 }
1863         }
1864 }
1865
1866 /*
1867   dump the current seeds - useful for continuing a backtrack
1868 */
1869 static void dump_seeds(void)
1870 {
1871         int i;
1872         FILE *f;
1873
1874         if (!options.seeds_file) {
1875                 return;
1876         }
1877         f = fopen("seeds.tmp", "w");
1878         if (!f) return;
1879
1880         for (i=0;i<options.numops;i++) {
1881                 fprintf(f, "%u\n", op_parms[i].seed);
1882         }
1883         fclose(f);
1884         rename("seeds.tmp", options.seeds_file);
1885 }
1886
1887
1888
1889 /*
1890   the list of top-level operations that we will generate
1891 */
1892 static struct {
1893         const char *name;
1894         BOOL (*handler)(int instance);
1895         int count, success_count;
1896 } gen_ops[] = {
1897         {"OPEN",       handler_open},
1898         {"OPENX",      handler_openx},
1899         {"NTCREATEX",  handler_ntcreatex},
1900         {"CLOSE",      handler_close},
1901         {"UNLINK",     handler_unlink},
1902         {"MKDIR",      handler_mkdir},
1903         {"RMDIR",      handler_rmdir},
1904         {"RENAME",     handler_rename},
1905         {"NTRENAME",   handler_ntrename},
1906         {"READX",      handler_readx},
1907         {"WRITEX",     handler_writex},
1908         {"CHKPATH",    handler_chkpath},
1909         {"LOCKINGX",   handler_lockingx},
1910         {"QPATHINFO",  handler_qpathinfo},
1911         {"QFILEINFO",  handler_qfileinfo},
1912         {"SPATHINFO",  handler_spathinfo},
1913         {"SFILEINFO",  handler_sfileinfo},
1914         {"NOTIFY",     handler_notify},
1915         {"SEEK",       handler_seek},
1916 };
1917
1918
1919 /*
1920   run the test with the current set of op_parms parameters
1921   return the number of operations that completed successfully
1922 */
1923 static int run_test(void)
1924 {
1925         int op, i;
1926
1927         if (!connect_servers()) {
1928                 printf("Failed to connect to servers\n");
1929                 exit(1);
1930         }
1931
1932         dump_seeds();
1933
1934         /* wipe any leftover files from old runs */
1935         wipe_files();
1936
1937         /* reset the open handles array */
1938         memset(open_handles, 0, options.max_open_handles * sizeof(open_handles[0]));
1939         num_open_handles = 0;
1940
1941         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1942                 gen_ops[i].count = 0;
1943                 gen_ops[i].success_count = 0;
1944         }
1945
1946         for (op=0; op<options.numops; op++) {
1947                 int instance, which_op;
1948                 BOOL ret;
1949
1950                 if (op_parms[op].disabled) continue;
1951
1952                 srandom(op_parms[op].seed);
1953
1954                 instance = gen_int_range(0, NINSTANCES-1);
1955
1956                 /* generate a non-ignored operation */
1957                 do {
1958                         which_op = gen_int_range(0, ARRAY_SIZE(gen_ops)-1);
1959                 } while (ignore_pattern(gen_ops[which_op].name));
1960
1961                 DEBUG(3,("Generating op %s on instance %d\n",
1962                          gen_ops[which_op].name, instance));
1963
1964                 current_op.seed = op_parms[op].seed;
1965                 current_op.opnum = op;
1966                 current_op.name = gen_ops[which_op].name;
1967                 current_op.status = NT_STATUS_OK;
1968                 current_op.mem_ctx = talloc_init("%s", current_op.name);
1969
1970                 ret = gen_ops[which_op].handler(instance);
1971
1972                 talloc_free(current_op.mem_ctx);
1973
1974                 gen_ops[which_op].count++;
1975                 if (NT_STATUS_IS_OK(current_op.status)) {
1976                         gen_ops[which_op].success_count++;                      
1977                 }
1978
1979                 if (!ret) {
1980                         printf("Failed at operation %d - %s\n",
1981                                op, gen_ops[which_op].name);
1982                         return op;
1983                 }
1984
1985                 if (op % 100 == 0) {
1986                         printf("%d\n", op);
1987                 }
1988         }
1989
1990         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1991                 printf("Op %-10s got %d/%d success\n", 
1992                        gen_ops[i].name,
1993                        gen_ops[i].success_count,
1994                        gen_ops[i].count);
1995         }
1996
1997         return op;
1998 }
1999
2000 /* 
2001    perform a backtracking analysis of the minimal set of operations
2002    to generate an error
2003 */
2004 static void backtrack_analyze(void)
2005 {
2006         int chunk, ret;
2007
2008         chunk = options.numops / 2;
2009
2010         do {
2011                 int base;
2012                 for (base=0; 
2013                      chunk > 0 && base+chunk < options.numops && options.numops > 1; ) {
2014                         int i, max;
2015
2016                         chunk = MIN(chunk, options.numops / 2);
2017
2018                         /* mark this range as disabled */
2019                         max = MIN(options.numops, base+chunk);
2020                         for (i=base;i<max; i++) {
2021                                 op_parms[i].disabled = True;
2022                         }
2023                         printf("Testing %d ops with %d-%d disabled\n", 
2024                                options.numops, base, max-1);
2025                         ret = run_test();
2026                         printf("Completed %d of %d ops\n", ret, options.numops);
2027                         for (i=base;i<max; i++) {
2028                                 op_parms[i].disabled = False;
2029                         }
2030                         if (ret == options.numops) {
2031                                 /* this chunk is needed */
2032                                 base += chunk;
2033                         } else if (ret < base) {
2034                                 printf("damn - inconsistent errors! found early error\n");
2035                                 options.numops = ret+1;
2036                                 base = 0;
2037                         } else {
2038                                 /* it failed - this chunk isn't needed for a failure */
2039                                 memmove(&op_parms[base], &op_parms[max], 
2040                                         sizeof(op_parms[0]) * (options.numops - max));
2041                                 options.numops = (ret+1) - (max - base);
2042                         }
2043                 }
2044
2045                 if (chunk == 2) {
2046                         chunk = 1;
2047                 } else {
2048                         chunk *= 0.4;
2049                 }
2050
2051                 if (options.analyze_continuous && chunk == 0 && options.numops != 1) {
2052                         chunk = 1;
2053                 }
2054         } while (chunk > 0);
2055
2056         printf("Reduced to %d ops\n", options.numops);
2057         ret = run_test();
2058         if (ret != options.numops - 1) {
2059                 printf("Inconsistent result? ret=%d numops=%d\n", ret, options.numops);
2060         }
2061 }
2062
2063 /* 
2064    start the main gentest process
2065 */
2066 static BOOL start_gentest(void)
2067 {
2068         int op;
2069         int ret;
2070
2071         /* allocate the open_handles array */
2072         open_handles = calloc(options.max_open_handles, sizeof(open_handles[0]));
2073
2074         srandom(options.seed);
2075         op_parms = calloc(options.numops, sizeof(op_parms[0]));
2076
2077         /* generate the seeds - after this everything is deterministic */
2078         if (options.use_preset_seeds) {
2079                 int numops;
2080                 char **preset = file_lines_load(options.seeds_file, &numops, NULL);
2081                 if (!preset) {
2082                         printf("Failed to load %s - %s\n", options.seeds_file, strerror(errno));
2083                         exit(1);
2084                 }
2085                 if (numops < options.numops) {
2086                         options.numops = numops;
2087                 }
2088                 for (op=0;op<options.numops;op++) {
2089                         if (!preset[op]) {
2090                                 printf("Not enough seeds in %s\n", options.seeds_file);
2091                                 exit(1);
2092                         }
2093                         op_parms[op].seed = atoi(preset[op]);
2094                 }
2095                 printf("Loaded %d seeds from %s\n", options.numops, options.seeds_file);
2096         } else {
2097                 for (op=0; op<options.numops; op++) {
2098                         op_parms[op].seed = random();
2099                 }
2100         }
2101
2102         ret = run_test();
2103
2104         if (ret != options.numops && options.analyze) {
2105                 options.numops = ret+1;
2106                 backtrack_analyze();
2107         } else if (options.analyze_always) {
2108                 backtrack_analyze();
2109         } else if (options.analyze_continuous) {
2110                 while (run_test() == options.numops) ;
2111         }
2112
2113         return ret == options.numops;
2114 }
2115
2116
2117 static void usage(void)
2118 {
2119         printf(
2120 "Usage:\n\
2121   gentest2 //server1/share1 //server2/share2 [options..]\n\
2122   options:\n\
2123         -U user%%pass        (can be specified twice)\n\
2124         -s seed\n\
2125         -o numops\n\
2126         -a            (show all ops)\n\
2127         -A            backtrack to find minimal ops\n\
2128         -i FILE       add a list of wildcard exclusions\n\
2129         -O            enable oplocks\n\
2130         -S FILE       set preset seeds file\n\
2131         -L            use preset seeds\n\
2132         -F            fast reconnect (just close files)\n\
2133         -C            continuous analysis mode\n\
2134         -X            analyse even when test OK\n\
2135 ");
2136 }
2137
2138 /****************************************************************************
2139   main program
2140 ****************************************************************************/
2141  int main(int argc, char *argv[])
2142 {
2143         int opt;
2144         int i, username_count=0;
2145         BOOL ret;
2146
2147         setlinebuf(stdout);
2148
2149         setup_logging("gentest", DEBUG_STDOUT);
2150
2151         if (argc < 3 || argv[1][0] == '-') {
2152                 usage();
2153                 exit(1);
2154         }
2155
2156         setup_logging(argv[0], DEBUG_STDOUT);
2157
2158         for (i=0;i<NSERVERS;i++) {
2159                 const char *share = argv[1+i];
2160                 servers[i].credentials = cli_credentials_init(NULL);
2161                 if (!split_unc_name(share, &servers[i].server_name, &servers[i].share_name)) {
2162                         printf("Invalid share name '%s'\n", share);
2163                         return -1;
2164                 }
2165         }
2166
2167         argc -= NSERVERS;
2168         argv += NSERVERS;
2169
2170         lp_load();
2171
2172         servers[0].credentials = cli_credentials_init(talloc_autofree_context());
2173         servers[1].credentials = cli_credentials_init(talloc_autofree_context());
2174         cli_credentials_guess(servers[0].credentials);
2175         cli_credentials_guess(servers[1].credentials);
2176
2177         options.seed = time(NULL);
2178         options.numops = 1000;
2179         options.max_open_handles = 20;
2180         options.seeds_file = "gentest_seeds.dat";
2181
2182         while ((opt = getopt(argc, argv, "U:s:o:ad:i:AOhS:LFXC")) != EOF) {
2183                 switch (opt) {
2184                 case 'U':
2185                         if (username_count == 2) {
2186                                 usage();
2187                                 exit(1);
2188                         }
2189                         cli_credentials_parse_string(servers[username_count].credentials, 
2190                                                      optarg, CRED_SPECIFIED);
2191                         username_count++;
2192                         break;
2193                 case 'd':
2194                         DEBUGLEVEL = atoi(optarg);
2195                         setup_logging(NULL, DEBUG_STDOUT);
2196                         break;
2197                 case 's':
2198                         options.seed = atoi(optarg);
2199                         break;
2200                 case 'S':
2201                         options.seeds_file = optarg;
2202                         break;
2203                 case 'L':
2204                         options.use_preset_seeds = True;
2205                         break;
2206                 case 'F':
2207                         options.fast_reconnect = True;
2208                         break;
2209                 case 'o':
2210                         options.numops = atoi(optarg);
2211                         break;
2212                 case 'O':
2213                         options.use_oplocks = True;
2214                         break;
2215                 case 'a':
2216                         options.showall = True;
2217                         break;
2218                 case 'A':
2219                         options.analyze = True;
2220                         break;
2221                 case 'X':
2222                         options.analyze_always = True;
2223                         break;
2224                 case 'C':
2225                         options.analyze_continuous = True;
2226                         break;
2227                 case 'i':
2228                         options.ignore_patterns = file_lines_load(optarg, NULL, NULL);
2229                         break;
2230                 case 'h':
2231                         usage();
2232                         exit(1);
2233                 default:
2234                         printf("Unknown option %c (%d)\n", (char)opt, opt);
2235                         exit(1);
2236                 }
2237         }
2238
2239         gensec_init();
2240
2241         if (username_count == 0) {
2242                 usage();
2243                 return -1;
2244         }
2245         if (username_count == 1) {
2246                 servers[1].credentials = servers[0].credentials;
2247         }
2248
2249         printf("seed=%u\n", options.seed);
2250
2251         ret = start_gentest();
2252
2253         if (ret) {
2254                 printf("gentest completed - no errors\n");
2255         } else {
2256                 printf("gentest failed\n");
2257         }
2258
2259         return ret?0:-1;
2260 }