71101dd672ec0f02d9356508d1c6fe19ed4b5fda
[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 } 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 < 5) {
353                 if (gen_chance(90)) 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(uint64_t min, uint64_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 //      if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF);
385         return gen_int_range(0, 1024*1024);
386 }
387
388 /*
389   generate a io count
390 */
391 static int gen_io_count(void)
392 {
393         if (gen_chance(20)) return 0;
394 //      if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF);
395         return gen_int_range(0, 4096);
396 }
397
398 /*
399   generate a filename
400 */
401 static const char *gen_fname(void)
402 {
403         const char *names[] = {"gentest\\gentest.dat", 
404                                "gentest\\foo", 
405                                "gentest\\foo2.sym", 
406                                "gentest\\foo3.dll", 
407                                "gentest\\foo4", 
408                                "gentest\\foo4:teststream1", 
409                                "gentest\\foo4:teststream2", 
410                                "gentest\\foo5.exe", 
411                                "gentest\\foo5.exe:teststream3", 
412                                "gentest\\foo5.exe:teststream4", 
413                                "gentest\\foo6.com", 
414                                "gentest\\blah", 
415                                "gentest\\blah\\blergh.txt", 
416                                "gentest\\blah\\blergh2", 
417                                "gentest\\blah\\blergh3.txt", 
418                                "gentest\\blah\\blergh4", 
419                                "gentest\\blah\\blergh5.txt", 
420                                "gentest\\blah\\blergh5", 
421                                "gentest\\blah\\.", 
422 #if 0
423                                /* this causes problem with w2k3 */
424                                "gentest\\blah\\..", 
425 #endif
426                                "gentest\\a_very_long_name.bin", 
427                                "gentest\\x.y", 
428                                "gentest\\blah"};
429         int i;
430
431         do {
432                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
433         } while (ignore_pattern(names[i]));
434
435         return names[i];
436 }
437
438 /*
439   generate a filename with a higher chance of choosing an already 
440   open file
441 */
442 static const char *gen_fname_open(int instance)
443 {
444         uint16_t h;
445         h = gen_fnum(instance);
446         if (h == BAD_HANDLE) {
447                 return gen_fname();
448         }
449         return open_handles[h].name;
450 }
451
452 /*
453   generate a wildcard pattern
454 */
455 static const char *gen_pattern(void)
456 {
457         int i;
458         const char *names[] = {"gentest\\*.dat", 
459                                "gentest\\*", 
460                                "gentest\\*.*", 
461                                "gentest\\blah\\*.*", 
462                                "gentest\\blah\\*", 
463                                "gentest\\?"};
464
465         if (gen_chance(50)) return gen_fname();
466
467         do {
468                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
469         } while (ignore_pattern(names[i]));
470
471         return names[i];
472 }
473
474 static uint32_t gen_bits_levels(int nlevels, ...)
475 {
476         va_list ap;
477         uint32_t pct;
478         uint32_t mask;
479         int i;
480         va_start(ap, nlevels);
481         for (i=0;i<nlevels;i++) {
482                 pct = va_arg(ap, uint32_t);
483                 mask = va_arg(ap, uint32_t);
484                 if (pct == 100 || gen_chance(pct)) {
485                         va_end(ap);
486                         return mask & random();
487                 }
488         }
489         va_end(ap);
490         return 0;
491 }
492
493 /*
494   generate a bitmask
495 */
496 static uint32_t gen_bits_mask(uint_t mask)
497 {
498         uint_t ret = random();
499         return ret & mask;
500 }
501
502 /*
503   generate a bitmask with high probability of the first mask
504   and low of the second
505 */
506 static uint32_t gen_bits_mask2(uint32_t mask1, uint32_t mask2)
507 {
508         if (gen_chance(10)) return gen_bits_mask(mask2);
509         return gen_bits_mask(mask1);
510 }
511
512 /*
513   generate a boolean
514 */
515 static bool gen_bool(void)
516 {
517         return gen_bits_mask2(0x1, 0xFF);
518 }
519
520 /*
521   generate ntrename flags
522 */
523 static uint16_t gen_rename_flags(void)
524 {
525         if (gen_chance(30)) return RENAME_FLAG_RENAME;
526         if (gen_chance(30)) return RENAME_FLAG_HARD_LINK;
527         if (gen_chance(30)) return RENAME_FLAG_COPY;
528         return gen_bits_mask(0xFFFF);
529 }
530
531
532 /*
533   return a set of lock flags
534 */
535 static uint16_t gen_lock_flags(void)
536 {
537         if (gen_chance(5))  return gen_bits_mask(0xFFFF);
538         if (gen_chance(20)) return gen_bits_mask(0x1F);
539         if (gen_chance(50)) return SMB2_LOCK_FLAG_UNLOCK;
540         return gen_bits_mask(SMB2_LOCK_FLAG_SHARED | 
541                              SMB2_LOCK_FLAG_EXCLUSIVE | 
542                              SMB2_LOCK_FLAG_FAIL_IMMEDIATELY);
543 }
544
545 /*
546   generate a pid 
547 */
548 static uint16_t gen_pid(void)
549 {
550         if (gen_chance(10)) return gen_bits_mask(0xFFFF);
551         return getpid();
552 }
553
554 /*
555   generate a lock count
556 */
557 static off_t gen_lock_count(void)
558 {
559         return gen_int_range(0, 3);
560 }
561
562 /*
563   generate a ntcreatex flags field
564 */
565 static uint32_t gen_ntcreatex_flags(void)
566 {
567         if (gen_chance(70)) return NTCREATEX_FLAGS_EXTENDED;
568         return gen_bits_mask2(0x1F, 0xFFFFFFFF);
569 }
570
571 /*
572   generate a NT access mask
573 */
574 static uint32_t gen_access_mask(void)
575 {
576         if (gen_chance(70)) return SEC_FLAG_MAXIMUM_ALLOWED;
577         if (gen_chance(70)) return SEC_FILE_ALL;
578         return gen_bits_mask(0xFFFFFFFF);
579 }
580
581 /*
582   generate a ntcreatex create options bitfield
583 */
584 static uint32_t gen_create_options(void)
585 {
586         if (gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
587         if (gen_chance(50)) return 0;
588         return gen_bits_mask(NTCREATEX_OPTIONS_DELETE_ON_CLOSE | NTCREATEX_OPTIONS_DIRECTORY);
589 }
590
591 /*
592   generate a ntcreatex open disposition
593 */
594 static uint32_t gen_open_disp(void)
595 {
596         if (gen_chance(50)) return NTCREATEX_DISP_OPEN_IF;
597         if (gen_chance(10)) return gen_bits_mask(0xFFFFFFFF);
598         return gen_int_range(0, 5);
599 }
600
601 /*
602   generate an openx open mode
603 */
604 static uint16_t gen_openx_mode(void)
605 {
606         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
607         if (gen_chance(20)) return gen_bits_mask(0xFF);
608         return OPENX_MODE_DENY_NONE | gen_bits_mask(0x3);
609 }
610
611 /*
612   generate an openx flags field
613 */
614 static uint16_t gen_openx_flags(void)
615 {
616         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
617         return gen_bits_mask(0x7);
618 }
619
620 /*
621   generate an openx open function
622 */
623 static uint16_t gen_openx_func(void)
624 {
625         if (gen_chance(20)) return gen_bits_mask(0xFFFF);
626         return gen_bits_mask(0x13);
627 }
628
629 /*
630   generate a file attrib combination
631 */
632 static uint32_t gen_attrib(void)
633 {
634         if (gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
635         return gen_bits_mask(FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
636 }
637
638 /*
639   generate a unix timestamp
640 */
641 static time_t gen_timet(void)
642 {
643         if (gen_chance(30)) return 0;
644         return (time_t)random();
645 }
646
647 /*
648   generate a unix timestamp
649 */
650 static NTTIME gen_nttime(void)
651 {
652         NTTIME ret;
653         unix_to_nt_time(&ret, gen_timet());
654         return ret;
655 }
656
657 /*
658   generate a milliseconds protocol timeout
659 */
660 static uint32_t gen_timeout(void)
661 {
662         if (gen_chance(98)) return 0;
663         return random() % 50;
664 }
665
666 /*
667   generate a file allocation size
668 */
669 static uint_t gen_alloc_size(void)
670 {
671         uint_t ret;
672
673         if (gen_chance(30)) return 0;
674
675         ret = random() % 4*1024*1024;
676         /* give a high chance of a round number */
677         if (gen_chance(60)) {
678                 ret &= ~(1024*1024 - 1);
679         }
680         return ret;
681 }
682
683 /*
684   generate an ea_struct
685 */
686 static struct ea_struct gen_ea_struct(void)
687 {
688         struct ea_struct ea;
689         const char *names[] = {"EAONE", 
690                                "", 
691                                "FOO!", 
692                                " WITH SPACES ", 
693                                ".", 
694                                "AVERYLONGATTRIBUTENAME"};
695         const char *values[] = {"VALUE1", 
696                                "", 
697                                "NOT MUCH FOO", 
698                                " LEADING SPACES ", 
699                                ":", 
700                                "ASOMEWHATLONGERATTRIBUTEVALUE"};
701         int i;
702
703         ZERO_STRUCT(ea);
704
705         do {
706                 i = gen_int_range(0, ARRAY_SIZE(names)-1);
707         } while (ignore_pattern(names[i]));
708
709         ea.name.s = names[i];
710
711         do {
712                 i = gen_int_range(0, ARRAY_SIZE(values)-1);
713         } while (ignore_pattern(values[i]));
714
715         ea.value = data_blob(values[i], strlen(values[i]));
716
717         if (gen_chance(10)) ea.flags = gen_bits_mask(0xFF);
718         ea.flags = 0;
719
720         return ea;
721 }
722
723 /*
724   the idle function tries to cope with getting an oplock break on a connection, and
725   an operation on another connection blocking until that break is acked
726   we check for operations on all transports in the idle function
727 */
728 static void idle_func(struct smb2_transport *transport, void *private)
729 {
730         int i, j;
731         for (i=0;i<NSERVERS;i++) {
732                 for (j=0;j<NINSTANCES;j++) {
733                         if (servers[i].tree[j] &&
734                             transport != servers[i].tree[j]->session->transport) {
735                                 // smb2_transport_process(servers[i].tree[j]->session->transport);
736                         }
737                 }
738         }
739
740 }
741
742
743 /*
744   compare NTSTATUS, using checking ignored patterns
745 */
746 static bool compare_status(NTSTATUS status1, NTSTATUS status2)
747 {
748         if (NT_STATUS_EQUAL(status1, status2)) return true;
749
750         /* one code being an error and the other OK is always an error */
751         if (NT_STATUS_IS_OK(status1) || NT_STATUS_IS_OK(status2)) return false;
752
753         /* if we are ignoring one of the status codes then consider this a match */
754         if (ignore_pattern(nt_errstr(status1)) ||
755             ignore_pattern(nt_errstr(status2))) {
756                 return true;
757         }
758         return false;
759 }
760
761
762 /*
763   check for pending packets on all connections
764 */
765 static void check_pending(void)
766 {
767         int i, j;
768
769         msleep(20);
770
771         for (j=0;j<NINSTANCES;j++) {
772                 for (i=0;i<NSERVERS;i++) {
773                         // smb2_transport_process(servers[i].tree[j]->session->transport);
774                 }
775         }       
776 }
777
778 /*
779   check that the same oplock breaks have been received by all instances
780 */
781 static bool check_oplocks(const char *call)
782 {
783 #if 0
784         int i, j;
785         int tries = 0;
786
787 again:
788         check_pending();
789
790         for (j=0;j<NINSTANCES;j++) {
791                 for (i=1;i<NSERVERS;i++) {
792                         if (oplocks[0][j].got_break != oplocks[i][j].got_break ||
793                             oplocks[0][j].handle != oplocks[i][j].handle ||
794                             oplocks[0][j].level != oplocks[i][j].level) {
795                                 if (tries++ < 10) goto again;
796                                 printf("oplock break inconsistent - %d/%d/%d vs %d/%d/%d\n",
797                                        oplocks[0][j].got_break, 
798                                        oplocks[0][j].handle, 
799                                        oplocks[0][j].level, 
800                                        oplocks[i][j].got_break, 
801                                        oplocks[i][j].handle, 
802                                        oplocks[i][j].level);
803                                 return false;
804                         }
805                 }
806         }
807
808         /* if we got a break and closed then remove the handle */
809         for (j=0;j<NINSTANCES;j++) {
810                 if (oplocks[0][j].got_break &&
811                     oplocks[0][j].do_close) {
812                         uint16_t fnums[NSERVERS];
813                         for (i=0;i<NSERVERS;i++) {
814                                 fnums[i] = oplocks[i][j].fnum;
815                         }
816                         gen_remove_handle(j, fnums);
817                         break;
818                 }
819         }       
820 #endif
821         return true;
822 }
823
824
825 /*
826   check that the same change notify info has been received by all instances
827 */
828 static bool check_notifies(const char *call)
829 {
830 #if 0
831         int i, j;
832         int tries = 0;
833
834 again:
835         check_pending();
836
837         for (j=0;j<NINSTANCES;j++) {
838                 for (i=1;i<NSERVERS;i++) {
839                         int n;
840                         union smb_notify not1, not2;
841
842                         if (notifies[0][j].notify_count != notifies[i][j].notify_count) {
843                                 if (tries++ < 10) goto again;
844                                 printf("Notify count inconsistent %d %d\n",
845                                        notifies[0][j].notify_count,
846                                        notifies[i][j].notify_count);
847                                 return false;
848                         }
849
850                         if (notifies[0][j].notify_count == 0) continue;
851
852                         if (!NT_STATUS_EQUAL(notifies[0][j].status,
853                                              notifies[i][j].status)) {
854                                 printf("Notify status mismatch - %s - %s\n",
855                                        nt_errstr(notifies[0][j].status),
856                                        nt_errstr(notifies[i][j].status));
857                                 return false;
858                         }
859
860                         if (!NT_STATUS_IS_OK(notifies[0][j].status)) {
861                                 continue;
862                         }
863
864                         not1 = notifies[0][j].notify;
865                         not2 = notifies[i][j].notify;
866
867                         for (n=0;n<not1.nttrans.out.num_changes;n++) {
868                                 if (not1.nttrans.out.changes[n].action != 
869                                     not2.nttrans.out.changes[n].action) {
870                                         printf("Notify action %d inconsistent %d %d\n", n,
871                                                not1.nttrans.out.changes[n].action,
872                                                not2.nttrans.out.changes[n].action);
873                                         return false;
874                                 }
875                                 if (strcmp(not1.nttrans.out.changes[n].name.s,
876                                            not2.nttrans.out.changes[n].name.s)) {
877                                         printf("Notify name %d inconsistent %s %s\n", n,
878                                                not1.nttrans.out.changes[n].name.s,
879                                                not2.nttrans.out.changes[n].name.s);
880                                         return false;
881                                 }
882                                 if (not1.nttrans.out.changes[n].name.private_length !=
883                                     not2.nttrans.out.changes[n].name.private_length) {
884                                         printf("Notify name length %d inconsistent %d %d\n", n,
885                                                not1.nttrans.out.changes[n].name.private_length,
886                                                not2.nttrans.out.changes[n].name.private_length);
887                                         return false;
888                                 }
889                         }
890                 }
891         }
892
893         ZERO_STRUCT(notifies);
894
895 #endif
896         return true;
897 }
898
899 #define GEN_COPY_PARM do { \
900         int i; \
901         for (i=1;i<NSERVERS;i++) { \
902                 parm[i] = parm[0]; \
903         } \
904 } while (0)
905
906 #define GEN_CALL(call) do { \
907         int i; \
908         ZERO_STRUCT(oplocks); \
909         ZERO_STRUCT(notifies); \
910         for (i=0;i<NSERVERS;i++) { \
911                 struct smb2_tree *tree = servers[i].tree[instance]; \
912                 status[i] = call; \
913         } \
914         current_op.status = status[0]; \
915         for (i=1;i<NSERVERS;i++) { \
916                 if (!compare_status(status[i], status[0])) { \
917                         printf("status different in %s - %s %s\n", #call, \
918                                nt_errstr(status[0]), nt_errstr(status[i])); \
919                         return false; \
920                 } \
921         } \
922         if (!check_oplocks(#call)) return false;        \
923         if (!check_notifies(#call)) return false;       \
924         if (!NT_STATUS_IS_OK(status[0])) { \
925                 return true; \
926         } \
927 } while(0)
928
929 #define ADD_HANDLE(name, field) do { \
930         struct smb2_handle handles[NSERVERS]; \
931         int i; \
932         for (i=0;i<NSERVERS;i++) { \
933                 handles[i] = parm[i].field; \
934         } \
935         gen_add_handle(instance, name, handles); \
936 } while(0)
937
938 #define REMOVE_HANDLE(field) do { \
939         struct smb2_handle handles[NSERVERS]; \
940         int i; \
941         for (i=0;i<NSERVERS;i++) { \
942                 handles[i] = parm[i].field; \
943         } \
944         gen_remove_handle(instance, handles); \
945 } while(0)
946
947 #define GEN_SET_FNUM(field) do { \
948         int i; \
949         for (i=0;i<NSERVERS;i++) { \
950                 parm[i].field = gen_lookup_handle(i, parm[i].field.data[0]); \
951         } \
952 } while(0)
953
954 #define CHECK_EQUAL(field) do { \
955         if (parm[0].field != parm[1].field && !ignore_pattern(#field)) { \
956                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
957                        (int)parm[0].field, (int)parm[1].field); \
958                 return false; \
959         } \
960 } while(0)
961
962 #define CHECK_WSTR_EQUAL(field) do { \
963         if ((!parm[0].field.s && parm[1].field.s) || (parm[0].field.s && !parm[1].field.s)) { \
964                 printf("%s is NULL!\n", #field); \
965                 return false; \
966         } \
967         if (parm[0].field.s && strcmp(parm[0].field.s, parm[1].field.s) != 0 && !ignore_pattern(#field)) { \
968                 printf("Mismatch in %s - %s %s\n", #field, \
969                        parm[0].field.s, parm[1].field.s); \
970                 return false; \
971         } \
972         CHECK_EQUAL(field.private_length); \
973 } while(0)
974
975 #define CHECK_BLOB_EQUAL(field) do { \
976         if (memcmp(parm[0].field.data, parm[1].field.data, parm[0].field.length) != 0 && !ignore_pattern(#field)) { \
977                 printf("Mismatch in %s\n", #field); \
978                 return false; \
979         } \
980         CHECK_EQUAL(field.length); \
981 } while(0)
982
983 #define CHECK_TIMES_EQUAL(field) do { \
984         if (labs(parm[0].field - parm[1].field) > time_skew() && \
985             !ignore_pattern(#field)) { \
986                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
987                        (int)parm[0].field, (int)parm[1].field); \
988                 return false; \
989         } \
990 } while(0)
991
992 #define CHECK_NTTIMES_EQUAL(field) do { \
993         if (labs(nt_time_to_unix(parm[0].field) - \
994                 nt_time_to_unix(parm[1].field)) > time_skew() && \
995             !ignore_pattern(#field)) { \
996                 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
997                        (int)nt_time_to_unix(parm[0].field), \
998                        (int)nt_time_to_unix(parm[1].field)); \
999                 return false; \
1000         } \
1001 } while(0)
1002
1003 /*
1004   generate ntcreatex operations
1005 */
1006 static bool handler_create(int instance)
1007 {
1008         struct smb2_create parm[NSERVERS];
1009         NTSTATUS status[NSERVERS];
1010
1011         ZERO_STRUCT(parm[0]);
1012         parm[0].in.security_flags             = gen_bits_levels(3, 90, 0x0, 70, 0x3, 100, 0xFF);
1013         parm[0].in.oplock_level               = gen_bits_levels(3, 90, 0x0, 70, 0x9, 100, 0xFF);
1014         parm[0].in.impersonation_level        = gen_bits_levels(3, 90, 0x0, 70, 0x3, 100, 0xFFFFFFFF);
1015         parm[0].in.create_flags               = gen_bits_levels(2, 90, 0x0, 100, 0xFFFFFFFF);
1016         if (gen_chance(2)) {
1017                 parm[0].in.create_flags       |= gen_bits_mask(0xFFFFFFFF);
1018         }
1019         parm[0].in.reserved                   = gen_bits_levels(2, 95, 0x0, 100, 0xFFFFFFFF);
1020         if (gen_chance(2)) {
1021                 parm[0].in.reserved           |= gen_bits_mask(0xFFFFFFFF);
1022         }
1023         parm[0].in.desired_access             = gen_access_mask();
1024         parm[0].in.file_attributes            = gen_attrib();
1025         parm[0].in.share_access               = gen_bits_mask2(0x7, 0xFFFFFFFF);
1026         parm[0].in.create_disposition         = gen_open_disp();
1027         parm[0].in.create_options             = gen_create_options();
1028         parm[0].in.fname                      = gen_fname_open(instance);
1029
1030         if (!options.use_oplocks) {
1031                 /* mask out oplocks */
1032                 parm[0].in.oplock_level = 0;
1033         }
1034         
1035         GEN_COPY_PARM;
1036         GEN_CALL(smb2_create(tree, current_op.mem_ctx, &parm[i]));
1037
1038         CHECK_EQUAL(out.oplock_level);
1039         CHECK_EQUAL(out.reserved);
1040         CHECK_EQUAL(out.create_action);
1041         CHECK_NTTIMES_EQUAL(out.create_time);
1042         CHECK_NTTIMES_EQUAL(out.access_time);
1043         CHECK_NTTIMES_EQUAL(out.write_time);
1044         CHECK_NTTIMES_EQUAL(out.change_time);
1045         CHECK_EQUAL(out.alloc_size);
1046         CHECK_EQUAL(out.size);
1047         CHECK_EQUAL(out.file_attr);
1048         CHECK_EQUAL(out.reserved2);
1049
1050         /* ntcreatex creates a new file handle */
1051         ADD_HANDLE(parm[0].in.fname, out.file.handle);
1052
1053         return true;
1054 }
1055
1056 /*
1057   generate close operations
1058 */
1059 static bool handler_close(int instance)
1060 {
1061         struct smb2_close parm[NSERVERS];
1062         NTSTATUS status[NSERVERS];
1063
1064         ZERO_STRUCT(parm[0]);
1065         parm[0].in.file.handle.data[0] = gen_fnum_close(instance);
1066         parm[0].in.flags               = gen_bits_mask2(0x1, 0xFFFF);
1067
1068         GEN_COPY_PARM;
1069         GEN_SET_FNUM(in.file.handle);
1070         GEN_CALL(smb2_close(tree, &parm[i]));
1071
1072         CHECK_EQUAL(out.flags);
1073         CHECK_EQUAL(out._pad);
1074         CHECK_NTTIMES_EQUAL(out.create_time);
1075         CHECK_NTTIMES_EQUAL(out.access_time);
1076         CHECK_NTTIMES_EQUAL(out.write_time);
1077         CHECK_NTTIMES_EQUAL(out.change_time);
1078         CHECK_EQUAL(out.alloc_size);
1079         CHECK_EQUAL(out.size);
1080         CHECK_EQUAL(out.file_attr);
1081
1082         REMOVE_HANDLE(in.file.handle);
1083
1084         return true;
1085 }
1086
1087 /*
1088   generate read operations
1089 */
1090 static bool handler_read(int instance)
1091 {
1092         struct smb2_read parm[NSERVERS];
1093         NTSTATUS status[NSERVERS];
1094
1095         parm[0].in.file.handle.data[0] = gen_fnum(instance);
1096         parm[0].in.reserved    = gen_bits_mask2(0x0, 0xFF);
1097         parm[0].in.length      = gen_io_count();
1098         parm[0].in.offset      = gen_offset();
1099         parm[0].in.min_count   = gen_io_count();
1100         parm[0].in.channel     = gen_bits_mask2(0x0, 0xFFFFFFFF);
1101         parm[0].in.remaining   = gen_bits_mask2(0x0, 0xFFFFFFFF);
1102         parm[0].in.channel_offset = gen_bits_mask2(0x0, 0xFFFF);
1103         parm[0].in.channel_length = gen_bits_mask2(0x0, 0xFFFF);
1104
1105         GEN_COPY_PARM;
1106         GEN_SET_FNUM(in.file.handle);
1107         GEN_CALL(smb2_read(tree, current_op.mem_ctx, &parm[i]));
1108
1109         CHECK_EQUAL(out.remaining);
1110         CHECK_EQUAL(out.reserved);
1111         CHECK_EQUAL(out.data.length);
1112
1113         return true;
1114 }
1115
1116 /*
1117   generate write operations
1118 */
1119 static bool handler_write(int instance)
1120 {
1121         struct smb2_write parm[NSERVERS];
1122         NTSTATUS status[NSERVERS];
1123
1124         parm[0].in.file.handle.data[0] = gen_fnum(instance);
1125         parm[0].in.offset = gen_offset();
1126         parm[0].in.unknown1 = gen_bits_mask2(0, 0xFFFFFFFF);
1127         parm[0].in.unknown2 = gen_bits_mask2(0, 0xFFFFFFFF);
1128         parm[0].in.data = data_blob_talloc(current_op.mem_ctx, NULL,
1129                                             gen_io_count());
1130
1131         GEN_COPY_PARM;
1132         GEN_SET_FNUM(in.file.handle);
1133         GEN_CALL(smb2_write(tree, &parm[i]));
1134
1135         CHECK_EQUAL(out._pad);
1136         CHECK_EQUAL(out.nwritten);
1137         CHECK_EQUAL(out.unknown1);
1138
1139         return true;
1140 }
1141
1142 /*
1143   generate lockingx operations
1144 */
1145 static bool handler_lock(int instance)
1146 {
1147         struct smb2_lock parm[NSERVERS];
1148         NTSTATUS status[NSERVERS];
1149         int n;
1150
1151         parm[0].level = RAW_LOCK_LOCKX;
1152         parm[0].in.file.handle.data[0] = gen_fnum(instance);
1153         parm[0].in.lock_count = gen_lock_count();
1154         parm[0].in.reserved = gen_bits_mask2(0, 0xFFFFFFFF);
1155         
1156         parm[0].in.locks = talloc_array(current_op.mem_ctx,
1157                                         struct smb2_lock_element,
1158                                         parm[0].in.lock_count);
1159         for (n=0;n<parm[0].in.lock_count;n++) {
1160                 parm[0].in.locks[n].offset = gen_offset();
1161                 parm[0].in.locks[n].length = gen_io_count();
1162                 /* don't yet cope with async replies */
1163                 parm[0].in.locks[n].flags  = gen_lock_flags() | 
1164                         SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1165                 parm[0].in.locks[n].reserved = gen_bits_mask2(0x0, 0xFFFFFFFF);
1166         }
1167
1168         GEN_COPY_PARM;
1169         GEN_SET_FNUM(in.file.handle);
1170         GEN_CALL(smb2_lock(tree, &parm[i]));
1171
1172         return true;
1173 }
1174
1175 /*
1176   generate flush operations
1177 */
1178 static bool handler_flush(int instance)
1179 {
1180         struct smb2_flush parm[NSERVERS];
1181         NTSTATUS status[NSERVERS];
1182
1183         ZERO_STRUCT(parm[0]);
1184         parm[0].in.file.handle.data[0] = gen_fnum(instance);
1185         parm[0].in.reserved1  = gen_bits_mask2(0x0, 0xFFFF);
1186         parm[0].in.reserved2  = gen_bits_mask2(0x0, 0xFFFFFFFF);
1187
1188         GEN_COPY_PARM;
1189         GEN_SET_FNUM(in.file.handle);
1190         GEN_CALL(smb2_flush(tree, &parm[i]));
1191
1192         CHECK_EQUAL(out.reserved);
1193
1194         return true;
1195 }
1196
1197 /*
1198   generate echo operations
1199 */
1200 static bool handler_echo(int instance)
1201 {
1202         NTSTATUS status[NSERVERS];
1203
1204         GEN_CALL(smb2_keepalive(tree->session->transport));
1205
1206         return true;
1207 }
1208
1209 #if 0
1210
1211 /*
1212   generate a fileinfo query structure
1213 */
1214 static void gen_fileinfo(int instance, union smb_fileinfo *info)
1215 {
1216         int i;
1217         #define LVL(v) {RAW_FILEINFO_ ## v, "RAW_FILEINFO_" #v}
1218         struct {
1219                 enum smb_fileinfo_level level;
1220                 const char *name;
1221         }  levels[] = {
1222                 LVL(GETATTR), LVL(GETATTRE), LVL(STANDARD),
1223                 LVL(EA_SIZE), LVL(ALL_EAS), LVL(IS_NAME_VALID),
1224                 LVL(BASIC_INFO), LVL(STANDARD_INFO), LVL(EA_INFO),
1225                 LVL(NAME_INFO), LVL(ALL_INFO), LVL(ALT_NAME_INFO),
1226                 LVL(STREAM_INFO), LVL(COMPRESSION_INFO), LVL(BASIC_INFORMATION),
1227                 LVL(STANDARD_INFORMATION), LVL(INTERNAL_INFORMATION), LVL(EA_INFORMATION),
1228                 LVL(ACCESS_INFORMATION), LVL(NAME_INFORMATION), LVL(POSITION_INFORMATION),
1229                 LVL(MODE_INFORMATION), LVL(ALIGNMENT_INFORMATION), LVL(ALL_INFORMATION),
1230                 LVL(ALT_NAME_INFORMATION), LVL(STREAM_INFORMATION), LVL(COMPRESSION_INFORMATION),
1231                 LVL(NETWORK_OPEN_INFORMATION), LVL(ATTRIBUTE_TAG_INFORMATION)
1232         };
1233         do {
1234                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1235         } while (ignore_pattern(levels[i].name));
1236
1237         info->generic.level = levels[i].level;
1238 }
1239
1240 /*
1241   compare returned fileinfo structures
1242 */
1243 static bool cmp_fileinfo(int instance, 
1244                          union smb_fileinfo parm[NSERVERS],
1245                          NTSTATUS status[NSERVERS])
1246 {
1247         int i;
1248
1249         switch (parm[0].generic.level) {
1250         case RAW_FILEINFO_GENERIC:
1251                 return false;
1252
1253         case RAW_FILEINFO_GETATTR:
1254                 CHECK_EQUAL(getattr.out.attrib);
1255                 CHECK_EQUAL(getattr.out.size);
1256                 CHECK_TIMES_EQUAL(getattr.out.write_time);
1257                 break;
1258
1259         case RAW_FILEINFO_GETATTRE:
1260                 CHECK_TIMES_EQUAL(getattre.out.create_time);
1261                 CHECK_TIMES_EQUAL(getattre.out.access_time);
1262                 CHECK_TIMES_EQUAL(getattre.out.write_time);
1263                 CHECK_EQUAL(getattre.out.size);
1264                 CHECK_EQUAL(getattre.out.alloc_size);
1265                 CHECK_EQUAL(getattre.out.attrib);
1266                 break;
1267
1268         case RAW_FILEINFO_STANDARD:
1269                 CHECK_TIMES_EQUAL(standard.out.create_time);
1270                 CHECK_TIMES_EQUAL(standard.out.access_time);
1271                 CHECK_TIMES_EQUAL(standard.out.write_time);
1272                 CHECK_EQUAL(standard.out.size);
1273                 CHECK_EQUAL(standard.out.alloc_size);
1274                 CHECK_EQUAL(standard.out.attrib);
1275                 break;
1276
1277         case RAW_FILEINFO_EA_SIZE:
1278                 CHECK_TIMES_EQUAL(ea_size.out.create_time);
1279                 CHECK_TIMES_EQUAL(ea_size.out.access_time);
1280                 CHECK_TIMES_EQUAL(ea_size.out.write_time);
1281                 CHECK_EQUAL(ea_size.out.size);
1282                 CHECK_EQUAL(ea_size.out.alloc_size);
1283                 CHECK_EQUAL(ea_size.out.attrib);
1284                 CHECK_EQUAL(ea_size.out.ea_size);
1285                 break;
1286
1287         case RAW_FILEINFO_ALL_EAS:
1288                 CHECK_EQUAL(all_eas.out.num_eas);
1289                 for (i=0;i<parm[0].all_eas.out.num_eas;i++) {
1290                         CHECK_EQUAL(all_eas.out.eas[i].flags);
1291                         CHECK_WSTR_EQUAL(all_eas.out.eas[i].name);
1292                         CHECK_BLOB_EQUAL(all_eas.out.eas[i].value);
1293                 }
1294                 break;
1295
1296         case RAW_FILEINFO_IS_NAME_VALID:
1297                 break;
1298                 
1299         case RAW_FILEINFO_BASIC_INFO:
1300         case RAW_FILEINFO_BASIC_INFORMATION:
1301                 CHECK_NTTIMES_EQUAL(basic_info.out.create_time);
1302                 CHECK_NTTIMES_EQUAL(basic_info.out.access_time);
1303                 CHECK_NTTIMES_EQUAL(basic_info.out.write_time);
1304                 CHECK_NTTIMES_EQUAL(basic_info.out.change_time);
1305                 CHECK_EQUAL(basic_info.out.attrib);
1306                 break;
1307
1308         case RAW_FILEINFO_STANDARD_INFO:
1309         case RAW_FILEINFO_STANDARD_INFORMATION:
1310                 CHECK_EQUAL(standard_info.out.alloc_size);
1311                 CHECK_EQUAL(standard_info.out.size);
1312                 CHECK_EQUAL(standard_info.out.nlink);
1313                 CHECK_EQUAL(standard_info.out.delete_pending);
1314                 CHECK_EQUAL(standard_info.out.directory);
1315                 break;
1316
1317         case RAW_FILEINFO_EA_INFO:
1318         case RAW_FILEINFO_EA_INFORMATION:
1319                 CHECK_EQUAL(ea_info.out.ea_size);
1320                 break;
1321
1322         case RAW_FILEINFO_NAME_INFO:
1323         case RAW_FILEINFO_NAME_INFORMATION:
1324                 CHECK_WSTR_EQUAL(name_info.out.fname);
1325                 break;
1326
1327         case RAW_FILEINFO_ALL_INFO:
1328         case RAW_FILEINFO_ALL_INFORMATION:
1329                 CHECK_NTTIMES_EQUAL(all_info.out.create_time);
1330                 CHECK_NTTIMES_EQUAL(all_info.out.access_time);
1331                 CHECK_NTTIMES_EQUAL(all_info.out.write_time);
1332                 CHECK_NTTIMES_EQUAL(all_info.out.change_time);
1333                 CHECK_EQUAL(all_info.out.attrib);
1334                 CHECK_EQUAL(all_info.out.alloc_size);
1335                 CHECK_EQUAL(all_info.out.size);
1336                 CHECK_EQUAL(all_info.out.nlink);
1337                 CHECK_EQUAL(all_info.out.delete_pending);
1338                 CHECK_EQUAL(all_info.out.directory);
1339                 CHECK_EQUAL(all_info.out.ea_size);
1340                 CHECK_WSTR_EQUAL(all_info.out.fname);
1341                 break;
1342
1343         case RAW_FILEINFO_ALT_NAME_INFO:
1344         case RAW_FILEINFO_ALT_NAME_INFORMATION:
1345                 CHECK_WSTR_EQUAL(alt_name_info.out.fname);
1346                 break;
1347
1348         case RAW_FILEINFO_STREAM_INFO:
1349         case RAW_FILEINFO_STREAM_INFORMATION:
1350                 CHECK_EQUAL(stream_info.out.num_streams);
1351                 for (i=0;i<parm[0].stream_info.out.num_streams;i++) {
1352                         CHECK_EQUAL(stream_info.out.streams[i].size);
1353                         CHECK_EQUAL(stream_info.out.streams[i].alloc_size);
1354                         CHECK_WSTR_EQUAL(stream_info.out.streams[i].stream_name);
1355                 }
1356                 break;
1357
1358         case RAW_FILEINFO_COMPRESSION_INFO:
1359         case RAW_FILEINFO_COMPRESSION_INFORMATION:
1360                 CHECK_EQUAL(compression_info.out.compressed_size);
1361                 CHECK_EQUAL(compression_info.out.format);
1362                 CHECK_EQUAL(compression_info.out.unit_shift);
1363                 CHECK_EQUAL(compression_info.out.chunk_shift);
1364                 CHECK_EQUAL(compression_info.out.cluster_shift);
1365                 break;
1366
1367         case RAW_FILEINFO_INTERNAL_INFORMATION:
1368                 CHECK_EQUAL(internal_information.out.file_id);
1369                 break;
1370
1371         case RAW_FILEINFO_ACCESS_INFORMATION:
1372                 CHECK_EQUAL(access_information.out.access_flags);
1373                 break;
1374
1375         case RAW_FILEINFO_POSITION_INFORMATION:
1376                 CHECK_EQUAL(position_information.out.position);
1377                 break;
1378
1379         case RAW_FILEINFO_MODE_INFORMATION:
1380                 CHECK_EQUAL(mode_information.out.mode);
1381                 break;
1382
1383         case RAW_FILEINFO_ALIGNMENT_INFORMATION:
1384                 CHECK_EQUAL(alignment_information.out.alignment_requirement);
1385                 break;
1386
1387         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1388                 CHECK_NTTIMES_EQUAL(network_open_information.out.create_time);
1389                 CHECK_NTTIMES_EQUAL(network_open_information.out.access_time);
1390                 CHECK_NTTIMES_EQUAL(network_open_information.out.write_time);
1391                 CHECK_NTTIMES_EQUAL(network_open_information.out.change_time);
1392                 CHECK_EQUAL(network_open_information.out.alloc_size);
1393                 CHECK_EQUAL(network_open_information.out.size);
1394                 CHECK_EQUAL(network_open_information.out.attrib);
1395                 break;
1396
1397         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1398                 CHECK_EQUAL(attribute_tag_information.out.attrib);
1399                 CHECK_EQUAL(attribute_tag_information.out.reparse_tag);
1400                 break;
1401
1402                 /* Unhandled levels */
1403
1404         case RAW_FILEINFO_SEC_DESC:
1405         case RAW_FILEINFO_EA_LIST:
1406         case RAW_FILEINFO_UNIX_BASIC:
1407         case RAW_FILEINFO_UNIX_LINK:
1408         case RAW_FILEINFO_SMB2_ALL_EAS:
1409         case RAW_FILEINFO_SMB2_ALL_INFORMATION:
1410         case RAW_FILEINFO_UNIX_INFO2:
1411                 break;
1412         }
1413
1414         return true;
1415 }
1416
1417 /*
1418   generate qfileinfo operations
1419 */
1420 static bool handler_qfileinfo(int instance)
1421 {
1422         union smb_fileinfo parm[NSERVERS];
1423         NTSTATUS status[NSERVERS];
1424
1425         parm[0].generic.in.file.fnum = gen_fnum(instance);
1426
1427         gen_fileinfo(instance, &parm[0]);
1428
1429         GEN_COPY_PARM;
1430         GEN_SET_FNUM(generic.in.file.fnum);
1431         GEN_CALL(smb_raw_fileinfo(tree, current_op.mem_ctx, &parm[i]));
1432
1433         return cmp_fileinfo(instance, parm, status);
1434 }
1435
1436
1437 /*
1438   generate a fileinfo query structure
1439 */
1440 static void gen_setfileinfo(int instance, union smb_setfileinfo *info)
1441 {
1442         int i;
1443         #undef LVL
1444         #define LVL(v) {RAW_SFILEINFO_ ## v, "RAW_SFILEINFO_" #v}
1445         struct {
1446                 enum smb_setfileinfo_level level;
1447                 const char *name;
1448         }  levels[] = {
1449 #if 0
1450                 /* disabled until win2003 can handle them ... */
1451                 LVL(EA_SET), LVL(BASIC_INFO), LVL(DISPOSITION_INFO), 
1452                 LVL(STANDARD), LVL(ALLOCATION_INFO), LVL(END_OF_FILE_INFO), 
1453 #endif
1454                 LVL(SETATTR), LVL(SETATTRE), LVL(BASIC_INFORMATION),
1455                 LVL(RENAME_INFORMATION), LVL(DISPOSITION_INFORMATION), 
1456                 LVL(POSITION_INFORMATION), LVL(MODE_INFORMATION),
1457                 LVL(ALLOCATION_INFORMATION), LVL(END_OF_FILE_INFORMATION), 
1458                 LVL(1023), LVL(1025), LVL(1029), LVL(1032), LVL(1039), LVL(1040)
1459         };
1460         do {
1461                 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1462         } while (ignore_pattern(levels[i].name));
1463
1464         info->generic.level = levels[i].level;
1465
1466         switch (info->generic.level) {
1467         case RAW_SFILEINFO_SETATTR:
1468                 info->setattr.in.attrib = gen_attrib();
1469                 info->setattr.in.write_time = gen_timet();
1470                 break;
1471         case RAW_SFILEINFO_SETATTRE:
1472                 info->setattre.in.create_time = gen_timet();
1473                 info->setattre.in.access_time = gen_timet();
1474                 info->setattre.in.write_time = gen_timet();
1475                 break;
1476         case RAW_SFILEINFO_STANDARD:
1477                 info->standard.in.create_time = gen_timet();
1478                 info->standard.in.access_time = gen_timet();
1479                 info->standard.in.write_time = gen_timet();
1480                 break;
1481         case RAW_SFILEINFO_EA_SET: {
1482                 static struct ea_struct ea;
1483                 info->ea_set.in.num_eas = 1;
1484                 info->ea_set.in.eas = &ea;
1485                 info->ea_set.in.eas[0] = gen_ea_struct();
1486         }
1487                 break;
1488         case RAW_SFILEINFO_BASIC_INFO:
1489         case RAW_SFILEINFO_BASIC_INFORMATION:
1490                 info->basic_info.in.create_time = gen_nttime();
1491                 info->basic_info.in.access_time = gen_nttime();
1492                 info->basic_info.in.write_time = gen_nttime();
1493                 info->basic_info.in.change_time = gen_nttime();
1494                 info->basic_info.in.attrib = gen_attrib();
1495                 break;
1496         case RAW_SFILEINFO_DISPOSITION_INFO:
1497         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
1498                 info->disposition_info.in.delete_on_close = gen_bool();
1499                 break;
1500         case RAW_SFILEINFO_ALLOCATION_INFO:
1501         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
1502                 info->allocation_info.in.alloc_size = gen_alloc_size();
1503                 break;
1504         case RAW_SFILEINFO_END_OF_FILE_INFO:
1505         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
1506                 info->end_of_file_info.in.size = gen_offset();
1507                 break;
1508         case RAW_SFILEINFO_RENAME_INFORMATION:
1509         case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
1510                 info->rename_information.in.overwrite = gen_bool();
1511                 info->rename_information.in.root_fid = gen_root_fid(instance);
1512                 info->rename_information.in.new_name = gen_fname_open(instance);
1513                 break;
1514         case RAW_SFILEINFO_POSITION_INFORMATION:
1515                 info->position_information.in.position = gen_offset();
1516                 break;
1517         case RAW_SFILEINFO_MODE_INFORMATION:
1518                 info->mode_information.in.mode = gen_bits_mask(0xFFFFFFFF);
1519                 break;
1520         case RAW_SFILEINFO_GENERIC:
1521         case RAW_SFILEINFO_SEC_DESC:
1522         case RAW_SFILEINFO_UNIX_BASIC:
1523         case RAW_SFILEINFO_UNIX_LINK:
1524         case RAW_SFILEINFO_UNIX_HLINK:
1525         case RAW_SFILEINFO_1023:
1526         case RAW_SFILEINFO_1025:
1527         case RAW_SFILEINFO_1029:
1528         case RAW_SFILEINFO_1032:
1529         case RAW_SFILEINFO_1039:
1530         case RAW_SFILEINFO_1040:
1531         case RAW_SFILEINFO_UNIX_INFO2:
1532                 /* Untested */
1533                 break;
1534         }
1535 }
1536
1537 /*
1538   generate setfileinfo operations
1539 */
1540 static bool handler_sfileinfo(int instance)
1541 {
1542         union smb_setfileinfo parm[NSERVERS];
1543         NTSTATUS status[NSERVERS];
1544
1545         parm[0].generic.in.file.fnum = gen_fnum(instance);
1546
1547         gen_setfileinfo(instance, &parm[0]);
1548
1549         GEN_COPY_PARM;
1550         GEN_SET_FNUM(generic.in.file.fnum);
1551         GEN_CALL(smb_raw_setfileinfo(tree, &parm[i]));
1552
1553         return true;
1554 }
1555
1556 #endif
1557
1558
1559 /*
1560   wipe any relevant files
1561 */
1562 static void wipe_files(void)
1563 {
1564         int i;
1565         NTSTATUS status;
1566
1567         for (i=0;i<NSERVERS;i++) {
1568                 int n = smb2_deltree(servers[i].tree[0], "gentest");
1569                 if (n == -1) {
1570                         printf("Failed to wipe tree on server %d\n", i);
1571                         exit(1);
1572                 }
1573                 status = smb2_util_mkdir(servers[i].tree[0], "gentest");
1574                 if (NT_STATUS_IS_ERR(status)) {
1575                         printf("Failed to create gentest on server %d - %s\n", i, nt_errstr(status));
1576                         exit(1);
1577                 }
1578                 if (n > 0) {
1579                         printf("Deleted %d files on server %d\n", n, i);
1580                 }
1581         }
1582 }
1583
1584 /*
1585   dump the current seeds - useful for continuing a backtrack
1586 */
1587 static void dump_seeds(void)
1588 {
1589         int i;
1590         FILE *f;
1591
1592         if (!options.seeds_file) {
1593                 return;
1594         }
1595         f = fopen("seeds.tmp", "w");
1596         if (!f) return;
1597
1598         for (i=0;i<options.numops;i++) {
1599                 fprintf(f, "%u\n", op_parms[i].seed);
1600         }
1601         fclose(f);
1602         rename("seeds.tmp", options.seeds_file);
1603 }
1604
1605
1606
1607 /*
1608   the list of top-level operations that we will generate
1609 */
1610 static struct {
1611         const char *name;
1612         bool (*handler)(int instance);
1613         int count, success_count;
1614 } gen_ops[] = {
1615         {"CREATE",     handler_create},
1616         {"CLOSE",      handler_close},
1617         {"READ",       handler_read},
1618         {"WRITE",      handler_write},
1619         {"LOCK",       handler_lock},
1620         {"FLUSH",      handler_flush},
1621         {"ECHO",       handler_echo},
1622 };
1623
1624
1625 /*
1626   run the test with the current set of op_parms parameters
1627   return the number of operations that completed successfully
1628 */
1629 static int run_test(struct event_context *ev, struct loadparm_context *lp_ctx)
1630 {
1631         int op, i;
1632
1633         if (!connect_servers(ev, lp_ctx)) {
1634                 printf("Failed to connect to servers\n");
1635                 exit(1);
1636         }
1637
1638         dump_seeds();
1639
1640         /* wipe any leftover files from old runs */
1641         wipe_files();
1642
1643         /* reset the open handles array */
1644         memset(open_handles, 0, options.max_open_handles * sizeof(open_handles[0]));
1645         num_open_handles = 0;
1646
1647         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1648                 gen_ops[i].count = 0;
1649                 gen_ops[i].success_count = 0;
1650         }
1651
1652         for (op=0; op<options.numops; op++) {
1653                 int instance, which_op;
1654                 bool ret;
1655
1656                 if (op_parms[op].disabled) continue;
1657
1658                 srandom(op_parms[op].seed);
1659
1660                 instance = gen_int_range(0, NINSTANCES-1);
1661
1662                 /* generate a non-ignored operation */
1663                 do {
1664                         which_op = gen_int_range(0, ARRAY_SIZE(gen_ops)-1);
1665                 } while (ignore_pattern(gen_ops[which_op].name));
1666
1667                 DEBUG(3,("Generating op %s on instance %d\n",
1668                          gen_ops[which_op].name, instance));
1669
1670                 current_op.seed = op_parms[op].seed;
1671                 current_op.opnum = op;
1672                 current_op.name = gen_ops[which_op].name;
1673                 current_op.status = NT_STATUS_OK;
1674                 current_op.mem_ctx = talloc_named(NULL, 0, "%s", current_op.name);
1675
1676                 ret = gen_ops[which_op].handler(instance);
1677
1678                 talloc_free(current_op.mem_ctx);
1679
1680                 gen_ops[which_op].count++;
1681                 if (NT_STATUS_IS_OK(current_op.status)) {
1682                         gen_ops[which_op].success_count++;                      
1683                 }
1684
1685                 if (!ret) {
1686                         printf("Failed at operation %d - %s\n",
1687                                op, gen_ops[which_op].name);
1688                         return op;
1689                 }
1690
1691                 if (op % 100 == 0) {
1692                         printf("%d\n", op);
1693                 }
1694         }
1695
1696         for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1697                 printf("Op %-10s got %d/%d success\n", 
1698                        gen_ops[i].name,
1699                        gen_ops[i].success_count,
1700                        gen_ops[i].count);
1701         }
1702
1703         return op;
1704 }
1705
1706 /* 
1707    perform a backtracking analysis of the minimal set of operations
1708    to generate an error
1709 */
1710 static void backtrack_analyze(struct event_context *ev,
1711                               struct loadparm_context *lp_ctx)
1712 {
1713         int chunk, ret;
1714
1715         chunk = options.numops / 2;
1716
1717         do {
1718                 int base;
1719                 for (base=0; 
1720                      chunk > 0 && base+chunk < options.numops && options.numops > 1; ) {
1721                         int i, max;
1722
1723                         chunk = MIN(chunk, options.numops / 2);
1724
1725                         /* mark this range as disabled */
1726                         max = MIN(options.numops, base+chunk);
1727                         for (i=base;i<max; i++) {
1728                                 op_parms[i].disabled = true;
1729                         }
1730                         printf("Testing %d ops with %d-%d disabled\n", 
1731                                options.numops, base, max-1);
1732                         ret = run_test(ev, lp_ctx);
1733                         printf("Completed %d of %d ops\n", ret, options.numops);
1734                         for (i=base;i<max; i++) {
1735                                 op_parms[i].disabled = false;
1736                         }
1737                         if (ret == options.numops) {
1738                                 /* this chunk is needed */
1739                                 base += chunk;
1740                         } else if (ret < base) {
1741                                 printf("damn - inconsistent errors! found early error\n");
1742                                 options.numops = ret+1;
1743                                 base = 0;
1744                         } else {
1745                                 /* it failed - this chunk isn't needed for a failure */
1746                                 memmove(&op_parms[base], &op_parms[max], 
1747                                         sizeof(op_parms[0]) * (options.numops - max));
1748                                 options.numops = (ret+1) - (max - base);
1749                         }
1750                 }
1751
1752                 if (chunk == 2) {
1753                         chunk = 1;
1754                 } else {
1755                         chunk *= 0.4;
1756                 }
1757
1758                 if (options.analyze_continuous && chunk == 0 && options.numops != 1) {
1759                         chunk = 1;
1760                 }
1761         } while (chunk > 0);
1762
1763         printf("Reduced to %d ops\n", options.numops);
1764         ret = run_test(ev, lp_ctx);
1765         if (ret != options.numops - 1) {
1766                 printf("Inconsistent result? ret=%d numops=%d\n", ret, options.numops);
1767         }
1768 }
1769
1770 /* 
1771    start the main gentest process
1772 */
1773 static bool start_gentest(struct event_context *ev,
1774                           struct loadparm_context *lp_ctx)
1775 {
1776         int op;
1777         int ret;
1778
1779         /* allocate the open_handles array */
1780         open_handles = calloc(options.max_open_handles, sizeof(open_handles[0]));
1781
1782         srandom(options.seed);
1783         op_parms = calloc(options.numops, sizeof(op_parms[0]));
1784
1785         /* generate the seeds - after this everything is deterministic */
1786         if (options.use_preset_seeds) {
1787                 int numops;
1788                 char **preset = file_lines_load(options.seeds_file, &numops, NULL);
1789                 if (!preset) {
1790                         printf("Failed to load %s - %s\n", options.seeds_file, strerror(errno));
1791                         exit(1);
1792                 }
1793                 if (numops < options.numops) {
1794                         options.numops = numops;
1795                 }
1796                 for (op=0;op<options.numops;op++) {
1797                         if (!preset[op]) {
1798                                 printf("Not enough seeds in %s\n", options.seeds_file);
1799                                 exit(1);
1800                         }
1801                         op_parms[op].seed = atoi(preset[op]);
1802                 }
1803                 printf("Loaded %d seeds from %s\n", options.numops, options.seeds_file);
1804         } else {
1805                 for (op=0; op<options.numops; op++) {
1806                         op_parms[op].seed = random();
1807                 }
1808         }
1809
1810         ret = run_test(ev, lp_ctx);
1811
1812         if (ret != options.numops && options.analyze) {
1813                 options.numops = ret+1;
1814                 backtrack_analyze(ev, lp_ctx);
1815         } else if (options.analyze_always) {
1816                 backtrack_analyze(ev, lp_ctx);
1817         } else if (options.analyze_continuous) {
1818                 while (run_test(ev, lp_ctx) == options.numops) ;
1819         }
1820
1821         return ret == options.numops;
1822 }
1823
1824
1825 static void usage(poptContext pc)
1826 {
1827         printf(
1828 "Usage:\n\
1829   gentest //server1/share1 //server2/share2 [options..]\n\
1830 ");
1831         poptPrintUsage(pc, stdout, 0);
1832 }
1833
1834 /**
1835   split a UNC name into server and share names
1836 */
1837 static bool split_unc_name(const char *unc, char **server, char **share)
1838 {
1839         char *p = strdup(unc);
1840         if (!p) return false;
1841         all_string_sub(p, "\\", "/", 0);
1842         if (strncmp(p, "//", 2) != 0) return false;
1843
1844         (*server) = p+2;
1845         p = strchr(*server, '/');
1846         if (!p) return false;
1847
1848         *p = 0;
1849         (*share) = p+1;
1850         
1851         return true;
1852 }
1853
1854
1855
1856 /****************************************************************************
1857   main program
1858 ****************************************************************************/
1859  int main(int argc, char *argv[])
1860 {
1861         int opt;
1862         int i, username_count=0;
1863         bool ret;
1864         char *ignore_file=NULL;
1865         struct event_context *ev;
1866         struct loadparm_context *lp_ctx;
1867         poptContext pc;
1868         int argc_new;
1869         char **argv_new;
1870         enum {OPT_UNCLIST=1000};
1871         struct poptOption long_options[] = {
1872                 POPT_AUTOHELP
1873                 {"seed",          0, POPT_ARG_INT,  &options.seed,      0,      "Seed to use for randomizer",   NULL},
1874                 {"num-ops",       0, POPT_ARG_INT,  &options.numops,    0,      "num ops",      NULL},
1875                 {"oplocks",       0, POPT_ARG_NONE, &options.use_oplocks,0,      "use oplocks", NULL},
1876                 {"showall",       0, POPT_ARG_NONE, &options.showall,    0,      "display all operations", NULL},
1877                 {"analyse",       0, POPT_ARG_NONE, &options.analyze,    0,      "do backtrack analysis", NULL},
1878                 {"analysealways", 0, POPT_ARG_NONE, &options.analyze_always,    0,      "analysis always", NULL},
1879                 {"analysecontinuous", 0, POPT_ARG_NONE, &options.analyze_continuous,    0,      "analysis continuous", NULL},
1880                 {"ignore",        0, POPT_ARG_STRING, &ignore_file,    0,      "ignore from file", NULL},
1881                 {"preset",        0, POPT_ARG_NONE, &options.use_preset_seeds,    0,      "use preset seeds", NULL},
1882                 {"fast",          0, POPT_ARG_NONE, &options.fast_reconnect,    0,      "use fast reconnect", NULL},
1883                 {"unclist",       0, POPT_ARG_STRING,   NULL,   OPT_UNCLIST,    "unclist",      NULL},
1884                 {"seedsfile",     0, POPT_ARG_STRING,  &options.seeds_file, 0,  "seed file",    NULL},
1885                 { "user", 'U',       POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
1886                 POPT_COMMON_SAMBA
1887                 POPT_COMMON_CONNECTION
1888                 POPT_COMMON_CREDENTIALS
1889                 POPT_COMMON_VERSION
1890                 { NULL }
1891         };
1892
1893         memset(&bad_smb2_handle, 0xFF, sizeof(bad_smb2_handle));
1894
1895         setlinebuf(stdout);
1896         options.seed = time(NULL);
1897         options.numops = 1000;
1898         options.max_open_handles = 20;
1899         options.seeds_file = "gentest_seeds.dat";
1900
1901         pc = poptGetContext("gentest", argc, (const char **) argv, long_options, 
1902                             POPT_CONTEXT_KEEP_FIRST);
1903
1904         poptSetOtherOptionHelp(pc, "<unc1> <unc2>");
1905
1906         lp_ctx = cmdline_lp_ctx;
1907         servers[0].credentials = cli_credentials_init(talloc_autofree_context());
1908         servers[1].credentials = cli_credentials_init(talloc_autofree_context());
1909         cli_credentials_guess(servers[0].credentials, lp_ctx);
1910         cli_credentials_guess(servers[1].credentials, lp_ctx);
1911
1912         while((opt = poptGetNextOpt(pc)) != -1) {
1913                 switch (opt) {
1914                 case OPT_UNCLIST:
1915                         lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
1916                         break;
1917                 case 'U':
1918                         if (username_count == 2) {
1919                                 usage(pc);
1920                                 exit(1);
1921                         }
1922                         cli_credentials_parse_string(servers[username_count].credentials, poptGetOptArg(pc), CRED_SPECIFIED);
1923                         username_count++;
1924                         break;
1925                 }
1926         }
1927
1928         if (ignore_file) {
1929                 options.ignore_patterns = file_lines_load(ignore_file, NULL, NULL);
1930         }
1931
1932         argv_new = discard_const_p(char *, poptGetArgs(pc));
1933         argc_new = argc;
1934         for (i=0; i<argc; i++) {
1935                 if (argv_new[i] == NULL) {
1936                         argc_new = i;
1937                         break;
1938                 }
1939         }
1940
1941         if (!(argc_new >= 3)) {
1942                 usage(pc);
1943                 exit(1);
1944         }
1945
1946         setlinebuf(stdout);
1947
1948         setup_logging("gentest", DEBUG_STDOUT);
1949
1950         if (argc < 3 || argv[1][0] == '-') {
1951                 usage(pc);
1952                 exit(1);
1953         }
1954
1955         setup_logging(argv[0], DEBUG_STDOUT);
1956
1957         for (i=0;i<NSERVERS;i++) {
1958                 const char *share = argv[1+i];
1959                 if (!split_unc_name(share, &servers[i].server_name, &servers[i].share_name)) {
1960                         printf("Invalid share name '%s'\n", share);
1961                         return -1;
1962                 }
1963         }
1964
1965         if (username_count == 0) {
1966                 usage(pc);
1967                 return -1;
1968         }
1969         if (username_count == 1) {
1970                 servers[1].credentials = servers[0].credentials;
1971         }
1972
1973         printf("seed=%u\n", options.seed);
1974
1975         ev = event_context_init(talloc_autofree_context());
1976
1977         gensec_init(lp_ctx);
1978
1979         ret = start_gentest(ev, lp_ctx);
1980
1981         if (ret) {
1982                 printf("gentest completed - no errors\n");
1983         } else {
1984                 printf("gentest failed\n");
1985         }
1986
1987         return ret?0:-1;
1988 }