Fix bug #9724 - is_encrypted_packet() function incorrectly used inside server.
[Samba.git] / source3 / torture / torture.c
blobd37d83c782446feb8a07041ec65a597a13a9b4cb
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 "system/shmem.h"
23 #include "wbc_async.h"
24 #include "torture/proto.h"
25 #include "libcli/security/security.h"
26 #include "tldap.h"
27 #include "tldap_util.h"
28 #include "../librpc/gen_ndr/svcctl.h"
29 #include "memcache.h"
30 #include "nsswitch/winbind_client.h"
31 #include "dbwrap.h"
32 #include "talloc_dict.h"
33 #include "async_smb.h"
34 #include "libsmb/libsmb.h"
35 #include "libsmb/clirap.h"
36 #include "trans2.h"
37 #include "libsmb/nmblib.h"
38 #include "../lib/util/tevent_ntstatus.h"
39 #include "util_tdb.h"
41 extern char *optarg;
42 extern int optind;
44 static fstring host, workgroup, share, password, username, myname;
45 static int max_protocol = PROTOCOL_NT1;
46 static const char *sockops="TCP_NODELAY";
47 static int nprocs=1;
48 static int port_to_use=0;
49 int torture_numops=100;
50 int torture_blocksize=1024*1024;
51 static int procnum; /* records process count number when forking */
52 static struct cli_state *current_cli;
53 static fstring randomfname;
54 static bool use_oplocks;
55 static bool use_level_II_oplocks;
56 static const char *client_txt = "client_oplocks.txt";
57 static bool use_kerberos;
58 static fstring multishare_conn_fname;
59 static bool use_multishare_conn = False;
60 static bool do_encrypt;
61 static const char *local_path = NULL;
62 static int signing_state = Undefined;
64 bool torture_showall = False;
66 static double create_procs(bool (*fn)(int), bool *result);
69 /* return a pointer to a anonymous shared memory segment of size "size"
70 which will persist across fork() but will disappear when all processes
71 exit
73 The memory is not zeroed
75 This function uses system5 shared memory. It takes advantage of a property
76 that the memory is not destroyed if it is attached when the id is removed
78 void *shm_setup(int size)
80 int shmid;
81 void *ret;
83 #ifdef __QNXNTO__
84 shmid = shm_open("private", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
85 if (shmid == -1) {
86 printf("can't get shared memory\n");
87 exit(1);
89 shm_unlink("private");
90 if (ftruncate(shmid, size) == -1) {
91 printf("can't set shared memory size\n");
92 exit(1);
94 ret = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0);
95 if (ret == MAP_FAILED) {
96 printf("can't map shared memory\n");
97 exit(1);
99 #else
100 shmid = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
101 if (shmid == -1) {
102 printf("can't get shared memory\n");
103 exit(1);
105 ret = (void *)shmat(shmid, 0, 0);
106 if (!ret || ret == (void *)-1) {
107 printf("can't attach to shared memory\n");
108 return NULL;
110 /* the following releases the ipc, but note that this process
111 and all its children will still have access to the memory, its
112 just that the shmid is no longer valid for other shm calls. This
113 means we don't leave behind lots of shm segments after we exit
115 See Stevens "advanced programming in unix env" for details
117 shmctl(shmid, IPC_RMID, 0);
118 #endif
120 return ret;
123 /********************************************************************
124 Ensure a connection is encrypted.
125 ********************************************************************/
127 static bool force_cli_encryption(struct cli_state *c,
128 const char *sharename)
130 uint16 major, minor;
131 uint32 caplow, caphigh;
132 NTSTATUS status;
134 if (!SERVER_HAS_UNIX_CIFS(c)) {
135 d_printf("Encryption required and "
136 "server that doesn't support "
137 "UNIX extensions - failing connect\n");
138 return false;
141 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
142 &caphigh);
143 if (!NT_STATUS_IS_OK(status)) {
144 d_printf("Encryption required and "
145 "can't get UNIX CIFS extensions "
146 "version from server: %s\n", nt_errstr(status));
147 return false;
150 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
151 d_printf("Encryption required and "
152 "share %s doesn't support "
153 "encryption.\n", sharename);
154 return false;
157 if (c->use_kerberos) {
158 status = cli_gss_smb_encryption_start(c);
159 } else {
160 status = cli_raw_ntlm_smb_encryption_start(c,
161 username,
162 password,
163 workgroup);
166 if (!NT_STATUS_IS_OK(status)) {
167 d_printf("Encryption required and "
168 "setup failed with error %s.\n",
169 nt_errstr(status));
170 return false;
173 return true;
177 static struct cli_state *open_nbt_connection(void)
179 struct nmb_name called, calling;
180 struct sockaddr_storage ss;
181 struct cli_state *c;
182 NTSTATUS status;
184 make_nmb_name(&calling, myname, 0x0);
185 make_nmb_name(&called , host, 0x20);
187 zero_sockaddr(&ss);
189 if (!(c = cli_initialise_ex(signing_state))) {
190 printf("Failed initialize cli_struct to connect with %s\n", host);
191 return NULL;
194 c->port = port_to_use;
196 status = cli_connect(c, host, &ss);
197 if (!NT_STATUS_IS_OK(status)) {
198 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
199 return NULL;
202 c->use_kerberos = use_kerberos;
204 c->timeout = 120000; /* set a really long timeout (2 minutes) */
205 if (use_oplocks) c->use_oplocks = True;
206 if (use_level_II_oplocks) c->use_level_II_oplocks = True;
208 if (!cli_session_request(c, &calling, &called)) {
210 * Well, that failed, try *SMBSERVER ...
211 * However, we must reconnect as well ...
213 status = cli_connect(c, host, &ss);
214 if (!NT_STATUS_IS_OK(status)) {
215 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
216 return NULL;
219 make_nmb_name(&called, "*SMBSERVER", 0x20);
220 if (!cli_session_request(c, &calling, &called)) {
221 printf("%s rejected the session\n",host);
222 printf("We tried with a called name of %s & %s\n",
223 host, "*SMBSERVER");
224 cli_shutdown(c);
225 return NULL;
229 return c;
232 /****************************************************************************
233 Send a corrupt session request. See rfc1002.txt 4.3 and 4.3.2.
234 ****************************************************************************/
236 static bool cli_bad_session_request(struct cli_state *cli,
237 struct nmb_name *calling, struct nmb_name *called)
239 char *p;
240 int len = 4;
241 int namelen = 0;
242 char *tmp;
244 memcpy(&(cli->calling), calling, sizeof(*calling));
245 memcpy(&(cli->called ), called , sizeof(*called ));
247 /* put in the destination name */
249 tmp = name_mangle(talloc_tos(), cli->called.name,
250 cli->called.name_type);
251 if (tmp == NULL) {
252 return false;
255 p = cli->outbuf+len;
256 namelen = name_len((unsigned char *)tmp, talloc_get_size(tmp));
257 if (namelen > 0) {
258 memcpy(p, tmp, namelen);
259 len += namelen;
261 TALLOC_FREE(tmp);
263 /* Deliberately corrupt the name len (first byte) */
264 *p = 100;
266 /* and my name */
268 tmp = name_mangle(talloc_tos(), cli->calling.name,
269 cli->calling.name_type);
270 if (tmp == NULL) {
271 return false;
274 p = cli->outbuf+len;
275 namelen = name_len((unsigned char *)tmp, talloc_get_size(tmp));
276 if (namelen > 0) {
277 memcpy(p, tmp, namelen);
278 len += namelen;
280 TALLOC_FREE(tmp);
281 /* Deliberately corrupt the name len (first byte) */
282 *p = 100;
284 /* send a session request (RFC 1002) */
285 /* setup the packet length
286 * Remove four bytes from the length count, since the length
287 * field in the NBT Session Service header counts the number
288 * of bytes which follow. The cli_send_smb() function knows
289 * about this and accounts for those four bytes.
290 * CRH.
292 len -= 4;
293 _smb_setlen(cli->outbuf,len);
294 SCVAL(cli->outbuf,0,0x81);
296 cli_send_smb(cli);
297 DEBUG(5,("Sent session request\n"));
299 if (!cli_receive_smb(cli))
300 return False;
302 if (CVAL(cli->inbuf,0) != 0x82) {
303 /* This is the wrong place to put the error... JRA. */
304 cli->rap_error = CVAL(cli->inbuf,4);
305 return False;
307 return(True);
310 static struct cli_state *open_bad_nbt_connection(void)
312 struct nmb_name called, calling;
313 struct sockaddr_storage ss;
314 struct cli_state *c;
315 NTSTATUS status;
317 make_nmb_name(&calling, myname, 0x0);
318 make_nmb_name(&called , host, 0x20);
320 zero_sockaddr(&ss);
322 if (!(c = cli_initialise_ex(signing_state))) {
323 printf("Failed initialize cli_struct to connect with %s\n", host);
324 return NULL;
327 c->port = 139;
329 status = cli_connect(c, host, &ss);
330 if (!NT_STATUS_IS_OK(status)) {
331 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
332 return NULL;
335 c->timeout = 4000; /* set a short timeout (4 seconds) */
337 if (!cli_bad_session_request(c, &calling, &called)) {
338 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
339 return NULL;
342 return c;
346 /* Insert a NULL at the first separator of the given path and return a pointer
347 * to the remainder of the string.
349 static char *
350 terminate_path_at_separator(char * path)
352 char * p;
354 if (!path) {
355 return NULL;
358 if ((p = strchr_m(path, '/'))) {
359 *p = '\0';
360 return p + 1;
363 if ((p = strchr_m(path, '\\'))) {
364 *p = '\0';
365 return p + 1;
368 /* No separator. */
369 return NULL;
373 parse a //server/share type UNC name
375 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
376 char **hostname, char **sharename)
378 char *p;
380 *hostname = *sharename = NULL;
382 if (strncmp(unc_name, "\\\\", 2) &&
383 strncmp(unc_name, "//", 2)) {
384 return False;
387 *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
388 p = terminate_path_at_separator(*hostname);
390 if (p && *p) {
391 *sharename = talloc_strdup(mem_ctx, p);
392 terminate_path_at_separator(*sharename);
395 if (*hostname && *sharename) {
396 return True;
399 TALLOC_FREE(*hostname);
400 TALLOC_FREE(*sharename);
401 return False;
404 static bool torture_open_connection_share(struct cli_state **c,
405 const char *hostname,
406 const char *sharename)
408 int flags = 0;
409 NTSTATUS status;
411 if (use_kerberos)
412 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
413 if (use_oplocks)
414 flags |= CLI_FULL_CONNECTION_OPLOCKS;
415 if (use_level_II_oplocks)
416 flags |= CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS;
418 status = cli_full_connection(c, myname,
419 hostname, NULL, port_to_use,
420 sharename, "?????",
421 username, workgroup,
422 password, flags, signing_state);
423 if (!NT_STATUS_IS_OK(status)) {
424 printf("failed to open share connection: //%s/%s port:%d - %s\n",
425 hostname, sharename, port_to_use, nt_errstr(status));
426 return False;
429 (*c)->timeout = 120000; /* set a really long timeout (2 minutes) */
431 if (do_encrypt) {
432 return force_cli_encryption(*c,
433 sharename);
435 return True;
438 bool torture_open_connection(struct cli_state **c, int conn_index)
440 char **unc_list = NULL;
441 int num_unc_names = 0;
442 bool result;
444 if (use_multishare_conn==True) {
445 char *h, *s;
446 unc_list = file_lines_load(multishare_conn_fname, &num_unc_names, 0, NULL);
447 if (!unc_list || num_unc_names <= 0) {
448 printf("Failed to load unc names list from '%s'\n", multishare_conn_fname);
449 exit(1);
452 if (!smbcli_parse_unc(unc_list[conn_index % num_unc_names],
453 NULL, &h, &s)) {
454 printf("Failed to parse UNC name %s\n",
455 unc_list[conn_index % num_unc_names]);
456 TALLOC_FREE(unc_list);
457 exit(1);
460 result = torture_open_connection_share(c, h, s);
462 /* h, s were copied earlier */
463 TALLOC_FREE(unc_list);
464 return result;
467 return torture_open_connection_share(c, host, share);
470 bool torture_cli_session_setup2(struct cli_state *cli, uint16 *new_vuid)
472 uint16 old_vuid = cli->vuid;
473 fstring old_user_name;
474 size_t passlen = strlen(password);
475 NTSTATUS status;
476 bool ret;
478 fstrcpy(old_user_name, cli->user_name);
479 cli->vuid = 0;
480 ret = NT_STATUS_IS_OK(cli_session_setup(cli, username,
481 password, passlen,
482 password, passlen,
483 workgroup));
484 *new_vuid = cli->vuid;
485 cli->vuid = old_vuid;
486 status = cli_set_username(cli, old_user_name);
487 if (!NT_STATUS_IS_OK(status)) {
488 return false;
490 return ret;
494 bool torture_close_connection(struct cli_state *c)
496 bool ret = True;
497 NTSTATUS status;
499 status = cli_tdis(c);
500 if (!NT_STATUS_IS_OK(status)) {
501 printf("tdis failed (%s)\n", nt_errstr(status));
502 ret = False;
505 cli_shutdown(c);
507 return ret;
511 /* check if the server produced the expected error code */
512 static bool check_error(int line, struct cli_state *c,
513 uint8 eclass, uint32 ecode, NTSTATUS nterr)
515 if (cli_is_dos_error(c)) {
516 uint8 cclass;
517 uint32 num;
519 /* Check DOS error */
521 cli_dos_error(c, &cclass, &num);
523 if (eclass != cclass || ecode != num) {
524 printf("unexpected error code class=%d code=%d\n",
525 (int)cclass, (int)num);
526 printf(" expected %d/%d %s (line=%d)\n",
527 (int)eclass, (int)ecode, nt_errstr(nterr), line);
528 return False;
531 } else {
532 NTSTATUS status;
534 /* Check NT error */
536 status = cli_nt_error(c);
538 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
539 printf("unexpected error code %s\n", nt_errstr(status));
540 printf(" expected %s (line=%d)\n", nt_errstr(nterr), line);
541 return False;
545 return True;
549 static bool wait_lock(struct cli_state *c, int fnum, uint32 offset, uint32 len)
551 while (!cli_lock(c, fnum, offset, len, -1, WRITE_LOCK)) {
552 if (!check_error(__LINE__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
554 return True;
558 static bool rw_torture(struct cli_state *c)
560 const char *lockfname = "\\torture.lck";
561 fstring fname;
562 uint16_t fnum;
563 uint16_t fnum2;
564 pid_t pid2, pid = getpid();
565 int i, j;
566 char buf[1024];
567 bool correct = True;
568 NTSTATUS status;
570 memset(buf, '\0', sizeof(buf));
572 status = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
573 DENY_NONE, &fnum2);
574 if (!NT_STATUS_IS_OK(status)) {
575 status = cli_open(c, lockfname, O_RDWR, DENY_NONE, &fnum2);
577 if (!NT_STATUS_IS_OK(status)) {
578 printf("open of %s failed (%s)\n", lockfname, cli_errstr(c));
579 return False;
582 for (i=0;i<torture_numops;i++) {
583 unsigned n = (unsigned)sys_random()%10;
585 if (i % 10 == 0) {
586 printf("%d\r", i); fflush(stdout);
588 slprintf(fname, sizeof(fstring) - 1, "\\torture.%u", n);
590 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
591 return False;
594 if (!NT_STATUS_IS_OK(cli_open(c, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL, &fnum))) {
595 printf("open failed (%s)\n", cli_errstr(c));
596 correct = False;
597 break;
600 status = cli_writeall(c, fnum, 0, (uint8_t *)&pid, 0,
601 sizeof(pid), NULL);
602 if (!NT_STATUS_IS_OK(status)) {
603 printf("write failed (%s)\n", nt_errstr(status));
604 correct = False;
607 for (j=0;j<50;j++) {
608 status = cli_writeall(c, fnum, 0, (uint8_t *)buf,
609 sizeof(pid)+(j*sizeof(buf)),
610 sizeof(buf), NULL);
611 if (!NT_STATUS_IS_OK(status)) {
612 printf("write failed (%s)\n",
613 nt_errstr(status));
614 correct = False;
618 pid2 = 0;
620 if (cli_read(c, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
621 printf("read failed (%s)\n", cli_errstr(c));
622 correct = False;
625 if (pid2 != pid) {
626 printf("data corruption!\n");
627 correct = False;
630 if (!NT_STATUS_IS_OK(cli_close(c, fnum))) {
631 printf("close failed (%s)\n", cli_errstr(c));
632 correct = False;
635 if (!NT_STATUS_IS_OK(cli_unlink(c, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
636 printf("unlink failed (%s)\n", cli_errstr(c));
637 correct = False;
640 if (!NT_STATUS_IS_OK(cli_unlock(c, fnum2, n*sizeof(int), sizeof(int)))) {
641 printf("unlock failed (%s)\n", cli_errstr(c));
642 correct = False;
646 cli_close(c, fnum2);
647 cli_unlink(c, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
649 printf("%d\n", i);
651 return correct;
654 static bool run_torture(int dummy)
656 struct cli_state *cli;
657 bool ret;
659 cli = current_cli;
661 cli_sockopt(cli, sockops);
663 ret = rw_torture(cli);
665 if (!torture_close_connection(cli)) {
666 ret = False;
669 return ret;
672 static bool rw_torture3(struct cli_state *c, char *lockfname)
674 uint16_t fnum = (uint16_t)-1;
675 unsigned int i = 0;
676 char buf[131072];
677 char buf_rd[131072];
678 unsigned count;
679 unsigned countprev = 0;
680 ssize_t sent = 0;
681 bool correct = True;
682 NTSTATUS status = NT_STATUS_OK;
684 srandom(1);
685 for (i = 0; i < sizeof(buf); i += sizeof(uint32))
687 SIVAL(buf, i, sys_random());
690 if (procnum == 0)
692 if (!NT_STATUS_IS_OK(cli_unlink(c, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
693 printf("unlink failed (%s) (normal, this file should not exist)\n", cli_errstr(c));
696 if (!NT_STATUS_IS_OK(cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
697 DENY_NONE, &fnum))) {
698 printf("first open read/write of %s failed (%s)\n",
699 lockfname, cli_errstr(c));
700 return False;
703 else
705 for (i = 0; i < 500 && fnum == (uint16_t)-1; i++)
707 status = cli_open(c, lockfname, O_RDONLY,
708 DENY_NONE, &fnum);
709 if (!NT_STATUS_IS_OK(status)) {
710 break;
712 smb_msleep(10);
714 if (!NT_STATUS_IS_OK(status)) {
715 printf("second open read-only of %s failed (%s)\n",
716 lockfname, cli_errstr(c));
717 return False;
721 i = 0;
722 for (count = 0; count < sizeof(buf); count += sent)
724 if (count >= countprev) {
725 printf("%d %8d\r", i, count);
726 fflush(stdout);
727 i++;
728 countprev += (sizeof(buf) / 20);
731 if (procnum == 0)
733 sent = ((unsigned)sys_random()%(20))+ 1;
734 if (sent > sizeof(buf) - count)
736 sent = sizeof(buf) - count;
739 status = cli_writeall(c, fnum, 0, (uint8_t *)buf+count,
740 count, (size_t)sent, NULL);
741 if (!NT_STATUS_IS_OK(status)) {
742 printf("write failed (%s)\n",
743 nt_errstr(status));
744 correct = False;
747 else
749 sent = cli_read(c, fnum, buf_rd+count, count,
750 sizeof(buf)-count);
751 if (sent < 0)
753 printf("read failed offset:%d size:%ld (%s)\n",
754 count, (unsigned long)sizeof(buf)-count,
755 cli_errstr(c));
756 correct = False;
757 sent = 0;
759 if (sent > 0)
761 if (memcmp(buf_rd+count, buf+count, sent) != 0)
763 printf("read/write compare failed\n");
764 printf("offset: %d req %ld recvd %ld\n", count, (unsigned long)sizeof(buf)-count, (unsigned long)sent);
765 correct = False;
766 break;
773 if (!NT_STATUS_IS_OK(cli_close(c, fnum))) {
774 printf("close failed (%s)\n", cli_errstr(c));
775 correct = False;
778 return correct;
781 static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
783 const char *lockfname = "\\torture2.lck";
784 uint16_t fnum1;
785 uint16_t fnum2;
786 int i;
787 char buf[131072];
788 char buf_rd[131072];
789 bool correct = True;
790 ssize_t bytes_read;
792 if (!NT_STATUS_IS_OK(cli_unlink(c1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
793 printf("unlink failed (%s) (normal, this file should not exist)\n", cli_errstr(c1));
796 if (!NT_STATUS_IS_OK(cli_open(c1, lockfname, O_RDWR | O_CREAT | O_EXCL,
797 DENY_NONE, &fnum1))) {
798 printf("first open read/write of %s failed (%s)\n",
799 lockfname, cli_errstr(c1));
800 return False;
802 if (!NT_STATUS_IS_OK(cli_open(c2, lockfname, O_RDONLY,
803 DENY_NONE, &fnum2))) {
804 printf("second open read-only of %s failed (%s)\n",
805 lockfname, cli_errstr(c2));
806 cli_close(c1, fnum1);
807 return False;
810 for (i=0;i<torture_numops;i++)
812 NTSTATUS status;
813 size_t buf_size = ((unsigned)sys_random()%(sizeof(buf)-1))+ 1;
814 if (i % 10 == 0) {
815 printf("%d\r", i); fflush(stdout);
818 generate_random_buffer((unsigned char *)buf, buf_size);
820 status = cli_writeall(c1, fnum1, 0, (uint8_t *)buf, 0,
821 buf_size, NULL);
822 if (!NT_STATUS_IS_OK(status)) {
823 printf("write failed (%s)\n", nt_errstr(status));
824 correct = False;
825 break;
828 if ((bytes_read = cli_read(c2, fnum2, buf_rd, 0, buf_size)) != buf_size) {
829 printf("read failed (%s)\n", cli_errstr(c2));
830 printf("read %d, expected %ld\n", (int)bytes_read,
831 (unsigned long)buf_size);
832 correct = False;
833 break;
836 if (memcmp(buf_rd, buf, buf_size) != 0)
838 printf("read/write compare failed\n");
839 correct = False;
840 break;
844 if (!NT_STATUS_IS_OK(cli_close(c2, fnum2))) {
845 printf("close failed (%s)\n", cli_errstr(c2));
846 correct = False;
848 if (!NT_STATUS_IS_OK(cli_close(c1, fnum1))) {
849 printf("close failed (%s)\n", cli_errstr(c1));
850 correct = False;
853 if (!NT_STATUS_IS_OK(cli_unlink(c1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
854 printf("unlink failed (%s)\n", cli_errstr(c1));
855 correct = False;
858 return correct;
861 static bool run_readwritetest(int dummy)
863 struct cli_state *cli1, *cli2;
864 bool test1, test2 = False;
866 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
867 return False;
869 cli_sockopt(cli1, sockops);
870 cli_sockopt(cli2, sockops);
872 printf("starting readwritetest\n");
874 test1 = rw_torture2(cli1, cli2);
875 printf("Passed readwritetest v1: %s\n", BOOLSTR(test1));
877 if (test1) {
878 test2 = rw_torture2(cli1, cli1);
879 printf("Passed readwritetest v2: %s\n", BOOLSTR(test2));
882 if (!torture_close_connection(cli1)) {
883 test1 = False;
886 if (!torture_close_connection(cli2)) {
887 test2 = False;
890 return (test1 && test2);
893 static bool run_readwritemulti(int dummy)
895 struct cli_state *cli;
896 bool test;
898 cli = current_cli;
900 cli_sockopt(cli, sockops);
902 printf("run_readwritemulti: fname %s\n", randomfname);
903 test = rw_torture3(cli, randomfname);
905 if (!torture_close_connection(cli)) {
906 test = False;
909 return test;
912 static bool run_readwritelarge_internal(int max_xmit_k)
914 static struct cli_state *cli1;
915 uint16_t fnum1;
916 const char *lockfname = "\\large.dat";
917 SMB_OFF_T fsize;
918 char buf[126*1024];
919 bool correct = True;
921 if (!torture_open_connection(&cli1, 0)) {
922 return False;
924 cli_sockopt(cli1, sockops);
925 memset(buf,'\0',sizeof(buf));
927 cli1->max_xmit = max_xmit_k*1024;
929 if (signing_state == Required) {
930 /* Horrible cheat to force
931 multiple signed outstanding
932 packets against a Samba server.
934 cli1->is_samba = false;
937 printf("starting readwritelarge_internal\n");
939 cli_unlink(cli1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
941 if (!NT_STATUS_IS_OK(cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE, &fnum1))) {
942 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
943 return False;
946 cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, sizeof(buf), NULL);
948 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
949 cli1, fnum1, NULL, &fsize, NULL, NULL,
950 NULL, NULL, NULL))) {
951 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
952 correct = False;
955 if (fsize == sizeof(buf))
956 printf("readwritelarge_internal test 1 succeeded (size = %lx)\n",
957 (unsigned long)fsize);
958 else {
959 printf("readwritelarge_internal test 1 failed (size = %lx)\n",
960 (unsigned long)fsize);
961 correct = False;
964 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
965 printf("close failed (%s)\n", cli_errstr(cli1));
966 correct = False;
969 if (!NT_STATUS_IS_OK(cli_unlink(cli1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
970 printf("unlink failed (%s)\n", cli_errstr(cli1));
971 correct = False;
974 if (!NT_STATUS_IS_OK(cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE, &fnum1))) {
975 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
976 return False;
979 cli1->max_xmit = 4*1024;
981 cli_smbwrite(cli1, fnum1, buf, 0, sizeof(buf), NULL);
983 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
984 cli1, fnum1, NULL, &fsize, NULL, NULL,
985 NULL, NULL, NULL))) {
986 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
987 correct = False;
990 if (fsize == sizeof(buf))
991 printf("readwritelarge_internal test 2 succeeded (size = %lx)\n",
992 (unsigned long)fsize);
993 else {
994 printf("readwritelarge_internal test 2 failed (size = %lx)\n",
995 (unsigned long)fsize);
996 correct = False;
999 #if 0
1000 /* ToDo - set allocation. JRA */
1001 if(!cli_set_allocation_size(cli1, fnum1, 0)) {
1002 printf("set allocation size to zero failed (%s)\n", cli_errstr(&cli1));
1003 return False;
1005 if (!cli_qfileinfo_basic(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL,
1006 NULL, NULL)) {
1007 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
1008 correct = False;
1010 if (fsize != 0)
1011 printf("readwritelarge test 3 (truncate test) succeeded (size = %x)\n", fsize);
1012 #endif
1014 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
1015 printf("close failed (%s)\n", cli_errstr(cli1));
1016 correct = False;
1019 if (!torture_close_connection(cli1)) {
1020 correct = False;
1022 return correct;
1025 static bool run_readwritelarge(int dummy)
1027 return run_readwritelarge_internal(128);
1030 static bool run_readwritelarge_signtest(int dummy)
1032 bool ret;
1033 signing_state = Required;
1034 ret = run_readwritelarge_internal(2);
1035 signing_state = Undefined;
1036 return ret;
1039 int line_count = 0;
1040 int nbio_id;
1042 #define ival(s) strtol(s, NULL, 0)
1044 /* run a test that simulates an approximate netbench client load */
1045 static bool run_netbench(int client)
1047 struct cli_state *cli;
1048 int i;
1049 char line[1024];
1050 char cname[20];
1051 FILE *f;
1052 const char *params[20];
1053 bool correct = True;
1055 cli = current_cli;
1057 nbio_id = client;
1059 cli_sockopt(cli, sockops);
1061 nb_setup(cli);
1063 slprintf(cname,sizeof(cname)-1, "client%d", client);
1065 f = fopen(client_txt, "r");
1067 if (!f) {
1068 perror(client_txt);
1069 return False;
1072 while (fgets(line, sizeof(line)-1, f)) {
1073 char *saveptr;
1074 line_count++;
1076 line[strlen(line)-1] = 0;
1078 /* printf("[%d] %s\n", line_count, line); */
1080 all_string_sub(line,"client1", cname, sizeof(line));
1082 /* parse the command parameters */
1083 params[0] = strtok_r(line, " ", &saveptr);
1084 i = 0;
1085 while (params[i]) params[++i] = strtok_r(NULL, " ", &saveptr);
1087 params[i] = "";
1089 if (i < 2) continue;
1091 if (!strncmp(params[0],"SMB", 3)) {
1092 printf("ERROR: You are using a dbench 1 load file\n");
1093 exit(1);
1096 if (!strcmp(params[0],"NTCreateX")) {
1097 nb_createx(params[1], ival(params[2]), ival(params[3]),
1098 ival(params[4]));
1099 } else if (!strcmp(params[0],"Close")) {
1100 nb_close(ival(params[1]));
1101 } else if (!strcmp(params[0],"Rename")) {
1102 nb_rename(params[1], params[2]);
1103 } else if (!strcmp(params[0],"Unlink")) {
1104 nb_unlink(params[1]);
1105 } else if (!strcmp(params[0],"Deltree")) {
1106 nb_deltree(params[1]);
1107 } else if (!strcmp(params[0],"Rmdir")) {
1108 nb_rmdir(params[1]);
1109 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
1110 nb_qpathinfo(params[1]);
1111 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
1112 nb_qfileinfo(ival(params[1]));
1113 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
1114 nb_qfsinfo(ival(params[1]));
1115 } else if (!strcmp(params[0],"FIND_FIRST")) {
1116 nb_findfirst(params[1]);
1117 } else if (!strcmp(params[0],"WriteX")) {
1118 nb_writex(ival(params[1]),
1119 ival(params[2]), ival(params[3]), ival(params[4]));
1120 } else if (!strcmp(params[0],"ReadX")) {
1121 nb_readx(ival(params[1]),
1122 ival(params[2]), ival(params[3]), ival(params[4]));
1123 } else if (!strcmp(params[0],"Flush")) {
1124 nb_flush(ival(params[1]));
1125 } else {
1126 printf("Unknown operation %s\n", params[0]);
1127 exit(1);
1130 fclose(f);
1132 nb_cleanup();
1134 if (!torture_close_connection(cli)) {
1135 correct = False;
1138 return correct;
1142 /* run a test that simulates an approximate netbench client load */
1143 static bool run_nbench(int dummy)
1145 double t;
1146 bool correct = True;
1148 nbio_shmem(nprocs);
1150 nbio_id = -1;
1152 signal(SIGALRM, nb_alarm);
1153 alarm(1);
1154 t = create_procs(run_netbench, &correct);
1155 alarm(0);
1157 printf("\nThroughput %g MB/sec\n",
1158 1.0e-6 * nbio_total() / t);
1159 return correct;
1164 This test checks for two things:
1166 1) correct support for retaining locks over a close (ie. the server
1167 must not use posix semantics)
1168 2) support for lock timeouts
1170 static bool run_locktest1(int dummy)
1172 struct cli_state *cli1, *cli2;
1173 const char *fname = "\\lockt1.lck";
1174 uint16_t fnum1, fnum2, fnum3;
1175 time_t t1, t2;
1176 unsigned lock_timeout;
1178 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1179 return False;
1181 cli_sockopt(cli1, sockops);
1182 cli_sockopt(cli2, sockops);
1184 printf("starting locktest1\n");
1186 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1188 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1189 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1190 return False;
1192 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum2))) {
1193 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli1));
1194 return False;
1196 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum3))) {
1197 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli2));
1198 return False;
1201 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
1202 printf("lock1 failed (%s)\n", cli_errstr(cli1));
1203 return False;
1207 if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1208 printf("lock2 succeeded! This is a locking bug\n");
1209 return False;
1210 } else {
1211 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1212 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1216 lock_timeout = (1 + (random() % 20));
1217 printf("Testing lock timeout with timeout=%u\n", lock_timeout);
1218 t1 = time(NULL);
1219 if (cli_lock(cli2, fnum3, 0, 4, lock_timeout * 1000, WRITE_LOCK)) {
1220 printf("lock3 succeeded! This is a locking bug\n");
1221 return False;
1222 } else {
1223 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1224 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1226 t2 = time(NULL);
1228 if (ABS(t2 - t1) < lock_timeout-1) {
1229 printf("error: This server appears not to support timed lock requests\n");
1232 printf("server slept for %u seconds for a %u second timeout\n",
1233 (unsigned int)(t2-t1), lock_timeout);
1235 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
1236 printf("close1 failed (%s)\n", cli_errstr(cli1));
1237 return False;
1240 if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1241 printf("lock4 succeeded! This is a locking bug\n");
1242 return False;
1243 } else {
1244 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1245 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1248 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
1249 printf("close2 failed (%s)\n", cli_errstr(cli1));
1250 return False;
1253 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum3))) {
1254 printf("close3 failed (%s)\n", cli_errstr(cli2));
1255 return False;
1258 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
1259 printf("unlink failed (%s)\n", cli_errstr(cli1));
1260 return False;
1264 if (!torture_close_connection(cli1)) {
1265 return False;
1268 if (!torture_close_connection(cli2)) {
1269 return False;
1272 printf("Passed locktest1\n");
1273 return True;
1277 this checks to see if a secondary tconx can use open files from an
1278 earlier tconx
1280 static bool run_tcon_test(int dummy)
1282 static struct cli_state *cli;
1283 const char *fname = "\\tcontest.tmp";
1284 uint16 fnum1;
1285 uint16 cnum1, cnum2, cnum3;
1286 uint16 vuid1, vuid2;
1287 char buf[4];
1288 bool ret = True;
1289 NTSTATUS status;
1291 memset(buf, '\0', sizeof(buf));
1293 if (!torture_open_connection(&cli, 0)) {
1294 return False;
1296 cli_sockopt(cli, sockops);
1298 printf("starting tcontest\n");
1300 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1302 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1303 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1304 return False;
1307 cnum1 = cli->cnum;
1308 vuid1 = cli->vuid;
1310 status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
1311 if (!NT_STATUS_IS_OK(status)) {
1312 printf("initial write failed (%s)", nt_errstr(status));
1313 return False;
1316 status = cli_tcon_andx(cli, share, "?????",
1317 password, strlen(password)+1);
1318 if (!NT_STATUS_IS_OK(status)) {
1319 printf("%s refused 2nd tree connect (%s)\n", host,
1320 nt_errstr(status));
1321 cli_shutdown(cli);
1322 return False;
1325 cnum2 = cli->cnum;
1326 cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
1327 vuid2 = cli->vuid + 1;
1329 /* try a write with the wrong tid */
1330 cli->cnum = cnum2;
1332 status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
1333 if (NT_STATUS_IS_OK(status)) {
1334 printf("* server allows write with wrong TID\n");
1335 ret = False;
1336 } else {
1337 printf("server fails write with wrong TID : %s\n",
1338 nt_errstr(status));
1342 /* try a write with an invalid tid */
1343 cli->cnum = cnum3;
1345 status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
1346 if (NT_STATUS_IS_OK(status)) {
1347 printf("* server allows write with invalid TID\n");
1348 ret = False;
1349 } else {
1350 printf("server fails write with invalid TID : %s\n",
1351 nt_errstr(status));
1354 /* try a write with an invalid vuid */
1355 cli->vuid = vuid2;
1356 cli->cnum = cnum1;
1358 status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
1359 if (NT_STATUS_IS_OK(status)) {
1360 printf("* server allows write with invalid VUID\n");
1361 ret = False;
1362 } else {
1363 printf("server fails write with invalid VUID : %s\n",
1364 nt_errstr(status));
1367 cli->cnum = cnum1;
1368 cli->vuid = vuid1;
1370 if (!NT_STATUS_IS_OK(cli_close(cli, fnum1))) {
1371 printf("close failed (%s)\n", cli_errstr(cli));
1372 return False;
1375 cli->cnum = cnum2;
1377 status = cli_tdis(cli);
1378 if (!NT_STATUS_IS_OK(status)) {
1379 printf("secondary tdis failed (%s)\n", nt_errstr(status));
1380 return False;
1383 cli->cnum = cnum1;
1385 if (!torture_close_connection(cli)) {
1386 return False;
1389 return ret;
1394 checks for old style tcon support
1396 static bool run_tcon2_test(int dummy)
1398 static struct cli_state *cli;
1399 uint16 cnum, max_xmit;
1400 char *service;
1401 NTSTATUS status;
1403 if (!torture_open_connection(&cli, 0)) {
1404 return False;
1406 cli_sockopt(cli, sockops);
1408 printf("starting tcon2 test\n");
1410 if (asprintf(&service, "\\\\%s\\%s", host, share) == -1) {
1411 return false;
1414 status = cli_raw_tcon(cli, service, password, "?????", &max_xmit, &cnum);
1416 SAFE_FREE(service);
1418 if (!NT_STATUS_IS_OK(status)) {
1419 printf("tcon2 failed : %s\n", nt_errstr(status));
1420 } else {
1421 printf("tcon OK : max_xmit=%d cnum=%d\n",
1422 (int)max_xmit, (int)cnum);
1425 if (!torture_close_connection(cli)) {
1426 return False;
1429 printf("Passed tcon2 test\n");
1430 return True;
1433 static bool tcon_devtest(struct cli_state *cli,
1434 const char *myshare, const char *devtype,
1435 const char *return_devtype,
1436 NTSTATUS expected_error)
1438 NTSTATUS status;
1439 bool ret;
1441 status = cli_tcon_andx(cli, myshare, devtype,
1442 password, strlen(password)+1);
1444 if (NT_STATUS_IS_OK(expected_error)) {
1445 if (NT_STATUS_IS_OK(status)) {
1446 if (strcmp(cli->dev, return_devtype) == 0) {
1447 ret = True;
1448 } else {
1449 printf("tconX to share %s with type %s "
1450 "succeeded but returned the wrong "
1451 "device type (got [%s] but should have got [%s])\n",
1452 myshare, devtype, cli->dev, return_devtype);
1453 ret = False;
1455 } else {
1456 printf("tconX to share %s with type %s "
1457 "should have succeeded but failed\n",
1458 myshare, devtype);
1459 ret = False;
1461 cli_tdis(cli);
1462 } else {
1463 if (NT_STATUS_IS_OK(status)) {
1464 printf("tconx to share %s with type %s "
1465 "should have failed but succeeded\n",
1466 myshare, devtype);
1467 ret = False;
1468 } else {
1469 if (NT_STATUS_EQUAL(cli_nt_error(cli),
1470 expected_error)) {
1471 ret = True;
1472 } else {
1473 printf("Returned unexpected error\n");
1474 ret = False;
1478 return ret;
1482 checks for correct tconX support
1484 static bool run_tcon_devtype_test(int dummy)
1486 static struct cli_state *cli1 = NULL;
1487 int flags = 0;
1488 NTSTATUS status;
1489 bool ret = True;
1491 status = cli_full_connection(&cli1, myname,
1492 host, NULL, port_to_use,
1493 NULL, NULL,
1494 username, workgroup,
1495 password, flags, signing_state);
1497 if (!NT_STATUS_IS_OK(status)) {
1498 printf("could not open connection\n");
1499 return False;
1502 if (!tcon_devtest(cli1, "IPC$", "A:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1503 ret = False;
1505 if (!tcon_devtest(cli1, "IPC$", "?????", "IPC", NT_STATUS_OK))
1506 ret = False;
1508 if (!tcon_devtest(cli1, "IPC$", "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1509 ret = False;
1511 if (!tcon_devtest(cli1, "IPC$", "IPC", "IPC", NT_STATUS_OK))
1512 ret = False;
1514 if (!tcon_devtest(cli1, "IPC$", "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1515 ret = False;
1517 if (!tcon_devtest(cli1, share, "A:", "A:", NT_STATUS_OK))
1518 ret = False;
1520 if (!tcon_devtest(cli1, share, "?????", "A:", NT_STATUS_OK))
1521 ret = False;
1523 if (!tcon_devtest(cli1, share, "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1524 ret = False;
1526 if (!tcon_devtest(cli1, share, "IPC", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1527 ret = False;
1529 if (!tcon_devtest(cli1, share, "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1530 ret = False;
1532 cli_shutdown(cli1);
1534 if (ret)
1535 printf("Passed tcondevtest\n");
1537 return ret;
1542 This test checks that
1544 1) the server supports multiple locking contexts on the one SMB
1545 connection, distinguished by PID.
1547 2) the server correctly fails overlapping locks made by the same PID (this
1548 goes against POSIX behaviour, which is why it is tricky to implement)
1550 3) the server denies unlock requests by an incorrect client PID
1552 static bool run_locktest2(int dummy)
1554 static struct cli_state *cli;
1555 const char *fname = "\\lockt2.lck";
1556 uint16_t fnum1, fnum2, fnum3;
1557 bool correct = True;
1559 if (!torture_open_connection(&cli, 0)) {
1560 return False;
1563 cli_sockopt(cli, sockops);
1565 printf("starting locktest2\n");
1567 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1569 cli_setpid(cli, 1);
1571 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1572 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1573 return False;
1576 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum2))) {
1577 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli));
1578 return False;
1581 cli_setpid(cli, 2);
1583 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum3))) {
1584 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli));
1585 return False;
1588 cli_setpid(cli, 1);
1590 if (!cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1591 printf("lock1 failed (%s)\n", cli_errstr(cli));
1592 return False;
1595 if (cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1596 printf("WRITE lock1 succeeded! This is a locking bug\n");
1597 correct = False;
1598 } else {
1599 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1600 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1603 if (cli_lock(cli, fnum2, 0, 4, 0, WRITE_LOCK)) {
1604 printf("WRITE lock2 succeeded! This is a locking bug\n");
1605 correct = False;
1606 } else {
1607 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1608 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1611 if (cli_lock(cli, fnum2, 0, 4, 0, READ_LOCK)) {
1612 printf("READ lock2 succeeded! This is a locking bug\n");
1613 correct = False;
1614 } else {
1615 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1616 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1619 if (!cli_lock(cli, fnum1, 100, 4, 0, WRITE_LOCK)) {
1620 printf("lock at 100 failed (%s)\n", cli_errstr(cli));
1622 cli_setpid(cli, 2);
1623 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 100, 4))) {
1624 printf("unlock at 100 succeeded! This is a locking bug\n");
1625 correct = False;
1628 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 0, 4))) {
1629 printf("unlock1 succeeded! This is a locking bug\n");
1630 correct = False;
1631 } else {
1632 if (!check_error(__LINE__, cli,
1633 ERRDOS, ERRlock,
1634 NT_STATUS_RANGE_NOT_LOCKED)) return False;
1637 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 0, 8))) {
1638 printf("unlock2 succeeded! This is a locking bug\n");
1639 correct = False;
1640 } else {
1641 if (!check_error(__LINE__, cli,
1642 ERRDOS, ERRlock,
1643 NT_STATUS_RANGE_NOT_LOCKED)) return False;
1646 if (cli_lock(cli, fnum3, 0, 4, 0, WRITE_LOCK)) {
1647 printf("lock3 succeeded! This is a locking bug\n");
1648 correct = False;
1649 } else {
1650 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
1653 cli_setpid(cli, 1);
1655 if (!NT_STATUS_IS_OK(cli_close(cli, fnum1))) {
1656 printf("close1 failed (%s)\n", cli_errstr(cli));
1657 return False;
1660 if (!NT_STATUS_IS_OK(cli_close(cli, fnum2))) {
1661 printf("close2 failed (%s)\n", cli_errstr(cli));
1662 return False;
1665 if (!NT_STATUS_IS_OK(cli_close(cli, fnum3))) {
1666 printf("close3 failed (%s)\n", cli_errstr(cli));
1667 return False;
1670 if (!torture_close_connection(cli)) {
1671 correct = False;
1674 printf("locktest2 finished\n");
1676 return correct;
1681 This test checks that
1683 1) the server supports the full offset range in lock requests
1685 static bool run_locktest3(int dummy)
1687 static struct cli_state *cli1, *cli2;
1688 const char *fname = "\\lockt3.lck";
1689 uint16_t fnum1, fnum2;
1690 int i;
1691 uint32 offset;
1692 bool correct = True;
1694 #define NEXT_OFFSET offset += (~(uint32)0) / torture_numops
1696 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1697 return False;
1699 cli_sockopt(cli1, sockops);
1700 cli_sockopt(cli2, sockops);
1702 printf("starting locktest3\n");
1704 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1706 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1707 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1708 return False;
1710 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2))) {
1711 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli2));
1712 return False;
1715 for (offset=i=0;i<torture_numops;i++) {
1716 NEXT_OFFSET;
1717 if (!cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1718 printf("lock1 %d failed (%s)\n",
1720 cli_errstr(cli1));
1721 return False;
1724 if (!cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1725 printf("lock2 %d failed (%s)\n",
1727 cli_errstr(cli1));
1728 return False;
1732 for (offset=i=0;i<torture_numops;i++) {
1733 NEXT_OFFSET;
1735 if (cli_lock(cli1, fnum1, offset-2, 1, 0, WRITE_LOCK)) {
1736 printf("error: lock1 %d succeeded!\n", i);
1737 return False;
1740 if (cli_lock(cli2, fnum2, offset-1, 1, 0, WRITE_LOCK)) {
1741 printf("error: lock2 %d succeeded!\n", i);
1742 return False;
1745 if (cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1746 printf("error: lock3 %d succeeded!\n", i);
1747 return False;
1750 if (cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1751 printf("error: lock4 %d succeeded!\n", i);
1752 return False;
1756 for (offset=i=0;i<torture_numops;i++) {
1757 NEXT_OFFSET;
1759 if (!NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, offset-1, 1))) {
1760 printf("unlock1 %d failed (%s)\n",
1762 cli_errstr(cli1));
1763 return False;
1766 if (!NT_STATUS_IS_OK(cli_unlock(cli2, fnum2, offset-2, 1))) {
1767 printf("unlock2 %d failed (%s)\n",
1769 cli_errstr(cli1));
1770 return False;
1774 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
1775 printf("close1 failed (%s)\n", cli_errstr(cli1));
1776 return False;
1779 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
1780 printf("close2 failed (%s)\n", cli_errstr(cli2));
1781 return False;
1784 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
1785 printf("unlink failed (%s)\n", cli_errstr(cli1));
1786 return False;
1789 if (!torture_close_connection(cli1)) {
1790 correct = False;
1793 if (!torture_close_connection(cli2)) {
1794 correct = False;
1797 printf("finished locktest3\n");
1799 return correct;
1802 #define EXPECTED(ret, v) if ((ret) != (v)) { \
1803 printf("** "); correct = False; \
1807 looks at overlapping locks
1809 static bool run_locktest4(int dummy)
1811 static struct cli_state *cli1, *cli2;
1812 const char *fname = "\\lockt4.lck";
1813 uint16_t fnum1, fnum2, f;
1814 bool ret;
1815 char buf[1000];
1816 bool correct = True;
1817 NTSTATUS status;
1819 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1820 return False;
1823 cli_sockopt(cli1, sockops);
1824 cli_sockopt(cli2, sockops);
1826 printf("starting locktest4\n");
1828 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1830 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1831 cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
1833 memset(buf, 0, sizeof(buf));
1835 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, sizeof(buf),
1836 NULL);
1837 if (!NT_STATUS_IS_OK(status)) {
1838 printf("Failed to create file: %s\n", nt_errstr(status));
1839 correct = False;
1840 goto fail;
1843 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1844 cli_lock(cli1, fnum1, 2, 4, 0, WRITE_LOCK);
1845 EXPECTED(ret, False);
1846 printf("the same process %s set overlapping write locks\n", ret?"can":"cannot");
1848 ret = cli_lock(cli1, fnum1, 10, 4, 0, READ_LOCK) &&
1849 cli_lock(cli1, fnum1, 12, 4, 0, READ_LOCK);
1850 EXPECTED(ret, True);
1851 printf("the same process %s set overlapping read locks\n", ret?"can":"cannot");
1853 ret = cli_lock(cli1, fnum1, 20, 4, 0, WRITE_LOCK) &&
1854 cli_lock(cli2, fnum2, 22, 4, 0, WRITE_LOCK);
1855 EXPECTED(ret, False);
1856 printf("a different connection %s set overlapping write locks\n", ret?"can":"cannot");
1858 ret = cli_lock(cli1, fnum1, 30, 4, 0, READ_LOCK) &&
1859 cli_lock(cli2, fnum2, 32, 4, 0, READ_LOCK);
1860 EXPECTED(ret, True);
1861 printf("a different connection %s set overlapping read locks\n", ret?"can":"cannot");
1863 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 40, 4, 0, WRITE_LOCK)) &&
1864 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 42, 4, 0, WRITE_LOCK));
1865 EXPECTED(ret, False);
1866 printf("a different pid %s set overlapping write locks\n", ret?"can":"cannot");
1868 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 50, 4, 0, READ_LOCK)) &&
1869 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 52, 4, 0, READ_LOCK));
1870 EXPECTED(ret, True);
1871 printf("a different pid %s set overlapping read locks\n", ret?"can":"cannot");
1873 ret = cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK) &&
1874 cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK);
1875 EXPECTED(ret, True);
1876 printf("the same process %s set the same read lock twice\n", ret?"can":"cannot");
1878 ret = cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK) &&
1879 cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK);
1880 EXPECTED(ret, False);
1881 printf("the same process %s set the same write lock twice\n", ret?"can":"cannot");
1883 ret = cli_lock(cli1, fnum1, 80, 4, 0, READ_LOCK) &&
1884 cli_lock(cli1, fnum1, 80, 4, 0, WRITE_LOCK);
1885 EXPECTED(ret, False);
1886 printf("the same process %s overlay a read lock with a write lock\n", ret?"can":"cannot");
1888 ret = cli_lock(cli1, fnum1, 90, 4, 0, WRITE_LOCK) &&
1889 cli_lock(cli1, fnum1, 90, 4, 0, READ_LOCK);
1890 EXPECTED(ret, True);
1891 printf("the same process %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1893 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 100, 4, 0, WRITE_LOCK)) &&
1894 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 100, 4, 0, READ_LOCK));
1895 EXPECTED(ret, False);
1896 printf("a different pid %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1898 ret = cli_lock(cli1, fnum1, 110, 4, 0, READ_LOCK) &&
1899 cli_lock(cli1, fnum1, 112, 4, 0, READ_LOCK) &&
1900 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 110, 6));
1901 EXPECTED(ret, False);
1902 printf("the same process %s coalesce read locks\n", ret?"can":"cannot");
1905 ret = cli_lock(cli1, fnum1, 120, 4, 0, WRITE_LOCK) &&
1906 (cli_read(cli2, fnum2, buf, 120, 4) == 4);
1907 EXPECTED(ret, False);
1908 printf("this server %s strict write locking\n", ret?"doesn't do":"does");
1910 ret = cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK);
1911 if (ret) {
1912 status = cli_writeall(cli2, fnum2, 0, (uint8_t *)buf, 130, 4,
1913 NULL);
1914 ret = NT_STATUS_IS_OK(status);
1916 EXPECTED(ret, False);
1917 printf("this server %s strict read locking\n", ret?"doesn't do":"does");
1920 ret = cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1921 cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1922 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4)) &&
1923 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4));
1924 EXPECTED(ret, True);
1925 printf("this server %s do recursive read locking\n", ret?"does":"doesn't");
1928 ret = cli_lock(cli1, fnum1, 150, 4, 0, WRITE_LOCK) &&
1929 cli_lock(cli1, fnum1, 150, 4, 0, READ_LOCK) &&
1930 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4)) &&
1931 (cli_read(cli2, fnum2, buf, 150, 4) == 4) &&
1932 !(NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
1933 150, 4, NULL))) &&
1934 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4));
1935 EXPECTED(ret, True);
1936 printf("this server %s do recursive lock overlays\n", ret?"does":"doesn't");
1938 ret = cli_lock(cli1, fnum1, 160, 4, 0, READ_LOCK) &&
1939 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 160, 4)) &&
1940 NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
1941 160, 4, NULL)) &&
1942 (cli_read(cli2, fnum2, buf, 160, 4) == 4);
1943 EXPECTED(ret, True);
1944 printf("the same process %s remove a read lock using write locking\n", ret?"can":"cannot");
1946 ret = cli_lock(cli1, fnum1, 170, 4, 0, WRITE_LOCK) &&
1947 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 170, 4)) &&
1948 NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
1949 170, 4, NULL)) &&
1950 (cli_read(cli2, fnum2, buf, 170, 4) == 4);
1951 EXPECTED(ret, True);
1952 printf("the same process %s remove a write lock using read locking\n", ret?"can":"cannot");
1954 ret = cli_lock(cli1, fnum1, 190, 4, 0, WRITE_LOCK) &&
1955 cli_lock(cli1, fnum1, 190, 4, 0, READ_LOCK) &&
1956 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 190, 4)) &&
1957 !NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
1958 190, 4, NULL)) &&
1959 (cli_read(cli2, fnum2, buf, 190, 4) == 4);
1960 EXPECTED(ret, True);
1961 printf("the same process %s remove the first lock first\n", ret?"does":"doesn't");
1963 cli_close(cli1, fnum1);
1964 cli_close(cli2, fnum2);
1965 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
1966 cli_open(cli1, fname, O_RDWR, DENY_NONE, &f);
1967 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1968 cli_lock(cli1, f, 0, 1, 0, READ_LOCK) &&
1969 NT_STATUS_IS_OK(cli_close(cli1, fnum1)) &&
1970 NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1)) &&
1971 cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1972 cli_close(cli1, f);
1973 cli_close(cli1, fnum1);
1974 EXPECTED(ret, True);
1975 printf("the server %s have the NT byte range lock bug\n", !ret?"does":"doesn't");
1977 fail:
1978 cli_close(cli1, fnum1);
1979 cli_close(cli2, fnum2);
1980 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1981 torture_close_connection(cli1);
1982 torture_close_connection(cli2);
1984 printf("finished locktest4\n");
1985 return correct;
1989 looks at lock upgrade/downgrade.
1991 static bool run_locktest5(int dummy)
1993 static struct cli_state *cli1, *cli2;
1994 const char *fname = "\\lockt5.lck";
1995 uint16_t fnum1, fnum2, fnum3;
1996 bool ret;
1997 char buf[1000];
1998 bool correct = True;
1999 NTSTATUS status;
2001 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
2002 return False;
2005 cli_sockopt(cli1, sockops);
2006 cli_sockopt(cli2, sockops);
2008 printf("starting locktest5\n");
2010 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2012 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
2013 cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
2014 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum3);
2016 memset(buf, 0, sizeof(buf));
2018 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, sizeof(buf),
2019 NULL);
2020 if (!NT_STATUS_IS_OK(status)) {
2021 printf("Failed to create file: %s\n", nt_errstr(status));
2022 correct = False;
2023 goto fail;
2026 /* Check for NT bug... */
2027 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
2028 cli_lock(cli1, fnum3, 0, 1, 0, READ_LOCK);
2029 cli_close(cli1, fnum1);
2030 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
2031 ret = cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
2032 EXPECTED(ret, True);
2033 printf("this server %s the NT locking bug\n", ret ? "doesn't have" : "has");
2034 cli_close(cli1, fnum1);
2035 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
2036 cli_unlock(cli1, fnum3, 0, 1);
2038 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
2039 cli_lock(cli1, fnum1, 1, 1, 0, READ_LOCK);
2040 EXPECTED(ret, True);
2041 printf("the same process %s overlay a write with a read lock\n", ret?"can":"cannot");
2043 ret = cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
2044 EXPECTED(ret, False);
2046 printf("a different processs %s get a read lock on the first process lock stack\n", ret?"can":"cannot");
2048 /* Unlock the process 2 lock. */
2049 cli_unlock(cli2, fnum2, 0, 4);
2051 ret = cli_lock(cli1, fnum3, 0, 4, 0, READ_LOCK);
2052 EXPECTED(ret, False);
2054 printf("the same processs on a different fnum %s get a read lock\n", ret?"can":"cannot");
2056 /* Unlock the process 1 fnum3 lock. */
2057 cli_unlock(cli1, fnum3, 0, 4);
2059 /* Stack 2 more locks here. */
2060 ret = cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK) &&
2061 cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK);
2063 EXPECTED(ret, True);
2064 printf("the same process %s stack read locks\n", ret?"can":"cannot");
2066 /* Unlock the first process lock, then check this was the WRITE lock that was
2067 removed. */
2069 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4)) &&
2070 cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
2072 EXPECTED(ret, True);
2073 printf("the first unlock removes the %s lock\n", ret?"WRITE":"READ");
2075 /* Unlock the process 2 lock. */
2076 cli_unlock(cli2, fnum2, 0, 4);
2078 /* We should have 3 stacked locks here. Ensure we need to do 3 unlocks. */
2080 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 1, 1)) &&
2081 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4)) &&
2082 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4));
2084 EXPECTED(ret, True);
2085 printf("the same process %s unlock the stack of 4 locks\n", ret?"can":"cannot");
2087 /* Ensure the next unlock fails. */
2088 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4));
2089 EXPECTED(ret, False);
2090 printf("the same process %s count the lock stack\n", !ret?"can":"cannot");
2092 /* Ensure connection 2 can get a write lock. */
2093 ret = cli_lock(cli2, fnum2, 0, 4, 0, WRITE_LOCK);
2094 EXPECTED(ret, True);
2096 printf("a different processs %s get a write lock on the unlocked stack\n", ret?"can":"cannot");
2099 fail:
2100 cli_close(cli1, fnum1);
2101 cli_close(cli2, fnum2);
2102 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2103 if (!torture_close_connection(cli1)) {
2104 correct = False;
2106 if (!torture_close_connection(cli2)) {
2107 correct = False;
2110 printf("finished locktest5\n");
2112 return correct;
2116 tries the unusual lockingX locktype bits
2118 static bool run_locktest6(int dummy)
2120 static struct cli_state *cli;
2121 const char *fname[1] = { "\\lock6.txt" };
2122 int i;
2123 uint16_t fnum;
2124 NTSTATUS status;
2126 if (!torture_open_connection(&cli, 0)) {
2127 return False;
2130 cli_sockopt(cli, sockops);
2132 printf("starting locktest6\n");
2134 for (i=0;i<1;i++) {
2135 printf("Testing %s\n", fname[i]);
2137 cli_unlink(cli, fname[i], FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2139 cli_open(cli, fname[i], O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
2140 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CHANGE_LOCKTYPE);
2141 cli_close(cli, fnum);
2142 printf("CHANGE_LOCKTYPE gave %s\n", nt_errstr(status));
2144 cli_open(cli, fname[i], O_RDWR, DENY_NONE, &fnum);
2145 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CANCEL_LOCK);
2146 cli_close(cli, fnum);
2147 printf("CANCEL_LOCK gave %s\n", nt_errstr(status));
2149 cli_unlink(cli, fname[i], FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2152 torture_close_connection(cli);
2154 printf("finished locktest6\n");
2155 return True;
2158 static bool run_locktest7(int dummy)
2160 struct cli_state *cli1;
2161 const char *fname = "\\lockt7.lck";
2162 uint16_t fnum1;
2163 char buf[200];
2164 bool correct = False;
2165 NTSTATUS status;
2167 if (!torture_open_connection(&cli1, 0)) {
2168 return False;
2171 cli_sockopt(cli1, sockops);
2173 printf("starting locktest7\n");
2175 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2177 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
2179 memset(buf, 0, sizeof(buf));
2181 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, sizeof(buf),
2182 NULL);
2183 if (!NT_STATUS_IS_OK(status)) {
2184 printf("Failed to create file: %s\n", nt_errstr(status));
2185 goto fail;
2188 cli_setpid(cli1, 1);
2190 if (!cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK)) {
2191 printf("Unable to apply read lock on range 130:4, error was %s\n", cli_errstr(cli1));
2192 goto fail;
2193 } else {
2194 printf("pid1 successfully locked range 130:4 for READ\n");
2197 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2198 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2199 goto fail;
2200 } else {
2201 printf("pid1 successfully read the range 130:4\n");
2204 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2205 if (!NT_STATUS_IS_OK(status)) {
2206 printf("pid1 unable to write to the range 130:4, error was "
2207 "%s\n", nt_errstr(status));
2208 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
2209 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2210 goto fail;
2212 } else {
2213 printf("pid1 successfully wrote to the range 130:4 (should be denied)\n");
2214 goto fail;
2217 cli_setpid(cli1, 2);
2219 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2220 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2221 } else {
2222 printf("pid2 successfully read the range 130:4\n");
2225 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2226 if (!NT_STATUS_IS_OK(status)) {
2227 printf("pid2 unable to write to the range 130:4, error was "
2228 "%s\n", nt_errstr(status));
2229 if (!NT_STATUS_EQUAL(status, 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_setpid(cli1, 1);
2239 cli_unlock(cli1, fnum1, 130, 4);
2241 if (!cli_lock(cli1, fnum1, 130, 4, 0, WRITE_LOCK)) {
2242 printf("Unable to apply write lock on range 130:4, error was %s\n", cli_errstr(cli1));
2243 goto fail;
2244 } else {
2245 printf("pid1 successfully locked range 130:4 for WRITE\n");
2248 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2249 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2250 goto fail;
2251 } else {
2252 printf("pid1 successfully read the range 130:4\n");
2255 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2256 if (!NT_STATUS_IS_OK(status)) {
2257 printf("pid1 unable to write to the range 130:4, error was "
2258 "%s\n", nt_errstr(status));
2259 goto fail;
2260 } else {
2261 printf("pid1 successfully wrote to the range 130:4\n");
2264 cli_setpid(cli1, 2);
2266 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2267 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2268 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2269 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2270 goto fail;
2272 } else {
2273 printf("pid2 successfully read the range 130:4 (should be denied)\n");
2274 goto fail;
2277 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2278 if (!NT_STATUS_IS_OK(status)) {
2279 printf("pid2 unable to write to the range 130:4, error was "
2280 "%s\n", nt_errstr(status));
2281 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
2282 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2283 goto fail;
2285 } else {
2286 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2287 goto fail;
2290 cli_unlock(cli1, fnum1, 130, 0);
2291 correct = True;
2293 fail:
2294 cli_close(cli1, fnum1);
2295 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2296 torture_close_connection(cli1);
2298 printf("finished locktest7\n");
2299 return correct;
2303 * This demonstrates a problem with our use of GPFS share modes: A file
2304 * descriptor sitting in the pending close queue holding a GPFS share mode
2305 * blocks opening a file another time. Happens with Word 2007 temp files.
2306 * With "posix locking = yes" and "gpfs:sharemodes = yes" enabled, the third
2307 * open is denied with NT_STATUS_SHARING_VIOLATION.
2310 static bool run_locktest8(int dummy)
2312 struct cli_state *cli1;
2313 const char *fname = "\\lockt8.lck";
2314 uint16_t fnum1, fnum2;
2315 char buf[200];
2316 bool correct = False;
2317 NTSTATUS status;
2319 if (!torture_open_connection(&cli1, 0)) {
2320 return False;
2323 cli_sockopt(cli1, sockops);
2325 printf("starting locktest8\n");
2327 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2329 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_WRITE,
2330 &fnum1);
2331 if (!NT_STATUS_IS_OK(status)) {
2332 d_fprintf(stderr, "cli_open returned %s\n", cli_errstr(cli1));
2333 return false;
2336 memset(buf, 0, sizeof(buf));
2338 status = cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum2);
2339 if (!NT_STATUS_IS_OK(status)) {
2340 d_fprintf(stderr, "cli_open second time returned %s\n",
2341 cli_errstr(cli1));
2342 goto fail;
2345 if (!cli_lock(cli1, fnum2, 1, 1, 0, READ_LOCK)) {
2346 printf("Unable to apply read lock on range 1:1, error was "
2347 "%s\n", cli_errstr(cli1));
2348 goto fail;
2351 status = cli_close(cli1, fnum1);
2352 if (!NT_STATUS_IS_OK(status)) {
2353 d_fprintf(stderr, "cli_close(fnum1) %s\n", cli_errstr(cli1));
2354 goto fail;
2357 status = cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
2358 if (!NT_STATUS_IS_OK(status)) {
2359 d_fprintf(stderr, "cli_open third time returned %s\n",
2360 cli_errstr(cli1));
2361 goto fail;
2364 correct = true;
2366 fail:
2367 cli_close(cli1, fnum1);
2368 cli_close(cli1, fnum2);
2369 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2370 torture_close_connection(cli1);
2372 printf("finished locktest8\n");
2373 return correct;
2377 * This test is designed to be run in conjunction with
2378 * external NFS or POSIX locks taken in the filesystem.
2379 * It checks that the smbd server will block until the
2380 * lock is released and then acquire it. JRA.
2383 static bool got_alarm;
2384 static int alarm_fd;
2386 static void alarm_handler(int dummy)
2388 got_alarm = True;
2391 static void alarm_handler_parent(int dummy)
2393 close(alarm_fd);
2396 static void do_local_lock(int read_fd, int write_fd)
2398 int fd;
2399 char c = '\0';
2400 struct flock lock;
2401 const char *local_pathname = NULL;
2402 int ret;
2404 local_pathname = talloc_asprintf(talloc_tos(),
2405 "%s/lockt9.lck", local_path);
2406 if (!local_pathname) {
2407 printf("child: alloc fail\n");
2408 exit(1);
2411 unlink(local_pathname);
2412 fd = open(local_pathname, O_RDWR|O_CREAT, 0666);
2413 if (fd == -1) {
2414 printf("child: open of %s failed %s.\n",
2415 local_pathname, strerror(errno));
2416 exit(1);
2419 /* Now take a fcntl lock. */
2420 lock.l_type = F_WRLCK;
2421 lock.l_whence = SEEK_SET;
2422 lock.l_start = 0;
2423 lock.l_len = 4;
2424 lock.l_pid = getpid();
2426 ret = fcntl(fd,F_SETLK,&lock);
2427 if (ret == -1) {
2428 printf("child: failed to get lock 0:4 on file %s. Error %s\n",
2429 local_pathname, strerror(errno));
2430 exit(1);
2431 } else {
2432 printf("child: got lock 0:4 on file %s.\n",
2433 local_pathname );
2434 fflush(stdout);
2437 CatchSignal(SIGALRM, alarm_handler);
2438 alarm(5);
2439 /* Signal the parent. */
2440 if (write(write_fd, &c, 1) != 1) {
2441 printf("child: start signal fail %s.\n",
2442 strerror(errno));
2443 exit(1);
2445 alarm(0);
2447 alarm(10);
2448 /* Wait for the parent to be ready. */
2449 if (read(read_fd, &c, 1) != 1) {
2450 printf("child: reply signal fail %s.\n",
2451 strerror(errno));
2452 exit(1);
2454 alarm(0);
2456 sleep(5);
2457 close(fd);
2458 printf("child: released lock 0:4 on file %s.\n",
2459 local_pathname );
2460 fflush(stdout);
2461 exit(0);
2464 static bool run_locktest9(int dummy)
2466 struct cli_state *cli1;
2467 const char *fname = "\\lockt9.lck";
2468 uint16_t fnum;
2469 bool correct = False;
2470 int pipe_in[2], pipe_out[2];
2471 pid_t child_pid;
2472 char c = '\0';
2473 int ret;
2474 struct timeval start;
2475 double seconds;
2476 NTSTATUS status;
2478 printf("starting locktest9\n");
2480 if (local_path == NULL) {
2481 d_fprintf(stderr, "locktest9 must be given a local path via -l <localpath>\n");
2482 return false;
2485 if (pipe(pipe_in) == -1 || pipe(pipe_out) == -1) {
2486 return false;
2489 child_pid = fork();
2490 if (child_pid == -1) {
2491 return false;
2494 if (child_pid == 0) {
2495 /* Child. */
2496 do_local_lock(pipe_out[0], pipe_in[1]);
2497 exit(0);
2500 close(pipe_out[0]);
2501 close(pipe_in[1]);
2502 pipe_out[0] = -1;
2503 pipe_in[1] = -1;
2505 /* Parent. */
2506 ret = read(pipe_in[0], &c, 1);
2507 if (ret != 1) {
2508 d_fprintf(stderr, "failed to read start signal from child. %s\n",
2509 strerror(errno));
2510 return false;
2513 if (!torture_open_connection(&cli1, 0)) {
2514 return false;
2517 cli_sockopt(cli1, sockops);
2519 status = cli_open(cli1, fname, O_RDWR, DENY_NONE,
2520 &fnum);
2521 if (!NT_STATUS_IS_OK(status)) {
2522 d_fprintf(stderr, "cli_open returned %s\n", cli_errstr(cli1));
2523 return false;
2526 /* Ensure the child has the lock. */
2527 if (cli_lock(cli1, fnum, 0, 4, 0, WRITE_LOCK)) {
2528 d_fprintf(stderr, "Got the lock on range 0:4 - this should not happen !\n");
2529 goto fail;
2530 } else {
2531 d_printf("Child has the lock.\n");
2534 /* Tell the child to wait 5 seconds then exit. */
2535 ret = write(pipe_out[1], &c, 1);
2536 if (ret != 1) {
2537 d_fprintf(stderr, "failed to send exit signal to child. %s\n",
2538 strerror(errno));
2539 goto fail;
2542 /* Wait 20 seconds for the lock. */
2543 alarm_fd = cli1->fd;
2544 CatchSignal(SIGALRM, alarm_handler_parent);
2545 alarm(20);
2547 start = timeval_current();
2549 if (!cli_lock(cli1, fnum, 0, 4, -1, WRITE_LOCK)) {
2550 d_fprintf(stderr, "Unable to apply write lock on range 0:4, error was "
2551 "%s\n", cli_errstr(cli1));
2552 goto fail_nofd;
2554 alarm(0);
2556 seconds = timeval_elapsed(&start);
2558 printf("Parent got the lock after %.2f seconds.\n",
2559 seconds);
2561 status = cli_close(cli1, fnum);
2562 if (!NT_STATUS_IS_OK(status)) {
2563 d_fprintf(stderr, "cli_close(fnum1) %s\n", cli_errstr(cli1));
2564 goto fail;
2567 correct = true;
2569 fail:
2570 cli_close(cli1, fnum);
2571 torture_close_connection(cli1);
2573 fail_nofd:
2575 printf("finished locktest9\n");
2576 return correct;
2580 test whether fnums and tids open on one VC are available on another (a major
2581 security hole)
2583 static bool run_fdpasstest(int dummy)
2585 struct cli_state *cli1, *cli2;
2586 const char *fname = "\\fdpass.tst";
2587 uint16_t fnum1;
2588 char buf[1024];
2589 NTSTATUS status;
2591 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
2592 return False;
2594 cli_sockopt(cli1, sockops);
2595 cli_sockopt(cli2, sockops);
2597 printf("starting fdpasstest\n");
2599 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2601 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
2602 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2603 return False;
2606 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)"hello world\n", 0,
2607 13, NULL);
2608 if (!NT_STATUS_IS_OK(status)) {
2609 printf("write failed (%s)\n", nt_errstr(status));
2610 return False;
2613 cli2->vuid = cli1->vuid;
2614 cli2->cnum = cli1->cnum;
2615 cli2->pid = cli1->pid;
2617 if (cli_read(cli2, fnum1, buf, 0, 13) == 13) {
2618 printf("read succeeded! nasty security hole [%s]\n",
2619 buf);
2620 return False;
2623 cli_close(cli1, fnum1);
2624 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2626 torture_close_connection(cli1);
2627 torture_close_connection(cli2);
2629 printf("finished fdpasstest\n");
2630 return True;
2633 static bool run_fdsesstest(int dummy)
2635 struct cli_state *cli;
2636 uint16 new_vuid;
2637 uint16 saved_vuid;
2638 uint16 new_cnum;
2639 uint16 saved_cnum;
2640 const char *fname = "\\fdsess.tst";
2641 const char *fname1 = "\\fdsess1.tst";
2642 uint16_t fnum1;
2643 uint16_t fnum2;
2644 char buf[1024];
2645 bool ret = True;
2646 NTSTATUS status;
2648 if (!torture_open_connection(&cli, 0))
2649 return False;
2650 cli_sockopt(cli, sockops);
2652 if (!torture_cli_session_setup2(cli, &new_vuid))
2653 return False;
2655 saved_cnum = cli->cnum;
2656 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, share, "?????", "", 1)))
2657 return False;
2658 new_cnum = cli->cnum;
2659 cli->cnum = saved_cnum;
2661 printf("starting fdsesstest\n");
2663 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2664 cli_unlink(cli, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2666 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
2667 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2668 return False;
2671 status = cli_writeall(cli, fnum1, 0, (uint8_t *)"hello world\n", 0, 13,
2672 NULL);
2673 if (!NT_STATUS_IS_OK(status)) {
2674 printf("write failed (%s)\n", nt_errstr(status));
2675 return False;
2678 saved_vuid = cli->vuid;
2679 cli->vuid = new_vuid;
2681 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2682 printf("read succeeded with different vuid! nasty security hole [%s]\n",
2683 buf);
2684 ret = False;
2686 /* Try to open a file with different vuid, samba cnum. */
2687 if (NT_STATUS_IS_OK(cli_open(cli, fname1, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum2))) {
2688 printf("create with different vuid, same cnum succeeded.\n");
2689 cli_close(cli, fnum2);
2690 cli_unlink(cli, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2691 } else {
2692 printf("create with different vuid, same cnum failed.\n");
2693 printf("This will cause problems with service clients.\n");
2694 ret = False;
2697 cli->vuid = saved_vuid;
2699 /* Try with same vuid, different cnum. */
2700 cli->cnum = new_cnum;
2702 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2703 printf("read succeeded with different cnum![%s]\n",
2704 buf);
2705 ret = False;
2708 cli->cnum = saved_cnum;
2709 cli_close(cli, fnum1);
2710 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2712 torture_close_connection(cli);
2714 printf("finished fdsesstest\n");
2715 return ret;
2719 This test checks that
2721 1) the server does not allow an unlink on a file that is open
2723 static bool run_unlinktest(int dummy)
2725 struct cli_state *cli;
2726 const char *fname = "\\unlink.tst";
2727 uint16_t fnum;
2728 bool correct = True;
2730 if (!torture_open_connection(&cli, 0)) {
2731 return False;
2734 cli_sockopt(cli, sockops);
2736 printf("starting unlink test\n");
2738 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2740 cli_setpid(cli, 1);
2742 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
2743 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2744 return False;
2747 if (NT_STATUS_IS_OK(cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
2748 printf("error: server allowed unlink on an open file\n");
2749 correct = False;
2750 } else {
2751 correct = check_error(__LINE__, cli, ERRDOS, ERRbadshare,
2752 NT_STATUS_SHARING_VIOLATION);
2755 cli_close(cli, fnum);
2756 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2758 if (!torture_close_connection(cli)) {
2759 correct = False;
2762 printf("unlink test finished\n");
2764 return correct;
2769 test how many open files this server supports on the one socket
2771 static bool run_maxfidtest(int dummy)
2773 struct cli_state *cli;
2774 const char *ftemplate = "\\maxfid.%d.%d";
2775 fstring fname;
2776 uint16_t fnums[0x11000];
2777 int i;
2778 int retries=4;
2779 bool correct = True;
2781 cli = current_cli;
2783 if (retries <= 0) {
2784 printf("failed to connect\n");
2785 return False;
2788 cli_sockopt(cli, sockops);
2790 for (i=0; i<0x11000; i++) {
2791 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2792 if (!NT_STATUS_IS_OK(cli_open(cli, fname,
2793 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnums[i]))) {
2794 printf("open of %s failed (%s)\n",
2795 fname, cli_errstr(cli));
2796 printf("maximum fnum is %d\n", i);
2797 break;
2799 printf("%6d\r", i);
2801 printf("%6d\n", i);
2802 i--;
2804 printf("cleaning up\n");
2805 for (;i>=0;i--) {
2806 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2807 cli_close(cli, fnums[i]);
2808 if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
2809 printf("unlink of %s failed (%s)\n",
2810 fname, cli_errstr(cli));
2811 correct = False;
2813 printf("%6d\r", i);
2815 printf("%6d\n", 0);
2817 printf("maxfid test finished\n");
2818 if (!torture_close_connection(cli)) {
2819 correct = False;
2821 return correct;
2824 /* generate a random buffer */
2825 static void rand_buf(char *buf, int len)
2827 while (len--) {
2828 *buf = (char)sys_random();
2829 buf++;
2833 /* send smb negprot commands, not reading the response */
2834 static bool run_negprot_nowait(int dummy)
2836 struct tevent_context *ev;
2837 int i;
2838 struct cli_state *cli;
2839 bool correct = True;
2841 printf("starting negprot nowait test\n");
2843 ev = tevent_context_init(talloc_tos());
2844 if (ev == NULL) {
2845 return false;
2848 if (!(cli = open_nbt_connection())) {
2849 TALLOC_FREE(ev);
2850 return False;
2853 for (i=0;i<50000;i++) {
2854 struct tevent_req *req;
2856 req = cli_negprot_send(ev, ev, cli);
2857 if (req == NULL) {
2858 TALLOC_FREE(ev);
2859 return false;
2861 if (!tevent_req_poll(req, ev)) {
2862 d_fprintf(stderr, "tevent_req_poll failed: %s\n",
2863 strerror(errno));
2864 TALLOC_FREE(ev);
2865 return false;
2867 TALLOC_FREE(req);
2870 if (torture_close_connection(cli)) {
2871 correct = False;
2874 printf("finished negprot nowait test\n");
2876 return correct;
2879 /* send smb negprot commands, not reading the response */
2880 static bool run_bad_nbt_session(int dummy)
2882 static struct cli_state *cli;
2884 printf("starting bad nbt session test\n");
2886 if (!(cli = open_bad_nbt_connection())) {
2887 return False;
2890 cli_shutdown(cli);
2891 printf("finished bad nbt session test\n");
2892 return true;
2895 /* send random IPC commands */
2896 static bool run_randomipc(int dummy)
2898 char *rparam = NULL;
2899 char *rdata = NULL;
2900 unsigned int rdrcnt,rprcnt;
2901 char param[1024];
2902 int api, param_len, i;
2903 struct cli_state *cli;
2904 bool correct = True;
2905 int count = 50000;
2907 printf("starting random ipc test\n");
2909 if (!torture_open_connection(&cli, 0)) {
2910 return False;
2913 for (i=0;i<count;i++) {
2914 api = sys_random() % 500;
2915 param_len = (sys_random() % 64);
2917 rand_buf(param, param_len);
2919 SSVAL(param,0,api);
2921 cli_api(cli,
2922 param, param_len, 8,
2923 NULL, 0, BUFFER_SIZE,
2924 &rparam, &rprcnt,
2925 &rdata, &rdrcnt);
2926 if (i % 100 == 0) {
2927 printf("%d/%d\r", i,count);
2930 printf("%d/%d\n", i, count);
2932 if (!torture_close_connection(cli)) {
2933 correct = False;
2936 printf("finished random ipc test\n");
2938 return correct;
2943 static void browse_callback(const char *sname, uint32 stype,
2944 const char *comment, void *state)
2946 printf("\t%20.20s %08x %s\n", sname, stype, comment);
2952 This test checks the browse list code
2955 static bool run_browsetest(int dummy)
2957 static struct cli_state *cli;
2958 bool correct = True;
2960 printf("starting browse test\n");
2962 if (!torture_open_connection(&cli, 0)) {
2963 return False;
2966 printf("domain list:\n");
2967 cli_NetServerEnum(cli, cli->server_domain,
2968 SV_TYPE_DOMAIN_ENUM,
2969 browse_callback, NULL);
2971 printf("machine list:\n");
2972 cli_NetServerEnum(cli, cli->server_domain,
2973 SV_TYPE_ALL,
2974 browse_callback, NULL);
2976 if (!torture_close_connection(cli)) {
2977 correct = False;
2980 printf("browse test finished\n");
2982 return correct;
2988 This checks how the getatr calls works
2990 static bool run_attrtest(int dummy)
2992 struct cli_state *cli;
2993 uint16_t fnum;
2994 time_t t, t2;
2995 const char *fname = "\\attrib123456789.tst";
2996 bool correct = True;
2998 printf("starting attrib test\n");
3000 if (!torture_open_connection(&cli, 0)) {
3001 return False;
3004 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3005 cli_open(cli, fname,
3006 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3007 cli_close(cli, fnum);
3008 if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
3009 printf("getatr failed (%s)\n", cli_errstr(cli));
3010 correct = False;
3013 if (abs(t - time(NULL)) > 60*60*24*10) {
3014 printf("ERROR: SMBgetatr bug. time is %s",
3015 ctime(&t));
3016 t = time(NULL);
3017 correct = True;
3020 t2 = t-60*60*24; /* 1 day ago */
3022 if (!NT_STATUS_IS_OK(cli_setatr(cli, fname, 0, t2))) {
3023 printf("setatr failed (%s)\n", cli_errstr(cli));
3024 correct = True;
3027 if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
3028 printf("getatr failed (%s)\n", cli_errstr(cli));
3029 correct = True;
3032 if (t != t2) {
3033 printf("ERROR: getatr/setatr bug. times are\n%s",
3034 ctime(&t));
3035 printf("%s", ctime(&t2));
3036 correct = True;
3039 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3041 if (!torture_close_connection(cli)) {
3042 correct = False;
3045 printf("attrib test finished\n");
3047 return correct;
3052 This checks a couple of trans2 calls
3054 static bool run_trans2test(int dummy)
3056 struct cli_state *cli;
3057 uint16_t fnum;
3058 SMB_OFF_T size;
3059 time_t c_time, a_time, m_time;
3060 struct timespec c_time_ts, a_time_ts, m_time_ts, w_time_ts, m_time2_ts;
3061 const char *fname = "\\trans2.tst";
3062 const char *dname = "\\trans2";
3063 const char *fname2 = "\\trans2\\trans2.tst";
3064 char pname[1024];
3065 bool correct = True;
3066 NTSTATUS status;
3067 uint32_t fs_attr;
3069 printf("starting trans2 test\n");
3071 if (!torture_open_connection(&cli, 0)) {
3072 return False;
3075 status = cli_get_fs_attr_info(cli, &fs_attr);
3076 if (!NT_STATUS_IS_OK(status)) {
3077 printf("ERROR: cli_get_fs_attr_info returned %s\n",
3078 nt_errstr(status));
3079 correct = false;
3082 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3083 cli_open(cli, fname,
3084 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3085 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
3086 cli, fnum, NULL, &size, &c_time_ts,
3087 &a_time_ts, &w_time_ts,
3088 &m_time_ts, NULL))) {
3089 printf("ERROR: qfileinfo failed (%s)\n", cli_errstr(cli));
3090 correct = False;
3093 if (!NT_STATUS_IS_OK(cli_qfilename(cli, fnum, pname, sizeof(pname)))) {
3094 printf("ERROR: qfilename failed (%s)\n", cli_errstr(cli));
3095 correct = False;
3098 if (strcmp(pname, fname)) {
3099 printf("qfilename gave different name? [%s] [%s]\n",
3100 fname, pname);
3101 correct = False;
3104 cli_close(cli, fnum);
3106 sleep(2);
3108 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3109 if (!NT_STATUS_IS_OK(cli_open(cli, fname,
3110 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum))) {
3111 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
3112 return False;
3114 cli_close(cli, fnum);
3116 status = cli_qpathinfo1(cli, fname, &c_time, &a_time, &m_time, &size,
3117 NULL);
3118 if (!NT_STATUS_IS_OK(status)) {
3119 printf("ERROR: qpathinfo failed (%s)\n", nt_errstr(status));
3120 correct = False;
3121 } else {
3122 if (c_time != m_time) {
3123 printf("create time=%s", ctime(&c_time));
3124 printf("modify time=%s", ctime(&m_time));
3125 printf("This system appears to have sticky create times\n");
3127 if (a_time % (60*60) == 0) {
3128 printf("access time=%s", ctime(&a_time));
3129 printf("This system appears to set a midnight access time\n");
3130 correct = False;
3133 if (abs(m_time - time(NULL)) > 60*60*24*7) {
3134 printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
3135 correct = False;
3140 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3141 cli_open(cli, fname,
3142 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3143 cli_close(cli, fnum);
3144 status = cli_qpathinfo2(cli, fname, &c_time_ts, &a_time_ts, &w_time_ts,
3145 &m_time_ts, &size, NULL, NULL);
3146 if (!NT_STATUS_IS_OK(status)) {
3147 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3148 correct = False;
3149 } else {
3150 if (w_time_ts.tv_sec < 60*60*24*2) {
3151 printf("write time=%s", ctime(&w_time_ts.tv_sec));
3152 printf("This system appears to set a initial 0 write time\n");
3153 correct = False;
3157 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3160 /* check if the server updates the directory modification time
3161 when creating a new file */
3162 if (!NT_STATUS_IS_OK(cli_mkdir(cli, dname))) {
3163 printf("ERROR: mkdir failed (%s)\n", cli_errstr(cli));
3164 correct = False;
3166 sleep(3);
3167 status = cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts,
3168 &w_time_ts, &m_time_ts, &size, NULL, NULL);
3169 if (!NT_STATUS_IS_OK(status)) {
3170 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3171 correct = False;
3174 cli_open(cli, fname2,
3175 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3176 cli_writeall(cli, fnum, 0, (uint8_t *)&fnum, 0, sizeof(fnum), NULL);
3177 cli_close(cli, fnum);
3178 status = cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts,
3179 &w_time_ts, &m_time2_ts, &size, NULL, NULL);
3180 if (!NT_STATUS_IS_OK(status)) {
3181 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3182 correct = False;
3183 } else {
3184 if (memcmp(&m_time_ts, &m_time2_ts, sizeof(struct timespec))
3185 == 0) {
3186 printf("This system does not update directory modification times\n");
3187 correct = False;
3190 cli_unlink(cli, fname2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3191 cli_rmdir(cli, dname);
3193 if (!torture_close_connection(cli)) {
3194 correct = False;
3197 printf("trans2 test finished\n");
3199 return correct;
3203 This checks new W2K calls.
3206 static NTSTATUS new_trans(struct cli_state *pcli, int fnum, int level)
3208 uint8_t *buf = NULL;
3209 uint32 len;
3210 NTSTATUS status;
3212 status = cli_qfileinfo(talloc_tos(), pcli, fnum, level, 0,
3213 pcli->max_xmit, &buf, &len);
3214 if (!NT_STATUS_IS_OK(status)) {
3215 printf("ERROR: qfileinfo (%d) failed (%s)\n", level,
3216 nt_errstr(status));
3217 } else {
3218 printf("qfileinfo: level %d, len = %u\n", level, len);
3219 dump_data(0, (uint8 *)buf, len);
3220 printf("\n");
3222 TALLOC_FREE(buf);
3223 return status;
3226 static bool run_w2ktest(int dummy)
3228 struct cli_state *cli;
3229 uint16_t fnum;
3230 const char *fname = "\\w2ktest\\w2k.tst";
3231 int level;
3232 bool correct = True;
3234 printf("starting w2k test\n");
3236 if (!torture_open_connection(&cli, 0)) {
3237 return False;
3240 cli_open(cli, fname,
3241 O_RDWR | O_CREAT , DENY_NONE, &fnum);
3243 for (level = 1004; level < 1040; level++) {
3244 new_trans(cli, fnum, level);
3247 cli_close(cli, fnum);
3249 if (!torture_close_connection(cli)) {
3250 correct = False;
3253 printf("w2k test finished\n");
3255 return correct;
3260 this is a harness for some oplock tests
3262 static bool run_oplock1(int dummy)
3264 struct cli_state *cli1;
3265 const char *fname = "\\lockt1.lck";
3266 uint16_t fnum1;
3267 bool correct = True;
3269 printf("starting oplock test 1\n");
3271 if (!torture_open_connection(&cli1, 0)) {
3272 return False;
3275 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3277 cli_sockopt(cli1, sockops);
3279 cli1->use_oplocks = True;
3281 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
3282 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3283 return False;
3286 cli1->use_oplocks = False;
3288 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3289 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3291 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3292 printf("close2 failed (%s)\n", cli_errstr(cli1));
3293 return False;
3296 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
3297 printf("unlink failed (%s)\n", cli_errstr(cli1));
3298 return False;
3301 if (!torture_close_connection(cli1)) {
3302 correct = False;
3305 printf("finished oplock test 1\n");
3307 return correct;
3310 static bool run_oplock2(int dummy)
3312 struct cli_state *cli1, *cli2;
3313 const char *fname = "\\lockt2.lck";
3314 uint16_t fnum1, fnum2;
3315 int saved_use_oplocks = use_oplocks;
3316 char buf[4];
3317 bool correct = True;
3318 volatile bool *shared_correct;
3320 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
3321 *shared_correct = True;
3323 use_level_II_oplocks = True;
3324 use_oplocks = True;
3326 printf("starting oplock test 2\n");
3328 if (!torture_open_connection(&cli1, 0)) {
3329 use_level_II_oplocks = False;
3330 use_oplocks = saved_use_oplocks;
3331 return False;
3334 cli1->use_oplocks = True;
3335 cli1->use_level_II_oplocks = True;
3337 if (!torture_open_connection(&cli2, 1)) {
3338 use_level_II_oplocks = False;
3339 use_oplocks = saved_use_oplocks;
3340 return False;
3343 cli2->use_oplocks = True;
3344 cli2->use_level_II_oplocks = True;
3346 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3348 cli_sockopt(cli1, sockops);
3349 cli_sockopt(cli2, sockops);
3351 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
3352 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3353 return False;
3356 /* Don't need the globals any more. */
3357 use_level_II_oplocks = False;
3358 use_oplocks = saved_use_oplocks;
3360 if (fork() == 0) {
3361 /* Child code */
3362 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2))) {
3363 printf("second open of %s failed (%s)\n", fname, cli_errstr(cli1));
3364 *shared_correct = False;
3365 exit(0);
3368 sleep(2);
3370 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
3371 printf("close2 failed (%s)\n", cli_errstr(cli1));
3372 *shared_correct = False;
3375 exit(0);
3378 sleep(2);
3380 /* Ensure cli1 processes the break. Empty file should always return 0
3381 * bytes. */
3383 if (cli_read(cli1, fnum1, buf, 0, 4) != 0) {
3384 printf("read on fnum1 failed (%s)\n", cli_errstr(cli1));
3385 correct = False;
3388 /* Should now be at level II. */
3389 /* Test if sending a write locks causes a break to none. */
3391 if (!cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK)) {
3392 printf("lock failed (%s)\n", cli_errstr(cli1));
3393 correct = False;
3396 cli_unlock(cli1, fnum1, 0, 4);
3398 sleep(2);
3400 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
3401 printf("lock failed (%s)\n", cli_errstr(cli1));
3402 correct = False;
3405 cli_unlock(cli1, fnum1, 0, 4);
3407 sleep(2);
3409 cli_read(cli1, fnum1, buf, 0, 4);
3411 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3412 printf("close1 failed (%s)\n", cli_errstr(cli1));
3413 correct = False;
3416 sleep(4);
3418 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
3419 printf("unlink failed (%s)\n", cli_errstr(cli1));
3420 correct = False;
3423 if (!torture_close_connection(cli1)) {
3424 correct = False;
3427 if (!*shared_correct) {
3428 correct = False;
3431 printf("finished oplock test 2\n");
3433 return correct;
3436 /* handler for oplock 3 tests */
3437 static NTSTATUS oplock3_handler(struct cli_state *cli, uint16_t fnum, unsigned char level)
3439 printf("got oplock break fnum=%d level=%d\n",
3440 fnum, level);
3441 return cli_oplock_ack(cli, fnum, level);
3444 static bool run_oplock3(int dummy)
3446 struct cli_state *cli;
3447 const char *fname = "\\oplockt3.dat";
3448 uint16_t fnum;
3449 char buf[4] = "abcd";
3450 bool correct = True;
3451 volatile bool *shared_correct;
3453 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
3454 *shared_correct = True;
3456 printf("starting oplock test 3\n");
3458 if (fork() == 0) {
3459 /* Child code */
3460 use_oplocks = True;
3461 use_level_II_oplocks = True;
3462 if (!torture_open_connection(&cli, 0)) {
3463 *shared_correct = False;
3464 exit(0);
3466 sleep(2);
3467 /* try to trigger a oplock break in parent */
3468 cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum);
3469 cli_writeall(cli, fnum, 0, (uint8_t *)buf, 0, 4, NULL);
3470 exit(0);
3473 /* parent code */
3474 use_oplocks = True;
3475 use_level_II_oplocks = True;
3476 if (!torture_open_connection(&cli, 1)) { /* other is forked */
3477 return False;
3479 cli_oplock_handler(cli, oplock3_handler);
3480 cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
3481 cli_writeall(cli, fnum, 0, (uint8_t *)buf, 0, 4, NULL);
3482 cli_close(cli, fnum);
3483 cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum);
3484 cli->timeout = 20000;
3485 cli_receive_smb(cli);
3486 printf("finished oplock test 3\n");
3488 return (correct && *shared_correct);
3490 /* What are we looking for here? What's sucess and what's FAILURE? */
3493 /* handler for oplock 4 tests */
3494 bool *oplock4_shared_correct;
3496 static NTSTATUS oplock4_handler(struct cli_state *cli, uint16_t fnum, unsigned char level)
3498 printf("got oplock break fnum=%d level=%d\n",
3499 fnum, level);
3500 *oplock4_shared_correct = true;
3501 cli_oplock_ack(cli, fnum, level);
3502 return NT_STATUS_UNSUCCESSFUL; /* Cause cli_receive_smb to return. */
3505 static bool run_oplock4(int dummy)
3507 struct cli_state *cli1, *cli2;
3508 const char *fname = "\\lockt4.lck";
3509 const char *fname_ln = "\\lockt4_ln.lck";
3510 uint16_t fnum1, fnum2;
3511 int saved_use_oplocks = use_oplocks;
3512 NTSTATUS status;
3513 bool correct = true;
3515 oplock4_shared_correct = (bool *)shm_setup(sizeof(bool));
3516 *oplock4_shared_correct = false;
3518 printf("starting oplock test 4\n");
3520 if (!torture_open_connection(&cli1, 0)) {
3521 use_level_II_oplocks = false;
3522 use_oplocks = saved_use_oplocks;
3523 return false;
3526 if (!torture_open_connection(&cli2, 1)) {
3527 use_level_II_oplocks = false;
3528 use_oplocks = saved_use_oplocks;
3529 return false;
3532 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3533 cli_unlink(cli1, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3535 cli_sockopt(cli1, sockops);
3536 cli_sockopt(cli2, sockops);
3538 /* Create the file. */
3539 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
3540 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3541 return false;
3544 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3545 printf("close1 failed (%s)\n", cli_errstr(cli1));
3546 return false;
3549 /* Now create a hardlink. */
3550 if (!NT_STATUS_IS_OK(cli_nt_hardlink(cli1, fname, fname_ln))) {
3551 printf("nt hardlink failed (%s)\n", cli_errstr(cli1));
3552 return false;
3555 /* Prove that opening hardlinks cause deny modes to conflict. */
3556 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum1))) {
3557 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3558 return false;
3561 status = cli_open(cli1, fname_ln, O_RDWR, DENY_NONE, &fnum2);
3562 if (NT_STATUS_IS_OK(status)) {
3563 printf("open of %s succeeded - should fail with sharing violation.\n",
3564 fname_ln);
3565 return false;
3568 if (!NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
3569 printf("open of %s should fail with sharing violation. Got %s\n",
3570 fname_ln, nt_errstr(status));
3571 return false;
3574 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3575 printf("close1 failed (%s)\n", cli_errstr(cli1));
3576 return false;
3579 cli1->use_oplocks = true;
3580 cli1->use_level_II_oplocks = true;
3582 cli2->use_oplocks = true;
3583 cli2->use_level_II_oplocks = true;
3585 cli_oplock_handler(cli1, oplock4_handler);
3586 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
3587 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3588 return false;
3591 if (fork() == 0) {
3592 /* Child code */
3593 if (!NT_STATUS_IS_OK(cli_open(cli2, fname_ln, O_RDWR, DENY_NONE, &fnum2))) {
3594 printf("open of %s failed (%s)\n", fname_ln, cli_errstr(cli1));
3595 *oplock4_shared_correct = false;
3596 exit(0);
3599 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
3600 printf("close2 failed (%s)\n", cli_errstr(cli1));
3601 *oplock4_shared_correct = false;
3604 exit(0);
3607 sleep(2);
3609 /* Process the oplock break. */
3610 cli_receive_smb(cli1);
3612 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3613 printf("close1 failed (%s)\n", cli_errstr(cli1));
3614 correct = false;
3617 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
3618 printf("unlink failed (%s)\n", cli_errstr(cli1));
3619 correct = false;
3621 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
3622 printf("unlink failed (%s)\n", cli_errstr(cli1));
3623 correct = false;
3626 if (!torture_close_connection(cli1)) {
3627 correct = false;
3630 if (!*oplock4_shared_correct) {
3631 correct = false;
3634 printf("finished oplock test 4\n");
3636 return correct;
3641 Test delete on close semantics.
3643 static bool run_deletetest(int dummy)
3645 struct cli_state *cli1 = NULL;
3646 struct cli_state *cli2 = NULL;
3647 const char *fname = "\\delete.file";
3648 uint16_t fnum1 = (uint16_t)-1;
3649 uint16_t fnum2 = (uint16_t)-1;
3650 bool correct = True;
3652 printf("starting delete test\n");
3654 if (!torture_open_connection(&cli1, 0)) {
3655 return False;
3658 cli_sockopt(cli1, sockops);
3660 /* Test 1 - this should delete the file on close. */
3662 cli_setatr(cli1, fname, 0, 0);
3663 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3665 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
3666 0, FILE_OVERWRITE_IF,
3667 FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3668 printf("[1] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3669 correct = False;
3670 goto fail;
3673 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3674 printf("[1] close failed (%s)\n", cli_errstr(cli1));
3675 correct = False;
3676 goto fail;
3679 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
3680 printf("[1] open of %s succeeded (should fail)\n", fname);
3681 correct = False;
3682 goto fail;
3685 printf("first delete on close test succeeded.\n");
3687 /* Test 2 - this should delete the file on close. */
3689 cli_setatr(cli1, fname, 0, 0);
3690 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3692 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
3693 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
3694 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3695 printf("[2] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3696 correct = False;
3697 goto fail;
3700 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3701 printf("[2] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3702 correct = False;
3703 goto fail;
3706 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3707 printf("[2] close failed (%s)\n", cli_errstr(cli1));
3708 correct = False;
3709 goto fail;
3712 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3713 printf("[2] open of %s succeeded should have been deleted on close !\n", fname);
3714 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3715 printf("[2] close failed (%s)\n", cli_errstr(cli1));
3716 correct = False;
3717 goto fail;
3719 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3720 } else
3721 printf("second delete on close test succeeded.\n");
3723 /* Test 3 - ... */
3724 cli_setatr(cli1, fname, 0, 0);
3725 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3727 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
3728 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3729 printf("[3] open - 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3730 correct = False;
3731 goto fail;
3734 /* This should fail with a sharing violation - open for delete is only compatible
3735 with SHARE_DELETE. */
3737 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3738 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0, 0, &fnum2))) {
3739 printf("[3] open - 2 of %s succeeded - should have failed.\n", fname);
3740 correct = False;
3741 goto fail;
3744 /* This should succeed. */
3746 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3747 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0, &fnum2))) {
3748 printf("[3] open - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3749 correct = False;
3750 goto fail;
3753 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3754 printf("[3] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3755 correct = False;
3756 goto fail;
3759 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3760 printf("[3] close 1 failed (%s)\n", cli_errstr(cli1));
3761 correct = False;
3762 goto fail;
3765 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
3766 printf("[3] close 2 failed (%s)\n", cli_errstr(cli1));
3767 correct = False;
3768 goto fail;
3771 /* This should fail - file should no longer be there. */
3773 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3774 printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
3775 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3776 printf("[3] close failed (%s)\n", cli_errstr(cli1));
3778 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3779 correct = False;
3780 goto fail;
3781 } else
3782 printf("third delete on close test succeeded.\n");
3784 /* Test 4 ... */
3785 cli_setatr(cli1, fname, 0, 0);
3786 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3788 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3789 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3790 printf("[4] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3791 correct = False;
3792 goto fail;
3795 /* This should succeed. */
3796 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3797 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0, &fnum2))) {
3798 printf("[4] open - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3799 correct = False;
3800 goto fail;
3803 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
3804 printf("[4] close - 1 failed (%s)\n", cli_errstr(cli1));
3805 correct = False;
3806 goto fail;
3809 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3810 printf("[4] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3811 correct = False;
3812 goto fail;
3815 /* This should fail - no more opens once delete on close set. */
3816 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3817 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3818 FILE_OPEN, 0, 0, &fnum2))) {
3819 printf("[4] open - 3 of %s succeeded ! Should have failed.\n", fname );
3820 correct = False;
3821 goto fail;
3822 } else
3823 printf("fourth delete on close test succeeded.\n");
3825 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3826 printf("[4] close - 2 failed (%s)\n", cli_errstr(cli1));
3827 correct = False;
3828 goto fail;
3831 /* Test 5 ... */
3832 cli_setatr(cli1, fname, 0, 0);
3833 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3835 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum1))) {
3836 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3837 correct = False;
3838 goto fail;
3841 /* This should fail - only allowed on NT opens with DELETE access. */
3843 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3844 printf("[5] setting delete_on_close on OpenX file succeeded - should fail !\n");
3845 correct = False;
3846 goto fail;
3849 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3850 printf("[5] close - 2 failed (%s)\n", cli_errstr(cli1));
3851 correct = False;
3852 goto fail;
3855 printf("fifth delete on close test succeeded.\n");
3857 /* Test 6 ... */
3858 cli_setatr(cli1, fname, 0, 0);
3859 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3861 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
3862 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3863 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3864 printf("[6] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3865 correct = False;
3866 goto fail;
3869 /* This should fail - only allowed on NT opens with DELETE access. */
3871 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3872 printf("[6] setting delete_on_close on file with no delete access succeeded - should fail !\n");
3873 correct = False;
3874 goto fail;
3877 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3878 printf("[6] close - 2 failed (%s)\n", cli_errstr(cli1));
3879 correct = False;
3880 goto fail;
3883 printf("sixth delete on close test succeeded.\n");
3885 /* Test 7 ... */
3886 cli_setatr(cli1, fname, 0, 0);
3887 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3889 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3890 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3891 printf("[7] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3892 correct = False;
3893 goto fail;
3896 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3897 printf("[7] setting delete_on_close on file failed !\n");
3898 correct = False;
3899 goto fail;
3902 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, false))) {
3903 printf("[7] unsetting delete_on_close on file failed !\n");
3904 correct = False;
3905 goto fail;
3908 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3909 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3910 correct = False;
3911 goto fail;
3914 /* This next open should succeed - we reset the flag. */
3916 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3917 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3918 correct = False;
3919 goto fail;
3922 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3923 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3924 correct = False;
3925 goto fail;
3928 printf("seventh delete on close test succeeded.\n");
3930 /* Test 7 ... */
3931 cli_setatr(cli1, fname, 0, 0);
3932 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3934 if (!torture_open_connection(&cli2, 1)) {
3935 printf("[8] failed to open second connection.\n");
3936 correct = False;
3937 goto fail;
3940 cli_sockopt(cli1, sockops);
3942 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3943 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3944 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3945 printf("[8] open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3946 correct = False;
3947 goto fail;
3950 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3951 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3952 FILE_OPEN, 0, 0, &fnum2))) {
3953 printf("[8] open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3954 correct = False;
3955 goto fail;
3958 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3959 printf("[8] setting delete_on_close on file failed !\n");
3960 correct = False;
3961 goto fail;
3964 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3965 printf("[8] close - 1 failed (%s)\n", cli_errstr(cli1));
3966 correct = False;
3967 goto fail;
3970 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
3971 printf("[8] close - 2 failed (%s)\n", cli_errstr(cli2));
3972 correct = False;
3973 goto fail;
3976 /* This should fail.. */
3977 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3978 printf("[8] open of %s succeeded should have been deleted on close !\n", fname);
3979 goto fail;
3980 correct = False;
3981 } else
3982 printf("eighth delete on close test succeeded.\n");
3984 /* This should fail - we need to set DELETE_ACCESS. */
3985 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,FILE_READ_DATA|FILE_WRITE_DATA,
3986 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3987 printf("[9] open of %s succeeded should have failed!\n", fname);
3988 correct = False;
3989 goto fail;
3992 printf("ninth delete on close test succeeded.\n");
3994 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3995 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3996 printf("[10] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3997 correct = False;
3998 goto fail;
4001 /* This should delete the file. */
4002 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4003 printf("[10] close failed (%s)\n", cli_errstr(cli1));
4004 correct = False;
4005 goto fail;
4008 /* This should fail.. */
4009 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
4010 printf("[10] open of %s succeeded should have been deleted on close !\n", fname);
4011 goto fail;
4012 correct = False;
4013 } else
4014 printf("tenth delete on close test succeeded.\n");
4016 cli_setatr(cli1, fname, 0, 0);
4017 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4019 /* What error do we get when attempting to open a read-only file with
4020 delete access ? */
4022 /* Create a readonly file. */
4023 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
4024 FILE_ATTRIBUTE_READONLY, FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4025 printf("[11] open of %s failed (%s)\n", fname, cli_errstr(cli1));
4026 correct = False;
4027 goto fail;
4030 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4031 printf("[11] close failed (%s)\n", cli_errstr(cli1));
4032 correct = False;
4033 goto fail;
4036 /* Now try open for delete access. */
4037 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES|DELETE_ACCESS,
4038 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4039 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4040 printf("[11] open of %s succeeded should have been denied with ACCESS_DENIED!\n", fname);
4041 cli_close(cli1, fnum1);
4042 goto fail;
4043 correct = False;
4044 } else {
4045 NTSTATUS nterr = cli_nt_error(cli1);
4046 if (!NT_STATUS_EQUAL(nterr,NT_STATUS_ACCESS_DENIED)) {
4047 printf("[11] open of %s should have been denied with ACCESS_DENIED! Got error %s\n", fname, nt_errstr(nterr));
4048 goto fail;
4049 correct = False;
4050 } else {
4051 printf("eleventh delete on close test succeeded.\n");
4055 printf("finished delete test\n");
4057 fail:
4058 /* FIXME: This will crash if we aborted before cli2 got
4059 * intialized, because these functions don't handle
4060 * uninitialized connections. */
4062 if (fnum1 != (uint16_t)-1) cli_close(cli1, fnum1);
4063 if (fnum2 != (uint16_t)-1) cli_close(cli1, fnum2);
4064 cli_setatr(cli1, fname, 0, 0);
4065 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4067 if (cli1 && !torture_close_connection(cli1)) {
4068 correct = False;
4070 if (cli2 && !torture_close_connection(cli2)) {
4071 correct = False;
4073 return correct;
4076 static bool run_deletetest_ln(int dummy)
4078 struct cli_state *cli;
4079 const char *fname = "\\delete1";
4080 const char *fname_ln = "\\delete1_ln";
4081 uint16_t fnum;
4082 uint16_t fnum1;
4083 NTSTATUS status;
4084 bool correct = true;
4085 time_t t;
4087 printf("starting deletetest-ln\n");
4089 if (!torture_open_connection(&cli, 0)) {
4090 return false;
4093 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4094 cli_unlink(cli, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4096 cli_sockopt(cli, sockops);
4098 /* Create the file. */
4099 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
4100 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
4101 return false;
4104 if (!NT_STATUS_IS_OK(cli_close(cli, fnum))) {
4105 printf("close1 failed (%s)\n", cli_errstr(cli));
4106 return false;
4109 /* Now create a hardlink. */
4110 if (!NT_STATUS_IS_OK(cli_nt_hardlink(cli, fname, fname_ln))) {
4111 printf("nt hardlink failed (%s)\n", cli_errstr(cli));
4112 return false;
4115 /* Open the original file. */
4116 status = cli_ntcreate(cli, fname, 0, FILE_READ_DATA,
4117 FILE_ATTRIBUTE_NORMAL,
4118 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4119 FILE_OPEN_IF, 0, 0, &fnum);
4120 if (!NT_STATUS_IS_OK(status)) {
4121 printf("ntcreate of %s failed (%s)\n", fname, nt_errstr(status));
4122 return false;
4125 /* Unlink the hard link path. */
4126 status = cli_ntcreate(cli, fname_ln, 0, DELETE_ACCESS,
4127 FILE_ATTRIBUTE_NORMAL,
4128 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4129 FILE_OPEN_IF, 0, 0, &fnum1);
4130 if (!NT_STATUS_IS_OK(status)) {
4131 printf("ntcreate of %s failed (%s)\n", fname_ln, nt_errstr(status));
4132 return false;
4134 status = cli_nt_delete_on_close(cli, fnum1, true);
4135 if (!NT_STATUS_IS_OK(status)) {
4136 d_printf("(%s) failed to set delete_on_close %s: %s\n",
4137 __location__, fname_ln, nt_errstr(status));
4138 return false;
4141 status = cli_close(cli, fnum1);
4142 if (!NT_STATUS_IS_OK(status)) {
4143 printf("close %s failed (%s)\n",
4144 fname_ln, nt_errstr(status));
4145 return false;
4148 status = cli_close(cli, fnum);
4149 if (!NT_STATUS_IS_OK(status)) {
4150 printf("close %s failed (%s)\n",
4151 fname, nt_errstr(status));
4152 return false;
4155 /* Ensure the original file is still there. */
4156 status = cli_getatr(cli, fname, NULL, NULL, &t);
4157 if (!NT_STATUS_IS_OK(status)) {
4158 printf("%s getatr on file %s failed (%s)\n",
4159 __location__,
4160 fname,
4161 nt_errstr(status));
4162 correct = False;
4165 /* Ensure the link path is gone. */
4166 status = cli_getatr(cli, fname_ln, NULL, NULL, &t);
4167 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
4168 printf("%s, getatr for file %s returned wrong error code %s "
4169 "- should have been deleted\n",
4170 __location__,
4171 fname_ln, nt_errstr(status));
4172 correct = False;
4175 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4176 cli_unlink(cli, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4178 if (!torture_close_connection(cli)) {
4179 correct = false;
4182 printf("finished deletetest-ln\n");
4184 return correct;
4188 print out server properties
4190 static bool run_properties(int dummy)
4192 struct cli_state *cli;
4193 bool correct = True;
4195 printf("starting properties test\n");
4197 ZERO_STRUCT(cli);
4199 if (!torture_open_connection(&cli, 0)) {
4200 return False;
4203 cli_sockopt(cli, sockops);
4205 d_printf("Capabilities 0x%08x\n", cli->capabilities);
4207 if (!torture_close_connection(cli)) {
4208 correct = False;
4211 return correct;
4216 /* FIRST_DESIRED_ACCESS 0xf019f */
4217 #define FIRST_DESIRED_ACCESS FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|\
4218 FILE_READ_EA| /* 0xf */ \
4219 FILE_WRITE_EA|FILE_READ_ATTRIBUTES| /* 0x90 */ \
4220 FILE_WRITE_ATTRIBUTES| /* 0x100 */ \
4221 DELETE_ACCESS|READ_CONTROL_ACCESS|\
4222 WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS /* 0xf0000 */
4223 /* SECOND_DESIRED_ACCESS 0xe0080 */
4224 #define SECOND_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
4225 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
4226 WRITE_OWNER_ACCESS /* 0xe0000 */
4228 #if 0
4229 #define THIRD_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
4230 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
4231 FILE_READ_DATA|\
4232 WRITE_OWNER_ACCESS /* */
4233 #endif
4236 Test ntcreate calls made by xcopy
4238 static bool run_xcopy(int dummy)
4240 static struct cli_state *cli1;
4241 const char *fname = "\\test.txt";
4242 bool correct = True;
4243 uint16_t fnum1, fnum2;
4245 printf("starting xcopy test\n");
4247 if (!torture_open_connection(&cli1, 0)) {
4248 return False;
4251 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,
4252 FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
4253 FILE_SHARE_NONE, FILE_OVERWRITE_IF,
4254 0x4044, 0, &fnum1))) {
4255 printf("First open failed - %s\n", cli_errstr(cli1));
4256 return False;
4259 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,
4260 SECOND_DESIRED_ACCESS, 0,
4261 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN,
4262 0x200000, 0, &fnum2))) {
4263 printf("second open failed - %s\n", cli_errstr(cli1));
4264 return False;
4267 if (!torture_close_connection(cli1)) {
4268 correct = False;
4271 return correct;
4275 Test rename on files open with share delete and no share delete.
4277 static bool run_rename(int dummy)
4279 static struct cli_state *cli1;
4280 const char *fname = "\\test.txt";
4281 const char *fname1 = "\\test1.txt";
4282 bool correct = True;
4283 uint16_t fnum1;
4284 uint16_t attr;
4285 NTSTATUS status;
4287 printf("starting rename test\n");
4289 if (!torture_open_connection(&cli1, 0)) {
4290 return False;
4293 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4294 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4295 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4296 FILE_SHARE_READ, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4297 printf("First open failed - %s\n", cli_errstr(cli1));
4298 return False;
4301 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
4302 printf("First rename failed (SHARE_READ) (this is correct) - %s\n", cli_errstr(cli1));
4303 } else {
4304 printf("First rename succeeded (SHARE_READ) - this should have failed !\n");
4305 correct = False;
4308 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4309 printf("close - 1 failed (%s)\n", cli_errstr(cli1));
4310 return False;
4313 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4314 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4315 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4316 #if 0
4317 FILE_SHARE_DELETE|FILE_SHARE_NONE,
4318 #else
4319 FILE_SHARE_DELETE|FILE_SHARE_READ,
4320 #endif
4321 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4322 if (!NT_STATUS_IS_OK(status)) {
4323 printf("Second open failed - %s\n", cli_errstr(cli1));
4324 return False;
4327 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
4328 printf("Second rename failed (SHARE_DELETE | SHARE_READ) - this should have succeeded - %s\n", cli_errstr(cli1));
4329 correct = False;
4330 } else {
4331 printf("Second rename succeeded (SHARE_DELETE | SHARE_READ)\n");
4334 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4335 printf("close - 2 failed (%s)\n", cli_errstr(cli1));
4336 return False;
4339 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4340 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4342 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, READ_CONTROL_ACCESS, FILE_ATTRIBUTE_NORMAL,
4343 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4344 printf("Third open failed - %s\n", cli_errstr(cli1));
4345 return False;
4349 #if 0
4351 uint16_t fnum2;
4353 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
4354 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
4355 printf("Fourth open failed - %s\n", cli_errstr(cli1));
4356 return False;
4358 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum2, true))) {
4359 printf("[8] setting delete_on_close on file failed !\n");
4360 return False;
4363 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
4364 printf("close - 4 failed (%s)\n", cli_errstr(cli1));
4365 return False;
4368 #endif
4370 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
4371 printf("Third rename failed (SHARE_NONE) - this should have succeeded - %s\n", cli_errstr(cli1));
4372 correct = False;
4373 } else {
4374 printf("Third rename succeeded (SHARE_NONE)\n");
4377 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4378 printf("close - 3 failed (%s)\n", cli_errstr(cli1));
4379 return False;
4382 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4383 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4385 /*----*/
4387 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4388 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4389 printf("Fourth open failed - %s\n", cli_errstr(cli1));
4390 return False;
4393 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
4394 printf("Fourth rename failed (SHARE_READ | SHARE_WRITE) (this is correct) - %s\n", cli_errstr(cli1));
4395 } else {
4396 printf("Fourth rename succeeded (SHARE_READ | SHARE_WRITE) - this should have failed !\n");
4397 correct = False;
4400 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4401 printf("close - 4 failed (%s)\n", cli_errstr(cli1));
4402 return False;
4405 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4406 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4408 /*--*/
4410 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4411 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4412 printf("Fifth open failed - %s\n", cli_errstr(cli1));
4413 return False;
4416 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
4417 printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have succeeded - %s ! \n",
4418 cli_errstr(cli1));
4419 correct = False;
4420 } else {
4421 printf("Fifth rename succeeded (SHARE_READ | SHARE_WRITE | SHARE_DELETE) (this is correct) - %s\n", cli_errstr(cli1));
4425 * Now check if the first name still exists ...
4428 /* if (!NT_STATUS_OP(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4429 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
4430 printf("Opening original file after rename of open file fails: %s\n",
4431 cli_errstr(cli1));
4433 else {
4434 printf("Opening original file after rename of open file works ...\n");
4435 (void)cli_close(cli1, fnum2);
4436 } */
4438 /*--*/
4439 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4440 printf("close - 5 failed (%s)\n", cli_errstr(cli1));
4441 return False;
4444 /* Check that the renamed file has FILE_ATTRIBUTE_ARCHIVE. */
4445 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname1, &attr, NULL, NULL))) {
4446 printf("getatr on file %s failed - %s ! \n",
4447 fname1,
4448 cli_errstr(cli1));
4449 correct = False;
4450 } else {
4451 if (attr != FILE_ATTRIBUTE_ARCHIVE) {
4452 printf("Renamed file %s has wrong attr 0x%x "
4453 "(should be 0x%x)\n",
4454 fname1,
4455 attr,
4456 (unsigned int)FILE_ATTRIBUTE_ARCHIVE);
4457 correct = False;
4458 } else {
4459 printf("Renamed file %s has archive bit set\n", fname1);
4463 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4464 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4466 if (!torture_close_connection(cli1)) {
4467 correct = False;
4470 return correct;
4473 static bool run_pipe_number(int dummy)
4475 struct cli_state *cli1;
4476 const char *pipe_name = "\\SPOOLSS";
4477 uint16_t fnum;
4478 int num_pipes = 0;
4480 printf("starting pipenumber test\n");
4481 if (!torture_open_connection(&cli1, 0)) {
4482 return False;
4485 cli_sockopt(cli1, sockops);
4486 while(1) {
4487 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, pipe_name, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
4488 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF, 0, 0, &fnum))) {
4489 printf("Open of pipe %s failed with error (%s)\n", pipe_name, cli_errstr(cli1));
4490 break;
4492 num_pipes++;
4493 printf("\r%6d", num_pipes);
4496 printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
4497 torture_close_connection(cli1);
4498 return True;
4502 Test open mode returns on read-only files.
4504 static bool run_opentest(int dummy)
4506 static struct cli_state *cli1;
4507 static struct cli_state *cli2;
4508 const char *fname = "\\readonly.file";
4509 uint16_t fnum1, fnum2;
4510 char buf[20];
4511 SMB_OFF_T fsize;
4512 bool correct = True;
4513 char *tmp_path;
4514 NTSTATUS status;
4516 printf("starting open test\n");
4518 if (!torture_open_connection(&cli1, 0)) {
4519 return False;
4522 cli_setatr(cli1, fname, 0, 0);
4523 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4525 cli_sockopt(cli1, sockops);
4527 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
4528 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4529 return False;
4532 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4533 printf("close2 failed (%s)\n", cli_errstr(cli1));
4534 return False;
4537 if (!NT_STATUS_IS_OK(cli_setatr(cli1, fname, FILE_ATTRIBUTE_READONLY, 0))) {
4538 printf("cli_setatr failed (%s)\n", cli_errstr(cli1));
4539 return False;
4542 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1))) {
4543 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4544 return False;
4547 /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
4548 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4550 if (check_error(__LINE__, cli1, ERRDOS, ERRnoaccess,
4551 NT_STATUS_ACCESS_DENIED)) {
4552 printf("correct error code ERRDOS/ERRnoaccess returned\n");
4555 printf("finished open test 1\n");
4557 cli_close(cli1, fnum1);
4559 /* Now try not readonly and ensure ERRbadshare is returned. */
4561 cli_setatr(cli1, fname, 0, 0);
4563 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1))) {
4564 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4565 return False;
4568 /* This will fail - but the error should be ERRshare. */
4569 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4571 if (check_error(__LINE__, cli1, ERRDOS, ERRbadshare,
4572 NT_STATUS_SHARING_VIOLATION)) {
4573 printf("correct error code ERRDOS/ERRbadshare returned\n");
4576 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4577 printf("close2 failed (%s)\n", cli_errstr(cli1));
4578 return False;
4581 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4583 printf("finished open test 2\n");
4585 /* Test truncate open disposition on file opened for read. */
4587 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
4588 printf("(3) open (1) of %s failed (%s)\n", fname, cli_errstr(cli1));
4589 return False;
4592 /* write 20 bytes. */
4594 memset(buf, '\0', 20);
4596 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, 20, NULL);
4597 if (!NT_STATUS_IS_OK(status)) {
4598 printf("write failed (%s)\n", nt_errstr(status));
4599 correct = False;
4602 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4603 printf("(3) close1 failed (%s)\n", cli_errstr(cli1));
4604 return False;
4607 /* Ensure size == 20. */
4608 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
4609 printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
4610 return False;
4613 if (fsize != 20) {
4614 printf("(3) file size != 20\n");
4615 return False;
4618 /* Now test if we can truncate a file opened for readonly. */
4620 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY|O_TRUNC, DENY_NONE, &fnum1))) {
4621 printf("(3) open (2) of %s failed (%s)\n", fname, cli_errstr(cli1));
4622 return False;
4625 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4626 printf("close2 failed (%s)\n", cli_errstr(cli1));
4627 return False;
4630 /* Ensure size == 0. */
4631 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
4632 printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
4633 return False;
4636 if (fsize != 0) {
4637 printf("(3) file size != 0\n");
4638 return False;
4640 printf("finished open test 3\n");
4642 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4644 printf("Do ctemp tests\n");
4645 if (!NT_STATUS_IS_OK(cli_ctemp(cli1, talloc_tos(), "\\", &fnum1, &tmp_path))) {
4646 printf("ctemp failed (%s)\n", cli_errstr(cli1));
4647 return False;
4649 printf("ctemp gave path %s\n", tmp_path);
4650 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4651 printf("close of temp failed (%s)\n", cli_errstr(cli1));
4653 if (!NT_STATUS_IS_OK(cli_unlink(cli1, tmp_path, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
4654 printf("unlink of temp failed (%s)\n", cli_errstr(cli1));
4657 /* Test the non-io opens... */
4659 if (!torture_open_connection(&cli2, 1)) {
4660 return False;
4663 cli_setatr(cli2, fname, 0, 0);
4664 cli_unlink(cli2, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4666 cli_sockopt(cli2, sockops);
4668 printf("TEST #1 testing 2 non-io opens (no delete)\n");
4670 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4671 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4672 printf("TEST #1 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4673 return False;
4676 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4677 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4678 printf("TEST #1 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4679 return False;
4682 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4683 printf("TEST #1 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4684 return False;
4686 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4687 printf("TEST #1 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4688 return False;
4691 printf("non-io open test #1 passed.\n");
4693 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4695 printf("TEST #2 testing 2 non-io opens (first with delete)\n");
4697 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4698 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4699 printf("TEST #2 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4700 return False;
4703 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4704 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4705 printf("TEST #2 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4706 return False;
4709 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4710 printf("TEST #2 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4711 return False;
4713 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4714 printf("TEST #2 close 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
4715 return False;
4718 printf("non-io open test #2 passed.\n");
4720 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4722 printf("TEST #3 testing 2 non-io opens (second with delete)\n");
4724 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4725 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4726 printf("TEST #3 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4727 return False;
4730 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4731 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4732 printf("TEST #3 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4733 return False;
4736 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4737 printf("TEST #3 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4738 return False;
4740 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4741 printf("TEST #3 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4742 return False;
4745 printf("non-io open test #3 passed.\n");
4747 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4749 printf("TEST #4 testing 2 non-io opens (both with delete)\n");
4751 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4752 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4753 printf("TEST #4 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4754 return False;
4757 if (NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4758 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4759 printf("TEST #4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
4760 return False;
4763 printf("TEST #4 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
4765 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4766 printf("TEST #4 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4767 return False;
4770 printf("non-io open test #4 passed.\n");
4772 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4774 printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
4776 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4777 FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4778 printf("TEST #5 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4779 return False;
4782 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4783 FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4784 printf("TEST #5 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4785 return False;
4788 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4789 printf("TEST #5 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4790 return False;
4793 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4794 printf("TEST #5 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4795 return False;
4798 printf("non-io open test #5 passed.\n");
4800 printf("TEST #6 testing 1 non-io open, one io open\n");
4802 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4804 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
4805 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4806 printf("TEST #6 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4807 return False;
4810 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4811 FILE_SHARE_READ, FILE_OPEN_IF, 0, 0, &fnum2))) {
4812 printf("TEST #6 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4813 return False;
4816 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4817 printf("TEST #6 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4818 return False;
4821 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4822 printf("TEST #6 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4823 return False;
4826 printf("non-io open test #6 passed.\n");
4828 printf("TEST #7 testing 1 non-io open, one io open with delete\n");
4830 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4832 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
4833 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4834 printf("TEST #7 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4835 return False;
4838 if (NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4839 FILE_SHARE_READ|FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4840 printf("TEST #7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
4841 return False;
4844 printf("TEST #7 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
4846 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4847 printf("TEST #7 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4848 return False;
4851 printf("non-io open test #7 passed.\n");
4853 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4855 printf("TEST #8 testing open without WRITE_ATTRIBUTES, updating close write time.\n");
4856 status = cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL,
4857 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4858 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4859 if (!NT_STATUS_IS_OK(status)) {
4860 printf("TEST #8 open of %s failed (%s)\n", fname, nt_errstr(status));
4861 correct = false;
4862 goto out;
4865 /* Write to ensure we have to update the file time. */
4866 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)"TEST DATA\n", 0, 10,
4867 NULL);
4868 if (!NT_STATUS_IS_OK(status)) {
4869 printf("TEST #8 cli_write failed: %s\n", nt_errstr(status));
4870 correct = false;
4871 goto out;
4874 status = cli_close(cli1, fnum1);
4875 if (!NT_STATUS_IS_OK(status)) {
4876 printf("TEST #8 close of %s failed (%s)\n", fname, nt_errstr(status));
4877 correct = false;
4880 out:
4882 if (!torture_close_connection(cli1)) {
4883 correct = False;
4885 if (!torture_close_connection(cli2)) {
4886 correct = False;
4889 return correct;
4892 NTSTATUS torture_setup_unix_extensions(struct cli_state *cli)
4894 uint16 major, minor;
4895 uint32 caplow, caphigh;
4896 NTSTATUS status;
4898 if (!SERVER_HAS_UNIX_CIFS(cli)) {
4899 printf("Server doesn't support UNIX CIFS extensions.\n");
4900 return NT_STATUS_NOT_SUPPORTED;
4903 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
4904 &caphigh);
4905 if (!NT_STATUS_IS_OK(status)) {
4906 printf("Server didn't return UNIX CIFS extensions: %s\n",
4907 nt_errstr(status));
4908 return status;
4911 status = cli_set_unix_extensions_capabilities(cli, major, minor,
4912 caplow, caphigh);
4913 if (!NT_STATUS_IS_OK(status)) {
4914 printf("Server doesn't support setting UNIX CIFS extensions: "
4915 "%s.\n", nt_errstr(status));
4916 return status;
4919 return NT_STATUS_OK;
4923 Test POSIX open /mkdir calls.
4925 static bool run_simple_posix_open_test(int dummy)
4927 static struct cli_state *cli1;
4928 const char *fname = "posix:file";
4929 const char *hname = "posix:hlink";
4930 const char *sname = "posix:symlink";
4931 const char *dname = "posix:dir";
4932 char buf[10];
4933 char namebuf[11];
4934 uint16_t fnum1 = (uint16_t)-1;
4935 SMB_STRUCT_STAT sbuf;
4936 bool correct = false;
4937 NTSTATUS status;
4939 printf("Starting simple POSIX open test\n");
4941 if (!torture_open_connection(&cli1, 0)) {
4942 return false;
4945 cli_sockopt(cli1, sockops);
4947 status = torture_setup_unix_extensions(cli1);
4948 if (!NT_STATUS_IS_OK(status)) {
4949 return false;
4952 cli_setatr(cli1, fname, 0, 0);
4953 cli_posix_unlink(cli1, fname);
4954 cli_setatr(cli1, dname, 0, 0);
4955 cli_posix_rmdir(cli1, dname);
4956 cli_setatr(cli1, hname, 0, 0);
4957 cli_posix_unlink(cli1, hname);
4958 cli_setatr(cli1, sname, 0, 0);
4959 cli_posix_unlink(cli1, sname);
4961 /* Create a directory. */
4962 if (!NT_STATUS_IS_OK(cli_posix_mkdir(cli1, dname, 0777))) {
4963 printf("POSIX mkdir of %s failed (%s)\n", dname, cli_errstr(cli1));
4964 goto out;
4967 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1))) {
4968 printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
4969 goto out;
4972 /* Test ftruncate - set file size. */
4973 if (!NT_STATUS_IS_OK(cli_ftruncate(cli1, fnum1, 1000))) {
4974 printf("ftruncate failed (%s)\n", cli_errstr(cli1));
4975 goto out;
4978 /* Ensure st_size == 1000 */
4979 if (!NT_STATUS_IS_OK(cli_posix_stat(cli1, fname, &sbuf))) {
4980 printf("stat failed (%s)\n", cli_errstr(cli1));
4981 goto out;
4984 if (sbuf.st_ex_size != 1000) {
4985 printf("ftruncate - stat size (%u) != 1000\n", (unsigned int)sbuf.st_ex_size);
4986 goto out;
4989 /* Test ftruncate - set file size back to zero. */
4990 if (!NT_STATUS_IS_OK(cli_ftruncate(cli1, fnum1, 0))) {
4991 printf("ftruncate failed (%s)\n", cli_errstr(cli1));
4992 goto out;
4995 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4996 printf("close failed (%s)\n", cli_errstr(cli1));
4997 goto out;
5000 /* Now open the file again for read only. */
5001 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1))) {
5002 printf("POSIX open of %s failed (%s)\n", fname, cli_errstr(cli1));
5003 goto out;
5006 /* Now unlink while open. */
5007 if (!NT_STATUS_IS_OK(cli_posix_unlink(cli1, fname))) {
5008 printf("POSIX unlink of %s failed (%s)\n", fname, cli_errstr(cli1));
5009 goto out;
5012 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
5013 printf("close(2) failed (%s)\n", cli_errstr(cli1));
5014 goto out;
5017 /* Ensure the file has gone. */
5018 if (NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1))) {
5019 printf("POSIX open of %s succeeded, should have been deleted.\n", fname);
5020 goto out;
5023 /* Create again to test open with O_TRUNC. */
5024 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1))) {
5025 printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
5026 goto out;
5029 /* Test ftruncate - set file size. */
5030 if (!NT_STATUS_IS_OK(cli_ftruncate(cli1, fnum1, 1000))) {
5031 printf("ftruncate failed (%s)\n", cli_errstr(cli1));
5032 goto out;
5035 /* Ensure st_size == 1000 */
5036 if (!NT_STATUS_IS_OK(cli_posix_stat(cli1, fname, &sbuf))) {
5037 printf("stat failed (%s)\n", cli_errstr(cli1));
5038 goto out;
5041 if (sbuf.st_ex_size != 1000) {
5042 printf("ftruncate - stat size (%u) != 1000\n", (unsigned int)sbuf.st_ex_size);
5043 goto out;
5046 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
5047 printf("close(2) failed (%s)\n", cli_errstr(cli1));
5048 goto out;
5051 /* Re-open with O_TRUNC. */
5052 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_WRONLY|O_TRUNC, 0600, &fnum1))) {
5053 printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
5054 goto out;
5057 /* Ensure st_size == 0 */
5058 if (!NT_STATUS_IS_OK(cli_posix_stat(cli1, fname, &sbuf))) {
5059 printf("stat failed (%s)\n", cli_errstr(cli1));
5060 goto out;
5063 if (sbuf.st_ex_size != 0) {
5064 printf("O_TRUNC - stat size (%u) != 0\n", (unsigned int)sbuf.st_ex_size);
5065 goto out;
5068 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
5069 printf("close failed (%s)\n", cli_errstr(cli1));
5070 goto out;
5073 if (!NT_STATUS_IS_OK(cli_posix_unlink(cli1, fname))) {
5074 printf("POSIX unlink of %s failed (%s)\n", fname, cli_errstr(cli1));
5075 goto out;
5078 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, dname, O_RDONLY, 0, &fnum1))) {
5079 printf("POSIX open directory O_RDONLY of %s failed (%s)\n",
5080 dname, cli_errstr(cli1));
5081 goto out;
5084 cli_close(cli1, fnum1);
5086 /* What happens when we try and POSIX open a directory for write ? */
5087 if (NT_STATUS_IS_OK(cli_posix_open(cli1, dname, O_RDWR, 0, &fnum1))) {
5088 printf("POSIX open of directory %s succeeded, should have failed.\n", fname);
5089 goto out;
5090 } else {
5091 if (!check_error(__LINE__, cli1, ERRDOS, EISDIR,
5092 NT_STATUS_FILE_IS_A_DIRECTORY)) {
5093 goto out;
5097 /* Create the file. */
5098 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1))) {
5099 printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
5100 goto out;
5103 /* Write some data into it. */
5104 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)"TEST DATA\n", 0, 10,
5105 NULL);
5106 if (!NT_STATUS_IS_OK(status)) {
5107 printf("cli_write failed: %s\n", nt_errstr(status));
5108 goto out;
5111 cli_close(cli1, fnum1);
5113 /* Now create a hardlink. */
5114 if (!NT_STATUS_IS_OK(cli_posix_hardlink(cli1, fname, hname))) {
5115 printf("POSIX hardlink of %s failed (%s)\n", hname, cli_errstr(cli1));
5116 goto out;
5119 /* Now create a symlink. */
5120 if (!NT_STATUS_IS_OK(cli_posix_symlink(cli1, fname, sname))) {
5121 printf("POSIX symlink of %s failed (%s)\n", sname, cli_errstr(cli1));
5122 goto out;
5125 /* Open the hardlink for read. */
5126 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, hname, O_RDONLY, 0, &fnum1))) {
5127 printf("POSIX open of %s failed (%s)\n", hname, cli_errstr(cli1));
5128 goto out;
5131 if (cli_read(cli1, fnum1, buf, 0, 10) != 10) {
5132 printf("POSIX read of %s failed (%s)\n", hname, cli_errstr(cli1));
5133 goto out;
5136 if (memcmp(buf, "TEST DATA\n", 10)) {
5137 printf("invalid data read from hardlink\n");
5138 goto out;
5141 /* Do a POSIX lock/unlock. */
5142 if (!NT_STATUS_IS_OK(cli_posix_lock(cli1, fnum1, 0, 100, true, READ_LOCK))) {
5143 printf("POSIX lock failed %s\n", cli_errstr(cli1));
5144 goto out;
5147 /* Punch a hole in the locked area. */
5148 if (!NT_STATUS_IS_OK(cli_posix_unlock(cli1, fnum1, 10, 80))) {
5149 printf("POSIX unlock failed %s\n", cli_errstr(cli1));
5150 goto out;
5153 cli_close(cli1, fnum1);
5155 /* Open the symlink for read - this should fail. A POSIX
5156 client should not be doing opens on a symlink. */
5157 if (NT_STATUS_IS_OK(cli_posix_open(cli1, sname, O_RDONLY, 0, &fnum1))) {
5158 printf("POSIX open of %s succeeded (should have failed)\n", sname);
5159 goto out;
5160 } else {
5161 if (!check_error(__LINE__, cli1, ERRDOS, ERRbadpath,
5162 NT_STATUS_OBJECT_PATH_NOT_FOUND)) {
5163 printf("POSIX open of %s should have failed "
5164 "with NT_STATUS_OBJECT_PATH_NOT_FOUND, "
5165 "failed with %s instead.\n",
5166 sname, cli_errstr(cli1));
5167 goto out;
5171 if (!NT_STATUS_IS_OK(cli_posix_readlink(cli1, sname, namebuf, sizeof(namebuf)))) {
5172 printf("POSIX readlink on %s failed (%s)\n", sname, cli_errstr(cli1));
5173 goto out;
5176 if (strcmp(namebuf, fname) != 0) {
5177 printf("POSIX readlink on %s failed to match name %s (read %s)\n",
5178 sname, fname, namebuf);
5179 goto out;
5182 if (!NT_STATUS_IS_OK(cli_posix_rmdir(cli1, dname))) {
5183 printf("POSIX rmdir failed (%s)\n", cli_errstr(cli1));
5184 goto out;
5187 printf("Simple POSIX open test passed\n");
5188 correct = true;
5190 out:
5192 if (fnum1 != (uint16_t)-1) {
5193 cli_close(cli1, fnum1);
5194 fnum1 = (uint16_t)-1;
5197 cli_setatr(cli1, sname, 0, 0);
5198 cli_posix_unlink(cli1, sname);
5199 cli_setatr(cli1, hname, 0, 0);
5200 cli_posix_unlink(cli1, hname);
5201 cli_setatr(cli1, fname, 0, 0);
5202 cli_posix_unlink(cli1, fname);
5203 cli_setatr(cli1, dname, 0, 0);
5204 cli_posix_rmdir(cli1, dname);
5206 if (!torture_close_connection(cli1)) {
5207 correct = false;
5210 return correct;
5214 static uint32 open_attrs_table[] = {
5215 FILE_ATTRIBUTE_NORMAL,
5216 FILE_ATTRIBUTE_ARCHIVE,
5217 FILE_ATTRIBUTE_READONLY,
5218 FILE_ATTRIBUTE_HIDDEN,
5219 FILE_ATTRIBUTE_SYSTEM,
5221 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY,
5222 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN,
5223 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,
5224 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
5225 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
5226 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
5228 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
5229 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
5230 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
5231 FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_SYSTEM,
5234 struct trunc_open_results {
5235 unsigned int num;
5236 uint32 init_attr;
5237 uint32 trunc_attr;
5238 uint32 result_attr;
5241 static struct trunc_open_results attr_results[] = {
5242 { 0, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
5243 { 1, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
5244 { 2, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
5245 { 16, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
5246 { 17, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
5247 { 18, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
5248 { 51, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5249 { 54, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5250 { 56, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
5251 { 68, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5252 { 71, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5253 { 73, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
5254 { 99, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5255 { 102, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5256 { 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 },
5257 { 116, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5258 { 119, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5259 { 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 },
5260 { 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 },
5261 { 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 },
5262 { 227, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5263 { 230, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5264 { 232, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
5265 { 244, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5266 { 247, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5267 { 249, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM }
5270 static bool run_openattrtest(int dummy)
5272 static struct cli_state *cli1;
5273 const char *fname = "\\openattr.file";
5274 uint16_t fnum1;
5275 bool correct = True;
5276 uint16 attr;
5277 unsigned int i, j, k, l;
5279 printf("starting open attr test\n");
5281 if (!torture_open_connection(&cli1, 0)) {
5282 return False;
5285 cli_sockopt(cli1, sockops);
5287 for (k = 0, i = 0; i < sizeof(open_attrs_table)/sizeof(uint32); i++) {
5288 cli_setatr(cli1, fname, 0, 0);
5289 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5290 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA, open_attrs_table[i],
5291 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
5292 printf("open %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
5293 return False;
5296 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
5297 printf("close %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
5298 return False;
5301 for (j = 0; j < sizeof(open_attrs_table)/sizeof(uint32); j++) {
5302 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA, open_attrs_table[j],
5303 FILE_SHARE_NONE, FILE_OVERWRITE, 0, 0, &fnum1))) {
5304 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
5305 if (attr_results[l].num == k) {
5306 printf("[%d] trunc open 0x%x -> 0x%x of %s failed - should have succeeded !(0x%x:%s)\n",
5307 k, open_attrs_table[i],
5308 open_attrs_table[j],
5309 fname, NT_STATUS_V(cli_nt_error(cli1)), cli_errstr(cli1));
5310 correct = False;
5313 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
5314 printf("[%d] trunc open 0x%x -> 0x%x failed with wrong error code %s\n",
5315 k, open_attrs_table[i], open_attrs_table[j],
5316 cli_errstr(cli1));
5317 correct = False;
5319 #if 0
5320 printf("[%d] trunc open 0x%x -> 0x%x failed\n", k, open_attrs_table[i], open_attrs_table[j]);
5321 #endif
5322 k++;
5323 continue;
5326 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
5327 printf("close %d (2) of %s failed (%s)\n", j, fname, cli_errstr(cli1));
5328 return False;
5331 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, &attr, NULL, NULL))) {
5332 printf("getatr(2) failed (%s)\n", cli_errstr(cli1));
5333 return False;
5336 #if 0
5337 printf("[%d] getatr check [0x%x] trunc [0x%x] got attr 0x%x\n",
5338 k, open_attrs_table[i], open_attrs_table[j], attr );
5339 #endif
5341 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
5342 if (attr_results[l].num == k) {
5343 if (attr != attr_results[l].result_attr ||
5344 open_attrs_table[i] != attr_results[l].init_attr ||
5345 open_attrs_table[j] != attr_results[l].trunc_attr) {
5346 printf("getatr check failed. [0x%x] trunc [0x%x] got attr 0x%x, should be 0x%x\n",
5347 open_attrs_table[i],
5348 open_attrs_table[j],
5349 (unsigned int)attr,
5350 attr_results[l].result_attr);
5351 correct = False;
5353 break;
5356 k++;
5360 cli_setatr(cli1, fname, 0, 0);
5361 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5363 printf("open attr test %s.\n", correct ? "passed" : "failed");
5365 if (!torture_close_connection(cli1)) {
5366 correct = False;
5368 return correct;
5371 static NTSTATUS list_fn(const char *mnt, struct file_info *finfo,
5372 const char *name, void *state)
5374 int *matched = (int *)state;
5375 if (matched != NULL) {
5376 *matched += 1;
5378 return NT_STATUS_OK;
5382 test directory listing speed
5384 static bool run_dirtest(int dummy)
5386 int i;
5387 static struct cli_state *cli;
5388 uint16_t fnum;
5389 struct timeval core_start;
5390 bool correct = True;
5391 int matched;
5393 printf("starting directory test\n");
5395 if (!torture_open_connection(&cli, 0)) {
5396 return False;
5399 cli_sockopt(cli, sockops);
5401 srandom(0);
5402 for (i=0;i<torture_numops;i++) {
5403 fstring fname;
5404 slprintf(fname, sizeof(fname), "\\%x", (int)random());
5405 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum))) {
5406 fprintf(stderr,"Failed to open %s\n", fname);
5407 return False;
5409 cli_close(cli, fnum);
5412 core_start = timeval_current();
5414 matched = 0;
5415 cli_list(cli, "a*.*", 0, list_fn, &matched);
5416 printf("Matched %d\n", matched);
5418 matched = 0;
5419 cli_list(cli, "b*.*", 0, list_fn, &matched);
5420 printf("Matched %d\n", matched);
5422 matched = 0;
5423 cli_list(cli, "xyzabc", 0, list_fn, &matched);
5424 printf("Matched %d\n", matched);
5426 printf("dirtest core %g seconds\n", timeval_elapsed(&core_start));
5428 srandom(0);
5429 for (i=0;i<torture_numops;i++) {
5430 fstring fname;
5431 slprintf(fname, sizeof(fname), "\\%x", (int)random());
5432 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5435 if (!torture_close_connection(cli)) {
5436 correct = False;
5439 printf("finished dirtest\n");
5441 return correct;
5444 static NTSTATUS del_fn(const char *mnt, struct file_info *finfo, const char *mask,
5445 void *state)
5447 struct cli_state *pcli = (struct cli_state *)state;
5448 fstring fname;
5449 slprintf(fname, sizeof(fname), "\\LISTDIR\\%s", finfo->name);
5451 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
5452 return NT_STATUS_OK;
5454 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
5455 if (!NT_STATUS_IS_OK(cli_rmdir(pcli, fname)))
5456 printf("del_fn: failed to rmdir %s\n,", fname );
5457 } else {
5458 if (!NT_STATUS_IS_OK(cli_unlink(pcli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)))
5459 printf("del_fn: failed to unlink %s\n,", fname );
5461 return NT_STATUS_OK;
5466 sees what IOCTLs are supported
5468 bool torture_ioctl_test(int dummy)
5470 static struct cli_state *cli;
5471 uint16_t device, function;
5472 uint16_t fnum;
5473 const char *fname = "\\ioctl.dat";
5474 DATA_BLOB blob;
5475 NTSTATUS status;
5477 if (!torture_open_connection(&cli, 0)) {
5478 return False;
5481 printf("starting ioctl test\n");
5483 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5485 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
5486 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
5487 return False;
5490 status = cli_raw_ioctl(cli, fnum, 0x2d0000 | (0x0420<<2), &blob);
5491 printf("ioctl device info: %s\n", nt_errstr(status));
5493 status = cli_raw_ioctl(cli, fnum, IOCTL_QUERY_JOB_INFO, &blob);
5494 printf("ioctl job info: %s\n", nt_errstr(status));
5496 for (device=0;device<0x100;device++) {
5497 printf("ioctl test with device = 0x%x\n", device);
5498 for (function=0;function<0x100;function++) {
5499 uint32 code = (device<<16) | function;
5501 status = cli_raw_ioctl(cli, fnum, code, &blob);
5503 if (NT_STATUS_IS_OK(status)) {
5504 printf("ioctl 0x%x OK : %d bytes\n", (int)code,
5505 (int)blob.length);
5506 data_blob_free(&blob);
5511 if (!torture_close_connection(cli)) {
5512 return False;
5515 return True;
5520 tries varients of chkpath
5522 bool torture_chkpath_test(int dummy)
5524 static struct cli_state *cli;
5525 uint16_t fnum;
5526 bool ret;
5528 if (!torture_open_connection(&cli, 0)) {
5529 return False;
5532 printf("starting chkpath test\n");
5534 /* cleanup from an old run */
5535 cli_rmdir(cli, "\\chkpath.dir\\dir2");
5536 cli_unlink(cli, "\\chkpath.dir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5537 cli_rmdir(cli, "\\chkpath.dir");
5539 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\chkpath.dir"))) {
5540 printf("mkdir1 failed : %s\n", cli_errstr(cli));
5541 return False;
5544 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\chkpath.dir\\dir2"))) {
5545 printf("mkdir2 failed : %s\n", cli_errstr(cli));
5546 return False;
5549 if (!NT_STATUS_IS_OK(cli_open(cli, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
5550 printf("open1 failed (%s)\n", cli_errstr(cli));
5551 return False;
5553 cli_close(cli, fnum);
5555 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir"))) {
5556 printf("chkpath1 failed: %s\n", cli_errstr(cli));
5557 ret = False;
5560 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dir2"))) {
5561 printf("chkpath2 failed: %s\n", cli_errstr(cli));
5562 ret = False;
5565 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\foo.txt"))) {
5566 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
5567 NT_STATUS_NOT_A_DIRECTORY);
5568 } else {
5569 printf("* chkpath on a file should fail\n");
5570 ret = False;
5573 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\bar.txt"))) {
5574 ret = check_error(__LINE__, cli, ERRDOS, ERRbadfile,
5575 NT_STATUS_OBJECT_NAME_NOT_FOUND);
5576 } else {
5577 printf("* chkpath on a non existant file should fail\n");
5578 ret = False;
5581 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dirxx\\bar.txt"))) {
5582 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
5583 NT_STATUS_OBJECT_PATH_NOT_FOUND);
5584 } else {
5585 printf("* chkpath on a non existent component should fail\n");
5586 ret = False;
5589 cli_rmdir(cli, "\\chkpath.dir\\dir2");
5590 cli_unlink(cli, "\\chkpath.dir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5591 cli_rmdir(cli, "\\chkpath.dir");
5593 if (!torture_close_connection(cli)) {
5594 return False;
5597 return ret;
5600 static bool run_eatest(int dummy)
5602 static struct cli_state *cli;
5603 const char *fname = "\\eatest.txt";
5604 bool correct = True;
5605 uint16_t fnum;
5606 int i;
5607 size_t num_eas;
5608 struct ea_struct *ea_list = NULL;
5609 TALLOC_CTX *mem_ctx = talloc_init("eatest");
5610 NTSTATUS status;
5612 printf("starting eatest\n");
5614 if (!torture_open_connection(&cli, 0)) {
5615 talloc_destroy(mem_ctx);
5616 return False;
5619 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5620 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0,
5621 FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
5622 FILE_SHARE_NONE, FILE_OVERWRITE_IF,
5623 0x4044, 0, &fnum))) {
5624 printf("open failed - %s\n", cli_errstr(cli));
5625 talloc_destroy(mem_ctx);
5626 return False;
5629 for (i = 0; i < 10; i++) {
5630 fstring ea_name, ea_val;
5632 slprintf(ea_name, sizeof(ea_name), "EA_%d", i);
5633 memset(ea_val, (char)i+1, i+1);
5634 status = cli_set_ea_fnum(cli, fnum, ea_name, ea_val, i+1);
5635 if (!NT_STATUS_IS_OK(status)) {
5636 printf("ea_set of name %s failed - %s\n", ea_name,
5637 nt_errstr(status));
5638 talloc_destroy(mem_ctx);
5639 return False;
5643 cli_close(cli, fnum);
5644 for (i = 0; i < 10; i++) {
5645 fstring ea_name, ea_val;
5647 slprintf(ea_name, sizeof(ea_name), "EA_%d", i+10);
5648 memset(ea_val, (char)i+1, i+1);
5649 status = cli_set_ea_path(cli, fname, ea_name, ea_val, i+1);
5650 if (!NT_STATUS_IS_OK(status)) {
5651 printf("ea_set of name %s failed - %s\n", ea_name,
5652 nt_errstr(status));
5653 talloc_destroy(mem_ctx);
5654 return False;
5658 status = cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list);
5659 if (!NT_STATUS_IS_OK(status)) {
5660 printf("ea_get list failed - %s\n", nt_errstr(status));
5661 correct = False;
5664 printf("num_eas = %d\n", (int)num_eas);
5666 if (num_eas != 20) {
5667 printf("Should be 20 EA's stored... failing.\n");
5668 correct = False;
5671 for (i = 0; i < num_eas; i++) {
5672 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
5673 dump_data(0, ea_list[i].value.data,
5674 ea_list[i].value.length);
5677 /* Setting EA's to zero length deletes them. Test this */
5678 printf("Now deleting all EA's - case indepenent....\n");
5680 #if 1
5681 cli_set_ea_path(cli, fname, "", "", 0);
5682 #else
5683 for (i = 0; i < 20; i++) {
5684 fstring ea_name;
5685 slprintf(ea_name, sizeof(ea_name), "ea_%d", i);
5686 status = cli_set_ea_path(cli, fname, ea_name, "", 0);
5687 if (!NT_STATUS_IS_OK(status)) {
5688 printf("ea_set of name %s failed - %s\n", ea_name,
5689 nt_errstr(status));
5690 talloc_destroy(mem_ctx);
5691 return False;
5694 #endif
5696 status = cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list);
5697 if (!NT_STATUS_IS_OK(status)) {
5698 printf("ea_get list failed - %s\n", nt_errstr(status));
5699 correct = False;
5702 printf("num_eas = %d\n", (int)num_eas);
5703 for (i = 0; i < num_eas; i++) {
5704 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
5705 dump_data(0, ea_list[i].value.data,
5706 ea_list[i].value.length);
5709 if (num_eas != 0) {
5710 printf("deleting EA's failed.\n");
5711 correct = False;
5714 /* Try and delete a non existant EA. */
5715 status = cli_set_ea_path(cli, fname, "foo", "", 0);
5716 if (!NT_STATUS_IS_OK(status)) {
5717 printf("deleting non-existant EA 'foo' should succeed. %s\n",
5718 nt_errstr(status));
5719 correct = False;
5722 talloc_destroy(mem_ctx);
5723 if (!torture_close_connection(cli)) {
5724 correct = False;
5727 return correct;
5730 static bool run_dirtest1(int dummy)
5732 int i;
5733 static struct cli_state *cli;
5734 uint16_t fnum;
5735 int num_seen;
5736 bool correct = True;
5738 printf("starting directory test\n");
5740 if (!torture_open_connection(&cli, 0)) {
5741 return False;
5744 cli_sockopt(cli, sockops);
5746 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
5747 cli_list(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, del_fn, cli);
5748 cli_rmdir(cli, "\\LISTDIR");
5749 cli_mkdir(cli, "\\LISTDIR");
5751 /* Create 1000 files and 1000 directories. */
5752 for (i=0;i<1000;i++) {
5753 fstring fname;
5754 slprintf(fname, sizeof(fname), "\\LISTDIR\\f%d", i);
5755 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
5756 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
5757 fprintf(stderr,"Failed to open %s\n", fname);
5758 return False;
5760 cli_close(cli, fnum);
5762 for (i=0;i<1000;i++) {
5763 fstring fname;
5764 slprintf(fname, sizeof(fname), "\\LISTDIR\\d%d", i);
5765 if (!NT_STATUS_IS_OK(cli_mkdir(cli, fname))) {
5766 fprintf(stderr,"Failed to open %s\n", fname);
5767 return False;
5771 /* Now ensure that doing an old list sees both files and directories. */
5772 num_seen = 0;
5773 cli_list_old(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
5774 printf("num_seen = %d\n", num_seen );
5775 /* We should see 100 files + 1000 directories + . and .. */
5776 if (num_seen != 2002)
5777 correct = False;
5779 /* Ensure if we have the "must have" bits we only see the
5780 * relevent entries.
5782 num_seen = 0;
5783 cli_list_old(cli, "\\LISTDIR\\*", (FILE_ATTRIBUTE_DIRECTORY<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
5784 printf("num_seen = %d\n", num_seen );
5785 if (num_seen != 1002)
5786 correct = False;
5788 num_seen = 0;
5789 cli_list_old(cli, "\\LISTDIR\\*", (FILE_ATTRIBUTE_ARCHIVE<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
5790 printf("num_seen = %d\n", num_seen );
5791 if (num_seen != 1000)
5792 correct = False;
5794 /* Delete everything. */
5795 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
5796 cli_list(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, del_fn, cli);
5797 cli_rmdir(cli, "\\LISTDIR");
5799 #if 0
5800 printf("Matched %d\n", cli_list(cli, "a*.*", 0, list_fn, NULL));
5801 printf("Matched %d\n", cli_list(cli, "b*.*", 0, list_fn, NULL));
5802 printf("Matched %d\n", cli_list(cli, "xyzabc", 0, list_fn, NULL));
5803 #endif
5805 if (!torture_close_connection(cli)) {
5806 correct = False;
5809 printf("finished dirtest1\n");
5811 return correct;
5814 static bool run_error_map_extract(int dummy) {
5816 static struct cli_state *c_dos;
5817 static struct cli_state *c_nt;
5818 NTSTATUS status;
5820 uint32 error;
5822 uint32 flgs2, errnum;
5823 uint8 errclass;
5825 NTSTATUS nt_status;
5827 fstring user;
5829 /* NT-Error connection */
5831 if (!(c_nt = open_nbt_connection())) {
5832 return False;
5835 c_nt->use_spnego = False;
5837 status = cli_negprot(c_nt);
5839 if (!NT_STATUS_IS_OK(status)) {
5840 printf("%s rejected the NT-error negprot (%s)\n", host,
5841 nt_errstr(status));
5842 cli_shutdown(c_nt);
5843 return False;
5846 if (!NT_STATUS_IS_OK(cli_session_setup(c_nt, "", "", 0, "", 0,
5847 workgroup))) {
5848 printf("%s rejected the NT-error initial session setup (%s)\n",host, cli_errstr(c_nt));
5849 return False;
5852 /* DOS-Error connection */
5854 if (!(c_dos = open_nbt_connection())) {
5855 return False;
5858 c_dos->use_spnego = False;
5859 c_dos->force_dos_errors = True;
5861 status = cli_negprot(c_dos);
5862 if (!NT_STATUS_IS_OK(status)) {
5863 printf("%s rejected the DOS-error negprot (%s)\n", host,
5864 nt_errstr(status));
5865 cli_shutdown(c_dos);
5866 return False;
5869 if (!NT_STATUS_IS_OK(cli_session_setup(c_dos, "", "", 0, "", 0,
5870 workgroup))) {
5871 printf("%s rejected the DOS-error initial session setup (%s)\n",host, cli_errstr(c_dos));
5872 return False;
5875 for (error=(0xc0000000 | 0x1); error < (0xc0000000| 0xFFF); error++) {
5876 fstr_sprintf(user, "%X", error);
5878 if (NT_STATUS_IS_OK(cli_session_setup(c_nt, user,
5879 password, strlen(password),
5880 password, strlen(password),
5881 workgroup))) {
5882 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
5885 flgs2 = SVAL(c_nt->inbuf,smb_flg2);
5887 /* Case #1: 32-bit NT errors */
5888 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
5889 nt_status = NT_STATUS(IVAL(c_nt->inbuf,smb_rcls));
5890 } else {
5891 printf("/** Dos error on NT connection! (%s) */\n",
5892 cli_errstr(c_nt));
5893 nt_status = NT_STATUS(0xc0000000);
5896 if (NT_STATUS_IS_OK(cli_session_setup(c_dos, user,
5897 password, strlen(password),
5898 password, strlen(password),
5899 workgroup))) {
5900 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
5902 flgs2 = SVAL(c_dos->inbuf,smb_flg2), errnum;
5904 /* Case #1: 32-bit NT errors */
5905 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
5906 printf("/** NT error on DOS connection! (%s) */\n",
5907 cli_errstr(c_nt));
5908 errnum = errclass = 0;
5909 } else {
5910 cli_dos_error(c_dos, &errclass, &errnum);
5913 if (NT_STATUS_V(nt_status) != error) {
5914 printf("/*\t{ This NT error code was 'sqashed'\n\t from %s to %s \n\t during the session setup }\n*/\n",
5915 get_nt_error_c_code(NT_STATUS(error)),
5916 get_nt_error_c_code(nt_status));
5919 printf("\t{%s,\t%s,\t%s},\n",
5920 smb_dos_err_class(errclass),
5921 smb_dos_err_name(errclass, errnum),
5922 get_nt_error_c_code(NT_STATUS(error)));
5924 return True;
5927 static bool run_sesssetup_bench(int dummy)
5929 static struct cli_state *c;
5930 const char *fname = "\\file.dat";
5931 uint16_t fnum;
5932 NTSTATUS status;
5933 int i;
5935 if (!torture_open_connection(&c, 0)) {
5936 return false;
5939 if (!NT_STATUS_IS_OK(cli_ntcreate(
5940 c, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
5941 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
5942 FILE_DELETE_ON_CLOSE, 0, &fnum))) {
5943 d_printf("open %s failed: %s\n", fname, cli_errstr(c));
5944 return false;
5947 for (i=0; i<torture_numops; i++) {
5948 status = cli_session_setup(
5949 c, username,
5950 password, strlen(password),
5951 password, strlen(password),
5952 workgroup);
5953 if (!NT_STATUS_IS_OK(status)) {
5954 d_printf("(%s) cli_session_setup failed: %s\n",
5955 __location__, nt_errstr(status));
5956 return false;
5959 d_printf("\r%d ", (int)c->vuid);
5961 status = cli_ulogoff(c);
5962 if (!NT_STATUS_IS_OK(status)) {
5963 d_printf("(%s) cli_ulogoff failed: %s\n",
5964 __location__, nt_errstr(status));
5965 return false;
5967 c->vuid = 0;
5970 return true;
5973 static bool subst_test(const char *str, const char *user, const char *domain,
5974 uid_t uid, gid_t gid, const char *expected)
5976 char *subst;
5977 bool result = true;
5979 subst = talloc_sub_specified(talloc_tos(), str, user, domain, uid, gid);
5981 if (strcmp(subst, expected) != 0) {
5982 printf("sub_specified(%s, %s, %s, %d, %d) returned [%s], expected "
5983 "[%s]\n", str, user, domain, (int)uid, (int)gid, subst,
5984 expected);
5985 result = false;
5988 TALLOC_FREE(subst);
5989 return result;
5992 static void chain1_open_completion(struct tevent_req *req)
5994 uint16_t fnum;
5995 NTSTATUS status;
5996 status = cli_open_recv(req, &fnum);
5997 TALLOC_FREE(req);
5999 d_printf("cli_open_recv returned %s: %d\n",
6000 nt_errstr(status),
6001 NT_STATUS_IS_OK(status) ? fnum : -1);
6004 static void chain1_write_completion(struct tevent_req *req)
6006 size_t written;
6007 NTSTATUS status;
6008 status = cli_write_andx_recv(req, &written);
6009 TALLOC_FREE(req);
6011 d_printf("cli_write_andx_recv returned %s: %d\n",
6012 nt_errstr(status),
6013 NT_STATUS_IS_OK(status) ? (int)written : -1);
6016 static void chain1_close_completion(struct tevent_req *req)
6018 NTSTATUS status;
6019 bool *done = (bool *)tevent_req_callback_data_void(req);
6021 status = cli_close_recv(req);
6022 *done = true;
6024 TALLOC_FREE(req);
6026 d_printf("cli_close returned %s\n", nt_errstr(status));
6029 static bool run_chain1(int dummy)
6031 struct cli_state *cli1;
6032 struct event_context *evt = event_context_init(NULL);
6033 struct tevent_req *reqs[3], *smbreqs[3];
6034 bool done = false;
6035 const char *str = "foobar";
6036 NTSTATUS status;
6038 printf("starting chain1 test\n");
6039 if (!torture_open_connection(&cli1, 0)) {
6040 return False;
6043 cli_sockopt(cli1, sockops);
6045 reqs[0] = cli_open_create(talloc_tos(), evt, cli1, "\\test",
6046 O_CREAT|O_RDWR, 0, &smbreqs[0]);
6047 if (reqs[0] == NULL) return false;
6048 tevent_req_set_callback(reqs[0], chain1_open_completion, NULL);
6051 reqs[1] = cli_write_andx_create(talloc_tos(), evt, cli1, 0, 0,
6052 (uint8_t *)str, 0, strlen(str)+1,
6053 smbreqs, 1, &smbreqs[1]);
6054 if (reqs[1] == NULL) return false;
6055 tevent_req_set_callback(reqs[1], chain1_write_completion, NULL);
6057 reqs[2] = cli_close_create(talloc_tos(), evt, cli1, 0, &smbreqs[2]);
6058 if (reqs[2] == NULL) return false;
6059 tevent_req_set_callback(reqs[2], chain1_close_completion, &done);
6061 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
6062 if (!NT_STATUS_IS_OK(status)) {
6063 return false;
6066 while (!done) {
6067 event_loop_once(evt);
6070 torture_close_connection(cli1);
6071 return True;
6074 static void chain2_sesssetup_completion(struct tevent_req *req)
6076 NTSTATUS status;
6077 status = cli_session_setup_guest_recv(req);
6078 d_printf("sesssetup returned %s\n", nt_errstr(status));
6081 static void chain2_tcon_completion(struct tevent_req *req)
6083 bool *done = (bool *)tevent_req_callback_data_void(req);
6084 NTSTATUS status;
6085 status = cli_tcon_andx_recv(req);
6086 d_printf("tcon_and_x returned %s\n", nt_errstr(status));
6087 *done = true;
6090 static bool run_chain2(int dummy)
6092 struct cli_state *cli1;
6093 struct event_context *evt = event_context_init(NULL);
6094 struct tevent_req *reqs[2], *smbreqs[2];
6095 bool done = false;
6096 NTSTATUS status;
6098 printf("starting chain2 test\n");
6099 status = cli_start_connection(&cli1, global_myname(), host, NULL,
6100 port_to_use, Undefined, 0);
6101 if (!NT_STATUS_IS_OK(status)) {
6102 return False;
6105 cli_sockopt(cli1, sockops);
6107 reqs[0] = cli_session_setup_guest_create(talloc_tos(), evt, cli1,
6108 &smbreqs[0]);
6109 if (reqs[0] == NULL) return false;
6110 tevent_req_set_callback(reqs[0], chain2_sesssetup_completion, NULL);
6112 reqs[1] = cli_tcon_andx_create(talloc_tos(), evt, cli1, "IPC$",
6113 "?????", NULL, 0, &smbreqs[1]);
6114 if (reqs[1] == NULL) return false;
6115 tevent_req_set_callback(reqs[1], chain2_tcon_completion, &done);
6117 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
6118 if (!NT_STATUS_IS_OK(status)) {
6119 return false;
6122 while (!done) {
6123 event_loop_once(evt);
6126 torture_close_connection(cli1);
6127 return True;
6131 struct torture_createdel_state {
6132 struct tevent_context *ev;
6133 struct cli_state *cli;
6136 static void torture_createdel_created(struct tevent_req *subreq);
6137 static void torture_createdel_closed(struct tevent_req *subreq);
6139 static struct tevent_req *torture_createdel_send(TALLOC_CTX *mem_ctx,
6140 struct tevent_context *ev,
6141 struct cli_state *cli,
6142 const char *name)
6144 struct tevent_req *req, *subreq;
6145 struct torture_createdel_state *state;
6147 req = tevent_req_create(mem_ctx, &state,
6148 struct torture_createdel_state);
6149 if (req == NULL) {
6150 return NULL;
6152 state->ev = ev;
6153 state->cli = cli;
6155 subreq = cli_ntcreate_send(
6156 state, ev, cli, name, 0,
6157 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
6158 FILE_ATTRIBUTE_NORMAL,
6159 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
6160 FILE_OPEN_IF, FILE_DELETE_ON_CLOSE, 0);
6162 if (tevent_req_nomem(subreq, req)) {
6163 return tevent_req_post(req, ev);
6165 tevent_req_set_callback(subreq, torture_createdel_created, req);
6166 return req;
6169 static void torture_createdel_created(struct tevent_req *subreq)
6171 struct tevent_req *req = tevent_req_callback_data(
6172 subreq, struct tevent_req);
6173 struct torture_createdel_state *state = tevent_req_data(
6174 req, struct torture_createdel_state);
6175 NTSTATUS status;
6176 uint16_t fnum;
6178 status = cli_ntcreate_recv(subreq, &fnum);
6179 TALLOC_FREE(subreq);
6180 if (!NT_STATUS_IS_OK(status)) {
6181 DEBUG(10, ("cli_ntcreate_recv returned %s\n",
6182 nt_errstr(status)));
6183 tevent_req_nterror(req, status);
6184 return;
6187 subreq = cli_close_send(state, state->ev, state->cli, fnum);
6188 if (tevent_req_nomem(subreq, req)) {
6189 return;
6191 tevent_req_set_callback(subreq, torture_createdel_closed, req);
6194 static void torture_createdel_closed(struct tevent_req *subreq)
6196 struct tevent_req *req = tevent_req_callback_data(
6197 subreq, struct tevent_req);
6198 NTSTATUS status;
6200 status = cli_close_recv(subreq);
6201 if (!NT_STATUS_IS_OK(status)) {
6202 DEBUG(10, ("cli_close_recv returned %s\n", nt_errstr(status)));
6203 tevent_req_nterror(req, status);
6204 return;
6206 tevent_req_done(req);
6209 static NTSTATUS torture_createdel_recv(struct tevent_req *req)
6211 return tevent_req_simple_recv_ntstatus(req);
6214 struct torture_createdels_state {
6215 struct tevent_context *ev;
6216 struct cli_state *cli;
6217 const char *base_name;
6218 int sent;
6219 int received;
6220 int num_files;
6221 struct tevent_req **reqs;
6224 static void torture_createdels_done(struct tevent_req *subreq);
6226 static struct tevent_req *torture_createdels_send(TALLOC_CTX *mem_ctx,
6227 struct tevent_context *ev,
6228 struct cli_state *cli,
6229 const char *base_name,
6230 int num_parallel,
6231 int num_files)
6233 struct tevent_req *req;
6234 struct torture_createdels_state *state;
6235 int i;
6237 req = tevent_req_create(mem_ctx, &state,
6238 struct torture_createdels_state);
6239 if (req == NULL) {
6240 return NULL;
6242 state->ev = ev;
6243 state->cli = cli;
6244 state->base_name = talloc_strdup(state, base_name);
6245 if (tevent_req_nomem(state->base_name, req)) {
6246 return tevent_req_post(req, ev);
6248 state->num_files = MAX(num_parallel, num_files);
6249 state->sent = 0;
6250 state->received = 0;
6252 state->reqs = talloc_array(state, struct tevent_req *, num_parallel);
6253 if (tevent_req_nomem(state->reqs, req)) {
6254 return tevent_req_post(req, ev);
6257 for (i=0; i<num_parallel; i++) {
6258 char *name;
6260 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
6261 state->sent);
6262 if (tevent_req_nomem(name, req)) {
6263 return tevent_req_post(req, ev);
6265 state->reqs[i] = torture_createdel_send(
6266 state->reqs, state->ev, state->cli, name);
6267 if (tevent_req_nomem(state->reqs[i], req)) {
6268 return tevent_req_post(req, ev);
6270 name = talloc_move(state->reqs[i], &name);
6271 tevent_req_set_callback(state->reqs[i],
6272 torture_createdels_done, req);
6273 state->sent += 1;
6275 return req;
6278 static void torture_createdels_done(struct tevent_req *subreq)
6280 struct tevent_req *req = tevent_req_callback_data(
6281 subreq, struct tevent_req);
6282 struct torture_createdels_state *state = tevent_req_data(
6283 req, struct torture_createdels_state);
6284 size_t num_parallel = talloc_array_length(state->reqs);
6285 NTSTATUS status;
6286 char *name;
6287 int i;
6289 status = torture_createdel_recv(subreq);
6290 if (!NT_STATUS_IS_OK(status)){
6291 DEBUG(10, ("torture_createdel_recv returned %s\n",
6292 nt_errstr(status)));
6293 TALLOC_FREE(subreq);
6294 tevent_req_nterror(req, status);
6295 return;
6298 for (i=0; i<num_parallel; i++) {
6299 if (subreq == state->reqs[i]) {
6300 break;
6303 if (i == num_parallel) {
6304 DEBUG(10, ("received something we did not send\n"));
6305 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
6306 return;
6308 TALLOC_FREE(state->reqs[i]);
6310 if (state->sent >= state->num_files) {
6311 tevent_req_done(req);
6312 return;
6315 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
6316 state->sent);
6317 if (tevent_req_nomem(name, req)) {
6318 return;
6320 state->reqs[i] = torture_createdel_send(state->reqs, state->ev,
6321 state->cli, name);
6322 if (tevent_req_nomem(state->reqs[i], req)) {
6323 return;
6325 name = talloc_move(state->reqs[i], &name);
6326 tevent_req_set_callback(state->reqs[i], torture_createdels_done, req);
6327 state->sent += 1;
6330 static NTSTATUS torture_createdels_recv(struct tevent_req *req)
6332 return tevent_req_simple_recv_ntstatus(req);
6335 struct swallow_notify_state {
6336 struct tevent_context *ev;
6337 struct cli_state *cli;
6338 uint16_t fnum;
6339 uint32_t completion_filter;
6340 bool recursive;
6341 bool (*fn)(uint32_t action, const char *name, void *priv);
6342 void *priv;
6345 static void swallow_notify_done(struct tevent_req *subreq);
6347 static struct tevent_req *swallow_notify_send(TALLOC_CTX *mem_ctx,
6348 struct tevent_context *ev,
6349 struct cli_state *cli,
6350 uint16_t fnum,
6351 uint32_t completion_filter,
6352 bool recursive,
6353 bool (*fn)(uint32_t action,
6354 const char *name,
6355 void *priv),
6356 void *priv)
6358 struct tevent_req *req, *subreq;
6359 struct swallow_notify_state *state;
6361 req = tevent_req_create(mem_ctx, &state,
6362 struct swallow_notify_state);
6363 if (req == NULL) {
6364 return NULL;
6366 state->ev = ev;
6367 state->cli = cli;
6368 state->fnum = fnum;
6369 state->completion_filter = completion_filter;
6370 state->recursive = recursive;
6371 state->fn = fn;
6372 state->priv = priv;
6374 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
6375 0xffff, state->completion_filter,
6376 state->recursive);
6377 if (tevent_req_nomem(subreq, req)) {
6378 return tevent_req_post(req, ev);
6380 tevent_req_set_callback(subreq, swallow_notify_done, req);
6381 return req;
6384 static void swallow_notify_done(struct tevent_req *subreq)
6386 struct tevent_req *req = tevent_req_callback_data(
6387 subreq, struct tevent_req);
6388 struct swallow_notify_state *state = tevent_req_data(
6389 req, struct swallow_notify_state);
6390 NTSTATUS status;
6391 uint32_t i, num_changes;
6392 struct notify_change *changes;
6394 status = cli_notify_recv(subreq, state, &num_changes, &changes);
6395 TALLOC_FREE(subreq);
6396 if (!NT_STATUS_IS_OK(status)) {
6397 DEBUG(10, ("cli_notify_recv returned %s\n",
6398 nt_errstr(status)));
6399 tevent_req_nterror(req, status);
6400 return;
6403 for (i=0; i<num_changes; i++) {
6404 state->fn(changes[i].action, changes[i].name, state->priv);
6406 TALLOC_FREE(changes);
6408 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
6409 0xffff, state->completion_filter,
6410 state->recursive);
6411 if (tevent_req_nomem(subreq, req)) {
6412 return;
6414 tevent_req_set_callback(subreq, swallow_notify_done, req);
6417 static bool print_notifies(uint32_t action, const char *name, void *priv)
6419 if (DEBUGLEVEL > 5) {
6420 d_printf("%d %s\n", (int)action, name);
6422 return true;
6425 static void notify_bench_done(struct tevent_req *req)
6427 int *num_finished = (int *)tevent_req_callback_data_void(req);
6428 *num_finished += 1;
6431 static bool run_notify_bench(int dummy)
6433 const char *dname = "\\notify-bench";
6434 struct tevent_context *ev;
6435 NTSTATUS status;
6436 uint16_t dnum;
6437 struct tevent_req *req1;
6438 struct tevent_req *req2 = NULL;
6439 int i, num_unc_names;
6440 int num_finished = 0;
6442 printf("starting notify-bench test\n");
6444 if (use_multishare_conn) {
6445 char **unc_list;
6446 unc_list = file_lines_load(multishare_conn_fname,
6447 &num_unc_names, 0, NULL);
6448 if (!unc_list || num_unc_names <= 0) {
6449 d_printf("Failed to load unc names list from '%s'\n",
6450 multishare_conn_fname);
6451 return false;
6453 TALLOC_FREE(unc_list);
6454 } else {
6455 num_unc_names = 1;
6458 ev = tevent_context_init(talloc_tos());
6459 if (ev == NULL) {
6460 d_printf("tevent_context_init failed\n");
6461 return false;
6464 for (i=0; i<num_unc_names; i++) {
6465 struct cli_state *cli;
6466 char *base_fname;
6468 base_fname = talloc_asprintf(talloc_tos(), "%s\\file%3.3d.",
6469 dname, i);
6470 if (base_fname == NULL) {
6471 return false;
6474 if (!torture_open_connection(&cli, i)) {
6475 return false;
6478 status = cli_ntcreate(cli, dname, 0,
6479 MAXIMUM_ALLOWED_ACCESS,
6480 0, FILE_SHARE_READ|FILE_SHARE_WRITE|
6481 FILE_SHARE_DELETE,
6482 FILE_OPEN_IF, FILE_DIRECTORY_FILE, 0,
6483 &dnum);
6485 if (!NT_STATUS_IS_OK(status)) {
6486 d_printf("Could not create %s: %s\n", dname,
6487 nt_errstr(status));
6488 return false;
6491 req1 = swallow_notify_send(talloc_tos(), ev, cli, dnum,
6492 FILE_NOTIFY_CHANGE_FILE_NAME |
6493 FILE_NOTIFY_CHANGE_DIR_NAME |
6494 FILE_NOTIFY_CHANGE_ATTRIBUTES |
6495 FILE_NOTIFY_CHANGE_LAST_WRITE,
6496 false, print_notifies, NULL);
6497 if (req1 == NULL) {
6498 d_printf("Could not create notify request\n");
6499 return false;
6502 req2 = torture_createdels_send(talloc_tos(), ev, cli,
6503 base_fname, 10, torture_numops);
6504 if (req2 == NULL) {
6505 d_printf("Could not create createdels request\n");
6506 return false;
6508 TALLOC_FREE(base_fname);
6510 tevent_req_set_callback(req2, notify_bench_done,
6511 &num_finished);
6514 while (num_finished < num_unc_names) {
6515 int ret;
6516 ret = tevent_loop_once(ev);
6517 if (ret != 0) {
6518 d_printf("tevent_loop_once failed\n");
6519 return false;
6523 if (!tevent_req_poll(req2, ev)) {
6524 d_printf("tevent_req_poll failed\n");
6527 status = torture_createdels_recv(req2);
6528 d_printf("torture_createdels_recv returned %s\n", nt_errstr(status));
6530 return true;
6533 static bool run_mangle1(int dummy)
6535 struct cli_state *cli;
6536 const char *fname = "this_is_a_long_fname_to_be_mangled.txt";
6537 uint16_t fnum;
6538 fstring alt_name;
6539 NTSTATUS status;
6540 time_t change_time, access_time, write_time;
6541 SMB_OFF_T size;
6542 uint16_t mode;
6544 printf("starting mangle1 test\n");
6545 if (!torture_open_connection(&cli, 0)) {
6546 return False;
6549 cli_sockopt(cli, sockops);
6551 if (!NT_STATUS_IS_OK(cli_ntcreate(
6552 cli, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
6553 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
6554 d_printf("open %s failed: %s\n", fname, cli_errstr(cli));
6555 return false;
6557 cli_close(cli, fnum);
6559 status = cli_qpathinfo_alt_name(cli, fname, alt_name);
6560 if (!NT_STATUS_IS_OK(status)) {
6561 d_printf("cli_qpathinfo_alt_name failed: %s\n",
6562 nt_errstr(status));
6563 return false;
6565 d_printf("alt_name: %s\n", alt_name);
6567 if (!NT_STATUS_IS_OK(cli_open(cli, alt_name, O_RDONLY, DENY_NONE, &fnum))) {
6568 d_printf("cli_open(%s) failed: %s\n", alt_name,
6569 cli_errstr(cli));
6570 return false;
6572 cli_close(cli, fnum);
6574 status = cli_qpathinfo1(cli, alt_name, &change_time, &access_time,
6575 &write_time, &size, &mode);
6576 if (!NT_STATUS_IS_OK(status)) {
6577 d_printf("cli_qpathinfo1(%s) failed: %s\n", alt_name,
6578 nt_errstr(status));
6579 return false;
6582 return true;
6585 static size_t null_source(uint8_t *buf, size_t n, void *priv)
6587 size_t *to_pull = (size_t *)priv;
6588 size_t thistime = *to_pull;
6590 thistime = MIN(thistime, n);
6591 if (thistime == 0) {
6592 return 0;
6595 memset(buf, 0, thistime);
6596 *to_pull -= thistime;
6597 return thistime;
6600 static bool run_windows_write(int dummy)
6602 struct cli_state *cli1;
6603 uint16_t fnum;
6604 int i;
6605 bool ret = false;
6606 const char *fname = "\\writetest.txt";
6607 struct timeval start_time;
6608 double seconds;
6609 double kbytes;
6611 printf("starting windows_write test\n");
6612 if (!torture_open_connection(&cli1, 0)) {
6613 return False;
6616 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
6617 printf("open failed (%s)\n", cli_errstr(cli1));
6618 return False;
6621 cli_sockopt(cli1, sockops);
6623 start_time = timeval_current();
6625 for (i=0; i<torture_numops; i++) {
6626 uint8_t c = 0;
6627 off_t start = i * torture_blocksize;
6628 NTSTATUS status;
6629 size_t to_pull = torture_blocksize - 1;
6631 status = cli_writeall(cli1, fnum, 0, &c,
6632 start + torture_blocksize - 1, 1, NULL);
6633 if (!NT_STATUS_IS_OK(status)) {
6634 printf("cli_write failed: %s\n", nt_errstr(status));
6635 goto fail;
6638 status = cli_push(cli1, fnum, 0, i * torture_blocksize, torture_blocksize,
6639 null_source, &to_pull);
6640 if (!NT_STATUS_IS_OK(status)) {
6641 printf("cli_push returned: %s\n", nt_errstr(status));
6642 goto fail;
6646 seconds = timeval_elapsed(&start_time);
6647 kbytes = (double)torture_blocksize * torture_numops;
6648 kbytes /= 1024;
6650 printf("Wrote %d kbytes in %.2f seconds: %d kb/sec\n", (int)kbytes,
6651 (double)seconds, (int)(kbytes/seconds));
6653 ret = true;
6654 fail:
6655 cli_close(cli1, fnum);
6656 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
6657 torture_close_connection(cli1);
6658 return ret;
6661 static bool run_cli_echo(int dummy)
6663 struct cli_state *cli;
6664 NTSTATUS status;
6666 printf("starting cli_echo test\n");
6667 if (!torture_open_connection(&cli, 0)) {
6668 return false;
6670 cli_sockopt(cli, sockops);
6672 status = cli_echo(cli, 5, data_blob_const("hello", 5));
6674 d_printf("cli_echo returned %s\n", nt_errstr(status));
6676 torture_close_connection(cli);
6677 return NT_STATUS_IS_OK(status);
6680 static bool run_uid_regression_test(int dummy)
6682 static struct cli_state *cli;
6683 int16_t old_vuid;
6684 int16_t old_cnum;
6685 bool correct = True;
6686 NTSTATUS status;
6688 printf("starting uid regression test\n");
6690 if (!torture_open_connection(&cli, 0)) {
6691 return False;
6694 cli_sockopt(cli, sockops);
6696 /* Ok - now save then logoff our current user. */
6697 old_vuid = cli->vuid;
6699 status = cli_ulogoff(cli);
6700 if (!NT_STATUS_IS_OK(status)) {
6701 d_printf("(%s) cli_ulogoff failed: %s\n",
6702 __location__, nt_errstr(status));
6703 correct = false;
6704 goto out;
6707 cli->vuid = old_vuid;
6709 /* Try an operation. */
6710 status = cli_mkdir(cli, "\\uid_reg_test");
6711 if (NT_STATUS_IS_OK(status)) {
6712 d_printf("(%s) cli_mkdir succeeded\n",
6713 __location__);
6714 correct = false;
6715 goto out;
6716 } else {
6717 /* Should be bad uid. */
6718 if (!check_error(__LINE__, cli, ERRSRV, ERRbaduid,
6719 NT_STATUS_USER_SESSION_DELETED)) {
6720 correct = false;
6721 goto out;
6725 old_cnum = cli->cnum;
6727 /* Now try a SMBtdis with the invald vuid set to zero. */
6728 cli->vuid = 0;
6730 /* This should succeed. */
6731 status = cli_tdis(cli);
6733 if (NT_STATUS_IS_OK(status)) {
6734 d_printf("First tdis with invalid vuid should succeed.\n");
6735 } else {
6736 d_printf("First tdis failed (%s)\n", nt_errstr(status));
6737 correct = false;
6738 goto out;
6741 cli->vuid = old_vuid;
6742 cli->cnum = old_cnum;
6744 /* This should fail. */
6745 status = cli_tdis(cli);
6746 if (NT_STATUS_IS_OK(status)) {
6747 d_printf("Second tdis with invalid vuid should fail - succeeded instead !.\n");
6748 correct = false;
6749 goto out;
6750 } else {
6751 /* Should be bad tid. */
6752 if (!check_error(__LINE__, cli, ERRSRV, ERRinvnid,
6753 NT_STATUS_NETWORK_NAME_DELETED)) {
6754 correct = false;
6755 goto out;
6759 cli_rmdir(cli, "\\uid_reg_test");
6761 out:
6763 cli_shutdown(cli);
6764 return correct;
6768 static const char *illegal_chars = "*\\/?<>|\":";
6769 static char force_shortname_chars[] = " +,.[];=\177";
6771 static NTSTATUS shortname_del_fn(const char *mnt, struct file_info *finfo,
6772 const char *mask, void *state)
6774 struct cli_state *pcli = (struct cli_state *)state;
6775 fstring fname;
6776 NTSTATUS status = NT_STATUS_OK;
6778 slprintf(fname, sizeof(fname), "\\shortname\\%s", finfo->name);
6780 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
6781 return NT_STATUS_OK;
6783 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
6784 status = cli_rmdir(pcli, fname);
6785 if (!NT_STATUS_IS_OK(status)) {
6786 printf("del_fn: failed to rmdir %s\n,", fname );
6788 } else {
6789 status = cli_unlink(pcli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
6790 if (!NT_STATUS_IS_OK(status)) {
6791 printf("del_fn: failed to unlink %s\n,", fname );
6794 return status;
6797 struct sn_state {
6798 int matched;
6799 int i;
6800 bool val;
6803 static NTSTATUS shortname_list_fn(const char *mnt, struct file_info *finfo,
6804 const char *name, void *state)
6806 struct sn_state *s = (struct sn_state *)state;
6807 int i = s->i;
6809 #if 0
6810 printf("shortname list: i = %d, name = |%s|, shortname = |%s|\n",
6811 i, finfo->name, finfo->short_name);
6812 #endif
6814 if (strchr(force_shortname_chars, i)) {
6815 if (!finfo->short_name[0]) {
6816 /* Shortname not created when it should be. */
6817 d_printf("(%s) ERROR: Shortname was not created for file %s containing %d\n",
6818 __location__, finfo->name, i);
6819 s->val = true;
6821 } else if (finfo->short_name[0]){
6822 /* Shortname created when it should not be. */
6823 d_printf("(%s) ERROR: Shortname %s was created for file %s\n",
6824 __location__, finfo->short_name, finfo->name);
6825 s->val = true;
6827 s->matched += 1;
6828 return NT_STATUS_OK;
6831 static bool run_shortname_test(int dummy)
6833 static struct cli_state *cli;
6834 bool correct = True;
6835 int i;
6836 struct sn_state s;
6837 char fname[20];
6839 printf("starting shortname test\n");
6841 if (!torture_open_connection(&cli, 0)) {
6842 return False;
6845 cli_sockopt(cli, sockops);
6847 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
6848 cli_list(cli, "\\shortname\\*", FILE_ATTRIBUTE_DIRECTORY, shortname_del_fn, cli);
6849 cli_rmdir(cli, "\\shortname");
6851 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\shortname"))) {
6852 d_printf("(%s) cli_mkdir of \\shortname failed: %s\n",
6853 __location__, cli_errstr(cli));
6854 correct = false;
6855 goto out;
6858 strlcpy(fname, "\\shortname\\", sizeof(fname));
6859 strlcat(fname, "test .txt", sizeof(fname));
6861 s.val = false;
6863 for (i = 32; i < 128; i++) {
6864 NTSTATUS status;
6865 uint16_t fnum = (uint16_t)-1;
6867 s.i = i;
6869 if (strchr(illegal_chars, i)) {
6870 continue;
6872 fname[15] = i;
6874 status = cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
6875 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum);
6876 if (!NT_STATUS_IS_OK(status)) {
6877 d_printf("(%s) cli_nt_create of %s failed: %s\n",
6878 __location__, fname, cli_errstr(cli));
6879 correct = false;
6880 goto out;
6882 cli_close(cli, fnum);
6884 s.matched = 0;
6885 cli_list(cli, "\\shortname\\test*.*", 0, shortname_list_fn,
6886 &s);
6887 if (s.matched != 1) {
6888 d_printf("(%s) failed to list %s: %s\n",
6889 __location__, fname, cli_errstr(cli));
6890 correct = false;
6891 goto out;
6893 if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
6894 d_printf("(%s) failed to delete %s: %s\n",
6895 __location__, fname, cli_errstr(cli));
6896 correct = false;
6897 goto out;
6900 if (s.val) {
6901 correct = false;
6902 goto out;
6906 out:
6908 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
6909 cli_list(cli, "\\shortname\\*", FILE_ATTRIBUTE_DIRECTORY, shortname_del_fn, cli);
6910 cli_rmdir(cli, "\\shortname");
6911 torture_close_connection(cli);
6912 return correct;
6915 static void pagedsearch_cb(struct tevent_req *req)
6917 int rc;
6918 struct tldap_message *msg;
6919 char *dn;
6921 rc = tldap_search_paged_recv(req, talloc_tos(), &msg);
6922 if (rc != TLDAP_SUCCESS) {
6923 d_printf("tldap_search_paged_recv failed: %s\n",
6924 tldap_err2string(rc));
6925 return;
6927 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
6928 TALLOC_FREE(msg);
6929 return;
6931 if (!tldap_entry_dn(msg, &dn)) {
6932 d_printf("tldap_entry_dn failed\n");
6933 return;
6935 d_printf("%s\n", dn);
6936 TALLOC_FREE(msg);
6939 static bool run_tldap(int dummy)
6941 struct tldap_context *ld;
6942 int fd, rc;
6943 NTSTATUS status;
6944 struct sockaddr_storage addr;
6945 struct tevent_context *ev;
6946 struct tevent_req *req;
6947 char *basedn;
6948 const char *filter;
6950 if (!resolve_name(host, &addr, 0, false)) {
6951 d_printf("could not find host %s\n", host);
6952 return false;
6954 status = open_socket_out(&addr, 389, 9999, &fd);
6955 if (!NT_STATUS_IS_OK(status)) {
6956 d_printf("open_socket_out failed: %s\n", nt_errstr(status));
6957 return false;
6960 ld = tldap_context_create(talloc_tos(), fd);
6961 if (ld == NULL) {
6962 close(fd);
6963 d_printf("tldap_context_create failed\n");
6964 return false;
6967 rc = tldap_fetch_rootdse(ld);
6968 if (rc != TLDAP_SUCCESS) {
6969 d_printf("tldap_fetch_rootdse failed: %s\n",
6970 tldap_errstr(talloc_tos(), ld, rc));
6971 return false;
6974 basedn = tldap_talloc_single_attribute(
6975 tldap_rootdse(ld), "defaultNamingContext", talloc_tos());
6976 if (basedn == NULL) {
6977 d_printf("no defaultNamingContext\n");
6978 return false;
6980 d_printf("defaultNamingContext: %s\n", basedn);
6982 ev = tevent_context_init(talloc_tos());
6983 if (ev == NULL) {
6984 d_printf("tevent_context_init failed\n");
6985 return false;
6988 req = tldap_search_paged_send(talloc_tos(), ev, ld, basedn,
6989 TLDAP_SCOPE_SUB, "(objectclass=*)",
6990 NULL, 0, 0,
6991 NULL, 0, NULL, 0, 0, 0, 0, 5);
6992 if (req == NULL) {
6993 d_printf("tldap_search_paged_send failed\n");
6994 return false;
6996 tevent_req_set_callback(req, pagedsearch_cb, NULL);
6998 tevent_req_poll(req, ev);
7000 TALLOC_FREE(req);
7002 /* test search filters against rootDSE */
7003 filter = "(&(|(name=samba)(nextRid<=10000000)(usnChanged>=10)(samba~=ambas)(!(name=s*m*a)))"
7004 "(|(name:=samba)(name:dn:2.5.13.5:=samba)(:dn:2.5.13.5:=samba)(!(name=*samba))))";
7006 rc = tldap_search(ld, "", TLDAP_SCOPE_BASE, filter,
7007 NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0,
7008 talloc_tos(), NULL, NULL);
7009 if (rc != TLDAP_SUCCESS) {
7010 d_printf("tldap_search with complex filter failed: %s\n",
7011 tldap_errstr(talloc_tos(), ld, rc));
7012 return false;
7015 TALLOC_FREE(ld);
7016 return true;
7019 /* Torture test to ensure no regression of :
7020 https://bugzilla.samba.org/show_bug.cgi?id=7084
7023 static bool run_dir_createtime(int dummy)
7025 struct cli_state *cli;
7026 const char *dname = "\\testdir";
7027 const char *fname = "\\testdir\\testfile";
7028 NTSTATUS status;
7029 struct timespec create_time;
7030 struct timespec create_time1;
7031 uint16_t fnum;
7032 bool ret = false;
7034 if (!torture_open_connection(&cli, 0)) {
7035 return false;
7038 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7039 cli_rmdir(cli, dname);
7041 status = cli_mkdir(cli, dname);
7042 if (!NT_STATUS_IS_OK(status)) {
7043 printf("mkdir failed: %s\n", nt_errstr(status));
7044 goto out;
7047 status = cli_qpathinfo2(cli, dname, &create_time, NULL, NULL, NULL,
7048 NULL, NULL, NULL);
7049 if (!NT_STATUS_IS_OK(status)) {
7050 printf("cli_qpathinfo2 returned %s\n",
7051 nt_errstr(status));
7052 goto out;
7055 /* Sleep 3 seconds, then create a file. */
7056 sleep(3);
7058 status = cli_open(cli, fname, O_RDWR | O_CREAT | O_EXCL,
7059 DENY_NONE, &fnum);
7060 if (!NT_STATUS_IS_OK(status)) {
7061 printf("cli_open failed: %s\n", nt_errstr(status));
7062 goto out;
7065 status = cli_qpathinfo2(cli, dname, &create_time1, NULL, NULL, NULL,
7066 NULL, NULL, NULL);
7067 if (!NT_STATUS_IS_OK(status)) {
7068 printf("cli_qpathinfo2 (2) returned %s\n",
7069 nt_errstr(status));
7070 goto out;
7073 if (timespec_compare(&create_time1, &create_time)) {
7074 printf("run_dir_createtime: create time was updated (error)\n");
7075 } else {
7076 printf("run_dir_createtime: create time was not updated (correct)\n");
7077 ret = true;
7080 out:
7082 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7083 cli_rmdir(cli, dname);
7084 if (!torture_close_connection(cli)) {
7085 ret = false;
7087 return ret;
7091 static bool run_streamerror(int dummy)
7093 struct cli_state *cli;
7094 const char *dname = "\\testdir";
7095 const char *streamname =
7096 "testdir:{4c8cc155-6c1e-11d1-8e41-00c04fb9386d}:$DATA";
7097 NTSTATUS status;
7098 time_t change_time, access_time, write_time;
7099 SMB_OFF_T size;
7100 uint16_t mode, fnum;
7101 bool ret = true;
7103 if (!torture_open_connection(&cli, 0)) {
7104 return false;
7107 cli_unlink(cli, "\\testdir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7108 cli_rmdir(cli, dname);
7110 status = cli_mkdir(cli, dname);
7111 if (!NT_STATUS_IS_OK(status)) {
7112 printf("mkdir failed: %s\n", nt_errstr(status));
7113 return false;
7116 cli_qpathinfo1(cli, streamname, &change_time, &access_time, &write_time,
7117 &size, &mode);
7118 status = cli_nt_error(cli);
7120 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
7121 printf("pathinfo returned %s, expected "
7122 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
7123 nt_errstr(status));
7124 ret = false;
7127 status = cli_ntcreate(cli, streamname, 0x16,
7128 FILE_READ_DATA|FILE_READ_EA|
7129 FILE_READ_ATTRIBUTES|READ_CONTROL_ACCESS,
7130 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
7131 FILE_OPEN, 0, 0, &fnum);
7133 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
7134 printf("ntcreate returned %s, expected "
7135 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
7136 nt_errstr(status));
7137 ret = false;
7141 cli_rmdir(cli, dname);
7142 return ret;
7145 static bool run_local_substitute(int dummy)
7147 bool ok = true;
7149 ok &= subst_test("%U", "bla", "", -1, -1, "bla");
7150 ok &= subst_test("%u%U", "bla", "", -1, -1, "blabla");
7151 ok &= subst_test("%g", "", "", -1, -1, "NO_GROUP");
7152 ok &= subst_test("%G", "", "", -1, -1, "NO_GROUP");
7153 ok &= subst_test("%g", "", "", -1, 0, gidtoname(0));
7154 ok &= subst_test("%G", "", "", -1, 0, gidtoname(0));
7155 ok &= subst_test("%D%u", "u", "dom", -1, 0, "domu");
7156 ok &= subst_test("%i %I", "", "", -1, -1, "0.0.0.0 0.0.0.0");
7158 /* Different captialization rules in sub_basic... */
7160 ok &= (strcmp(talloc_sub_basic(talloc_tos(), "BLA", "dom", "%U%D"),
7161 "blaDOM") == 0);
7163 return ok;
7166 static bool run_local_base64(int dummy)
7168 int i;
7169 bool ret = true;
7171 for (i=1; i<2000; i++) {
7172 DATA_BLOB blob1, blob2;
7173 char *b64;
7175 blob1.data = talloc_array(talloc_tos(), uint8_t, i);
7176 blob1.length = i;
7177 generate_random_buffer(blob1.data, blob1.length);
7179 b64 = base64_encode_data_blob(talloc_tos(), blob1);
7180 if (b64 == NULL) {
7181 d_fprintf(stderr, "base64_encode_data_blob failed "
7182 "for %d bytes\n", i);
7183 ret = false;
7185 blob2 = base64_decode_data_blob(b64);
7186 TALLOC_FREE(b64);
7188 if (data_blob_cmp(&blob1, &blob2)) {
7189 d_fprintf(stderr, "data_blob_cmp failed for %d "
7190 "bytes\n", i);
7191 ret = false;
7193 TALLOC_FREE(blob1.data);
7194 data_blob_free(&blob2);
7196 return ret;
7199 static bool run_local_gencache(int dummy)
7201 char *val;
7202 time_t tm;
7203 DATA_BLOB blob;
7205 if (!gencache_set("foo", "bar", time(NULL) + 1000)) {
7206 d_printf("%s: gencache_set() failed\n", __location__);
7207 return False;
7210 if (!gencache_get("foo", NULL, NULL)) {
7211 d_printf("%s: gencache_get() failed\n", __location__);
7212 return False;
7215 if (!gencache_get("foo", &val, &tm)) {
7216 d_printf("%s: gencache_get() failed\n", __location__);
7217 return False;
7220 if (strcmp(val, "bar") != 0) {
7221 d_printf("%s: gencache_get() returned %s, expected %s\n",
7222 __location__, val, "bar");
7223 SAFE_FREE(val);
7224 return False;
7227 SAFE_FREE(val);
7229 if (!gencache_del("foo")) {
7230 d_printf("%s: gencache_del() failed\n", __location__);
7231 return False;
7233 if (gencache_del("foo")) {
7234 d_printf("%s: second gencache_del() succeeded\n",
7235 __location__);
7236 return False;
7239 if (gencache_get("foo", &val, &tm)) {
7240 d_printf("%s: gencache_get() on deleted entry "
7241 "succeeded\n", __location__);
7242 return False;
7245 blob = data_blob_string_const_null("bar");
7246 tm = time(NULL) + 60;
7248 if (!gencache_set_data_blob("foo", &blob, tm)) {
7249 d_printf("%s: gencache_set_data_blob() failed\n", __location__);
7250 return False;
7253 if (!gencache_get_data_blob("foo", &blob, NULL, NULL)) {
7254 d_printf("%s: gencache_get_data_blob() failed\n", __location__);
7255 return False;
7258 if (strcmp((const char *)blob.data, "bar") != 0) {
7259 d_printf("%s: gencache_get_data_blob() returned %s, expected %s\n",
7260 __location__, (const char *)blob.data, "bar");
7261 data_blob_free(&blob);
7262 return False;
7265 data_blob_free(&blob);
7267 if (!gencache_del("foo")) {
7268 d_printf("%s: gencache_del() failed\n", __location__);
7269 return False;
7271 if (gencache_del("foo")) {
7272 d_printf("%s: second gencache_del() succeeded\n",
7273 __location__);
7274 return False;
7277 if (gencache_get_data_blob("foo", &blob, NULL, NULL)) {
7278 d_printf("%s: gencache_get_data_blob() on deleted entry "
7279 "succeeded\n", __location__);
7280 return False;
7283 return True;
7286 static bool rbt_testval(struct db_context *db, const char *key,
7287 const char *value)
7289 struct db_record *rec;
7290 TDB_DATA data = string_tdb_data(value);
7291 bool ret = false;
7292 NTSTATUS status;
7294 rec = db->fetch_locked(db, db, string_tdb_data(key));
7295 if (rec == NULL) {
7296 d_fprintf(stderr, "fetch_locked failed\n");
7297 goto done;
7299 status = rec->store(rec, data, 0);
7300 if (!NT_STATUS_IS_OK(status)) {
7301 d_fprintf(stderr, "store failed: %s\n", nt_errstr(status));
7302 goto done;
7304 TALLOC_FREE(rec);
7306 rec = db->fetch_locked(db, db, string_tdb_data(key));
7307 if (rec == NULL) {
7308 d_fprintf(stderr, "second fetch_locked failed\n");
7309 goto done;
7311 if ((rec->value.dsize != data.dsize)
7312 || (memcmp(rec->value.dptr, data.dptr, data.dsize) != 0)) {
7313 d_fprintf(stderr, "Got wrong data back\n");
7314 goto done;
7317 ret = true;
7318 done:
7319 TALLOC_FREE(rec);
7320 return ret;
7323 static bool run_local_rbtree(int dummy)
7325 struct db_context *db;
7326 bool ret = false;
7327 int i;
7329 db = db_open_rbt(NULL);
7331 if (db == NULL) {
7332 d_fprintf(stderr, "db_open_rbt failed\n");
7333 return false;
7336 for (i=0; i<1000; i++) {
7337 char *key, *value;
7339 if (asprintf(&key, "key%ld", random()) == -1) {
7340 goto done;
7342 if (asprintf(&value, "value%ld", random()) == -1) {
7343 SAFE_FREE(key);
7344 goto done;
7347 if (!rbt_testval(db, key, value)) {
7348 SAFE_FREE(key);
7349 SAFE_FREE(value);
7350 goto done;
7353 SAFE_FREE(value);
7354 if (asprintf(&value, "value%ld", random()) == -1) {
7355 SAFE_FREE(key);
7356 goto done;
7359 if (!rbt_testval(db, key, value)) {
7360 SAFE_FREE(key);
7361 SAFE_FREE(value);
7362 goto done;
7365 SAFE_FREE(key);
7366 SAFE_FREE(value);
7369 ret = true;
7371 done:
7372 TALLOC_FREE(db);
7373 return ret;
7376 struct talloc_dict_test {
7377 int content;
7380 static int talloc_dict_traverse_fn(DATA_BLOB key, void *data, void *priv)
7382 int *count = (int *)priv;
7383 *count += 1;
7384 return 0;
7387 static bool run_local_talloc_dict(int dummy)
7389 struct talloc_dict *dict;
7390 struct talloc_dict_test *t;
7391 int key, count;
7393 dict = talloc_dict_init(talloc_tos());
7394 if (dict == NULL) {
7395 return false;
7398 t = talloc(talloc_tos(), struct talloc_dict_test);
7399 if (t == NULL) {
7400 return false;
7403 key = 1;
7404 t->content = 1;
7405 if (!talloc_dict_set(dict, data_blob_const(&key, sizeof(key)), t)) {
7406 return false;
7409 count = 0;
7410 if (talloc_dict_traverse(dict, talloc_dict_traverse_fn, &count) != 0) {
7411 return false;
7414 if (count != 1) {
7415 return false;
7418 TALLOC_FREE(dict);
7420 return true;
7423 static bool run_local_string_to_sid(int dummy) {
7424 struct dom_sid sid;
7426 if (string_to_sid(&sid, "S--1-5-32-545")) {
7427 printf("allowing S--1-5-32-545\n");
7428 return false;
7430 if (string_to_sid(&sid, "S-1-5-32-+545")) {
7431 printf("allowing S-1-5-32-+545\n");
7432 return false;
7434 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")) {
7435 printf("allowing S-1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0\n");
7436 return false;
7438 if (string_to_sid(&sid, "S-1-5-32-545-abc")) {
7439 printf("allowing S-1-5-32-545-abc\n");
7440 return false;
7442 if (!string_to_sid(&sid, "S-1-5-32-545")) {
7443 printf("could not parse S-1-5-32-545\n");
7444 return false;
7446 if (!dom_sid_equal(&sid, &global_sid_Builtin_Users)) {
7447 printf("mis-parsed S-1-5-32-545 as %s\n",
7448 sid_string_tos(&sid));
7449 return false;
7451 return true;
7454 static bool run_local_binary_to_sid(int dummy) {
7455 struct dom_sid *sid = talloc(NULL, struct dom_sid);
7456 static const char good_binary_sid[] = {
7457 0x1, /* revision number */
7458 15, /* num auths */
7459 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7460 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7461 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7462 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7463 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7464 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7465 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7466 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7467 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7468 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7469 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7470 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7471 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7472 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7473 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7474 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7477 static const char long_binary_sid[] = {
7478 0x1, /* revision number */
7479 15, /* num auths */
7480 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7481 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7482 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7483 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7484 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7485 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7486 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7487 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7488 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7489 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7490 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7491 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7492 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7493 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7494 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7495 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7496 0x1, 0x1, 0x1, 0x1, /* auth[15] */
7497 0x1, 0x1, 0x1, 0x1, /* auth[16] */
7498 0x1, 0x1, 0x1, 0x1, /* auth[17] */
7501 static const char long_binary_sid2[] = {
7502 0x1, /* revision number */
7503 32, /* num auths */
7504 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7505 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7506 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7507 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7508 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7509 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7510 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7511 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7512 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7513 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7514 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7515 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7516 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7517 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7518 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7519 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7520 0x1, 0x1, 0x1, 0x1, /* auth[15] */
7521 0x1, 0x1, 0x1, 0x1, /* auth[16] */
7522 0x1, 0x1, 0x1, 0x1, /* auth[17] */
7523 0x1, 0x1, 0x1, 0x1, /* auth[18] */
7524 0x1, 0x1, 0x1, 0x1, /* auth[19] */
7525 0x1, 0x1, 0x1, 0x1, /* auth[20] */
7526 0x1, 0x1, 0x1, 0x1, /* auth[21] */
7527 0x1, 0x1, 0x1, 0x1, /* auth[22] */
7528 0x1, 0x1, 0x1, 0x1, /* auth[23] */
7529 0x1, 0x1, 0x1, 0x1, /* auth[24] */
7530 0x1, 0x1, 0x1, 0x1, /* auth[25] */
7531 0x1, 0x1, 0x1, 0x1, /* auth[26] */
7532 0x1, 0x1, 0x1, 0x1, /* auth[27] */
7533 0x1, 0x1, 0x1, 0x1, /* auth[28] */
7534 0x1, 0x1, 0x1, 0x1, /* auth[29] */
7535 0x1, 0x1, 0x1, 0x1, /* auth[30] */
7536 0x1, 0x1, 0x1, 0x1, /* auth[31] */
7539 if (!sid_parse(good_binary_sid, sizeof(good_binary_sid), sid)) {
7540 return false;
7542 if (sid_parse(long_binary_sid2, sizeof(long_binary_sid2), sid)) {
7543 return false;
7545 if (sid_parse(long_binary_sid, sizeof(long_binary_sid), sid)) {
7546 return false;
7548 return true;
7551 /* Split a path name into filename and stream name components. Canonicalise
7552 * such that an implicit $DATA token is always explicit.
7554 * The "specification" of this function can be found in the
7555 * run_local_stream_name() function in torture.c, I've tried those
7556 * combinations against a W2k3 server.
7559 static NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
7560 char **pbase, char **pstream)
7562 char *base = NULL;
7563 char *stream = NULL;
7564 char *sname; /* stream name */
7565 const char *stype; /* stream type */
7567 DEBUG(10, ("split_ntfs_stream_name called for [%s]\n", fname));
7569 sname = strchr_m(fname, ':');
7571 if (lp_posix_pathnames() || (sname == NULL)) {
7572 if (pbase != NULL) {
7573 base = talloc_strdup(mem_ctx, fname);
7574 NT_STATUS_HAVE_NO_MEMORY(base);
7576 goto done;
7579 if (pbase != NULL) {
7580 base = talloc_strndup(mem_ctx, fname, PTR_DIFF(sname, fname));
7581 NT_STATUS_HAVE_NO_MEMORY(base);
7584 sname += 1;
7586 stype = strchr_m(sname, ':');
7588 if (stype == NULL) {
7589 sname = talloc_strdup(mem_ctx, sname);
7590 stype = "$DATA";
7592 else {
7593 if (StrCaseCmp(stype, ":$DATA") != 0) {
7595 * If there is an explicit stream type, so far we only
7596 * allow $DATA. Is there anything else allowed? -- vl
7598 DEBUG(10, ("[%s] is an invalid stream type\n", stype));
7599 TALLOC_FREE(base);
7600 return NT_STATUS_OBJECT_NAME_INVALID;
7602 sname = talloc_strndup(mem_ctx, sname, PTR_DIFF(stype, sname));
7603 stype += 1;
7606 if (sname == NULL) {
7607 TALLOC_FREE(base);
7608 return NT_STATUS_NO_MEMORY;
7611 if (sname[0] == '\0') {
7613 * no stream name, so no stream
7615 goto done;
7618 if (pstream != NULL) {
7619 stream = talloc_asprintf(mem_ctx, "%s:%s", sname, stype);
7620 if (stream == NULL) {
7621 TALLOC_FREE(sname);
7622 TALLOC_FREE(base);
7623 return NT_STATUS_NO_MEMORY;
7626 * upper-case the type field
7628 strupper_m(strchr_m(stream, ':')+1);
7631 done:
7632 if (pbase != NULL) {
7633 *pbase = base;
7635 if (pstream != NULL) {
7636 *pstream = stream;
7638 return NT_STATUS_OK;
7641 static bool test_stream_name(const char *fname, const char *expected_base,
7642 const char *expected_stream,
7643 NTSTATUS expected_status)
7645 NTSTATUS status;
7646 char *base = NULL;
7647 char *stream = NULL;
7649 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &stream);
7650 if (!NT_STATUS_EQUAL(status, expected_status)) {
7651 goto error;
7654 if (!NT_STATUS_IS_OK(status)) {
7655 return true;
7658 if (base == NULL) goto error;
7660 if (strcmp(expected_base, base) != 0) goto error;
7662 if ((expected_stream != NULL) && (stream == NULL)) goto error;
7663 if ((expected_stream == NULL) && (stream != NULL)) goto error;
7665 if ((stream != NULL) && (strcmp(expected_stream, stream) != 0))
7666 goto error;
7668 TALLOC_FREE(base);
7669 TALLOC_FREE(stream);
7670 return true;
7672 error:
7673 d_fprintf(stderr, "Do test_stream(%s, %s, %s, %s)\n",
7674 fname, expected_base ? expected_base : "<NULL>",
7675 expected_stream ? expected_stream : "<NULL>",
7676 nt_errstr(expected_status));
7677 d_fprintf(stderr, "-> base=%s, stream=%s, status=%s\n",
7678 base ? base : "<NULL>", stream ? stream : "<NULL>",
7679 nt_errstr(status));
7680 TALLOC_FREE(base);
7681 TALLOC_FREE(stream);
7682 return false;
7685 static bool run_local_stream_name(int dummy)
7687 bool ret = true;
7689 ret &= test_stream_name(
7690 "bla", "bla", NULL, NT_STATUS_OK);
7691 ret &= test_stream_name(
7692 "bla::$DATA", "bla", NULL, NT_STATUS_OK);
7693 ret &= test_stream_name(
7694 "bla:blub:", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
7695 ret &= test_stream_name(
7696 "bla::", NULL, NULL, NT_STATUS_OBJECT_NAME_INVALID);
7697 ret &= test_stream_name(
7698 "bla::123", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
7699 ret &= test_stream_name(
7700 "bla:$DATA", "bla", "$DATA:$DATA", NT_STATUS_OK);
7701 ret &= test_stream_name(
7702 "bla:x:$DATA", "bla", "x:$DATA", NT_STATUS_OK);
7703 ret &= test_stream_name(
7704 "bla:x", "bla", "x:$DATA", NT_STATUS_OK);
7706 return ret;
7709 static bool data_blob_equal(DATA_BLOB a, DATA_BLOB b)
7711 if (a.length != b.length) {
7712 printf("a.length=%d != b.length=%d\n",
7713 (int)a.length, (int)b.length);
7714 return false;
7716 if (memcmp(a.data, b.data, a.length) != 0) {
7717 printf("a.data and b.data differ\n");
7718 return false;
7720 return true;
7723 static bool run_local_memcache(int dummy)
7725 struct memcache *cache;
7726 DATA_BLOB k1, k2;
7727 DATA_BLOB d1, d2, d3;
7728 DATA_BLOB v1, v2, v3;
7730 TALLOC_CTX *mem_ctx;
7731 char *str1, *str2;
7732 size_t size1, size2;
7733 bool ret = false;
7735 cache = memcache_init(NULL, 100);
7737 if (cache == NULL) {
7738 printf("memcache_init failed\n");
7739 return false;
7742 d1 = data_blob_const("d1", 2);
7743 d2 = data_blob_const("d2", 2);
7744 d3 = data_blob_const("d3", 2);
7746 k1 = data_blob_const("d1", 2);
7747 k2 = data_blob_const("d2", 2);
7749 memcache_add(cache, STAT_CACHE, k1, d1);
7750 memcache_add(cache, GETWD_CACHE, k2, d2);
7752 if (!memcache_lookup(cache, STAT_CACHE, k1, &v1)) {
7753 printf("could not find k1\n");
7754 return false;
7756 if (!data_blob_equal(d1, v1)) {
7757 return false;
7760 if (!memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
7761 printf("could not find k2\n");
7762 return false;
7764 if (!data_blob_equal(d2, v2)) {
7765 return false;
7768 memcache_add(cache, STAT_CACHE, k1, d3);
7770 if (!memcache_lookup(cache, STAT_CACHE, k1, &v3)) {
7771 printf("could not find replaced k1\n");
7772 return false;
7774 if (!data_blob_equal(d3, v3)) {
7775 return false;
7778 memcache_add(cache, GETWD_CACHE, k1, d1);
7780 if (memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
7781 printf("Did find k2, should have been purged\n");
7782 return false;
7785 TALLOC_FREE(cache);
7787 cache = memcache_init(NULL, 0);
7789 mem_ctx = talloc_init("foo");
7791 str1 = talloc_strdup(mem_ctx, "string1");
7792 str2 = talloc_strdup(mem_ctx, "string2");
7794 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
7795 data_blob_string_const("torture"), &str1);
7796 size1 = talloc_total_size(cache);
7798 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
7799 data_blob_string_const("torture"), &str2);
7800 size2 = talloc_total_size(cache);
7802 printf("size1=%d, size2=%d\n", (int)size1, (int)size2);
7804 if (size2 > size1) {
7805 printf("memcache leaks memory!\n");
7806 goto fail;
7809 ret = true;
7810 fail:
7811 TALLOC_FREE(cache);
7812 return ret;
7815 static void wbclient_done(struct tevent_req *req)
7817 wbcErr wbc_err;
7818 struct winbindd_response *wb_resp;
7819 int *i = (int *)tevent_req_callback_data_void(req);
7821 wbc_err = wb_trans_recv(req, req, &wb_resp);
7822 TALLOC_FREE(req);
7823 *i += 1;
7824 d_printf("wb_trans_recv %d returned %s\n", *i, wbcErrorString(wbc_err));
7827 static bool run_local_wbclient(int dummy)
7829 struct event_context *ev;
7830 struct wb_context **wb_ctx;
7831 struct winbindd_request wb_req;
7832 bool result = false;
7833 int i, j;
7835 BlockSignals(True, SIGPIPE);
7837 ev = tevent_context_init_byname(talloc_tos(), "epoll");
7838 if (ev == NULL) {
7839 goto fail;
7842 wb_ctx = TALLOC_ARRAY(ev, struct wb_context *, nprocs);
7843 if (wb_ctx == NULL) {
7844 goto fail;
7847 ZERO_STRUCT(wb_req);
7848 wb_req.cmd = WINBINDD_PING;
7850 d_printf("nprocs=%d, numops=%d\n", (int)nprocs, (int)torture_numops);
7852 for (i=0; i<nprocs; i++) {
7853 wb_ctx[i] = wb_context_init(ev, NULL);
7854 if (wb_ctx[i] == NULL) {
7855 goto fail;
7857 for (j=0; j<torture_numops; j++) {
7858 struct tevent_req *req;
7859 req = wb_trans_send(ev, ev, wb_ctx[i],
7860 (j % 2) == 0, &wb_req);
7861 if (req == NULL) {
7862 goto fail;
7864 tevent_req_set_callback(req, wbclient_done, &i);
7868 i = 0;
7870 while (i < nprocs * torture_numops) {
7871 event_loop_once(ev);
7874 result = true;
7875 fail:
7876 TALLOC_FREE(ev);
7877 return result;
7880 static void getaddrinfo_finished(struct tevent_req *req)
7882 char *name = (char *)tevent_req_callback_data_void(req);
7883 struct addrinfo *ainfo;
7884 int res;
7886 res = getaddrinfo_recv(req, &ainfo);
7887 if (res != 0) {
7888 d_printf("gai(%s) returned %s\n", name, gai_strerror(res));
7889 return;
7891 d_printf("gai(%s) succeeded\n", name);
7892 freeaddrinfo(ainfo);
7895 static bool run_getaddrinfo_send(int dummy)
7897 TALLOC_CTX *frame = talloc_stackframe();
7898 struct fncall_context *ctx;
7899 struct tevent_context *ev;
7900 bool result = false;
7901 const char *names[4] = { "www.samba.org", "notfound.samba.org",
7902 "www.slashdot.org", "heise.de" };
7903 struct tevent_req *reqs[4];
7904 int i;
7906 ev = event_context_init(frame);
7907 if (ev == NULL) {
7908 goto fail;
7911 ctx = fncall_context_init(frame, 4);
7913 for (i=0; i<ARRAY_SIZE(names); i++) {
7914 reqs[i] = getaddrinfo_send(frame, ev, ctx, names[i], NULL,
7915 NULL);
7916 if (reqs[i] == NULL) {
7917 goto fail;
7919 tevent_req_set_callback(reqs[i], getaddrinfo_finished,
7920 (void *)names[i]);
7923 for (i=0; i<ARRAY_SIZE(reqs); i++) {
7924 tevent_loop_once(ev);
7927 result = true;
7928 fail:
7929 TALLOC_FREE(frame);
7930 return result;
7933 static bool dbtrans_inc(struct db_context *db)
7935 struct db_record *rec;
7936 uint32_t *val;
7937 bool ret = false;
7938 NTSTATUS status;
7940 rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
7941 if (rec == NULL) {
7942 printf(__location__ "fetch_lock failed\n");
7943 return false;
7946 if (rec->value.dsize != sizeof(uint32_t)) {
7947 printf(__location__ "value.dsize = %d\n",
7948 (int)rec->value.dsize);
7949 goto fail;
7952 val = (uint32_t *)rec->value.dptr;
7953 *val += 1;
7955 status = rec->store(rec, make_tdb_data((uint8_t *)val,
7956 sizeof(uint32_t)),
7958 if (!NT_STATUS_IS_OK(status)) {
7959 printf(__location__ "store failed: %s\n",
7960 nt_errstr(status));
7961 goto fail;
7964 ret = true;
7965 fail:
7966 TALLOC_FREE(rec);
7967 return ret;
7970 static bool run_local_dbtrans(int dummy)
7972 struct db_context *db;
7973 struct db_record *rec;
7974 NTSTATUS status;
7975 uint32_t initial;
7976 int res;
7978 db = db_open(talloc_tos(), "transtest.tdb", 0, TDB_DEFAULT,
7979 O_RDWR|O_CREAT, 0600);
7980 if (db == NULL) {
7981 printf("Could not open transtest.db\n");
7982 return false;
7985 res = db->transaction_start(db);
7986 if (res == -1) {
7987 printf(__location__ "transaction_start failed\n");
7988 return false;
7991 rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
7992 if (rec == NULL) {
7993 printf(__location__ "fetch_lock failed\n");
7994 return false;
7997 if (rec->value.dptr == NULL) {
7998 initial = 0;
7999 status = rec->store(
8000 rec, make_tdb_data((uint8_t *)&initial,
8001 sizeof(initial)),
8003 if (!NT_STATUS_IS_OK(status)) {
8004 printf(__location__ "store returned %s\n",
8005 nt_errstr(status));
8006 return false;
8010 TALLOC_FREE(rec);
8012 res = db->transaction_commit(db);
8013 if (res == -1) {
8014 printf(__location__ "transaction_commit failed\n");
8015 return false;
8018 while (true) {
8019 uint32_t val, val2;
8020 int i;
8022 res = db->transaction_start(db);
8023 if (res == -1) {
8024 printf(__location__ "transaction_start failed\n");
8025 break;
8028 if (!dbwrap_fetch_uint32(db, "transtest", &val)) {
8029 printf(__location__ "dbwrap_fetch_uint32 failed\n");
8030 break;
8033 for (i=0; i<10; i++) {
8034 if (!dbtrans_inc(db)) {
8035 return false;
8039 if (!dbwrap_fetch_uint32(db, "transtest", &val2)) {
8040 printf(__location__ "dbwrap_fetch_uint32 failed\n");
8041 break;
8044 if (val2 != val + 10) {
8045 printf(__location__ "val=%d, val2=%d\n",
8046 (int)val, (int)val2);
8047 break;
8050 printf("val2=%d\r", val2);
8052 res = db->transaction_commit(db);
8053 if (res == -1) {
8054 printf(__location__ "transaction_commit failed\n");
8055 break;
8059 TALLOC_FREE(db);
8060 return true;
8064 * Just a dummy test to be run under a debugger. There's no real way
8065 * to inspect the tevent_select specific function from outside of
8066 * tevent_select.c.
8069 static bool run_local_tevent_select(int dummy)
8071 struct tevent_context *ev;
8072 struct tevent_fd *fd1, *fd2;
8073 bool result = false;
8075 ev = tevent_context_init_byname(NULL, "select");
8076 if (ev == NULL) {
8077 d_fprintf(stderr, "tevent_context_init_byname failed\n");
8078 goto fail;
8081 fd1 = tevent_add_fd(ev, ev, 2, 0, NULL, NULL);
8082 if (fd1 == NULL) {
8083 d_fprintf(stderr, "tevent_add_fd failed\n");
8084 goto fail;
8086 fd2 = tevent_add_fd(ev, ev, 3, 0, NULL, NULL);
8087 if (fd2 == NULL) {
8088 d_fprintf(stderr, "tevent_add_fd failed\n");
8089 goto fail;
8091 TALLOC_FREE(fd2);
8093 fd2 = tevent_add_fd(ev, ev, 1, 0, NULL, NULL);
8094 if (fd2 == NULL) {
8095 d_fprintf(stderr, "tevent_add_fd failed\n");
8096 goto fail;
8099 result = true;
8100 fail:
8101 TALLOC_FREE(ev);
8102 return result;
8105 static bool run_local_tdb_opener(int dummy)
8107 TDB_CONTEXT *t;
8108 unsigned v = 0;
8110 while (1) {
8111 t = tdb_open("test.tdb", 1000, TDB_CLEAR_IF_FIRST,
8112 O_RDWR|O_CREAT, 0755);
8113 if (t == NULL) {
8114 perror("tdb_open failed");
8115 return false;
8117 tdb_close(t);
8119 v += 1;
8120 printf("\r%u", v);
8122 return true;
8125 static bool run_local_tdb_writer(int dummy)
8127 TDB_CONTEXT *t;
8128 unsigned v = 0;
8129 TDB_DATA val;
8131 t = tdb_open("test.tdb", 1000, 0, O_RDWR|O_CREAT, 0755);
8132 if (t == 0) {
8133 perror("tdb_open failed");
8134 return 1;
8137 val.dptr = (uint8_t *)&v;
8138 val.dsize = sizeof(v);
8140 while (1) {
8141 TDB_DATA data;
8142 int ret;
8144 ret = tdb_store(t, val, val, 0);
8145 if (ret != 0) {
8146 printf("%s\n", tdb_errorstr(t));
8148 v += 1;
8149 printf("\r%u", v);
8151 data = tdb_fetch(t, val);
8152 if (data.dptr != NULL) {
8153 SAFE_FREE(data.dptr);
8156 return true;
8159 static double create_procs(bool (*fn)(int), bool *result)
8161 int i, status;
8162 volatile pid_t *child_status;
8163 volatile bool *child_status_out;
8164 int synccount;
8165 int tries = 8;
8166 struct timeval start;
8168 synccount = 0;
8170 child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*nprocs);
8171 if (!child_status) {
8172 printf("Failed to setup shared memory\n");
8173 return -1;
8176 child_status_out = (volatile bool *)shm_setup(sizeof(bool)*nprocs);
8177 if (!child_status_out) {
8178 printf("Failed to setup result status shared memory\n");
8179 return -1;
8182 for (i = 0; i < nprocs; i++) {
8183 child_status[i] = 0;
8184 child_status_out[i] = True;
8187 start = timeval_current();
8189 for (i=0;i<nprocs;i++) {
8190 procnum = i;
8191 if (fork() == 0) {
8192 pid_t mypid = getpid();
8193 sys_srandom(((int)mypid) ^ ((int)time(NULL)));
8195 slprintf(myname,sizeof(myname),"CLIENT%d", i);
8197 while (1) {
8198 if (torture_open_connection(&current_cli, i)) break;
8199 if (tries-- == 0) {
8200 printf("pid %d failed to start\n", (int)getpid());
8201 _exit(1);
8203 smb_msleep(10);
8206 child_status[i] = getpid();
8208 while (child_status[i] && timeval_elapsed(&start) < 5) smb_msleep(2);
8210 child_status_out[i] = fn(i);
8211 _exit(0);
8215 do {
8216 synccount = 0;
8217 for (i=0;i<nprocs;i++) {
8218 if (child_status[i]) synccount++;
8220 if (synccount == nprocs) break;
8221 smb_msleep(10);
8222 } while (timeval_elapsed(&start) < 30);
8224 if (synccount != nprocs) {
8225 printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount);
8226 *result = False;
8227 return timeval_elapsed(&start);
8230 /* start the client load */
8231 start = timeval_current();
8233 for (i=0;i<nprocs;i++) {
8234 child_status[i] = 0;
8237 printf("%d clients started\n", nprocs);
8239 for (i=0;i<nprocs;i++) {
8240 while (waitpid(0, &status, 0) == -1 && errno == EINTR) /* noop */ ;
8243 printf("\n");
8245 for (i=0;i<nprocs;i++) {
8246 if (!child_status_out[i]) {
8247 *result = False;
8250 return timeval_elapsed(&start);
8253 #define FLAG_MULTIPROC 1
8255 static struct {
8256 const char *name;
8257 bool (*fn)(int);
8258 unsigned flags;
8259 } torture_ops[] = {
8260 {"FDPASS", run_fdpasstest, 0},
8261 {"LOCK1", run_locktest1, 0},
8262 {"LOCK2", run_locktest2, 0},
8263 {"LOCK3", run_locktest3, 0},
8264 {"LOCK4", run_locktest4, 0},
8265 {"LOCK5", run_locktest5, 0},
8266 {"LOCK6", run_locktest6, 0},
8267 {"LOCK7", run_locktest7, 0},
8268 {"LOCK8", run_locktest8, 0},
8269 {"LOCK9", run_locktest9, 0},
8270 {"UNLINK", run_unlinktest, 0},
8271 {"BROWSE", run_browsetest, 0},
8272 {"ATTR", run_attrtest, 0},
8273 {"TRANS2", run_trans2test, 0},
8274 {"MAXFID", run_maxfidtest, FLAG_MULTIPROC},
8275 {"TORTURE",run_torture, FLAG_MULTIPROC},
8276 {"RANDOMIPC", run_randomipc, 0},
8277 {"NEGNOWAIT", run_negprot_nowait, 0},
8278 {"NBENCH", run_nbench, 0},
8279 {"NBENCH2", run_nbench2, 0},
8280 {"OPLOCK1", run_oplock1, 0},
8281 {"OPLOCK2", run_oplock2, 0},
8282 {"OPLOCK3", run_oplock3, 0},
8283 {"OPLOCK4", run_oplock4, 0},
8284 {"DIR", run_dirtest, 0},
8285 {"DIR1", run_dirtest1, 0},
8286 {"DIR-CREATETIME", run_dir_createtime, 0},
8287 {"DENY1", torture_denytest1, 0},
8288 {"DENY2", torture_denytest2, 0},
8289 {"TCON", run_tcon_test, 0},
8290 {"TCONDEV", run_tcon_devtype_test, 0},
8291 {"RW1", run_readwritetest, 0},
8292 {"RW2", run_readwritemulti, FLAG_MULTIPROC},
8293 {"RW3", run_readwritelarge, 0},
8294 {"RW-SIGNING", run_readwritelarge_signtest, 0},
8295 {"OPEN", run_opentest, 0},
8296 {"POSIX", run_simple_posix_open_test, 0},
8297 {"POSIX-APPEND", run_posix_append, 0},
8298 {"ASYNC-ECHO", run_async_echo, 0},
8299 { "UID-REGRESSION-TEST", run_uid_regression_test, 0},
8300 { "SHORTNAME-TEST", run_shortname_test, 0},
8301 { "ADDRCHANGE", run_addrchange, 0},
8302 #if 1
8303 {"OPENATTR", run_openattrtest, 0},
8304 #endif
8305 {"XCOPY", run_xcopy, 0},
8306 {"RENAME", run_rename, 0},
8307 {"DELETE", run_deletetest, 0},
8308 {"DELETE-LN", run_deletetest_ln, 0},
8309 {"PROPERTIES", run_properties, 0},
8310 {"MANGLE", torture_mangle, 0},
8311 {"MANGLE1", run_mangle1, 0},
8312 {"W2K", run_w2ktest, 0},
8313 {"TRANS2SCAN", torture_trans2_scan, 0},
8314 {"NTTRANSSCAN", torture_nttrans_scan, 0},
8315 {"UTABLE", torture_utable, 0},
8316 {"CASETABLE", torture_casetable, 0},
8317 {"ERRMAPEXTRACT", run_error_map_extract, 0},
8318 {"PIPE_NUMBER", run_pipe_number, 0},
8319 {"TCON2", run_tcon2_test, 0},
8320 {"IOCTL", torture_ioctl_test, 0},
8321 {"CHKPATH", torture_chkpath_test, 0},
8322 {"FDSESS", run_fdsesstest, 0},
8323 { "EATEST", run_eatest, 0},
8324 { "SESSSETUP_BENCH", run_sesssetup_bench, 0},
8325 { "CHAIN1", run_chain1, 0},
8326 { "CHAIN2", run_chain2, 0},
8327 { "WINDOWS-WRITE", run_windows_write, 0},
8328 { "CLI_ECHO", run_cli_echo, 0},
8329 { "GETADDRINFO", run_getaddrinfo_send, 0},
8330 { "TLDAP", run_tldap },
8331 { "STREAMERROR", run_streamerror },
8332 { "NOTIFY-BENCH", run_notify_bench },
8333 { "BAD-NBT-SESSION", run_bad_nbt_session },
8334 { "SMB-ANY-CONNECT", run_smb_any_connect },
8335 { "LOCAL-SUBSTITUTE", run_local_substitute, 0},
8336 { "LOCAL-GENCACHE", run_local_gencache, 0},
8337 { "LOCAL-TALLOC-DICT", run_local_talloc_dict, 0},
8338 { "LOCAL-BASE64", run_local_base64, 0},
8339 { "LOCAL-RBTREE", run_local_rbtree, 0},
8340 { "LOCAL-MEMCACHE", run_local_memcache, 0},
8341 { "LOCAL-STREAM-NAME", run_local_stream_name, 0},
8342 { "LOCAL-WBCLIENT", run_local_wbclient, 0},
8343 { "LOCAL-string_to_sid", run_local_string_to_sid, 0},
8344 { "LOCAL-binary_to_sid", run_local_binary_to_sid, 0},
8345 { "LOCAL-DBTRANS", run_local_dbtrans, 0},
8346 { "LOCAL-TEVENT-SELECT", run_local_tevent_select, 0},
8347 { "LOCAL-TDB-OPENER", run_local_tdb_opener, 0 },
8348 { "LOCAL-TDB-WRITER", run_local_tdb_writer, 0 },
8349 {NULL, NULL, 0}};
8353 /****************************************************************************
8354 run a specified test or "ALL"
8355 ****************************************************************************/
8356 static bool run_test(const char *name)
8358 bool ret = True;
8359 bool result = True;
8360 bool found = False;
8361 int i;
8362 double t;
8363 if (strequal(name,"ALL")) {
8364 for (i=0;torture_ops[i].name;i++) {
8365 run_test(torture_ops[i].name);
8367 found = True;
8370 for (i=0;torture_ops[i].name;i++) {
8371 fstr_sprintf(randomfname, "\\XX%x",
8372 (unsigned)random());
8374 if (strequal(name, torture_ops[i].name)) {
8375 found = True;
8376 printf("Running %s\n", name);
8377 if (torture_ops[i].flags & FLAG_MULTIPROC) {
8378 t = create_procs(torture_ops[i].fn, &result);
8379 if (!result) {
8380 ret = False;
8381 printf("TEST %s FAILED!\n", name);
8383 } else {
8384 struct timeval start;
8385 start = timeval_current();
8386 if (!torture_ops[i].fn(0)) {
8387 ret = False;
8388 printf("TEST %s FAILED!\n", name);
8390 t = timeval_elapsed(&start);
8392 printf("%s took %g secs\n\n", name, t);
8396 if (!found) {
8397 printf("Did not find a test named %s\n", name);
8398 ret = False;
8401 return ret;
8405 static void usage(void)
8407 int i;
8409 printf("WARNING samba4 test suite is much more complete nowadays.\n");
8410 printf("Please use samba4 torture.\n\n");
8412 printf("Usage: smbtorture //server/share <options> TEST1 TEST2 ...\n");
8414 printf("\t-d debuglevel\n");
8415 printf("\t-U user%%pass\n");
8416 printf("\t-k use kerberos\n");
8417 printf("\t-N numprocs\n");
8418 printf("\t-n my_netbios_name\n");
8419 printf("\t-W workgroup\n");
8420 printf("\t-o num_operations\n");
8421 printf("\t-O socket_options\n");
8422 printf("\t-m maximum protocol\n");
8423 printf("\t-L use oplocks\n");
8424 printf("\t-c CLIENT.TXT specify client load file for NBENCH\n");
8425 printf("\t-A showall\n");
8426 printf("\t-p port\n");
8427 printf("\t-s seed\n");
8428 printf("\t-b unclist_filename specify multiple shares for multiple connections\n");
8429 printf("\n\n");
8431 printf("tests are:");
8432 for (i=0;torture_ops[i].name;i++) {
8433 printf(" %s", torture_ops[i].name);
8435 printf("\n");
8437 printf("default test is ALL\n");
8439 exit(1);
8442 /****************************************************************************
8443 main program
8444 ****************************************************************************/
8445 int main(int argc,char *argv[])
8447 int opt, i;
8448 char *p;
8449 int gotuser = 0;
8450 int gotpass = 0;
8451 bool correct = True;
8452 TALLOC_CTX *frame = talloc_stackframe();
8453 int seed = time(NULL);
8455 #ifdef HAVE_SETBUFFER
8456 setbuffer(stdout, NULL, 0);
8457 #endif
8459 setup_logging("smbtorture", DEBUG_STDOUT);
8461 load_case_tables();
8463 if (is_default_dyn_CONFIGFILE()) {
8464 if(getenv("SMB_CONF_PATH")) {
8465 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
8468 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
8469 load_interfaces();
8471 if (argc < 2) {
8472 usage();
8475 for(p = argv[1]; *p; p++)
8476 if(*p == '\\')
8477 *p = '/';
8479 if (strncmp(argv[1], "//", 2)) {
8480 usage();
8483 fstrcpy(host, &argv[1][2]);
8484 p = strchr_m(&host[2],'/');
8485 if (!p) {
8486 usage();
8488 *p = 0;
8489 fstrcpy(share, p+1);
8491 fstrcpy(myname, get_myname(talloc_tos()));
8492 if (!*myname) {
8493 fprintf(stderr, "Failed to get my hostname.\n");
8494 return 1;
8497 if (*username == 0 && getenv("LOGNAME")) {
8498 fstrcpy(username,getenv("LOGNAME"));
8501 argc--;
8502 argv++;
8504 fstrcpy(workgroup, lp_workgroup());
8506 while ((opt = getopt(argc, argv, "p:hW:U:n:N:O:o:m:Ll:d:Aec:ks:b:B:")) != EOF) {
8507 switch (opt) {
8508 case 'p':
8509 port_to_use = atoi(optarg);
8510 break;
8511 case 's':
8512 seed = atoi(optarg);
8513 break;
8514 case 'W':
8515 fstrcpy(workgroup,optarg);
8516 break;
8517 case 'm':
8518 max_protocol = interpret_protocol(optarg, max_protocol);
8519 break;
8520 case 'N':
8521 nprocs = atoi(optarg);
8522 break;
8523 case 'o':
8524 torture_numops = atoi(optarg);
8525 break;
8526 case 'd':
8527 lp_set_cmdline("log level", optarg);
8528 break;
8529 case 'O':
8530 sockops = optarg;
8531 break;
8532 case 'L':
8533 use_oplocks = True;
8534 break;
8535 case 'l':
8536 local_path = optarg;
8537 break;
8538 case 'A':
8539 torture_showall = True;
8540 break;
8541 case 'n':
8542 fstrcpy(myname, optarg);
8543 break;
8544 case 'c':
8545 client_txt = optarg;
8546 break;
8547 case 'e':
8548 do_encrypt = true;
8549 break;
8550 case 'k':
8551 #ifdef HAVE_KRB5
8552 use_kerberos = True;
8553 #else
8554 d_printf("No kerberos support compiled in\n");
8555 exit(1);
8556 #endif
8557 break;
8558 case 'U':
8559 gotuser = 1;
8560 fstrcpy(username,optarg);
8561 p = strchr_m(username,'%');
8562 if (p) {
8563 *p = 0;
8564 fstrcpy(password, p+1);
8565 gotpass = 1;
8567 break;
8568 case 'b':
8569 fstrcpy(multishare_conn_fname, optarg);
8570 use_multishare_conn = True;
8571 break;
8572 case 'B':
8573 torture_blocksize = atoi(optarg);
8574 break;
8575 default:
8576 printf("Unknown option %c (%d)\n", (char)opt, opt);
8577 usage();
8581 d_printf("using seed %d\n", seed);
8583 srandom(seed);
8585 if(use_kerberos && !gotuser) gotpass = True;
8587 while (!gotpass) {
8588 p = getpass("Password:");
8589 if (p) {
8590 fstrcpy(password, p);
8591 gotpass = 1;
8595 printf("host=%s share=%s user=%s myname=%s\n",
8596 host, share, username, myname);
8598 if (argc == optind) {
8599 correct = run_test("ALL");
8600 } else {
8601 for (i=optind;i<argc;i++) {
8602 if (!run_test(argv[i])) {
8603 correct = False;
8608 TALLOC_FREE(frame);
8610 if (correct) {
8611 return(0);
8612 } else {
8613 return(1);