2 Unix SMB/CIFS implementation.
4 generic testing tool - version with SMB2 support
6 Copyright (C) Andrew Tridgell 2003-2008
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.
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.
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/>.
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"
43 static struct gentest_options {
47 int analyze_continuous;
48 uint_t max_open_handles;
52 char **ignore_patterns;
53 const char *seeds_file;
61 /* mapping between open handles on the server and local handles */
65 struct smb2_handle server_handle[NSERVERS];
68 static uint_t num_open_handles;
70 /* state information for the servers. We open NINSTANCES connections to
73 struct smb2_tree *tree[NINSTANCES];
76 struct cli_credentials *credentials;
79 /* the seeds and flags for each operation */
86 /* oplock break info */
89 struct smb2_handle server_handle;
93 } oplocks[NSERVERS][NINSTANCES];
95 /* change notify reply info */
99 union smb_notify notify;
100 } notifies[NSERVERS][NINSTANCES];
102 /* info relevant to the current operation */
111 static struct smb2_handle bad_smb2_handle;
114 #define BAD_HANDLE 0xFFFE
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);
121 check if a string should be ignored. This is used as the basis
122 for all error ignore settings
124 static bool ignore_pattern(const char *str)
127 if (!options.ignore_patterns) return false;
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));
139 /*****************************************************
140 connect to the servers
141 *******************************************************/
142 static bool connect_servers_fast(void)
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)) {
155 open_handles[h].active = false;
165 /*****************************************************
166 connect to the servers
167 *******************************************************/
168 static bool connect_servers(struct event_context *ev,
169 struct loadparm_context *lp_ctx)
173 if (options.fast_reconnect && servers[0].tree[0]) {
174 if (connect_servers_fast()) {
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;
190 for (i=0;i<NSERVERS;i++) {
191 for (j=0;j<NINSTANCES;j++) {
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);
197 cli_credentials_set_workstation(servers[i].credentials,
198 "gentest", CRED_SPECIFIED);
200 status = smb2_connect(NULL, servers[i].server_name,
201 servers[i].share_name,
202 lp_resolve_context(lp_ctx),
203 servers[i].credentials,
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,
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);
223 work out the time skew between the servers - be conservative
225 static uint_t time_skew(void)
228 ret = labs(servers[0].tree[0]->session->transport->negotiate.system_time -
229 servers[1].tree[0]->session->transport->negotiate.system_time);
234 static bool smb2_handle_equal(const struct smb2_handle *h1, const struct smb2_handle *h2)
236 return memcmp(h1, h2, sizeof(struct smb2_handle)) == 0;
240 turn a server handle into a local handle
242 static uint_t fnum_to_handle(int server, int instance, struct smb2_handle server_handle)
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)) {
252 printf("Invalid server handle in fnum_to_handle on server %d instance %d\n",
258 add some newly opened handles
260 static void gen_add_handle(int instance, const char *name, struct smb2_handle handles[NSERVERS])
263 for (h=0;h<options.max_open_handles;h++) {
264 if (!open_handles[h].active) break;
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++) {
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",
278 printf("Recovered handle %d\n", h);
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;
289 printf("OPEN num_open_handles=%d h=%d (%s)\n",
290 num_open_handles, h, name);
294 remove a closed handle
296 static void gen_remove_handle(int instance, struct smb2_handle handles[NSERVERS])
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;
304 printf("CLOSE num_open_handles=%d h=%d (%s)\n",
306 open_handles[h].name);
310 printf("Removing invalid handle!?\n");
315 return true with 'chance' probability as a percentage
317 static bool gen_chance(uint_t chance)
319 return ((random() % 100) <= chance);
323 map an internal handle number to a server handle
325 static struct smb2_handle gen_lookup_handle(int server, uint16_t handle)
327 if (handle == BAD_HANDLE) return bad_smb2_handle;
328 return open_handles[handle].server_handle[server];
334 static uint16_t gen_fnum(int instance)
339 if (gen_chance(20)) return BAD_HANDLE;
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) {
352 return a file handle, but skewed so we don't close the last
353 couple of handles too readily
355 static uint16_t gen_fnum_close(int instance)
357 if (num_open_handles < 5) {
358 if (gen_chance(90)) return BAD_HANDLE;
361 return gen_fnum(instance);
365 generate an integer in a specified range
367 static int gen_int_range(uint64_t min, uint64_t max)
370 return min + (r % (1+max-min));
374 return a fnum for use as a root fid
375 be careful to call GEN_SET_FNUM() when you use this!
377 static uint16_t gen_root_fid(int instance)
379 if (gen_chance(5)) return gen_fnum(instance);
384 generate a file offset
386 static int gen_offset(void)
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);
396 static int gen_io_count(void)
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);
406 static const char *gen_fname(void)
408 const char *names[] = {"gentest\\gentest.dat",
413 "gentest\\foo4:teststream1",
414 "gentest\\foo4:teststream2",
416 "gentest\\foo5.exe:teststream3",
417 "gentest\\foo5.exe:teststream4",
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",
428 /* this causes problem with w2k3 */
431 "gentest\\a_very_long_name.bin",
437 i = gen_int_range(0, ARRAY_SIZE(names)-1);
438 } while (ignore_pattern(names[i]));
444 generate a filename with a higher chance of choosing an already
447 static const char *gen_fname_open(int instance)
450 h = gen_fnum(instance);
451 if (h == BAD_HANDLE) {
454 return open_handles[h].name;
458 generate a wildcard pattern
460 static const char *gen_pattern(void)
463 const char *names[] = {"gentest\\*.dat",
466 "gentest\\blah\\*.*",
470 if (gen_chance(50)) return gen_fname();
473 i = gen_int_range(0, ARRAY_SIZE(names)-1);
474 } while (ignore_pattern(names[i]));
479 static uint32_t gen_bits_levels(int nlevels, ...)
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)) {
491 return mask & random();
501 static uint32_t gen_bits_mask(uint_t mask)
503 uint_t ret = random();
508 generate a bitmask with high probability of the first mask
509 and low of the second
511 static uint32_t gen_bits_mask2(uint32_t mask1, uint32_t mask2)
513 if (gen_chance(10)) return gen_bits_mask(mask2);
514 return gen_bits_mask(mask1);
518 generate reserved values
520 static uint64_t gen_reserved8(void)
522 return gen_bits_mask(0xFF);
525 static uint64_t gen_reserved16(void)
527 return gen_bits_mask(0xFFFF);
530 static uint64_t gen_reserved32(void)
532 return gen_bits_mask(0xFFFFFFFF);
535 static uint64_t gen_reserved64(void)
537 return gen_bits_mask(0xFFFFFFFF) | (((uint64_t)gen_bits_mask(0xFFFFFFFF))<<32);
545 static bool gen_bool(void)
547 return gen_bits_mask2(0x1, 0xFF);
551 return a set of lock flags
553 static uint16_t gen_lock_flags(void)
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);
564 generate a lock count
566 static off_t gen_lock_count(void)
568 return gen_int_range(0, 3);
572 generate a NT access mask
574 static uint32_t gen_access_mask(void)
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);
582 generate a ntcreatex create options bitfield
584 static uint32_t gen_create_options(void)
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);
592 generate a ntcreatex open disposition
594 static uint32_t gen_open_disp(void)
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);
602 generate a file attrib combination
604 static uint32_t gen_attrib(void)
606 if (gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
607 return gen_bits_mask(FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
611 generate a unix timestamp
613 static time_t gen_timet(void)
615 if (gen_chance(30)) return 0;
616 return (time_t)random();
620 generate a unix timestamp
622 static NTTIME gen_nttime(void)
625 unix_to_nt_time(&ret, gen_timet());
630 generate a file allocation size
632 static uint_t gen_alloc_size(void)
636 if (gen_chance(30)) return 0;
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);
647 generate an ea_struct
649 static struct ea_struct gen_ea_struct(void)
652 const char *names[] = {"EAONE",
657 "AVERYLONGATTRIBUTENAME"};
658 const char *values[] = {"VALUE1",
663 "ASOMEWHATLONGERATTRIBUTEVALUE"};
669 i = gen_int_range(0, ARRAY_SIZE(names)-1);
670 } while (ignore_pattern(names[i]));
672 ea.name.s = names[i];
675 i = gen_int_range(0, ARRAY_SIZE(values)-1);
676 } while (ignore_pattern(values[i]));
678 ea.value = data_blob(values[i], strlen(values[i]));
680 if (gen_chance(10)) ea.flags = gen_bits_mask(0xFF);
687 generate an ea_struct
689 static struct smb_ea_list gen_ea_list(void)
691 struct smb_ea_list eas;
693 if (options.no_eas) {
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();
705 static void oplock_handler_close_recv(struct smb2_request *req)
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");
716 static void oplock_handler_ack_callback(struct smb2_request *req)
719 struct smb2_break br;
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");
728 static bool send_oplock_ack(struct smb2_tree *tree, struct smb2_handle handle,
731 struct smb2_break br;
732 struct smb2_request *req;
735 br.in.file.handle = handle;
736 br.in.oplock_level = level;
737 br.in.reserved = gen_reserved8();
738 br.in.reserved2 = gen_reserved32();
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;
748 the oplock handler will either ack the break or close the file
750 static bool oplock_handler(struct smb2_transport *transport, const struct smb2_handle *handle,
751 uint8_t level, void *private_data)
753 struct smb2_close io;
756 struct smb2_tree *tree = NULL;
757 struct smb2_request *req;
759 srandom(current_op.seed);
760 do_close = gen_chance(50);
762 i = ((uintptr_t)private_data) >> 8;
763 j = ((uintptr_t)private_data) & 0xFF;
765 if (i >= NSERVERS || j >= NINSTANCES) {
766 printf("Bad private_data in oplock_handler\n");
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);
778 printf("Oplock break not for one of our trees!?\n");
783 printf("oplock ack handle=%d\n", oplocks[i][j].handle);
784 return send_oplock_ack(tree, *handle, level);
787 printf("oplock close fnum=%d\n", oplocks[i][j].handle);
790 io.in.file.handle = *handle;
792 req = smb2_close_send(tree, &io);
795 printf("WARNING: close failed in oplock_handler_close\n");
799 req->async.fn = oplock_handler_close_recv;
800 req->async.private_data = NULL;
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
811 static void idle_func(struct smb2_transport *transport, void *private)
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);
827 compare NTSTATUS, using checking ignored patterns
829 static bool compare_status(NTSTATUS status1, NTSTATUS status2)
831 if (NT_STATUS_EQUAL(status1, status2)) return true;
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;
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))) {
846 check for pending packets on all connections
848 static void check_pending(void)
854 for (j=0;j<NINSTANCES;j++) {
855 for (i=0;i<NSERVERS;i++) {
856 // smb2_transport_process(servers[i].tree[j]->session->transport);
863 check that the same oplock breaks have been received by all instances
865 static bool check_oplocks(const char *call)
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,
884 oplocks[i][j].got_break,
885 oplocks[i][j].handle,
886 oplocks[i][j].level);
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;
900 gen_remove_handle(j, fnums);
910 check that the same change notify info has been received by all instances
912 static bool check_notifies(const char *call)
921 for (j=0;j<NINSTANCES;j++) {
922 for (i=1;i<NSERVERS;i++) {
924 union smb_notify not1, not2;
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);
934 if (notifies[0][j].notify_count == 0) continue;
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));
944 if (!NT_STATUS_IS_OK(notifies[0][j].status)) {
948 not1 = notifies[0][j].notify;
949 not2 = notifies[i][j].notify;
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);
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);
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);
977 ZERO_STRUCT(notifies);
983 #define GEN_COPY_PARM do { \
985 for (i=1;i<NSERVERS;i++) { \
990 #define GEN_CALL(call) do { \
992 ZERO_STRUCT(oplocks); \
993 ZERO_STRUCT(notifies); \
994 for (i=0;i<NSERVERS;i++) { \
995 struct smb2_tree *tree = servers[i].tree[instance]; \
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])); \
1006 if (!check_oplocks(#call)) return false; \
1007 if (!check_notifies(#call)) return false; \
1008 if (!NT_STATUS_IS_OK(status[0])) { \
1013 #define ADD_HANDLE(name, field) do { \
1014 struct smb2_handle handles[NSERVERS]; \
1016 for (i=0;i<NSERVERS;i++) { \
1017 handles[i] = parm[i].field; \
1019 gen_add_handle(instance, name, handles); \
1022 #define REMOVE_HANDLE(field) do { \
1023 struct smb2_handle handles[NSERVERS]; \
1025 for (i=0;i<NSERVERS;i++) { \
1026 handles[i] = parm[i].field; \
1028 gen_remove_handle(instance, handles); \
1031 #define GEN_SET_FNUM(field) do { \
1033 for (i=0;i<NSERVERS;i++) { \
1034 parm[i].field = gen_lookup_handle(i, parm[i].field.data[0]); \
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); \
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); \
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); \
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); \
1066 CHECK_EQUAL(field.private_length); \
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); \
1074 CHECK_EQUAL(field.length); \
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)); \
1089 generate ntcreatex operations
1091 static bool handler_create(int instance)
1093 struct smb2_create parm[NSERVERS];
1094 NTSTATUS status[NSERVERS];
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);
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();
1113 if (!options.use_oplocks) {
1114 /* mask out oplocks */
1115 parm[0].in.oplock_level = 0;
1119 GEN_CALL(smb2_create(tree, current_op.mem_ctx, &parm[i]));
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);
1133 /* ntcreatex creates a new file handle */
1134 ADD_HANDLE(parm[0].in.fname, out.file.handle);
1140 generate close operations
1142 static bool handler_close(int instance)
1144 struct smb2_close parm[NSERVERS];
1145 NTSTATUS status[NSERVERS];
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);
1152 GEN_SET_FNUM(in.file.handle);
1153 GEN_CALL(smb2_close(tree, &parm[i]));
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);
1165 REMOVE_HANDLE(in.file.handle);
1171 generate read operations
1173 static bool handler_read(int instance)
1175 struct smb2_read parm[NSERVERS];
1176 NTSTATUS status[NSERVERS];
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);
1189 GEN_SET_FNUM(in.file.handle);
1190 GEN_CALL(smb2_read(tree, current_op.mem_ctx, &parm[i]));
1192 CHECK_EQUAL(out.remaining);
1193 CHECK_EQUAL(out.reserved);
1194 CHECK_EQUAL(out.data.length);
1200 generate write operations
1202 static bool handler_write(int instance)
1204 struct smb2_write parm[NSERVERS];
1205 NTSTATUS status[NSERVERS];
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,
1215 GEN_SET_FNUM(in.file.handle);
1216 GEN_CALL(smb2_write(tree, &parm[i]));
1218 CHECK_EQUAL(out._pad);
1219 CHECK_EQUAL(out.nwritten);
1220 CHECK_EQUAL(out.unknown1);
1226 generate lockingx operations
1228 static bool handler_lock(int instance)
1230 struct smb2_lock parm[NSERVERS];
1231 NTSTATUS status[NSERVERS];
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();
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);
1252 GEN_SET_FNUM(in.file.handle);
1253 GEN_CALL(smb2_lock(tree, &parm[i]));
1259 generate flush operations
1261 static bool handler_flush(int instance)
1263 struct smb2_flush parm[NSERVERS];
1264 NTSTATUS status[NSERVERS];
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();
1272 GEN_SET_FNUM(in.file.handle);
1273 GEN_CALL(smb2_flush(tree, &parm[i]));
1275 CHECK_EQUAL(out.reserved);
1281 generate echo operations
1283 static bool handler_echo(int instance)
1285 NTSTATUS status[NSERVERS];
1287 GEN_CALL(smb2_keepalive(tree->session->transport));
1295 generate a fileinfo query structure
1297 static void gen_fileinfo(int instance, union smb_fileinfo *info)
1300 #define LVL(v) {RAW_FILEINFO_ ## v, "RAW_FILEINFO_" #v}
1302 enum smb_fileinfo_level level;
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),
1314 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1315 } while (ignore_pattern(levels[i].name));
1317 info->generic.level = levels[i].level;
1321 compare returned fileinfo structures
1323 static bool cmp_fileinfo(int instance,
1324 union smb_fileinfo parm[NSERVERS],
1325 NTSTATUS status[NSERVERS])
1329 switch (parm[0].generic.level) {
1330 case RAW_FILEINFO_GENERIC:
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:
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);
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);
1366 case RAW_FILEINFO_EA_INFORMATION:
1367 CHECK_EQUAL(ea_info.out.ea_size);
1370 case RAW_FILEINFO_NAME_INFORMATION:
1371 CHECK_WSTR_EQUAL(name_info.out.fname);
1374 case RAW_FILEINFO_ALT_NAME_INFORMATION:
1375 CHECK_WSTR_EQUAL(alt_name_info.out.fname);
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);
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);
1395 case RAW_FILEINFO_INTERNAL_INFORMATION:
1396 CHECK_EQUAL(internal_information.out.file_id);
1399 case RAW_FILEINFO_ACCESS_INFORMATION:
1400 CHECK_EQUAL(access_information.out.access_flags);
1403 case RAW_FILEINFO_POSITION_INFORMATION:
1404 CHECK_EQUAL(position_information.out.position);
1407 case RAW_FILEINFO_MODE_INFORMATION:
1408 CHECK_EQUAL(mode_information.out.mode);
1411 case RAW_FILEINFO_ALIGNMENT_INFORMATION:
1412 CHECK_EQUAL(alignment_information.out.alignment_requirement);
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);
1425 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1426 CHECK_ATTRIB(attribute_tag_information.out.attrib);
1427 CHECK_EQUAL(attribute_tag_information.out.reparse_tag);
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);
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);
1461 /* Unhandled levels */
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:
1475 generate qfileinfo operations
1477 static bool handler_qfileinfo(int instance)
1479 union smb_fileinfo parm[NSERVERS];
1480 NTSTATUS status[NSERVERS];
1482 parm[0].generic.in.file.handle.data[0] = gen_fnum(instance);
1484 gen_fileinfo(instance, &parm[0]);
1487 GEN_SET_FNUM(generic.in.file.handle);
1488 GEN_CALL(smb2_getinfo_file(tree, current_op.mem_ctx, &parm[i]));
1490 return cmp_fileinfo(instance, parm, status);
1495 generate a fileinfo query structure
1497 static void gen_setfileinfo(int instance, union smb_setfileinfo *info)
1501 #define LVL(v) {RAW_SFILEINFO_ ## v, "RAW_SFILEINFO_" #v}
1503 enum smb_setfileinfo_level level;
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)
1513 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1514 } while (ignore_pattern(levels[i].name));
1516 info->generic.level = levels[i].level;
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:
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();
1536 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
1537 info->disposition_info.in.delete_on_close = gen_bool();
1539 case RAW_SFILEINFO_ALLOCATION_INFORMATION:
1540 info->allocation_info.in.alloc_size = gen_alloc_size();
1542 case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
1543 info->end_of_file_info.in.size = gen_offset();
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);
1551 case RAW_SFILEINFO_POSITION_INFORMATION:
1552 info->position_information.in.position = gen_offset();
1554 case RAW_SFILEINFO_MODE_INFORMATION:
1555 info->mode_information.in.mode = gen_bits_mask(0xFFFFFFFF);
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:
1575 generate setfileinfo operations
1577 static bool handler_sfileinfo(int instance)
1579 union smb_setfileinfo parm[NSERVERS];
1580 NTSTATUS status[NSERVERS];
1582 parm[0].generic.in.file.fnum = gen_fnum(instance);
1584 gen_setfileinfo(instance, &parm[0]);
1587 GEN_SET_FNUM(generic.in.file.handle);
1588 GEN_CALL(smb2_setinfo_file(tree, &parm[i]));
1594 wipe any relevant files
1596 static void wipe_files(void)
1601 if (options.skip_cleanup) {
1605 for (i=0;i<NSERVERS;i++) {
1606 int n = smb2_deltree(servers[i].tree[0], "gentest");
1608 printf("Failed to wipe tree on server %d\n", i);
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));
1617 printf("Deleted %d files on server %d\n", n, i);
1623 dump the current seeds - useful for continuing a backtrack
1625 static void dump_seeds(void)
1630 if (!options.seeds_file) {
1633 f = fopen("seeds.tmp", "w");
1636 for (i=0;i<options.numops;i++) {
1637 fprintf(f, "%u\n", op_parms[i].seed);
1640 rename("seeds.tmp", options.seeds_file);
1646 the list of top-level operations that we will generate
1650 bool (*handler)(int instance);
1651 int count, success_count;
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},
1666 run the test with the current set of op_parms parameters
1667 return the number of operations that completed successfully
1669 static int run_test(struct event_context *ev, struct loadparm_context *lp_ctx)
1673 if (!connect_servers(ev, lp_ctx)) {
1674 printf("Failed to connect to servers\n");
1680 /* wipe any leftover files from old runs */
1683 /* reset the open handles array */
1684 memset(open_handles, 0, options.max_open_handles * sizeof(open_handles[0]));
1685 num_open_handles = 0;
1687 for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1688 gen_ops[i].count = 0;
1689 gen_ops[i].success_count = 0;
1692 for (op=0; op<options.numops; op++) {
1693 int instance, which_op;
1696 if (op_parms[op].disabled) continue;
1698 srandom(op_parms[op].seed);
1700 instance = gen_int_range(0, NINSTANCES-1);
1702 /* generate a non-ignored operation */
1704 which_op = gen_int_range(0, ARRAY_SIZE(gen_ops)-1);
1705 } while (ignore_pattern(gen_ops[which_op].name));
1707 DEBUG(3,("Generating op %s on instance %d\n",
1708 gen_ops[which_op].name, instance));
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);
1716 ret = gen_ops[which_op].handler(instance);
1718 talloc_free(current_op.mem_ctx);
1720 gen_ops[which_op].count++;
1721 if (NT_STATUS_IS_OK(current_op.status)) {
1722 gen_ops[which_op].success_count++;
1726 printf("Failed at operation %d - %s\n",
1727 op, gen_ops[which_op].name);
1731 if (op % 100 == 0) {
1736 for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1737 printf("Op %-10s got %d/%d success\n",
1739 gen_ops[i].success_count,
1747 perform a backtracking analysis of the minimal set of operations
1748 to generate an error
1750 static void backtrack_analyze(struct event_context *ev,
1751 struct loadparm_context *lp_ctx)
1755 chunk = options.numops / 2;
1760 chunk > 0 && base+chunk < options.numops && options.numops > 1; ) {
1763 chunk = MIN(chunk, options.numops / 2);
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;
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;
1777 if (ret == options.numops) {
1778 /* this chunk is needed */
1780 } else if (ret < base) {
1781 printf("damn - inconsistent errors! found early error\n");
1782 options.numops = ret+1;
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);
1798 if (options.analyze_continuous && chunk == 0 && options.numops != 1) {
1801 } while (chunk > 0);
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);
1811 start the main gentest process
1813 static bool start_gentest(struct event_context *ev,
1814 struct loadparm_context *lp_ctx)
1819 /* allocate the open_handles array */
1820 open_handles = calloc(options.max_open_handles, sizeof(open_handles[0]));
1822 srandom(options.seed);
1823 op_parms = calloc(options.numops, sizeof(op_parms[0]));
1825 /* generate the seeds - after this everything is deterministic */
1826 if (options.use_preset_seeds) {
1828 char **preset = file_lines_load(options.seeds_file, &numops, NULL);
1830 printf("Failed to load %s - %s\n", options.seeds_file, strerror(errno));
1833 if (numops < options.numops) {
1834 options.numops = numops;
1836 for (op=0;op<options.numops;op++) {
1838 printf("Not enough seeds in %s\n", options.seeds_file);
1841 op_parms[op].seed = atoi(preset[op]);
1843 printf("Loaded %d seeds from %s\n", options.numops, options.seeds_file);
1845 for (op=0; op<options.numops; op++) {
1846 op_parms[op].seed = random();
1850 ret = run_test(ev, lp_ctx);
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) ;
1861 return ret == options.numops;
1865 static void usage(poptContext pc)
1869 gentest //server1/share1 //server2/share2 [options..]\n\
1871 poptPrintUsage(pc, stdout, 0);
1875 split a UNC name into server and share names
1877 static bool split_unc_name(const char *unc, char **server, char **share)
1879 char *p = strdup(unc);
1880 if (!p) return false;
1881 all_string_sub(p, "\\", "/", 0);
1882 if (strncmp(p, "//", 2) != 0) return false;
1885 p = strchr(*server, '/');
1886 if (!p) return false;
1896 /****************************************************************************
1898 ****************************************************************************/
1899 int main(int argc, char *argv[])
1902 int i, username_count=0;
1904 char *ignore_file=NULL;
1905 struct event_context *ev;
1906 struct loadparm_context *lp_ctx;
1910 enum {OPT_UNCLIST=1000};
1911 struct poptOption long_options[] = {
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},
1930 POPT_COMMON_CONNECTION
1931 POPT_COMMON_CREDENTIALS
1936 memset(&bad_smb2_handle, 0xFF, sizeof(bad_smb2_handle));
1939 options.seed = time(NULL);
1940 options.numops = 1000;
1941 options.max_open_handles = 20;
1942 options.seeds_file = "gentest_seeds.dat";
1944 pc = poptGetContext("gentest", argc, (const char **) argv, long_options,
1945 POPT_CONTEXT_KEEP_FIRST);
1947 poptSetOtherOptionHelp(pc, "<unc1> <unc2>");
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);
1955 while((opt = poptGetNextOpt(pc)) != -1) {
1958 lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
1961 if (username_count == 2) {
1965 cli_credentials_parse_string(servers[username_count].credentials, poptGetOptArg(pc), CRED_SPECIFIED);
1972 options.ignore_patterns = file_lines_load(ignore_file, NULL, NULL);
1975 argv_new = discard_const_p(char *, poptGetArgs(pc));
1977 for (i=0; i<argc; i++) {
1978 if (argv_new[i] == NULL) {
1984 if (!(argc_new >= 3)) {
1991 setup_logging("gentest", DEBUG_STDOUT);
1993 if (argc < 3 || argv[1][0] == '-') {
1998 setup_logging(argv[0], DEBUG_STDOUT);
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);
2008 if (username_count == 0) {
2012 if (username_count == 1) {
2013 servers[1].credentials = servers[0].credentials;
2016 printf("seed=%u\n", options.seed);
2018 ev = event_context_init(talloc_autofree_context());
2020 gensec_init(lp_ctx);
2022 ret = start_gentest(ev, lp_ctx);
2025 printf("gentest completed - no errors\n");
2027 printf("gentest failed\n");