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