r19598: Ahead of a merge to current lorikeet-heimdal:
[bbaumbach/samba-autobuild/.git] / source4 / torture / gentest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    generic testing tool
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "libcli/raw/request.h"
25 #include "libcli/libcli.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "librpc/gen_ndr/security.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.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 = labs(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         ZERO_STRUCT(ea);
661
662         do {
663                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
664         } while (ignore_pattern(names[i]));
665
666         ea.name.s = names[i];
667
668         do {
669                 i = gen_int_range(0, ARRAY_SIZE(values)-1);
670         } while (ignore_pattern(values[i]));
671
672         ea.value = data_blob(values[i], strlen(values[i]));
673
674         if (gen_chance(10)) ea.flags = gen_bits_mask(0xFF);
675         ea.flags = 0;
676
677         return ea;
678 }
679
680
681 /*
682   this is called when a change notify reply comes in
683 */
684 static void async_notify(struct smbcli_request *req)
685 {
686         union smb_notify notify;
687         NTSTATUS status;
688         int i, j;
689         uint16_t tid;
690         struct smbcli_transport *transport = req->transport;
691
692         tid = SVAL(req->in.hdr, HDR_TID);
693
694         notify.nttrans.level = RAW_NOTIFY_NTTRANS;
695         status = smb_raw_changenotify_recv(req, current_op.mem_ctx, &notify);
696         if (NT_STATUS_IS_OK(status)) {
697                 printf("notify tid=%d num_changes=%d action=%d name=%s\n", 
698                        tid, 
699                        notify.nttrans.out.num_changes,
700                        notify.nttrans.out.changes[0].action,
701                        notify.nttrans.out.changes[0].name.s);
702         }
703
704         for (i=0;i<NSERVERS;i++) {
705                 for (j=0;j<NINSTANCES;j++) {
706                         if (transport == servers[i].cli[j]->transport &&
707                             tid == servers[i].cli[j]->tree->tid) {
708                                 notifies[i][j].notify_count++;
709                                 notifies[i][j].status = status;
710                                 notifies[i][j].notify = notify;
711                         }
712                 }
713         }
714 }
715
716 static void oplock_handler_close_recv(struct smbcli_request *req)
717 {
718         NTSTATUS status;
719         status = smbcli_request_simple_recv(req);
720         if (!NT_STATUS_IS_OK(status)) {
721                 printf("close failed in oplock_handler\n");
722                 smb_panic("close failed in oplock_handler");
723         }
724 }
725
726 /*
727   the oplock handler will either ack the break or close the file
728 */
729 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private)
730 {
731         union smb_close io;
732         int i, j;
733         BOOL do_close;
734         struct smbcli_tree *tree = NULL;
735         struct smbcli_request *req;
736
737         srandom(current_op.seed);
738         do_close = gen_chance(50);
739
740         for (i=0;i<NSERVERS;i++) {
741                 for (j=0;j<NINSTANCES;j++) {
742                         if (transport == servers[i].cli[j]->transport &&
743                             tid == servers[i].cli[j]->tree->tid) {
744                                 oplocks[i][j].got_break = True;
745                                 oplocks[i][j].fnum = fnum;
746                                 oplocks[i][j].handle = fnum_to_handle(i, j, fnum);
747                                 oplocks[i][j].level = level;
748                                 oplocks[i][j].do_close = do_close;
749                                 tree = servers[i].cli[j]->tree;
750                         }
751                 }
752         }
753
754         if (!tree) {
755                 printf("Oplock break not for one of our trees!?\n");
756                 return False;
757         }
758
759         if (!do_close) {
760                 printf("oplock ack fnum=%d\n", fnum);
761                 return smbcli_oplock_ack(tree, fnum, level);
762         }
763
764         printf("oplock close fnum=%d\n", fnum);
765
766         io.close.level = RAW_CLOSE_CLOSE;
767         io.close.in.file.fnum = fnum;
768         io.close.in.write_time = 0;
769         req = smb_raw_close_send(tree, &io);
770
771         if (req == NULL) {
772                 printf("WARNING: close failed in oplock_handler_close\n");
773                 return False;
774         }
775
776         req->async.fn = oplock_handler_close_recv;
777         req->async.private = NULL;
778
779         return True;
780 }
781
782
783 /*
784   the idle function tries to cope with getting an oplock break on a connection, and
785   an operation on another connection blocking until that break is acked
786   we check for operations on all transports in the idle function
787 */
788 static void idle_func(struct smbcli_transport *transport, void *private)
789 {
790         int i, j;
791         for (i=0;i<NSERVERS;i++) {
792                 for (j=0;j<NINSTANCES;j++) {
793                         if (servers[i].cli[j] &&
794                             transport != servers[i].cli[j]->transport) {
795                                 smbcli_transport_process(servers[i].cli[j]->transport);
796                         }
797                 }
798         }
799
800 }
801
802
803 /*
804   compare NTSTATUS, using checking ignored patterns
805 */
806 static BOOL compare_status(NTSTATUS status1, NTSTATUS status2)
807 {
808         if (NT_STATUS_EQUAL(status1, status2)) return True;
809
810         /* one code being an error and the other OK is always an error */
811         if (NT_STATUS_IS_OK(status1) || NT_STATUS_IS_OK(status2)) return False;
812
813         /* if we are ignoring one of the status codes then consider this a match */
814         if (ignore_pattern(nt_errstr(status1)) ||
815             ignore_pattern(nt_errstr(status2))) {
816                 return True;
817         }
818         return False;
819 }
820
821
822 /*
823   check for pending packets on all connections
824 */
825 static void check_pending(void)
826 {
827         int i, j;
828
829         msleep(20);
830
831         for (j=0;j<NINSTANCES;j++) {
832                 for (i=0;i<NSERVERS;i++) {
833                         smbcli_transport_process(servers[i].cli[j]->transport);
834                 }
835         }       
836 }
837
838 /*
839   check that the same oplock breaks have been received by all instances
840 */
841 static BOOL check_oplocks(const char *call)
842 {
843         int i, j;
844         int tries = 0;
845
846 again:
847         check_pending();
848
849         for (j=0;j<NINSTANCES;j++) {
850                 for (i=1;i<NSERVERS;i++) {
851                         if (oplocks[0][j].got_break != oplocks[i][j].got_break ||
852                             oplocks[0][j].handle != oplocks[i][j].handle ||
853                             oplocks[0][j].level != oplocks[i][j].level) {
854                                 if (tries++ < 10) goto again;
855                                 printf("oplock break inconsistent - %d/%d/%d vs %d/%d/%d\n",
856                                        oplocks[0][j].got_break, 
857                                        oplocks[0][j].handle, 
858                                        oplocks[0][j].level, 
859                                        oplocks[i][j].got_break, 
860                                        oplocks[i][j].handle, 
861                                        oplocks[i][j].level);
862                                 return False;
863                         }
864                 }
865         }
866
867         /* if we got a break and closed then remove the handle */
868         for (j=0;j<NINSTANCES;j++) {
869                 if (oplocks[0][j].got_break &&
870                     oplocks[0][j].do_close) {
871                         uint16_t fnums[NSERVERS];
872                         for (i=0;i<NSERVERS;i++) {
873                                 fnums[i] = oplocks[i][j].fnum;
874                         }
875                         gen_remove_handle(j, fnums);
876                         break;
877                 }
878         }       
879         return True;
880 }
881
882
883 /*
884   check that the same change notify info has been received by all instances
885 */
886 static BOOL check_notifies(const char *call)
887 {
888         int i, j;
889         int tries = 0;
890
891 again:
892         check_pending();
893
894         for (j=0;j<NINSTANCES;j++) {
895                 for (i=1;i<NSERVERS;i++) {
896                         int n;
897                         union smb_notify not1, not2;
898
899                         if (notifies[0][j].notify_count != notifies[i][j].notify_count) {
900                                 if (tries++ < 10) goto again;
901                                 printf("Notify count inconsistent %d %d\n",
902                                        notifies[0][j].notify_count,
903                                        notifies[i][j].notify_count);
904                                 return False;
905                         }
906
907                         if (notifies[0][j].notify_count == 0) continue;
908
909                         if (!NT_STATUS_EQUAL(notifies[0][j].status,
910                                              notifies[i][j].status)) {
911                                 printf("Notify status mismatch - %s - %s\n",
912                                        nt_errstr(notifies[0][j].status),
913                                        nt_errstr(notifies[i][j].status));
914                                 return False;
915                         }
916
917                         if (!NT_STATUS_IS_OK(notifies[0][j].status)) {
918                                 continue;
919                         }
920
921                         not1 = notifies[0][j].notify;
922                         not2 = notifies[i][j].notify;
923
924                         for (n=0;n<not1.nttrans.out.num_changes;n++) {
925                                 if (not1.nttrans.out.changes[n].action != 
926                                     not2.nttrans.out.changes[n].action) {
927                                         printf("Notify action %d inconsistent %d %d\n", n,
928                                                not1.nttrans.out.changes[n].action,
929                                                not2.nttrans.out.changes[n].action);
930                                         return False;
931                                 }
932                                 if (strcmp(not1.nttrans.out.changes[n].name.s,
933                                            not2.nttrans.out.changes[n].name.s)) {
934                                         printf("Notify name %d inconsistent %s %s\n", n,
935                                                not1.nttrans.out.changes[n].name.s,
936                                                not2.nttrans.out.changes[n].name.s);
937                                         return False;
938                                 }
939                                 if (not1.nttrans.out.changes[n].name.private_length !=
940                                     not2.nttrans.out.changes[n].name.private_length) {
941                                         printf("Notify name length %d inconsistent %d %d\n", n,
942                                                not1.nttrans.out.changes[n].name.private_length,
943                                                not2.nttrans.out.changes[n].name.private_length);
944                                         return False;
945                                 }
946                         }
947                 }
948         }
949
950         ZERO_STRUCT(notifies);
951
952         return True;
953 }
954
955
956 #define GEN_COPY_PARM do { \
957         int i; \
958         for (i=1;i<NSERVERS;i++) { \
959                 parm[i] = parm[0]; \
960         } \
961 } while (0)
962
963 #define GEN_CALL(call) do { \
964         int i; \
965         ZERO_STRUCT(oplocks); \
966         ZERO_STRUCT(notifies); \
967         for (i=0;i<NSERVERS;i++) { \
968                 struct smbcli_tree *tree = servers[i].cli[instance]->tree; \
969                 status[i] = call; \
970         } \
971         current_op.status = status[0]; \
972         for (i=1;i<NSERVERS;i++) { \
973                 if (!compare_status(status[i], status[0])) { \
974                         printf("status different in %s - %s %s\n", #call, \
975                                nt_errstr(status[0]), nt_errstr(status[i])); \
976                         return False; \
977                 } \
978         } \
979         if (!check_oplocks(#call)) return False; \
980         if (!check_notifies(#call)) return False; \
981         if (!NT_STATUS_IS_OK(status[0])) { \
982                 return True; \
983         } \
984 } while(0)
985
986 #define ADD_HANDLE(name, field) do { \
987         uint16_t fnums[NSERVERS]; \
988         int i; \
989         for (i=0;i<NSERVERS;i++) { \
990                 fnums[i] = parm[i].field; \
991         } \
992         gen_add_handle(instance, name, fnums); \
993 } while(0)
994
995 #define REMOVE_HANDLE(field) do { \
996         uint16_t fnums[NSERVERS]; \
997         int i; \
998         for (i=0;i<NSERVERS;i++) { \
999                 fnums[i] = parm[i].field; \
1000         } \
1001         gen_remove_handle(instance, fnums); \
1002 } while(0)
1003
1004 #define GEN_SET_FNUM(field) do { \
1005         int i; \
1006         for (i=0;i<NSERVERS;i++) { \
1007                 parm[i].field = gen_lookup_fnum(i, parm[i].field); \
1008         } \
1009 } while(0)
1010
1011 #define CHECK_EQUAL(field) do { \
1012         if (parm[0].field != parm[1].field && !ignore_pattern(#field)) { \
1013                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
1014                        (int)parm[0].field, (int)parm[1].field); \
1015                 return False; \
1016         } \
1017 } while(0)
1018
1019 #define CHECK_WSTR_EQUAL(field) do { \
1020         if ((!parm[0].field.s && parm[1].field.s) || (parm[0].field.s && !parm[1].field.s)) { \
1021                 printf("%s is NULL!\n", #field); \
1022                 return False; \
1023         } \
1024         if (parm[0].field.s && strcmp(parm[0].field.s, parm[1].field.s) != 0 && !ignore_pattern(#field)) { \
1025                 printf("Mismatch in %s - %s %s\n", #field, \
1026                        parm[0].field.s, parm[1].field.s); \
1027                 return False; \
1028         } \
1029         CHECK_EQUAL(field.private_length); \
1030 } while(0)
1031
1032 #define CHECK_BLOB_EQUAL(field) do { \
1033         if (memcmp(parm[0].field.data, parm[1].field.data, parm[0].field.length) != 0 && !ignore_pattern(#field)) { \
1034                 printf("Mismatch in %s\n", #field); \
1035                 return False; \
1036         } \
1037         CHECK_EQUAL(field.length); \
1038 } while(0)
1039
1040 #define CHECK_TIMES_EQUAL(field) do { \
1041         if (labs(parm[0].field - parm[1].field) > time_skew() && \
1042             !ignore_pattern(#field)) { \
1043                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
1044                        (int)parm[0].field, (int)parm[1].field); \
1045                 return False; \
1046         } \
1047 } while(0)
1048
1049 #define CHECK_NTTIMES_EQUAL(field) do { \
1050         if (labs(nt_time_to_unix(parm[0].field) - \
1051                 nt_time_to_unix(parm[1].field)) > time_skew() && \
1052             !ignore_pattern(#field)) { \
1053                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
1054                        (int)nt_time_to_unix(parm[0].field), \
1055                        (int)nt_time_to_unix(parm[1].field)); \
1056                 return False; \
1057         } \
1058 } while(0)
1059
1060 /*
1061   generate openx operations
1062 */
1063 static BOOL handler_openx(int instance)
1064 {
1065         union smb_open parm[NSERVERS];
1066         NTSTATUS status[NSERVERS];
1067
1068         parm[0].openx.level = RAW_OPEN_OPENX;
1069         parm[0].openx.in.flags = gen_openx_flags();
1070         parm[0].openx.in.open_mode = gen_openx_mode();
1071         parm[0].openx.in.search_attrs = gen_attrib();
1072         parm[0].openx.in.file_attrs = gen_attrib();
1073         parm[0].openx.in.write_time = gen_timet();
1074         parm[0].openx.in.open_func = gen_openx_func();
1075         parm[0].openx.in.size = gen_io_count();
1076         parm[0].openx.in.timeout = gen_timeout();
1077         parm[0].openx.in.fname = gen_fname_open(instance);
1078
1079         if (!options.use_oplocks) {
1080                 /* mask out oplocks */
1081                 parm[0].openx.in.flags &= ~(OPENX_FLAGS_REQUEST_OPLOCK|
1082                                             OPENX_FLAGS_REQUEST_BATCH_OPLOCK);
1083         }
1084         
1085         GEN_COPY_PARM;
1086         GEN_CALL(smb_raw_open(tree, current_op.mem_ctx, &parm[i]));
1087
1088         CHECK_EQUAL(openx.out.attrib);
1089         CHECK_EQUAL(openx.out.size);
1090         CHECK_EQUAL(openx.out.access);
1091         CHECK_EQUAL(openx.out.ftype);
1092         CHECK_EQUAL(openx.out.devstate);
1093         CHECK_EQUAL(openx.out.action);
1094         CHECK_EQUAL(openx.out.access_mask);
1095         CHECK_EQUAL(openx.out.unknown);
1096         CHECK_TIMES_EQUAL(openx.out.write_time);
1097
1098         /* open creates a new file handle */
1099         ADD_HANDLE(parm[0].openx.in.fname, openx.out.file.fnum);
1100
1101         return True;
1102 }
1103
1104
1105 /*
1106   generate open operations
1107 */
1108 static BOOL handler_open(int instance)
1109 {
1110         union smb_open parm[NSERVERS];
1111         NTSTATUS status[NSERVERS];
1112
1113         parm[0].openold.level = RAW_OPEN_OPEN;
1114         parm[0].openold.in.open_mode = gen_bits_mask2(0xF, 0xFFFF);
1115         parm[0].openold.in.search_attrs = gen_attrib();
1116         parm[0].openold.in.fname = gen_fname_open(instance);
1117
1118         if (!options.use_oplocks) {
1119                 /* mask out oplocks */
1120                 parm[0].openold.in.open_mode &= ~(OPENX_FLAGS_REQUEST_OPLOCK|
1121                                                   OPENX_FLAGS_REQUEST_BATCH_OPLOCK);
1122         }
1123         
1124         GEN_COPY_PARM;
1125         GEN_CALL(smb_raw_open(tree, current_op.mem_ctx, &parm[i]));
1126
1127         CHECK_EQUAL(openold.out.attrib);
1128         CHECK_TIMES_EQUAL(openold.out.write_time);
1129         CHECK_EQUAL(openold.out.size);
1130         CHECK_EQUAL(openold.out.rmode);
1131
1132         /* open creates a new file handle */
1133         ADD_HANDLE(parm[0].openold.in.fname, openold.out.file.fnum);
1134
1135         return True;
1136 }
1137
1138
1139 /*
1140   generate ntcreatex operations
1141 */
1142 static BOOL handler_ntcreatex(int instance)
1143 {
1144         union smb_open parm[NSERVERS];
1145         NTSTATUS status[NSERVERS];
1146
1147         parm[0].ntcreatex.level = RAW_OPEN_NTCREATEX;
1148         parm[0].ntcreatex.in.flags = gen_ntcreatex_flags();
1149         parm[0].ntcreatex.in.root_fid = gen_root_fid(instance);
1150         parm[0].ntcreatex.in.access_mask = gen_access_mask();
1151         parm[0].ntcreatex.in.alloc_size = gen_alloc_size();
1152         parm[0].ntcreatex.in.file_attr = gen_attrib();
1153         parm[0].ntcreatex.in.share_access = gen_bits_mask2(0x7, 0xFFFFFFFF);
1154         parm[0].ntcreatex.in.open_disposition = gen_open_disp();
1155         parm[0].ntcreatex.in.create_options = gen_create_options();
1156         parm[0].ntcreatex.in.impersonation = gen_bits_mask2(0, 0xFFFFFFFF);
1157         parm[0].ntcreatex.in.security_flags = gen_bits_mask2(0, 0xFF);
1158         parm[0].ntcreatex.in.fname = gen_fname_open(instance);
1159
1160         if (!options.use_oplocks) {
1161                 /* mask out oplocks */
1162                 parm[0].ntcreatex.in.flags &= ~(NTCREATEX_FLAGS_REQUEST_OPLOCK|
1163                                                 NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK);
1164         }
1165         
1166         GEN_COPY_PARM;
1167         if (parm[0].ntcreatex.in.root_fid != 0) {
1168                 GEN_SET_FNUM(ntcreatex.in.root_fid);
1169         }
1170         GEN_CALL(smb_raw_open(tree, current_op.mem_ctx, &parm[i]));
1171
1172         CHECK_EQUAL(ntcreatex.out.oplock_level);
1173         CHECK_EQUAL(ntcreatex.out.create_action);
1174         CHECK_NTTIMES_EQUAL(ntcreatex.out.create_time);
1175         CHECK_NTTIMES_EQUAL(ntcreatex.out.access_time);
1176         CHECK_NTTIMES_EQUAL(ntcreatex.out.write_time);
1177         CHECK_NTTIMES_EQUAL(ntcreatex.out.change_time);
1178         CHECK_EQUAL(ntcreatex.out.attrib);
1179         CHECK_EQUAL(ntcreatex.out.alloc_size);
1180         CHECK_EQUAL(ntcreatex.out.size);
1181         CHECK_EQUAL(ntcreatex.out.file_type);
1182         CHECK_EQUAL(ntcreatex.out.ipc_state);
1183         CHECK_EQUAL(ntcreatex.out.is_directory);
1184
1185         /* ntcreatex creates a new file handle */
1186         ADD_HANDLE(parm[0].ntcreatex.in.fname, ntcreatex.out.file.fnum);
1187
1188         return True;
1189 }
1190
1191 /*
1192   generate close operations
1193 */
1194 static BOOL handler_close(int instance)
1195 {
1196         union smb_close parm[NSERVERS];
1197         NTSTATUS status[NSERVERS];
1198
1199         parm[0].close.level = RAW_CLOSE_CLOSE;
1200         parm[0].close.in.file.fnum = gen_fnum_close(instance);
1201         parm[0].close.in.write_time = gen_timet();
1202
1203         GEN_COPY_PARM;
1204         GEN_SET_FNUM(close.in.file.fnum);
1205         GEN_CALL(smb_raw_close(tree, &parm[i]));
1206
1207         REMOVE_HANDLE(close.in.file.fnum);
1208
1209         return True;
1210 }
1211
1212 /*
1213   generate unlink operations
1214 */
1215 static BOOL handler_unlink(int instance)
1216 {
1217         union smb_unlink parm[NSERVERS];
1218         NTSTATUS status[NSERVERS];
1219
1220         parm[0].unlink.in.pattern = gen_pattern();
1221         parm[0].unlink.in.attrib = gen_attrib();
1222
1223         GEN_COPY_PARM;
1224         GEN_CALL(smb_raw_unlink(tree, &parm[i]));
1225
1226         return True;
1227 }
1228
1229 /*
1230   generate chkpath operations
1231 */
1232 static BOOL handler_chkpath(int instance)
1233 {
1234         union smb_chkpath parm[NSERVERS];
1235         NTSTATUS status[NSERVERS];
1236
1237         parm[0].chkpath.in.path = gen_fname_open(instance);
1238
1239         GEN_COPY_PARM;
1240         GEN_CALL(smb_raw_chkpath(tree, &parm[i]));
1241
1242         return True;
1243 }
1244
1245 /*
1246   generate mkdir operations
1247 */
1248 static BOOL handler_mkdir(int instance)
1249 {
1250         union smb_mkdir parm[NSERVERS];
1251         NTSTATUS status[NSERVERS];
1252
1253         parm[0].mkdir.level = RAW_MKDIR_MKDIR;
1254         parm[0].mkdir.in.path = gen_fname_open(instance);
1255
1256         GEN_COPY_PARM;
1257         GEN_CALL(smb_raw_mkdir(tree, &parm[i]));
1258
1259         return True;
1260 }
1261
1262 /*
1263   generate rmdir operations
1264 */
1265 static BOOL handler_rmdir(int instance)
1266 {
1267         struct smb_rmdir parm[NSERVERS];
1268         NTSTATUS status[NSERVERS];
1269
1270         parm[0].in.path = gen_fname_open(instance);
1271
1272         GEN_COPY_PARM;
1273         GEN_CALL(smb_raw_rmdir(tree, &parm[i]));
1274
1275         return True;
1276 }
1277
1278 /*
1279   generate rename operations
1280 */
1281 static BOOL handler_rename(int instance)
1282 {
1283         union smb_rename parm[NSERVERS];
1284         NTSTATUS status[NSERVERS];
1285
1286         parm[0].generic.level = RAW_RENAME_RENAME;
1287         parm[0].rename.in.pattern1 = gen_pattern();
1288         parm[0].rename.in.pattern2 = gen_pattern();
1289         parm[0].rename.in.attrib = gen_attrib();
1290
1291         GEN_COPY_PARM;
1292         GEN_CALL(smb_raw_rename(tree, &parm[i]));
1293
1294         return True;
1295 }
1296
1297 /*
1298   generate ntrename operations
1299 */
1300 static BOOL handler_ntrename(int instance)
1301 {
1302         union smb_rename parm[NSERVERS];
1303         NTSTATUS status[NSERVERS];
1304
1305         parm[0].generic.level = RAW_RENAME_NTRENAME;
1306         parm[0].ntrename.in.old_name = gen_fname();
1307         parm[0].ntrename.in.new_name = gen_fname();
1308         parm[0].ntrename.in.attrib = gen_attrib();
1309         parm[0].ntrename.in.cluster_size = gen_bits_mask2(0, 0xFFFFFFF);
1310         parm[0].ntrename.in.flags = gen_rename_flags();
1311
1312         GEN_COPY_PARM;
1313         GEN_CALL(smb_raw_rename(tree, &parm[i]));
1314
1315         return True;
1316 }
1317
1318
1319 /*
1320   generate seek operations
1321 */
1322 static BOOL handler_seek(int instance)
1323 {
1324         union smb_seek parm[NSERVERS];
1325         NTSTATUS status[NSERVERS];
1326
1327         parm[0].lseek.in.file.fnum = gen_fnum(instance);
1328         parm[0].lseek.in.mode = gen_bits_mask2(0x3, 0xFFFF);
1329         parm[0].lseek.in.offset = gen_offset();
1330
1331         GEN_COPY_PARM;
1332         GEN_SET_FNUM(lseek.in.file.fnum);
1333         GEN_CALL(smb_raw_seek(tree, &parm[i]));
1334
1335         CHECK_EQUAL(lseek.out.offset);
1336
1337         return True;
1338 }
1339
1340
1341 /*
1342   generate readx operations
1343 */
1344 static BOOL handler_readx(int instance)
1345 {
1346         union smb_read parm[NSERVERS];
1347         NTSTATUS status[NSERVERS];
1348
1349         parm[0].readx.level = RAW_READ_READX;
1350         parm[0].readx.in.file.fnum = gen_fnum(instance);
1351         parm[0].readx.in.offset = gen_offset();
1352         parm[0].readx.in.mincnt = gen_io_count();
1353         parm[0].readx.in.maxcnt = gen_io_count();
1354         parm[0].readx.in.remaining = gen_io_count();
1355         parm[0].readx.in.read_for_execute = gen_bool();
1356         parm[0].readx.out.data = talloc_size(current_op.mem_ctx,
1357                                              MAX(parm[0].readx.in.mincnt, parm[0].readx.in.maxcnt));
1358
1359         GEN_COPY_PARM;
1360         GEN_SET_FNUM(readx.in.file.fnum);
1361         GEN_CALL(smb_raw_read(tree, &parm[i]));
1362
1363         CHECK_EQUAL(readx.out.remaining);
1364         CHECK_EQUAL(readx.out.compaction_mode);
1365         CHECK_EQUAL(readx.out.nread);
1366
1367         return True;
1368 }
1369
1370 /*
1371   generate writex operations
1372 */
1373 static BOOL handler_writex(int instance)
1374 {
1375         union smb_write parm[NSERVERS];
1376         NTSTATUS status[NSERVERS];
1377
1378         parm[0].writex.level = RAW_WRITE_WRITEX;
1379         parm[0].writex.in.file.fnum = gen_fnum(instance);
1380         parm[0].writex.in.offset = gen_offset();
1381         parm[0].writex.in.wmode = gen_bits_mask(0xFFFF);
1382         parm[0].writex.in.remaining = gen_io_count();
1383         parm[0].writex.in.count = gen_io_count();
1384         parm[0].writex.in.data = talloc_zero_size(current_op.mem_ctx, parm[0].writex.in.count);
1385
1386         GEN_COPY_PARM;
1387         GEN_SET_FNUM(writex.in.file.fnum);
1388         GEN_CALL(smb_raw_write(tree, &parm[i]));
1389
1390         CHECK_EQUAL(writex.out.nwritten);
1391         CHECK_EQUAL(writex.out.remaining);
1392
1393         return True;
1394 }
1395
1396 /*
1397   generate lockingx operations
1398 */
1399 static BOOL handler_lockingx(int instance)
1400 {
1401         union smb_lock parm[NSERVERS];
1402         NTSTATUS status[NSERVERS];
1403         int n, nlocks;
1404
1405         parm[0].lockx.level = RAW_LOCK_LOCKX;
1406         parm[0].lockx.in.file.fnum = gen_fnum(instance);
1407         parm[0].lockx.in.mode = gen_lock_mode();
1408         parm[0].lockx.in.timeout = gen_timeout();
1409         do {
1410                 /* make sure we don't accidentially generate an oplock
1411                    break ack - otherwise the server can just block forever */
1412                 parm[0].lockx.in.ulock_cnt = gen_lock_count();
1413                 parm[0].lockx.in.lock_cnt = gen_lock_count();
1414                 nlocks = parm[0].lockx.in.ulock_cnt + parm[0].lockx.in.lock_cnt;
1415         } while (nlocks == 0);
1416
1417         if (nlocks > 0) {
1418                 parm[0].lockx.in.locks = talloc_array(current_op.mem_ctx,
1419                                                         struct smb_lock_entry,
1420                                                         nlocks);
1421                 for (n=0;n<nlocks;n++) {
1422                         parm[0].lockx.in.locks[n].pid = gen_pid();
1423                         parm[0].lockx.in.locks[n].offset = gen_offset();
1424                         parm[0].lockx.in.locks[n].count = gen_io_count();
1425                 }
1426         }
1427
1428         GEN_COPY_PARM;
1429         GEN_SET_FNUM(lockx.in.file.fnum);
1430         GEN_CALL(smb_raw_lock(tree, &parm[i]));
1431
1432         return True;
1433 }
1434
1435 /*
1436   generate a fileinfo query structure
1437 */
1438 static void gen_fileinfo(int instance, union smb_fileinfo *info)
1439 {
1440         int i;
1441         #define LVL(v) {RAW_FILEINFO_ ## v, "RAW_FILEINFO_" #v}
1442         struct {
1443                 enum smb_fileinfo_level level;
1444                 const char *name;
1445         }  levels[] = {
1446                 LVL(GETATTR), LVL(GETATTRE), LVL(STANDARD),
1447                 LVL(EA_SIZE), LVL(ALL_EAS), LVL(IS_NAME_VALID),
1448                 LVL(BASIC_INFO), LVL(STANDARD_INFO), LVL(EA_INFO),
1449                 LVL(NAME_INFO), LVL(ALL_INFO), LVL(ALT_NAME_INFO),
1450                 LVL(STREAM_INFO), LVL(COMPRESSION_INFO), LVL(BASIC_INFORMATION),
1451                 LVL(STANDARD_INFORMATION), LVL(INTERNAL_INFORMATION), LVL(EA_INFORMATION),
1452                 LVL(ACCESS_INFORMATION), LVL(NAME_INFORMATION), LVL(POSITION_INFORMATION),
1453                 LVL(MODE_INFORMATION), LVL(ALIGNMENT_INFORMATION), LVL(ALL_INFORMATION),
1454                 LVL(ALT_NAME_INFORMATION), LVL(STREAM_INFORMATION), LVL(COMPRESSION_INFORMATION),
1455                 LVL(NETWORK_OPEN_INFORMATION), LVL(ATTRIBUTE_TAG_INFORMATION)
1456         };
1457         do {
1458                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1459         } while (ignore_pattern(levels[i].name));
1460
1461         info->generic.level = levels[i].level;
1462 }
1463
1464 /*
1465   compare returned fileinfo structures
1466 */
1467 static BOOL cmp_fileinfo(int instance, 
1468                          union smb_fileinfo parm[NSERVERS],
1469                          NTSTATUS status[NSERVERS])
1470 {
1471         int i;
1472
1473         switch (parm[0].generic.level) {
1474         case RAW_FILEINFO_GENERIC:
1475                 return False;
1476
1477         case RAW_FILEINFO_GETATTR:
1478                 CHECK_EQUAL(getattr.out.attrib);
1479                 CHECK_EQUAL(getattr.out.size);
1480                 CHECK_TIMES_EQUAL(getattr.out.write_time);
1481                 break;
1482
1483         case RAW_FILEINFO_GETATTRE:
1484                 CHECK_TIMES_EQUAL(getattre.out.create_time);
1485                 CHECK_TIMES_EQUAL(getattre.out.access_time);
1486                 CHECK_TIMES_EQUAL(getattre.out.write_time);
1487                 CHECK_EQUAL(getattre.out.size);
1488                 CHECK_EQUAL(getattre.out.alloc_size);
1489                 CHECK_EQUAL(getattre.out.attrib);
1490                 break;
1491
1492         case RAW_FILEINFO_STANDARD:
1493                 CHECK_TIMES_EQUAL(standard.out.create_time);
1494                 CHECK_TIMES_EQUAL(standard.out.access_time);
1495                 CHECK_TIMES_EQUAL(standard.out.write_time);
1496                 CHECK_EQUAL(standard.out.size);
1497                 CHECK_EQUAL(standard.out.alloc_size);
1498                 CHECK_EQUAL(standard.out.attrib);
1499                 break;
1500
1501         case RAW_FILEINFO_EA_SIZE:
1502                 CHECK_TIMES_EQUAL(ea_size.out.create_time);
1503                 CHECK_TIMES_EQUAL(ea_size.out.access_time);
1504                 CHECK_TIMES_EQUAL(ea_size.out.write_time);
1505                 CHECK_EQUAL(ea_size.out.size);
1506                 CHECK_EQUAL(ea_size.out.alloc_size);
1507                 CHECK_EQUAL(ea_size.out.attrib);
1508                 CHECK_EQUAL(ea_size.out.ea_size);
1509                 break;
1510
1511         case RAW_FILEINFO_ALL_EAS:
1512                 CHECK_EQUAL(all_eas.out.num_eas);
1513                 for (i=0;i<parm[0].all_eas.out.num_eas;i++) {
1514                         CHECK_EQUAL(all_eas.out.eas[i].flags);
1515                         CHECK_WSTR_EQUAL(all_eas.out.eas[i].name);
1516                         CHECK_BLOB_EQUAL(all_eas.out.eas[i].value);
1517                 }
1518                 break;
1519
1520         case RAW_FILEINFO_IS_NAME_VALID:
1521                 break;
1522                 
1523         case RAW_FILEINFO_BASIC_INFO:
1524         case RAW_FILEINFO_BASIC_INFORMATION:
1525                 CHECK_NTTIMES_EQUAL(basic_info.out.create_time);
1526                 CHECK_NTTIMES_EQUAL(basic_info.out.access_time);
1527                 CHECK_NTTIMES_EQUAL(basic_info.out.write_time);
1528                 CHECK_NTTIMES_EQUAL(basic_info.out.change_time);
1529                 CHECK_EQUAL(basic_info.out.attrib);
1530                 break;
1531
1532         case RAW_FILEINFO_STANDARD_INFO:
1533         case RAW_FILEINFO_STANDARD_INFORMATION:
1534                 CHECK_EQUAL(standard_info.out.alloc_size);
1535                 CHECK_EQUAL(standard_info.out.size);
1536                 CHECK_EQUAL(standard_info.out.nlink);
1537                 CHECK_EQUAL(standard_info.out.delete_pending);
1538                 CHECK_EQUAL(standard_info.out.directory);
1539                 break;
1540
1541         case RAW_FILEINFO_EA_INFO:
1542         case RAW_FILEINFO_EA_INFORMATION:
1543                 CHECK_EQUAL(ea_info.out.ea_size);
1544                 break;
1545
1546         case RAW_FILEINFO_NAME_INFO:
1547         case RAW_FILEINFO_NAME_INFORMATION:
1548                 CHECK_WSTR_EQUAL(name_info.out.fname);
1549                 break;
1550
1551         case RAW_FILEINFO_ALL_INFO:
1552         case RAW_FILEINFO_ALL_INFORMATION:
1553                 CHECK_NTTIMES_EQUAL(all_info.out.create_time);
1554                 CHECK_NTTIMES_EQUAL(all_info.out.access_time);
1555                 CHECK_NTTIMES_EQUAL(all_info.out.write_time);
1556                 CHECK_NTTIMES_EQUAL(all_info.out.change_time);
1557                 CHECK_EQUAL(all_info.out.attrib);
1558                 CHECK_EQUAL(all_info.out.alloc_size);
1559                 CHECK_EQUAL(all_info.out.size);
1560                 CHECK_EQUAL(all_info.out.nlink);
1561                 CHECK_EQUAL(all_info.out.delete_pending);
1562                 CHECK_EQUAL(all_info.out.directory);
1563                 CHECK_EQUAL(all_info.out.ea_size);
1564                 CHECK_WSTR_EQUAL(all_info.out.fname);
1565                 break;
1566
1567         case RAW_FILEINFO_ALT_NAME_INFO:
1568         case RAW_FILEINFO_ALT_NAME_INFORMATION:
1569                 CHECK_WSTR_EQUAL(alt_name_info.out.fname);
1570                 break;
1571
1572         case RAW_FILEINFO_STREAM_INFO:
1573         case RAW_FILEINFO_STREAM_INFORMATION:
1574                 CHECK_EQUAL(stream_info.out.num_streams);
1575                 for (i=0;i<parm[0].stream_info.out.num_streams;i++) {
1576                         CHECK_EQUAL(stream_info.out.streams[i].size);
1577                         CHECK_EQUAL(stream_info.out.streams[i].alloc_size);
1578                         CHECK_WSTR_EQUAL(stream_info.out.streams[i].stream_name);
1579                 }
1580                 break;
1581
1582         case RAW_FILEINFO_COMPRESSION_INFO:
1583         case RAW_FILEINFO_COMPRESSION_INFORMATION:
1584                 CHECK_EQUAL(compression_info.out.compressed_size);
1585                 CHECK_EQUAL(compression_info.out.format);
1586                 CHECK_EQUAL(compression_info.out.unit_shift);
1587                 CHECK_EQUAL(compression_info.out.chunk_shift);
1588                 CHECK_EQUAL(compression_info.out.cluster_shift);
1589                 break;
1590
1591         case RAW_FILEINFO_INTERNAL_INFORMATION:
1592                 CHECK_EQUAL(internal_information.out.file_id);
1593                 break;
1594
1595         case RAW_FILEINFO_ACCESS_INFORMATION:
1596                 CHECK_EQUAL(access_information.out.access_flags);
1597                 break;
1598
1599         case RAW_FILEINFO_POSITION_INFORMATION:
1600                 CHECK_EQUAL(position_information.out.position);
1601                 break;
1602
1603         case RAW_FILEINFO_MODE_INFORMATION:
1604                 CHECK_EQUAL(mode_information.out.mode);
1605                 break;
1606
1607         case RAW_FILEINFO_ALIGNMENT_INFORMATION:
1608                 CHECK_EQUAL(alignment_information.out.alignment_requirement);
1609                 break;
1610
1611         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1612                 CHECK_NTTIMES_EQUAL(network_open_information.out.create_time);
1613                 CHECK_NTTIMES_EQUAL(network_open_information.out.access_time);
1614                 CHECK_NTTIMES_EQUAL(network_open_information.out.write_time);
1615                 CHECK_NTTIMES_EQUAL(network_open_information.out.change_time);
1616                 CHECK_EQUAL(network_open_information.out.alloc_size);
1617                 CHECK_EQUAL(network_open_information.out.size);
1618                 CHECK_EQUAL(network_open_information.out.attrib);
1619                 break;
1620
1621         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1622                 CHECK_EQUAL(attribute_tag_information.out.attrib);
1623                 CHECK_EQUAL(attribute_tag_information.out.reparse_tag);
1624                 break;
1625
1626                 /* Unhandled levels */
1627
1628         case RAW_FILEINFO_SEC_DESC:
1629         case RAW_FILEINFO_EA_LIST:
1630         case RAW_FILEINFO_UNIX_BASIC:
1631         case RAW_FILEINFO_UNIX_LINK:
1632         case RAW_FILEINFO_SMB2_ALL_EAS:
1633         case RAW_FILEINFO_SMB2_ALL_INFORMATION: 
1634                 break;
1635         }
1636
1637         return True;
1638 }
1639
1640 /*
1641   generate qpathinfo operations
1642 */
1643 static BOOL handler_qpathinfo(int instance)
1644 {
1645         union smb_fileinfo parm[NSERVERS];
1646         NTSTATUS status[NSERVERS];
1647
1648         parm[0].generic.in.file.path = gen_fname_open(instance);
1649
1650         gen_fileinfo(instance, &parm[0]);
1651
1652         GEN_COPY_PARM;
1653         GEN_CALL(smb_raw_pathinfo(tree, current_op.mem_ctx, &parm[i]));
1654
1655         return cmp_fileinfo(instance, parm, status);
1656 }
1657
1658 /*
1659   generate qfileinfo operations
1660 */
1661 static BOOL handler_qfileinfo(int instance)
1662 {
1663         union smb_fileinfo parm[NSERVERS];
1664         NTSTATUS status[NSERVERS];
1665
1666         parm[0].generic.in.file.fnum = gen_fnum(instance);
1667
1668         gen_fileinfo(instance, &parm[0]);
1669
1670         GEN_COPY_PARM;
1671         GEN_SET_FNUM(generic.in.file.fnum);
1672         GEN_CALL(smb_raw_fileinfo(tree, current_op.mem_ctx, &parm[i]));
1673
1674         return cmp_fileinfo(instance, parm, status);
1675 }
1676
1677
1678 /*
1679   generate a fileinfo query structure
1680 */
1681 static void gen_setfileinfo(int instance, union smb_setfileinfo *info)
1682 {
1683         int i;
1684         #undef LVL
1685         #define LVL(v) {RAW_SFILEINFO_ ## v, "RAW_SFILEINFO_" #v}
1686         struct {
1687                 enum smb_setfileinfo_level level;
1688                 const char *name;
1689         }  levels[] = {
1690 #if 0
1691                 /* disabled until win2003 can handle them ... */
1692                 LVL(EA_SET), LVL(BASIC_INFO), LVL(DISPOSITION_INFO), 
1693                 LVL(STANDARD), LVL(ALLOCATION_INFO), LVL(END_OF_FILE_INFO), 
1694 #endif
1695                 LVL(SETATTR), LVL(SETATTRE), LVL(BASIC_INFORMATION),
1696                 LVL(RENAME_INFORMATION), LVL(DISPOSITION_INFORMATION), 
1697                 LVL(POSITION_INFORMATION), LVL(MODE_INFORMATION),
1698                 LVL(ALLOCATION_INFORMATION), LVL(END_OF_FILE_INFORMATION), 
1699                 LVL(1023), LVL(1025), LVL(1029), LVL(1032), LVL(1039), LVL(1040)
1700         };
1701         do {
1702                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1703         } while (ignore_pattern(levels[i].name));
1704
1705         info->generic.level = levels[i].level;
1706
1707         switch (info->generic.level) {
1708         case RAW_SFILEINFO_SETATTR:
1709                 info->setattr.in.attrib = gen_attrib();
1710                 info->setattr.in.write_time = gen_timet();
1711                 break;
1712         case RAW_SFILEINFO_SETATTRE:
1713                 info->setattre.in.create_time = gen_timet();
1714                 info->setattre.in.access_time = gen_timet();
1715                 info->setattre.in.write_time = gen_timet();
1716                 break;
1717         case RAW_SFILEINFO_STANDARD:
1718                 info->standard.in.create_time = gen_timet();
1719                 info->standard.in.access_time = gen_timet();
1720                 info->standard.in.write_time = gen_timet();
1721                 break;
1722         case RAW_SFILEINFO_EA_SET: {
1723                 static struct ea_struct ea;
1724                 info->ea_set.in.num_eas = 1;
1725                 info->ea_set.in.eas = &ea;
1726                 info->ea_set.in.eas[0] = gen_ea_struct();
1727         }
1728                 break;
1729         case RAW_SFILEINFO_BASIC_INFO:
1730         case RAW_SFILEINFO_BASIC_INFORMATION:
1731                 info->basic_info.in.create_time = gen_nttime();
1732                 info->basic_info.in.access_time = gen_nttime();
1733                 info->basic_info.in.write_time = gen_nttime();
1734                 info->basic_info.in.change_time = gen_nttime();
1735                 info->basic_info.in.attrib = gen_attrib();
1736                 break;
1737         case RAW_SFILEINFO_DISPOSITION_INFO:
1738         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
1739                 info->disposition_info.in.delete_on_close = gen_bool();
1740                 break;
1741         case RAW_SFILEINFO_ALLOCATION_INFO:
1742         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
1743                 info->allocation_info.in.alloc_size = gen_alloc_size();
1744                 break;
1745         case RAW_SFILEINFO_END_OF_FILE_INFO:
1746         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
1747                 info->end_of_file_info.in.size = gen_offset();
1748                 break;
1749         case RAW_SFILEINFO_RENAME_INFORMATION:
1750                 info->rename_information.in.overwrite = gen_bool();
1751                 info->rename_information.in.root_fid = gen_root_fid(instance);
1752                 info->rename_information.in.new_name = gen_fname_open(instance);
1753                 break;
1754         case RAW_SFILEINFO_POSITION_INFORMATION:
1755                 info->position_information.in.position = gen_offset();
1756                 break;
1757         case RAW_SFILEINFO_MODE_INFORMATION:
1758                 info->mode_information.in.mode = gen_bits_mask(0xFFFFFFFF);
1759                 break;
1760         case RAW_SFILEINFO_GENERIC:
1761         case RAW_SFILEINFO_SEC_DESC:
1762         case RAW_SFILEINFO_UNIX_BASIC:
1763         case RAW_SFILEINFO_UNIX_LINK:
1764         case RAW_SFILEINFO_UNIX_HLINK:
1765         case RAW_SFILEINFO_1023:
1766         case RAW_SFILEINFO_1025:
1767         case RAW_SFILEINFO_1029:
1768         case RAW_SFILEINFO_1032:
1769         case RAW_SFILEINFO_1039:
1770         case RAW_SFILEINFO_1040:
1771                 /* Untested */
1772                 break;
1773         }
1774 }
1775
1776 /*
1777   generate setpathinfo operations
1778 */
1779 static BOOL handler_spathinfo(int instance)
1780 {
1781         union smb_setfileinfo parm[NSERVERS];
1782         NTSTATUS status[NSERVERS];
1783
1784         parm[0].generic.in.file.path = gen_fname_open(instance);
1785
1786         gen_setfileinfo(instance, &parm[0]);
1787
1788         GEN_COPY_PARM;
1789
1790         /* a special case for the fid in a RENAME */
1791         if (parm[0].generic.level == RAW_SFILEINFO_RENAME_INFORMATION &&
1792             parm[0].rename_information.in.root_fid != 0) {
1793                 GEN_SET_FNUM(rename_information.in.root_fid);
1794         }
1795
1796         GEN_CALL(smb_raw_setpathinfo(tree, &parm[i]));
1797
1798         return True;
1799 }
1800
1801
1802 /*
1803   generate setfileinfo operations
1804 */
1805 static BOOL handler_sfileinfo(int instance)
1806 {
1807         union smb_setfileinfo parm[NSERVERS];
1808         NTSTATUS status[NSERVERS];
1809
1810         parm[0].generic.in.file.fnum = gen_fnum(instance);
1811
1812         gen_setfileinfo(instance, &parm[0]);
1813
1814         GEN_COPY_PARM;
1815         GEN_SET_FNUM(generic.in.file.fnum);
1816         GEN_CALL(smb_raw_setfileinfo(tree, &parm[i]));
1817
1818         return True;
1819 }
1820
1821
1822 /*
1823   generate change notify operations
1824 */
1825 static BOOL handler_notify(int instance)
1826 {
1827         union smb_notify parm[NSERVERS];
1828         int n;
1829
1830         ZERO_STRUCT(parm[0]);
1831         parm[0].nttrans.level                   = RAW_NOTIFY_NTTRANS;
1832         parm[0].nttrans.in.buffer_size          = gen_io_count();
1833         parm[0].nttrans.in.completion_filter    = gen_bits_mask(0xFF);
1834         parm[0].nttrans.in.file.fnum            = gen_fnum(instance);
1835         parm[0].nttrans.in.recursive            = gen_bool();
1836
1837         GEN_COPY_PARM;
1838         GEN_SET_FNUM(nttrans.in.file.fnum);
1839
1840         for (n=0;n<NSERVERS;n++) {
1841                 struct smbcli_request *req;
1842                 req = smb_raw_changenotify_send(servers[n].cli[instance]->tree, &parm[n]);
1843                 req->async.fn = async_notify;
1844         }
1845
1846         return True;
1847 }
1848
1849 /*
1850   wipe any relevant files
1851 */
1852 static void wipe_files(void)
1853 {
1854         int i;
1855         for (i=0;i<NSERVERS;i++) {
1856                 int n = smbcli_deltree(servers[i].cli[0]->tree, "\\gentest");
1857                 if (n == -1) {
1858                         printf("Failed to wipe tree on server %d\n", i);
1859                         exit(1);
1860                 }
1861                 if (NT_STATUS_IS_ERR(smbcli_mkdir(servers[i].cli[0]->tree, "\\gentest"))) {
1862                         printf("Failed to create \\gentest - %s\n",
1863                                smbcli_errstr(servers[i].cli[0]->tree));
1864                         exit(1);
1865                 }
1866                 if (n > 0) {
1867                         printf("Deleted %d files on server %d\n", n, i);
1868                 }
1869         }
1870 }
1871
1872 /*
1873   dump the current seeds - useful for continuing a backtrack
1874 */
1875 static void dump_seeds(void)
1876 {
1877         int i;
1878         FILE *f;
1879
1880         if (!options.seeds_file) {
1881                 return;
1882         }
1883         f = fopen("seeds.tmp", "w");
1884         if (!f) return;
1885
1886         for (i=0;i<options.numops;i++) {
1887                 fprintf(f, "%u\n", op_parms[i].seed);
1888         }
1889         fclose(f);
1890         rename("seeds.tmp", options.seeds_file);
1891 }
1892
1893
1894
1895 /*
1896   the list of top-level operations that we will generate
1897 */
1898 static struct {
1899         const char *name;
1900         BOOL (*handler)(int instance);
1901         int count, success_count;
1902 } gen_ops[] = {
1903         {"OPEN",       handler_open},
1904         {"OPENX",      handler_openx},
1905         {"NTCREATEX",  handler_ntcreatex},
1906         {"CLOSE",      handler_close},
1907         {"UNLINK",     handler_unlink},
1908         {"MKDIR",      handler_mkdir},
1909         {"RMDIR",      handler_rmdir},
1910         {"RENAME",     handler_rename},
1911         {"NTRENAME",   handler_ntrename},
1912         {"READX",      handler_readx},
1913         {"WRITEX",     handler_writex},
1914         {"CHKPATH",    handler_chkpath},
1915         {"LOCKINGX",   handler_lockingx},
1916         {"QPATHINFO",  handler_qpathinfo},
1917         {"QFILEINFO",  handler_qfileinfo},
1918         {"SPATHINFO",  handler_spathinfo},
1919         {"SFILEINFO",  handler_sfileinfo},
1920         {"NOTIFY",     handler_notify},
1921         {"SEEK",       handler_seek},
1922 };
1923
1924
1925 /*
1926   run the test with the current set of op_parms parameters
1927   return the number of operations that completed successfully
1928 */
1929 static int run_test(void)
1930 {
1931         int op, i;
1932
1933         if (!connect_servers()) {
1934                 printf("Failed to connect to servers\n");
1935                 exit(1);
1936         }
1937
1938         dump_seeds();
1939
1940         /* wipe any leftover files from old runs */
1941         wipe_files();
1942
1943         /* reset the open handles array */
1944         memset(open_handles, 0, options.max_open_handles * sizeof(open_handles[0]));
1945         num_open_handles = 0;
1946
1947         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1948                 gen_ops[i].count = 0;
1949                 gen_ops[i].success_count = 0;
1950         }
1951
1952         for (op=0; op<options.numops; op++) {
1953                 int instance, which_op;
1954                 BOOL ret;
1955
1956                 if (op_parms[op].disabled) continue;
1957
1958                 srandom(op_parms[op].seed);
1959
1960                 instance = gen_int_range(0, NINSTANCES-1);
1961
1962                 /* generate a non-ignored operation */
1963                 do {
1964                         which_op = gen_int_range(0, ARRAY_SIZE(gen_ops)-1);
1965                 } while (ignore_pattern(gen_ops[which_op].name));
1966
1967                 DEBUG(3,("Generating op %s on instance %d\n",
1968                          gen_ops[which_op].name, instance));
1969
1970                 current_op.seed = op_parms[op].seed;
1971                 current_op.opnum = op;
1972                 current_op.name = gen_ops[which_op].name;
1973                 current_op.status = NT_STATUS_OK;
1974                 current_op.mem_ctx = talloc_named(NULL, 0, "%s", current_op.name);
1975
1976                 ret = gen_ops[which_op].handler(instance);
1977
1978                 talloc_free(current_op.mem_ctx);
1979
1980                 gen_ops[which_op].count++;
1981                 if (NT_STATUS_IS_OK(current_op.status)) {
1982                         gen_ops[which_op].success_count++;                      
1983                 }
1984
1985                 if (!ret) {
1986                         printf("Failed at operation %d - %s\n",
1987                                op, gen_ops[which_op].name);
1988                         return op;
1989                 }
1990
1991                 if (op % 100 == 0) {
1992                         printf("%d\n", op);
1993                 }
1994         }
1995
1996         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1997                 printf("Op %-10s got %d/%d success\n", 
1998                        gen_ops[i].name,
1999                        gen_ops[i].success_count,
2000                        gen_ops[i].count);
2001         }
2002
2003         return op;
2004 }
2005
2006 /* 
2007    perform a backtracking analysis of the minimal set of operations
2008    to generate an error
2009 */
2010 static void backtrack_analyze(void)
2011 {
2012         int chunk, ret;
2013
2014         chunk = options.numops / 2;
2015
2016         do {
2017                 int base;
2018                 for (base=0; 
2019                      chunk > 0 && base+chunk < options.numops && options.numops > 1; ) {
2020                         int i, max;
2021
2022                         chunk = MIN(chunk, options.numops / 2);
2023
2024                         /* mark this range as disabled */
2025                         max = MIN(options.numops, base+chunk);
2026                         for (i=base;i<max; i++) {
2027                                 op_parms[i].disabled = True;
2028                         }
2029                         printf("Testing %d ops with %d-%d disabled\n", 
2030                                options.numops, base, max-1);
2031                         ret = run_test();
2032                         printf("Completed %d of %d ops\n", ret, options.numops);
2033                         for (i=base;i<max; i++) {
2034                                 op_parms[i].disabled = False;
2035                         }
2036                         if (ret == options.numops) {
2037                                 /* this chunk is needed */
2038                                 base += chunk;
2039                         } else if (ret < base) {
2040                                 printf("damn - inconsistent errors! found early error\n");
2041                                 options.numops = ret+1;
2042                                 base = 0;
2043                         } else {
2044                                 /* it failed - this chunk isn't needed for a failure */
2045                                 memmove(&op_parms[base], &op_parms[max], 
2046                                         sizeof(op_parms[0]) * (options.numops - max));
2047                                 options.numops = (ret+1) - (max - base);
2048                         }
2049                 }
2050
2051                 if (chunk == 2) {
2052                         chunk = 1;
2053                 } else {
2054                         chunk *= 0.4;
2055                 }
2056
2057                 if (options.analyze_continuous && chunk == 0 && options.numops != 1) {
2058                         chunk = 1;
2059                 }
2060         } while (chunk > 0);
2061
2062         printf("Reduced to %d ops\n", options.numops);
2063         ret = run_test();
2064         if (ret != options.numops - 1) {
2065                 printf("Inconsistent result? ret=%d numops=%d\n", ret, options.numops);
2066         }
2067 }
2068
2069 /* 
2070    start the main gentest process
2071 */
2072 static BOOL start_gentest(void)
2073 {
2074         int op;
2075         int ret;
2076
2077         /* allocate the open_handles array */
2078         open_handles = calloc(options.max_open_handles, sizeof(open_handles[0]));
2079
2080         srandom(options.seed);
2081         op_parms = calloc(options.numops, sizeof(op_parms[0]));
2082
2083         /* generate the seeds - after this everything is deterministic */
2084         if (options.use_preset_seeds) {
2085                 int numops;
2086                 char **preset = file_lines_load(options.seeds_file, &numops, NULL);
2087                 if (!preset) {
2088                         printf("Failed to load %s - %s\n", options.seeds_file, strerror(errno));
2089                         exit(1);
2090                 }
2091                 if (numops < options.numops) {
2092                         options.numops = numops;
2093                 }
2094                 for (op=0;op<options.numops;op++) {
2095                         if (!preset[op]) {
2096                                 printf("Not enough seeds in %s\n", options.seeds_file);
2097                                 exit(1);
2098                         }
2099                         op_parms[op].seed = atoi(preset[op]);
2100                 }
2101                 printf("Loaded %d seeds from %s\n", options.numops, options.seeds_file);
2102         } else {
2103                 for (op=0; op<options.numops; op++) {
2104                         op_parms[op].seed = random();
2105                 }
2106         }
2107
2108         ret = run_test();
2109
2110         if (ret != options.numops && options.analyze) {
2111                 options.numops = ret+1;
2112                 backtrack_analyze();
2113         } else if (options.analyze_always) {
2114                 backtrack_analyze();
2115         } else if (options.analyze_continuous) {
2116                 while (run_test() == options.numops) ;
2117         }
2118
2119         return ret == options.numops;
2120 }
2121
2122
2123 static void usage(void)
2124 {
2125         printf(
2126 "Usage:\n\
2127   gentest2 //server1/share1 //server2/share2 [options..]\n\
2128   options:\n\
2129         -U user%%pass        (can be specified twice)\n\
2130         -s seed\n\
2131         -o numops\n\
2132         -a            (show all ops)\n\
2133         -A            backtrack to find minimal ops\n\
2134         -i FILE       add a list of wildcard exclusions\n\
2135         -O            enable oplocks\n\
2136         -S FILE       set preset seeds file\n\
2137         -L            use preset seeds\n\
2138         -F            fast reconnect (just close files)\n\
2139         -C            continuous analysis mode\n\
2140         -X            analyse even when test OK\n\
2141 ");
2142 }
2143
2144 /**
2145   split a UNC name into server and share names
2146 */
2147 static BOOL split_unc_name(const char *unc, char **server, char **share)
2148 {
2149         char *p = strdup(unc);
2150         if (!p) return False;
2151         all_string_sub(p, "\\", "/", 0);
2152         if (strncmp(p, "//", 2) != 0) return False;
2153
2154         (*server) = p+2;
2155         p = strchr(*server, '/');
2156         if (!p) return False;
2157
2158         *p = 0;
2159         (*share) = p+1;
2160         
2161         return True;
2162 }
2163
2164
2165
2166 /****************************************************************************
2167   main program
2168 ****************************************************************************/
2169  int main(int argc, char *argv[])
2170 {
2171         int opt;
2172         int i, username_count=0;
2173         BOOL ret;
2174
2175         setlinebuf(stdout);
2176
2177         setup_logging("gentest", DEBUG_STDOUT);
2178
2179         if (argc < 3 || argv[1][0] == '-') {
2180                 usage();
2181                 exit(1);
2182         }
2183
2184         setup_logging(argv[0], DEBUG_STDOUT);
2185
2186         for (i=0;i<NSERVERS;i++) {
2187                 const char *share = argv[1+i];
2188                 servers[i].credentials = cli_credentials_init(NULL);
2189                 if (!split_unc_name(share, &servers[i].server_name, &servers[i].share_name)) {
2190                         printf("Invalid share name '%s'\n", share);
2191                         return -1;
2192                 }
2193         }
2194
2195         argc -= NSERVERS;
2196         argv += NSERVERS;
2197
2198         lp_load();
2199
2200         servers[0].credentials = cli_credentials_init(talloc_autofree_context());
2201         servers[1].credentials = cli_credentials_init(talloc_autofree_context());
2202         cli_credentials_guess(servers[0].credentials);
2203         cli_credentials_guess(servers[1].credentials);
2204
2205         options.seed = time(NULL);
2206         options.numops = 1000;
2207         options.max_open_handles = 20;
2208         options.seeds_file = "gentest_seeds.dat";
2209
2210         while ((opt = getopt(argc, argv, "U:s:o:ad:i:AOhS:LFXC")) != EOF) {
2211                 switch (opt) {
2212                 case 'U':
2213                         if (username_count == 2) {
2214                                 usage();
2215                                 exit(1);
2216                         }
2217                         cli_credentials_parse_string(servers[username_count].credentials, 
2218                                                      optarg, CRED_SPECIFIED);
2219                         username_count++;
2220                         break;
2221                 case 'd':
2222                         DEBUGLEVEL = atoi(optarg);
2223                         setup_logging(NULL, DEBUG_STDOUT);
2224                         break;
2225                 case 's':
2226                         options.seed = atoi(optarg);
2227                         break;
2228                 case 'S':
2229                         options.seeds_file = optarg;
2230                         break;
2231                 case 'L':
2232                         options.use_preset_seeds = True;
2233                         break;
2234                 case 'F':
2235                         options.fast_reconnect = True;
2236                         break;
2237                 case 'o':
2238                         options.numops = atoi(optarg);
2239                         break;
2240                 case 'O':
2241                         options.use_oplocks = True;
2242                         break;
2243                 case 'a':
2244                         options.showall = True;
2245                         break;
2246                 case 'A':
2247                         options.analyze = True;
2248                         break;
2249                 case 'X':
2250                         options.analyze_always = True;
2251                         break;
2252                 case 'C':
2253                         options.analyze_continuous = True;
2254                         break;
2255                 case 'i':
2256                         options.ignore_patterns = file_lines_load(optarg, NULL, NULL);
2257                         break;
2258                 case 'h':
2259                         usage();
2260                         exit(1);
2261                 default:
2262                         printf("Unknown option %c (%d)\n", (char)opt, opt);
2263                         exit(1);
2264                 }
2265         }
2266
2267         gensec_init();
2268
2269         if (username_count == 0) {
2270                 usage();
2271                 return -1;
2272         }
2273         if (username_count == 1) {
2274                 servers[1].credentials = servers[0].credentials;
2275         }
2276
2277         printf("seed=%u\n", options.seed);
2278
2279         ret = start_gentest();
2280
2281         if (ret) {
2282                 printf("gentest completed - no errors\n");
2283         } else {
2284                 printf("gentest failed\n");
2285         }
2286
2287         return ret?0:-1;
2288 }