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