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