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