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