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