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/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;
62 /* mapping between open handles on the server and local handles */
66 struct smb2_handle server_handle[NSERVERS];
69 static uint_t num_open_handles;
71 /* state information for the servers. We open NINSTANCES connections to
74 struct smb2_tree *tree[NINSTANCES];
77 struct cli_credentials *credentials;
80 /* the seeds and flags for each operation */
87 /* oplock break info */
90 struct smb2_handle server_handle;
94 } oplocks[NSERVERS][NINSTANCES];
96 /* change notify reply info */
100 union smb_notify notify;
101 } notifies[NSERVERS][NINSTANCES];
103 /* info relevant to the current operation */
112 static struct smb2_handle bad_smb2_handle;
115 #define BAD_HANDLE 0xFFFE
117 static bool oplock_handler(struct smb2_transport *transport, const struct smb2_handle *handle,
118 uint8_t level, void *private_data);
119 static void idle_func(struct smb2_transport *transport, void *private);
122 check if a string should be ignored. This is used as the basis
123 for all error ignore settings
125 static bool ignore_pattern(const char *str)
128 if (!options.ignore_patterns) return false;
130 for (i=0;options.ignore_patterns[i];i++) {
131 if (strcmp(options.ignore_patterns[i], str) == 0 ||
132 gen_fnmatch(options.ignore_patterns[i], str) == 0) {
133 DEBUG(2,("Ignoring '%s'\n", str));
140 /*****************************************************
141 connect to the servers
142 *******************************************************/
143 static bool connect_servers_fast(void)
147 /* close all open files */
148 for (h=0;h<options.max_open_handles;h++) {
149 if (!open_handles[h].active) continue;
150 for (i=0;i<NSERVERS;i++) {
151 NTSTATUS status = smb2_util_close(servers[i].tree[open_handles[h].instance],
152 open_handles[h].server_handle[i]);
153 if (NT_STATUS_IS_ERR(status)) {
156 open_handles[h].active = false;
166 /*****************************************************
167 connect to the servers
168 *******************************************************/
169 static bool connect_servers(struct event_context *ev,
170 struct loadparm_context *lp_ctx)
174 if (options.fast_reconnect && servers[0].tree[0]) {
175 if (connect_servers_fast()) {
180 /* close any existing connections */
181 for (i=0;i<NSERVERS;i++) {
182 for (j=0;j<NINSTANCES;j++) {
183 if (servers[i].tree[j]) {
184 smb2_tdis(servers[i].tree[j]);
185 talloc_free(servers[i].tree[j]);
186 servers[i].tree[j] = NULL;
191 for (i=0;i<NSERVERS;i++) {
192 for (j=0;j<NINSTANCES;j++) {
194 printf("Connecting to \\\\%s\\%s as %s - instance %d\n",
195 servers[i].server_name, servers[i].share_name,
196 servers[i].credentials->username, j);
198 cli_credentials_set_workstation(servers[i].credentials,
199 "gentest", CRED_SPECIFIED);
201 status = smb2_connect(NULL, servers[i].server_name,
202 servers[i].share_name,
203 lp_resolve_context(lp_ctx),
204 servers[i].credentials,
207 if (!NT_STATUS_IS_OK(status)) {
208 printf("Failed to connect to \\\\%s\\%s - %s\n",
209 servers[i].server_name, servers[i].share_name,
214 servers[i].tree[j]->session->transport->oplock.handler = oplock_handler;
215 servers[i].tree[j]->session->transport->oplock.private_data = (void *)(uintptr_t)((i<<8)|j);
216 smb2_transport_idle_handler(servers[i].tree[j]->session->transport, idle_func, 50000, NULL);
224 work out the time skew between the servers - be conservative
226 static uint_t time_skew(void)
229 ret = labs(servers[0].tree[0]->session->transport->negotiate.system_time -
230 servers[1].tree[0]->session->transport->negotiate.system_time);
235 static bool smb2_handle_equal(const struct smb2_handle *h1, const struct smb2_handle *h2)
237 return memcmp(h1, h2, sizeof(struct smb2_handle)) == 0;
241 turn a server handle into a local handle
243 static uint_t fnum_to_handle(int server, int instance, struct smb2_handle server_handle)
246 for (i=0;i<options.max_open_handles;i++) {
247 if (!open_handles[i].active ||
248 instance != open_handles[i].instance) continue;
249 if (smb2_handle_equal(&open_handles[i].server_handle[server], &server_handle)) {
253 printf("Invalid server handle in fnum_to_handle on server %d instance %d\n",
259 add some newly opened handles
261 static void gen_add_handle(int instance, const char *name, struct smb2_handle handles[NSERVERS])
264 for (h=0;h<options.max_open_handles;h++) {
265 if (!open_handles[h].active) break;
267 if (h == options.max_open_handles) {
268 /* we have to force close a random handle */
269 h = random() % options.max_open_handles;
270 for (i=0;i<NSERVERS;i++) {
272 status = smb2_util_close(servers[i].tree[open_handles[h].instance],
273 open_handles[h].server_handle[i]);
274 if (NT_STATUS_IS_ERR(status)) {
275 printf("INTERNAL ERROR: Close failed when recovering handle! - %s\n",
279 printf("Recovered handle %d\n", h);
282 for (i=0;i<NSERVERS;i++) {
283 open_handles[h].server_handle[i] = handles[i];
284 open_handles[h].instance = instance;
285 open_handles[h].active = true;
286 open_handles[h].name = name;
290 printf("OPEN num_open_handles=%d h=%d (%s)\n",
291 num_open_handles, h, name);
295 remove a closed handle
297 static void gen_remove_handle(int instance, struct smb2_handle handles[NSERVERS])
300 for (h=0;h<options.max_open_handles;h++) {
301 if (instance == open_handles[h].instance &&
302 smb2_handle_equal(&open_handles[h].server_handle[0], &handles[0])) {
303 open_handles[h].active = false;
305 printf("CLOSE num_open_handles=%d h=%d (%s)\n",
307 open_handles[h].name);
311 printf("Removing invalid handle!?\n");
316 return true with 'chance' probability as a percentage
318 static bool gen_chance(uint_t chance)
320 return ((random() % 100) <= chance);
324 map an internal handle number to a server handle
326 static struct smb2_handle gen_lookup_handle(int server, uint16_t handle)
328 if (handle == BAD_HANDLE) return bad_smb2_handle;
329 return open_handles[handle].server_handle[server];
335 static uint16_t gen_fnum(int instance)
340 if (gen_chance(20)) return BAD_HANDLE;
342 while (num_open_handles > 0 && count++ < 10*options.max_open_handles) {
343 h = random() % options.max_open_handles;
344 if (open_handles[h].active &&
345 open_handles[h].instance == instance) {
353 return a file handle, but skewed so we don't close the last
354 couple of handles too readily
356 static uint16_t gen_fnum_close(int instance)
358 if (num_open_handles < 5) {
359 if (gen_chance(90)) return BAD_HANDLE;
362 return gen_fnum(instance);
366 generate an integer in a specified range
368 static int gen_int_range(uint64_t min, uint64_t max)
371 return min + (r % (1+max-min));
375 return a fnum for use as a root fid
376 be careful to call GEN_SET_FNUM() when you use this!
378 static uint16_t gen_root_fid(int instance)
380 if (gen_chance(5)) return gen_fnum(instance);
385 generate a file offset
387 static int gen_offset(void)
389 if (gen_chance(20)) return 0;
390 // if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF);
391 return gen_int_range(0, 1024*1024);
397 static int gen_io_count(void)
399 if (gen_chance(20)) return 0;
400 // if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF);
401 return gen_int_range(0, 4096);
407 static const char *gen_fname(void)
409 const char *names[] = {"gentest\\gentest.dat",
414 "gentest\\foo4:teststream1",
415 "gentest\\foo4:teststream2",
417 "gentest\\foo5.exe:teststream3",
418 "gentest\\foo5.exe:teststream4",
421 "gentest\\blah\\blergh.txt",
422 "gentest\\blah\\blergh2",
423 "gentest\\blah\\blergh3.txt",
424 "gentest\\blah\\blergh4",
425 "gentest\\blah\\blergh5.txt",
426 "gentest\\blah\\blergh5",
429 /* this causes problem with w2k3 */
432 "gentest\\a_very_long_name.bin",
438 i = gen_int_range(0, ARRAY_SIZE(names)-1);
439 } while (ignore_pattern(names[i]));
445 generate a filename with a higher chance of choosing an already
448 static const char *gen_fname_open(int instance)
451 h = gen_fnum(instance);
452 if (h == BAD_HANDLE) {
455 return open_handles[h].name;
459 generate a wildcard pattern
461 static const char *gen_pattern(void)
464 const char *names[] = {"gentest\\*.dat",
467 "gentest\\blah\\*.*",
471 if (gen_chance(50)) return gen_fname();
474 i = gen_int_range(0, ARRAY_SIZE(names)-1);
475 } while (ignore_pattern(names[i]));
480 static uint32_t gen_bits_levels(int nlevels, ...)
486 va_start(ap, nlevels);
487 for (i=0;i<nlevels;i++) {
488 pct = va_arg(ap, uint32_t);
489 mask = va_arg(ap, uint32_t);
490 if (pct == 100 || gen_chance(pct)) {
492 return mask & random();
502 static uint32_t gen_bits_mask(uint_t mask)
504 uint_t ret = random();
509 generate a bitmask with high probability of the first mask
510 and low of the second
512 static uint32_t gen_bits_mask2(uint32_t mask1, uint32_t mask2)
514 if (!options.valid && gen_chance(10)) return gen_bits_mask(mask2);
515 return gen_bits_mask(mask1);
519 generate reserved values
521 static uint64_t gen_reserved8(void)
523 if (options.valid) return 0;
524 return gen_bits_mask(0xFF);
527 static uint64_t gen_reserved16(void)
529 if (options.valid) return 0;
530 return gen_bits_mask(0xFFFF);
533 static uint64_t gen_reserved32(void)
535 if (options.valid) return 0;
536 return gen_bits_mask(0xFFFFFFFF);
539 static uint64_t gen_reserved64(void)
541 if (options.valid) return 0;
542 return gen_bits_mask(0xFFFFFFFF) | (((uint64_t)gen_bits_mask(0xFFFFFFFF))<<32);
550 static bool gen_bool(void)
552 return gen_bits_mask2(0x1, 0xFF);
556 return a set of lock flags
558 static uint16_t gen_lock_flags(void)
560 if (!options.valid && gen_chance(5)) return gen_bits_mask(0xFFFF);
561 if (gen_chance(20)) return gen_bits_mask(0x1F);
562 if (gen_chance(50)) return SMB2_LOCK_FLAG_UNLOCK;
563 return gen_bits_mask(SMB2_LOCK_FLAG_SHARED |
564 SMB2_LOCK_FLAG_EXCLUSIVE |
565 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY);
569 generate a lock count
571 static off_t gen_lock_count(void)
573 return gen_int_range(0, 3);
577 generate a NT access mask
579 static uint32_t gen_access_mask(void)
582 if (gen_chance(70)) return SEC_FLAG_MAXIMUM_ALLOWED;
583 if (gen_chance(70)) return SEC_FILE_ALL;
584 ret = gen_bits_mask(0xFFFFFFFF);
585 if (options.valid) ret &= ~SEC_MASK_INVALID;
590 generate a ntcreatex create options bitfield
592 static uint32_t gen_create_options(void)
594 if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFFFFFF);
595 if (gen_chance(50)) return 0;
596 return gen_bits_mask(NTCREATEX_OPTIONS_DELETE_ON_CLOSE | NTCREATEX_OPTIONS_DIRECTORY);
600 generate a ntcreatex open disposition
602 static uint32_t gen_open_disp(void)
604 if (gen_chance(50)) return NTCREATEX_DISP_OPEN_IF;
605 if (!options.valid && gen_chance(10)) return gen_bits_mask(0xFFFFFFFF);
606 return gen_int_range(0, 5);
610 generate a file attrib combination
612 static uint32_t gen_attrib(void)
615 if (gen_chance(20)) {
616 ret = gen_bits_mask(0xFFFFFFFF);
617 if (options.valid) ret &= FILE_ATTRIBUTE_ALL_MASK;
620 return gen_bits_mask(FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);
624 generate a unix timestamp
626 static time_t gen_timet(void)
628 if (gen_chance(30)) return 0;
629 return (time_t)random();
633 generate a unix timestamp
635 static NTTIME gen_nttime(void)
638 unix_to_nt_time(&ret, gen_timet());
643 generate a file allocation size
645 static uint_t gen_alloc_size(void)
649 if (gen_chance(30)) return 0;
651 ret = random() % 4*1024*1024;
652 /* give a high chance of a round number */
653 if (gen_chance(60)) {
654 ret &= ~(1024*1024 - 1);
660 generate an ea_struct
662 static struct ea_struct gen_ea_struct(void)
665 const char *names[] = {"EAONE",
670 "AVERYLONGATTRIBUTENAME"};
671 const char *values[] = {"VALUE1",
676 "ASOMEWHATLONGERATTRIBUTEVALUE"};
682 i = gen_int_range(0, ARRAY_SIZE(names)-1);
683 } while (ignore_pattern(names[i]));
685 ea.name.s = names[i];
688 i = gen_int_range(0, ARRAY_SIZE(values)-1);
689 } while (ignore_pattern(values[i]));
691 ea.value = data_blob(values[i], strlen(values[i]));
693 if (gen_chance(10)) ea.flags = gen_bits_mask(0xFF);
700 generate an ea_struct
702 static struct smb_ea_list gen_ea_list(void)
704 struct smb_ea_list eas;
706 if (options.no_eas) {
710 eas.num_eas = gen_int_range(0, 3);
711 eas.eas = talloc_array(current_op.mem_ctx, struct ea_struct, eas.num_eas);
712 for (i=0;i<eas.num_eas;i++) {
713 eas.eas[i] = gen_ea_struct();
718 static void oplock_handler_close_recv(struct smb2_request *req)
721 struct smb2_close io;
722 status = smb2_close_recv(req, &io);
723 if (!NT_STATUS_IS_OK(status)) {
724 printf("close failed in oplock_handler\n");
725 smb_panic("close failed in oplock_handler");
729 static void oplock_handler_ack_callback(struct smb2_request *req)
732 struct smb2_break br;
734 status = smb2_break_recv(req, &br);
735 if (!NT_STATUS_IS_OK(status)) {
736 printf("oplock break ack failed in oplock_handler\n");
737 smb_panic("oplock break ack failed in oplock_handler");
741 static bool send_oplock_ack(struct smb2_tree *tree, struct smb2_handle handle,
744 struct smb2_break br;
745 struct smb2_request *req;
748 br.in.file.handle = handle;
749 br.in.oplock_level = level;
750 br.in.reserved = gen_reserved8();
751 br.in.reserved2 = gen_reserved32();
753 req = smb2_break_send(tree, &br);
754 if (req == NULL) return false;
755 req->async.fn = oplock_handler_ack_callback;
756 req->async.private_data = NULL;
761 the oplock handler will either ack the break or close the file
763 static bool oplock_handler(struct smb2_transport *transport, const struct smb2_handle *handle,
764 uint8_t level, void *private_data)
766 struct smb2_close io;
769 struct smb2_tree *tree = NULL;
770 struct smb2_request *req;
772 srandom(current_op.seed);
773 do_close = gen_chance(50);
775 i = ((uintptr_t)private_data) >> 8;
776 j = ((uintptr_t)private_data) & 0xFF;
778 if (i >= NSERVERS || j >= NINSTANCES) {
779 printf("Bad private_data in oplock_handler\n");
783 oplocks[i][j].got_break = true;
784 oplocks[i][j].server_handle = *handle;
785 oplocks[i][j].handle = fnum_to_handle(i, j, *handle);
786 oplocks[i][j].level = level;
787 oplocks[i][j].do_close = do_close;
788 tree = talloc_get_type(servers[i].tree[j], struct smb2_tree);
791 printf("Oplock break not for one of our trees!?\n");
796 printf("oplock ack handle=%d\n", oplocks[i][j].handle);
797 return send_oplock_ack(tree, *handle, level);
800 printf("oplock close fnum=%d\n", oplocks[i][j].handle);
803 io.in.file.handle = *handle;
805 req = smb2_close_send(tree, &io);
808 printf("WARNING: close failed in oplock_handler_close\n");
812 req->async.fn = oplock_handler_close_recv;
813 req->async.private_data = NULL;
820 the idle function tries to cope with getting an oplock break on a connection, and
821 an operation on another connection blocking until that break is acked
822 we check for operations on all transports in the idle function
824 static void idle_func(struct smb2_transport *transport, void *private)
827 for (i=0;i<NSERVERS;i++) {
828 for (j=0;j<NINSTANCES;j++) {
829 if (servers[i].tree[j] &&
830 transport != servers[i].tree[j]->session->transport) {
831 // smb2_transport_process(servers[i].tree[j]->session->transport);
840 compare NTSTATUS, using checking ignored patterns
842 static bool compare_status(NTSTATUS status1, NTSTATUS status2)
844 if (NT_STATUS_EQUAL(status1, status2)) return true;
846 /* one code being an error and the other OK is always an error */
847 if (NT_STATUS_IS_OK(status1) || NT_STATUS_IS_OK(status2)) return false;
849 /* if we are ignoring one of the status codes then consider this a match */
850 if (ignore_pattern(nt_errstr(status1)) ||
851 ignore_pattern(nt_errstr(status2))) {
859 check for pending packets on all connections
861 static void check_pending(void)
867 for (j=0;j<NINSTANCES;j++) {
868 for (i=0;i<NSERVERS;i++) {
869 // smb2_transport_process(servers[i].tree[j]->session->transport);
876 check that the same oplock breaks have been received by all instances
878 static bool check_oplocks(const char *call)
887 for (j=0;j<NINSTANCES;j++) {
888 for (i=1;i<NSERVERS;i++) {
889 if (oplocks[0][j].got_break != oplocks[i][j].got_break ||
890 oplocks[0][j].handle != oplocks[i][j].handle ||
891 oplocks[0][j].level != oplocks[i][j].level) {
892 if (tries++ < 10) goto again;
893 printf("oplock break inconsistent - %d/%d/%d vs %d/%d/%d\n",
894 oplocks[0][j].got_break,
895 oplocks[0][j].handle,
897 oplocks[i][j].got_break,
898 oplocks[i][j].handle,
899 oplocks[i][j].level);
905 /* if we got a break and closed then remove the handle */
906 for (j=0;j<NINSTANCES;j++) {
907 if (oplocks[0][j].got_break &&
908 oplocks[0][j].do_close) {
909 uint16_t fnums[NSERVERS];
910 for (i=0;i<NSERVERS;i++) {
911 fnums[i] = oplocks[i][j].fnum;
913 gen_remove_handle(j, fnums);
923 check that the same change notify info has been received by all instances
925 static bool check_notifies(const char *call)
934 for (j=0;j<NINSTANCES;j++) {
935 for (i=1;i<NSERVERS;i++) {
937 union smb_notify not1, not2;
939 if (notifies[0][j].notify_count != notifies[i][j].notify_count) {
940 if (tries++ < 10) goto again;
941 printf("Notify count inconsistent %d %d\n",
942 notifies[0][j].notify_count,
943 notifies[i][j].notify_count);
947 if (notifies[0][j].notify_count == 0) continue;
949 if (!NT_STATUS_EQUAL(notifies[0][j].status,
950 notifies[i][j].status)) {
951 printf("Notify status mismatch - %s - %s\n",
952 nt_errstr(notifies[0][j].status),
953 nt_errstr(notifies[i][j].status));
957 if (!NT_STATUS_IS_OK(notifies[0][j].status)) {
961 not1 = notifies[0][j].notify;
962 not2 = notifies[i][j].notify;
964 for (n=0;n<not1.nttrans.out.num_changes;n++) {
965 if (not1.nttrans.out.changes[n].action !=
966 not2.nttrans.out.changes[n].action) {
967 printf("Notify action %d inconsistent %d %d\n", n,
968 not1.nttrans.out.changes[n].action,
969 not2.nttrans.out.changes[n].action);
972 if (strcmp(not1.nttrans.out.changes[n].name.s,
973 not2.nttrans.out.changes[n].name.s)) {
974 printf("Notify name %d inconsistent %s %s\n", n,
975 not1.nttrans.out.changes[n].name.s,
976 not2.nttrans.out.changes[n].name.s);
979 if (not1.nttrans.out.changes[n].name.private_length !=
980 not2.nttrans.out.changes[n].name.private_length) {
981 printf("Notify name length %d inconsistent %d %d\n", n,
982 not1.nttrans.out.changes[n].name.private_length,
983 not2.nttrans.out.changes[n].name.private_length);
990 ZERO_STRUCT(notifies);
996 #define GEN_COPY_PARM do { \
998 for (i=1;i<NSERVERS;i++) { \
1003 #define GEN_CALL(call) do { \
1005 ZERO_STRUCT(oplocks); \
1006 ZERO_STRUCT(notifies); \
1007 for (i=0;i<NSERVERS;i++) { \
1008 struct smb2_tree *tree = servers[i].tree[instance]; \
1011 current_op.status = status[0]; \
1012 for (i=1;i<NSERVERS;i++) { \
1013 if (!compare_status(status[i], status[0])) { \
1014 printf("status different in %s - %s %s\n", #call, \
1015 nt_errstr(status[0]), nt_errstr(status[i])); \
1019 if (!check_oplocks(#call)) return false; \
1020 if (!check_notifies(#call)) return false; \
1021 if (!NT_STATUS_IS_OK(status[0])) { \
1026 #define ADD_HANDLE(name, field) do { \
1027 struct smb2_handle handles[NSERVERS]; \
1029 for (i=0;i<NSERVERS;i++) { \
1030 handles[i] = parm[i].field; \
1032 gen_add_handle(instance, name, handles); \
1035 #define REMOVE_HANDLE(field) do { \
1036 struct smb2_handle handles[NSERVERS]; \
1038 for (i=0;i<NSERVERS;i++) { \
1039 handles[i] = parm[i].field; \
1041 gen_remove_handle(instance, handles); \
1044 #define GEN_SET_FNUM(field) do { \
1046 for (i=0;i<NSERVERS;i++) { \
1047 parm[i].field = gen_lookup_handle(i, parm[i].field.data[0]); \
1051 #define CHECK_EQUAL(field) do { \
1052 if (parm[0].field != parm[1].field && !ignore_pattern(#field)) { \
1053 printf("Mismatch in %s - 0x%llx 0x%llx\n", #field, \
1054 (unsigned long long)parm[0].field, (unsigned long long)parm[1].field); \
1059 #define CHECK_ATTRIB(field) do { \
1060 if (!options.mask_indexing) { \
1061 CHECK_EQUAL(field); \
1062 } else if ((~FILE_ATTRIBUTE_NONINDEXED & parm[0].field) != (~FILE_ATTRIBUTE_NONINDEXED & parm[1].field) && !ignore_pattern(#field)) { \
1063 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
1064 (int)parm[0].field, (int)parm[1].field); \
1069 #define CHECK_WSTR_EQUAL(field) do { \
1070 if ((!parm[0].field.s && parm[1].field.s) || (parm[0].field.s && !parm[1].field.s)) { \
1071 printf("%s is NULL!\n", #field); \
1074 if (parm[0].field.s && strcmp(parm[0].field.s, parm[1].field.s) != 0 && !ignore_pattern(#field)) { \
1075 printf("Mismatch in %s - %s %s\n", #field, \
1076 parm[0].field.s, parm[1].field.s); \
1079 CHECK_EQUAL(field.private_length); \
1082 #define CHECK_BLOB_EQUAL(field) do { \
1083 if (memcmp(parm[0].field.data, parm[1].field.data, parm[0].field.length) != 0 && !ignore_pattern(#field)) { \
1084 printf("Mismatch in %s\n", #field); \
1087 CHECK_EQUAL(field.length); \
1090 #define CHECK_NTTIMES_EQUAL(field) do { \
1091 if (labs(nt_time_to_unix(parm[0].field) - \
1092 nt_time_to_unix(parm[1].field)) > time_skew() && \
1093 !ignore_pattern(#field)) { \
1094 printf("Mismatch in %s - 0x%x 0x%x\n", #field, \
1095 (int)nt_time_to_unix(parm[0].field), \
1096 (int)nt_time_to_unix(parm[1].field)); \
1102 generate ntcreatex operations
1104 static bool handler_create(int instance)
1106 struct smb2_create parm[NSERVERS];
1107 NTSTATUS status[NSERVERS];
1109 ZERO_STRUCT(parm[0]);
1110 parm[0].in.security_flags = gen_bits_levels(3, 90, 0x0, 70, 0x3, 100, 0xFF);
1111 parm[0].in.oplock_level = gen_bits_levels(3, 90, 0x0, 70, 0x9, 100, 0xFF);
1112 parm[0].in.impersonation_level = gen_bits_levels(3, 90, 0x0, 70, 0x3, 100, 0xFFFFFFFF);
1113 parm[0].in.create_flags = gen_reserved64();
1114 parm[0].in.reserved = gen_reserved64();
1115 parm[0].in.desired_access = gen_access_mask();
1116 parm[0].in.file_attributes = gen_attrib();
1117 parm[0].in.share_access = gen_bits_mask2(0x7, 0xFFFFFFFF);
1118 parm[0].in.create_disposition = gen_open_disp();
1119 parm[0].in.create_options = gen_create_options();
1120 parm[0].in.fname = gen_fname_open(instance);
1121 parm[0].in.eas = gen_ea_list();
1123 if (!options.use_oplocks) {
1124 /* mask out oplocks */
1125 parm[0].in.oplock_level = 0;
1128 if (options.valid) {
1129 parm[0].in.security_flags &= 3;
1130 parm[0].in.oplock_level &= 9;
1131 parm[0].in.impersonation_level &= 3;
1135 GEN_CALL(smb2_create(tree, current_op.mem_ctx, &parm[i]));
1137 CHECK_EQUAL(out.oplock_level);
1138 CHECK_EQUAL(out.reserved);
1139 CHECK_EQUAL(out.create_action);
1140 CHECK_NTTIMES_EQUAL(out.create_time);
1141 CHECK_NTTIMES_EQUAL(out.access_time);
1142 CHECK_NTTIMES_EQUAL(out.write_time);
1143 CHECK_NTTIMES_EQUAL(out.change_time);
1144 CHECK_EQUAL(out.alloc_size);
1145 CHECK_EQUAL(out.size);
1146 CHECK_ATTRIB(out.file_attr);
1147 CHECK_EQUAL(out.reserved2);
1149 /* ntcreatex creates a new file handle */
1150 ADD_HANDLE(parm[0].in.fname, out.file.handle);
1156 generate close operations
1158 static bool handler_close(int instance)
1160 struct smb2_close parm[NSERVERS];
1161 NTSTATUS status[NSERVERS];
1163 ZERO_STRUCT(parm[0]);
1164 parm[0].in.file.handle.data[0] = gen_fnum_close(instance);
1165 parm[0].in.flags = gen_bits_mask2(0x1, 0xFFFF);
1168 GEN_SET_FNUM(in.file.handle);
1169 GEN_CALL(smb2_close(tree, &parm[i]));
1171 CHECK_EQUAL(out.flags);
1172 CHECK_EQUAL(out._pad);
1173 CHECK_NTTIMES_EQUAL(out.create_time);
1174 CHECK_NTTIMES_EQUAL(out.access_time);
1175 CHECK_NTTIMES_EQUAL(out.write_time);
1176 CHECK_NTTIMES_EQUAL(out.change_time);
1177 CHECK_EQUAL(out.alloc_size);
1178 CHECK_EQUAL(out.size);
1179 CHECK_ATTRIB(out.file_attr);
1181 REMOVE_HANDLE(in.file.handle);
1187 generate read operations
1189 static bool handler_read(int instance)
1191 struct smb2_read parm[NSERVERS];
1192 NTSTATUS status[NSERVERS];
1194 parm[0].in.file.handle.data[0] = gen_fnum(instance);
1195 parm[0].in.reserved = gen_reserved8();
1196 parm[0].in.length = gen_io_count();
1197 parm[0].in.offset = gen_offset();
1198 parm[0].in.min_count = gen_io_count();
1199 parm[0].in.channel = gen_bits_mask2(0x0, 0xFFFFFFFF);
1200 parm[0].in.remaining = gen_bits_mask2(0x0, 0xFFFFFFFF);
1201 parm[0].in.channel_offset = gen_bits_mask2(0x0, 0xFFFF);
1202 parm[0].in.channel_length = gen_bits_mask2(0x0, 0xFFFF);
1205 GEN_SET_FNUM(in.file.handle);
1206 GEN_CALL(smb2_read(tree, current_op.mem_ctx, &parm[i]));
1208 CHECK_EQUAL(out.remaining);
1209 CHECK_EQUAL(out.reserved);
1210 CHECK_EQUAL(out.data.length);
1216 generate write operations
1218 static bool handler_write(int instance)
1220 struct smb2_write parm[NSERVERS];
1221 NTSTATUS status[NSERVERS];
1223 parm[0].in.file.handle.data[0] = gen_fnum(instance);
1224 parm[0].in.offset = gen_offset();
1225 parm[0].in.unknown1 = gen_bits_mask2(0, 0xFFFFFFFF);
1226 parm[0].in.unknown2 = gen_bits_mask2(0, 0xFFFFFFFF);
1227 parm[0].in.data = data_blob_talloc(current_op.mem_ctx, NULL,
1231 GEN_SET_FNUM(in.file.handle);
1232 GEN_CALL(smb2_write(tree, &parm[i]));
1234 CHECK_EQUAL(out._pad);
1235 CHECK_EQUAL(out.nwritten);
1236 CHECK_EQUAL(out.unknown1);
1242 generate lockingx operations
1244 static bool handler_lock(int instance)
1246 struct smb2_lock parm[NSERVERS];
1247 NTSTATUS status[NSERVERS];
1250 parm[0].level = RAW_LOCK_LOCKX;
1251 parm[0].in.file.handle.data[0] = gen_fnum(instance);
1252 parm[0].in.lock_count = gen_lock_count();
1253 parm[0].in.reserved = gen_reserved32();
1255 parm[0].in.locks = talloc_array(current_op.mem_ctx,
1256 struct smb2_lock_element,
1257 parm[0].in.lock_count);
1258 for (n=0;n<parm[0].in.lock_count;n++) {
1259 parm[0].in.locks[n].offset = gen_offset();
1260 parm[0].in.locks[n].length = gen_io_count();
1261 /* don't yet cope with async replies */
1262 parm[0].in.locks[n].flags = gen_lock_flags() |
1263 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1264 parm[0].in.locks[n].reserved = gen_bits_mask2(0x0, 0xFFFFFFFF);
1268 GEN_SET_FNUM(in.file.handle);
1269 GEN_CALL(smb2_lock(tree, &parm[i]));
1275 generate flush operations
1277 static bool handler_flush(int instance)
1279 struct smb2_flush parm[NSERVERS];
1280 NTSTATUS status[NSERVERS];
1282 ZERO_STRUCT(parm[0]);
1283 parm[0].in.file.handle.data[0] = gen_fnum(instance);
1284 parm[0].in.reserved1 = gen_reserved16();
1285 parm[0].in.reserved2 = gen_reserved32();
1288 GEN_SET_FNUM(in.file.handle);
1289 GEN_CALL(smb2_flush(tree, &parm[i]));
1291 CHECK_EQUAL(out.reserved);
1297 generate echo operations
1299 static bool handler_echo(int instance)
1301 NTSTATUS status[NSERVERS];
1303 GEN_CALL(smb2_keepalive(tree->session->transport));
1311 generate a fileinfo query structure
1313 static void gen_fileinfo(int instance, union smb_fileinfo *info)
1316 #define LVL(v) {RAW_FILEINFO_ ## v, "RAW_FILEINFO_" #v}
1318 enum smb_fileinfo_level level;
1321 LVL(BASIC_INFORMATION),
1322 LVL(STANDARD_INFORMATION), LVL(INTERNAL_INFORMATION), LVL(EA_INFORMATION),
1323 LVL(ACCESS_INFORMATION), LVL(NAME_INFORMATION), LVL(POSITION_INFORMATION),
1324 LVL(MODE_INFORMATION), LVL(ALIGNMENT_INFORMATION), LVL(SMB2_ALL_INFORMATION),
1325 LVL(ALT_NAME_INFORMATION), LVL(STREAM_INFORMATION), LVL(COMPRESSION_INFORMATION),
1326 LVL(NETWORK_OPEN_INFORMATION), LVL(ATTRIBUTE_TAG_INFORMATION),
1327 LVL(SMB2_ALL_EAS), LVL(SMB2_ALL_INFORMATION),
1330 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1331 } while (ignore_pattern(levels[i].name));
1333 info->generic.level = levels[i].level;
1337 compare returned fileinfo structures
1339 static bool cmp_fileinfo(int instance,
1340 union smb_fileinfo parm[NSERVERS],
1341 NTSTATUS status[NSERVERS])
1345 switch (parm[0].generic.level) {
1346 case RAW_FILEINFO_GENERIC:
1349 /* SMB1 specific values */
1350 case RAW_FILEINFO_GETATTR:
1351 case RAW_FILEINFO_GETATTRE:
1352 case RAW_FILEINFO_STANDARD:
1353 case RAW_FILEINFO_EA_SIZE:
1354 case RAW_FILEINFO_ALL_EAS:
1355 case RAW_FILEINFO_IS_NAME_VALID:
1356 case RAW_FILEINFO_BASIC_INFO:
1357 case RAW_FILEINFO_STANDARD_INFO:
1358 case RAW_FILEINFO_EA_INFO:
1359 case RAW_FILEINFO_NAME_INFO:
1360 case RAW_FILEINFO_ALL_INFO:
1361 case RAW_FILEINFO_ALT_NAME_INFO:
1362 case RAW_FILEINFO_STREAM_INFO:
1363 case RAW_FILEINFO_COMPRESSION_INFO:
1366 case RAW_FILEINFO_BASIC_INFORMATION:
1367 CHECK_NTTIMES_EQUAL(basic_info.out.create_time);
1368 CHECK_NTTIMES_EQUAL(basic_info.out.access_time);
1369 CHECK_NTTIMES_EQUAL(basic_info.out.write_time);
1370 CHECK_NTTIMES_EQUAL(basic_info.out.change_time);
1371 CHECK_ATTRIB(basic_info.out.attrib);
1374 case RAW_FILEINFO_STANDARD_INFORMATION:
1375 CHECK_EQUAL(standard_info.out.alloc_size);
1376 CHECK_EQUAL(standard_info.out.size);
1377 CHECK_EQUAL(standard_info.out.nlink);
1378 CHECK_EQUAL(standard_info.out.delete_pending);
1379 CHECK_EQUAL(standard_info.out.directory);
1382 case RAW_FILEINFO_EA_INFORMATION:
1383 CHECK_EQUAL(ea_info.out.ea_size);
1386 case RAW_FILEINFO_NAME_INFORMATION:
1387 CHECK_WSTR_EQUAL(name_info.out.fname);
1390 case RAW_FILEINFO_ALT_NAME_INFORMATION:
1391 CHECK_WSTR_EQUAL(alt_name_info.out.fname);
1394 case RAW_FILEINFO_STREAM_INFORMATION:
1395 CHECK_EQUAL(stream_info.out.num_streams);
1396 for (i=0;i<parm[0].stream_info.out.num_streams;i++) {
1397 CHECK_EQUAL(stream_info.out.streams[i].size);
1398 CHECK_EQUAL(stream_info.out.streams[i].alloc_size);
1399 CHECK_WSTR_EQUAL(stream_info.out.streams[i].stream_name);
1403 case RAW_FILEINFO_COMPRESSION_INFORMATION:
1404 CHECK_EQUAL(compression_info.out.compressed_size);
1405 CHECK_EQUAL(compression_info.out.format);
1406 CHECK_EQUAL(compression_info.out.unit_shift);
1407 CHECK_EQUAL(compression_info.out.chunk_shift);
1408 CHECK_EQUAL(compression_info.out.cluster_shift);
1411 case RAW_FILEINFO_INTERNAL_INFORMATION:
1412 CHECK_EQUAL(internal_information.out.file_id);
1415 case RAW_FILEINFO_ACCESS_INFORMATION:
1416 CHECK_EQUAL(access_information.out.access_flags);
1419 case RAW_FILEINFO_POSITION_INFORMATION:
1420 CHECK_EQUAL(position_information.out.position);
1423 case RAW_FILEINFO_MODE_INFORMATION:
1424 CHECK_EQUAL(mode_information.out.mode);
1427 case RAW_FILEINFO_ALIGNMENT_INFORMATION:
1428 CHECK_EQUAL(alignment_information.out.alignment_requirement);
1431 case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1432 CHECK_NTTIMES_EQUAL(network_open_information.out.create_time);
1433 CHECK_NTTIMES_EQUAL(network_open_information.out.access_time);
1434 CHECK_NTTIMES_EQUAL(network_open_information.out.write_time);
1435 CHECK_NTTIMES_EQUAL(network_open_information.out.change_time);
1436 CHECK_EQUAL(network_open_information.out.alloc_size);
1437 CHECK_EQUAL(network_open_information.out.size);
1438 CHECK_ATTRIB(network_open_information.out.attrib);
1441 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1442 CHECK_ATTRIB(attribute_tag_information.out.attrib);
1443 CHECK_EQUAL(attribute_tag_information.out.reparse_tag);
1446 case RAW_FILEINFO_ALL_INFORMATION:
1447 case RAW_FILEINFO_SMB2_ALL_INFORMATION:
1448 CHECK_NTTIMES_EQUAL(all_info2.out.create_time);
1449 CHECK_NTTIMES_EQUAL(all_info2.out.access_time);
1450 CHECK_NTTIMES_EQUAL(all_info2.out.write_time);
1451 CHECK_NTTIMES_EQUAL(all_info2.out.change_time);
1452 CHECK_ATTRIB(all_info2.out.attrib);
1453 CHECK_EQUAL(all_info2.out.unknown1);
1454 CHECK_EQUAL(all_info2.out.alloc_size);
1455 CHECK_EQUAL(all_info2.out.size);
1456 CHECK_EQUAL(all_info2.out.nlink);
1457 CHECK_EQUAL(all_info2.out.delete_pending);
1458 CHECK_EQUAL(all_info2.out.directory);
1459 CHECK_EQUAL(all_info2.out.file_id);
1460 CHECK_EQUAL(all_info2.out.ea_size);
1461 CHECK_EQUAL(all_info2.out.access_mask);
1462 CHECK_EQUAL(all_info2.out.position);
1463 CHECK_EQUAL(all_info2.out.mode);
1464 CHECK_EQUAL(all_info2.out.alignment_requirement);
1465 CHECK_WSTR_EQUAL(all_info2.out.fname);
1468 case RAW_FILEINFO_SMB2_ALL_EAS:
1469 CHECK_EQUAL(all_eas.out.num_eas);
1470 for (i=0;i<parm[0].all_eas.out.num_eas;i++) {
1471 CHECK_EQUAL(all_eas.out.eas[i].flags);
1472 CHECK_WSTR_EQUAL(all_eas.out.eas[i].name);
1473 CHECK_BLOB_EQUAL(all_eas.out.eas[i].value);
1477 /* Unhandled levels */
1479 case RAW_FILEINFO_SEC_DESC:
1480 case RAW_FILEINFO_EA_LIST:
1481 case RAW_FILEINFO_UNIX_BASIC:
1482 case RAW_FILEINFO_UNIX_LINK:
1483 case RAW_FILEINFO_UNIX_INFO2:
1491 generate qfileinfo operations
1493 static bool handler_qfileinfo(int instance)
1495 union smb_fileinfo parm[NSERVERS];
1496 NTSTATUS status[NSERVERS];
1498 parm[0].generic.in.file.handle.data[0] = gen_fnum(instance);
1500 gen_fileinfo(instance, &parm[0]);
1503 GEN_SET_FNUM(generic.in.file.handle);
1504 GEN_CALL(smb2_getinfo_file(tree, current_op.mem_ctx, &parm[i]));
1506 return cmp_fileinfo(instance, parm, status);
1511 generate a fileinfo query structure
1513 static void gen_setfileinfo(int instance, union smb_setfileinfo *info)
1517 #define LVL(v) {RAW_SFILEINFO_ ## v, "RAW_SFILEINFO_" #v}
1519 enum smb_setfileinfo_level level;
1522 LVL(BASIC_INFORMATION),
1523 LVL(RENAME_INFORMATION), LVL(DISPOSITION_INFORMATION),
1524 LVL(POSITION_INFORMATION), LVL(MODE_INFORMATION),
1525 LVL(ALLOCATION_INFORMATION), LVL(END_OF_FILE_INFORMATION),
1526 LVL(1023), LVL(1025), LVL(1029), LVL(1032), LVL(1039), LVL(1040)
1529 i = gen_int_range(0, ARRAY_SIZE(levels)-1);
1530 } while (ignore_pattern(levels[i].name));
1532 info->generic.level = levels[i].level;
1534 switch (info->generic.level) {
1535 case RAW_SFILEINFO_SETATTR:
1536 case RAW_SFILEINFO_SETATTRE:
1537 case RAW_SFILEINFO_STANDARD:
1538 case RAW_SFILEINFO_EA_SET:
1539 case RAW_SFILEINFO_BASIC_INFO:
1540 case RAW_SFILEINFO_DISPOSITION_INFO:
1541 case RAW_SFILEINFO_END_OF_FILE_INFO:
1542 case RAW_SFILEINFO_ALLOCATION_INFO:
1545 case RAW_SFILEINFO_BASIC_INFORMATION:
1546 info->basic_info.in.create_time = gen_nttime();
1547 info->basic_info.in.access_time = gen_nttime();
1548 info->basic_info.in.write_time = gen_nttime();
1549 info->basic_info.in.change_time = gen_nttime();
1550 info->basic_info.in.attrib = gen_attrib();
1552 case RAW_SFILEINFO_DISPOSITION_INFORMATION:
1553 info->disposition_info.in.delete_on_close = gen_bool();
1555 case RAW_SFILEINFO_ALLOCATION_INFORMATION:
1556 info->allocation_info.in.alloc_size = gen_alloc_size();
1558 case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
1559 info->end_of_file_info.in.size = gen_offset();
1561 case RAW_SFILEINFO_RENAME_INFORMATION:
1562 case RAW_SFILEINFO_RENAME_INFORMATION_SMB2:
1563 info->rename_information.in.overwrite = gen_bool();
1564 info->rename_information.in.root_fid = gen_root_fid(instance);
1565 info->rename_information.in.new_name = gen_fname_open(instance);
1567 case RAW_SFILEINFO_POSITION_INFORMATION:
1568 info->position_information.in.position = gen_offset();
1570 case RAW_SFILEINFO_MODE_INFORMATION:
1571 info->mode_information.in.mode = gen_bits_mask(0xFFFFFFFF);
1573 case RAW_SFILEINFO_GENERIC:
1574 case RAW_SFILEINFO_SEC_DESC:
1575 case RAW_SFILEINFO_1023:
1576 case RAW_SFILEINFO_1025:
1577 case RAW_SFILEINFO_1029:
1578 case RAW_SFILEINFO_1032:
1579 case RAW_SFILEINFO_1039:
1580 case RAW_SFILEINFO_1040:
1581 case RAW_SFILEINFO_UNIX_BASIC:
1582 case RAW_SFILEINFO_UNIX_INFO2:
1583 case RAW_SFILEINFO_UNIX_LINK:
1584 case RAW_SFILEINFO_UNIX_HLINK:
1591 generate setfileinfo operations
1593 static bool handler_sfileinfo(int instance)
1595 union smb_setfileinfo parm[NSERVERS];
1596 NTSTATUS status[NSERVERS];
1598 parm[0].generic.in.file.fnum = gen_fnum(instance);
1600 gen_setfileinfo(instance, &parm[0]);
1603 GEN_SET_FNUM(generic.in.file.handle);
1604 GEN_CALL(smb2_setinfo_file(tree, &parm[i]));
1610 wipe any relevant files
1612 static void wipe_files(void)
1617 if (options.skip_cleanup) {
1621 for (i=0;i<NSERVERS;i++) {
1622 int n = smb2_deltree(servers[i].tree[0], "gentest");
1624 printf("Failed to wipe tree on server %d\n", i);
1627 status = smb2_util_mkdir(servers[i].tree[0], "gentest");
1628 if (NT_STATUS_IS_ERR(status)) {
1629 printf("Failed to create gentest on server %d - %s\n", i, nt_errstr(status));
1633 printf("Deleted %d files on server %d\n", n, i);
1639 dump the current seeds - useful for continuing a backtrack
1641 static void dump_seeds(void)
1646 if (!options.seeds_file) {
1649 f = fopen("seeds.tmp", "w");
1652 for (i=0;i<options.numops;i++) {
1653 fprintf(f, "%u\n", op_parms[i].seed);
1656 rename("seeds.tmp", options.seeds_file);
1662 the list of top-level operations that we will generate
1666 bool (*handler)(int instance);
1667 int count, success_count;
1669 {"CREATE", handler_create},
1670 {"CLOSE", handler_close},
1671 {"READ", handler_read},
1672 {"WRITE", handler_write},
1673 {"LOCK", handler_lock},
1674 {"FLUSH", handler_flush},
1675 {"ECHO", handler_echo},
1676 {"QFILEINFO", handler_qfileinfo},
1677 {"SFILEINFO", handler_sfileinfo},
1682 run the test with the current set of op_parms parameters
1683 return the number of operations that completed successfully
1685 static int run_test(struct event_context *ev, struct loadparm_context *lp_ctx)
1689 if (!connect_servers(ev, lp_ctx)) {
1690 printf("Failed to connect to servers\n");
1696 /* wipe any leftover files from old runs */
1699 /* reset the open handles array */
1700 memset(open_handles, 0, options.max_open_handles * sizeof(open_handles[0]));
1701 num_open_handles = 0;
1703 for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1704 gen_ops[i].count = 0;
1705 gen_ops[i].success_count = 0;
1708 for (op=0; op<options.numops; op++) {
1709 int instance, which_op;
1712 if (op_parms[op].disabled) continue;
1714 srandom(op_parms[op].seed);
1716 instance = gen_int_range(0, NINSTANCES-1);
1718 /* generate a non-ignored operation */
1720 which_op = gen_int_range(0, ARRAY_SIZE(gen_ops)-1);
1721 } while (ignore_pattern(gen_ops[which_op].name));
1723 DEBUG(3,("Generating op %s on instance %d\n",
1724 gen_ops[which_op].name, instance));
1726 current_op.seed = op_parms[op].seed;
1727 current_op.opnum = op;
1728 current_op.name = gen_ops[which_op].name;
1729 current_op.status = NT_STATUS_OK;
1730 current_op.mem_ctx = talloc_named(NULL, 0, "%s", current_op.name);
1732 ret = gen_ops[which_op].handler(instance);
1734 talloc_free(current_op.mem_ctx);
1736 gen_ops[which_op].count++;
1737 if (NT_STATUS_IS_OK(current_op.status)) {
1738 gen_ops[which_op].success_count++;
1742 printf("Failed at operation %d - %s\n",
1743 op, gen_ops[which_op].name);
1747 if (op % 100 == 0) {
1752 for (i=0;i<ARRAY_SIZE(gen_ops);i++) {
1753 printf("Op %-10s got %d/%d success\n",
1755 gen_ops[i].success_count,
1763 perform a backtracking analysis of the minimal set of operations
1764 to generate an error
1766 static void backtrack_analyze(struct event_context *ev,
1767 struct loadparm_context *lp_ctx)
1771 chunk = options.numops / 2;
1776 chunk > 0 && base+chunk < options.numops && options.numops > 1; ) {
1779 chunk = MIN(chunk, options.numops / 2);
1781 /* mark this range as disabled */
1782 max = MIN(options.numops, base+chunk);
1783 for (i=base;i<max; i++) {
1784 op_parms[i].disabled = true;
1786 printf("Testing %d ops with %d-%d disabled\n",
1787 options.numops, base, max-1);
1788 ret = run_test(ev, lp_ctx);
1789 printf("Completed %d of %d ops\n", ret, options.numops);
1790 for (i=base;i<max; i++) {
1791 op_parms[i].disabled = false;
1793 if (ret == options.numops) {
1794 /* this chunk is needed */
1796 } else if (ret < base) {
1797 printf("damn - inconsistent errors! found early error\n");
1798 options.numops = ret+1;
1801 /* it failed - this chunk isn't needed for a failure */
1802 memmove(&op_parms[base], &op_parms[max],
1803 sizeof(op_parms[0]) * (options.numops - max));
1804 options.numops = (ret+1) - (max - base);
1814 if (options.analyze_continuous && chunk == 0 && options.numops != 1) {
1817 } while (chunk > 0);
1819 printf("Reduced to %d ops\n", options.numops);
1820 ret = run_test(ev, lp_ctx);
1821 if (ret != options.numops - 1) {
1822 printf("Inconsistent result? ret=%d numops=%d\n", ret, options.numops);
1827 start the main gentest process
1829 static bool start_gentest(struct event_context *ev,
1830 struct loadparm_context *lp_ctx)
1835 /* allocate the open_handles array */
1836 open_handles = calloc(options.max_open_handles, sizeof(open_handles[0]));
1838 srandom(options.seed);
1839 op_parms = calloc(options.numops, sizeof(op_parms[0]));
1841 /* generate the seeds - after this everything is deterministic */
1842 if (options.use_preset_seeds) {
1844 char **preset = file_lines_load(options.seeds_file, &numops, NULL);
1846 printf("Failed to load %s - %s\n", options.seeds_file, strerror(errno));
1849 if (numops < options.numops) {
1850 options.numops = numops;
1852 for (op=0;op<options.numops;op++) {
1854 printf("Not enough seeds in %s\n", options.seeds_file);
1857 op_parms[op].seed = atoi(preset[op]);
1859 printf("Loaded %d seeds from %s\n", options.numops, options.seeds_file);
1861 for (op=0; op<options.numops; op++) {
1862 op_parms[op].seed = random();
1866 ret = run_test(ev, lp_ctx);
1868 if (ret != options.numops && options.analyze) {
1869 options.numops = ret+1;
1870 backtrack_analyze(ev, lp_ctx);
1871 } else if (options.analyze_always) {
1872 backtrack_analyze(ev, lp_ctx);
1873 } else if (options.analyze_continuous) {
1874 while (run_test(ev, lp_ctx) == options.numops) ;
1877 return ret == options.numops;
1881 static void usage(poptContext pc)
1885 gentest //server1/share1 //server2/share2 [options..]\n\
1887 poptPrintUsage(pc, stdout, 0);
1891 split a UNC name into server and share names
1893 static bool split_unc_name(const char *unc, char **server, char **share)
1895 char *p = strdup(unc);
1896 if (!p) return false;
1897 all_string_sub(p, "\\", "/", 0);
1898 if (strncmp(p, "//", 2) != 0) return false;
1901 p = strchr(*server, '/');
1902 if (!p) return false;
1912 /****************************************************************************
1914 ****************************************************************************/
1915 int main(int argc, char *argv[])
1918 int i, username_count=0;
1920 char *ignore_file=NULL;
1921 struct event_context *ev;
1922 struct loadparm_context *lp_ctx;
1926 enum {OPT_UNCLIST=1000};
1927 struct poptOption long_options[] = {
1929 {"seed", 0, POPT_ARG_INT, &options.seed, 0, "Seed to use for randomizer", NULL},
1930 {"num-ops", 0, POPT_ARG_INT, &options.numops, 0, "num ops", NULL},
1931 {"oplocks", 0, POPT_ARG_NONE, &options.use_oplocks,0, "use oplocks", NULL},
1932 {"showall", 0, POPT_ARG_NONE, &options.showall, 0, "display all operations", NULL},
1933 {"analyse", 0, POPT_ARG_NONE, &options.analyze, 0, "do backtrack analysis", NULL},
1934 {"analysealways", 0, POPT_ARG_NONE, &options.analyze_always, 0, "analysis always", NULL},
1935 {"analysecontinuous", 0, POPT_ARG_NONE, &options.analyze_continuous, 0, "analysis continuous", NULL},
1936 {"ignore", 0, POPT_ARG_STRING, &ignore_file, 0, "ignore from file", NULL},
1937 {"preset", 0, POPT_ARG_NONE, &options.use_preset_seeds, 0, "use preset seeds", NULL},
1938 {"fast", 0, POPT_ARG_NONE, &options.fast_reconnect, 0, "use fast reconnect", NULL},
1939 {"unclist", 0, POPT_ARG_STRING, NULL, OPT_UNCLIST, "unclist", NULL},
1940 {"seedsfile", 0, POPT_ARG_STRING, &options.seeds_file, 0, "seed file", NULL},
1941 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
1942 {"maskindexing", 0, POPT_ARG_NONE, &options.mask_indexing, 0, "mask out the indexed file attrib", NULL},
1943 {"noeas", 0, POPT_ARG_NONE, &options.no_eas, 0, "don't use extended attributes", NULL},
1944 {"skip-cleanup", 0, POPT_ARG_NONE, &options.skip_cleanup, 0, "don't delete files at start", NULL},
1945 {"valid", 0, POPT_ARG_NONE, &options.valid, 0, "generate only valid fields", NULL},
1947 POPT_COMMON_CONNECTION
1948 POPT_COMMON_CREDENTIALS
1953 memset(&bad_smb2_handle, 0xFF, sizeof(bad_smb2_handle));
1956 options.seed = time(NULL);
1957 options.numops = 1000;
1958 options.max_open_handles = 20;
1959 options.seeds_file = "gentest_seeds.dat";
1961 pc = poptGetContext("gentest", argc, (const char **) argv, long_options,
1962 POPT_CONTEXT_KEEP_FIRST);
1964 poptSetOtherOptionHelp(pc, "<unc1> <unc2>");
1966 lp_ctx = cmdline_lp_ctx;
1967 servers[0].credentials = cli_credentials_init(talloc_autofree_context());
1968 servers[1].credentials = cli_credentials_init(talloc_autofree_context());
1969 cli_credentials_guess(servers[0].credentials, lp_ctx);
1970 cli_credentials_guess(servers[1].credentials, lp_ctx);
1972 while((opt = poptGetNextOpt(pc)) != -1) {
1975 lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
1978 if (username_count == 2) {
1982 cli_credentials_parse_string(servers[username_count].credentials, poptGetOptArg(pc), CRED_SPECIFIED);
1989 options.ignore_patterns = file_lines_load(ignore_file, NULL, NULL);
1992 argv_new = discard_const_p(char *, poptGetArgs(pc));
1994 for (i=0; i<argc; i++) {
1995 if (argv_new[i] == NULL) {
2001 if (!(argc_new >= 3)) {
2008 setup_logging("gentest", DEBUG_STDOUT);
2010 if (argc < 3 || argv[1][0] == '-') {
2015 setup_logging(argv[0], DEBUG_STDOUT);
2017 for (i=0;i<NSERVERS;i++) {
2018 const char *share = argv[1+i];
2019 if (!split_unc_name(share, &servers[i].server_name, &servers[i].share_name)) {
2020 printf("Invalid share name '%s'\n", share);
2025 if (username_count == 0) {
2029 if (username_count == 1) {
2030 servers[1].credentials = servers[0].credentials;
2033 printf("seed=%u\n", options.seed);
2035 ev = event_context_init(talloc_autofree_context());
2037 gensec_init(lp_ctx);
2039 ret = start_gentest(ev, lp_ctx);
2042 printf("gentest completed - no errors\n");
2044 printf("gentest failed\n");