Torture test added to run_opentest() to ensure we don't regress on
[Samba/gebeck_regimport.git] / source3 / torture / torture.c
blob2670485bcca7597e761c42c0dd5be1f13f026879
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 "wbc_async.h"
23 #include "torture/proto.h"
24 #include "libcli/security/security.h"
25 #include "tldap.h"
26 #include "tldap_util.h"
27 #include "../librpc/gen_ndr/svcctl.h"
28 #include "memcache.h"
29 #include "nsswitch/winbind_client.h"
30 #include "dbwrap.h"
31 #include "talloc_dict.h"
32 #include "async_smb.h"
34 extern char *optarg;
35 extern int optind;
37 static fstring host, workgroup, share, password, username, myname;
38 static int max_protocol = PROTOCOL_NT1;
39 static const char *sockops="TCP_NODELAY";
40 static int nprocs=1;
41 static int port_to_use=0;
42 int torture_numops=100;
43 int torture_blocksize=1024*1024;
44 static int procnum; /* records process count number when forking */
45 static struct cli_state *current_cli;
46 static fstring randomfname;
47 static bool use_oplocks;
48 static bool use_level_II_oplocks;
49 static const char *client_txt = "client_oplocks.txt";
50 static bool use_kerberos;
51 static fstring multishare_conn_fname;
52 static bool use_multishare_conn = False;
53 static bool do_encrypt;
54 static const char *local_path = NULL;
55 static int signing_state = Undefined;
57 bool torture_showall = False;
59 static double create_procs(bool (*fn)(int), bool *result);
62 /* return a pointer to a anonymous shared memory segment of size "size"
63 which will persist across fork() but will disappear when all processes
64 exit
66 The memory is not zeroed
68 This function uses system5 shared memory. It takes advantage of a property
69 that the memory is not destroyed if it is attached when the id is removed
71 void *shm_setup(int size)
73 int shmid;
74 void *ret;
76 #ifdef __QNXNTO__
77 shmid = shm_open("private", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
78 if (shmid == -1) {
79 printf("can't get shared memory\n");
80 exit(1);
82 shm_unlink("private");
83 if (ftruncate(shmid, size) == -1) {
84 printf("can't set shared memory size\n");
85 exit(1);
87 ret = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0);
88 if (ret == MAP_FAILED) {
89 printf("can't map shared memory\n");
90 exit(1);
92 #else
93 shmid = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
94 if (shmid == -1) {
95 printf("can't get shared memory\n");
96 exit(1);
98 ret = (void *)shmat(shmid, 0, 0);
99 if (!ret || ret == (void *)-1) {
100 printf("can't attach to shared memory\n");
101 return NULL;
103 /* the following releases the ipc, but note that this process
104 and all its children will still have access to the memory, its
105 just that the shmid is no longer valid for other shm calls. This
106 means we don't leave behind lots of shm segments after we exit
108 See Stevens "advanced programming in unix env" for details
110 shmctl(shmid, IPC_RMID, 0);
111 #endif
113 return ret;
116 /********************************************************************
117 Ensure a connection is encrypted.
118 ********************************************************************/
120 static bool force_cli_encryption(struct cli_state *c,
121 const char *sharename)
123 uint16 major, minor;
124 uint32 caplow, caphigh;
125 NTSTATUS status;
127 if (!SERVER_HAS_UNIX_CIFS(c)) {
128 d_printf("Encryption required and "
129 "server that doesn't support "
130 "UNIX extensions - failing connect\n");
131 return false;
134 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
135 &caphigh);
136 if (!NT_STATUS_IS_OK(status)) {
137 d_printf("Encryption required and "
138 "can't get UNIX CIFS extensions "
139 "version from server: %s\n", nt_errstr(status));
140 return false;
143 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
144 d_printf("Encryption required and "
145 "share %s doesn't support "
146 "encryption.\n", sharename);
147 return false;
150 if (c->use_kerberos) {
151 status = cli_gss_smb_encryption_start(c);
152 } else {
153 status = cli_raw_ntlm_smb_encryption_start(c,
154 username,
155 password,
156 workgroup);
159 if (!NT_STATUS_IS_OK(status)) {
160 d_printf("Encryption required and "
161 "setup failed with error %s.\n",
162 nt_errstr(status));
163 return false;
166 return true;
170 static struct cli_state *open_nbt_connection(void)
172 struct nmb_name called, calling;
173 struct sockaddr_storage ss;
174 struct cli_state *c;
175 NTSTATUS status;
177 make_nmb_name(&calling, myname, 0x0);
178 make_nmb_name(&called , host, 0x20);
180 zero_sockaddr(&ss);
182 if (!(c = cli_initialise_ex(signing_state))) {
183 printf("Failed initialize cli_struct to connect with %s\n", host);
184 return NULL;
187 c->port = port_to_use;
189 status = cli_connect(c, host, &ss);
190 if (!NT_STATUS_IS_OK(status)) {
191 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
192 return NULL;
195 c->use_kerberos = use_kerberos;
197 c->timeout = 120000; /* set a really long timeout (2 minutes) */
198 if (use_oplocks) c->use_oplocks = True;
199 if (use_level_II_oplocks) c->use_level_II_oplocks = True;
201 if (!cli_session_request(c, &calling, &called)) {
203 * Well, that failed, try *SMBSERVER ...
204 * However, we must reconnect as well ...
206 status = cli_connect(c, host, &ss);
207 if (!NT_STATUS_IS_OK(status)) {
208 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
209 return NULL;
212 make_nmb_name(&called, "*SMBSERVER", 0x20);
213 if (!cli_session_request(c, &calling, &called)) {
214 printf("%s rejected the session\n",host);
215 printf("We tried with a called name of %s & %s\n",
216 host, "*SMBSERVER");
217 cli_shutdown(c);
218 return NULL;
222 return c;
225 /****************************************************************************
226 Send a corrupt session request. See rfc1002.txt 4.3 and 4.3.2.
227 ****************************************************************************/
229 static bool cli_bad_session_request(struct cli_state *cli,
230 struct nmb_name *calling, struct nmb_name *called)
232 char *p;
233 int len = 4;
234 int namelen = 0;
235 char *tmp;
237 memcpy(&(cli->calling), calling, sizeof(*calling));
238 memcpy(&(cli->called ), called , sizeof(*called ));
240 /* put in the destination name */
242 tmp = name_mangle(talloc_tos(), cli->called.name,
243 cli->called.name_type);
244 if (tmp == NULL) {
245 return false;
248 p = cli->outbuf+len;
249 namelen = name_len((unsigned char *)tmp, talloc_get_size(tmp));
250 if (namelen > 0) {
251 memcpy(p, tmp, namelen);
252 len += namelen;
254 TALLOC_FREE(tmp);
256 /* Deliberately corrupt the name len (first byte) */
257 *p = 100;
259 /* and my name */
261 tmp = name_mangle(talloc_tos(), cli->calling.name,
262 cli->calling.name_type);
263 if (tmp == NULL) {
264 return false;
267 p = cli->outbuf+len;
268 namelen = name_len((unsigned char *)tmp, talloc_get_size(tmp));
269 if (namelen > 0) {
270 memcpy(p, tmp, namelen);
271 len += namelen;
273 TALLOC_FREE(tmp);
274 /* Deliberately corrupt the name len (first byte) */
275 *p = 100;
277 /* send a session request (RFC 1002) */
278 /* setup the packet length
279 * Remove four bytes from the length count, since the length
280 * field in the NBT Session Service header counts the number
281 * of bytes which follow. The cli_send_smb() function knows
282 * about this and accounts for those four bytes.
283 * CRH.
285 len -= 4;
286 _smb_setlen(cli->outbuf,len);
287 SCVAL(cli->outbuf,0,0x81);
289 cli_send_smb(cli);
290 DEBUG(5,("Sent session request\n"));
292 if (!cli_receive_smb(cli))
293 return False;
295 if (CVAL(cli->inbuf,0) != 0x82) {
296 /* This is the wrong place to put the error... JRA. */
297 cli->rap_error = CVAL(cli->inbuf,4);
298 return False;
300 return(True);
303 static struct cli_state *open_bad_nbt_connection(void)
305 struct nmb_name called, calling;
306 struct sockaddr_storage ss;
307 struct cli_state *c;
308 NTSTATUS status;
310 make_nmb_name(&calling, myname, 0x0);
311 make_nmb_name(&called , host, 0x20);
313 zero_sockaddr(&ss);
315 if (!(c = cli_initialise_ex(signing_state))) {
316 printf("Failed initialize cli_struct to connect with %s\n", host);
317 return NULL;
320 c->port = 139;
322 status = cli_connect(c, host, &ss);
323 if (!NT_STATUS_IS_OK(status)) {
324 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
325 return NULL;
328 c->timeout = 4000; /* set a short timeout (4 seconds) */
330 if (!cli_bad_session_request(c, &calling, &called)) {
331 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
332 return NULL;
335 return c;
339 /* Insert a NULL at the first separator of the given path and return a pointer
340 * to the remainder of the string.
342 static char *
343 terminate_path_at_separator(char * path)
345 char * p;
347 if (!path) {
348 return NULL;
351 if ((p = strchr_m(path, '/'))) {
352 *p = '\0';
353 return p + 1;
356 if ((p = strchr_m(path, '\\'))) {
357 *p = '\0';
358 return p + 1;
361 /* No separator. */
362 return NULL;
366 parse a //server/share type UNC name
368 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
369 char **hostname, char **sharename)
371 char *p;
373 *hostname = *sharename = NULL;
375 if (strncmp(unc_name, "\\\\", 2) &&
376 strncmp(unc_name, "//", 2)) {
377 return False;
380 *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
381 p = terminate_path_at_separator(*hostname);
383 if (p && *p) {
384 *sharename = talloc_strdup(mem_ctx, p);
385 terminate_path_at_separator(*sharename);
388 if (*hostname && *sharename) {
389 return True;
392 TALLOC_FREE(*hostname);
393 TALLOC_FREE(*sharename);
394 return False;
397 static bool torture_open_connection_share(struct cli_state **c,
398 const char *hostname,
399 const char *sharename)
401 int flags = 0;
402 NTSTATUS status;
404 if (use_kerberos)
405 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
406 if (use_oplocks)
407 flags |= CLI_FULL_CONNECTION_OPLOCKS;
408 if (use_level_II_oplocks)
409 flags |= CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS;
411 status = cli_full_connection(c, myname,
412 hostname, NULL, port_to_use,
413 sharename, "?????",
414 username, workgroup,
415 password, flags, signing_state);
416 if (!NT_STATUS_IS_OK(status)) {
417 printf("failed to open share connection: //%s/%s port:%d - %s\n",
418 hostname, sharename, port_to_use, nt_errstr(status));
419 return False;
422 (*c)->timeout = 120000; /* set a really long timeout (2 minutes) */
424 if (do_encrypt) {
425 return force_cli_encryption(*c,
426 sharename);
428 return True;
431 bool torture_open_connection(struct cli_state **c, int conn_index)
433 char **unc_list = NULL;
434 int num_unc_names = 0;
435 bool result;
437 if (use_multishare_conn==True) {
438 char *h, *s;
439 unc_list = file_lines_load(multishare_conn_fname, &num_unc_names, 0, NULL);
440 if (!unc_list || num_unc_names <= 0) {
441 printf("Failed to load unc names list from '%s'\n", multishare_conn_fname);
442 exit(1);
445 if (!smbcli_parse_unc(unc_list[conn_index % num_unc_names],
446 NULL, &h, &s)) {
447 printf("Failed to parse UNC name %s\n",
448 unc_list[conn_index % num_unc_names]);
449 TALLOC_FREE(unc_list);
450 exit(1);
453 result = torture_open_connection_share(c, h, s);
455 /* h, s were copied earlier */
456 TALLOC_FREE(unc_list);
457 return result;
460 return torture_open_connection_share(c, host, share);
463 bool torture_cli_session_setup2(struct cli_state *cli, uint16 *new_vuid)
465 uint16 old_vuid = cli->vuid;
466 fstring old_user_name;
467 size_t passlen = strlen(password);
468 NTSTATUS status;
469 bool ret;
471 fstrcpy(old_user_name, cli->user_name);
472 cli->vuid = 0;
473 ret = NT_STATUS_IS_OK(cli_session_setup(cli, username,
474 password, passlen,
475 password, passlen,
476 workgroup));
477 *new_vuid = cli->vuid;
478 cli->vuid = old_vuid;
479 status = cli_set_username(cli, old_user_name);
480 if (!NT_STATUS_IS_OK(status)) {
481 return false;
483 return ret;
487 bool torture_close_connection(struct cli_state *c)
489 bool ret = True;
490 NTSTATUS status;
492 status = cli_tdis(c);
493 if (!NT_STATUS_IS_OK(status)) {
494 printf("tdis failed (%s)\n", nt_errstr(status));
495 ret = False;
498 cli_shutdown(c);
500 return ret;
504 /* check if the server produced the expected error code */
505 static bool check_error(int line, struct cli_state *c,
506 uint8 eclass, uint32 ecode, NTSTATUS nterr)
508 if (cli_is_dos_error(c)) {
509 uint8 cclass;
510 uint32 num;
512 /* Check DOS error */
514 cli_dos_error(c, &cclass, &num);
516 if (eclass != cclass || ecode != num) {
517 printf("unexpected error code class=%d code=%d\n",
518 (int)cclass, (int)num);
519 printf(" expected %d/%d %s (line=%d)\n",
520 (int)eclass, (int)ecode, nt_errstr(nterr), line);
521 return False;
524 } else {
525 NTSTATUS status;
527 /* Check NT error */
529 status = cli_nt_error(c);
531 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
532 printf("unexpected error code %s\n", nt_errstr(status));
533 printf(" expected %s (line=%d)\n", nt_errstr(nterr), line);
534 return False;
538 return True;
542 static bool wait_lock(struct cli_state *c, int fnum, uint32 offset, uint32 len)
544 while (!cli_lock(c, fnum, offset, len, -1, WRITE_LOCK)) {
545 if (!check_error(__LINE__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
547 return True;
551 static bool rw_torture(struct cli_state *c)
553 const char *lockfname = "\\torture.lck";
554 fstring fname;
555 uint16_t fnum;
556 uint16_t fnum2;
557 pid_t pid2, pid = getpid();
558 int i, j;
559 char buf[1024];
560 bool correct = True;
561 NTSTATUS status;
563 memset(buf, '\0', sizeof(buf));
565 status = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
566 DENY_NONE, &fnum2);
567 if (!NT_STATUS_IS_OK(status)) {
568 status = cli_open(c, lockfname, O_RDWR, DENY_NONE, &fnum2);
570 if (!NT_STATUS_IS_OK(status)) {
571 printf("open of %s failed (%s)\n", lockfname, cli_errstr(c));
572 return False;
575 for (i=0;i<torture_numops;i++) {
576 unsigned n = (unsigned)sys_random()%10;
577 if (i % 10 == 0) {
578 printf("%d\r", i); fflush(stdout);
580 slprintf(fname, sizeof(fstring) - 1, "\\torture.%u", n);
582 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
583 return False;
586 if (!NT_STATUS_IS_OK(cli_open(c, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL, &fnum))) {
587 printf("open failed (%s)\n", cli_errstr(c));
588 correct = False;
589 break;
592 if (cli_write(c, fnum, 0, (char *)&pid, 0, sizeof(pid)) != sizeof(pid)) {
593 printf("write failed (%s)\n", cli_errstr(c));
594 correct = False;
597 for (j=0;j<50;j++) {
598 if (cli_write(c, fnum, 0, (char *)buf,
599 sizeof(pid)+(j*sizeof(buf)),
600 sizeof(buf)) != sizeof(buf)) {
601 printf("write failed (%s)\n", cli_errstr(c));
602 correct = False;
606 pid2 = 0;
608 if (cli_read(c, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
609 printf("read failed (%s)\n", cli_errstr(c));
610 correct = False;
613 if (pid2 != pid) {
614 printf("data corruption!\n");
615 correct = False;
618 if (!NT_STATUS_IS_OK(cli_close(c, fnum))) {
619 printf("close failed (%s)\n", cli_errstr(c));
620 correct = False;
623 if (!NT_STATUS_IS_OK(cli_unlink(c, fname, aSYSTEM | aHIDDEN))) {
624 printf("unlink failed (%s)\n", cli_errstr(c));
625 correct = False;
628 if (!NT_STATUS_IS_OK(cli_unlock(c, fnum2, n*sizeof(int), sizeof(int)))) {
629 printf("unlock failed (%s)\n", cli_errstr(c));
630 correct = False;
634 cli_close(c, fnum2);
635 cli_unlink(c, lockfname, aSYSTEM | aHIDDEN);
637 printf("%d\n", i);
639 return correct;
642 static bool run_torture(int dummy)
644 struct cli_state *cli;
645 bool ret;
647 cli = current_cli;
649 cli_sockopt(cli, sockops);
651 ret = rw_torture(cli);
653 if (!torture_close_connection(cli)) {
654 ret = False;
657 return ret;
660 static bool rw_torture3(struct cli_state *c, char *lockfname)
662 uint16_t fnum = (uint16_t)-1;
663 unsigned int i = 0;
664 char buf[131072];
665 char buf_rd[131072];
666 unsigned count;
667 unsigned countprev = 0;
668 ssize_t sent = 0;
669 bool correct = True;
670 NTSTATUS status = NT_STATUS_OK;
672 srandom(1);
673 for (i = 0; i < sizeof(buf); i += sizeof(uint32))
675 SIVAL(buf, i, sys_random());
678 if (procnum == 0)
680 if (!NT_STATUS_IS_OK(cli_unlink(c, lockfname, aSYSTEM | aHIDDEN))) {
681 printf("unlink failed (%s) (normal, this file should not exist)\n", cli_errstr(c));
684 if (!NT_STATUS_IS_OK(cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
685 DENY_NONE, &fnum))) {
686 printf("first open read/write of %s failed (%s)\n",
687 lockfname, cli_errstr(c));
688 return False;
691 else
693 for (i = 0; i < 500 && fnum == (uint16_t)-1; i++)
695 status = cli_open(c, lockfname, O_RDONLY,
696 DENY_NONE, &fnum);
697 if (!NT_STATUS_IS_OK(status)) {
698 break;
700 smb_msleep(10);
702 if (!NT_STATUS_IS_OK(status)) {
703 printf("second open read-only of %s failed (%s)\n",
704 lockfname, cli_errstr(c));
705 return False;
709 i = 0;
710 for (count = 0; count < sizeof(buf); count += sent)
712 if (count >= countprev) {
713 printf("%d %8d\r", i, count);
714 fflush(stdout);
715 i++;
716 countprev += (sizeof(buf) / 20);
719 if (procnum == 0)
721 sent = ((unsigned)sys_random()%(20))+ 1;
722 if (sent > sizeof(buf) - count)
724 sent = sizeof(buf) - count;
727 if (cli_write(c, fnum, 0, buf+count, count, (size_t)sent) != sent) {
728 printf("write failed (%s)\n", cli_errstr(c));
729 correct = False;
732 else
734 sent = cli_read(c, fnum, buf_rd+count, count,
735 sizeof(buf)-count);
736 if (sent < 0)
738 printf("read failed offset:%d size:%ld (%s)\n",
739 count, (unsigned long)sizeof(buf)-count,
740 cli_errstr(c));
741 correct = False;
742 sent = 0;
744 if (sent > 0)
746 if (memcmp(buf_rd+count, buf+count, sent) != 0)
748 printf("read/write compare failed\n");
749 printf("offset: %d req %ld recvd %ld\n", count, (unsigned long)sizeof(buf)-count, (unsigned long)sent);
750 correct = False;
751 break;
758 if (!NT_STATUS_IS_OK(cli_close(c, fnum))) {
759 printf("close failed (%s)\n", cli_errstr(c));
760 correct = False;
763 return correct;
766 static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
768 const char *lockfname = "\\torture2.lck";
769 uint16_t fnum1;
770 uint16_t fnum2;
771 int i;
772 char buf[131072];
773 char buf_rd[131072];
774 bool correct = True;
775 ssize_t bytes_read;
777 if (!NT_STATUS_IS_OK(cli_unlink(c1, lockfname, aSYSTEM | aHIDDEN))) {
778 printf("unlink failed (%s) (normal, this file should not exist)\n", cli_errstr(c1));
781 if (!NT_STATUS_IS_OK(cli_open(c1, lockfname, O_RDWR | O_CREAT | O_EXCL,
782 DENY_NONE, &fnum1))) {
783 printf("first open read/write of %s failed (%s)\n",
784 lockfname, cli_errstr(c1));
785 return False;
787 if (!NT_STATUS_IS_OK(cli_open(c2, lockfname, O_RDONLY,
788 DENY_NONE, &fnum2))) {
789 printf("second open read-only of %s failed (%s)\n",
790 lockfname, cli_errstr(c2));
791 cli_close(c1, fnum1);
792 return False;
795 for (i=0;i<torture_numops;i++)
797 size_t buf_size = ((unsigned)sys_random()%(sizeof(buf)-1))+ 1;
798 if (i % 10 == 0) {
799 printf("%d\r", i); fflush(stdout);
802 generate_random_buffer((unsigned char *)buf, buf_size);
804 if (cli_write(c1, fnum1, 0, buf, 0, buf_size) != buf_size) {
805 printf("write failed (%s)\n", cli_errstr(c1));
806 correct = False;
807 break;
810 if ((bytes_read = cli_read(c2, fnum2, buf_rd, 0, buf_size)) != buf_size) {
811 printf("read failed (%s)\n", cli_errstr(c2));
812 printf("read %d, expected %ld\n", (int)bytes_read,
813 (unsigned long)buf_size);
814 correct = False;
815 break;
818 if (memcmp(buf_rd, buf, buf_size) != 0)
820 printf("read/write compare failed\n");
821 correct = False;
822 break;
826 if (!NT_STATUS_IS_OK(cli_close(c2, fnum2))) {
827 printf("close failed (%s)\n", cli_errstr(c2));
828 correct = False;
830 if (!NT_STATUS_IS_OK(cli_close(c1, fnum1))) {
831 printf("close failed (%s)\n", cli_errstr(c1));
832 correct = False;
835 if (!NT_STATUS_IS_OK(cli_unlink(c1, lockfname, aSYSTEM | aHIDDEN))) {
836 printf("unlink failed (%s)\n", cli_errstr(c1));
837 correct = False;
840 return correct;
843 static bool run_readwritetest(int dummy)
845 struct cli_state *cli1, *cli2;
846 bool test1, test2 = False;
848 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
849 return False;
851 cli_sockopt(cli1, sockops);
852 cli_sockopt(cli2, sockops);
854 printf("starting readwritetest\n");
856 test1 = rw_torture2(cli1, cli2);
857 printf("Passed readwritetest v1: %s\n", BOOLSTR(test1));
859 if (test1) {
860 test2 = rw_torture2(cli1, cli1);
861 printf("Passed readwritetest v2: %s\n", BOOLSTR(test2));
864 if (!torture_close_connection(cli1)) {
865 test1 = False;
868 if (!torture_close_connection(cli2)) {
869 test2 = False;
872 return (test1 && test2);
875 static bool run_readwritemulti(int dummy)
877 struct cli_state *cli;
878 bool test;
880 cli = current_cli;
882 cli_sockopt(cli, sockops);
884 printf("run_readwritemulti: fname %s\n", randomfname);
885 test = rw_torture3(cli, randomfname);
887 if (!torture_close_connection(cli)) {
888 test = False;
891 return test;
894 static bool run_readwritelarge_internal(int max_xmit_k)
896 static struct cli_state *cli1;
897 uint16_t fnum1;
898 const char *lockfname = "\\large.dat";
899 SMB_OFF_T fsize;
900 char buf[126*1024];
901 bool correct = True;
903 if (!torture_open_connection(&cli1, 0)) {
904 return False;
906 cli_sockopt(cli1, sockops);
907 memset(buf,'\0',sizeof(buf));
909 cli1->max_xmit = max_xmit_k*1024;
911 if (signing_state == Required) {
912 /* Horrible cheat to force
913 multiple signed outstanding
914 packets against a Samba server.
916 cli1->is_samba = false;
919 printf("starting readwritelarge_internal\n");
921 cli_unlink(cli1, lockfname, aSYSTEM | aHIDDEN);
923 if (!NT_STATUS_IS_OK(cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE, &fnum1))) {
924 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
925 return False;
928 cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf));
930 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
931 cli1, fnum1, NULL, &fsize, NULL, NULL,
932 NULL, NULL, NULL))) {
933 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
934 correct = False;
937 if (fsize == sizeof(buf))
938 printf("readwritelarge_internal test 1 succeeded (size = %lx)\n",
939 (unsigned long)fsize);
940 else {
941 printf("readwritelarge_internal test 1 failed (size = %lx)\n",
942 (unsigned long)fsize);
943 correct = False;
946 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
947 printf("close failed (%s)\n", cli_errstr(cli1));
948 correct = False;
951 if (!NT_STATUS_IS_OK(cli_unlink(cli1, lockfname, aSYSTEM | aHIDDEN))) {
952 printf("unlink failed (%s)\n", cli_errstr(cli1));
953 correct = False;
956 if (!NT_STATUS_IS_OK(cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE, &fnum1))) {
957 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
958 return False;
961 cli1->max_xmit = 4*1024;
963 cli_smbwrite(cli1, fnum1, buf, 0, sizeof(buf));
965 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
966 cli1, fnum1, NULL, &fsize, NULL, NULL,
967 NULL, NULL, NULL))) {
968 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
969 correct = False;
972 if (fsize == sizeof(buf))
973 printf("readwritelarge_internal test 2 succeeded (size = %lx)\n",
974 (unsigned long)fsize);
975 else {
976 printf("readwritelarge_internal test 2 failed (size = %lx)\n",
977 (unsigned long)fsize);
978 correct = False;
981 #if 0
982 /* ToDo - set allocation. JRA */
983 if(!cli_set_allocation_size(cli1, fnum1, 0)) {
984 printf("set allocation size to zero failed (%s)\n", cli_errstr(&cli1));
985 return False;
987 if (!cli_qfileinfo_basic(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL,
988 NULL, NULL)) {
989 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
990 correct = False;
992 if (fsize != 0)
993 printf("readwritelarge test 3 (truncate test) succeeded (size = %x)\n", fsize);
994 #endif
996 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
997 printf("close failed (%s)\n", cli_errstr(cli1));
998 correct = False;
1001 if (!torture_close_connection(cli1)) {
1002 correct = False;
1004 return correct;
1007 static bool run_readwritelarge(int dummy)
1009 return run_readwritelarge_internal(128);
1012 static bool run_readwritelarge_signtest(int dummy)
1014 bool ret;
1015 signing_state = Required;
1016 ret = run_readwritelarge_internal(2);
1017 signing_state = Undefined;
1018 return ret;
1021 int line_count = 0;
1022 int nbio_id;
1024 #define ival(s) strtol(s, NULL, 0)
1026 /* run a test that simulates an approximate netbench client load */
1027 static bool run_netbench(int client)
1029 struct cli_state *cli;
1030 int i;
1031 char line[1024];
1032 char cname[20];
1033 FILE *f;
1034 const char *params[20];
1035 bool correct = True;
1037 cli = current_cli;
1039 nbio_id = client;
1041 cli_sockopt(cli, sockops);
1043 nb_setup(cli);
1045 slprintf(cname,sizeof(cname)-1, "client%d", client);
1047 f = fopen(client_txt, "r");
1049 if (!f) {
1050 perror(client_txt);
1051 return False;
1054 while (fgets(line, sizeof(line)-1, f)) {
1055 char *saveptr;
1056 line_count++;
1058 line[strlen(line)-1] = 0;
1060 /* printf("[%d] %s\n", line_count, line); */
1062 all_string_sub(line,"client1", cname, sizeof(line));
1064 /* parse the command parameters */
1065 params[0] = strtok_r(line, " ", &saveptr);
1066 i = 0;
1067 while (params[i]) params[++i] = strtok_r(NULL, " ", &saveptr);
1069 params[i] = "";
1071 if (i < 2) continue;
1073 if (!strncmp(params[0],"SMB", 3)) {
1074 printf("ERROR: You are using a dbench 1 load file\n");
1075 exit(1);
1078 if (!strcmp(params[0],"NTCreateX")) {
1079 nb_createx(params[1], ival(params[2]), ival(params[3]),
1080 ival(params[4]));
1081 } else if (!strcmp(params[0],"Close")) {
1082 nb_close(ival(params[1]));
1083 } else if (!strcmp(params[0],"Rename")) {
1084 nb_rename(params[1], params[2]);
1085 } else if (!strcmp(params[0],"Unlink")) {
1086 nb_unlink(params[1]);
1087 } else if (!strcmp(params[0],"Deltree")) {
1088 nb_deltree(params[1]);
1089 } else if (!strcmp(params[0],"Rmdir")) {
1090 nb_rmdir(params[1]);
1091 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
1092 nb_qpathinfo(params[1]);
1093 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
1094 nb_qfileinfo(ival(params[1]));
1095 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
1096 nb_qfsinfo(ival(params[1]));
1097 } else if (!strcmp(params[0],"FIND_FIRST")) {
1098 nb_findfirst(params[1]);
1099 } else if (!strcmp(params[0],"WriteX")) {
1100 nb_writex(ival(params[1]),
1101 ival(params[2]), ival(params[3]), ival(params[4]));
1102 } else if (!strcmp(params[0],"ReadX")) {
1103 nb_readx(ival(params[1]),
1104 ival(params[2]), ival(params[3]), ival(params[4]));
1105 } else if (!strcmp(params[0],"Flush")) {
1106 nb_flush(ival(params[1]));
1107 } else {
1108 printf("Unknown operation %s\n", params[0]);
1109 exit(1);
1112 fclose(f);
1114 nb_cleanup();
1116 if (!torture_close_connection(cli)) {
1117 correct = False;
1120 return correct;
1124 /* run a test that simulates an approximate netbench client load */
1125 static bool run_nbench(int dummy)
1127 double t;
1128 bool correct = True;
1130 nbio_shmem(nprocs);
1132 nbio_id = -1;
1134 signal(SIGALRM, nb_alarm);
1135 alarm(1);
1136 t = create_procs(run_netbench, &correct);
1137 alarm(0);
1139 printf("\nThroughput %g MB/sec\n",
1140 1.0e-6 * nbio_total() / t);
1141 return correct;
1146 This test checks for two things:
1148 1) correct support for retaining locks over a close (ie. the server
1149 must not use posix semantics)
1150 2) support for lock timeouts
1152 static bool run_locktest1(int dummy)
1154 struct cli_state *cli1, *cli2;
1155 const char *fname = "\\lockt1.lck";
1156 uint16_t fnum1, fnum2, fnum3;
1157 time_t t1, t2;
1158 unsigned lock_timeout;
1160 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1161 return False;
1163 cli_sockopt(cli1, sockops);
1164 cli_sockopt(cli2, sockops);
1166 printf("starting locktest1\n");
1168 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1170 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1171 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1172 return False;
1174 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum2))) {
1175 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli1));
1176 return False;
1178 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum3))) {
1179 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli2));
1180 return False;
1183 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
1184 printf("lock1 failed (%s)\n", cli_errstr(cli1));
1185 return False;
1189 if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1190 printf("lock2 succeeded! This is a locking bug\n");
1191 return False;
1192 } else {
1193 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1194 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1198 lock_timeout = (1 + (random() % 20));
1199 printf("Testing lock timeout with timeout=%u\n", lock_timeout);
1200 t1 = time(NULL);
1201 if (cli_lock(cli2, fnum3, 0, 4, lock_timeout * 1000, WRITE_LOCK)) {
1202 printf("lock3 succeeded! This is a locking bug\n");
1203 return False;
1204 } else {
1205 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1206 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1208 t2 = time(NULL);
1210 if (ABS(t2 - t1) < lock_timeout-1) {
1211 printf("error: This server appears not to support timed lock requests\n");
1214 printf("server slept for %u seconds for a %u second timeout\n",
1215 (unsigned int)(t2-t1), lock_timeout);
1217 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
1218 printf("close1 failed (%s)\n", cli_errstr(cli1));
1219 return False;
1222 if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1223 printf("lock4 succeeded! This is a locking bug\n");
1224 return False;
1225 } else {
1226 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1227 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1230 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
1231 printf("close2 failed (%s)\n", cli_errstr(cli1));
1232 return False;
1235 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum3))) {
1236 printf("close3 failed (%s)\n", cli_errstr(cli2));
1237 return False;
1240 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
1241 printf("unlink failed (%s)\n", cli_errstr(cli1));
1242 return False;
1246 if (!torture_close_connection(cli1)) {
1247 return False;
1250 if (!torture_close_connection(cli2)) {
1251 return False;
1254 printf("Passed locktest1\n");
1255 return True;
1259 this checks to see if a secondary tconx can use open files from an
1260 earlier tconx
1262 static bool run_tcon_test(int dummy)
1264 static struct cli_state *cli;
1265 const char *fname = "\\tcontest.tmp";
1266 uint16 fnum1;
1267 uint16 cnum1, cnum2, cnum3;
1268 uint16 vuid1, vuid2;
1269 char buf[4];
1270 bool ret = True;
1271 NTSTATUS status;
1273 memset(buf, '\0', sizeof(buf));
1275 if (!torture_open_connection(&cli, 0)) {
1276 return False;
1278 cli_sockopt(cli, sockops);
1280 printf("starting tcontest\n");
1282 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
1284 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1285 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1286 return False;
1289 cnum1 = cli->cnum;
1290 vuid1 = cli->vuid;
1292 if (cli_write(cli, fnum1, 0, buf, 130, 4) != 4) {
1293 printf("initial write failed (%s)", cli_errstr(cli));
1294 return False;
1297 status = cli_tcon_andx(cli, share, "?????",
1298 password, strlen(password)+1);
1299 if (!NT_STATUS_IS_OK(status)) {
1300 printf("%s refused 2nd tree connect (%s)\n", host,
1301 nt_errstr(status));
1302 cli_shutdown(cli);
1303 return False;
1306 cnum2 = cli->cnum;
1307 cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
1308 vuid2 = cli->vuid + 1;
1310 /* try a write with the wrong tid */
1311 cli->cnum = cnum2;
1313 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1314 printf("* server allows write with wrong TID\n");
1315 ret = False;
1316 } else {
1317 printf("server fails write with wrong TID : %s\n", cli_errstr(cli));
1321 /* try a write with an invalid tid */
1322 cli->cnum = cnum3;
1324 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1325 printf("* server allows write with invalid TID\n");
1326 ret = False;
1327 } else {
1328 printf("server fails write with invalid TID : %s\n", cli_errstr(cli));
1331 /* try a write with an invalid vuid */
1332 cli->vuid = vuid2;
1333 cli->cnum = cnum1;
1335 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1336 printf("* server allows write with invalid VUID\n");
1337 ret = False;
1338 } else {
1339 printf("server fails write with invalid VUID : %s\n", cli_errstr(cli));
1342 cli->cnum = cnum1;
1343 cli->vuid = vuid1;
1345 if (!NT_STATUS_IS_OK(cli_close(cli, fnum1))) {
1346 printf("close failed (%s)\n", cli_errstr(cli));
1347 return False;
1350 cli->cnum = cnum2;
1352 status = cli_tdis(cli);
1353 if (!NT_STATUS_IS_OK(status)) {
1354 printf("secondary tdis failed (%s)\n", nt_errstr(status));
1355 return False;
1358 cli->cnum = cnum1;
1360 if (!torture_close_connection(cli)) {
1361 return False;
1364 return ret;
1369 checks for old style tcon support
1371 static bool run_tcon2_test(int dummy)
1373 static struct cli_state *cli;
1374 uint16 cnum, max_xmit;
1375 char *service;
1376 NTSTATUS status;
1378 if (!torture_open_connection(&cli, 0)) {
1379 return False;
1381 cli_sockopt(cli, sockops);
1383 printf("starting tcon2 test\n");
1385 if (asprintf(&service, "\\\\%s\\%s", host, share) == -1) {
1386 return false;
1389 status = cli_raw_tcon(cli, service, password, "?????", &max_xmit, &cnum);
1391 if (!NT_STATUS_IS_OK(status)) {
1392 printf("tcon2 failed : %s\n", cli_errstr(cli));
1393 } else {
1394 printf("tcon OK : max_xmit=%d cnum=%d tid=%d\n",
1395 (int)max_xmit, (int)cnum, SVAL(cli->inbuf, smb_tid));
1398 if (!torture_close_connection(cli)) {
1399 return False;
1402 printf("Passed tcon2 test\n");
1403 return True;
1406 static bool tcon_devtest(struct cli_state *cli,
1407 const char *myshare, const char *devtype,
1408 const char *return_devtype,
1409 NTSTATUS expected_error)
1411 NTSTATUS status;
1412 bool ret;
1414 status = cli_tcon_andx(cli, myshare, devtype,
1415 password, strlen(password)+1);
1417 if (NT_STATUS_IS_OK(expected_error)) {
1418 if (NT_STATUS_IS_OK(status)) {
1419 if (strcmp(cli->dev, return_devtype) == 0) {
1420 ret = True;
1421 } else {
1422 printf("tconX to share %s with type %s "
1423 "succeeded but returned the wrong "
1424 "device type (got [%s] but should have got [%s])\n",
1425 myshare, devtype, cli->dev, return_devtype);
1426 ret = False;
1428 } else {
1429 printf("tconX to share %s with type %s "
1430 "should have succeeded but failed\n",
1431 myshare, devtype);
1432 ret = False;
1434 cli_tdis(cli);
1435 } else {
1436 if (NT_STATUS_IS_OK(status)) {
1437 printf("tconx to share %s with type %s "
1438 "should have failed but succeeded\n",
1439 myshare, devtype);
1440 ret = False;
1441 } else {
1442 if (NT_STATUS_EQUAL(cli_nt_error(cli),
1443 expected_error)) {
1444 ret = True;
1445 } else {
1446 printf("Returned unexpected error\n");
1447 ret = False;
1451 return ret;
1455 checks for correct tconX support
1457 static bool run_tcon_devtype_test(int dummy)
1459 static struct cli_state *cli1 = NULL;
1460 int flags = 0;
1461 NTSTATUS status;
1462 bool ret = True;
1464 status = cli_full_connection(&cli1, myname,
1465 host, NULL, port_to_use,
1466 NULL, NULL,
1467 username, workgroup,
1468 password, flags, signing_state);
1470 if (!NT_STATUS_IS_OK(status)) {
1471 printf("could not open connection\n");
1472 return False;
1475 if (!tcon_devtest(cli1, "IPC$", "A:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1476 ret = False;
1478 if (!tcon_devtest(cli1, "IPC$", "?????", "IPC", NT_STATUS_OK))
1479 ret = False;
1481 if (!tcon_devtest(cli1, "IPC$", "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1482 ret = False;
1484 if (!tcon_devtest(cli1, "IPC$", "IPC", "IPC", NT_STATUS_OK))
1485 ret = False;
1487 if (!tcon_devtest(cli1, "IPC$", "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1488 ret = False;
1490 if (!tcon_devtest(cli1, share, "A:", "A:", NT_STATUS_OK))
1491 ret = False;
1493 if (!tcon_devtest(cli1, share, "?????", "A:", NT_STATUS_OK))
1494 ret = False;
1496 if (!tcon_devtest(cli1, share, "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1497 ret = False;
1499 if (!tcon_devtest(cli1, share, "IPC", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1500 ret = False;
1502 if (!tcon_devtest(cli1, share, "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1503 ret = False;
1505 cli_shutdown(cli1);
1507 if (ret)
1508 printf("Passed tcondevtest\n");
1510 return ret;
1515 This test checks that
1517 1) the server supports multiple locking contexts on the one SMB
1518 connection, distinguished by PID.
1520 2) the server correctly fails overlapping locks made by the same PID (this
1521 goes against POSIX behaviour, which is why it is tricky to implement)
1523 3) the server denies unlock requests by an incorrect client PID
1525 static bool run_locktest2(int dummy)
1527 static struct cli_state *cli;
1528 const char *fname = "\\lockt2.lck";
1529 uint16_t fnum1, fnum2, fnum3;
1530 bool correct = True;
1532 if (!torture_open_connection(&cli, 0)) {
1533 return False;
1536 cli_sockopt(cli, sockops);
1538 printf("starting locktest2\n");
1540 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
1542 cli_setpid(cli, 1);
1544 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1545 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1546 return False;
1549 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum2))) {
1550 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli));
1551 return False;
1554 cli_setpid(cli, 2);
1556 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum3))) {
1557 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli));
1558 return False;
1561 cli_setpid(cli, 1);
1563 if (!cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1564 printf("lock1 failed (%s)\n", cli_errstr(cli));
1565 return False;
1568 if (cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1569 printf("WRITE lock1 succeeded! This is a locking bug\n");
1570 correct = False;
1571 } else {
1572 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1573 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1576 if (cli_lock(cli, fnum2, 0, 4, 0, WRITE_LOCK)) {
1577 printf("WRITE lock2 succeeded! This is a locking bug\n");
1578 correct = False;
1579 } else {
1580 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1581 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1584 if (cli_lock(cli, fnum2, 0, 4, 0, READ_LOCK)) {
1585 printf("READ lock2 succeeded! This is a locking bug\n");
1586 correct = False;
1587 } else {
1588 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1589 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1592 if (!cli_lock(cli, fnum1, 100, 4, 0, WRITE_LOCK)) {
1593 printf("lock at 100 failed (%s)\n", cli_errstr(cli));
1595 cli_setpid(cli, 2);
1596 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 100, 4))) {
1597 printf("unlock at 100 succeeded! This is a locking bug\n");
1598 correct = False;
1601 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 0, 4))) {
1602 printf("unlock1 succeeded! This is a locking bug\n");
1603 correct = False;
1604 } else {
1605 if (!check_error(__LINE__, cli,
1606 ERRDOS, ERRlock,
1607 NT_STATUS_RANGE_NOT_LOCKED)) return False;
1610 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 0, 8))) {
1611 printf("unlock2 succeeded! This is a locking bug\n");
1612 correct = False;
1613 } else {
1614 if (!check_error(__LINE__, cli,
1615 ERRDOS, ERRlock,
1616 NT_STATUS_RANGE_NOT_LOCKED)) return False;
1619 if (cli_lock(cli, fnum3, 0, 4, 0, WRITE_LOCK)) {
1620 printf("lock3 succeeded! This is a locking bug\n");
1621 correct = False;
1622 } else {
1623 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
1626 cli_setpid(cli, 1);
1628 if (!NT_STATUS_IS_OK(cli_close(cli, fnum1))) {
1629 printf("close1 failed (%s)\n", cli_errstr(cli));
1630 return False;
1633 if (!NT_STATUS_IS_OK(cli_close(cli, fnum2))) {
1634 printf("close2 failed (%s)\n", cli_errstr(cli));
1635 return False;
1638 if (!NT_STATUS_IS_OK(cli_close(cli, fnum3))) {
1639 printf("close3 failed (%s)\n", cli_errstr(cli));
1640 return False;
1643 if (!torture_close_connection(cli)) {
1644 correct = False;
1647 printf("locktest2 finished\n");
1649 return correct;
1654 This test checks that
1656 1) the server supports the full offset range in lock requests
1658 static bool run_locktest3(int dummy)
1660 static struct cli_state *cli1, *cli2;
1661 const char *fname = "\\lockt3.lck";
1662 uint16_t fnum1, fnum2;
1663 int i;
1664 uint32 offset;
1665 bool correct = True;
1667 #define NEXT_OFFSET offset += (~(uint32)0) / torture_numops
1669 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1670 return False;
1672 cli_sockopt(cli1, sockops);
1673 cli_sockopt(cli2, sockops);
1675 printf("starting locktest3\n");
1677 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1679 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1680 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1681 return False;
1683 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2))) {
1684 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli2));
1685 return False;
1688 for (offset=i=0;i<torture_numops;i++) {
1689 NEXT_OFFSET;
1690 if (!cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1691 printf("lock1 %d failed (%s)\n",
1693 cli_errstr(cli1));
1694 return False;
1697 if (!cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1698 printf("lock2 %d failed (%s)\n",
1700 cli_errstr(cli1));
1701 return False;
1705 for (offset=i=0;i<torture_numops;i++) {
1706 NEXT_OFFSET;
1708 if (cli_lock(cli1, fnum1, offset-2, 1, 0, WRITE_LOCK)) {
1709 printf("error: lock1 %d succeeded!\n", i);
1710 return False;
1713 if (cli_lock(cli2, fnum2, offset-1, 1, 0, WRITE_LOCK)) {
1714 printf("error: lock2 %d succeeded!\n", i);
1715 return False;
1718 if (cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1719 printf("error: lock3 %d succeeded!\n", i);
1720 return False;
1723 if (cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1724 printf("error: lock4 %d succeeded!\n", i);
1725 return False;
1729 for (offset=i=0;i<torture_numops;i++) {
1730 NEXT_OFFSET;
1732 if (!NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, offset-1, 1))) {
1733 printf("unlock1 %d failed (%s)\n",
1735 cli_errstr(cli1));
1736 return False;
1739 if (!NT_STATUS_IS_OK(cli_unlock(cli2, fnum2, offset-2, 1))) {
1740 printf("unlock2 %d failed (%s)\n",
1742 cli_errstr(cli1));
1743 return False;
1747 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
1748 printf("close1 failed (%s)\n", cli_errstr(cli1));
1749 return False;
1752 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
1753 printf("close2 failed (%s)\n", cli_errstr(cli2));
1754 return False;
1757 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
1758 printf("unlink failed (%s)\n", cli_errstr(cli1));
1759 return False;
1762 if (!torture_close_connection(cli1)) {
1763 correct = False;
1766 if (!torture_close_connection(cli2)) {
1767 correct = False;
1770 printf("finished locktest3\n");
1772 return correct;
1775 #define EXPECTED(ret, v) if ((ret) != (v)) { \
1776 printf("** "); correct = False; \
1780 looks at overlapping locks
1782 static bool run_locktest4(int dummy)
1784 static struct cli_state *cli1, *cli2;
1785 const char *fname = "\\lockt4.lck";
1786 uint16_t fnum1, fnum2, f;
1787 bool ret;
1788 char buf[1000];
1789 bool correct = True;
1791 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1792 return False;
1795 cli_sockopt(cli1, sockops);
1796 cli_sockopt(cli2, sockops);
1798 printf("starting locktest4\n");
1800 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1802 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1803 cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
1805 memset(buf, 0, sizeof(buf));
1807 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1808 printf("Failed to create file\n");
1809 correct = False;
1810 goto fail;
1813 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1814 cli_lock(cli1, fnum1, 2, 4, 0, WRITE_LOCK);
1815 EXPECTED(ret, False);
1816 printf("the same process %s set overlapping write locks\n", ret?"can":"cannot");
1818 ret = cli_lock(cli1, fnum1, 10, 4, 0, READ_LOCK) &&
1819 cli_lock(cli1, fnum1, 12, 4, 0, READ_LOCK);
1820 EXPECTED(ret, True);
1821 printf("the same process %s set overlapping read locks\n", ret?"can":"cannot");
1823 ret = cli_lock(cli1, fnum1, 20, 4, 0, WRITE_LOCK) &&
1824 cli_lock(cli2, fnum2, 22, 4, 0, WRITE_LOCK);
1825 EXPECTED(ret, False);
1826 printf("a different connection %s set overlapping write locks\n", ret?"can":"cannot");
1828 ret = cli_lock(cli1, fnum1, 30, 4, 0, READ_LOCK) &&
1829 cli_lock(cli2, fnum2, 32, 4, 0, READ_LOCK);
1830 EXPECTED(ret, True);
1831 printf("a different connection %s set overlapping read locks\n", ret?"can":"cannot");
1833 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 40, 4, 0, WRITE_LOCK)) &&
1834 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 42, 4, 0, WRITE_LOCK));
1835 EXPECTED(ret, False);
1836 printf("a different pid %s set overlapping write locks\n", ret?"can":"cannot");
1838 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 50, 4, 0, READ_LOCK)) &&
1839 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 52, 4, 0, READ_LOCK));
1840 EXPECTED(ret, True);
1841 printf("a different pid %s set overlapping read locks\n", ret?"can":"cannot");
1843 ret = cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK) &&
1844 cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK);
1845 EXPECTED(ret, True);
1846 printf("the same process %s set the same read lock twice\n", ret?"can":"cannot");
1848 ret = cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK) &&
1849 cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK);
1850 EXPECTED(ret, False);
1851 printf("the same process %s set the same write lock twice\n", ret?"can":"cannot");
1853 ret = cli_lock(cli1, fnum1, 80, 4, 0, READ_LOCK) &&
1854 cli_lock(cli1, fnum1, 80, 4, 0, WRITE_LOCK);
1855 EXPECTED(ret, False);
1856 printf("the same process %s overlay a read lock with a write lock\n", ret?"can":"cannot");
1858 ret = cli_lock(cli1, fnum1, 90, 4, 0, WRITE_LOCK) &&
1859 cli_lock(cli1, fnum1, 90, 4, 0, READ_LOCK);
1860 EXPECTED(ret, True);
1861 printf("the same process %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1863 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 100, 4, 0, WRITE_LOCK)) &&
1864 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 100, 4, 0, READ_LOCK));
1865 EXPECTED(ret, False);
1866 printf("a different pid %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1868 ret = cli_lock(cli1, fnum1, 110, 4, 0, READ_LOCK) &&
1869 cli_lock(cli1, fnum1, 112, 4, 0, READ_LOCK) &&
1870 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 110, 6));
1871 EXPECTED(ret, False);
1872 printf("the same process %s coalesce read locks\n", ret?"can":"cannot");
1875 ret = cli_lock(cli1, fnum1, 120, 4, 0, WRITE_LOCK) &&
1876 (cli_read(cli2, fnum2, buf, 120, 4) == 4);
1877 EXPECTED(ret, False);
1878 printf("this server %s strict write locking\n", ret?"doesn't do":"does");
1880 ret = cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK) &&
1881 (cli_write(cli2, fnum2, 0, buf, 130, 4) == 4);
1882 EXPECTED(ret, False);
1883 printf("this server %s strict read locking\n", ret?"doesn't do":"does");
1886 ret = cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1887 cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1888 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4)) &&
1889 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4));
1890 EXPECTED(ret, True);
1891 printf("this server %s do recursive read locking\n", ret?"does":"doesn't");
1894 ret = cli_lock(cli1, fnum1, 150, 4, 0, WRITE_LOCK) &&
1895 cli_lock(cli1, fnum1, 150, 4, 0, READ_LOCK) &&
1896 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4)) &&
1897 (cli_read(cli2, fnum2, buf, 150, 4) == 4) &&
1898 !(cli_write(cli2, fnum2, 0, buf, 150, 4) == 4) &&
1899 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4));
1900 EXPECTED(ret, True);
1901 printf("this server %s do recursive lock overlays\n", ret?"does":"doesn't");
1903 ret = cli_lock(cli1, fnum1, 160, 4, 0, READ_LOCK) &&
1904 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 160, 4)) &&
1905 (cli_write(cli2, fnum2, 0, buf, 160, 4) == 4) &&
1906 (cli_read(cli2, fnum2, buf, 160, 4) == 4);
1907 EXPECTED(ret, True);
1908 printf("the same process %s remove a read lock using write locking\n", ret?"can":"cannot");
1910 ret = cli_lock(cli1, fnum1, 170, 4, 0, WRITE_LOCK) &&
1911 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 170, 4)) &&
1912 (cli_write(cli2, fnum2, 0, buf, 170, 4) == 4) &&
1913 (cli_read(cli2, fnum2, buf, 170, 4) == 4);
1914 EXPECTED(ret, True);
1915 printf("the same process %s remove a write lock using read locking\n", ret?"can":"cannot");
1917 ret = cli_lock(cli1, fnum1, 190, 4, 0, WRITE_LOCK) &&
1918 cli_lock(cli1, fnum1, 190, 4, 0, READ_LOCK) &&
1919 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 190, 4)) &&
1920 !(cli_write(cli2, fnum2, 0, buf, 190, 4) == 4) &&
1921 (cli_read(cli2, fnum2, buf, 190, 4) == 4);
1922 EXPECTED(ret, True);
1923 printf("the same process %s remove the first lock first\n", ret?"does":"doesn't");
1925 cli_close(cli1, fnum1);
1926 cli_close(cli2, fnum2);
1927 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
1928 cli_open(cli1, fname, O_RDWR, DENY_NONE, &f);
1929 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1930 cli_lock(cli1, f, 0, 1, 0, READ_LOCK) &&
1931 NT_STATUS_IS_OK(cli_close(cli1, fnum1)) &&
1932 NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1)) &&
1933 cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1934 cli_close(cli1, f);
1935 cli_close(cli1, fnum1);
1936 EXPECTED(ret, True);
1937 printf("the server %s have the NT byte range lock bug\n", !ret?"does":"doesn't");
1939 fail:
1940 cli_close(cli1, fnum1);
1941 cli_close(cli2, fnum2);
1942 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1943 torture_close_connection(cli1);
1944 torture_close_connection(cli2);
1946 printf("finished locktest4\n");
1947 return correct;
1951 looks at lock upgrade/downgrade.
1953 static bool run_locktest5(int dummy)
1955 static struct cli_state *cli1, *cli2;
1956 const char *fname = "\\lockt5.lck";
1957 uint16_t fnum1, fnum2, fnum3;
1958 bool ret;
1959 char buf[1000];
1960 bool correct = True;
1962 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1963 return False;
1966 cli_sockopt(cli1, sockops);
1967 cli_sockopt(cli2, sockops);
1969 printf("starting locktest5\n");
1971 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1973 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1974 cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
1975 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum3);
1977 memset(buf, 0, sizeof(buf));
1979 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1980 printf("Failed to create file\n");
1981 correct = False;
1982 goto fail;
1985 /* Check for NT bug... */
1986 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1987 cli_lock(cli1, fnum3, 0, 1, 0, READ_LOCK);
1988 cli_close(cli1, fnum1);
1989 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
1990 ret = cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1991 EXPECTED(ret, True);
1992 printf("this server %s the NT locking bug\n", ret ? "doesn't have" : "has");
1993 cli_close(cli1, fnum1);
1994 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
1995 cli_unlock(cli1, fnum3, 0, 1);
1997 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1998 cli_lock(cli1, fnum1, 1, 1, 0, READ_LOCK);
1999 EXPECTED(ret, True);
2000 printf("the same process %s overlay a write with a read lock\n", ret?"can":"cannot");
2002 ret = cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
2003 EXPECTED(ret, False);
2005 printf("a different processs %s get a read lock on the first process lock stack\n", ret?"can":"cannot");
2007 /* Unlock the process 2 lock. */
2008 cli_unlock(cli2, fnum2, 0, 4);
2010 ret = cli_lock(cli1, fnum3, 0, 4, 0, READ_LOCK);
2011 EXPECTED(ret, False);
2013 printf("the same processs on a different fnum %s get a read lock\n", ret?"can":"cannot");
2015 /* Unlock the process 1 fnum3 lock. */
2016 cli_unlock(cli1, fnum3, 0, 4);
2018 /* Stack 2 more locks here. */
2019 ret = cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK) &&
2020 cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK);
2022 EXPECTED(ret, True);
2023 printf("the same process %s stack read locks\n", ret?"can":"cannot");
2025 /* Unlock the first process lock, then check this was the WRITE lock that was
2026 removed. */
2028 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4)) &&
2029 cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
2031 EXPECTED(ret, True);
2032 printf("the first unlock removes the %s lock\n", ret?"WRITE":"READ");
2034 /* Unlock the process 2 lock. */
2035 cli_unlock(cli2, fnum2, 0, 4);
2037 /* We should have 3 stacked locks here. Ensure we need to do 3 unlocks. */
2039 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 1, 1)) &&
2040 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4)) &&
2041 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4));
2043 EXPECTED(ret, True);
2044 printf("the same process %s unlock the stack of 4 locks\n", ret?"can":"cannot");
2046 /* Ensure the next unlock fails. */
2047 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4));
2048 EXPECTED(ret, False);
2049 printf("the same process %s count the lock stack\n", !ret?"can":"cannot");
2051 /* Ensure connection 2 can get a write lock. */
2052 ret = cli_lock(cli2, fnum2, 0, 4, 0, WRITE_LOCK);
2053 EXPECTED(ret, True);
2055 printf("a different processs %s get a write lock on the unlocked stack\n", ret?"can":"cannot");
2058 fail:
2059 cli_close(cli1, fnum1);
2060 cli_close(cli2, fnum2);
2061 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2062 if (!torture_close_connection(cli1)) {
2063 correct = False;
2065 if (!torture_close_connection(cli2)) {
2066 correct = False;
2069 printf("finished locktest5\n");
2071 return correct;
2075 tries the unusual lockingX locktype bits
2077 static bool run_locktest6(int dummy)
2079 static struct cli_state *cli;
2080 const char *fname[1] = { "\\lock6.txt" };
2081 int i;
2082 uint16_t fnum;
2083 NTSTATUS status;
2085 if (!torture_open_connection(&cli, 0)) {
2086 return False;
2089 cli_sockopt(cli, sockops);
2091 printf("starting locktest6\n");
2093 for (i=0;i<1;i++) {
2094 printf("Testing %s\n", fname[i]);
2096 cli_unlink(cli, fname[i], aSYSTEM | aHIDDEN);
2098 cli_open(cli, fname[i], O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
2099 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CHANGE_LOCKTYPE);
2100 cli_close(cli, fnum);
2101 printf("CHANGE_LOCKTYPE gave %s\n", nt_errstr(status));
2103 cli_open(cli, fname[i], O_RDWR, DENY_NONE, &fnum);
2104 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CANCEL_LOCK);
2105 cli_close(cli, fnum);
2106 printf("CANCEL_LOCK gave %s\n", nt_errstr(status));
2108 cli_unlink(cli, fname[i], aSYSTEM | aHIDDEN);
2111 torture_close_connection(cli);
2113 printf("finished locktest6\n");
2114 return True;
2117 static bool run_locktest7(int dummy)
2119 struct cli_state *cli1;
2120 const char *fname = "\\lockt7.lck";
2121 uint16_t fnum1;
2122 char buf[200];
2123 bool correct = False;
2125 if (!torture_open_connection(&cli1, 0)) {
2126 return False;
2129 cli_sockopt(cli1, sockops);
2131 printf("starting locktest7\n");
2133 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2135 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
2137 memset(buf, 0, sizeof(buf));
2139 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
2140 printf("Failed to create file\n");
2141 goto fail;
2144 cli_setpid(cli1, 1);
2146 if (!cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK)) {
2147 printf("Unable to apply read lock on range 130:4, error was %s\n", cli_errstr(cli1));
2148 goto fail;
2149 } else {
2150 printf("pid1 successfully locked range 130:4 for READ\n");
2153 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2154 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2155 goto fail;
2156 } else {
2157 printf("pid1 successfully read the range 130:4\n");
2160 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2161 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2162 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2163 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2164 goto fail;
2166 } else {
2167 printf("pid1 successfully wrote to the range 130:4 (should be denied)\n");
2168 goto fail;
2171 cli_setpid(cli1, 2);
2173 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2174 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2175 } else {
2176 printf("pid2 successfully read the range 130:4\n");
2179 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2180 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2181 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2182 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2183 goto fail;
2185 } else {
2186 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2187 goto fail;
2190 cli_setpid(cli1, 1);
2191 cli_unlock(cli1, fnum1, 130, 4);
2193 if (!cli_lock(cli1, fnum1, 130, 4, 0, WRITE_LOCK)) {
2194 printf("Unable to apply write lock on range 130:4, error was %s\n", cli_errstr(cli1));
2195 goto fail;
2196 } else {
2197 printf("pid1 successfully locked range 130:4 for WRITE\n");
2200 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2201 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2202 goto fail;
2203 } else {
2204 printf("pid1 successfully read the range 130:4\n");
2207 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2208 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2209 goto fail;
2210 } else {
2211 printf("pid1 successfully wrote to the range 130:4\n");
2214 cli_setpid(cli1, 2);
2216 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2217 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2218 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2219 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2220 goto fail;
2222 } else {
2223 printf("pid2 successfully read the range 130:4 (should be denied)\n");
2224 goto fail;
2227 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2228 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2229 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2230 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2231 goto fail;
2233 } else {
2234 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2235 goto fail;
2238 cli_unlock(cli1, fnum1, 130, 0);
2239 correct = True;
2241 fail:
2242 cli_close(cli1, fnum1);
2243 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2244 torture_close_connection(cli1);
2246 printf("finished locktest7\n");
2247 return correct;
2251 * This demonstrates a problem with our use of GPFS share modes: A file
2252 * descriptor sitting in the pending close queue holding a GPFS share mode
2253 * blocks opening a file another time. Happens with Word 2007 temp files.
2254 * With "posix locking = yes" and "gpfs:sharemodes = yes" enabled, the third
2255 * open is denied with NT_STATUS_SHARING_VIOLATION.
2258 static bool run_locktest8(int dummy)
2260 struct cli_state *cli1;
2261 const char *fname = "\\lockt8.lck";
2262 uint16_t fnum1, fnum2;
2263 char buf[200];
2264 bool correct = False;
2265 NTSTATUS status;
2267 if (!torture_open_connection(&cli1, 0)) {
2268 return False;
2271 cli_sockopt(cli1, sockops);
2273 printf("starting locktest8\n");
2275 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2277 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_WRITE,
2278 &fnum1);
2279 if (!NT_STATUS_IS_OK(status)) {
2280 d_fprintf(stderr, "cli_open returned %s\n", cli_errstr(cli1));
2281 return false;
2284 memset(buf, 0, sizeof(buf));
2286 status = cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum2);
2287 if (!NT_STATUS_IS_OK(status)) {
2288 d_fprintf(stderr, "cli_open second time returned %s\n",
2289 cli_errstr(cli1));
2290 goto fail;
2293 if (!cli_lock(cli1, fnum2, 1, 1, 0, READ_LOCK)) {
2294 printf("Unable to apply read lock on range 1:1, error was "
2295 "%s\n", cli_errstr(cli1));
2296 goto fail;
2299 status = cli_close(cli1, fnum1);
2300 if (!NT_STATUS_IS_OK(status)) {
2301 d_fprintf(stderr, "cli_close(fnum1) %s\n", cli_errstr(cli1));
2302 goto fail;
2305 status = cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
2306 if (!NT_STATUS_IS_OK(status)) {
2307 d_fprintf(stderr, "cli_open third time returned %s\n",
2308 cli_errstr(cli1));
2309 goto fail;
2312 correct = true;
2314 fail:
2315 cli_close(cli1, fnum1);
2316 cli_close(cli1, fnum2);
2317 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2318 torture_close_connection(cli1);
2320 printf("finished locktest8\n");
2321 return correct;
2325 * This test is designed to be run in conjunction with
2326 * external NFS or POSIX locks taken in the filesystem.
2327 * It checks that the smbd server will block until the
2328 * lock is released and then acquire it. JRA.
2331 static bool got_alarm;
2332 static int alarm_fd;
2334 static void alarm_handler(int dummy)
2336 got_alarm = True;
2339 static void alarm_handler_parent(int dummy)
2341 close(alarm_fd);
2344 static void do_local_lock(int read_fd, int write_fd)
2346 int fd;
2347 char c = '\0';
2348 struct flock lock;
2349 const char *local_pathname = NULL;
2350 int ret;
2352 local_pathname = talloc_asprintf(talloc_tos(),
2353 "%s/lockt9.lck", local_path);
2354 if (!local_pathname) {
2355 printf("child: alloc fail\n");
2356 exit(1);
2359 unlink(local_pathname);
2360 fd = open(local_pathname, O_RDWR|O_CREAT, 0666);
2361 if (fd == -1) {
2362 printf("child: open of %s failed %s.\n",
2363 local_pathname, strerror(errno));
2364 exit(1);
2367 /* Now take a fcntl lock. */
2368 lock.l_type = F_WRLCK;
2369 lock.l_whence = SEEK_SET;
2370 lock.l_start = 0;
2371 lock.l_len = 4;
2372 lock.l_pid = getpid();
2374 ret = fcntl(fd,F_SETLK,&lock);
2375 if (ret == -1) {
2376 printf("child: failed to get lock 0:4 on file %s. Error %s\n",
2377 local_pathname, strerror(errno));
2378 exit(1);
2379 } else {
2380 printf("child: got lock 0:4 on file %s.\n",
2381 local_pathname );
2382 fflush(stdout);
2385 CatchSignal(SIGALRM, alarm_handler);
2386 alarm(5);
2387 /* Signal the parent. */
2388 if (write(write_fd, &c, 1) != 1) {
2389 printf("child: start signal fail %s.\n",
2390 strerror(errno));
2391 exit(1);
2393 alarm(0);
2395 alarm(10);
2396 /* Wait for the parent to be ready. */
2397 if (read(read_fd, &c, 1) != 1) {
2398 printf("child: reply signal fail %s.\n",
2399 strerror(errno));
2400 exit(1);
2402 alarm(0);
2404 sleep(5);
2405 close(fd);
2406 printf("child: released lock 0:4 on file %s.\n",
2407 local_pathname );
2408 fflush(stdout);
2409 exit(0);
2412 static bool run_locktest9(int dummy)
2414 struct cli_state *cli1;
2415 const char *fname = "\\lockt9.lck";
2416 uint16_t fnum;
2417 bool correct = False;
2418 int pipe_in[2], pipe_out[2];
2419 pid_t child_pid;
2420 char c = '\0';
2421 int ret;
2422 struct timeval start;
2423 double seconds;
2424 NTSTATUS status;
2426 printf("starting locktest9\n");
2428 if (local_path == NULL) {
2429 d_fprintf(stderr, "locktest9 must be given a local path via -l <localpath>\n");
2430 return false;
2433 if (pipe(pipe_in) == -1 || pipe(pipe_out) == -1) {
2434 return false;
2437 child_pid = fork();
2438 if (child_pid == -1) {
2439 return false;
2442 if (child_pid == 0) {
2443 /* Child. */
2444 do_local_lock(pipe_out[0], pipe_in[1]);
2445 exit(0);
2448 close(pipe_out[0]);
2449 close(pipe_in[1]);
2450 pipe_out[0] = -1;
2451 pipe_in[1] = -1;
2453 /* Parent. */
2454 ret = read(pipe_in[0], &c, 1);
2455 if (ret != 1) {
2456 d_fprintf(stderr, "failed to read start signal from child. %s\n",
2457 strerror(errno));
2458 return false;
2461 if (!torture_open_connection(&cli1, 0)) {
2462 return false;
2465 cli_sockopt(cli1, sockops);
2467 status = cli_open(cli1, fname, O_RDWR, DENY_NONE,
2468 &fnum);
2469 if (!NT_STATUS_IS_OK(status)) {
2470 d_fprintf(stderr, "cli_open returned %s\n", cli_errstr(cli1));
2471 return false;
2474 /* Ensure the child has the lock. */
2475 if (cli_lock(cli1, fnum, 0, 4, 0, WRITE_LOCK)) {
2476 d_fprintf(stderr, "Got the lock on range 0:4 - this should not happen !\n");
2477 goto fail;
2478 } else {
2479 d_printf("Child has the lock.\n");
2482 /* Tell the child to wait 5 seconds then exit. */
2483 ret = write(pipe_out[1], &c, 1);
2484 if (ret != 1) {
2485 d_fprintf(stderr, "failed to send exit signal to child. %s\n",
2486 strerror(errno));
2487 goto fail;
2490 /* Wait 20 seconds for the lock. */
2491 alarm_fd = cli1->fd;
2492 CatchSignal(SIGALRM, alarm_handler_parent);
2493 alarm(20);
2495 start = timeval_current();
2497 if (!cli_lock(cli1, fnum, 0, 4, -1, WRITE_LOCK)) {
2498 d_fprintf(stderr, "Unable to apply write lock on range 0:4, error was "
2499 "%s\n", cli_errstr(cli1));
2500 goto fail_nofd;
2502 alarm(0);
2504 seconds = timeval_elapsed(&start);
2506 printf("Parent got the lock after %.2f seconds.\n",
2507 seconds);
2509 status = cli_close(cli1, fnum);
2510 if (!NT_STATUS_IS_OK(status)) {
2511 d_fprintf(stderr, "cli_close(fnum1) %s\n", cli_errstr(cli1));
2512 goto fail;
2515 correct = true;
2517 fail:
2518 cli_close(cli1, fnum);
2519 torture_close_connection(cli1);
2521 fail_nofd:
2523 printf("finished locktest9\n");
2524 return correct;
2528 test whether fnums and tids open on one VC are available on another (a major
2529 security hole)
2531 static bool run_fdpasstest(int dummy)
2533 struct cli_state *cli1, *cli2;
2534 const char *fname = "\\fdpass.tst";
2535 uint16_t fnum1;
2536 char buf[1024];
2538 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
2539 return False;
2541 cli_sockopt(cli1, sockops);
2542 cli_sockopt(cli2, sockops);
2544 printf("starting fdpasstest\n");
2546 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2548 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
2549 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2550 return False;
2553 if (cli_write(cli1, fnum1, 0, "hello world\n", 0, 13) != 13) {
2554 printf("write failed (%s)\n", cli_errstr(cli1));
2555 return False;
2558 cli2->vuid = cli1->vuid;
2559 cli2->cnum = cli1->cnum;
2560 cli2->pid = cli1->pid;
2562 if (cli_read(cli2, fnum1, buf, 0, 13) == 13) {
2563 printf("read succeeded! nasty security hole [%s]\n",
2564 buf);
2565 return False;
2568 cli_close(cli1, fnum1);
2569 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2571 torture_close_connection(cli1);
2572 torture_close_connection(cli2);
2574 printf("finished fdpasstest\n");
2575 return True;
2578 static bool run_fdsesstest(int dummy)
2580 struct cli_state *cli;
2581 uint16 new_vuid;
2582 uint16 saved_vuid;
2583 uint16 new_cnum;
2584 uint16 saved_cnum;
2585 const char *fname = "\\fdsess.tst";
2586 const char *fname1 = "\\fdsess1.tst";
2587 uint16_t fnum1;
2588 uint16_t fnum2;
2589 char buf[1024];
2590 bool ret = True;
2592 if (!torture_open_connection(&cli, 0))
2593 return False;
2594 cli_sockopt(cli, sockops);
2596 if (!torture_cli_session_setup2(cli, &new_vuid))
2597 return False;
2599 saved_cnum = cli->cnum;
2600 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, share, "?????", "", 1)))
2601 return False;
2602 new_cnum = cli->cnum;
2603 cli->cnum = saved_cnum;
2605 printf("starting fdsesstest\n");
2607 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2608 cli_unlink(cli, fname1, aSYSTEM | aHIDDEN);
2610 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
2611 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2612 return False;
2615 if (cli_write(cli, fnum1, 0, "hello world\n", 0, 13) != 13) {
2616 printf("write failed (%s)\n", cli_errstr(cli));
2617 return False;
2620 saved_vuid = cli->vuid;
2621 cli->vuid = new_vuid;
2623 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2624 printf("read succeeded with different vuid! nasty security hole [%s]\n",
2625 buf);
2626 ret = False;
2628 /* Try to open a file with different vuid, samba cnum. */
2629 if (NT_STATUS_IS_OK(cli_open(cli, fname1, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum2))) {
2630 printf("create with different vuid, same cnum succeeded.\n");
2631 cli_close(cli, fnum2);
2632 cli_unlink(cli, fname1, aSYSTEM | aHIDDEN);
2633 } else {
2634 printf("create with different vuid, same cnum failed.\n");
2635 printf("This will cause problems with service clients.\n");
2636 ret = False;
2639 cli->vuid = saved_vuid;
2641 /* Try with same vuid, different cnum. */
2642 cli->cnum = new_cnum;
2644 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2645 printf("read succeeded with different cnum![%s]\n",
2646 buf);
2647 ret = False;
2650 cli->cnum = saved_cnum;
2651 cli_close(cli, fnum1);
2652 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2654 torture_close_connection(cli);
2656 printf("finished fdsesstest\n");
2657 return ret;
2661 This test checks that
2663 1) the server does not allow an unlink on a file that is open
2665 static bool run_unlinktest(int dummy)
2667 struct cli_state *cli;
2668 const char *fname = "\\unlink.tst";
2669 uint16_t fnum;
2670 bool correct = True;
2672 if (!torture_open_connection(&cli, 0)) {
2673 return False;
2676 cli_sockopt(cli, sockops);
2678 printf("starting unlink test\n");
2680 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2682 cli_setpid(cli, 1);
2684 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
2685 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2686 return False;
2689 if (NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
2690 printf("error: server allowed unlink on an open file\n");
2691 correct = False;
2692 } else {
2693 correct = check_error(__LINE__, cli, ERRDOS, ERRbadshare,
2694 NT_STATUS_SHARING_VIOLATION);
2697 cli_close(cli, fnum);
2698 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2700 if (!torture_close_connection(cli)) {
2701 correct = False;
2704 printf("unlink test finished\n");
2706 return correct;
2711 test how many open files this server supports on the one socket
2713 static bool run_maxfidtest(int dummy)
2715 struct cli_state *cli;
2716 const char *ftemplate = "\\maxfid.%d.%d";
2717 fstring fname;
2718 uint16_t fnums[0x11000];
2719 int i;
2720 int retries=4;
2721 bool correct = True;
2723 cli = current_cli;
2725 if (retries <= 0) {
2726 printf("failed to connect\n");
2727 return False;
2730 cli_sockopt(cli, sockops);
2732 for (i=0; i<0x11000; i++) {
2733 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2734 if (!NT_STATUS_IS_OK(cli_open(cli, fname,
2735 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnums[i]))) {
2736 printf("open of %s failed (%s)\n",
2737 fname, cli_errstr(cli));
2738 printf("maximum fnum is %d\n", i);
2739 break;
2741 printf("%6d\r", i);
2743 printf("%6d\n", i);
2744 i--;
2746 printf("cleaning up\n");
2747 for (;i>=0;i--) {
2748 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2749 cli_close(cli, fnums[i]);
2750 if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
2751 printf("unlink of %s failed (%s)\n",
2752 fname, cli_errstr(cli));
2753 correct = False;
2755 printf("%6d\r", i);
2757 printf("%6d\n", 0);
2759 printf("maxfid test finished\n");
2760 if (!torture_close_connection(cli)) {
2761 correct = False;
2763 return correct;
2766 /* generate a random buffer */
2767 static void rand_buf(char *buf, int len)
2769 while (len--) {
2770 *buf = (char)sys_random();
2771 buf++;
2775 /* send smb negprot commands, not reading the response */
2776 static bool run_negprot_nowait(int dummy)
2778 int i;
2779 static struct cli_state *cli;
2780 bool correct = True;
2782 printf("starting negprot nowait test\n");
2784 if (!(cli = open_nbt_connection())) {
2785 return False;
2788 for (i=0;i<50000;i++) {
2789 cli_negprot_sendsync(cli);
2792 if (!torture_close_connection(cli)) {
2793 correct = False;
2796 printf("finished negprot nowait test\n");
2798 return correct;
2801 /* send smb negprot commands, not reading the response */
2802 static bool run_bad_nbt_session(int dummy)
2804 static struct cli_state *cli;
2806 printf("starting bad nbt session test\n");
2808 if (!(cli = open_bad_nbt_connection())) {
2809 return False;
2812 cli_shutdown(cli);
2813 printf("finished bad nbt session test\n");
2814 return true;
2817 /* send random IPC commands */
2818 static bool run_randomipc(int dummy)
2820 char *rparam = NULL;
2821 char *rdata = NULL;
2822 unsigned int rdrcnt,rprcnt;
2823 char param[1024];
2824 int api, param_len, i;
2825 struct cli_state *cli;
2826 bool correct = True;
2827 int count = 50000;
2829 printf("starting random ipc test\n");
2831 if (!torture_open_connection(&cli, 0)) {
2832 return False;
2835 for (i=0;i<count;i++) {
2836 api = sys_random() % 500;
2837 param_len = (sys_random() % 64);
2839 rand_buf(param, param_len);
2841 SSVAL(param,0,api);
2843 cli_api(cli,
2844 param, param_len, 8,
2845 NULL, 0, BUFFER_SIZE,
2846 &rparam, &rprcnt,
2847 &rdata, &rdrcnt);
2848 if (i % 100 == 0) {
2849 printf("%d/%d\r", i,count);
2852 printf("%d/%d\n", i, count);
2854 if (!torture_close_connection(cli)) {
2855 correct = False;
2858 printf("finished random ipc test\n");
2860 return correct;
2865 static void browse_callback(const char *sname, uint32 stype,
2866 const char *comment, void *state)
2868 printf("\t%20.20s %08x %s\n", sname, stype, comment);
2874 This test checks the browse list code
2877 static bool run_browsetest(int dummy)
2879 static struct cli_state *cli;
2880 bool correct = True;
2882 printf("starting browse test\n");
2884 if (!torture_open_connection(&cli, 0)) {
2885 return False;
2888 printf("domain list:\n");
2889 cli_NetServerEnum(cli, cli->server_domain,
2890 SV_TYPE_DOMAIN_ENUM,
2891 browse_callback, NULL);
2893 printf("machine list:\n");
2894 cli_NetServerEnum(cli, cli->server_domain,
2895 SV_TYPE_ALL,
2896 browse_callback, NULL);
2898 if (!torture_close_connection(cli)) {
2899 correct = False;
2902 printf("browse test finished\n");
2904 return correct;
2910 This checks how the getatr calls works
2912 static bool run_attrtest(int dummy)
2914 struct cli_state *cli;
2915 uint16_t fnum;
2916 time_t t, t2;
2917 const char *fname = "\\attrib123456789.tst";
2918 bool correct = True;
2920 printf("starting attrib test\n");
2922 if (!torture_open_connection(&cli, 0)) {
2923 return False;
2926 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2927 cli_open(cli, fname,
2928 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
2929 cli_close(cli, fnum);
2930 if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
2931 printf("getatr failed (%s)\n", cli_errstr(cli));
2932 correct = False;
2935 if (abs(t - time(NULL)) > 60*60*24*10) {
2936 printf("ERROR: SMBgetatr bug. time is %s",
2937 ctime(&t));
2938 t = time(NULL);
2939 correct = True;
2942 t2 = t-60*60*24; /* 1 day ago */
2944 if (!NT_STATUS_IS_OK(cli_setatr(cli, fname, 0, t2))) {
2945 printf("setatr failed (%s)\n", cli_errstr(cli));
2946 correct = True;
2949 if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
2950 printf("getatr failed (%s)\n", cli_errstr(cli));
2951 correct = True;
2954 if (t != t2) {
2955 printf("ERROR: getatr/setatr bug. times are\n%s",
2956 ctime(&t));
2957 printf("%s", ctime(&t2));
2958 correct = True;
2961 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2963 if (!torture_close_connection(cli)) {
2964 correct = False;
2967 printf("attrib test finished\n");
2969 return correct;
2974 This checks a couple of trans2 calls
2976 static bool run_trans2test(int dummy)
2978 struct cli_state *cli;
2979 uint16_t fnum;
2980 SMB_OFF_T size;
2981 time_t c_time, a_time, m_time;
2982 struct timespec c_time_ts, a_time_ts, m_time_ts, w_time_ts, m_time2_ts;
2983 const char *fname = "\\trans2.tst";
2984 const char *dname = "\\trans2";
2985 const char *fname2 = "\\trans2\\trans2.tst";
2986 char pname[1024];
2987 bool correct = True;
2988 NTSTATUS status;
2989 uint32_t fs_attr;
2991 printf("starting trans2 test\n");
2993 if (!torture_open_connection(&cli, 0)) {
2994 return False;
2997 status = cli_get_fs_attr_info(cli, &fs_attr);
2998 if (!NT_STATUS_IS_OK(status)) {
2999 printf("ERROR: cli_get_fs_attr_info returned %s\n",
3000 nt_errstr(status));
3001 correct = false;
3004 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
3005 cli_open(cli, fname,
3006 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3007 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
3008 cli, fnum, NULL, &size, &c_time_ts,
3009 &a_time_ts, &w_time_ts,
3010 &m_time_ts, NULL))) {
3011 printf("ERROR: qfileinfo failed (%s)\n", cli_errstr(cli));
3012 correct = False;
3015 if (!NT_STATUS_IS_OK(cli_qfilename(cli, fnum, pname, sizeof(pname)))) {
3016 printf("ERROR: qfilename failed (%s)\n", cli_errstr(cli));
3017 correct = False;
3020 if (strcmp(pname, fname)) {
3021 printf("qfilename gave different name? [%s] [%s]\n",
3022 fname, pname);
3023 correct = False;
3026 cli_close(cli, fnum);
3028 sleep(2);
3030 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
3031 if (!NT_STATUS_IS_OK(cli_open(cli, fname,
3032 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum))) {
3033 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
3034 return False;
3036 cli_close(cli, fnum);
3038 status = cli_qpathinfo1(cli, fname, &c_time, &a_time, &m_time, &size,
3039 NULL);
3040 if (!NT_STATUS_IS_OK(status)) {
3041 printf("ERROR: qpathinfo failed (%s)\n", nt_errstr(status));
3042 correct = False;
3043 } else {
3044 if (c_time != m_time) {
3045 printf("create time=%s", ctime(&c_time));
3046 printf("modify time=%s", ctime(&m_time));
3047 printf("This system appears to have sticky create times\n");
3049 if (a_time % (60*60) == 0) {
3050 printf("access time=%s", ctime(&a_time));
3051 printf("This system appears to set a midnight access time\n");
3052 correct = False;
3055 if (abs(m_time - time(NULL)) > 60*60*24*7) {
3056 printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
3057 correct = False;
3062 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
3063 cli_open(cli, fname,
3064 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3065 cli_close(cli, fnum);
3066 status = cli_qpathinfo2(cli, fname, &c_time_ts, &a_time_ts, &w_time_ts,
3067 &m_time_ts, &size, NULL, NULL);
3068 if (!NT_STATUS_IS_OK(status)) {
3069 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3070 correct = False;
3071 } else {
3072 if (w_time_ts.tv_sec < 60*60*24*2) {
3073 printf("write time=%s", ctime(&w_time_ts.tv_sec));
3074 printf("This system appears to set a initial 0 write time\n");
3075 correct = False;
3079 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
3082 /* check if the server updates the directory modification time
3083 when creating a new file */
3084 if (!NT_STATUS_IS_OK(cli_mkdir(cli, dname))) {
3085 printf("ERROR: mkdir failed (%s)\n", cli_errstr(cli));
3086 correct = False;
3088 sleep(3);
3089 status = cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts,
3090 &w_time_ts, &m_time_ts, &size, NULL, NULL);
3091 if (!NT_STATUS_IS_OK(status)) {
3092 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3093 correct = False;
3096 cli_open(cli, fname2,
3097 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3098 cli_write(cli, fnum, 0, (char *)&fnum, 0, sizeof(fnum));
3099 cli_close(cli, fnum);
3100 status = cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts,
3101 &w_time_ts, &m_time2_ts, &size, NULL, NULL);
3102 if (!NT_STATUS_IS_OK(status)) {
3103 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3104 correct = False;
3105 } else {
3106 if (memcmp(&m_time_ts, &m_time2_ts, sizeof(struct timespec))
3107 == 0) {
3108 printf("This system does not update directory modification times\n");
3109 correct = False;
3112 cli_unlink(cli, fname2, aSYSTEM | aHIDDEN);
3113 cli_rmdir(cli, dname);
3115 if (!torture_close_connection(cli)) {
3116 correct = False;
3119 printf("trans2 test finished\n");
3121 return correct;
3125 This checks new W2K calls.
3128 static NTSTATUS new_trans(struct cli_state *pcli, int fnum, int level)
3130 uint8_t *buf = NULL;
3131 uint32 len;
3132 NTSTATUS status;
3134 status = cli_qfileinfo(talloc_tos(), pcli, fnum, level, 0,
3135 pcli->max_xmit, &buf, &len);
3136 if (!NT_STATUS_IS_OK(status)) {
3137 printf("ERROR: qfileinfo (%d) failed (%s)\n", level,
3138 nt_errstr(status));
3139 } else {
3140 printf("qfileinfo: level %d, len = %u\n", level, len);
3141 dump_data(0, (uint8 *)buf, len);
3142 printf("\n");
3144 TALLOC_FREE(buf);
3145 return status;
3148 static bool run_w2ktest(int dummy)
3150 struct cli_state *cli;
3151 uint16_t fnum;
3152 const char *fname = "\\w2ktest\\w2k.tst";
3153 int level;
3154 bool correct = True;
3156 printf("starting w2k test\n");
3158 if (!torture_open_connection(&cli, 0)) {
3159 return False;
3162 cli_open(cli, fname,
3163 O_RDWR | O_CREAT , DENY_NONE, &fnum);
3165 for (level = 1004; level < 1040; level++) {
3166 new_trans(cli, fnum, level);
3169 cli_close(cli, fnum);
3171 if (!torture_close_connection(cli)) {
3172 correct = False;
3175 printf("w2k test finished\n");
3177 return correct;
3182 this is a harness for some oplock tests
3184 static bool run_oplock1(int dummy)
3186 struct cli_state *cli1;
3187 const char *fname = "\\lockt1.lck";
3188 uint16_t fnum1;
3189 bool correct = True;
3191 printf("starting oplock test 1\n");
3193 if (!torture_open_connection(&cli1, 0)) {
3194 return False;
3197 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3199 cli_sockopt(cli1, sockops);
3201 cli1->use_oplocks = True;
3203 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
3204 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3205 return False;
3208 cli1->use_oplocks = False;
3210 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3211 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3213 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3214 printf("close2 failed (%s)\n", cli_errstr(cli1));
3215 return False;
3218 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
3219 printf("unlink failed (%s)\n", cli_errstr(cli1));
3220 return False;
3223 if (!torture_close_connection(cli1)) {
3224 correct = False;
3227 printf("finished oplock test 1\n");
3229 return correct;
3232 static bool run_oplock2(int dummy)
3234 struct cli_state *cli1, *cli2;
3235 const char *fname = "\\lockt2.lck";
3236 uint16_t fnum1, fnum2;
3237 int saved_use_oplocks = use_oplocks;
3238 char buf[4];
3239 bool correct = True;
3240 volatile bool *shared_correct;
3242 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
3243 *shared_correct = True;
3245 use_level_II_oplocks = True;
3246 use_oplocks = True;
3248 printf("starting oplock test 2\n");
3250 if (!torture_open_connection(&cli1, 0)) {
3251 use_level_II_oplocks = False;
3252 use_oplocks = saved_use_oplocks;
3253 return False;
3256 cli1->use_oplocks = True;
3257 cli1->use_level_II_oplocks = True;
3259 if (!torture_open_connection(&cli2, 1)) {
3260 use_level_II_oplocks = False;
3261 use_oplocks = saved_use_oplocks;
3262 return False;
3265 cli2->use_oplocks = True;
3266 cli2->use_level_II_oplocks = True;
3268 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3270 cli_sockopt(cli1, sockops);
3271 cli_sockopt(cli2, sockops);
3273 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
3274 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3275 return False;
3278 /* Don't need the globals any more. */
3279 use_level_II_oplocks = False;
3280 use_oplocks = saved_use_oplocks;
3282 if (fork() == 0) {
3283 /* Child code */
3284 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2))) {
3285 printf("second open of %s failed (%s)\n", fname, cli_errstr(cli1));
3286 *shared_correct = False;
3287 exit(0);
3290 sleep(2);
3292 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
3293 printf("close2 failed (%s)\n", cli_errstr(cli1));
3294 *shared_correct = False;
3297 exit(0);
3300 sleep(2);
3302 /* Ensure cli1 processes the break. Empty file should always return 0
3303 * bytes. */
3305 if (cli_read(cli1, fnum1, buf, 0, 4) != 0) {
3306 printf("read on fnum1 failed (%s)\n", cli_errstr(cli1));
3307 correct = False;
3310 /* Should now be at level II. */
3311 /* Test if sending a write locks causes a break to none. */
3313 if (!cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK)) {
3314 printf("lock failed (%s)\n", cli_errstr(cli1));
3315 correct = False;
3318 cli_unlock(cli1, fnum1, 0, 4);
3320 sleep(2);
3322 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
3323 printf("lock failed (%s)\n", cli_errstr(cli1));
3324 correct = False;
3327 cli_unlock(cli1, fnum1, 0, 4);
3329 sleep(2);
3331 cli_read(cli1, fnum1, buf, 0, 4);
3333 #if 0
3334 if (cli_write(cli1, fnum1, 0, buf, 0, 4) != 4) {
3335 printf("write on fnum1 failed (%s)\n", cli_errstr(cli1));
3336 correct = False;
3338 #endif
3340 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3341 printf("close1 failed (%s)\n", cli_errstr(cli1));
3342 correct = False;
3345 sleep(4);
3347 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
3348 printf("unlink failed (%s)\n", cli_errstr(cli1));
3349 correct = False;
3352 if (!torture_close_connection(cli1)) {
3353 correct = False;
3356 if (!*shared_correct) {
3357 correct = False;
3360 printf("finished oplock test 2\n");
3362 return correct;
3365 /* handler for oplock 3 tests */
3366 static NTSTATUS oplock3_handler(struct cli_state *cli, uint16_t fnum, unsigned char level)
3368 printf("got oplock break fnum=%d level=%d\n",
3369 fnum, level);
3370 return cli_oplock_ack(cli, fnum, level);
3373 static bool run_oplock3(int dummy)
3375 struct cli_state *cli;
3376 const char *fname = "\\oplockt3.dat";
3377 uint16_t fnum;
3378 char buf[4] = "abcd";
3379 bool correct = True;
3380 volatile bool *shared_correct;
3382 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
3383 *shared_correct = True;
3385 printf("starting oplock test 3\n");
3387 if (fork() == 0) {
3388 /* Child code */
3389 use_oplocks = True;
3390 use_level_II_oplocks = True;
3391 if (!torture_open_connection(&cli, 0)) {
3392 *shared_correct = False;
3393 exit(0);
3395 sleep(2);
3396 /* try to trigger a oplock break in parent */
3397 cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum);
3398 cli_write(cli, fnum, 0, buf, 0, 4);
3399 exit(0);
3402 /* parent code */
3403 use_oplocks = True;
3404 use_level_II_oplocks = True;
3405 if (!torture_open_connection(&cli, 1)) { /* other is forked */
3406 return False;
3408 cli_oplock_handler(cli, oplock3_handler);
3409 cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
3410 cli_write(cli, fnum, 0, buf, 0, 4);
3411 cli_close(cli, fnum);
3412 cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum);
3413 cli->timeout = 20000;
3414 cli_receive_smb(cli);
3415 printf("finished oplock test 3\n");
3417 return (correct && *shared_correct);
3419 /* What are we looking for here? What's sucess and what's FAILURE? */
3422 /* handler for oplock 4 tests */
3423 bool *oplock4_shared_correct;
3425 static NTSTATUS oplock4_handler(struct cli_state *cli, uint16_t fnum, unsigned char level)
3427 printf("got oplock break fnum=%d level=%d\n",
3428 fnum, level);
3429 *oplock4_shared_correct = true;
3430 cli_oplock_ack(cli, fnum, level);
3431 return NT_STATUS_UNSUCCESSFUL; /* Cause cli_receive_smb to return. */
3434 static bool run_oplock4(int dummy)
3436 struct cli_state *cli1, *cli2;
3437 const char *fname = "\\lockt4.lck";
3438 const char *fname_ln = "\\lockt4_ln.lck";
3439 uint16_t fnum1, fnum2;
3440 int saved_use_oplocks = use_oplocks;
3441 NTSTATUS status;
3442 bool correct = true;
3444 oplock4_shared_correct = (bool *)shm_setup(sizeof(bool));
3445 *oplock4_shared_correct = false;
3447 printf("starting oplock test 4\n");
3449 if (!torture_open_connection(&cli1, 0)) {
3450 use_level_II_oplocks = false;
3451 use_oplocks = saved_use_oplocks;
3452 return false;
3455 if (!torture_open_connection(&cli2, 1)) {
3456 use_level_II_oplocks = false;
3457 use_oplocks = saved_use_oplocks;
3458 return false;
3461 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3462 cli_unlink(cli1, fname_ln, aSYSTEM | aHIDDEN);
3464 cli_sockopt(cli1, sockops);
3465 cli_sockopt(cli2, sockops);
3467 /* Create the file. */
3468 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
3469 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3470 return false;
3473 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3474 printf("close1 failed (%s)\n", cli_errstr(cli1));
3475 return false;
3478 /* Now create a hardlink. */
3479 if (!NT_STATUS_IS_OK(cli_nt_hardlink(cli1, fname, fname_ln))) {
3480 printf("nt hardlink failed (%s)\n", cli_errstr(cli1));
3481 return false;
3484 /* Prove that opening hardlinks cause deny modes to conflict. */
3485 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum1))) {
3486 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3487 return false;
3490 status = cli_open(cli1, fname_ln, O_RDWR, DENY_NONE, &fnum2);
3491 if (NT_STATUS_IS_OK(status)) {
3492 printf("open of %s succeeded - should fail with sharing violation.\n",
3493 fname_ln);
3494 return false;
3497 if (!NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
3498 printf("open of %s should fail with sharing violation. Got %s\n",
3499 fname_ln, nt_errstr(status));
3500 return false;
3503 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3504 printf("close1 failed (%s)\n", cli_errstr(cli1));
3505 return false;
3508 cli1->use_oplocks = true;
3509 cli1->use_level_II_oplocks = true;
3511 cli2->use_oplocks = true;
3512 cli2->use_level_II_oplocks = true;
3514 cli_oplock_handler(cli1, oplock4_handler);
3515 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
3516 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3517 return false;
3520 if (fork() == 0) {
3521 /* Child code */
3522 if (!NT_STATUS_IS_OK(cli_open(cli2, fname_ln, O_RDWR, DENY_NONE, &fnum2))) {
3523 printf("open of %s failed (%s)\n", fname_ln, cli_errstr(cli1));
3524 *oplock4_shared_correct = false;
3525 exit(0);
3528 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
3529 printf("close2 failed (%s)\n", cli_errstr(cli1));
3530 *oplock4_shared_correct = false;
3533 exit(0);
3536 sleep(2);
3538 /* Process the oplock break. */
3539 cli_receive_smb(cli1);
3541 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3542 printf("close1 failed (%s)\n", cli_errstr(cli1));
3543 correct = false;
3546 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
3547 printf("unlink failed (%s)\n", cli_errstr(cli1));
3548 correct = false;
3550 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname_ln, aSYSTEM | aHIDDEN))) {
3551 printf("unlink failed (%s)\n", cli_errstr(cli1));
3552 correct = false;
3555 if (!torture_close_connection(cli1)) {
3556 correct = false;
3559 if (!*oplock4_shared_correct) {
3560 correct = false;
3563 printf("finished oplock test 4\n");
3565 return correct;
3570 Test delete on close semantics.
3572 static bool run_deletetest(int dummy)
3574 struct cli_state *cli1 = NULL;
3575 struct cli_state *cli2 = NULL;
3576 const char *fname = "\\delete.file";
3577 uint16_t fnum1 = (uint16_t)-1;
3578 uint16_t fnum2 = (uint16_t)-1;
3579 bool correct = True;
3581 printf("starting delete test\n");
3583 if (!torture_open_connection(&cli1, 0)) {
3584 return False;
3587 cli_sockopt(cli1, sockops);
3589 /* Test 1 - this should delete the file on close. */
3591 cli_setatr(cli1, fname, 0, 0);
3592 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3594 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
3595 0, FILE_OVERWRITE_IF,
3596 FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3597 printf("[1] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3598 correct = False;
3599 goto fail;
3602 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3603 printf("[1] close failed (%s)\n", cli_errstr(cli1));
3604 correct = False;
3605 goto fail;
3608 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
3609 printf("[1] open of %s succeeded (should fail)\n", fname);
3610 correct = False;
3611 goto fail;
3614 printf("first delete on close test succeeded.\n");
3616 /* Test 2 - this should delete the file on close. */
3618 cli_setatr(cli1, fname, 0, 0);
3619 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3621 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
3622 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
3623 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3624 printf("[2] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3625 correct = False;
3626 goto fail;
3629 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3630 printf("[2] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3631 correct = False;
3632 goto fail;
3635 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3636 printf("[2] close failed (%s)\n", cli_errstr(cli1));
3637 correct = False;
3638 goto fail;
3641 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3642 printf("[2] open of %s succeeded should have been deleted on close !\n", fname);
3643 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3644 printf("[2] close failed (%s)\n", cli_errstr(cli1));
3645 correct = False;
3646 goto fail;
3648 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3649 } else
3650 printf("second delete on close test succeeded.\n");
3652 /* Test 3 - ... */
3653 cli_setatr(cli1, fname, 0, 0);
3654 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3656 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
3657 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3658 printf("[3] open - 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3659 correct = False;
3660 goto fail;
3663 /* This should fail with a sharing violation - open for delete is only compatible
3664 with SHARE_DELETE. */
3666 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3667 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0, 0, &fnum2))) {
3668 printf("[3] open - 2 of %s succeeded - should have failed.\n", fname);
3669 correct = False;
3670 goto fail;
3673 /* This should succeed. */
3675 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3676 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0, &fnum2))) {
3677 printf("[3] open - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3678 correct = False;
3679 goto fail;
3682 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3683 printf("[3] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3684 correct = False;
3685 goto fail;
3688 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3689 printf("[3] close 1 failed (%s)\n", cli_errstr(cli1));
3690 correct = False;
3691 goto fail;
3694 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
3695 printf("[3] close 2 failed (%s)\n", cli_errstr(cli1));
3696 correct = False;
3697 goto fail;
3700 /* This should fail - file should no longer be there. */
3702 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3703 printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
3704 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3705 printf("[3] close failed (%s)\n", cli_errstr(cli1));
3707 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3708 correct = False;
3709 goto fail;
3710 } else
3711 printf("third delete on close test succeeded.\n");
3713 /* Test 4 ... */
3714 cli_setatr(cli1, fname, 0, 0);
3715 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3717 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3718 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3719 printf("[4] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3720 correct = False;
3721 goto fail;
3724 /* This should succeed. */
3725 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3726 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0, &fnum2))) {
3727 printf("[4] open - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3728 correct = False;
3729 goto fail;
3732 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
3733 printf("[4] close - 1 failed (%s)\n", cli_errstr(cli1));
3734 correct = False;
3735 goto fail;
3738 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3739 printf("[4] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3740 correct = False;
3741 goto fail;
3744 /* This should fail - no more opens once delete on close set. */
3745 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3746 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3747 FILE_OPEN, 0, 0, &fnum2))) {
3748 printf("[4] open - 3 of %s succeeded ! Should have failed.\n", fname );
3749 correct = False;
3750 goto fail;
3751 } else
3752 printf("fourth delete on close test succeeded.\n");
3754 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3755 printf("[4] close - 2 failed (%s)\n", cli_errstr(cli1));
3756 correct = False;
3757 goto fail;
3760 /* Test 5 ... */
3761 cli_setatr(cli1, fname, 0, 0);
3762 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3764 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum1))) {
3765 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3766 correct = False;
3767 goto fail;
3770 /* This should fail - only allowed on NT opens with DELETE access. */
3772 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3773 printf("[5] setting delete_on_close on OpenX file succeeded - should fail !\n");
3774 correct = False;
3775 goto fail;
3778 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3779 printf("[5] close - 2 failed (%s)\n", cli_errstr(cli1));
3780 correct = False;
3781 goto fail;
3784 printf("fifth delete on close test succeeded.\n");
3786 /* Test 6 ... */
3787 cli_setatr(cli1, fname, 0, 0);
3788 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3790 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
3791 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3792 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3793 printf("[6] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3794 correct = False;
3795 goto fail;
3798 /* This should fail - only allowed on NT opens with DELETE access. */
3800 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3801 printf("[6] setting delete_on_close on file with no delete access succeeded - should fail !\n");
3802 correct = False;
3803 goto fail;
3806 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3807 printf("[6] close - 2 failed (%s)\n", cli_errstr(cli1));
3808 correct = False;
3809 goto fail;
3812 printf("sixth delete on close test succeeded.\n");
3814 /* Test 7 ... */
3815 cli_setatr(cli1, fname, 0, 0);
3816 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3818 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3819 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3820 printf("[7] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3821 correct = False;
3822 goto fail;
3825 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3826 printf("[7] setting delete_on_close on file failed !\n");
3827 correct = False;
3828 goto fail;
3831 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, false))) {
3832 printf("[7] unsetting delete_on_close on file failed !\n");
3833 correct = False;
3834 goto fail;
3837 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3838 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3839 correct = False;
3840 goto fail;
3843 /* This next open should succeed - we reset the flag. */
3845 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3846 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3847 correct = False;
3848 goto fail;
3851 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3852 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3853 correct = False;
3854 goto fail;
3857 printf("seventh delete on close test succeeded.\n");
3859 /* Test 7 ... */
3860 cli_setatr(cli1, fname, 0, 0);
3861 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3863 if (!torture_open_connection(&cli2, 1)) {
3864 printf("[8] failed to open second connection.\n");
3865 correct = False;
3866 goto fail;
3869 cli_sockopt(cli1, sockops);
3871 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3872 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3873 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3874 printf("[8] open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3875 correct = False;
3876 goto fail;
3879 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3880 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3881 FILE_OPEN, 0, 0, &fnum2))) {
3882 printf("[8] open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3883 correct = False;
3884 goto fail;
3887 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3888 printf("[8] setting delete_on_close on file failed !\n");
3889 correct = False;
3890 goto fail;
3893 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3894 printf("[8] close - 1 failed (%s)\n", cli_errstr(cli1));
3895 correct = False;
3896 goto fail;
3899 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
3900 printf("[8] close - 2 failed (%s)\n", cli_errstr(cli2));
3901 correct = False;
3902 goto fail;
3905 /* This should fail.. */
3906 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3907 printf("[8] open of %s succeeded should have been deleted on close !\n", fname);
3908 goto fail;
3909 correct = False;
3910 } else
3911 printf("eighth delete on close test succeeded.\n");
3913 /* This should fail - we need to set DELETE_ACCESS. */
3914 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,FILE_READ_DATA|FILE_WRITE_DATA,
3915 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3916 printf("[9] open of %s succeeded should have failed!\n", fname);
3917 correct = False;
3918 goto fail;
3921 printf("ninth delete on close test succeeded.\n");
3923 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3924 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3925 printf("[10] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3926 correct = False;
3927 goto fail;
3930 /* This should delete the file. */
3931 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3932 printf("[10] close failed (%s)\n", cli_errstr(cli1));
3933 correct = False;
3934 goto fail;
3937 /* This should fail.. */
3938 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3939 printf("[10] open of %s succeeded should have been deleted on close !\n", fname);
3940 goto fail;
3941 correct = False;
3942 } else
3943 printf("tenth delete on close test succeeded.\n");
3945 cli_setatr(cli1, fname, 0, 0);
3946 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3948 /* What error do we get when attempting to open a read-only file with
3949 delete access ? */
3951 /* Create a readonly file. */
3952 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
3953 FILE_ATTRIBUTE_READONLY, FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3954 printf("[11] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3955 correct = False;
3956 goto fail;
3959 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3960 printf("[11] close failed (%s)\n", cli_errstr(cli1));
3961 correct = False;
3962 goto fail;
3965 /* Now try open for delete access. */
3966 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES|DELETE_ACCESS,
3967 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3968 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3969 printf("[11] open of %s succeeded should have been denied with ACCESS_DENIED!\n", fname);
3970 cli_close(cli1, fnum1);
3971 goto fail;
3972 correct = False;
3973 } else {
3974 NTSTATUS nterr = cli_nt_error(cli1);
3975 if (!NT_STATUS_EQUAL(nterr,NT_STATUS_ACCESS_DENIED)) {
3976 printf("[11] open of %s should have been denied with ACCESS_DENIED! Got error %s\n", fname, nt_errstr(nterr));
3977 goto fail;
3978 correct = False;
3979 } else {
3980 printf("eleventh delete on close test succeeded.\n");
3984 printf("finished delete test\n");
3986 fail:
3987 /* FIXME: This will crash if we aborted before cli2 got
3988 * intialized, because these functions don't handle
3989 * uninitialized connections. */
3991 if (fnum1 != (uint16_t)-1) cli_close(cli1, fnum1);
3992 if (fnum2 != (uint16_t)-1) cli_close(cli1, fnum2);
3993 cli_setatr(cli1, fname, 0, 0);
3994 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3996 if (cli1 && !torture_close_connection(cli1)) {
3997 correct = False;
3999 if (cli2 && !torture_close_connection(cli2)) {
4000 correct = False;
4002 return correct;
4005 static bool run_deletetest_ln(int dummy)
4007 struct cli_state *cli;
4008 const char *fname = "\\delete1";
4009 const char *fname_ln = "\\delete1_ln";
4010 uint16_t fnum;
4011 uint16_t fnum1;
4012 NTSTATUS status;
4013 bool correct = true;
4014 time_t t;
4016 printf("starting deletetest-ln\n");
4018 if (!torture_open_connection(&cli, 0)) {
4019 return false;
4022 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
4023 cli_unlink(cli, fname_ln, aSYSTEM | aHIDDEN);
4025 cli_sockopt(cli, sockops);
4027 /* Create the file. */
4028 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
4029 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
4030 return false;
4033 if (!NT_STATUS_IS_OK(cli_close(cli, fnum))) {
4034 printf("close1 failed (%s)\n", cli_errstr(cli));
4035 return false;
4038 /* Now create a hardlink. */
4039 if (!NT_STATUS_IS_OK(cli_nt_hardlink(cli, fname, fname_ln))) {
4040 printf("nt hardlink failed (%s)\n", cli_errstr(cli));
4041 return false;
4044 /* Open the original file. */
4045 status = cli_ntcreate(cli, fname, 0, FILE_READ_DATA,
4046 FILE_ATTRIBUTE_NORMAL,
4047 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4048 FILE_OPEN_IF, 0, 0, &fnum);
4049 if (!NT_STATUS_IS_OK(status)) {
4050 printf("ntcreate of %s failed (%s)\n", fname, nt_errstr(status));
4051 return false;
4054 /* Unlink the hard link path. */
4055 status = cli_ntcreate(cli, fname_ln, 0, DELETE_ACCESS,
4056 FILE_ATTRIBUTE_NORMAL,
4057 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4058 FILE_OPEN_IF, 0, 0, &fnum1);
4059 if (!NT_STATUS_IS_OK(status)) {
4060 printf("ntcreate of %s failed (%s)\n", fname_ln, nt_errstr(status));
4061 return false;
4063 status = cli_nt_delete_on_close(cli, fnum1, true);
4064 if (!NT_STATUS_IS_OK(status)) {
4065 d_printf("(%s) failed to set delete_on_close %s: %s\n",
4066 __location__, fname_ln, nt_errstr(status));
4067 return false;
4070 status = cli_close(cli, fnum1);
4071 if (!NT_STATUS_IS_OK(status)) {
4072 printf("close %s failed (%s)\n",
4073 fname_ln, nt_errstr(status));
4074 return false;
4077 status = cli_close(cli, fnum);
4078 if (!NT_STATUS_IS_OK(status)) {
4079 printf("close %s failed (%s)\n",
4080 fname, nt_errstr(status));
4081 return false;
4084 /* Ensure the original file is still there. */
4085 status = cli_getatr(cli, fname, NULL, NULL, &t);
4086 if (!NT_STATUS_IS_OK(status)) {
4087 printf("%s getatr on file %s failed (%s)\n",
4088 __location__,
4089 fname,
4090 nt_errstr(status));
4091 correct = False;
4094 /* Ensure the link path is gone. */
4095 status = cli_getatr(cli, fname_ln, NULL, NULL, &t);
4096 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
4097 printf("%s, getatr for file %s returned wrong error code %s "
4098 "- should have been deleted\n",
4099 __location__,
4100 fname_ln, nt_errstr(status));
4101 correct = False;
4104 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
4105 cli_unlink(cli, fname_ln, aSYSTEM | aHIDDEN);
4107 if (!torture_close_connection(cli)) {
4108 correct = false;
4111 printf("finished deletetest-ln\n");
4113 return correct;
4117 print out server properties
4119 static bool run_properties(int dummy)
4121 struct cli_state *cli;
4122 bool correct = True;
4124 printf("starting properties test\n");
4126 ZERO_STRUCT(cli);
4128 if (!torture_open_connection(&cli, 0)) {
4129 return False;
4132 cli_sockopt(cli, sockops);
4134 d_printf("Capabilities 0x%08x\n", cli->capabilities);
4136 if (!torture_close_connection(cli)) {
4137 correct = False;
4140 return correct;
4145 /* FIRST_DESIRED_ACCESS 0xf019f */
4146 #define FIRST_DESIRED_ACCESS FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|\
4147 FILE_READ_EA| /* 0xf */ \
4148 FILE_WRITE_EA|FILE_READ_ATTRIBUTES| /* 0x90 */ \
4149 FILE_WRITE_ATTRIBUTES| /* 0x100 */ \
4150 DELETE_ACCESS|READ_CONTROL_ACCESS|\
4151 WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS /* 0xf0000 */
4152 /* SECOND_DESIRED_ACCESS 0xe0080 */
4153 #define SECOND_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
4154 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
4155 WRITE_OWNER_ACCESS /* 0xe0000 */
4157 #if 0
4158 #define THIRD_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
4159 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
4160 FILE_READ_DATA|\
4161 WRITE_OWNER_ACCESS /* */
4162 #endif
4165 Test ntcreate calls made by xcopy
4167 static bool run_xcopy(int dummy)
4169 static struct cli_state *cli1;
4170 const char *fname = "\\test.txt";
4171 bool correct = True;
4172 uint16_t fnum1, fnum2;
4174 printf("starting xcopy test\n");
4176 if (!torture_open_connection(&cli1, 0)) {
4177 return False;
4180 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,
4181 FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
4182 FILE_SHARE_NONE, FILE_OVERWRITE_IF,
4183 0x4044, 0, &fnum1))) {
4184 printf("First open failed - %s\n", cli_errstr(cli1));
4185 return False;
4188 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,
4189 SECOND_DESIRED_ACCESS, 0,
4190 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN,
4191 0x200000, 0, &fnum2))) {
4192 printf("second open failed - %s\n", cli_errstr(cli1));
4193 return False;
4196 if (!torture_close_connection(cli1)) {
4197 correct = False;
4200 return correct;
4204 Test rename on files open with share delete and no share delete.
4206 static bool run_rename(int dummy)
4208 static struct cli_state *cli1;
4209 const char *fname = "\\test.txt";
4210 const char *fname1 = "\\test1.txt";
4211 bool correct = True;
4212 uint16_t fnum1;
4213 uint16_t attr;
4214 NTSTATUS status;
4216 printf("starting rename test\n");
4218 if (!torture_open_connection(&cli1, 0)) {
4219 return False;
4222 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4223 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
4224 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4225 FILE_SHARE_READ, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4226 printf("First open failed - %s\n", cli_errstr(cli1));
4227 return False;
4230 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
4231 printf("First rename failed (SHARE_READ) (this is correct) - %s\n", cli_errstr(cli1));
4232 } else {
4233 printf("First rename succeeded (SHARE_READ) - this should have failed !\n");
4234 correct = False;
4237 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4238 printf("close - 1 failed (%s)\n", cli_errstr(cli1));
4239 return False;
4242 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4243 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
4244 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4245 #if 0
4246 FILE_SHARE_DELETE|FILE_SHARE_NONE,
4247 #else
4248 FILE_SHARE_DELETE|FILE_SHARE_READ,
4249 #endif
4250 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4251 if (!NT_STATUS_IS_OK(status)) {
4252 printf("Second open failed - %s\n", cli_errstr(cli1));
4253 return False;
4256 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
4257 printf("Second rename failed (SHARE_DELETE | SHARE_READ) - this should have succeeded - %s\n", cli_errstr(cli1));
4258 correct = False;
4259 } else {
4260 printf("Second rename succeeded (SHARE_DELETE | SHARE_READ)\n");
4263 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4264 printf("close - 2 failed (%s)\n", cli_errstr(cli1));
4265 return False;
4268 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4269 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
4271 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, READ_CONTROL_ACCESS, FILE_ATTRIBUTE_NORMAL,
4272 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4273 printf("Third open failed - %s\n", cli_errstr(cli1));
4274 return False;
4278 #if 0
4280 uint16_t fnum2;
4282 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
4283 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
4284 printf("Fourth open failed - %s\n", cli_errstr(cli1));
4285 return False;
4287 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum2, true))) {
4288 printf("[8] setting delete_on_close on file failed !\n");
4289 return False;
4292 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
4293 printf("close - 4 failed (%s)\n", cli_errstr(cli1));
4294 return False;
4297 #endif
4299 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
4300 printf("Third rename failed (SHARE_NONE) - this should have succeeded - %s\n", cli_errstr(cli1));
4301 correct = False;
4302 } else {
4303 printf("Third rename succeeded (SHARE_NONE)\n");
4306 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4307 printf("close - 3 failed (%s)\n", cli_errstr(cli1));
4308 return False;
4311 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4312 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
4314 /*----*/
4316 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4317 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4318 printf("Fourth open failed - %s\n", cli_errstr(cli1));
4319 return False;
4322 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
4323 printf("Fourth rename failed (SHARE_READ | SHARE_WRITE) (this is correct) - %s\n", cli_errstr(cli1));
4324 } else {
4325 printf("Fourth rename succeeded (SHARE_READ | SHARE_WRITE) - this should have failed !\n");
4326 correct = False;
4329 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4330 printf("close - 4 failed (%s)\n", cli_errstr(cli1));
4331 return False;
4334 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4335 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
4337 /*--*/
4339 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4340 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4341 printf("Fifth open failed - %s\n", cli_errstr(cli1));
4342 return False;
4345 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
4346 printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have succeeded - %s ! \n",
4347 cli_errstr(cli1));
4348 correct = False;
4349 } else {
4350 printf("Fifth rename succeeded (SHARE_READ | SHARE_WRITE | SHARE_DELETE) (this is correct) - %s\n", cli_errstr(cli1));
4354 * Now check if the first name still exists ...
4357 /* if (!NT_STATUS_OP(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4358 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
4359 printf("Opening original file after rename of open file fails: %s\n",
4360 cli_errstr(cli1));
4362 else {
4363 printf("Opening original file after rename of open file works ...\n");
4364 (void)cli_close(cli1, fnum2);
4365 } */
4367 /*--*/
4368 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4369 printf("close - 5 failed (%s)\n", cli_errstr(cli1));
4370 return False;
4373 /* Check that the renamed file has FILE_ATTRIBUTE_ARCHIVE. */
4374 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname1, &attr, NULL, NULL))) {
4375 printf("getatr on file %s failed - %s ! \n",
4376 fname1,
4377 cli_errstr(cli1));
4378 correct = False;
4379 } else {
4380 if (attr != FILE_ATTRIBUTE_ARCHIVE) {
4381 printf("Renamed file %s has wrong attr 0x%x "
4382 "(should be 0x%x)\n",
4383 fname1,
4384 attr,
4385 (unsigned int)FILE_ATTRIBUTE_ARCHIVE);
4386 correct = False;
4387 } else {
4388 printf("Renamed file %s has archive bit set\n", fname1);
4392 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4393 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
4395 if (!torture_close_connection(cli1)) {
4396 correct = False;
4399 return correct;
4402 static bool run_pipe_number(int dummy)
4404 struct cli_state *cli1;
4405 const char *pipe_name = "\\SPOOLSS";
4406 uint16_t fnum;
4407 int num_pipes = 0;
4409 printf("starting pipenumber test\n");
4410 if (!torture_open_connection(&cli1, 0)) {
4411 return False;
4414 cli_sockopt(cli1, sockops);
4415 while(1) {
4416 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, pipe_name, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
4417 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF, 0, 0, &fnum))) {
4418 printf("Open of pipe %s failed with error (%s)\n", pipe_name, cli_errstr(cli1));
4419 break;
4421 num_pipes++;
4422 printf("\r%6d", num_pipes);
4425 printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
4426 torture_close_connection(cli1);
4427 return True;
4431 Test open mode returns on read-only files.
4433 static bool run_opentest(int dummy)
4435 static struct cli_state *cli1;
4436 static struct cli_state *cli2;
4437 const char *fname = "\\readonly.file";
4438 uint16_t fnum1, fnum2;
4439 char buf[20];
4440 SMB_OFF_T fsize;
4441 bool correct = True;
4442 char *tmp_path;
4443 NTSTATUS status;
4445 printf("starting open test\n");
4447 if (!torture_open_connection(&cli1, 0)) {
4448 return False;
4451 cli_setatr(cli1, fname, 0, 0);
4452 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4454 cli_sockopt(cli1, sockops);
4456 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
4457 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4458 return False;
4461 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4462 printf("close2 failed (%s)\n", cli_errstr(cli1));
4463 return False;
4466 if (!NT_STATUS_IS_OK(cli_setatr(cli1, fname, aRONLY, 0))) {
4467 printf("cli_setatr failed (%s)\n", cli_errstr(cli1));
4468 return False;
4471 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1))) {
4472 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4473 return False;
4476 /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
4477 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4479 if (check_error(__LINE__, cli1, ERRDOS, ERRnoaccess,
4480 NT_STATUS_ACCESS_DENIED)) {
4481 printf("correct error code ERRDOS/ERRnoaccess returned\n");
4484 printf("finished open test 1\n");
4486 cli_close(cli1, fnum1);
4488 /* Now try not readonly and ensure ERRbadshare is returned. */
4490 cli_setatr(cli1, fname, 0, 0);
4492 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1))) {
4493 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4494 return False;
4497 /* This will fail - but the error should be ERRshare. */
4498 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4500 if (check_error(__LINE__, cli1, ERRDOS, ERRbadshare,
4501 NT_STATUS_SHARING_VIOLATION)) {
4502 printf("correct error code ERRDOS/ERRbadshare returned\n");
4505 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4506 printf("close2 failed (%s)\n", cli_errstr(cli1));
4507 return False;
4510 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4512 printf("finished open test 2\n");
4514 /* Test truncate open disposition on file opened for read. */
4516 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
4517 printf("(3) open (1) of %s failed (%s)\n", fname, cli_errstr(cli1));
4518 return False;
4521 /* write 20 bytes. */
4523 memset(buf, '\0', 20);
4525 if (cli_write(cli1, fnum1, 0, buf, 0, 20) != 20) {
4526 printf("write failed (%s)\n", cli_errstr(cli1));
4527 correct = False;
4530 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4531 printf("(3) close1 failed (%s)\n", cli_errstr(cli1));
4532 return False;
4535 /* Ensure size == 20. */
4536 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
4537 printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
4538 return False;
4541 if (fsize != 20) {
4542 printf("(3) file size != 20\n");
4543 return False;
4546 /* Now test if we can truncate a file opened for readonly. */
4548 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY|O_TRUNC, DENY_NONE, &fnum1))) {
4549 printf("(3) open (2) of %s failed (%s)\n", fname, cli_errstr(cli1));
4550 return False;
4553 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4554 printf("close2 failed (%s)\n", cli_errstr(cli1));
4555 return False;
4558 /* Ensure size == 0. */
4559 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
4560 printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
4561 return False;
4564 if (fsize != 0) {
4565 printf("(3) file size != 0\n");
4566 return False;
4568 printf("finished open test 3\n");
4570 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4572 printf("Do ctemp tests\n");
4573 if (!NT_STATUS_IS_OK(cli_ctemp(cli1, talloc_tos(), "\\", &fnum1, &tmp_path))) {
4574 printf("ctemp failed (%s)\n", cli_errstr(cli1));
4575 return False;
4577 printf("ctemp gave path %s\n", tmp_path);
4578 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4579 printf("close of temp failed (%s)\n", cli_errstr(cli1));
4581 if (!NT_STATUS_IS_OK(cli_unlink(cli1, tmp_path, aSYSTEM | aHIDDEN))) {
4582 printf("unlink of temp failed (%s)\n", cli_errstr(cli1));
4585 /* Test the non-io opens... */
4587 if (!torture_open_connection(&cli2, 1)) {
4588 return False;
4591 cli_setatr(cli2, fname, 0, 0);
4592 cli_unlink(cli2, fname, aSYSTEM | aHIDDEN);
4594 cli_sockopt(cli2, sockops);
4596 printf("TEST #1 testing 2 non-io opens (no delete)\n");
4598 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4599 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4600 printf("TEST #1 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4601 return False;
4604 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4605 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4606 printf("TEST #1 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4607 return False;
4610 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4611 printf("TEST #1 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4612 return False;
4614 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4615 printf("TEST #1 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4616 return False;
4619 printf("non-io open test #1 passed.\n");
4621 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4623 printf("TEST #2 testing 2 non-io opens (first with delete)\n");
4625 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4626 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4627 printf("TEST #2 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4628 return False;
4631 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4632 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4633 printf("TEST #2 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4634 return False;
4637 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4638 printf("TEST #2 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4639 return False;
4641 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4642 printf("TEST #2 close 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
4643 return False;
4646 printf("non-io open test #2 passed.\n");
4648 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4650 printf("TEST #3 testing 2 non-io opens (second with delete)\n");
4652 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4653 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4654 printf("TEST #3 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4655 return False;
4658 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4659 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4660 printf("TEST #3 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4661 return False;
4664 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4665 printf("TEST #3 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4666 return False;
4668 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4669 printf("TEST #3 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4670 return False;
4673 printf("non-io open test #3 passed.\n");
4675 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4677 printf("TEST #4 testing 2 non-io opens (both with delete)\n");
4679 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4680 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4681 printf("TEST #4 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4682 return False;
4685 if (NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4686 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4687 printf("TEST #4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
4688 return False;
4691 printf("TEST #4 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
4693 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4694 printf("TEST #4 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4695 return False;
4698 printf("non-io open test #4 passed.\n");
4700 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4702 printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
4704 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4705 FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4706 printf("TEST #5 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4707 return False;
4710 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4711 FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4712 printf("TEST #5 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4713 return False;
4716 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4717 printf("TEST #5 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4718 return False;
4721 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4722 printf("TEST #5 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4723 return False;
4726 printf("non-io open test #5 passed.\n");
4728 printf("TEST #6 testing 1 non-io open, one io open\n");
4730 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4732 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
4733 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4734 printf("TEST #6 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4735 return False;
4738 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4739 FILE_SHARE_READ, FILE_OPEN_IF, 0, 0, &fnum2))) {
4740 printf("TEST #6 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4741 return False;
4744 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4745 printf("TEST #6 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4746 return False;
4749 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4750 printf("TEST #6 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4751 return False;
4754 printf("non-io open test #6 passed.\n");
4756 printf("TEST #7 testing 1 non-io open, one io open with delete\n");
4758 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4760 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
4761 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4762 printf("TEST #7 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4763 return False;
4766 if (NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4767 FILE_SHARE_READ|FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4768 printf("TEST #7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
4769 return False;
4772 printf("TEST #7 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
4774 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4775 printf("TEST #7 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4776 return False;
4779 printf("non-io open test #7 passed.\n");
4781 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4783 printf("TEST #8 testing open without WRITE_ATTRIBUTES, updating close write time.\n");
4784 status = cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL,
4785 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4786 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4787 if (!NT_STATUS_IS_OK(status)) {
4788 printf("TEST #8 open of %s failed (%s)\n", fname, nt_errstr(status));
4789 correct = false;
4790 goto out;
4793 /* Write to ensure we have to update the file time. */
4794 if (cli_write(cli1, fnum1, 0, "TEST DATA\n", 0, 10) != 10) {
4795 printf("TEST #8 cli_write failed: %s\n", cli_errstr(cli1));
4796 correct = false;
4797 goto out;
4800 status = cli_close(cli1, fnum1);
4801 if (!NT_STATUS_IS_OK(status)) {
4802 printf("TEST #8 close of %s failed (%s)\n", fname, nt_errstr(status));
4803 correct = false;
4806 out:
4808 if (!torture_close_connection(cli1)) {
4809 correct = False;
4811 if (!torture_close_connection(cli2)) {
4812 correct = False;
4815 return correct;
4818 NTSTATUS torture_setup_unix_extensions(struct cli_state *cli)
4820 uint16 major, minor;
4821 uint32 caplow, caphigh;
4822 NTSTATUS status;
4824 if (!SERVER_HAS_UNIX_CIFS(cli)) {
4825 printf("Server doesn't support UNIX CIFS extensions.\n");
4826 return NT_STATUS_NOT_SUPPORTED;
4829 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
4830 &caphigh);
4831 if (!NT_STATUS_IS_OK(status)) {
4832 printf("Server didn't return UNIX CIFS extensions: %s\n",
4833 nt_errstr(status));
4834 return status;
4837 status = cli_set_unix_extensions_capabilities(cli, major, minor,
4838 caplow, caphigh);
4839 if (!NT_STATUS_IS_OK(status)) {
4840 printf("Server doesn't support setting UNIX CIFS extensions: "
4841 "%s.\n", nt_errstr(status));
4842 return status;
4845 return NT_STATUS_OK;
4849 Test POSIX open /mkdir calls.
4851 static bool run_simple_posix_open_test(int dummy)
4853 static struct cli_state *cli1;
4854 const char *fname = "posix:file";
4855 const char *hname = "posix:hlink";
4856 const char *sname = "posix:symlink";
4857 const char *dname = "posix:dir";
4858 char buf[10];
4859 char namebuf[11];
4860 uint16_t fnum1 = (uint16_t)-1;
4861 SMB_STRUCT_STAT sbuf;
4862 bool correct = false;
4863 NTSTATUS status;
4865 printf("Starting simple POSIX open test\n");
4867 if (!torture_open_connection(&cli1, 0)) {
4868 return false;
4871 cli_sockopt(cli1, sockops);
4873 status = torture_setup_unix_extensions(cli1);
4874 if (!NT_STATUS_IS_OK(status)) {
4875 return false;
4878 cli_setatr(cli1, fname, 0, 0);
4879 cli_posix_unlink(cli1, fname);
4880 cli_setatr(cli1, dname, 0, 0);
4881 cli_posix_rmdir(cli1, dname);
4882 cli_setatr(cli1, hname, 0, 0);
4883 cli_posix_unlink(cli1, hname);
4884 cli_setatr(cli1, sname, 0, 0);
4885 cli_posix_unlink(cli1, sname);
4887 /* Create a directory. */
4888 if (!NT_STATUS_IS_OK(cli_posix_mkdir(cli1, dname, 0777))) {
4889 printf("POSIX mkdir of %s failed (%s)\n", dname, cli_errstr(cli1));
4890 goto out;
4893 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1))) {
4894 printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
4895 goto out;
4898 /* Test ftruncate - set file size. */
4899 if (!NT_STATUS_IS_OK(cli_ftruncate(cli1, fnum1, 1000))) {
4900 printf("ftruncate failed (%s)\n", cli_errstr(cli1));
4901 goto out;
4904 /* Ensure st_size == 1000 */
4905 if (!NT_STATUS_IS_OK(cli_posix_stat(cli1, fname, &sbuf))) {
4906 printf("stat failed (%s)\n", cli_errstr(cli1));
4907 goto out;
4910 if (sbuf.st_ex_size != 1000) {
4911 printf("ftruncate - stat size (%u) != 1000\n", (unsigned int)sbuf.st_ex_size);
4912 goto out;
4915 /* Test ftruncate - set file size back to zero. */
4916 if (!NT_STATUS_IS_OK(cli_ftruncate(cli1, fnum1, 0))) {
4917 printf("ftruncate failed (%s)\n", cli_errstr(cli1));
4918 goto out;
4921 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4922 printf("close failed (%s)\n", cli_errstr(cli1));
4923 goto out;
4926 /* Now open the file again for read only. */
4927 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1))) {
4928 printf("POSIX open of %s failed (%s)\n", fname, cli_errstr(cli1));
4929 goto out;
4932 /* Now unlink while open. */
4933 if (!NT_STATUS_IS_OK(cli_posix_unlink(cli1, fname))) {
4934 printf("POSIX unlink of %s failed (%s)\n", fname, cli_errstr(cli1));
4935 goto out;
4938 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4939 printf("close(2) failed (%s)\n", cli_errstr(cli1));
4940 goto out;
4943 /* Ensure the file has gone. */
4944 if (NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1))) {
4945 printf("POSIX open of %s succeeded, should have been deleted.\n", fname);
4946 goto out;
4949 /* What happens when we try and POSIX open a directory ? */
4950 if (NT_STATUS_IS_OK(cli_posix_open(cli1, dname, O_RDONLY, 0, &fnum1))) {
4951 printf("POSIX open of directory %s succeeded, should have failed.\n", fname);
4952 goto out;
4953 } else {
4954 if (!check_error(__LINE__, cli1, ERRDOS, EISDIR,
4955 NT_STATUS_FILE_IS_A_DIRECTORY)) {
4956 goto out;
4960 /* Create the file. */
4961 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1))) {
4962 printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
4963 goto out;
4966 /* Write some data into it. */
4967 if (cli_write(cli1, fnum1, 0, "TEST DATA\n", 0, 10) != 10) {
4968 printf("cli_write failed: %s\n", cli_errstr(cli1));
4969 goto out;
4972 cli_close(cli1, fnum1);
4974 /* Now create a hardlink. */
4975 if (!NT_STATUS_IS_OK(cli_posix_hardlink(cli1, fname, hname))) {
4976 printf("POSIX hardlink of %s failed (%s)\n", hname, cli_errstr(cli1));
4977 goto out;
4980 /* Now create a symlink. */
4981 if (!NT_STATUS_IS_OK(cli_posix_symlink(cli1, fname, sname))) {
4982 printf("POSIX symlink of %s failed (%s)\n", sname, cli_errstr(cli1));
4983 goto out;
4986 /* Open the hardlink for read. */
4987 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, hname, O_RDONLY, 0, &fnum1))) {
4988 printf("POSIX open of %s failed (%s)\n", hname, cli_errstr(cli1));
4989 goto out;
4992 if (cli_read(cli1, fnum1, buf, 0, 10) != 10) {
4993 printf("POSIX read of %s failed (%s)\n", hname, cli_errstr(cli1));
4994 goto out;
4997 if (memcmp(buf, "TEST DATA\n", 10)) {
4998 printf("invalid data read from hardlink\n");
4999 goto out;
5002 /* Do a POSIX lock/unlock. */
5003 if (!NT_STATUS_IS_OK(cli_posix_lock(cli1, fnum1, 0, 100, true, READ_LOCK))) {
5004 printf("POSIX lock failed %s\n", cli_errstr(cli1));
5005 goto out;
5008 /* Punch a hole in the locked area. */
5009 if (!NT_STATUS_IS_OK(cli_posix_unlock(cli1, fnum1, 10, 80))) {
5010 printf("POSIX unlock failed %s\n", cli_errstr(cli1));
5011 goto out;
5014 cli_close(cli1, fnum1);
5016 /* Open the symlink for read - this should fail. A POSIX
5017 client should not be doing opens on a symlink. */
5018 if (NT_STATUS_IS_OK(cli_posix_open(cli1, sname, O_RDONLY, 0, &fnum1))) {
5019 printf("POSIX open of %s succeeded (should have failed)\n", sname);
5020 goto out;
5021 } else {
5022 if (!check_error(__LINE__, cli1, ERRDOS, ERRbadpath,
5023 NT_STATUS_OBJECT_PATH_NOT_FOUND)) {
5024 printf("POSIX open of %s should have failed "
5025 "with NT_STATUS_OBJECT_PATH_NOT_FOUND, "
5026 "failed with %s instead.\n",
5027 sname, cli_errstr(cli1));
5028 goto out;
5032 if (!NT_STATUS_IS_OK(cli_posix_readlink(cli1, sname, namebuf, sizeof(namebuf)))) {
5033 printf("POSIX readlink on %s failed (%s)\n", sname, cli_errstr(cli1));
5034 goto out;
5037 if (strcmp(namebuf, fname) != 0) {
5038 printf("POSIX readlink on %s failed to match name %s (read %s)\n",
5039 sname, fname, namebuf);
5040 goto out;
5043 if (!NT_STATUS_IS_OK(cli_posix_rmdir(cli1, dname))) {
5044 printf("POSIX rmdir failed (%s)\n", cli_errstr(cli1));
5045 goto out;
5048 printf("Simple POSIX open test passed\n");
5049 correct = true;
5051 out:
5053 if (fnum1 != (uint16_t)-1) {
5054 cli_close(cli1, fnum1);
5055 fnum1 = (uint16_t)-1;
5058 cli_setatr(cli1, sname, 0, 0);
5059 cli_posix_unlink(cli1, sname);
5060 cli_setatr(cli1, hname, 0, 0);
5061 cli_posix_unlink(cli1, hname);
5062 cli_setatr(cli1, fname, 0, 0);
5063 cli_posix_unlink(cli1, fname);
5064 cli_setatr(cli1, dname, 0, 0);
5065 cli_posix_rmdir(cli1, dname);
5067 if (!torture_close_connection(cli1)) {
5068 correct = false;
5071 return correct;
5075 static uint32 open_attrs_table[] = {
5076 FILE_ATTRIBUTE_NORMAL,
5077 FILE_ATTRIBUTE_ARCHIVE,
5078 FILE_ATTRIBUTE_READONLY,
5079 FILE_ATTRIBUTE_HIDDEN,
5080 FILE_ATTRIBUTE_SYSTEM,
5082 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY,
5083 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN,
5084 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,
5085 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
5086 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
5087 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
5089 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
5090 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
5091 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
5092 FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_SYSTEM,
5095 struct trunc_open_results {
5096 unsigned int num;
5097 uint32 init_attr;
5098 uint32 trunc_attr;
5099 uint32 result_attr;
5102 static struct trunc_open_results attr_results[] = {
5103 { 0, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
5104 { 1, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
5105 { 2, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
5106 { 16, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
5107 { 17, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
5108 { 18, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
5109 { 51, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5110 { 54, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5111 { 56, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
5112 { 68, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5113 { 71, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5114 { 73, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
5115 { 99, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5116 { 102, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5117 { 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 },
5118 { 116, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5119 { 119, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5120 { 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 },
5121 { 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 },
5122 { 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 },
5123 { 227, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5124 { 230, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5125 { 232, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
5126 { 244, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5127 { 247, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5128 { 249, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM }
5131 static bool run_openattrtest(int dummy)
5133 static struct cli_state *cli1;
5134 const char *fname = "\\openattr.file";
5135 uint16_t fnum1;
5136 bool correct = True;
5137 uint16 attr;
5138 unsigned int i, j, k, l;
5140 printf("starting open attr test\n");
5142 if (!torture_open_connection(&cli1, 0)) {
5143 return False;
5146 cli_sockopt(cli1, sockops);
5148 for (k = 0, i = 0; i < sizeof(open_attrs_table)/sizeof(uint32); i++) {
5149 cli_setatr(cli1, fname, 0, 0);
5150 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
5151 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA, open_attrs_table[i],
5152 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
5153 printf("open %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
5154 return False;
5157 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
5158 printf("close %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
5159 return False;
5162 for (j = 0; j < sizeof(open_attrs_table)/sizeof(uint32); j++) {
5163 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA, open_attrs_table[j],
5164 FILE_SHARE_NONE, FILE_OVERWRITE, 0, 0, &fnum1))) {
5165 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
5166 if (attr_results[l].num == k) {
5167 printf("[%d] trunc open 0x%x -> 0x%x of %s failed - should have succeeded !(0x%x:%s)\n",
5168 k, open_attrs_table[i],
5169 open_attrs_table[j],
5170 fname, NT_STATUS_V(cli_nt_error(cli1)), cli_errstr(cli1));
5171 correct = False;
5174 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
5175 printf("[%d] trunc open 0x%x -> 0x%x failed with wrong error code %s\n",
5176 k, open_attrs_table[i], open_attrs_table[j],
5177 cli_errstr(cli1));
5178 correct = False;
5180 #if 0
5181 printf("[%d] trunc open 0x%x -> 0x%x failed\n", k, open_attrs_table[i], open_attrs_table[j]);
5182 #endif
5183 k++;
5184 continue;
5187 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
5188 printf("close %d (2) of %s failed (%s)\n", j, fname, cli_errstr(cli1));
5189 return False;
5192 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, &attr, NULL, NULL))) {
5193 printf("getatr(2) failed (%s)\n", cli_errstr(cli1));
5194 return False;
5197 #if 0
5198 printf("[%d] getatr check [0x%x] trunc [0x%x] got attr 0x%x\n",
5199 k, open_attrs_table[i], open_attrs_table[j], attr );
5200 #endif
5202 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
5203 if (attr_results[l].num == k) {
5204 if (attr != attr_results[l].result_attr ||
5205 open_attrs_table[i] != attr_results[l].init_attr ||
5206 open_attrs_table[j] != attr_results[l].trunc_attr) {
5207 printf("getatr check failed. [0x%x] trunc [0x%x] got attr 0x%x, should be 0x%x\n",
5208 open_attrs_table[i],
5209 open_attrs_table[j],
5210 (unsigned int)attr,
5211 attr_results[l].result_attr);
5212 correct = False;
5214 break;
5217 k++;
5221 cli_setatr(cli1, fname, 0, 0);
5222 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
5224 printf("open attr test %s.\n", correct ? "passed" : "failed");
5226 if (!torture_close_connection(cli1)) {
5227 correct = False;
5229 return correct;
5232 static NTSTATUS list_fn(const char *mnt, struct file_info *finfo,
5233 const char *name, void *state)
5235 int *matched = (int *)state;
5236 if (matched != NULL) {
5237 *matched += 1;
5239 return NT_STATUS_OK;
5243 test directory listing speed
5245 static bool run_dirtest(int dummy)
5247 int i;
5248 static struct cli_state *cli;
5249 uint16_t fnum;
5250 struct timeval core_start;
5251 bool correct = True;
5252 int matched;
5254 printf("starting directory test\n");
5256 if (!torture_open_connection(&cli, 0)) {
5257 return False;
5260 cli_sockopt(cli, sockops);
5262 srandom(0);
5263 for (i=0;i<torture_numops;i++) {
5264 fstring fname;
5265 slprintf(fname, sizeof(fname), "\\%x", (int)random());
5266 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum))) {
5267 fprintf(stderr,"Failed to open %s\n", fname);
5268 return False;
5270 cli_close(cli, fnum);
5273 core_start = timeval_current();
5275 matched = 0;
5276 cli_list(cli, "a*.*", 0, list_fn, &matched);
5277 printf("Matched %d\n", matched);
5279 matched = 0;
5280 cli_list(cli, "b*.*", 0, list_fn, &matched);
5281 printf("Matched %d\n", matched);
5283 matched = 0;
5284 cli_list(cli, "xyzabc", 0, list_fn, &matched);
5285 printf("Matched %d\n", matched);
5287 printf("dirtest core %g seconds\n", timeval_elapsed(&core_start));
5289 srandom(0);
5290 for (i=0;i<torture_numops;i++) {
5291 fstring fname;
5292 slprintf(fname, sizeof(fname), "\\%x", (int)random());
5293 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
5296 if (!torture_close_connection(cli)) {
5297 correct = False;
5300 printf("finished dirtest\n");
5302 return correct;
5305 static NTSTATUS del_fn(const char *mnt, struct file_info *finfo, const char *mask,
5306 void *state)
5308 struct cli_state *pcli = (struct cli_state *)state;
5309 fstring fname;
5310 slprintf(fname, sizeof(fname), "\\LISTDIR\\%s", finfo->name);
5312 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
5313 return NT_STATUS_OK;
5315 if (finfo->mode & aDIR) {
5316 if (!NT_STATUS_IS_OK(cli_rmdir(pcli, fname)))
5317 printf("del_fn: failed to rmdir %s\n,", fname );
5318 } else {
5319 if (!NT_STATUS_IS_OK(cli_unlink(pcli, fname, aSYSTEM | aHIDDEN)))
5320 printf("del_fn: failed to unlink %s\n,", fname );
5322 return NT_STATUS_OK;
5327 sees what IOCTLs are supported
5329 bool torture_ioctl_test(int dummy)
5331 static struct cli_state *cli;
5332 uint16_t device, function;
5333 uint16_t fnum;
5334 const char *fname = "\\ioctl.dat";
5335 DATA_BLOB blob;
5336 NTSTATUS status;
5338 if (!torture_open_connection(&cli, 0)) {
5339 return False;
5342 printf("starting ioctl test\n");
5344 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
5346 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
5347 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
5348 return False;
5351 status = cli_raw_ioctl(cli, fnum, 0x2d0000 | (0x0420<<2), &blob);
5352 printf("ioctl device info: %s\n", nt_errstr(status));
5354 status = cli_raw_ioctl(cli, fnum, IOCTL_QUERY_JOB_INFO, &blob);
5355 printf("ioctl job info: %s\n", nt_errstr(status));
5357 for (device=0;device<0x100;device++) {
5358 printf("ioctl test with device = 0x%x\n", device);
5359 for (function=0;function<0x100;function++) {
5360 uint32 code = (device<<16) | function;
5362 status = cli_raw_ioctl(cli, fnum, code, &blob);
5364 if (NT_STATUS_IS_OK(status)) {
5365 printf("ioctl 0x%x OK : %d bytes\n", (int)code,
5366 (int)blob.length);
5367 data_blob_free(&blob);
5372 if (!torture_close_connection(cli)) {
5373 return False;
5376 return True;
5381 tries varients of chkpath
5383 bool torture_chkpath_test(int dummy)
5385 static struct cli_state *cli;
5386 uint16_t fnum;
5387 bool ret;
5389 if (!torture_open_connection(&cli, 0)) {
5390 return False;
5393 printf("starting chkpath test\n");
5395 /* cleanup from an old run */
5396 cli_rmdir(cli, "\\chkpath.dir\\dir2");
5397 cli_unlink(cli, "\\chkpath.dir\\*", aSYSTEM | aHIDDEN);
5398 cli_rmdir(cli, "\\chkpath.dir");
5400 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\chkpath.dir"))) {
5401 printf("mkdir1 failed : %s\n", cli_errstr(cli));
5402 return False;
5405 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\chkpath.dir\\dir2"))) {
5406 printf("mkdir2 failed : %s\n", cli_errstr(cli));
5407 return False;
5410 if (!NT_STATUS_IS_OK(cli_open(cli, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
5411 printf("open1 failed (%s)\n", cli_errstr(cli));
5412 return False;
5414 cli_close(cli, fnum);
5416 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir"))) {
5417 printf("chkpath1 failed: %s\n", cli_errstr(cli));
5418 ret = False;
5421 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dir2"))) {
5422 printf("chkpath2 failed: %s\n", cli_errstr(cli));
5423 ret = False;
5426 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\foo.txt"))) {
5427 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
5428 NT_STATUS_NOT_A_DIRECTORY);
5429 } else {
5430 printf("* chkpath on a file should fail\n");
5431 ret = False;
5434 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\bar.txt"))) {
5435 ret = check_error(__LINE__, cli, ERRDOS, ERRbadfile,
5436 NT_STATUS_OBJECT_NAME_NOT_FOUND);
5437 } else {
5438 printf("* chkpath on a non existant file should fail\n");
5439 ret = False;
5442 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dirxx\\bar.txt"))) {
5443 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
5444 NT_STATUS_OBJECT_PATH_NOT_FOUND);
5445 } else {
5446 printf("* chkpath on a non existent component should fail\n");
5447 ret = False;
5450 cli_rmdir(cli, "\\chkpath.dir\\dir2");
5451 cli_unlink(cli, "\\chkpath.dir\\*", aSYSTEM | aHIDDEN);
5452 cli_rmdir(cli, "\\chkpath.dir");
5454 if (!torture_close_connection(cli)) {
5455 return False;
5458 return ret;
5461 static bool run_eatest(int dummy)
5463 static struct cli_state *cli;
5464 const char *fname = "\\eatest.txt";
5465 bool correct = True;
5466 uint16_t fnum;
5467 int i;
5468 size_t num_eas;
5469 struct ea_struct *ea_list = NULL;
5470 TALLOC_CTX *mem_ctx = talloc_init("eatest");
5471 NTSTATUS status;
5473 printf("starting eatest\n");
5475 if (!torture_open_connection(&cli, 0)) {
5476 talloc_destroy(mem_ctx);
5477 return False;
5480 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
5481 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0,
5482 FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
5483 FILE_SHARE_NONE, FILE_OVERWRITE_IF,
5484 0x4044, 0, &fnum))) {
5485 printf("open failed - %s\n", cli_errstr(cli));
5486 talloc_destroy(mem_ctx);
5487 return False;
5490 for (i = 0; i < 10; i++) {
5491 fstring ea_name, ea_val;
5493 slprintf(ea_name, sizeof(ea_name), "EA_%d", i);
5494 memset(ea_val, (char)i+1, i+1);
5495 status = cli_set_ea_fnum(cli, fnum, ea_name, ea_val, i+1);
5496 if (!NT_STATUS_IS_OK(status)) {
5497 printf("ea_set of name %s failed - %s\n", ea_name,
5498 nt_errstr(status));
5499 talloc_destroy(mem_ctx);
5500 return False;
5504 cli_close(cli, fnum);
5505 for (i = 0; i < 10; i++) {
5506 fstring ea_name, ea_val;
5508 slprintf(ea_name, sizeof(ea_name), "EA_%d", i+10);
5509 memset(ea_val, (char)i+1, i+1);
5510 status = cli_set_ea_path(cli, fname, ea_name, ea_val, i+1);
5511 if (!NT_STATUS_IS_OK(status)) {
5512 printf("ea_set of name %s failed - %s\n", ea_name,
5513 nt_errstr(status));
5514 talloc_destroy(mem_ctx);
5515 return False;
5519 status = cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list);
5520 if (!NT_STATUS_IS_OK(status)) {
5521 printf("ea_get list failed - %s\n", nt_errstr(status));
5522 correct = False;
5525 printf("num_eas = %d\n", (int)num_eas);
5527 if (num_eas != 20) {
5528 printf("Should be 20 EA's stored... failing.\n");
5529 correct = False;
5532 for (i = 0; i < num_eas; i++) {
5533 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
5534 dump_data(0, ea_list[i].value.data,
5535 ea_list[i].value.length);
5538 /* Setting EA's to zero length deletes them. Test this */
5539 printf("Now deleting all EA's - case indepenent....\n");
5541 #if 1
5542 cli_set_ea_path(cli, fname, "", "", 0);
5543 #else
5544 for (i = 0; i < 20; i++) {
5545 fstring ea_name;
5546 slprintf(ea_name, sizeof(ea_name), "ea_%d", i);
5547 status = cli_set_ea_path(cli, fname, ea_name, "", 0);
5548 if (!NT_STATUS_IS_OK(status)) {
5549 printf("ea_set of name %s failed - %s\n", ea_name,
5550 nt_errstr(status));
5551 talloc_destroy(mem_ctx);
5552 return False;
5555 #endif
5557 status = cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list);
5558 if (!NT_STATUS_IS_OK(status)) {
5559 printf("ea_get list failed - %s\n", nt_errstr(status));
5560 correct = False;
5563 printf("num_eas = %d\n", (int)num_eas);
5564 for (i = 0; i < num_eas; i++) {
5565 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
5566 dump_data(0, ea_list[i].value.data,
5567 ea_list[i].value.length);
5570 if (num_eas != 0) {
5571 printf("deleting EA's failed.\n");
5572 correct = False;
5575 /* Try and delete a non existant EA. */
5576 status = cli_set_ea_path(cli, fname, "foo", "", 0);
5577 if (!NT_STATUS_IS_OK(status)) {
5578 printf("deleting non-existant EA 'foo' should succeed. %s\n",
5579 nt_errstr(status));
5580 correct = False;
5583 talloc_destroy(mem_ctx);
5584 if (!torture_close_connection(cli)) {
5585 correct = False;
5588 return correct;
5591 static bool run_dirtest1(int dummy)
5593 int i;
5594 static struct cli_state *cli;
5595 uint16_t fnum;
5596 int num_seen;
5597 bool correct = True;
5599 printf("starting directory test\n");
5601 if (!torture_open_connection(&cli, 0)) {
5602 return False;
5605 cli_sockopt(cli, sockops);
5607 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
5608 cli_list(cli, "\\LISTDIR\\*", aDIR, del_fn, cli);
5609 cli_rmdir(cli, "\\LISTDIR");
5610 cli_mkdir(cli, "\\LISTDIR");
5612 /* Create 1000 files and 1000 directories. */
5613 for (i=0;i<1000;i++) {
5614 fstring fname;
5615 slprintf(fname, sizeof(fname), "\\LISTDIR\\f%d", i);
5616 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
5617 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
5618 fprintf(stderr,"Failed to open %s\n", fname);
5619 return False;
5621 cli_close(cli, fnum);
5623 for (i=0;i<1000;i++) {
5624 fstring fname;
5625 slprintf(fname, sizeof(fname), "\\LISTDIR\\d%d", i);
5626 if (!NT_STATUS_IS_OK(cli_mkdir(cli, fname))) {
5627 fprintf(stderr,"Failed to open %s\n", fname);
5628 return False;
5632 /* Now ensure that doing an old list sees both files and directories. */
5633 num_seen = 0;
5634 cli_list_old(cli, "\\LISTDIR\\*", aDIR, list_fn, &num_seen);
5635 printf("num_seen = %d\n", num_seen );
5636 /* We should see 100 files + 1000 directories + . and .. */
5637 if (num_seen != 2002)
5638 correct = False;
5640 /* Ensure if we have the "must have" bits we only see the
5641 * relevent entries.
5643 num_seen = 0;
5644 cli_list_old(cli, "\\LISTDIR\\*", (aDIR<<8)|aDIR, list_fn, &num_seen);
5645 printf("num_seen = %d\n", num_seen );
5646 if (num_seen != 1002)
5647 correct = False;
5649 num_seen = 0;
5650 cli_list_old(cli, "\\LISTDIR\\*", (aARCH<<8)|aDIR, list_fn, &num_seen);
5651 printf("num_seen = %d\n", num_seen );
5652 if (num_seen != 1000)
5653 correct = False;
5655 /* Delete everything. */
5656 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
5657 cli_list(cli, "\\LISTDIR\\*", aDIR, del_fn, cli);
5658 cli_rmdir(cli, "\\LISTDIR");
5660 #if 0
5661 printf("Matched %d\n", cli_list(cli, "a*.*", 0, list_fn, NULL));
5662 printf("Matched %d\n", cli_list(cli, "b*.*", 0, list_fn, NULL));
5663 printf("Matched %d\n", cli_list(cli, "xyzabc", 0, list_fn, NULL));
5664 #endif
5666 if (!torture_close_connection(cli)) {
5667 correct = False;
5670 printf("finished dirtest1\n");
5672 return correct;
5675 static bool run_error_map_extract(int dummy) {
5677 static struct cli_state *c_dos;
5678 static struct cli_state *c_nt;
5679 NTSTATUS status;
5681 uint32 error;
5683 uint32 flgs2, errnum;
5684 uint8 errclass;
5686 NTSTATUS nt_status;
5688 fstring user;
5690 /* NT-Error connection */
5692 if (!(c_nt = open_nbt_connection())) {
5693 return False;
5696 c_nt->use_spnego = False;
5698 status = cli_negprot(c_nt);
5700 if (!NT_STATUS_IS_OK(status)) {
5701 printf("%s rejected the NT-error negprot (%s)\n", host,
5702 nt_errstr(status));
5703 cli_shutdown(c_nt);
5704 return False;
5707 if (!NT_STATUS_IS_OK(cli_session_setup(c_nt, "", "", 0, "", 0,
5708 workgroup))) {
5709 printf("%s rejected the NT-error initial session setup (%s)\n",host, cli_errstr(c_nt));
5710 return False;
5713 /* DOS-Error connection */
5715 if (!(c_dos = open_nbt_connection())) {
5716 return False;
5719 c_dos->use_spnego = False;
5720 c_dos->force_dos_errors = True;
5722 status = cli_negprot(c_dos);
5723 if (!NT_STATUS_IS_OK(status)) {
5724 printf("%s rejected the DOS-error negprot (%s)\n", host,
5725 nt_errstr(status));
5726 cli_shutdown(c_dos);
5727 return False;
5730 if (!NT_STATUS_IS_OK(cli_session_setup(c_dos, "", "", 0, "", 0,
5731 workgroup))) {
5732 printf("%s rejected the DOS-error initial session setup (%s)\n",host, cli_errstr(c_dos));
5733 return False;
5736 for (error=(0xc0000000 | 0x1); error < (0xc0000000| 0xFFF); error++) {
5737 fstr_sprintf(user, "%X", error);
5739 if (NT_STATUS_IS_OK(cli_session_setup(c_nt, user,
5740 password, strlen(password),
5741 password, strlen(password),
5742 workgroup))) {
5743 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
5746 flgs2 = SVAL(c_nt->inbuf,smb_flg2);
5748 /* Case #1: 32-bit NT errors */
5749 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
5750 nt_status = NT_STATUS(IVAL(c_nt->inbuf,smb_rcls));
5751 } else {
5752 printf("/** Dos error on NT connection! (%s) */\n",
5753 cli_errstr(c_nt));
5754 nt_status = NT_STATUS(0xc0000000);
5757 if (NT_STATUS_IS_OK(cli_session_setup(c_dos, user,
5758 password, strlen(password),
5759 password, strlen(password),
5760 workgroup))) {
5761 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
5763 flgs2 = SVAL(c_dos->inbuf,smb_flg2), errnum;
5765 /* Case #1: 32-bit NT errors */
5766 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
5767 printf("/** NT error on DOS connection! (%s) */\n",
5768 cli_errstr(c_nt));
5769 errnum = errclass = 0;
5770 } else {
5771 cli_dos_error(c_dos, &errclass, &errnum);
5774 if (NT_STATUS_V(nt_status) != error) {
5775 printf("/*\t{ This NT error code was 'sqashed'\n\t from %s to %s \n\t during the session setup }\n*/\n",
5776 get_nt_error_c_code(NT_STATUS(error)),
5777 get_nt_error_c_code(nt_status));
5780 printf("\t{%s,\t%s,\t%s},\n",
5781 smb_dos_err_class(errclass),
5782 smb_dos_err_name(errclass, errnum),
5783 get_nt_error_c_code(NT_STATUS(error)));
5785 return True;
5788 static bool run_sesssetup_bench(int dummy)
5790 static struct cli_state *c;
5791 const char *fname = "\\file.dat";
5792 uint16_t fnum;
5793 NTSTATUS status;
5794 int i;
5796 if (!torture_open_connection(&c, 0)) {
5797 return false;
5800 if (!NT_STATUS_IS_OK(cli_ntcreate(
5801 c, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
5802 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
5803 FILE_DELETE_ON_CLOSE, 0, &fnum))) {
5804 d_printf("open %s failed: %s\n", fname, cli_errstr(c));
5805 return false;
5808 for (i=0; i<torture_numops; i++) {
5809 status = cli_session_setup(
5810 c, username,
5811 password, strlen(password),
5812 password, strlen(password),
5813 workgroup);
5814 if (!NT_STATUS_IS_OK(status)) {
5815 d_printf("(%s) cli_session_setup failed: %s\n",
5816 __location__, nt_errstr(status));
5817 return false;
5820 d_printf("\r%d ", (int)c->vuid);
5822 status = cli_ulogoff(c);
5823 if (!NT_STATUS_IS_OK(status)) {
5824 d_printf("(%s) cli_ulogoff failed: %s\n",
5825 __location__, nt_errstr(status));
5826 return false;
5828 c->vuid = 0;
5831 return true;
5834 static bool subst_test(const char *str, const char *user, const char *domain,
5835 uid_t uid, gid_t gid, const char *expected)
5837 char *subst;
5838 bool result = true;
5840 subst = talloc_sub_specified(talloc_tos(), str, user, domain, uid, gid);
5842 if (strcmp(subst, expected) != 0) {
5843 printf("sub_specified(%s, %s, %s, %d, %d) returned [%s], expected "
5844 "[%s]\n", str, user, domain, (int)uid, (int)gid, subst,
5845 expected);
5846 result = false;
5849 TALLOC_FREE(subst);
5850 return result;
5853 static void chain1_open_completion(struct tevent_req *req)
5855 uint16_t fnum;
5856 NTSTATUS status;
5857 status = cli_open_recv(req, &fnum);
5858 TALLOC_FREE(req);
5860 d_printf("cli_open_recv returned %s: %d\n",
5861 nt_errstr(status),
5862 NT_STATUS_IS_OK(status) ? fnum : -1);
5865 static void chain1_write_completion(struct tevent_req *req)
5867 size_t written;
5868 NTSTATUS status;
5869 status = cli_write_andx_recv(req, &written);
5870 TALLOC_FREE(req);
5872 d_printf("cli_write_andx_recv returned %s: %d\n",
5873 nt_errstr(status),
5874 NT_STATUS_IS_OK(status) ? (int)written : -1);
5877 static void chain1_close_completion(struct tevent_req *req)
5879 NTSTATUS status;
5880 bool *done = (bool *)tevent_req_callback_data_void(req);
5882 status = cli_close_recv(req);
5883 *done = true;
5885 TALLOC_FREE(req);
5887 d_printf("cli_close returned %s\n", nt_errstr(status));
5890 static bool run_chain1(int dummy)
5892 struct cli_state *cli1;
5893 struct event_context *evt = event_context_init(NULL);
5894 struct tevent_req *reqs[3], *smbreqs[3];
5895 bool done = false;
5896 const char *str = "foobar";
5897 NTSTATUS status;
5899 printf("starting chain1 test\n");
5900 if (!torture_open_connection(&cli1, 0)) {
5901 return False;
5904 cli_sockopt(cli1, sockops);
5906 reqs[0] = cli_open_create(talloc_tos(), evt, cli1, "\\test",
5907 O_CREAT|O_RDWR, 0, &smbreqs[0]);
5908 if (reqs[0] == NULL) return false;
5909 tevent_req_set_callback(reqs[0], chain1_open_completion, NULL);
5912 reqs[1] = cli_write_andx_create(talloc_tos(), evt, cli1, 0, 0,
5913 (uint8_t *)str, 0, strlen(str)+1,
5914 smbreqs, 1, &smbreqs[1]);
5915 if (reqs[1] == NULL) return false;
5916 tevent_req_set_callback(reqs[1], chain1_write_completion, NULL);
5918 reqs[2] = cli_close_create(talloc_tos(), evt, cli1, 0, &smbreqs[2]);
5919 if (reqs[2] == NULL) return false;
5920 tevent_req_set_callback(reqs[2], chain1_close_completion, &done);
5922 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
5923 if (!NT_STATUS_IS_OK(status)) {
5924 return false;
5927 while (!done) {
5928 event_loop_once(evt);
5931 torture_close_connection(cli1);
5932 return True;
5935 static void chain2_sesssetup_completion(struct tevent_req *req)
5937 NTSTATUS status;
5938 status = cli_session_setup_guest_recv(req);
5939 d_printf("sesssetup returned %s\n", nt_errstr(status));
5942 static void chain2_tcon_completion(struct tevent_req *req)
5944 bool *done = (bool *)tevent_req_callback_data_void(req);
5945 NTSTATUS status;
5946 status = cli_tcon_andx_recv(req);
5947 d_printf("tcon_and_x returned %s\n", nt_errstr(status));
5948 *done = true;
5951 static bool run_chain2(int dummy)
5953 struct cli_state *cli1;
5954 struct event_context *evt = event_context_init(NULL);
5955 struct tevent_req *reqs[2], *smbreqs[2];
5956 bool done = false;
5957 NTSTATUS status;
5959 printf("starting chain2 test\n");
5960 status = cli_start_connection(&cli1, global_myname(), host, NULL,
5961 port_to_use, Undefined, 0);
5962 if (!NT_STATUS_IS_OK(status)) {
5963 return False;
5966 cli_sockopt(cli1, sockops);
5968 reqs[0] = cli_session_setup_guest_create(talloc_tos(), evt, cli1,
5969 &smbreqs[0]);
5970 if (reqs[0] == NULL) return false;
5971 tevent_req_set_callback(reqs[0], chain2_sesssetup_completion, NULL);
5973 reqs[1] = cli_tcon_andx_create(talloc_tos(), evt, cli1, "IPC$",
5974 "?????", NULL, 0, &smbreqs[1]);
5975 if (reqs[1] == NULL) return false;
5976 tevent_req_set_callback(reqs[1], chain2_tcon_completion, &done);
5978 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
5979 if (!NT_STATUS_IS_OK(status)) {
5980 return false;
5983 while (!done) {
5984 event_loop_once(evt);
5987 torture_close_connection(cli1);
5988 return True;
5992 struct torture_createdel_state {
5993 struct tevent_context *ev;
5994 struct cli_state *cli;
5997 static void torture_createdel_created(struct tevent_req *subreq);
5998 static void torture_createdel_closed(struct tevent_req *subreq);
6000 static struct tevent_req *torture_createdel_send(TALLOC_CTX *mem_ctx,
6001 struct tevent_context *ev,
6002 struct cli_state *cli,
6003 const char *name)
6005 struct tevent_req *req, *subreq;
6006 struct torture_createdel_state *state;
6008 req = tevent_req_create(mem_ctx, &state,
6009 struct torture_createdel_state);
6010 if (req == NULL) {
6011 return NULL;
6013 state->ev = ev;
6014 state->cli = cli;
6016 subreq = cli_ntcreate_send(
6017 state, ev, cli, name, 0,
6018 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
6019 FILE_ATTRIBUTE_NORMAL,
6020 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
6021 FILE_OPEN_IF, FILE_DELETE_ON_CLOSE, 0);
6023 if (tevent_req_nomem(subreq, req)) {
6024 return tevent_req_post(req, ev);
6026 tevent_req_set_callback(subreq, torture_createdel_created, req);
6027 return req;
6030 static void torture_createdel_created(struct tevent_req *subreq)
6032 struct tevent_req *req = tevent_req_callback_data(
6033 subreq, struct tevent_req);
6034 struct torture_createdel_state *state = tevent_req_data(
6035 req, struct torture_createdel_state);
6036 NTSTATUS status;
6037 uint16_t fnum;
6039 status = cli_ntcreate_recv(subreq, &fnum);
6040 TALLOC_FREE(subreq);
6041 if (!NT_STATUS_IS_OK(status)) {
6042 DEBUG(10, ("cli_ntcreate_recv returned %s\n",
6043 nt_errstr(status)));
6044 tevent_req_nterror(req, status);
6045 return;
6048 subreq = cli_close_send(state, state->ev, state->cli, fnum);
6049 if (tevent_req_nomem(subreq, req)) {
6050 return;
6052 tevent_req_set_callback(subreq, torture_createdel_closed, req);
6055 static void torture_createdel_closed(struct tevent_req *subreq)
6057 struct tevent_req *req = tevent_req_callback_data(
6058 subreq, struct tevent_req);
6059 NTSTATUS status;
6061 status = cli_close_recv(subreq);
6062 if (!NT_STATUS_IS_OK(status)) {
6063 DEBUG(10, ("cli_close_recv returned %s\n", nt_errstr(status)));
6064 tevent_req_nterror(req, status);
6065 return;
6067 tevent_req_done(req);
6070 static NTSTATUS torture_createdel_recv(struct tevent_req *req)
6072 return tevent_req_simple_recv_ntstatus(req);
6075 struct torture_createdels_state {
6076 struct tevent_context *ev;
6077 struct cli_state *cli;
6078 const char *base_name;
6079 int sent;
6080 int received;
6081 int num_files;
6082 struct tevent_req **reqs;
6085 static void torture_createdels_done(struct tevent_req *subreq);
6087 static struct tevent_req *torture_createdels_send(TALLOC_CTX *mem_ctx,
6088 struct tevent_context *ev,
6089 struct cli_state *cli,
6090 const char *base_name,
6091 int num_parallel,
6092 int num_files)
6094 struct tevent_req *req;
6095 struct torture_createdels_state *state;
6096 int i;
6098 req = tevent_req_create(mem_ctx, &state,
6099 struct torture_createdels_state);
6100 if (req == NULL) {
6101 return NULL;
6103 state->ev = ev;
6104 state->cli = cli;
6105 state->base_name = talloc_strdup(state, base_name);
6106 if (tevent_req_nomem(state->base_name, req)) {
6107 return tevent_req_post(req, ev);
6109 state->num_files = MAX(num_parallel, num_files);
6110 state->sent = 0;
6111 state->received = 0;
6113 state->reqs = talloc_array(state, struct tevent_req *, num_parallel);
6114 if (tevent_req_nomem(state->reqs, req)) {
6115 return tevent_req_post(req, ev);
6118 for (i=0; i<num_parallel; i++) {
6119 char *name;
6121 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
6122 state->sent);
6123 if (tevent_req_nomem(name, req)) {
6124 return tevent_req_post(req, ev);
6126 state->reqs[i] = torture_createdel_send(
6127 state->reqs, state->ev, state->cli, name);
6128 if (tevent_req_nomem(state->reqs[i], req)) {
6129 return tevent_req_post(req, ev);
6131 name = talloc_move(state->reqs[i], &name);
6132 tevent_req_set_callback(state->reqs[i],
6133 torture_createdels_done, req);
6134 state->sent += 1;
6136 return req;
6139 static void torture_createdels_done(struct tevent_req *subreq)
6141 struct tevent_req *req = tevent_req_callback_data(
6142 subreq, struct tevent_req);
6143 struct torture_createdels_state *state = tevent_req_data(
6144 req, struct torture_createdels_state);
6145 size_t num_parallel = talloc_array_length(state->reqs);
6146 NTSTATUS status;
6147 char *name;
6148 int i;
6150 status = torture_createdel_recv(subreq);
6151 if (!NT_STATUS_IS_OK(status)){
6152 DEBUG(10, ("torture_createdel_recv returned %s\n",
6153 nt_errstr(status)));
6154 TALLOC_FREE(subreq);
6155 tevent_req_nterror(req, status);
6156 return;
6159 for (i=0; i<num_parallel; i++) {
6160 if (subreq == state->reqs[i]) {
6161 break;
6164 if (i == num_parallel) {
6165 DEBUG(10, ("received something we did not send\n"));
6166 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
6167 return;
6169 TALLOC_FREE(state->reqs[i]);
6171 if (state->sent >= state->num_files) {
6172 tevent_req_done(req);
6173 return;
6176 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
6177 state->sent);
6178 if (tevent_req_nomem(name, req)) {
6179 return;
6181 state->reqs[i] = torture_createdel_send(state->reqs, state->ev,
6182 state->cli, name);
6183 if (tevent_req_nomem(state->reqs[i], req)) {
6184 return;
6186 name = talloc_move(state->reqs[i], &name);
6187 tevent_req_set_callback(state->reqs[i], torture_createdels_done, req);
6188 state->sent += 1;
6191 static NTSTATUS torture_createdels_recv(struct tevent_req *req)
6193 return tevent_req_simple_recv_ntstatus(req);
6196 struct swallow_notify_state {
6197 struct tevent_context *ev;
6198 struct cli_state *cli;
6199 uint16_t fnum;
6200 uint32_t completion_filter;
6201 bool recursive;
6202 bool (*fn)(uint32_t action, const char *name, void *priv);
6203 void *priv;
6206 static void swallow_notify_done(struct tevent_req *subreq);
6208 static struct tevent_req *swallow_notify_send(TALLOC_CTX *mem_ctx,
6209 struct tevent_context *ev,
6210 struct cli_state *cli,
6211 uint16_t fnum,
6212 uint32_t completion_filter,
6213 bool recursive,
6214 bool (*fn)(uint32_t action,
6215 const char *name,
6216 void *priv),
6217 void *priv)
6219 struct tevent_req *req, *subreq;
6220 struct swallow_notify_state *state;
6222 req = tevent_req_create(mem_ctx, &state,
6223 struct swallow_notify_state);
6224 if (req == NULL) {
6225 return NULL;
6227 state->ev = ev;
6228 state->cli = cli;
6229 state->fnum = fnum;
6230 state->completion_filter = completion_filter;
6231 state->recursive = recursive;
6232 state->fn = fn;
6233 state->priv = priv;
6235 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
6236 0xffff, state->completion_filter,
6237 state->recursive);
6238 if (tevent_req_nomem(subreq, req)) {
6239 return tevent_req_post(req, ev);
6241 tevent_req_set_callback(subreq, swallow_notify_done, req);
6242 return req;
6245 static void swallow_notify_done(struct tevent_req *subreq)
6247 struct tevent_req *req = tevent_req_callback_data(
6248 subreq, struct tevent_req);
6249 struct swallow_notify_state *state = tevent_req_data(
6250 req, struct swallow_notify_state);
6251 NTSTATUS status;
6252 uint32_t i, num_changes;
6253 struct notify_change *changes;
6255 status = cli_notify_recv(subreq, state, &num_changes, &changes);
6256 TALLOC_FREE(subreq);
6257 if (!NT_STATUS_IS_OK(status)) {
6258 DEBUG(10, ("cli_notify_recv returned %s\n",
6259 nt_errstr(status)));
6260 tevent_req_nterror(req, status);
6261 return;
6264 for (i=0; i<num_changes; i++) {
6265 state->fn(changes[i].action, changes[i].name, state->priv);
6267 TALLOC_FREE(changes);
6269 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
6270 0xffff, state->completion_filter,
6271 state->recursive);
6272 if (tevent_req_nomem(subreq, req)) {
6273 return;
6275 tevent_req_set_callback(subreq, swallow_notify_done, req);
6278 static bool print_notifies(uint32_t action, const char *name, void *priv)
6280 if (DEBUGLEVEL > 5) {
6281 d_printf("%d %s\n", (int)action, name);
6283 return true;
6286 static void notify_bench_done(struct tevent_req *req)
6288 int *num_finished = (int *)tevent_req_callback_data_void(req);
6289 *num_finished += 1;
6292 static bool run_notify_bench(int dummy)
6294 const char *dname = "\\notify-bench";
6295 struct tevent_context *ev;
6296 NTSTATUS status;
6297 uint16_t dnum;
6298 struct tevent_req *req1;
6299 struct tevent_req *req2 = NULL;
6300 int i, num_unc_names;
6301 int num_finished = 0;
6303 printf("starting notify-bench test\n");
6305 if (use_multishare_conn) {
6306 char **unc_list;
6307 unc_list = file_lines_load(multishare_conn_fname,
6308 &num_unc_names, 0, NULL);
6309 if (!unc_list || num_unc_names <= 0) {
6310 d_printf("Failed to load unc names list from '%s'\n",
6311 multishare_conn_fname);
6312 return false;
6314 TALLOC_FREE(unc_list);
6315 } else {
6316 num_unc_names = 1;
6319 ev = tevent_context_init(talloc_tos());
6320 if (ev == NULL) {
6321 d_printf("tevent_context_init failed\n");
6322 return false;
6325 for (i=0; i<num_unc_names; i++) {
6326 struct cli_state *cli;
6327 char *base_fname;
6329 base_fname = talloc_asprintf(talloc_tos(), "%s\\file%3.3d.",
6330 dname, i);
6331 if (base_fname == NULL) {
6332 return false;
6335 if (!torture_open_connection(&cli, i)) {
6336 return false;
6339 status = cli_ntcreate(cli, dname, 0,
6340 MAXIMUM_ALLOWED_ACCESS,
6341 0, FILE_SHARE_READ|FILE_SHARE_WRITE|
6342 FILE_SHARE_DELETE,
6343 FILE_OPEN_IF, FILE_DIRECTORY_FILE, 0,
6344 &dnum);
6346 if (!NT_STATUS_IS_OK(status)) {
6347 d_printf("Could not create %s: %s\n", dname,
6348 nt_errstr(status));
6349 return false;
6352 req1 = swallow_notify_send(talloc_tos(), ev, cli, dnum,
6353 FILE_NOTIFY_CHANGE_FILE_NAME |
6354 FILE_NOTIFY_CHANGE_DIR_NAME |
6355 FILE_NOTIFY_CHANGE_ATTRIBUTES |
6356 FILE_NOTIFY_CHANGE_LAST_WRITE,
6357 false, print_notifies, NULL);
6358 if (req1 == NULL) {
6359 d_printf("Could not create notify request\n");
6360 return false;
6363 req2 = torture_createdels_send(talloc_tos(), ev, cli,
6364 base_fname, 10, torture_numops);
6365 if (req2 == NULL) {
6366 d_printf("Could not create createdels request\n");
6367 return false;
6369 TALLOC_FREE(base_fname);
6371 tevent_req_set_callback(req2, notify_bench_done,
6372 &num_finished);
6375 while (num_finished < num_unc_names) {
6376 int ret;
6377 ret = tevent_loop_once(ev);
6378 if (ret != 0) {
6379 d_printf("tevent_loop_once failed\n");
6380 return false;
6384 if (!tevent_req_poll(req2, ev)) {
6385 d_printf("tevent_req_poll failed\n");
6388 status = torture_createdels_recv(req2);
6389 d_printf("torture_createdels_recv returned %s\n", nt_errstr(status));
6391 return true;
6394 static bool run_mangle1(int dummy)
6396 struct cli_state *cli;
6397 const char *fname = "this_is_a_long_fname_to_be_mangled.txt";
6398 uint16_t fnum;
6399 fstring alt_name;
6400 NTSTATUS status;
6401 time_t change_time, access_time, write_time;
6402 SMB_OFF_T size;
6403 uint16_t mode;
6405 printf("starting mangle1 test\n");
6406 if (!torture_open_connection(&cli, 0)) {
6407 return False;
6410 cli_sockopt(cli, sockops);
6412 if (!NT_STATUS_IS_OK(cli_ntcreate(
6413 cli, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
6414 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
6415 d_printf("open %s failed: %s\n", fname, cli_errstr(cli));
6416 return false;
6418 cli_close(cli, fnum);
6420 status = cli_qpathinfo_alt_name(cli, fname, alt_name);
6421 if (!NT_STATUS_IS_OK(status)) {
6422 d_printf("cli_qpathinfo_alt_name failed: %s\n",
6423 nt_errstr(status));
6424 return false;
6426 d_printf("alt_name: %s\n", alt_name);
6428 if (!NT_STATUS_IS_OK(cli_open(cli, alt_name, O_RDONLY, DENY_NONE, &fnum))) {
6429 d_printf("cli_open(%s) failed: %s\n", alt_name,
6430 cli_errstr(cli));
6431 return false;
6433 cli_close(cli, fnum);
6435 status = cli_qpathinfo1(cli, alt_name, &change_time, &access_time,
6436 &write_time, &size, &mode);
6437 if (!NT_STATUS_IS_OK(status)) {
6438 d_printf("cli_qpathinfo1(%s) failed: %s\n", alt_name,
6439 nt_errstr(status));
6440 return false;
6443 return true;
6446 static size_t null_source(uint8_t *buf, size_t n, void *priv)
6448 size_t *to_pull = (size_t *)priv;
6449 size_t thistime = *to_pull;
6451 thistime = MIN(thistime, n);
6452 if (thistime == 0) {
6453 return 0;
6456 memset(buf, 0, thistime);
6457 *to_pull -= thistime;
6458 return thistime;
6461 static bool run_windows_write(int dummy)
6463 struct cli_state *cli1;
6464 uint16_t fnum;
6465 int i;
6466 bool ret = false;
6467 const char *fname = "\\writetest.txt";
6468 struct timeval start_time;
6469 double seconds;
6470 double kbytes;
6472 printf("starting windows_write test\n");
6473 if (!torture_open_connection(&cli1, 0)) {
6474 return False;
6477 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
6478 printf("open failed (%s)\n", cli_errstr(cli1));
6479 return False;
6482 cli_sockopt(cli1, sockops);
6484 start_time = timeval_current();
6486 for (i=0; i<torture_numops; i++) {
6487 char c = 0;
6488 off_t start = i * torture_blocksize;
6489 NTSTATUS status;
6490 size_t to_pull = torture_blocksize - 1;
6492 if (cli_write(cli1, fnum, 0, &c,
6493 start + torture_blocksize - 1, 1) != 1) {
6494 printf("cli_write failed: %s\n", cli_errstr(cli1));
6495 goto fail;
6498 status = cli_push(cli1, fnum, 0, i * torture_blocksize, torture_blocksize,
6499 null_source, &to_pull);
6500 if (!NT_STATUS_IS_OK(status)) {
6501 printf("cli_push returned: %s\n", nt_errstr(status));
6502 goto fail;
6506 seconds = timeval_elapsed(&start_time);
6507 kbytes = (double)torture_blocksize * torture_numops;
6508 kbytes /= 1024;
6510 printf("Wrote %d kbytes in %.2f seconds: %d kb/sec\n", (int)kbytes,
6511 (double)seconds, (int)(kbytes/seconds));
6513 ret = true;
6514 fail:
6515 cli_close(cli1, fnum);
6516 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
6517 torture_close_connection(cli1);
6518 return ret;
6521 static bool run_cli_echo(int dummy)
6523 struct cli_state *cli;
6524 NTSTATUS status;
6526 printf("starting cli_echo test\n");
6527 if (!torture_open_connection(&cli, 0)) {
6528 return false;
6530 cli_sockopt(cli, sockops);
6532 status = cli_echo(cli, 5, data_blob_const("hello", 5));
6534 d_printf("cli_echo returned %s\n", nt_errstr(status));
6536 torture_close_connection(cli);
6537 return NT_STATUS_IS_OK(status);
6540 static bool run_uid_regression_test(int dummy)
6542 static struct cli_state *cli;
6543 int16_t old_vuid;
6544 int16_t old_cnum;
6545 bool correct = True;
6546 NTSTATUS status;
6548 printf("starting uid regression test\n");
6550 if (!torture_open_connection(&cli, 0)) {
6551 return False;
6554 cli_sockopt(cli, sockops);
6556 /* Ok - now save then logoff our current user. */
6557 old_vuid = cli->vuid;
6559 status = cli_ulogoff(cli);
6560 if (!NT_STATUS_IS_OK(status)) {
6561 d_printf("(%s) cli_ulogoff failed: %s\n",
6562 __location__, nt_errstr(status));
6563 correct = false;
6564 goto out;
6567 cli->vuid = old_vuid;
6569 /* Try an operation. */
6570 status = cli_mkdir(cli, "\\uid_reg_test");
6571 if (NT_STATUS_IS_OK(status)) {
6572 d_printf("(%s) cli_mkdir succeeded\n",
6573 __location__);
6574 correct = false;
6575 goto out;
6576 } else {
6577 /* Should be bad uid. */
6578 if (!check_error(__LINE__, cli, ERRSRV, ERRbaduid,
6579 NT_STATUS_USER_SESSION_DELETED)) {
6580 correct = false;
6581 goto out;
6585 old_cnum = cli->cnum;
6587 /* Now try a SMBtdis with the invald vuid set to zero. */
6588 cli->vuid = 0;
6590 /* This should succeed. */
6591 status = cli_tdis(cli);
6593 if (NT_STATUS_IS_OK(status)) {
6594 d_printf("First tdis with invalid vuid should succeed.\n");
6595 } else {
6596 d_printf("First tdis failed (%s)\n", nt_errstr(status));
6597 correct = false;
6598 goto out;
6601 cli->vuid = old_vuid;
6602 cli->cnum = old_cnum;
6604 /* This should fail. */
6605 status = cli_tdis(cli);
6606 if (NT_STATUS_IS_OK(status)) {
6607 d_printf("Second tdis with invalid vuid should fail - succeeded instead !.\n");
6608 correct = false;
6609 goto out;
6610 } else {
6611 /* Should be bad tid. */
6612 if (!check_error(__LINE__, cli, ERRSRV, ERRinvnid,
6613 NT_STATUS_NETWORK_NAME_DELETED)) {
6614 correct = false;
6615 goto out;
6619 cli_rmdir(cli, "\\uid_reg_test");
6621 out:
6623 cli_shutdown(cli);
6624 return correct;
6628 static const char *illegal_chars = "*\\/?<>|\":";
6629 static char force_shortname_chars[] = " +,.[];=\177";
6631 static NTSTATUS shortname_del_fn(const char *mnt, struct file_info *finfo,
6632 const char *mask, void *state)
6634 struct cli_state *pcli = (struct cli_state *)state;
6635 fstring fname;
6636 NTSTATUS status = NT_STATUS_OK;
6638 slprintf(fname, sizeof(fname), "\\shortname\\%s", finfo->name);
6640 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
6641 return NT_STATUS_OK;
6643 if (finfo->mode & aDIR) {
6644 status = cli_rmdir(pcli, fname);
6645 if (!NT_STATUS_IS_OK(status)) {
6646 printf("del_fn: failed to rmdir %s\n,", fname );
6648 } else {
6649 status = cli_unlink(pcli, fname, aSYSTEM | aHIDDEN);
6650 if (!NT_STATUS_IS_OK(status)) {
6651 printf("del_fn: failed to unlink %s\n,", fname );
6654 return status;
6657 struct sn_state {
6658 int matched;
6659 int i;
6660 bool val;
6663 static NTSTATUS shortname_list_fn(const char *mnt, struct file_info *finfo,
6664 const char *name, void *state)
6666 struct sn_state *s = (struct sn_state *)state;
6667 int i = s->i;
6669 #if 0
6670 printf("shortname list: i = %d, name = |%s|, shortname = |%s|\n",
6671 i, finfo->name, finfo->short_name);
6672 #endif
6674 if (strchr(force_shortname_chars, i)) {
6675 if (!finfo->short_name[0]) {
6676 /* Shortname not created when it should be. */
6677 d_printf("(%s) ERROR: Shortname was not created for file %s containing %d\n",
6678 __location__, finfo->name, i);
6679 s->val = true;
6681 } else if (finfo->short_name[0]){
6682 /* Shortname created when it should not be. */
6683 d_printf("(%s) ERROR: Shortname %s was created for file %s\n",
6684 __location__, finfo->short_name, finfo->name);
6685 s->val = true;
6687 s->matched += 1;
6688 return NT_STATUS_OK;
6691 static bool run_shortname_test(int dummy)
6693 static struct cli_state *cli;
6694 bool correct = True;
6695 int i;
6696 struct sn_state s;
6697 char fname[20];
6699 printf("starting shortname test\n");
6701 if (!torture_open_connection(&cli, 0)) {
6702 return False;
6705 cli_sockopt(cli, sockops);
6707 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
6708 cli_list(cli, "\\shortname\\*", aDIR, shortname_del_fn, cli);
6709 cli_rmdir(cli, "\\shortname");
6711 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\shortname"))) {
6712 d_printf("(%s) cli_mkdir of \\shortname failed: %s\n",
6713 __location__, cli_errstr(cli));
6714 correct = false;
6715 goto out;
6718 strlcpy(fname, "\\shortname\\", sizeof(fname));
6719 strlcat(fname, "test .txt", sizeof(fname));
6721 s.val = false;
6723 for (i = 32; i < 128; i++) {
6724 NTSTATUS status;
6725 uint16_t fnum = (uint16_t)-1;
6727 s.i = i;
6729 if (strchr(illegal_chars, i)) {
6730 continue;
6732 fname[15] = i;
6734 status = cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
6735 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum);
6736 if (!NT_STATUS_IS_OK(status)) {
6737 d_printf("(%s) cli_nt_create of %s failed: %s\n",
6738 __location__, fname, cli_errstr(cli));
6739 correct = false;
6740 goto out;
6742 cli_close(cli, fnum);
6744 s.matched = 0;
6745 cli_list(cli, "\\shortname\\test*.*", 0, shortname_list_fn,
6746 &s);
6747 if (s.matched != 1) {
6748 d_printf("(%s) failed to list %s: %s\n",
6749 __location__, fname, cli_errstr(cli));
6750 correct = false;
6751 goto out;
6753 if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
6754 d_printf("(%s) failed to delete %s: %s\n",
6755 __location__, fname, cli_errstr(cli));
6756 correct = false;
6757 goto out;
6760 if (s.val) {
6761 correct = false;
6762 goto out;
6766 out:
6768 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
6769 cli_list(cli, "\\shortname\\*", aDIR, shortname_del_fn, cli);
6770 cli_rmdir(cli, "\\shortname");
6771 torture_close_connection(cli);
6772 return correct;
6775 static void pagedsearch_cb(struct tevent_req *req)
6777 int rc;
6778 struct tldap_message *msg;
6779 char *dn;
6781 rc = tldap_search_paged_recv(req, talloc_tos(), &msg);
6782 if (rc != TLDAP_SUCCESS) {
6783 d_printf("tldap_search_paged_recv failed: %s\n",
6784 tldap_err2string(rc));
6785 return;
6787 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
6788 TALLOC_FREE(msg);
6789 return;
6791 if (!tldap_entry_dn(msg, &dn)) {
6792 d_printf("tldap_entry_dn failed\n");
6793 return;
6795 d_printf("%s\n", dn);
6796 TALLOC_FREE(msg);
6799 static bool run_tldap(int dummy)
6801 struct tldap_context *ld;
6802 int fd, rc;
6803 NTSTATUS status;
6804 struct sockaddr_storage addr;
6805 struct tevent_context *ev;
6806 struct tevent_req *req;
6807 char *basedn;
6808 const char *filter;
6810 if (!resolve_name(host, &addr, 0, false)) {
6811 d_printf("could not find host %s\n", host);
6812 return false;
6814 status = open_socket_out(&addr, 389, 9999, &fd);
6815 if (!NT_STATUS_IS_OK(status)) {
6816 d_printf("open_socket_out failed: %s\n", nt_errstr(status));
6817 return false;
6820 ld = tldap_context_create(talloc_tos(), fd);
6821 if (ld == NULL) {
6822 close(fd);
6823 d_printf("tldap_context_create failed\n");
6824 return false;
6827 rc = tldap_fetch_rootdse(ld);
6828 if (rc != TLDAP_SUCCESS) {
6829 d_printf("tldap_fetch_rootdse failed: %s\n",
6830 tldap_errstr(talloc_tos(), ld, rc));
6831 return false;
6834 basedn = tldap_talloc_single_attribute(
6835 tldap_rootdse(ld), "defaultNamingContext", talloc_tos());
6836 if (basedn == NULL) {
6837 d_printf("no defaultNamingContext\n");
6838 return false;
6840 d_printf("defaultNamingContext: %s\n", basedn);
6842 ev = tevent_context_init(talloc_tos());
6843 if (ev == NULL) {
6844 d_printf("tevent_context_init failed\n");
6845 return false;
6848 req = tldap_search_paged_send(talloc_tos(), ev, ld, basedn,
6849 TLDAP_SCOPE_SUB, "(objectclass=*)",
6850 NULL, 0, 0,
6851 NULL, 0, NULL, 0, 0, 0, 0, 5);
6852 if (req == NULL) {
6853 d_printf("tldap_search_paged_send failed\n");
6854 return false;
6856 tevent_req_set_callback(req, pagedsearch_cb, NULL);
6858 tevent_req_poll(req, ev);
6860 TALLOC_FREE(req);
6862 /* test search filters against rootDSE */
6863 filter = "(&(|(name=samba)(nextRid<=10000000)(usnChanged>=10)(samba~=ambas)(!(name=s*m*a)))"
6864 "(|(name:=samba)(name:dn:2.5.13.5:=samba)(:dn:2.5.13.5:=samba)(!(name=*samba))))";
6866 rc = tldap_search(ld, "", TLDAP_SCOPE_BASE, filter,
6867 NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0,
6868 talloc_tos(), NULL, NULL);
6869 if (rc != TLDAP_SUCCESS) {
6870 d_printf("tldap_search with complex filter failed: %s\n",
6871 tldap_errstr(talloc_tos(), ld, rc));
6872 return false;
6875 TALLOC_FREE(ld);
6876 return true;
6879 /* Torture test to ensure no regression of :
6880 https://bugzilla.samba.org/show_bug.cgi?id=7084
6883 static bool run_dir_createtime(int dummy)
6885 struct cli_state *cli;
6886 const char *dname = "\\testdir";
6887 const char *fname = "\\testdir\\testfile";
6888 NTSTATUS status;
6889 struct timespec create_time;
6890 struct timespec create_time1;
6891 uint16_t fnum;
6892 bool ret = false;
6894 if (!torture_open_connection(&cli, 0)) {
6895 return false;
6898 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
6899 cli_rmdir(cli, dname);
6901 status = cli_mkdir(cli, dname);
6902 if (!NT_STATUS_IS_OK(status)) {
6903 printf("mkdir failed: %s\n", nt_errstr(status));
6904 goto out;
6907 status = cli_qpathinfo2(cli, dname, &create_time, NULL, NULL, NULL,
6908 NULL, NULL, NULL);
6909 if (!NT_STATUS_IS_OK(status)) {
6910 printf("cli_qpathinfo2 returned %s\n",
6911 nt_errstr(status));
6912 goto out;
6915 /* Sleep 3 seconds, then create a file. */
6916 sleep(3);
6918 status = cli_open(cli, fname, O_RDWR | O_CREAT | O_EXCL,
6919 DENY_NONE, &fnum);
6920 if (!NT_STATUS_IS_OK(status)) {
6921 printf("cli_open failed: %s\n", nt_errstr(status));
6922 goto out;
6925 status = cli_qpathinfo2(cli, dname, &create_time1, NULL, NULL, NULL,
6926 NULL, NULL, NULL);
6927 if (!NT_STATUS_IS_OK(status)) {
6928 printf("cli_qpathinfo2 (2) returned %s\n",
6929 nt_errstr(status));
6930 goto out;
6933 if (timespec_compare(&create_time1, &create_time)) {
6934 printf("run_dir_createtime: create time was updated (error)\n");
6935 } else {
6936 printf("run_dir_createtime: create time was not updated (correct)\n");
6937 ret = true;
6940 out:
6942 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
6943 cli_rmdir(cli, dname);
6944 if (!torture_close_connection(cli)) {
6945 ret = false;
6947 return ret;
6951 static bool run_streamerror(int dummy)
6953 struct cli_state *cli;
6954 const char *dname = "\\testdir";
6955 const char *streamname =
6956 "testdir:{4c8cc155-6c1e-11d1-8e41-00c04fb9386d}:$DATA";
6957 NTSTATUS status;
6958 time_t change_time, access_time, write_time;
6959 SMB_OFF_T size;
6960 uint16_t mode, fnum;
6961 bool ret = true;
6963 if (!torture_open_connection(&cli, 0)) {
6964 return false;
6967 cli_unlink(cli, "\\testdir\\*", aSYSTEM | aHIDDEN);
6968 cli_rmdir(cli, dname);
6970 status = cli_mkdir(cli, dname);
6971 if (!NT_STATUS_IS_OK(status)) {
6972 printf("mkdir failed: %s\n", nt_errstr(status));
6973 return false;
6976 cli_qpathinfo1(cli, streamname, &change_time, &access_time, &write_time,
6977 &size, &mode);
6978 status = cli_nt_error(cli);
6980 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
6981 printf("pathinfo returned %s, expected "
6982 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
6983 nt_errstr(status));
6984 ret = false;
6987 status = cli_ntcreate(cli, streamname, 0x16,
6988 FILE_READ_DATA|FILE_READ_EA|
6989 FILE_READ_ATTRIBUTES|READ_CONTROL_ACCESS,
6990 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
6991 FILE_OPEN, 0, 0, &fnum);
6993 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
6994 printf("ntcreate returned %s, expected "
6995 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
6996 nt_errstr(status));
6997 ret = false;
7001 cli_rmdir(cli, dname);
7002 return ret;
7005 static bool run_local_substitute(int dummy)
7007 bool ok = true;
7009 ok &= subst_test("%U", "bla", "", -1, -1, "bla");
7010 ok &= subst_test("%u%U", "bla", "", -1, -1, "blabla");
7011 ok &= subst_test("%g", "", "", -1, -1, "NO_GROUP");
7012 ok &= subst_test("%G", "", "", -1, -1, "NO_GROUP");
7013 ok &= subst_test("%g", "", "", -1, 0, gidtoname(0));
7014 ok &= subst_test("%G", "", "", -1, 0, gidtoname(0));
7015 ok &= subst_test("%D%u", "u", "dom", -1, 0, "domu");
7016 ok &= subst_test("%i %I", "", "", -1, -1, "0.0.0.0 0.0.0.0");
7018 /* Different captialization rules in sub_basic... */
7020 ok &= (strcmp(talloc_sub_basic(talloc_tos(), "BLA", "dom", "%U%D"),
7021 "blaDOM") == 0);
7023 return ok;
7026 static bool run_local_base64(int dummy)
7028 int i;
7029 bool ret = true;
7031 for (i=1; i<2000; i++) {
7032 DATA_BLOB blob1, blob2;
7033 char *b64;
7035 blob1.data = talloc_array(talloc_tos(), uint8_t, i);
7036 blob1.length = i;
7037 generate_random_buffer(blob1.data, blob1.length);
7039 b64 = base64_encode_data_blob(talloc_tos(), blob1);
7040 if (b64 == NULL) {
7041 d_fprintf(stderr, "base64_encode_data_blob failed "
7042 "for %d bytes\n", i);
7043 ret = false;
7045 blob2 = base64_decode_data_blob(b64);
7046 TALLOC_FREE(b64);
7048 if (data_blob_cmp(&blob1, &blob2)) {
7049 d_fprintf(stderr, "data_blob_cmp failed for %d "
7050 "bytes\n", i);
7051 ret = false;
7053 TALLOC_FREE(blob1.data);
7054 data_blob_free(&blob2);
7056 return ret;
7059 static bool run_local_gencache(int dummy)
7061 char *val;
7062 time_t tm;
7063 DATA_BLOB blob;
7065 if (!gencache_set("foo", "bar", time(NULL) + 1000)) {
7066 d_printf("%s: gencache_set() failed\n", __location__);
7067 return False;
7070 if (!gencache_get("foo", NULL, NULL)) {
7071 d_printf("%s: gencache_get() failed\n", __location__);
7072 return False;
7075 if (!gencache_get("foo", &val, &tm)) {
7076 d_printf("%s: gencache_get() failed\n", __location__);
7077 return False;
7080 if (strcmp(val, "bar") != 0) {
7081 d_printf("%s: gencache_get() returned %s, expected %s\n",
7082 __location__, val, "bar");
7083 SAFE_FREE(val);
7084 return False;
7087 SAFE_FREE(val);
7089 if (!gencache_del("foo")) {
7090 d_printf("%s: gencache_del() failed\n", __location__);
7091 return False;
7093 if (gencache_del("foo")) {
7094 d_printf("%s: second gencache_del() succeeded\n",
7095 __location__);
7096 return False;
7099 if (gencache_get("foo", &val, &tm)) {
7100 d_printf("%s: gencache_get() on deleted entry "
7101 "succeeded\n", __location__);
7102 return False;
7105 blob = data_blob_string_const_null("bar");
7106 tm = time(NULL) + 60;
7108 if (!gencache_set_data_blob("foo", &blob, tm)) {
7109 d_printf("%s: gencache_set_data_blob() failed\n", __location__);
7110 return False;
7113 if (!gencache_get_data_blob("foo", &blob, NULL, NULL)) {
7114 d_printf("%s: gencache_get_data_blob() failed\n", __location__);
7115 return False;
7118 if (strcmp((const char *)blob.data, "bar") != 0) {
7119 d_printf("%s: gencache_get_data_blob() returned %s, expected %s\n",
7120 __location__, (const char *)blob.data, "bar");
7121 data_blob_free(&blob);
7122 return False;
7125 data_blob_free(&blob);
7127 if (!gencache_del("foo")) {
7128 d_printf("%s: gencache_del() failed\n", __location__);
7129 return False;
7131 if (gencache_del("foo")) {
7132 d_printf("%s: second gencache_del() succeeded\n",
7133 __location__);
7134 return False;
7137 if (gencache_get_data_blob("foo", &blob, NULL, NULL)) {
7138 d_printf("%s: gencache_get_data_blob() on deleted entry "
7139 "succeeded\n", __location__);
7140 return False;
7143 return True;
7146 static bool rbt_testval(struct db_context *db, const char *key,
7147 const char *value)
7149 struct db_record *rec;
7150 TDB_DATA data = string_tdb_data(value);
7151 bool ret = false;
7152 NTSTATUS status;
7154 rec = db->fetch_locked(db, db, string_tdb_data(key));
7155 if (rec == NULL) {
7156 d_fprintf(stderr, "fetch_locked failed\n");
7157 goto done;
7159 status = rec->store(rec, data, 0);
7160 if (!NT_STATUS_IS_OK(status)) {
7161 d_fprintf(stderr, "store failed: %s\n", nt_errstr(status));
7162 goto done;
7164 TALLOC_FREE(rec);
7166 rec = db->fetch_locked(db, db, string_tdb_data(key));
7167 if (rec == NULL) {
7168 d_fprintf(stderr, "second fetch_locked failed\n");
7169 goto done;
7171 if ((rec->value.dsize != data.dsize)
7172 || (memcmp(rec->value.dptr, data.dptr, data.dsize) != 0)) {
7173 d_fprintf(stderr, "Got wrong data back\n");
7174 goto done;
7177 ret = true;
7178 done:
7179 TALLOC_FREE(rec);
7180 return ret;
7183 static bool run_local_rbtree(int dummy)
7185 struct db_context *db;
7186 bool ret = false;
7187 int i;
7189 db = db_open_rbt(NULL);
7191 if (db == NULL) {
7192 d_fprintf(stderr, "db_open_rbt failed\n");
7193 return false;
7196 for (i=0; i<1000; i++) {
7197 char *key, *value;
7199 if (asprintf(&key, "key%ld", random()) == -1) {
7200 goto done;
7202 if (asprintf(&value, "value%ld", random()) == -1) {
7203 SAFE_FREE(key);
7204 goto done;
7207 if (!rbt_testval(db, key, value)) {
7208 SAFE_FREE(key);
7209 SAFE_FREE(value);
7210 goto done;
7213 SAFE_FREE(value);
7214 if (asprintf(&value, "value%ld", random()) == -1) {
7215 SAFE_FREE(key);
7216 goto done;
7219 if (!rbt_testval(db, key, value)) {
7220 SAFE_FREE(key);
7221 SAFE_FREE(value);
7222 goto done;
7225 SAFE_FREE(key);
7226 SAFE_FREE(value);
7229 ret = true;
7231 done:
7232 TALLOC_FREE(db);
7233 return ret;
7236 struct talloc_dict_test {
7237 int content;
7240 static int talloc_dict_traverse_fn(DATA_BLOB key, void *data, void *priv)
7242 int *count = (int *)priv;
7243 *count += 1;
7244 return 0;
7247 static bool run_local_talloc_dict(int dummy)
7249 struct talloc_dict *dict;
7250 struct talloc_dict_test *t;
7251 int key, count;
7253 dict = talloc_dict_init(talloc_tos());
7254 if (dict == NULL) {
7255 return false;
7258 t = talloc(talloc_tos(), struct talloc_dict_test);
7259 if (t == NULL) {
7260 return false;
7263 key = 1;
7264 t->content = 1;
7265 if (!talloc_dict_set(dict, data_blob_const(&key, sizeof(key)), t)) {
7266 return false;
7269 count = 0;
7270 if (talloc_dict_traverse(dict, talloc_dict_traverse_fn, &count) != 0) {
7271 return false;
7274 if (count != 1) {
7275 return false;
7278 TALLOC_FREE(dict);
7280 return true;
7283 static bool run_local_string_to_sid(int dummy) {
7284 struct dom_sid sid;
7286 if (string_to_sid(&sid, "S--1-5-32-545")) {
7287 printf("allowing S--1-5-32-545\n");
7288 return false;
7290 if (string_to_sid(&sid, "S-1-5-32-+545")) {
7291 printf("allowing S-1-5-32-+545\n");
7292 return false;
7294 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")) {
7295 printf("allowing S-1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0\n");
7296 return false;
7298 if (string_to_sid(&sid, "S-1-5-32-545-abc")) {
7299 printf("allowing S-1-5-32-545-abc\n");
7300 return false;
7302 if (!string_to_sid(&sid, "S-1-5-32-545")) {
7303 printf("could not parse S-1-5-32-545\n");
7304 return false;
7306 if (!dom_sid_equal(&sid, &global_sid_Builtin_Users)) {
7307 printf("mis-parsed S-1-5-32-545 as %s\n",
7308 sid_string_tos(&sid));
7309 return false;
7311 return true;
7314 static bool run_local_binary_to_sid(int dummy) {
7315 struct dom_sid *sid = talloc(NULL, struct dom_sid);
7316 static const char good_binary_sid[] = {
7317 0x1, /* revision number */
7318 15, /* num auths */
7319 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7320 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7321 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7322 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7323 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7324 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7325 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7326 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7327 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7328 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7329 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7330 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7331 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7332 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7333 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7334 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7337 static const char long_binary_sid[] = {
7338 0x1, /* revision number */
7339 15, /* num auths */
7340 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7341 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7342 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7343 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7344 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7345 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7346 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7347 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7348 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7349 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7350 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7351 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7352 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7353 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7354 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7355 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7356 0x1, 0x1, 0x1, 0x1, /* auth[15] */
7357 0x1, 0x1, 0x1, 0x1, /* auth[16] */
7358 0x1, 0x1, 0x1, 0x1, /* auth[17] */
7361 static const char long_binary_sid2[] = {
7362 0x1, /* revision number */
7363 32, /* num auths */
7364 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7365 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7366 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7367 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7368 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7369 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7370 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7371 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7372 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7373 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7374 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7375 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7376 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7377 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7378 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7379 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7380 0x1, 0x1, 0x1, 0x1, /* auth[15] */
7381 0x1, 0x1, 0x1, 0x1, /* auth[16] */
7382 0x1, 0x1, 0x1, 0x1, /* auth[17] */
7383 0x1, 0x1, 0x1, 0x1, /* auth[18] */
7384 0x1, 0x1, 0x1, 0x1, /* auth[19] */
7385 0x1, 0x1, 0x1, 0x1, /* auth[20] */
7386 0x1, 0x1, 0x1, 0x1, /* auth[21] */
7387 0x1, 0x1, 0x1, 0x1, /* auth[22] */
7388 0x1, 0x1, 0x1, 0x1, /* auth[23] */
7389 0x1, 0x1, 0x1, 0x1, /* auth[24] */
7390 0x1, 0x1, 0x1, 0x1, /* auth[25] */
7391 0x1, 0x1, 0x1, 0x1, /* auth[26] */
7392 0x1, 0x1, 0x1, 0x1, /* auth[27] */
7393 0x1, 0x1, 0x1, 0x1, /* auth[28] */
7394 0x1, 0x1, 0x1, 0x1, /* auth[29] */
7395 0x1, 0x1, 0x1, 0x1, /* auth[30] */
7396 0x1, 0x1, 0x1, 0x1, /* auth[31] */
7399 if (!sid_parse(good_binary_sid, sizeof(good_binary_sid), sid)) {
7400 return false;
7402 if (sid_parse(long_binary_sid2, sizeof(long_binary_sid2), sid)) {
7403 return false;
7405 if (sid_parse(long_binary_sid, sizeof(long_binary_sid), sid)) {
7406 return false;
7408 return true;
7411 /* Split a path name into filename and stream name components. Canonicalise
7412 * such that an implicit $DATA token is always explicit.
7414 * The "specification" of this function can be found in the
7415 * run_local_stream_name() function in torture.c, I've tried those
7416 * combinations against a W2k3 server.
7419 static NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
7420 char **pbase, char **pstream)
7422 char *base = NULL;
7423 char *stream = NULL;
7424 char *sname; /* stream name */
7425 const char *stype; /* stream type */
7427 DEBUG(10, ("split_ntfs_stream_name called for [%s]\n", fname));
7429 sname = strchr_m(fname, ':');
7431 if (lp_posix_pathnames() || (sname == NULL)) {
7432 if (pbase != NULL) {
7433 base = talloc_strdup(mem_ctx, fname);
7434 NT_STATUS_HAVE_NO_MEMORY(base);
7436 goto done;
7439 if (pbase != NULL) {
7440 base = talloc_strndup(mem_ctx, fname, PTR_DIFF(sname, fname));
7441 NT_STATUS_HAVE_NO_MEMORY(base);
7444 sname += 1;
7446 stype = strchr_m(sname, ':');
7448 if (stype == NULL) {
7449 sname = talloc_strdup(mem_ctx, sname);
7450 stype = "$DATA";
7452 else {
7453 if (StrCaseCmp(stype, ":$DATA") != 0) {
7455 * If there is an explicit stream type, so far we only
7456 * allow $DATA. Is there anything else allowed? -- vl
7458 DEBUG(10, ("[%s] is an invalid stream type\n", stype));
7459 TALLOC_FREE(base);
7460 return NT_STATUS_OBJECT_NAME_INVALID;
7462 sname = talloc_strndup(mem_ctx, sname, PTR_DIFF(stype, sname));
7463 stype += 1;
7466 if (sname == NULL) {
7467 TALLOC_FREE(base);
7468 return NT_STATUS_NO_MEMORY;
7471 if (sname[0] == '\0') {
7473 * no stream name, so no stream
7475 goto done;
7478 if (pstream != NULL) {
7479 stream = talloc_asprintf(mem_ctx, "%s:%s", sname, stype);
7480 if (stream == NULL) {
7481 TALLOC_FREE(sname);
7482 TALLOC_FREE(base);
7483 return NT_STATUS_NO_MEMORY;
7486 * upper-case the type field
7488 strupper_m(strchr_m(stream, ':')+1);
7491 done:
7492 if (pbase != NULL) {
7493 *pbase = base;
7495 if (pstream != NULL) {
7496 *pstream = stream;
7498 return NT_STATUS_OK;
7501 static bool test_stream_name(const char *fname, const char *expected_base,
7502 const char *expected_stream,
7503 NTSTATUS expected_status)
7505 NTSTATUS status;
7506 char *base = NULL;
7507 char *stream = NULL;
7509 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &stream);
7510 if (!NT_STATUS_EQUAL(status, expected_status)) {
7511 goto error;
7514 if (!NT_STATUS_IS_OK(status)) {
7515 return true;
7518 if (base == NULL) goto error;
7520 if (strcmp(expected_base, base) != 0) goto error;
7522 if ((expected_stream != NULL) && (stream == NULL)) goto error;
7523 if ((expected_stream == NULL) && (stream != NULL)) goto error;
7525 if ((stream != NULL) && (strcmp(expected_stream, stream) != 0))
7526 goto error;
7528 TALLOC_FREE(base);
7529 TALLOC_FREE(stream);
7530 return true;
7532 error:
7533 d_fprintf(stderr, "Do test_stream(%s, %s, %s, %s)\n",
7534 fname, expected_base ? expected_base : "<NULL>",
7535 expected_stream ? expected_stream : "<NULL>",
7536 nt_errstr(expected_status));
7537 d_fprintf(stderr, "-> base=%s, stream=%s, status=%s\n",
7538 base ? base : "<NULL>", stream ? stream : "<NULL>",
7539 nt_errstr(status));
7540 TALLOC_FREE(base);
7541 TALLOC_FREE(stream);
7542 return false;
7545 static bool run_local_stream_name(int dummy)
7547 bool ret = true;
7549 ret &= test_stream_name(
7550 "bla", "bla", NULL, NT_STATUS_OK);
7551 ret &= test_stream_name(
7552 "bla::$DATA", "bla", NULL, NT_STATUS_OK);
7553 ret &= test_stream_name(
7554 "bla:blub:", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
7555 ret &= test_stream_name(
7556 "bla::", NULL, NULL, NT_STATUS_OBJECT_NAME_INVALID);
7557 ret &= test_stream_name(
7558 "bla::123", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
7559 ret &= test_stream_name(
7560 "bla:$DATA", "bla", "$DATA:$DATA", NT_STATUS_OK);
7561 ret &= test_stream_name(
7562 "bla:x:$DATA", "bla", "x:$DATA", NT_STATUS_OK);
7563 ret &= test_stream_name(
7564 "bla:x", "bla", "x:$DATA", NT_STATUS_OK);
7566 return ret;
7569 static bool data_blob_equal(DATA_BLOB a, DATA_BLOB b)
7571 if (a.length != b.length) {
7572 printf("a.length=%d != b.length=%d\n",
7573 (int)a.length, (int)b.length);
7574 return false;
7576 if (memcmp(a.data, b.data, a.length) != 0) {
7577 printf("a.data and b.data differ\n");
7578 return false;
7580 return true;
7583 static bool run_local_memcache(int dummy)
7585 struct memcache *cache;
7586 DATA_BLOB k1, k2;
7587 DATA_BLOB d1, d2, d3;
7588 DATA_BLOB v1, v2, v3;
7590 TALLOC_CTX *mem_ctx;
7591 char *str1, *str2;
7592 size_t size1, size2;
7593 bool ret = false;
7595 cache = memcache_init(NULL, 100);
7597 if (cache == NULL) {
7598 printf("memcache_init failed\n");
7599 return false;
7602 d1 = data_blob_const("d1", 2);
7603 d2 = data_blob_const("d2", 2);
7604 d3 = data_blob_const("d3", 2);
7606 k1 = data_blob_const("d1", 2);
7607 k2 = data_blob_const("d2", 2);
7609 memcache_add(cache, STAT_CACHE, k1, d1);
7610 memcache_add(cache, GETWD_CACHE, k2, d2);
7612 if (!memcache_lookup(cache, STAT_CACHE, k1, &v1)) {
7613 printf("could not find k1\n");
7614 return false;
7616 if (!data_blob_equal(d1, v1)) {
7617 return false;
7620 if (!memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
7621 printf("could not find k2\n");
7622 return false;
7624 if (!data_blob_equal(d2, v2)) {
7625 return false;
7628 memcache_add(cache, STAT_CACHE, k1, d3);
7630 if (!memcache_lookup(cache, STAT_CACHE, k1, &v3)) {
7631 printf("could not find replaced k1\n");
7632 return false;
7634 if (!data_blob_equal(d3, v3)) {
7635 return false;
7638 memcache_add(cache, GETWD_CACHE, k1, d1);
7640 if (memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
7641 printf("Did find k2, should have been purged\n");
7642 return false;
7645 TALLOC_FREE(cache);
7647 cache = memcache_init(NULL, 0);
7649 mem_ctx = talloc_init("foo");
7651 str1 = talloc_strdup(mem_ctx, "string1");
7652 str2 = talloc_strdup(mem_ctx, "string2");
7654 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
7655 data_blob_string_const("torture"), &str1);
7656 size1 = talloc_total_size(cache);
7658 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
7659 data_blob_string_const("torture"), &str2);
7660 size2 = talloc_total_size(cache);
7662 printf("size1=%d, size2=%d\n", (int)size1, (int)size2);
7664 if (size2 > size1) {
7665 printf("memcache leaks memory!\n");
7666 goto fail;
7669 ret = true;
7670 fail:
7671 TALLOC_FREE(cache);
7672 return ret;
7675 static void wbclient_done(struct tevent_req *req)
7677 wbcErr wbc_err;
7678 struct winbindd_response *wb_resp;
7679 int *i = (int *)tevent_req_callback_data_void(req);
7681 wbc_err = wb_trans_recv(req, req, &wb_resp);
7682 TALLOC_FREE(req);
7683 *i += 1;
7684 d_printf("wb_trans_recv %d returned %s\n", *i, wbcErrorString(wbc_err));
7687 static bool run_local_wbclient(int dummy)
7689 struct event_context *ev;
7690 struct wb_context **wb_ctx;
7691 struct winbindd_request wb_req;
7692 bool result = false;
7693 int i, j;
7695 BlockSignals(True, SIGPIPE);
7697 ev = tevent_context_init_byname(talloc_tos(), "epoll");
7698 if (ev == NULL) {
7699 goto fail;
7702 wb_ctx = TALLOC_ARRAY(ev, struct wb_context *, nprocs);
7703 if (wb_ctx == NULL) {
7704 goto fail;
7707 ZERO_STRUCT(wb_req);
7708 wb_req.cmd = WINBINDD_PING;
7710 d_printf("nprocs=%d, numops=%d\n", (int)nprocs, (int)torture_numops);
7712 for (i=0; i<nprocs; i++) {
7713 wb_ctx[i] = wb_context_init(ev, NULL);
7714 if (wb_ctx[i] == NULL) {
7715 goto fail;
7717 for (j=0; j<torture_numops; j++) {
7718 struct tevent_req *req;
7719 req = wb_trans_send(ev, ev, wb_ctx[i],
7720 (j % 2) == 0, &wb_req);
7721 if (req == NULL) {
7722 goto fail;
7724 tevent_req_set_callback(req, wbclient_done, &i);
7728 i = 0;
7730 while (i < nprocs * torture_numops) {
7731 event_loop_once(ev);
7734 result = true;
7735 fail:
7736 TALLOC_FREE(ev);
7737 return result;
7740 static void getaddrinfo_finished(struct tevent_req *req)
7742 char *name = (char *)tevent_req_callback_data_void(req);
7743 struct addrinfo *ainfo;
7744 int res;
7746 res = getaddrinfo_recv(req, &ainfo);
7747 if (res != 0) {
7748 d_printf("gai(%s) returned %s\n", name, gai_strerror(res));
7749 return;
7751 d_printf("gai(%s) succeeded\n", name);
7752 freeaddrinfo(ainfo);
7755 static bool run_getaddrinfo_send(int dummy)
7757 TALLOC_CTX *frame = talloc_stackframe();
7758 struct fncall_context *ctx;
7759 struct tevent_context *ev;
7760 bool result = false;
7761 const char *names[4] = { "www.samba.org", "notfound.samba.org",
7762 "www.slashdot.org", "heise.de" };
7763 struct tevent_req *reqs[4];
7764 int i;
7766 ev = event_context_init(frame);
7767 if (ev == NULL) {
7768 goto fail;
7771 ctx = fncall_context_init(frame, 4);
7773 for (i=0; i<ARRAY_SIZE(names); i++) {
7774 reqs[i] = getaddrinfo_send(frame, ev, ctx, names[i], NULL,
7775 NULL);
7776 if (reqs[i] == NULL) {
7777 goto fail;
7779 tevent_req_set_callback(reqs[i], getaddrinfo_finished,
7780 (void *)names[i]);
7783 for (i=0; i<ARRAY_SIZE(reqs); i++) {
7784 tevent_loop_once(ev);
7787 result = true;
7788 fail:
7789 TALLOC_FREE(frame);
7790 return result;
7793 static bool dbtrans_inc(struct db_context *db)
7795 struct db_record *rec;
7796 uint32_t *val;
7797 bool ret = false;
7798 NTSTATUS status;
7800 rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
7801 if (rec == NULL) {
7802 printf(__location__ "fetch_lock failed\n");
7803 return false;
7806 if (rec->value.dsize != sizeof(uint32_t)) {
7807 printf(__location__ "value.dsize = %d\n",
7808 (int)rec->value.dsize);
7809 goto fail;
7812 val = (uint32_t *)rec->value.dptr;
7813 *val += 1;
7815 status = rec->store(rec, make_tdb_data((uint8_t *)val,
7816 sizeof(uint32_t)),
7818 if (!NT_STATUS_IS_OK(status)) {
7819 printf(__location__ "store failed: %s\n",
7820 nt_errstr(status));
7821 goto fail;
7824 ret = true;
7825 fail:
7826 TALLOC_FREE(rec);
7827 return ret;
7830 static bool run_local_dbtrans(int dummy)
7832 struct db_context *db;
7833 struct db_record *rec;
7834 NTSTATUS status;
7835 uint32_t initial;
7836 int res;
7838 db = db_open(talloc_tos(), "transtest.tdb", 0, TDB_DEFAULT,
7839 O_RDWR|O_CREAT, 0600);
7840 if (db == NULL) {
7841 printf("Could not open transtest.db\n");
7842 return false;
7845 res = db->transaction_start(db);
7846 if (res == -1) {
7847 printf(__location__ "transaction_start failed\n");
7848 return false;
7851 rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
7852 if (rec == NULL) {
7853 printf(__location__ "fetch_lock failed\n");
7854 return false;
7857 if (rec->value.dptr == NULL) {
7858 initial = 0;
7859 status = rec->store(
7860 rec, make_tdb_data((uint8_t *)&initial,
7861 sizeof(initial)),
7863 if (!NT_STATUS_IS_OK(status)) {
7864 printf(__location__ "store returned %s\n",
7865 nt_errstr(status));
7866 return false;
7870 TALLOC_FREE(rec);
7872 res = db->transaction_commit(db);
7873 if (res == -1) {
7874 printf(__location__ "transaction_commit failed\n");
7875 return false;
7878 while (true) {
7879 uint32_t val, val2;
7880 int i;
7882 res = db->transaction_start(db);
7883 if (res == -1) {
7884 printf(__location__ "transaction_start failed\n");
7885 break;
7888 if (!dbwrap_fetch_uint32(db, "transtest", &val)) {
7889 printf(__location__ "dbwrap_fetch_uint32 failed\n");
7890 break;
7893 for (i=0; i<10; i++) {
7894 if (!dbtrans_inc(db)) {
7895 return false;
7899 if (!dbwrap_fetch_uint32(db, "transtest", &val2)) {
7900 printf(__location__ "dbwrap_fetch_uint32 failed\n");
7901 break;
7904 if (val2 != val + 10) {
7905 printf(__location__ "val=%d, val2=%d\n",
7906 (int)val, (int)val2);
7907 break;
7910 printf("val2=%d\r", val2);
7912 res = db->transaction_commit(db);
7913 if (res == -1) {
7914 printf(__location__ "transaction_commit failed\n");
7915 break;
7919 TALLOC_FREE(db);
7920 return true;
7924 * Just a dummy test to be run under a debugger. There's no real way
7925 * to inspect the tevent_select specific function from outside of
7926 * tevent_select.c.
7929 static bool run_local_tevent_select(int dummy)
7931 struct tevent_context *ev;
7932 struct tevent_fd *fd1, *fd2;
7933 bool result = false;
7935 ev = tevent_context_init_byname(NULL, "select");
7936 if (ev == NULL) {
7937 d_fprintf(stderr, "tevent_context_init_byname failed\n");
7938 goto fail;
7941 fd1 = tevent_add_fd(ev, ev, 2, 0, NULL, NULL);
7942 if (fd1 == NULL) {
7943 d_fprintf(stderr, "tevent_add_fd failed\n");
7944 goto fail;
7946 fd2 = tevent_add_fd(ev, ev, 3, 0, NULL, NULL);
7947 if (fd2 == NULL) {
7948 d_fprintf(stderr, "tevent_add_fd failed\n");
7949 goto fail;
7951 TALLOC_FREE(fd2);
7953 fd2 = tevent_add_fd(ev, ev, 1, 0, NULL, NULL);
7954 if (fd2 == NULL) {
7955 d_fprintf(stderr, "tevent_add_fd failed\n");
7956 goto fail;
7959 result = true;
7960 fail:
7961 TALLOC_FREE(ev);
7962 return result;
7965 static double create_procs(bool (*fn)(int), bool *result)
7967 int i, status;
7968 volatile pid_t *child_status;
7969 volatile bool *child_status_out;
7970 int synccount;
7971 int tries = 8;
7972 struct timeval start;
7974 synccount = 0;
7976 child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*nprocs);
7977 if (!child_status) {
7978 printf("Failed to setup shared memory\n");
7979 return -1;
7982 child_status_out = (volatile bool *)shm_setup(sizeof(bool)*nprocs);
7983 if (!child_status_out) {
7984 printf("Failed to setup result status shared memory\n");
7985 return -1;
7988 for (i = 0; i < nprocs; i++) {
7989 child_status[i] = 0;
7990 child_status_out[i] = True;
7993 start = timeval_current();
7995 for (i=0;i<nprocs;i++) {
7996 procnum = i;
7997 if (fork() == 0) {
7998 pid_t mypid = getpid();
7999 sys_srandom(((int)mypid) ^ ((int)time(NULL)));
8001 slprintf(myname,sizeof(myname),"CLIENT%d", i);
8003 while (1) {
8004 if (torture_open_connection(&current_cli, i)) break;
8005 if (tries-- == 0) {
8006 printf("pid %d failed to start\n", (int)getpid());
8007 _exit(1);
8009 smb_msleep(10);
8012 child_status[i] = getpid();
8014 while (child_status[i] && timeval_elapsed(&start) < 5) smb_msleep(2);
8016 child_status_out[i] = fn(i);
8017 _exit(0);
8021 do {
8022 synccount = 0;
8023 for (i=0;i<nprocs;i++) {
8024 if (child_status[i]) synccount++;
8026 if (synccount == nprocs) break;
8027 smb_msleep(10);
8028 } while (timeval_elapsed(&start) < 30);
8030 if (synccount != nprocs) {
8031 printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount);
8032 *result = False;
8033 return timeval_elapsed(&start);
8036 /* start the client load */
8037 start = timeval_current();
8039 for (i=0;i<nprocs;i++) {
8040 child_status[i] = 0;
8043 printf("%d clients started\n", nprocs);
8045 for (i=0;i<nprocs;i++) {
8046 while (waitpid(0, &status, 0) == -1 && errno == EINTR) /* noop */ ;
8049 printf("\n");
8051 for (i=0;i<nprocs;i++) {
8052 if (!child_status_out[i]) {
8053 *result = False;
8056 return timeval_elapsed(&start);
8059 #define FLAG_MULTIPROC 1
8061 static struct {
8062 const char *name;
8063 bool (*fn)(int);
8064 unsigned flags;
8065 } torture_ops[] = {
8066 {"FDPASS", run_fdpasstest, 0},
8067 {"LOCK1", run_locktest1, 0},
8068 {"LOCK2", run_locktest2, 0},
8069 {"LOCK3", run_locktest3, 0},
8070 {"LOCK4", run_locktest4, 0},
8071 {"LOCK5", run_locktest5, 0},
8072 {"LOCK6", run_locktest6, 0},
8073 {"LOCK7", run_locktest7, 0},
8074 {"LOCK8", run_locktest8, 0},
8075 {"LOCK9", run_locktest9, 0},
8076 {"UNLINK", run_unlinktest, 0},
8077 {"BROWSE", run_browsetest, 0},
8078 {"ATTR", run_attrtest, 0},
8079 {"TRANS2", run_trans2test, 0},
8080 {"MAXFID", run_maxfidtest, FLAG_MULTIPROC},
8081 {"TORTURE",run_torture, FLAG_MULTIPROC},
8082 {"RANDOMIPC", run_randomipc, 0},
8083 {"NEGNOWAIT", run_negprot_nowait, 0},
8084 {"NBENCH", run_nbench, 0},
8085 {"NBENCH2", run_nbench2, 0},
8086 {"OPLOCK1", run_oplock1, 0},
8087 {"OPLOCK2", run_oplock2, 0},
8088 {"OPLOCK3", run_oplock3, 0},
8089 {"OPLOCK4", run_oplock4, 0},
8090 {"DIR", run_dirtest, 0},
8091 {"DIR1", run_dirtest1, 0},
8092 {"DIR-CREATETIME", run_dir_createtime, 0},
8093 {"DENY1", torture_denytest1, 0},
8094 {"DENY2", torture_denytest2, 0},
8095 {"TCON", run_tcon_test, 0},
8096 {"TCONDEV", run_tcon_devtype_test, 0},
8097 {"RW1", run_readwritetest, 0},
8098 {"RW2", run_readwritemulti, FLAG_MULTIPROC},
8099 {"RW3", run_readwritelarge, 0},
8100 {"RW-SIGNING", run_readwritelarge_signtest, 0},
8101 {"OPEN", run_opentest, 0},
8102 {"POSIX", run_simple_posix_open_test, 0},
8103 {"POSIX-APPEND", run_posix_append, 0},
8104 {"ASYNC-ECHO", run_async_echo, 0},
8105 { "UID-REGRESSION-TEST", run_uid_regression_test, 0},
8106 { "SHORTNAME-TEST", run_shortname_test, 0},
8107 { "ADDRCHANGE", run_addrchange, 0},
8108 #if 1
8109 {"OPENATTR", run_openattrtest, 0},
8110 #endif
8111 {"XCOPY", run_xcopy, 0},
8112 {"RENAME", run_rename, 0},
8113 {"DELETE", run_deletetest, 0},
8114 {"DELETE-LN", run_deletetest_ln, 0},
8115 {"PROPERTIES", run_properties, 0},
8116 {"MANGLE", torture_mangle, 0},
8117 {"MANGLE1", run_mangle1, 0},
8118 {"W2K", run_w2ktest, 0},
8119 {"TRANS2SCAN", torture_trans2_scan, 0},
8120 {"NTTRANSSCAN", torture_nttrans_scan, 0},
8121 {"UTABLE", torture_utable, 0},
8122 {"CASETABLE", torture_casetable, 0},
8123 {"ERRMAPEXTRACT", run_error_map_extract, 0},
8124 {"PIPE_NUMBER", run_pipe_number, 0},
8125 {"TCON2", run_tcon2_test, 0},
8126 {"IOCTL", torture_ioctl_test, 0},
8127 {"CHKPATH", torture_chkpath_test, 0},
8128 {"FDSESS", run_fdsesstest, 0},
8129 { "EATEST", run_eatest, 0},
8130 { "SESSSETUP_BENCH", run_sesssetup_bench, 0},
8131 { "CHAIN1", run_chain1, 0},
8132 { "CHAIN2", run_chain2, 0},
8133 { "WINDOWS-WRITE", run_windows_write, 0},
8134 { "CLI_ECHO", run_cli_echo, 0},
8135 { "GETADDRINFO", run_getaddrinfo_send, 0},
8136 { "TLDAP", run_tldap },
8137 { "STREAMERROR", run_streamerror },
8138 { "NOTIFY-BENCH", run_notify_bench },
8139 { "BAD-NBT-SESSION", run_bad_nbt_session },
8140 { "SMB-ANY-CONNECT", run_smb_any_connect },
8141 { "LOCAL-SUBSTITUTE", run_local_substitute, 0},
8142 { "LOCAL-GENCACHE", run_local_gencache, 0},
8143 { "LOCAL-TALLOC-DICT", run_local_talloc_dict, 0},
8144 { "LOCAL-BASE64", run_local_base64, 0},
8145 { "LOCAL-RBTREE", run_local_rbtree, 0},
8146 { "LOCAL-MEMCACHE", run_local_memcache, 0},
8147 { "LOCAL-STREAM-NAME", run_local_stream_name, 0},
8148 { "LOCAL-WBCLIENT", run_local_wbclient, 0},
8149 { "LOCAL-string_to_sid", run_local_string_to_sid, 0},
8150 { "LOCAL-binary_to_sid", run_local_binary_to_sid, 0},
8151 { "LOCAL-DBTRANS", run_local_dbtrans, 0},
8152 { "LOCAL-TEVENT-SELECT", run_local_tevent_select, 0},
8153 {NULL, NULL, 0}};
8157 /****************************************************************************
8158 run a specified test or "ALL"
8159 ****************************************************************************/
8160 static bool run_test(const char *name)
8162 bool ret = True;
8163 bool result = True;
8164 bool found = False;
8165 int i;
8166 double t;
8167 if (strequal(name,"ALL")) {
8168 for (i=0;torture_ops[i].name;i++) {
8169 run_test(torture_ops[i].name);
8171 found = True;
8174 for (i=0;torture_ops[i].name;i++) {
8175 fstr_sprintf(randomfname, "\\XX%x",
8176 (unsigned)random());
8178 if (strequal(name, torture_ops[i].name)) {
8179 found = True;
8180 printf("Running %s\n", name);
8181 if (torture_ops[i].flags & FLAG_MULTIPROC) {
8182 t = create_procs(torture_ops[i].fn, &result);
8183 if (!result) {
8184 ret = False;
8185 printf("TEST %s FAILED!\n", name);
8187 } else {
8188 struct timeval start;
8189 start = timeval_current();
8190 if (!torture_ops[i].fn(0)) {
8191 ret = False;
8192 printf("TEST %s FAILED!\n", name);
8194 t = timeval_elapsed(&start);
8196 printf("%s took %g secs\n\n", name, t);
8200 if (!found) {
8201 printf("Did not find a test named %s\n", name);
8202 ret = False;
8205 return ret;
8209 static void usage(void)
8211 int i;
8213 printf("WARNING samba4 test suite is much more complete nowadays.\n");
8214 printf("Please use samba4 torture.\n\n");
8216 printf("Usage: smbtorture //server/share <options> TEST1 TEST2 ...\n");
8218 printf("\t-d debuglevel\n");
8219 printf("\t-U user%%pass\n");
8220 printf("\t-k use kerberos\n");
8221 printf("\t-N numprocs\n");
8222 printf("\t-n my_netbios_name\n");
8223 printf("\t-W workgroup\n");
8224 printf("\t-o num_operations\n");
8225 printf("\t-O socket_options\n");
8226 printf("\t-m maximum protocol\n");
8227 printf("\t-L use oplocks\n");
8228 printf("\t-c CLIENT.TXT specify client load file for NBENCH\n");
8229 printf("\t-A showall\n");
8230 printf("\t-p port\n");
8231 printf("\t-s seed\n");
8232 printf("\t-b unclist_filename specify multiple shares for multiple connections\n");
8233 printf("\n\n");
8235 printf("tests are:");
8236 for (i=0;torture_ops[i].name;i++) {
8237 printf(" %s", torture_ops[i].name);
8239 printf("\n");
8241 printf("default test is ALL\n");
8243 exit(1);
8246 /****************************************************************************
8247 main program
8248 ****************************************************************************/
8249 int main(int argc,char *argv[])
8251 int opt, i;
8252 char *p;
8253 int gotuser = 0;
8254 int gotpass = 0;
8255 bool correct = True;
8256 TALLOC_CTX *frame = talloc_stackframe();
8257 int seed = time(NULL);
8259 #ifdef HAVE_SETBUFFER
8260 setbuffer(stdout, NULL, 0);
8261 #endif
8263 setup_logging("smbtorture", DEBUG_STDOUT);
8265 load_case_tables();
8267 if (is_default_dyn_CONFIGFILE()) {
8268 if(getenv("SMB_CONF_PATH")) {
8269 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
8272 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
8273 load_interfaces();
8275 if (argc < 2) {
8276 usage();
8279 for(p = argv[1]; *p; p++)
8280 if(*p == '\\')
8281 *p = '/';
8283 if (strncmp(argv[1], "//", 2)) {
8284 usage();
8287 fstrcpy(host, &argv[1][2]);
8288 p = strchr_m(&host[2],'/');
8289 if (!p) {
8290 usage();
8292 *p = 0;
8293 fstrcpy(share, p+1);
8295 fstrcpy(myname, get_myname(talloc_tos()));
8296 if (!*myname) {
8297 fprintf(stderr, "Failed to get my hostname.\n");
8298 return 1;
8301 if (*username == 0 && getenv("LOGNAME")) {
8302 fstrcpy(username,getenv("LOGNAME"));
8305 argc--;
8306 argv++;
8308 fstrcpy(workgroup, lp_workgroup());
8310 while ((opt = getopt(argc, argv, "p:hW:U:n:N:O:o:m:Ll:d:Aec:ks:b:B:")) != EOF) {
8311 switch (opt) {
8312 case 'p':
8313 port_to_use = atoi(optarg);
8314 break;
8315 case 's':
8316 seed = atoi(optarg);
8317 break;
8318 case 'W':
8319 fstrcpy(workgroup,optarg);
8320 break;
8321 case 'm':
8322 max_protocol = interpret_protocol(optarg, max_protocol);
8323 break;
8324 case 'N':
8325 nprocs = atoi(optarg);
8326 break;
8327 case 'o':
8328 torture_numops = atoi(optarg);
8329 break;
8330 case 'd':
8331 lp_set_cmdline("log level", optarg);
8332 break;
8333 case 'O':
8334 sockops = optarg;
8335 break;
8336 case 'L':
8337 use_oplocks = True;
8338 break;
8339 case 'l':
8340 local_path = optarg;
8341 break;
8342 case 'A':
8343 torture_showall = True;
8344 break;
8345 case 'n':
8346 fstrcpy(myname, optarg);
8347 break;
8348 case 'c':
8349 client_txt = optarg;
8350 break;
8351 case 'e':
8352 do_encrypt = true;
8353 break;
8354 case 'k':
8355 #ifdef HAVE_KRB5
8356 use_kerberos = True;
8357 #else
8358 d_printf("No kerberos support compiled in\n");
8359 exit(1);
8360 #endif
8361 break;
8362 case 'U':
8363 gotuser = 1;
8364 fstrcpy(username,optarg);
8365 p = strchr_m(username,'%');
8366 if (p) {
8367 *p = 0;
8368 fstrcpy(password, p+1);
8369 gotpass = 1;
8371 break;
8372 case 'b':
8373 fstrcpy(multishare_conn_fname, optarg);
8374 use_multishare_conn = True;
8375 break;
8376 case 'B':
8377 torture_blocksize = atoi(optarg);
8378 break;
8379 default:
8380 printf("Unknown option %c (%d)\n", (char)opt, opt);
8381 usage();
8385 d_printf("using seed %d\n", seed);
8387 srandom(seed);
8389 if(use_kerberos && !gotuser) gotpass = True;
8391 while (!gotpass) {
8392 p = getpass("Password:");
8393 if (p) {
8394 fstrcpy(password, p);
8395 gotpass = 1;
8399 printf("host=%s share=%s user=%s myname=%s\n",
8400 host, share, username, myname);
8402 if (argc == optind) {
8403 correct = run_test("ALL");
8404 } else {
8405 for (i=optind;i<argc;i++) {
8406 if (!run_test(argv[i])) {
8407 correct = False;
8412 TALLOC_FREE(frame);
8414 if (correct) {
8415 return(0);
8416 } else {
8417 return(1);