2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1997-2003
5 Copyright (C) Jelmer Vernooij 2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "lib/cmdline/popt_common.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "system/time.h"
26 #include "system/wait.h"
27 #include "system/filesys.h"
28 #include "libcli/raw/ioctl.h"
29 #include "libcli/libcli.h"
30 #include "lib/ldb/include/ldb.h"
31 #include "lib/events/events.h"
32 #include "libcli/resolve/resolve.h"
33 #include "auth/credentials/credentials.h"
34 #include "libcli/ldap/ldap_client.h"
35 #include "librpc/gen_ndr/ndr_nbt.h"
37 #include "torture/raw/proto.h"
38 #include "torture/smb2/proto.h"
39 #include "torture/rap/proto.h"
40 #include "torture/auth/proto.h"
41 #include "torture/local/proto.h"
42 #include "torture/nbench/proto.h"
43 #include "torture/ldap/proto.h"
44 #include "torture/com/proto.h"
45 #include "torture/nbt/proto.h"
46 #include "torture/libnet/proto.h"
47 #include "torture/torture.h"
48 #include "torture/util.h"
50 #include "dlinklist.h"
52 _PUBLIC_ int torture_nprocs=4;
53 _PUBLIC_ int torture_numops=10;
54 _PUBLIC_ int torture_entries=1000;
55 _PUBLIC_ int torture_failures=1;
56 _PUBLIC_ int torture_seed=0;
57 _PUBLIC_ BOOL use_oplocks;
58 static int procnum; /* records process count number when forking */
59 static struct smbcli_state *current_cli;
60 _PUBLIC_ BOOL use_level_II_oplocks;
61 _PUBLIC_ BOOL torture_showall = False;
64 BOOL torture_open_connection_share(TALLOC_CTX *mem_ctx,
65 struct smbcli_state **c,
67 const char *sharename,
68 struct event_context *ev)
72 status = smbcli_full_connection(mem_ctx, c, hostname,
74 cmdline_credentials, ev);
75 if (!NT_STATUS_IS_OK(status)) {
76 printf("Failed to open connection - %s\n", nt_errstr(status));
80 (*c)->transport->options.use_oplocks = use_oplocks;
81 (*c)->transport->options.use_level2_oplocks = use_level_II_oplocks;
86 _PUBLIC_ BOOL torture_open_connection(struct smbcli_state **c)
88 const char *host = lp_parm_string(-1, "torture", "host");
89 const char *share = lp_parm_string(-1, "torture", "share");
91 return torture_open_connection_share(NULL, c, host, share, NULL);
96 _PUBLIC_ BOOL torture_close_connection(struct smbcli_state *c)
100 if (NT_STATUS_IS_ERR(smbcli_tdis(c))) {
101 printf("tdis failed (%s)\n", smbcli_errstr(c->tree));
109 /* check if the server produced the expected error code */
110 _PUBLIC_ BOOL check_error(const char *location, struct smbcli_state *c,
111 uint8_t eclass, uint32_t ecode, NTSTATUS nterr)
115 status = smbcli_nt_error(c->tree);
116 if (NT_STATUS_IS_DOS(status)) {
118 class = NT_STATUS_DOS_CLASS(status);
119 num = NT_STATUS_DOS_CODE(status);
120 if (eclass != class || ecode != num) {
121 printf("unexpected error code %s\n", nt_errstr(status));
122 printf(" expected %s or %s (at %s)\n",
123 nt_errstr(NT_STATUS_DOS(eclass, ecode)),
124 nt_errstr(nterr), location);
128 if (!NT_STATUS_EQUAL(nterr, status)) {
129 printf("unexpected error code %s\n", nt_errstr(status));
130 printf(" expected %s (at %s)\n", nt_errstr(nterr), location);
140 static BOOL wait_lock(struct smbcli_state *c, int fnum, uint32_t offset, uint32_t len)
142 while (NT_STATUS_IS_ERR(smbcli_lock(c->tree, fnum, offset, len, -1, WRITE_LOCK))) {
143 if (!check_error(__location__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
149 static BOOL rw_torture(struct smbcli_state *c)
151 const char *lockfname = "\\torture.lck";
155 pid_t pid2, pid = getpid();
160 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL,
163 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR, DENY_NONE);
165 printf("open of %s failed (%s)\n", lockfname, smbcli_errstr(c->tree));
170 for (i=0;i<torture_numops;i++) {
171 uint_t n = (uint_t)random()%10;
173 printf("%d\r", i); fflush(stdout);
175 asprintf(&fname, "\\torture.%u", n);
177 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
181 fnum = smbcli_open(c->tree, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
183 printf("open failed (%s)\n", smbcli_errstr(c->tree));
188 if (smbcli_write(c->tree, fnum, 0, &pid, 0, sizeof(pid)) != sizeof(pid)) {
189 printf("write failed (%s)\n", smbcli_errstr(c->tree));
194 if (smbcli_write(c->tree, fnum, 0, buf,
195 sizeof(pid)+(j*sizeof(buf)),
196 sizeof(buf)) != sizeof(buf)) {
197 printf("write failed (%s)\n", smbcli_errstr(c->tree));
204 if (smbcli_read(c->tree, fnum, &pid2, 0, sizeof(pid)) != sizeof(pid)) {
205 printf("read failed (%s)\n", smbcli_errstr(c->tree));
210 printf("data corruption!\n");
214 if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
215 printf("close failed (%s)\n", smbcli_errstr(c->tree));
219 if (NT_STATUS_IS_ERR(smbcli_unlink(c->tree, fname))) {
220 printf("unlink failed (%s)\n", smbcli_errstr(c->tree));
224 if (NT_STATUS_IS_ERR(smbcli_unlock(c->tree, fnum2, n*sizeof(int), sizeof(int)))) {
225 printf("unlock failed (%s)\n", smbcli_errstr(c->tree));
231 smbcli_close(c->tree, fnum2);
232 smbcli_unlink(c->tree, lockfname);
239 static BOOL run_torture(struct smbcli_state *cli, int dummy)
243 ret = rw_torture(cli);
245 if (!torture_close_connection(cli)) {
254 see how many RPC pipes we can open at once
256 static BOOL run_pipe_number(void)
258 struct smbcli_state *cli1;
259 const char *pipe_name = "\\WKSSVC";
263 printf("starting pipenumber test\n");
264 if (!torture_open_connection(&cli1)) {
269 fnum = smbcli_nt_create_full(cli1->tree, pipe_name, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
270 NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OPEN_IF, 0, 0);
273 printf("Open of pipe %s failed with error (%s)\n", pipe_name, smbcli_errstr(cli1->tree));
277 printf("%d\r", num_pipes);
281 printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
282 torture_close_connection(cli1);
290 open N connections to the server and just hold them open
291 used for testing performance when there are N idle users
294 static BOOL torture_holdcon(void)
297 struct smbcli_state **cli;
300 printf("Opening %d connections\n", torture_numops);
302 cli = malloc_array_p(struct smbcli_state *, torture_numops);
304 for (i=0;i<torture_numops;i++) {
305 if (!torture_open_connection(&cli[i])) {
308 printf("opened %d connections\r", i);
312 printf("\nStarting pings\n");
315 for (i=0;i<torture_numops;i++) {
318 status = smbcli_chkpath(cli[i]->tree, "\\");
319 if (!NT_STATUS_IS_OK(status)) {
320 printf("Connection %d is dead\n", i);
328 if (num_dead == torture_numops) {
329 printf("All connections dead - finishing\n");
341 test how many open files this server supports on the one socket
343 static BOOL run_maxfidtest(struct smbcli_state *cli, int dummy)
345 #define MAXFID_TEMPLATE "\\maxfid\\fid%d\\maxfid.%d.%d"
347 int fnums[0x11000], i;
348 int retries=4, maxfid;
352 printf("failed to connect\n");
356 if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
357 printf("Failed to deltree \\maxfid - %s\n",
358 smbcli_errstr(cli->tree));
361 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\maxfid"))) {
362 printf("Failed to mkdir \\maxfid, error=%s\n",
363 smbcli_errstr(cli->tree));
367 printf("Testing maximum number of open files\n");
369 for (i=0; i<0x11000; i++) {
371 asprintf(&fname, "\\maxfid\\fid%d", i/1000);
372 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
373 printf("Failed to mkdir %s, error=%s\n",
374 fname, smbcli_errstr(cli->tree));
379 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
380 if ((fnums[i] = smbcli_open(cli->tree, fname,
381 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
383 printf("open of %s failed (%s)\n",
384 fname, smbcli_errstr(cli->tree));
385 printf("maximum fnum is %d\n", i);
396 printf("cleaning up\n");
397 for (i=0;i<maxfid/2;i++) {
398 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
399 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[i]))) {
400 printf("Close of fnum %d failed - %s\n", fnums[i], smbcli_errstr(cli->tree));
402 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
403 printf("unlink of %s failed (%s)\n",
404 fname, smbcli_errstr(cli->tree));
409 asprintf(&fname, MAXFID_TEMPLATE, (maxfid-i)/1000, maxfid-i,(int)getpid());
410 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[maxfid-i]))) {
411 printf("Close of fnum %d failed - %s\n", fnums[maxfid-i], smbcli_errstr(cli->tree));
413 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
414 printf("unlink of %s failed (%s)\n",
415 fname, smbcli_errstr(cli->tree));
420 printf("%6d %6d\r", i, maxfid-i);
424 if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
425 printf("Failed to deltree \\maxfid - %s\n",
426 smbcli_errstr(cli->tree));
430 printf("maxfid test finished\n");
431 if (!torture_close_connection(cli)) {
435 #undef MAXFID_TEMPLATE
441 sees what IOCTLs are supported
443 static BOOL torture_ioctl_test(void)
445 struct smbcli_state *cli;
446 uint16_t device, function;
448 const char *fname = "\\ioctl.dat";
450 union smb_ioctl parms;
453 if (!torture_open_connection(&cli)) {
457 mem_ctx = talloc_init("ioctl_test");
459 printf("starting ioctl test\n");
461 smbcli_unlink(cli->tree, fname);
463 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
465 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
469 parms.ioctl.level = RAW_IOCTL_IOCTL;
470 parms.ioctl.in.file.fnum = fnum;
471 parms.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
472 status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
473 printf("ioctl job info: %s\n", smbcli_errstr(cli->tree));
475 for (device=0;device<0x100;device++) {
476 printf("testing device=0x%x\n", device);
477 for (function=0;function<0x100;function++) {
478 parms.ioctl.in.request = (device << 16) | function;
479 status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
481 if (NT_STATUS_IS_OK(status)) {
482 printf("ioctl device=0x%x function=0x%x OK : %d bytes\n",
483 device, function, (int)parms.ioctl.out.blob.length);
488 if (!torture_close_connection(cli)) {
496 static void sigcont(int sig)
500 double torture_create_procs(BOOL (*fn)(struct smbcli_state *, int), BOOL *result)
503 volatile pid_t *child_status;
504 volatile BOOL *child_status_out;
507 double start_time_limit = 10 + (torture_nprocs * 1.5);
508 char **unc_list = NULL;
510 int num_unc_names = 0;
517 signal(SIGCONT, sigcont);
519 child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*torture_nprocs);
521 printf("Failed to setup shared memory\n");
525 child_status_out = (volatile BOOL *)shm_setup(sizeof(BOOL)*torture_nprocs);
526 if (!child_status_out) {
527 printf("Failed to setup result status shared memory\n");
531 p = lp_parm_string(-1, "torture", "unclist");
533 unc_list = file_lines_load(p, &num_unc_names, NULL);
534 if (!unc_list || num_unc_names <= 0) {
535 printf("Failed to load unc names list from '%s'\n", p);
540 for (i = 0; i < torture_nprocs; i++) {
542 child_status_out[i] = True;
545 tv = timeval_current();
547 for (i=0;i<torture_nprocs;i++) {
551 char *hostname=NULL, *sharename;
553 pid_t mypid = getpid();
554 srandom(((int)mypid) ^ ((int)time(NULL)));
556 asprintf(&myname, "CLIENT%d", i);
557 lp_set_cmdline("netbios name", myname);
562 if (!smbcli_parse_unc(unc_list[i % num_unc_names],
563 NULL, &hostname, &sharename)) {
564 printf("Failed to parse UNC name %s\n",
565 unc_list[i % num_unc_names]);
572 if (torture_open_connection_share(NULL,
579 } else if (torture_open_connection(¤t_cli)) {
583 printf("pid %d failed to start\n", (int)getpid());
589 child_status[i] = getpid();
593 if (child_status[i]) {
594 printf("Child %d failed to start!\n", i);
595 child_status_out[i] = 1;
599 child_status_out[i] = fn(current_cli, i);
606 for (i=0;i<torture_nprocs;i++) {
607 if (child_status[i]) synccount++;
609 if (synccount == torture_nprocs) break;
611 } while (timeval_elapsed(&tv) < start_time_limit);
613 if (synccount != torture_nprocs) {
614 printf("FAILED TO START %d CLIENTS (started %d)\n", torture_nprocs, synccount);
616 return timeval_elapsed(&tv);
619 printf("Starting %d clients\n", torture_nprocs);
621 /* start the client load */
622 tv = timeval_current();
623 for (i=0;i<torture_nprocs;i++) {
627 printf("%d clients started\n", torture_nprocs);
631 for (i=0;i<torture_nprocs;i++) {
633 while ((ret=waitpid(0, &status, 0)) == -1 && errno == EINTR) /* noop */ ;
634 if (ret == -1 || WEXITSTATUS(status) != 0) {
641 for (i=0;i<torture_nprocs;i++) {
642 if (!child_status_out[i]) {
646 return timeval_elapsed(&tv);
649 #define FLAG_MULTIPROC 1
654 BOOL (*multi_fn)(struct smbcli_state *, int );
655 } builtin_torture_ops[] = {
656 /* benchmarking tests */
657 {"BENCH-HOLDCON", torture_holdcon, 0},
658 {"BENCH-NBENCH", torture_nbench, 0},
659 {"BENCH-TORTURE", NULL, run_torture},
660 {"BENCH-NBT", torture_bench_nbt, 0},
661 {"BENCH-WINS", torture_bench_wins, 0},
662 {"BENCH-CLDAP", torture_bench_cldap, 0},
665 {"RAW-QFSINFO", torture_raw_qfsinfo, 0},
666 {"RAW-QFILEINFO", torture_raw_qfileinfo, 0},
667 {"RAW-SFILEINFO", torture_raw_sfileinfo, 0},
668 {"RAW-SFILEINFO-BUG", torture_raw_sfileinfo_bug, 0},
669 {"RAW-SEARCH", torture_raw_search, 0},
670 {"RAW-CLOSE", torture_raw_close, 0},
671 {"RAW-OPEN", torture_raw_open, 0},
672 {"RAW-MKDIR", torture_raw_mkdir, 0},
673 {"RAW-OPLOCK", torture_raw_oplock, 0},
674 {"RAW-NOTIFY", torture_raw_notify, 0},
675 {"RAW-MUX", torture_raw_mux, 0},
676 {"RAW-IOCTL", torture_raw_ioctl, 0},
677 {"RAW-CHKPATH", torture_raw_chkpath, 0},
678 {"RAW-UNLINK", torture_raw_unlink, 0},
679 {"RAW-READ", torture_raw_read, 0},
680 {"RAW-WRITE", torture_raw_write, 0},
681 {"RAW-LOCK", torture_raw_lock, 0},
682 {"RAW-CONTEXT", torture_raw_context, 0},
683 {"RAW-RENAME", torture_raw_rename, 0},
684 {"RAW-SEEK", torture_raw_seek, 0},
685 {"RAW-EAS", torture_raw_eas, 0},
686 {"RAW-EAMAX", torture_max_eas, 0},
687 {"RAW-STREAMS", torture_raw_streams, 0},
688 {"RAW-ACLS", torture_raw_acls, 0},
689 {"RAW-RAP", torture_raw_rap, 0},
690 {"RAW-COMPOSITE", torture_raw_composite, 0},
693 {"SMB2-CONNECT", torture_smb2_connect, 0},
694 {"SMB2-SCAN", torture_smb2_scan, 0},
695 {"SMB2-SCANGETINFO", torture_smb2_getinfo_scan, 0},
696 {"SMB2-SCANSETINFO", torture_smb2_setinfo_scan, 0},
697 {"SMB2-SCANFIND", torture_smb2_find_scan, 0},
698 {"SMB2-GETINFO", torture_smb2_getinfo, 0},
699 {"SMB2-SETINFO", torture_smb2_setinfo, 0},
700 {"SMB2-FIND", torture_smb2_find, 0},
702 /* protocol scanners */
703 {"SCAN-MAXFID", NULL, run_maxfidtest},
704 {"SCAN-PIPE_NUMBER", run_pipe_number, 0},
705 {"SCAN-IOCTL", torture_ioctl_test, 0},
706 {"SCAN-RAP", torture_rap_scan, 0},
708 /* local (no server) testers */
709 {"LOCAL-NTLMSSP", torture_ntlmssp_self_check, 0},
710 {"LOCAL-ICONV", torture_local_iconv, 0},
711 {"LOCAL-TALLOC", torture_local_talloc, 0},
712 {"LOCAL-MESSAGING", torture_local_messaging, 0},
713 {"LOCAL-IRPC", torture_local_irpc, 0},
714 {"LOCAL-BINDING", torture_local_binding_string, 0},
715 {"LOCAL-STRLIST", torture_local_util_strlist, 0},
716 {"LOCAL-FILE", torture_local_util_file, 0},
717 {"LOCAL-IDTREE", torture_local_idtree, 0},
718 {"LOCAL-SOCKET", torture_local_socket, 0},
719 {"LOCAL-PAC", torture_pac, 0},
720 {"LOCAL-REGISTRY", torture_registry, 0},
721 {"LOCAL-RESOLVE", torture_local_resolve, 0},
722 {"LOCAL-SDDL", torture_local_sddl, 0},
723 {"LOCAL-NDR", torture_local_ndr, 0},
726 {"LDAP-BASIC", torture_ldap_basic, 0},
727 {"LDAP-CLDAP", torture_cldap, 0},
730 {"NBT-REGISTER", torture_nbt_register, 0},
731 {"NBT-WINS", torture_nbt_wins, 0},
732 {"NBT-DGRAM", torture_nbt_dgram, 0},
733 {"NBT-BROWSE", torture_nbt_browse, 0},
734 {"NBT-WINSREPLICATION-SIMPLE", torture_nbt_winsreplication_simple, 0},
735 {"NBT-WINSREPLICATION-REPLICA", torture_nbt_winsreplication_replica, 0},
736 {"NBT-WINSREPLICATION-OWNED", torture_nbt_winsreplication_owned, 0},
740 static void register_builtin_ops(void)
743 for (i = 0; builtin_torture_ops[i].name; i++) {
744 register_torture_op(builtin_torture_ops[i].name,
745 builtin_torture_ops[i].fn,
746 builtin_torture_ops[i].multi_fn);
751 struct torture_op *torture_ops = NULL;
753 static struct torture_op *find_torture_op(const char *name)
755 struct torture_op *o;
756 for (o = torture_ops; o; o = o->next) {
757 if (strcmp(name, o->name) == 0)
764 _PUBLIC_ NTSTATUS register_torture_op(const char *name, BOOL (*fn)(void), BOOL (*multi_fn)(struct smbcli_state *, int ))
766 struct torture_op *op;
768 /* Check for duplicates */
769 if (find_torture_op(name) != NULL) {
770 DEBUG(0,("There already is a torture op registered with the name %s!\n", name));
771 return NT_STATUS_OBJECT_NAME_COLLISION;
774 op = talloc(talloc_autofree_context(), struct torture_op);
776 op->name = talloc_strdup(op, name);
778 op->multi_fn = multi_fn;
780 DLIST_ADD(torture_ops, op);
785 int torture_init(void)
787 init_module_fn static_init[] = STATIC_torture_MODULES;
788 init_module_fn *shared_init = load_samba_modules(NULL, "torture");
790 register_builtin_ops();
792 run_init_functions(static_init);
793 run_init_functions(shared_init);
795 talloc_free(shared_init);