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