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 "libcli/raw/libcliraw.h"
24 #include "system/time.h"
25 #include "system/wait.h"
26 #include "system/filesys.h"
27 #include "libcli/raw/ioctl.h"
28 #include "libcli/libcli.h"
29 #include "lib/ldb/include/ldb.h"
30 #include "lib/events/events.h"
31 #include "libcli/resolve/resolve.h"
32 #include "auth/credentials/credentials.h"
33 #include "libcli/ldap/ldap_client.h"
34 #include "librpc/gen_ndr/ndr_nbt.h"
35 #include "torture/torture.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/nbt/proto.h"
46 #include "torture/libnet/proto.h"
47 #include "torture/util.h"
49 #include "dlinklist.h"
51 _PUBLIC_ int torture_nprocs=4;
52 _PUBLIC_ int torture_numops=10;
53 _PUBLIC_ int torture_entries=1000;
54 _PUBLIC_ int torture_failures=1;
55 _PUBLIC_ int torture_seed=0;
56 _PUBLIC_ int torture_numasync=100;
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;
66 static BOOL wait_lock(struct smbcli_state *c, int fnum, uint32_t offset, uint32_t len)
68 while (NT_STATUS_IS_ERR(smbcli_lock(c->tree, fnum, offset, len, -1, WRITE_LOCK))) {
69 if (!check_error(__location__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
75 static BOOL rw_torture(struct smbcli_state *c)
77 const char *lockfname = "\\torture.lck";
81 pid_t pid2, pid = getpid();
86 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL,
89 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR, DENY_NONE);
91 printf("open of %s failed (%s)\n", lockfname, smbcli_errstr(c->tree));
96 for (i=0;i<torture_numops;i++) {
97 uint_t n = (uint_t)random()%10;
99 printf("%d\r", i); fflush(stdout);
101 asprintf(&fname, "\\torture.%u", n);
103 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
107 fnum = smbcli_open(c->tree, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
109 printf("open failed (%s)\n", smbcli_errstr(c->tree));
114 if (smbcli_write(c->tree, fnum, 0, &pid, 0, sizeof(pid)) != sizeof(pid)) {
115 printf("write failed (%s)\n", smbcli_errstr(c->tree));
120 if (smbcli_write(c->tree, fnum, 0, buf,
121 sizeof(pid)+(j*sizeof(buf)),
122 sizeof(buf)) != sizeof(buf)) {
123 printf("write failed (%s)\n", smbcli_errstr(c->tree));
130 if (smbcli_read(c->tree, fnum, &pid2, 0, sizeof(pid)) != sizeof(pid)) {
131 printf("read failed (%s)\n", smbcli_errstr(c->tree));
136 printf("data corruption!\n");
140 if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
141 printf("close failed (%s)\n", smbcli_errstr(c->tree));
145 if (NT_STATUS_IS_ERR(smbcli_unlink(c->tree, fname))) {
146 printf("unlink failed (%s)\n", smbcli_errstr(c->tree));
150 if (NT_STATUS_IS_ERR(smbcli_unlock(c->tree, fnum2, n*sizeof(int), sizeof(int)))) {
151 printf("unlock failed (%s)\n", smbcli_errstr(c->tree));
157 smbcli_close(c->tree, fnum2);
158 smbcli_unlink(c->tree, lockfname);
165 static BOOL run_torture(struct smbcli_state *cli, int dummy)
169 ret = rw_torture(cli);
171 if (!torture_close_connection(cli)) {
180 see how many RPC pipes we can open at once
182 static BOOL run_pipe_number(struct torture_context *torture)
184 struct smbcli_state *cli1;
185 const char *pipe_name = "\\WKSSVC";
189 printf("starting pipenumber test\n");
190 if (!torture_open_connection(&cli1)) {
195 fnum = smbcli_nt_create_full(cli1->tree, pipe_name, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
196 NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OPEN_IF, 0, 0);
199 printf("Open of pipe %s failed with error (%s)\n", pipe_name, smbcli_errstr(cli1->tree));
203 printf("%d\r", num_pipes);
207 printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
208 torture_close_connection(cli1);
216 open N connections to the server and just hold them open
217 used for testing performance when there are N idle users
220 static BOOL torture_holdcon(struct torture_context *torture)
223 struct smbcli_state **cli;
226 printf("Opening %d connections\n", torture_numops);
228 cli = malloc_array_p(struct smbcli_state *, torture_numops);
230 for (i=0;i<torture_numops;i++) {
231 if (!torture_open_connection(&cli[i])) {
234 printf("opened %d connections\r", i);
238 printf("\nStarting pings\n");
241 for (i=0;i<torture_numops;i++) {
244 status = smbcli_chkpath(cli[i]->tree, "\\");
245 if (!NT_STATUS_IS_OK(status)) {
246 printf("Connection %d is dead\n", i);
254 if (num_dead == torture_numops) {
255 printf("All connections dead - finishing\n");
267 test how many open files this server supports on the one socket
269 static BOOL run_maxfidtest(struct smbcli_state *cli, int dummy)
271 #define MAXFID_TEMPLATE "\\maxfid\\fid%d\\maxfid.%d.%d"
273 int fnums[0x11000], i;
274 int retries=4, maxfid;
278 printf("failed to connect\n");
282 if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
283 printf("Failed to deltree \\maxfid - %s\n",
284 smbcli_errstr(cli->tree));
287 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\maxfid"))) {
288 printf("Failed to mkdir \\maxfid, error=%s\n",
289 smbcli_errstr(cli->tree));
293 printf("Testing maximum number of open files\n");
295 for (i=0; i<0x11000; i++) {
297 asprintf(&fname, "\\maxfid\\fid%d", i/1000);
298 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
299 printf("Failed to mkdir %s, error=%s\n",
300 fname, smbcli_errstr(cli->tree));
305 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
306 if ((fnums[i] = smbcli_open(cli->tree, fname,
307 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
309 printf("open of %s failed (%s)\n",
310 fname, smbcli_errstr(cli->tree));
311 printf("maximum fnum is %d\n", i);
322 printf("cleaning up\n");
323 for (i=0;i<maxfid/2;i++) {
324 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
325 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[i]))) {
326 printf("Close of fnum %d failed - %s\n", fnums[i], smbcli_errstr(cli->tree));
328 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
329 printf("unlink of %s failed (%s)\n",
330 fname, smbcli_errstr(cli->tree));
335 asprintf(&fname, MAXFID_TEMPLATE, (maxfid-i)/1000, maxfid-i,(int)getpid());
336 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[maxfid-i]))) {
337 printf("Close of fnum %d failed - %s\n", fnums[maxfid-i], smbcli_errstr(cli->tree));
339 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
340 printf("unlink of %s failed (%s)\n",
341 fname, smbcli_errstr(cli->tree));
346 printf("%6d %6d\r", i, maxfid-i);
350 if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
351 printf("Failed to deltree \\maxfid - %s\n",
352 smbcli_errstr(cli->tree));
356 printf("maxfid test finished\n");
357 if (!torture_close_connection(cli)) {
361 #undef MAXFID_TEMPLATE
367 sees what IOCTLs are supported
369 static BOOL torture_ioctl_test(struct torture_context *torture)
371 struct smbcli_state *cli;
372 uint16_t device, function;
374 const char *fname = "\\ioctl.dat";
376 union smb_ioctl parms;
379 if (!torture_open_connection(&cli)) {
383 mem_ctx = talloc_init("ioctl_test");
385 printf("starting ioctl test\n");
387 smbcli_unlink(cli->tree, fname);
389 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
391 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
395 parms.ioctl.level = RAW_IOCTL_IOCTL;
396 parms.ioctl.in.file.fnum = fnum;
397 parms.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
398 status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
399 printf("ioctl job info: %s\n", smbcli_errstr(cli->tree));
401 for (device=0;device<0x100;device++) {
402 printf("testing device=0x%x\n", device);
403 for (function=0;function<0x100;function++) {
404 parms.ioctl.in.request = (device << 16) | function;
405 status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
407 if (NT_STATUS_IS_OK(status)) {
408 printf("ioctl device=0x%x function=0x%x OK : %d bytes\n",
409 device, function, (int)parms.ioctl.out.blob.length);
414 if (!torture_close_connection(cli)) {
422 static void sigcont(int sig)
426 double torture_create_procs(BOOL (*fn)(struct smbcli_state *, int), BOOL *result)
429 volatile pid_t *child_status;
430 volatile BOOL *child_status_out;
433 double start_time_limit = 10 + (torture_nprocs * 1.5);
434 char **unc_list = NULL;
436 int num_unc_names = 0;
443 signal(SIGCONT, sigcont);
445 child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*torture_nprocs);
447 printf("Failed to setup shared memory\n");
451 child_status_out = (volatile BOOL *)shm_setup(sizeof(BOOL)*torture_nprocs);
452 if (!child_status_out) {
453 printf("Failed to setup result status shared memory\n");
457 p = lp_parm_string(-1, "torture", "unclist");
459 unc_list = file_lines_load(p, &num_unc_names, NULL);
460 if (!unc_list || num_unc_names <= 0) {
461 printf("Failed to load unc names list from '%s'\n", p);
466 for (i = 0; i < torture_nprocs; i++) {
468 child_status_out[i] = True;
471 tv = timeval_current();
473 for (i=0;i<torture_nprocs;i++) {
477 char *hostname=NULL, *sharename;
479 pid_t mypid = getpid();
480 srandom(((int)mypid) ^ ((int)time(NULL)));
482 asprintf(&myname, "CLIENT%d", i);
483 lp_set_cmdline("netbios name", myname);
488 if (!smbcli_parse_unc(unc_list[i % num_unc_names],
489 NULL, &hostname, &sharename)) {
490 printf("Failed to parse UNC name %s\n",
491 unc_list[i % num_unc_names]);
498 if (torture_open_connection_share(NULL,
505 } else if (torture_open_connection(¤t_cli)) {
509 printf("pid %d failed to start\n", (int)getpid());
515 child_status[i] = getpid();
519 if (child_status[i]) {
520 printf("Child %d failed to start!\n", i);
521 child_status_out[i] = 1;
525 child_status_out[i] = fn(current_cli, i);
532 for (i=0;i<torture_nprocs;i++) {
533 if (child_status[i]) synccount++;
535 if (synccount == torture_nprocs) break;
537 } while (timeval_elapsed(&tv) < start_time_limit);
539 if (synccount != torture_nprocs) {
540 printf("FAILED TO START %d CLIENTS (started %d)\n", torture_nprocs, synccount);
542 return timeval_elapsed(&tv);
545 printf("Starting %d clients\n", torture_nprocs);
547 /* start the client load */
548 tv = timeval_current();
549 for (i=0;i<torture_nprocs;i++) {
553 printf("%d clients started\n", torture_nprocs);
557 for (i=0;i<torture_nprocs;i++) {
559 while ((ret=waitpid(0, &status, 0)) == -1 && errno == EINTR) /* noop */ ;
560 if (ret == -1 || WEXITSTATUS(status) != 0) {
567 for (i=0;i<torture_nprocs;i++) {
568 if (!child_status_out[i]) {
572 return timeval_elapsed(&tv);
575 #define FLAG_MULTIPROC 1
579 BOOL (*fn)(struct torture_context *);
580 BOOL (*multi_fn)(struct smbcli_state *, int );
581 } builtin_torture_ops[] = {
582 /* benchmarking tests */
583 {"BENCH-HOLDCON", torture_holdcon, 0},
584 {"BENCH-NBENCH", torture_nbench, 0},
585 {"BENCH-TORTURE", NULL, run_torture},
586 {"BENCH-NBT", torture_bench_nbt, 0},
587 {"BENCH-WINS", torture_bench_wins, 0},
588 {"BENCH-CLDAP", torture_bench_cldap, 0},
589 {"BENCH-OPLOCK", torture_bench_oplock, 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-STREAMS", torture_raw_streams, 0},
614 {"RAW-ACLS", torture_raw_acls, 0},
615 {"RAW-COMPOSITE", torture_raw_composite, 0},
616 {"RAW-SAMBA3HIDE", torture_samba3_hide, 0},
619 {"SMB2-CONNECT", torture_smb2_connect, 0},
620 {"SMB2-SCAN", torture_smb2_scan, 0},
621 {"SMB2-SCANGETINFO", torture_smb2_getinfo_scan, 0},
622 {"SMB2-SCANSETINFO", torture_smb2_setinfo_scan, 0},
623 {"SMB2-SCANFIND", torture_smb2_find_scan, 0},
624 {"SMB2-GETINFO", torture_smb2_getinfo, 0},
625 {"SMB2-SETINFO", torture_smb2_setinfo, 0},
626 {"SMB2-FIND", torture_smb2_find, 0},
627 {"SMB2-LOCK", torture_smb2_lock, 0},
630 {"RAP-BASIC", torture_rap_basic, 0},
632 /* protocol scanners */
633 {"SCAN-MAXFID", NULL, run_maxfidtest},
634 {"SCAN-PIPE_NUMBER", run_pipe_number, 0},
635 {"SCAN-IOCTL", torture_ioctl_test, 0},
636 {"SCAN-RAP", torture_rap_scan, 0},
637 {"SCAN-EAMAX", torture_max_eas, 0},
639 /* local (no server) testers */
640 {"LOCAL-NTLMSSP", torture_ntlmssp_self_check, 0},
641 {"LOCAL-ICONV", torture_local_iconv, 0},
642 {"LOCAL-TALLOC", torture_local_talloc, 0},
643 {"LOCAL-MESSAGING", torture_local_messaging, 0},
644 {"LOCAL-IRPC", torture_local_irpc, 0},
645 {"LOCAL-BINDING", torture_local_binding_string, 0},
646 {"LOCAL-STRLIST", torture_local_util_strlist, 0},
647 {"LOCAL-FILE", torture_local_util_file, 0},
648 {"LOCAL-IDTREE", torture_local_idtree, 0},
649 {"LOCAL-SOCKET", torture_local_socket, 0},
650 {"LOCAL-PAC", torture_pac, 0},
651 {"LOCAL-REGISTRY", torture_registry, 0},
652 {"LOCAL-RESOLVE", torture_local_resolve, 0},
653 {"LOCAL-SDDL", torture_local_sddl, 0},
654 {"LOCAL-NDR", torture_local_ndr, 0},
655 {"LOCAL-EVENT", torture_local_event, 0},
658 {"LDAP-BASIC", torture_ldap_basic, 0},
659 {"LDAP-SCHEMA", torture_ldap_schema, 0},
660 {"LDAP-CLDAP", torture_cldap, 0},
663 {"NBT-REGISTER", torture_nbt_register, 0},
664 {"NBT-WINS", torture_nbt_wins, 0},
665 {"NBT-DGRAM", torture_nbt_dgram, 0},
666 {"NBT-BROWSE", torture_nbt_browse, 0},
667 {"NBT-WINSREPLICATION-SIMPLE", torture_nbt_winsreplication_simple, 0},
668 {"NBT-WINSREPLICATION-REPLICA", torture_nbt_winsreplication_replica, 0},
669 {"NBT-WINSREPLICATION-OWNED", torture_nbt_winsreplication_owned, 0},
673 static void register_builtin_ops(void)
676 for (i = 0; builtin_torture_ops[i].name; i++) {
677 register_torture_op(builtin_torture_ops[i].name,
678 builtin_torture_ops[i].fn,
679 builtin_torture_ops[i].multi_fn);
683 struct torture_op *torture_ops = NULL;
685 _PUBLIC_ NTSTATUS register_torture_op(const char *name, BOOL (*fn)(struct torture_context *), BOOL (*multi_fn)(struct smbcli_state *, int ))
687 struct torture_op *op, *p;
689 op = talloc(talloc_autofree_context(), struct torture_op);
691 op->name = talloc_strdup(op, name);
693 op->multi_fn = multi_fn;
695 for (p = torture_ops; p; p = p->next) {
696 if (strcmp(p->name, op->name) == 0) {
697 /* Check for duplicates */
698 DEBUG(0,("There already is a torture op registered with the name %s!\n", name));
700 return NT_STATUS_OBJECT_NAME_COLLISION;
703 if (strcmp(p->name, op->name) < 0 &&
704 (!p->next || strcmp(p->next->name, op->name) > 0)) {
705 DLIST_ADD_AFTER(torture_ops, op, p);
710 DLIST_ADD(torture_ops, op);
715 int torture_init(void)
717 init_module_fn static_init[] = STATIC_torture_MODULES;
718 init_module_fn *shared_init = load_samba_modules(NULL, "torture");
720 register_builtin_ops();
722 run_init_functions(static_init);
723 run_init_functions(shared_init);
725 talloc_free(shared_init);