libcli/named_pipe_auth: we need to hide length of the message mode header from the...
[Samba/gebeck_regimport.git] / source3 / torture / torture.c
blobb454a7d46dd34d7fa5bf98321fb52c56e6f98709
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
4 Copyright (C) Andrew Tridgell 1997-1998
5 Copyright (C) Jeremy Allison 2009
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "nsswitch/libwbclient/wbc_async.h"
23 #include "torture/proto.h"
24 #include "libcli/security/dom_sid.h"
26 extern char *optarg;
27 extern int optind;
29 static fstring host, workgroup, share, password, username, myname;
30 static int max_protocol = PROTOCOL_NT1;
31 static const char *sockops="TCP_NODELAY";
32 static int nprocs=1;
33 static int port_to_use=0;
34 int torture_numops=100;
35 int torture_blocksize=1024*1024;
36 static int procnum; /* records process count number when forking */
37 static struct cli_state *current_cli;
38 static fstring randomfname;
39 static bool use_oplocks;
40 static bool use_level_II_oplocks;
41 static const char *client_txt = "client_oplocks.txt";
42 static bool use_kerberos;
43 static fstring multishare_conn_fname;
44 static bool use_multishare_conn = False;
45 static bool do_encrypt;
46 static const char *local_path = NULL;
48 bool torture_showall = False;
50 static double create_procs(bool (*fn)(int), bool *result);
53 /* return a pointer to a anonymous shared memory segment of size "size"
54 which will persist across fork() but will disappear when all processes
55 exit
57 The memory is not zeroed
59 This function uses system5 shared memory. It takes advantage of a property
60 that the memory is not destroyed if it is attached when the id is removed
62 void *shm_setup(int size)
64 int shmid;
65 void *ret;
67 #ifdef __QNXNTO__
68 shmid = shm_open("private", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
69 if (shmid == -1) {
70 printf("can't get shared memory\n");
71 exit(1);
73 shm_unlink("private");
74 if (ftruncate(shmid, size) == -1) {
75 printf("can't set shared memory size\n");
76 exit(1);
78 ret = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0);
79 if (ret == MAP_FAILED) {
80 printf("can't map shared memory\n");
81 exit(1);
83 #else
84 shmid = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
85 if (shmid == -1) {
86 printf("can't get shared memory\n");
87 exit(1);
89 ret = (void *)shmat(shmid, 0, 0);
90 if (!ret || ret == (void *)-1) {
91 printf("can't attach to shared memory\n");
92 return NULL;
94 /* the following releases the ipc, but note that this process
95 and all its children will still have access to the memory, its
96 just that the shmid is no longer valid for other shm calls. This
97 means we don't leave behind lots of shm segments after we exit
99 See Stevens "advanced programming in unix env" for details
101 shmctl(shmid, IPC_RMID, 0);
102 #endif
104 return ret;
107 /********************************************************************
108 Ensure a connection is encrypted.
109 ********************************************************************/
111 static bool force_cli_encryption(struct cli_state *c,
112 const char *sharename)
114 uint16 major, minor;
115 uint32 caplow, caphigh;
116 NTSTATUS status;
118 if (!SERVER_HAS_UNIX_CIFS(c)) {
119 d_printf("Encryption required and "
120 "server that doesn't support "
121 "UNIX extensions - failing connect\n");
122 return false;
125 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
126 &caphigh);
127 if (!NT_STATUS_IS_OK(status)) {
128 d_printf("Encryption required and "
129 "can't get UNIX CIFS extensions "
130 "version from server: %s\n", nt_errstr(status));
131 return false;
134 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
135 d_printf("Encryption required and "
136 "share %s doesn't support "
137 "encryption.\n", sharename);
138 return false;
141 if (c->use_kerberos) {
142 status = cli_gss_smb_encryption_start(c);
143 } else {
144 status = cli_raw_ntlm_smb_encryption_start(c,
145 username,
146 password,
147 workgroup);
150 if (!NT_STATUS_IS_OK(status)) {
151 d_printf("Encryption required and "
152 "setup failed with error %s.\n",
153 nt_errstr(status));
154 return false;
157 return true;
161 static struct cli_state *open_nbt_connection(void)
163 struct nmb_name called, calling;
164 struct sockaddr_storage ss;
165 struct cli_state *c;
166 NTSTATUS status;
168 make_nmb_name(&calling, myname, 0x0);
169 make_nmb_name(&called , host, 0x20);
171 zero_sockaddr(&ss);
173 if (!(c = cli_initialise())) {
174 printf("Failed initialize cli_struct to connect with %s\n", host);
175 return NULL;
178 c->port = port_to_use;
180 status = cli_connect(c, host, &ss);
181 if (!NT_STATUS_IS_OK(status)) {
182 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
183 return NULL;
186 c->use_kerberos = use_kerberos;
188 c->timeout = 120000; /* set a really long timeout (2 minutes) */
189 if (use_oplocks) c->use_oplocks = True;
190 if (use_level_II_oplocks) c->use_level_II_oplocks = True;
192 if (!cli_session_request(c, &calling, &called)) {
194 * Well, that failed, try *SMBSERVER ...
195 * However, we must reconnect as well ...
197 status = cli_connect(c, host, &ss);
198 if (!NT_STATUS_IS_OK(status)) {
199 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
200 return NULL;
203 make_nmb_name(&called, "*SMBSERVER", 0x20);
204 if (!cli_session_request(c, &calling, &called)) {
205 printf("%s rejected the session\n",host);
206 printf("We tried with a called name of %s & %s\n",
207 host, "*SMBSERVER");
208 cli_shutdown(c);
209 return NULL;
213 return c;
216 /* Insert a NULL at the first separator of the given path and return a pointer
217 * to the remainder of the string.
219 static char *
220 terminate_path_at_separator(char * path)
222 char * p;
224 if (!path) {
225 return NULL;
228 if ((p = strchr_m(path, '/'))) {
229 *p = '\0';
230 return p + 1;
233 if ((p = strchr_m(path, '\\'))) {
234 *p = '\0';
235 return p + 1;
238 /* No separator. */
239 return NULL;
243 parse a //server/share type UNC name
245 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
246 char **hostname, char **sharename)
248 char *p;
250 *hostname = *sharename = NULL;
252 if (strncmp(unc_name, "\\\\", 2) &&
253 strncmp(unc_name, "//", 2)) {
254 return False;
257 *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
258 p = terminate_path_at_separator(*hostname);
260 if (p && *p) {
261 *sharename = talloc_strdup(mem_ctx, p);
262 terminate_path_at_separator(*sharename);
265 if (*hostname && *sharename) {
266 return True;
269 TALLOC_FREE(*hostname);
270 TALLOC_FREE(*sharename);
271 return False;
274 static bool torture_open_connection_share(struct cli_state **c,
275 const char *hostname,
276 const char *sharename)
278 bool retry;
279 int flags = 0;
280 NTSTATUS status;
282 if (use_kerberos)
283 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
284 if (use_oplocks)
285 flags |= CLI_FULL_CONNECTION_OPLOCKS;
286 if (use_level_II_oplocks)
287 flags |= CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS;
289 status = cli_full_connection(c, myname,
290 hostname, NULL, port_to_use,
291 sharename, "?????",
292 username, workgroup,
293 password, flags, Undefined, &retry);
294 if (!NT_STATUS_IS_OK(status)) {
295 printf("failed to open share connection: //%s/%s port:%d - %s\n",
296 hostname, sharename, port_to_use, nt_errstr(status));
297 return False;
300 (*c)->timeout = 120000; /* set a really long timeout (2 minutes) */
302 if (do_encrypt) {
303 return force_cli_encryption(*c,
304 sharename);
306 return True;
309 bool torture_open_connection(struct cli_state **c, int conn_index)
311 char **unc_list = NULL;
312 int num_unc_names = 0;
313 bool result;
315 if (use_multishare_conn==True) {
316 char *h, *s;
317 unc_list = file_lines_load(multishare_conn_fname, &num_unc_names, 0, NULL);
318 if (!unc_list || num_unc_names <= 0) {
319 printf("Failed to load unc names list from '%s'\n", multishare_conn_fname);
320 exit(1);
323 if (!smbcli_parse_unc(unc_list[conn_index % num_unc_names],
324 NULL, &h, &s)) {
325 printf("Failed to parse UNC name %s\n",
326 unc_list[conn_index % num_unc_names]);
327 TALLOC_FREE(unc_list);
328 exit(1);
331 result = torture_open_connection_share(c, h, s);
333 /* h, s were copied earlier */
334 TALLOC_FREE(unc_list);
335 return result;
338 return torture_open_connection_share(c, host, share);
341 bool torture_cli_session_setup2(struct cli_state *cli, uint16 *new_vuid)
343 uint16 old_vuid = cli->vuid;
344 fstring old_user_name;
345 size_t passlen = strlen(password);
346 NTSTATUS status;
347 bool ret;
349 fstrcpy(old_user_name, cli->user_name);
350 cli->vuid = 0;
351 ret = NT_STATUS_IS_OK(cli_session_setup(cli, username,
352 password, passlen,
353 password, passlen,
354 workgroup));
355 *new_vuid = cli->vuid;
356 cli->vuid = old_vuid;
357 status = cli_set_username(cli, old_user_name);
358 if (!NT_STATUS_IS_OK(status)) {
359 return false;
361 return ret;
365 bool torture_close_connection(struct cli_state *c)
367 bool ret = True;
368 NTSTATUS status;
370 status = cli_tdis(c);
371 if (!NT_STATUS_IS_OK(status)) {
372 printf("tdis failed (%s)\n", nt_errstr(status));
373 ret = False;
376 cli_shutdown(c);
378 return ret;
382 /* check if the server produced the expected error code */
383 static bool check_error(int line, struct cli_state *c,
384 uint8 eclass, uint32 ecode, NTSTATUS nterr)
386 if (cli_is_dos_error(c)) {
387 uint8 cclass;
388 uint32 num;
390 /* Check DOS error */
392 cli_dos_error(c, &cclass, &num);
394 if (eclass != cclass || ecode != num) {
395 printf("unexpected error code class=%d code=%d\n",
396 (int)cclass, (int)num);
397 printf(" expected %d/%d %s (line=%d)\n",
398 (int)eclass, (int)ecode, nt_errstr(nterr), line);
399 return False;
402 } else {
403 NTSTATUS status;
405 /* Check NT error */
407 status = cli_nt_error(c);
409 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
410 printf("unexpected error code %s\n", nt_errstr(status));
411 printf(" expected %s (line=%d)\n", nt_errstr(nterr), line);
412 return False;
416 return True;
420 static bool wait_lock(struct cli_state *c, int fnum, uint32 offset, uint32 len)
422 while (!cli_lock(c, fnum, offset, len, -1, WRITE_LOCK)) {
423 if (!check_error(__LINE__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
425 return True;
429 static bool rw_torture(struct cli_state *c)
431 const char *lockfname = "\\torture.lck";
432 fstring fname;
433 uint16_t fnum;
434 uint16_t fnum2;
435 pid_t pid2, pid = getpid();
436 int i, j;
437 char buf[1024];
438 bool correct = True;
439 NTSTATUS status;
441 memset(buf, '\0', sizeof(buf));
443 status = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
444 DENY_NONE, &fnum2);
445 if (!NT_STATUS_IS_OK(status)) {
446 status = cli_open(c, lockfname, O_RDWR, DENY_NONE, &fnum2);
448 if (!NT_STATUS_IS_OK(status)) {
449 printf("open of %s failed (%s)\n", lockfname, cli_errstr(c));
450 return False;
453 for (i=0;i<torture_numops;i++) {
454 unsigned n = (unsigned)sys_random()%10;
455 if (i % 10 == 0) {
456 printf("%d\r", i); fflush(stdout);
458 slprintf(fname, sizeof(fstring) - 1, "\\torture.%u", n);
460 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
461 return False;
464 if (!NT_STATUS_IS_OK(cli_open(c, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL, &fnum))) {
465 printf("open failed (%s)\n", cli_errstr(c));
466 correct = False;
467 break;
470 if (cli_write(c, fnum, 0, (char *)&pid, 0, sizeof(pid)) != sizeof(pid)) {
471 printf("write failed (%s)\n", cli_errstr(c));
472 correct = False;
475 for (j=0;j<50;j++) {
476 if (cli_write(c, fnum, 0, (char *)buf,
477 sizeof(pid)+(j*sizeof(buf)),
478 sizeof(buf)) != sizeof(buf)) {
479 printf("write failed (%s)\n", cli_errstr(c));
480 correct = False;
484 pid2 = 0;
486 if (cli_read(c, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
487 printf("read failed (%s)\n", cli_errstr(c));
488 correct = False;
491 if (pid2 != pid) {
492 printf("data corruption!\n");
493 correct = False;
496 if (!NT_STATUS_IS_OK(cli_close(c, fnum))) {
497 printf("close failed (%s)\n", cli_errstr(c));
498 correct = False;
501 if (!NT_STATUS_IS_OK(cli_unlink(c, fname, aSYSTEM | aHIDDEN))) {
502 printf("unlink failed (%s)\n", cli_errstr(c));
503 correct = False;
506 if (!NT_STATUS_IS_OK(cli_unlock(c, fnum2, n*sizeof(int), sizeof(int)))) {
507 printf("unlock failed (%s)\n", cli_errstr(c));
508 correct = False;
512 cli_close(c, fnum2);
513 cli_unlink(c, lockfname, aSYSTEM | aHIDDEN);
515 printf("%d\n", i);
517 return correct;
520 static bool run_torture(int dummy)
522 struct cli_state *cli;
523 bool ret;
525 cli = current_cli;
527 cli_sockopt(cli, sockops);
529 ret = rw_torture(cli);
531 if (!torture_close_connection(cli)) {
532 ret = False;
535 return ret;
538 static bool rw_torture3(struct cli_state *c, char *lockfname)
540 uint16_t fnum = (uint16_t)-1;
541 unsigned int i = 0;
542 char buf[131072];
543 char buf_rd[131072];
544 unsigned count;
545 unsigned countprev = 0;
546 ssize_t sent = 0;
547 bool correct = True;
548 NTSTATUS status;
550 srandom(1);
551 for (i = 0; i < sizeof(buf); i += sizeof(uint32))
553 SIVAL(buf, i, sys_random());
556 if (procnum == 0)
558 if (!NT_STATUS_IS_OK(cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
559 DENY_NONE, &fnum))) {
560 printf("first open read/write of %s failed (%s)\n",
561 lockfname, cli_errstr(c));
562 return False;
565 else
567 for (i = 0; i < 500 && fnum == (uint16_t)-1; i++)
569 status = cli_open(c, lockfname, O_RDONLY,
570 DENY_NONE, &fnum);
571 if (!NT_STATUS_IS_OK(status)) {
572 break;
574 smb_msleep(10);
576 if (!NT_STATUS_IS_OK(status)) {
577 printf("second open read-only of %s failed (%s)\n",
578 lockfname, cli_errstr(c));
579 return False;
583 i = 0;
584 for (count = 0; count < sizeof(buf); count += sent)
586 if (count >= countprev) {
587 printf("%d %8d\r", i, count);
588 fflush(stdout);
589 i++;
590 countprev += (sizeof(buf) / 20);
593 if (procnum == 0)
595 sent = ((unsigned)sys_random()%(20))+ 1;
596 if (sent > sizeof(buf) - count)
598 sent = sizeof(buf) - count;
601 if (cli_write(c, fnum, 0, buf+count, count, (size_t)sent) != sent) {
602 printf("write failed (%s)\n", cli_errstr(c));
603 correct = False;
606 else
608 sent = cli_read(c, fnum, buf_rd+count, count,
609 sizeof(buf)-count);
610 if (sent < 0)
612 printf("read failed offset:%d size:%ld (%s)\n",
613 count, (unsigned long)sizeof(buf)-count,
614 cli_errstr(c));
615 correct = False;
616 sent = 0;
618 if (sent > 0)
620 if (memcmp(buf_rd+count, buf+count, sent) != 0)
622 printf("read/write compare failed\n");
623 printf("offset: %d req %ld recvd %ld\n", count, (unsigned long)sizeof(buf)-count, (unsigned long)sent);
624 correct = False;
625 break;
632 if (!NT_STATUS_IS_OK(cli_close(c, fnum))) {
633 printf("close failed (%s)\n", cli_errstr(c));
634 correct = False;
637 return correct;
640 static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
642 const char *lockfname = "\\torture2.lck";
643 uint16_t fnum1;
644 uint16_t fnum2;
645 int i;
646 char buf[131072];
647 char buf_rd[131072];
648 bool correct = True;
649 ssize_t bytes_read;
651 if (!NT_STATUS_IS_OK(cli_unlink(c1, lockfname, aSYSTEM | aHIDDEN))) {
652 printf("unlink failed (%s) (normal, this file should not exist)\n", cli_errstr(c1));
655 if (!NT_STATUS_IS_OK(cli_open(c1, lockfname, O_RDWR | O_CREAT | O_EXCL,
656 DENY_NONE, &fnum1))) {
657 printf("first open read/write of %s failed (%s)\n",
658 lockfname, cli_errstr(c1));
659 return False;
661 if (!NT_STATUS_IS_OK(cli_open(c2, lockfname, O_RDONLY,
662 DENY_NONE, &fnum2))) {
663 printf("second open read-only of %s failed (%s)\n",
664 lockfname, cli_errstr(c2));
665 cli_close(c1, fnum1);
666 return False;
669 for (i=0;i<torture_numops;i++)
671 size_t buf_size = ((unsigned)sys_random()%(sizeof(buf)-1))+ 1;
672 if (i % 10 == 0) {
673 printf("%d\r", i); fflush(stdout);
676 generate_random_buffer((unsigned char *)buf, buf_size);
678 if (cli_write(c1, fnum1, 0, buf, 0, buf_size) != buf_size) {
679 printf("write failed (%s)\n", cli_errstr(c1));
680 correct = False;
681 break;
684 if ((bytes_read = cli_read(c2, fnum2, buf_rd, 0, buf_size)) != buf_size) {
685 printf("read failed (%s)\n", cli_errstr(c2));
686 printf("read %d, expected %ld\n", (int)bytes_read,
687 (unsigned long)buf_size);
688 correct = False;
689 break;
692 if (memcmp(buf_rd, buf, buf_size) != 0)
694 printf("read/write compare failed\n");
695 correct = False;
696 break;
700 if (!NT_STATUS_IS_OK(cli_close(c2, fnum2))) {
701 printf("close failed (%s)\n", cli_errstr(c2));
702 correct = False;
704 if (!NT_STATUS_IS_OK(cli_close(c1, fnum1))) {
705 printf("close failed (%s)\n", cli_errstr(c1));
706 correct = False;
709 if (!NT_STATUS_IS_OK(cli_unlink(c1, lockfname, aSYSTEM | aHIDDEN))) {
710 printf("unlink failed (%s)\n", cli_errstr(c1));
711 correct = False;
714 return correct;
717 static bool run_readwritetest(int dummy)
719 struct cli_state *cli1, *cli2;
720 bool test1, test2 = False;
722 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
723 return False;
725 cli_sockopt(cli1, sockops);
726 cli_sockopt(cli2, sockops);
728 printf("starting readwritetest\n");
730 test1 = rw_torture2(cli1, cli2);
731 printf("Passed readwritetest v1: %s\n", BOOLSTR(test1));
733 if (test1) {
734 test2 = rw_torture2(cli1, cli1);
735 printf("Passed readwritetest v2: %s\n", BOOLSTR(test2));
738 if (!torture_close_connection(cli1)) {
739 test1 = False;
742 if (!torture_close_connection(cli2)) {
743 test2 = False;
746 return (test1 && test2);
749 static bool run_readwritemulti(int dummy)
751 struct cli_state *cli;
752 bool test;
754 cli = current_cli;
756 cli_sockopt(cli, sockops);
758 printf("run_readwritemulti: fname %s\n", randomfname);
759 test = rw_torture3(cli, randomfname);
761 if (!torture_close_connection(cli)) {
762 test = False;
765 return test;
768 static bool run_readwritelarge(int dummy)
770 static struct cli_state *cli1;
771 uint16_t fnum1;
772 const char *lockfname = "\\large.dat";
773 SMB_OFF_T fsize;
774 char buf[126*1024];
775 bool correct = True;
777 if (!torture_open_connection(&cli1, 0)) {
778 return False;
780 cli_sockopt(cli1, sockops);
781 memset(buf,'\0',sizeof(buf));
783 cli1->max_xmit = 128*1024;
785 printf("starting readwritelarge\n");
787 cli_unlink(cli1, lockfname, aSYSTEM | aHIDDEN);
789 if (!NT_STATUS_IS_OK(cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE, &fnum1))) {
790 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
791 return False;
794 cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf));
796 if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
797 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
798 correct = False;
801 if (fsize == sizeof(buf))
802 printf("readwritelarge test 1 succeeded (size = %lx)\n",
803 (unsigned long)fsize);
804 else {
805 printf("readwritelarge test 1 failed (size = %lx)\n",
806 (unsigned long)fsize);
807 correct = False;
810 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
811 printf("close failed (%s)\n", cli_errstr(cli1));
812 correct = False;
815 if (!NT_STATUS_IS_OK(cli_unlink(cli1, lockfname, aSYSTEM | aHIDDEN))) {
816 printf("unlink failed (%s)\n", cli_errstr(cli1));
817 correct = False;
820 if (!NT_STATUS_IS_OK(cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE, &fnum1))) {
821 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
822 return False;
825 cli1->max_xmit = 4*1024;
827 cli_smbwrite(cli1, fnum1, buf, 0, sizeof(buf));
829 if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
830 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
831 correct = False;
834 if (fsize == sizeof(buf))
835 printf("readwritelarge test 2 succeeded (size = %lx)\n",
836 (unsigned long)fsize);
837 else {
838 printf("readwritelarge test 2 failed (size = %lx)\n",
839 (unsigned long)fsize);
840 correct = False;
843 #if 0
844 /* ToDo - set allocation. JRA */
845 if(!cli_set_allocation_size(cli1, fnum1, 0)) {
846 printf("set allocation size to zero failed (%s)\n", cli_errstr(&cli1));
847 return False;
849 if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
850 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
851 correct = False;
853 if (fsize != 0)
854 printf("readwritelarge test 3 (truncate test) succeeded (size = %x)\n", fsize);
855 #endif
857 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
858 printf("close failed (%s)\n", cli_errstr(cli1));
859 correct = False;
862 if (!torture_close_connection(cli1)) {
863 correct = False;
865 return correct;
868 int line_count = 0;
869 int nbio_id;
871 #define ival(s) strtol(s, NULL, 0)
873 /* run a test that simulates an approximate netbench client load */
874 static bool run_netbench(int client)
876 struct cli_state *cli;
877 int i;
878 char line[1024];
879 char cname[20];
880 FILE *f;
881 const char *params[20];
882 bool correct = True;
884 cli = current_cli;
886 nbio_id = client;
888 cli_sockopt(cli, sockops);
890 nb_setup(cli);
892 slprintf(cname,sizeof(cname)-1, "client%d", client);
894 f = fopen(client_txt, "r");
896 if (!f) {
897 perror(client_txt);
898 return False;
901 while (fgets(line, sizeof(line)-1, f)) {
902 char *saveptr;
903 line_count++;
905 line[strlen(line)-1] = 0;
907 /* printf("[%d] %s\n", line_count, line); */
909 all_string_sub(line,"client1", cname, sizeof(line));
911 /* parse the command parameters */
912 params[0] = strtok_r(line, " ", &saveptr);
913 i = 0;
914 while (params[i]) params[++i] = strtok_r(NULL, " ", &saveptr);
916 params[i] = "";
918 if (i < 2) continue;
920 if (!strncmp(params[0],"SMB", 3)) {
921 printf("ERROR: You are using a dbench 1 load file\n");
922 exit(1);
925 if (!strcmp(params[0],"NTCreateX")) {
926 nb_createx(params[1], ival(params[2]), ival(params[3]),
927 ival(params[4]));
928 } else if (!strcmp(params[0],"Close")) {
929 nb_close(ival(params[1]));
930 } else if (!strcmp(params[0],"Rename")) {
931 nb_rename(params[1], params[2]);
932 } else if (!strcmp(params[0],"Unlink")) {
933 nb_unlink(params[1]);
934 } else if (!strcmp(params[0],"Deltree")) {
935 nb_deltree(params[1]);
936 } else if (!strcmp(params[0],"Rmdir")) {
937 nb_rmdir(params[1]);
938 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
939 nb_qpathinfo(params[1]);
940 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
941 nb_qfileinfo(ival(params[1]));
942 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
943 nb_qfsinfo(ival(params[1]));
944 } else if (!strcmp(params[0],"FIND_FIRST")) {
945 nb_findfirst(params[1]);
946 } else if (!strcmp(params[0],"WriteX")) {
947 nb_writex(ival(params[1]),
948 ival(params[2]), ival(params[3]), ival(params[4]));
949 } else if (!strcmp(params[0],"ReadX")) {
950 nb_readx(ival(params[1]),
951 ival(params[2]), ival(params[3]), ival(params[4]));
952 } else if (!strcmp(params[0],"Flush")) {
953 nb_flush(ival(params[1]));
954 } else {
955 printf("Unknown operation %s\n", params[0]);
956 exit(1);
959 fclose(f);
961 nb_cleanup();
963 if (!torture_close_connection(cli)) {
964 correct = False;
967 return correct;
971 /* run a test that simulates an approximate netbench client load */
972 static bool run_nbench(int dummy)
974 double t;
975 bool correct = True;
977 nbio_shmem(nprocs);
979 nbio_id = -1;
981 signal(SIGALRM, nb_alarm);
982 alarm(1);
983 t = create_procs(run_netbench, &correct);
984 alarm(0);
986 printf("\nThroughput %g MB/sec\n",
987 1.0e-6 * nbio_total() / t);
988 return correct;
993 This test checks for two things:
995 1) correct support for retaining locks over a close (ie. the server
996 must not use posix semantics)
997 2) support for lock timeouts
999 static bool run_locktest1(int dummy)
1001 struct cli_state *cli1, *cli2;
1002 const char *fname = "\\lockt1.lck";
1003 uint16_t fnum1, fnum2, fnum3;
1004 time_t t1, t2;
1005 unsigned lock_timeout;
1007 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1008 return False;
1010 cli_sockopt(cli1, sockops);
1011 cli_sockopt(cli2, sockops);
1013 printf("starting locktest1\n");
1015 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1017 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1018 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1019 return False;
1021 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum2))) {
1022 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli1));
1023 return False;
1025 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum3))) {
1026 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli2));
1027 return False;
1030 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
1031 printf("lock1 failed (%s)\n", cli_errstr(cli1));
1032 return False;
1036 if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1037 printf("lock2 succeeded! This is a locking bug\n");
1038 return False;
1039 } else {
1040 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1041 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1045 lock_timeout = (1 + (random() % 20));
1046 printf("Testing lock timeout with timeout=%u\n", lock_timeout);
1047 t1 = time(NULL);
1048 if (cli_lock(cli2, fnum3, 0, 4, lock_timeout * 1000, WRITE_LOCK)) {
1049 printf("lock3 succeeded! This is a locking bug\n");
1050 return False;
1051 } else {
1052 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1053 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1055 t2 = time(NULL);
1057 if (ABS(t2 - t1) < lock_timeout-1) {
1058 printf("error: This server appears not to support timed lock requests\n");
1061 printf("server slept for %u seconds for a %u second timeout\n",
1062 (unsigned int)(t2-t1), lock_timeout);
1064 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
1065 printf("close1 failed (%s)\n", cli_errstr(cli1));
1066 return False;
1069 if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1070 printf("lock4 succeeded! This is a locking bug\n");
1071 return False;
1072 } else {
1073 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1074 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1077 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
1078 printf("close2 failed (%s)\n", cli_errstr(cli1));
1079 return False;
1082 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum3))) {
1083 printf("close3 failed (%s)\n", cli_errstr(cli2));
1084 return False;
1087 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
1088 printf("unlink failed (%s)\n", cli_errstr(cli1));
1089 return False;
1093 if (!torture_close_connection(cli1)) {
1094 return False;
1097 if (!torture_close_connection(cli2)) {
1098 return False;
1101 printf("Passed locktest1\n");
1102 return True;
1106 this checks to see if a secondary tconx can use open files from an
1107 earlier tconx
1109 static bool run_tcon_test(int dummy)
1111 static struct cli_state *cli;
1112 const char *fname = "\\tcontest.tmp";
1113 uint16 fnum1;
1114 uint16 cnum1, cnum2, cnum3;
1115 uint16 vuid1, vuid2;
1116 char buf[4];
1117 bool ret = True;
1118 NTSTATUS status;
1120 memset(buf, '\0', sizeof(buf));
1122 if (!torture_open_connection(&cli, 0)) {
1123 return False;
1125 cli_sockopt(cli, sockops);
1127 printf("starting tcontest\n");
1129 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
1131 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1132 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1133 return False;
1136 cnum1 = cli->cnum;
1137 vuid1 = cli->vuid;
1139 if (cli_write(cli, fnum1, 0, buf, 130, 4) != 4) {
1140 printf("initial write failed (%s)", cli_errstr(cli));
1141 return False;
1144 status = cli_tcon_andx(cli, share, "?????",
1145 password, strlen(password)+1);
1146 if (!NT_STATUS_IS_OK(status)) {
1147 printf("%s refused 2nd tree connect (%s)\n", host,
1148 nt_errstr(status));
1149 cli_shutdown(cli);
1150 return False;
1153 cnum2 = cli->cnum;
1154 cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
1155 vuid2 = cli->vuid + 1;
1157 /* try a write with the wrong tid */
1158 cli->cnum = cnum2;
1160 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1161 printf("* server allows write with wrong TID\n");
1162 ret = False;
1163 } else {
1164 printf("server fails write with wrong TID : %s\n", cli_errstr(cli));
1168 /* try a write with an invalid tid */
1169 cli->cnum = cnum3;
1171 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1172 printf("* server allows write with invalid TID\n");
1173 ret = False;
1174 } else {
1175 printf("server fails write with invalid TID : %s\n", cli_errstr(cli));
1178 /* try a write with an invalid vuid */
1179 cli->vuid = vuid2;
1180 cli->cnum = cnum1;
1182 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1183 printf("* server allows write with invalid VUID\n");
1184 ret = False;
1185 } else {
1186 printf("server fails write with invalid VUID : %s\n", cli_errstr(cli));
1189 cli->cnum = cnum1;
1190 cli->vuid = vuid1;
1192 if (!NT_STATUS_IS_OK(cli_close(cli, fnum1))) {
1193 printf("close failed (%s)\n", cli_errstr(cli));
1194 return False;
1197 cli->cnum = cnum2;
1199 status = cli_tdis(cli);
1200 if (!NT_STATUS_IS_OK(status)) {
1201 printf("secondary tdis failed (%s)\n", nt_errstr(status));
1202 return False;
1205 cli->cnum = cnum1;
1207 if (!torture_close_connection(cli)) {
1208 return False;
1211 return ret;
1216 checks for old style tcon support
1218 static bool run_tcon2_test(int dummy)
1220 static struct cli_state *cli;
1221 uint16 cnum, max_xmit;
1222 char *service;
1223 NTSTATUS status;
1225 if (!torture_open_connection(&cli, 0)) {
1226 return False;
1228 cli_sockopt(cli, sockops);
1230 printf("starting tcon2 test\n");
1232 if (asprintf(&service, "\\\\%s\\%s", host, share) == -1) {
1233 return false;
1236 status = cli_raw_tcon(cli, service, password, "?????", &max_xmit, &cnum);
1238 if (!NT_STATUS_IS_OK(status)) {
1239 printf("tcon2 failed : %s\n", cli_errstr(cli));
1240 } else {
1241 printf("tcon OK : max_xmit=%d cnum=%d tid=%d\n",
1242 (int)max_xmit, (int)cnum, SVAL(cli->inbuf, smb_tid));
1245 if (!torture_close_connection(cli)) {
1246 return False;
1249 printf("Passed tcon2 test\n");
1250 return True;
1253 static bool tcon_devtest(struct cli_state *cli,
1254 const char *myshare, const char *devtype,
1255 const char *return_devtype,
1256 NTSTATUS expected_error)
1258 NTSTATUS status;
1259 bool ret;
1261 status = cli_tcon_andx(cli, myshare, devtype,
1262 password, strlen(password)+1);
1264 if (NT_STATUS_IS_OK(expected_error)) {
1265 if (NT_STATUS_IS_OK(status)) {
1266 if (strcmp(cli->dev, return_devtype) == 0) {
1267 ret = True;
1268 } else {
1269 printf("tconX to share %s with type %s "
1270 "succeeded but returned the wrong "
1271 "device type (got [%s] but should have got [%s])\n",
1272 myshare, devtype, cli->dev, return_devtype);
1273 ret = False;
1275 } else {
1276 printf("tconX to share %s with type %s "
1277 "should have succeeded but failed\n",
1278 myshare, devtype);
1279 ret = False;
1281 cli_tdis(cli);
1282 } else {
1283 if (NT_STATUS_IS_OK(status)) {
1284 printf("tconx to share %s with type %s "
1285 "should have failed but succeeded\n",
1286 myshare, devtype);
1287 ret = False;
1288 } else {
1289 if (NT_STATUS_EQUAL(cli_nt_error(cli),
1290 expected_error)) {
1291 ret = True;
1292 } else {
1293 printf("Returned unexpected error\n");
1294 ret = False;
1298 return ret;
1302 checks for correct tconX support
1304 static bool run_tcon_devtype_test(int dummy)
1306 static struct cli_state *cli1 = NULL;
1307 bool retry;
1308 int flags = 0;
1309 NTSTATUS status;
1310 bool ret = True;
1312 status = cli_full_connection(&cli1, myname,
1313 host, NULL, port_to_use,
1314 NULL, NULL,
1315 username, workgroup,
1316 password, flags, Undefined, &retry);
1318 if (!NT_STATUS_IS_OK(status)) {
1319 printf("could not open connection\n");
1320 return False;
1323 if (!tcon_devtest(cli1, "IPC$", "A:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1324 ret = False;
1326 if (!tcon_devtest(cli1, "IPC$", "?????", "IPC", NT_STATUS_OK))
1327 ret = False;
1329 if (!tcon_devtest(cli1, "IPC$", "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1330 ret = False;
1332 if (!tcon_devtest(cli1, "IPC$", "IPC", "IPC", NT_STATUS_OK))
1333 ret = False;
1335 if (!tcon_devtest(cli1, "IPC$", "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1336 ret = False;
1338 if (!tcon_devtest(cli1, share, "A:", "A:", NT_STATUS_OK))
1339 ret = False;
1341 if (!tcon_devtest(cli1, share, "?????", "A:", NT_STATUS_OK))
1342 ret = False;
1344 if (!tcon_devtest(cli1, share, "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1345 ret = False;
1347 if (!tcon_devtest(cli1, share, "IPC", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1348 ret = False;
1350 if (!tcon_devtest(cli1, share, "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1351 ret = False;
1353 cli_shutdown(cli1);
1355 if (ret)
1356 printf("Passed tcondevtest\n");
1358 return ret;
1363 This test checks that
1365 1) the server supports multiple locking contexts on the one SMB
1366 connection, distinguished by PID.
1368 2) the server correctly fails overlapping locks made by the same PID (this
1369 goes against POSIX behaviour, which is why it is tricky to implement)
1371 3) the server denies unlock requests by an incorrect client PID
1373 static bool run_locktest2(int dummy)
1375 static struct cli_state *cli;
1376 const char *fname = "\\lockt2.lck";
1377 uint16_t fnum1, fnum2, fnum3;
1378 bool correct = True;
1380 if (!torture_open_connection(&cli, 0)) {
1381 return False;
1384 cli_sockopt(cli, sockops);
1386 printf("starting locktest2\n");
1388 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
1390 cli_setpid(cli, 1);
1392 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1393 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1394 return False;
1397 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum2))) {
1398 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli));
1399 return False;
1402 cli_setpid(cli, 2);
1404 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum3))) {
1405 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli));
1406 return False;
1409 cli_setpid(cli, 1);
1411 if (!cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1412 printf("lock1 failed (%s)\n", cli_errstr(cli));
1413 return False;
1416 if (cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1417 printf("WRITE lock1 succeeded! This is a locking bug\n");
1418 correct = False;
1419 } else {
1420 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1421 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1424 if (cli_lock(cli, fnum2, 0, 4, 0, WRITE_LOCK)) {
1425 printf("WRITE lock2 succeeded! This is a locking bug\n");
1426 correct = False;
1427 } else {
1428 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1429 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1432 if (cli_lock(cli, fnum2, 0, 4, 0, READ_LOCK)) {
1433 printf("READ lock2 succeeded! This is a locking bug\n");
1434 correct = False;
1435 } else {
1436 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1437 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1440 if (!cli_lock(cli, fnum1, 100, 4, 0, WRITE_LOCK)) {
1441 printf("lock at 100 failed (%s)\n", cli_errstr(cli));
1443 cli_setpid(cli, 2);
1444 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 100, 4))) {
1445 printf("unlock at 100 succeeded! This is a locking bug\n");
1446 correct = False;
1449 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 0, 4))) {
1450 printf("unlock1 succeeded! This is a locking bug\n");
1451 correct = False;
1452 } else {
1453 if (!check_error(__LINE__, cli,
1454 ERRDOS, ERRlock,
1455 NT_STATUS_RANGE_NOT_LOCKED)) return False;
1458 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 0, 8))) {
1459 printf("unlock2 succeeded! This is a locking bug\n");
1460 correct = False;
1461 } else {
1462 if (!check_error(__LINE__, cli,
1463 ERRDOS, ERRlock,
1464 NT_STATUS_RANGE_NOT_LOCKED)) return False;
1467 if (cli_lock(cli, fnum3, 0, 4, 0, WRITE_LOCK)) {
1468 printf("lock3 succeeded! This is a locking bug\n");
1469 correct = False;
1470 } else {
1471 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
1474 cli_setpid(cli, 1);
1476 if (!NT_STATUS_IS_OK(cli_close(cli, fnum1))) {
1477 printf("close1 failed (%s)\n", cli_errstr(cli));
1478 return False;
1481 if (!NT_STATUS_IS_OK(cli_close(cli, fnum2))) {
1482 printf("close2 failed (%s)\n", cli_errstr(cli));
1483 return False;
1486 if (!NT_STATUS_IS_OK(cli_close(cli, fnum3))) {
1487 printf("close3 failed (%s)\n", cli_errstr(cli));
1488 return False;
1491 if (!torture_close_connection(cli)) {
1492 correct = False;
1495 printf("locktest2 finished\n");
1497 return correct;
1502 This test checks that
1504 1) the server supports the full offset range in lock requests
1506 static bool run_locktest3(int dummy)
1508 static struct cli_state *cli1, *cli2;
1509 const char *fname = "\\lockt3.lck";
1510 uint16_t fnum1, fnum2;
1511 int i;
1512 uint32 offset;
1513 bool correct = True;
1515 #define NEXT_OFFSET offset += (~(uint32)0) / torture_numops
1517 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1518 return False;
1520 cli_sockopt(cli1, sockops);
1521 cli_sockopt(cli2, sockops);
1523 printf("starting locktest3\n");
1525 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1527 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1528 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1529 return False;
1531 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2))) {
1532 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli2));
1533 return False;
1536 for (offset=i=0;i<torture_numops;i++) {
1537 NEXT_OFFSET;
1538 if (!cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1539 printf("lock1 %d failed (%s)\n",
1541 cli_errstr(cli1));
1542 return False;
1545 if (!cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1546 printf("lock2 %d failed (%s)\n",
1548 cli_errstr(cli1));
1549 return False;
1553 for (offset=i=0;i<torture_numops;i++) {
1554 NEXT_OFFSET;
1556 if (cli_lock(cli1, fnum1, offset-2, 1, 0, WRITE_LOCK)) {
1557 printf("error: lock1 %d succeeded!\n", i);
1558 return False;
1561 if (cli_lock(cli2, fnum2, offset-1, 1, 0, WRITE_LOCK)) {
1562 printf("error: lock2 %d succeeded!\n", i);
1563 return False;
1566 if (cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1567 printf("error: lock3 %d succeeded!\n", i);
1568 return False;
1571 if (cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1572 printf("error: lock4 %d succeeded!\n", i);
1573 return False;
1577 for (offset=i=0;i<torture_numops;i++) {
1578 NEXT_OFFSET;
1580 if (!NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, offset-1, 1))) {
1581 printf("unlock1 %d failed (%s)\n",
1583 cli_errstr(cli1));
1584 return False;
1587 if (!NT_STATUS_IS_OK(cli_unlock(cli2, fnum2, offset-2, 1))) {
1588 printf("unlock2 %d failed (%s)\n",
1590 cli_errstr(cli1));
1591 return False;
1595 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
1596 printf("close1 failed (%s)\n", cli_errstr(cli1));
1597 return False;
1600 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
1601 printf("close2 failed (%s)\n", cli_errstr(cli2));
1602 return False;
1605 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
1606 printf("unlink failed (%s)\n", cli_errstr(cli1));
1607 return False;
1610 if (!torture_close_connection(cli1)) {
1611 correct = False;
1614 if (!torture_close_connection(cli2)) {
1615 correct = False;
1618 printf("finished locktest3\n");
1620 return correct;
1623 #define EXPECTED(ret, v) if ((ret) != (v)) { \
1624 printf("** "); correct = False; \
1628 looks at overlapping locks
1630 static bool run_locktest4(int dummy)
1632 static struct cli_state *cli1, *cli2;
1633 const char *fname = "\\lockt4.lck";
1634 uint16_t fnum1, fnum2, f;
1635 bool ret;
1636 char buf[1000];
1637 bool correct = True;
1639 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1640 return False;
1643 cli_sockopt(cli1, sockops);
1644 cli_sockopt(cli2, sockops);
1646 printf("starting locktest4\n");
1648 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1650 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1651 cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
1653 memset(buf, 0, sizeof(buf));
1655 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1656 printf("Failed to create file\n");
1657 correct = False;
1658 goto fail;
1661 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1662 cli_lock(cli1, fnum1, 2, 4, 0, WRITE_LOCK);
1663 EXPECTED(ret, False);
1664 printf("the same process %s set overlapping write locks\n", ret?"can":"cannot");
1666 ret = cli_lock(cli1, fnum1, 10, 4, 0, READ_LOCK) &&
1667 cli_lock(cli1, fnum1, 12, 4, 0, READ_LOCK);
1668 EXPECTED(ret, True);
1669 printf("the same process %s set overlapping read locks\n", ret?"can":"cannot");
1671 ret = cli_lock(cli1, fnum1, 20, 4, 0, WRITE_LOCK) &&
1672 cli_lock(cli2, fnum2, 22, 4, 0, WRITE_LOCK);
1673 EXPECTED(ret, False);
1674 printf("a different connection %s set overlapping write locks\n", ret?"can":"cannot");
1676 ret = cli_lock(cli1, fnum1, 30, 4, 0, READ_LOCK) &&
1677 cli_lock(cli2, fnum2, 32, 4, 0, READ_LOCK);
1678 EXPECTED(ret, True);
1679 printf("a different connection %s set overlapping read locks\n", ret?"can":"cannot");
1681 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 40, 4, 0, WRITE_LOCK)) &&
1682 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 42, 4, 0, WRITE_LOCK));
1683 EXPECTED(ret, False);
1684 printf("a different pid %s set overlapping write locks\n", ret?"can":"cannot");
1686 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 50, 4, 0, READ_LOCK)) &&
1687 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 52, 4, 0, READ_LOCK));
1688 EXPECTED(ret, True);
1689 printf("a different pid %s set overlapping read locks\n", ret?"can":"cannot");
1691 ret = cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK) &&
1692 cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK);
1693 EXPECTED(ret, True);
1694 printf("the same process %s set the same read lock twice\n", ret?"can":"cannot");
1696 ret = cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK) &&
1697 cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK);
1698 EXPECTED(ret, False);
1699 printf("the same process %s set the same write lock twice\n", ret?"can":"cannot");
1701 ret = cli_lock(cli1, fnum1, 80, 4, 0, READ_LOCK) &&
1702 cli_lock(cli1, fnum1, 80, 4, 0, WRITE_LOCK);
1703 EXPECTED(ret, False);
1704 printf("the same process %s overlay a read lock with a write lock\n", ret?"can":"cannot");
1706 ret = cli_lock(cli1, fnum1, 90, 4, 0, WRITE_LOCK) &&
1707 cli_lock(cli1, fnum1, 90, 4, 0, READ_LOCK);
1708 EXPECTED(ret, True);
1709 printf("the same process %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1711 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 100, 4, 0, WRITE_LOCK)) &&
1712 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 100, 4, 0, READ_LOCK));
1713 EXPECTED(ret, False);
1714 printf("a different pid %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1716 ret = cli_lock(cli1, fnum1, 110, 4, 0, READ_LOCK) &&
1717 cli_lock(cli1, fnum1, 112, 4, 0, READ_LOCK) &&
1718 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 110, 6));
1719 EXPECTED(ret, False);
1720 printf("the same process %s coalesce read locks\n", ret?"can":"cannot");
1723 ret = cli_lock(cli1, fnum1, 120, 4, 0, WRITE_LOCK) &&
1724 (cli_read(cli2, fnum2, buf, 120, 4) == 4);
1725 EXPECTED(ret, False);
1726 printf("this server %s strict write locking\n", ret?"doesn't do":"does");
1728 ret = cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK) &&
1729 (cli_write(cli2, fnum2, 0, buf, 130, 4) == 4);
1730 EXPECTED(ret, False);
1731 printf("this server %s strict read locking\n", ret?"doesn't do":"does");
1734 ret = cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1735 cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1736 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4)) &&
1737 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4));
1738 EXPECTED(ret, True);
1739 printf("this server %s do recursive read locking\n", ret?"does":"doesn't");
1742 ret = cli_lock(cli1, fnum1, 150, 4, 0, WRITE_LOCK) &&
1743 cli_lock(cli1, fnum1, 150, 4, 0, READ_LOCK) &&
1744 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4)) &&
1745 (cli_read(cli2, fnum2, buf, 150, 4) == 4) &&
1746 !(cli_write(cli2, fnum2, 0, buf, 150, 4) == 4) &&
1747 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4));
1748 EXPECTED(ret, True);
1749 printf("this server %s do recursive lock overlays\n", ret?"does":"doesn't");
1751 ret = cli_lock(cli1, fnum1, 160, 4, 0, READ_LOCK) &&
1752 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 160, 4)) &&
1753 (cli_write(cli2, fnum2, 0, buf, 160, 4) == 4) &&
1754 (cli_read(cli2, fnum2, buf, 160, 4) == 4);
1755 EXPECTED(ret, True);
1756 printf("the same process %s remove a read lock using write locking\n", ret?"can":"cannot");
1758 ret = cli_lock(cli1, fnum1, 170, 4, 0, WRITE_LOCK) &&
1759 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 170, 4)) &&
1760 (cli_write(cli2, fnum2, 0, buf, 170, 4) == 4) &&
1761 (cli_read(cli2, fnum2, buf, 170, 4) == 4);
1762 EXPECTED(ret, True);
1763 printf("the same process %s remove a write lock using read locking\n", ret?"can":"cannot");
1765 ret = cli_lock(cli1, fnum1, 190, 4, 0, WRITE_LOCK) &&
1766 cli_lock(cli1, fnum1, 190, 4, 0, READ_LOCK) &&
1767 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 190, 4)) &&
1768 !(cli_write(cli2, fnum2, 0, buf, 190, 4) == 4) &&
1769 (cli_read(cli2, fnum2, buf, 190, 4) == 4);
1770 EXPECTED(ret, True);
1771 printf("the same process %s remove the first lock first\n", ret?"does":"doesn't");
1773 cli_close(cli1, fnum1);
1774 cli_close(cli2, fnum2);
1775 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
1776 cli_open(cli1, fname, O_RDWR, DENY_NONE, &f);
1777 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1778 cli_lock(cli1, f, 0, 1, 0, READ_LOCK) &&
1779 NT_STATUS_IS_OK(cli_close(cli1, fnum1)) &&
1780 NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1)) &&
1781 cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1782 cli_close(cli1, f);
1783 cli_close(cli1, fnum1);
1784 EXPECTED(ret, True);
1785 printf("the server %s have the NT byte range lock bug\n", !ret?"does":"doesn't");
1787 fail:
1788 cli_close(cli1, fnum1);
1789 cli_close(cli2, fnum2);
1790 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1791 torture_close_connection(cli1);
1792 torture_close_connection(cli2);
1794 printf("finished locktest4\n");
1795 return correct;
1799 looks at lock upgrade/downgrade.
1801 static bool run_locktest5(int dummy)
1803 static struct cli_state *cli1, *cli2;
1804 const char *fname = "\\lockt5.lck";
1805 uint16_t fnum1, fnum2, fnum3;
1806 bool ret;
1807 char buf[1000];
1808 bool correct = True;
1810 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1811 return False;
1814 cli_sockopt(cli1, sockops);
1815 cli_sockopt(cli2, sockops);
1817 printf("starting locktest5\n");
1819 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1821 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1822 cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
1823 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum3);
1825 memset(buf, 0, sizeof(buf));
1827 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1828 printf("Failed to create file\n");
1829 correct = False;
1830 goto fail;
1833 /* Check for NT bug... */
1834 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1835 cli_lock(cli1, fnum3, 0, 1, 0, READ_LOCK);
1836 cli_close(cli1, fnum1);
1837 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
1838 ret = cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1839 EXPECTED(ret, True);
1840 printf("this server %s the NT locking bug\n", ret ? "doesn't have" : "has");
1841 cli_close(cli1, fnum1);
1842 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
1843 cli_unlock(cli1, fnum3, 0, 1);
1845 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1846 cli_lock(cli1, fnum1, 1, 1, 0, READ_LOCK);
1847 EXPECTED(ret, True);
1848 printf("the same process %s overlay a write with a read lock\n", ret?"can":"cannot");
1850 ret = cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
1851 EXPECTED(ret, False);
1853 printf("a different processs %s get a read lock on the first process lock stack\n", ret?"can":"cannot");
1855 /* Unlock the process 2 lock. */
1856 cli_unlock(cli2, fnum2, 0, 4);
1858 ret = cli_lock(cli1, fnum3, 0, 4, 0, READ_LOCK);
1859 EXPECTED(ret, False);
1861 printf("the same processs on a different fnum %s get a read lock\n", ret?"can":"cannot");
1863 /* Unlock the process 1 fnum3 lock. */
1864 cli_unlock(cli1, fnum3, 0, 4);
1866 /* Stack 2 more locks here. */
1867 ret = cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK) &&
1868 cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK);
1870 EXPECTED(ret, True);
1871 printf("the same process %s stack read locks\n", ret?"can":"cannot");
1873 /* Unlock the first process lock, then check this was the WRITE lock that was
1874 removed. */
1876 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4)) &&
1877 cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
1879 EXPECTED(ret, True);
1880 printf("the first unlock removes the %s lock\n", ret?"WRITE":"READ");
1882 /* Unlock the process 2 lock. */
1883 cli_unlock(cli2, fnum2, 0, 4);
1885 /* We should have 3 stacked locks here. Ensure we need to do 3 unlocks. */
1887 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 1, 1)) &&
1888 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4)) &&
1889 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4));
1891 EXPECTED(ret, True);
1892 printf("the same process %s unlock the stack of 4 locks\n", ret?"can":"cannot");
1894 /* Ensure the next unlock fails. */
1895 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4));
1896 EXPECTED(ret, False);
1897 printf("the same process %s count the lock stack\n", !ret?"can":"cannot");
1899 /* Ensure connection 2 can get a write lock. */
1900 ret = cli_lock(cli2, fnum2, 0, 4, 0, WRITE_LOCK);
1901 EXPECTED(ret, True);
1903 printf("a different processs %s get a write lock on the unlocked stack\n", ret?"can":"cannot");
1906 fail:
1907 cli_close(cli1, fnum1);
1908 cli_close(cli2, fnum2);
1909 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1910 if (!torture_close_connection(cli1)) {
1911 correct = False;
1913 if (!torture_close_connection(cli2)) {
1914 correct = False;
1917 printf("finished locktest5\n");
1919 return correct;
1923 tries the unusual lockingX locktype bits
1925 static bool run_locktest6(int dummy)
1927 static struct cli_state *cli;
1928 const char *fname[1] = { "\\lock6.txt" };
1929 int i;
1930 uint16_t fnum;
1931 NTSTATUS status;
1933 if (!torture_open_connection(&cli, 0)) {
1934 return False;
1937 cli_sockopt(cli, sockops);
1939 printf("starting locktest6\n");
1941 for (i=0;i<1;i++) {
1942 printf("Testing %s\n", fname[i]);
1944 cli_unlink(cli, fname[i], aSYSTEM | aHIDDEN);
1946 cli_open(cli, fname[i], O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
1947 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CHANGE_LOCKTYPE);
1948 cli_close(cli, fnum);
1949 printf("CHANGE_LOCKTYPE gave %s\n", nt_errstr(status));
1951 cli_open(cli, fname[i], O_RDWR, DENY_NONE, &fnum);
1952 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CANCEL_LOCK);
1953 cli_close(cli, fnum);
1954 printf("CANCEL_LOCK gave %s\n", nt_errstr(status));
1956 cli_unlink(cli, fname[i], aSYSTEM | aHIDDEN);
1959 torture_close_connection(cli);
1961 printf("finished locktest6\n");
1962 return True;
1965 static bool run_locktest7(int dummy)
1967 struct cli_state *cli1;
1968 const char *fname = "\\lockt7.lck";
1969 uint16_t fnum1;
1970 char buf[200];
1971 bool correct = False;
1973 if (!torture_open_connection(&cli1, 0)) {
1974 return False;
1977 cli_sockopt(cli1, sockops);
1979 printf("starting locktest7\n");
1981 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1983 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1985 memset(buf, 0, sizeof(buf));
1987 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1988 printf("Failed to create file\n");
1989 goto fail;
1992 cli_setpid(cli1, 1);
1994 if (!cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK)) {
1995 printf("Unable to apply read lock on range 130:4, error was %s\n", cli_errstr(cli1));
1996 goto fail;
1997 } else {
1998 printf("pid1 successfully locked range 130:4 for READ\n");
2001 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2002 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2003 goto fail;
2004 } else {
2005 printf("pid1 successfully read the range 130:4\n");
2008 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2009 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2010 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2011 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2012 goto fail;
2014 } else {
2015 printf("pid1 successfully wrote to the range 130:4 (should be denied)\n");
2016 goto fail;
2019 cli_setpid(cli1, 2);
2021 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2022 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2023 } else {
2024 printf("pid2 successfully read the range 130:4\n");
2027 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2028 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2029 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2030 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2031 goto fail;
2033 } else {
2034 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2035 goto fail;
2038 cli_setpid(cli1, 1);
2039 cli_unlock(cli1, fnum1, 130, 4);
2041 if (!cli_lock(cli1, fnum1, 130, 4, 0, WRITE_LOCK)) {
2042 printf("Unable to apply write lock on range 130:4, error was %s\n", cli_errstr(cli1));
2043 goto fail;
2044 } else {
2045 printf("pid1 successfully locked range 130:4 for WRITE\n");
2048 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2049 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2050 goto fail;
2051 } else {
2052 printf("pid1 successfully read the range 130:4\n");
2055 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2056 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2057 goto fail;
2058 } else {
2059 printf("pid1 successfully wrote to the range 130:4\n");
2062 cli_setpid(cli1, 2);
2064 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2065 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2066 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2067 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2068 goto fail;
2070 } else {
2071 printf("pid2 successfully read the range 130:4 (should be denied)\n");
2072 goto fail;
2075 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2076 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2077 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2078 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2079 goto fail;
2081 } else {
2082 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2083 goto fail;
2086 cli_unlock(cli1, fnum1, 130, 0);
2087 correct = True;
2089 fail:
2090 cli_close(cli1, fnum1);
2091 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2092 torture_close_connection(cli1);
2094 printf("finished locktest7\n");
2095 return correct;
2099 * This demonstrates a problem with our use of GPFS share modes: A file
2100 * descriptor sitting in the pending close queue holding a GPFS share mode
2101 * blocks opening a file another time. Happens with Word 2007 temp files.
2102 * With "posix locking = yes" and "gpfs:sharemodes = yes" enabled, the third
2103 * open is denied with NT_STATUS_SHARING_VIOLATION.
2106 static bool run_locktest8(int dummy)
2108 struct cli_state *cli1;
2109 const char *fname = "\\lockt8.lck";
2110 uint16_t fnum1, fnum2;
2111 char buf[200];
2112 bool correct = False;
2113 NTSTATUS status;
2115 if (!torture_open_connection(&cli1, 0)) {
2116 return False;
2119 cli_sockopt(cli1, sockops);
2121 printf("starting locktest8\n");
2123 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2125 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_WRITE,
2126 &fnum1);
2127 if (!NT_STATUS_IS_OK(status)) {
2128 d_fprintf(stderr, "cli_open returned %s\n", cli_errstr(cli1));
2129 return false;
2132 memset(buf, 0, sizeof(buf));
2134 status = cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum2);
2135 if (!NT_STATUS_IS_OK(status)) {
2136 d_fprintf(stderr, "cli_open second time returned %s\n",
2137 cli_errstr(cli1));
2138 goto fail;
2141 if (!cli_lock(cli1, fnum2, 1, 1, 0, READ_LOCK)) {
2142 printf("Unable to apply read lock on range 1:1, error was "
2143 "%s\n", cli_errstr(cli1));
2144 goto fail;
2147 status = cli_close(cli1, fnum1);
2148 if (!NT_STATUS_IS_OK(status)) {
2149 d_fprintf(stderr, "cli_close(fnum1) %s\n", cli_errstr(cli1));
2150 goto fail;
2153 status = cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
2154 if (!NT_STATUS_IS_OK(status)) {
2155 d_fprintf(stderr, "cli_open third time returned %s\n",
2156 cli_errstr(cli1));
2157 goto fail;
2160 correct = true;
2162 fail:
2163 cli_close(cli1, fnum1);
2164 cli_close(cli1, fnum2);
2165 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2166 torture_close_connection(cli1);
2168 printf("finished locktest8\n");
2169 return correct;
2173 * This test is designed to be run in conjunction with
2174 * external NFS or POSIX locks taken in the filesystem.
2175 * It checks that the smbd server will block until the
2176 * lock is released and then acquire it. JRA.
2179 static bool got_alarm;
2180 static int alarm_fd;
2182 static void alarm_handler(int dummy)
2184 got_alarm = True;
2187 static void alarm_handler_parent(int dummy)
2189 close(alarm_fd);
2192 static void do_local_lock(int read_fd, int write_fd)
2194 int fd;
2195 char c = '\0';
2196 struct flock lock;
2197 const char *local_pathname = NULL;
2198 int ret;
2200 local_pathname = talloc_asprintf(talloc_tos(),
2201 "%s/lockt9.lck", local_path);
2202 if (!local_pathname) {
2203 printf("child: alloc fail\n");
2204 exit(1);
2207 unlink(local_pathname);
2208 fd = open(local_pathname, O_RDWR|O_CREAT, 0666);
2209 if (fd == -1) {
2210 printf("child: open of %s failed %s.\n",
2211 local_pathname, strerror(errno));
2212 exit(1);
2215 /* Now take a fcntl lock. */
2216 lock.l_type = F_WRLCK;
2217 lock.l_whence = SEEK_SET;
2218 lock.l_start = 0;
2219 lock.l_len = 4;
2220 lock.l_pid = getpid();
2222 ret = fcntl(fd,F_SETLK,&lock);
2223 if (ret == -1) {
2224 printf("child: failed to get lock 0:4 on file %s. Error %s\n",
2225 local_pathname, strerror(errno));
2226 exit(1);
2227 } else {
2228 printf("child: got lock 0:4 on file %s.\n",
2229 local_pathname );
2230 fflush(stdout);
2233 CatchSignal(SIGALRM, alarm_handler);
2234 alarm(5);
2235 /* Signal the parent. */
2236 if (write(write_fd, &c, 1) != 1) {
2237 printf("child: start signal fail %s.\n",
2238 strerror(errno));
2239 exit(1);
2241 alarm(0);
2243 alarm(10);
2244 /* Wait for the parent to be ready. */
2245 if (read(read_fd, &c, 1) != 1) {
2246 printf("child: reply signal fail %s.\n",
2247 strerror(errno));
2248 exit(1);
2250 alarm(0);
2252 sleep(5);
2253 close(fd);
2254 printf("child: released lock 0:4 on file %s.\n",
2255 local_pathname );
2256 fflush(stdout);
2257 exit(0);
2260 static bool run_locktest9(int dummy)
2262 struct cli_state *cli1;
2263 const char *fname = "\\lockt9.lck";
2264 uint16_t fnum;
2265 bool correct = False;
2266 int pipe_in[2], pipe_out[2];
2267 pid_t child_pid;
2268 char c = '\0';
2269 int ret;
2270 struct timeval start;
2271 double seconds;
2272 NTSTATUS status;
2274 printf("starting locktest9\n");
2276 if (local_path == NULL) {
2277 d_fprintf(stderr, "locktest9 must be given a local path via -l <localpath>\n");
2278 return false;
2281 if (pipe(pipe_in) == -1 || pipe(pipe_out) == -1) {
2282 return false;
2285 child_pid = fork();
2286 if (child_pid == -1) {
2287 return false;
2290 if (child_pid == 0) {
2291 /* Child. */
2292 do_local_lock(pipe_out[0], pipe_in[1]);
2293 exit(0);
2296 close(pipe_out[0]);
2297 close(pipe_in[1]);
2298 pipe_out[0] = -1;
2299 pipe_in[1] = -1;
2301 /* Parent. */
2302 ret = read(pipe_in[0], &c, 1);
2303 if (ret != 1) {
2304 d_fprintf(stderr, "failed to read start signal from child. %s\n",
2305 strerror(errno));
2306 return false;
2309 if (!torture_open_connection(&cli1, 0)) {
2310 return false;
2313 cli_sockopt(cli1, sockops);
2315 status = cli_open(cli1, fname, O_RDWR, DENY_NONE,
2316 &fnum);
2317 if (!NT_STATUS_IS_OK(status)) {
2318 d_fprintf(stderr, "cli_open returned %s\n", cli_errstr(cli1));
2319 return false;
2322 /* Ensure the child has the lock. */
2323 if (cli_lock(cli1, fnum, 0, 4, 0, WRITE_LOCK)) {
2324 d_fprintf(stderr, "Got the lock on range 0:4 - this should not happen !\n");
2325 goto fail;
2326 } else {
2327 d_printf("Child has the lock.\n");
2330 /* Tell the child to wait 5 seconds then exit. */
2331 ret = write(pipe_out[1], &c, 1);
2332 if (ret != 1) {
2333 d_fprintf(stderr, "failed to send exit signal to child. %s\n",
2334 strerror(errno));
2335 goto fail;
2338 /* Wait 20 seconds for the lock. */
2339 alarm_fd = cli1->fd;
2340 CatchSignal(SIGALRM, alarm_handler_parent);
2341 alarm(20);
2343 start = timeval_current();
2345 if (!cli_lock(cli1, fnum, 0, 4, -1, WRITE_LOCK)) {
2346 d_fprintf(stderr, "Unable to apply write lock on range 0:4, error was "
2347 "%s\n", cli_errstr(cli1));
2348 goto fail_nofd;
2350 alarm(0);
2352 seconds = timeval_elapsed(&start);
2354 printf("Parent got the lock after %.2f seconds.\n",
2355 seconds);
2357 status = cli_close(cli1, fnum);
2358 if (!NT_STATUS_IS_OK(status)) {
2359 d_fprintf(stderr, "cli_close(fnum1) %s\n", cli_errstr(cli1));
2360 goto fail;
2363 correct = true;
2365 fail:
2366 cli_close(cli1, fnum);
2367 torture_close_connection(cli1);
2369 fail_nofd:
2371 printf("finished locktest9\n");
2372 return correct;
2376 test whether fnums and tids open on one VC are available on another (a major
2377 security hole)
2379 static bool run_fdpasstest(int dummy)
2381 struct cli_state *cli1, *cli2;
2382 const char *fname = "\\fdpass.tst";
2383 uint16_t fnum1;
2384 char buf[1024];
2386 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
2387 return False;
2389 cli_sockopt(cli1, sockops);
2390 cli_sockopt(cli2, sockops);
2392 printf("starting fdpasstest\n");
2394 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2396 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
2397 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2398 return False;
2401 if (cli_write(cli1, fnum1, 0, "hello world\n", 0, 13) != 13) {
2402 printf("write failed (%s)\n", cli_errstr(cli1));
2403 return False;
2406 cli2->vuid = cli1->vuid;
2407 cli2->cnum = cli1->cnum;
2408 cli2->pid = cli1->pid;
2410 if (cli_read(cli2, fnum1, buf, 0, 13) == 13) {
2411 printf("read succeeded! nasty security hole [%s]\n",
2412 buf);
2413 return False;
2416 cli_close(cli1, fnum1);
2417 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2419 torture_close_connection(cli1);
2420 torture_close_connection(cli2);
2422 printf("finished fdpasstest\n");
2423 return True;
2426 static bool run_fdsesstest(int dummy)
2428 struct cli_state *cli;
2429 uint16 new_vuid;
2430 uint16 saved_vuid;
2431 uint16 new_cnum;
2432 uint16 saved_cnum;
2433 const char *fname = "\\fdsess.tst";
2434 const char *fname1 = "\\fdsess1.tst";
2435 uint16_t fnum1;
2436 uint16_t fnum2;
2437 char buf[1024];
2438 bool ret = True;
2440 if (!torture_open_connection(&cli, 0))
2441 return False;
2442 cli_sockopt(cli, sockops);
2444 if (!torture_cli_session_setup2(cli, &new_vuid))
2445 return False;
2447 saved_cnum = cli->cnum;
2448 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, share, "?????", "", 1)))
2449 return False;
2450 new_cnum = cli->cnum;
2451 cli->cnum = saved_cnum;
2453 printf("starting fdsesstest\n");
2455 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2456 cli_unlink(cli, fname1, aSYSTEM | aHIDDEN);
2458 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
2459 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2460 return False;
2463 if (cli_write(cli, fnum1, 0, "hello world\n", 0, 13) != 13) {
2464 printf("write failed (%s)\n", cli_errstr(cli));
2465 return False;
2468 saved_vuid = cli->vuid;
2469 cli->vuid = new_vuid;
2471 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2472 printf("read succeeded with different vuid! nasty security hole [%s]\n",
2473 buf);
2474 ret = False;
2476 /* Try to open a file with different vuid, samba cnum. */
2477 if (NT_STATUS_IS_OK(cli_open(cli, fname1, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum2))) {
2478 printf("create with different vuid, same cnum succeeded.\n");
2479 cli_close(cli, fnum2);
2480 cli_unlink(cli, fname1, aSYSTEM | aHIDDEN);
2481 } else {
2482 printf("create with different vuid, same cnum failed.\n");
2483 printf("This will cause problems with service clients.\n");
2484 ret = False;
2487 cli->vuid = saved_vuid;
2489 /* Try with same vuid, different cnum. */
2490 cli->cnum = new_cnum;
2492 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2493 printf("read succeeded with different cnum![%s]\n",
2494 buf);
2495 ret = False;
2498 cli->cnum = saved_cnum;
2499 cli_close(cli, fnum1);
2500 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2502 torture_close_connection(cli);
2504 printf("finished fdsesstest\n");
2505 return ret;
2509 This test checks that
2511 1) the server does not allow an unlink on a file that is open
2513 static bool run_unlinktest(int dummy)
2515 struct cli_state *cli;
2516 const char *fname = "\\unlink.tst";
2517 uint16_t fnum;
2518 bool correct = True;
2520 if (!torture_open_connection(&cli, 0)) {
2521 return False;
2524 cli_sockopt(cli, sockops);
2526 printf("starting unlink test\n");
2528 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2530 cli_setpid(cli, 1);
2532 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
2533 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2534 return False;
2537 if (NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
2538 printf("error: server allowed unlink on an open file\n");
2539 correct = False;
2540 } else {
2541 correct = check_error(__LINE__, cli, ERRDOS, ERRbadshare,
2542 NT_STATUS_SHARING_VIOLATION);
2545 cli_close(cli, fnum);
2546 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2548 if (!torture_close_connection(cli)) {
2549 correct = False;
2552 printf("unlink test finished\n");
2554 return correct;
2559 test how many open files this server supports on the one socket
2561 static bool run_maxfidtest(int dummy)
2563 struct cli_state *cli;
2564 const char *ftemplate = "\\maxfid.%d.%d";
2565 fstring fname;
2566 uint16_t fnums[0x11000];
2567 int i;
2568 int retries=4;
2569 bool correct = True;
2571 cli = current_cli;
2573 if (retries <= 0) {
2574 printf("failed to connect\n");
2575 return False;
2578 cli_sockopt(cli, sockops);
2580 for (i=0; i<0x11000; i++) {
2581 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2582 if (!NT_STATUS_IS_OK(cli_open(cli, fname,
2583 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnums[i]))) {
2584 printf("open of %s failed (%s)\n",
2585 fname, cli_errstr(cli));
2586 printf("maximum fnum is %d\n", i);
2587 break;
2589 printf("%6d\r", i);
2591 printf("%6d\n", i);
2592 i--;
2594 printf("cleaning up\n");
2595 for (;i>=0;i--) {
2596 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2597 cli_close(cli, fnums[i]);
2598 if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
2599 printf("unlink of %s failed (%s)\n",
2600 fname, cli_errstr(cli));
2601 correct = False;
2603 printf("%6d\r", i);
2605 printf("%6d\n", 0);
2607 printf("maxfid test finished\n");
2608 if (!torture_close_connection(cli)) {
2609 correct = False;
2611 return correct;
2614 /* generate a random buffer */
2615 static void rand_buf(char *buf, int len)
2617 while (len--) {
2618 *buf = (char)sys_random();
2619 buf++;
2623 /* send smb negprot commands, not reading the response */
2624 static bool run_negprot_nowait(int dummy)
2626 int i;
2627 static struct cli_state *cli;
2628 bool correct = True;
2630 printf("starting negprot nowait test\n");
2632 if (!(cli = open_nbt_connection())) {
2633 return False;
2636 for (i=0;i<50000;i++) {
2637 cli_negprot_sendsync(cli);
2640 if (!torture_close_connection(cli)) {
2641 correct = False;
2644 printf("finished negprot nowait test\n");
2646 return correct;
2650 /* send random IPC commands */
2651 static bool run_randomipc(int dummy)
2653 char *rparam = NULL;
2654 char *rdata = NULL;
2655 unsigned int rdrcnt,rprcnt;
2656 char param[1024];
2657 int api, param_len, i;
2658 struct cli_state *cli;
2659 bool correct = True;
2660 int count = 50000;
2662 printf("starting random ipc test\n");
2664 if (!torture_open_connection(&cli, 0)) {
2665 return False;
2668 for (i=0;i<count;i++) {
2669 api = sys_random() % 500;
2670 param_len = (sys_random() % 64);
2672 rand_buf(param, param_len);
2674 SSVAL(param,0,api);
2676 cli_api(cli,
2677 param, param_len, 8,
2678 NULL, 0, BUFFER_SIZE,
2679 &rparam, &rprcnt,
2680 &rdata, &rdrcnt);
2681 if (i % 100 == 0) {
2682 printf("%d/%d\r", i,count);
2685 printf("%d/%d\n", i, count);
2687 if (!torture_close_connection(cli)) {
2688 correct = False;
2691 printf("finished random ipc test\n");
2693 return correct;
2698 static void browse_callback(const char *sname, uint32 stype,
2699 const char *comment, void *state)
2701 printf("\t%20.20s %08x %s\n", sname, stype, comment);
2707 This test checks the browse list code
2710 static bool run_browsetest(int dummy)
2712 static struct cli_state *cli;
2713 bool correct = True;
2715 printf("starting browse test\n");
2717 if (!torture_open_connection(&cli, 0)) {
2718 return False;
2721 printf("domain list:\n");
2722 cli_NetServerEnum(cli, cli->server_domain,
2723 SV_TYPE_DOMAIN_ENUM,
2724 browse_callback, NULL);
2726 printf("machine list:\n");
2727 cli_NetServerEnum(cli, cli->server_domain,
2728 SV_TYPE_ALL,
2729 browse_callback, NULL);
2731 if (!torture_close_connection(cli)) {
2732 correct = False;
2735 printf("browse test finished\n");
2737 return correct;
2743 This checks how the getatr calls works
2745 static bool run_attrtest(int dummy)
2747 struct cli_state *cli;
2748 uint16_t fnum;
2749 time_t t, t2;
2750 const char *fname = "\\attrib123456789.tst";
2751 bool correct = True;
2753 printf("starting attrib test\n");
2755 if (!torture_open_connection(&cli, 0)) {
2756 return False;
2759 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2760 cli_open(cli, fname,
2761 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
2762 cli_close(cli, fnum);
2763 if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
2764 printf("getatr failed (%s)\n", cli_errstr(cli));
2765 correct = False;
2768 if (abs(t - time(NULL)) > 60*60*24*10) {
2769 printf("ERROR: SMBgetatr bug. time is %s",
2770 ctime(&t));
2771 t = time(NULL);
2772 correct = True;
2775 t2 = t-60*60*24; /* 1 day ago */
2777 if (!NT_STATUS_IS_OK(cli_setatr(cli, fname, 0, t2))) {
2778 printf("setatr failed (%s)\n", cli_errstr(cli));
2779 correct = True;
2782 if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
2783 printf("getatr failed (%s)\n", cli_errstr(cli));
2784 correct = True;
2787 if (t != t2) {
2788 printf("ERROR: getatr/setatr bug. times are\n%s",
2789 ctime(&t));
2790 printf("%s", ctime(&t2));
2791 correct = True;
2794 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2796 if (!torture_close_connection(cli)) {
2797 correct = False;
2800 printf("attrib test finished\n");
2802 return correct;
2807 This checks a couple of trans2 calls
2809 static bool run_trans2test(int dummy)
2811 struct cli_state *cli;
2812 uint16_t fnum;
2813 SMB_OFF_T size;
2814 time_t c_time, a_time, m_time;
2815 struct timespec c_time_ts, a_time_ts, m_time_ts, w_time_ts, m_time2_ts;
2816 const char *fname = "\\trans2.tst";
2817 const char *dname = "\\trans2";
2818 const char *fname2 = "\\trans2\\trans2.tst";
2819 char pname[1024];
2820 bool correct = True;
2821 NTSTATUS status;
2822 uint32_t fs_attr;
2824 printf("starting trans2 test\n");
2826 if (!torture_open_connection(&cli, 0)) {
2827 return False;
2830 status = cli_get_fs_attr_info(cli, &fs_attr);
2831 if (!NT_STATUS_IS_OK(status)) {
2832 printf("ERROR: cli_get_fs_attr_info returned %s\n",
2833 nt_errstr(status));
2834 correct = false;
2837 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2838 cli_open(cli, fname,
2839 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
2840 if (!cli_qfileinfo(cli, fnum, NULL, &size, &c_time_ts, &a_time_ts, &w_time_ts,
2841 &m_time_ts, NULL)) {
2842 printf("ERROR: qfileinfo failed (%s)\n", cli_errstr(cli));
2843 correct = False;
2846 if (!cli_qfilename(cli, fnum, pname, sizeof(pname))) {
2847 printf("ERROR: qfilename failed (%s)\n", cli_errstr(cli));
2848 correct = False;
2851 if (strcmp(pname, fname)) {
2852 printf("qfilename gave different name? [%s] [%s]\n",
2853 fname, pname);
2854 correct = False;
2857 cli_close(cli, fnum);
2859 sleep(2);
2861 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2862 if (!NT_STATUS_IS_OK(cli_open(cli, fname,
2863 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum))) {
2864 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2865 return False;
2867 cli_close(cli, fnum);
2869 if (!cli_qpathinfo(cli, fname, &c_time, &a_time, &m_time, &size, NULL)) {
2870 printf("ERROR: qpathinfo failed (%s)\n", cli_errstr(cli));
2871 correct = False;
2872 } else {
2873 if (c_time != m_time) {
2874 printf("create time=%s", ctime(&c_time));
2875 printf("modify time=%s", ctime(&m_time));
2876 printf("This system appears to have sticky create times\n");
2878 if (a_time % (60*60) == 0) {
2879 printf("access time=%s", ctime(&a_time));
2880 printf("This system appears to set a midnight access time\n");
2881 correct = False;
2884 if (abs(m_time - time(NULL)) > 60*60*24*7) {
2885 printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
2886 correct = False;
2891 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2892 cli_open(cli, fname,
2893 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
2894 cli_close(cli, fnum);
2895 if (!cli_qpathinfo2(cli, fname, &c_time_ts, &a_time_ts, &w_time_ts,
2896 &m_time_ts, &size, NULL, NULL)) {
2897 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2898 correct = False;
2899 } else {
2900 if (w_time_ts.tv_sec < 60*60*24*2) {
2901 printf("write time=%s", ctime(&w_time_ts.tv_sec));
2902 printf("This system appears to set a initial 0 write time\n");
2903 correct = False;
2907 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2910 /* check if the server updates the directory modification time
2911 when creating a new file */
2912 if (!NT_STATUS_IS_OK(cli_mkdir(cli, dname))) {
2913 printf("ERROR: mkdir failed (%s)\n", cli_errstr(cli));
2914 correct = False;
2916 sleep(3);
2917 if (!cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts, &w_time_ts,
2918 &m_time_ts, &size, NULL, NULL)) {
2919 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2920 correct = False;
2923 cli_open(cli, fname2,
2924 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
2925 cli_write(cli, fnum, 0, (char *)&fnum, 0, sizeof(fnum));
2926 cli_close(cli, fnum);
2927 if (!cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts, &w_time_ts,
2928 &m_time2_ts, &size, NULL, NULL)) {
2929 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2930 correct = False;
2931 } else {
2932 if (memcmp(&m_time_ts, &m_time2_ts, sizeof(struct timespec))
2933 == 0) {
2934 printf("This system does not update directory modification times\n");
2935 correct = False;
2938 cli_unlink(cli, fname2, aSYSTEM | aHIDDEN);
2939 cli_rmdir(cli, dname);
2941 if (!torture_close_connection(cli)) {
2942 correct = False;
2945 printf("trans2 test finished\n");
2947 return correct;
2951 This checks new W2K calls.
2954 static bool new_trans(struct cli_state *pcli, int fnum, int level)
2956 char *buf = NULL;
2957 uint32 len;
2958 bool correct = True;
2960 if (!cli_qfileinfo_test(pcli, fnum, level, &buf, &len)) {
2961 printf("ERROR: qfileinfo (%d) failed (%s)\n", level, cli_errstr(pcli));
2962 correct = False;
2963 } else {
2964 printf("qfileinfo: level %d, len = %u\n", level, len);
2965 dump_data(0, (uint8 *)buf, len);
2966 printf("\n");
2968 SAFE_FREE(buf);
2969 return correct;
2972 static bool run_w2ktest(int dummy)
2974 struct cli_state *cli;
2975 uint16_t fnum;
2976 const char *fname = "\\w2ktest\\w2k.tst";
2977 int level;
2978 bool correct = True;
2980 printf("starting w2k test\n");
2982 if (!torture_open_connection(&cli, 0)) {
2983 return False;
2986 cli_open(cli, fname,
2987 O_RDWR | O_CREAT , DENY_NONE, &fnum);
2989 for (level = 1004; level < 1040; level++) {
2990 new_trans(cli, fnum, level);
2993 cli_close(cli, fnum);
2995 if (!torture_close_connection(cli)) {
2996 correct = False;
2999 printf("w2k test finished\n");
3001 return correct;
3006 this is a harness for some oplock tests
3008 static bool run_oplock1(int dummy)
3010 struct cli_state *cli1;
3011 const char *fname = "\\lockt1.lck";
3012 uint16_t fnum1;
3013 bool correct = True;
3015 printf("starting oplock test 1\n");
3017 if (!torture_open_connection(&cli1, 0)) {
3018 return False;
3021 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3023 cli_sockopt(cli1, sockops);
3025 cli1->use_oplocks = True;
3027 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
3028 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3029 return False;
3032 cli1->use_oplocks = False;
3034 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3035 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3037 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3038 printf("close2 failed (%s)\n", cli_errstr(cli1));
3039 return False;
3042 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
3043 printf("unlink failed (%s)\n", cli_errstr(cli1));
3044 return False;
3047 if (!torture_close_connection(cli1)) {
3048 correct = False;
3051 printf("finished oplock test 1\n");
3053 return correct;
3056 static bool run_oplock2(int dummy)
3058 struct cli_state *cli1, *cli2;
3059 const char *fname = "\\lockt2.lck";
3060 uint16_t fnum1, fnum2;
3061 int saved_use_oplocks = use_oplocks;
3062 char buf[4];
3063 bool correct = True;
3064 volatile bool *shared_correct;
3066 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
3067 *shared_correct = True;
3069 use_level_II_oplocks = True;
3070 use_oplocks = True;
3072 printf("starting oplock test 2\n");
3074 if (!torture_open_connection(&cli1, 0)) {
3075 use_level_II_oplocks = False;
3076 use_oplocks = saved_use_oplocks;
3077 return False;
3080 cli1->use_oplocks = True;
3081 cli1->use_level_II_oplocks = True;
3083 if (!torture_open_connection(&cli2, 1)) {
3084 use_level_II_oplocks = False;
3085 use_oplocks = saved_use_oplocks;
3086 return False;
3089 cli2->use_oplocks = True;
3090 cli2->use_level_II_oplocks = True;
3092 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3094 cli_sockopt(cli1, sockops);
3095 cli_sockopt(cli2, sockops);
3097 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
3098 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3099 return False;
3102 /* Don't need the globals any more. */
3103 use_level_II_oplocks = False;
3104 use_oplocks = saved_use_oplocks;
3106 if (fork() == 0) {
3107 /* Child code */
3108 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2))) {
3109 printf("second open of %s failed (%s)\n", fname, cli_errstr(cli1));
3110 *shared_correct = False;
3111 exit(0);
3114 sleep(2);
3116 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
3117 printf("close2 failed (%s)\n", cli_errstr(cli1));
3118 *shared_correct = False;
3121 exit(0);
3124 sleep(2);
3126 /* Ensure cli1 processes the break. Empty file should always return 0
3127 * bytes. */
3129 if (cli_read(cli1, fnum1, buf, 0, 4) != 0) {
3130 printf("read on fnum1 failed (%s)\n", cli_errstr(cli1));
3131 correct = False;
3134 /* Should now be at level II. */
3135 /* Test if sending a write locks causes a break to none. */
3137 if (!cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK)) {
3138 printf("lock failed (%s)\n", cli_errstr(cli1));
3139 correct = False;
3142 cli_unlock(cli1, fnum1, 0, 4);
3144 sleep(2);
3146 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
3147 printf("lock failed (%s)\n", cli_errstr(cli1));
3148 correct = False;
3151 cli_unlock(cli1, fnum1, 0, 4);
3153 sleep(2);
3155 cli_read(cli1, fnum1, buf, 0, 4);
3157 #if 0
3158 if (cli_write(cli1, fnum1, 0, buf, 0, 4) != 4) {
3159 printf("write on fnum1 failed (%s)\n", cli_errstr(cli1));
3160 correct = False;
3162 #endif
3164 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3165 printf("close1 failed (%s)\n", cli_errstr(cli1));
3166 correct = False;
3169 sleep(4);
3171 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
3172 printf("unlink failed (%s)\n", cli_errstr(cli1));
3173 correct = False;
3176 if (!torture_close_connection(cli1)) {
3177 correct = False;
3180 if (!*shared_correct) {
3181 correct = False;
3184 printf("finished oplock test 2\n");
3186 return correct;
3189 /* handler for oplock 3 tests */
3190 static NTSTATUS oplock3_handler(struct cli_state *cli, uint16_t fnum, unsigned char level)
3192 printf("got oplock break fnum=%d level=%d\n",
3193 fnum, level);
3194 return cli_oplock_ack(cli, fnum, level);
3197 static bool run_oplock3(int dummy)
3199 struct cli_state *cli;
3200 const char *fname = "\\oplockt3.dat";
3201 uint16_t fnum;
3202 char buf[4] = "abcd";
3203 bool correct = True;
3204 volatile bool *shared_correct;
3206 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
3207 *shared_correct = True;
3209 printf("starting oplock test 3\n");
3211 if (fork() == 0) {
3212 /* Child code */
3213 use_oplocks = True;
3214 use_level_II_oplocks = True;
3215 if (!torture_open_connection(&cli, 0)) {
3216 *shared_correct = False;
3217 exit(0);
3219 sleep(2);
3220 /* try to trigger a oplock break in parent */
3221 cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum);
3222 cli_write(cli, fnum, 0, buf, 0, 4);
3223 exit(0);
3226 /* parent code */
3227 use_oplocks = True;
3228 use_level_II_oplocks = True;
3229 if (!torture_open_connection(&cli, 1)) { /* other is forked */
3230 return False;
3232 cli_oplock_handler(cli, oplock3_handler);
3233 cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
3234 cli_write(cli, fnum, 0, buf, 0, 4);
3235 cli_close(cli, fnum);
3236 cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum);
3237 cli->timeout = 20000;
3238 cli_receive_smb(cli);
3239 printf("finished oplock test 3\n");
3241 return (correct && *shared_correct);
3243 /* What are we looking for here? What's sucess and what's FAILURE? */
3249 Test delete on close semantics.
3251 static bool run_deletetest(int dummy)
3253 struct cli_state *cli1 = NULL;
3254 struct cli_state *cli2 = NULL;
3255 const char *fname = "\\delete.file";
3256 uint16_t fnum1 = (uint16_t)-1;
3257 uint16_t fnum2 = (uint16_t)-1;
3258 bool correct = True;
3260 printf("starting delete test\n");
3262 if (!torture_open_connection(&cli1, 0)) {
3263 return False;
3266 cli_sockopt(cli1, sockops);
3268 /* Test 1 - this should delete the file on close. */
3270 cli_setatr(cli1, fname, 0, 0);
3271 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3273 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
3274 0, FILE_OVERWRITE_IF,
3275 FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3276 printf("[1] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3277 correct = False;
3278 goto fail;
3281 #if 0 /* JRATEST */
3283 uint32 *accinfo = NULL;
3284 uint32 len;
3285 cli_qfileinfo_test(cli1, fnum1, SMB_FILE_ACCESS_INFORMATION, (char **)&accinfo, &len);
3286 if (accinfo)
3287 printf("access mode = 0x%lx\n", *accinfo);
3288 SAFE_FREE(accinfo);
3290 #endif
3292 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3293 printf("[1] close failed (%s)\n", cli_errstr(cli1));
3294 correct = False;
3295 goto fail;
3298 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
3299 printf("[1] open of %s succeeded (should fail)\n", fname);
3300 correct = False;
3301 goto fail;
3304 printf("first delete on close test succeeded.\n");
3306 /* Test 2 - this should delete the file on close. */
3308 cli_setatr(cli1, fname, 0, 0);
3309 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3311 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
3312 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
3313 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3314 printf("[2] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3315 correct = False;
3316 goto fail;
3319 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3320 printf("[2] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3321 correct = False;
3322 goto fail;
3325 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3326 printf("[2] close failed (%s)\n", cli_errstr(cli1));
3327 correct = False;
3328 goto fail;
3331 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3332 printf("[2] open of %s succeeded should have been deleted on close !\n", fname);
3333 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3334 printf("[2] close failed (%s)\n", cli_errstr(cli1));
3335 correct = False;
3336 goto fail;
3338 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3339 } else
3340 printf("second delete on close test succeeded.\n");
3342 /* Test 3 - ... */
3343 cli_setatr(cli1, fname, 0, 0);
3344 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3346 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
3347 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3348 printf("[3] open - 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3349 correct = False;
3350 goto fail;
3353 /* This should fail with a sharing violation - open for delete is only compatible
3354 with SHARE_DELETE. */
3356 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3357 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0, 0, &fnum2))) {
3358 printf("[3] open - 2 of %s succeeded - should have failed.\n", fname);
3359 correct = False;
3360 goto fail;
3363 /* This should succeed. */
3365 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3366 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0, &fnum2))) {
3367 printf("[3] open - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3368 correct = False;
3369 goto fail;
3372 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3373 printf("[3] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3374 correct = False;
3375 goto fail;
3378 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3379 printf("[3] close 1 failed (%s)\n", cli_errstr(cli1));
3380 correct = False;
3381 goto fail;
3384 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
3385 printf("[3] close 2 failed (%s)\n", cli_errstr(cli1));
3386 correct = False;
3387 goto fail;
3390 /* This should fail - file should no longer be there. */
3392 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3393 printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
3394 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3395 printf("[3] close failed (%s)\n", cli_errstr(cli1));
3397 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3398 correct = False;
3399 goto fail;
3400 } else
3401 printf("third delete on close test succeeded.\n");
3403 /* Test 4 ... */
3404 cli_setatr(cli1, fname, 0, 0);
3405 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3407 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3408 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3409 printf("[4] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3410 correct = False;
3411 goto fail;
3414 /* This should succeed. */
3415 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3416 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0, &fnum2))) {
3417 printf("[4] open - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3418 correct = False;
3419 goto fail;
3422 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
3423 printf("[4] close - 1 failed (%s)\n", cli_errstr(cli1));
3424 correct = False;
3425 goto fail;
3428 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3429 printf("[4] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3430 correct = False;
3431 goto fail;
3434 /* This should fail - no more opens once delete on close set. */
3435 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3436 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3437 FILE_OPEN, 0, 0, &fnum2))) {
3438 printf("[4] open - 3 of %s succeeded ! Should have failed.\n", fname );
3439 correct = False;
3440 goto fail;
3441 } else
3442 printf("fourth delete on close test succeeded.\n");
3444 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3445 printf("[4] close - 2 failed (%s)\n", cli_errstr(cli1));
3446 correct = False;
3447 goto fail;
3450 /* Test 5 ... */
3451 cli_setatr(cli1, fname, 0, 0);
3452 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3454 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum1))) {
3455 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3456 correct = False;
3457 goto fail;
3460 /* This should fail - only allowed on NT opens with DELETE access. */
3462 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3463 printf("[5] setting delete_on_close on OpenX file succeeded - should fail !\n");
3464 correct = False;
3465 goto fail;
3468 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3469 printf("[5] close - 2 failed (%s)\n", cli_errstr(cli1));
3470 correct = False;
3471 goto fail;
3474 printf("fifth delete on close test succeeded.\n");
3476 /* Test 6 ... */
3477 cli_setatr(cli1, fname, 0, 0);
3478 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3480 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
3481 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3482 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3483 printf("[6] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3484 correct = False;
3485 goto fail;
3488 /* This should fail - only allowed on NT opens with DELETE access. */
3490 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3491 printf("[6] setting delete_on_close on file with no delete access succeeded - should fail !\n");
3492 correct = False;
3493 goto fail;
3496 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3497 printf("[6] close - 2 failed (%s)\n", cli_errstr(cli1));
3498 correct = False;
3499 goto fail;
3502 printf("sixth delete on close test succeeded.\n");
3504 /* Test 7 ... */
3505 cli_setatr(cli1, fname, 0, 0);
3506 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3508 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3509 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3510 printf("[7] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3511 correct = False;
3512 goto fail;
3515 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3516 printf("[7] setting delete_on_close on file failed !\n");
3517 correct = False;
3518 goto fail;
3521 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, false))) {
3522 printf("[7] unsetting delete_on_close on file failed !\n");
3523 correct = False;
3524 goto fail;
3527 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3528 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3529 correct = False;
3530 goto fail;
3533 /* This next open should succeed - we reset the flag. */
3535 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3536 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3537 correct = False;
3538 goto fail;
3541 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3542 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3543 correct = False;
3544 goto fail;
3547 printf("seventh delete on close test succeeded.\n");
3549 /* Test 7 ... */
3550 cli_setatr(cli1, fname, 0, 0);
3551 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3553 if (!torture_open_connection(&cli2, 1)) {
3554 printf("[8] failed to open second connection.\n");
3555 correct = False;
3556 goto fail;
3559 cli_sockopt(cli1, sockops);
3561 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3562 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3563 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3564 printf("[8] open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3565 correct = False;
3566 goto fail;
3569 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3570 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3571 FILE_OPEN, 0, 0, &fnum2))) {
3572 printf("[8] open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3573 correct = False;
3574 goto fail;
3577 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3578 printf("[8] setting delete_on_close on file failed !\n");
3579 correct = False;
3580 goto fail;
3583 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3584 printf("[8] close - 1 failed (%s)\n", cli_errstr(cli1));
3585 correct = False;
3586 goto fail;
3589 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
3590 printf("[8] close - 2 failed (%s)\n", cli_errstr(cli2));
3591 correct = False;
3592 goto fail;
3595 /* This should fail.. */
3596 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3597 printf("[8] open of %s succeeded should have been deleted on close !\n", fname);
3598 goto fail;
3599 correct = False;
3600 } else
3601 printf("eighth delete on close test succeeded.\n");
3603 /* This should fail - we need to set DELETE_ACCESS. */
3604 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,FILE_READ_DATA|FILE_WRITE_DATA,
3605 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3606 printf("[9] open of %s succeeded should have failed!\n", fname);
3607 correct = False;
3608 goto fail;
3611 printf("ninth delete on close test succeeded.\n");
3613 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3614 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3615 printf("[10] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3616 correct = False;
3617 goto fail;
3620 /* This should delete the file. */
3621 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3622 printf("[10] close failed (%s)\n", cli_errstr(cli1));
3623 correct = False;
3624 goto fail;
3627 /* This should fail.. */
3628 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3629 printf("[10] open of %s succeeded should have been deleted on close !\n", fname);
3630 goto fail;
3631 correct = False;
3632 } else
3633 printf("tenth delete on close test succeeded.\n");
3635 cli_setatr(cli1, fname, 0, 0);
3636 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3638 /* What error do we get when attempting to open a read-only file with
3639 delete access ? */
3641 /* Create a readonly file. */
3642 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
3643 FILE_ATTRIBUTE_READONLY, FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3644 printf("[11] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3645 correct = False;
3646 goto fail;
3649 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3650 printf("[11] close failed (%s)\n", cli_errstr(cli1));
3651 correct = False;
3652 goto fail;
3655 /* Now try open for delete access. */
3656 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES|DELETE_ACCESS,
3657 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3658 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3659 printf("[11] open of %s succeeded should have been denied with ACCESS_DENIED!\n", fname);
3660 cli_close(cli1, fnum1);
3661 goto fail;
3662 correct = False;
3663 } else {
3664 NTSTATUS nterr = cli_nt_error(cli1);
3665 if (!NT_STATUS_EQUAL(nterr,NT_STATUS_ACCESS_DENIED)) {
3666 printf("[11] open of %s should have been denied with ACCESS_DENIED! Got error %s\n", fname, nt_errstr(nterr));
3667 goto fail;
3668 correct = False;
3669 } else {
3670 printf("eleventh delete on close test succeeded.\n");
3674 printf("finished delete test\n");
3676 fail:
3677 /* FIXME: This will crash if we aborted before cli2 got
3678 * intialized, because these functions don't handle
3679 * uninitialized connections. */
3681 if (fnum1 != (uint16_t)-1) cli_close(cli1, fnum1);
3682 if (fnum2 != (uint16_t)-1) cli_close(cli1, fnum2);
3683 cli_setatr(cli1, fname, 0, 0);
3684 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3686 if (cli1 && !torture_close_connection(cli1)) {
3687 correct = False;
3689 if (cli2 && !torture_close_connection(cli2)) {
3690 correct = False;
3692 return correct;
3697 print out server properties
3699 static bool run_properties(int dummy)
3701 struct cli_state *cli;
3702 bool correct = True;
3704 printf("starting properties test\n");
3706 ZERO_STRUCT(cli);
3708 if (!torture_open_connection(&cli, 0)) {
3709 return False;
3712 cli_sockopt(cli, sockops);
3714 d_printf("Capabilities 0x%08x\n", cli->capabilities);
3716 if (!torture_close_connection(cli)) {
3717 correct = False;
3720 return correct;
3725 /* FIRST_DESIRED_ACCESS 0xf019f */
3726 #define FIRST_DESIRED_ACCESS FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|\
3727 FILE_READ_EA| /* 0xf */ \
3728 FILE_WRITE_EA|FILE_READ_ATTRIBUTES| /* 0x90 */ \
3729 FILE_WRITE_ATTRIBUTES| /* 0x100 */ \
3730 DELETE_ACCESS|READ_CONTROL_ACCESS|\
3731 WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS /* 0xf0000 */
3732 /* SECOND_DESIRED_ACCESS 0xe0080 */
3733 #define SECOND_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
3734 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
3735 WRITE_OWNER_ACCESS /* 0xe0000 */
3737 #if 0
3738 #define THIRD_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
3739 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
3740 FILE_READ_DATA|\
3741 WRITE_OWNER_ACCESS /* */
3742 #endif
3745 Test ntcreate calls made by xcopy
3747 static bool run_xcopy(int dummy)
3749 static struct cli_state *cli1;
3750 const char *fname = "\\test.txt";
3751 bool correct = True;
3752 uint16_t fnum1, fnum2;
3754 printf("starting xcopy test\n");
3756 if (!torture_open_connection(&cli1, 0)) {
3757 return False;
3760 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,
3761 FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
3762 FILE_SHARE_NONE, FILE_OVERWRITE_IF,
3763 0x4044, 0, &fnum1))) {
3764 printf("First open failed - %s\n", cli_errstr(cli1));
3765 return False;
3768 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,
3769 SECOND_DESIRED_ACCESS, 0,
3770 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN,
3771 0x200000, 0, &fnum2))) {
3772 printf("second open failed - %s\n", cli_errstr(cli1));
3773 return False;
3776 if (!torture_close_connection(cli1)) {
3777 correct = False;
3780 return correct;
3784 Test rename on files open with share delete and no share delete.
3786 static bool run_rename(int dummy)
3788 static struct cli_state *cli1;
3789 const char *fname = "\\test.txt";
3790 const char *fname1 = "\\test1.txt";
3791 bool correct = True;
3792 uint16_t fnum1;
3793 uint16_t attr;
3794 NTSTATUS status;
3796 printf("starting rename test\n");
3798 if (!torture_open_connection(&cli1, 0)) {
3799 return False;
3802 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3803 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3804 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3805 FILE_SHARE_READ, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3806 printf("First open failed - %s\n", cli_errstr(cli1));
3807 return False;
3810 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
3811 printf("First rename failed (SHARE_READ) (this is correct) - %s\n", cli_errstr(cli1));
3812 } else {
3813 printf("First rename succeeded (SHARE_READ) - this should have failed !\n");
3814 correct = False;
3817 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3818 printf("close - 1 failed (%s)\n", cli_errstr(cli1));
3819 return False;
3822 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3823 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3824 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3825 #if 0
3826 FILE_SHARE_DELETE|FILE_SHARE_NONE,
3827 #else
3828 FILE_SHARE_DELETE|FILE_SHARE_READ,
3829 #endif
3830 FILE_OVERWRITE_IF, 0, 0, &fnum1);
3831 if (!NT_STATUS_IS_OK(status)) {
3832 printf("Second open failed - %s\n", cli_errstr(cli1));
3833 return False;
3836 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
3837 printf("Second rename failed (SHARE_DELETE | SHARE_READ) - this should have succeeded - %s\n", cli_errstr(cli1));
3838 correct = False;
3839 } else {
3840 printf("Second rename succeeded (SHARE_DELETE | SHARE_READ)\n");
3843 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3844 printf("close - 2 failed (%s)\n", cli_errstr(cli1));
3845 return False;
3848 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3849 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3851 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, READ_CONTROL_ACCESS, FILE_ATTRIBUTE_NORMAL,
3852 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3853 printf("Third open failed - %s\n", cli_errstr(cli1));
3854 return False;
3858 #if 0
3860 uint16_t fnum2;
3862 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
3863 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
3864 printf("Fourth open failed - %s\n", cli_errstr(cli1));
3865 return False;
3867 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum2, true))) {
3868 printf("[8] setting delete_on_close on file failed !\n");
3869 return False;
3872 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
3873 printf("close - 4 failed (%s)\n", cli_errstr(cli1));
3874 return False;
3877 #endif
3879 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
3880 printf("Third rename failed (SHARE_NONE) - this should have succeeded - %s\n", cli_errstr(cli1));
3881 correct = False;
3882 } else {
3883 printf("Third rename succeeded (SHARE_NONE)\n");
3886 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3887 printf("close - 3 failed (%s)\n", cli_errstr(cli1));
3888 return False;
3891 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3892 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3894 /*----*/
3896 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3897 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3898 printf("Fourth open failed - %s\n", cli_errstr(cli1));
3899 return False;
3902 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
3903 printf("Fourth rename failed (SHARE_READ | SHARE_WRITE) (this is correct) - %s\n", cli_errstr(cli1));
3904 } else {
3905 printf("Fourth rename succeeded (SHARE_READ | SHARE_WRITE) - this should have failed !\n");
3906 correct = False;
3909 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3910 printf("close - 4 failed (%s)\n", cli_errstr(cli1));
3911 return False;
3914 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3915 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3917 /*--*/
3919 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3920 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3921 printf("Fifth open failed - %s\n", cli_errstr(cli1));
3922 return False;
3925 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
3926 printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have succeeded - %s ! \n",
3927 cli_errstr(cli1));
3928 correct = False;
3929 } else {
3930 printf("Fifth rename succeeded (SHARE_READ | SHARE_WRITE | SHARE_DELETE) (this is correct) - %s\n", cli_errstr(cli1));
3934 * Now check if the first name still exists ...
3937 /* if (!NT_STATUS_OP(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3938 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
3939 printf("Opening original file after rename of open file fails: %s\n",
3940 cli_errstr(cli1));
3942 else {
3943 printf("Opening original file after rename of open file works ...\n");
3944 (void)cli_close(cli1, fnum2);
3945 } */
3947 /*--*/
3948 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3949 printf("close - 5 failed (%s)\n", cli_errstr(cli1));
3950 return False;
3953 /* Check that the renamed file has FILE_ATTRIBUTE_ARCHIVE. */
3954 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname1, &attr, NULL, NULL))) {
3955 printf("getatr on file %s failed - %s ! \n",
3956 fname1,
3957 cli_errstr(cli1));
3958 correct = False;
3959 } else {
3960 if (attr != FILE_ATTRIBUTE_ARCHIVE) {
3961 printf("Renamed file %s has wrong attr 0x%x "
3962 "(should be 0x%x)\n",
3963 fname1,
3964 attr,
3965 (unsigned int)FILE_ATTRIBUTE_ARCHIVE);
3966 correct = False;
3967 } else {
3968 printf("Renamed file %s has archive bit set\n", fname1);
3972 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3973 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3975 if (!torture_close_connection(cli1)) {
3976 correct = False;
3979 return correct;
3982 static bool run_pipe_number(int dummy)
3984 struct cli_state *cli1;
3985 const char *pipe_name = "\\SPOOLSS";
3986 uint16_t fnum;
3987 int num_pipes = 0;
3989 printf("starting pipenumber test\n");
3990 if (!torture_open_connection(&cli1, 0)) {
3991 return False;
3994 cli_sockopt(cli1, sockops);
3995 while(1) {
3996 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, pipe_name, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
3997 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF, 0, 0, &fnum))) {
3998 printf("Open of pipe %s failed with error (%s)\n", pipe_name, cli_errstr(cli1));
3999 break;
4001 num_pipes++;
4002 printf("\r%6d", num_pipes);
4005 printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
4006 torture_close_connection(cli1);
4007 return True;
4011 Test open mode returns on read-only files.
4013 static bool run_opentest(int dummy)
4015 static struct cli_state *cli1;
4016 static struct cli_state *cli2;
4017 const char *fname = "\\readonly.file";
4018 uint16_t fnum1, fnum2;
4019 char buf[20];
4020 SMB_OFF_T fsize;
4021 bool correct = True;
4022 char *tmp_path;
4024 printf("starting open test\n");
4026 if (!torture_open_connection(&cli1, 0)) {
4027 return False;
4030 cli_setatr(cli1, fname, 0, 0);
4031 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4033 cli_sockopt(cli1, sockops);
4035 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
4036 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4037 return False;
4040 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4041 printf("close2 failed (%s)\n", cli_errstr(cli1));
4042 return False;
4045 if (!NT_STATUS_IS_OK(cli_setatr(cli1, fname, aRONLY, 0))) {
4046 printf("cli_setatr failed (%s)\n", cli_errstr(cli1));
4047 return False;
4050 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1))) {
4051 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4052 return False;
4055 /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
4056 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4058 if (check_error(__LINE__, cli1, ERRDOS, ERRnoaccess,
4059 NT_STATUS_ACCESS_DENIED)) {
4060 printf("correct error code ERRDOS/ERRnoaccess returned\n");
4063 printf("finished open test 1\n");
4065 cli_close(cli1, fnum1);
4067 /* Now try not readonly and ensure ERRbadshare is returned. */
4069 cli_setatr(cli1, fname, 0, 0);
4071 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1))) {
4072 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4073 return False;
4076 /* This will fail - but the error should be ERRshare. */
4077 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4079 if (check_error(__LINE__, cli1, ERRDOS, ERRbadshare,
4080 NT_STATUS_SHARING_VIOLATION)) {
4081 printf("correct error code ERRDOS/ERRbadshare returned\n");
4084 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4085 printf("close2 failed (%s)\n", cli_errstr(cli1));
4086 return False;
4089 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4091 printf("finished open test 2\n");
4093 /* Test truncate open disposition on file opened for read. */
4095 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
4096 printf("(3) open (1) of %s failed (%s)\n", fname, cli_errstr(cli1));
4097 return False;
4100 /* write 20 bytes. */
4102 memset(buf, '\0', 20);
4104 if (cli_write(cli1, fnum1, 0, buf, 0, 20) != 20) {
4105 printf("write failed (%s)\n", cli_errstr(cli1));
4106 correct = False;
4109 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4110 printf("(3) close1 failed (%s)\n", cli_errstr(cli1));
4111 return False;
4114 /* Ensure size == 20. */
4115 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
4116 printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
4117 return False;
4120 if (fsize != 20) {
4121 printf("(3) file size != 20\n");
4122 return False;
4125 /* Now test if we can truncate a file opened for readonly. */
4127 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY|O_TRUNC, DENY_NONE, &fnum1))) {
4128 printf("(3) open (2) of %s failed (%s)\n", fname, cli_errstr(cli1));
4129 return False;
4132 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4133 printf("close2 failed (%s)\n", cli_errstr(cli1));
4134 return False;
4137 /* Ensure size == 0. */
4138 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
4139 printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
4140 return False;
4143 if (fsize != 0) {
4144 printf("(3) file size != 0\n");
4145 return False;
4147 printf("finished open test 3\n");
4149 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4152 printf("testing ctemp\n");
4153 if (!NT_STATUS_IS_OK(cli_ctemp(cli1, talloc_tos(), "\\", &fnum1, &tmp_path))) {
4154 printf("ctemp failed (%s)\n", cli_errstr(cli1));
4155 return False;
4157 printf("ctemp gave path %s\n", tmp_path);
4158 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4159 printf("close of temp failed (%s)\n", cli_errstr(cli1));
4161 if (!NT_STATUS_IS_OK(cli_unlink(cli1, tmp_path, aSYSTEM | aHIDDEN))) {
4162 printf("unlink of temp failed (%s)\n", cli_errstr(cli1));
4165 /* Test the non-io opens... */
4167 if (!torture_open_connection(&cli2, 1)) {
4168 return False;
4171 cli_setatr(cli2, fname, 0, 0);
4172 cli_unlink(cli2, fname, aSYSTEM | aHIDDEN);
4174 cli_sockopt(cli2, sockops);
4176 printf("TEST #1 testing 2 non-io opens (no delete)\n");
4178 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4179 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4180 printf("test 1 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4181 return False;
4184 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4185 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4186 printf("test 1 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4187 return False;
4190 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4191 printf("test 1 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4192 return False;
4194 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4195 printf("test 1 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4196 return False;
4199 printf("non-io open test #1 passed.\n");
4201 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4203 printf("TEST #2 testing 2 non-io opens (first with delete)\n");
4205 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4206 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4207 printf("test 2 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4208 return False;
4211 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4212 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4213 printf("test 2 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4214 return False;
4217 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4218 printf("test 1 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4219 return False;
4221 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4222 printf("test 1 close 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
4223 return False;
4226 printf("non-io open test #2 passed.\n");
4228 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4230 printf("TEST #3 testing 2 non-io opens (second with delete)\n");
4232 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4233 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4234 printf("test 3 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4235 return False;
4238 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4239 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4240 printf("test 3 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4241 return False;
4244 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4245 printf("test 3 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4246 return False;
4248 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4249 printf("test 3 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4250 return False;
4253 printf("non-io open test #3 passed.\n");
4255 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4257 printf("TEST #4 testing 2 non-io opens (both with delete)\n");
4259 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4260 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4261 printf("test 4 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4262 return False;
4265 if (NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4266 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4267 printf("test 4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
4268 return False;
4271 printf("test 3 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
4273 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4274 printf("test 4 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4275 return False;
4278 printf("non-io open test #4 passed.\n");
4280 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4282 printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
4284 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4285 FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4286 printf("test 5 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4287 return False;
4290 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4291 FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4292 printf("test 5 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4293 return False;
4296 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4297 printf("test 5 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4298 return False;
4301 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4302 printf("test 5 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4303 return False;
4306 printf("non-io open test #5 passed.\n");
4308 printf("TEST #6 testing 1 non-io open, one io open\n");
4310 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4312 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
4313 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4314 printf("test 6 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4315 return False;
4318 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4319 FILE_SHARE_READ, FILE_OPEN_IF, 0, 0, &fnum2))) {
4320 printf("test 6 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4321 return False;
4324 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4325 printf("test 6 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4326 return False;
4329 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4330 printf("test 6 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4331 return False;
4334 printf("non-io open test #6 passed.\n");
4336 printf("TEST #7 testing 1 non-io open, one io open with delete\n");
4338 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4340 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
4341 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4342 printf("test 7 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4343 return False;
4346 if (NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4347 FILE_SHARE_READ|FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4348 printf("test 7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
4349 return False;
4352 printf("test 7 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
4354 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4355 printf("test 7 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4356 return False;
4359 printf("non-io open test #7 passed.\n");
4361 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4363 if (!torture_close_connection(cli1)) {
4364 correct = False;
4366 if (!torture_close_connection(cli2)) {
4367 correct = False;
4370 return correct;
4373 NTSTATUS torture_setup_unix_extensions(struct cli_state *cli)
4375 uint16 major, minor;
4376 uint32 caplow, caphigh;
4377 NTSTATUS status;
4379 if (!SERVER_HAS_UNIX_CIFS(cli)) {
4380 printf("Server doesn't support UNIX CIFS extensions.\n");
4381 return NT_STATUS_NOT_SUPPORTED;
4384 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
4385 &caphigh);
4386 if (!NT_STATUS_IS_OK(status)) {
4387 printf("Server didn't return UNIX CIFS extensions: %s\n",
4388 nt_errstr(status));
4389 return status;
4392 status = cli_set_unix_extensions_capabilities(cli, major, minor,
4393 caplow, caphigh);
4394 if (!NT_STATUS_IS_OK(status)) {
4395 printf("Server doesn't support setting UNIX CIFS extensions: "
4396 "%s.\n", nt_errstr(status));
4397 return status;
4400 return NT_STATUS_OK;
4404 Test POSIX open /mkdir calls.
4406 static bool run_simple_posix_open_test(int dummy)
4408 static struct cli_state *cli1;
4409 const char *fname = "posix:file";
4410 const char *hname = "posix:hlink";
4411 const char *sname = "posix:symlink";
4412 const char *dname = "posix:dir";
4413 char buf[10];
4414 char namebuf[11];
4415 uint16_t fnum1 = (uint16_t)-1;
4416 SMB_STRUCT_STAT sbuf;
4417 bool correct = false;
4418 NTSTATUS status;
4420 printf("Starting simple POSIX open test\n");
4422 if (!torture_open_connection(&cli1, 0)) {
4423 return false;
4426 cli_sockopt(cli1, sockops);
4428 status = torture_setup_unix_extensions(cli1);
4429 if (!NT_STATUS_IS_OK(status)) {
4430 return false;
4433 cli_setatr(cli1, fname, 0, 0);
4434 cli_posix_unlink(cli1, fname);
4435 cli_setatr(cli1, dname, 0, 0);
4436 cli_posix_rmdir(cli1, dname);
4437 cli_setatr(cli1, hname, 0, 0);
4438 cli_posix_unlink(cli1, hname);
4439 cli_setatr(cli1, sname, 0, 0);
4440 cli_posix_unlink(cli1, sname);
4442 /* Create a directory. */
4443 if (!NT_STATUS_IS_OK(cli_posix_mkdir(cli1, dname, 0777))) {
4444 printf("POSIX mkdir of %s failed (%s)\n", dname, cli_errstr(cli1));
4445 goto out;
4448 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1))) {
4449 printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
4450 goto out;
4453 /* Test ftruncate - set file size. */
4454 if (!NT_STATUS_IS_OK(cli_ftruncate(cli1, fnum1, 1000))) {
4455 printf("ftruncate failed (%s)\n", cli_errstr(cli1));
4456 goto out;
4459 /* Ensure st_size == 1000 */
4460 if (!NT_STATUS_IS_OK(cli_posix_stat(cli1, fname, &sbuf))) {
4461 printf("stat failed (%s)\n", cli_errstr(cli1));
4462 goto out;
4465 if (sbuf.st_ex_size != 1000) {
4466 printf("ftruncate - stat size (%u) != 1000\n", (unsigned int)sbuf.st_ex_size);
4467 goto out;
4470 /* Test ftruncate - set file size back to zero. */
4471 if (!NT_STATUS_IS_OK(cli_ftruncate(cli1, fnum1, 0))) {
4472 printf("ftruncate failed (%s)\n", cli_errstr(cli1));
4473 goto out;
4476 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4477 printf("close failed (%s)\n", cli_errstr(cli1));
4478 goto out;
4481 /* Now open the file again for read only. */
4482 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1))) {
4483 printf("POSIX open of %s failed (%s)\n", fname, cli_errstr(cli1));
4484 goto out;
4487 /* Now unlink while open. */
4488 if (!NT_STATUS_IS_OK(cli_posix_unlink(cli1, fname))) {
4489 printf("POSIX unlink of %s failed (%s)\n", fname, cli_errstr(cli1));
4490 goto out;
4493 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4494 printf("close(2) failed (%s)\n", cli_errstr(cli1));
4495 goto out;
4498 /* Ensure the file has gone. */
4499 if (NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1))) {
4500 printf("POSIX open of %s succeeded, should have been deleted.\n", fname);
4501 goto out;
4504 /* What happens when we try and POSIX open a directory ? */
4505 if (NT_STATUS_IS_OK(cli_posix_open(cli1, dname, O_RDONLY, 0, &fnum1))) {
4506 printf("POSIX open of directory %s succeeded, should have failed.\n", fname);
4507 goto out;
4508 } else {
4509 if (!check_error(__LINE__, cli1, ERRDOS, EISDIR,
4510 NT_STATUS_FILE_IS_A_DIRECTORY)) {
4511 goto out;
4515 /* Create the file. */
4516 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1))) {
4517 printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
4518 goto out;
4521 /* Write some data into it. */
4522 if (cli_write(cli1, fnum1, 0, "TEST DATA\n", 0, 10) != 10) {
4523 printf("cli_write failed: %s\n", cli_errstr(cli1));
4524 goto out;
4527 cli_close(cli1, fnum1);
4529 /* Now create a hardlink. */
4530 if (!NT_STATUS_IS_OK(cli_posix_hardlink(cli1, fname, hname))) {
4531 printf("POSIX hardlink of %s failed (%s)\n", hname, cli_errstr(cli1));
4532 goto out;
4535 /* Now create a symlink. */
4536 if (!NT_STATUS_IS_OK(cli_posix_symlink(cli1, fname, sname))) {
4537 printf("POSIX symlink of %s failed (%s)\n", sname, cli_errstr(cli1));
4538 goto out;
4541 /* Open the hardlink for read. */
4542 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, hname, O_RDONLY, 0, &fnum1))) {
4543 printf("POSIX open of %s failed (%s)\n", hname, cli_errstr(cli1));
4544 goto out;
4547 if (cli_read(cli1, fnum1, buf, 0, 10) != 10) {
4548 printf("POSIX read of %s failed (%s)\n", hname, cli_errstr(cli1));
4549 goto out;
4552 if (memcmp(buf, "TEST DATA\n", 10)) {
4553 printf("invalid data read from hardlink\n");
4554 goto out;
4557 /* Do a POSIX lock/unlock. */
4558 if (!NT_STATUS_IS_OK(cli_posix_lock(cli1, fnum1, 0, 100, true, READ_LOCK))) {
4559 printf("POSIX lock failed %s\n", cli_errstr(cli1));
4560 goto out;
4563 /* Punch a hole in the locked area. */
4564 if (!NT_STATUS_IS_OK(cli_posix_unlock(cli1, fnum1, 10, 80))) {
4565 printf("POSIX unlock failed %s\n", cli_errstr(cli1));
4566 goto out;
4569 cli_close(cli1, fnum1);
4571 /* Open the symlink for read - this should fail. A POSIX
4572 client should not be doing opens on a symlink. */
4573 if (NT_STATUS_IS_OK(cli_posix_open(cli1, sname, O_RDONLY, 0, &fnum1))) {
4574 printf("POSIX open of %s succeeded (should have failed)\n", sname);
4575 goto out;
4576 } else {
4577 if (!check_error(__LINE__, cli1, ERRDOS, ERRbadpath,
4578 NT_STATUS_OBJECT_PATH_NOT_FOUND)) {
4579 printf("POSIX open of %s should have failed "
4580 "with NT_STATUS_OBJECT_PATH_NOT_FOUND, "
4581 "failed with %s instead.\n",
4582 sname, cli_errstr(cli1));
4583 goto out;
4587 if (!NT_STATUS_IS_OK(cli_posix_readlink(cli1, sname, namebuf, sizeof(namebuf)))) {
4588 printf("POSIX readlink on %s failed (%s)\n", sname, cli_errstr(cli1));
4589 goto out;
4592 if (strcmp(namebuf, fname) != 0) {
4593 printf("POSIX readlink on %s failed to match name %s (read %s)\n",
4594 sname, fname, namebuf);
4595 goto out;
4598 if (!NT_STATUS_IS_OK(cli_posix_rmdir(cli1, dname))) {
4599 printf("POSIX rmdir failed (%s)\n", cli_errstr(cli1));
4600 goto out;
4603 printf("Simple POSIX open test passed\n");
4604 correct = true;
4606 out:
4608 if (fnum1 != (uint16_t)-1) {
4609 cli_close(cli1, fnum1);
4610 fnum1 = (uint16_t)-1;
4613 cli_setatr(cli1, sname, 0, 0);
4614 cli_posix_unlink(cli1, sname);
4615 cli_setatr(cli1, hname, 0, 0);
4616 cli_posix_unlink(cli1, hname);
4617 cli_setatr(cli1, fname, 0, 0);
4618 cli_posix_unlink(cli1, fname);
4619 cli_setatr(cli1, dname, 0, 0);
4620 cli_posix_rmdir(cli1, dname);
4622 if (!torture_close_connection(cli1)) {
4623 correct = false;
4626 return correct;
4630 static uint32 open_attrs_table[] = {
4631 FILE_ATTRIBUTE_NORMAL,
4632 FILE_ATTRIBUTE_ARCHIVE,
4633 FILE_ATTRIBUTE_READONLY,
4634 FILE_ATTRIBUTE_HIDDEN,
4635 FILE_ATTRIBUTE_SYSTEM,
4637 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY,
4638 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN,
4639 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,
4640 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
4641 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
4642 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
4644 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
4645 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
4646 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
4647 FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_SYSTEM,
4650 struct trunc_open_results {
4651 unsigned int num;
4652 uint32 init_attr;
4653 uint32 trunc_attr;
4654 uint32 result_attr;
4657 static struct trunc_open_results attr_results[] = {
4658 { 0, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
4659 { 1, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
4660 { 2, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
4661 { 16, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
4662 { 17, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
4663 { 18, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
4664 { 51, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4665 { 54, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4666 { 56, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
4667 { 68, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4668 { 71, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4669 { 73, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
4670 { 99, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4671 { 102, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4672 { 104, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
4673 { 116, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4674 { 119, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4675 { 121, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
4676 { 170, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN },
4677 { 173, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM },
4678 { 227, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4679 { 230, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4680 { 232, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
4681 { 244, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4682 { 247, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4683 { 249, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM }
4686 static bool run_openattrtest(int dummy)
4688 static struct cli_state *cli1;
4689 const char *fname = "\\openattr.file";
4690 uint16_t fnum1;
4691 bool correct = True;
4692 uint16 attr;
4693 unsigned int i, j, k, l;
4695 printf("starting open attr test\n");
4697 if (!torture_open_connection(&cli1, 0)) {
4698 return False;
4701 cli_sockopt(cli1, sockops);
4703 for (k = 0, i = 0; i < sizeof(open_attrs_table)/sizeof(uint32); i++) {
4704 cli_setatr(cli1, fname, 0, 0);
4705 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4706 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA, open_attrs_table[i],
4707 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4708 printf("open %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
4709 return False;
4712 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4713 printf("close %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
4714 return False;
4717 for (j = 0; j < sizeof(open_attrs_table)/sizeof(uint32); j++) {
4718 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA, open_attrs_table[j],
4719 FILE_SHARE_NONE, FILE_OVERWRITE, 0, 0, &fnum1))) {
4720 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
4721 if (attr_results[l].num == k) {
4722 printf("[%d] trunc open 0x%x -> 0x%x of %s failed - should have succeeded !(0x%x:%s)\n",
4723 k, open_attrs_table[i],
4724 open_attrs_table[j],
4725 fname, NT_STATUS_V(cli_nt_error(cli1)), cli_errstr(cli1));
4726 correct = False;
4729 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
4730 printf("[%d] trunc open 0x%x -> 0x%x failed with wrong error code %s\n",
4731 k, open_attrs_table[i], open_attrs_table[j],
4732 cli_errstr(cli1));
4733 correct = False;
4735 #if 0
4736 printf("[%d] trunc open 0x%x -> 0x%x failed\n", k, open_attrs_table[i], open_attrs_table[j]);
4737 #endif
4738 k++;
4739 continue;
4742 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4743 printf("close %d (2) of %s failed (%s)\n", j, fname, cli_errstr(cli1));
4744 return False;
4747 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, &attr, NULL, NULL))) {
4748 printf("getatr(2) failed (%s)\n", cli_errstr(cli1));
4749 return False;
4752 #if 0
4753 printf("[%d] getatr check [0x%x] trunc [0x%x] got attr 0x%x\n",
4754 k, open_attrs_table[i], open_attrs_table[j], attr );
4755 #endif
4757 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
4758 if (attr_results[l].num == k) {
4759 if (attr != attr_results[l].result_attr ||
4760 open_attrs_table[i] != attr_results[l].init_attr ||
4761 open_attrs_table[j] != attr_results[l].trunc_attr) {
4762 printf("getatr check failed. [0x%x] trunc [0x%x] got attr 0x%x, should be 0x%x\n",
4763 open_attrs_table[i],
4764 open_attrs_table[j],
4765 (unsigned int)attr,
4766 attr_results[l].result_attr);
4767 correct = False;
4769 break;
4772 k++;
4776 cli_setatr(cli1, fname, 0, 0);
4777 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4779 printf("open attr test %s.\n", correct ? "passed" : "failed");
4781 if (!torture_close_connection(cli1)) {
4782 correct = False;
4784 return correct;
4787 static void list_fn(const char *mnt, file_info *finfo, const char *name, void *state)
4793 test directory listing speed
4795 static bool run_dirtest(int dummy)
4797 int i;
4798 static struct cli_state *cli;
4799 uint16_t fnum;
4800 struct timeval core_start;
4801 bool correct = True;
4803 printf("starting directory test\n");
4805 if (!torture_open_connection(&cli, 0)) {
4806 return False;
4809 cli_sockopt(cli, sockops);
4811 srandom(0);
4812 for (i=0;i<torture_numops;i++) {
4813 fstring fname;
4814 slprintf(fname, sizeof(fname), "\\%x", (int)random());
4815 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum))) {
4816 fprintf(stderr,"Failed to open %s\n", fname);
4817 return False;
4819 cli_close(cli, fnum);
4822 core_start = timeval_current();
4824 printf("Matched %d\n", cli_list(cli, "a*.*", 0, list_fn, NULL));
4825 printf("Matched %d\n", cli_list(cli, "b*.*", 0, list_fn, NULL));
4826 printf("Matched %d\n", cli_list(cli, "xyzabc", 0, list_fn, NULL));
4828 printf("dirtest core %g seconds\n", timeval_elapsed(&core_start));
4830 srandom(0);
4831 for (i=0;i<torture_numops;i++) {
4832 fstring fname;
4833 slprintf(fname, sizeof(fname), "\\%x", (int)random());
4834 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
4837 if (!torture_close_connection(cli)) {
4838 correct = False;
4841 printf("finished dirtest\n");
4843 return correct;
4846 static void del_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
4848 struct cli_state *pcli = (struct cli_state *)state;
4849 fstring fname;
4850 slprintf(fname, sizeof(fname), "\\LISTDIR\\%s", finfo->name);
4852 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
4853 return;
4855 if (finfo->mode & aDIR) {
4856 if (!NT_STATUS_IS_OK(cli_rmdir(pcli, fname)))
4857 printf("del_fn: failed to rmdir %s\n,", fname );
4858 } else {
4859 if (!NT_STATUS_IS_OK(cli_unlink(pcli, fname, aSYSTEM | aHIDDEN)))
4860 printf("del_fn: failed to unlink %s\n,", fname );
4866 sees what IOCTLs are supported
4868 bool torture_ioctl_test(int dummy)
4870 static struct cli_state *cli;
4871 uint16_t device, function;
4872 uint16_t fnum;
4873 const char *fname = "\\ioctl.dat";
4874 DATA_BLOB blob;
4875 NTSTATUS status;
4877 if (!torture_open_connection(&cli, 0)) {
4878 return False;
4881 printf("starting ioctl test\n");
4883 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
4885 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
4886 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
4887 return False;
4890 status = cli_raw_ioctl(cli, fnum, 0x2d0000 | (0x0420<<2), &blob);
4891 printf("ioctl device info: %s\n", nt_errstr(status));
4893 status = cli_raw_ioctl(cli, fnum, IOCTL_QUERY_JOB_INFO, &blob);
4894 printf("ioctl job info: %s\n", nt_errstr(status));
4896 for (device=0;device<0x100;device++) {
4897 printf("testing device=0x%x\n", device);
4898 for (function=0;function<0x100;function++) {
4899 uint32 code = (device<<16) | function;
4901 status = cli_raw_ioctl(cli, fnum, code, &blob);
4903 if (NT_STATUS_IS_OK(status)) {
4904 printf("ioctl 0x%x OK : %d bytes\n", (int)code,
4905 (int)blob.length);
4906 data_blob_free(&blob);
4911 if (!torture_close_connection(cli)) {
4912 return False;
4915 return True;
4920 tries varients of chkpath
4922 bool torture_chkpath_test(int dummy)
4924 static struct cli_state *cli;
4925 uint16_t fnum;
4926 bool ret;
4928 if (!torture_open_connection(&cli, 0)) {
4929 return False;
4932 printf("starting chkpath test\n");
4934 /* cleanup from an old run */
4935 cli_rmdir(cli, "\\chkpath.dir\\dir2");
4936 cli_unlink(cli, "\\chkpath.dir\\*", aSYSTEM | aHIDDEN);
4937 cli_rmdir(cli, "\\chkpath.dir");
4939 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\chkpath.dir"))) {
4940 printf("mkdir1 failed : %s\n", cli_errstr(cli));
4941 return False;
4944 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\chkpath.dir\\dir2"))) {
4945 printf("mkdir2 failed : %s\n", cli_errstr(cli));
4946 return False;
4949 if (!NT_STATUS_IS_OK(cli_open(cli, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
4950 printf("open1 failed (%s)\n", cli_errstr(cli));
4951 return False;
4953 cli_close(cli, fnum);
4955 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir"))) {
4956 printf("chkpath1 failed: %s\n", cli_errstr(cli));
4957 ret = False;
4960 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dir2"))) {
4961 printf("chkpath2 failed: %s\n", cli_errstr(cli));
4962 ret = False;
4965 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\foo.txt"))) {
4966 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
4967 NT_STATUS_NOT_A_DIRECTORY);
4968 } else {
4969 printf("* chkpath on a file should fail\n");
4970 ret = False;
4973 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\bar.txt"))) {
4974 ret = check_error(__LINE__, cli, ERRDOS, ERRbadfile,
4975 NT_STATUS_OBJECT_NAME_NOT_FOUND);
4976 } else {
4977 printf("* chkpath on a non existant file should fail\n");
4978 ret = False;
4981 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dirxx\\bar.txt"))) {
4982 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
4983 NT_STATUS_OBJECT_PATH_NOT_FOUND);
4984 } else {
4985 printf("* chkpath on a non existent component should fail\n");
4986 ret = False;
4989 cli_rmdir(cli, "\\chkpath.dir\\dir2");
4990 cli_unlink(cli, "\\chkpath.dir\\*", aSYSTEM | aHIDDEN);
4991 cli_rmdir(cli, "\\chkpath.dir");
4993 if (!torture_close_connection(cli)) {
4994 return False;
4997 return ret;
5000 static bool run_eatest(int dummy)
5002 static struct cli_state *cli;
5003 const char *fname = "\\eatest.txt";
5004 bool correct = True;
5005 uint16_t fnum;
5006 int i;
5007 size_t num_eas;
5008 struct ea_struct *ea_list = NULL;
5009 TALLOC_CTX *mem_ctx = talloc_init("eatest");
5011 printf("starting eatest\n");
5013 if (!torture_open_connection(&cli, 0)) {
5014 talloc_destroy(mem_ctx);
5015 return False;
5018 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
5019 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0,
5020 FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
5021 FILE_SHARE_NONE, FILE_OVERWRITE_IF,
5022 0x4044, 0, &fnum))) {
5023 printf("open failed - %s\n", cli_errstr(cli));
5024 talloc_destroy(mem_ctx);
5025 return False;
5028 for (i = 0; i < 10; i++) {
5029 fstring ea_name, ea_val;
5031 slprintf(ea_name, sizeof(ea_name), "EA_%d", i);
5032 memset(ea_val, (char)i+1, i+1);
5033 if (!cli_set_ea_fnum(cli, fnum, ea_name, ea_val, i+1)) {
5034 printf("ea_set of name %s failed - %s\n", ea_name, cli_errstr(cli));
5035 talloc_destroy(mem_ctx);
5036 return False;
5040 cli_close(cli, fnum);
5041 for (i = 0; i < 10; i++) {
5042 fstring ea_name, ea_val;
5044 slprintf(ea_name, sizeof(ea_name), "EA_%d", i+10);
5045 memset(ea_val, (char)i+1, i+1);
5046 if (!cli_set_ea_path(cli, fname, ea_name, ea_val, i+1)) {
5047 printf("ea_set of name %s failed - %s\n", ea_name, cli_errstr(cli));
5048 talloc_destroy(mem_ctx);
5049 return False;
5053 if (!cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list)) {
5054 printf("ea_get list failed - %s\n", cli_errstr(cli));
5055 correct = False;
5058 printf("num_eas = %d\n", (int)num_eas);
5060 if (num_eas != 20) {
5061 printf("Should be 20 EA's stored... failing.\n");
5062 correct = False;
5065 for (i = 0; i < num_eas; i++) {
5066 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
5067 dump_data(0, ea_list[i].value.data,
5068 ea_list[i].value.length);
5071 /* Setting EA's to zero length deletes them. Test this */
5072 printf("Now deleting all EA's - case indepenent....\n");
5074 #if 1
5075 cli_set_ea_path(cli, fname, "", "", 0);
5076 #else
5077 for (i = 0; i < 20; i++) {
5078 fstring ea_name;
5079 slprintf(ea_name, sizeof(ea_name), "ea_%d", i);
5080 if (!cli_set_ea_path(cli, fname, ea_name, "", 0)) {
5081 printf("ea_set of name %s failed - %s\n", ea_name, cli_errstr(cli));
5082 talloc_destroy(mem_ctx);
5083 return False;
5086 #endif
5088 if (!cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list)) {
5089 printf("ea_get list failed - %s\n", cli_errstr(cli));
5090 correct = False;
5093 printf("num_eas = %d\n", (int)num_eas);
5094 for (i = 0; i < num_eas; i++) {
5095 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
5096 dump_data(0, ea_list[i].value.data,
5097 ea_list[i].value.length);
5100 if (num_eas != 0) {
5101 printf("deleting EA's failed.\n");
5102 correct = False;
5105 /* Try and delete a non existant EA. */
5106 if (!cli_set_ea_path(cli, fname, "foo", "", 0)) {
5107 printf("deleting non-existant EA 'foo' should succeed. %s\n", cli_errstr(cli));
5108 correct = False;
5111 talloc_destroy(mem_ctx);
5112 if (!torture_close_connection(cli)) {
5113 correct = False;
5116 return correct;
5119 static bool run_dirtest1(int dummy)
5121 int i;
5122 static struct cli_state *cli;
5123 uint16_t fnum;
5124 int num_seen;
5125 bool correct = True;
5127 printf("starting directory test\n");
5129 if (!torture_open_connection(&cli, 0)) {
5130 return False;
5133 cli_sockopt(cli, sockops);
5135 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
5136 cli_list(cli, "\\LISTDIR\\*", aDIR, del_fn, cli);
5137 cli_rmdir(cli, "\\LISTDIR");
5138 cli_mkdir(cli, "\\LISTDIR");
5140 /* Create 1000 files and 1000 directories. */
5141 for (i=0;i<1000;i++) {
5142 fstring fname;
5143 slprintf(fname, sizeof(fname), "\\LISTDIR\\f%d", i);
5144 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
5145 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
5146 fprintf(stderr,"Failed to open %s\n", fname);
5147 return False;
5149 cli_close(cli, fnum);
5151 for (i=0;i<1000;i++) {
5152 fstring fname;
5153 slprintf(fname, sizeof(fname), "\\LISTDIR\\d%d", i);
5154 if (!NT_STATUS_IS_OK(cli_mkdir(cli, fname))) {
5155 fprintf(stderr,"Failed to open %s\n", fname);
5156 return False;
5160 /* Now ensure that doing an old list sees both files and directories. */
5161 num_seen = cli_list_old(cli, "\\LISTDIR\\*", aDIR, list_fn, NULL);
5162 printf("num_seen = %d\n", num_seen );
5163 /* We should see 100 files + 1000 directories + . and .. */
5164 if (num_seen != 2002)
5165 correct = False;
5167 /* Ensure if we have the "must have" bits we only see the
5168 * relevent entries.
5170 num_seen = cli_list_old(cli, "\\LISTDIR\\*", (aDIR<<8)|aDIR, list_fn, NULL);
5171 printf("num_seen = %d\n", num_seen );
5172 if (num_seen != 1002)
5173 correct = False;
5175 num_seen = cli_list_old(cli, "\\LISTDIR\\*", (aARCH<<8)|aDIR, list_fn, NULL);
5176 printf("num_seen = %d\n", num_seen );
5177 if (num_seen != 1000)
5178 correct = False;
5180 /* Delete everything. */
5181 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
5182 cli_list(cli, "\\LISTDIR\\*", aDIR, del_fn, cli);
5183 cli_rmdir(cli, "\\LISTDIR");
5185 #if 0
5186 printf("Matched %d\n", cli_list(cli, "a*.*", 0, list_fn, NULL));
5187 printf("Matched %d\n", cli_list(cli, "b*.*", 0, list_fn, NULL));
5188 printf("Matched %d\n", cli_list(cli, "xyzabc", 0, list_fn, NULL));
5189 #endif
5191 if (!torture_close_connection(cli)) {
5192 correct = False;
5195 printf("finished dirtest1\n");
5197 return correct;
5200 static bool run_error_map_extract(int dummy) {
5202 static struct cli_state *c_dos;
5203 static struct cli_state *c_nt;
5204 NTSTATUS status;
5206 uint32 error;
5208 uint32 flgs2, errnum;
5209 uint8 errclass;
5211 NTSTATUS nt_status;
5213 fstring user;
5215 /* NT-Error connection */
5217 if (!(c_nt = open_nbt_connection())) {
5218 return False;
5221 c_nt->use_spnego = False;
5223 status = cli_negprot(c_nt);
5225 if (!NT_STATUS_IS_OK(status)) {
5226 printf("%s rejected the NT-error negprot (%s)\n", host,
5227 nt_errstr(status));
5228 cli_shutdown(c_nt);
5229 return False;
5232 if (!NT_STATUS_IS_OK(cli_session_setup(c_nt, "", "", 0, "", 0,
5233 workgroup))) {
5234 printf("%s rejected the NT-error initial session setup (%s)\n",host, cli_errstr(c_nt));
5235 return False;
5238 /* DOS-Error connection */
5240 if (!(c_dos = open_nbt_connection())) {
5241 return False;
5244 c_dos->use_spnego = False;
5245 c_dos->force_dos_errors = True;
5247 status = cli_negprot(c_dos);
5248 if (!NT_STATUS_IS_OK(status)) {
5249 printf("%s rejected the DOS-error negprot (%s)\n", host,
5250 nt_errstr(status));
5251 cli_shutdown(c_dos);
5252 return False;
5255 if (!NT_STATUS_IS_OK(cli_session_setup(c_dos, "", "", 0, "", 0,
5256 workgroup))) {
5257 printf("%s rejected the DOS-error initial session setup (%s)\n",host, cli_errstr(c_dos));
5258 return False;
5261 for (error=(0xc0000000 | 0x1); error < (0xc0000000| 0xFFF); error++) {
5262 fstr_sprintf(user, "%X", error);
5264 if (NT_STATUS_IS_OK(cli_session_setup(c_nt, user,
5265 password, strlen(password),
5266 password, strlen(password),
5267 workgroup))) {
5268 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
5271 flgs2 = SVAL(c_nt->inbuf,smb_flg2);
5273 /* Case #1: 32-bit NT errors */
5274 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
5275 nt_status = NT_STATUS(IVAL(c_nt->inbuf,smb_rcls));
5276 } else {
5277 printf("/** Dos error on NT connection! (%s) */\n",
5278 cli_errstr(c_nt));
5279 nt_status = NT_STATUS(0xc0000000);
5282 if (NT_STATUS_IS_OK(cli_session_setup(c_dos, user,
5283 password, strlen(password),
5284 password, strlen(password),
5285 workgroup))) {
5286 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
5288 flgs2 = SVAL(c_dos->inbuf,smb_flg2), errnum;
5290 /* Case #1: 32-bit NT errors */
5291 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
5292 printf("/** NT error on DOS connection! (%s) */\n",
5293 cli_errstr(c_nt));
5294 errnum = errclass = 0;
5295 } else {
5296 cli_dos_error(c_dos, &errclass, &errnum);
5299 if (NT_STATUS_V(nt_status) != error) {
5300 printf("/*\t{ This NT error code was 'sqashed'\n\t from %s to %s \n\t during the session setup }\n*/\n",
5301 get_nt_error_c_code(NT_STATUS(error)),
5302 get_nt_error_c_code(nt_status));
5305 printf("\t{%s,\t%s,\t%s},\n",
5306 smb_dos_err_class(errclass),
5307 smb_dos_err_name(errclass, errnum),
5308 get_nt_error_c_code(NT_STATUS(error)));
5310 return True;
5313 static bool run_sesssetup_bench(int dummy)
5315 static struct cli_state *c;
5316 const char *fname = "\\file.dat";
5317 uint16_t fnum;
5318 NTSTATUS status;
5319 int i;
5321 if (!torture_open_connection(&c, 0)) {
5322 return false;
5325 if (!NT_STATUS_IS_OK(cli_ntcreate(
5326 c, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
5327 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
5328 FILE_DELETE_ON_CLOSE, 0, &fnum))) {
5329 d_printf("open %s failed: %s\n", fname, cli_errstr(c));
5330 return false;
5333 for (i=0; i<torture_numops; i++) {
5334 status = cli_session_setup(
5335 c, username,
5336 password, strlen(password),
5337 password, strlen(password),
5338 workgroup);
5339 if (!NT_STATUS_IS_OK(status)) {
5340 d_printf("(%s) cli_session_setup failed: %s\n",
5341 __location__, nt_errstr(status));
5342 return false;
5345 d_printf("\r%d ", (int)c->vuid);
5347 status = cli_ulogoff(c);
5348 if (!NT_STATUS_IS_OK(status)) {
5349 d_printf("(%s) cli_ulogoff failed: %s\n",
5350 __location__, nt_errstr(status));
5351 return false;
5353 c->vuid = 0;
5356 return true;
5359 static bool subst_test(const char *str, const char *user, const char *domain,
5360 uid_t uid, gid_t gid, const char *expected)
5362 char *subst;
5363 bool result = true;
5365 subst = talloc_sub_specified(talloc_tos(), str, user, domain, uid, gid);
5367 if (strcmp(subst, expected) != 0) {
5368 printf("sub_specified(%s, %s, %s, %d, %d) returned [%s], expected "
5369 "[%s]\n", str, user, domain, (int)uid, (int)gid, subst,
5370 expected);
5371 result = false;
5374 TALLOC_FREE(subst);
5375 return result;
5378 static void chain1_open_completion(struct tevent_req *req)
5380 uint16_t fnum;
5381 NTSTATUS status;
5382 status = cli_open_recv(req, &fnum);
5383 TALLOC_FREE(req);
5385 d_printf("cli_open_recv returned %s: %d\n",
5386 nt_errstr(status),
5387 NT_STATUS_IS_OK(status) ? fnum : -1);
5390 static void chain1_write_completion(struct tevent_req *req)
5392 size_t written;
5393 NTSTATUS status;
5394 status = cli_write_andx_recv(req, &written);
5395 TALLOC_FREE(req);
5397 d_printf("cli_write_andx_recv returned %s: %d\n",
5398 nt_errstr(status),
5399 NT_STATUS_IS_OK(status) ? (int)written : -1);
5402 static void chain1_close_completion(struct tevent_req *req)
5404 NTSTATUS status;
5405 bool *done = (bool *)tevent_req_callback_data_void(req);
5407 status = cli_close_recv(req);
5408 *done = true;
5410 TALLOC_FREE(req);
5412 d_printf("cli_close returned %s\n", nt_errstr(status));
5415 static bool run_chain1(int dummy)
5417 struct cli_state *cli1;
5418 struct event_context *evt = event_context_init(NULL);
5419 struct tevent_req *reqs[3], *smbreqs[3];
5420 bool done = false;
5421 const char *str = "foobar";
5422 NTSTATUS status;
5424 printf("starting chain1 test\n");
5425 if (!torture_open_connection(&cli1, 0)) {
5426 return False;
5429 cli_sockopt(cli1, sockops);
5431 reqs[0] = cli_open_create(talloc_tos(), evt, cli1, "\\test",
5432 O_CREAT|O_RDWR, 0, &smbreqs[0]);
5433 if (reqs[0] == NULL) return false;
5434 tevent_req_set_callback(reqs[0], chain1_open_completion, NULL);
5437 reqs[1] = cli_write_andx_create(talloc_tos(), evt, cli1, 0, 0,
5438 (uint8_t *)str, 0, strlen(str)+1,
5439 smbreqs, 1, &smbreqs[1]);
5440 if (reqs[1] == NULL) return false;
5441 tevent_req_set_callback(reqs[1], chain1_write_completion, NULL);
5443 reqs[2] = cli_close_create(talloc_tos(), evt, cli1, 0, &smbreqs[2]);
5444 if (reqs[2] == NULL) return false;
5445 tevent_req_set_callback(reqs[2], chain1_close_completion, &done);
5447 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
5448 if (!NT_STATUS_IS_OK(status)) {
5449 return false;
5452 while (!done) {
5453 event_loop_once(evt);
5456 torture_close_connection(cli1);
5457 return True;
5460 static void chain2_sesssetup_completion(struct tevent_req *req)
5462 NTSTATUS status;
5463 status = cli_session_setup_guest_recv(req);
5464 d_printf("sesssetup returned %s\n", nt_errstr(status));
5467 static void chain2_tcon_completion(struct tevent_req *req)
5469 bool *done = (bool *)tevent_req_callback_data_void(req);
5470 NTSTATUS status;
5471 status = cli_tcon_andx_recv(req);
5472 d_printf("tcon_and_x returned %s\n", nt_errstr(status));
5473 *done = true;
5476 static bool run_chain2(int dummy)
5478 struct cli_state *cli1;
5479 struct event_context *evt = event_context_init(NULL);
5480 struct tevent_req *reqs[2], *smbreqs[2];
5481 bool done = false;
5482 NTSTATUS status;
5484 printf("starting chain2 test\n");
5485 status = cli_start_connection(&cli1, global_myname(), host, NULL,
5486 port_to_use, Undefined, 0, NULL);
5487 if (!NT_STATUS_IS_OK(status)) {
5488 return False;
5491 cli_sockopt(cli1, sockops);
5493 reqs[0] = cli_session_setup_guest_create(talloc_tos(), evt, cli1,
5494 &smbreqs[0]);
5495 if (reqs[0] == NULL) return false;
5496 tevent_req_set_callback(reqs[0], chain2_sesssetup_completion, NULL);
5498 reqs[1] = cli_tcon_andx_create(talloc_tos(), evt, cli1, "IPC$",
5499 "?????", NULL, 0, &smbreqs[1]);
5500 if (reqs[1] == NULL) return false;
5501 tevent_req_set_callback(reqs[1], chain2_tcon_completion, &done);
5503 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
5504 if (!NT_STATUS_IS_OK(status)) {
5505 return false;
5508 while (!done) {
5509 event_loop_once(evt);
5512 torture_close_connection(cli1);
5513 return True;
5517 struct torture_createdel_state {
5518 struct tevent_context *ev;
5519 struct cli_state *cli;
5522 static void torture_createdel_created(struct tevent_req *subreq);
5523 static void torture_createdel_closed(struct tevent_req *subreq);
5525 static struct tevent_req *torture_createdel_send(TALLOC_CTX *mem_ctx,
5526 struct tevent_context *ev,
5527 struct cli_state *cli,
5528 const char *name)
5530 struct tevent_req *req, *subreq;
5531 struct torture_createdel_state *state;
5533 req = tevent_req_create(mem_ctx, &state,
5534 struct torture_createdel_state);
5535 if (req == NULL) {
5536 return NULL;
5538 state->ev = ev;
5539 state->cli = cli;
5541 subreq = cli_ntcreate_send(
5542 state, ev, cli, name, 0,
5543 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
5544 FILE_ATTRIBUTE_NORMAL,
5545 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
5546 FILE_OPEN_IF, FILE_DELETE_ON_CLOSE, 0);
5548 if (tevent_req_nomem(subreq, req)) {
5549 return tevent_req_post(req, ev);
5551 tevent_req_set_callback(subreq, torture_createdel_created, req);
5552 return req;
5555 static void torture_createdel_created(struct tevent_req *subreq)
5557 struct tevent_req *req = tevent_req_callback_data(
5558 subreq, struct tevent_req);
5559 struct torture_createdel_state *state = tevent_req_data(
5560 req, struct torture_createdel_state);
5561 NTSTATUS status;
5562 uint16_t fnum;
5564 status = cli_ntcreate_recv(subreq, &fnum);
5565 TALLOC_FREE(subreq);
5566 if (!NT_STATUS_IS_OK(status)) {
5567 DEBUG(10, ("cli_ntcreate_recv returned %s\n",
5568 nt_errstr(status)));
5569 tevent_req_nterror(req, status);
5570 return;
5573 subreq = cli_close_send(state, state->ev, state->cli, fnum);
5574 if (tevent_req_nomem(subreq, req)) {
5575 return;
5577 tevent_req_set_callback(subreq, torture_createdel_closed, req);
5580 static void torture_createdel_closed(struct tevent_req *subreq)
5582 struct tevent_req *req = tevent_req_callback_data(
5583 subreq, struct tevent_req);
5584 NTSTATUS status;
5586 status = cli_close_recv(subreq);
5587 if (!NT_STATUS_IS_OK(status)) {
5588 DEBUG(10, ("cli_close_recv returned %s\n", nt_errstr(status)));
5589 tevent_req_nterror(req, status);
5590 return;
5592 tevent_req_done(req);
5595 static NTSTATUS torture_createdel_recv(struct tevent_req *req)
5597 return tevent_req_simple_recv_ntstatus(req);
5600 struct torture_createdels_state {
5601 struct tevent_context *ev;
5602 struct cli_state *cli;
5603 const char *base_name;
5604 int sent;
5605 int received;
5606 int num_files;
5607 struct tevent_req **reqs;
5610 static void torture_createdels_done(struct tevent_req *subreq);
5612 static struct tevent_req *torture_createdels_send(TALLOC_CTX *mem_ctx,
5613 struct tevent_context *ev,
5614 struct cli_state *cli,
5615 const char *base_name,
5616 int num_parallel,
5617 int num_files)
5619 struct tevent_req *req;
5620 struct torture_createdels_state *state;
5621 int i;
5623 req = tevent_req_create(mem_ctx, &state,
5624 struct torture_createdels_state);
5625 if (req == NULL) {
5626 return NULL;
5628 state->ev = ev;
5629 state->cli = cli;
5630 state->base_name = talloc_strdup(state, base_name);
5631 if (tevent_req_nomem(state->base_name, req)) {
5632 return tevent_req_post(req, ev);
5634 state->num_files = MAX(num_parallel, num_files);
5635 state->sent = 0;
5636 state->received = 0;
5638 state->reqs = talloc_array(state, struct tevent_req *, num_parallel);
5639 if (tevent_req_nomem(state->reqs, req)) {
5640 return tevent_req_post(req, ev);
5643 for (i=0; i<num_parallel; i++) {
5644 char *name;
5646 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
5647 state->sent);
5648 if (tevent_req_nomem(name, req)) {
5649 return tevent_req_post(req, ev);
5651 state->reqs[i] = torture_createdel_send(
5652 state->reqs, state->ev, state->cli, name);
5653 if (tevent_req_nomem(state->reqs[i], req)) {
5654 return tevent_req_post(req, ev);
5656 name = talloc_move(state->reqs[i], &name);
5657 tevent_req_set_callback(state->reqs[i],
5658 torture_createdels_done, req);
5659 state->sent += 1;
5661 return req;
5664 static void torture_createdels_done(struct tevent_req *subreq)
5666 struct tevent_req *req = tevent_req_callback_data(
5667 subreq, struct tevent_req);
5668 struct torture_createdels_state *state = tevent_req_data(
5669 req, struct torture_createdels_state);
5670 size_t num_parallel = talloc_array_length(state->reqs);
5671 NTSTATUS status;
5672 char *name;
5673 int i;
5675 status = torture_createdel_recv(subreq);
5676 if (!NT_STATUS_IS_OK(status)){
5677 DEBUG(10, ("torture_createdel_recv returned %s\n",
5678 nt_errstr(status)));
5679 TALLOC_FREE(subreq);
5680 tevent_req_nterror(req, status);
5681 return;
5684 for (i=0; i<num_parallel; i++) {
5685 if (subreq == state->reqs[i]) {
5686 break;
5689 if (i == num_parallel) {
5690 DEBUG(10, ("received something we did not send\n"));
5691 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5692 return;
5694 TALLOC_FREE(state->reqs[i]);
5696 if (state->sent >= state->num_files) {
5697 tevent_req_done(req);
5698 return;
5701 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
5702 state->sent);
5703 if (tevent_req_nomem(name, req)) {
5704 return;
5706 state->reqs[i] = torture_createdel_send(state->reqs, state->ev,
5707 state->cli, name);
5708 if (tevent_req_nomem(state->reqs[i], req)) {
5709 return;
5711 name = talloc_move(state->reqs[i], &name);
5712 tevent_req_set_callback(state->reqs[i], torture_createdels_done, req);
5713 state->sent += 1;
5716 static NTSTATUS torture_createdels_recv(struct tevent_req *req)
5718 return tevent_req_simple_recv_ntstatus(req);
5721 struct swallow_notify_state {
5722 struct tevent_context *ev;
5723 struct cli_state *cli;
5724 uint16_t fnum;
5725 uint32_t completion_filter;
5726 bool recursive;
5727 bool (*fn)(uint32_t action, const char *name, void *priv);
5728 void *priv;
5731 static void swallow_notify_done(struct tevent_req *subreq);
5733 static struct tevent_req *swallow_notify_send(TALLOC_CTX *mem_ctx,
5734 struct tevent_context *ev,
5735 struct cli_state *cli,
5736 uint16_t fnum,
5737 uint32_t completion_filter,
5738 bool recursive,
5739 bool (*fn)(uint32_t action,
5740 const char *name,
5741 void *priv),
5742 void *priv)
5744 struct tevent_req *req, *subreq;
5745 struct swallow_notify_state *state;
5747 req = tevent_req_create(mem_ctx, &state,
5748 struct swallow_notify_state);
5749 if (req == NULL) {
5750 return NULL;
5752 state->ev = ev;
5753 state->cli = cli;
5754 state->fnum = fnum;
5755 state->completion_filter = completion_filter;
5756 state->recursive = recursive;
5757 state->fn = fn;
5758 state->priv = priv;
5760 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
5761 0xffff, state->completion_filter,
5762 state->recursive);
5763 if (tevent_req_nomem(subreq, req)) {
5764 return tevent_req_post(req, ev);
5766 tevent_req_set_callback(subreq, swallow_notify_done, req);
5767 return req;
5770 static void swallow_notify_done(struct tevent_req *subreq)
5772 struct tevent_req *req = tevent_req_callback_data(
5773 subreq, struct tevent_req);
5774 struct swallow_notify_state *state = tevent_req_data(
5775 req, struct swallow_notify_state);
5776 NTSTATUS status;
5777 uint32_t i, num_changes;
5778 struct notify_change *changes;
5780 status = cli_notify_recv(subreq, state, &num_changes, &changes);
5781 TALLOC_FREE(subreq);
5782 if (!NT_STATUS_IS_OK(status)) {
5783 DEBUG(10, ("cli_notify_recv returned %s\n",
5784 nt_errstr(status)));
5785 tevent_req_nterror(req, status);
5786 return;
5789 for (i=0; i<num_changes; i++) {
5790 state->fn(changes[i].action, changes[i].name, state->priv);
5792 TALLOC_FREE(changes);
5794 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
5795 0xffff, state->completion_filter,
5796 state->recursive);
5797 if (tevent_req_nomem(subreq, req)) {
5798 return;
5800 tevent_req_set_callback(subreq, swallow_notify_done, req);
5803 static bool print_notifies(uint32_t action, const char *name, void *priv)
5805 if (DEBUGLEVEL > 5) {
5806 d_printf("%d %s\n", (int)action, name);
5808 return true;
5811 static void notify_bench_done(struct tevent_req *req)
5813 int *num_finished = (int *)tevent_req_callback_data_void(req);
5814 *num_finished += 1;
5817 static bool run_notify_bench(int dummy)
5819 const char *dname = "\\notify-bench";
5820 struct tevent_context *ev;
5821 NTSTATUS status;
5822 uint16_t dnum;
5823 struct tevent_req *req1;
5824 struct tevent_req *req2 = NULL;
5825 int i, num_unc_names;
5826 int num_finished = 0;
5828 printf("starting notify-bench test\n");
5830 if (use_multishare_conn) {
5831 char **unc_list;
5832 unc_list = file_lines_load(multishare_conn_fname,
5833 &num_unc_names, 0, NULL);
5834 if (!unc_list || num_unc_names <= 0) {
5835 d_printf("Failed to load unc names list from '%s'\n",
5836 multishare_conn_fname);
5837 return false;
5839 TALLOC_FREE(unc_list);
5840 } else {
5841 num_unc_names = 1;
5844 ev = tevent_context_init(talloc_tos());
5845 if (ev == NULL) {
5846 d_printf("tevent_context_init failed\n");
5847 return false;
5850 for (i=0; i<num_unc_names; i++) {
5851 struct cli_state *cli;
5852 char *base_fname;
5854 base_fname = talloc_asprintf(talloc_tos(), "%s\\file%3.3d.",
5855 dname, i);
5856 if (base_fname == NULL) {
5857 return false;
5860 if (!torture_open_connection(&cli, i)) {
5861 return false;
5864 status = cli_ntcreate(cli, dname, 0,
5865 MAXIMUM_ALLOWED_ACCESS,
5866 0, FILE_SHARE_READ|FILE_SHARE_WRITE|
5867 FILE_SHARE_DELETE,
5868 FILE_OPEN_IF, FILE_DIRECTORY_FILE, 0,
5869 &dnum);
5871 if (!NT_STATUS_IS_OK(status)) {
5872 d_printf("Could not create %s: %s\n", dname,
5873 nt_errstr(status));
5874 return false;
5877 req1 = swallow_notify_send(talloc_tos(), ev, cli, dnum,
5878 FILE_NOTIFY_CHANGE_FILE_NAME |
5879 FILE_NOTIFY_CHANGE_DIR_NAME |
5880 FILE_NOTIFY_CHANGE_ATTRIBUTES |
5881 FILE_NOTIFY_CHANGE_LAST_WRITE,
5882 false, print_notifies, NULL);
5883 if (req1 == NULL) {
5884 d_printf("Could not create notify request\n");
5885 return false;
5888 req2 = torture_createdels_send(talloc_tos(), ev, cli,
5889 base_fname, 10, torture_numops);
5890 if (req2 == NULL) {
5891 d_printf("Could not create createdels request\n");
5892 return false;
5894 TALLOC_FREE(base_fname);
5896 tevent_req_set_callback(req2, notify_bench_done,
5897 &num_finished);
5900 while (num_finished < num_unc_names) {
5901 int ret;
5902 ret = tevent_loop_once(ev);
5903 if (ret != 0) {
5904 d_printf("tevent_loop_once failed\n");
5905 return false;
5909 if (!tevent_req_poll(req2, ev)) {
5910 d_printf("tevent_req_poll failed\n");
5913 status = torture_createdels_recv(req2);
5914 d_printf("torture_createdels_recv returned %s\n", nt_errstr(status));
5916 return true;
5919 static bool run_mangle1(int dummy)
5921 struct cli_state *cli;
5922 const char *fname = "this_is_a_long_fname_to_be_mangled.txt";
5923 uint16_t fnum;
5924 fstring alt_name;
5925 NTSTATUS status;
5926 time_t change_time, access_time, write_time;
5927 SMB_OFF_T size;
5928 uint16_t mode;
5930 printf("starting mangle1 test\n");
5931 if (!torture_open_connection(&cli, 0)) {
5932 return False;
5935 cli_sockopt(cli, sockops);
5937 if (!NT_STATUS_IS_OK(cli_ntcreate(
5938 cli, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
5939 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
5940 d_printf("open %s failed: %s\n", fname, cli_errstr(cli));
5941 return false;
5943 cli_close(cli, fnum);
5945 status = cli_qpathinfo_alt_name(cli, fname, alt_name);
5946 if (!NT_STATUS_IS_OK(status)) {
5947 d_printf("cli_qpathinfo_alt_name failed: %s\n",
5948 nt_errstr(status));
5949 return false;
5951 d_printf("alt_name: %s\n", alt_name);
5953 if (!NT_STATUS_IS_OK(cli_open(cli, alt_name, O_RDONLY, DENY_NONE, &fnum))) {
5954 d_printf("cli_open(%s) failed: %s\n", alt_name,
5955 cli_errstr(cli));
5956 return false;
5958 cli_close(cli, fnum);
5960 if (!cli_qpathinfo(cli, alt_name, &change_time, &access_time,
5961 &write_time, &size, &mode)) {
5962 d_printf("cli_qpathinfo(%s) failed: %s\n", alt_name,
5963 cli_errstr(cli));
5964 return false;
5967 return true;
5970 static size_t null_source(uint8_t *buf, size_t n, void *priv)
5972 size_t *to_pull = (size_t *)priv;
5973 size_t thistime = *to_pull;
5975 thistime = MIN(thistime, n);
5976 if (thistime == 0) {
5977 return 0;
5980 memset(buf, 0, thistime);
5981 *to_pull -= thistime;
5982 return thistime;
5985 static bool run_windows_write(int dummy)
5987 struct cli_state *cli1;
5988 uint16_t fnum;
5989 int i;
5990 bool ret = false;
5991 const char *fname = "\\writetest.txt";
5992 struct timeval start_time;
5993 double seconds;
5994 double kbytes;
5996 printf("starting windows_write test\n");
5997 if (!torture_open_connection(&cli1, 0)) {
5998 return False;
6001 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
6002 printf("open failed (%s)\n", cli_errstr(cli1));
6003 return False;
6006 cli_sockopt(cli1, sockops);
6008 start_time = timeval_current();
6010 for (i=0; i<torture_numops; i++) {
6011 char c = 0;
6012 off_t start = i * torture_blocksize;
6013 NTSTATUS status;
6014 size_t to_pull = torture_blocksize - 1;
6016 if (cli_write(cli1, fnum, 0, &c,
6017 start + torture_blocksize - 1, 1) != 1) {
6018 printf("cli_write failed: %s\n", cli_errstr(cli1));
6019 goto fail;
6022 status = cli_push(cli1, fnum, 0, i * torture_blocksize, torture_blocksize,
6023 null_source, &to_pull);
6024 if (!NT_STATUS_IS_OK(status)) {
6025 printf("cli_push returned: %s\n", nt_errstr(status));
6026 goto fail;
6030 seconds = timeval_elapsed(&start_time);
6031 kbytes = (double)torture_blocksize * torture_numops;
6032 kbytes /= 1024;
6034 printf("Wrote %d kbytes in %.2f seconds: %d kb/sec\n", (int)kbytes,
6035 (double)seconds, (int)(kbytes/seconds));
6037 ret = true;
6038 fail:
6039 cli_close(cli1, fnum);
6040 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
6041 torture_close_connection(cli1);
6042 return ret;
6045 static bool run_cli_echo(int dummy)
6047 struct cli_state *cli;
6048 NTSTATUS status;
6050 printf("starting cli_echo test\n");
6051 if (!torture_open_connection(&cli, 0)) {
6052 return false;
6054 cli_sockopt(cli, sockops);
6056 status = cli_echo(cli, 5, data_blob_const("hello", 5));
6058 d_printf("cli_echo returned %s\n", nt_errstr(status));
6060 torture_close_connection(cli);
6061 return NT_STATUS_IS_OK(status);
6064 static bool run_uid_regression_test(int dummy)
6066 static struct cli_state *cli;
6067 int16_t old_vuid;
6068 int16_t old_cnum;
6069 bool correct = True;
6070 NTSTATUS status;
6072 printf("starting uid regression test\n");
6074 if (!torture_open_connection(&cli, 0)) {
6075 return False;
6078 cli_sockopt(cli, sockops);
6080 /* Ok - now save then logoff our current user. */
6081 old_vuid = cli->vuid;
6083 status = cli_ulogoff(cli);
6084 if (!NT_STATUS_IS_OK(status)) {
6085 d_printf("(%s) cli_ulogoff failed: %s\n",
6086 __location__, nt_errstr(status));
6087 correct = false;
6088 goto out;
6091 cli->vuid = old_vuid;
6093 /* Try an operation. */
6094 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\uid_reg_test"))) {
6095 /* We expect bad uid. */
6096 if (!check_error(__LINE__, cli, ERRSRV, ERRbaduid,
6097 NT_STATUS_NO_SUCH_USER)) {
6098 return False;
6102 old_cnum = cli->cnum;
6104 /* Now try a SMBtdis with the invald vuid set to zero. */
6105 cli->vuid = 0;
6107 /* This should succeed. */
6108 status = cli_tdis(cli);
6110 if (NT_STATUS_IS_OK(status)) {
6111 printf("First tdis with invalid vuid should succeed.\n");
6112 } else {
6113 printf("First tdis failed (%s)\n", nt_errstr(status));
6116 cli->vuid = old_vuid;
6117 cli->cnum = old_cnum;
6119 /* This should fail. */
6120 status = cli_tdis(cli);
6121 if (NT_STATUS_IS_OK(status)) {
6122 printf("Second tdis with invalid vuid should fail - succeeded instead !.\n");
6123 } else {
6124 /* Should be bad tid. */
6125 if (!check_error(__LINE__, cli, ERRSRV, ERRinvnid,
6126 NT_STATUS_NETWORK_NAME_DELETED)) {
6127 return False;
6131 cli_rmdir(cli, "\\uid_reg_test");
6133 out:
6135 cli_shutdown(cli);
6136 return correct;
6140 static const char *illegal_chars = "*\\/?<>|\":";
6141 static char force_shortname_chars[] = " +,.[];=\177";
6143 static void shortname_del_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
6145 struct cli_state *pcli = (struct cli_state *)state;
6146 fstring fname;
6147 slprintf(fname, sizeof(fname), "\\shortname\\%s", finfo->name);
6149 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
6150 return;
6152 if (finfo->mode & aDIR) {
6153 if (!NT_STATUS_IS_OK(cli_rmdir(pcli, fname)))
6154 printf("del_fn: failed to rmdir %s\n,", fname );
6155 } else {
6156 if (!NT_STATUS_IS_OK(cli_unlink(pcli, fname, aSYSTEM | aHIDDEN)))
6157 printf("del_fn: failed to unlink %s\n,", fname );
6161 struct sn_state {
6162 int i;
6163 bool val;
6166 static void shortname_list_fn(const char *mnt, file_info *finfo, const char *name, void *state)
6168 struct sn_state *s = (struct sn_state *)state;
6169 int i = s->i;
6171 #if 0
6172 printf("shortname list: i = %d, name = |%s|, shortname = |%s|\n",
6173 i, finfo->name, finfo->short_name);
6174 #endif
6176 if (strchr(force_shortname_chars, i)) {
6177 if (!finfo->short_name[0]) {
6178 /* Shortname not created when it should be. */
6179 d_printf("(%s) ERROR: Shortname was not created for file %s containing %d\n",
6180 __location__, finfo->name, i);
6181 s->val = true;
6183 } else if (finfo->short_name[0]){
6184 /* Shortname created when it should not be. */
6185 d_printf("(%s) ERROR: Shortname %s was created for file %s\n",
6186 __location__, finfo->short_name, finfo->name);
6187 s->val = true;
6191 static bool run_shortname_test(int dummy)
6193 static struct cli_state *cli;
6194 bool correct = True;
6195 int i;
6196 struct sn_state s;
6197 char fname[20];
6199 printf("starting shortname test\n");
6201 if (!torture_open_connection(&cli, 0)) {
6202 return False;
6205 cli_sockopt(cli, sockops);
6207 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
6208 cli_list(cli, "\\shortname\\*", aDIR, shortname_del_fn, cli);
6209 cli_rmdir(cli, "\\shortname");
6211 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\shortname"))) {
6212 d_printf("(%s) cli_mkdir of \\shortname failed: %s\n",
6213 __location__, cli_errstr(cli));
6214 correct = false;
6215 goto out;
6218 strlcpy(fname, "\\shortname\\", sizeof(fname));
6219 strlcat(fname, "test .txt", sizeof(fname));
6221 s.val = false;
6223 for (i = 32; i < 128; i++) {
6224 NTSTATUS status;
6225 uint16_t fnum = (uint16_t)-1;
6227 s.i = i;
6229 if (strchr(illegal_chars, i)) {
6230 continue;
6232 fname[15] = i;
6234 status = cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
6235 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum);
6236 if (!NT_STATUS_IS_OK(status)) {
6237 d_printf("(%s) cli_nt_create of %s failed: %s\n",
6238 __location__, fname, cli_errstr(cli));
6239 correct = false;
6240 goto out;
6242 cli_close(cli, fnum);
6243 if (cli_list(cli, "\\shortname\\test*.*", 0, shortname_list_fn, &s) != 1) {
6244 d_printf("(%s) failed to list %s: %s\n",
6245 __location__, fname, cli_errstr(cli));
6246 correct = false;
6247 goto out;
6249 if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
6250 d_printf("(%s) failed to delete %s: %s\n",
6251 __location__, fname, cli_errstr(cli));
6252 correct = false;
6253 goto out;
6256 if (s.val) {
6257 correct = false;
6258 goto out;
6262 out:
6264 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
6265 cli_list(cli, "\\shortname\\*", aDIR, shortname_del_fn, cli);
6266 cli_rmdir(cli, "\\shortname");
6267 torture_close_connection(cli);
6268 return correct;
6271 static void pagedsearch_cb(struct tevent_req *req)
6273 int rc;
6274 struct tldap_message *msg;
6275 char *dn;
6277 rc = tldap_search_paged_recv(req, talloc_tos(), &msg);
6278 if (rc != TLDAP_SUCCESS) {
6279 d_printf("tldap_search_paged_recv failed: %s\n",
6280 tldap_err2string(rc));
6281 return;
6283 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
6284 TALLOC_FREE(msg);
6285 return;
6287 if (!tldap_entry_dn(msg, &dn)) {
6288 d_printf("tldap_entry_dn failed\n");
6289 return;
6291 d_printf("%s\n", dn);
6292 TALLOC_FREE(msg);
6295 static bool run_tldap(int dummy)
6297 struct tldap_context *ld;
6298 int fd, rc;
6299 NTSTATUS status;
6300 struct sockaddr_storage addr;
6301 struct tevent_context *ev;
6302 struct tevent_req *req;
6303 char *basedn;
6304 const char *filter;
6306 if (!resolve_name(host, &addr, 0, false)) {
6307 d_printf("could not find host %s\n", host);
6308 return false;
6310 status = open_socket_out(&addr, 389, 9999, &fd);
6311 if (!NT_STATUS_IS_OK(status)) {
6312 d_printf("open_socket_out failed: %s\n", nt_errstr(status));
6313 return false;
6316 ld = tldap_context_create(talloc_tos(), fd);
6317 if (ld == NULL) {
6318 close(fd);
6319 d_printf("tldap_context_create failed\n");
6320 return false;
6323 rc = tldap_fetch_rootdse(ld);
6324 if (rc != TLDAP_SUCCESS) {
6325 d_printf("tldap_fetch_rootdse failed: %s\n",
6326 tldap_errstr(talloc_tos(), ld, rc));
6327 return false;
6330 basedn = tldap_talloc_single_attribute(
6331 tldap_rootdse(ld), "defaultNamingContext", talloc_tos());
6332 if (basedn == NULL) {
6333 d_printf("no defaultNamingContext\n");
6334 return false;
6336 d_printf("defaultNamingContext: %s\n", basedn);
6338 ev = tevent_context_init(talloc_tos());
6339 if (ev == NULL) {
6340 d_printf("tevent_context_init failed\n");
6341 return false;
6344 req = tldap_search_paged_send(talloc_tos(), ev, ld, basedn,
6345 TLDAP_SCOPE_SUB, "(objectclass=*)",
6346 NULL, 0, 0,
6347 NULL, 0, NULL, 0, 0, 0, 0, 5);
6348 if (req == NULL) {
6349 d_printf("tldap_search_paged_send failed\n");
6350 return false;
6352 tevent_req_set_callback(req, pagedsearch_cb, NULL);
6354 tevent_req_poll(req, ev);
6356 TALLOC_FREE(req);
6358 /* test search filters against rootDSE */
6359 filter = "(&(|(name=samba)(nextRid<=10000000)(usnChanged>=10)(samba~=ambas)(!(name=s*m*a)))"
6360 "(|(name:=samba)(name:dn:2.5.13.5:=samba)(:dn:2.5.13.5:=samba)(!(name=*samba))))";
6362 rc = tldap_search(ld, "", TLDAP_SCOPE_BASE, filter,
6363 NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0,
6364 talloc_tos(), NULL, NULL);
6365 if (rc != TLDAP_SUCCESS) {
6366 d_printf("tldap_search with complex filter failed: %s\n",
6367 tldap_errstr(talloc_tos(), ld, rc));
6368 return false;
6371 TALLOC_FREE(ld);
6372 return true;
6375 /* Torture test to ensure no regression of :
6376 https://bugzilla.samba.org/show_bug.cgi?id=7084
6379 static bool run_dir_createtime(int dummy)
6381 struct cli_state *cli;
6382 const char *dname = "\\testdir";
6383 const char *fname = "\\testdir\\testfile";
6384 NTSTATUS status;
6385 struct timespec create_time;
6386 struct timespec create_time1;
6387 uint16_t fnum;
6388 bool ret = false;
6390 if (!torture_open_connection(&cli, 0)) {
6391 return false;
6394 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
6395 cli_rmdir(cli, dname);
6397 status = cli_mkdir(cli, dname);
6398 if (!NT_STATUS_IS_OK(status)) {
6399 printf("mkdir failed: %s\n", nt_errstr(status));
6400 goto out;
6403 if (!cli_qpathinfo2(cli,
6404 dname,
6405 &create_time,
6406 NULL,
6407 NULL,
6408 NULL,
6409 NULL,
6410 NULL,
6411 NULL)) {
6412 status = cli_nt_error(cli);
6413 printf("cli_qpathinfo2 returned %s\n",
6414 nt_errstr(status));
6415 goto out;
6418 /* Sleep 3 seconds, then create a file. */
6419 sleep(3);
6421 status = cli_open(cli, fname, O_RDWR | O_CREAT | O_EXCL,
6422 DENY_NONE, &fnum);
6423 if (!NT_STATUS_IS_OK(status)) {
6424 printf("cli_open failed: %s\n", nt_errstr(status));
6425 goto out;
6428 if (!cli_qpathinfo2(cli,
6429 dname,
6430 &create_time1,
6431 NULL,
6432 NULL,
6433 NULL,
6434 NULL,
6435 NULL,
6436 NULL)) {
6437 status = cli_nt_error(cli);
6438 printf("cli_qpathinfo2 (2) returned %s\n",
6439 nt_errstr(status));
6440 goto out;
6443 if (timespec_compare(&create_time1, &create_time)) {
6444 printf("run_dir_createtime: create time was updated (error)\n");
6445 } else {
6446 printf("run_dir_createtime: create time was not updated (correct)\n");
6447 ret = true;
6450 out:
6452 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
6453 cli_rmdir(cli, dname);
6454 if (!torture_close_connection(cli)) {
6455 ret = false;
6457 return ret;
6461 static bool run_streamerror(int dummy)
6463 struct cli_state *cli;
6464 const char *dname = "\\testdir";
6465 const char *streamname =
6466 "testdir:{4c8cc155-6c1e-11d1-8e41-00c04fb9386d}:$DATA";
6467 NTSTATUS status;
6468 time_t change_time, access_time, write_time;
6469 SMB_OFF_T size;
6470 uint16_t mode, fnum;
6471 bool ret = true;
6473 if (!torture_open_connection(&cli, 0)) {
6474 return false;
6477 cli_unlink(cli, "\\testdir\\*", aSYSTEM | aHIDDEN);
6478 cli_rmdir(cli, dname);
6480 status = cli_mkdir(cli, dname);
6481 if (!NT_STATUS_IS_OK(status)) {
6482 printf("mkdir failed: %s\n", nt_errstr(status));
6483 return false;
6486 cli_qpathinfo(cli, streamname, &change_time, &access_time, &write_time,
6487 &size, &mode);
6488 status = cli_nt_error(cli);
6490 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
6491 printf("pathinfo returned %s, expected "
6492 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
6493 nt_errstr(status));
6494 ret = false;
6497 status = cli_ntcreate(cli, streamname, 0x16,
6498 FILE_READ_DATA|FILE_READ_EA|
6499 FILE_READ_ATTRIBUTES|READ_CONTROL_ACCESS,
6500 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
6501 FILE_OPEN, 0, 0, &fnum);
6503 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
6504 printf("ntcreate returned %s, expected "
6505 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
6506 nt_errstr(status));
6507 ret = false;
6511 cli_rmdir(cli, dname);
6512 return ret;
6515 static bool run_local_substitute(int dummy)
6517 bool ok = true;
6519 ok &= subst_test("%U", "bla", "", -1, -1, "bla");
6520 ok &= subst_test("%u%U", "bla", "", -1, -1, "blabla");
6521 ok &= subst_test("%g", "", "", -1, -1, "NO_GROUP");
6522 ok &= subst_test("%G", "", "", -1, -1, "NO_GROUP");
6523 ok &= subst_test("%g", "", "", -1, 0, gidtoname(0));
6524 ok &= subst_test("%G", "", "", -1, 0, gidtoname(0));
6525 ok &= subst_test("%D%u", "u", "dom", -1, 0, "domu");
6526 ok &= subst_test("%i %I", "", "", -1, -1, "0.0.0.0 0.0.0.0");
6528 /* Different captialization rules in sub_basic... */
6530 ok &= (strcmp(talloc_sub_basic(talloc_tos(), "BLA", "dom", "%U%D"),
6531 "blaDOM") == 0);
6533 return ok;
6536 static bool run_local_base64(int dummy)
6538 int i;
6539 bool ret = true;
6541 for (i=1; i<2000; i++) {
6542 DATA_BLOB blob1, blob2;
6543 char *b64;
6545 blob1.data = talloc_array(talloc_tos(), uint8_t, i);
6546 blob1.length = i;
6547 generate_random_buffer(blob1.data, blob1.length);
6549 b64 = base64_encode_data_blob(talloc_tos(), blob1);
6550 if (b64 == NULL) {
6551 d_fprintf(stderr, "base64_encode_data_blob failed "
6552 "for %d bytes\n", i);
6553 ret = false;
6555 blob2 = base64_decode_data_blob(b64);
6556 TALLOC_FREE(b64);
6558 if (data_blob_cmp(&blob1, &blob2)) {
6559 d_fprintf(stderr, "data_blob_cmp failed for %d "
6560 "bytes\n", i);
6561 ret = false;
6563 TALLOC_FREE(blob1.data);
6564 data_blob_free(&blob2);
6566 return ret;
6569 static bool run_local_gencache(int dummy)
6571 char *val;
6572 time_t tm;
6573 DATA_BLOB blob;
6575 if (!gencache_set("foo", "bar", time(NULL) + 1000)) {
6576 d_printf("%s: gencache_set() failed\n", __location__);
6577 return False;
6580 if (!gencache_get("foo", NULL, NULL)) {
6581 d_printf("%s: gencache_get() failed\n", __location__);
6582 return False;
6585 if (!gencache_get("foo", &val, &tm)) {
6586 d_printf("%s: gencache_get() failed\n", __location__);
6587 return False;
6590 if (strcmp(val, "bar") != 0) {
6591 d_printf("%s: gencache_get() returned %s, expected %s\n",
6592 __location__, val, "bar");
6593 SAFE_FREE(val);
6594 return False;
6597 SAFE_FREE(val);
6599 if (!gencache_del("foo")) {
6600 d_printf("%s: gencache_del() failed\n", __location__);
6601 return False;
6603 if (gencache_del("foo")) {
6604 d_printf("%s: second gencache_del() succeeded\n",
6605 __location__);
6606 return False;
6609 if (gencache_get("foo", &val, &tm)) {
6610 d_printf("%s: gencache_get() on deleted entry "
6611 "succeeded\n", __location__);
6612 return False;
6615 blob = data_blob_string_const_null("bar");
6616 tm = time(NULL) + 60;
6618 if (!gencache_set_data_blob("foo", &blob, tm)) {
6619 d_printf("%s: gencache_set_data_blob() failed\n", __location__);
6620 return False;
6623 if (!gencache_get_data_blob("foo", &blob, NULL, NULL)) {
6624 d_printf("%s: gencache_get_data_blob() failed\n", __location__);
6625 return False;
6628 if (strcmp((const char *)blob.data, "bar") != 0) {
6629 d_printf("%s: gencache_get_data_blob() returned %s, expected %s\n",
6630 __location__, (const char *)blob.data, "bar");
6631 data_blob_free(&blob);
6632 return False;
6635 data_blob_free(&blob);
6637 if (!gencache_del("foo")) {
6638 d_printf("%s: gencache_del() failed\n", __location__);
6639 return False;
6641 if (gencache_del("foo")) {
6642 d_printf("%s: second gencache_del() succeeded\n",
6643 __location__);
6644 return False;
6647 if (gencache_get_data_blob("foo", &blob, NULL, NULL)) {
6648 d_printf("%s: gencache_get_data_blob() on deleted entry "
6649 "succeeded\n", __location__);
6650 return False;
6653 return True;
6656 static bool rbt_testval(struct db_context *db, const char *key,
6657 const char *value)
6659 struct db_record *rec;
6660 TDB_DATA data = string_tdb_data(value);
6661 bool ret = false;
6662 NTSTATUS status;
6664 rec = db->fetch_locked(db, db, string_tdb_data(key));
6665 if (rec == NULL) {
6666 d_fprintf(stderr, "fetch_locked failed\n");
6667 goto done;
6669 status = rec->store(rec, data, 0);
6670 if (!NT_STATUS_IS_OK(status)) {
6671 d_fprintf(stderr, "store failed: %s\n", nt_errstr(status));
6672 goto done;
6674 TALLOC_FREE(rec);
6676 rec = db->fetch_locked(db, db, string_tdb_data(key));
6677 if (rec == NULL) {
6678 d_fprintf(stderr, "second fetch_locked failed\n");
6679 goto done;
6681 if ((rec->value.dsize != data.dsize)
6682 || (memcmp(rec->value.dptr, data.dptr, data.dsize) != 0)) {
6683 d_fprintf(stderr, "Got wrong data back\n");
6684 goto done;
6687 ret = true;
6688 done:
6689 TALLOC_FREE(rec);
6690 return ret;
6693 static bool run_local_rbtree(int dummy)
6695 struct db_context *db;
6696 bool ret = false;
6697 int i;
6699 db = db_open_rbt(NULL);
6701 if (db == NULL) {
6702 d_fprintf(stderr, "db_open_rbt failed\n");
6703 return false;
6706 for (i=0; i<1000; i++) {
6707 char *key, *value;
6709 if (asprintf(&key, "key%ld", random()) == -1) {
6710 goto done;
6712 if (asprintf(&value, "value%ld", random()) == -1) {
6713 SAFE_FREE(key);
6714 goto done;
6717 if (!rbt_testval(db, key, value)) {
6718 SAFE_FREE(key);
6719 SAFE_FREE(value);
6720 goto done;
6723 SAFE_FREE(value);
6724 if (asprintf(&value, "value%ld", random()) == -1) {
6725 SAFE_FREE(key);
6726 goto done;
6729 if (!rbt_testval(db, key, value)) {
6730 SAFE_FREE(key);
6731 SAFE_FREE(value);
6732 goto done;
6735 SAFE_FREE(key);
6736 SAFE_FREE(value);
6739 ret = true;
6741 done:
6742 TALLOC_FREE(db);
6743 return ret;
6746 struct talloc_dict_test {
6747 int content;
6750 static int talloc_dict_traverse_fn(DATA_BLOB key, void *data, void *priv)
6752 int *count = (int *)priv;
6753 *count += 1;
6754 return 0;
6757 static bool run_local_talloc_dict(int dummy)
6759 struct talloc_dict *dict;
6760 struct talloc_dict_test *t;
6761 int key, count;
6763 dict = talloc_dict_init(talloc_tos());
6764 if (dict == NULL) {
6765 return false;
6768 t = talloc(talloc_tos(), struct talloc_dict_test);
6769 if (t == NULL) {
6770 return false;
6773 key = 1;
6774 t->content = 1;
6775 if (!talloc_dict_set(dict, data_blob_const(&key, sizeof(key)), t)) {
6776 return false;
6779 count = 0;
6780 if (talloc_dict_traverse(dict, talloc_dict_traverse_fn, &count) != 0) {
6781 return false;
6784 if (count != 1) {
6785 return false;
6788 TALLOC_FREE(dict);
6790 return true;
6793 static bool run_local_string_to_sid(int dummy) {
6794 struct dom_sid sid;
6796 if (string_to_sid(&sid, "S--1-5-32-545")) {
6797 printf("allowing S--1-5-32-545\n");
6798 return false;
6800 if (string_to_sid(&sid, "S-1-5-32-+545")) {
6801 printf("allowing S-1-5-32-+545\n");
6802 return false;
6804 if (string_to_sid(&sid, "S-1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0")) {
6805 printf("allowing S-1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0\n");
6806 return false;
6808 if (string_to_sid(&sid, "S-1-5-32-545-abc")) {
6809 printf("allowing S-1-5-32-545-abc\n");
6810 return false;
6812 if (!string_to_sid(&sid, "S-1-5-32-545")) {
6813 printf("could not parse S-1-5-32-545\n");
6814 return false;
6816 if (!sid_equal(&sid, &global_sid_Builtin_Users)) {
6817 printf("mis-parsed S-1-5-32-545 as %s\n",
6818 sid_string_tos(&sid));
6819 return false;
6821 return true;
6824 /* Split a path name into filename and stream name components. Canonicalise
6825 * such that an implicit $DATA token is always explicit.
6827 * The "specification" of this function can be found in the
6828 * run_local_stream_name() function in torture.c, I've tried those
6829 * combinations against a W2k3 server.
6832 static NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
6833 char **pbase, char **pstream)
6835 char *base = NULL;
6836 char *stream = NULL;
6837 char *sname; /* stream name */
6838 const char *stype; /* stream type */
6840 DEBUG(10, ("split_ntfs_stream_name called for [%s]\n", fname));
6842 sname = strchr_m(fname, ':');
6844 if (lp_posix_pathnames() || (sname == NULL)) {
6845 if (pbase != NULL) {
6846 base = talloc_strdup(mem_ctx, fname);
6847 NT_STATUS_HAVE_NO_MEMORY(base);
6849 goto done;
6852 if (pbase != NULL) {
6853 base = talloc_strndup(mem_ctx, fname, PTR_DIFF(sname, fname));
6854 NT_STATUS_HAVE_NO_MEMORY(base);
6857 sname += 1;
6859 stype = strchr_m(sname, ':');
6861 if (stype == NULL) {
6862 sname = talloc_strdup(mem_ctx, sname);
6863 stype = "$DATA";
6865 else {
6866 if (StrCaseCmp(stype, ":$DATA") != 0) {
6868 * If there is an explicit stream type, so far we only
6869 * allow $DATA. Is there anything else allowed? -- vl
6871 DEBUG(10, ("[%s] is an invalid stream type\n", stype));
6872 TALLOC_FREE(base);
6873 return NT_STATUS_OBJECT_NAME_INVALID;
6875 sname = talloc_strndup(mem_ctx, sname, PTR_DIFF(stype, sname));
6876 stype += 1;
6879 if (sname == NULL) {
6880 TALLOC_FREE(base);
6881 return NT_STATUS_NO_MEMORY;
6884 if (sname[0] == '\0') {
6886 * no stream name, so no stream
6888 goto done;
6891 if (pstream != NULL) {
6892 stream = talloc_asprintf(mem_ctx, "%s:%s", sname, stype);
6893 if (stream == NULL) {
6894 TALLOC_FREE(sname);
6895 TALLOC_FREE(base);
6896 return NT_STATUS_NO_MEMORY;
6899 * upper-case the type field
6901 strupper_m(strchr_m(stream, ':')+1);
6904 done:
6905 if (pbase != NULL) {
6906 *pbase = base;
6908 if (pstream != NULL) {
6909 *pstream = stream;
6911 return NT_STATUS_OK;
6914 static bool test_stream_name(const char *fname, const char *expected_base,
6915 const char *expected_stream,
6916 NTSTATUS expected_status)
6918 NTSTATUS status;
6919 char *base = NULL;
6920 char *stream = NULL;
6922 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &stream);
6923 if (!NT_STATUS_EQUAL(status, expected_status)) {
6924 goto error;
6927 if (!NT_STATUS_IS_OK(status)) {
6928 return true;
6931 if (base == NULL) goto error;
6933 if (strcmp(expected_base, base) != 0) goto error;
6935 if ((expected_stream != NULL) && (stream == NULL)) goto error;
6936 if ((expected_stream == NULL) && (stream != NULL)) goto error;
6938 if ((stream != NULL) && (strcmp(expected_stream, stream) != 0))
6939 goto error;
6941 TALLOC_FREE(base);
6942 TALLOC_FREE(stream);
6943 return true;
6945 error:
6946 d_fprintf(stderr, "test_stream(%s, %s, %s, %s)\n",
6947 fname, expected_base ? expected_base : "<NULL>",
6948 expected_stream ? expected_stream : "<NULL>",
6949 nt_errstr(expected_status));
6950 d_fprintf(stderr, "-> base=%s, stream=%s, status=%s\n",
6951 base ? base : "<NULL>", stream ? stream : "<NULL>",
6952 nt_errstr(status));
6953 TALLOC_FREE(base);
6954 TALLOC_FREE(stream);
6955 return false;
6958 static bool run_local_stream_name(int dummy)
6960 bool ret = true;
6962 ret &= test_stream_name(
6963 "bla", "bla", NULL, NT_STATUS_OK);
6964 ret &= test_stream_name(
6965 "bla::$DATA", "bla", NULL, NT_STATUS_OK);
6966 ret &= test_stream_name(
6967 "bla:blub:", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
6968 ret &= test_stream_name(
6969 "bla::", NULL, NULL, NT_STATUS_OBJECT_NAME_INVALID);
6970 ret &= test_stream_name(
6971 "bla::123", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
6972 ret &= test_stream_name(
6973 "bla:$DATA", "bla", "$DATA:$DATA", NT_STATUS_OK);
6974 ret &= test_stream_name(
6975 "bla:x:$DATA", "bla", "x:$DATA", NT_STATUS_OK);
6976 ret &= test_stream_name(
6977 "bla:x", "bla", "x:$DATA", NT_STATUS_OK);
6979 return ret;
6982 static bool data_blob_equal(DATA_BLOB a, DATA_BLOB b)
6984 if (a.length != b.length) {
6985 printf("a.length=%d != b.length=%d\n",
6986 (int)a.length, (int)b.length);
6987 return false;
6989 if (memcmp(a.data, b.data, a.length) != 0) {
6990 printf("a.data and b.data differ\n");
6991 return false;
6993 return true;
6996 static bool run_local_memcache(int dummy)
6998 struct memcache *cache;
6999 DATA_BLOB k1, k2;
7000 DATA_BLOB d1, d2, d3;
7001 DATA_BLOB v1, v2, v3;
7003 TALLOC_CTX *mem_ctx;
7004 char *str1, *str2;
7005 size_t size1, size2;
7006 bool ret = false;
7008 cache = memcache_init(NULL, 100);
7010 if (cache == NULL) {
7011 printf("memcache_init failed\n");
7012 return false;
7015 d1 = data_blob_const("d1", 2);
7016 d2 = data_blob_const("d2", 2);
7017 d3 = data_blob_const("d3", 2);
7019 k1 = data_blob_const("d1", 2);
7020 k2 = data_blob_const("d2", 2);
7022 memcache_add(cache, STAT_CACHE, k1, d1);
7023 memcache_add(cache, GETWD_CACHE, k2, d2);
7025 if (!memcache_lookup(cache, STAT_CACHE, k1, &v1)) {
7026 printf("could not find k1\n");
7027 return false;
7029 if (!data_blob_equal(d1, v1)) {
7030 return false;
7033 if (!memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
7034 printf("could not find k2\n");
7035 return false;
7037 if (!data_blob_equal(d2, v2)) {
7038 return false;
7041 memcache_add(cache, STAT_CACHE, k1, d3);
7043 if (!memcache_lookup(cache, STAT_CACHE, k1, &v3)) {
7044 printf("could not find replaced k1\n");
7045 return false;
7047 if (!data_blob_equal(d3, v3)) {
7048 return false;
7051 memcache_add(cache, GETWD_CACHE, k1, d1);
7053 if (memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
7054 printf("Did find k2, should have been purged\n");
7055 return false;
7058 TALLOC_FREE(cache);
7060 cache = memcache_init(NULL, 0);
7062 mem_ctx = talloc_init("foo");
7064 str1 = talloc_strdup(mem_ctx, "string1");
7065 str2 = talloc_strdup(mem_ctx, "string2");
7067 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
7068 data_blob_string_const("torture"), &str1);
7069 size1 = talloc_total_size(cache);
7071 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
7072 data_blob_string_const("torture"), &str2);
7073 size2 = talloc_total_size(cache);
7075 printf("size1=%d, size2=%d\n", (int)size1, (int)size2);
7077 if (size2 > size1) {
7078 printf("memcache leaks memory!\n");
7079 goto fail;
7082 ret = true;
7083 fail:
7084 TALLOC_FREE(cache);
7085 return ret;
7088 static void wbclient_done(struct tevent_req *req)
7090 wbcErr wbc_err;
7091 struct winbindd_response *wb_resp;
7092 int *i = (int *)tevent_req_callback_data_void(req);
7094 wbc_err = wb_trans_recv(req, req, &wb_resp);
7095 TALLOC_FREE(req);
7096 *i += 1;
7097 d_printf("wb_trans_recv %d returned %s\n", *i, wbcErrorString(wbc_err));
7100 static bool run_local_wbclient(int dummy)
7102 struct event_context *ev;
7103 struct wb_context **wb_ctx;
7104 struct winbindd_request wb_req;
7105 bool result = false;
7106 int i, j;
7108 BlockSignals(True, SIGPIPE);
7110 ev = tevent_context_init_byname(talloc_tos(), "epoll");
7111 if (ev == NULL) {
7112 goto fail;
7115 wb_ctx = TALLOC_ARRAY(ev, struct wb_context *, nprocs);
7116 if (wb_ctx == NULL) {
7117 goto fail;
7120 ZERO_STRUCT(wb_req);
7121 wb_req.cmd = WINBINDD_PING;
7123 d_printf("nprocs=%d, numops=%d\n", (int)nprocs, (int)torture_numops);
7125 for (i=0; i<nprocs; i++) {
7126 wb_ctx[i] = wb_context_init(ev, NULL);
7127 if (wb_ctx[i] == NULL) {
7128 goto fail;
7130 for (j=0; j<torture_numops; j++) {
7131 struct tevent_req *req;
7132 req = wb_trans_send(ev, ev, wb_ctx[i],
7133 (j % 2) == 0, &wb_req);
7134 if (req == NULL) {
7135 goto fail;
7137 tevent_req_set_callback(req, wbclient_done, &i);
7141 i = 0;
7143 while (i < nprocs * torture_numops) {
7144 event_loop_once(ev);
7147 result = true;
7148 fail:
7149 TALLOC_FREE(ev);
7150 return result;
7153 static void getaddrinfo_finished(struct tevent_req *req)
7155 char *name = (char *)tevent_req_callback_data_void(req);
7156 struct addrinfo *ainfo;
7157 int res;
7159 res = getaddrinfo_recv(req, &ainfo);
7160 if (res != 0) {
7161 d_printf("gai(%s) returned %s\n", name, gai_strerror(res));
7162 return;
7164 d_printf("gai(%s) succeeded\n", name);
7165 freeaddrinfo(ainfo);
7168 static bool run_getaddrinfo_send(int dummy)
7170 TALLOC_CTX *frame = talloc_stackframe();
7171 struct fncall_context *ctx;
7172 struct tevent_context *ev;
7173 bool result = false;
7174 const char *names[4] = { "www.samba.org", "notfound.samba.org",
7175 "www.slashdot.org", "heise.de" };
7176 struct tevent_req *reqs[4];
7177 int i;
7179 ev = event_context_init(frame);
7180 if (ev == NULL) {
7181 goto fail;
7184 ctx = fncall_context_init(frame, 4);
7186 for (i=0; i<ARRAY_SIZE(names); i++) {
7187 reqs[i] = getaddrinfo_send(frame, ev, ctx, names[i], NULL,
7188 NULL);
7189 if (reqs[i] == NULL) {
7190 goto fail;
7192 tevent_req_set_callback(reqs[i], getaddrinfo_finished,
7193 (void *)names[i]);
7196 for (i=0; i<ARRAY_SIZE(reqs); i++) {
7197 tevent_loop_once(ev);
7200 result = true;
7201 fail:
7202 TALLOC_FREE(frame);
7203 return result;
7206 static bool dbtrans_inc(struct db_context *db)
7208 struct db_record *rec;
7209 uint32_t *val;
7210 bool ret = false;
7211 NTSTATUS status;
7213 rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
7214 if (rec == NULL) {
7215 printf(__location__ "fetch_lock failed\n");
7216 return false;
7219 if (rec->value.dsize != sizeof(uint32_t)) {
7220 printf(__location__ "value.dsize = %d\n",
7221 (int)rec->value.dsize);
7222 goto fail;
7225 val = (uint32_t *)rec->value.dptr;
7226 *val += 1;
7228 status = rec->store(rec, make_tdb_data((uint8_t *)val,
7229 sizeof(uint32_t)),
7231 if (!NT_STATUS_IS_OK(status)) {
7232 printf(__location__ "store failed: %s\n",
7233 nt_errstr(status));
7234 goto fail;
7237 ret = true;
7238 fail:
7239 TALLOC_FREE(rec);
7240 return ret;
7243 static bool run_local_dbtrans(int dummy)
7245 struct db_context *db;
7246 struct db_record *rec;
7247 NTSTATUS status;
7248 uint32_t initial;
7249 int res;
7251 db = db_open(talloc_tos(), "transtest.tdb", 0, TDB_DEFAULT,
7252 O_RDWR|O_CREAT, 0600);
7253 if (db == NULL) {
7254 printf("Could not open transtest.db\n");
7255 return false;
7258 res = db->transaction_start(db);
7259 if (res == -1) {
7260 printf(__location__ "transaction_start failed\n");
7261 return false;
7264 rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
7265 if (rec == NULL) {
7266 printf(__location__ "fetch_lock failed\n");
7267 return false;
7270 if (rec->value.dptr == NULL) {
7271 initial = 0;
7272 status = rec->store(
7273 rec, make_tdb_data((uint8_t *)&initial,
7274 sizeof(initial)),
7276 if (!NT_STATUS_IS_OK(status)) {
7277 printf(__location__ "store returned %s\n",
7278 nt_errstr(status));
7279 return false;
7283 TALLOC_FREE(rec);
7285 res = db->transaction_commit(db);
7286 if (res == -1) {
7287 printf(__location__ "transaction_commit failed\n");
7288 return false;
7291 while (true) {
7292 uint32_t val, val2;
7293 int i;
7295 res = db->transaction_start(db);
7296 if (res == -1) {
7297 printf(__location__ "transaction_start failed\n");
7298 break;
7301 if (!dbwrap_fetch_uint32(db, "transtest", &val)) {
7302 printf(__location__ "dbwrap_fetch_uint32 failed\n");
7303 break;
7306 for (i=0; i<10; i++) {
7307 if (!dbtrans_inc(db)) {
7308 return false;
7312 if (!dbwrap_fetch_uint32(db, "transtest", &val2)) {
7313 printf(__location__ "dbwrap_fetch_uint32 failed\n");
7314 break;
7317 if (val2 != val + 10) {
7318 printf(__location__ "val=%d, val2=%d\n",
7319 (int)val, (int)val2);
7320 break;
7323 printf("val2=%d\r", val2);
7325 res = db->transaction_commit(db);
7326 if (res == -1) {
7327 printf(__location__ "transaction_commit failed\n");
7328 break;
7332 TALLOC_FREE(db);
7333 return true;
7336 static double create_procs(bool (*fn)(int), bool *result)
7338 int i, status;
7339 volatile pid_t *child_status;
7340 volatile bool *child_status_out;
7341 int synccount;
7342 int tries = 8;
7343 struct timeval start;
7345 synccount = 0;
7347 child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*nprocs);
7348 if (!child_status) {
7349 printf("Failed to setup shared memory\n");
7350 return -1;
7353 child_status_out = (volatile bool *)shm_setup(sizeof(bool)*nprocs);
7354 if (!child_status_out) {
7355 printf("Failed to setup result status shared memory\n");
7356 return -1;
7359 for (i = 0; i < nprocs; i++) {
7360 child_status[i] = 0;
7361 child_status_out[i] = True;
7364 start = timeval_current();
7366 for (i=0;i<nprocs;i++) {
7367 procnum = i;
7368 if (fork() == 0) {
7369 pid_t mypid = getpid();
7370 sys_srandom(((int)mypid) ^ ((int)time(NULL)));
7372 slprintf(myname,sizeof(myname),"CLIENT%d", i);
7374 while (1) {
7375 if (torture_open_connection(&current_cli, i)) break;
7376 if (tries-- == 0) {
7377 printf("pid %d failed to start\n", (int)getpid());
7378 _exit(1);
7380 smb_msleep(10);
7383 child_status[i] = getpid();
7385 while (child_status[i] && timeval_elapsed(&start) < 5) smb_msleep(2);
7387 child_status_out[i] = fn(i);
7388 _exit(0);
7392 do {
7393 synccount = 0;
7394 for (i=0;i<nprocs;i++) {
7395 if (child_status[i]) synccount++;
7397 if (synccount == nprocs) break;
7398 smb_msleep(10);
7399 } while (timeval_elapsed(&start) < 30);
7401 if (synccount != nprocs) {
7402 printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount);
7403 *result = False;
7404 return timeval_elapsed(&start);
7407 /* start the client load */
7408 start = timeval_current();
7410 for (i=0;i<nprocs;i++) {
7411 child_status[i] = 0;
7414 printf("%d clients started\n", nprocs);
7416 for (i=0;i<nprocs;i++) {
7417 while (waitpid(0, &status, 0) == -1 && errno == EINTR) /* noop */ ;
7420 printf("\n");
7422 for (i=0;i<nprocs;i++) {
7423 if (!child_status_out[i]) {
7424 *result = False;
7427 return timeval_elapsed(&start);
7430 #define FLAG_MULTIPROC 1
7432 static struct {
7433 const char *name;
7434 bool (*fn)(int);
7435 unsigned flags;
7436 } torture_ops[] = {
7437 {"FDPASS", run_fdpasstest, 0},
7438 {"LOCK1", run_locktest1, 0},
7439 {"LOCK2", run_locktest2, 0},
7440 {"LOCK3", run_locktest3, 0},
7441 {"LOCK4", run_locktest4, 0},
7442 {"LOCK5", run_locktest5, 0},
7443 {"LOCK6", run_locktest6, 0},
7444 {"LOCK7", run_locktest7, 0},
7445 {"LOCK8", run_locktest8, 0},
7446 {"LOCK9", run_locktest9, 0},
7447 {"UNLINK", run_unlinktest, 0},
7448 {"BROWSE", run_browsetest, 0},
7449 {"ATTR", run_attrtest, 0},
7450 {"TRANS2", run_trans2test, 0},
7451 {"MAXFID", run_maxfidtest, FLAG_MULTIPROC},
7452 {"TORTURE",run_torture, FLAG_MULTIPROC},
7453 {"RANDOMIPC", run_randomipc, 0},
7454 {"NEGNOWAIT", run_negprot_nowait, 0},
7455 {"NBENCH", run_nbench, 0},
7456 {"OPLOCK1", run_oplock1, 0},
7457 {"OPLOCK2", run_oplock2, 0},
7458 {"OPLOCK3", run_oplock3, 0},
7459 {"DIR", run_dirtest, 0},
7460 {"DIR1", run_dirtest1, 0},
7461 {"DIR-CREATETIME", run_dir_createtime, 0},
7462 {"DENY1", torture_denytest1, 0},
7463 {"DENY2", torture_denytest2, 0},
7464 {"TCON", run_tcon_test, 0},
7465 {"TCONDEV", run_tcon_devtype_test, 0},
7466 {"RW1", run_readwritetest, 0},
7467 {"RW2", run_readwritemulti, FLAG_MULTIPROC},
7468 {"RW3", run_readwritelarge, 0},
7469 {"OPEN", run_opentest, 0},
7470 {"POSIX", run_simple_posix_open_test, 0},
7471 {"POSIX-APPEND", run_posix_append, 0},
7472 { "UID-REGRESSION-TEST", run_uid_regression_test, 0},
7473 { "SHORTNAME-TEST", run_shortname_test, 0},
7474 #if 1
7475 {"OPENATTR", run_openattrtest, 0},
7476 #endif
7477 {"XCOPY", run_xcopy, 0},
7478 {"RENAME", run_rename, 0},
7479 {"DELETE", run_deletetest, 0},
7480 {"PROPERTIES", run_properties, 0},
7481 {"MANGLE", torture_mangle, 0},
7482 {"MANGLE1", run_mangle1, 0},
7483 {"W2K", run_w2ktest, 0},
7484 {"TRANS2SCAN", torture_trans2_scan, 0},
7485 {"NTTRANSSCAN", torture_nttrans_scan, 0},
7486 {"UTABLE", torture_utable, 0},
7487 {"CASETABLE", torture_casetable, 0},
7488 {"ERRMAPEXTRACT", run_error_map_extract, 0},
7489 {"PIPE_NUMBER", run_pipe_number, 0},
7490 {"TCON2", run_tcon2_test, 0},
7491 {"IOCTL", torture_ioctl_test, 0},
7492 {"CHKPATH", torture_chkpath_test, 0},
7493 {"FDSESS", run_fdsesstest, 0},
7494 { "EATEST", run_eatest, 0},
7495 { "SESSSETUP_BENCH", run_sesssetup_bench, 0},
7496 { "CHAIN1", run_chain1, 0},
7497 { "CHAIN2", run_chain2, 0},
7498 { "WINDOWS-WRITE", run_windows_write, 0},
7499 { "CLI_ECHO", run_cli_echo, 0},
7500 { "GETADDRINFO", run_getaddrinfo_send, 0},
7501 { "TLDAP", run_tldap },
7502 { "STREAMERROR", run_streamerror },
7503 { "NOTIFY-BENCH", run_notify_bench },
7504 { "LOCAL-SUBSTITUTE", run_local_substitute, 0},
7505 { "LOCAL-GENCACHE", run_local_gencache, 0},
7506 { "LOCAL-TALLOC-DICT", run_local_talloc_dict, 0},
7507 { "LOCAL-BASE64", run_local_base64, 0},
7508 { "LOCAL-RBTREE", run_local_rbtree, 0},
7509 { "LOCAL-MEMCACHE", run_local_memcache, 0},
7510 { "LOCAL-STREAM-NAME", run_local_stream_name, 0},
7511 { "LOCAL-WBCLIENT", run_local_wbclient, 0},
7512 { "LOCAL-string_to_sid", run_local_string_to_sid, 0},
7513 { "LOCAL-DBTRANS", run_local_dbtrans, 0},
7514 {NULL, NULL, 0}};
7518 /****************************************************************************
7519 run a specified test or "ALL"
7520 ****************************************************************************/
7521 static bool run_test(const char *name)
7523 bool ret = True;
7524 bool result = True;
7525 bool found = False;
7526 int i;
7527 double t;
7528 if (strequal(name,"ALL")) {
7529 for (i=0;torture_ops[i].name;i++) {
7530 run_test(torture_ops[i].name);
7532 found = True;
7535 for (i=0;torture_ops[i].name;i++) {
7536 fstr_sprintf(randomfname, "\\XX%x",
7537 (unsigned)random());
7539 if (strequal(name, torture_ops[i].name)) {
7540 found = True;
7541 printf("Running %s\n", name);
7542 if (torture_ops[i].flags & FLAG_MULTIPROC) {
7543 t = create_procs(torture_ops[i].fn, &result);
7544 if (!result) {
7545 ret = False;
7546 printf("TEST %s FAILED!\n", name);
7548 } else {
7549 struct timeval start;
7550 start = timeval_current();
7551 if (!torture_ops[i].fn(0)) {
7552 ret = False;
7553 printf("TEST %s FAILED!\n", name);
7555 t = timeval_elapsed(&start);
7557 printf("%s took %g secs\n\n", name, t);
7561 if (!found) {
7562 printf("Did not find a test named %s\n", name);
7563 ret = False;
7566 return ret;
7570 static void usage(void)
7572 int i;
7574 printf("WARNING samba4 test suite is much more complete nowadays.\n");
7575 printf("Please use samba4 torture.\n\n");
7577 printf("Usage: smbtorture //server/share <options> TEST1 TEST2 ...\n");
7579 printf("\t-d debuglevel\n");
7580 printf("\t-U user%%pass\n");
7581 printf("\t-k use kerberos\n");
7582 printf("\t-N numprocs\n");
7583 printf("\t-n my_netbios_name\n");
7584 printf("\t-W workgroup\n");
7585 printf("\t-o num_operations\n");
7586 printf("\t-O socket_options\n");
7587 printf("\t-m maximum protocol\n");
7588 printf("\t-L use oplocks\n");
7589 printf("\t-c CLIENT.TXT specify client load file for NBENCH\n");
7590 printf("\t-A showall\n");
7591 printf("\t-p port\n");
7592 printf("\t-s seed\n");
7593 printf("\t-b unclist_filename specify multiple shares for multiple connections\n");
7594 printf("\n\n");
7596 printf("tests are:");
7597 for (i=0;torture_ops[i].name;i++) {
7598 printf(" %s", torture_ops[i].name);
7600 printf("\n");
7602 printf("default test is ALL\n");
7604 exit(1);
7607 /****************************************************************************
7608 main program
7609 ****************************************************************************/
7610 int main(int argc,char *argv[])
7612 int opt, i;
7613 char *p;
7614 int gotuser = 0;
7615 int gotpass = 0;
7616 bool correct = True;
7617 TALLOC_CTX *frame = talloc_stackframe();
7618 int seed = time(NULL);
7620 dbf = x_stdout;
7622 #ifdef HAVE_SETBUFFER
7623 setbuffer(stdout, NULL, 0);
7624 #endif
7626 load_case_tables();
7628 setup_logging("smbtorture", true);
7630 if (is_default_dyn_CONFIGFILE()) {
7631 if(getenv("SMB_CONF_PATH")) {
7632 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
7635 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
7636 load_interfaces();
7638 if (argc < 2) {
7639 usage();
7642 for(p = argv[1]; *p; p++)
7643 if(*p == '\\')
7644 *p = '/';
7646 if (strncmp(argv[1], "//", 2)) {
7647 usage();
7650 fstrcpy(host, &argv[1][2]);
7651 p = strchr_m(&host[2],'/');
7652 if (!p) {
7653 usage();
7655 *p = 0;
7656 fstrcpy(share, p+1);
7658 fstrcpy(myname, get_myname(talloc_tos()));
7659 if (!*myname) {
7660 fprintf(stderr, "Failed to get my hostname.\n");
7661 return 1;
7664 if (*username == 0 && getenv("LOGNAME")) {
7665 fstrcpy(username,getenv("LOGNAME"));
7668 argc--;
7669 argv++;
7671 fstrcpy(workgroup, lp_workgroup());
7673 while ((opt = getopt(argc, argv, "p:hW:U:n:N:O:o:m:Ll:d:Aec:ks:b:B:")) != EOF) {
7674 switch (opt) {
7675 case 'p':
7676 port_to_use = atoi(optarg);
7677 break;
7678 case 's':
7679 seed = atoi(optarg);
7680 break;
7681 case 'W':
7682 fstrcpy(workgroup,optarg);
7683 break;
7684 case 'm':
7685 max_protocol = interpret_protocol(optarg, max_protocol);
7686 break;
7687 case 'N':
7688 nprocs = atoi(optarg);
7689 break;
7690 case 'o':
7691 torture_numops = atoi(optarg);
7692 break;
7693 case 'd':
7694 DEBUGLEVEL = atoi(optarg);
7695 break;
7696 case 'O':
7697 sockops = optarg;
7698 break;
7699 case 'L':
7700 use_oplocks = True;
7701 break;
7702 case 'l':
7703 local_path = optarg;
7704 break;
7705 case 'A':
7706 torture_showall = True;
7707 break;
7708 case 'n':
7709 fstrcpy(myname, optarg);
7710 break;
7711 case 'c':
7712 client_txt = optarg;
7713 break;
7714 case 'e':
7715 do_encrypt = true;
7716 break;
7717 case 'k':
7718 #ifdef HAVE_KRB5
7719 use_kerberos = True;
7720 #else
7721 d_printf("No kerberos support compiled in\n");
7722 exit(1);
7723 #endif
7724 break;
7725 case 'U':
7726 gotuser = 1;
7727 fstrcpy(username,optarg);
7728 p = strchr_m(username,'%');
7729 if (p) {
7730 *p = 0;
7731 fstrcpy(password, p+1);
7732 gotpass = 1;
7734 break;
7735 case 'b':
7736 fstrcpy(multishare_conn_fname, optarg);
7737 use_multishare_conn = True;
7738 break;
7739 case 'B':
7740 torture_blocksize = atoi(optarg);
7741 break;
7742 default:
7743 printf("Unknown option %c (%d)\n", (char)opt, opt);
7744 usage();
7748 d_printf("using seed %d\n", seed);
7750 srandom(seed);
7752 if(use_kerberos && !gotuser) gotpass = True;
7754 while (!gotpass) {
7755 p = getpass("Password:");
7756 if (p) {
7757 fstrcpy(password, p);
7758 gotpass = 1;
7762 printf("host=%s share=%s user=%s myname=%s\n",
7763 host, share, username, myname);
7765 if (argc == optind) {
7766 correct = run_test("ALL");
7767 } else {
7768 for (i=optind;i<argc;i++) {
7769 if (!run_test(argv[i])) {
7770 correct = False;
7775 TALLOC_FREE(frame);
7777 if (correct) {
7778 return(0);
7779 } else {
7780 return(1);