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