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