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