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 "libcli/smb2/smb2.h"
39 #include "torture/smb2/proto.h"
40 #include "torture/rap/proto.h"
41 #include "torture/auth/proto.h"
42 #include "torture/local/proto.h"
43 #include "torture/nbench/proto.h"
44 #include "torture/ldap/proto.h"
45 #include "torture/com/proto.h"
46 #include "torture/nbt/proto.h"
47 #include "torture/libnet/proto.h"
48 #include "torture/torture.h"
49 #include "torture/util.h"
51 #include "dlinklist.h"
53 _PUBLIC_ int torture_nprocs=4;
54 _PUBLIC_ int torture_numops=10;
55 _PUBLIC_ int torture_entries=1000;
56 _PUBLIC_ int torture_failures=1;
57 _PUBLIC_ int torture_seed=0;
58 _PUBLIC_ BOOL use_oplocks;
59 static int procnum; /* records process count number when forking */
60 static struct smbcli_state *current_cli;
61 _PUBLIC_ BOOL use_level_II_oplocks;
62 _PUBLIC_ BOOL torture_showall = False;
67 static BOOL wait_lock(struct smbcli_state *c, int fnum, uint32_t offset, uint32_t len)
69 while (NT_STATUS_IS_ERR(smbcli_lock(c->tree, fnum, offset, len, -1, WRITE_LOCK))) {
70 if (!check_error(__location__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
76 static BOOL rw_torture(struct smbcli_state *c)
78 const char *lockfname = "\\torture.lck";
82 pid_t pid2, pid = getpid();
87 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL,
90 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR, DENY_NONE);
92 printf("open of %s failed (%s)\n", lockfname, smbcli_errstr(c->tree));
97 for (i=0;i<torture_numops;i++) {
98 uint_t n = (uint_t)random()%10;
100 printf("%d\r", i); fflush(stdout);
102 asprintf(&fname, "\\torture.%u", n);
104 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
108 fnum = smbcli_open(c->tree, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
110 printf("open failed (%s)\n", smbcli_errstr(c->tree));
115 if (smbcli_write(c->tree, fnum, 0, &pid, 0, sizeof(pid)) != sizeof(pid)) {
116 printf("write failed (%s)\n", smbcli_errstr(c->tree));
121 if (smbcli_write(c->tree, fnum, 0, buf,
122 sizeof(pid)+(j*sizeof(buf)),
123 sizeof(buf)) != sizeof(buf)) {
124 printf("write failed (%s)\n", smbcli_errstr(c->tree));
131 if (smbcli_read(c->tree, fnum, &pid2, 0, sizeof(pid)) != sizeof(pid)) {
132 printf("read failed (%s)\n", smbcli_errstr(c->tree));
137 printf("data corruption!\n");
141 if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
142 printf("close failed (%s)\n", smbcli_errstr(c->tree));
146 if (NT_STATUS_IS_ERR(smbcli_unlink(c->tree, fname))) {
147 printf("unlink failed (%s)\n", smbcli_errstr(c->tree));
151 if (NT_STATUS_IS_ERR(smbcli_unlock(c->tree, fnum2, n*sizeof(int), sizeof(int)))) {
152 printf("unlock failed (%s)\n", smbcli_errstr(c->tree));
158 smbcli_close(c->tree, fnum2);
159 smbcli_unlink(c->tree, lockfname);
166 static BOOL run_torture(struct smbcli_state *cli, int dummy)
170 ret = rw_torture(cli);
172 if (!torture_close_connection(cli)) {
181 see how many RPC pipes we can open at once
183 static BOOL run_pipe_number(void)
185 struct smbcli_state *cli1;
186 const char *pipe_name = "\\WKSSVC";
190 printf("starting pipenumber test\n");
191 if (!torture_open_connection(&cli1)) {
196 fnum = smbcli_nt_create_full(cli1->tree, pipe_name, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
197 NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OPEN_IF, 0, 0);
200 printf("Open of pipe %s failed with error (%s)\n", pipe_name, smbcli_errstr(cli1->tree));
204 printf("%d\r", num_pipes);
208 printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
209 torture_close_connection(cli1);
217 open N connections to the server and just hold them open
218 used for testing performance when there are N idle users
221 static BOOL torture_holdcon(void)
224 struct smbcli_state **cli;
227 printf("Opening %d connections\n", torture_numops);
229 cli = malloc_array_p(struct smbcli_state *, torture_numops);
231 for (i=0;i<torture_numops;i++) {
232 if (!torture_open_connection(&cli[i])) {
235 printf("opened %d connections\r", i);
239 printf("\nStarting pings\n");
242 for (i=0;i<torture_numops;i++) {
245 status = smbcli_chkpath(cli[i]->tree, "\\");
246 if (!NT_STATUS_IS_OK(status)) {
247 printf("Connection %d is dead\n", i);
255 if (num_dead == torture_numops) {
256 printf("All connections dead - finishing\n");
268 test how many open files this server supports on the one socket
270 static BOOL run_maxfidtest(struct smbcli_state *cli, int dummy)
272 #define MAXFID_TEMPLATE "\\maxfid\\fid%d\\maxfid.%d.%d"
274 int fnums[0x11000], i;
275 int retries=4, maxfid;
279 printf("failed to connect\n");
283 if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
284 printf("Failed to deltree \\maxfid - %s\n",
285 smbcli_errstr(cli->tree));
288 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\maxfid"))) {
289 printf("Failed to mkdir \\maxfid, error=%s\n",
290 smbcli_errstr(cli->tree));
294 printf("Testing maximum number of open files\n");
296 for (i=0; i<0x11000; i++) {
298 asprintf(&fname, "\\maxfid\\fid%d", i/1000);
299 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
300 printf("Failed to mkdir %s, error=%s\n",
301 fname, smbcli_errstr(cli->tree));
306 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
307 if ((fnums[i] = smbcli_open(cli->tree, fname,
308 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
310 printf("open of %s failed (%s)\n",
311 fname, smbcli_errstr(cli->tree));
312 printf("maximum fnum is %d\n", i);
323 printf("cleaning up\n");
324 for (i=0;i<maxfid/2;i++) {
325 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
326 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[i]))) {
327 printf("Close of fnum %d failed - %s\n", fnums[i], smbcli_errstr(cli->tree));
329 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
330 printf("unlink of %s failed (%s)\n",
331 fname, smbcli_errstr(cli->tree));
336 asprintf(&fname, MAXFID_TEMPLATE, (maxfid-i)/1000, maxfid-i,(int)getpid());
337 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[maxfid-i]))) {
338 printf("Close of fnum %d failed - %s\n", fnums[maxfid-i], smbcli_errstr(cli->tree));
340 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
341 printf("unlink of %s failed (%s)\n",
342 fname, smbcli_errstr(cli->tree));
347 printf("%6d %6d\r", i, maxfid-i);
351 if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
352 printf("Failed to deltree \\maxfid - %s\n",
353 smbcli_errstr(cli->tree));
357 printf("maxfid test finished\n");
358 if (!torture_close_connection(cli)) {
362 #undef MAXFID_TEMPLATE
368 sees what IOCTLs are supported
370 static BOOL torture_ioctl_test(void)
372 struct smbcli_state *cli;
373 uint16_t device, function;
375 const char *fname = "\\ioctl.dat";
377 union smb_ioctl parms;
380 if (!torture_open_connection(&cli)) {
384 mem_ctx = talloc_init("ioctl_test");
386 printf("starting ioctl test\n");
388 smbcli_unlink(cli->tree, fname);
390 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
392 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
396 parms.ioctl.level = RAW_IOCTL_IOCTL;
397 parms.ioctl.in.file.fnum = fnum;
398 parms.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
399 status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
400 printf("ioctl job info: %s\n", smbcli_errstr(cli->tree));
402 for (device=0;device<0x100;device++) {
403 printf("testing device=0x%x\n", device);
404 for (function=0;function<0x100;function++) {
405 parms.ioctl.in.request = (device << 16) | function;
406 status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
408 if (NT_STATUS_IS_OK(status)) {
409 printf("ioctl device=0x%x function=0x%x OK : %d bytes\n",
410 device, function, (int)parms.ioctl.out.blob.length);
415 if (!torture_close_connection(cli)) {
423 static void sigcont(int sig)
427 double torture_create_procs(BOOL (*fn)(struct smbcli_state *, int), BOOL *result)
430 volatile pid_t *child_status;
431 volatile BOOL *child_status_out;
434 double start_time_limit = 10 + (torture_nprocs * 1.5);
435 char **unc_list = NULL;
437 int num_unc_names = 0;
444 signal(SIGCONT, sigcont);
446 child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*torture_nprocs);
448 printf("Failed to setup shared memory\n");
452 child_status_out = (volatile BOOL *)shm_setup(sizeof(BOOL)*torture_nprocs);
453 if (!child_status_out) {
454 printf("Failed to setup result status shared memory\n");
458 p = lp_parm_string(-1, "torture", "unclist");
460 unc_list = file_lines_load(p, &num_unc_names, NULL);
461 if (!unc_list || num_unc_names <= 0) {
462 printf("Failed to load unc names list from '%s'\n", p);
467 for (i = 0; i < torture_nprocs; i++) {
469 child_status_out[i] = True;
472 tv = timeval_current();
474 for (i=0;i<torture_nprocs;i++) {
478 char *hostname=NULL, *sharename;
480 pid_t mypid = getpid();
481 srandom(((int)mypid) ^ ((int)time(NULL)));
483 asprintf(&myname, "CLIENT%d", i);
484 lp_set_cmdline("netbios name", myname);
489 if (!smbcli_parse_unc(unc_list[i % num_unc_names],
490 NULL, &hostname, &sharename)) {
491 printf("Failed to parse UNC name %s\n",
492 unc_list[i % num_unc_names]);
499 if (torture_open_connection_share(NULL,
506 } else if (torture_open_connection(¤t_cli)) {
510 printf("pid %d failed to start\n", (int)getpid());
516 child_status[i] = getpid();
520 if (child_status[i]) {
521 printf("Child %d failed to start!\n", i);
522 child_status_out[i] = 1;
526 child_status_out[i] = fn(current_cli, i);
533 for (i=0;i<torture_nprocs;i++) {
534 if (child_status[i]) synccount++;
536 if (synccount == torture_nprocs) break;
538 } while (timeval_elapsed(&tv) < start_time_limit);
540 if (synccount != torture_nprocs) {
541 printf("FAILED TO START %d CLIENTS (started %d)\n", torture_nprocs, synccount);
543 return timeval_elapsed(&tv);
546 printf("Starting %d clients\n", torture_nprocs);
548 /* start the client load */
549 tv = timeval_current();
550 for (i=0;i<torture_nprocs;i++) {
554 printf("%d clients started\n", torture_nprocs);
558 for (i=0;i<torture_nprocs;i++) {
560 while ((ret=waitpid(0, &status, 0)) == -1 && errno == EINTR) /* noop */ ;
561 if (ret == -1 || WEXITSTATUS(status) != 0) {
568 for (i=0;i<torture_nprocs;i++) {
569 if (!child_status_out[i]) {
573 return timeval_elapsed(&tv);
576 #define FLAG_MULTIPROC 1
581 BOOL (*multi_fn)(struct smbcli_state *, int );
582 } builtin_torture_ops[] = {
583 /* benchmarking tests */
584 {"BENCH-HOLDCON", torture_holdcon, 0},
585 {"BENCH-NBENCH", torture_nbench, 0},
586 {"BENCH-TORTURE", NULL, run_torture},
587 {"BENCH-NBT", torture_bench_nbt, 0},
588 {"BENCH-WINS", torture_bench_wins, 0},
589 {"BENCH-CLDAP", torture_bench_cldap, 0},
592 {"RAW-QFSINFO", torture_raw_qfsinfo, 0},
593 {"RAW-QFILEINFO", torture_raw_qfileinfo, 0},
594 {"RAW-SFILEINFO", torture_raw_sfileinfo, 0},
595 {"RAW-SFILEINFO-BUG", torture_raw_sfileinfo_bug, 0},
596 {"RAW-SEARCH", torture_raw_search, 0},
597 {"RAW-CLOSE", torture_raw_close, 0},
598 {"RAW-OPEN", torture_raw_open, 0},
599 {"RAW-MKDIR", torture_raw_mkdir, 0},
600 {"RAW-OPLOCK", torture_raw_oplock, 0},
601 {"RAW-NOTIFY", torture_raw_notify, 0},
602 {"RAW-MUX", torture_raw_mux, 0},
603 {"RAW-IOCTL", torture_raw_ioctl, 0},
604 {"RAW-CHKPATH", torture_raw_chkpath, 0},
605 {"RAW-UNLINK", torture_raw_unlink, 0},
606 {"RAW-READ", torture_raw_read, 0},
607 {"RAW-WRITE", torture_raw_write, 0},
608 {"RAW-LOCK", torture_raw_lock, 0},
609 {"RAW-CONTEXT", torture_raw_context, 0},
610 {"RAW-RENAME", torture_raw_rename, 0},
611 {"RAW-SEEK", torture_raw_seek, 0},
612 {"RAW-EAS", torture_raw_eas, 0},
613 {"RAW-EAMAX", torture_max_eas, 0},
614 {"RAW-STREAMS", torture_raw_streams, 0},
615 {"RAW-ACLS", torture_raw_acls, 0},
616 {"RAW-RAP", torture_raw_rap, 0},
617 {"RAW-COMPOSITE", torture_raw_composite, 0},
620 {"SMB2-CONNECT", torture_smb2_connect, 0},
621 {"SMB2-SCAN", torture_smb2_scan, 0},
622 {"SMB2-SCANGETINFO", torture_smb2_getinfo_scan, 0},
623 {"SMB2-SCANSETINFO", torture_smb2_setinfo_scan, 0},
624 {"SMB2-SCANFIND", torture_smb2_find_scan, 0},
625 {"SMB2-GETINFO", torture_smb2_getinfo, 0},
626 {"SMB2-SETINFO", torture_smb2_setinfo, 0},
627 {"SMB2-FIND", torture_smb2_find, 0},
629 /* protocol scanners */
630 {"SCAN-MAXFID", NULL, run_maxfidtest},
631 {"SCAN-PIPE_NUMBER", run_pipe_number, 0},
632 {"SCAN-IOCTL", torture_ioctl_test, 0},
633 {"SCAN-RAP", torture_rap_scan, 0},
635 /* local (no server) testers */
636 {"LOCAL-NTLMSSP", torture_ntlmssp_self_check, 0},
637 {"LOCAL-ICONV", torture_local_iconv, 0},
638 {"LOCAL-TALLOC", torture_local_talloc, 0},
639 {"LOCAL-MESSAGING", torture_local_messaging, 0},
640 {"LOCAL-IRPC", torture_local_irpc, 0},
641 {"LOCAL-BINDING", torture_local_binding_string, 0},
642 {"LOCAL-STRLIST", torture_local_util_strlist, 0},
643 {"LOCAL-FILE", torture_local_util_file, 0},
644 {"LOCAL-IDTREE", torture_local_idtree, 0},
645 {"LOCAL-SOCKET", torture_local_socket, 0},
646 {"LOCAL-PAC", torture_pac, 0},
647 {"LOCAL-REGISTRY", torture_registry, 0},
648 {"LOCAL-RESOLVE", torture_local_resolve, 0},
649 {"LOCAL-SDDL", torture_local_sddl, 0},
650 {"LOCAL-NDR", torture_local_ndr, 0},
653 {"LDAP-BASIC", torture_ldap_basic, 0},
654 {"LDAP-CLDAP", torture_cldap, 0},
657 {"NBT-REGISTER", torture_nbt_register, 0},
658 {"NBT-WINS", torture_nbt_wins, 0},
659 {"NBT-DGRAM", torture_nbt_dgram, 0},
660 {"NBT-BROWSE", torture_nbt_browse, 0},
661 {"NBT-WINSREPLICATION-SIMPLE", torture_nbt_winsreplication_simple, 0},
662 {"NBT-WINSREPLICATION-REPLICA", torture_nbt_winsreplication_replica, 0},
663 {"NBT-WINSREPLICATION-OWNED", torture_nbt_winsreplication_owned, 0},
667 static void register_builtin_ops(void)
670 for (i = 0; builtin_torture_ops[i].name; i++) {
671 register_torture_op(builtin_torture_ops[i].name,
672 builtin_torture_ops[i].fn,
673 builtin_torture_ops[i].multi_fn);
677 struct torture_op *torture_ops = NULL;
679 _PUBLIC_ NTSTATUS register_torture_op(const char *name, BOOL (*fn)(void), BOOL (*multi_fn)(struct smbcli_state *, int ))
681 struct torture_op *op, *p;
683 op = talloc(talloc_autofree_context(), struct torture_op);
685 op->name = talloc_strdup(op, name);
687 op->multi_fn = multi_fn;
689 for (p = torture_ops; p; p = p->next) {
690 if (strcmp(p->name, op->name) == 0) {
691 /* Check for duplicates */
692 DEBUG(0,("There already is a torture op registered with the name %s!\n", name));
694 return NT_STATUS_OBJECT_NAME_COLLISION;
697 if (strcmp(p->name, op->name) < 0 &&
698 (!p->next || strcmp(p->next->name, op->name) > 0)) {
699 DLIST_ADD_AFTER(torture_ops, op, p);
704 DLIST_ADD(torture_ops, op);
709 int torture_init(void)
711 init_module_fn static_init[] = STATIC_torture_MODULES;
712 init_module_fn *shared_init = load_samba_modules(NULL, "torture");
714 register_builtin_ops();
716 run_init_functions(static_init);
717 run_init_functions(shared_init);
719 talloc_free(shared_init);