s3-torture: run_locktest3(): replace cli_lock() with cli_lock32()
[Samba/gebeck_regimport.git] / source3 / torture / torture.c
blob1338ce030adb3b82c504f0ebbf3043aa22b038d9
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"
40 #include "libsmb/read_smb.h"
42 extern char *optarg;
43 extern int optind;
45 fstring host, workgroup, share, password, username, myname;
46 static int max_protocol = PROTOCOL_NT1;
47 static const char *sockops="TCP_NODELAY";
48 static int nprocs=1;
49 static int port_to_use=0;
50 int torture_numops=100;
51 int torture_blocksize=1024*1024;
52 static int procnum; /* records process count number when forking */
53 static struct cli_state *current_cli;
54 static fstring randomfname;
55 static bool use_oplocks;
56 static bool use_level_II_oplocks;
57 static const char *client_txt = "client_oplocks.txt";
58 static bool use_kerberos;
59 static fstring multishare_conn_fname;
60 static bool use_multishare_conn = False;
61 static bool do_encrypt;
62 static const char *local_path = NULL;
63 static int signing_state = Undefined;
64 char *test_filename;
66 bool torture_showall = False;
68 static double create_procs(bool (*fn)(int), bool *result);
71 /* return a pointer to a anonymous shared memory segment of size "size"
72 which will persist across fork() but will disappear when all processes
73 exit
75 The memory is not zeroed
77 This function uses system5 shared memory. It takes advantage of a property
78 that the memory is not destroyed if it is attached when the id is removed
80 void *shm_setup(int size)
82 int shmid;
83 void *ret;
85 #ifdef __QNXNTO__
86 shmid = shm_open("private", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
87 if (shmid == -1) {
88 printf("can't get shared memory\n");
89 exit(1);
91 shm_unlink("private");
92 if (ftruncate(shmid, size) == -1) {
93 printf("can't set shared memory size\n");
94 exit(1);
96 ret = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0);
97 if (ret == MAP_FAILED) {
98 printf("can't map shared memory\n");
99 exit(1);
101 #else
102 shmid = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
103 if (shmid == -1) {
104 printf("can't get shared memory\n");
105 exit(1);
107 ret = (void *)shmat(shmid, 0, 0);
108 if (!ret || ret == (void *)-1) {
109 printf("can't attach to shared memory\n");
110 return NULL;
112 /* the following releases the ipc, but note that this process
113 and all its children will still have access to the memory, its
114 just that the shmid is no longer valid for other shm calls. This
115 means we don't leave behind lots of shm segments after we exit
117 See Stevens "advanced programming in unix env" for details
119 shmctl(shmid, IPC_RMID, 0);
120 #endif
122 return ret;
125 /********************************************************************
126 Ensure a connection is encrypted.
127 ********************************************************************/
129 static bool force_cli_encryption(struct cli_state *c,
130 const char *sharename)
132 uint16 major, minor;
133 uint32 caplow, caphigh;
134 NTSTATUS status;
136 if (!SERVER_HAS_UNIX_CIFS(c)) {
137 d_printf("Encryption required and "
138 "server that doesn't support "
139 "UNIX extensions - failing connect\n");
140 return false;
143 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
144 &caphigh);
145 if (!NT_STATUS_IS_OK(status)) {
146 d_printf("Encryption required and "
147 "can't get UNIX CIFS extensions "
148 "version from server: %s\n", nt_errstr(status));
149 return false;
152 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
153 d_printf("Encryption required and "
154 "share %s doesn't support "
155 "encryption.\n", sharename);
156 return false;
159 if (c->use_kerberos) {
160 status = cli_gss_smb_encryption_start(c);
161 } else {
162 status = cli_raw_ntlm_smb_encryption_start(c,
163 username,
164 password,
165 workgroup);
168 if (!NT_STATUS_IS_OK(status)) {
169 d_printf("Encryption required and "
170 "setup failed with error %s.\n",
171 nt_errstr(status));
172 return false;
175 return true;
179 static struct cli_state *open_nbt_connection(void)
181 struct cli_state *c;
182 NTSTATUS status;
184 status = cli_connect_nb(host, NULL, port_to_use, 0x20, myname,
185 signing_state, &c);
186 if (!NT_STATUS_IS_OK(status)) {
187 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
188 return NULL;
191 c->use_kerberos = use_kerberos;
193 c->timeout = 120000; /* set a really long timeout (2 minutes) */
194 if (use_oplocks) c->use_oplocks = True;
195 if (use_level_II_oplocks) c->use_level_II_oplocks = True;
197 return c;
200 /****************************************************************************
201 Send a corrupt session request. See rfc1002.txt 4.3 and 4.3.2.
202 ****************************************************************************/
204 static bool cli_bad_session_request(int fd,
205 struct nmb_name *calling, struct nmb_name *called)
207 TALLOC_CTX *frame;
208 uint8_t len_buf[4];
209 struct iovec iov[3];
210 ssize_t len;
211 uint8_t *inbuf;
212 int err;
213 bool ret = false;
214 uint8_t message_type;
215 uint8_t error;
217 frame = talloc_stackframe();
219 iov[0].iov_base = len_buf;
220 iov[0].iov_len = sizeof(len_buf);
222 /* put in the destination name */
224 iov[1].iov_base = name_mangle(talloc_tos(), called->name,
225 called->name_type);
226 if (iov[1].iov_base == NULL) {
227 goto fail;
229 iov[1].iov_len = name_len((unsigned char *)iov[1].iov_base,
230 talloc_get_size(iov[1].iov_base));
232 /* and my name */
234 iov[2].iov_base = name_mangle(talloc_tos(), calling->name,
235 calling->name_type);
236 if (iov[2].iov_base == NULL) {
237 goto fail;
239 iov[2].iov_len = name_len((unsigned char *)iov[2].iov_base,
240 talloc_get_size(iov[2].iov_base));
242 /* Deliberately corrupt the name len (first byte) */
243 *((uint8_t *)iov[2].iov_base) = 100;
245 /* send a session request (RFC 1002) */
246 /* setup the packet length
247 * Remove four bytes from the length count, since the length
248 * field in the NBT Session Service header counts the number
249 * of bytes which follow. The cli_send_smb() function knows
250 * about this and accounts for those four bytes.
251 * CRH.
254 _smb_setlen(len_buf, iov[1].iov_len + iov[2].iov_len);
255 SCVAL(len_buf,0,0x81);
257 len = write_data_iov(fd, iov, 3);
258 if (len == -1) {
259 goto fail;
261 len = read_smb(fd, talloc_tos(), &inbuf, &err);
262 if (len == -1) {
263 errno = err;
264 goto fail;
267 message_type = CVAL(inbuf, 0);
268 if (message_type != 0x83) {
269 d_fprintf(stderr, "Expected msg type 0x83, got 0x%2.2x\n",
270 message_type);
271 goto fail;
274 if (smb_len(inbuf) != 1) {
275 d_fprintf(stderr, "Expected smb_len 1, got %d\n",
276 (int)smb_len(inbuf));
277 goto fail;
280 error = CVAL(inbuf, 4);
281 if (error != 0x82) {
282 d_fprintf(stderr, "Expected error 0x82, got %d\n",
283 (int)error);
284 goto fail;
287 ret = true;
288 fail:
289 TALLOC_FREE(frame);
290 return ret;
293 /* Insert a NULL at the first separator of the given path and return a pointer
294 * to the remainder of the string.
296 static char *
297 terminate_path_at_separator(char * path)
299 char * p;
301 if (!path) {
302 return NULL;
305 if ((p = strchr_m(path, '/'))) {
306 *p = '\0';
307 return p + 1;
310 if ((p = strchr_m(path, '\\'))) {
311 *p = '\0';
312 return p + 1;
315 /* No separator. */
316 return NULL;
320 parse a //server/share type UNC name
322 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
323 char **hostname, char **sharename)
325 char *p;
327 *hostname = *sharename = NULL;
329 if (strncmp(unc_name, "\\\\", 2) &&
330 strncmp(unc_name, "//", 2)) {
331 return False;
334 *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
335 p = terminate_path_at_separator(*hostname);
337 if (p && *p) {
338 *sharename = talloc_strdup(mem_ctx, p);
339 terminate_path_at_separator(*sharename);
342 if (*hostname && *sharename) {
343 return True;
346 TALLOC_FREE(*hostname);
347 TALLOC_FREE(*sharename);
348 return False;
351 static bool torture_open_connection_share(struct cli_state **c,
352 const char *hostname,
353 const char *sharename)
355 int flags = 0;
356 NTSTATUS status;
358 if (use_kerberos)
359 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
360 if (use_oplocks)
361 flags |= CLI_FULL_CONNECTION_OPLOCKS;
362 if (use_level_II_oplocks)
363 flags |= CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS;
365 status = cli_full_connection(c, myname,
366 hostname, NULL, port_to_use,
367 sharename, "?????",
368 username, workgroup,
369 password, flags, signing_state);
370 if (!NT_STATUS_IS_OK(status)) {
371 printf("failed to open share connection: //%s/%s port:%d - %s\n",
372 hostname, sharename, port_to_use, nt_errstr(status));
373 return False;
376 (*c)->timeout = 120000; /* set a really long timeout (2 minutes) */
378 if (do_encrypt) {
379 return force_cli_encryption(*c,
380 sharename);
382 return True;
385 bool torture_open_connection(struct cli_state **c, int conn_index)
387 char **unc_list = NULL;
388 int num_unc_names = 0;
389 bool result;
391 if (use_multishare_conn==True) {
392 char *h, *s;
393 unc_list = file_lines_load(multishare_conn_fname, &num_unc_names, 0, NULL);
394 if (!unc_list || num_unc_names <= 0) {
395 printf("Failed to load unc names list from '%s'\n", multishare_conn_fname);
396 exit(1);
399 if (!smbcli_parse_unc(unc_list[conn_index % num_unc_names],
400 NULL, &h, &s)) {
401 printf("Failed to parse UNC name %s\n",
402 unc_list[conn_index % num_unc_names]);
403 TALLOC_FREE(unc_list);
404 exit(1);
407 result = torture_open_connection_share(c, h, s);
409 /* h, s were copied earlier */
410 TALLOC_FREE(unc_list);
411 return result;
414 return torture_open_connection_share(c, host, share);
417 bool torture_init_connection(struct cli_state **pcli)
419 struct cli_state *cli;
421 cli = open_nbt_connection();
422 if (cli == NULL) {
423 return false;
426 *pcli = cli;
427 return true;
430 bool torture_cli_session_setup2(struct cli_state *cli, uint16 *new_vuid)
432 uint16 old_vuid = cli->vuid;
433 fstring old_user_name;
434 size_t passlen = strlen(password);
435 NTSTATUS status;
436 bool ret;
438 fstrcpy(old_user_name, cli->user_name);
439 cli->vuid = 0;
440 ret = NT_STATUS_IS_OK(cli_session_setup(cli, username,
441 password, passlen,
442 password, passlen,
443 workgroup));
444 *new_vuid = cli->vuid;
445 cli->vuid = old_vuid;
446 status = cli_set_username(cli, old_user_name);
447 if (!NT_STATUS_IS_OK(status)) {
448 return false;
450 return ret;
454 bool torture_close_connection(struct cli_state *c)
456 bool ret = True;
457 NTSTATUS status;
459 status = cli_tdis(c);
460 if (!NT_STATUS_IS_OK(status)) {
461 printf("tdis failed (%s)\n", nt_errstr(status));
462 ret = False;
465 cli_shutdown(c);
467 return ret;
471 /* check if the server produced the expected dos or nt error code */
472 static bool check_both_error(int line, NTSTATUS status,
473 uint8 eclass, uint32 ecode, NTSTATUS nterr)
475 if (NT_STATUS_IS_DOS(status)) {
476 uint8 cclass;
477 uint32 num;
479 /* Check DOS error */
480 cclass = NT_STATUS_DOS_CLASS(status);
481 num = NT_STATUS_DOS_CODE(status);
483 if (eclass != cclass || ecode != num) {
484 printf("unexpected error code class=%d code=%d\n",
485 (int)cclass, (int)num);
486 printf(" expected %d/%d %s (line=%d)\n",
487 (int)eclass, (int)ecode, nt_errstr(nterr), line);
488 return false;
490 } else {
491 /* Check NT error */
492 if (!NT_STATUS_EQUAL(nterr, status)) {
493 printf("unexpected error code %s\n",
494 nt_errstr(status));
495 printf(" expected %s (line=%d)\n",
496 nt_errstr(nterr), line);
497 return false;
501 return true;
505 /* check if the server produced the expected error code */
506 static bool check_error(int line, struct cli_state *c,
507 uint8 eclass, uint32 ecode, NTSTATUS nterr)
509 if (cli_is_dos_error(c)) {
510 uint8 cclass;
511 uint32 num;
513 /* Check DOS error */
515 cli_dos_error(c, &cclass, &num);
517 if (eclass != cclass || ecode != num) {
518 printf("unexpected error code class=%d code=%d\n",
519 (int)cclass, (int)num);
520 printf(" expected %d/%d %s (line=%d)\n",
521 (int)eclass, (int)ecode, nt_errstr(nterr), line);
522 return False;
525 } else {
526 NTSTATUS status;
528 /* Check NT error */
530 status = cli_nt_error(c);
532 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
533 printf("unexpected error code %s\n", nt_errstr(status));
534 printf(" expected %s (line=%d)\n", nt_errstr(nterr), line);
535 return False;
539 return True;
543 static bool wait_lock(struct cli_state *c, int fnum, uint32 offset, uint32 len)
545 NTSTATUS status;
547 status = cli_lock32(c, fnum, offset, len, -1, WRITE_LOCK);
549 while (!NT_STATUS_IS_OK(status)) {
550 if (!check_both_error(__LINE__, status, ERRDOS,
551 ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) {
552 return false;
555 status = cli_lock32(c, fnum, offset, len, -1, WRITE_LOCK);
558 return true;
562 static bool rw_torture(struct cli_state *c)
564 const char *lockfname = "\\torture.lck";
565 fstring fname;
566 uint16_t fnum;
567 uint16_t fnum2;
568 pid_t pid2, pid = getpid();
569 int i, j;
570 char buf[1024];
571 bool correct = True;
572 NTSTATUS status;
574 memset(buf, '\0', sizeof(buf));
576 status = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
577 DENY_NONE, &fnum2);
578 if (!NT_STATUS_IS_OK(status)) {
579 status = cli_open(c, lockfname, O_RDWR, DENY_NONE, &fnum2);
581 if (!NT_STATUS_IS_OK(status)) {
582 printf("open of %s failed (%s)\n",
583 lockfname, nt_errstr(status));
584 return False;
587 for (i=0;i<torture_numops;i++) {
588 unsigned n = (unsigned)sys_random()%10;
590 if (i % 10 == 0) {
591 printf("%d\r", i); fflush(stdout);
593 slprintf(fname, sizeof(fstring) - 1, "\\torture.%u", n);
595 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
596 return False;
599 status = cli_open(c, fname, O_RDWR | O_CREAT | O_TRUNC,
600 DENY_ALL, &fnum);
601 if (!NT_STATUS_IS_OK(status)) {
602 printf("open failed (%s)\n", nt_errstr(status));
603 correct = False;
604 break;
607 status = cli_writeall(c, fnum, 0, (uint8_t *)&pid, 0,
608 sizeof(pid), NULL);
609 if (!NT_STATUS_IS_OK(status)) {
610 printf("write failed (%s)\n", nt_errstr(status));
611 correct = False;
614 for (j=0;j<50;j++) {
615 status = cli_writeall(c, fnum, 0, (uint8_t *)buf,
616 sizeof(pid)+(j*sizeof(buf)),
617 sizeof(buf), NULL);
618 if (!NT_STATUS_IS_OK(status)) {
619 printf("write failed (%s)\n",
620 nt_errstr(status));
621 correct = False;
625 pid2 = 0;
627 if (cli_read(c, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
628 printf("read failed (%s)\n", cli_errstr(c));
629 correct = False;
632 if (pid2 != pid) {
633 printf("data corruption!\n");
634 correct = False;
637 status = cli_close(c, fnum);
638 if (!NT_STATUS_IS_OK(status)) {
639 printf("close failed (%s)\n", nt_errstr(status));
640 correct = False;
643 status = cli_unlink(c, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
644 if (!NT_STATUS_IS_OK(status)) {
645 printf("unlink failed (%s)\n", nt_errstr(status));
646 correct = False;
649 status = cli_unlock(c, fnum2, n*sizeof(int), sizeof(int));
650 if (!NT_STATUS_IS_OK(status)) {
651 printf("unlock failed (%s)\n", nt_errstr(status));
652 correct = False;
656 cli_close(c, fnum2);
657 cli_unlink(c, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
659 printf("%d\n", i);
661 return correct;
664 static bool run_torture(int dummy)
666 struct cli_state *cli;
667 bool ret;
669 cli = current_cli;
671 cli_sockopt(cli, sockops);
673 ret = rw_torture(cli);
675 if (!torture_close_connection(cli)) {
676 ret = False;
679 return ret;
682 static bool rw_torture3(struct cli_state *c, char *lockfname)
684 uint16_t fnum = (uint16_t)-1;
685 unsigned int i = 0;
686 char buf[131072];
687 char buf_rd[131072];
688 unsigned count;
689 unsigned countprev = 0;
690 ssize_t sent = 0;
691 bool correct = True;
692 NTSTATUS status = NT_STATUS_OK;
694 srandom(1);
695 for (i = 0; i < sizeof(buf); i += sizeof(uint32))
697 SIVAL(buf, i, sys_random());
700 if (procnum == 0)
702 status = cli_unlink(
703 c, lockfname,
704 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
705 if (!NT_STATUS_IS_OK(status)) {
706 printf("unlink failed (%s) (normal, this file should "
707 "not exist)\n", nt_errstr(status));
710 status = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
711 DENY_NONE, &fnum);
712 if (!NT_STATUS_IS_OK(status)) {
713 printf("first open read/write of %s failed (%s)\n",
714 lockfname, nt_errstr(status));
715 return False;
718 else
720 for (i = 0; i < 500 && fnum == (uint16_t)-1; i++)
722 status = cli_open(c, lockfname, O_RDONLY,
723 DENY_NONE, &fnum);
724 if (!NT_STATUS_IS_OK(status)) {
725 break;
727 smb_msleep(10);
729 if (!NT_STATUS_IS_OK(status)) {
730 printf("second open read-only of %s failed (%s)\n",
731 lockfname, nt_errstr(status));
732 return False;
736 i = 0;
737 for (count = 0; count < sizeof(buf); count += sent)
739 if (count >= countprev) {
740 printf("%d %8d\r", i, count);
741 fflush(stdout);
742 i++;
743 countprev += (sizeof(buf) / 20);
746 if (procnum == 0)
748 sent = ((unsigned)sys_random()%(20))+ 1;
749 if (sent > sizeof(buf) - count)
751 sent = sizeof(buf) - count;
754 status = cli_writeall(c, fnum, 0, (uint8_t *)buf+count,
755 count, (size_t)sent, NULL);
756 if (!NT_STATUS_IS_OK(status)) {
757 printf("write failed (%s)\n",
758 nt_errstr(status));
759 correct = False;
762 else
764 sent = cli_read(c, fnum, buf_rd+count, count,
765 sizeof(buf)-count);
766 if (sent < 0)
768 printf("read failed offset:%d size:%ld (%s)\n",
769 count, (unsigned long)sizeof(buf)-count,
770 cli_errstr(c));
771 correct = False;
772 sent = 0;
774 if (sent > 0)
776 if (memcmp(buf_rd+count, buf+count, sent) != 0)
778 printf("read/write compare failed\n");
779 printf("offset: %d req %ld recvd %ld\n", count, (unsigned long)sizeof(buf)-count, (unsigned long)sent);
780 correct = False;
781 break;
788 status = cli_close(c, fnum);
789 if (!NT_STATUS_IS_OK(status)) {
790 printf("close failed (%s)\n", nt_errstr(status));
791 correct = False;
794 return correct;
797 static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
799 const char *lockfname = "\\torture2.lck";
800 uint16_t fnum1;
801 uint16_t fnum2;
802 int i;
803 char buf[131072];
804 char buf_rd[131072];
805 bool correct = True;
806 ssize_t bytes_read;
807 NTSTATUS status;
809 status = cli_unlink(c1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
810 if (!NT_STATUS_IS_OK(status)) {
811 printf("unlink failed (%s) (normal, this file should not exist)\n", nt_errstr(status));
814 status = cli_open(c1, lockfname, O_RDWR | O_CREAT | O_EXCL,
815 DENY_NONE, &fnum1);
816 if (!NT_STATUS_IS_OK(status)) {
817 printf("first open read/write of %s failed (%s)\n",
818 lockfname, nt_errstr(status));
819 return False;
822 status = cli_open(c2, lockfname, O_RDONLY, DENY_NONE, &fnum2);
823 if (!NT_STATUS_IS_OK(status)) {
824 printf("second open read-only of %s failed (%s)\n",
825 lockfname, nt_errstr(status));
826 cli_close(c1, fnum1);
827 return False;
830 for (i = 0; i < torture_numops; i++)
832 size_t buf_size = ((unsigned)sys_random()%(sizeof(buf)-1))+ 1;
833 if (i % 10 == 0) {
834 printf("%d\r", i); fflush(stdout);
837 generate_random_buffer((unsigned char *)buf, buf_size);
839 status = cli_writeall(c1, fnum1, 0, (uint8_t *)buf, 0,
840 buf_size, NULL);
841 if (!NT_STATUS_IS_OK(status)) {
842 printf("write failed (%s)\n", nt_errstr(status));
843 correct = False;
844 break;
847 if ((bytes_read = cli_read(c2, fnum2, buf_rd, 0, buf_size)) != buf_size) {
848 printf("read failed (%s)\n", cli_errstr(c2));
849 printf("read %d, expected %ld\n", (int)bytes_read,
850 (unsigned long)buf_size);
851 correct = False;
852 break;
855 if (memcmp(buf_rd, buf, buf_size) != 0)
857 printf("read/write compare failed\n");
858 correct = False;
859 break;
863 status = cli_close(c2, fnum2);
864 if (!NT_STATUS_IS_OK(status)) {
865 printf("close failed (%s)\n", nt_errstr(status));
866 correct = False;
869 status = cli_close(c1, fnum1);
870 if (!NT_STATUS_IS_OK(status)) {
871 printf("close failed (%s)\n", nt_errstr(status));
872 correct = False;
875 status = cli_unlink(c1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
876 if (!NT_STATUS_IS_OK(status)) {
877 printf("unlink failed (%s)\n", nt_errstr(status));
878 correct = False;
881 return correct;
884 static bool run_readwritetest(int dummy)
886 struct cli_state *cli1, *cli2;
887 bool test1, test2 = False;
889 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
890 return False;
892 cli_sockopt(cli1, sockops);
893 cli_sockopt(cli2, sockops);
895 printf("starting readwritetest\n");
897 test1 = rw_torture2(cli1, cli2);
898 printf("Passed readwritetest v1: %s\n", BOOLSTR(test1));
900 if (test1) {
901 test2 = rw_torture2(cli1, cli1);
902 printf("Passed readwritetest v2: %s\n", BOOLSTR(test2));
905 if (!torture_close_connection(cli1)) {
906 test1 = False;
909 if (!torture_close_connection(cli2)) {
910 test2 = False;
913 return (test1 && test2);
916 static bool run_readwritemulti(int dummy)
918 struct cli_state *cli;
919 bool test;
921 cli = current_cli;
923 cli_sockopt(cli, sockops);
925 printf("run_readwritemulti: fname %s\n", randomfname);
926 test = rw_torture3(cli, randomfname);
928 if (!torture_close_connection(cli)) {
929 test = False;
932 return test;
935 static bool run_readwritelarge_internal(int max_xmit_k)
937 static struct cli_state *cli1;
938 uint16_t fnum1;
939 const char *lockfname = "\\large.dat";
940 SMB_OFF_T fsize;
941 char buf[126*1024];
942 bool correct = True;
943 NTSTATUS status;
945 if (!torture_open_connection(&cli1, 0)) {
946 return False;
948 cli_sockopt(cli1, sockops);
949 memset(buf,'\0',sizeof(buf));
951 cli1->max_xmit = max_xmit_k*1024;
953 if (signing_state == Required) {
954 /* Horrible cheat to force
955 multiple signed outstanding
956 packets against a Samba server.
958 cli1->is_samba = false;
961 printf("starting readwritelarge_internal\n");
963 cli_unlink(cli1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
965 status = cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL,
966 DENY_NONE, &fnum1);
967 if (!NT_STATUS_IS_OK(status)) {
968 printf("open read/write of %s failed (%s)\n", lockfname, nt_errstr(status));
969 return False;
972 cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, sizeof(buf), NULL);
974 status = cli_qfileinfo_basic(cli1, fnum1, NULL, &fsize, NULL, NULL,
975 NULL, NULL, NULL);
976 if (!NT_STATUS_IS_OK(status)) {
977 printf("qfileinfo failed (%s)\n", nt_errstr(status));
978 correct = False;
981 if (fsize == sizeof(buf))
982 printf("readwritelarge_internal test 1 succeeded (size = %lx)\n",
983 (unsigned long)fsize);
984 else {
985 printf("readwritelarge_internal test 1 failed (size = %lx)\n",
986 (unsigned long)fsize);
987 correct = False;
990 status = cli_close(cli1, fnum1);
991 if (!NT_STATUS_IS_OK(status)) {
992 printf("close failed (%s)\n", nt_errstr(status));
993 correct = False;
996 status = cli_unlink(cli1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
997 if (!NT_STATUS_IS_OK(status)) {
998 printf("unlink failed (%s)\n", nt_errstr(status));
999 correct = False;
1002 status = cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL,
1003 DENY_NONE, &fnum1);
1004 if (!NT_STATUS_IS_OK(status)) {
1005 printf("open read/write of %s failed (%s)\n", lockfname, nt_errstr(status));
1006 return False;
1009 cli1->max_xmit = 4*1024;
1011 cli_smbwrite(cli1, fnum1, buf, 0, sizeof(buf), NULL);
1013 status = cli_qfileinfo_basic(cli1, fnum1, NULL, &fsize, NULL, NULL,
1014 NULL, NULL, NULL);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 printf("qfileinfo failed (%s)\n", nt_errstr(status));
1017 correct = False;
1020 if (fsize == sizeof(buf))
1021 printf("readwritelarge_internal test 2 succeeded (size = %lx)\n",
1022 (unsigned long)fsize);
1023 else {
1024 printf("readwritelarge_internal test 2 failed (size = %lx)\n",
1025 (unsigned long)fsize);
1026 correct = False;
1029 #if 0
1030 /* ToDo - set allocation. JRA */
1031 if(!cli_set_allocation_size(cli1, fnum1, 0)) {
1032 printf("set allocation size to zero failed (%s)\n", cli_errstr(&cli1));
1033 return False;
1035 if (!cli_qfileinfo_basic(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL,
1036 NULL, NULL)) {
1037 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
1038 correct = False;
1040 if (fsize != 0)
1041 printf("readwritelarge test 3 (truncate test) succeeded (size = %x)\n", fsize);
1042 #endif
1044 status = cli_close(cli1, fnum1);
1045 if (!NT_STATUS_IS_OK(status)) {
1046 printf("close failed (%s)\n", nt_errstr(status));
1047 correct = False;
1050 if (!torture_close_connection(cli1)) {
1051 correct = False;
1053 return correct;
1056 static bool run_readwritelarge(int dummy)
1058 return run_readwritelarge_internal(128);
1061 static bool run_readwritelarge_signtest(int dummy)
1063 bool ret;
1064 signing_state = Required;
1065 ret = run_readwritelarge_internal(2);
1066 signing_state = Undefined;
1067 return ret;
1070 int line_count = 0;
1071 int nbio_id;
1073 #define ival(s) strtol(s, NULL, 0)
1075 /* run a test that simulates an approximate netbench client load */
1076 static bool run_netbench(int client)
1078 struct cli_state *cli;
1079 int i;
1080 char line[1024];
1081 char cname[20];
1082 FILE *f;
1083 const char *params[20];
1084 bool correct = True;
1086 cli = current_cli;
1088 nbio_id = client;
1090 cli_sockopt(cli, sockops);
1092 nb_setup(cli);
1094 slprintf(cname,sizeof(cname)-1, "client%d", client);
1096 f = fopen(client_txt, "r");
1098 if (!f) {
1099 perror(client_txt);
1100 return False;
1103 while (fgets(line, sizeof(line)-1, f)) {
1104 char *saveptr;
1105 line_count++;
1107 line[strlen(line)-1] = 0;
1109 /* printf("[%d] %s\n", line_count, line); */
1111 all_string_sub(line,"client1", cname, sizeof(line));
1113 /* parse the command parameters */
1114 params[0] = strtok_r(line, " ", &saveptr);
1115 i = 0;
1116 while (params[i]) params[++i] = strtok_r(NULL, " ", &saveptr);
1118 params[i] = "";
1120 if (i < 2) continue;
1122 if (!strncmp(params[0],"SMB", 3)) {
1123 printf("ERROR: You are using a dbench 1 load file\n");
1124 exit(1);
1127 if (!strcmp(params[0],"NTCreateX")) {
1128 nb_createx(params[1], ival(params[2]), ival(params[3]),
1129 ival(params[4]));
1130 } else if (!strcmp(params[0],"Close")) {
1131 nb_close(ival(params[1]));
1132 } else if (!strcmp(params[0],"Rename")) {
1133 nb_rename(params[1], params[2]);
1134 } else if (!strcmp(params[0],"Unlink")) {
1135 nb_unlink(params[1]);
1136 } else if (!strcmp(params[0],"Deltree")) {
1137 nb_deltree(params[1]);
1138 } else if (!strcmp(params[0],"Rmdir")) {
1139 nb_rmdir(params[1]);
1140 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
1141 nb_qpathinfo(params[1]);
1142 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
1143 nb_qfileinfo(ival(params[1]));
1144 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
1145 nb_qfsinfo(ival(params[1]));
1146 } else if (!strcmp(params[0],"FIND_FIRST")) {
1147 nb_findfirst(params[1]);
1148 } else if (!strcmp(params[0],"WriteX")) {
1149 nb_writex(ival(params[1]),
1150 ival(params[2]), ival(params[3]), ival(params[4]));
1151 } else if (!strcmp(params[0],"ReadX")) {
1152 nb_readx(ival(params[1]),
1153 ival(params[2]), ival(params[3]), ival(params[4]));
1154 } else if (!strcmp(params[0],"Flush")) {
1155 nb_flush(ival(params[1]));
1156 } else {
1157 printf("Unknown operation %s\n", params[0]);
1158 exit(1);
1161 fclose(f);
1163 nb_cleanup();
1165 if (!torture_close_connection(cli)) {
1166 correct = False;
1169 return correct;
1173 /* run a test that simulates an approximate netbench client load */
1174 static bool run_nbench(int dummy)
1176 double t;
1177 bool correct = True;
1179 nbio_shmem(nprocs);
1181 nbio_id = -1;
1183 signal(SIGALRM, nb_alarm);
1184 alarm(1);
1185 t = create_procs(run_netbench, &correct);
1186 alarm(0);
1188 printf("\nThroughput %g MB/sec\n",
1189 1.0e-6 * nbio_total() / t);
1190 return correct;
1195 This test checks for two things:
1197 1) correct support for retaining locks over a close (ie. the server
1198 must not use posix semantics)
1199 2) support for lock timeouts
1201 static bool run_locktest1(int dummy)
1203 struct cli_state *cli1, *cli2;
1204 const char *fname = "\\lockt1.lck";
1205 uint16_t fnum1, fnum2, fnum3;
1206 time_t t1, t2;
1207 unsigned lock_timeout;
1208 NTSTATUS status;
1210 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1211 return False;
1213 cli_sockopt(cli1, sockops);
1214 cli_sockopt(cli2, sockops);
1216 printf("starting locktest1\n");
1218 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1220 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
1221 &fnum1);
1222 if (!NT_STATUS_IS_OK(status)) {
1223 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
1224 return False;
1227 status = cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum2);
1228 if (!NT_STATUS_IS_OK(status)) {
1229 printf("open2 of %s failed (%s)\n", fname, nt_errstr(status));
1230 return False;
1233 status = cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum3);
1234 if (!NT_STATUS_IS_OK(status)) {
1235 printf("open3 of %s failed (%s)\n", fname, nt_errstr(status));
1236 return False;
1239 status = cli_lock32(cli1, fnum1, 0, 4, 0, WRITE_LOCK);
1240 if (!NT_STATUS_IS_OK(status)) {
1241 printf("lock1 failed (%s)\n", nt_errstr(status));
1242 return false;
1245 status = cli_lock32(cli2, fnum3, 0, 4, 0, WRITE_LOCK);
1246 if (NT_STATUS_IS_OK(status)) {
1247 printf("lock2 succeeded! This is a locking bug\n");
1248 return false;
1249 } else {
1250 if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
1251 NT_STATUS_LOCK_NOT_GRANTED)) {
1252 return false;
1256 lock_timeout = (1 + (random() % 20));
1257 printf("Testing lock timeout with timeout=%u\n", lock_timeout);
1258 t1 = time(NULL);
1259 status = cli_lock32(cli2, fnum3, 0, 4, lock_timeout * 1000, WRITE_LOCK);
1260 if (NT_STATUS_IS_OK(status)) {
1261 printf("lock3 succeeded! This is a locking bug\n");
1262 return false;
1263 } else {
1264 if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
1265 NT_STATUS_FILE_LOCK_CONFLICT)) {
1266 return false;
1269 t2 = time(NULL);
1271 if (ABS(t2 - t1) < lock_timeout-1) {
1272 printf("error: This server appears not to support timed lock requests\n");
1275 printf("server slept for %u seconds for a %u second timeout\n",
1276 (unsigned int)(t2-t1), lock_timeout);
1278 status = cli_close(cli1, fnum2);
1279 if (!NT_STATUS_IS_OK(status)) {
1280 printf("close1 failed (%s)\n", nt_errstr(status));
1281 return False;
1284 status = cli_lock32(cli2, fnum3, 0, 4, 0, WRITE_LOCK);
1285 if (NT_STATUS_IS_OK(status)) {
1286 printf("lock4 succeeded! This is a locking bug\n");
1287 return false;
1288 } else {
1289 if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
1290 NT_STATUS_FILE_LOCK_CONFLICT)) {
1291 return false;
1295 status = cli_close(cli1, fnum1);
1296 if (!NT_STATUS_IS_OK(status)) {
1297 printf("close2 failed (%s)\n", nt_errstr(status));
1298 return False;
1301 status = cli_close(cli2, fnum3);
1302 if (!NT_STATUS_IS_OK(status)) {
1303 printf("close3 failed (%s)\n", nt_errstr(status));
1304 return False;
1307 status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1308 if (!NT_STATUS_IS_OK(status)) {
1309 printf("unlink failed (%s)\n", nt_errstr(status));
1310 return False;
1314 if (!torture_close_connection(cli1)) {
1315 return False;
1318 if (!torture_close_connection(cli2)) {
1319 return False;
1322 printf("Passed locktest1\n");
1323 return True;
1327 this checks to see if a secondary tconx can use open files from an
1328 earlier tconx
1330 static bool run_tcon_test(int dummy)
1332 static struct cli_state *cli;
1333 const char *fname = "\\tcontest.tmp";
1334 uint16 fnum1;
1335 uint16 cnum1, cnum2, cnum3;
1336 uint16 vuid1, vuid2;
1337 char buf[4];
1338 bool ret = True;
1339 NTSTATUS status;
1341 memset(buf, '\0', sizeof(buf));
1343 if (!torture_open_connection(&cli, 0)) {
1344 return False;
1346 cli_sockopt(cli, sockops);
1348 printf("starting tcontest\n");
1350 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1352 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1353 if (!NT_STATUS_IS_OK(status)) {
1354 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
1355 return False;
1358 cnum1 = cli->cnum;
1359 vuid1 = cli->vuid;
1361 status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
1362 if (!NT_STATUS_IS_OK(status)) {
1363 printf("initial write failed (%s)", nt_errstr(status));
1364 return False;
1367 status = cli_tcon_andx(cli, share, "?????",
1368 password, strlen(password)+1);
1369 if (!NT_STATUS_IS_OK(status)) {
1370 printf("%s refused 2nd tree connect (%s)\n", host,
1371 nt_errstr(status));
1372 cli_shutdown(cli);
1373 return False;
1376 cnum2 = cli->cnum;
1377 cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
1378 vuid2 = cli->vuid + 1;
1380 /* try a write with the wrong tid */
1381 cli->cnum = cnum2;
1383 status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
1384 if (NT_STATUS_IS_OK(status)) {
1385 printf("* server allows write with wrong TID\n");
1386 ret = False;
1387 } else {
1388 printf("server fails write with wrong TID : %s\n",
1389 nt_errstr(status));
1393 /* try a write with an invalid tid */
1394 cli->cnum = cnum3;
1396 status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
1397 if (NT_STATUS_IS_OK(status)) {
1398 printf("* server allows write with invalid TID\n");
1399 ret = False;
1400 } else {
1401 printf("server fails write with invalid TID : %s\n",
1402 nt_errstr(status));
1405 /* try a write with an invalid vuid */
1406 cli->vuid = vuid2;
1407 cli->cnum = cnum1;
1409 status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
1410 if (NT_STATUS_IS_OK(status)) {
1411 printf("* server allows write with invalid VUID\n");
1412 ret = False;
1413 } else {
1414 printf("server fails write with invalid VUID : %s\n",
1415 nt_errstr(status));
1418 cli->cnum = cnum1;
1419 cli->vuid = vuid1;
1421 status = cli_close(cli, fnum1);
1422 if (!NT_STATUS_IS_OK(status)) {
1423 printf("close failed (%s)\n", nt_errstr(status));
1424 return False;
1427 cli->cnum = cnum2;
1429 status = cli_tdis(cli);
1430 if (!NT_STATUS_IS_OK(status)) {
1431 printf("secondary tdis failed (%s)\n", nt_errstr(status));
1432 return False;
1435 cli->cnum = cnum1;
1437 if (!torture_close_connection(cli)) {
1438 return False;
1441 return ret;
1446 checks for old style tcon support
1448 static bool run_tcon2_test(int dummy)
1450 static struct cli_state *cli;
1451 uint16 cnum, max_xmit;
1452 char *service;
1453 NTSTATUS status;
1455 if (!torture_open_connection(&cli, 0)) {
1456 return False;
1458 cli_sockopt(cli, sockops);
1460 printf("starting tcon2 test\n");
1462 if (asprintf(&service, "\\\\%s\\%s", host, share) == -1) {
1463 return false;
1466 status = cli_raw_tcon(cli, service, password, "?????", &max_xmit, &cnum);
1468 SAFE_FREE(service);
1470 if (!NT_STATUS_IS_OK(status)) {
1471 printf("tcon2 failed : %s\n", nt_errstr(status));
1472 } else {
1473 printf("tcon OK : max_xmit=%d cnum=%d\n",
1474 (int)max_xmit, (int)cnum);
1477 if (!torture_close_connection(cli)) {
1478 return False;
1481 printf("Passed tcon2 test\n");
1482 return True;
1485 static bool tcon_devtest(struct cli_state *cli,
1486 const char *myshare, const char *devtype,
1487 const char *return_devtype,
1488 NTSTATUS expected_error)
1490 NTSTATUS status;
1491 bool ret;
1493 status = cli_tcon_andx(cli, myshare, devtype,
1494 password, strlen(password)+1);
1496 if (NT_STATUS_IS_OK(expected_error)) {
1497 if (NT_STATUS_IS_OK(status)) {
1498 if (strcmp(cli->dev, return_devtype) == 0) {
1499 ret = True;
1500 } else {
1501 printf("tconX to share %s with type %s "
1502 "succeeded but returned the wrong "
1503 "device type (got [%s] but should have got [%s])\n",
1504 myshare, devtype, cli->dev, return_devtype);
1505 ret = False;
1507 } else {
1508 printf("tconX to share %s with type %s "
1509 "should have succeeded but failed\n",
1510 myshare, devtype);
1511 ret = False;
1513 cli_tdis(cli);
1514 } else {
1515 if (NT_STATUS_IS_OK(status)) {
1516 printf("tconx to share %s with type %s "
1517 "should have failed but succeeded\n",
1518 myshare, devtype);
1519 ret = False;
1520 } else {
1521 if (NT_STATUS_EQUAL(cli_nt_error(cli),
1522 expected_error)) {
1523 ret = True;
1524 } else {
1525 printf("Returned unexpected error\n");
1526 ret = False;
1530 return ret;
1534 checks for correct tconX support
1536 static bool run_tcon_devtype_test(int dummy)
1538 static struct cli_state *cli1 = NULL;
1539 int flags = 0;
1540 NTSTATUS status;
1541 bool ret = True;
1543 status = cli_full_connection(&cli1, myname,
1544 host, NULL, port_to_use,
1545 NULL, NULL,
1546 username, workgroup,
1547 password, flags, signing_state);
1549 if (!NT_STATUS_IS_OK(status)) {
1550 printf("could not open connection\n");
1551 return False;
1554 if (!tcon_devtest(cli1, "IPC$", "A:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1555 ret = False;
1557 if (!tcon_devtest(cli1, "IPC$", "?????", "IPC", NT_STATUS_OK))
1558 ret = False;
1560 if (!tcon_devtest(cli1, "IPC$", "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1561 ret = False;
1563 if (!tcon_devtest(cli1, "IPC$", "IPC", "IPC", NT_STATUS_OK))
1564 ret = False;
1566 if (!tcon_devtest(cli1, "IPC$", "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1567 ret = False;
1569 if (!tcon_devtest(cli1, share, "A:", "A:", NT_STATUS_OK))
1570 ret = False;
1572 if (!tcon_devtest(cli1, share, "?????", "A:", NT_STATUS_OK))
1573 ret = False;
1575 if (!tcon_devtest(cli1, share, "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1576 ret = False;
1578 if (!tcon_devtest(cli1, share, "IPC", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1579 ret = False;
1581 if (!tcon_devtest(cli1, share, "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1582 ret = False;
1584 cli_shutdown(cli1);
1586 if (ret)
1587 printf("Passed tcondevtest\n");
1589 return ret;
1594 This test checks that
1596 1) the server supports multiple locking contexts on the one SMB
1597 connection, distinguished by PID.
1599 2) the server correctly fails overlapping locks made by the same PID (this
1600 goes against POSIX behaviour, which is why it is tricky to implement)
1602 3) the server denies unlock requests by an incorrect client PID
1604 static bool run_locktest2(int dummy)
1606 static struct cli_state *cli;
1607 const char *fname = "\\lockt2.lck";
1608 uint16_t fnum1, fnum2, fnum3;
1609 bool correct = True;
1610 NTSTATUS status;
1612 if (!torture_open_connection(&cli, 0)) {
1613 return False;
1616 cli_sockopt(cli, sockops);
1618 printf("starting locktest2\n");
1620 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1622 cli_setpid(cli, 1);
1624 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1625 if (!NT_STATUS_IS_OK(status)) {
1626 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
1627 return False;
1630 status = cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum2);
1631 if (!NT_STATUS_IS_OK(status)) {
1632 printf("open2 of %s failed (%s)\n", fname, nt_errstr(status));
1633 return False;
1636 cli_setpid(cli, 2);
1638 status = cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum3);
1639 if (!NT_STATUS_IS_OK(status)) {
1640 printf("open3 of %s failed (%s)\n", fname, nt_errstr(status));
1641 return False;
1644 cli_setpid(cli, 1);
1646 status = cli_lock32(cli, fnum1, 0, 4, 0, WRITE_LOCK);
1647 if (!NT_STATUS_IS_OK(status)) {
1648 printf("lock1 failed (%s)\n", nt_errstr(status));
1649 return false;
1652 status = cli_lock32(cli, fnum1, 0, 4, 0, WRITE_LOCK);
1653 if (NT_STATUS_IS_OK(status)) {
1654 printf("WRITE lock1 succeeded! This is a locking bug\n");
1655 correct = false;
1656 } else {
1657 if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
1658 NT_STATUS_LOCK_NOT_GRANTED)) {
1659 return false;
1663 status = cli_lock32(cli, fnum2, 0, 4, 0, WRITE_LOCK);
1664 if (NT_STATUS_IS_OK(status)) {
1665 printf("WRITE lock2 succeeded! This is a locking bug\n");
1666 correct = false;
1667 } else {
1668 if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
1669 NT_STATUS_LOCK_NOT_GRANTED)) {
1670 return false;
1674 status = cli_lock32(cli, fnum2, 0, 4, 0, READ_LOCK);
1675 if (NT_STATUS_IS_OK(status)) {
1676 printf("READ lock2 succeeded! This is a locking bug\n");
1677 correct = false;
1678 } else {
1679 if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
1680 NT_STATUS_FILE_LOCK_CONFLICT)) {
1681 return false;
1685 status = cli_lock32(cli, fnum1, 100, 4, 0, WRITE_LOCK);
1686 if (!NT_STATUS_IS_OK(status)) {
1687 printf("lock at 100 failed (%s)\n", nt_errstr(status));
1689 cli_setpid(cli, 2);
1690 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 100, 4))) {
1691 printf("unlock at 100 succeeded! This is a locking bug\n");
1692 correct = False;
1695 status = cli_unlock(cli, fnum1, 0, 4);
1696 if (NT_STATUS_IS_OK(status)) {
1697 printf("unlock1 succeeded! This is a locking bug\n");
1698 correct = false;
1699 } else {
1700 if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
1701 NT_STATUS_RANGE_NOT_LOCKED)) {
1702 return false;
1706 status = cli_unlock(cli, fnum1, 0, 8);
1707 if (NT_STATUS_IS_OK(status)) {
1708 printf("unlock2 succeeded! This is a locking bug\n");
1709 correct = false;
1710 } else {
1711 if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
1712 NT_STATUS_RANGE_NOT_LOCKED)) {
1713 return false;
1717 status = cli_lock32(cli, fnum3, 0, 4, 0, WRITE_LOCK);
1718 if (NT_STATUS_IS_OK(status)) {
1719 printf("lock3 succeeded! This is a locking bug\n");
1720 correct = false;
1721 } else {
1722 if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
1723 NT_STATUS_LOCK_NOT_GRANTED)) {
1724 return false;
1728 cli_setpid(cli, 1);
1730 status = cli_close(cli, fnum1);
1731 if (!NT_STATUS_IS_OK(status)) {
1732 printf("close1 failed (%s)\n", nt_errstr(status));
1733 return False;
1736 status = cli_close(cli, fnum2);
1737 if (!NT_STATUS_IS_OK(status)) {
1738 printf("close2 failed (%s)\n", nt_errstr(status));
1739 return False;
1742 status = cli_close(cli, fnum3);
1743 if (!NT_STATUS_IS_OK(status)) {
1744 printf("close3 failed (%s)\n", nt_errstr(status));
1745 return False;
1748 if (!torture_close_connection(cli)) {
1749 correct = False;
1752 printf("locktest2 finished\n");
1754 return correct;
1759 This test checks that
1761 1) the server supports the full offset range in lock requests
1763 static bool run_locktest3(int dummy)
1765 static struct cli_state *cli1, *cli2;
1766 const char *fname = "\\lockt3.lck";
1767 uint16_t fnum1, fnum2;
1768 int i;
1769 uint32 offset;
1770 bool correct = True;
1771 NTSTATUS status;
1773 #define NEXT_OFFSET offset += (~(uint32)0) / torture_numops
1775 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1776 return False;
1778 cli_sockopt(cli1, sockops);
1779 cli_sockopt(cli2, sockops);
1781 printf("starting locktest3\n");
1783 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1785 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
1786 &fnum1);
1787 if (!NT_STATUS_IS_OK(status)) {
1788 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
1789 return False;
1792 status = cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
1793 if (!NT_STATUS_IS_OK(status)) {
1794 printf("open2 of %s failed (%s)\n", fname, nt_errstr(status));
1795 return False;
1798 for (offset=i=0;i<torture_numops;i++) {
1799 NEXT_OFFSET;
1801 status = cli_lock32(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK);
1802 if (!NT_STATUS_IS_OK(status)) {
1803 printf("lock1 %d failed (%s)\n",
1805 nt_errstr(status));
1806 return False;
1809 status = cli_lock32(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK);
1810 if (!NT_STATUS_IS_OK(status)) {
1811 printf("lock2 %d failed (%s)\n",
1813 nt_errstr(status));
1814 return False;
1818 for (offset=i=0;i<torture_numops;i++) {
1819 NEXT_OFFSET;
1821 status = cli_lock32(cli1, fnum1, offset-2, 1, 0, WRITE_LOCK);
1822 if (NT_STATUS_IS_OK(status)) {
1823 printf("error: lock1 %d succeeded!\n", i);
1824 return False;
1827 status = cli_lock32(cli2, fnum2, offset-1, 1, 0, WRITE_LOCK);
1828 if (NT_STATUS_IS_OK(status)) {
1829 printf("error: lock2 %d succeeded!\n", i);
1830 return False;
1833 status = cli_lock32(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK);
1834 if (NT_STATUS_IS_OK(status)) {
1835 printf("error: lock3 %d succeeded!\n", i);
1836 return False;
1839 status = cli_lock32(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK);
1840 if (NT_STATUS_IS_OK(status)) {
1841 printf("error: lock4 %d succeeded!\n", i);
1842 return False;
1846 for (offset=i=0;i<torture_numops;i++) {
1847 NEXT_OFFSET;
1849 status = cli_unlock(cli1, fnum1, offset-1, 1);
1850 if (!NT_STATUS_IS_OK(status)) {
1851 printf("unlock1 %d failed (%s)\n",
1853 nt_errstr(status));
1854 return False;
1857 status = cli_unlock(cli2, fnum2, offset-2, 1);
1858 if (!NT_STATUS_IS_OK(status)) {
1859 printf("unlock2 %d failed (%s)\n",
1861 nt_errstr(status));
1862 return False;
1866 status = cli_close(cli1, fnum1);
1867 if (!NT_STATUS_IS_OK(status)) {
1868 printf("close1 failed (%s)\n", nt_errstr(status));
1869 return False;
1872 status = cli_close(cli2, fnum2);
1873 if (!NT_STATUS_IS_OK(status)) {
1874 printf("close2 failed (%s)\n", nt_errstr(status));
1875 return False;
1878 status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1879 if (!NT_STATUS_IS_OK(status)) {
1880 printf("unlink failed (%s)\n", nt_errstr(status));
1881 return False;
1884 if (!torture_close_connection(cli1)) {
1885 correct = False;
1888 if (!torture_close_connection(cli2)) {
1889 correct = False;
1892 printf("finished locktest3\n");
1894 return correct;
1897 #define EXPECTED(ret, v) if ((ret) != (v)) { \
1898 printf("** "); correct = False; \
1902 looks at overlapping locks
1904 static bool run_locktest4(int dummy)
1906 static struct cli_state *cli1, *cli2;
1907 const char *fname = "\\lockt4.lck";
1908 uint16_t fnum1, fnum2, f;
1909 bool ret;
1910 char buf[1000];
1911 bool correct = True;
1912 NTSTATUS status;
1914 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1915 return False;
1918 cli_sockopt(cli1, sockops);
1919 cli_sockopt(cli2, sockops);
1921 printf("starting locktest4\n");
1923 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1925 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1926 cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
1928 memset(buf, 0, sizeof(buf));
1930 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, sizeof(buf),
1931 NULL);
1932 if (!NT_STATUS_IS_OK(status)) {
1933 printf("Failed to create file: %s\n", nt_errstr(status));
1934 correct = False;
1935 goto fail;
1938 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1939 cli_lock(cli1, fnum1, 2, 4, 0, WRITE_LOCK);
1940 EXPECTED(ret, False);
1941 printf("the same process %s set overlapping write locks\n", ret?"can":"cannot");
1943 ret = cli_lock(cli1, fnum1, 10, 4, 0, READ_LOCK) &&
1944 cli_lock(cli1, fnum1, 12, 4, 0, READ_LOCK);
1945 EXPECTED(ret, True);
1946 printf("the same process %s set overlapping read locks\n", ret?"can":"cannot");
1948 ret = cli_lock(cli1, fnum1, 20, 4, 0, WRITE_LOCK) &&
1949 cli_lock(cli2, fnum2, 22, 4, 0, WRITE_LOCK);
1950 EXPECTED(ret, False);
1951 printf("a different connection %s set overlapping write locks\n", ret?"can":"cannot");
1953 ret = cli_lock(cli1, fnum1, 30, 4, 0, READ_LOCK) &&
1954 cli_lock(cli2, fnum2, 32, 4, 0, READ_LOCK);
1955 EXPECTED(ret, True);
1956 printf("a different connection %s set overlapping read locks\n", ret?"can":"cannot");
1958 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 40, 4, 0, WRITE_LOCK)) &&
1959 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 42, 4, 0, WRITE_LOCK));
1960 EXPECTED(ret, False);
1961 printf("a different pid %s set overlapping write locks\n", ret?"can":"cannot");
1963 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 50, 4, 0, READ_LOCK)) &&
1964 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 52, 4, 0, READ_LOCK));
1965 EXPECTED(ret, True);
1966 printf("a different pid %s set overlapping read locks\n", ret?"can":"cannot");
1968 ret = cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK) &&
1969 cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK);
1970 EXPECTED(ret, True);
1971 printf("the same process %s set the same read lock twice\n", ret?"can":"cannot");
1973 ret = cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK) &&
1974 cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK);
1975 EXPECTED(ret, False);
1976 printf("the same process %s set the same write lock twice\n", ret?"can":"cannot");
1978 ret = cli_lock(cli1, fnum1, 80, 4, 0, READ_LOCK) &&
1979 cli_lock(cli1, fnum1, 80, 4, 0, WRITE_LOCK);
1980 EXPECTED(ret, False);
1981 printf("the same process %s overlay a read lock with a write lock\n", ret?"can":"cannot");
1983 ret = cli_lock(cli1, fnum1, 90, 4, 0, WRITE_LOCK) &&
1984 cli_lock(cli1, fnum1, 90, 4, 0, READ_LOCK);
1985 EXPECTED(ret, True);
1986 printf("the same process %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1988 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 100, 4, 0, WRITE_LOCK)) &&
1989 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 100, 4, 0, READ_LOCK));
1990 EXPECTED(ret, False);
1991 printf("a different pid %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1993 ret = cli_lock(cli1, fnum1, 110, 4, 0, READ_LOCK) &&
1994 cli_lock(cli1, fnum1, 112, 4, 0, READ_LOCK) &&
1995 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 110, 6));
1996 EXPECTED(ret, False);
1997 printf("the same process %s coalesce read locks\n", ret?"can":"cannot");
2000 ret = cli_lock(cli1, fnum1, 120, 4, 0, WRITE_LOCK) &&
2001 (cli_read(cli2, fnum2, buf, 120, 4) == 4);
2002 EXPECTED(ret, False);
2003 printf("this server %s strict write locking\n", ret?"doesn't do":"does");
2005 ret = cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK);
2006 if (ret) {
2007 status = cli_writeall(cli2, fnum2, 0, (uint8_t *)buf, 130, 4,
2008 NULL);
2009 ret = NT_STATUS_IS_OK(status);
2011 EXPECTED(ret, False);
2012 printf("this server %s strict read locking\n", ret?"doesn't do":"does");
2015 ret = cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
2016 cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
2017 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4)) &&
2018 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4));
2019 EXPECTED(ret, True);
2020 printf("this server %s do recursive read locking\n", ret?"does":"doesn't");
2023 ret = cli_lock(cli1, fnum1, 150, 4, 0, WRITE_LOCK) &&
2024 cli_lock(cli1, fnum1, 150, 4, 0, READ_LOCK) &&
2025 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4)) &&
2026 (cli_read(cli2, fnum2, buf, 150, 4) == 4) &&
2027 !(NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
2028 150, 4, NULL))) &&
2029 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4));
2030 EXPECTED(ret, True);
2031 printf("this server %s do recursive lock overlays\n", ret?"does":"doesn't");
2033 ret = cli_lock(cli1, fnum1, 160, 4, 0, READ_LOCK) &&
2034 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 160, 4)) &&
2035 NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
2036 160, 4, NULL)) &&
2037 (cli_read(cli2, fnum2, buf, 160, 4) == 4);
2038 EXPECTED(ret, True);
2039 printf("the same process %s remove a read lock using write locking\n", ret?"can":"cannot");
2041 ret = cli_lock(cli1, fnum1, 170, 4, 0, WRITE_LOCK) &&
2042 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 170, 4)) &&
2043 NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
2044 170, 4, NULL)) &&
2045 (cli_read(cli2, fnum2, buf, 170, 4) == 4);
2046 EXPECTED(ret, True);
2047 printf("the same process %s remove a write lock using read locking\n", ret?"can":"cannot");
2049 ret = cli_lock(cli1, fnum1, 190, 4, 0, WRITE_LOCK) &&
2050 cli_lock(cli1, fnum1, 190, 4, 0, READ_LOCK) &&
2051 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 190, 4)) &&
2052 !NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
2053 190, 4, NULL)) &&
2054 (cli_read(cli2, fnum2, buf, 190, 4) == 4);
2055 EXPECTED(ret, True);
2056 printf("the same process %s remove the first lock first\n", ret?"does":"doesn't");
2058 cli_close(cli1, fnum1);
2059 cli_close(cli2, fnum2);
2060 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
2061 cli_open(cli1, fname, O_RDWR, DENY_NONE, &f);
2062 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
2063 cli_lock(cli1, f, 0, 1, 0, READ_LOCK) &&
2064 NT_STATUS_IS_OK(cli_close(cli1, fnum1)) &&
2065 NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1)) &&
2066 cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
2067 cli_close(cli1, f);
2068 cli_close(cli1, fnum1);
2069 EXPECTED(ret, True);
2070 printf("the server %s have the NT byte range lock bug\n", !ret?"does":"doesn't");
2072 fail:
2073 cli_close(cli1, fnum1);
2074 cli_close(cli2, fnum2);
2075 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2076 torture_close_connection(cli1);
2077 torture_close_connection(cli2);
2079 printf("finished locktest4\n");
2080 return correct;
2084 looks at lock upgrade/downgrade.
2086 static bool run_locktest5(int dummy)
2088 static struct cli_state *cli1, *cli2;
2089 const char *fname = "\\lockt5.lck";
2090 uint16_t fnum1, fnum2, fnum3;
2091 bool ret;
2092 char buf[1000];
2093 bool correct = True;
2094 NTSTATUS status;
2096 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
2097 return False;
2100 cli_sockopt(cli1, sockops);
2101 cli_sockopt(cli2, sockops);
2103 printf("starting locktest5\n");
2105 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2107 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
2108 cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
2109 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum3);
2111 memset(buf, 0, sizeof(buf));
2113 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, sizeof(buf),
2114 NULL);
2115 if (!NT_STATUS_IS_OK(status)) {
2116 printf("Failed to create file: %s\n", nt_errstr(status));
2117 correct = False;
2118 goto fail;
2121 /* Check for NT bug... */
2122 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
2123 cli_lock(cli1, fnum3, 0, 1, 0, READ_LOCK);
2124 cli_close(cli1, fnum1);
2125 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
2126 ret = cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
2127 EXPECTED(ret, True);
2128 printf("this server %s the NT locking bug\n", ret ? "doesn't have" : "has");
2129 cli_close(cli1, fnum1);
2130 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
2131 cli_unlock(cli1, fnum3, 0, 1);
2133 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
2134 cli_lock(cli1, fnum1, 1, 1, 0, READ_LOCK);
2135 EXPECTED(ret, True);
2136 printf("the same process %s overlay a write with a read lock\n", ret?"can":"cannot");
2138 ret = cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
2139 EXPECTED(ret, False);
2141 printf("a different processs %s get a read lock on the first process lock stack\n", ret?"can":"cannot");
2143 /* Unlock the process 2 lock. */
2144 cli_unlock(cli2, fnum2, 0, 4);
2146 ret = cli_lock(cli1, fnum3, 0, 4, 0, READ_LOCK);
2147 EXPECTED(ret, False);
2149 printf("the same processs on a different fnum %s get a read lock\n", ret?"can":"cannot");
2151 /* Unlock the process 1 fnum3 lock. */
2152 cli_unlock(cli1, fnum3, 0, 4);
2154 /* Stack 2 more locks here. */
2155 ret = cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK) &&
2156 cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK);
2158 EXPECTED(ret, True);
2159 printf("the same process %s stack read locks\n", ret?"can":"cannot");
2161 /* Unlock the first process lock, then check this was the WRITE lock that was
2162 removed. */
2164 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4)) &&
2165 cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
2167 EXPECTED(ret, True);
2168 printf("the first unlock removes the %s lock\n", ret?"WRITE":"READ");
2170 /* Unlock the process 2 lock. */
2171 cli_unlock(cli2, fnum2, 0, 4);
2173 /* We should have 3 stacked locks here. Ensure we need to do 3 unlocks. */
2175 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 1, 1)) &&
2176 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4)) &&
2177 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4));
2179 EXPECTED(ret, True);
2180 printf("the same process %s unlock the stack of 4 locks\n", ret?"can":"cannot");
2182 /* Ensure the next unlock fails. */
2183 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4));
2184 EXPECTED(ret, False);
2185 printf("the same process %s count the lock stack\n", !ret?"can":"cannot");
2187 /* Ensure connection 2 can get a write lock. */
2188 ret = cli_lock(cli2, fnum2, 0, 4, 0, WRITE_LOCK);
2189 EXPECTED(ret, True);
2191 printf("a different processs %s get a write lock on the unlocked stack\n", ret?"can":"cannot");
2194 fail:
2195 cli_close(cli1, fnum1);
2196 cli_close(cli2, fnum2);
2197 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2198 if (!torture_close_connection(cli1)) {
2199 correct = False;
2201 if (!torture_close_connection(cli2)) {
2202 correct = False;
2205 printf("finished locktest5\n");
2207 return correct;
2211 tries the unusual lockingX locktype bits
2213 static bool run_locktest6(int dummy)
2215 static struct cli_state *cli;
2216 const char *fname[1] = { "\\lock6.txt" };
2217 int i;
2218 uint16_t fnum;
2219 NTSTATUS status;
2221 if (!torture_open_connection(&cli, 0)) {
2222 return False;
2225 cli_sockopt(cli, sockops);
2227 printf("starting locktest6\n");
2229 for (i=0;i<1;i++) {
2230 printf("Testing %s\n", fname[i]);
2232 cli_unlink(cli, fname[i], FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2234 cli_open(cli, fname[i], O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
2235 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CHANGE_LOCKTYPE);
2236 cli_close(cli, fnum);
2237 printf("CHANGE_LOCKTYPE gave %s\n", nt_errstr(status));
2239 cli_open(cli, fname[i], O_RDWR, DENY_NONE, &fnum);
2240 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CANCEL_LOCK);
2241 cli_close(cli, fnum);
2242 printf("CANCEL_LOCK gave %s\n", nt_errstr(status));
2244 cli_unlink(cli, fname[i], FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2247 torture_close_connection(cli);
2249 printf("finished locktest6\n");
2250 return True;
2253 static bool run_locktest7(int dummy)
2255 struct cli_state *cli1;
2256 const char *fname = "\\lockt7.lck";
2257 uint16_t fnum1;
2258 char buf[200];
2259 bool correct = False;
2260 NTSTATUS status;
2262 if (!torture_open_connection(&cli1, 0)) {
2263 return False;
2266 cli_sockopt(cli1, sockops);
2268 printf("starting locktest7\n");
2270 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2272 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
2274 memset(buf, 0, sizeof(buf));
2276 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, sizeof(buf),
2277 NULL);
2278 if (!NT_STATUS_IS_OK(status)) {
2279 printf("Failed to create file: %s\n", nt_errstr(status));
2280 goto fail;
2283 cli_setpid(cli1, 1);
2285 if (!cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK)) {
2286 printf("Unable to apply read lock on range 130:4, error was %s\n", cli_errstr(cli1));
2287 goto fail;
2288 } else {
2289 printf("pid1 successfully locked range 130:4 for READ\n");
2292 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2293 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2294 goto fail;
2295 } else {
2296 printf("pid1 successfully read the range 130:4\n");
2299 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2300 if (!NT_STATUS_IS_OK(status)) {
2301 printf("pid1 unable to write to the range 130:4, error was "
2302 "%s\n", nt_errstr(status));
2303 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
2304 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2305 goto fail;
2307 } else {
2308 printf("pid1 successfully wrote to the range 130:4 (should be denied)\n");
2309 goto fail;
2312 cli_setpid(cli1, 2);
2314 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2315 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2316 } else {
2317 printf("pid2 successfully read the range 130:4\n");
2320 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2321 if (!NT_STATUS_IS_OK(status)) {
2322 printf("pid2 unable to write to the range 130:4, error was "
2323 "%s\n", nt_errstr(status));
2324 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
2325 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2326 goto fail;
2328 } else {
2329 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2330 goto fail;
2333 cli_setpid(cli1, 1);
2334 cli_unlock(cli1, fnum1, 130, 4);
2336 if (!cli_lock(cli1, fnum1, 130, 4, 0, WRITE_LOCK)) {
2337 printf("Unable to apply write lock on range 130:4, error was %s\n", cli_errstr(cli1));
2338 goto fail;
2339 } else {
2340 printf("pid1 successfully locked range 130:4 for WRITE\n");
2343 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2344 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2345 goto fail;
2346 } else {
2347 printf("pid1 successfully read the range 130:4\n");
2350 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2351 if (!NT_STATUS_IS_OK(status)) {
2352 printf("pid1 unable to write to the range 130:4, error was "
2353 "%s\n", nt_errstr(status));
2354 goto fail;
2355 } else {
2356 printf("pid1 successfully wrote to the range 130:4\n");
2359 cli_setpid(cli1, 2);
2361 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2362 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2363 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2364 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2365 goto fail;
2367 } else {
2368 printf("pid2 successfully read the range 130:4 (should be denied)\n");
2369 goto fail;
2372 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2373 if (!NT_STATUS_IS_OK(status)) {
2374 printf("pid2 unable to write to the range 130:4, error was "
2375 "%s\n", nt_errstr(status));
2376 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
2377 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2378 goto fail;
2380 } else {
2381 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2382 goto fail;
2385 cli_unlock(cli1, fnum1, 130, 0);
2386 correct = True;
2388 fail:
2389 cli_close(cli1, fnum1);
2390 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2391 torture_close_connection(cli1);
2393 printf("finished locktest7\n");
2394 return correct;
2398 * This demonstrates a problem with our use of GPFS share modes: A file
2399 * descriptor sitting in the pending close queue holding a GPFS share mode
2400 * blocks opening a file another time. Happens with Word 2007 temp files.
2401 * With "posix locking = yes" and "gpfs:sharemodes = yes" enabled, the third
2402 * open is denied with NT_STATUS_SHARING_VIOLATION.
2405 static bool run_locktest8(int dummy)
2407 struct cli_state *cli1;
2408 const char *fname = "\\lockt8.lck";
2409 uint16_t fnum1, fnum2;
2410 char buf[200];
2411 bool correct = False;
2412 NTSTATUS status;
2414 if (!torture_open_connection(&cli1, 0)) {
2415 return False;
2418 cli_sockopt(cli1, sockops);
2420 printf("starting locktest8\n");
2422 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2424 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_WRITE,
2425 &fnum1);
2426 if (!NT_STATUS_IS_OK(status)) {
2427 d_fprintf(stderr, "cli_open returned %s\n", nt_errstr(status));
2428 return false;
2431 memset(buf, 0, sizeof(buf));
2433 status = cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum2);
2434 if (!NT_STATUS_IS_OK(status)) {
2435 d_fprintf(stderr, "cli_open second time returned %s\n",
2436 nt_errstr(status));
2437 goto fail;
2440 if (!cli_lock(cli1, fnum2, 1, 1, 0, READ_LOCK)) {
2441 printf("Unable to apply read lock on range 1:1, error was "
2442 "%s\n", cli_errstr(cli1));
2443 goto fail;
2446 status = cli_close(cli1, fnum1);
2447 if (!NT_STATUS_IS_OK(status)) {
2448 d_fprintf(stderr, "cli_close(fnum1) %s\n", nt_errstr(status));
2449 goto fail;
2452 status = cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
2453 if (!NT_STATUS_IS_OK(status)) {
2454 d_fprintf(stderr, "cli_open third time returned %s\n",
2455 nt_errstr(status));
2456 goto fail;
2459 correct = true;
2461 fail:
2462 cli_close(cli1, fnum1);
2463 cli_close(cli1, fnum2);
2464 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2465 torture_close_connection(cli1);
2467 printf("finished locktest8\n");
2468 return correct;
2472 * This test is designed to be run in conjunction with
2473 * external NFS or POSIX locks taken in the filesystem.
2474 * It checks that the smbd server will block until the
2475 * lock is released and then acquire it. JRA.
2478 static bool got_alarm;
2479 static int alarm_fd;
2481 static void alarm_handler(int dummy)
2483 got_alarm = True;
2486 static void alarm_handler_parent(int dummy)
2488 close(alarm_fd);
2491 static void do_local_lock(int read_fd, int write_fd)
2493 int fd;
2494 char c = '\0';
2495 struct flock lock;
2496 const char *local_pathname = NULL;
2497 int ret;
2499 local_pathname = talloc_asprintf(talloc_tos(),
2500 "%s/lockt9.lck", local_path);
2501 if (!local_pathname) {
2502 printf("child: alloc fail\n");
2503 exit(1);
2506 unlink(local_pathname);
2507 fd = open(local_pathname, O_RDWR|O_CREAT, 0666);
2508 if (fd == -1) {
2509 printf("child: open of %s failed %s.\n",
2510 local_pathname, strerror(errno));
2511 exit(1);
2514 /* Now take a fcntl lock. */
2515 lock.l_type = F_WRLCK;
2516 lock.l_whence = SEEK_SET;
2517 lock.l_start = 0;
2518 lock.l_len = 4;
2519 lock.l_pid = getpid();
2521 ret = fcntl(fd,F_SETLK,&lock);
2522 if (ret == -1) {
2523 printf("child: failed to get lock 0:4 on file %s. Error %s\n",
2524 local_pathname, strerror(errno));
2525 exit(1);
2526 } else {
2527 printf("child: got lock 0:4 on file %s.\n",
2528 local_pathname );
2529 fflush(stdout);
2532 CatchSignal(SIGALRM, alarm_handler);
2533 alarm(5);
2534 /* Signal the parent. */
2535 if (write(write_fd, &c, 1) != 1) {
2536 printf("child: start signal fail %s.\n",
2537 strerror(errno));
2538 exit(1);
2540 alarm(0);
2542 alarm(10);
2543 /* Wait for the parent to be ready. */
2544 if (read(read_fd, &c, 1) != 1) {
2545 printf("child: reply signal fail %s.\n",
2546 strerror(errno));
2547 exit(1);
2549 alarm(0);
2551 sleep(5);
2552 close(fd);
2553 printf("child: released lock 0:4 on file %s.\n",
2554 local_pathname );
2555 fflush(stdout);
2556 exit(0);
2559 static bool run_locktest9(int dummy)
2561 struct cli_state *cli1;
2562 const char *fname = "\\lockt9.lck";
2563 uint16_t fnum;
2564 bool correct = False;
2565 int pipe_in[2], pipe_out[2];
2566 pid_t child_pid;
2567 char c = '\0';
2568 int ret;
2569 struct timeval start;
2570 double seconds;
2571 NTSTATUS status;
2573 printf("starting locktest9\n");
2575 if (local_path == NULL) {
2576 d_fprintf(stderr, "locktest9 must be given a local path via -l <localpath>\n");
2577 return false;
2580 if (pipe(pipe_in) == -1 || pipe(pipe_out) == -1) {
2581 return false;
2584 child_pid = fork();
2585 if (child_pid == -1) {
2586 return false;
2589 if (child_pid == 0) {
2590 /* Child. */
2591 do_local_lock(pipe_out[0], pipe_in[1]);
2592 exit(0);
2595 close(pipe_out[0]);
2596 close(pipe_in[1]);
2597 pipe_out[0] = -1;
2598 pipe_in[1] = -1;
2600 /* Parent. */
2601 ret = read(pipe_in[0], &c, 1);
2602 if (ret != 1) {
2603 d_fprintf(stderr, "failed to read start signal from child. %s\n",
2604 strerror(errno));
2605 return false;
2608 if (!torture_open_connection(&cli1, 0)) {
2609 return false;
2612 cli_sockopt(cli1, sockops);
2614 status = cli_open(cli1, fname, O_RDWR, DENY_NONE,
2615 &fnum);
2616 if (!NT_STATUS_IS_OK(status)) {
2617 d_fprintf(stderr, "cli_open returned %s\n", nt_errstr(status));
2618 return false;
2621 /* Ensure the child has the lock. */
2622 if (cli_lock(cli1, fnum, 0, 4, 0, WRITE_LOCK)) {
2623 d_fprintf(stderr, "Got the lock on range 0:4 - this should not happen !\n");
2624 goto fail;
2625 } else {
2626 d_printf("Child has the lock.\n");
2629 /* Tell the child to wait 5 seconds then exit. */
2630 ret = write(pipe_out[1], &c, 1);
2631 if (ret != 1) {
2632 d_fprintf(stderr, "failed to send exit signal to child. %s\n",
2633 strerror(errno));
2634 goto fail;
2637 /* Wait 20 seconds for the lock. */
2638 alarm_fd = cli1->fd;
2639 CatchSignal(SIGALRM, alarm_handler_parent);
2640 alarm(20);
2642 start = timeval_current();
2644 if (!cli_lock(cli1, fnum, 0, 4, -1, WRITE_LOCK)) {
2645 d_fprintf(stderr, "Unable to apply write lock on range 0:4, error was "
2646 "%s\n", cli_errstr(cli1));
2647 goto fail_nofd;
2649 alarm(0);
2651 seconds = timeval_elapsed(&start);
2653 printf("Parent got the lock after %.2f seconds.\n",
2654 seconds);
2656 status = cli_close(cli1, fnum);
2657 if (!NT_STATUS_IS_OK(status)) {
2658 d_fprintf(stderr, "cli_close(fnum1) %s\n", nt_errstr(status));
2659 goto fail;
2662 correct = true;
2664 fail:
2665 cli_close(cli1, fnum);
2666 torture_close_connection(cli1);
2668 fail_nofd:
2670 printf("finished locktest9\n");
2671 return correct;
2675 test whether fnums and tids open on one VC are available on another (a major
2676 security hole)
2678 static bool run_fdpasstest(int dummy)
2680 struct cli_state *cli1, *cli2;
2681 const char *fname = "\\fdpass.tst";
2682 uint16_t fnum1;
2683 char buf[1024];
2684 NTSTATUS status;
2686 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
2687 return False;
2689 cli_sockopt(cli1, sockops);
2690 cli_sockopt(cli2, sockops);
2692 printf("starting fdpasstest\n");
2694 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2696 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
2697 &fnum1);
2698 if (!NT_STATUS_IS_OK(status)) {
2699 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
2700 return False;
2703 status = cli_writeall(cli1, fnum1, 0, (const uint8_t *)"hello world\n", 0,
2704 13, NULL);
2705 if (!NT_STATUS_IS_OK(status)) {
2706 printf("write failed (%s)\n", nt_errstr(status));
2707 return False;
2710 cli2->vuid = cli1->vuid;
2711 cli2->cnum = cli1->cnum;
2712 cli2->pid = cli1->pid;
2714 if (cli_read(cli2, fnum1, buf, 0, 13) == 13) {
2715 printf("read succeeded! nasty security hole [%s]\n",
2716 buf);
2717 return False;
2720 cli_close(cli1, fnum1);
2721 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2723 torture_close_connection(cli1);
2724 torture_close_connection(cli2);
2726 printf("finished fdpasstest\n");
2727 return True;
2730 static bool run_fdsesstest(int dummy)
2732 struct cli_state *cli;
2733 uint16 new_vuid;
2734 uint16 saved_vuid;
2735 uint16 new_cnum;
2736 uint16 saved_cnum;
2737 const char *fname = "\\fdsess.tst";
2738 const char *fname1 = "\\fdsess1.tst";
2739 uint16_t fnum1;
2740 uint16_t fnum2;
2741 char buf[1024];
2742 bool ret = True;
2743 NTSTATUS status;
2745 if (!torture_open_connection(&cli, 0))
2746 return False;
2747 cli_sockopt(cli, sockops);
2749 if (!torture_cli_session_setup2(cli, &new_vuid))
2750 return False;
2752 saved_cnum = cli->cnum;
2753 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, share, "?????", "", 1)))
2754 return False;
2755 new_cnum = cli->cnum;
2756 cli->cnum = saved_cnum;
2758 printf("starting fdsesstest\n");
2760 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2761 cli_unlink(cli, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2763 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
2764 if (!NT_STATUS_IS_OK(status)) {
2765 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
2766 return False;
2769 status = cli_writeall(cli, fnum1, 0, (const uint8_t *)"hello world\n", 0, 13,
2770 NULL);
2771 if (!NT_STATUS_IS_OK(status)) {
2772 printf("write failed (%s)\n", nt_errstr(status));
2773 return False;
2776 saved_vuid = cli->vuid;
2777 cli->vuid = new_vuid;
2779 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2780 printf("read succeeded with different vuid! nasty security hole [%s]\n",
2781 buf);
2782 ret = False;
2784 /* Try to open a file with different vuid, samba cnum. */
2785 if (NT_STATUS_IS_OK(cli_open(cli, fname1, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum2))) {
2786 printf("create with different vuid, same cnum succeeded.\n");
2787 cli_close(cli, fnum2);
2788 cli_unlink(cli, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2789 } else {
2790 printf("create with different vuid, same cnum failed.\n");
2791 printf("This will cause problems with service clients.\n");
2792 ret = False;
2795 cli->vuid = saved_vuid;
2797 /* Try with same vuid, different cnum. */
2798 cli->cnum = new_cnum;
2800 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2801 printf("read succeeded with different cnum![%s]\n",
2802 buf);
2803 ret = False;
2806 cli->cnum = saved_cnum;
2807 cli_close(cli, fnum1);
2808 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2810 torture_close_connection(cli);
2812 printf("finished fdsesstest\n");
2813 return ret;
2817 This test checks that
2819 1) the server does not allow an unlink on a file that is open
2821 static bool run_unlinktest(int dummy)
2823 struct cli_state *cli;
2824 const char *fname = "\\unlink.tst";
2825 uint16_t fnum;
2826 bool correct = True;
2827 NTSTATUS status;
2829 if (!torture_open_connection(&cli, 0)) {
2830 return False;
2833 cli_sockopt(cli, sockops);
2835 printf("starting unlink test\n");
2837 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2839 cli_setpid(cli, 1);
2841 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
2842 if (!NT_STATUS_IS_OK(status)) {
2843 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
2844 return False;
2847 status = cli_unlink(cli, fname,
2848 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2849 if (NT_STATUS_IS_OK(status)) {
2850 printf("error: server allowed unlink on an open file\n");
2851 correct = False;
2852 } else {
2853 correct = check_error(__LINE__, cli, ERRDOS, ERRbadshare,
2854 NT_STATUS_SHARING_VIOLATION);
2857 cli_close(cli, fnum);
2858 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2860 if (!torture_close_connection(cli)) {
2861 correct = False;
2864 printf("unlink test finished\n");
2866 return correct;
2871 test how many open files this server supports on the one socket
2873 static bool run_maxfidtest(int dummy)
2875 struct cli_state *cli;
2876 fstring fname;
2877 uint16_t fnums[0x11000];
2878 int i;
2879 int retries=4;
2880 bool correct = True;
2881 NTSTATUS status;
2883 cli = current_cli;
2885 if (retries <= 0) {
2886 printf("failed to connect\n");
2887 return False;
2890 cli_sockopt(cli, sockops);
2892 for (i=0; i<0x11000; i++) {
2893 slprintf(fname,sizeof(fname)-1,"\\maxfid.%d.%d", i,(int)getpid());
2894 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE,
2895 &fnums[i]);
2896 if (!NT_STATUS_IS_OK(status)) {
2897 printf("open of %s failed (%s)\n",
2898 fname, nt_errstr(status));
2899 printf("maximum fnum is %d\n", i);
2900 break;
2902 printf("%6d\r", i);
2904 printf("%6d\n", i);
2905 i--;
2907 printf("cleaning up\n");
2908 for (;i>=0;i--) {
2909 slprintf(fname,sizeof(fname)-1,"\\maxfid.%d.%d", i,(int)getpid());
2910 cli_close(cli, fnums[i]);
2912 status = cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2913 if (!NT_STATUS_IS_OK(status)) {
2914 printf("unlink of %s failed (%s)\n",
2915 fname, nt_errstr(status));
2916 correct = False;
2918 printf("%6d\r", i);
2920 printf("%6d\n", 0);
2922 printf("maxfid test finished\n");
2923 if (!torture_close_connection(cli)) {
2924 correct = False;
2926 return correct;
2929 /* generate a random buffer */
2930 static void rand_buf(char *buf, int len)
2932 while (len--) {
2933 *buf = (char)sys_random();
2934 buf++;
2938 /* send smb negprot commands, not reading the response */
2939 static bool run_negprot_nowait(int dummy)
2941 struct tevent_context *ev;
2942 int i;
2943 struct cli_state *cli;
2944 bool correct = True;
2946 printf("starting negprot nowait test\n");
2948 ev = tevent_context_init(talloc_tos());
2949 if (ev == NULL) {
2950 return false;
2953 if (!(cli = open_nbt_connection())) {
2954 TALLOC_FREE(ev);
2955 return False;
2958 for (i=0;i<50000;i++) {
2959 struct tevent_req *req;
2961 req = cli_negprot_send(ev, ev, cli);
2962 if (req == NULL) {
2963 TALLOC_FREE(ev);
2964 return false;
2966 if (!tevent_req_poll(req, ev)) {
2967 d_fprintf(stderr, "tevent_req_poll failed: %s\n",
2968 strerror(errno));
2969 TALLOC_FREE(ev);
2970 return false;
2972 TALLOC_FREE(req);
2975 if (torture_close_connection(cli)) {
2976 correct = False;
2979 printf("finished negprot nowait test\n");
2981 return correct;
2984 /* send smb negprot commands, not reading the response */
2985 static bool run_bad_nbt_session(int dummy)
2987 struct nmb_name called, calling;
2988 struct sockaddr_storage ss;
2989 NTSTATUS status;
2990 int fd;
2991 bool ret;
2993 printf("starting bad nbt session test\n");
2995 make_nmb_name(&calling, myname, 0x0);
2996 make_nmb_name(&called , host, 0x20);
2998 if (!resolve_name(host, &ss, 0x20, true)) {
2999 d_fprintf(stderr, "Could not resolve name %s\n", host);
3000 return false;
3003 status = open_socket_out(&ss, 139, 10000, &fd);
3004 if (!NT_STATUS_IS_OK(status)) {
3005 d_fprintf(stderr, "open_socket_out failed: %s\n",
3006 nt_errstr(status));
3007 return false;
3010 ret = cli_bad_session_request(fd, &calling, &called);
3011 close(fd);
3012 if (!ret) {
3013 d_fprintf(stderr, "open_socket_out failed: %s\n",
3014 nt_errstr(status));
3015 return false;
3018 printf("finished bad nbt session test\n");
3019 return true;
3022 /* send random IPC commands */
3023 static bool run_randomipc(int dummy)
3025 char *rparam = NULL;
3026 char *rdata = NULL;
3027 unsigned int rdrcnt,rprcnt;
3028 char param[1024];
3029 int api, param_len, i;
3030 struct cli_state *cli;
3031 bool correct = True;
3032 int count = 50000;
3034 printf("starting random ipc test\n");
3036 if (!torture_open_connection(&cli, 0)) {
3037 return False;
3040 for (i=0;i<count;i++) {
3041 api = sys_random() % 500;
3042 param_len = (sys_random() % 64);
3044 rand_buf(param, param_len);
3046 SSVAL(param,0,api);
3048 cli_api(cli,
3049 param, param_len, 8,
3050 NULL, 0, BUFFER_SIZE,
3051 &rparam, &rprcnt,
3052 &rdata, &rdrcnt);
3053 if (i % 100 == 0) {
3054 printf("%d/%d\r", i,count);
3057 printf("%d/%d\n", i, count);
3059 if (!torture_close_connection(cli)) {
3060 correct = False;
3063 printf("finished random ipc test\n");
3065 return correct;
3070 static void browse_callback(const char *sname, uint32 stype,
3071 const char *comment, void *state)
3073 printf("\t%20.20s %08x %s\n", sname, stype, comment);
3079 This test checks the browse list code
3082 static bool run_browsetest(int dummy)
3084 static struct cli_state *cli;
3085 bool correct = True;
3087 printf("starting browse test\n");
3089 if (!torture_open_connection(&cli, 0)) {
3090 return False;
3093 printf("domain list:\n");
3094 cli_NetServerEnum(cli, cli->server_domain,
3095 SV_TYPE_DOMAIN_ENUM,
3096 browse_callback, NULL);
3098 printf("machine list:\n");
3099 cli_NetServerEnum(cli, cli->server_domain,
3100 SV_TYPE_ALL,
3101 browse_callback, NULL);
3103 if (!torture_close_connection(cli)) {
3104 correct = False;
3107 printf("browse test finished\n");
3109 return correct;
3115 This checks how the getatr calls works
3117 static bool run_attrtest(int dummy)
3119 struct cli_state *cli;
3120 uint16_t fnum;
3121 time_t t, t2;
3122 const char *fname = "\\attrib123456789.tst";
3123 bool correct = True;
3124 NTSTATUS status;
3126 printf("starting attrib test\n");
3128 if (!torture_open_connection(&cli, 0)) {
3129 return False;
3132 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3133 cli_open(cli, fname,
3134 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3135 cli_close(cli, fnum);
3137 status = cli_getatr(cli, fname, NULL, NULL, &t);
3138 if (!NT_STATUS_IS_OK(status)) {
3139 printf("getatr failed (%s)\n", nt_errstr(status));
3140 correct = False;
3143 if (abs(t - time(NULL)) > 60*60*24*10) {
3144 printf("ERROR: SMBgetatr bug. time is %s",
3145 ctime(&t));
3146 t = time(NULL);
3147 correct = True;
3150 t2 = t-60*60*24; /* 1 day ago */
3152 status = cli_setatr(cli, fname, 0, t2);
3153 if (!NT_STATUS_IS_OK(status)) {
3154 printf("setatr failed (%s)\n", nt_errstr(status));
3155 correct = True;
3158 status = cli_getatr(cli, fname, NULL, NULL, &t);
3159 if (!NT_STATUS_IS_OK(status)) {
3160 printf("getatr failed (%s)\n", nt_errstr(status));
3161 correct = True;
3164 if (t != t2) {
3165 printf("ERROR: getatr/setatr bug. times are\n%s",
3166 ctime(&t));
3167 printf("%s", ctime(&t2));
3168 correct = True;
3171 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3173 if (!torture_close_connection(cli)) {
3174 correct = False;
3177 printf("attrib test finished\n");
3179 return correct;
3184 This checks a couple of trans2 calls
3186 static bool run_trans2test(int dummy)
3188 struct cli_state *cli;
3189 uint16_t fnum;
3190 SMB_OFF_T size;
3191 time_t c_time, a_time, m_time;
3192 struct timespec c_time_ts, a_time_ts, m_time_ts, w_time_ts, m_time2_ts;
3193 const char *fname = "\\trans2.tst";
3194 const char *dname = "\\trans2";
3195 const char *fname2 = "\\trans2\\trans2.tst";
3196 char *pname;
3197 bool correct = True;
3198 NTSTATUS status;
3199 uint32_t fs_attr;
3201 printf("starting trans2 test\n");
3203 if (!torture_open_connection(&cli, 0)) {
3204 return False;
3207 status = cli_get_fs_attr_info(cli, &fs_attr);
3208 if (!NT_STATUS_IS_OK(status)) {
3209 printf("ERROR: cli_get_fs_attr_info returned %s\n",
3210 nt_errstr(status));
3211 correct = false;
3214 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3215 cli_open(cli, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3216 status = cli_qfileinfo_basic(cli, fnum, NULL, &size, &c_time_ts,
3217 &a_time_ts, &w_time_ts, &m_time_ts, NULL);
3218 if (!NT_STATUS_IS_OK(status)) {
3219 printf("ERROR: qfileinfo failed (%s)\n", nt_errstr(status));
3220 correct = False;
3223 status = cli_qfilename(cli, fnum, talloc_tos(), &pname);
3224 if (!NT_STATUS_IS_OK(status)) {
3225 printf("ERROR: qfilename failed (%s)\n", nt_errstr(status));
3226 correct = False;
3229 if (strcmp(pname, fname)) {
3230 printf("qfilename gave different name? [%s] [%s]\n",
3231 fname, pname);
3232 correct = False;
3235 cli_close(cli, fnum);
3237 sleep(2);
3239 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3240 status = cli_open(cli, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE,
3241 &fnum);
3242 if (!NT_STATUS_IS_OK(status)) {
3243 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3244 return False;
3246 cli_close(cli, fnum);
3248 status = cli_qpathinfo1(cli, fname, &c_time, &a_time, &m_time, &size,
3249 NULL);
3250 if (!NT_STATUS_IS_OK(status)) {
3251 printf("ERROR: qpathinfo failed (%s)\n", nt_errstr(status));
3252 correct = False;
3253 } else {
3254 if (c_time != m_time) {
3255 printf("create time=%s", ctime(&c_time));
3256 printf("modify time=%s", ctime(&m_time));
3257 printf("This system appears to have sticky create times\n");
3259 if (a_time % (60*60) == 0) {
3260 printf("access time=%s", ctime(&a_time));
3261 printf("This system appears to set a midnight access time\n");
3262 correct = False;
3265 if (abs(m_time - time(NULL)) > 60*60*24*7) {
3266 printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
3267 correct = False;
3272 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3273 cli_open(cli, fname,
3274 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3275 cli_close(cli, fnum);
3276 status = cli_qpathinfo2(cli, fname, &c_time_ts, &a_time_ts, &w_time_ts,
3277 &m_time_ts, &size, NULL, NULL);
3278 if (!NT_STATUS_IS_OK(status)) {
3279 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3280 correct = False;
3281 } else {
3282 if (w_time_ts.tv_sec < 60*60*24*2) {
3283 printf("write time=%s", ctime(&w_time_ts.tv_sec));
3284 printf("This system appears to set a initial 0 write time\n");
3285 correct = False;
3289 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3292 /* check if the server updates the directory modification time
3293 when creating a new file */
3294 status = cli_mkdir(cli, dname);
3295 if (!NT_STATUS_IS_OK(status)) {
3296 printf("ERROR: mkdir failed (%s)\n", nt_errstr(status));
3297 correct = False;
3299 sleep(3);
3300 status = cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts,
3301 &w_time_ts, &m_time_ts, &size, NULL, NULL);
3302 if (!NT_STATUS_IS_OK(status)) {
3303 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3304 correct = False;
3307 cli_open(cli, fname2,
3308 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3309 cli_writeall(cli, fnum, 0, (uint8_t *)&fnum, 0, sizeof(fnum), NULL);
3310 cli_close(cli, fnum);
3311 status = cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts,
3312 &w_time_ts, &m_time2_ts, &size, NULL, NULL);
3313 if (!NT_STATUS_IS_OK(status)) {
3314 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3315 correct = False;
3316 } else {
3317 if (memcmp(&m_time_ts, &m_time2_ts, sizeof(struct timespec))
3318 == 0) {
3319 printf("This system does not update directory modification times\n");
3320 correct = False;
3323 cli_unlink(cli, fname2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3324 cli_rmdir(cli, dname);
3326 if (!torture_close_connection(cli)) {
3327 correct = False;
3330 printf("trans2 test finished\n");
3332 return correct;
3336 This checks new W2K calls.
3339 static NTSTATUS new_trans(struct cli_state *pcli, int fnum, int level)
3341 uint8_t *buf = NULL;
3342 uint32 len;
3343 NTSTATUS status;
3345 status = cli_qfileinfo(talloc_tos(), pcli, fnum, level, 0,
3346 pcli->max_xmit, NULL, &buf, &len);
3347 if (!NT_STATUS_IS_OK(status)) {
3348 printf("ERROR: qfileinfo (%d) failed (%s)\n", level,
3349 nt_errstr(status));
3350 } else {
3351 printf("qfileinfo: level %d, len = %u\n", level, len);
3352 dump_data(0, (uint8 *)buf, len);
3353 printf("\n");
3355 TALLOC_FREE(buf);
3356 return status;
3359 static bool run_w2ktest(int dummy)
3361 struct cli_state *cli;
3362 uint16_t fnum;
3363 const char *fname = "\\w2ktest\\w2k.tst";
3364 int level;
3365 bool correct = True;
3367 printf("starting w2k test\n");
3369 if (!torture_open_connection(&cli, 0)) {
3370 return False;
3373 cli_open(cli, fname,
3374 O_RDWR | O_CREAT , DENY_NONE, &fnum);
3376 for (level = 1004; level < 1040; level++) {
3377 new_trans(cli, fnum, level);
3380 cli_close(cli, fnum);
3382 if (!torture_close_connection(cli)) {
3383 correct = False;
3386 printf("w2k test finished\n");
3388 return correct;
3393 this is a harness for some oplock tests
3395 static bool run_oplock1(int dummy)
3397 struct cli_state *cli1;
3398 const char *fname = "\\lockt1.lck";
3399 uint16_t fnum1;
3400 bool correct = True;
3401 NTSTATUS status;
3403 printf("starting oplock test 1\n");
3405 if (!torture_open_connection(&cli1, 0)) {
3406 return False;
3409 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3411 cli_sockopt(cli1, sockops);
3413 cli1->use_oplocks = True;
3415 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
3416 &fnum1);
3417 if (!NT_STATUS_IS_OK(status)) {
3418 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3419 return False;
3422 cli1->use_oplocks = False;
3424 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3425 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3427 status = cli_close(cli1, fnum1);
3428 if (!NT_STATUS_IS_OK(status)) {
3429 printf("close2 failed (%s)\n", nt_errstr(status));
3430 return False;
3433 status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3434 if (!NT_STATUS_IS_OK(status)) {
3435 printf("unlink failed (%s)\n", nt_errstr(status));
3436 return False;
3439 if (!torture_close_connection(cli1)) {
3440 correct = False;
3443 printf("finished oplock test 1\n");
3445 return correct;
3448 static bool run_oplock2(int dummy)
3450 struct cli_state *cli1, *cli2;
3451 const char *fname = "\\lockt2.lck";
3452 uint16_t fnum1, fnum2;
3453 int saved_use_oplocks = use_oplocks;
3454 char buf[4];
3455 bool correct = True;
3456 volatile bool *shared_correct;
3457 NTSTATUS status;
3459 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
3460 *shared_correct = True;
3462 use_level_II_oplocks = True;
3463 use_oplocks = True;
3465 printf("starting oplock test 2\n");
3467 if (!torture_open_connection(&cli1, 0)) {
3468 use_level_II_oplocks = False;
3469 use_oplocks = saved_use_oplocks;
3470 return False;
3473 cli1->use_oplocks = True;
3474 cli1->use_level_II_oplocks = True;
3476 if (!torture_open_connection(&cli2, 1)) {
3477 use_level_II_oplocks = False;
3478 use_oplocks = saved_use_oplocks;
3479 return False;
3482 cli2->use_oplocks = True;
3483 cli2->use_level_II_oplocks = True;
3485 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3487 cli_sockopt(cli1, sockops);
3488 cli_sockopt(cli2, sockops);
3490 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
3491 &fnum1);
3492 if (!NT_STATUS_IS_OK(status)) {
3493 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3494 return False;
3497 /* Don't need the globals any more. */
3498 use_level_II_oplocks = False;
3499 use_oplocks = saved_use_oplocks;
3501 if (fork() == 0) {
3502 /* Child code */
3503 status = cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
3504 if (!NT_STATUS_IS_OK(status)) {
3505 printf("second open of %s failed (%s)\n", fname, nt_errstr(status));
3506 *shared_correct = False;
3507 exit(0);
3510 sleep(2);
3512 status = cli_close(cli2, fnum2);
3513 if (!NT_STATUS_IS_OK(status)) {
3514 printf("close2 failed (%s)\n", nt_errstr(status));
3515 *shared_correct = False;
3518 exit(0);
3521 sleep(2);
3523 /* Ensure cli1 processes the break. Empty file should always return 0
3524 * bytes. */
3526 if (cli_read(cli1, fnum1, buf, 0, 4) != 0) {
3527 printf("read on fnum1 failed (%s)\n", cli_errstr(cli1));
3528 correct = False;
3531 /* Should now be at level II. */
3532 /* Test if sending a write locks causes a break to none. */
3534 if (!cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK)) {
3535 printf("lock failed (%s)\n", cli_errstr(cli1));
3536 correct = False;
3539 cli_unlock(cli1, fnum1, 0, 4);
3541 sleep(2);
3543 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
3544 printf("lock failed (%s)\n", cli_errstr(cli1));
3545 correct = False;
3548 cli_unlock(cli1, fnum1, 0, 4);
3550 sleep(2);
3552 cli_read(cli1, fnum1, buf, 0, 4);
3554 status = cli_close(cli1, fnum1);
3555 if (!NT_STATUS_IS_OK(status)) {
3556 printf("close1 failed (%s)\n", nt_errstr(status));
3557 correct = False;
3560 sleep(4);
3562 status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3563 if (!NT_STATUS_IS_OK(status)) {
3564 printf("unlink failed (%s)\n", nt_errstr(status));
3565 correct = False;
3568 if (!torture_close_connection(cli1)) {
3569 correct = False;
3572 if (!*shared_correct) {
3573 correct = False;
3576 printf("finished oplock test 2\n");
3578 return correct;
3581 struct oplock4_state {
3582 struct tevent_context *ev;
3583 struct cli_state *cli;
3584 bool *got_break;
3585 uint16_t *fnum2;
3588 static void oplock4_got_break(struct tevent_req *req);
3589 static void oplock4_got_open(struct tevent_req *req);
3591 static bool run_oplock4(int dummy)
3593 struct tevent_context *ev;
3594 struct cli_state *cli1, *cli2;
3595 struct tevent_req *oplock_req, *open_req;
3596 const char *fname = "\\lockt4.lck";
3597 const char *fname_ln = "\\lockt4_ln.lck";
3598 uint16_t fnum1, fnum2;
3599 int saved_use_oplocks = use_oplocks;
3600 NTSTATUS status;
3601 bool correct = true;
3603 bool got_break;
3605 struct oplock4_state *state;
3607 printf("starting oplock test 4\n");
3609 if (!torture_open_connection(&cli1, 0)) {
3610 use_level_II_oplocks = false;
3611 use_oplocks = saved_use_oplocks;
3612 return false;
3615 if (!torture_open_connection(&cli2, 1)) {
3616 use_level_II_oplocks = false;
3617 use_oplocks = saved_use_oplocks;
3618 return false;
3621 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3622 cli_unlink(cli1, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3624 cli_sockopt(cli1, sockops);
3625 cli_sockopt(cli2, sockops);
3627 /* Create the file. */
3628 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
3629 &fnum1);
3630 if (!NT_STATUS_IS_OK(status)) {
3631 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3632 return false;
3635 status = cli_close(cli1, fnum1);
3636 if (!NT_STATUS_IS_OK(status)) {
3637 printf("close1 failed (%s)\n", nt_errstr(status));
3638 return false;
3641 /* Now create a hardlink. */
3642 status = cli_nt_hardlink(cli1, fname, fname_ln);
3643 if (!NT_STATUS_IS_OK(status)) {
3644 printf("nt hardlink failed (%s)\n", nt_errstr(status));
3645 return false;
3648 /* Prove that opening hardlinks cause deny modes to conflict. */
3649 status = cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum1);
3650 if (!NT_STATUS_IS_OK(status)) {
3651 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3652 return false;
3655 status = cli_open(cli1, fname_ln, O_RDWR, DENY_NONE, &fnum2);
3656 if (NT_STATUS_IS_OK(status)) {
3657 printf("open of %s succeeded - should fail with sharing violation.\n",
3658 fname_ln);
3659 return false;
3662 if (!NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
3663 printf("open of %s should fail with sharing violation. Got %s\n",
3664 fname_ln, nt_errstr(status));
3665 return false;
3668 status = cli_close(cli1, fnum1);
3669 if (!NT_STATUS_IS_OK(status)) {
3670 printf("close1 failed (%s)\n", nt_errstr(status));
3671 return false;
3674 cli1->use_oplocks = true;
3675 cli1->use_level_II_oplocks = true;
3677 cli2->use_oplocks = true;
3678 cli2->use_level_II_oplocks = true;
3680 status = cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
3681 if (!NT_STATUS_IS_OK(status)) {
3682 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3683 return false;
3686 ev = tevent_context_init(talloc_tos());
3687 if (ev == NULL) {
3688 printf("tevent_req_create failed\n");
3689 return false;
3692 state = talloc(ev, struct oplock4_state);
3693 if (state == NULL) {
3694 printf("talloc failed\n");
3695 return false;
3697 state->ev = ev;
3698 state->cli = cli1;
3699 state->got_break = &got_break;
3700 state->fnum2 = &fnum2;
3702 oplock_req = cli_smb_oplock_break_waiter_send(
3703 talloc_tos(), ev, cli1);
3704 if (oplock_req == NULL) {
3705 printf("cli_smb_oplock_break_waiter_send failed\n");
3706 return false;
3708 tevent_req_set_callback(oplock_req, oplock4_got_break, state);
3710 open_req = cli_open_send(
3711 talloc_tos(), ev, cli2, fname_ln, O_RDWR, DENY_NONE);
3712 if (oplock_req == NULL) {
3713 printf("cli_open_send failed\n");
3714 return false;
3716 tevent_req_set_callback(open_req, oplock4_got_open, state);
3718 got_break = false;
3719 fnum2 = 0xffff;
3721 while (!got_break || fnum2 == 0xffff) {
3722 int ret;
3723 ret = tevent_loop_once(ev);
3724 if (ret == -1) {
3725 printf("tevent_loop_once failed: %s\n",
3726 strerror(errno));
3727 return false;
3731 status = cli_close(cli2, fnum2);
3732 if (!NT_STATUS_IS_OK(status)) {
3733 printf("close2 failed (%s)\n", nt_errstr(status));
3734 correct = false;
3737 status = cli_close(cli1, fnum1);
3738 if (!NT_STATUS_IS_OK(status)) {
3739 printf("close1 failed (%s)\n", nt_errstr(status));
3740 correct = false;
3743 status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3744 if (!NT_STATUS_IS_OK(status)) {
3745 printf("unlink failed (%s)\n", nt_errstr(status));
3746 correct = false;
3749 status = cli_unlink(cli1, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3750 if (!NT_STATUS_IS_OK(status)) {
3751 printf("unlink failed (%s)\n", nt_errstr(status));
3752 correct = false;
3755 if (!torture_close_connection(cli1)) {
3756 correct = false;
3759 if (!got_break) {
3760 correct = false;
3763 printf("finished oplock test 4\n");
3765 return correct;
3768 static void oplock4_got_break(struct tevent_req *req)
3770 struct oplock4_state *state = tevent_req_callback_data(
3771 req, struct oplock4_state);
3772 uint16_t fnum;
3773 uint8_t level;
3774 NTSTATUS status;
3776 status = cli_smb_oplock_break_waiter_recv(req, &fnum, &level);
3777 TALLOC_FREE(req);
3778 if (!NT_STATUS_IS_OK(status)) {
3779 printf("cli_smb_oplock_break_waiter_recv returned %s\n",
3780 nt_errstr(status));
3781 return;
3783 *state->got_break = true;
3785 req = cli_oplock_ack_send(state, state->ev, state->cli, fnum,
3786 NO_OPLOCK);
3787 if (req == NULL) {
3788 printf("cli_oplock_ack_send failed\n");
3789 return;
3793 static void oplock4_got_open(struct tevent_req *req)
3795 struct oplock4_state *state = tevent_req_callback_data(
3796 req, struct oplock4_state);
3797 NTSTATUS status;
3799 status = cli_open_recv(req, state->fnum2);
3800 if (!NT_STATUS_IS_OK(status)) {
3801 printf("cli_open_recv returned %s\n", nt_errstr(status));
3802 *state->fnum2 = 0xffff;
3807 Test delete on close semantics.
3809 static bool run_deletetest(int dummy)
3811 struct cli_state *cli1 = NULL;
3812 struct cli_state *cli2 = NULL;
3813 const char *fname = "\\delete.file";
3814 uint16_t fnum1 = (uint16_t)-1;
3815 uint16_t fnum2 = (uint16_t)-1;
3816 bool correct = True;
3817 NTSTATUS status;
3819 printf("starting delete test\n");
3821 if (!torture_open_connection(&cli1, 0)) {
3822 return False;
3825 cli_sockopt(cli1, sockops);
3827 /* Test 1 - this should delete the file on close. */
3829 cli_setatr(cli1, fname, 0, 0);
3830 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3832 status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
3833 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
3834 FILE_DELETE_ON_CLOSE, 0, &fnum1);
3835 if (!NT_STATUS_IS_OK(status)) {
3836 printf("[1] open of %s failed (%s)\n", fname, nt_errstr(status));
3837 correct = False;
3838 goto fail;
3841 status = cli_close(cli1, fnum1);
3842 if (!NT_STATUS_IS_OK(status)) {
3843 printf("[1] close failed (%s)\n", nt_errstr(status));
3844 correct = False;
3845 goto fail;
3848 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
3849 printf("[1] open of %s succeeded (should fail)\n", fname);
3850 correct = False;
3851 goto fail;
3854 printf("first delete on close test succeeded.\n");
3856 /* Test 2 - this should delete the file on close. */
3858 cli_setatr(cli1, fname, 0, 0);
3859 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3861 status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
3862 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
3863 FILE_OVERWRITE_IF, 0, 0, &fnum1);
3864 if (!NT_STATUS_IS_OK(status)) {
3865 printf("[2] open of %s failed (%s)\n", fname, nt_errstr(status));
3866 correct = False;
3867 goto fail;
3870 status = cli_nt_delete_on_close(cli1, fnum1, true);
3871 if (!NT_STATUS_IS_OK(status)) {
3872 printf("[2] setting delete_on_close failed (%s)\n", nt_errstr(status));
3873 correct = False;
3874 goto fail;
3877 status = cli_close(cli1, fnum1);
3878 if (!NT_STATUS_IS_OK(status)) {
3879 printf("[2] close failed (%s)\n", nt_errstr(status));
3880 correct = False;
3881 goto fail;
3884 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3885 printf("[2] open of %s succeeded should have been deleted on close !\n", fname);
3886 status = cli_close(cli1, fnum1);
3887 if (!NT_STATUS_IS_OK(status)) {
3888 printf("[2] close failed (%s)\n", nt_errstr(status));
3889 correct = False;
3890 goto fail;
3892 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3893 } else
3894 printf("second delete on close test succeeded.\n");
3896 /* Test 3 - ... */
3897 cli_setatr(cli1, fname, 0, 0);
3898 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3900 status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
3901 FILE_ATTRIBUTE_NORMAL,
3902 FILE_SHARE_READ|FILE_SHARE_WRITE,
3903 FILE_OVERWRITE_IF, 0, 0, &fnum1);
3904 if (!NT_STATUS_IS_OK(status)) {
3905 printf("[3] open - 1 of %s failed (%s)\n", fname, nt_errstr(status));
3906 correct = False;
3907 goto fail;
3910 /* This should fail with a sharing violation - open for delete is only compatible
3911 with SHARE_DELETE. */
3913 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3914 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0, 0, &fnum2))) {
3915 printf("[3] open - 2 of %s succeeded - should have failed.\n", fname);
3916 correct = False;
3917 goto fail;
3920 /* This should succeed. */
3921 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3922 FILE_ATTRIBUTE_NORMAL,
3923 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3924 FILE_OPEN, 0, 0, &fnum2);
3925 if (!NT_STATUS_IS_OK(status)) {
3926 printf("[3] open - 2 of %s failed (%s)\n", fname, nt_errstr(status));
3927 correct = False;
3928 goto fail;
3931 status = cli_nt_delete_on_close(cli1, fnum1, true);
3932 if (!NT_STATUS_IS_OK(status)) {
3933 printf("[3] setting delete_on_close failed (%s)\n", nt_errstr(status));
3934 correct = False;
3935 goto fail;
3938 status = cli_close(cli1, fnum1);
3939 if (!NT_STATUS_IS_OK(status)) {
3940 printf("[3] close 1 failed (%s)\n", nt_errstr(status));
3941 correct = False;
3942 goto fail;
3945 status = cli_close(cli1, fnum2);
3946 if (!NT_STATUS_IS_OK(status)) {
3947 printf("[3] close 2 failed (%s)\n", nt_errstr(status));
3948 correct = False;
3949 goto fail;
3952 /* This should fail - file should no longer be there. */
3954 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3955 printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
3956 status = cli_close(cli1, fnum1);
3957 if (!NT_STATUS_IS_OK(status)) {
3958 printf("[3] close failed (%s)\n", nt_errstr(status));
3960 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3961 correct = False;
3962 goto fail;
3963 } else
3964 printf("third delete on close test succeeded.\n");
3966 /* Test 4 ... */
3967 cli_setatr(cli1, fname, 0, 0);
3968 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3970 status = cli_ntcreate(cli1, fname, 0,
3971 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3972 FILE_ATTRIBUTE_NORMAL,
3973 FILE_SHARE_READ|FILE_SHARE_WRITE,
3974 FILE_OVERWRITE_IF, 0, 0, &fnum1);
3975 if (!NT_STATUS_IS_OK(status)) {
3976 printf("[4] open of %s failed (%s)\n", fname, nt_errstr(status));
3977 correct = False;
3978 goto fail;
3981 /* This should succeed. */
3982 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3983 FILE_ATTRIBUTE_NORMAL,
3984 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3985 FILE_OPEN, 0, 0, &fnum2);
3986 if (!NT_STATUS_IS_OK(status)) {
3987 printf("[4] open - 2 of %s failed (%s)\n", fname, nt_errstr(status));
3988 correct = False;
3989 goto fail;
3992 status = cli_close(cli1, fnum2);
3993 if (!NT_STATUS_IS_OK(status)) {
3994 printf("[4] close - 1 failed (%s)\n", nt_errstr(status));
3995 correct = False;
3996 goto fail;
3999 status = cli_nt_delete_on_close(cli1, fnum1, true);
4000 if (!NT_STATUS_IS_OK(status)) {
4001 printf("[4] setting delete_on_close failed (%s)\n", nt_errstr(status));
4002 correct = False;
4003 goto fail;
4006 /* This should fail - no more opens once delete on close set. */
4007 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
4008 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4009 FILE_OPEN, 0, 0, &fnum2))) {
4010 printf("[4] open - 3 of %s succeeded ! Should have failed.\n", fname );
4011 correct = False;
4012 goto fail;
4013 } else
4014 printf("fourth delete on close test succeeded.\n");
4016 status = cli_close(cli1, fnum1);
4017 if (!NT_STATUS_IS_OK(status)) {
4018 printf("[4] close - 2 failed (%s)\n", nt_errstr(status));
4019 correct = False;
4020 goto fail;
4023 /* Test 5 ... */
4024 cli_setatr(cli1, fname, 0, 0);
4025 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4027 status = cli_open(cli1, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum1);
4028 if (!NT_STATUS_IS_OK(status)) {
4029 printf("[5] open of %s failed (%s)\n", fname, nt_errstr(status));
4030 correct = False;
4031 goto fail;
4034 /* This should fail - only allowed on NT opens with DELETE access. */
4036 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
4037 printf("[5] setting delete_on_close on OpenX file succeeded - should fail !\n");
4038 correct = False;
4039 goto fail;
4042 status = cli_close(cli1, fnum1);
4043 if (!NT_STATUS_IS_OK(status)) {
4044 printf("[5] close - 2 failed (%s)\n", nt_errstr(status));
4045 correct = False;
4046 goto fail;
4049 printf("fifth delete on close test succeeded.\n");
4051 /* Test 6 ... */
4052 cli_setatr(cli1, fname, 0, 0);
4053 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4055 status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
4056 FILE_ATTRIBUTE_NORMAL,
4057 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4058 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4059 if (!NT_STATUS_IS_OK(status)) {
4060 printf("[6] open of %s failed (%s)\n", fname,
4061 nt_errstr(status));
4062 correct = False;
4063 goto fail;
4066 /* This should fail - only allowed on NT opens with DELETE access. */
4068 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
4069 printf("[6] setting delete_on_close on file with no delete access succeeded - should fail !\n");
4070 correct = False;
4071 goto fail;
4074 status = cli_close(cli1, fnum1);
4075 if (!NT_STATUS_IS_OK(status)) {
4076 printf("[6] close - 2 failed (%s)\n", nt_errstr(status));
4077 correct = False;
4078 goto fail;
4081 printf("sixth delete on close test succeeded.\n");
4083 /* Test 7 ... */
4084 cli_setatr(cli1, fname, 0, 0);
4085 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4087 status = cli_ntcreate(cli1, fname, 0,
4088 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
4089 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
4090 0, 0, &fnum1);
4091 if (!NT_STATUS_IS_OK(status)) {
4092 printf("[7] open of %s failed (%s)\n", fname, nt_errstr(status));
4093 correct = False;
4094 goto fail;
4097 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
4098 printf("[7] setting delete_on_close on file failed !\n");
4099 correct = False;
4100 goto fail;
4103 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, false))) {
4104 printf("[7] unsetting delete_on_close on file failed !\n");
4105 correct = False;
4106 goto fail;
4109 status = cli_close(cli1, fnum1);
4110 if (!NT_STATUS_IS_OK(status)) {
4111 printf("[7] close - 2 failed (%s)\n", nt_errstr(status));
4112 correct = False;
4113 goto fail;
4116 /* This next open should succeed - we reset the flag. */
4117 status = cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1);
4118 if (!NT_STATUS_IS_OK(status)) {
4119 printf("[5] open of %s failed (%s)\n", fname, nt_errstr(status));
4120 correct = False;
4121 goto fail;
4124 status = cli_close(cli1, fnum1);
4125 if (!NT_STATUS_IS_OK(status)) {
4126 printf("[7] close - 2 failed (%s)\n", nt_errstr(status));
4127 correct = False;
4128 goto fail;
4131 printf("seventh delete on close test succeeded.\n");
4133 /* Test 7 ... */
4134 cli_setatr(cli1, fname, 0, 0);
4135 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4137 if (!torture_open_connection(&cli2, 1)) {
4138 printf("[8] failed to open second connection.\n");
4139 correct = False;
4140 goto fail;
4143 cli_sockopt(cli1, sockops);
4145 status = cli_ntcreate(cli1, fname, 0,
4146 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
4147 FILE_ATTRIBUTE_NORMAL,
4148 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4149 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4150 if (!NT_STATUS_IS_OK(status)) {
4151 printf("[8] open 1 of %s failed (%s)\n", fname, nt_errstr(status));
4152 correct = False;
4153 goto fail;
4156 status = cli_ntcreate(cli2, fname, 0,
4157 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
4158 FILE_ATTRIBUTE_NORMAL,
4159 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4160 FILE_OPEN, 0, 0, &fnum2);
4161 if (!NT_STATUS_IS_OK(status)) {
4162 printf("[8] open 2 of %s failed (%s)\n", fname, nt_errstr(status));
4163 correct = False;
4164 goto fail;
4167 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
4168 printf("[8] setting delete_on_close on file failed !\n");
4169 correct = False;
4170 goto fail;
4173 status = cli_close(cli1, fnum1);
4174 if (!NT_STATUS_IS_OK(status)) {
4175 printf("[8] close - 1 failed (%s)\n", nt_errstr(status));
4176 correct = False;
4177 goto fail;
4180 status = cli_close(cli2, fnum2);
4181 if (!NT_STATUS_IS_OK(status)) {
4182 printf("[8] close - 2 failed (%s)\n", nt_errstr(status));
4183 correct = False;
4184 goto fail;
4187 /* This should fail.. */
4188 status = cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1);
4189 if (NT_STATUS_IS_OK(status)) {
4190 printf("[8] open of %s succeeded should have been deleted on close !\n", fname);
4191 goto fail;
4192 correct = False;
4193 } else
4194 printf("eighth delete on close test succeeded.\n");
4196 /* This should fail - we need to set DELETE_ACCESS. */
4197 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,FILE_READ_DATA|FILE_WRITE_DATA,
4198 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
4199 printf("[9] open of %s succeeded should have failed!\n", fname);
4200 correct = False;
4201 goto fail;
4204 printf("ninth delete on close test succeeded.\n");
4206 status = cli_ntcreate(cli1, fname, 0,
4207 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
4208 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4209 FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE,
4210 0, &fnum1);
4211 if (!NT_STATUS_IS_OK(status)) {
4212 printf("[10] open of %s failed (%s)\n", fname, nt_errstr(status));
4213 correct = False;
4214 goto fail;
4217 /* This should delete the file. */
4218 status = cli_close(cli1, fnum1);
4219 if (!NT_STATUS_IS_OK(status)) {
4220 printf("[10] close failed (%s)\n", nt_errstr(status));
4221 correct = False;
4222 goto fail;
4225 /* This should fail.. */
4226 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
4227 printf("[10] open of %s succeeded should have been deleted on close !\n", fname);
4228 goto fail;
4229 correct = False;
4230 } else
4231 printf("tenth delete on close test succeeded.\n");
4233 cli_setatr(cli1, fname, 0, 0);
4234 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4236 /* What error do we get when attempting to open a read-only file with
4237 delete access ? */
4239 /* Create a readonly file. */
4240 status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
4241 FILE_ATTRIBUTE_READONLY, FILE_SHARE_NONE,
4242 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4243 if (!NT_STATUS_IS_OK(status)) {
4244 printf("[11] open of %s failed (%s)\n", fname, nt_errstr(status));
4245 correct = False;
4246 goto fail;
4249 status = cli_close(cli1, fnum1);
4250 if (!NT_STATUS_IS_OK(status)) {
4251 printf("[11] close failed (%s)\n", nt_errstr(status));
4252 correct = False;
4253 goto fail;
4256 /* Now try open for delete access. */
4257 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES|DELETE_ACCESS,
4258 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4259 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4260 printf("[11] open of %s succeeded should have been denied with ACCESS_DENIED!\n", fname);
4261 cli_close(cli1, fnum1);
4262 goto fail;
4263 correct = False;
4264 } else {
4265 NTSTATUS nterr = cli_nt_error(cli1);
4266 if (!NT_STATUS_EQUAL(nterr,NT_STATUS_ACCESS_DENIED)) {
4267 printf("[11] open of %s should have been denied with ACCESS_DENIED! Got error %s\n", fname, nt_errstr(nterr));
4268 goto fail;
4269 correct = False;
4270 } else {
4271 printf("eleventh delete on close test succeeded.\n");
4275 printf("finished delete test\n");
4277 fail:
4278 /* FIXME: This will crash if we aborted before cli2 got
4279 * intialized, because these functions don't handle
4280 * uninitialized connections. */
4282 if (fnum1 != (uint16_t)-1) cli_close(cli1, fnum1);
4283 if (fnum2 != (uint16_t)-1) cli_close(cli1, fnum2);
4284 cli_setatr(cli1, fname, 0, 0);
4285 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4287 if (cli1 && !torture_close_connection(cli1)) {
4288 correct = False;
4290 if (cli2 && !torture_close_connection(cli2)) {
4291 correct = False;
4293 return correct;
4296 static bool run_deletetest_ln(int dummy)
4298 struct cli_state *cli;
4299 const char *fname = "\\delete1";
4300 const char *fname_ln = "\\delete1_ln";
4301 uint16_t fnum;
4302 uint16_t fnum1;
4303 NTSTATUS status;
4304 bool correct = true;
4305 time_t t;
4307 printf("starting deletetest-ln\n");
4309 if (!torture_open_connection(&cli, 0)) {
4310 return false;
4313 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4314 cli_unlink(cli, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4316 cli_sockopt(cli, sockops);
4318 /* Create the file. */
4319 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
4320 if (!NT_STATUS_IS_OK(status)) {
4321 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
4322 return false;
4325 status = cli_close(cli, fnum);
4326 if (!NT_STATUS_IS_OK(status)) {
4327 printf("close1 failed (%s)\n", nt_errstr(status));
4328 return false;
4331 /* Now create a hardlink. */
4332 status = cli_nt_hardlink(cli, fname, fname_ln);
4333 if (!NT_STATUS_IS_OK(status)) {
4334 printf("nt hardlink failed (%s)\n", nt_errstr(status));
4335 return false;
4338 /* Open the original file. */
4339 status = cli_ntcreate(cli, fname, 0, FILE_READ_DATA,
4340 FILE_ATTRIBUTE_NORMAL,
4341 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4342 FILE_OPEN_IF, 0, 0, &fnum);
4343 if (!NT_STATUS_IS_OK(status)) {
4344 printf("ntcreate of %s failed (%s)\n", fname, nt_errstr(status));
4345 return false;
4348 /* Unlink the hard link path. */
4349 status = cli_ntcreate(cli, fname_ln, 0, DELETE_ACCESS,
4350 FILE_ATTRIBUTE_NORMAL,
4351 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4352 FILE_OPEN_IF, 0, 0, &fnum1);
4353 if (!NT_STATUS_IS_OK(status)) {
4354 printf("ntcreate of %s failed (%s)\n", fname_ln, nt_errstr(status));
4355 return false;
4357 status = cli_nt_delete_on_close(cli, fnum1, true);
4358 if (!NT_STATUS_IS_OK(status)) {
4359 d_printf("(%s) failed to set delete_on_close %s: %s\n",
4360 __location__, fname_ln, nt_errstr(status));
4361 return false;
4364 status = cli_close(cli, fnum1);
4365 if (!NT_STATUS_IS_OK(status)) {
4366 printf("close %s failed (%s)\n",
4367 fname_ln, nt_errstr(status));
4368 return false;
4371 status = cli_close(cli, fnum);
4372 if (!NT_STATUS_IS_OK(status)) {
4373 printf("close %s failed (%s)\n",
4374 fname, nt_errstr(status));
4375 return false;
4378 /* Ensure the original file is still there. */
4379 status = cli_getatr(cli, fname, NULL, NULL, &t);
4380 if (!NT_STATUS_IS_OK(status)) {
4381 printf("%s getatr on file %s failed (%s)\n",
4382 __location__,
4383 fname,
4384 nt_errstr(status));
4385 correct = False;
4388 /* Ensure the link path is gone. */
4389 status = cli_getatr(cli, fname_ln, NULL, NULL, &t);
4390 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
4391 printf("%s, getatr for file %s returned wrong error code %s "
4392 "- should have been deleted\n",
4393 __location__,
4394 fname_ln, nt_errstr(status));
4395 correct = False;
4398 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4399 cli_unlink(cli, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4401 if (!torture_close_connection(cli)) {
4402 correct = false;
4405 printf("finished deletetest-ln\n");
4407 return correct;
4411 print out server properties
4413 static bool run_properties(int dummy)
4415 struct cli_state *cli;
4416 bool correct = True;
4418 printf("starting properties test\n");
4420 ZERO_STRUCT(cli);
4422 if (!torture_open_connection(&cli, 0)) {
4423 return False;
4426 cli_sockopt(cli, sockops);
4428 d_printf("Capabilities 0x%08x\n", cli->capabilities);
4430 if (!torture_close_connection(cli)) {
4431 correct = False;
4434 return correct;
4439 /* FIRST_DESIRED_ACCESS 0xf019f */
4440 #define FIRST_DESIRED_ACCESS FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|\
4441 FILE_READ_EA| /* 0xf */ \
4442 FILE_WRITE_EA|FILE_READ_ATTRIBUTES| /* 0x90 */ \
4443 FILE_WRITE_ATTRIBUTES| /* 0x100 */ \
4444 DELETE_ACCESS|READ_CONTROL_ACCESS|\
4445 WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS /* 0xf0000 */
4446 /* SECOND_DESIRED_ACCESS 0xe0080 */
4447 #define SECOND_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
4448 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
4449 WRITE_OWNER_ACCESS /* 0xe0000 */
4451 #if 0
4452 #define THIRD_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
4453 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
4454 FILE_READ_DATA|\
4455 WRITE_OWNER_ACCESS /* */
4456 #endif
4459 Test ntcreate calls made by xcopy
4461 static bool run_xcopy(int dummy)
4463 static struct cli_state *cli1;
4464 const char *fname = "\\test.txt";
4465 bool correct = True;
4466 uint16_t fnum1, fnum2;
4467 NTSTATUS status;
4469 printf("starting xcopy test\n");
4471 if (!torture_open_connection(&cli1, 0)) {
4472 return False;
4475 status = cli_ntcreate(cli1, fname, 0, FIRST_DESIRED_ACCESS,
4476 FILE_ATTRIBUTE_ARCHIVE, FILE_SHARE_NONE,
4477 FILE_OVERWRITE_IF, 0x4044, 0, &fnum1);
4478 if (!NT_STATUS_IS_OK(status)) {
4479 printf("First open failed - %s\n", nt_errstr(status));
4480 return False;
4483 status = cli_ntcreate(cli1, fname, 0, SECOND_DESIRED_ACCESS, 0,
4484 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4485 FILE_OPEN, 0x200000, 0, &fnum2);
4486 if (!NT_STATUS_IS_OK(status)) {
4487 printf("second open failed - %s\n", nt_errstr(status));
4488 return False;
4491 if (!torture_close_connection(cli1)) {
4492 correct = False;
4495 return correct;
4499 Test rename on files open with share delete and no share delete.
4501 static bool run_rename(int dummy)
4503 static struct cli_state *cli1;
4504 const char *fname = "\\test.txt";
4505 const char *fname1 = "\\test1.txt";
4506 bool correct = True;
4507 uint16_t fnum1;
4508 uint16_t attr;
4509 NTSTATUS status;
4511 printf("starting rename test\n");
4513 if (!torture_open_connection(&cli1, 0)) {
4514 return False;
4517 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4518 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4520 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
4521 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
4522 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4523 if (!NT_STATUS_IS_OK(status)) {
4524 printf("First open failed - %s\n", nt_errstr(status));
4525 return False;
4528 status = cli_rename(cli1, fname, fname1);
4529 if (!NT_STATUS_IS_OK(status)) {
4530 printf("First rename failed (SHARE_READ) (this is correct) - %s\n", nt_errstr(status));
4531 } else {
4532 printf("First rename succeeded (SHARE_READ) - this should have failed !\n");
4533 correct = False;
4536 status = cli_close(cli1, fnum1);
4537 if (!NT_STATUS_IS_OK(status)) {
4538 printf("close - 1 failed (%s)\n", nt_errstr(status));
4539 return False;
4542 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4543 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4544 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4545 #if 0
4546 FILE_SHARE_DELETE|FILE_SHARE_NONE,
4547 #else
4548 FILE_SHARE_DELETE|FILE_SHARE_READ,
4549 #endif
4550 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4551 if (!NT_STATUS_IS_OK(status)) {
4552 printf("Second open failed - %s\n", nt_errstr(status));
4553 return False;
4556 status = cli_rename(cli1, fname, fname1);
4557 if (!NT_STATUS_IS_OK(status)) {
4558 printf("Second rename failed (SHARE_DELETE | SHARE_READ) - this should have succeeded - %s\n", nt_errstr(status));
4559 correct = False;
4560 } else {
4561 printf("Second rename succeeded (SHARE_DELETE | SHARE_READ)\n");
4564 status = cli_close(cli1, fnum1);
4565 if (!NT_STATUS_IS_OK(status)) {
4566 printf("close - 2 failed (%s)\n", nt_errstr(status));
4567 return False;
4570 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4571 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4573 status = cli_ntcreate(cli1, fname, 0, READ_CONTROL_ACCESS,
4574 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4575 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4576 if (!NT_STATUS_IS_OK(status)) {
4577 printf("Third open failed - %s\n", nt_errstr(status));
4578 return False;
4582 #if 0
4584 uint16_t fnum2;
4586 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
4587 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
4588 printf("Fourth open failed - %s\n", cli_errstr(cli1));
4589 return False;
4591 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum2, true))) {
4592 printf("[8] setting delete_on_close on file failed !\n");
4593 return False;
4596 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
4597 printf("close - 4 failed (%s)\n", cli_errstr(cli1));
4598 return False;
4601 #endif
4603 status = cli_rename(cli1, fname, fname1);
4604 if (!NT_STATUS_IS_OK(status)) {
4605 printf("Third rename failed (SHARE_NONE) - this should have succeeded - %s\n", nt_errstr(status));
4606 correct = False;
4607 } else {
4608 printf("Third rename succeeded (SHARE_NONE)\n");
4611 status = cli_close(cli1, fnum1);
4612 if (!NT_STATUS_IS_OK(status)) {
4613 printf("close - 3 failed (%s)\n", nt_errstr(status));
4614 return False;
4617 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4618 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4620 /*----*/
4622 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
4623 FILE_ATTRIBUTE_NORMAL,
4624 FILE_SHARE_READ | FILE_SHARE_WRITE,
4625 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4626 if (!NT_STATUS_IS_OK(status)) {
4627 printf("Fourth open failed - %s\n", nt_errstr(status));
4628 return False;
4631 status = cli_rename(cli1, fname, fname1);
4632 if (!NT_STATUS_IS_OK(status)) {
4633 printf("Fourth rename failed (SHARE_READ | SHARE_WRITE) (this is correct) - %s\n", nt_errstr(status));
4634 } else {
4635 printf("Fourth rename succeeded (SHARE_READ | SHARE_WRITE) - this should have failed !\n");
4636 correct = False;
4639 status = cli_close(cli1, fnum1);
4640 if (!NT_STATUS_IS_OK(status)) {
4641 printf("close - 4 failed (%s)\n", nt_errstr(status));
4642 return False;
4645 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4646 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4648 /*--*/
4650 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
4651 FILE_ATTRIBUTE_NORMAL,
4652 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4653 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4654 if (!NT_STATUS_IS_OK(status)) {
4655 printf("Fifth open failed - %s\n", nt_errstr(status));
4656 return False;
4659 status = cli_rename(cli1, fname, fname1);
4660 if (!NT_STATUS_IS_OK(status)) {
4661 printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have succeeded - %s ! \n", nt_errstr(status));
4662 correct = False;
4663 } else {
4664 printf("Fifth rename succeeded (SHARE_READ | SHARE_WRITE | SHARE_DELETE) (this is correct) - %s\n", nt_errstr(status));
4668 * Now check if the first name still exists ...
4671 /* if (!NT_STATUS_OP(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4672 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
4673 printf("Opening original file after rename of open file fails: %s\n",
4674 cli_errstr(cli1));
4676 else {
4677 printf("Opening original file after rename of open file works ...\n");
4678 (void)cli_close(cli1, fnum2);
4679 } */
4681 /*--*/
4682 status = cli_close(cli1, fnum1);
4683 if (!NT_STATUS_IS_OK(status)) {
4684 printf("close - 5 failed (%s)\n", nt_errstr(status));
4685 return False;
4688 /* Check that the renamed file has FILE_ATTRIBUTE_ARCHIVE. */
4689 status = cli_getatr(cli1, fname1, &attr, NULL, NULL);
4690 if (!NT_STATUS_IS_OK(status)) {
4691 printf("getatr on file %s failed - %s ! \n",
4692 fname1, nt_errstr(status));
4693 correct = False;
4694 } else {
4695 if (attr != FILE_ATTRIBUTE_ARCHIVE) {
4696 printf("Renamed file %s has wrong attr 0x%x "
4697 "(should be 0x%x)\n",
4698 fname1,
4699 attr,
4700 (unsigned int)FILE_ATTRIBUTE_ARCHIVE);
4701 correct = False;
4702 } else {
4703 printf("Renamed file %s has archive bit set\n", fname1);
4707 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4708 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4710 if (!torture_close_connection(cli1)) {
4711 correct = False;
4714 return correct;
4717 static bool run_pipe_number(int dummy)
4719 struct cli_state *cli1;
4720 const char *pipe_name = "\\SPOOLSS";
4721 uint16_t fnum;
4722 int num_pipes = 0;
4723 NTSTATUS status;
4725 printf("starting pipenumber test\n");
4726 if (!torture_open_connection(&cli1, 0)) {
4727 return False;
4730 cli_sockopt(cli1, sockops);
4731 while(1) {
4732 status = cli_ntcreate(cli1, pipe_name, 0, FILE_READ_DATA,
4733 FILE_ATTRIBUTE_NORMAL,
4734 FILE_SHARE_READ|FILE_SHARE_WRITE,
4735 FILE_OPEN_IF, 0, 0, &fnum);
4736 if (!NT_STATUS_IS_OK(status)) {
4737 printf("Open of pipe %s failed with error (%s)\n", pipe_name, nt_errstr(status));
4738 break;
4740 num_pipes++;
4741 printf("\r%6d", num_pipes);
4744 printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
4745 torture_close_connection(cli1);
4746 return True;
4750 Test open mode returns on read-only files.
4752 static bool run_opentest(int dummy)
4754 static struct cli_state *cli1;
4755 static struct cli_state *cli2;
4756 const char *fname = "\\readonly.file";
4757 uint16_t fnum1, fnum2;
4758 char buf[20];
4759 SMB_OFF_T fsize;
4760 bool correct = True;
4761 char *tmp_path;
4762 NTSTATUS status;
4764 printf("starting open test\n");
4766 if (!torture_open_connection(&cli1, 0)) {
4767 return False;
4770 cli_setatr(cli1, fname, 0, 0);
4771 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4773 cli_sockopt(cli1, sockops);
4775 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
4776 if (!NT_STATUS_IS_OK(status)) {
4777 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
4778 return False;
4781 status = cli_close(cli1, fnum1);
4782 if (!NT_STATUS_IS_OK(status)) {
4783 printf("close2 failed (%s)\n", nt_errstr(status));
4784 return False;
4787 status = cli_setatr(cli1, fname, FILE_ATTRIBUTE_READONLY, 0);
4788 if (!NT_STATUS_IS_OK(status)) {
4789 printf("cli_setatr failed (%s)\n", nt_errstr(status));
4790 return False;
4793 status = cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1);
4794 if (!NT_STATUS_IS_OK(status)) {
4795 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
4796 return False;
4799 /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
4800 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4802 if (check_error(__LINE__, cli1, ERRDOS, ERRnoaccess,
4803 NT_STATUS_ACCESS_DENIED)) {
4804 printf("correct error code ERRDOS/ERRnoaccess returned\n");
4807 printf("finished open test 1\n");
4809 cli_close(cli1, fnum1);
4811 /* Now try not readonly and ensure ERRbadshare is returned. */
4813 cli_setatr(cli1, fname, 0, 0);
4815 status = cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1);
4816 if (!NT_STATUS_IS_OK(status)) {
4817 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
4818 return False;
4821 /* This will fail - but the error should be ERRshare. */
4822 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4824 if (check_error(__LINE__, cli1, ERRDOS, ERRbadshare,
4825 NT_STATUS_SHARING_VIOLATION)) {
4826 printf("correct error code ERRDOS/ERRbadshare returned\n");
4829 status = cli_close(cli1, fnum1);
4830 if (!NT_STATUS_IS_OK(status)) {
4831 printf("close2 failed (%s)\n", nt_errstr(status));
4832 return False;
4835 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4837 printf("finished open test 2\n");
4839 /* Test truncate open disposition on file opened for read. */
4840 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
4841 if (!NT_STATUS_IS_OK(status)) {
4842 printf("(3) open (1) of %s failed (%s)\n", fname, nt_errstr(status));
4843 return False;
4846 /* write 20 bytes. */
4848 memset(buf, '\0', 20);
4850 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, 20, NULL);
4851 if (!NT_STATUS_IS_OK(status)) {
4852 printf("write failed (%s)\n", nt_errstr(status));
4853 correct = False;
4856 status = cli_close(cli1, fnum1);
4857 if (!NT_STATUS_IS_OK(status)) {
4858 printf("(3) close1 failed (%s)\n", nt_errstr(status));
4859 return False;
4862 /* Ensure size == 20. */
4863 status = cli_getatr(cli1, fname, NULL, &fsize, NULL);
4864 if (!NT_STATUS_IS_OK(status)) {
4865 printf("(3) getatr failed (%s)\n", nt_errstr(status));
4866 return False;
4869 if (fsize != 20) {
4870 printf("(3) file size != 20\n");
4871 return False;
4874 /* Now test if we can truncate a file opened for readonly. */
4875 status = cli_open(cli1, fname, O_RDONLY|O_TRUNC, DENY_NONE, &fnum1);
4876 if (!NT_STATUS_IS_OK(status)) {
4877 printf("(3) open (2) of %s failed (%s)\n", fname, nt_errstr(status));
4878 return False;
4881 status = cli_close(cli1, fnum1);
4882 if (!NT_STATUS_IS_OK(status)) {
4883 printf("close2 failed (%s)\n", nt_errstr(status));
4884 return False;
4887 /* Ensure size == 0. */
4888 status = cli_getatr(cli1, fname, NULL, &fsize, NULL);
4889 if (!NT_STATUS_IS_OK(status)) {
4890 printf("(3) getatr failed (%s)\n", nt_errstr(status));
4891 return False;
4894 if (fsize != 0) {
4895 printf("(3) file size != 0\n");
4896 return False;
4898 printf("finished open test 3\n");
4900 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4902 printf("Do ctemp tests\n");
4903 status = cli_ctemp(cli1, talloc_tos(), "\\", &fnum1, &tmp_path);
4904 if (!NT_STATUS_IS_OK(status)) {
4905 printf("ctemp failed (%s)\n", nt_errstr(status));
4906 return False;
4909 printf("ctemp gave path %s\n", tmp_path);
4910 status = cli_close(cli1, fnum1);
4911 if (!NT_STATUS_IS_OK(status)) {
4912 printf("close of temp failed (%s)\n", nt_errstr(status));
4915 status = cli_unlink(cli1, tmp_path, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4916 if (!NT_STATUS_IS_OK(status)) {
4917 printf("unlink of temp failed (%s)\n", nt_errstr(status));
4920 /* Test the non-io opens... */
4922 if (!torture_open_connection(&cli2, 1)) {
4923 return False;
4926 cli_setatr(cli2, fname, 0, 0);
4927 cli_unlink(cli2, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4929 cli_sockopt(cli2, sockops);
4931 printf("TEST #1 testing 2 non-io opens (no delete)\n");
4932 status = cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES,
4933 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4934 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4935 if (!NT_STATUS_IS_OK(status)) {
4936 printf("TEST #1 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
4937 return False;
4940 status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
4941 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4942 FILE_OPEN_IF, 0, 0, &fnum2);
4943 if (!NT_STATUS_IS_OK(status)) {
4944 printf("TEST #1 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
4945 return False;
4948 status = cli_close(cli1, fnum1);
4949 if (!NT_STATUS_IS_OK(status)) {
4950 printf("TEST #1 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
4951 return False;
4954 status = cli_close(cli2, fnum2);
4955 if (!NT_STATUS_IS_OK(status)) {
4956 printf("TEST #1 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
4957 return False;
4960 printf("non-io open test #1 passed.\n");
4962 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4964 printf("TEST #2 testing 2 non-io opens (first with delete)\n");
4966 status = cli_ntcreate(cli1, fname, 0,
4967 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
4968 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4969 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4970 if (!NT_STATUS_IS_OK(status)) {
4971 printf("TEST #2 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
4972 return False;
4975 status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
4976 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4977 FILE_OPEN_IF, 0, 0, &fnum2);
4978 if (!NT_STATUS_IS_OK(status)) {
4979 printf("TEST #2 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
4980 return False;
4983 status = cli_close(cli1, fnum1);
4984 if (!NT_STATUS_IS_OK(status)) {
4985 printf("TEST #2 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
4986 return False;
4989 status = cli_close(cli2, fnum2);
4990 if (!NT_STATUS_IS_OK(status)) {
4991 printf("TEST #2 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
4992 return False;
4995 printf("non-io open test #2 passed.\n");
4997 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4999 printf("TEST #3 testing 2 non-io opens (second with delete)\n");
5001 status = cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES,
5002 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5003 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5004 if (!NT_STATUS_IS_OK(status)) {
5005 printf("TEST #3 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
5006 return False;
5009 status = cli_ntcreate(cli2, fname, 0,
5010 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5011 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5012 FILE_OPEN_IF, 0, 0, &fnum2);
5013 if (!NT_STATUS_IS_OK(status)) {
5014 printf("TEST #3 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
5015 return False;
5018 status = cli_close(cli1, fnum1);
5019 if (!NT_STATUS_IS_OK(status)) {
5020 printf("TEST #3 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
5021 return False;
5024 status = cli_close(cli2, fnum2);
5025 if (!NT_STATUS_IS_OK(status)) {
5026 printf("TEST #3 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
5027 return False;
5030 printf("non-io open test #3 passed.\n");
5032 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5034 printf("TEST #4 testing 2 non-io opens (both with delete)\n");
5036 status = cli_ntcreate(cli1, fname, 0,
5037 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5038 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5039 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5040 if (!NT_STATUS_IS_OK(status)) {
5041 printf("TEST #4 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
5042 return False;
5045 status = cli_ntcreate(cli2, fname, 0,
5046 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5047 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5048 FILE_OPEN_IF, 0, 0, &fnum2);
5049 if (NT_STATUS_IS_OK(status)) {
5050 printf("TEST #4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, nt_errstr(status));
5051 return False;
5054 printf("TEST #4 open 2 of %s gave %s (correct error should be %s)\n", fname, nt_errstr(status), "sharing violation");
5056 status = cli_close(cli1, fnum1);
5057 if (!NT_STATUS_IS_OK(status)) {
5058 printf("TEST #4 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
5059 return False;
5062 printf("non-io open test #4 passed.\n");
5064 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5066 printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
5068 status = cli_ntcreate(cli1, fname, 0,
5069 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5070 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_DELETE,
5071 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5072 if (!NT_STATUS_IS_OK(status)) {
5073 printf("TEST #5 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
5074 return False;
5077 status = cli_ntcreate(cli2, fname, 0,
5078 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5079 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_DELETE,
5080 FILE_OPEN_IF, 0, 0, &fnum2);
5081 if (!NT_STATUS_IS_OK(status)) {
5082 printf("TEST #5 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
5083 return False;
5086 status = cli_close(cli1, fnum1);
5087 if (!NT_STATUS_IS_OK(status)) {
5088 printf("TEST #5 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
5089 return False;
5092 status = cli_close(cli2, fnum2);
5093 if (!NT_STATUS_IS_OK(status)) {
5094 printf("TEST #5 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
5095 return False;
5098 printf("non-io open test #5 passed.\n");
5100 printf("TEST #6 testing 1 non-io open, one io open\n");
5102 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5104 status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA,
5105 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5106 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5107 if (!NT_STATUS_IS_OK(status)) {
5108 printf("TEST #6 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
5109 return False;
5112 status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
5113 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
5114 FILE_OPEN_IF, 0, 0, &fnum2);
5115 if (!NT_STATUS_IS_OK(status)) {
5116 printf("TEST #6 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
5117 return False;
5120 status = cli_close(cli1, fnum1);
5121 if (!NT_STATUS_IS_OK(status)) {
5122 printf("TEST #6 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
5123 return False;
5126 status = cli_close(cli2, fnum2);
5127 if (!NT_STATUS_IS_OK(status)) {
5128 printf("TEST #6 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
5129 return False;
5132 printf("non-io open test #6 passed.\n");
5134 printf("TEST #7 testing 1 non-io open, one io open with delete\n");
5136 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5138 status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA,
5139 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5140 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5141 if (!NT_STATUS_IS_OK(status)) {
5142 printf("TEST #7 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
5143 return False;
5146 status = cli_ntcreate(cli2, fname, 0,
5147 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5148 FILE_ATTRIBUTE_NORMAL,
5149 FILE_SHARE_READ|FILE_SHARE_DELETE,
5150 FILE_OPEN_IF, 0, 0, &fnum2);
5151 if (NT_STATUS_IS_OK(status)) {
5152 printf("TEST #7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, nt_errstr(status));
5153 return False;
5156 printf("TEST #7 open 2 of %s gave %s (correct error should be %s)\n", fname, nt_errstr(status), "sharing violation");
5158 status = cli_close(cli1, fnum1);
5159 if (!NT_STATUS_IS_OK(status)) {
5160 printf("TEST #7 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
5161 return False;
5164 printf("non-io open test #7 passed.\n");
5166 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5168 printf("TEST #8 testing open without WRITE_ATTRIBUTES, updating close write time.\n");
5169 status = cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL,
5170 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
5171 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5172 if (!NT_STATUS_IS_OK(status)) {
5173 printf("TEST #8 open of %s failed (%s)\n", fname, nt_errstr(status));
5174 correct = false;
5175 goto out;
5178 /* Write to ensure we have to update the file time. */
5179 status = cli_writeall(cli1, fnum1, 0, (const uint8_t *)"TEST DATA\n", 0, 10,
5180 NULL);
5181 if (!NT_STATUS_IS_OK(status)) {
5182 printf("TEST #8 cli_write failed: %s\n", nt_errstr(status));
5183 correct = false;
5184 goto out;
5187 status = cli_close(cli1, fnum1);
5188 if (!NT_STATUS_IS_OK(status)) {
5189 printf("TEST #8 close of %s failed (%s)\n", fname, nt_errstr(status));
5190 correct = false;
5193 out:
5195 if (!torture_close_connection(cli1)) {
5196 correct = False;
5198 if (!torture_close_connection(cli2)) {
5199 correct = False;
5202 return correct;
5205 NTSTATUS torture_setup_unix_extensions(struct cli_state *cli)
5207 uint16 major, minor;
5208 uint32 caplow, caphigh;
5209 NTSTATUS status;
5211 if (!SERVER_HAS_UNIX_CIFS(cli)) {
5212 printf("Server doesn't support UNIX CIFS extensions.\n");
5213 return NT_STATUS_NOT_SUPPORTED;
5216 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
5217 &caphigh);
5218 if (!NT_STATUS_IS_OK(status)) {
5219 printf("Server didn't return UNIX CIFS extensions: %s\n",
5220 nt_errstr(status));
5221 return status;
5224 status = cli_set_unix_extensions_capabilities(cli, major, minor,
5225 caplow, caphigh);
5226 if (!NT_STATUS_IS_OK(status)) {
5227 printf("Server doesn't support setting UNIX CIFS extensions: "
5228 "%s.\n", nt_errstr(status));
5229 return status;
5232 return NT_STATUS_OK;
5236 Test POSIX open /mkdir calls.
5238 static bool run_simple_posix_open_test(int dummy)
5240 static struct cli_state *cli1;
5241 const char *fname = "posix:file";
5242 const char *hname = "posix:hlink";
5243 const char *sname = "posix:symlink";
5244 const char *dname = "posix:dir";
5245 char buf[10];
5246 char namebuf[11];
5247 uint16_t fnum1 = (uint16_t)-1;
5248 SMB_STRUCT_STAT sbuf;
5249 bool correct = false;
5250 NTSTATUS status;
5252 printf("Starting simple POSIX open test\n");
5254 if (!torture_open_connection(&cli1, 0)) {
5255 return false;
5258 cli_sockopt(cli1, sockops);
5260 status = torture_setup_unix_extensions(cli1);
5261 if (!NT_STATUS_IS_OK(status)) {
5262 return false;
5265 cli_setatr(cli1, fname, 0, 0);
5266 cli_posix_unlink(cli1, fname);
5267 cli_setatr(cli1, dname, 0, 0);
5268 cli_posix_rmdir(cli1, dname);
5269 cli_setatr(cli1, hname, 0, 0);
5270 cli_posix_unlink(cli1, hname);
5271 cli_setatr(cli1, sname, 0, 0);
5272 cli_posix_unlink(cli1, sname);
5274 /* Create a directory. */
5275 status = cli_posix_mkdir(cli1, dname, 0777);
5276 if (!NT_STATUS_IS_OK(status)) {
5277 printf("POSIX mkdir of %s failed (%s)\n", dname, nt_errstr(status));
5278 goto out;
5281 status = cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL,
5282 0600, &fnum1);
5283 if (!NT_STATUS_IS_OK(status)) {
5284 printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
5285 goto out;
5288 /* Test ftruncate - set file size. */
5289 status = cli_ftruncate(cli1, fnum1, 1000);
5290 if (!NT_STATUS_IS_OK(status)) {
5291 printf("ftruncate failed (%s)\n", nt_errstr(status));
5292 goto out;
5295 /* Ensure st_size == 1000 */
5296 status = cli_posix_stat(cli1, fname, &sbuf);
5297 if (!NT_STATUS_IS_OK(status)) {
5298 printf("stat failed (%s)\n", nt_errstr(status));
5299 goto out;
5302 if (sbuf.st_ex_size != 1000) {
5303 printf("ftruncate - stat size (%u) != 1000\n", (unsigned int)sbuf.st_ex_size);
5304 goto out;
5307 /* Test ftruncate - set file size back to zero. */
5308 status = cli_ftruncate(cli1, fnum1, 0);
5309 if (!NT_STATUS_IS_OK(status)) {
5310 printf("ftruncate failed (%s)\n", nt_errstr(status));
5311 goto out;
5314 status = cli_close(cli1, fnum1);
5315 if (!NT_STATUS_IS_OK(status)) {
5316 printf("close failed (%s)\n", nt_errstr(status));
5317 goto out;
5320 /* Now open the file again for read only. */
5321 status = cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1);
5322 if (!NT_STATUS_IS_OK(status)) {
5323 printf("POSIX open of %s failed (%s)\n", fname, nt_errstr(status));
5324 goto out;
5327 /* Now unlink while open. */
5328 status = cli_posix_unlink(cli1, fname);
5329 if (!NT_STATUS_IS_OK(status)) {
5330 printf("POSIX unlink of %s failed (%s)\n", fname, nt_errstr(status));
5331 goto out;
5334 status = cli_close(cli1, fnum1);
5335 if (!NT_STATUS_IS_OK(status)) {
5336 printf("close(2) failed (%s)\n", nt_errstr(status));
5337 goto out;
5340 /* Ensure the file has gone. */
5341 status = cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1);
5342 if (NT_STATUS_IS_OK(status)) {
5343 printf("POSIX open of %s succeeded, should have been deleted.\n", fname);
5344 goto out;
5347 /* Create again to test open with O_TRUNC. */
5348 status = cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1);
5349 if (!NT_STATUS_IS_OK(status)) {
5350 printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
5351 goto out;
5354 /* Test ftruncate - set file size. */
5355 status = cli_ftruncate(cli1, fnum1, 1000);
5356 if (!NT_STATUS_IS_OK(status)) {
5357 printf("ftruncate failed (%s)\n", nt_errstr(status));
5358 goto out;
5361 /* Ensure st_size == 1000 */
5362 status = cli_posix_stat(cli1, fname, &sbuf);
5363 if (!NT_STATUS_IS_OK(status)) {
5364 printf("stat failed (%s)\n", nt_errstr(status));
5365 goto out;
5368 if (sbuf.st_ex_size != 1000) {
5369 printf("ftruncate - stat size (%u) != 1000\n", (unsigned int)sbuf.st_ex_size);
5370 goto out;
5373 status = cli_close(cli1, fnum1);
5374 if (!NT_STATUS_IS_OK(status)) {
5375 printf("close(2) failed (%s)\n", nt_errstr(status));
5376 goto out;
5379 /* Re-open with O_TRUNC. */
5380 status = cli_posix_open(cli1, fname, O_WRONLY|O_TRUNC, 0600, &fnum1);
5381 if (!NT_STATUS_IS_OK(status)) {
5382 printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
5383 goto out;
5386 /* Ensure st_size == 0 */
5387 status = cli_posix_stat(cli1, fname, &sbuf);
5388 if (!NT_STATUS_IS_OK(status)) {
5389 printf("stat failed (%s)\n", nt_errstr(status));
5390 goto out;
5393 if (sbuf.st_ex_size != 0) {
5394 printf("O_TRUNC - stat size (%u) != 0\n", (unsigned int)sbuf.st_ex_size);
5395 goto out;
5398 status = cli_close(cli1, fnum1);
5399 if (!NT_STATUS_IS_OK(status)) {
5400 printf("close failed (%s)\n", nt_errstr(status));
5401 goto out;
5404 status = cli_posix_unlink(cli1, fname);
5405 if (!NT_STATUS_IS_OK(status)) {
5406 printf("POSIX unlink of %s failed (%s)\n", fname, nt_errstr(status));
5407 goto out;
5410 status = cli_posix_open(cli1, dname, O_RDONLY, 0, &fnum1);
5411 if (!NT_STATUS_IS_OK(status)) {
5412 printf("POSIX open directory O_RDONLY of %s failed (%s)\n",
5413 dname, nt_errstr(status));
5414 goto out;
5417 cli_close(cli1, fnum1);
5419 /* What happens when we try and POSIX open a directory for write ? */
5420 status = cli_posix_open(cli1, dname, O_RDWR, 0, &fnum1);
5421 if (NT_STATUS_IS_OK(status)) {
5422 printf("POSIX open of directory %s succeeded, should have failed.\n", fname);
5423 goto out;
5424 } else {
5425 if (!check_both_error(__LINE__, status, ERRDOS, EISDIR,
5426 NT_STATUS_FILE_IS_A_DIRECTORY)) {
5427 goto out;
5431 /* Create the file. */
5432 status = cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL,
5433 0600, &fnum1);
5434 if (!NT_STATUS_IS_OK(status)) {
5435 printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
5436 goto out;
5439 /* Write some data into it. */
5440 status = cli_writeall(cli1, fnum1, 0, (const uint8_t *)"TEST DATA\n", 0, 10,
5441 NULL);
5442 if (!NT_STATUS_IS_OK(status)) {
5443 printf("cli_write failed: %s\n", nt_errstr(status));
5444 goto out;
5447 cli_close(cli1, fnum1);
5449 /* Now create a hardlink. */
5450 status = cli_posix_hardlink(cli1, fname, hname);
5451 if (!NT_STATUS_IS_OK(status)) {
5452 printf("POSIX hardlink of %s failed (%s)\n", hname, nt_errstr(status));
5453 goto out;
5456 /* Now create a symlink. */
5457 status = cli_posix_symlink(cli1, fname, sname);
5458 if (!NT_STATUS_IS_OK(status)) {
5459 printf("POSIX symlink of %s failed (%s)\n", sname, nt_errstr(status));
5460 goto out;
5463 /* Open the hardlink for read. */
5464 status = cli_posix_open(cli1, hname, O_RDONLY, 0, &fnum1);
5465 if (!NT_STATUS_IS_OK(status)) {
5466 printf("POSIX open of %s failed (%s)\n", hname, nt_errstr(status));
5467 goto out;
5470 if (cli_read(cli1, fnum1, buf, 0, 10) != 10) {
5471 printf("POSIX read of %s failed (%s)\n", hname, cli_errstr(cli1));
5472 goto out;
5475 if (memcmp(buf, "TEST DATA\n", 10)) {
5476 printf("invalid data read from hardlink\n");
5477 goto out;
5480 /* Do a POSIX lock/unlock. */
5481 status = cli_posix_lock(cli1, fnum1, 0, 100, true, READ_LOCK);
5482 if (!NT_STATUS_IS_OK(status)) {
5483 printf("POSIX lock failed %s\n", nt_errstr(status));
5484 goto out;
5487 /* Punch a hole in the locked area. */
5488 status = cli_posix_unlock(cli1, fnum1, 10, 80);
5489 if (!NT_STATUS_IS_OK(status)) {
5490 printf("POSIX unlock failed %s\n", nt_errstr(status));
5491 goto out;
5494 cli_close(cli1, fnum1);
5496 /* Open the symlink for read - this should fail. A POSIX
5497 client should not be doing opens on a symlink. */
5498 status = cli_posix_open(cli1, sname, O_RDONLY, 0, &fnum1);
5499 if (NT_STATUS_IS_OK(status)) {
5500 printf("POSIX open of %s succeeded (should have failed)\n", sname);
5501 goto out;
5502 } else {
5503 if (!check_both_error(__LINE__, status, ERRDOS, ERRbadpath,
5504 NT_STATUS_OBJECT_PATH_NOT_FOUND)) {
5505 printf("POSIX open of %s should have failed "
5506 "with NT_STATUS_OBJECT_PATH_NOT_FOUND, "
5507 "failed with %s instead.\n",
5508 sname, nt_errstr(status));
5509 goto out;
5513 status = cli_posix_readlink(cli1, sname, namebuf, sizeof(namebuf));
5514 if (!NT_STATUS_IS_OK(status)) {
5515 printf("POSIX readlink on %s failed (%s)\n", sname, nt_errstr(status));
5516 goto out;
5519 if (strcmp(namebuf, fname) != 0) {
5520 printf("POSIX readlink on %s failed to match name %s (read %s)\n",
5521 sname, fname, namebuf);
5522 goto out;
5525 status = cli_posix_rmdir(cli1, dname);
5526 if (!NT_STATUS_IS_OK(status)) {
5527 printf("POSIX rmdir failed (%s)\n", nt_errstr(status));
5528 goto out;
5531 printf("Simple POSIX open test passed\n");
5532 correct = true;
5534 out:
5536 if (fnum1 != (uint16_t)-1) {
5537 cli_close(cli1, fnum1);
5538 fnum1 = (uint16_t)-1;
5541 cli_setatr(cli1, sname, 0, 0);
5542 cli_posix_unlink(cli1, sname);
5543 cli_setatr(cli1, hname, 0, 0);
5544 cli_posix_unlink(cli1, hname);
5545 cli_setatr(cli1, fname, 0, 0);
5546 cli_posix_unlink(cli1, fname);
5547 cli_setatr(cli1, dname, 0, 0);
5548 cli_posix_rmdir(cli1, dname);
5550 if (!torture_close_connection(cli1)) {
5551 correct = false;
5554 return correct;
5558 static uint32 open_attrs_table[] = {
5559 FILE_ATTRIBUTE_NORMAL,
5560 FILE_ATTRIBUTE_ARCHIVE,
5561 FILE_ATTRIBUTE_READONLY,
5562 FILE_ATTRIBUTE_HIDDEN,
5563 FILE_ATTRIBUTE_SYSTEM,
5565 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY,
5566 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN,
5567 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,
5568 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
5569 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
5570 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
5572 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
5573 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
5574 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
5575 FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_SYSTEM,
5578 struct trunc_open_results {
5579 unsigned int num;
5580 uint32 init_attr;
5581 uint32 trunc_attr;
5582 uint32 result_attr;
5585 static struct trunc_open_results attr_results[] = {
5586 { 0, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
5587 { 1, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
5588 { 2, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
5589 { 16, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
5590 { 17, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
5591 { 18, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
5592 { 51, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5593 { 54, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5594 { 56, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
5595 { 68, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5596 { 71, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5597 { 73, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
5598 { 99, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5599 { 102, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5600 { 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 },
5601 { 116, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5602 { 119, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5603 { 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 },
5604 { 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 },
5605 { 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 },
5606 { 227, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5607 { 230, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5608 { 232, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
5609 { 244, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5610 { 247, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5611 { 249, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM }
5614 static bool run_openattrtest(int dummy)
5616 static struct cli_state *cli1;
5617 const char *fname = "\\openattr.file";
5618 uint16_t fnum1;
5619 bool correct = True;
5620 uint16 attr;
5621 unsigned int i, j, k, l;
5622 NTSTATUS status;
5624 printf("starting open attr test\n");
5626 if (!torture_open_connection(&cli1, 0)) {
5627 return False;
5630 cli_sockopt(cli1, sockops);
5632 for (k = 0, i = 0; i < sizeof(open_attrs_table)/sizeof(uint32); i++) {
5633 cli_setatr(cli1, fname, 0, 0);
5634 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5636 status = cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA,
5637 open_attrs_table[i], FILE_SHARE_NONE,
5638 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5639 if (!NT_STATUS_IS_OK(status)) {
5640 printf("open %d (1) of %s failed (%s)\n", i, fname, nt_errstr(status));
5641 return False;
5644 status = cli_close(cli1, fnum1);
5645 if (!NT_STATUS_IS_OK(status)) {
5646 printf("close %d (1) of %s failed (%s)\n", i, fname, nt_errstr(status));
5647 return False;
5650 for (j = 0; j < sizeof(open_attrs_table)/sizeof(uint32); j++) {
5651 status = cli_ntcreate(cli1, fname, 0,
5652 FILE_READ_DATA|FILE_WRITE_DATA,
5653 open_attrs_table[j],
5654 FILE_SHARE_NONE, FILE_OVERWRITE,
5655 0, 0, &fnum1);
5656 if (!NT_STATUS_IS_OK(status)) {
5657 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
5658 if (attr_results[l].num == k) {
5659 printf("[%d] trunc open 0x%x -> 0x%x of %s failed - should have succeeded !(0x%x:%s)\n",
5660 k, open_attrs_table[i],
5661 open_attrs_table[j],
5662 fname, NT_STATUS_V(status), nt_errstr(status));
5663 correct = False;
5667 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5668 printf("[%d] trunc open 0x%x -> 0x%x failed with wrong error code %s\n",
5669 k, open_attrs_table[i], open_attrs_table[j],
5670 nt_errstr(status));
5671 correct = False;
5673 #if 0
5674 printf("[%d] trunc open 0x%x -> 0x%x failed\n", k, open_attrs_table[i], open_attrs_table[j]);
5675 #endif
5676 k++;
5677 continue;
5680 status = cli_close(cli1, fnum1);
5681 if (!NT_STATUS_IS_OK(status)) {
5682 printf("close %d (2) of %s failed (%s)\n", j, fname, nt_errstr(status));
5683 return False;
5686 status = cli_getatr(cli1, fname, &attr, NULL, NULL);
5687 if (!NT_STATUS_IS_OK(status)) {
5688 printf("getatr(2) failed (%s)\n", nt_errstr(status));
5689 return False;
5692 #if 0
5693 printf("[%d] getatr check [0x%x] trunc [0x%x] got attr 0x%x\n",
5694 k, open_attrs_table[i], open_attrs_table[j], attr );
5695 #endif
5697 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
5698 if (attr_results[l].num == k) {
5699 if (attr != attr_results[l].result_attr ||
5700 open_attrs_table[i] != attr_results[l].init_attr ||
5701 open_attrs_table[j] != attr_results[l].trunc_attr) {
5702 printf("getatr check failed. [0x%x] trunc [0x%x] got attr 0x%x, should be 0x%x\n",
5703 open_attrs_table[i],
5704 open_attrs_table[j],
5705 (unsigned int)attr,
5706 attr_results[l].result_attr);
5707 correct = False;
5709 break;
5712 k++;
5716 cli_setatr(cli1, fname, 0, 0);
5717 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5719 printf("open attr test %s.\n", correct ? "passed" : "failed");
5721 if (!torture_close_connection(cli1)) {
5722 correct = False;
5724 return correct;
5727 static NTSTATUS list_fn(const char *mnt, struct file_info *finfo,
5728 const char *name, void *state)
5730 int *matched = (int *)state;
5731 if (matched != NULL) {
5732 *matched += 1;
5734 return NT_STATUS_OK;
5738 test directory listing speed
5740 static bool run_dirtest(int dummy)
5742 int i;
5743 static struct cli_state *cli;
5744 uint16_t fnum;
5745 struct timeval core_start;
5746 bool correct = True;
5747 int matched;
5749 printf("starting directory test\n");
5751 if (!torture_open_connection(&cli, 0)) {
5752 return False;
5755 cli_sockopt(cli, sockops);
5757 srandom(0);
5758 for (i=0;i<torture_numops;i++) {
5759 fstring fname;
5760 slprintf(fname, sizeof(fname), "\\%x", (int)random());
5761 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum))) {
5762 fprintf(stderr,"Failed to open %s\n", fname);
5763 return False;
5765 cli_close(cli, fnum);
5768 core_start = timeval_current();
5770 matched = 0;
5771 cli_list(cli, "a*.*", 0, list_fn, &matched);
5772 printf("Matched %d\n", matched);
5774 matched = 0;
5775 cli_list(cli, "b*.*", 0, list_fn, &matched);
5776 printf("Matched %d\n", matched);
5778 matched = 0;
5779 cli_list(cli, "xyzabc", 0, list_fn, &matched);
5780 printf("Matched %d\n", matched);
5782 printf("dirtest core %g seconds\n", timeval_elapsed(&core_start));
5784 srandom(0);
5785 for (i=0;i<torture_numops;i++) {
5786 fstring fname;
5787 slprintf(fname, sizeof(fname), "\\%x", (int)random());
5788 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5791 if (!torture_close_connection(cli)) {
5792 correct = False;
5795 printf("finished dirtest\n");
5797 return correct;
5800 static NTSTATUS del_fn(const char *mnt, struct file_info *finfo, const char *mask,
5801 void *state)
5803 struct cli_state *pcli = (struct cli_state *)state;
5804 fstring fname;
5805 slprintf(fname, sizeof(fname), "\\LISTDIR\\%s", finfo->name);
5807 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
5808 return NT_STATUS_OK;
5810 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
5811 if (!NT_STATUS_IS_OK(cli_rmdir(pcli, fname)))
5812 printf("del_fn: failed to rmdir %s\n,", fname );
5813 } else {
5814 if (!NT_STATUS_IS_OK(cli_unlink(pcli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)))
5815 printf("del_fn: failed to unlink %s\n,", fname );
5817 return NT_STATUS_OK;
5822 sees what IOCTLs are supported
5824 bool torture_ioctl_test(int dummy)
5826 static struct cli_state *cli;
5827 uint16_t device, function;
5828 uint16_t fnum;
5829 const char *fname = "\\ioctl.dat";
5830 DATA_BLOB blob;
5831 NTSTATUS status;
5833 if (!torture_open_connection(&cli, 0)) {
5834 return False;
5837 printf("starting ioctl test\n");
5839 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5841 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
5842 if (!NT_STATUS_IS_OK(status)) {
5843 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
5844 return False;
5847 status = cli_raw_ioctl(cli, fnum, 0x2d0000 | (0x0420<<2), &blob);
5848 printf("ioctl device info: %s\n", nt_errstr(status));
5850 status = cli_raw_ioctl(cli, fnum, IOCTL_QUERY_JOB_INFO, &blob);
5851 printf("ioctl job info: %s\n", nt_errstr(status));
5853 for (device=0;device<0x100;device++) {
5854 printf("ioctl test with device = 0x%x\n", device);
5855 for (function=0;function<0x100;function++) {
5856 uint32 code = (device<<16) | function;
5858 status = cli_raw_ioctl(cli, fnum, code, &blob);
5860 if (NT_STATUS_IS_OK(status)) {
5861 printf("ioctl 0x%x OK : %d bytes\n", (int)code,
5862 (int)blob.length);
5863 data_blob_free(&blob);
5868 if (!torture_close_connection(cli)) {
5869 return False;
5872 return True;
5877 tries varients of chkpath
5879 bool torture_chkpath_test(int dummy)
5881 static struct cli_state *cli;
5882 uint16_t fnum;
5883 bool ret;
5884 NTSTATUS status;
5886 if (!torture_open_connection(&cli, 0)) {
5887 return False;
5890 printf("starting chkpath test\n");
5892 /* cleanup from an old run */
5893 cli_rmdir(cli, "\\chkpath.dir\\dir2");
5894 cli_unlink(cli, "\\chkpath.dir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5895 cli_rmdir(cli, "\\chkpath.dir");
5897 status = cli_mkdir(cli, "\\chkpath.dir");
5898 if (!NT_STATUS_IS_OK(status)) {
5899 printf("mkdir1 failed : %s\n", nt_errstr(status));
5900 return False;
5903 status = cli_mkdir(cli, "\\chkpath.dir\\dir2");
5904 if (!NT_STATUS_IS_OK(status)) {
5905 printf("mkdir2 failed : %s\n", nt_errstr(status));
5906 return False;
5909 status = cli_open(cli, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL,
5910 DENY_NONE, &fnum);
5911 if (!NT_STATUS_IS_OK(status)) {
5912 printf("open1 failed (%s)\n", nt_errstr(status));
5913 return False;
5915 cli_close(cli, fnum);
5917 status = cli_chkpath(cli, "\\chkpath.dir");
5918 if (!NT_STATUS_IS_OK(status)) {
5919 printf("chkpath1 failed: %s\n", nt_errstr(status));
5920 ret = False;
5923 status = cli_chkpath(cli, "\\chkpath.dir\\dir2");
5924 if (!NT_STATUS_IS_OK(status)) {
5925 printf("chkpath2 failed: %s\n", nt_errstr(status));
5926 ret = False;
5929 status = cli_chkpath(cli, "\\chkpath.dir\\foo.txt");
5930 if (!NT_STATUS_IS_OK(status)) {
5931 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
5932 NT_STATUS_NOT_A_DIRECTORY);
5933 } else {
5934 printf("* chkpath on a file should fail\n");
5935 ret = False;
5938 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\bar.txt"))) {
5939 ret = check_error(__LINE__, cli, ERRDOS, ERRbadfile,
5940 NT_STATUS_OBJECT_NAME_NOT_FOUND);
5941 } else {
5942 printf("* chkpath on a non existant file should fail\n");
5943 ret = False;
5946 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dirxx\\bar.txt"))) {
5947 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
5948 NT_STATUS_OBJECT_PATH_NOT_FOUND);
5949 } else {
5950 printf("* chkpath on a non existent component should fail\n");
5951 ret = False;
5954 cli_rmdir(cli, "\\chkpath.dir\\dir2");
5955 cli_unlink(cli, "\\chkpath.dir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5956 cli_rmdir(cli, "\\chkpath.dir");
5958 if (!torture_close_connection(cli)) {
5959 return False;
5962 return ret;
5965 static bool run_eatest(int dummy)
5967 static struct cli_state *cli;
5968 const char *fname = "\\eatest.txt";
5969 bool correct = True;
5970 uint16_t fnum;
5971 int i;
5972 size_t num_eas;
5973 struct ea_struct *ea_list = NULL;
5974 TALLOC_CTX *mem_ctx = talloc_init("eatest");
5975 NTSTATUS status;
5977 printf("starting eatest\n");
5979 if (!torture_open_connection(&cli, 0)) {
5980 talloc_destroy(mem_ctx);
5981 return False;
5984 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5986 status = cli_ntcreate(cli, fname, 0,
5987 FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
5988 FILE_SHARE_NONE, FILE_OVERWRITE_IF,
5989 0x4044, 0, &fnum);
5990 if (!NT_STATUS_IS_OK(status)) {
5991 printf("open failed - %s\n", nt_errstr(status));
5992 talloc_destroy(mem_ctx);
5993 return False;
5996 for (i = 0; i < 10; i++) {
5997 fstring ea_name, ea_val;
5999 slprintf(ea_name, sizeof(ea_name), "EA_%d", i);
6000 memset(ea_val, (char)i+1, i+1);
6001 status = cli_set_ea_fnum(cli, fnum, ea_name, ea_val, i+1);
6002 if (!NT_STATUS_IS_OK(status)) {
6003 printf("ea_set of name %s failed - %s\n", ea_name,
6004 nt_errstr(status));
6005 talloc_destroy(mem_ctx);
6006 return False;
6010 cli_close(cli, fnum);
6011 for (i = 0; i < 10; i++) {
6012 fstring ea_name, ea_val;
6014 slprintf(ea_name, sizeof(ea_name), "EA_%d", i+10);
6015 memset(ea_val, (char)i+1, i+1);
6016 status = cli_set_ea_path(cli, fname, ea_name, ea_val, i+1);
6017 if (!NT_STATUS_IS_OK(status)) {
6018 printf("ea_set of name %s failed - %s\n", ea_name,
6019 nt_errstr(status));
6020 talloc_destroy(mem_ctx);
6021 return False;
6025 status = cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list);
6026 if (!NT_STATUS_IS_OK(status)) {
6027 printf("ea_get list failed - %s\n", nt_errstr(status));
6028 correct = False;
6031 printf("num_eas = %d\n", (int)num_eas);
6033 if (num_eas != 20) {
6034 printf("Should be 20 EA's stored... failing.\n");
6035 correct = False;
6038 for (i = 0; i < num_eas; i++) {
6039 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
6040 dump_data(0, ea_list[i].value.data,
6041 ea_list[i].value.length);
6044 /* Setting EA's to zero length deletes them. Test this */
6045 printf("Now deleting all EA's - case indepenent....\n");
6047 #if 1
6048 cli_set_ea_path(cli, fname, "", "", 0);
6049 #else
6050 for (i = 0; i < 20; i++) {
6051 fstring ea_name;
6052 slprintf(ea_name, sizeof(ea_name), "ea_%d", i);
6053 status = cli_set_ea_path(cli, fname, ea_name, "", 0);
6054 if (!NT_STATUS_IS_OK(status)) {
6055 printf("ea_set of name %s failed - %s\n", ea_name,
6056 nt_errstr(status));
6057 talloc_destroy(mem_ctx);
6058 return False;
6061 #endif
6063 status = cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list);
6064 if (!NT_STATUS_IS_OK(status)) {
6065 printf("ea_get list failed - %s\n", nt_errstr(status));
6066 correct = False;
6069 printf("num_eas = %d\n", (int)num_eas);
6070 for (i = 0; i < num_eas; i++) {
6071 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
6072 dump_data(0, ea_list[i].value.data,
6073 ea_list[i].value.length);
6076 if (num_eas != 0) {
6077 printf("deleting EA's failed.\n");
6078 correct = False;
6081 /* Try and delete a non existant EA. */
6082 status = cli_set_ea_path(cli, fname, "foo", "", 0);
6083 if (!NT_STATUS_IS_OK(status)) {
6084 printf("deleting non-existant EA 'foo' should succeed. %s\n",
6085 nt_errstr(status));
6086 correct = False;
6089 talloc_destroy(mem_ctx);
6090 if (!torture_close_connection(cli)) {
6091 correct = False;
6094 return correct;
6097 static bool run_dirtest1(int dummy)
6099 int i;
6100 static struct cli_state *cli;
6101 uint16_t fnum;
6102 int num_seen;
6103 bool correct = True;
6105 printf("starting directory test\n");
6107 if (!torture_open_connection(&cli, 0)) {
6108 return False;
6111 cli_sockopt(cli, sockops);
6113 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
6114 cli_list(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, del_fn, cli);
6115 cli_rmdir(cli, "\\LISTDIR");
6116 cli_mkdir(cli, "\\LISTDIR");
6118 /* Create 1000 files and 1000 directories. */
6119 for (i=0;i<1000;i++) {
6120 fstring fname;
6121 slprintf(fname, sizeof(fname), "\\LISTDIR\\f%d", i);
6122 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
6123 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
6124 fprintf(stderr,"Failed to open %s\n", fname);
6125 return False;
6127 cli_close(cli, fnum);
6129 for (i=0;i<1000;i++) {
6130 fstring fname;
6131 slprintf(fname, sizeof(fname), "\\LISTDIR\\d%d", i);
6132 if (!NT_STATUS_IS_OK(cli_mkdir(cli, fname))) {
6133 fprintf(stderr,"Failed to open %s\n", fname);
6134 return False;
6138 /* Now ensure that doing an old list sees both files and directories. */
6139 num_seen = 0;
6140 cli_list_old(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
6141 printf("num_seen = %d\n", num_seen );
6142 /* We should see 100 files + 1000 directories + . and .. */
6143 if (num_seen != 2002)
6144 correct = False;
6146 /* Ensure if we have the "must have" bits we only see the
6147 * relevent entries.
6149 num_seen = 0;
6150 cli_list_old(cli, "\\LISTDIR\\*", (FILE_ATTRIBUTE_DIRECTORY<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
6151 printf("num_seen = %d\n", num_seen );
6152 if (num_seen != 1002)
6153 correct = False;
6155 num_seen = 0;
6156 cli_list_old(cli, "\\LISTDIR\\*", (FILE_ATTRIBUTE_ARCHIVE<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
6157 printf("num_seen = %d\n", num_seen );
6158 if (num_seen != 1000)
6159 correct = False;
6161 /* Delete everything. */
6162 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
6163 cli_list(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, del_fn, cli);
6164 cli_rmdir(cli, "\\LISTDIR");
6166 #if 0
6167 printf("Matched %d\n", cli_list(cli, "a*.*", 0, list_fn, NULL));
6168 printf("Matched %d\n", cli_list(cli, "b*.*", 0, list_fn, NULL));
6169 printf("Matched %d\n", cli_list(cli, "xyzabc", 0, list_fn, NULL));
6170 #endif
6172 if (!torture_close_connection(cli)) {
6173 correct = False;
6176 printf("finished dirtest1\n");
6178 return correct;
6181 static bool run_error_map_extract(int dummy) {
6183 static struct cli_state *c_dos;
6184 static struct cli_state *c_nt;
6185 NTSTATUS status;
6187 uint32 error;
6189 uint32 errnum;
6190 uint8 errclass;
6192 NTSTATUS nt_status;
6194 fstring user;
6196 /* NT-Error connection */
6198 if (!(c_nt = open_nbt_connection())) {
6199 return False;
6202 c_nt->use_spnego = False;
6204 status = cli_negprot(c_nt);
6206 if (!NT_STATUS_IS_OK(status)) {
6207 printf("%s rejected the NT-error negprot (%s)\n", host,
6208 nt_errstr(status));
6209 cli_shutdown(c_nt);
6210 return False;
6213 status = cli_session_setup(c_nt, "", "", 0, "", 0, workgroup);
6214 if (!NT_STATUS_IS_OK(status)) {
6215 printf("%s rejected the NT-error initial session setup (%s)\n",host, nt_errstr(status));
6216 return False;
6219 /* DOS-Error connection */
6221 if (!(c_dos = open_nbt_connection())) {
6222 return False;
6225 c_dos->use_spnego = False;
6226 c_dos->force_dos_errors = True;
6228 status = cli_negprot(c_dos);
6229 if (!NT_STATUS_IS_OK(status)) {
6230 printf("%s rejected the DOS-error negprot (%s)\n", host,
6231 nt_errstr(status));
6232 cli_shutdown(c_dos);
6233 return False;
6236 status = cli_session_setup(c_dos, "", "", 0, "", 0, workgroup);
6237 if (!NT_STATUS_IS_OK(status)) {
6238 printf("%s rejected the DOS-error initial session setup (%s)\n",
6239 host, nt_errstr(status));
6240 return False;
6243 for (error=(0xc0000000 | 0x1); error < (0xc0000000| 0xFFF); error++) {
6244 fstr_sprintf(user, "%X", error);
6246 status = cli_session_setup(c_nt, user,
6247 password, strlen(password),
6248 password, strlen(password),
6249 workgroup);
6250 if (NT_STATUS_IS_OK(status)) {
6251 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
6254 /* Case #1: 32-bit NT errors */
6255 if (cli_is_nt_error(c_nt)) {
6256 nt_status = cli_nt_error(c_nt);
6257 } else {
6258 printf("/** Dos error on NT connection! (%s) */\n",
6259 cli_errstr(c_nt));
6260 nt_status = NT_STATUS(0xc0000000);
6263 status = cli_session_setup(c_dos, user,
6264 password, strlen(password),
6265 password, strlen(password),
6266 workgroup);
6267 if (NT_STATUS_IS_OK(status)) {
6268 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
6271 /* Case #1: 32-bit NT errors */
6272 if (!cli_is_dos_error(c_dos)) {
6273 printf("/** NT error on DOS connection! (%s) */\n",
6274 cli_errstr(c_dos));
6275 errnum = errclass = 0;
6276 } else {
6277 cli_dos_error(c_dos, &errclass, &errnum);
6280 if (NT_STATUS_V(nt_status) != error) {
6281 printf("/*\t{ This NT error code was 'sqashed'\n\t from %s to %s \n\t during the session setup }\n*/\n",
6282 get_nt_error_c_code(talloc_tos(), NT_STATUS(error)),
6283 get_nt_error_c_code(talloc_tos(), nt_status));
6286 printf("\t{%s,\t%s,\t%s},\n",
6287 smb_dos_err_class(errclass),
6288 smb_dos_err_name(errclass, errnum),
6289 get_nt_error_c_code(talloc_tos(), NT_STATUS(error)));
6291 return True;
6294 static bool run_sesssetup_bench(int dummy)
6296 static struct cli_state *c;
6297 const char *fname = "\\file.dat";
6298 uint16_t fnum;
6299 NTSTATUS status;
6300 int i;
6302 if (!torture_open_connection(&c, 0)) {
6303 return false;
6306 status = cli_ntcreate(c, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
6307 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
6308 FILE_DELETE_ON_CLOSE, 0, &fnum);
6309 if (!NT_STATUS_IS_OK(status)) {
6310 d_printf("open %s failed: %s\n", fname, nt_errstr(status));
6311 return false;
6314 for (i=0; i<torture_numops; i++) {
6315 status = cli_session_setup(
6316 c, username,
6317 password, strlen(password),
6318 password, strlen(password),
6319 workgroup);
6320 if (!NT_STATUS_IS_OK(status)) {
6321 d_printf("(%s) cli_session_setup failed: %s\n",
6322 __location__, nt_errstr(status));
6323 return false;
6326 d_printf("\r%d ", (int)c->vuid);
6328 status = cli_ulogoff(c);
6329 if (!NT_STATUS_IS_OK(status)) {
6330 d_printf("(%s) cli_ulogoff failed: %s\n",
6331 __location__, nt_errstr(status));
6332 return false;
6334 c->vuid = 0;
6337 return true;
6340 static bool subst_test(const char *str, const char *user, const char *domain,
6341 uid_t uid, gid_t gid, const char *expected)
6343 char *subst;
6344 bool result = true;
6346 subst = talloc_sub_specified(talloc_tos(), str, user, domain, uid, gid);
6348 if (strcmp(subst, expected) != 0) {
6349 printf("sub_specified(%s, %s, %s, %d, %d) returned [%s], expected "
6350 "[%s]\n", str, user, domain, (int)uid, (int)gid, subst,
6351 expected);
6352 result = false;
6355 TALLOC_FREE(subst);
6356 return result;
6359 static void chain1_open_completion(struct tevent_req *req)
6361 uint16_t fnum;
6362 NTSTATUS status;
6363 status = cli_open_recv(req, &fnum);
6364 TALLOC_FREE(req);
6366 d_printf("cli_open_recv returned %s: %d\n",
6367 nt_errstr(status),
6368 NT_STATUS_IS_OK(status) ? fnum : -1);
6371 static void chain1_write_completion(struct tevent_req *req)
6373 size_t written;
6374 NTSTATUS status;
6375 status = cli_write_andx_recv(req, &written);
6376 TALLOC_FREE(req);
6378 d_printf("cli_write_andx_recv returned %s: %d\n",
6379 nt_errstr(status),
6380 NT_STATUS_IS_OK(status) ? (int)written : -1);
6383 static void chain1_close_completion(struct tevent_req *req)
6385 NTSTATUS status;
6386 bool *done = (bool *)tevent_req_callback_data_void(req);
6388 status = cli_close_recv(req);
6389 *done = true;
6391 TALLOC_FREE(req);
6393 d_printf("cli_close returned %s\n", nt_errstr(status));
6396 static bool run_chain1(int dummy)
6398 struct cli_state *cli1;
6399 struct event_context *evt = event_context_init(NULL);
6400 struct tevent_req *reqs[3], *smbreqs[3];
6401 bool done = false;
6402 const char *str = "foobar";
6403 NTSTATUS status;
6405 printf("starting chain1 test\n");
6406 if (!torture_open_connection(&cli1, 0)) {
6407 return False;
6410 cli_sockopt(cli1, sockops);
6412 reqs[0] = cli_open_create(talloc_tos(), evt, cli1, "\\test",
6413 O_CREAT|O_RDWR, 0, &smbreqs[0]);
6414 if (reqs[0] == NULL) return false;
6415 tevent_req_set_callback(reqs[0], chain1_open_completion, NULL);
6418 reqs[1] = cli_write_andx_create(talloc_tos(), evt, cli1, 0, 0,
6419 (const uint8_t *)str, 0, strlen(str)+1,
6420 smbreqs, 1, &smbreqs[1]);
6421 if (reqs[1] == NULL) return false;
6422 tevent_req_set_callback(reqs[1], chain1_write_completion, NULL);
6424 reqs[2] = cli_close_create(talloc_tos(), evt, cli1, 0, &smbreqs[2]);
6425 if (reqs[2] == NULL) return false;
6426 tevent_req_set_callback(reqs[2], chain1_close_completion, &done);
6428 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
6429 if (!NT_STATUS_IS_OK(status)) {
6430 return false;
6433 while (!done) {
6434 event_loop_once(evt);
6437 torture_close_connection(cli1);
6438 return True;
6441 static void chain2_sesssetup_completion(struct tevent_req *req)
6443 NTSTATUS status;
6444 status = cli_session_setup_guest_recv(req);
6445 d_printf("sesssetup returned %s\n", nt_errstr(status));
6448 static void chain2_tcon_completion(struct tevent_req *req)
6450 bool *done = (bool *)tevent_req_callback_data_void(req);
6451 NTSTATUS status;
6452 status = cli_tcon_andx_recv(req);
6453 d_printf("tcon_and_x returned %s\n", nt_errstr(status));
6454 *done = true;
6457 static bool run_chain2(int dummy)
6459 struct cli_state *cli1;
6460 struct event_context *evt = event_context_init(NULL);
6461 struct tevent_req *reqs[2], *smbreqs[2];
6462 bool done = false;
6463 NTSTATUS status;
6465 printf("starting chain2 test\n");
6466 status = cli_start_connection(&cli1, lp_netbios_name(), host, NULL,
6467 port_to_use, Undefined, 0);
6468 if (!NT_STATUS_IS_OK(status)) {
6469 return False;
6472 cli_sockopt(cli1, sockops);
6474 reqs[0] = cli_session_setup_guest_create(talloc_tos(), evt, cli1,
6475 &smbreqs[0]);
6476 if (reqs[0] == NULL) return false;
6477 tevent_req_set_callback(reqs[0], chain2_sesssetup_completion, NULL);
6479 reqs[1] = cli_tcon_andx_create(talloc_tos(), evt, cli1, "IPC$",
6480 "?????", NULL, 0, &smbreqs[1]);
6481 if (reqs[1] == NULL) return false;
6482 tevent_req_set_callback(reqs[1], chain2_tcon_completion, &done);
6484 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
6485 if (!NT_STATUS_IS_OK(status)) {
6486 return false;
6489 while (!done) {
6490 event_loop_once(evt);
6493 torture_close_connection(cli1);
6494 return True;
6498 struct torture_createdel_state {
6499 struct tevent_context *ev;
6500 struct cli_state *cli;
6503 static void torture_createdel_created(struct tevent_req *subreq);
6504 static void torture_createdel_closed(struct tevent_req *subreq);
6506 static struct tevent_req *torture_createdel_send(TALLOC_CTX *mem_ctx,
6507 struct tevent_context *ev,
6508 struct cli_state *cli,
6509 const char *name)
6511 struct tevent_req *req, *subreq;
6512 struct torture_createdel_state *state;
6514 req = tevent_req_create(mem_ctx, &state,
6515 struct torture_createdel_state);
6516 if (req == NULL) {
6517 return NULL;
6519 state->ev = ev;
6520 state->cli = cli;
6522 subreq = cli_ntcreate_send(
6523 state, ev, cli, name, 0,
6524 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
6525 FILE_ATTRIBUTE_NORMAL,
6526 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
6527 FILE_OPEN_IF, FILE_DELETE_ON_CLOSE, 0);
6529 if (tevent_req_nomem(subreq, req)) {
6530 return tevent_req_post(req, ev);
6532 tevent_req_set_callback(subreq, torture_createdel_created, req);
6533 return req;
6536 static void torture_createdel_created(struct tevent_req *subreq)
6538 struct tevent_req *req = tevent_req_callback_data(
6539 subreq, struct tevent_req);
6540 struct torture_createdel_state *state = tevent_req_data(
6541 req, struct torture_createdel_state);
6542 NTSTATUS status;
6543 uint16_t fnum;
6545 status = cli_ntcreate_recv(subreq, &fnum);
6546 TALLOC_FREE(subreq);
6547 if (!NT_STATUS_IS_OK(status)) {
6548 DEBUG(10, ("cli_ntcreate_recv returned %s\n",
6549 nt_errstr(status)));
6550 tevent_req_nterror(req, status);
6551 return;
6554 subreq = cli_close_send(state, state->ev, state->cli, fnum);
6555 if (tevent_req_nomem(subreq, req)) {
6556 return;
6558 tevent_req_set_callback(subreq, torture_createdel_closed, req);
6561 static void torture_createdel_closed(struct tevent_req *subreq)
6563 struct tevent_req *req = tevent_req_callback_data(
6564 subreq, struct tevent_req);
6565 NTSTATUS status;
6567 status = cli_close_recv(subreq);
6568 if (!NT_STATUS_IS_OK(status)) {
6569 DEBUG(10, ("cli_close_recv returned %s\n", nt_errstr(status)));
6570 tevent_req_nterror(req, status);
6571 return;
6573 tevent_req_done(req);
6576 static NTSTATUS torture_createdel_recv(struct tevent_req *req)
6578 return tevent_req_simple_recv_ntstatus(req);
6581 struct torture_createdels_state {
6582 struct tevent_context *ev;
6583 struct cli_state *cli;
6584 const char *base_name;
6585 int sent;
6586 int received;
6587 int num_files;
6588 struct tevent_req **reqs;
6591 static void torture_createdels_done(struct tevent_req *subreq);
6593 static struct tevent_req *torture_createdels_send(TALLOC_CTX *mem_ctx,
6594 struct tevent_context *ev,
6595 struct cli_state *cli,
6596 const char *base_name,
6597 int num_parallel,
6598 int num_files)
6600 struct tevent_req *req;
6601 struct torture_createdels_state *state;
6602 int i;
6604 req = tevent_req_create(mem_ctx, &state,
6605 struct torture_createdels_state);
6606 if (req == NULL) {
6607 return NULL;
6609 state->ev = ev;
6610 state->cli = cli;
6611 state->base_name = talloc_strdup(state, base_name);
6612 if (tevent_req_nomem(state->base_name, req)) {
6613 return tevent_req_post(req, ev);
6615 state->num_files = MAX(num_parallel, num_files);
6616 state->sent = 0;
6617 state->received = 0;
6619 state->reqs = talloc_array(state, struct tevent_req *, num_parallel);
6620 if (tevent_req_nomem(state->reqs, req)) {
6621 return tevent_req_post(req, ev);
6624 for (i=0; i<num_parallel; i++) {
6625 char *name;
6627 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
6628 state->sent);
6629 if (tevent_req_nomem(name, req)) {
6630 return tevent_req_post(req, ev);
6632 state->reqs[i] = torture_createdel_send(
6633 state->reqs, state->ev, state->cli, name);
6634 if (tevent_req_nomem(state->reqs[i], req)) {
6635 return tevent_req_post(req, ev);
6637 name = talloc_move(state->reqs[i], &name);
6638 tevent_req_set_callback(state->reqs[i],
6639 torture_createdels_done, req);
6640 state->sent += 1;
6642 return req;
6645 static void torture_createdels_done(struct tevent_req *subreq)
6647 struct tevent_req *req = tevent_req_callback_data(
6648 subreq, struct tevent_req);
6649 struct torture_createdels_state *state = tevent_req_data(
6650 req, struct torture_createdels_state);
6651 size_t num_parallel = talloc_array_length(state->reqs);
6652 NTSTATUS status;
6653 char *name;
6654 int i;
6656 status = torture_createdel_recv(subreq);
6657 if (!NT_STATUS_IS_OK(status)){
6658 DEBUG(10, ("torture_createdel_recv returned %s\n",
6659 nt_errstr(status)));
6660 TALLOC_FREE(subreq);
6661 tevent_req_nterror(req, status);
6662 return;
6665 for (i=0; i<num_parallel; i++) {
6666 if (subreq == state->reqs[i]) {
6667 break;
6670 if (i == num_parallel) {
6671 DEBUG(10, ("received something we did not send\n"));
6672 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
6673 return;
6675 TALLOC_FREE(state->reqs[i]);
6677 if (state->sent >= state->num_files) {
6678 tevent_req_done(req);
6679 return;
6682 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
6683 state->sent);
6684 if (tevent_req_nomem(name, req)) {
6685 return;
6687 state->reqs[i] = torture_createdel_send(state->reqs, state->ev,
6688 state->cli, name);
6689 if (tevent_req_nomem(state->reqs[i], req)) {
6690 return;
6692 name = talloc_move(state->reqs[i], &name);
6693 tevent_req_set_callback(state->reqs[i], torture_createdels_done, req);
6694 state->sent += 1;
6697 static NTSTATUS torture_createdels_recv(struct tevent_req *req)
6699 return tevent_req_simple_recv_ntstatus(req);
6702 struct swallow_notify_state {
6703 struct tevent_context *ev;
6704 struct cli_state *cli;
6705 uint16_t fnum;
6706 uint32_t completion_filter;
6707 bool recursive;
6708 bool (*fn)(uint32_t action, const char *name, void *priv);
6709 void *priv;
6712 static void swallow_notify_done(struct tevent_req *subreq);
6714 static struct tevent_req *swallow_notify_send(TALLOC_CTX *mem_ctx,
6715 struct tevent_context *ev,
6716 struct cli_state *cli,
6717 uint16_t fnum,
6718 uint32_t completion_filter,
6719 bool recursive,
6720 bool (*fn)(uint32_t action,
6721 const char *name,
6722 void *priv),
6723 void *priv)
6725 struct tevent_req *req, *subreq;
6726 struct swallow_notify_state *state;
6728 req = tevent_req_create(mem_ctx, &state,
6729 struct swallow_notify_state);
6730 if (req == NULL) {
6731 return NULL;
6733 state->ev = ev;
6734 state->cli = cli;
6735 state->fnum = fnum;
6736 state->completion_filter = completion_filter;
6737 state->recursive = recursive;
6738 state->fn = fn;
6739 state->priv = priv;
6741 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
6742 0xffff, state->completion_filter,
6743 state->recursive);
6744 if (tevent_req_nomem(subreq, req)) {
6745 return tevent_req_post(req, ev);
6747 tevent_req_set_callback(subreq, swallow_notify_done, req);
6748 return req;
6751 static void swallow_notify_done(struct tevent_req *subreq)
6753 struct tevent_req *req = tevent_req_callback_data(
6754 subreq, struct tevent_req);
6755 struct swallow_notify_state *state = tevent_req_data(
6756 req, struct swallow_notify_state);
6757 NTSTATUS status;
6758 uint32_t i, num_changes;
6759 struct notify_change *changes;
6761 status = cli_notify_recv(subreq, state, &num_changes, &changes);
6762 TALLOC_FREE(subreq);
6763 if (!NT_STATUS_IS_OK(status)) {
6764 DEBUG(10, ("cli_notify_recv returned %s\n",
6765 nt_errstr(status)));
6766 tevent_req_nterror(req, status);
6767 return;
6770 for (i=0; i<num_changes; i++) {
6771 state->fn(changes[i].action, changes[i].name, state->priv);
6773 TALLOC_FREE(changes);
6775 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
6776 0xffff, state->completion_filter,
6777 state->recursive);
6778 if (tevent_req_nomem(subreq, req)) {
6779 return;
6781 tevent_req_set_callback(subreq, swallow_notify_done, req);
6784 static bool print_notifies(uint32_t action, const char *name, void *priv)
6786 if (DEBUGLEVEL > 5) {
6787 d_printf("%d %s\n", (int)action, name);
6789 return true;
6792 static void notify_bench_done(struct tevent_req *req)
6794 int *num_finished = (int *)tevent_req_callback_data_void(req);
6795 *num_finished += 1;
6798 static bool run_notify_bench(int dummy)
6800 const char *dname = "\\notify-bench";
6801 struct tevent_context *ev;
6802 NTSTATUS status;
6803 uint16_t dnum;
6804 struct tevent_req *req1;
6805 struct tevent_req *req2 = NULL;
6806 int i, num_unc_names;
6807 int num_finished = 0;
6809 printf("starting notify-bench test\n");
6811 if (use_multishare_conn) {
6812 char **unc_list;
6813 unc_list = file_lines_load(multishare_conn_fname,
6814 &num_unc_names, 0, NULL);
6815 if (!unc_list || num_unc_names <= 0) {
6816 d_printf("Failed to load unc names list from '%s'\n",
6817 multishare_conn_fname);
6818 return false;
6820 TALLOC_FREE(unc_list);
6821 } else {
6822 num_unc_names = 1;
6825 ev = tevent_context_init(talloc_tos());
6826 if (ev == NULL) {
6827 d_printf("tevent_context_init failed\n");
6828 return false;
6831 for (i=0; i<num_unc_names; i++) {
6832 struct cli_state *cli;
6833 char *base_fname;
6835 base_fname = talloc_asprintf(talloc_tos(), "%s\\file%3.3d.",
6836 dname, i);
6837 if (base_fname == NULL) {
6838 return false;
6841 if (!torture_open_connection(&cli, i)) {
6842 return false;
6845 status = cli_ntcreate(cli, dname, 0,
6846 MAXIMUM_ALLOWED_ACCESS,
6847 0, FILE_SHARE_READ|FILE_SHARE_WRITE|
6848 FILE_SHARE_DELETE,
6849 FILE_OPEN_IF, FILE_DIRECTORY_FILE, 0,
6850 &dnum);
6852 if (!NT_STATUS_IS_OK(status)) {
6853 d_printf("Could not create %s: %s\n", dname,
6854 nt_errstr(status));
6855 return false;
6858 req1 = swallow_notify_send(talloc_tos(), ev, cli, dnum,
6859 FILE_NOTIFY_CHANGE_FILE_NAME |
6860 FILE_NOTIFY_CHANGE_DIR_NAME |
6861 FILE_NOTIFY_CHANGE_ATTRIBUTES |
6862 FILE_NOTIFY_CHANGE_LAST_WRITE,
6863 false, print_notifies, NULL);
6864 if (req1 == NULL) {
6865 d_printf("Could not create notify request\n");
6866 return false;
6869 req2 = torture_createdels_send(talloc_tos(), ev, cli,
6870 base_fname, 10, torture_numops);
6871 if (req2 == NULL) {
6872 d_printf("Could not create createdels request\n");
6873 return false;
6875 TALLOC_FREE(base_fname);
6877 tevent_req_set_callback(req2, notify_bench_done,
6878 &num_finished);
6881 while (num_finished < num_unc_names) {
6882 int ret;
6883 ret = tevent_loop_once(ev);
6884 if (ret != 0) {
6885 d_printf("tevent_loop_once failed\n");
6886 return false;
6890 if (!tevent_req_poll(req2, ev)) {
6891 d_printf("tevent_req_poll failed\n");
6894 status = torture_createdels_recv(req2);
6895 d_printf("torture_createdels_recv returned %s\n", nt_errstr(status));
6897 return true;
6900 static bool run_mangle1(int dummy)
6902 struct cli_state *cli;
6903 const char *fname = "this_is_a_long_fname_to_be_mangled.txt";
6904 uint16_t fnum;
6905 fstring alt_name;
6906 NTSTATUS status;
6907 time_t change_time, access_time, write_time;
6908 SMB_OFF_T size;
6909 uint16_t mode;
6911 printf("starting mangle1 test\n");
6912 if (!torture_open_connection(&cli, 0)) {
6913 return False;
6916 cli_sockopt(cli, sockops);
6918 status = cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
6919 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
6920 0, 0, &fnum);
6921 if (!NT_STATUS_IS_OK(status)) {
6922 d_printf("open %s failed: %s\n", fname, nt_errstr(status));
6923 return false;
6925 cli_close(cli, fnum);
6927 status = cli_qpathinfo_alt_name(cli, fname, alt_name);
6928 if (!NT_STATUS_IS_OK(status)) {
6929 d_printf("cli_qpathinfo_alt_name failed: %s\n",
6930 nt_errstr(status));
6931 return false;
6933 d_printf("alt_name: %s\n", alt_name);
6935 status = cli_open(cli, alt_name, O_RDONLY, DENY_NONE, &fnum);
6936 if (!NT_STATUS_IS_OK(status)) {
6937 d_printf("cli_open(%s) failed: %s\n", alt_name,
6938 nt_errstr(status));
6939 return false;
6941 cli_close(cli, fnum);
6943 status = cli_qpathinfo1(cli, alt_name, &change_time, &access_time,
6944 &write_time, &size, &mode);
6945 if (!NT_STATUS_IS_OK(status)) {
6946 d_printf("cli_qpathinfo1(%s) failed: %s\n", alt_name,
6947 nt_errstr(status));
6948 return false;
6951 return true;
6954 static size_t null_source(uint8_t *buf, size_t n, void *priv)
6956 size_t *to_pull = (size_t *)priv;
6957 size_t thistime = *to_pull;
6959 thistime = MIN(thistime, n);
6960 if (thistime == 0) {
6961 return 0;
6964 memset(buf, 0, thistime);
6965 *to_pull -= thistime;
6966 return thistime;
6969 static bool run_windows_write(int dummy)
6971 struct cli_state *cli1;
6972 uint16_t fnum;
6973 int i;
6974 bool ret = false;
6975 const char *fname = "\\writetest.txt";
6976 struct timeval start_time;
6977 double seconds;
6978 double kbytes;
6979 NTSTATUS status;
6981 printf("starting windows_write test\n");
6982 if (!torture_open_connection(&cli1, 0)) {
6983 return False;
6986 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
6987 if (!NT_STATUS_IS_OK(status)) {
6988 printf("open failed (%s)\n", nt_errstr(status));
6989 return False;
6992 cli_sockopt(cli1, sockops);
6994 start_time = timeval_current();
6996 for (i=0; i<torture_numops; i++) {
6997 uint8_t c = 0;
6998 off_t start = i * torture_blocksize;
6999 size_t to_pull = torture_blocksize - 1;
7001 status = cli_writeall(cli1, fnum, 0, &c,
7002 start + torture_blocksize - 1, 1, NULL);
7003 if (!NT_STATUS_IS_OK(status)) {
7004 printf("cli_write failed: %s\n", nt_errstr(status));
7005 goto fail;
7008 status = cli_push(cli1, fnum, 0, i * torture_blocksize, torture_blocksize,
7009 null_source, &to_pull);
7010 if (!NT_STATUS_IS_OK(status)) {
7011 printf("cli_push returned: %s\n", nt_errstr(status));
7012 goto fail;
7016 seconds = timeval_elapsed(&start_time);
7017 kbytes = (double)torture_blocksize * torture_numops;
7018 kbytes /= 1024;
7020 printf("Wrote %d kbytes in %.2f seconds: %d kb/sec\n", (int)kbytes,
7021 (double)seconds, (int)(kbytes/seconds));
7023 ret = true;
7024 fail:
7025 cli_close(cli1, fnum);
7026 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7027 torture_close_connection(cli1);
7028 return ret;
7031 static bool run_cli_echo(int dummy)
7033 struct cli_state *cli;
7034 NTSTATUS status;
7036 printf("starting cli_echo test\n");
7037 if (!torture_open_connection(&cli, 0)) {
7038 return false;
7040 cli_sockopt(cli, sockops);
7042 status = cli_echo(cli, 5, data_blob_const("hello", 5));
7044 d_printf("cli_echo returned %s\n", nt_errstr(status));
7046 torture_close_connection(cli);
7047 return NT_STATUS_IS_OK(status);
7050 static bool run_uid_regression_test(int dummy)
7052 static struct cli_state *cli;
7053 int16_t old_vuid;
7054 int16_t old_cnum;
7055 bool correct = True;
7056 NTSTATUS status;
7058 printf("starting uid regression test\n");
7060 if (!torture_open_connection(&cli, 0)) {
7061 return False;
7064 cli_sockopt(cli, sockops);
7066 /* Ok - now save then logoff our current user. */
7067 old_vuid = cli->vuid;
7069 status = cli_ulogoff(cli);
7070 if (!NT_STATUS_IS_OK(status)) {
7071 d_printf("(%s) cli_ulogoff failed: %s\n",
7072 __location__, nt_errstr(status));
7073 correct = false;
7074 goto out;
7077 cli->vuid = old_vuid;
7079 /* Try an operation. */
7080 status = cli_mkdir(cli, "\\uid_reg_test");
7081 if (NT_STATUS_IS_OK(status)) {
7082 d_printf("(%s) cli_mkdir succeeded\n",
7083 __location__);
7084 correct = false;
7085 goto out;
7086 } else {
7087 /* Should be bad uid. */
7088 if (!check_error(__LINE__, cli, ERRSRV, ERRbaduid,
7089 NT_STATUS_USER_SESSION_DELETED)) {
7090 correct = false;
7091 goto out;
7095 old_cnum = cli->cnum;
7097 /* Now try a SMBtdis with the invald vuid set to zero. */
7098 cli->vuid = 0;
7100 /* This should succeed. */
7101 status = cli_tdis(cli);
7103 if (NT_STATUS_IS_OK(status)) {
7104 d_printf("First tdis with invalid vuid should succeed.\n");
7105 } else {
7106 d_printf("First tdis failed (%s)\n", nt_errstr(status));
7107 correct = false;
7108 goto out;
7111 cli->vuid = old_vuid;
7112 cli->cnum = old_cnum;
7114 /* This should fail. */
7115 status = cli_tdis(cli);
7116 if (NT_STATUS_IS_OK(status)) {
7117 d_printf("Second tdis with invalid vuid should fail - succeeded instead !.\n");
7118 correct = false;
7119 goto out;
7120 } else {
7121 /* Should be bad tid. */
7122 if (!check_error(__LINE__, cli, ERRSRV, ERRinvnid,
7123 NT_STATUS_NETWORK_NAME_DELETED)) {
7124 correct = false;
7125 goto out;
7129 cli_rmdir(cli, "\\uid_reg_test");
7131 out:
7133 cli_shutdown(cli);
7134 return correct;
7138 static const char *illegal_chars = "*\\/?<>|\":";
7139 static char force_shortname_chars[] = " +,.[];=\177";
7141 static NTSTATUS shortname_del_fn(const char *mnt, struct file_info *finfo,
7142 const char *mask, void *state)
7144 struct cli_state *pcli = (struct cli_state *)state;
7145 fstring fname;
7146 NTSTATUS status = NT_STATUS_OK;
7148 slprintf(fname, sizeof(fname), "\\shortname\\%s", finfo->name);
7150 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
7151 return NT_STATUS_OK;
7153 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
7154 status = cli_rmdir(pcli, fname);
7155 if (!NT_STATUS_IS_OK(status)) {
7156 printf("del_fn: failed to rmdir %s\n,", fname );
7158 } else {
7159 status = cli_unlink(pcli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7160 if (!NT_STATUS_IS_OK(status)) {
7161 printf("del_fn: failed to unlink %s\n,", fname );
7164 return status;
7167 struct sn_state {
7168 int matched;
7169 int i;
7170 bool val;
7173 static NTSTATUS shortname_list_fn(const char *mnt, struct file_info *finfo,
7174 const char *name, void *state)
7176 struct sn_state *s = (struct sn_state *)state;
7177 int i = s->i;
7179 #if 0
7180 printf("shortname list: i = %d, name = |%s|, shortname = |%s|\n",
7181 i, finfo->name, finfo->short_name);
7182 #endif
7184 if (strchr(force_shortname_chars, i)) {
7185 if (!finfo->short_name) {
7186 /* Shortname not created when it should be. */
7187 d_printf("(%s) ERROR: Shortname was not created for file %s containing %d\n",
7188 __location__, finfo->name, i);
7189 s->val = true;
7191 } else if (finfo->short_name){
7192 /* Shortname created when it should not be. */
7193 d_printf("(%s) ERROR: Shortname %s was created for file %s\n",
7194 __location__, finfo->short_name, finfo->name);
7195 s->val = true;
7197 s->matched += 1;
7198 return NT_STATUS_OK;
7201 static bool run_shortname_test(int dummy)
7203 static struct cli_state *cli;
7204 bool correct = True;
7205 int i;
7206 struct sn_state s;
7207 char fname[20];
7208 NTSTATUS status;
7210 printf("starting shortname test\n");
7212 if (!torture_open_connection(&cli, 0)) {
7213 return False;
7216 cli_sockopt(cli, sockops);
7218 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
7219 cli_list(cli, "\\shortname\\*", FILE_ATTRIBUTE_DIRECTORY, shortname_del_fn, cli);
7220 cli_rmdir(cli, "\\shortname");
7222 status = cli_mkdir(cli, "\\shortname");
7223 if (!NT_STATUS_IS_OK(status)) {
7224 d_printf("(%s) cli_mkdir of \\shortname failed: %s\n",
7225 __location__, nt_errstr(status));
7226 correct = false;
7227 goto out;
7230 strlcpy(fname, "\\shortname\\", sizeof(fname));
7231 strlcat(fname, "test .txt", sizeof(fname));
7233 s.val = false;
7235 for (i = 32; i < 128; i++) {
7236 uint16_t fnum = (uint16_t)-1;
7238 s.i = i;
7240 if (strchr(illegal_chars, i)) {
7241 continue;
7243 fname[15] = i;
7245 status = cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
7246 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum);
7247 if (!NT_STATUS_IS_OK(status)) {
7248 d_printf("(%s) cli_nt_create of %s failed: %s\n",
7249 __location__, fname, nt_errstr(status));
7250 correct = false;
7251 goto out;
7253 cli_close(cli, fnum);
7255 s.matched = 0;
7256 status = cli_list(cli, "\\shortname\\test*.*", 0,
7257 shortname_list_fn, &s);
7258 if (s.matched != 1) {
7259 d_printf("(%s) failed to list %s: %s\n",
7260 __location__, fname, nt_errstr(status));
7261 correct = false;
7262 goto out;
7265 status = cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7266 if (!NT_STATUS_IS_OK(status)) {
7267 d_printf("(%s) failed to delete %s: %s\n",
7268 __location__, fname, nt_errstr(status));
7269 correct = false;
7270 goto out;
7273 if (s.val) {
7274 correct = false;
7275 goto out;
7279 out:
7281 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
7282 cli_list(cli, "\\shortname\\*", FILE_ATTRIBUTE_DIRECTORY, shortname_del_fn, cli);
7283 cli_rmdir(cli, "\\shortname");
7284 torture_close_connection(cli);
7285 return correct;
7288 static void pagedsearch_cb(struct tevent_req *req)
7290 int rc;
7291 struct tldap_message *msg;
7292 char *dn;
7294 rc = tldap_search_paged_recv(req, talloc_tos(), &msg);
7295 if (rc != TLDAP_SUCCESS) {
7296 d_printf("tldap_search_paged_recv failed: %s\n",
7297 tldap_err2string(rc));
7298 return;
7300 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
7301 TALLOC_FREE(msg);
7302 return;
7304 if (!tldap_entry_dn(msg, &dn)) {
7305 d_printf("tldap_entry_dn failed\n");
7306 return;
7308 d_printf("%s\n", dn);
7309 TALLOC_FREE(msg);
7312 static bool run_tldap(int dummy)
7314 struct tldap_context *ld;
7315 int fd, rc;
7316 NTSTATUS status;
7317 struct sockaddr_storage addr;
7318 struct tevent_context *ev;
7319 struct tevent_req *req;
7320 char *basedn;
7321 const char *filter;
7323 if (!resolve_name(host, &addr, 0, false)) {
7324 d_printf("could not find host %s\n", host);
7325 return false;
7327 status = open_socket_out(&addr, 389, 9999, &fd);
7328 if (!NT_STATUS_IS_OK(status)) {
7329 d_printf("open_socket_out failed: %s\n", nt_errstr(status));
7330 return false;
7333 ld = tldap_context_create(talloc_tos(), fd);
7334 if (ld == NULL) {
7335 close(fd);
7336 d_printf("tldap_context_create failed\n");
7337 return false;
7340 rc = tldap_fetch_rootdse(ld);
7341 if (rc != TLDAP_SUCCESS) {
7342 d_printf("tldap_fetch_rootdse failed: %s\n",
7343 tldap_errstr(talloc_tos(), ld, rc));
7344 return false;
7347 basedn = tldap_talloc_single_attribute(
7348 tldap_rootdse(ld), "defaultNamingContext", talloc_tos());
7349 if (basedn == NULL) {
7350 d_printf("no defaultNamingContext\n");
7351 return false;
7353 d_printf("defaultNamingContext: %s\n", basedn);
7355 ev = tevent_context_init(talloc_tos());
7356 if (ev == NULL) {
7357 d_printf("tevent_context_init failed\n");
7358 return false;
7361 req = tldap_search_paged_send(talloc_tos(), ev, ld, basedn,
7362 TLDAP_SCOPE_SUB, "(objectclass=*)",
7363 NULL, 0, 0,
7364 NULL, 0, NULL, 0, 0, 0, 0, 5);
7365 if (req == NULL) {
7366 d_printf("tldap_search_paged_send failed\n");
7367 return false;
7369 tevent_req_set_callback(req, pagedsearch_cb, NULL);
7371 tevent_req_poll(req, ev);
7373 TALLOC_FREE(req);
7375 /* test search filters against rootDSE */
7376 filter = "(&(|(name=samba)(nextRid<=10000000)(usnChanged>=10)(samba~=ambas)(!(name=s*m*a)))"
7377 "(|(name:=samba)(name:dn:2.5.13.5:=samba)(:dn:2.5.13.5:=samba)(!(name=*samba))))";
7379 rc = tldap_search(ld, "", TLDAP_SCOPE_BASE, filter,
7380 NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0,
7381 talloc_tos(), NULL, NULL);
7382 if (rc != TLDAP_SUCCESS) {
7383 d_printf("tldap_search with complex filter failed: %s\n",
7384 tldap_errstr(talloc_tos(), ld, rc));
7385 return false;
7388 TALLOC_FREE(ld);
7389 return true;
7392 /* Torture test to ensure no regression of :
7393 https://bugzilla.samba.org/show_bug.cgi?id=7084
7396 static bool run_dir_createtime(int dummy)
7398 struct cli_state *cli;
7399 const char *dname = "\\testdir";
7400 const char *fname = "\\testdir\\testfile";
7401 NTSTATUS status;
7402 struct timespec create_time;
7403 struct timespec create_time1;
7404 uint16_t fnum;
7405 bool ret = false;
7407 if (!torture_open_connection(&cli, 0)) {
7408 return false;
7411 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7412 cli_rmdir(cli, dname);
7414 status = cli_mkdir(cli, dname);
7415 if (!NT_STATUS_IS_OK(status)) {
7416 printf("mkdir failed: %s\n", nt_errstr(status));
7417 goto out;
7420 status = cli_qpathinfo2(cli, dname, &create_time, NULL, NULL, NULL,
7421 NULL, NULL, NULL);
7422 if (!NT_STATUS_IS_OK(status)) {
7423 printf("cli_qpathinfo2 returned %s\n",
7424 nt_errstr(status));
7425 goto out;
7428 /* Sleep 3 seconds, then create a file. */
7429 sleep(3);
7431 status = cli_open(cli, fname, O_RDWR | O_CREAT | O_EXCL,
7432 DENY_NONE, &fnum);
7433 if (!NT_STATUS_IS_OK(status)) {
7434 printf("cli_open failed: %s\n", nt_errstr(status));
7435 goto out;
7438 status = cli_qpathinfo2(cli, dname, &create_time1, NULL, NULL, NULL,
7439 NULL, NULL, NULL);
7440 if (!NT_STATUS_IS_OK(status)) {
7441 printf("cli_qpathinfo2 (2) returned %s\n",
7442 nt_errstr(status));
7443 goto out;
7446 if (timespec_compare(&create_time1, &create_time)) {
7447 printf("run_dir_createtime: create time was updated (error)\n");
7448 } else {
7449 printf("run_dir_createtime: create time was not updated (correct)\n");
7450 ret = true;
7453 out:
7455 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7456 cli_rmdir(cli, dname);
7457 if (!torture_close_connection(cli)) {
7458 ret = false;
7460 return ret;
7464 static bool run_streamerror(int dummy)
7466 struct cli_state *cli;
7467 const char *dname = "\\testdir";
7468 const char *streamname =
7469 "testdir:{4c8cc155-6c1e-11d1-8e41-00c04fb9386d}:$DATA";
7470 NTSTATUS status;
7471 time_t change_time, access_time, write_time;
7472 SMB_OFF_T size;
7473 uint16_t mode, fnum;
7474 bool ret = true;
7476 if (!torture_open_connection(&cli, 0)) {
7477 return false;
7480 cli_unlink(cli, "\\testdir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7481 cli_rmdir(cli, dname);
7483 status = cli_mkdir(cli, dname);
7484 if (!NT_STATUS_IS_OK(status)) {
7485 printf("mkdir failed: %s\n", nt_errstr(status));
7486 return false;
7489 cli_qpathinfo1(cli, streamname, &change_time, &access_time, &write_time,
7490 &size, &mode);
7491 status = cli_nt_error(cli);
7493 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
7494 printf("pathinfo returned %s, expected "
7495 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
7496 nt_errstr(status));
7497 ret = false;
7500 status = cli_ntcreate(cli, streamname, 0x16,
7501 FILE_READ_DATA|FILE_READ_EA|
7502 FILE_READ_ATTRIBUTES|READ_CONTROL_ACCESS,
7503 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
7504 FILE_OPEN, 0, 0, &fnum);
7506 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
7507 printf("ntcreate returned %s, expected "
7508 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
7509 nt_errstr(status));
7510 ret = false;
7514 cli_rmdir(cli, dname);
7515 return ret;
7518 static bool run_local_substitute(int dummy)
7520 bool ok = true;
7522 ok &= subst_test("%U", "bla", "", -1, -1, "bla");
7523 ok &= subst_test("%u%U", "bla", "", -1, -1, "blabla");
7524 ok &= subst_test("%g", "", "", -1, -1, "NO_GROUP");
7525 ok &= subst_test("%G", "", "", -1, -1, "NO_GROUP");
7526 ok &= subst_test("%g", "", "", -1, 0, gidtoname(0));
7527 ok &= subst_test("%G", "", "", -1, 0, gidtoname(0));
7528 ok &= subst_test("%D%u", "u", "dom", -1, 0, "domu");
7529 ok &= subst_test("%i %I", "", "", -1, -1, "0.0.0.0 0.0.0.0");
7531 /* Different captialization rules in sub_basic... */
7533 ok &= (strcmp(talloc_sub_basic(talloc_tos(), "BLA", "dom", "%U%D"),
7534 "blaDOM") == 0);
7536 return ok;
7539 static bool run_local_base64(int dummy)
7541 int i;
7542 bool ret = true;
7544 for (i=1; i<2000; i++) {
7545 DATA_BLOB blob1, blob2;
7546 char *b64;
7548 blob1.data = talloc_array(talloc_tos(), uint8_t, i);
7549 blob1.length = i;
7550 generate_random_buffer(blob1.data, blob1.length);
7552 b64 = base64_encode_data_blob(talloc_tos(), blob1);
7553 if (b64 == NULL) {
7554 d_fprintf(stderr, "base64_encode_data_blob failed "
7555 "for %d bytes\n", i);
7556 ret = false;
7558 blob2 = base64_decode_data_blob(b64);
7559 TALLOC_FREE(b64);
7561 if (data_blob_cmp(&blob1, &blob2)) {
7562 d_fprintf(stderr, "data_blob_cmp failed for %d "
7563 "bytes\n", i);
7564 ret = false;
7566 TALLOC_FREE(blob1.data);
7567 data_blob_free(&blob2);
7569 return ret;
7572 static bool run_local_gencache(int dummy)
7574 char *val;
7575 time_t tm;
7576 DATA_BLOB blob;
7578 if (!gencache_set("foo", "bar", time(NULL) + 1000)) {
7579 d_printf("%s: gencache_set() failed\n", __location__);
7580 return False;
7583 if (!gencache_get("foo", NULL, NULL)) {
7584 d_printf("%s: gencache_get() failed\n", __location__);
7585 return False;
7588 if (!gencache_get("foo", &val, &tm)) {
7589 d_printf("%s: gencache_get() failed\n", __location__);
7590 return False;
7593 if (strcmp(val, "bar") != 0) {
7594 d_printf("%s: gencache_get() returned %s, expected %s\n",
7595 __location__, val, "bar");
7596 SAFE_FREE(val);
7597 return False;
7600 SAFE_FREE(val);
7602 if (!gencache_del("foo")) {
7603 d_printf("%s: gencache_del() failed\n", __location__);
7604 return False;
7606 if (gencache_del("foo")) {
7607 d_printf("%s: second gencache_del() succeeded\n",
7608 __location__);
7609 return False;
7612 if (gencache_get("foo", &val, &tm)) {
7613 d_printf("%s: gencache_get() on deleted entry "
7614 "succeeded\n", __location__);
7615 return False;
7618 blob = data_blob_string_const_null("bar");
7619 tm = time(NULL) + 60;
7621 if (!gencache_set_data_blob("foo", &blob, tm)) {
7622 d_printf("%s: gencache_set_data_blob() failed\n", __location__);
7623 return False;
7626 if (!gencache_get_data_blob("foo", &blob, NULL, NULL)) {
7627 d_printf("%s: gencache_get_data_blob() failed\n", __location__);
7628 return False;
7631 if (strcmp((const char *)blob.data, "bar") != 0) {
7632 d_printf("%s: gencache_get_data_blob() returned %s, expected %s\n",
7633 __location__, (const char *)blob.data, "bar");
7634 data_blob_free(&blob);
7635 return False;
7638 data_blob_free(&blob);
7640 if (!gencache_del("foo")) {
7641 d_printf("%s: gencache_del() failed\n", __location__);
7642 return False;
7644 if (gencache_del("foo")) {
7645 d_printf("%s: second gencache_del() succeeded\n",
7646 __location__);
7647 return False;
7650 if (gencache_get_data_blob("foo", &blob, NULL, NULL)) {
7651 d_printf("%s: gencache_get_data_blob() on deleted entry "
7652 "succeeded\n", __location__);
7653 return False;
7656 return True;
7659 static bool rbt_testval(struct db_context *db, const char *key,
7660 const char *value)
7662 struct db_record *rec;
7663 TDB_DATA data = string_tdb_data(value);
7664 bool ret = false;
7665 NTSTATUS status;
7667 rec = db->fetch_locked(db, db, string_tdb_data(key));
7668 if (rec == NULL) {
7669 d_fprintf(stderr, "fetch_locked failed\n");
7670 goto done;
7672 status = rec->store(rec, data, 0);
7673 if (!NT_STATUS_IS_OK(status)) {
7674 d_fprintf(stderr, "store failed: %s\n", nt_errstr(status));
7675 goto done;
7677 TALLOC_FREE(rec);
7679 rec = db->fetch_locked(db, db, string_tdb_data(key));
7680 if (rec == NULL) {
7681 d_fprintf(stderr, "second fetch_locked failed\n");
7682 goto done;
7684 if ((rec->value.dsize != data.dsize)
7685 || (memcmp(rec->value.dptr, data.dptr, data.dsize) != 0)) {
7686 d_fprintf(stderr, "Got wrong data back\n");
7687 goto done;
7690 ret = true;
7691 done:
7692 TALLOC_FREE(rec);
7693 return ret;
7696 static bool run_local_rbtree(int dummy)
7698 struct db_context *db;
7699 bool ret = false;
7700 int i;
7702 db = db_open_rbt(NULL);
7704 if (db == NULL) {
7705 d_fprintf(stderr, "db_open_rbt failed\n");
7706 return false;
7709 for (i=0; i<1000; i++) {
7710 char *key, *value;
7712 if (asprintf(&key, "key%ld", random()) == -1) {
7713 goto done;
7715 if (asprintf(&value, "value%ld", random()) == -1) {
7716 SAFE_FREE(key);
7717 goto done;
7720 if (!rbt_testval(db, key, value)) {
7721 SAFE_FREE(key);
7722 SAFE_FREE(value);
7723 goto done;
7726 SAFE_FREE(value);
7727 if (asprintf(&value, "value%ld", random()) == -1) {
7728 SAFE_FREE(key);
7729 goto done;
7732 if (!rbt_testval(db, key, value)) {
7733 SAFE_FREE(key);
7734 SAFE_FREE(value);
7735 goto done;
7738 SAFE_FREE(key);
7739 SAFE_FREE(value);
7742 ret = true;
7744 done:
7745 TALLOC_FREE(db);
7746 return ret;
7751 local test for character set functions
7753 This is a very simple test for the functionality in convert_string_error()
7755 static bool run_local_convert_string(int dummy)
7757 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
7758 const char *test_strings[2] = { "March", "M\303\244rz" };
7759 char dst[7];
7760 int i;
7762 for (i=0; i<2; i++) {
7763 const char *str = test_strings[i];
7764 int len = strlen(str);
7765 size_t converted_size;
7766 bool ret;
7768 memset(dst, 'X', sizeof(dst));
7770 /* first try with real source length */
7771 ret = convert_string_error(CH_UNIX, CH_UTF8,
7772 str, len,
7773 dst, sizeof(dst),
7774 &converted_size);
7775 if (ret != true) {
7776 d_fprintf(stderr, "Failed to convert '%s' to CH_DISPLAY\n", str);
7777 goto failed;
7780 if (converted_size != len) {
7781 d_fprintf(stderr, "Converted size of '%s' should be %d - got %d\n",
7782 str, len, (int)converted_size);
7783 goto failed;
7786 if (strncmp(str, dst, converted_size) != 0) {
7787 d_fprintf(stderr, "Expected '%s' to match '%s'\n", str, dst);
7788 goto failed;
7791 if (strlen(str) != converted_size) {
7792 d_fprintf(stderr, "Expected '%s' length %d - got %d\n", str,
7793 (int)strlen(str), (int)converted_size);
7794 goto failed;
7797 if (dst[converted_size] != 'X') {
7798 d_fprintf(stderr, "Expected no termination of '%s'\n", dst);
7799 goto failed;
7802 /* now with srclen==-1, this causes the nul to be
7803 * converted too */
7804 ret = convert_string_error(CH_UNIX, CH_UTF8,
7805 str, -1,
7806 dst, sizeof(dst),
7807 &converted_size);
7808 if (ret != true) {
7809 d_fprintf(stderr, "Failed to convert '%s' to CH_DISPLAY\n", str);
7810 goto failed;
7813 if (converted_size != len+1) {
7814 d_fprintf(stderr, "Converted size of '%s' should be %d - got %d\n",
7815 str, len, (int)converted_size);
7816 goto failed;
7819 if (strncmp(str, dst, converted_size) != 0) {
7820 d_fprintf(stderr, "Expected '%s' to match '%s'\n", str, dst);
7821 goto failed;
7824 if (len+1 != converted_size) {
7825 d_fprintf(stderr, "Expected '%s' length %d - got %d\n", str,
7826 len+1, (int)converted_size);
7827 goto failed;
7830 if (dst[converted_size] != 'X') {
7831 d_fprintf(stderr, "Expected no termination of '%s'\n", dst);
7832 goto failed;
7838 TALLOC_FREE(tmp_ctx);
7839 return true;
7840 failed:
7841 TALLOC_FREE(tmp_ctx);
7842 return false;
7846 struct talloc_dict_test {
7847 int content;
7850 static int talloc_dict_traverse_fn(DATA_BLOB key, void *data, void *priv)
7852 int *count = (int *)priv;
7853 *count += 1;
7854 return 0;
7857 static bool run_local_talloc_dict(int dummy)
7859 struct talloc_dict *dict;
7860 struct talloc_dict_test *t;
7861 int key, count;
7863 dict = talloc_dict_init(talloc_tos());
7864 if (dict == NULL) {
7865 return false;
7868 t = talloc(talloc_tos(), struct talloc_dict_test);
7869 if (t == NULL) {
7870 return false;
7873 key = 1;
7874 t->content = 1;
7875 if (!talloc_dict_set(dict, data_blob_const(&key, sizeof(key)), t)) {
7876 return false;
7879 count = 0;
7880 if (talloc_dict_traverse(dict, talloc_dict_traverse_fn, &count) != 0) {
7881 return false;
7884 if (count != 1) {
7885 return false;
7888 TALLOC_FREE(dict);
7890 return true;
7893 static bool run_local_string_to_sid(int dummy) {
7894 struct dom_sid sid;
7896 if (string_to_sid(&sid, "S--1-5-32-545")) {
7897 printf("allowing S--1-5-32-545\n");
7898 return false;
7900 if (string_to_sid(&sid, "S-1-5-32-+545")) {
7901 printf("allowing S-1-5-32-+545\n");
7902 return false;
7904 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")) {
7905 printf("allowing S-1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0\n");
7906 return false;
7908 if (string_to_sid(&sid, "S-1-5-32-545-abc")) {
7909 printf("allowing S-1-5-32-545-abc\n");
7910 return false;
7912 if (!string_to_sid(&sid, "S-1-5-32-545")) {
7913 printf("could not parse S-1-5-32-545\n");
7914 return false;
7916 if (!dom_sid_equal(&sid, &global_sid_Builtin_Users)) {
7917 printf("mis-parsed S-1-5-32-545 as %s\n",
7918 sid_string_tos(&sid));
7919 return false;
7921 return true;
7924 static bool run_local_binary_to_sid(int dummy) {
7925 struct dom_sid *sid = talloc(NULL, struct dom_sid);
7926 static const char good_binary_sid[] = {
7927 0x1, /* revision number */
7928 15, /* num auths */
7929 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7930 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7931 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7932 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7933 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7934 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7935 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7936 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7937 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7938 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7939 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7940 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7941 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7942 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7943 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7944 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7947 static const char long_binary_sid[] = {
7948 0x1, /* revision number */
7949 15, /* num auths */
7950 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7951 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7952 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7953 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7954 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7955 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7956 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7957 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7958 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7959 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7960 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7961 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7962 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7963 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7964 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7965 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7966 0x1, 0x1, 0x1, 0x1, /* auth[15] */
7967 0x1, 0x1, 0x1, 0x1, /* auth[16] */
7968 0x1, 0x1, 0x1, 0x1, /* auth[17] */
7971 static const char long_binary_sid2[] = {
7972 0x1, /* revision number */
7973 32, /* num auths */
7974 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7975 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7976 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7977 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7978 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7979 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7980 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7981 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7982 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7983 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7984 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7985 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7986 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7987 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7988 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7989 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7990 0x1, 0x1, 0x1, 0x1, /* auth[15] */
7991 0x1, 0x1, 0x1, 0x1, /* auth[16] */
7992 0x1, 0x1, 0x1, 0x1, /* auth[17] */
7993 0x1, 0x1, 0x1, 0x1, /* auth[18] */
7994 0x1, 0x1, 0x1, 0x1, /* auth[19] */
7995 0x1, 0x1, 0x1, 0x1, /* auth[20] */
7996 0x1, 0x1, 0x1, 0x1, /* auth[21] */
7997 0x1, 0x1, 0x1, 0x1, /* auth[22] */
7998 0x1, 0x1, 0x1, 0x1, /* auth[23] */
7999 0x1, 0x1, 0x1, 0x1, /* auth[24] */
8000 0x1, 0x1, 0x1, 0x1, /* auth[25] */
8001 0x1, 0x1, 0x1, 0x1, /* auth[26] */
8002 0x1, 0x1, 0x1, 0x1, /* auth[27] */
8003 0x1, 0x1, 0x1, 0x1, /* auth[28] */
8004 0x1, 0x1, 0x1, 0x1, /* auth[29] */
8005 0x1, 0x1, 0x1, 0x1, /* auth[30] */
8006 0x1, 0x1, 0x1, 0x1, /* auth[31] */
8009 if (!sid_parse(good_binary_sid, sizeof(good_binary_sid), sid)) {
8010 return false;
8012 if (sid_parse(long_binary_sid2, sizeof(long_binary_sid2), sid)) {
8013 return false;
8015 if (sid_parse(long_binary_sid, sizeof(long_binary_sid), sid)) {
8016 return false;
8018 return true;
8021 /* Split a path name into filename and stream name components. Canonicalise
8022 * such that an implicit $DATA token is always explicit.
8024 * The "specification" of this function can be found in the
8025 * run_local_stream_name() function in torture.c, I've tried those
8026 * combinations against a W2k3 server.
8029 static NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
8030 char **pbase, char **pstream)
8032 char *base = NULL;
8033 char *stream = NULL;
8034 char *sname; /* stream name */
8035 const char *stype; /* stream type */
8037 DEBUG(10, ("split_ntfs_stream_name called for [%s]\n", fname));
8039 sname = strchr_m(fname, ':');
8041 if (lp_posix_pathnames() || (sname == NULL)) {
8042 if (pbase != NULL) {
8043 base = talloc_strdup(mem_ctx, fname);
8044 NT_STATUS_HAVE_NO_MEMORY(base);
8046 goto done;
8049 if (pbase != NULL) {
8050 base = talloc_strndup(mem_ctx, fname, PTR_DIFF(sname, fname));
8051 NT_STATUS_HAVE_NO_MEMORY(base);
8054 sname += 1;
8056 stype = strchr_m(sname, ':');
8058 if (stype == NULL) {
8059 sname = talloc_strdup(mem_ctx, sname);
8060 stype = "$DATA";
8062 else {
8063 if (strcasecmp_m(stype, ":$DATA") != 0) {
8065 * If there is an explicit stream type, so far we only
8066 * allow $DATA. Is there anything else allowed? -- vl
8068 DEBUG(10, ("[%s] is an invalid stream type\n", stype));
8069 TALLOC_FREE(base);
8070 return NT_STATUS_OBJECT_NAME_INVALID;
8072 sname = talloc_strndup(mem_ctx, sname, PTR_DIFF(stype, sname));
8073 stype += 1;
8076 if (sname == NULL) {
8077 TALLOC_FREE(base);
8078 return NT_STATUS_NO_MEMORY;
8081 if (sname[0] == '\0') {
8083 * no stream name, so no stream
8085 goto done;
8088 if (pstream != NULL) {
8089 stream = talloc_asprintf(mem_ctx, "%s:%s", sname, stype);
8090 if (stream == NULL) {
8091 TALLOC_FREE(sname);
8092 TALLOC_FREE(base);
8093 return NT_STATUS_NO_MEMORY;
8096 * upper-case the type field
8098 strupper_m(strchr_m(stream, ':')+1);
8101 done:
8102 if (pbase != NULL) {
8103 *pbase = base;
8105 if (pstream != NULL) {
8106 *pstream = stream;
8108 return NT_STATUS_OK;
8111 static bool test_stream_name(const char *fname, const char *expected_base,
8112 const char *expected_stream,
8113 NTSTATUS expected_status)
8115 NTSTATUS status;
8116 char *base = NULL;
8117 char *stream = NULL;
8119 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &stream);
8120 if (!NT_STATUS_EQUAL(status, expected_status)) {
8121 goto error;
8124 if (!NT_STATUS_IS_OK(status)) {
8125 return true;
8128 if (base == NULL) goto error;
8130 if (strcmp(expected_base, base) != 0) goto error;
8132 if ((expected_stream != NULL) && (stream == NULL)) goto error;
8133 if ((expected_stream == NULL) && (stream != NULL)) goto error;
8135 if ((stream != NULL) && (strcmp(expected_stream, stream) != 0))
8136 goto error;
8138 TALLOC_FREE(base);
8139 TALLOC_FREE(stream);
8140 return true;
8142 error:
8143 d_fprintf(stderr, "Do test_stream(%s, %s, %s, %s)\n",
8144 fname, expected_base ? expected_base : "<NULL>",
8145 expected_stream ? expected_stream : "<NULL>",
8146 nt_errstr(expected_status));
8147 d_fprintf(stderr, "-> base=%s, stream=%s, status=%s\n",
8148 base ? base : "<NULL>", stream ? stream : "<NULL>",
8149 nt_errstr(status));
8150 TALLOC_FREE(base);
8151 TALLOC_FREE(stream);
8152 return false;
8155 static bool run_local_stream_name(int dummy)
8157 bool ret = true;
8159 ret &= test_stream_name(
8160 "bla", "bla", NULL, NT_STATUS_OK);
8161 ret &= test_stream_name(
8162 "bla::$DATA", "bla", NULL, NT_STATUS_OK);
8163 ret &= test_stream_name(
8164 "bla:blub:", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
8165 ret &= test_stream_name(
8166 "bla::", NULL, NULL, NT_STATUS_OBJECT_NAME_INVALID);
8167 ret &= test_stream_name(
8168 "bla::123", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
8169 ret &= test_stream_name(
8170 "bla:$DATA", "bla", "$DATA:$DATA", NT_STATUS_OK);
8171 ret &= test_stream_name(
8172 "bla:x:$DATA", "bla", "x:$DATA", NT_STATUS_OK);
8173 ret &= test_stream_name(
8174 "bla:x", "bla", "x:$DATA", NT_STATUS_OK);
8176 return ret;
8179 static bool data_blob_equal(DATA_BLOB a, DATA_BLOB b)
8181 if (a.length != b.length) {
8182 printf("a.length=%d != b.length=%d\n",
8183 (int)a.length, (int)b.length);
8184 return false;
8186 if (memcmp(a.data, b.data, a.length) != 0) {
8187 printf("a.data and b.data differ\n");
8188 return false;
8190 return true;
8193 static bool run_local_memcache(int dummy)
8195 struct memcache *cache;
8196 DATA_BLOB k1, k2;
8197 DATA_BLOB d1, d2, d3;
8198 DATA_BLOB v1, v2, v3;
8200 TALLOC_CTX *mem_ctx;
8201 char *str1, *str2;
8202 size_t size1, size2;
8203 bool ret = false;
8205 cache = memcache_init(NULL, 100);
8207 if (cache == NULL) {
8208 printf("memcache_init failed\n");
8209 return false;
8212 d1 = data_blob_const("d1", 2);
8213 d2 = data_blob_const("d2", 2);
8214 d3 = data_blob_const("d3", 2);
8216 k1 = data_blob_const("d1", 2);
8217 k2 = data_blob_const("d2", 2);
8219 memcache_add(cache, STAT_CACHE, k1, d1);
8220 memcache_add(cache, GETWD_CACHE, k2, d2);
8222 if (!memcache_lookup(cache, STAT_CACHE, k1, &v1)) {
8223 printf("could not find k1\n");
8224 return false;
8226 if (!data_blob_equal(d1, v1)) {
8227 return false;
8230 if (!memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
8231 printf("could not find k2\n");
8232 return false;
8234 if (!data_blob_equal(d2, v2)) {
8235 return false;
8238 memcache_add(cache, STAT_CACHE, k1, d3);
8240 if (!memcache_lookup(cache, STAT_CACHE, k1, &v3)) {
8241 printf("could not find replaced k1\n");
8242 return false;
8244 if (!data_blob_equal(d3, v3)) {
8245 return false;
8248 memcache_add(cache, GETWD_CACHE, k1, d1);
8250 if (memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
8251 printf("Did find k2, should have been purged\n");
8252 return false;
8255 TALLOC_FREE(cache);
8257 cache = memcache_init(NULL, 0);
8259 mem_ctx = talloc_init("foo");
8261 str1 = talloc_strdup(mem_ctx, "string1");
8262 str2 = talloc_strdup(mem_ctx, "string2");
8264 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
8265 data_blob_string_const("torture"), &str1);
8266 size1 = talloc_total_size(cache);
8268 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
8269 data_blob_string_const("torture"), &str2);
8270 size2 = talloc_total_size(cache);
8272 printf("size1=%d, size2=%d\n", (int)size1, (int)size2);
8274 if (size2 > size1) {
8275 printf("memcache leaks memory!\n");
8276 goto fail;
8279 ret = true;
8280 fail:
8281 TALLOC_FREE(cache);
8282 return ret;
8285 static void wbclient_done(struct tevent_req *req)
8287 wbcErr wbc_err;
8288 struct winbindd_response *wb_resp;
8289 int *i = (int *)tevent_req_callback_data_void(req);
8291 wbc_err = wb_trans_recv(req, req, &wb_resp);
8292 TALLOC_FREE(req);
8293 *i += 1;
8294 d_printf("wb_trans_recv %d returned %s\n", *i, wbcErrorString(wbc_err));
8297 static bool run_local_wbclient(int dummy)
8299 struct event_context *ev;
8300 struct wb_context **wb_ctx;
8301 struct winbindd_request wb_req;
8302 bool result = false;
8303 int i, j;
8305 BlockSignals(True, SIGPIPE);
8307 ev = tevent_context_init_byname(talloc_tos(), "epoll");
8308 if (ev == NULL) {
8309 goto fail;
8312 wb_ctx = talloc_array(ev, struct wb_context *, nprocs);
8313 if (wb_ctx == NULL) {
8314 goto fail;
8317 ZERO_STRUCT(wb_req);
8318 wb_req.cmd = WINBINDD_PING;
8320 d_printf("nprocs=%d, numops=%d\n", (int)nprocs, (int)torture_numops);
8322 for (i=0; i<nprocs; i++) {
8323 wb_ctx[i] = wb_context_init(ev, NULL);
8324 if (wb_ctx[i] == NULL) {
8325 goto fail;
8327 for (j=0; j<torture_numops; j++) {
8328 struct tevent_req *req;
8329 req = wb_trans_send(ev, ev, wb_ctx[i],
8330 (j % 2) == 0, &wb_req);
8331 if (req == NULL) {
8332 goto fail;
8334 tevent_req_set_callback(req, wbclient_done, &i);
8338 i = 0;
8340 while (i < nprocs * torture_numops) {
8341 event_loop_once(ev);
8344 result = true;
8345 fail:
8346 TALLOC_FREE(ev);
8347 return result;
8350 static void getaddrinfo_finished(struct tevent_req *req)
8352 char *name = (char *)tevent_req_callback_data_void(req);
8353 struct addrinfo *ainfo;
8354 int res;
8356 res = getaddrinfo_recv(req, &ainfo);
8357 if (res != 0) {
8358 d_printf("gai(%s) returned %s\n", name, gai_strerror(res));
8359 return;
8361 d_printf("gai(%s) succeeded\n", name);
8362 freeaddrinfo(ainfo);
8365 static bool run_getaddrinfo_send(int dummy)
8367 TALLOC_CTX *frame = talloc_stackframe();
8368 struct fncall_context *ctx;
8369 struct tevent_context *ev;
8370 bool result = false;
8371 const char *names[4] = { "www.samba.org", "notfound.samba.org",
8372 "www.slashdot.org", "heise.de" };
8373 struct tevent_req *reqs[4];
8374 int i;
8376 ev = event_context_init(frame);
8377 if (ev == NULL) {
8378 goto fail;
8381 ctx = fncall_context_init(frame, 4);
8383 for (i=0; i<ARRAY_SIZE(names); i++) {
8384 reqs[i] = getaddrinfo_send(frame, ev, ctx, names[i], NULL,
8385 NULL);
8386 if (reqs[i] == NULL) {
8387 goto fail;
8389 tevent_req_set_callback(reqs[i], getaddrinfo_finished,
8390 discard_const_p(void, names[i]));
8393 for (i=0; i<ARRAY_SIZE(reqs); i++) {
8394 tevent_loop_once(ev);
8397 result = true;
8398 fail:
8399 TALLOC_FREE(frame);
8400 return result;
8403 static bool dbtrans_inc(struct db_context *db)
8405 struct db_record *rec;
8406 uint32_t *val;
8407 bool ret = false;
8408 NTSTATUS status;
8410 rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
8411 if (rec == NULL) {
8412 printf(__location__ "fetch_lock failed\n");
8413 return false;
8416 if (rec->value.dsize != sizeof(uint32_t)) {
8417 printf(__location__ "value.dsize = %d\n",
8418 (int)rec->value.dsize);
8419 goto fail;
8422 val = (uint32_t *)rec->value.dptr;
8423 *val += 1;
8425 status = rec->store(rec, make_tdb_data((uint8_t *)val,
8426 sizeof(uint32_t)),
8428 if (!NT_STATUS_IS_OK(status)) {
8429 printf(__location__ "store failed: %s\n",
8430 nt_errstr(status));
8431 goto fail;
8434 ret = true;
8435 fail:
8436 TALLOC_FREE(rec);
8437 return ret;
8440 static bool run_local_dbtrans(int dummy)
8442 struct db_context *db;
8443 struct db_record *rec;
8444 NTSTATUS status;
8445 uint32_t initial;
8446 int res;
8448 db = db_open(talloc_tos(), "transtest.tdb", 0, TDB_DEFAULT,
8449 O_RDWR|O_CREAT, 0600);
8450 if (db == NULL) {
8451 printf("Could not open transtest.db\n");
8452 return false;
8455 res = db->transaction_start(db);
8456 if (res != 0) {
8457 printf(__location__ "transaction_start failed\n");
8458 return false;
8461 rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
8462 if (rec == NULL) {
8463 printf(__location__ "fetch_lock failed\n");
8464 return false;
8467 if (rec->value.dptr == NULL) {
8468 initial = 0;
8469 status = rec->store(
8470 rec, make_tdb_data((uint8_t *)&initial,
8471 sizeof(initial)),
8473 if (!NT_STATUS_IS_OK(status)) {
8474 printf(__location__ "store returned %s\n",
8475 nt_errstr(status));
8476 return false;
8480 TALLOC_FREE(rec);
8482 res = db->transaction_commit(db);
8483 if (res != 0) {
8484 printf(__location__ "transaction_commit failed\n");
8485 return false;
8488 while (true) {
8489 uint32_t val, val2;
8490 int i;
8492 res = db->transaction_start(db);
8493 if (res != 0) {
8494 printf(__location__ "transaction_start failed\n");
8495 break;
8498 if (!dbwrap_fetch_uint32(db, "transtest", &val)) {
8499 printf(__location__ "dbwrap_fetch_uint32 failed\n");
8500 break;
8503 for (i=0; i<10; i++) {
8504 if (!dbtrans_inc(db)) {
8505 return false;
8509 if (!dbwrap_fetch_uint32(db, "transtest", &val2)) {
8510 printf(__location__ "dbwrap_fetch_uint32 failed\n");
8511 break;
8514 if (val2 != val + 10) {
8515 printf(__location__ "val=%d, val2=%d\n",
8516 (int)val, (int)val2);
8517 break;
8520 printf("val2=%d\r", val2);
8522 res = db->transaction_commit(db);
8523 if (res != 0) {
8524 printf(__location__ "transaction_commit failed\n");
8525 break;
8529 TALLOC_FREE(db);
8530 return true;
8534 * Just a dummy test to be run under a debugger. There's no real way
8535 * to inspect the tevent_select specific function from outside of
8536 * tevent_select.c.
8539 static bool run_local_tevent_select(int dummy)
8541 struct tevent_context *ev;
8542 struct tevent_fd *fd1, *fd2;
8543 bool result = false;
8545 ev = tevent_context_init_byname(NULL, "select");
8546 if (ev == NULL) {
8547 d_fprintf(stderr, "tevent_context_init_byname failed\n");
8548 goto fail;
8551 fd1 = tevent_add_fd(ev, ev, 2, 0, NULL, NULL);
8552 if (fd1 == NULL) {
8553 d_fprintf(stderr, "tevent_add_fd failed\n");
8554 goto fail;
8556 fd2 = tevent_add_fd(ev, ev, 3, 0, NULL, NULL);
8557 if (fd2 == NULL) {
8558 d_fprintf(stderr, "tevent_add_fd failed\n");
8559 goto fail;
8561 TALLOC_FREE(fd2);
8563 fd2 = tevent_add_fd(ev, ev, 1, 0, NULL, NULL);
8564 if (fd2 == NULL) {
8565 d_fprintf(stderr, "tevent_add_fd failed\n");
8566 goto fail;
8569 result = true;
8570 fail:
8571 TALLOC_FREE(ev);
8572 return result;
8575 static double create_procs(bool (*fn)(int), bool *result)
8577 int i, status;
8578 volatile pid_t *child_status;
8579 volatile bool *child_status_out;
8580 int synccount;
8581 int tries = 8;
8582 struct timeval start;
8584 synccount = 0;
8586 child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*nprocs);
8587 if (!child_status) {
8588 printf("Failed to setup shared memory\n");
8589 return -1;
8592 child_status_out = (volatile bool *)shm_setup(sizeof(bool)*nprocs);
8593 if (!child_status_out) {
8594 printf("Failed to setup result status shared memory\n");
8595 return -1;
8598 for (i = 0; i < nprocs; i++) {
8599 child_status[i] = 0;
8600 child_status_out[i] = True;
8603 start = timeval_current();
8605 for (i=0;i<nprocs;i++) {
8606 procnum = i;
8607 if (fork() == 0) {
8608 pid_t mypid = getpid();
8609 sys_srandom(((int)mypid) ^ ((int)time(NULL)));
8611 slprintf(myname,sizeof(myname),"CLIENT%d", i);
8613 while (1) {
8614 if (torture_open_connection(&current_cli, i)) break;
8615 if (tries-- == 0) {
8616 printf("pid %d failed to start\n", (int)getpid());
8617 _exit(1);
8619 smb_msleep(10);
8622 child_status[i] = getpid();
8624 while (child_status[i] && timeval_elapsed(&start) < 5) smb_msleep(2);
8626 child_status_out[i] = fn(i);
8627 _exit(0);
8631 do {
8632 synccount = 0;
8633 for (i=0;i<nprocs;i++) {
8634 if (child_status[i]) synccount++;
8636 if (synccount == nprocs) break;
8637 smb_msleep(10);
8638 } while (timeval_elapsed(&start) < 30);
8640 if (synccount != nprocs) {
8641 printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount);
8642 *result = False;
8643 return timeval_elapsed(&start);
8646 /* start the client load */
8647 start = timeval_current();
8649 for (i=0;i<nprocs;i++) {
8650 child_status[i] = 0;
8653 printf("%d clients started\n", nprocs);
8655 for (i=0;i<nprocs;i++) {
8656 while (waitpid(0, &status, 0) == -1 && errno == EINTR) /* noop */ ;
8659 printf("\n");
8661 for (i=0;i<nprocs;i++) {
8662 if (!child_status_out[i]) {
8663 *result = False;
8666 return timeval_elapsed(&start);
8669 #define FLAG_MULTIPROC 1
8671 static struct {
8672 const char *name;
8673 bool (*fn)(int);
8674 unsigned flags;
8675 } torture_ops[] = {
8676 {"FDPASS", run_fdpasstest, 0},
8677 {"LOCK1", run_locktest1, 0},
8678 {"LOCK2", run_locktest2, 0},
8679 {"LOCK3", run_locktest3, 0},
8680 {"LOCK4", run_locktest4, 0},
8681 {"LOCK5", run_locktest5, 0},
8682 {"LOCK6", run_locktest6, 0},
8683 {"LOCK7", run_locktest7, 0},
8684 {"LOCK8", run_locktest8, 0},
8685 {"LOCK9", run_locktest9, 0},
8686 {"UNLINK", run_unlinktest, 0},
8687 {"BROWSE", run_browsetest, 0},
8688 {"ATTR", run_attrtest, 0},
8689 {"TRANS2", run_trans2test, 0},
8690 {"MAXFID", run_maxfidtest, FLAG_MULTIPROC},
8691 {"TORTURE",run_torture, FLAG_MULTIPROC},
8692 {"RANDOMIPC", run_randomipc, 0},
8693 {"NEGNOWAIT", run_negprot_nowait, 0},
8694 {"NBENCH", run_nbench, 0},
8695 {"NBENCH2", run_nbench2, 0},
8696 {"OPLOCK1", run_oplock1, 0},
8697 {"OPLOCK2", run_oplock2, 0},
8698 {"OPLOCK4", run_oplock4, 0},
8699 {"DIR", run_dirtest, 0},
8700 {"DIR1", run_dirtest1, 0},
8701 {"DIR-CREATETIME", run_dir_createtime, 0},
8702 {"DENY1", torture_denytest1, 0},
8703 {"DENY2", torture_denytest2, 0},
8704 {"TCON", run_tcon_test, 0},
8705 {"TCONDEV", run_tcon_devtype_test, 0},
8706 {"RW1", run_readwritetest, 0},
8707 {"RW2", run_readwritemulti, FLAG_MULTIPROC},
8708 {"RW3", run_readwritelarge, 0},
8709 {"RW-SIGNING", run_readwritelarge_signtest, 0},
8710 {"OPEN", run_opentest, 0},
8711 {"POSIX", run_simple_posix_open_test, 0},
8712 {"POSIX-APPEND", run_posix_append, 0},
8713 {"CASE-INSENSITIVE-CREATE", run_case_insensitive_create, 0},
8714 {"ASYNC-ECHO", run_async_echo, 0},
8715 { "UID-REGRESSION-TEST", run_uid_regression_test, 0},
8716 { "SHORTNAME-TEST", run_shortname_test, 0},
8717 { "ADDRCHANGE", run_addrchange, 0},
8718 #if 1
8719 {"OPENATTR", run_openattrtest, 0},
8720 #endif
8721 {"XCOPY", run_xcopy, 0},
8722 {"RENAME", run_rename, 0},
8723 {"DELETE", run_deletetest, 0},
8724 {"DELETE-LN", run_deletetest_ln, 0},
8725 {"PROPERTIES", run_properties, 0},
8726 {"MANGLE", torture_mangle, 0},
8727 {"MANGLE1", run_mangle1, 0},
8728 {"W2K", run_w2ktest, 0},
8729 {"TRANS2SCAN", torture_trans2_scan, 0},
8730 {"NTTRANSSCAN", torture_nttrans_scan, 0},
8731 {"UTABLE", torture_utable, 0},
8732 {"CASETABLE", torture_casetable, 0},
8733 {"ERRMAPEXTRACT", run_error_map_extract, 0},
8734 {"PIPE_NUMBER", run_pipe_number, 0},
8735 {"TCON2", run_tcon2_test, 0},
8736 {"IOCTL", torture_ioctl_test, 0},
8737 {"CHKPATH", torture_chkpath_test, 0},
8738 {"FDSESS", run_fdsesstest, 0},
8739 { "EATEST", run_eatest, 0},
8740 { "SESSSETUP_BENCH", run_sesssetup_bench, 0},
8741 { "CHAIN1", run_chain1, 0},
8742 { "CHAIN2", run_chain2, 0},
8743 { "WINDOWS-WRITE", run_windows_write, 0},
8744 { "NTTRANS-CREATE", run_nttrans_create, 0},
8745 { "CLI_ECHO", run_cli_echo, 0},
8746 { "GETADDRINFO", run_getaddrinfo_send, 0},
8747 { "TLDAP", run_tldap },
8748 { "STREAMERROR", run_streamerror },
8749 { "NOTIFY-BENCH", run_notify_bench },
8750 { "BAD-NBT-SESSION", run_bad_nbt_session },
8751 { "SMB-ANY-CONNECT", run_smb_any_connect },
8752 { "NOTIFY-ONLINE", run_notify_online },
8753 { "SMB2-BASIC", run_smb2_basic },
8754 { "LOCAL-SUBSTITUTE", run_local_substitute, 0},
8755 { "LOCAL-GENCACHE", run_local_gencache, 0},
8756 { "LOCAL-TALLOC-DICT", run_local_talloc_dict, 0},
8757 { "LOCAL-BASE64", run_local_base64, 0},
8758 { "LOCAL-RBTREE", run_local_rbtree, 0},
8759 { "LOCAL-MEMCACHE", run_local_memcache, 0},
8760 { "LOCAL-STREAM-NAME", run_local_stream_name, 0},
8761 { "LOCAL-WBCLIENT", run_local_wbclient, 0},
8762 { "LOCAL-string_to_sid", run_local_string_to_sid, 0},
8763 { "LOCAL-binary_to_sid", run_local_binary_to_sid, 0},
8764 { "LOCAL-DBTRANS", run_local_dbtrans, 0},
8765 { "LOCAL-TEVENT-SELECT", run_local_tevent_select, 0},
8766 { "LOCAL-CONVERT-STRING", run_local_convert_string, 0},
8767 {NULL, NULL, 0}};
8771 /****************************************************************************
8772 run a specified test or "ALL"
8773 ****************************************************************************/
8774 static bool run_test(const char *name)
8776 bool ret = True;
8777 bool result = True;
8778 bool found = False;
8779 int i;
8780 double t;
8781 if (strequal(name,"ALL")) {
8782 for (i=0;torture_ops[i].name;i++) {
8783 run_test(torture_ops[i].name);
8785 found = True;
8788 for (i=0;torture_ops[i].name;i++) {
8789 fstr_sprintf(randomfname, "\\XX%x",
8790 (unsigned)random());
8792 if (strequal(name, torture_ops[i].name)) {
8793 found = True;
8794 printf("Running %s\n", name);
8795 if (torture_ops[i].flags & FLAG_MULTIPROC) {
8796 t = create_procs(torture_ops[i].fn, &result);
8797 if (!result) {
8798 ret = False;
8799 printf("TEST %s FAILED!\n", name);
8801 } else {
8802 struct timeval start;
8803 start = timeval_current();
8804 if (!torture_ops[i].fn(0)) {
8805 ret = False;
8806 printf("TEST %s FAILED!\n", name);
8808 t = timeval_elapsed(&start);
8810 printf("%s took %g secs\n\n", name, t);
8814 if (!found) {
8815 printf("Did not find a test named %s\n", name);
8816 ret = False;
8819 return ret;
8823 static void usage(void)
8825 int i;
8827 printf("WARNING samba4 test suite is much more complete nowadays.\n");
8828 printf("Please use samba4 torture.\n\n");
8830 printf("Usage: smbtorture //server/share <options> TEST1 TEST2 ...\n");
8832 printf("\t-d debuglevel\n");
8833 printf("\t-U user%%pass\n");
8834 printf("\t-k use kerberos\n");
8835 printf("\t-N numprocs\n");
8836 printf("\t-n my_netbios_name\n");
8837 printf("\t-W workgroup\n");
8838 printf("\t-o num_operations\n");
8839 printf("\t-O socket_options\n");
8840 printf("\t-m maximum protocol\n");
8841 printf("\t-L use oplocks\n");
8842 printf("\t-c CLIENT.TXT specify client load file for NBENCH\n");
8843 printf("\t-A showall\n");
8844 printf("\t-p port\n");
8845 printf("\t-s seed\n");
8846 printf("\t-b unclist_filename specify multiple shares for multiple connections\n");
8847 printf("\t-f filename filename to test\n");
8848 printf("\n\n");
8850 printf("tests are:");
8851 for (i=0;torture_ops[i].name;i++) {
8852 printf(" %s", torture_ops[i].name);
8854 printf("\n");
8856 printf("default test is ALL\n");
8858 exit(1);
8861 /****************************************************************************
8862 main program
8863 ****************************************************************************/
8864 int main(int argc,char *argv[])
8866 int opt, i;
8867 char *p;
8868 int gotuser = 0;
8869 int gotpass = 0;
8870 bool correct = True;
8871 TALLOC_CTX *frame = talloc_stackframe();
8872 int seed = time(NULL);
8874 #ifdef HAVE_SETBUFFER
8875 setbuffer(stdout, NULL, 0);
8876 #endif
8878 setup_logging("smbtorture", DEBUG_STDOUT);
8880 load_case_tables();
8882 if (is_default_dyn_CONFIGFILE()) {
8883 if(getenv("SMB_CONF_PATH")) {
8884 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
8887 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
8888 load_interfaces();
8890 if (argc < 2) {
8891 usage();
8894 for(p = argv[1]; *p; p++)
8895 if(*p == '\\')
8896 *p = '/';
8898 if (strncmp(argv[1], "//", 2)) {
8899 usage();
8902 fstrcpy(host, &argv[1][2]);
8903 p = strchr_m(&host[2],'/');
8904 if (!p) {
8905 usage();
8907 *p = 0;
8908 fstrcpy(share, p+1);
8910 fstrcpy(myname, get_myname(talloc_tos()));
8911 if (!*myname) {
8912 fprintf(stderr, "Failed to get my hostname.\n");
8913 return 1;
8916 if (*username == 0 && getenv("LOGNAME")) {
8917 fstrcpy(username,getenv("LOGNAME"));
8920 argc--;
8921 argv++;
8923 fstrcpy(workgroup, lp_workgroup());
8925 while ((opt = getopt(argc, argv, "p:hW:U:n:N:O:o:m:Ll:d:Aec:ks:b:B:f:"))
8926 != EOF) {
8927 switch (opt) {
8928 case 'p':
8929 port_to_use = atoi(optarg);
8930 break;
8931 case 's':
8932 seed = atoi(optarg);
8933 break;
8934 case 'W':
8935 fstrcpy(workgroup,optarg);
8936 break;
8937 case 'm':
8938 max_protocol = interpret_protocol(optarg, max_protocol);
8939 break;
8940 case 'N':
8941 nprocs = atoi(optarg);
8942 break;
8943 case 'o':
8944 torture_numops = atoi(optarg);
8945 break;
8946 case 'd':
8947 lp_set_cmdline("log level", optarg);
8948 break;
8949 case 'O':
8950 sockops = optarg;
8951 break;
8952 case 'L':
8953 use_oplocks = True;
8954 break;
8955 case 'l':
8956 local_path = optarg;
8957 break;
8958 case 'A':
8959 torture_showall = True;
8960 break;
8961 case 'n':
8962 fstrcpy(myname, optarg);
8963 break;
8964 case 'c':
8965 client_txt = optarg;
8966 break;
8967 case 'e':
8968 do_encrypt = true;
8969 break;
8970 case 'k':
8971 #ifdef HAVE_KRB5
8972 use_kerberos = True;
8973 #else
8974 d_printf("No kerberos support compiled in\n");
8975 exit(1);
8976 #endif
8977 break;
8978 case 'U':
8979 gotuser = 1;
8980 fstrcpy(username,optarg);
8981 p = strchr_m(username,'%');
8982 if (p) {
8983 *p = 0;
8984 fstrcpy(password, p+1);
8985 gotpass = 1;
8987 break;
8988 case 'b':
8989 fstrcpy(multishare_conn_fname, optarg);
8990 use_multishare_conn = True;
8991 break;
8992 case 'B':
8993 torture_blocksize = atoi(optarg);
8994 break;
8995 case 'f':
8996 test_filename = SMB_STRDUP(optarg);
8997 break;
8998 default:
8999 printf("Unknown option %c (%d)\n", (char)opt, opt);
9000 usage();
9004 d_printf("using seed %d\n", seed);
9006 srandom(seed);
9008 if(use_kerberos && !gotuser) gotpass = True;
9010 while (!gotpass) {
9011 p = getpass("Password:");
9012 if (p) {
9013 fstrcpy(password, p);
9014 gotpass = 1;
9018 printf("host=%s share=%s user=%s myname=%s\n",
9019 host, share, username, myname);
9021 if (argc == optind) {
9022 correct = run_test("ALL");
9023 } else {
9024 for (i=optind;i<argc;i++) {
9025 if (!run_test(argv[i])) {
9026 correct = False;
9031 TALLOC_FREE(frame);
9033 if (correct) {
9034 return(0);
9035 } else {
9036 return(1);