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