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