s3-torture: run_locktest7(): replace cli_lock() with cli_lock32()
[Samba/gebeck_regimport.git] / source3 / torture / torture.c
blob4cec3c22e54aff8ec709ec3216c35277d8819257
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 status = cli_lock32(cli1, fnum1, 130, 4, 0, READ_LOCK);
2286 if (!NT_STATUS_IS_OK(status)) {
2287 printf("Unable to apply read lock on range 130:4, error was %s\n", nt_errstr(status));
2288 goto fail;
2289 } else {
2290 printf("pid1 successfully locked range 130:4 for READ\n");
2293 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2294 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2295 goto fail;
2296 } else {
2297 printf("pid1 successfully read the range 130:4\n");
2300 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2301 if (!NT_STATUS_IS_OK(status)) {
2302 printf("pid1 unable to write to the range 130:4, error was "
2303 "%s\n", nt_errstr(status));
2304 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
2305 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2306 goto fail;
2308 } else {
2309 printf("pid1 successfully wrote to the range 130:4 (should be denied)\n");
2310 goto fail;
2313 cli_setpid(cli1, 2);
2315 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2316 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2317 } else {
2318 printf("pid2 successfully read the range 130:4\n");
2321 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2322 if (!NT_STATUS_IS_OK(status)) {
2323 printf("pid2 unable to write to the range 130:4, error was "
2324 "%s\n", nt_errstr(status));
2325 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
2326 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2327 goto fail;
2329 } else {
2330 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2331 goto fail;
2334 cli_setpid(cli1, 1);
2335 cli_unlock(cli1, fnum1, 130, 4);
2337 status = cli_lock32(cli1, fnum1, 130, 4, 0, WRITE_LOCK);
2338 if (!NT_STATUS_IS_OK(status)) {
2339 printf("Unable to apply write lock on range 130:4, error was %s\n", nt_errstr(status));
2340 goto fail;
2341 } else {
2342 printf("pid1 successfully locked range 130:4 for WRITE\n");
2345 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2346 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2347 goto fail;
2348 } else {
2349 printf("pid1 successfully read the range 130:4\n");
2352 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2353 if (!NT_STATUS_IS_OK(status)) {
2354 printf("pid1 unable to write to the range 130:4, error was "
2355 "%s\n", nt_errstr(status));
2356 goto fail;
2357 } else {
2358 printf("pid1 successfully wrote to the range 130:4\n");
2361 cli_setpid(cli1, 2);
2363 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2364 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2365 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2366 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2367 goto fail;
2369 } else {
2370 printf("pid2 successfully read the range 130:4 (should be denied)\n");
2371 goto fail;
2374 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
2375 if (!NT_STATUS_IS_OK(status)) {
2376 printf("pid2 unable to write to the range 130:4, error was "
2377 "%s\n", nt_errstr(status));
2378 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
2379 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2380 goto fail;
2382 } else {
2383 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2384 goto fail;
2387 cli_unlock(cli1, fnum1, 130, 0);
2388 correct = True;
2390 fail:
2391 cli_close(cli1, fnum1);
2392 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2393 torture_close_connection(cli1);
2395 printf("finished locktest7\n");
2396 return correct;
2400 * This demonstrates a problem with our use of GPFS share modes: A file
2401 * descriptor sitting in the pending close queue holding a GPFS share mode
2402 * blocks opening a file another time. Happens with Word 2007 temp files.
2403 * With "posix locking = yes" and "gpfs:sharemodes = yes" enabled, the third
2404 * open is denied with NT_STATUS_SHARING_VIOLATION.
2407 static bool run_locktest8(int dummy)
2409 struct cli_state *cli1;
2410 const char *fname = "\\lockt8.lck";
2411 uint16_t fnum1, fnum2;
2412 char buf[200];
2413 bool correct = False;
2414 NTSTATUS status;
2416 if (!torture_open_connection(&cli1, 0)) {
2417 return False;
2420 cli_sockopt(cli1, sockops);
2422 printf("starting locktest8\n");
2424 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2426 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_WRITE,
2427 &fnum1);
2428 if (!NT_STATUS_IS_OK(status)) {
2429 d_fprintf(stderr, "cli_open returned %s\n", nt_errstr(status));
2430 return false;
2433 memset(buf, 0, sizeof(buf));
2435 status = cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum2);
2436 if (!NT_STATUS_IS_OK(status)) {
2437 d_fprintf(stderr, "cli_open second time returned %s\n",
2438 nt_errstr(status));
2439 goto fail;
2442 if (!cli_lock(cli1, fnum2, 1, 1, 0, READ_LOCK)) {
2443 printf("Unable to apply read lock on range 1:1, error was "
2444 "%s\n", cli_errstr(cli1));
2445 goto fail;
2448 status = cli_close(cli1, fnum1);
2449 if (!NT_STATUS_IS_OK(status)) {
2450 d_fprintf(stderr, "cli_close(fnum1) %s\n", nt_errstr(status));
2451 goto fail;
2454 status = cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
2455 if (!NT_STATUS_IS_OK(status)) {
2456 d_fprintf(stderr, "cli_open third time returned %s\n",
2457 nt_errstr(status));
2458 goto fail;
2461 correct = true;
2463 fail:
2464 cli_close(cli1, fnum1);
2465 cli_close(cli1, fnum2);
2466 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2467 torture_close_connection(cli1);
2469 printf("finished locktest8\n");
2470 return correct;
2474 * This test is designed to be run in conjunction with
2475 * external NFS or POSIX locks taken in the filesystem.
2476 * It checks that the smbd server will block until the
2477 * lock is released and then acquire it. JRA.
2480 static bool got_alarm;
2481 static int alarm_fd;
2483 static void alarm_handler(int dummy)
2485 got_alarm = True;
2488 static void alarm_handler_parent(int dummy)
2490 close(alarm_fd);
2493 static void do_local_lock(int read_fd, int write_fd)
2495 int fd;
2496 char c = '\0';
2497 struct flock lock;
2498 const char *local_pathname = NULL;
2499 int ret;
2501 local_pathname = talloc_asprintf(talloc_tos(),
2502 "%s/lockt9.lck", local_path);
2503 if (!local_pathname) {
2504 printf("child: alloc fail\n");
2505 exit(1);
2508 unlink(local_pathname);
2509 fd = open(local_pathname, O_RDWR|O_CREAT, 0666);
2510 if (fd == -1) {
2511 printf("child: open of %s failed %s.\n",
2512 local_pathname, strerror(errno));
2513 exit(1);
2516 /* Now take a fcntl lock. */
2517 lock.l_type = F_WRLCK;
2518 lock.l_whence = SEEK_SET;
2519 lock.l_start = 0;
2520 lock.l_len = 4;
2521 lock.l_pid = getpid();
2523 ret = fcntl(fd,F_SETLK,&lock);
2524 if (ret == -1) {
2525 printf("child: failed to get lock 0:4 on file %s. Error %s\n",
2526 local_pathname, strerror(errno));
2527 exit(1);
2528 } else {
2529 printf("child: got lock 0:4 on file %s.\n",
2530 local_pathname );
2531 fflush(stdout);
2534 CatchSignal(SIGALRM, alarm_handler);
2535 alarm(5);
2536 /* Signal the parent. */
2537 if (write(write_fd, &c, 1) != 1) {
2538 printf("child: start signal fail %s.\n",
2539 strerror(errno));
2540 exit(1);
2542 alarm(0);
2544 alarm(10);
2545 /* Wait for the parent to be ready. */
2546 if (read(read_fd, &c, 1) != 1) {
2547 printf("child: reply signal fail %s.\n",
2548 strerror(errno));
2549 exit(1);
2551 alarm(0);
2553 sleep(5);
2554 close(fd);
2555 printf("child: released lock 0:4 on file %s.\n",
2556 local_pathname );
2557 fflush(stdout);
2558 exit(0);
2561 static bool run_locktest9(int dummy)
2563 struct cli_state *cli1;
2564 const char *fname = "\\lockt9.lck";
2565 uint16_t fnum;
2566 bool correct = False;
2567 int pipe_in[2], pipe_out[2];
2568 pid_t child_pid;
2569 char c = '\0';
2570 int ret;
2571 struct timeval start;
2572 double seconds;
2573 NTSTATUS status;
2575 printf("starting locktest9\n");
2577 if (local_path == NULL) {
2578 d_fprintf(stderr, "locktest9 must be given a local path via -l <localpath>\n");
2579 return false;
2582 if (pipe(pipe_in) == -1 || pipe(pipe_out) == -1) {
2583 return false;
2586 child_pid = fork();
2587 if (child_pid == -1) {
2588 return false;
2591 if (child_pid == 0) {
2592 /* Child. */
2593 do_local_lock(pipe_out[0], pipe_in[1]);
2594 exit(0);
2597 close(pipe_out[0]);
2598 close(pipe_in[1]);
2599 pipe_out[0] = -1;
2600 pipe_in[1] = -1;
2602 /* Parent. */
2603 ret = read(pipe_in[0], &c, 1);
2604 if (ret != 1) {
2605 d_fprintf(stderr, "failed to read start signal from child. %s\n",
2606 strerror(errno));
2607 return false;
2610 if (!torture_open_connection(&cli1, 0)) {
2611 return false;
2614 cli_sockopt(cli1, sockops);
2616 status = cli_open(cli1, fname, O_RDWR, DENY_NONE,
2617 &fnum);
2618 if (!NT_STATUS_IS_OK(status)) {
2619 d_fprintf(stderr, "cli_open returned %s\n", nt_errstr(status));
2620 return false;
2623 /* Ensure the child has the lock. */
2624 if (cli_lock(cli1, fnum, 0, 4, 0, WRITE_LOCK)) {
2625 d_fprintf(stderr, "Got the lock on range 0:4 - this should not happen !\n");
2626 goto fail;
2627 } else {
2628 d_printf("Child has the lock.\n");
2631 /* Tell the child to wait 5 seconds then exit. */
2632 ret = write(pipe_out[1], &c, 1);
2633 if (ret != 1) {
2634 d_fprintf(stderr, "failed to send exit signal to child. %s\n",
2635 strerror(errno));
2636 goto fail;
2639 /* Wait 20 seconds for the lock. */
2640 alarm_fd = cli1->fd;
2641 CatchSignal(SIGALRM, alarm_handler_parent);
2642 alarm(20);
2644 start = timeval_current();
2646 if (!cli_lock(cli1, fnum, 0, 4, -1, WRITE_LOCK)) {
2647 d_fprintf(stderr, "Unable to apply write lock on range 0:4, error was "
2648 "%s\n", cli_errstr(cli1));
2649 goto fail_nofd;
2651 alarm(0);
2653 seconds = timeval_elapsed(&start);
2655 printf("Parent got the lock after %.2f seconds.\n",
2656 seconds);
2658 status = cli_close(cli1, fnum);
2659 if (!NT_STATUS_IS_OK(status)) {
2660 d_fprintf(stderr, "cli_close(fnum1) %s\n", nt_errstr(status));
2661 goto fail;
2664 correct = true;
2666 fail:
2667 cli_close(cli1, fnum);
2668 torture_close_connection(cli1);
2670 fail_nofd:
2672 printf("finished locktest9\n");
2673 return correct;
2677 test whether fnums and tids open on one VC are available on another (a major
2678 security hole)
2680 static bool run_fdpasstest(int dummy)
2682 struct cli_state *cli1, *cli2;
2683 const char *fname = "\\fdpass.tst";
2684 uint16_t fnum1;
2685 char buf[1024];
2686 NTSTATUS status;
2688 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
2689 return False;
2691 cli_sockopt(cli1, sockops);
2692 cli_sockopt(cli2, sockops);
2694 printf("starting fdpasstest\n");
2696 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2698 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
2699 &fnum1);
2700 if (!NT_STATUS_IS_OK(status)) {
2701 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
2702 return False;
2705 status = cli_writeall(cli1, fnum1, 0, (const uint8_t *)"hello world\n", 0,
2706 13, NULL);
2707 if (!NT_STATUS_IS_OK(status)) {
2708 printf("write failed (%s)\n", nt_errstr(status));
2709 return False;
2712 cli2->vuid = cli1->vuid;
2713 cli2->cnum = cli1->cnum;
2714 cli2->pid = cli1->pid;
2716 if (cli_read(cli2, fnum1, buf, 0, 13) == 13) {
2717 printf("read succeeded! nasty security hole [%s]\n",
2718 buf);
2719 return False;
2722 cli_close(cli1, fnum1);
2723 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2725 torture_close_connection(cli1);
2726 torture_close_connection(cli2);
2728 printf("finished fdpasstest\n");
2729 return True;
2732 static bool run_fdsesstest(int dummy)
2734 struct cli_state *cli;
2735 uint16 new_vuid;
2736 uint16 saved_vuid;
2737 uint16 new_cnum;
2738 uint16 saved_cnum;
2739 const char *fname = "\\fdsess.tst";
2740 const char *fname1 = "\\fdsess1.tst";
2741 uint16_t fnum1;
2742 uint16_t fnum2;
2743 char buf[1024];
2744 bool ret = True;
2745 NTSTATUS status;
2747 if (!torture_open_connection(&cli, 0))
2748 return False;
2749 cli_sockopt(cli, sockops);
2751 if (!torture_cli_session_setup2(cli, &new_vuid))
2752 return False;
2754 saved_cnum = cli->cnum;
2755 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, share, "?????", "", 1)))
2756 return False;
2757 new_cnum = cli->cnum;
2758 cli->cnum = saved_cnum;
2760 printf("starting fdsesstest\n");
2762 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2763 cli_unlink(cli, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2765 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
2766 if (!NT_STATUS_IS_OK(status)) {
2767 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
2768 return False;
2771 status = cli_writeall(cli, fnum1, 0, (const uint8_t *)"hello world\n", 0, 13,
2772 NULL);
2773 if (!NT_STATUS_IS_OK(status)) {
2774 printf("write failed (%s)\n", nt_errstr(status));
2775 return False;
2778 saved_vuid = cli->vuid;
2779 cli->vuid = new_vuid;
2781 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2782 printf("read succeeded with different vuid! nasty security hole [%s]\n",
2783 buf);
2784 ret = False;
2786 /* Try to open a file with different vuid, samba cnum. */
2787 if (NT_STATUS_IS_OK(cli_open(cli, fname1, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum2))) {
2788 printf("create with different vuid, same cnum succeeded.\n");
2789 cli_close(cli, fnum2);
2790 cli_unlink(cli, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2791 } else {
2792 printf("create with different vuid, same cnum failed.\n");
2793 printf("This will cause problems with service clients.\n");
2794 ret = False;
2797 cli->vuid = saved_vuid;
2799 /* Try with same vuid, different cnum. */
2800 cli->cnum = new_cnum;
2802 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2803 printf("read succeeded with different cnum![%s]\n",
2804 buf);
2805 ret = False;
2808 cli->cnum = saved_cnum;
2809 cli_close(cli, fnum1);
2810 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2812 torture_close_connection(cli);
2814 printf("finished fdsesstest\n");
2815 return ret;
2819 This test checks that
2821 1) the server does not allow an unlink on a file that is open
2823 static bool run_unlinktest(int dummy)
2825 struct cli_state *cli;
2826 const char *fname = "\\unlink.tst";
2827 uint16_t fnum;
2828 bool correct = True;
2829 NTSTATUS status;
2831 if (!torture_open_connection(&cli, 0)) {
2832 return False;
2835 cli_sockopt(cli, sockops);
2837 printf("starting unlink test\n");
2839 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2841 cli_setpid(cli, 1);
2843 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
2844 if (!NT_STATUS_IS_OK(status)) {
2845 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
2846 return False;
2849 status = cli_unlink(cli, fname,
2850 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2851 if (NT_STATUS_IS_OK(status)) {
2852 printf("error: server allowed unlink on an open file\n");
2853 correct = False;
2854 } else {
2855 correct = check_error(__LINE__, cli, ERRDOS, ERRbadshare,
2856 NT_STATUS_SHARING_VIOLATION);
2859 cli_close(cli, fnum);
2860 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2862 if (!torture_close_connection(cli)) {
2863 correct = False;
2866 printf("unlink test finished\n");
2868 return correct;
2873 test how many open files this server supports on the one socket
2875 static bool run_maxfidtest(int dummy)
2877 struct cli_state *cli;
2878 fstring fname;
2879 uint16_t fnums[0x11000];
2880 int i;
2881 int retries=4;
2882 bool correct = True;
2883 NTSTATUS status;
2885 cli = current_cli;
2887 if (retries <= 0) {
2888 printf("failed to connect\n");
2889 return False;
2892 cli_sockopt(cli, sockops);
2894 for (i=0; i<0x11000; i++) {
2895 slprintf(fname,sizeof(fname)-1,"\\maxfid.%d.%d", i,(int)getpid());
2896 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE,
2897 &fnums[i]);
2898 if (!NT_STATUS_IS_OK(status)) {
2899 printf("open of %s failed (%s)\n",
2900 fname, nt_errstr(status));
2901 printf("maximum fnum is %d\n", i);
2902 break;
2904 printf("%6d\r", i);
2906 printf("%6d\n", i);
2907 i--;
2909 printf("cleaning up\n");
2910 for (;i>=0;i--) {
2911 slprintf(fname,sizeof(fname)-1,"\\maxfid.%d.%d", i,(int)getpid());
2912 cli_close(cli, fnums[i]);
2914 status = cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2915 if (!NT_STATUS_IS_OK(status)) {
2916 printf("unlink of %s failed (%s)\n",
2917 fname, nt_errstr(status));
2918 correct = False;
2920 printf("%6d\r", i);
2922 printf("%6d\n", 0);
2924 printf("maxfid test finished\n");
2925 if (!torture_close_connection(cli)) {
2926 correct = False;
2928 return correct;
2931 /* generate a random buffer */
2932 static void rand_buf(char *buf, int len)
2934 while (len--) {
2935 *buf = (char)sys_random();
2936 buf++;
2940 /* send smb negprot commands, not reading the response */
2941 static bool run_negprot_nowait(int dummy)
2943 struct tevent_context *ev;
2944 int i;
2945 struct cli_state *cli;
2946 bool correct = True;
2948 printf("starting negprot nowait test\n");
2950 ev = tevent_context_init(talloc_tos());
2951 if (ev == NULL) {
2952 return false;
2955 if (!(cli = open_nbt_connection())) {
2956 TALLOC_FREE(ev);
2957 return False;
2960 for (i=0;i<50000;i++) {
2961 struct tevent_req *req;
2963 req = cli_negprot_send(ev, ev, cli);
2964 if (req == NULL) {
2965 TALLOC_FREE(ev);
2966 return false;
2968 if (!tevent_req_poll(req, ev)) {
2969 d_fprintf(stderr, "tevent_req_poll failed: %s\n",
2970 strerror(errno));
2971 TALLOC_FREE(ev);
2972 return false;
2974 TALLOC_FREE(req);
2977 if (torture_close_connection(cli)) {
2978 correct = False;
2981 printf("finished negprot nowait test\n");
2983 return correct;
2986 /* send smb negprot commands, not reading the response */
2987 static bool run_bad_nbt_session(int dummy)
2989 struct nmb_name called, calling;
2990 struct sockaddr_storage ss;
2991 NTSTATUS status;
2992 int fd;
2993 bool ret;
2995 printf("starting bad nbt session test\n");
2997 make_nmb_name(&calling, myname, 0x0);
2998 make_nmb_name(&called , host, 0x20);
3000 if (!resolve_name(host, &ss, 0x20, true)) {
3001 d_fprintf(stderr, "Could not resolve name %s\n", host);
3002 return false;
3005 status = open_socket_out(&ss, 139, 10000, &fd);
3006 if (!NT_STATUS_IS_OK(status)) {
3007 d_fprintf(stderr, "open_socket_out failed: %s\n",
3008 nt_errstr(status));
3009 return false;
3012 ret = cli_bad_session_request(fd, &calling, &called);
3013 close(fd);
3014 if (!ret) {
3015 d_fprintf(stderr, "open_socket_out failed: %s\n",
3016 nt_errstr(status));
3017 return false;
3020 printf("finished bad nbt session test\n");
3021 return true;
3024 /* send random IPC commands */
3025 static bool run_randomipc(int dummy)
3027 char *rparam = NULL;
3028 char *rdata = NULL;
3029 unsigned int rdrcnt,rprcnt;
3030 char param[1024];
3031 int api, param_len, i;
3032 struct cli_state *cli;
3033 bool correct = True;
3034 int count = 50000;
3036 printf("starting random ipc test\n");
3038 if (!torture_open_connection(&cli, 0)) {
3039 return False;
3042 for (i=0;i<count;i++) {
3043 api = sys_random() % 500;
3044 param_len = (sys_random() % 64);
3046 rand_buf(param, param_len);
3048 SSVAL(param,0,api);
3050 cli_api(cli,
3051 param, param_len, 8,
3052 NULL, 0, BUFFER_SIZE,
3053 &rparam, &rprcnt,
3054 &rdata, &rdrcnt);
3055 if (i % 100 == 0) {
3056 printf("%d/%d\r", i,count);
3059 printf("%d/%d\n", i, count);
3061 if (!torture_close_connection(cli)) {
3062 correct = False;
3065 printf("finished random ipc test\n");
3067 return correct;
3072 static void browse_callback(const char *sname, uint32 stype,
3073 const char *comment, void *state)
3075 printf("\t%20.20s %08x %s\n", sname, stype, comment);
3081 This test checks the browse list code
3084 static bool run_browsetest(int dummy)
3086 static struct cli_state *cli;
3087 bool correct = True;
3089 printf("starting browse test\n");
3091 if (!torture_open_connection(&cli, 0)) {
3092 return False;
3095 printf("domain list:\n");
3096 cli_NetServerEnum(cli, cli->server_domain,
3097 SV_TYPE_DOMAIN_ENUM,
3098 browse_callback, NULL);
3100 printf("machine list:\n");
3101 cli_NetServerEnum(cli, cli->server_domain,
3102 SV_TYPE_ALL,
3103 browse_callback, NULL);
3105 if (!torture_close_connection(cli)) {
3106 correct = False;
3109 printf("browse test finished\n");
3111 return correct;
3117 This checks how the getatr calls works
3119 static bool run_attrtest(int dummy)
3121 struct cli_state *cli;
3122 uint16_t fnum;
3123 time_t t, t2;
3124 const char *fname = "\\attrib123456789.tst";
3125 bool correct = True;
3126 NTSTATUS status;
3128 printf("starting attrib test\n");
3130 if (!torture_open_connection(&cli, 0)) {
3131 return False;
3134 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3135 cli_open(cli, fname,
3136 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3137 cli_close(cli, fnum);
3139 status = cli_getatr(cli, fname, NULL, NULL, &t);
3140 if (!NT_STATUS_IS_OK(status)) {
3141 printf("getatr failed (%s)\n", nt_errstr(status));
3142 correct = False;
3145 if (abs(t - time(NULL)) > 60*60*24*10) {
3146 printf("ERROR: SMBgetatr bug. time is %s",
3147 ctime(&t));
3148 t = time(NULL);
3149 correct = True;
3152 t2 = t-60*60*24; /* 1 day ago */
3154 status = cli_setatr(cli, fname, 0, t2);
3155 if (!NT_STATUS_IS_OK(status)) {
3156 printf("setatr failed (%s)\n", nt_errstr(status));
3157 correct = True;
3160 status = cli_getatr(cli, fname, NULL, NULL, &t);
3161 if (!NT_STATUS_IS_OK(status)) {
3162 printf("getatr failed (%s)\n", nt_errstr(status));
3163 correct = True;
3166 if (t != t2) {
3167 printf("ERROR: getatr/setatr bug. times are\n%s",
3168 ctime(&t));
3169 printf("%s", ctime(&t2));
3170 correct = True;
3173 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3175 if (!torture_close_connection(cli)) {
3176 correct = False;
3179 printf("attrib test finished\n");
3181 return correct;
3186 This checks a couple of trans2 calls
3188 static bool run_trans2test(int dummy)
3190 struct cli_state *cli;
3191 uint16_t fnum;
3192 SMB_OFF_T size;
3193 time_t c_time, a_time, m_time;
3194 struct timespec c_time_ts, a_time_ts, m_time_ts, w_time_ts, m_time2_ts;
3195 const char *fname = "\\trans2.tst";
3196 const char *dname = "\\trans2";
3197 const char *fname2 = "\\trans2\\trans2.tst";
3198 char *pname;
3199 bool correct = True;
3200 NTSTATUS status;
3201 uint32_t fs_attr;
3203 printf("starting trans2 test\n");
3205 if (!torture_open_connection(&cli, 0)) {
3206 return False;
3209 status = cli_get_fs_attr_info(cli, &fs_attr);
3210 if (!NT_STATUS_IS_OK(status)) {
3211 printf("ERROR: cli_get_fs_attr_info returned %s\n",
3212 nt_errstr(status));
3213 correct = false;
3216 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3217 cli_open(cli, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3218 status = cli_qfileinfo_basic(cli, fnum, NULL, &size, &c_time_ts,
3219 &a_time_ts, &w_time_ts, &m_time_ts, NULL);
3220 if (!NT_STATUS_IS_OK(status)) {
3221 printf("ERROR: qfileinfo failed (%s)\n", nt_errstr(status));
3222 correct = False;
3225 status = cli_qfilename(cli, fnum, talloc_tos(), &pname);
3226 if (!NT_STATUS_IS_OK(status)) {
3227 printf("ERROR: qfilename failed (%s)\n", nt_errstr(status));
3228 correct = False;
3231 if (strcmp(pname, fname)) {
3232 printf("qfilename gave different name? [%s] [%s]\n",
3233 fname, pname);
3234 correct = False;
3237 cli_close(cli, fnum);
3239 sleep(2);
3241 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3242 status = cli_open(cli, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE,
3243 &fnum);
3244 if (!NT_STATUS_IS_OK(status)) {
3245 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3246 return False;
3248 cli_close(cli, fnum);
3250 status = cli_qpathinfo1(cli, fname, &c_time, &a_time, &m_time, &size,
3251 NULL);
3252 if (!NT_STATUS_IS_OK(status)) {
3253 printf("ERROR: qpathinfo failed (%s)\n", nt_errstr(status));
3254 correct = False;
3255 } else {
3256 if (c_time != m_time) {
3257 printf("create time=%s", ctime(&c_time));
3258 printf("modify time=%s", ctime(&m_time));
3259 printf("This system appears to have sticky create times\n");
3261 if (a_time % (60*60) == 0) {
3262 printf("access time=%s", ctime(&a_time));
3263 printf("This system appears to set a midnight access time\n");
3264 correct = False;
3267 if (abs(m_time - time(NULL)) > 60*60*24*7) {
3268 printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
3269 correct = False;
3274 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3275 cli_open(cli, fname,
3276 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3277 cli_close(cli, fnum);
3278 status = cli_qpathinfo2(cli, fname, &c_time_ts, &a_time_ts, &w_time_ts,
3279 &m_time_ts, &size, NULL, NULL);
3280 if (!NT_STATUS_IS_OK(status)) {
3281 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3282 correct = False;
3283 } else {
3284 if (w_time_ts.tv_sec < 60*60*24*2) {
3285 printf("write time=%s", ctime(&w_time_ts.tv_sec));
3286 printf("This system appears to set a initial 0 write time\n");
3287 correct = False;
3291 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3294 /* check if the server updates the directory modification time
3295 when creating a new file */
3296 status = cli_mkdir(cli, dname);
3297 if (!NT_STATUS_IS_OK(status)) {
3298 printf("ERROR: mkdir failed (%s)\n", nt_errstr(status));
3299 correct = False;
3301 sleep(3);
3302 status = cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts,
3303 &w_time_ts, &m_time_ts, &size, NULL, NULL);
3304 if (!NT_STATUS_IS_OK(status)) {
3305 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3306 correct = False;
3309 cli_open(cli, fname2,
3310 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
3311 cli_writeall(cli, fnum, 0, (uint8_t *)&fnum, 0, sizeof(fnum), NULL);
3312 cli_close(cli, fnum);
3313 status = cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts,
3314 &w_time_ts, &m_time2_ts, &size, NULL, NULL);
3315 if (!NT_STATUS_IS_OK(status)) {
3316 printf("ERROR: qpathinfo2 failed (%s)\n", nt_errstr(status));
3317 correct = False;
3318 } else {
3319 if (memcmp(&m_time_ts, &m_time2_ts, sizeof(struct timespec))
3320 == 0) {
3321 printf("This system does not update directory modification times\n");
3322 correct = False;
3325 cli_unlink(cli, fname2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3326 cli_rmdir(cli, dname);
3328 if (!torture_close_connection(cli)) {
3329 correct = False;
3332 printf("trans2 test finished\n");
3334 return correct;
3338 This checks new W2K calls.
3341 static NTSTATUS new_trans(struct cli_state *pcli, int fnum, int level)
3343 uint8_t *buf = NULL;
3344 uint32 len;
3345 NTSTATUS status;
3347 status = cli_qfileinfo(talloc_tos(), pcli, fnum, level, 0,
3348 pcli->max_xmit, NULL, &buf, &len);
3349 if (!NT_STATUS_IS_OK(status)) {
3350 printf("ERROR: qfileinfo (%d) failed (%s)\n", level,
3351 nt_errstr(status));
3352 } else {
3353 printf("qfileinfo: level %d, len = %u\n", level, len);
3354 dump_data(0, (uint8 *)buf, len);
3355 printf("\n");
3357 TALLOC_FREE(buf);
3358 return status;
3361 static bool run_w2ktest(int dummy)
3363 struct cli_state *cli;
3364 uint16_t fnum;
3365 const char *fname = "\\w2ktest\\w2k.tst";
3366 int level;
3367 bool correct = True;
3369 printf("starting w2k test\n");
3371 if (!torture_open_connection(&cli, 0)) {
3372 return False;
3375 cli_open(cli, fname,
3376 O_RDWR | O_CREAT , DENY_NONE, &fnum);
3378 for (level = 1004; level < 1040; level++) {
3379 new_trans(cli, fnum, level);
3382 cli_close(cli, fnum);
3384 if (!torture_close_connection(cli)) {
3385 correct = False;
3388 printf("w2k test finished\n");
3390 return correct;
3395 this is a harness for some oplock tests
3397 static bool run_oplock1(int dummy)
3399 struct cli_state *cli1;
3400 const char *fname = "\\lockt1.lck";
3401 uint16_t fnum1;
3402 bool correct = True;
3403 NTSTATUS status;
3405 printf("starting oplock test 1\n");
3407 if (!torture_open_connection(&cli1, 0)) {
3408 return False;
3411 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3413 cli_sockopt(cli1, sockops);
3415 cli1->use_oplocks = True;
3417 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
3418 &fnum1);
3419 if (!NT_STATUS_IS_OK(status)) {
3420 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3421 return False;
3424 cli1->use_oplocks = False;
3426 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3427 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3429 status = cli_close(cli1, fnum1);
3430 if (!NT_STATUS_IS_OK(status)) {
3431 printf("close2 failed (%s)\n", nt_errstr(status));
3432 return False;
3435 status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3436 if (!NT_STATUS_IS_OK(status)) {
3437 printf("unlink failed (%s)\n", nt_errstr(status));
3438 return False;
3441 if (!torture_close_connection(cli1)) {
3442 correct = False;
3445 printf("finished oplock test 1\n");
3447 return correct;
3450 static bool run_oplock2(int dummy)
3452 struct cli_state *cli1, *cli2;
3453 const char *fname = "\\lockt2.lck";
3454 uint16_t fnum1, fnum2;
3455 int saved_use_oplocks = use_oplocks;
3456 char buf[4];
3457 bool correct = True;
3458 volatile bool *shared_correct;
3459 NTSTATUS status;
3461 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
3462 *shared_correct = True;
3464 use_level_II_oplocks = True;
3465 use_oplocks = True;
3467 printf("starting oplock test 2\n");
3469 if (!torture_open_connection(&cli1, 0)) {
3470 use_level_II_oplocks = False;
3471 use_oplocks = saved_use_oplocks;
3472 return False;
3475 cli1->use_oplocks = True;
3476 cli1->use_level_II_oplocks = True;
3478 if (!torture_open_connection(&cli2, 1)) {
3479 use_level_II_oplocks = False;
3480 use_oplocks = saved_use_oplocks;
3481 return False;
3484 cli2->use_oplocks = True;
3485 cli2->use_level_II_oplocks = True;
3487 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3489 cli_sockopt(cli1, sockops);
3490 cli_sockopt(cli2, sockops);
3492 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
3493 &fnum1);
3494 if (!NT_STATUS_IS_OK(status)) {
3495 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3496 return False;
3499 /* Don't need the globals any more. */
3500 use_level_II_oplocks = False;
3501 use_oplocks = saved_use_oplocks;
3503 if (fork() == 0) {
3504 /* Child code */
3505 status = cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
3506 if (!NT_STATUS_IS_OK(status)) {
3507 printf("second open of %s failed (%s)\n", fname, nt_errstr(status));
3508 *shared_correct = False;
3509 exit(0);
3512 sleep(2);
3514 status = cli_close(cli2, fnum2);
3515 if (!NT_STATUS_IS_OK(status)) {
3516 printf("close2 failed (%s)\n", nt_errstr(status));
3517 *shared_correct = False;
3520 exit(0);
3523 sleep(2);
3525 /* Ensure cli1 processes the break. Empty file should always return 0
3526 * bytes. */
3528 if (cli_read(cli1, fnum1, buf, 0, 4) != 0) {
3529 printf("read on fnum1 failed (%s)\n", cli_errstr(cli1));
3530 correct = False;
3533 /* Should now be at level II. */
3534 /* Test if sending a write locks causes a break to none. */
3536 if (!cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK)) {
3537 printf("lock failed (%s)\n", cli_errstr(cli1));
3538 correct = False;
3541 cli_unlock(cli1, fnum1, 0, 4);
3543 sleep(2);
3545 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
3546 printf("lock failed (%s)\n", cli_errstr(cli1));
3547 correct = False;
3550 cli_unlock(cli1, fnum1, 0, 4);
3552 sleep(2);
3554 cli_read(cli1, fnum1, buf, 0, 4);
3556 status = cli_close(cli1, fnum1);
3557 if (!NT_STATUS_IS_OK(status)) {
3558 printf("close1 failed (%s)\n", nt_errstr(status));
3559 correct = False;
3562 sleep(4);
3564 status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3565 if (!NT_STATUS_IS_OK(status)) {
3566 printf("unlink failed (%s)\n", nt_errstr(status));
3567 correct = False;
3570 if (!torture_close_connection(cli1)) {
3571 correct = False;
3574 if (!*shared_correct) {
3575 correct = False;
3578 printf("finished oplock test 2\n");
3580 return correct;
3583 struct oplock4_state {
3584 struct tevent_context *ev;
3585 struct cli_state *cli;
3586 bool *got_break;
3587 uint16_t *fnum2;
3590 static void oplock4_got_break(struct tevent_req *req);
3591 static void oplock4_got_open(struct tevent_req *req);
3593 static bool run_oplock4(int dummy)
3595 struct tevent_context *ev;
3596 struct cli_state *cli1, *cli2;
3597 struct tevent_req *oplock_req, *open_req;
3598 const char *fname = "\\lockt4.lck";
3599 const char *fname_ln = "\\lockt4_ln.lck";
3600 uint16_t fnum1, fnum2;
3601 int saved_use_oplocks = use_oplocks;
3602 NTSTATUS status;
3603 bool correct = true;
3605 bool got_break;
3607 struct oplock4_state *state;
3609 printf("starting oplock test 4\n");
3611 if (!torture_open_connection(&cli1, 0)) {
3612 use_level_II_oplocks = false;
3613 use_oplocks = saved_use_oplocks;
3614 return false;
3617 if (!torture_open_connection(&cli2, 1)) {
3618 use_level_II_oplocks = false;
3619 use_oplocks = saved_use_oplocks;
3620 return false;
3623 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3624 cli_unlink(cli1, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3626 cli_sockopt(cli1, sockops);
3627 cli_sockopt(cli2, sockops);
3629 /* Create the file. */
3630 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
3631 &fnum1);
3632 if (!NT_STATUS_IS_OK(status)) {
3633 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3634 return false;
3637 status = cli_close(cli1, fnum1);
3638 if (!NT_STATUS_IS_OK(status)) {
3639 printf("close1 failed (%s)\n", nt_errstr(status));
3640 return false;
3643 /* Now create a hardlink. */
3644 status = cli_nt_hardlink(cli1, fname, fname_ln);
3645 if (!NT_STATUS_IS_OK(status)) {
3646 printf("nt hardlink failed (%s)\n", nt_errstr(status));
3647 return false;
3650 /* Prove that opening hardlinks cause deny modes to conflict. */
3651 status = cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum1);
3652 if (!NT_STATUS_IS_OK(status)) {
3653 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3654 return false;
3657 status = cli_open(cli1, fname_ln, O_RDWR, DENY_NONE, &fnum2);
3658 if (NT_STATUS_IS_OK(status)) {
3659 printf("open of %s succeeded - should fail with sharing violation.\n",
3660 fname_ln);
3661 return false;
3664 if (!NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
3665 printf("open of %s should fail with sharing violation. Got %s\n",
3666 fname_ln, nt_errstr(status));
3667 return false;
3670 status = cli_close(cli1, fnum1);
3671 if (!NT_STATUS_IS_OK(status)) {
3672 printf("close1 failed (%s)\n", nt_errstr(status));
3673 return false;
3676 cli1->use_oplocks = true;
3677 cli1->use_level_II_oplocks = true;
3679 cli2->use_oplocks = true;
3680 cli2->use_level_II_oplocks = true;
3682 status = cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
3683 if (!NT_STATUS_IS_OK(status)) {
3684 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
3685 return false;
3688 ev = tevent_context_init(talloc_tos());
3689 if (ev == NULL) {
3690 printf("tevent_req_create failed\n");
3691 return false;
3694 state = talloc(ev, struct oplock4_state);
3695 if (state == NULL) {
3696 printf("talloc failed\n");
3697 return false;
3699 state->ev = ev;
3700 state->cli = cli1;
3701 state->got_break = &got_break;
3702 state->fnum2 = &fnum2;
3704 oplock_req = cli_smb_oplock_break_waiter_send(
3705 talloc_tos(), ev, cli1);
3706 if (oplock_req == NULL) {
3707 printf("cli_smb_oplock_break_waiter_send failed\n");
3708 return false;
3710 tevent_req_set_callback(oplock_req, oplock4_got_break, state);
3712 open_req = cli_open_send(
3713 talloc_tos(), ev, cli2, fname_ln, O_RDWR, DENY_NONE);
3714 if (oplock_req == NULL) {
3715 printf("cli_open_send failed\n");
3716 return false;
3718 tevent_req_set_callback(open_req, oplock4_got_open, state);
3720 got_break = false;
3721 fnum2 = 0xffff;
3723 while (!got_break || fnum2 == 0xffff) {
3724 int ret;
3725 ret = tevent_loop_once(ev);
3726 if (ret == -1) {
3727 printf("tevent_loop_once failed: %s\n",
3728 strerror(errno));
3729 return false;
3733 status = cli_close(cli2, fnum2);
3734 if (!NT_STATUS_IS_OK(status)) {
3735 printf("close2 failed (%s)\n", nt_errstr(status));
3736 correct = false;
3739 status = cli_close(cli1, fnum1);
3740 if (!NT_STATUS_IS_OK(status)) {
3741 printf("close1 failed (%s)\n", nt_errstr(status));
3742 correct = false;
3745 status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3746 if (!NT_STATUS_IS_OK(status)) {
3747 printf("unlink failed (%s)\n", nt_errstr(status));
3748 correct = false;
3751 status = cli_unlink(cli1, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3752 if (!NT_STATUS_IS_OK(status)) {
3753 printf("unlink failed (%s)\n", nt_errstr(status));
3754 correct = false;
3757 if (!torture_close_connection(cli1)) {
3758 correct = false;
3761 if (!got_break) {
3762 correct = false;
3765 printf("finished oplock test 4\n");
3767 return correct;
3770 static void oplock4_got_break(struct tevent_req *req)
3772 struct oplock4_state *state = tevent_req_callback_data(
3773 req, struct oplock4_state);
3774 uint16_t fnum;
3775 uint8_t level;
3776 NTSTATUS status;
3778 status = cli_smb_oplock_break_waiter_recv(req, &fnum, &level);
3779 TALLOC_FREE(req);
3780 if (!NT_STATUS_IS_OK(status)) {
3781 printf("cli_smb_oplock_break_waiter_recv returned %s\n",
3782 nt_errstr(status));
3783 return;
3785 *state->got_break = true;
3787 req = cli_oplock_ack_send(state, state->ev, state->cli, fnum,
3788 NO_OPLOCK);
3789 if (req == NULL) {
3790 printf("cli_oplock_ack_send failed\n");
3791 return;
3795 static void oplock4_got_open(struct tevent_req *req)
3797 struct oplock4_state *state = tevent_req_callback_data(
3798 req, struct oplock4_state);
3799 NTSTATUS status;
3801 status = cli_open_recv(req, state->fnum2);
3802 if (!NT_STATUS_IS_OK(status)) {
3803 printf("cli_open_recv returned %s\n", nt_errstr(status));
3804 *state->fnum2 = 0xffff;
3809 Test delete on close semantics.
3811 static bool run_deletetest(int dummy)
3813 struct cli_state *cli1 = NULL;
3814 struct cli_state *cli2 = NULL;
3815 const char *fname = "\\delete.file";
3816 uint16_t fnum1 = (uint16_t)-1;
3817 uint16_t fnum2 = (uint16_t)-1;
3818 bool correct = True;
3819 NTSTATUS status;
3821 printf("starting delete test\n");
3823 if (!torture_open_connection(&cli1, 0)) {
3824 return False;
3827 cli_sockopt(cli1, sockops);
3829 /* Test 1 - this should delete the file on close. */
3831 cli_setatr(cli1, fname, 0, 0);
3832 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3834 status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
3835 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
3836 FILE_DELETE_ON_CLOSE, 0, &fnum1);
3837 if (!NT_STATUS_IS_OK(status)) {
3838 printf("[1] open of %s failed (%s)\n", fname, nt_errstr(status));
3839 correct = False;
3840 goto fail;
3843 status = cli_close(cli1, fnum1);
3844 if (!NT_STATUS_IS_OK(status)) {
3845 printf("[1] close failed (%s)\n", nt_errstr(status));
3846 correct = False;
3847 goto fail;
3850 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
3851 printf("[1] open of %s succeeded (should fail)\n", fname);
3852 correct = False;
3853 goto fail;
3856 printf("first delete on close test succeeded.\n");
3858 /* Test 2 - this should delete the file on close. */
3860 cli_setatr(cli1, fname, 0, 0);
3861 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3863 status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
3864 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
3865 FILE_OVERWRITE_IF, 0, 0, &fnum1);
3866 if (!NT_STATUS_IS_OK(status)) {
3867 printf("[2] open of %s failed (%s)\n", fname, nt_errstr(status));
3868 correct = False;
3869 goto fail;
3872 status = cli_nt_delete_on_close(cli1, fnum1, true);
3873 if (!NT_STATUS_IS_OK(status)) {
3874 printf("[2] setting delete_on_close failed (%s)\n", nt_errstr(status));
3875 correct = False;
3876 goto fail;
3879 status = cli_close(cli1, fnum1);
3880 if (!NT_STATUS_IS_OK(status)) {
3881 printf("[2] close failed (%s)\n", nt_errstr(status));
3882 correct = False;
3883 goto fail;
3886 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3887 printf("[2] open of %s succeeded should have been deleted on close !\n", fname);
3888 status = cli_close(cli1, fnum1);
3889 if (!NT_STATUS_IS_OK(status)) {
3890 printf("[2] close failed (%s)\n", nt_errstr(status));
3891 correct = False;
3892 goto fail;
3894 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3895 } else
3896 printf("second delete on close test succeeded.\n");
3898 /* Test 3 - ... */
3899 cli_setatr(cli1, fname, 0, 0);
3900 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3902 status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
3903 FILE_ATTRIBUTE_NORMAL,
3904 FILE_SHARE_READ|FILE_SHARE_WRITE,
3905 FILE_OVERWRITE_IF, 0, 0, &fnum1);
3906 if (!NT_STATUS_IS_OK(status)) {
3907 printf("[3] open - 1 of %s failed (%s)\n", fname, nt_errstr(status));
3908 correct = False;
3909 goto fail;
3912 /* This should fail with a sharing violation - open for delete is only compatible
3913 with SHARE_DELETE. */
3915 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3916 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0, 0, &fnum2))) {
3917 printf("[3] open - 2 of %s succeeded - should have failed.\n", fname);
3918 correct = False;
3919 goto fail;
3922 /* This should succeed. */
3923 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3924 FILE_ATTRIBUTE_NORMAL,
3925 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3926 FILE_OPEN, 0, 0, &fnum2);
3927 if (!NT_STATUS_IS_OK(status)) {
3928 printf("[3] open - 2 of %s failed (%s)\n", fname, nt_errstr(status));
3929 correct = False;
3930 goto fail;
3933 status = cli_nt_delete_on_close(cli1, fnum1, true);
3934 if (!NT_STATUS_IS_OK(status)) {
3935 printf("[3] setting delete_on_close failed (%s)\n", nt_errstr(status));
3936 correct = False;
3937 goto fail;
3940 status = cli_close(cli1, fnum1);
3941 if (!NT_STATUS_IS_OK(status)) {
3942 printf("[3] close 1 failed (%s)\n", nt_errstr(status));
3943 correct = False;
3944 goto fail;
3947 status = cli_close(cli1, fnum2);
3948 if (!NT_STATUS_IS_OK(status)) {
3949 printf("[3] close 2 failed (%s)\n", nt_errstr(status));
3950 correct = False;
3951 goto fail;
3954 /* This should fail - file should no longer be there. */
3956 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3957 printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
3958 status = cli_close(cli1, fnum1);
3959 if (!NT_STATUS_IS_OK(status)) {
3960 printf("[3] close failed (%s)\n", nt_errstr(status));
3962 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3963 correct = False;
3964 goto fail;
3965 } else
3966 printf("third delete on close test succeeded.\n");
3968 /* Test 4 ... */
3969 cli_setatr(cli1, fname, 0, 0);
3970 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
3972 status = cli_ntcreate(cli1, fname, 0,
3973 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3974 FILE_ATTRIBUTE_NORMAL,
3975 FILE_SHARE_READ|FILE_SHARE_WRITE,
3976 FILE_OVERWRITE_IF, 0, 0, &fnum1);
3977 if (!NT_STATUS_IS_OK(status)) {
3978 printf("[4] open of %s failed (%s)\n", fname, nt_errstr(status));
3979 correct = False;
3980 goto fail;
3983 /* This should succeed. */
3984 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3985 FILE_ATTRIBUTE_NORMAL,
3986 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3987 FILE_OPEN, 0, 0, &fnum2);
3988 if (!NT_STATUS_IS_OK(status)) {
3989 printf("[4] open - 2 of %s failed (%s)\n", fname, nt_errstr(status));
3990 correct = False;
3991 goto fail;
3994 status = cli_close(cli1, fnum2);
3995 if (!NT_STATUS_IS_OK(status)) {
3996 printf("[4] close - 1 failed (%s)\n", nt_errstr(status));
3997 correct = False;
3998 goto fail;
4001 status = cli_nt_delete_on_close(cli1, fnum1, true);
4002 if (!NT_STATUS_IS_OK(status)) {
4003 printf("[4] setting delete_on_close failed (%s)\n", nt_errstr(status));
4004 correct = False;
4005 goto fail;
4008 /* This should fail - no more opens once delete on close set. */
4009 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
4010 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4011 FILE_OPEN, 0, 0, &fnum2))) {
4012 printf("[4] open - 3 of %s succeeded ! Should have failed.\n", fname );
4013 correct = False;
4014 goto fail;
4015 } else
4016 printf("fourth delete on close test succeeded.\n");
4018 status = cli_close(cli1, fnum1);
4019 if (!NT_STATUS_IS_OK(status)) {
4020 printf("[4] close - 2 failed (%s)\n", nt_errstr(status));
4021 correct = False;
4022 goto fail;
4025 /* Test 5 ... */
4026 cli_setatr(cli1, fname, 0, 0);
4027 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4029 status = cli_open(cli1, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum1);
4030 if (!NT_STATUS_IS_OK(status)) {
4031 printf("[5] open of %s failed (%s)\n", fname, nt_errstr(status));
4032 correct = False;
4033 goto fail;
4036 /* This should fail - only allowed on NT opens with DELETE access. */
4038 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
4039 printf("[5] setting delete_on_close on OpenX file succeeded - should fail !\n");
4040 correct = False;
4041 goto fail;
4044 status = cli_close(cli1, fnum1);
4045 if (!NT_STATUS_IS_OK(status)) {
4046 printf("[5] close - 2 failed (%s)\n", nt_errstr(status));
4047 correct = False;
4048 goto fail;
4051 printf("fifth delete on close test succeeded.\n");
4053 /* Test 6 ... */
4054 cli_setatr(cli1, fname, 0, 0);
4055 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4057 status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
4058 FILE_ATTRIBUTE_NORMAL,
4059 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4060 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4061 if (!NT_STATUS_IS_OK(status)) {
4062 printf("[6] open of %s failed (%s)\n", fname,
4063 nt_errstr(status));
4064 correct = False;
4065 goto fail;
4068 /* This should fail - only allowed on NT opens with DELETE access. */
4070 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
4071 printf("[6] setting delete_on_close on file with no delete access succeeded - should fail !\n");
4072 correct = False;
4073 goto fail;
4076 status = cli_close(cli1, fnum1);
4077 if (!NT_STATUS_IS_OK(status)) {
4078 printf("[6] close - 2 failed (%s)\n", nt_errstr(status));
4079 correct = False;
4080 goto fail;
4083 printf("sixth delete on close test succeeded.\n");
4085 /* Test 7 ... */
4086 cli_setatr(cli1, fname, 0, 0);
4087 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4089 status = cli_ntcreate(cli1, fname, 0,
4090 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
4091 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
4092 0, 0, &fnum1);
4093 if (!NT_STATUS_IS_OK(status)) {
4094 printf("[7] open of %s failed (%s)\n", fname, nt_errstr(status));
4095 correct = False;
4096 goto fail;
4099 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
4100 printf("[7] setting delete_on_close on file failed !\n");
4101 correct = False;
4102 goto fail;
4105 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, false))) {
4106 printf("[7] unsetting delete_on_close on file failed !\n");
4107 correct = False;
4108 goto fail;
4111 status = cli_close(cli1, fnum1);
4112 if (!NT_STATUS_IS_OK(status)) {
4113 printf("[7] close - 2 failed (%s)\n", nt_errstr(status));
4114 correct = False;
4115 goto fail;
4118 /* This next open should succeed - we reset the flag. */
4119 status = cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1);
4120 if (!NT_STATUS_IS_OK(status)) {
4121 printf("[5] open of %s failed (%s)\n", fname, nt_errstr(status));
4122 correct = False;
4123 goto fail;
4126 status = cli_close(cli1, fnum1);
4127 if (!NT_STATUS_IS_OK(status)) {
4128 printf("[7] close - 2 failed (%s)\n", nt_errstr(status));
4129 correct = False;
4130 goto fail;
4133 printf("seventh delete on close test succeeded.\n");
4135 /* Test 7 ... */
4136 cli_setatr(cli1, fname, 0, 0);
4137 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4139 if (!torture_open_connection(&cli2, 1)) {
4140 printf("[8] failed to open second connection.\n");
4141 correct = False;
4142 goto fail;
4145 cli_sockopt(cli1, sockops);
4147 status = cli_ntcreate(cli1, fname, 0,
4148 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
4149 FILE_ATTRIBUTE_NORMAL,
4150 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4151 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4152 if (!NT_STATUS_IS_OK(status)) {
4153 printf("[8] open 1 of %s failed (%s)\n", fname, nt_errstr(status));
4154 correct = False;
4155 goto fail;
4158 status = cli_ntcreate(cli2, fname, 0,
4159 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
4160 FILE_ATTRIBUTE_NORMAL,
4161 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4162 FILE_OPEN, 0, 0, &fnum2);
4163 if (!NT_STATUS_IS_OK(status)) {
4164 printf("[8] open 2 of %s failed (%s)\n", fname, nt_errstr(status));
4165 correct = False;
4166 goto fail;
4169 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
4170 printf("[8] setting delete_on_close on file failed !\n");
4171 correct = False;
4172 goto fail;
4175 status = cli_close(cli1, fnum1);
4176 if (!NT_STATUS_IS_OK(status)) {
4177 printf("[8] close - 1 failed (%s)\n", nt_errstr(status));
4178 correct = False;
4179 goto fail;
4182 status = cli_close(cli2, fnum2);
4183 if (!NT_STATUS_IS_OK(status)) {
4184 printf("[8] close - 2 failed (%s)\n", nt_errstr(status));
4185 correct = False;
4186 goto fail;
4189 /* This should fail.. */
4190 status = cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1);
4191 if (NT_STATUS_IS_OK(status)) {
4192 printf("[8] open of %s succeeded should have been deleted on close !\n", fname);
4193 goto fail;
4194 correct = False;
4195 } else
4196 printf("eighth delete on close test succeeded.\n");
4198 /* This should fail - we need to set DELETE_ACCESS. */
4199 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,FILE_READ_DATA|FILE_WRITE_DATA,
4200 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
4201 printf("[9] open of %s succeeded should have failed!\n", fname);
4202 correct = False;
4203 goto fail;
4206 printf("ninth delete on close test succeeded.\n");
4208 status = cli_ntcreate(cli1, fname, 0,
4209 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
4210 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4211 FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE,
4212 0, &fnum1);
4213 if (!NT_STATUS_IS_OK(status)) {
4214 printf("[10] open of %s failed (%s)\n", fname, nt_errstr(status));
4215 correct = False;
4216 goto fail;
4219 /* This should delete the file. */
4220 status = cli_close(cli1, fnum1);
4221 if (!NT_STATUS_IS_OK(status)) {
4222 printf("[10] close failed (%s)\n", nt_errstr(status));
4223 correct = False;
4224 goto fail;
4227 /* This should fail.. */
4228 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
4229 printf("[10] open of %s succeeded should have been deleted on close !\n", fname);
4230 goto fail;
4231 correct = False;
4232 } else
4233 printf("tenth delete on close test succeeded.\n");
4235 cli_setatr(cli1, fname, 0, 0);
4236 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4238 /* What error do we get when attempting to open a read-only file with
4239 delete access ? */
4241 /* Create a readonly file. */
4242 status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
4243 FILE_ATTRIBUTE_READONLY, FILE_SHARE_NONE,
4244 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4245 if (!NT_STATUS_IS_OK(status)) {
4246 printf("[11] open of %s failed (%s)\n", fname, nt_errstr(status));
4247 correct = False;
4248 goto fail;
4251 status = cli_close(cli1, fnum1);
4252 if (!NT_STATUS_IS_OK(status)) {
4253 printf("[11] close failed (%s)\n", nt_errstr(status));
4254 correct = False;
4255 goto fail;
4258 /* Now try open for delete access. */
4259 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES|DELETE_ACCESS,
4260 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4261 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4262 printf("[11] open of %s succeeded should have been denied with ACCESS_DENIED!\n", fname);
4263 cli_close(cli1, fnum1);
4264 goto fail;
4265 correct = False;
4266 } else {
4267 NTSTATUS nterr = cli_nt_error(cli1);
4268 if (!NT_STATUS_EQUAL(nterr,NT_STATUS_ACCESS_DENIED)) {
4269 printf("[11] open of %s should have been denied with ACCESS_DENIED! Got error %s\n", fname, nt_errstr(nterr));
4270 goto fail;
4271 correct = False;
4272 } else {
4273 printf("eleventh delete on close test succeeded.\n");
4277 printf("finished delete test\n");
4279 fail:
4280 /* FIXME: This will crash if we aborted before cli2 got
4281 * intialized, because these functions don't handle
4282 * uninitialized connections. */
4284 if (fnum1 != (uint16_t)-1) cli_close(cli1, fnum1);
4285 if (fnum2 != (uint16_t)-1) cli_close(cli1, fnum2);
4286 cli_setatr(cli1, fname, 0, 0);
4287 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4289 if (cli1 && !torture_close_connection(cli1)) {
4290 correct = False;
4292 if (cli2 && !torture_close_connection(cli2)) {
4293 correct = False;
4295 return correct;
4298 static bool run_deletetest_ln(int dummy)
4300 struct cli_state *cli;
4301 const char *fname = "\\delete1";
4302 const char *fname_ln = "\\delete1_ln";
4303 uint16_t fnum;
4304 uint16_t fnum1;
4305 NTSTATUS status;
4306 bool correct = true;
4307 time_t t;
4309 printf("starting deletetest-ln\n");
4311 if (!torture_open_connection(&cli, 0)) {
4312 return false;
4315 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4316 cli_unlink(cli, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4318 cli_sockopt(cli, sockops);
4320 /* Create the file. */
4321 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
4322 if (!NT_STATUS_IS_OK(status)) {
4323 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
4324 return false;
4327 status = cli_close(cli, fnum);
4328 if (!NT_STATUS_IS_OK(status)) {
4329 printf("close1 failed (%s)\n", nt_errstr(status));
4330 return false;
4333 /* Now create a hardlink. */
4334 status = cli_nt_hardlink(cli, fname, fname_ln);
4335 if (!NT_STATUS_IS_OK(status)) {
4336 printf("nt hardlink failed (%s)\n", nt_errstr(status));
4337 return false;
4340 /* Open the original file. */
4341 status = cli_ntcreate(cli, fname, 0, FILE_READ_DATA,
4342 FILE_ATTRIBUTE_NORMAL,
4343 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4344 FILE_OPEN_IF, 0, 0, &fnum);
4345 if (!NT_STATUS_IS_OK(status)) {
4346 printf("ntcreate of %s failed (%s)\n", fname, nt_errstr(status));
4347 return false;
4350 /* Unlink the hard link path. */
4351 status = cli_ntcreate(cli, fname_ln, 0, DELETE_ACCESS,
4352 FILE_ATTRIBUTE_NORMAL,
4353 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4354 FILE_OPEN_IF, 0, 0, &fnum1);
4355 if (!NT_STATUS_IS_OK(status)) {
4356 printf("ntcreate of %s failed (%s)\n", fname_ln, nt_errstr(status));
4357 return false;
4359 status = cli_nt_delete_on_close(cli, fnum1, true);
4360 if (!NT_STATUS_IS_OK(status)) {
4361 d_printf("(%s) failed to set delete_on_close %s: %s\n",
4362 __location__, fname_ln, nt_errstr(status));
4363 return false;
4366 status = cli_close(cli, fnum1);
4367 if (!NT_STATUS_IS_OK(status)) {
4368 printf("close %s failed (%s)\n",
4369 fname_ln, nt_errstr(status));
4370 return false;
4373 status = cli_close(cli, fnum);
4374 if (!NT_STATUS_IS_OK(status)) {
4375 printf("close %s failed (%s)\n",
4376 fname, nt_errstr(status));
4377 return false;
4380 /* Ensure the original file is still there. */
4381 status = cli_getatr(cli, fname, NULL, NULL, &t);
4382 if (!NT_STATUS_IS_OK(status)) {
4383 printf("%s getatr on file %s failed (%s)\n",
4384 __location__,
4385 fname,
4386 nt_errstr(status));
4387 correct = False;
4390 /* Ensure the link path is gone. */
4391 status = cli_getatr(cli, fname_ln, NULL, NULL, &t);
4392 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
4393 printf("%s, getatr for file %s returned wrong error code %s "
4394 "- should have been deleted\n",
4395 __location__,
4396 fname_ln, nt_errstr(status));
4397 correct = False;
4400 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4401 cli_unlink(cli, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4403 if (!torture_close_connection(cli)) {
4404 correct = false;
4407 printf("finished deletetest-ln\n");
4409 return correct;
4413 print out server properties
4415 static bool run_properties(int dummy)
4417 struct cli_state *cli;
4418 bool correct = True;
4420 printf("starting properties test\n");
4422 ZERO_STRUCT(cli);
4424 if (!torture_open_connection(&cli, 0)) {
4425 return False;
4428 cli_sockopt(cli, sockops);
4430 d_printf("Capabilities 0x%08x\n", cli->capabilities);
4432 if (!torture_close_connection(cli)) {
4433 correct = False;
4436 return correct;
4441 /* FIRST_DESIRED_ACCESS 0xf019f */
4442 #define FIRST_DESIRED_ACCESS FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|\
4443 FILE_READ_EA| /* 0xf */ \
4444 FILE_WRITE_EA|FILE_READ_ATTRIBUTES| /* 0x90 */ \
4445 FILE_WRITE_ATTRIBUTES| /* 0x100 */ \
4446 DELETE_ACCESS|READ_CONTROL_ACCESS|\
4447 WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS /* 0xf0000 */
4448 /* SECOND_DESIRED_ACCESS 0xe0080 */
4449 #define SECOND_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
4450 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
4451 WRITE_OWNER_ACCESS /* 0xe0000 */
4453 #if 0
4454 #define THIRD_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
4455 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
4456 FILE_READ_DATA|\
4457 WRITE_OWNER_ACCESS /* */
4458 #endif
4461 Test ntcreate calls made by xcopy
4463 static bool run_xcopy(int dummy)
4465 static struct cli_state *cli1;
4466 const char *fname = "\\test.txt";
4467 bool correct = True;
4468 uint16_t fnum1, fnum2;
4469 NTSTATUS status;
4471 printf("starting xcopy test\n");
4473 if (!torture_open_connection(&cli1, 0)) {
4474 return False;
4477 status = cli_ntcreate(cli1, fname, 0, FIRST_DESIRED_ACCESS,
4478 FILE_ATTRIBUTE_ARCHIVE, FILE_SHARE_NONE,
4479 FILE_OVERWRITE_IF, 0x4044, 0, &fnum1);
4480 if (!NT_STATUS_IS_OK(status)) {
4481 printf("First open failed - %s\n", nt_errstr(status));
4482 return False;
4485 status = cli_ntcreate(cli1, fname, 0, SECOND_DESIRED_ACCESS, 0,
4486 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4487 FILE_OPEN, 0x200000, 0, &fnum2);
4488 if (!NT_STATUS_IS_OK(status)) {
4489 printf("second open failed - %s\n", nt_errstr(status));
4490 return False;
4493 if (!torture_close_connection(cli1)) {
4494 correct = False;
4497 return correct;
4501 Test rename on files open with share delete and no share delete.
4503 static bool run_rename(int dummy)
4505 static struct cli_state *cli1;
4506 const char *fname = "\\test.txt";
4507 const char *fname1 = "\\test1.txt";
4508 bool correct = True;
4509 uint16_t fnum1;
4510 uint16_t attr;
4511 NTSTATUS status;
4513 printf("starting rename test\n");
4515 if (!torture_open_connection(&cli1, 0)) {
4516 return False;
4519 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4520 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4522 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
4523 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
4524 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4525 if (!NT_STATUS_IS_OK(status)) {
4526 printf("First open failed - %s\n", nt_errstr(status));
4527 return False;
4530 status = cli_rename(cli1, fname, fname1);
4531 if (!NT_STATUS_IS_OK(status)) {
4532 printf("First rename failed (SHARE_READ) (this is correct) - %s\n", nt_errstr(status));
4533 } else {
4534 printf("First rename succeeded (SHARE_READ) - this should have failed !\n");
4535 correct = False;
4538 status = cli_close(cli1, fnum1);
4539 if (!NT_STATUS_IS_OK(status)) {
4540 printf("close - 1 failed (%s)\n", nt_errstr(status));
4541 return False;
4544 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4545 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4546 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4547 #if 0
4548 FILE_SHARE_DELETE|FILE_SHARE_NONE,
4549 #else
4550 FILE_SHARE_DELETE|FILE_SHARE_READ,
4551 #endif
4552 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4553 if (!NT_STATUS_IS_OK(status)) {
4554 printf("Second open failed - %s\n", nt_errstr(status));
4555 return False;
4558 status = cli_rename(cli1, fname, fname1);
4559 if (!NT_STATUS_IS_OK(status)) {
4560 printf("Second rename failed (SHARE_DELETE | SHARE_READ) - this should have succeeded - %s\n", nt_errstr(status));
4561 correct = False;
4562 } else {
4563 printf("Second rename succeeded (SHARE_DELETE | SHARE_READ)\n");
4566 status = cli_close(cli1, fnum1);
4567 if (!NT_STATUS_IS_OK(status)) {
4568 printf("close - 2 failed (%s)\n", nt_errstr(status));
4569 return False;
4572 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4573 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4575 status = cli_ntcreate(cli1, fname, 0, READ_CONTROL_ACCESS,
4576 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4577 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4578 if (!NT_STATUS_IS_OK(status)) {
4579 printf("Third open failed - %s\n", nt_errstr(status));
4580 return False;
4584 #if 0
4586 uint16_t fnum2;
4588 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
4589 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
4590 printf("Fourth open failed - %s\n", cli_errstr(cli1));
4591 return False;
4593 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum2, true))) {
4594 printf("[8] setting delete_on_close on file failed !\n");
4595 return False;
4598 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
4599 printf("close - 4 failed (%s)\n", cli_errstr(cli1));
4600 return False;
4603 #endif
4605 status = cli_rename(cli1, fname, fname1);
4606 if (!NT_STATUS_IS_OK(status)) {
4607 printf("Third rename failed (SHARE_NONE) - this should have succeeded - %s\n", nt_errstr(status));
4608 correct = False;
4609 } else {
4610 printf("Third rename succeeded (SHARE_NONE)\n");
4613 status = cli_close(cli1, fnum1);
4614 if (!NT_STATUS_IS_OK(status)) {
4615 printf("close - 3 failed (%s)\n", nt_errstr(status));
4616 return False;
4619 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4620 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4622 /*----*/
4624 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
4625 FILE_ATTRIBUTE_NORMAL,
4626 FILE_SHARE_READ | FILE_SHARE_WRITE,
4627 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4628 if (!NT_STATUS_IS_OK(status)) {
4629 printf("Fourth open failed - %s\n", nt_errstr(status));
4630 return False;
4633 status = cli_rename(cli1, fname, fname1);
4634 if (!NT_STATUS_IS_OK(status)) {
4635 printf("Fourth rename failed (SHARE_READ | SHARE_WRITE) (this is correct) - %s\n", nt_errstr(status));
4636 } else {
4637 printf("Fourth rename succeeded (SHARE_READ | SHARE_WRITE) - this should have failed !\n");
4638 correct = False;
4641 status = cli_close(cli1, fnum1);
4642 if (!NT_STATUS_IS_OK(status)) {
4643 printf("close - 4 failed (%s)\n", nt_errstr(status));
4644 return False;
4647 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4648 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4650 /*--*/
4652 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
4653 FILE_ATTRIBUTE_NORMAL,
4654 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
4655 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4656 if (!NT_STATUS_IS_OK(status)) {
4657 printf("Fifth open failed - %s\n", nt_errstr(status));
4658 return False;
4661 status = cli_rename(cli1, fname, fname1);
4662 if (!NT_STATUS_IS_OK(status)) {
4663 printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have succeeded - %s ! \n", nt_errstr(status));
4664 correct = False;
4665 } else {
4666 printf("Fifth rename succeeded (SHARE_READ | SHARE_WRITE | SHARE_DELETE) (this is correct) - %s\n", nt_errstr(status));
4670 * Now check if the first name still exists ...
4673 /* if (!NT_STATUS_OP(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
4674 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
4675 printf("Opening original file after rename of open file fails: %s\n",
4676 cli_errstr(cli1));
4678 else {
4679 printf("Opening original file after rename of open file works ...\n");
4680 (void)cli_close(cli1, fnum2);
4681 } */
4683 /*--*/
4684 status = cli_close(cli1, fnum1);
4685 if (!NT_STATUS_IS_OK(status)) {
4686 printf("close - 5 failed (%s)\n", nt_errstr(status));
4687 return False;
4690 /* Check that the renamed file has FILE_ATTRIBUTE_ARCHIVE. */
4691 status = cli_getatr(cli1, fname1, &attr, NULL, NULL);
4692 if (!NT_STATUS_IS_OK(status)) {
4693 printf("getatr on file %s failed - %s ! \n",
4694 fname1, nt_errstr(status));
4695 correct = False;
4696 } else {
4697 if (attr != FILE_ATTRIBUTE_ARCHIVE) {
4698 printf("Renamed file %s has wrong attr 0x%x "
4699 "(should be 0x%x)\n",
4700 fname1,
4701 attr,
4702 (unsigned int)FILE_ATTRIBUTE_ARCHIVE);
4703 correct = False;
4704 } else {
4705 printf("Renamed file %s has archive bit set\n", fname1);
4709 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4710 cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4712 if (!torture_close_connection(cli1)) {
4713 correct = False;
4716 return correct;
4719 static bool run_pipe_number(int dummy)
4721 struct cli_state *cli1;
4722 const char *pipe_name = "\\SPOOLSS";
4723 uint16_t fnum;
4724 int num_pipes = 0;
4725 NTSTATUS status;
4727 printf("starting pipenumber test\n");
4728 if (!torture_open_connection(&cli1, 0)) {
4729 return False;
4732 cli_sockopt(cli1, sockops);
4733 while(1) {
4734 status = cli_ntcreate(cli1, pipe_name, 0, FILE_READ_DATA,
4735 FILE_ATTRIBUTE_NORMAL,
4736 FILE_SHARE_READ|FILE_SHARE_WRITE,
4737 FILE_OPEN_IF, 0, 0, &fnum);
4738 if (!NT_STATUS_IS_OK(status)) {
4739 printf("Open of pipe %s failed with error (%s)\n", pipe_name, nt_errstr(status));
4740 break;
4742 num_pipes++;
4743 printf("\r%6d", num_pipes);
4746 printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
4747 torture_close_connection(cli1);
4748 return True;
4752 Test open mode returns on read-only files.
4754 static bool run_opentest(int dummy)
4756 static struct cli_state *cli1;
4757 static struct cli_state *cli2;
4758 const char *fname = "\\readonly.file";
4759 uint16_t fnum1, fnum2;
4760 char buf[20];
4761 SMB_OFF_T fsize;
4762 bool correct = True;
4763 char *tmp_path;
4764 NTSTATUS status;
4766 printf("starting open test\n");
4768 if (!torture_open_connection(&cli1, 0)) {
4769 return False;
4772 cli_setatr(cli1, fname, 0, 0);
4773 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4775 cli_sockopt(cli1, sockops);
4777 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
4778 if (!NT_STATUS_IS_OK(status)) {
4779 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
4780 return False;
4783 status = cli_close(cli1, fnum1);
4784 if (!NT_STATUS_IS_OK(status)) {
4785 printf("close2 failed (%s)\n", nt_errstr(status));
4786 return False;
4789 status = cli_setatr(cli1, fname, FILE_ATTRIBUTE_READONLY, 0);
4790 if (!NT_STATUS_IS_OK(status)) {
4791 printf("cli_setatr failed (%s)\n", nt_errstr(status));
4792 return False;
4795 status = cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1);
4796 if (!NT_STATUS_IS_OK(status)) {
4797 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
4798 return False;
4801 /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
4802 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4804 if (check_error(__LINE__, cli1, ERRDOS, ERRnoaccess,
4805 NT_STATUS_ACCESS_DENIED)) {
4806 printf("correct error code ERRDOS/ERRnoaccess returned\n");
4809 printf("finished open test 1\n");
4811 cli_close(cli1, fnum1);
4813 /* Now try not readonly and ensure ERRbadshare is returned. */
4815 cli_setatr(cli1, fname, 0, 0);
4817 status = cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1);
4818 if (!NT_STATUS_IS_OK(status)) {
4819 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
4820 return False;
4823 /* This will fail - but the error should be ERRshare. */
4824 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4826 if (check_error(__LINE__, cli1, ERRDOS, ERRbadshare,
4827 NT_STATUS_SHARING_VIOLATION)) {
4828 printf("correct error code ERRDOS/ERRbadshare returned\n");
4831 status = cli_close(cli1, fnum1);
4832 if (!NT_STATUS_IS_OK(status)) {
4833 printf("close2 failed (%s)\n", nt_errstr(status));
4834 return False;
4837 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4839 printf("finished open test 2\n");
4841 /* Test truncate open disposition on file opened for read. */
4842 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
4843 if (!NT_STATUS_IS_OK(status)) {
4844 printf("(3) open (1) of %s failed (%s)\n", fname, nt_errstr(status));
4845 return False;
4848 /* write 20 bytes. */
4850 memset(buf, '\0', 20);
4852 status = cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, 20, NULL);
4853 if (!NT_STATUS_IS_OK(status)) {
4854 printf("write failed (%s)\n", nt_errstr(status));
4855 correct = False;
4858 status = cli_close(cli1, fnum1);
4859 if (!NT_STATUS_IS_OK(status)) {
4860 printf("(3) close1 failed (%s)\n", nt_errstr(status));
4861 return False;
4864 /* Ensure size == 20. */
4865 status = cli_getatr(cli1, fname, NULL, &fsize, NULL);
4866 if (!NT_STATUS_IS_OK(status)) {
4867 printf("(3) getatr failed (%s)\n", nt_errstr(status));
4868 return False;
4871 if (fsize != 20) {
4872 printf("(3) file size != 20\n");
4873 return False;
4876 /* Now test if we can truncate a file opened for readonly. */
4877 status = cli_open(cli1, fname, O_RDONLY|O_TRUNC, DENY_NONE, &fnum1);
4878 if (!NT_STATUS_IS_OK(status)) {
4879 printf("(3) open (2) of %s failed (%s)\n", fname, nt_errstr(status));
4880 return False;
4883 status = cli_close(cli1, fnum1);
4884 if (!NT_STATUS_IS_OK(status)) {
4885 printf("close2 failed (%s)\n", nt_errstr(status));
4886 return False;
4889 /* Ensure size == 0. */
4890 status = cli_getatr(cli1, fname, NULL, &fsize, NULL);
4891 if (!NT_STATUS_IS_OK(status)) {
4892 printf("(3) getatr failed (%s)\n", nt_errstr(status));
4893 return False;
4896 if (fsize != 0) {
4897 printf("(3) file size != 0\n");
4898 return False;
4900 printf("finished open test 3\n");
4902 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4904 printf("Do ctemp tests\n");
4905 status = cli_ctemp(cli1, talloc_tos(), "\\", &fnum1, &tmp_path);
4906 if (!NT_STATUS_IS_OK(status)) {
4907 printf("ctemp failed (%s)\n", nt_errstr(status));
4908 return False;
4911 printf("ctemp gave path %s\n", tmp_path);
4912 status = cli_close(cli1, fnum1);
4913 if (!NT_STATUS_IS_OK(status)) {
4914 printf("close of temp failed (%s)\n", nt_errstr(status));
4917 status = cli_unlink(cli1, tmp_path, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4918 if (!NT_STATUS_IS_OK(status)) {
4919 printf("unlink of temp failed (%s)\n", nt_errstr(status));
4922 /* Test the non-io opens... */
4924 if (!torture_open_connection(&cli2, 1)) {
4925 return False;
4928 cli_setatr(cli2, fname, 0, 0);
4929 cli_unlink(cli2, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4931 cli_sockopt(cli2, sockops);
4933 printf("TEST #1 testing 2 non-io opens (no delete)\n");
4934 status = cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES,
4935 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4936 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4937 if (!NT_STATUS_IS_OK(status)) {
4938 printf("TEST #1 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
4939 return False;
4942 status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
4943 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4944 FILE_OPEN_IF, 0, 0, &fnum2);
4945 if (!NT_STATUS_IS_OK(status)) {
4946 printf("TEST #1 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
4947 return False;
4950 status = cli_close(cli1, fnum1);
4951 if (!NT_STATUS_IS_OK(status)) {
4952 printf("TEST #1 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
4953 return False;
4956 status = cli_close(cli2, fnum2);
4957 if (!NT_STATUS_IS_OK(status)) {
4958 printf("TEST #1 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
4959 return False;
4962 printf("non-io open test #1 passed.\n");
4964 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
4966 printf("TEST #2 testing 2 non-io opens (first with delete)\n");
4968 status = cli_ntcreate(cli1, fname, 0,
4969 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
4970 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4971 FILE_OVERWRITE_IF, 0, 0, &fnum1);
4972 if (!NT_STATUS_IS_OK(status)) {
4973 printf("TEST #2 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
4974 return False;
4977 status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
4978 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
4979 FILE_OPEN_IF, 0, 0, &fnum2);
4980 if (!NT_STATUS_IS_OK(status)) {
4981 printf("TEST #2 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
4982 return False;
4985 status = cli_close(cli1, fnum1);
4986 if (!NT_STATUS_IS_OK(status)) {
4987 printf("TEST #2 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
4988 return False;
4991 status = cli_close(cli2, fnum2);
4992 if (!NT_STATUS_IS_OK(status)) {
4993 printf("TEST #2 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
4994 return False;
4997 printf("non-io open test #2 passed.\n");
4999 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5001 printf("TEST #3 testing 2 non-io opens (second with delete)\n");
5003 status = cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES,
5004 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5005 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5006 if (!NT_STATUS_IS_OK(status)) {
5007 printf("TEST #3 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
5008 return False;
5011 status = cli_ntcreate(cli2, fname, 0,
5012 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5013 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5014 FILE_OPEN_IF, 0, 0, &fnum2);
5015 if (!NT_STATUS_IS_OK(status)) {
5016 printf("TEST #3 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
5017 return False;
5020 status = cli_close(cli1, fnum1);
5021 if (!NT_STATUS_IS_OK(status)) {
5022 printf("TEST #3 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
5023 return False;
5026 status = cli_close(cli2, fnum2);
5027 if (!NT_STATUS_IS_OK(status)) {
5028 printf("TEST #3 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
5029 return False;
5032 printf("non-io open test #3 passed.\n");
5034 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5036 printf("TEST #4 testing 2 non-io opens (both with delete)\n");
5038 status = cli_ntcreate(cli1, fname, 0,
5039 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5040 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5041 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5042 if (!NT_STATUS_IS_OK(status)) {
5043 printf("TEST #4 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
5044 return False;
5047 status = cli_ntcreate(cli2, fname, 0,
5048 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5049 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5050 FILE_OPEN_IF, 0, 0, &fnum2);
5051 if (NT_STATUS_IS_OK(status)) {
5052 printf("TEST #4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, nt_errstr(status));
5053 return False;
5056 printf("TEST #4 open 2 of %s gave %s (correct error should be %s)\n", fname, nt_errstr(status), "sharing violation");
5058 status = cli_close(cli1, fnum1);
5059 if (!NT_STATUS_IS_OK(status)) {
5060 printf("TEST #4 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
5061 return False;
5064 printf("non-io open test #4 passed.\n");
5066 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5068 printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
5070 status = cli_ntcreate(cli1, fname, 0,
5071 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5072 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_DELETE,
5073 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5074 if (!NT_STATUS_IS_OK(status)) {
5075 printf("TEST #5 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
5076 return False;
5079 status = cli_ntcreate(cli2, fname, 0,
5080 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5081 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_DELETE,
5082 FILE_OPEN_IF, 0, 0, &fnum2);
5083 if (!NT_STATUS_IS_OK(status)) {
5084 printf("TEST #5 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
5085 return False;
5088 status = cli_close(cli1, fnum1);
5089 if (!NT_STATUS_IS_OK(status)) {
5090 printf("TEST #5 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
5091 return False;
5094 status = cli_close(cli2, fnum2);
5095 if (!NT_STATUS_IS_OK(status)) {
5096 printf("TEST #5 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
5097 return False;
5100 printf("non-io open test #5 passed.\n");
5102 printf("TEST #6 testing 1 non-io open, one io open\n");
5104 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5106 status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA,
5107 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5108 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5109 if (!NT_STATUS_IS_OK(status)) {
5110 printf("TEST #6 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
5111 return False;
5114 status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
5115 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
5116 FILE_OPEN_IF, 0, 0, &fnum2);
5117 if (!NT_STATUS_IS_OK(status)) {
5118 printf("TEST #6 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
5119 return False;
5122 status = cli_close(cli1, fnum1);
5123 if (!NT_STATUS_IS_OK(status)) {
5124 printf("TEST #6 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
5125 return False;
5128 status = cli_close(cli2, fnum2);
5129 if (!NT_STATUS_IS_OK(status)) {
5130 printf("TEST #6 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
5131 return False;
5134 printf("non-io open test #6 passed.\n");
5136 printf("TEST #7 testing 1 non-io open, one io open with delete\n");
5138 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5140 status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA,
5141 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
5142 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5143 if (!NT_STATUS_IS_OK(status)) {
5144 printf("TEST #7 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
5145 return False;
5148 status = cli_ntcreate(cli2, fname, 0,
5149 DELETE_ACCESS|FILE_READ_ATTRIBUTES,
5150 FILE_ATTRIBUTE_NORMAL,
5151 FILE_SHARE_READ|FILE_SHARE_DELETE,
5152 FILE_OPEN_IF, 0, 0, &fnum2);
5153 if (NT_STATUS_IS_OK(status)) {
5154 printf("TEST #7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, nt_errstr(status));
5155 return False;
5158 printf("TEST #7 open 2 of %s gave %s (correct error should be %s)\n", fname, nt_errstr(status), "sharing violation");
5160 status = cli_close(cli1, fnum1);
5161 if (!NT_STATUS_IS_OK(status)) {
5162 printf("TEST #7 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
5163 return False;
5166 printf("non-io open test #7 passed.\n");
5168 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5170 printf("TEST #8 testing open without WRITE_ATTRIBUTES, updating close write time.\n");
5171 status = cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL,
5172 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
5173 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5174 if (!NT_STATUS_IS_OK(status)) {
5175 printf("TEST #8 open of %s failed (%s)\n", fname, nt_errstr(status));
5176 correct = false;
5177 goto out;
5180 /* Write to ensure we have to update the file time. */
5181 status = cli_writeall(cli1, fnum1, 0, (const uint8_t *)"TEST DATA\n", 0, 10,
5182 NULL);
5183 if (!NT_STATUS_IS_OK(status)) {
5184 printf("TEST #8 cli_write failed: %s\n", nt_errstr(status));
5185 correct = false;
5186 goto out;
5189 status = cli_close(cli1, fnum1);
5190 if (!NT_STATUS_IS_OK(status)) {
5191 printf("TEST #8 close of %s failed (%s)\n", fname, nt_errstr(status));
5192 correct = false;
5195 out:
5197 if (!torture_close_connection(cli1)) {
5198 correct = False;
5200 if (!torture_close_connection(cli2)) {
5201 correct = False;
5204 return correct;
5207 NTSTATUS torture_setup_unix_extensions(struct cli_state *cli)
5209 uint16 major, minor;
5210 uint32 caplow, caphigh;
5211 NTSTATUS status;
5213 if (!SERVER_HAS_UNIX_CIFS(cli)) {
5214 printf("Server doesn't support UNIX CIFS extensions.\n");
5215 return NT_STATUS_NOT_SUPPORTED;
5218 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
5219 &caphigh);
5220 if (!NT_STATUS_IS_OK(status)) {
5221 printf("Server didn't return UNIX CIFS extensions: %s\n",
5222 nt_errstr(status));
5223 return status;
5226 status = cli_set_unix_extensions_capabilities(cli, major, minor,
5227 caplow, caphigh);
5228 if (!NT_STATUS_IS_OK(status)) {
5229 printf("Server doesn't support setting UNIX CIFS extensions: "
5230 "%s.\n", nt_errstr(status));
5231 return status;
5234 return NT_STATUS_OK;
5238 Test POSIX open /mkdir calls.
5240 static bool run_simple_posix_open_test(int dummy)
5242 static struct cli_state *cli1;
5243 const char *fname = "posix:file";
5244 const char *hname = "posix:hlink";
5245 const char *sname = "posix:symlink";
5246 const char *dname = "posix:dir";
5247 char buf[10];
5248 char namebuf[11];
5249 uint16_t fnum1 = (uint16_t)-1;
5250 SMB_STRUCT_STAT sbuf;
5251 bool correct = false;
5252 NTSTATUS status;
5254 printf("Starting simple POSIX open test\n");
5256 if (!torture_open_connection(&cli1, 0)) {
5257 return false;
5260 cli_sockopt(cli1, sockops);
5262 status = torture_setup_unix_extensions(cli1);
5263 if (!NT_STATUS_IS_OK(status)) {
5264 return false;
5267 cli_setatr(cli1, fname, 0, 0);
5268 cli_posix_unlink(cli1, fname);
5269 cli_setatr(cli1, dname, 0, 0);
5270 cli_posix_rmdir(cli1, dname);
5271 cli_setatr(cli1, hname, 0, 0);
5272 cli_posix_unlink(cli1, hname);
5273 cli_setatr(cli1, sname, 0, 0);
5274 cli_posix_unlink(cli1, sname);
5276 /* Create a directory. */
5277 status = cli_posix_mkdir(cli1, dname, 0777);
5278 if (!NT_STATUS_IS_OK(status)) {
5279 printf("POSIX mkdir of %s failed (%s)\n", dname, nt_errstr(status));
5280 goto out;
5283 status = cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL,
5284 0600, &fnum1);
5285 if (!NT_STATUS_IS_OK(status)) {
5286 printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
5287 goto out;
5290 /* Test ftruncate - set file size. */
5291 status = cli_ftruncate(cli1, fnum1, 1000);
5292 if (!NT_STATUS_IS_OK(status)) {
5293 printf("ftruncate failed (%s)\n", nt_errstr(status));
5294 goto out;
5297 /* Ensure st_size == 1000 */
5298 status = cli_posix_stat(cli1, fname, &sbuf);
5299 if (!NT_STATUS_IS_OK(status)) {
5300 printf("stat failed (%s)\n", nt_errstr(status));
5301 goto out;
5304 if (sbuf.st_ex_size != 1000) {
5305 printf("ftruncate - stat size (%u) != 1000\n", (unsigned int)sbuf.st_ex_size);
5306 goto out;
5309 /* Test ftruncate - set file size back to zero. */
5310 status = cli_ftruncate(cli1, fnum1, 0);
5311 if (!NT_STATUS_IS_OK(status)) {
5312 printf("ftruncate failed (%s)\n", nt_errstr(status));
5313 goto out;
5316 status = cli_close(cli1, fnum1);
5317 if (!NT_STATUS_IS_OK(status)) {
5318 printf("close failed (%s)\n", nt_errstr(status));
5319 goto out;
5322 /* Now open the file again for read only. */
5323 status = cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1);
5324 if (!NT_STATUS_IS_OK(status)) {
5325 printf("POSIX open of %s failed (%s)\n", fname, nt_errstr(status));
5326 goto out;
5329 /* Now unlink while open. */
5330 status = cli_posix_unlink(cli1, fname);
5331 if (!NT_STATUS_IS_OK(status)) {
5332 printf("POSIX unlink of %s failed (%s)\n", fname, nt_errstr(status));
5333 goto out;
5336 status = cli_close(cli1, fnum1);
5337 if (!NT_STATUS_IS_OK(status)) {
5338 printf("close(2) failed (%s)\n", nt_errstr(status));
5339 goto out;
5342 /* Ensure the file has gone. */
5343 status = cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1);
5344 if (NT_STATUS_IS_OK(status)) {
5345 printf("POSIX open of %s succeeded, should have been deleted.\n", fname);
5346 goto out;
5349 /* Create again to test open with O_TRUNC. */
5350 status = cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1);
5351 if (!NT_STATUS_IS_OK(status)) {
5352 printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
5353 goto out;
5356 /* Test ftruncate - set file size. */
5357 status = cli_ftruncate(cli1, fnum1, 1000);
5358 if (!NT_STATUS_IS_OK(status)) {
5359 printf("ftruncate failed (%s)\n", nt_errstr(status));
5360 goto out;
5363 /* Ensure st_size == 1000 */
5364 status = cli_posix_stat(cli1, fname, &sbuf);
5365 if (!NT_STATUS_IS_OK(status)) {
5366 printf("stat failed (%s)\n", nt_errstr(status));
5367 goto out;
5370 if (sbuf.st_ex_size != 1000) {
5371 printf("ftruncate - stat size (%u) != 1000\n", (unsigned int)sbuf.st_ex_size);
5372 goto out;
5375 status = cli_close(cli1, fnum1);
5376 if (!NT_STATUS_IS_OK(status)) {
5377 printf("close(2) failed (%s)\n", nt_errstr(status));
5378 goto out;
5381 /* Re-open with O_TRUNC. */
5382 status = cli_posix_open(cli1, fname, O_WRONLY|O_TRUNC, 0600, &fnum1);
5383 if (!NT_STATUS_IS_OK(status)) {
5384 printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
5385 goto out;
5388 /* Ensure st_size == 0 */
5389 status = cli_posix_stat(cli1, fname, &sbuf);
5390 if (!NT_STATUS_IS_OK(status)) {
5391 printf("stat failed (%s)\n", nt_errstr(status));
5392 goto out;
5395 if (sbuf.st_ex_size != 0) {
5396 printf("O_TRUNC - stat size (%u) != 0\n", (unsigned int)sbuf.st_ex_size);
5397 goto out;
5400 status = cli_close(cli1, fnum1);
5401 if (!NT_STATUS_IS_OK(status)) {
5402 printf("close failed (%s)\n", nt_errstr(status));
5403 goto out;
5406 status = cli_posix_unlink(cli1, fname);
5407 if (!NT_STATUS_IS_OK(status)) {
5408 printf("POSIX unlink of %s failed (%s)\n", fname, nt_errstr(status));
5409 goto out;
5412 status = cli_posix_open(cli1, dname, O_RDONLY, 0, &fnum1);
5413 if (!NT_STATUS_IS_OK(status)) {
5414 printf("POSIX open directory O_RDONLY of %s failed (%s)\n",
5415 dname, nt_errstr(status));
5416 goto out;
5419 cli_close(cli1, fnum1);
5421 /* What happens when we try and POSIX open a directory for write ? */
5422 status = cli_posix_open(cli1, dname, O_RDWR, 0, &fnum1);
5423 if (NT_STATUS_IS_OK(status)) {
5424 printf("POSIX open of directory %s succeeded, should have failed.\n", fname);
5425 goto out;
5426 } else {
5427 if (!check_both_error(__LINE__, status, ERRDOS, EISDIR,
5428 NT_STATUS_FILE_IS_A_DIRECTORY)) {
5429 goto out;
5433 /* Create the file. */
5434 status = cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL,
5435 0600, &fnum1);
5436 if (!NT_STATUS_IS_OK(status)) {
5437 printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
5438 goto out;
5441 /* Write some data into it. */
5442 status = cli_writeall(cli1, fnum1, 0, (const uint8_t *)"TEST DATA\n", 0, 10,
5443 NULL);
5444 if (!NT_STATUS_IS_OK(status)) {
5445 printf("cli_write failed: %s\n", nt_errstr(status));
5446 goto out;
5449 cli_close(cli1, fnum1);
5451 /* Now create a hardlink. */
5452 status = cli_posix_hardlink(cli1, fname, hname);
5453 if (!NT_STATUS_IS_OK(status)) {
5454 printf("POSIX hardlink of %s failed (%s)\n", hname, nt_errstr(status));
5455 goto out;
5458 /* Now create a symlink. */
5459 status = cli_posix_symlink(cli1, fname, sname);
5460 if (!NT_STATUS_IS_OK(status)) {
5461 printf("POSIX symlink of %s failed (%s)\n", sname, nt_errstr(status));
5462 goto out;
5465 /* Open the hardlink for read. */
5466 status = cli_posix_open(cli1, hname, O_RDONLY, 0, &fnum1);
5467 if (!NT_STATUS_IS_OK(status)) {
5468 printf("POSIX open of %s failed (%s)\n", hname, nt_errstr(status));
5469 goto out;
5472 if (cli_read(cli1, fnum1, buf, 0, 10) != 10) {
5473 printf("POSIX read of %s failed (%s)\n", hname, cli_errstr(cli1));
5474 goto out;
5477 if (memcmp(buf, "TEST DATA\n", 10)) {
5478 printf("invalid data read from hardlink\n");
5479 goto out;
5482 /* Do a POSIX lock/unlock. */
5483 status = cli_posix_lock(cli1, fnum1, 0, 100, true, READ_LOCK);
5484 if (!NT_STATUS_IS_OK(status)) {
5485 printf("POSIX lock failed %s\n", nt_errstr(status));
5486 goto out;
5489 /* Punch a hole in the locked area. */
5490 status = cli_posix_unlock(cli1, fnum1, 10, 80);
5491 if (!NT_STATUS_IS_OK(status)) {
5492 printf("POSIX unlock failed %s\n", nt_errstr(status));
5493 goto out;
5496 cli_close(cli1, fnum1);
5498 /* Open the symlink for read - this should fail. A POSIX
5499 client should not be doing opens on a symlink. */
5500 status = cli_posix_open(cli1, sname, O_RDONLY, 0, &fnum1);
5501 if (NT_STATUS_IS_OK(status)) {
5502 printf("POSIX open of %s succeeded (should have failed)\n", sname);
5503 goto out;
5504 } else {
5505 if (!check_both_error(__LINE__, status, ERRDOS, ERRbadpath,
5506 NT_STATUS_OBJECT_PATH_NOT_FOUND)) {
5507 printf("POSIX open of %s should have failed "
5508 "with NT_STATUS_OBJECT_PATH_NOT_FOUND, "
5509 "failed with %s instead.\n",
5510 sname, nt_errstr(status));
5511 goto out;
5515 status = cli_posix_readlink(cli1, sname, namebuf, sizeof(namebuf));
5516 if (!NT_STATUS_IS_OK(status)) {
5517 printf("POSIX readlink on %s failed (%s)\n", sname, nt_errstr(status));
5518 goto out;
5521 if (strcmp(namebuf, fname) != 0) {
5522 printf("POSIX readlink on %s failed to match name %s (read %s)\n",
5523 sname, fname, namebuf);
5524 goto out;
5527 status = cli_posix_rmdir(cli1, dname);
5528 if (!NT_STATUS_IS_OK(status)) {
5529 printf("POSIX rmdir failed (%s)\n", nt_errstr(status));
5530 goto out;
5533 printf("Simple POSIX open test passed\n");
5534 correct = true;
5536 out:
5538 if (fnum1 != (uint16_t)-1) {
5539 cli_close(cli1, fnum1);
5540 fnum1 = (uint16_t)-1;
5543 cli_setatr(cli1, sname, 0, 0);
5544 cli_posix_unlink(cli1, sname);
5545 cli_setatr(cli1, hname, 0, 0);
5546 cli_posix_unlink(cli1, hname);
5547 cli_setatr(cli1, fname, 0, 0);
5548 cli_posix_unlink(cli1, fname);
5549 cli_setatr(cli1, dname, 0, 0);
5550 cli_posix_rmdir(cli1, dname);
5552 if (!torture_close_connection(cli1)) {
5553 correct = false;
5556 return correct;
5560 static uint32 open_attrs_table[] = {
5561 FILE_ATTRIBUTE_NORMAL,
5562 FILE_ATTRIBUTE_ARCHIVE,
5563 FILE_ATTRIBUTE_READONLY,
5564 FILE_ATTRIBUTE_HIDDEN,
5565 FILE_ATTRIBUTE_SYSTEM,
5567 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY,
5568 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN,
5569 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,
5570 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
5571 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
5572 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
5574 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
5575 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
5576 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
5577 FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_SYSTEM,
5580 struct trunc_open_results {
5581 unsigned int num;
5582 uint32 init_attr;
5583 uint32 trunc_attr;
5584 uint32 result_attr;
5587 static struct trunc_open_results attr_results[] = {
5588 { 0, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
5589 { 1, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
5590 { 2, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
5591 { 16, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
5592 { 17, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
5593 { 18, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
5594 { 51, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5595 { 54, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5596 { 56, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
5597 { 68, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5598 { 71, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5599 { 73, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
5600 { 99, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5601 { 102, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5602 { 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 },
5603 { 116, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5604 { 119, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5605 { 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 },
5606 { 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 },
5607 { 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 },
5608 { 227, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5609 { 230, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
5610 { 232, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
5611 { 244, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5612 { 247, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
5613 { 249, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM }
5616 static bool run_openattrtest(int dummy)
5618 static struct cli_state *cli1;
5619 const char *fname = "\\openattr.file";
5620 uint16_t fnum1;
5621 bool correct = True;
5622 uint16 attr;
5623 unsigned int i, j, k, l;
5624 NTSTATUS status;
5626 printf("starting open attr test\n");
5628 if (!torture_open_connection(&cli1, 0)) {
5629 return False;
5632 cli_sockopt(cli1, sockops);
5634 for (k = 0, i = 0; i < sizeof(open_attrs_table)/sizeof(uint32); i++) {
5635 cli_setatr(cli1, fname, 0, 0);
5636 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5638 status = cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA,
5639 open_attrs_table[i], FILE_SHARE_NONE,
5640 FILE_OVERWRITE_IF, 0, 0, &fnum1);
5641 if (!NT_STATUS_IS_OK(status)) {
5642 printf("open %d (1) of %s failed (%s)\n", i, fname, nt_errstr(status));
5643 return False;
5646 status = cli_close(cli1, fnum1);
5647 if (!NT_STATUS_IS_OK(status)) {
5648 printf("close %d (1) of %s failed (%s)\n", i, fname, nt_errstr(status));
5649 return False;
5652 for (j = 0; j < sizeof(open_attrs_table)/sizeof(uint32); j++) {
5653 status = cli_ntcreate(cli1, fname, 0,
5654 FILE_READ_DATA|FILE_WRITE_DATA,
5655 open_attrs_table[j],
5656 FILE_SHARE_NONE, FILE_OVERWRITE,
5657 0, 0, &fnum1);
5658 if (!NT_STATUS_IS_OK(status)) {
5659 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
5660 if (attr_results[l].num == k) {
5661 printf("[%d] trunc open 0x%x -> 0x%x of %s failed - should have succeeded !(0x%x:%s)\n",
5662 k, open_attrs_table[i],
5663 open_attrs_table[j],
5664 fname, NT_STATUS_V(status), nt_errstr(status));
5665 correct = False;
5669 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
5670 printf("[%d] trunc open 0x%x -> 0x%x failed with wrong error code %s\n",
5671 k, open_attrs_table[i], open_attrs_table[j],
5672 nt_errstr(status));
5673 correct = False;
5675 #if 0
5676 printf("[%d] trunc open 0x%x -> 0x%x failed\n", k, open_attrs_table[i], open_attrs_table[j]);
5677 #endif
5678 k++;
5679 continue;
5682 status = cli_close(cli1, fnum1);
5683 if (!NT_STATUS_IS_OK(status)) {
5684 printf("close %d (2) of %s failed (%s)\n", j, fname, nt_errstr(status));
5685 return False;
5688 status = cli_getatr(cli1, fname, &attr, NULL, NULL);
5689 if (!NT_STATUS_IS_OK(status)) {
5690 printf("getatr(2) failed (%s)\n", nt_errstr(status));
5691 return False;
5694 #if 0
5695 printf("[%d] getatr check [0x%x] trunc [0x%x] got attr 0x%x\n",
5696 k, open_attrs_table[i], open_attrs_table[j], attr );
5697 #endif
5699 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
5700 if (attr_results[l].num == k) {
5701 if (attr != attr_results[l].result_attr ||
5702 open_attrs_table[i] != attr_results[l].init_attr ||
5703 open_attrs_table[j] != attr_results[l].trunc_attr) {
5704 printf("getatr check failed. [0x%x] trunc [0x%x] got attr 0x%x, should be 0x%x\n",
5705 open_attrs_table[i],
5706 open_attrs_table[j],
5707 (unsigned int)attr,
5708 attr_results[l].result_attr);
5709 correct = False;
5711 break;
5714 k++;
5718 cli_setatr(cli1, fname, 0, 0);
5719 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5721 printf("open attr test %s.\n", correct ? "passed" : "failed");
5723 if (!torture_close_connection(cli1)) {
5724 correct = False;
5726 return correct;
5729 static NTSTATUS list_fn(const char *mnt, struct file_info *finfo,
5730 const char *name, void *state)
5732 int *matched = (int *)state;
5733 if (matched != NULL) {
5734 *matched += 1;
5736 return NT_STATUS_OK;
5740 test directory listing speed
5742 static bool run_dirtest(int dummy)
5744 int i;
5745 static struct cli_state *cli;
5746 uint16_t fnum;
5747 struct timeval core_start;
5748 bool correct = True;
5749 int matched;
5751 printf("starting directory test\n");
5753 if (!torture_open_connection(&cli, 0)) {
5754 return False;
5757 cli_sockopt(cli, sockops);
5759 srandom(0);
5760 for (i=0;i<torture_numops;i++) {
5761 fstring fname;
5762 slprintf(fname, sizeof(fname), "\\%x", (int)random());
5763 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum))) {
5764 fprintf(stderr,"Failed to open %s\n", fname);
5765 return False;
5767 cli_close(cli, fnum);
5770 core_start = timeval_current();
5772 matched = 0;
5773 cli_list(cli, "a*.*", 0, list_fn, &matched);
5774 printf("Matched %d\n", matched);
5776 matched = 0;
5777 cli_list(cli, "b*.*", 0, list_fn, &matched);
5778 printf("Matched %d\n", matched);
5780 matched = 0;
5781 cli_list(cli, "xyzabc", 0, list_fn, &matched);
5782 printf("Matched %d\n", matched);
5784 printf("dirtest core %g seconds\n", timeval_elapsed(&core_start));
5786 srandom(0);
5787 for (i=0;i<torture_numops;i++) {
5788 fstring fname;
5789 slprintf(fname, sizeof(fname), "\\%x", (int)random());
5790 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5793 if (!torture_close_connection(cli)) {
5794 correct = False;
5797 printf("finished dirtest\n");
5799 return correct;
5802 static NTSTATUS del_fn(const char *mnt, struct file_info *finfo, const char *mask,
5803 void *state)
5805 struct cli_state *pcli = (struct cli_state *)state;
5806 fstring fname;
5807 slprintf(fname, sizeof(fname), "\\LISTDIR\\%s", finfo->name);
5809 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
5810 return NT_STATUS_OK;
5812 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
5813 if (!NT_STATUS_IS_OK(cli_rmdir(pcli, fname)))
5814 printf("del_fn: failed to rmdir %s\n,", fname );
5815 } else {
5816 if (!NT_STATUS_IS_OK(cli_unlink(pcli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)))
5817 printf("del_fn: failed to unlink %s\n,", fname );
5819 return NT_STATUS_OK;
5824 sees what IOCTLs are supported
5826 bool torture_ioctl_test(int dummy)
5828 static struct cli_state *cli;
5829 uint16_t device, function;
5830 uint16_t fnum;
5831 const char *fname = "\\ioctl.dat";
5832 DATA_BLOB blob;
5833 NTSTATUS status;
5835 if (!torture_open_connection(&cli, 0)) {
5836 return False;
5839 printf("starting ioctl test\n");
5841 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5843 status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
5844 if (!NT_STATUS_IS_OK(status)) {
5845 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
5846 return False;
5849 status = cli_raw_ioctl(cli, fnum, 0x2d0000 | (0x0420<<2), &blob);
5850 printf("ioctl device info: %s\n", nt_errstr(status));
5852 status = cli_raw_ioctl(cli, fnum, IOCTL_QUERY_JOB_INFO, &blob);
5853 printf("ioctl job info: %s\n", nt_errstr(status));
5855 for (device=0;device<0x100;device++) {
5856 printf("ioctl test with device = 0x%x\n", device);
5857 for (function=0;function<0x100;function++) {
5858 uint32 code = (device<<16) | function;
5860 status = cli_raw_ioctl(cli, fnum, code, &blob);
5862 if (NT_STATUS_IS_OK(status)) {
5863 printf("ioctl 0x%x OK : %d bytes\n", (int)code,
5864 (int)blob.length);
5865 data_blob_free(&blob);
5870 if (!torture_close_connection(cli)) {
5871 return False;
5874 return True;
5879 tries varients of chkpath
5881 bool torture_chkpath_test(int dummy)
5883 static struct cli_state *cli;
5884 uint16_t fnum;
5885 bool ret;
5886 NTSTATUS status;
5888 if (!torture_open_connection(&cli, 0)) {
5889 return False;
5892 printf("starting chkpath test\n");
5894 /* cleanup from an old run */
5895 cli_rmdir(cli, "\\chkpath.dir\\dir2");
5896 cli_unlink(cli, "\\chkpath.dir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5897 cli_rmdir(cli, "\\chkpath.dir");
5899 status = cli_mkdir(cli, "\\chkpath.dir");
5900 if (!NT_STATUS_IS_OK(status)) {
5901 printf("mkdir1 failed : %s\n", nt_errstr(status));
5902 return False;
5905 status = cli_mkdir(cli, "\\chkpath.dir\\dir2");
5906 if (!NT_STATUS_IS_OK(status)) {
5907 printf("mkdir2 failed : %s\n", nt_errstr(status));
5908 return False;
5911 status = cli_open(cli, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL,
5912 DENY_NONE, &fnum);
5913 if (!NT_STATUS_IS_OK(status)) {
5914 printf("open1 failed (%s)\n", nt_errstr(status));
5915 return False;
5917 cli_close(cli, fnum);
5919 status = cli_chkpath(cli, "\\chkpath.dir");
5920 if (!NT_STATUS_IS_OK(status)) {
5921 printf("chkpath1 failed: %s\n", nt_errstr(status));
5922 ret = False;
5925 status = cli_chkpath(cli, "\\chkpath.dir\\dir2");
5926 if (!NT_STATUS_IS_OK(status)) {
5927 printf("chkpath2 failed: %s\n", nt_errstr(status));
5928 ret = False;
5931 status = cli_chkpath(cli, "\\chkpath.dir\\foo.txt");
5932 if (!NT_STATUS_IS_OK(status)) {
5933 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
5934 NT_STATUS_NOT_A_DIRECTORY);
5935 } else {
5936 printf("* chkpath on a file should fail\n");
5937 ret = False;
5940 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\bar.txt"))) {
5941 ret = check_error(__LINE__, cli, ERRDOS, ERRbadfile,
5942 NT_STATUS_OBJECT_NAME_NOT_FOUND);
5943 } else {
5944 printf("* chkpath on a non existant file should fail\n");
5945 ret = False;
5948 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dirxx\\bar.txt"))) {
5949 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
5950 NT_STATUS_OBJECT_PATH_NOT_FOUND);
5951 } else {
5952 printf("* chkpath on a non existent component should fail\n");
5953 ret = False;
5956 cli_rmdir(cli, "\\chkpath.dir\\dir2");
5957 cli_unlink(cli, "\\chkpath.dir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5958 cli_rmdir(cli, "\\chkpath.dir");
5960 if (!torture_close_connection(cli)) {
5961 return False;
5964 return ret;
5967 static bool run_eatest(int dummy)
5969 static struct cli_state *cli;
5970 const char *fname = "\\eatest.txt";
5971 bool correct = True;
5972 uint16_t fnum;
5973 int i;
5974 size_t num_eas;
5975 struct ea_struct *ea_list = NULL;
5976 TALLOC_CTX *mem_ctx = talloc_init("eatest");
5977 NTSTATUS status;
5979 printf("starting eatest\n");
5981 if (!torture_open_connection(&cli, 0)) {
5982 talloc_destroy(mem_ctx);
5983 return False;
5986 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
5988 status = cli_ntcreate(cli, fname, 0,
5989 FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
5990 FILE_SHARE_NONE, FILE_OVERWRITE_IF,
5991 0x4044, 0, &fnum);
5992 if (!NT_STATUS_IS_OK(status)) {
5993 printf("open failed - %s\n", nt_errstr(status));
5994 talloc_destroy(mem_ctx);
5995 return False;
5998 for (i = 0; i < 10; i++) {
5999 fstring ea_name, ea_val;
6001 slprintf(ea_name, sizeof(ea_name), "EA_%d", i);
6002 memset(ea_val, (char)i+1, i+1);
6003 status = cli_set_ea_fnum(cli, fnum, ea_name, ea_val, i+1);
6004 if (!NT_STATUS_IS_OK(status)) {
6005 printf("ea_set of name %s failed - %s\n", ea_name,
6006 nt_errstr(status));
6007 talloc_destroy(mem_ctx);
6008 return False;
6012 cli_close(cli, fnum);
6013 for (i = 0; i < 10; i++) {
6014 fstring ea_name, ea_val;
6016 slprintf(ea_name, sizeof(ea_name), "EA_%d", i+10);
6017 memset(ea_val, (char)i+1, i+1);
6018 status = cli_set_ea_path(cli, fname, ea_name, ea_val, i+1);
6019 if (!NT_STATUS_IS_OK(status)) {
6020 printf("ea_set of name %s failed - %s\n", ea_name,
6021 nt_errstr(status));
6022 talloc_destroy(mem_ctx);
6023 return False;
6027 status = cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list);
6028 if (!NT_STATUS_IS_OK(status)) {
6029 printf("ea_get list failed - %s\n", nt_errstr(status));
6030 correct = False;
6033 printf("num_eas = %d\n", (int)num_eas);
6035 if (num_eas != 20) {
6036 printf("Should be 20 EA's stored... failing.\n");
6037 correct = False;
6040 for (i = 0; i < num_eas; i++) {
6041 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
6042 dump_data(0, ea_list[i].value.data,
6043 ea_list[i].value.length);
6046 /* Setting EA's to zero length deletes them. Test this */
6047 printf("Now deleting all EA's - case indepenent....\n");
6049 #if 1
6050 cli_set_ea_path(cli, fname, "", "", 0);
6051 #else
6052 for (i = 0; i < 20; i++) {
6053 fstring ea_name;
6054 slprintf(ea_name, sizeof(ea_name), "ea_%d", i);
6055 status = cli_set_ea_path(cli, fname, ea_name, "", 0);
6056 if (!NT_STATUS_IS_OK(status)) {
6057 printf("ea_set of name %s failed - %s\n", ea_name,
6058 nt_errstr(status));
6059 talloc_destroy(mem_ctx);
6060 return False;
6063 #endif
6065 status = cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list);
6066 if (!NT_STATUS_IS_OK(status)) {
6067 printf("ea_get list failed - %s\n", nt_errstr(status));
6068 correct = False;
6071 printf("num_eas = %d\n", (int)num_eas);
6072 for (i = 0; i < num_eas; i++) {
6073 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
6074 dump_data(0, ea_list[i].value.data,
6075 ea_list[i].value.length);
6078 if (num_eas != 0) {
6079 printf("deleting EA's failed.\n");
6080 correct = False;
6083 /* Try and delete a non existant EA. */
6084 status = cli_set_ea_path(cli, fname, "foo", "", 0);
6085 if (!NT_STATUS_IS_OK(status)) {
6086 printf("deleting non-existant EA 'foo' should succeed. %s\n",
6087 nt_errstr(status));
6088 correct = False;
6091 talloc_destroy(mem_ctx);
6092 if (!torture_close_connection(cli)) {
6093 correct = False;
6096 return correct;
6099 static bool run_dirtest1(int dummy)
6101 int i;
6102 static struct cli_state *cli;
6103 uint16_t fnum;
6104 int num_seen;
6105 bool correct = True;
6107 printf("starting directory test\n");
6109 if (!torture_open_connection(&cli, 0)) {
6110 return False;
6113 cli_sockopt(cli, sockops);
6115 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
6116 cli_list(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, del_fn, cli);
6117 cli_rmdir(cli, "\\LISTDIR");
6118 cli_mkdir(cli, "\\LISTDIR");
6120 /* Create 1000 files and 1000 directories. */
6121 for (i=0;i<1000;i++) {
6122 fstring fname;
6123 slprintf(fname, sizeof(fname), "\\LISTDIR\\f%d", i);
6124 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
6125 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
6126 fprintf(stderr,"Failed to open %s\n", fname);
6127 return False;
6129 cli_close(cli, fnum);
6131 for (i=0;i<1000;i++) {
6132 fstring fname;
6133 slprintf(fname, sizeof(fname), "\\LISTDIR\\d%d", i);
6134 if (!NT_STATUS_IS_OK(cli_mkdir(cli, fname))) {
6135 fprintf(stderr,"Failed to open %s\n", fname);
6136 return False;
6140 /* Now ensure that doing an old list sees both files and directories. */
6141 num_seen = 0;
6142 cli_list_old(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
6143 printf("num_seen = %d\n", num_seen );
6144 /* We should see 100 files + 1000 directories + . and .. */
6145 if (num_seen != 2002)
6146 correct = False;
6148 /* Ensure if we have the "must have" bits we only see the
6149 * relevent entries.
6151 num_seen = 0;
6152 cli_list_old(cli, "\\LISTDIR\\*", (FILE_ATTRIBUTE_DIRECTORY<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
6153 printf("num_seen = %d\n", num_seen );
6154 if (num_seen != 1002)
6155 correct = False;
6157 num_seen = 0;
6158 cli_list_old(cli, "\\LISTDIR\\*", (FILE_ATTRIBUTE_ARCHIVE<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
6159 printf("num_seen = %d\n", num_seen );
6160 if (num_seen != 1000)
6161 correct = False;
6163 /* Delete everything. */
6164 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
6165 cli_list(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, del_fn, cli);
6166 cli_rmdir(cli, "\\LISTDIR");
6168 #if 0
6169 printf("Matched %d\n", cli_list(cli, "a*.*", 0, list_fn, NULL));
6170 printf("Matched %d\n", cli_list(cli, "b*.*", 0, list_fn, NULL));
6171 printf("Matched %d\n", cli_list(cli, "xyzabc", 0, list_fn, NULL));
6172 #endif
6174 if (!torture_close_connection(cli)) {
6175 correct = False;
6178 printf("finished dirtest1\n");
6180 return correct;
6183 static bool run_error_map_extract(int dummy) {
6185 static struct cli_state *c_dos;
6186 static struct cli_state *c_nt;
6187 NTSTATUS status;
6189 uint32 error;
6191 uint32 errnum;
6192 uint8 errclass;
6194 NTSTATUS nt_status;
6196 fstring user;
6198 /* NT-Error connection */
6200 if (!(c_nt = open_nbt_connection())) {
6201 return False;
6204 c_nt->use_spnego = False;
6206 status = cli_negprot(c_nt);
6208 if (!NT_STATUS_IS_OK(status)) {
6209 printf("%s rejected the NT-error negprot (%s)\n", host,
6210 nt_errstr(status));
6211 cli_shutdown(c_nt);
6212 return False;
6215 status = cli_session_setup(c_nt, "", "", 0, "", 0, workgroup);
6216 if (!NT_STATUS_IS_OK(status)) {
6217 printf("%s rejected the NT-error initial session setup (%s)\n",host, nt_errstr(status));
6218 return False;
6221 /* DOS-Error connection */
6223 if (!(c_dos = open_nbt_connection())) {
6224 return False;
6227 c_dos->use_spnego = False;
6228 c_dos->force_dos_errors = True;
6230 status = cli_negprot(c_dos);
6231 if (!NT_STATUS_IS_OK(status)) {
6232 printf("%s rejected the DOS-error negprot (%s)\n", host,
6233 nt_errstr(status));
6234 cli_shutdown(c_dos);
6235 return False;
6238 status = cli_session_setup(c_dos, "", "", 0, "", 0, workgroup);
6239 if (!NT_STATUS_IS_OK(status)) {
6240 printf("%s rejected the DOS-error initial session setup (%s)\n",
6241 host, nt_errstr(status));
6242 return False;
6245 for (error=(0xc0000000 | 0x1); error < (0xc0000000| 0xFFF); error++) {
6246 fstr_sprintf(user, "%X", error);
6248 status = cli_session_setup(c_nt, user,
6249 password, strlen(password),
6250 password, strlen(password),
6251 workgroup);
6252 if (NT_STATUS_IS_OK(status)) {
6253 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
6256 /* Case #1: 32-bit NT errors */
6257 if (cli_is_nt_error(c_nt)) {
6258 nt_status = cli_nt_error(c_nt);
6259 } else {
6260 printf("/** Dos error on NT connection! (%s) */\n",
6261 cli_errstr(c_nt));
6262 nt_status = NT_STATUS(0xc0000000);
6265 status = cli_session_setup(c_dos, user,
6266 password, strlen(password),
6267 password, strlen(password),
6268 workgroup);
6269 if (NT_STATUS_IS_OK(status)) {
6270 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
6273 /* Case #1: 32-bit NT errors */
6274 if (!cli_is_dos_error(c_dos)) {
6275 printf("/** NT error on DOS connection! (%s) */\n",
6276 cli_errstr(c_dos));
6277 errnum = errclass = 0;
6278 } else {
6279 cli_dos_error(c_dos, &errclass, &errnum);
6282 if (NT_STATUS_V(nt_status) != error) {
6283 printf("/*\t{ This NT error code was 'sqashed'\n\t from %s to %s \n\t during the session setup }\n*/\n",
6284 get_nt_error_c_code(talloc_tos(), NT_STATUS(error)),
6285 get_nt_error_c_code(talloc_tos(), nt_status));
6288 printf("\t{%s,\t%s,\t%s},\n",
6289 smb_dos_err_class(errclass),
6290 smb_dos_err_name(errclass, errnum),
6291 get_nt_error_c_code(talloc_tos(), NT_STATUS(error)));
6293 return True;
6296 static bool run_sesssetup_bench(int dummy)
6298 static struct cli_state *c;
6299 const char *fname = "\\file.dat";
6300 uint16_t fnum;
6301 NTSTATUS status;
6302 int i;
6304 if (!torture_open_connection(&c, 0)) {
6305 return false;
6308 status = cli_ntcreate(c, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
6309 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
6310 FILE_DELETE_ON_CLOSE, 0, &fnum);
6311 if (!NT_STATUS_IS_OK(status)) {
6312 d_printf("open %s failed: %s\n", fname, nt_errstr(status));
6313 return false;
6316 for (i=0; i<torture_numops; i++) {
6317 status = cli_session_setup(
6318 c, username,
6319 password, strlen(password),
6320 password, strlen(password),
6321 workgroup);
6322 if (!NT_STATUS_IS_OK(status)) {
6323 d_printf("(%s) cli_session_setup failed: %s\n",
6324 __location__, nt_errstr(status));
6325 return false;
6328 d_printf("\r%d ", (int)c->vuid);
6330 status = cli_ulogoff(c);
6331 if (!NT_STATUS_IS_OK(status)) {
6332 d_printf("(%s) cli_ulogoff failed: %s\n",
6333 __location__, nt_errstr(status));
6334 return false;
6336 c->vuid = 0;
6339 return true;
6342 static bool subst_test(const char *str, const char *user, const char *domain,
6343 uid_t uid, gid_t gid, const char *expected)
6345 char *subst;
6346 bool result = true;
6348 subst = talloc_sub_specified(talloc_tos(), str, user, domain, uid, gid);
6350 if (strcmp(subst, expected) != 0) {
6351 printf("sub_specified(%s, %s, %s, %d, %d) returned [%s], expected "
6352 "[%s]\n", str, user, domain, (int)uid, (int)gid, subst,
6353 expected);
6354 result = false;
6357 TALLOC_FREE(subst);
6358 return result;
6361 static void chain1_open_completion(struct tevent_req *req)
6363 uint16_t fnum;
6364 NTSTATUS status;
6365 status = cli_open_recv(req, &fnum);
6366 TALLOC_FREE(req);
6368 d_printf("cli_open_recv returned %s: %d\n",
6369 nt_errstr(status),
6370 NT_STATUS_IS_OK(status) ? fnum : -1);
6373 static void chain1_write_completion(struct tevent_req *req)
6375 size_t written;
6376 NTSTATUS status;
6377 status = cli_write_andx_recv(req, &written);
6378 TALLOC_FREE(req);
6380 d_printf("cli_write_andx_recv returned %s: %d\n",
6381 nt_errstr(status),
6382 NT_STATUS_IS_OK(status) ? (int)written : -1);
6385 static void chain1_close_completion(struct tevent_req *req)
6387 NTSTATUS status;
6388 bool *done = (bool *)tevent_req_callback_data_void(req);
6390 status = cli_close_recv(req);
6391 *done = true;
6393 TALLOC_FREE(req);
6395 d_printf("cli_close returned %s\n", nt_errstr(status));
6398 static bool run_chain1(int dummy)
6400 struct cli_state *cli1;
6401 struct event_context *evt = event_context_init(NULL);
6402 struct tevent_req *reqs[3], *smbreqs[3];
6403 bool done = false;
6404 const char *str = "foobar";
6405 NTSTATUS status;
6407 printf("starting chain1 test\n");
6408 if (!torture_open_connection(&cli1, 0)) {
6409 return False;
6412 cli_sockopt(cli1, sockops);
6414 reqs[0] = cli_open_create(talloc_tos(), evt, cli1, "\\test",
6415 O_CREAT|O_RDWR, 0, &smbreqs[0]);
6416 if (reqs[0] == NULL) return false;
6417 tevent_req_set_callback(reqs[0], chain1_open_completion, NULL);
6420 reqs[1] = cli_write_andx_create(talloc_tos(), evt, cli1, 0, 0,
6421 (const uint8_t *)str, 0, strlen(str)+1,
6422 smbreqs, 1, &smbreqs[1]);
6423 if (reqs[1] == NULL) return false;
6424 tevent_req_set_callback(reqs[1], chain1_write_completion, NULL);
6426 reqs[2] = cli_close_create(talloc_tos(), evt, cli1, 0, &smbreqs[2]);
6427 if (reqs[2] == NULL) return false;
6428 tevent_req_set_callback(reqs[2], chain1_close_completion, &done);
6430 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
6431 if (!NT_STATUS_IS_OK(status)) {
6432 return false;
6435 while (!done) {
6436 event_loop_once(evt);
6439 torture_close_connection(cli1);
6440 return True;
6443 static void chain2_sesssetup_completion(struct tevent_req *req)
6445 NTSTATUS status;
6446 status = cli_session_setup_guest_recv(req);
6447 d_printf("sesssetup returned %s\n", nt_errstr(status));
6450 static void chain2_tcon_completion(struct tevent_req *req)
6452 bool *done = (bool *)tevent_req_callback_data_void(req);
6453 NTSTATUS status;
6454 status = cli_tcon_andx_recv(req);
6455 d_printf("tcon_and_x returned %s\n", nt_errstr(status));
6456 *done = true;
6459 static bool run_chain2(int dummy)
6461 struct cli_state *cli1;
6462 struct event_context *evt = event_context_init(NULL);
6463 struct tevent_req *reqs[2], *smbreqs[2];
6464 bool done = false;
6465 NTSTATUS status;
6467 printf("starting chain2 test\n");
6468 status = cli_start_connection(&cli1, lp_netbios_name(), host, NULL,
6469 port_to_use, Undefined, 0);
6470 if (!NT_STATUS_IS_OK(status)) {
6471 return False;
6474 cli_sockopt(cli1, sockops);
6476 reqs[0] = cli_session_setup_guest_create(talloc_tos(), evt, cli1,
6477 &smbreqs[0]);
6478 if (reqs[0] == NULL) return false;
6479 tevent_req_set_callback(reqs[0], chain2_sesssetup_completion, NULL);
6481 reqs[1] = cli_tcon_andx_create(talloc_tos(), evt, cli1, "IPC$",
6482 "?????", NULL, 0, &smbreqs[1]);
6483 if (reqs[1] == NULL) return false;
6484 tevent_req_set_callback(reqs[1], chain2_tcon_completion, &done);
6486 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
6487 if (!NT_STATUS_IS_OK(status)) {
6488 return false;
6491 while (!done) {
6492 event_loop_once(evt);
6495 torture_close_connection(cli1);
6496 return True;
6500 struct torture_createdel_state {
6501 struct tevent_context *ev;
6502 struct cli_state *cli;
6505 static void torture_createdel_created(struct tevent_req *subreq);
6506 static void torture_createdel_closed(struct tevent_req *subreq);
6508 static struct tevent_req *torture_createdel_send(TALLOC_CTX *mem_ctx,
6509 struct tevent_context *ev,
6510 struct cli_state *cli,
6511 const char *name)
6513 struct tevent_req *req, *subreq;
6514 struct torture_createdel_state *state;
6516 req = tevent_req_create(mem_ctx, &state,
6517 struct torture_createdel_state);
6518 if (req == NULL) {
6519 return NULL;
6521 state->ev = ev;
6522 state->cli = cli;
6524 subreq = cli_ntcreate_send(
6525 state, ev, cli, name, 0,
6526 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
6527 FILE_ATTRIBUTE_NORMAL,
6528 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
6529 FILE_OPEN_IF, FILE_DELETE_ON_CLOSE, 0);
6531 if (tevent_req_nomem(subreq, req)) {
6532 return tevent_req_post(req, ev);
6534 tevent_req_set_callback(subreq, torture_createdel_created, req);
6535 return req;
6538 static void torture_createdel_created(struct tevent_req *subreq)
6540 struct tevent_req *req = tevent_req_callback_data(
6541 subreq, struct tevent_req);
6542 struct torture_createdel_state *state = tevent_req_data(
6543 req, struct torture_createdel_state);
6544 NTSTATUS status;
6545 uint16_t fnum;
6547 status = cli_ntcreate_recv(subreq, &fnum);
6548 TALLOC_FREE(subreq);
6549 if (!NT_STATUS_IS_OK(status)) {
6550 DEBUG(10, ("cli_ntcreate_recv returned %s\n",
6551 nt_errstr(status)));
6552 tevent_req_nterror(req, status);
6553 return;
6556 subreq = cli_close_send(state, state->ev, state->cli, fnum);
6557 if (tevent_req_nomem(subreq, req)) {
6558 return;
6560 tevent_req_set_callback(subreq, torture_createdel_closed, req);
6563 static void torture_createdel_closed(struct tevent_req *subreq)
6565 struct tevent_req *req = tevent_req_callback_data(
6566 subreq, struct tevent_req);
6567 NTSTATUS status;
6569 status = cli_close_recv(subreq);
6570 if (!NT_STATUS_IS_OK(status)) {
6571 DEBUG(10, ("cli_close_recv returned %s\n", nt_errstr(status)));
6572 tevent_req_nterror(req, status);
6573 return;
6575 tevent_req_done(req);
6578 static NTSTATUS torture_createdel_recv(struct tevent_req *req)
6580 return tevent_req_simple_recv_ntstatus(req);
6583 struct torture_createdels_state {
6584 struct tevent_context *ev;
6585 struct cli_state *cli;
6586 const char *base_name;
6587 int sent;
6588 int received;
6589 int num_files;
6590 struct tevent_req **reqs;
6593 static void torture_createdels_done(struct tevent_req *subreq);
6595 static struct tevent_req *torture_createdels_send(TALLOC_CTX *mem_ctx,
6596 struct tevent_context *ev,
6597 struct cli_state *cli,
6598 const char *base_name,
6599 int num_parallel,
6600 int num_files)
6602 struct tevent_req *req;
6603 struct torture_createdels_state *state;
6604 int i;
6606 req = tevent_req_create(mem_ctx, &state,
6607 struct torture_createdels_state);
6608 if (req == NULL) {
6609 return NULL;
6611 state->ev = ev;
6612 state->cli = cli;
6613 state->base_name = talloc_strdup(state, base_name);
6614 if (tevent_req_nomem(state->base_name, req)) {
6615 return tevent_req_post(req, ev);
6617 state->num_files = MAX(num_parallel, num_files);
6618 state->sent = 0;
6619 state->received = 0;
6621 state->reqs = talloc_array(state, struct tevent_req *, num_parallel);
6622 if (tevent_req_nomem(state->reqs, req)) {
6623 return tevent_req_post(req, ev);
6626 for (i=0; i<num_parallel; i++) {
6627 char *name;
6629 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
6630 state->sent);
6631 if (tevent_req_nomem(name, req)) {
6632 return tevent_req_post(req, ev);
6634 state->reqs[i] = torture_createdel_send(
6635 state->reqs, state->ev, state->cli, name);
6636 if (tevent_req_nomem(state->reqs[i], req)) {
6637 return tevent_req_post(req, ev);
6639 name = talloc_move(state->reqs[i], &name);
6640 tevent_req_set_callback(state->reqs[i],
6641 torture_createdels_done, req);
6642 state->sent += 1;
6644 return req;
6647 static void torture_createdels_done(struct tevent_req *subreq)
6649 struct tevent_req *req = tevent_req_callback_data(
6650 subreq, struct tevent_req);
6651 struct torture_createdels_state *state = tevent_req_data(
6652 req, struct torture_createdels_state);
6653 size_t num_parallel = talloc_array_length(state->reqs);
6654 NTSTATUS status;
6655 char *name;
6656 int i;
6658 status = torture_createdel_recv(subreq);
6659 if (!NT_STATUS_IS_OK(status)){
6660 DEBUG(10, ("torture_createdel_recv returned %s\n",
6661 nt_errstr(status)));
6662 TALLOC_FREE(subreq);
6663 tevent_req_nterror(req, status);
6664 return;
6667 for (i=0; i<num_parallel; i++) {
6668 if (subreq == state->reqs[i]) {
6669 break;
6672 if (i == num_parallel) {
6673 DEBUG(10, ("received something we did not send\n"));
6674 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
6675 return;
6677 TALLOC_FREE(state->reqs[i]);
6679 if (state->sent >= state->num_files) {
6680 tevent_req_done(req);
6681 return;
6684 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
6685 state->sent);
6686 if (tevent_req_nomem(name, req)) {
6687 return;
6689 state->reqs[i] = torture_createdel_send(state->reqs, state->ev,
6690 state->cli, name);
6691 if (tevent_req_nomem(state->reqs[i], req)) {
6692 return;
6694 name = talloc_move(state->reqs[i], &name);
6695 tevent_req_set_callback(state->reqs[i], torture_createdels_done, req);
6696 state->sent += 1;
6699 static NTSTATUS torture_createdels_recv(struct tevent_req *req)
6701 return tevent_req_simple_recv_ntstatus(req);
6704 struct swallow_notify_state {
6705 struct tevent_context *ev;
6706 struct cli_state *cli;
6707 uint16_t fnum;
6708 uint32_t completion_filter;
6709 bool recursive;
6710 bool (*fn)(uint32_t action, const char *name, void *priv);
6711 void *priv;
6714 static void swallow_notify_done(struct tevent_req *subreq);
6716 static struct tevent_req *swallow_notify_send(TALLOC_CTX *mem_ctx,
6717 struct tevent_context *ev,
6718 struct cli_state *cli,
6719 uint16_t fnum,
6720 uint32_t completion_filter,
6721 bool recursive,
6722 bool (*fn)(uint32_t action,
6723 const char *name,
6724 void *priv),
6725 void *priv)
6727 struct tevent_req *req, *subreq;
6728 struct swallow_notify_state *state;
6730 req = tevent_req_create(mem_ctx, &state,
6731 struct swallow_notify_state);
6732 if (req == NULL) {
6733 return NULL;
6735 state->ev = ev;
6736 state->cli = cli;
6737 state->fnum = fnum;
6738 state->completion_filter = completion_filter;
6739 state->recursive = recursive;
6740 state->fn = fn;
6741 state->priv = priv;
6743 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
6744 0xffff, state->completion_filter,
6745 state->recursive);
6746 if (tevent_req_nomem(subreq, req)) {
6747 return tevent_req_post(req, ev);
6749 tevent_req_set_callback(subreq, swallow_notify_done, req);
6750 return req;
6753 static void swallow_notify_done(struct tevent_req *subreq)
6755 struct tevent_req *req = tevent_req_callback_data(
6756 subreq, struct tevent_req);
6757 struct swallow_notify_state *state = tevent_req_data(
6758 req, struct swallow_notify_state);
6759 NTSTATUS status;
6760 uint32_t i, num_changes;
6761 struct notify_change *changes;
6763 status = cli_notify_recv(subreq, state, &num_changes, &changes);
6764 TALLOC_FREE(subreq);
6765 if (!NT_STATUS_IS_OK(status)) {
6766 DEBUG(10, ("cli_notify_recv returned %s\n",
6767 nt_errstr(status)));
6768 tevent_req_nterror(req, status);
6769 return;
6772 for (i=0; i<num_changes; i++) {
6773 state->fn(changes[i].action, changes[i].name, state->priv);
6775 TALLOC_FREE(changes);
6777 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
6778 0xffff, state->completion_filter,
6779 state->recursive);
6780 if (tevent_req_nomem(subreq, req)) {
6781 return;
6783 tevent_req_set_callback(subreq, swallow_notify_done, req);
6786 static bool print_notifies(uint32_t action, const char *name, void *priv)
6788 if (DEBUGLEVEL > 5) {
6789 d_printf("%d %s\n", (int)action, name);
6791 return true;
6794 static void notify_bench_done(struct tevent_req *req)
6796 int *num_finished = (int *)tevent_req_callback_data_void(req);
6797 *num_finished += 1;
6800 static bool run_notify_bench(int dummy)
6802 const char *dname = "\\notify-bench";
6803 struct tevent_context *ev;
6804 NTSTATUS status;
6805 uint16_t dnum;
6806 struct tevent_req *req1;
6807 struct tevent_req *req2 = NULL;
6808 int i, num_unc_names;
6809 int num_finished = 0;
6811 printf("starting notify-bench test\n");
6813 if (use_multishare_conn) {
6814 char **unc_list;
6815 unc_list = file_lines_load(multishare_conn_fname,
6816 &num_unc_names, 0, NULL);
6817 if (!unc_list || num_unc_names <= 0) {
6818 d_printf("Failed to load unc names list from '%s'\n",
6819 multishare_conn_fname);
6820 return false;
6822 TALLOC_FREE(unc_list);
6823 } else {
6824 num_unc_names = 1;
6827 ev = tevent_context_init(talloc_tos());
6828 if (ev == NULL) {
6829 d_printf("tevent_context_init failed\n");
6830 return false;
6833 for (i=0; i<num_unc_names; i++) {
6834 struct cli_state *cli;
6835 char *base_fname;
6837 base_fname = talloc_asprintf(talloc_tos(), "%s\\file%3.3d.",
6838 dname, i);
6839 if (base_fname == NULL) {
6840 return false;
6843 if (!torture_open_connection(&cli, i)) {
6844 return false;
6847 status = cli_ntcreate(cli, dname, 0,
6848 MAXIMUM_ALLOWED_ACCESS,
6849 0, FILE_SHARE_READ|FILE_SHARE_WRITE|
6850 FILE_SHARE_DELETE,
6851 FILE_OPEN_IF, FILE_DIRECTORY_FILE, 0,
6852 &dnum);
6854 if (!NT_STATUS_IS_OK(status)) {
6855 d_printf("Could not create %s: %s\n", dname,
6856 nt_errstr(status));
6857 return false;
6860 req1 = swallow_notify_send(talloc_tos(), ev, cli, dnum,
6861 FILE_NOTIFY_CHANGE_FILE_NAME |
6862 FILE_NOTIFY_CHANGE_DIR_NAME |
6863 FILE_NOTIFY_CHANGE_ATTRIBUTES |
6864 FILE_NOTIFY_CHANGE_LAST_WRITE,
6865 false, print_notifies, NULL);
6866 if (req1 == NULL) {
6867 d_printf("Could not create notify request\n");
6868 return false;
6871 req2 = torture_createdels_send(talloc_tos(), ev, cli,
6872 base_fname, 10, torture_numops);
6873 if (req2 == NULL) {
6874 d_printf("Could not create createdels request\n");
6875 return false;
6877 TALLOC_FREE(base_fname);
6879 tevent_req_set_callback(req2, notify_bench_done,
6880 &num_finished);
6883 while (num_finished < num_unc_names) {
6884 int ret;
6885 ret = tevent_loop_once(ev);
6886 if (ret != 0) {
6887 d_printf("tevent_loop_once failed\n");
6888 return false;
6892 if (!tevent_req_poll(req2, ev)) {
6893 d_printf("tevent_req_poll failed\n");
6896 status = torture_createdels_recv(req2);
6897 d_printf("torture_createdels_recv returned %s\n", nt_errstr(status));
6899 return true;
6902 static bool run_mangle1(int dummy)
6904 struct cli_state *cli;
6905 const char *fname = "this_is_a_long_fname_to_be_mangled.txt";
6906 uint16_t fnum;
6907 fstring alt_name;
6908 NTSTATUS status;
6909 time_t change_time, access_time, write_time;
6910 SMB_OFF_T size;
6911 uint16_t mode;
6913 printf("starting mangle1 test\n");
6914 if (!torture_open_connection(&cli, 0)) {
6915 return False;
6918 cli_sockopt(cli, sockops);
6920 status = cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
6921 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
6922 0, 0, &fnum);
6923 if (!NT_STATUS_IS_OK(status)) {
6924 d_printf("open %s failed: %s\n", fname, nt_errstr(status));
6925 return false;
6927 cli_close(cli, fnum);
6929 status = cli_qpathinfo_alt_name(cli, fname, alt_name);
6930 if (!NT_STATUS_IS_OK(status)) {
6931 d_printf("cli_qpathinfo_alt_name failed: %s\n",
6932 nt_errstr(status));
6933 return false;
6935 d_printf("alt_name: %s\n", alt_name);
6937 status = cli_open(cli, alt_name, O_RDONLY, DENY_NONE, &fnum);
6938 if (!NT_STATUS_IS_OK(status)) {
6939 d_printf("cli_open(%s) failed: %s\n", alt_name,
6940 nt_errstr(status));
6941 return false;
6943 cli_close(cli, fnum);
6945 status = cli_qpathinfo1(cli, alt_name, &change_time, &access_time,
6946 &write_time, &size, &mode);
6947 if (!NT_STATUS_IS_OK(status)) {
6948 d_printf("cli_qpathinfo1(%s) failed: %s\n", alt_name,
6949 nt_errstr(status));
6950 return false;
6953 return true;
6956 static size_t null_source(uint8_t *buf, size_t n, void *priv)
6958 size_t *to_pull = (size_t *)priv;
6959 size_t thistime = *to_pull;
6961 thistime = MIN(thistime, n);
6962 if (thistime == 0) {
6963 return 0;
6966 memset(buf, 0, thistime);
6967 *to_pull -= thistime;
6968 return thistime;
6971 static bool run_windows_write(int dummy)
6973 struct cli_state *cli1;
6974 uint16_t fnum;
6975 int i;
6976 bool ret = false;
6977 const char *fname = "\\writetest.txt";
6978 struct timeval start_time;
6979 double seconds;
6980 double kbytes;
6981 NTSTATUS status;
6983 printf("starting windows_write test\n");
6984 if (!torture_open_connection(&cli1, 0)) {
6985 return False;
6988 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
6989 if (!NT_STATUS_IS_OK(status)) {
6990 printf("open failed (%s)\n", nt_errstr(status));
6991 return False;
6994 cli_sockopt(cli1, sockops);
6996 start_time = timeval_current();
6998 for (i=0; i<torture_numops; i++) {
6999 uint8_t c = 0;
7000 off_t start = i * torture_blocksize;
7001 size_t to_pull = torture_blocksize - 1;
7003 status = cli_writeall(cli1, fnum, 0, &c,
7004 start + torture_blocksize - 1, 1, NULL);
7005 if (!NT_STATUS_IS_OK(status)) {
7006 printf("cli_write failed: %s\n", nt_errstr(status));
7007 goto fail;
7010 status = cli_push(cli1, fnum, 0, i * torture_blocksize, torture_blocksize,
7011 null_source, &to_pull);
7012 if (!NT_STATUS_IS_OK(status)) {
7013 printf("cli_push returned: %s\n", nt_errstr(status));
7014 goto fail;
7018 seconds = timeval_elapsed(&start_time);
7019 kbytes = (double)torture_blocksize * torture_numops;
7020 kbytes /= 1024;
7022 printf("Wrote %d kbytes in %.2f seconds: %d kb/sec\n", (int)kbytes,
7023 (double)seconds, (int)(kbytes/seconds));
7025 ret = true;
7026 fail:
7027 cli_close(cli1, fnum);
7028 cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7029 torture_close_connection(cli1);
7030 return ret;
7033 static bool run_cli_echo(int dummy)
7035 struct cli_state *cli;
7036 NTSTATUS status;
7038 printf("starting cli_echo test\n");
7039 if (!torture_open_connection(&cli, 0)) {
7040 return false;
7042 cli_sockopt(cli, sockops);
7044 status = cli_echo(cli, 5, data_blob_const("hello", 5));
7046 d_printf("cli_echo returned %s\n", nt_errstr(status));
7048 torture_close_connection(cli);
7049 return NT_STATUS_IS_OK(status);
7052 static bool run_uid_regression_test(int dummy)
7054 static struct cli_state *cli;
7055 int16_t old_vuid;
7056 int16_t old_cnum;
7057 bool correct = True;
7058 NTSTATUS status;
7060 printf("starting uid regression test\n");
7062 if (!torture_open_connection(&cli, 0)) {
7063 return False;
7066 cli_sockopt(cli, sockops);
7068 /* Ok - now save then logoff our current user. */
7069 old_vuid = cli->vuid;
7071 status = cli_ulogoff(cli);
7072 if (!NT_STATUS_IS_OK(status)) {
7073 d_printf("(%s) cli_ulogoff failed: %s\n",
7074 __location__, nt_errstr(status));
7075 correct = false;
7076 goto out;
7079 cli->vuid = old_vuid;
7081 /* Try an operation. */
7082 status = cli_mkdir(cli, "\\uid_reg_test");
7083 if (NT_STATUS_IS_OK(status)) {
7084 d_printf("(%s) cli_mkdir succeeded\n",
7085 __location__);
7086 correct = false;
7087 goto out;
7088 } else {
7089 /* Should be bad uid. */
7090 if (!check_error(__LINE__, cli, ERRSRV, ERRbaduid,
7091 NT_STATUS_USER_SESSION_DELETED)) {
7092 correct = false;
7093 goto out;
7097 old_cnum = cli->cnum;
7099 /* Now try a SMBtdis with the invald vuid set to zero. */
7100 cli->vuid = 0;
7102 /* This should succeed. */
7103 status = cli_tdis(cli);
7105 if (NT_STATUS_IS_OK(status)) {
7106 d_printf("First tdis with invalid vuid should succeed.\n");
7107 } else {
7108 d_printf("First tdis failed (%s)\n", nt_errstr(status));
7109 correct = false;
7110 goto out;
7113 cli->vuid = old_vuid;
7114 cli->cnum = old_cnum;
7116 /* This should fail. */
7117 status = cli_tdis(cli);
7118 if (NT_STATUS_IS_OK(status)) {
7119 d_printf("Second tdis with invalid vuid should fail - succeeded instead !.\n");
7120 correct = false;
7121 goto out;
7122 } else {
7123 /* Should be bad tid. */
7124 if (!check_error(__LINE__, cli, ERRSRV, ERRinvnid,
7125 NT_STATUS_NETWORK_NAME_DELETED)) {
7126 correct = false;
7127 goto out;
7131 cli_rmdir(cli, "\\uid_reg_test");
7133 out:
7135 cli_shutdown(cli);
7136 return correct;
7140 static const char *illegal_chars = "*\\/?<>|\":";
7141 static char force_shortname_chars[] = " +,.[];=\177";
7143 static NTSTATUS shortname_del_fn(const char *mnt, struct file_info *finfo,
7144 const char *mask, void *state)
7146 struct cli_state *pcli = (struct cli_state *)state;
7147 fstring fname;
7148 NTSTATUS status = NT_STATUS_OK;
7150 slprintf(fname, sizeof(fname), "\\shortname\\%s", finfo->name);
7152 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
7153 return NT_STATUS_OK;
7155 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
7156 status = cli_rmdir(pcli, fname);
7157 if (!NT_STATUS_IS_OK(status)) {
7158 printf("del_fn: failed to rmdir %s\n,", fname );
7160 } else {
7161 status = cli_unlink(pcli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7162 if (!NT_STATUS_IS_OK(status)) {
7163 printf("del_fn: failed to unlink %s\n,", fname );
7166 return status;
7169 struct sn_state {
7170 int matched;
7171 int i;
7172 bool val;
7175 static NTSTATUS shortname_list_fn(const char *mnt, struct file_info *finfo,
7176 const char *name, void *state)
7178 struct sn_state *s = (struct sn_state *)state;
7179 int i = s->i;
7181 #if 0
7182 printf("shortname list: i = %d, name = |%s|, shortname = |%s|\n",
7183 i, finfo->name, finfo->short_name);
7184 #endif
7186 if (strchr(force_shortname_chars, i)) {
7187 if (!finfo->short_name) {
7188 /* Shortname not created when it should be. */
7189 d_printf("(%s) ERROR: Shortname was not created for file %s containing %d\n",
7190 __location__, finfo->name, i);
7191 s->val = true;
7193 } else if (finfo->short_name){
7194 /* Shortname created when it should not be. */
7195 d_printf("(%s) ERROR: Shortname %s was created for file %s\n",
7196 __location__, finfo->short_name, finfo->name);
7197 s->val = true;
7199 s->matched += 1;
7200 return NT_STATUS_OK;
7203 static bool run_shortname_test(int dummy)
7205 static struct cli_state *cli;
7206 bool correct = True;
7207 int i;
7208 struct sn_state s;
7209 char fname[20];
7210 NTSTATUS status;
7212 printf("starting shortname test\n");
7214 if (!torture_open_connection(&cli, 0)) {
7215 return False;
7218 cli_sockopt(cli, sockops);
7220 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
7221 cli_list(cli, "\\shortname\\*", FILE_ATTRIBUTE_DIRECTORY, shortname_del_fn, cli);
7222 cli_rmdir(cli, "\\shortname");
7224 status = cli_mkdir(cli, "\\shortname");
7225 if (!NT_STATUS_IS_OK(status)) {
7226 d_printf("(%s) cli_mkdir of \\shortname failed: %s\n",
7227 __location__, nt_errstr(status));
7228 correct = false;
7229 goto out;
7232 strlcpy(fname, "\\shortname\\", sizeof(fname));
7233 strlcat(fname, "test .txt", sizeof(fname));
7235 s.val = false;
7237 for (i = 32; i < 128; i++) {
7238 uint16_t fnum = (uint16_t)-1;
7240 s.i = i;
7242 if (strchr(illegal_chars, i)) {
7243 continue;
7245 fname[15] = i;
7247 status = cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
7248 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum);
7249 if (!NT_STATUS_IS_OK(status)) {
7250 d_printf("(%s) cli_nt_create of %s failed: %s\n",
7251 __location__, fname, nt_errstr(status));
7252 correct = false;
7253 goto out;
7255 cli_close(cli, fnum);
7257 s.matched = 0;
7258 status = cli_list(cli, "\\shortname\\test*.*", 0,
7259 shortname_list_fn, &s);
7260 if (s.matched != 1) {
7261 d_printf("(%s) failed to list %s: %s\n",
7262 __location__, fname, nt_errstr(status));
7263 correct = false;
7264 goto out;
7267 status = cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7268 if (!NT_STATUS_IS_OK(status)) {
7269 d_printf("(%s) failed to delete %s: %s\n",
7270 __location__, fname, nt_errstr(status));
7271 correct = false;
7272 goto out;
7275 if (s.val) {
7276 correct = false;
7277 goto out;
7281 out:
7283 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
7284 cli_list(cli, "\\shortname\\*", FILE_ATTRIBUTE_DIRECTORY, shortname_del_fn, cli);
7285 cli_rmdir(cli, "\\shortname");
7286 torture_close_connection(cli);
7287 return correct;
7290 static void pagedsearch_cb(struct tevent_req *req)
7292 int rc;
7293 struct tldap_message *msg;
7294 char *dn;
7296 rc = tldap_search_paged_recv(req, talloc_tos(), &msg);
7297 if (rc != TLDAP_SUCCESS) {
7298 d_printf("tldap_search_paged_recv failed: %s\n",
7299 tldap_err2string(rc));
7300 return;
7302 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
7303 TALLOC_FREE(msg);
7304 return;
7306 if (!tldap_entry_dn(msg, &dn)) {
7307 d_printf("tldap_entry_dn failed\n");
7308 return;
7310 d_printf("%s\n", dn);
7311 TALLOC_FREE(msg);
7314 static bool run_tldap(int dummy)
7316 struct tldap_context *ld;
7317 int fd, rc;
7318 NTSTATUS status;
7319 struct sockaddr_storage addr;
7320 struct tevent_context *ev;
7321 struct tevent_req *req;
7322 char *basedn;
7323 const char *filter;
7325 if (!resolve_name(host, &addr, 0, false)) {
7326 d_printf("could not find host %s\n", host);
7327 return false;
7329 status = open_socket_out(&addr, 389, 9999, &fd);
7330 if (!NT_STATUS_IS_OK(status)) {
7331 d_printf("open_socket_out failed: %s\n", nt_errstr(status));
7332 return false;
7335 ld = tldap_context_create(talloc_tos(), fd);
7336 if (ld == NULL) {
7337 close(fd);
7338 d_printf("tldap_context_create failed\n");
7339 return false;
7342 rc = tldap_fetch_rootdse(ld);
7343 if (rc != TLDAP_SUCCESS) {
7344 d_printf("tldap_fetch_rootdse failed: %s\n",
7345 tldap_errstr(talloc_tos(), ld, rc));
7346 return false;
7349 basedn = tldap_talloc_single_attribute(
7350 tldap_rootdse(ld), "defaultNamingContext", talloc_tos());
7351 if (basedn == NULL) {
7352 d_printf("no defaultNamingContext\n");
7353 return false;
7355 d_printf("defaultNamingContext: %s\n", basedn);
7357 ev = tevent_context_init(talloc_tos());
7358 if (ev == NULL) {
7359 d_printf("tevent_context_init failed\n");
7360 return false;
7363 req = tldap_search_paged_send(talloc_tos(), ev, ld, basedn,
7364 TLDAP_SCOPE_SUB, "(objectclass=*)",
7365 NULL, 0, 0,
7366 NULL, 0, NULL, 0, 0, 0, 0, 5);
7367 if (req == NULL) {
7368 d_printf("tldap_search_paged_send failed\n");
7369 return false;
7371 tevent_req_set_callback(req, pagedsearch_cb, NULL);
7373 tevent_req_poll(req, ev);
7375 TALLOC_FREE(req);
7377 /* test search filters against rootDSE */
7378 filter = "(&(|(name=samba)(nextRid<=10000000)(usnChanged>=10)(samba~=ambas)(!(name=s*m*a)))"
7379 "(|(name:=samba)(name:dn:2.5.13.5:=samba)(:dn:2.5.13.5:=samba)(!(name=*samba))))";
7381 rc = tldap_search(ld, "", TLDAP_SCOPE_BASE, filter,
7382 NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0,
7383 talloc_tos(), NULL, NULL);
7384 if (rc != TLDAP_SUCCESS) {
7385 d_printf("tldap_search with complex filter failed: %s\n",
7386 tldap_errstr(talloc_tos(), ld, rc));
7387 return false;
7390 TALLOC_FREE(ld);
7391 return true;
7394 /* Torture test to ensure no regression of :
7395 https://bugzilla.samba.org/show_bug.cgi?id=7084
7398 static bool run_dir_createtime(int dummy)
7400 struct cli_state *cli;
7401 const char *dname = "\\testdir";
7402 const char *fname = "\\testdir\\testfile";
7403 NTSTATUS status;
7404 struct timespec create_time;
7405 struct timespec create_time1;
7406 uint16_t fnum;
7407 bool ret = false;
7409 if (!torture_open_connection(&cli, 0)) {
7410 return false;
7413 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7414 cli_rmdir(cli, dname);
7416 status = cli_mkdir(cli, dname);
7417 if (!NT_STATUS_IS_OK(status)) {
7418 printf("mkdir failed: %s\n", nt_errstr(status));
7419 goto out;
7422 status = cli_qpathinfo2(cli, dname, &create_time, NULL, NULL, NULL,
7423 NULL, NULL, NULL);
7424 if (!NT_STATUS_IS_OK(status)) {
7425 printf("cli_qpathinfo2 returned %s\n",
7426 nt_errstr(status));
7427 goto out;
7430 /* Sleep 3 seconds, then create a file. */
7431 sleep(3);
7433 status = cli_open(cli, fname, O_RDWR | O_CREAT | O_EXCL,
7434 DENY_NONE, &fnum);
7435 if (!NT_STATUS_IS_OK(status)) {
7436 printf("cli_open failed: %s\n", nt_errstr(status));
7437 goto out;
7440 status = cli_qpathinfo2(cli, dname, &create_time1, NULL, NULL, NULL,
7441 NULL, NULL, NULL);
7442 if (!NT_STATUS_IS_OK(status)) {
7443 printf("cli_qpathinfo2 (2) returned %s\n",
7444 nt_errstr(status));
7445 goto out;
7448 if (timespec_compare(&create_time1, &create_time)) {
7449 printf("run_dir_createtime: create time was updated (error)\n");
7450 } else {
7451 printf("run_dir_createtime: create time was not updated (correct)\n");
7452 ret = true;
7455 out:
7457 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7458 cli_rmdir(cli, dname);
7459 if (!torture_close_connection(cli)) {
7460 ret = false;
7462 return ret;
7466 static bool run_streamerror(int dummy)
7468 struct cli_state *cli;
7469 const char *dname = "\\testdir";
7470 const char *streamname =
7471 "testdir:{4c8cc155-6c1e-11d1-8e41-00c04fb9386d}:$DATA";
7472 NTSTATUS status;
7473 time_t change_time, access_time, write_time;
7474 SMB_OFF_T size;
7475 uint16_t mode, fnum;
7476 bool ret = true;
7478 if (!torture_open_connection(&cli, 0)) {
7479 return false;
7482 cli_unlink(cli, "\\testdir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
7483 cli_rmdir(cli, dname);
7485 status = cli_mkdir(cli, dname);
7486 if (!NT_STATUS_IS_OK(status)) {
7487 printf("mkdir failed: %s\n", nt_errstr(status));
7488 return false;
7491 cli_qpathinfo1(cli, streamname, &change_time, &access_time, &write_time,
7492 &size, &mode);
7493 status = cli_nt_error(cli);
7495 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
7496 printf("pathinfo returned %s, expected "
7497 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
7498 nt_errstr(status));
7499 ret = false;
7502 status = cli_ntcreate(cli, streamname, 0x16,
7503 FILE_READ_DATA|FILE_READ_EA|
7504 FILE_READ_ATTRIBUTES|READ_CONTROL_ACCESS,
7505 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
7506 FILE_OPEN, 0, 0, &fnum);
7508 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
7509 printf("ntcreate returned %s, expected "
7510 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
7511 nt_errstr(status));
7512 ret = false;
7516 cli_rmdir(cli, dname);
7517 return ret;
7520 static bool run_local_substitute(int dummy)
7522 bool ok = true;
7524 ok &= subst_test("%U", "bla", "", -1, -1, "bla");
7525 ok &= subst_test("%u%U", "bla", "", -1, -1, "blabla");
7526 ok &= subst_test("%g", "", "", -1, -1, "NO_GROUP");
7527 ok &= subst_test("%G", "", "", -1, -1, "NO_GROUP");
7528 ok &= subst_test("%g", "", "", -1, 0, gidtoname(0));
7529 ok &= subst_test("%G", "", "", -1, 0, gidtoname(0));
7530 ok &= subst_test("%D%u", "u", "dom", -1, 0, "domu");
7531 ok &= subst_test("%i %I", "", "", -1, -1, "0.0.0.0 0.0.0.0");
7533 /* Different captialization rules in sub_basic... */
7535 ok &= (strcmp(talloc_sub_basic(talloc_tos(), "BLA", "dom", "%U%D"),
7536 "blaDOM") == 0);
7538 return ok;
7541 static bool run_local_base64(int dummy)
7543 int i;
7544 bool ret = true;
7546 for (i=1; i<2000; i++) {
7547 DATA_BLOB blob1, blob2;
7548 char *b64;
7550 blob1.data = talloc_array(talloc_tos(), uint8_t, i);
7551 blob1.length = i;
7552 generate_random_buffer(blob1.data, blob1.length);
7554 b64 = base64_encode_data_blob(talloc_tos(), blob1);
7555 if (b64 == NULL) {
7556 d_fprintf(stderr, "base64_encode_data_blob failed "
7557 "for %d bytes\n", i);
7558 ret = false;
7560 blob2 = base64_decode_data_blob(b64);
7561 TALLOC_FREE(b64);
7563 if (data_blob_cmp(&blob1, &blob2)) {
7564 d_fprintf(stderr, "data_blob_cmp failed for %d "
7565 "bytes\n", i);
7566 ret = false;
7568 TALLOC_FREE(blob1.data);
7569 data_blob_free(&blob2);
7571 return ret;
7574 static bool run_local_gencache(int dummy)
7576 char *val;
7577 time_t tm;
7578 DATA_BLOB blob;
7580 if (!gencache_set("foo", "bar", time(NULL) + 1000)) {
7581 d_printf("%s: gencache_set() failed\n", __location__);
7582 return False;
7585 if (!gencache_get("foo", NULL, NULL)) {
7586 d_printf("%s: gencache_get() failed\n", __location__);
7587 return False;
7590 if (!gencache_get("foo", &val, &tm)) {
7591 d_printf("%s: gencache_get() failed\n", __location__);
7592 return False;
7595 if (strcmp(val, "bar") != 0) {
7596 d_printf("%s: gencache_get() returned %s, expected %s\n",
7597 __location__, val, "bar");
7598 SAFE_FREE(val);
7599 return False;
7602 SAFE_FREE(val);
7604 if (!gencache_del("foo")) {
7605 d_printf("%s: gencache_del() failed\n", __location__);
7606 return False;
7608 if (gencache_del("foo")) {
7609 d_printf("%s: second gencache_del() succeeded\n",
7610 __location__);
7611 return False;
7614 if (gencache_get("foo", &val, &tm)) {
7615 d_printf("%s: gencache_get() on deleted entry "
7616 "succeeded\n", __location__);
7617 return False;
7620 blob = data_blob_string_const_null("bar");
7621 tm = time(NULL) + 60;
7623 if (!gencache_set_data_blob("foo", &blob, tm)) {
7624 d_printf("%s: gencache_set_data_blob() failed\n", __location__);
7625 return False;
7628 if (!gencache_get_data_blob("foo", &blob, NULL, NULL)) {
7629 d_printf("%s: gencache_get_data_blob() failed\n", __location__);
7630 return False;
7633 if (strcmp((const char *)blob.data, "bar") != 0) {
7634 d_printf("%s: gencache_get_data_blob() returned %s, expected %s\n",
7635 __location__, (const char *)blob.data, "bar");
7636 data_blob_free(&blob);
7637 return False;
7640 data_blob_free(&blob);
7642 if (!gencache_del("foo")) {
7643 d_printf("%s: gencache_del() failed\n", __location__);
7644 return False;
7646 if (gencache_del("foo")) {
7647 d_printf("%s: second gencache_del() succeeded\n",
7648 __location__);
7649 return False;
7652 if (gencache_get_data_blob("foo", &blob, NULL, NULL)) {
7653 d_printf("%s: gencache_get_data_blob() on deleted entry "
7654 "succeeded\n", __location__);
7655 return False;
7658 return True;
7661 static bool rbt_testval(struct db_context *db, const char *key,
7662 const char *value)
7664 struct db_record *rec;
7665 TDB_DATA data = string_tdb_data(value);
7666 bool ret = false;
7667 NTSTATUS status;
7669 rec = db->fetch_locked(db, db, string_tdb_data(key));
7670 if (rec == NULL) {
7671 d_fprintf(stderr, "fetch_locked failed\n");
7672 goto done;
7674 status = rec->store(rec, data, 0);
7675 if (!NT_STATUS_IS_OK(status)) {
7676 d_fprintf(stderr, "store failed: %s\n", nt_errstr(status));
7677 goto done;
7679 TALLOC_FREE(rec);
7681 rec = db->fetch_locked(db, db, string_tdb_data(key));
7682 if (rec == NULL) {
7683 d_fprintf(stderr, "second fetch_locked failed\n");
7684 goto done;
7686 if ((rec->value.dsize != data.dsize)
7687 || (memcmp(rec->value.dptr, data.dptr, data.dsize) != 0)) {
7688 d_fprintf(stderr, "Got wrong data back\n");
7689 goto done;
7692 ret = true;
7693 done:
7694 TALLOC_FREE(rec);
7695 return ret;
7698 static bool run_local_rbtree(int dummy)
7700 struct db_context *db;
7701 bool ret = false;
7702 int i;
7704 db = db_open_rbt(NULL);
7706 if (db == NULL) {
7707 d_fprintf(stderr, "db_open_rbt failed\n");
7708 return false;
7711 for (i=0; i<1000; i++) {
7712 char *key, *value;
7714 if (asprintf(&key, "key%ld", random()) == -1) {
7715 goto done;
7717 if (asprintf(&value, "value%ld", random()) == -1) {
7718 SAFE_FREE(key);
7719 goto done;
7722 if (!rbt_testval(db, key, value)) {
7723 SAFE_FREE(key);
7724 SAFE_FREE(value);
7725 goto done;
7728 SAFE_FREE(value);
7729 if (asprintf(&value, "value%ld", random()) == -1) {
7730 SAFE_FREE(key);
7731 goto done;
7734 if (!rbt_testval(db, key, value)) {
7735 SAFE_FREE(key);
7736 SAFE_FREE(value);
7737 goto done;
7740 SAFE_FREE(key);
7741 SAFE_FREE(value);
7744 ret = true;
7746 done:
7747 TALLOC_FREE(db);
7748 return ret;
7753 local test for character set functions
7755 This is a very simple test for the functionality in convert_string_error()
7757 static bool run_local_convert_string(int dummy)
7759 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
7760 const char *test_strings[2] = { "March", "M\303\244rz" };
7761 char dst[7];
7762 int i;
7764 for (i=0; i<2; i++) {
7765 const char *str = test_strings[i];
7766 int len = strlen(str);
7767 size_t converted_size;
7768 bool ret;
7770 memset(dst, 'X', sizeof(dst));
7772 /* first try with real source length */
7773 ret = convert_string_error(CH_UNIX, CH_UTF8,
7774 str, len,
7775 dst, sizeof(dst),
7776 &converted_size);
7777 if (ret != true) {
7778 d_fprintf(stderr, "Failed to convert '%s' to CH_DISPLAY\n", str);
7779 goto failed;
7782 if (converted_size != len) {
7783 d_fprintf(stderr, "Converted size of '%s' should be %d - got %d\n",
7784 str, len, (int)converted_size);
7785 goto failed;
7788 if (strncmp(str, dst, converted_size) != 0) {
7789 d_fprintf(stderr, "Expected '%s' to match '%s'\n", str, dst);
7790 goto failed;
7793 if (strlen(str) != converted_size) {
7794 d_fprintf(stderr, "Expected '%s' length %d - got %d\n", str,
7795 (int)strlen(str), (int)converted_size);
7796 goto failed;
7799 if (dst[converted_size] != 'X') {
7800 d_fprintf(stderr, "Expected no termination of '%s'\n", dst);
7801 goto failed;
7804 /* now with srclen==-1, this causes the nul to be
7805 * converted too */
7806 ret = convert_string_error(CH_UNIX, CH_UTF8,
7807 str, -1,
7808 dst, sizeof(dst),
7809 &converted_size);
7810 if (ret != true) {
7811 d_fprintf(stderr, "Failed to convert '%s' to CH_DISPLAY\n", str);
7812 goto failed;
7815 if (converted_size != len+1) {
7816 d_fprintf(stderr, "Converted size of '%s' should be %d - got %d\n",
7817 str, len, (int)converted_size);
7818 goto failed;
7821 if (strncmp(str, dst, converted_size) != 0) {
7822 d_fprintf(stderr, "Expected '%s' to match '%s'\n", str, dst);
7823 goto failed;
7826 if (len+1 != converted_size) {
7827 d_fprintf(stderr, "Expected '%s' length %d - got %d\n", str,
7828 len+1, (int)converted_size);
7829 goto failed;
7832 if (dst[converted_size] != 'X') {
7833 d_fprintf(stderr, "Expected no termination of '%s'\n", dst);
7834 goto failed;
7840 TALLOC_FREE(tmp_ctx);
7841 return true;
7842 failed:
7843 TALLOC_FREE(tmp_ctx);
7844 return false;
7848 struct talloc_dict_test {
7849 int content;
7852 static int talloc_dict_traverse_fn(DATA_BLOB key, void *data, void *priv)
7854 int *count = (int *)priv;
7855 *count += 1;
7856 return 0;
7859 static bool run_local_talloc_dict(int dummy)
7861 struct talloc_dict *dict;
7862 struct talloc_dict_test *t;
7863 int key, count;
7865 dict = talloc_dict_init(talloc_tos());
7866 if (dict == NULL) {
7867 return false;
7870 t = talloc(talloc_tos(), struct talloc_dict_test);
7871 if (t == NULL) {
7872 return false;
7875 key = 1;
7876 t->content = 1;
7877 if (!talloc_dict_set(dict, data_blob_const(&key, sizeof(key)), t)) {
7878 return false;
7881 count = 0;
7882 if (talloc_dict_traverse(dict, talloc_dict_traverse_fn, &count) != 0) {
7883 return false;
7886 if (count != 1) {
7887 return false;
7890 TALLOC_FREE(dict);
7892 return true;
7895 static bool run_local_string_to_sid(int dummy) {
7896 struct dom_sid sid;
7898 if (string_to_sid(&sid, "S--1-5-32-545")) {
7899 printf("allowing S--1-5-32-545\n");
7900 return false;
7902 if (string_to_sid(&sid, "S-1-5-32-+545")) {
7903 printf("allowing S-1-5-32-+545\n");
7904 return false;
7906 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")) {
7907 printf("allowing S-1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0\n");
7908 return false;
7910 if (string_to_sid(&sid, "S-1-5-32-545-abc")) {
7911 printf("allowing S-1-5-32-545-abc\n");
7912 return false;
7914 if (!string_to_sid(&sid, "S-1-5-32-545")) {
7915 printf("could not parse S-1-5-32-545\n");
7916 return false;
7918 if (!dom_sid_equal(&sid, &global_sid_Builtin_Users)) {
7919 printf("mis-parsed S-1-5-32-545 as %s\n",
7920 sid_string_tos(&sid));
7921 return false;
7923 return true;
7926 static bool run_local_binary_to_sid(int dummy) {
7927 struct dom_sid *sid = talloc(NULL, struct dom_sid);
7928 static const char good_binary_sid[] = {
7929 0x1, /* revision number */
7930 15, /* num auths */
7931 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7932 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7933 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7934 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7935 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7936 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7937 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7938 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7939 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7940 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7941 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7942 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7943 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7944 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7945 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7946 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7949 static const char long_binary_sid[] = {
7950 0x1, /* revision number */
7951 15, /* num auths */
7952 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7953 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7954 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7955 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7956 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7957 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7958 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7959 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7960 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7961 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7962 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7963 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7964 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7965 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7966 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7967 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7968 0x1, 0x1, 0x1, 0x1, /* auth[15] */
7969 0x1, 0x1, 0x1, 0x1, /* auth[16] */
7970 0x1, 0x1, 0x1, 0x1, /* auth[17] */
7973 static const char long_binary_sid2[] = {
7974 0x1, /* revision number */
7975 32, /* num auths */
7976 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, /* id_auth */
7977 0x1, 0x1, 0x1, 0x1, /* auth[0] */
7978 0x1, 0x1, 0x1, 0x1, /* auth[1] */
7979 0x1, 0x1, 0x1, 0x1, /* auth[2] */
7980 0x1, 0x1, 0x1, 0x1, /* auth[3] */
7981 0x1, 0x1, 0x1, 0x1, /* auth[4] */
7982 0x1, 0x1, 0x1, 0x1, /* auth[5] */
7983 0x1, 0x1, 0x1, 0x1, /* auth[6] */
7984 0x1, 0x1, 0x1, 0x1, /* auth[7] */
7985 0x1, 0x1, 0x1, 0x1, /* auth[8] */
7986 0x1, 0x1, 0x1, 0x1, /* auth[9] */
7987 0x1, 0x1, 0x1, 0x1, /* auth[10] */
7988 0x1, 0x1, 0x1, 0x1, /* auth[11] */
7989 0x1, 0x1, 0x1, 0x1, /* auth[12] */
7990 0x1, 0x1, 0x1, 0x1, /* auth[13] */
7991 0x1, 0x1, 0x1, 0x1, /* auth[14] */
7992 0x1, 0x1, 0x1, 0x1, /* auth[15] */
7993 0x1, 0x1, 0x1, 0x1, /* auth[16] */
7994 0x1, 0x1, 0x1, 0x1, /* auth[17] */
7995 0x1, 0x1, 0x1, 0x1, /* auth[18] */
7996 0x1, 0x1, 0x1, 0x1, /* auth[19] */
7997 0x1, 0x1, 0x1, 0x1, /* auth[20] */
7998 0x1, 0x1, 0x1, 0x1, /* auth[21] */
7999 0x1, 0x1, 0x1, 0x1, /* auth[22] */
8000 0x1, 0x1, 0x1, 0x1, /* auth[23] */
8001 0x1, 0x1, 0x1, 0x1, /* auth[24] */
8002 0x1, 0x1, 0x1, 0x1, /* auth[25] */
8003 0x1, 0x1, 0x1, 0x1, /* auth[26] */
8004 0x1, 0x1, 0x1, 0x1, /* auth[27] */
8005 0x1, 0x1, 0x1, 0x1, /* auth[28] */
8006 0x1, 0x1, 0x1, 0x1, /* auth[29] */
8007 0x1, 0x1, 0x1, 0x1, /* auth[30] */
8008 0x1, 0x1, 0x1, 0x1, /* auth[31] */
8011 if (!sid_parse(good_binary_sid, sizeof(good_binary_sid), sid)) {
8012 return false;
8014 if (sid_parse(long_binary_sid2, sizeof(long_binary_sid2), sid)) {
8015 return false;
8017 if (sid_parse(long_binary_sid, sizeof(long_binary_sid), sid)) {
8018 return false;
8020 return true;
8023 /* Split a path name into filename and stream name components. Canonicalise
8024 * such that an implicit $DATA token is always explicit.
8026 * The "specification" of this function can be found in the
8027 * run_local_stream_name() function in torture.c, I've tried those
8028 * combinations against a W2k3 server.
8031 static NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
8032 char **pbase, char **pstream)
8034 char *base = NULL;
8035 char *stream = NULL;
8036 char *sname; /* stream name */
8037 const char *stype; /* stream type */
8039 DEBUG(10, ("split_ntfs_stream_name called for [%s]\n", fname));
8041 sname = strchr_m(fname, ':');
8043 if (lp_posix_pathnames() || (sname == NULL)) {
8044 if (pbase != NULL) {
8045 base = talloc_strdup(mem_ctx, fname);
8046 NT_STATUS_HAVE_NO_MEMORY(base);
8048 goto done;
8051 if (pbase != NULL) {
8052 base = talloc_strndup(mem_ctx, fname, PTR_DIFF(sname, fname));
8053 NT_STATUS_HAVE_NO_MEMORY(base);
8056 sname += 1;
8058 stype = strchr_m(sname, ':');
8060 if (stype == NULL) {
8061 sname = talloc_strdup(mem_ctx, sname);
8062 stype = "$DATA";
8064 else {
8065 if (strcasecmp_m(stype, ":$DATA") != 0) {
8067 * If there is an explicit stream type, so far we only
8068 * allow $DATA. Is there anything else allowed? -- vl
8070 DEBUG(10, ("[%s] is an invalid stream type\n", stype));
8071 TALLOC_FREE(base);
8072 return NT_STATUS_OBJECT_NAME_INVALID;
8074 sname = talloc_strndup(mem_ctx, sname, PTR_DIFF(stype, sname));
8075 stype += 1;
8078 if (sname == NULL) {
8079 TALLOC_FREE(base);
8080 return NT_STATUS_NO_MEMORY;
8083 if (sname[0] == '\0') {
8085 * no stream name, so no stream
8087 goto done;
8090 if (pstream != NULL) {
8091 stream = talloc_asprintf(mem_ctx, "%s:%s", sname, stype);
8092 if (stream == NULL) {
8093 TALLOC_FREE(sname);
8094 TALLOC_FREE(base);
8095 return NT_STATUS_NO_MEMORY;
8098 * upper-case the type field
8100 strupper_m(strchr_m(stream, ':')+1);
8103 done:
8104 if (pbase != NULL) {
8105 *pbase = base;
8107 if (pstream != NULL) {
8108 *pstream = stream;
8110 return NT_STATUS_OK;
8113 static bool test_stream_name(const char *fname, const char *expected_base,
8114 const char *expected_stream,
8115 NTSTATUS expected_status)
8117 NTSTATUS status;
8118 char *base = NULL;
8119 char *stream = NULL;
8121 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &stream);
8122 if (!NT_STATUS_EQUAL(status, expected_status)) {
8123 goto error;
8126 if (!NT_STATUS_IS_OK(status)) {
8127 return true;
8130 if (base == NULL) goto error;
8132 if (strcmp(expected_base, base) != 0) goto error;
8134 if ((expected_stream != NULL) && (stream == NULL)) goto error;
8135 if ((expected_stream == NULL) && (stream != NULL)) goto error;
8137 if ((stream != NULL) && (strcmp(expected_stream, stream) != 0))
8138 goto error;
8140 TALLOC_FREE(base);
8141 TALLOC_FREE(stream);
8142 return true;
8144 error:
8145 d_fprintf(stderr, "Do test_stream(%s, %s, %s, %s)\n",
8146 fname, expected_base ? expected_base : "<NULL>",
8147 expected_stream ? expected_stream : "<NULL>",
8148 nt_errstr(expected_status));
8149 d_fprintf(stderr, "-> base=%s, stream=%s, status=%s\n",
8150 base ? base : "<NULL>", stream ? stream : "<NULL>",
8151 nt_errstr(status));
8152 TALLOC_FREE(base);
8153 TALLOC_FREE(stream);
8154 return false;
8157 static bool run_local_stream_name(int dummy)
8159 bool ret = true;
8161 ret &= test_stream_name(
8162 "bla", "bla", NULL, NT_STATUS_OK);
8163 ret &= test_stream_name(
8164 "bla::$DATA", "bla", NULL, NT_STATUS_OK);
8165 ret &= test_stream_name(
8166 "bla:blub:", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
8167 ret &= test_stream_name(
8168 "bla::", NULL, NULL, NT_STATUS_OBJECT_NAME_INVALID);
8169 ret &= test_stream_name(
8170 "bla::123", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
8171 ret &= test_stream_name(
8172 "bla:$DATA", "bla", "$DATA:$DATA", NT_STATUS_OK);
8173 ret &= test_stream_name(
8174 "bla:x:$DATA", "bla", "x:$DATA", NT_STATUS_OK);
8175 ret &= test_stream_name(
8176 "bla:x", "bla", "x:$DATA", NT_STATUS_OK);
8178 return ret;
8181 static bool data_blob_equal(DATA_BLOB a, DATA_BLOB b)
8183 if (a.length != b.length) {
8184 printf("a.length=%d != b.length=%d\n",
8185 (int)a.length, (int)b.length);
8186 return false;
8188 if (memcmp(a.data, b.data, a.length) != 0) {
8189 printf("a.data and b.data differ\n");
8190 return false;
8192 return true;
8195 static bool run_local_memcache(int dummy)
8197 struct memcache *cache;
8198 DATA_BLOB k1, k2;
8199 DATA_BLOB d1, d2, d3;
8200 DATA_BLOB v1, v2, v3;
8202 TALLOC_CTX *mem_ctx;
8203 char *str1, *str2;
8204 size_t size1, size2;
8205 bool ret = false;
8207 cache = memcache_init(NULL, 100);
8209 if (cache == NULL) {
8210 printf("memcache_init failed\n");
8211 return false;
8214 d1 = data_blob_const("d1", 2);
8215 d2 = data_blob_const("d2", 2);
8216 d3 = data_blob_const("d3", 2);
8218 k1 = data_blob_const("d1", 2);
8219 k2 = data_blob_const("d2", 2);
8221 memcache_add(cache, STAT_CACHE, k1, d1);
8222 memcache_add(cache, GETWD_CACHE, k2, d2);
8224 if (!memcache_lookup(cache, STAT_CACHE, k1, &v1)) {
8225 printf("could not find k1\n");
8226 return false;
8228 if (!data_blob_equal(d1, v1)) {
8229 return false;
8232 if (!memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
8233 printf("could not find k2\n");
8234 return false;
8236 if (!data_blob_equal(d2, v2)) {
8237 return false;
8240 memcache_add(cache, STAT_CACHE, k1, d3);
8242 if (!memcache_lookup(cache, STAT_CACHE, k1, &v3)) {
8243 printf("could not find replaced k1\n");
8244 return false;
8246 if (!data_blob_equal(d3, v3)) {
8247 return false;
8250 memcache_add(cache, GETWD_CACHE, k1, d1);
8252 if (memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
8253 printf("Did find k2, should have been purged\n");
8254 return false;
8257 TALLOC_FREE(cache);
8259 cache = memcache_init(NULL, 0);
8261 mem_ctx = talloc_init("foo");
8263 str1 = talloc_strdup(mem_ctx, "string1");
8264 str2 = talloc_strdup(mem_ctx, "string2");
8266 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
8267 data_blob_string_const("torture"), &str1);
8268 size1 = talloc_total_size(cache);
8270 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
8271 data_blob_string_const("torture"), &str2);
8272 size2 = talloc_total_size(cache);
8274 printf("size1=%d, size2=%d\n", (int)size1, (int)size2);
8276 if (size2 > size1) {
8277 printf("memcache leaks memory!\n");
8278 goto fail;
8281 ret = true;
8282 fail:
8283 TALLOC_FREE(cache);
8284 return ret;
8287 static void wbclient_done(struct tevent_req *req)
8289 wbcErr wbc_err;
8290 struct winbindd_response *wb_resp;
8291 int *i = (int *)tevent_req_callback_data_void(req);
8293 wbc_err = wb_trans_recv(req, req, &wb_resp);
8294 TALLOC_FREE(req);
8295 *i += 1;
8296 d_printf("wb_trans_recv %d returned %s\n", *i, wbcErrorString(wbc_err));
8299 static bool run_local_wbclient(int dummy)
8301 struct event_context *ev;
8302 struct wb_context **wb_ctx;
8303 struct winbindd_request wb_req;
8304 bool result = false;
8305 int i, j;
8307 BlockSignals(True, SIGPIPE);
8309 ev = tevent_context_init_byname(talloc_tos(), "epoll");
8310 if (ev == NULL) {
8311 goto fail;
8314 wb_ctx = talloc_array(ev, struct wb_context *, nprocs);
8315 if (wb_ctx == NULL) {
8316 goto fail;
8319 ZERO_STRUCT(wb_req);
8320 wb_req.cmd = WINBINDD_PING;
8322 d_printf("nprocs=%d, numops=%d\n", (int)nprocs, (int)torture_numops);
8324 for (i=0; i<nprocs; i++) {
8325 wb_ctx[i] = wb_context_init(ev, NULL);
8326 if (wb_ctx[i] == NULL) {
8327 goto fail;
8329 for (j=0; j<torture_numops; j++) {
8330 struct tevent_req *req;
8331 req = wb_trans_send(ev, ev, wb_ctx[i],
8332 (j % 2) == 0, &wb_req);
8333 if (req == NULL) {
8334 goto fail;
8336 tevent_req_set_callback(req, wbclient_done, &i);
8340 i = 0;
8342 while (i < nprocs * torture_numops) {
8343 event_loop_once(ev);
8346 result = true;
8347 fail:
8348 TALLOC_FREE(ev);
8349 return result;
8352 static void getaddrinfo_finished(struct tevent_req *req)
8354 char *name = (char *)tevent_req_callback_data_void(req);
8355 struct addrinfo *ainfo;
8356 int res;
8358 res = getaddrinfo_recv(req, &ainfo);
8359 if (res != 0) {
8360 d_printf("gai(%s) returned %s\n", name, gai_strerror(res));
8361 return;
8363 d_printf("gai(%s) succeeded\n", name);
8364 freeaddrinfo(ainfo);
8367 static bool run_getaddrinfo_send(int dummy)
8369 TALLOC_CTX *frame = talloc_stackframe();
8370 struct fncall_context *ctx;
8371 struct tevent_context *ev;
8372 bool result = false;
8373 const char *names[4] = { "www.samba.org", "notfound.samba.org",
8374 "www.slashdot.org", "heise.de" };
8375 struct tevent_req *reqs[4];
8376 int i;
8378 ev = event_context_init(frame);
8379 if (ev == NULL) {
8380 goto fail;
8383 ctx = fncall_context_init(frame, 4);
8385 for (i=0; i<ARRAY_SIZE(names); i++) {
8386 reqs[i] = getaddrinfo_send(frame, ev, ctx, names[i], NULL,
8387 NULL);
8388 if (reqs[i] == NULL) {
8389 goto fail;
8391 tevent_req_set_callback(reqs[i], getaddrinfo_finished,
8392 discard_const_p(void, names[i]));
8395 for (i=0; i<ARRAY_SIZE(reqs); i++) {
8396 tevent_loop_once(ev);
8399 result = true;
8400 fail:
8401 TALLOC_FREE(frame);
8402 return result;
8405 static bool dbtrans_inc(struct db_context *db)
8407 struct db_record *rec;
8408 uint32_t *val;
8409 bool ret = false;
8410 NTSTATUS status;
8412 rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
8413 if (rec == NULL) {
8414 printf(__location__ "fetch_lock failed\n");
8415 return false;
8418 if (rec->value.dsize != sizeof(uint32_t)) {
8419 printf(__location__ "value.dsize = %d\n",
8420 (int)rec->value.dsize);
8421 goto fail;
8424 val = (uint32_t *)rec->value.dptr;
8425 *val += 1;
8427 status = rec->store(rec, make_tdb_data((uint8_t *)val,
8428 sizeof(uint32_t)),
8430 if (!NT_STATUS_IS_OK(status)) {
8431 printf(__location__ "store failed: %s\n",
8432 nt_errstr(status));
8433 goto fail;
8436 ret = true;
8437 fail:
8438 TALLOC_FREE(rec);
8439 return ret;
8442 static bool run_local_dbtrans(int dummy)
8444 struct db_context *db;
8445 struct db_record *rec;
8446 NTSTATUS status;
8447 uint32_t initial;
8448 int res;
8450 db = db_open(talloc_tos(), "transtest.tdb", 0, TDB_DEFAULT,
8451 O_RDWR|O_CREAT, 0600);
8452 if (db == NULL) {
8453 printf("Could not open transtest.db\n");
8454 return false;
8457 res = db->transaction_start(db);
8458 if (res != 0) {
8459 printf(__location__ "transaction_start failed\n");
8460 return false;
8463 rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
8464 if (rec == NULL) {
8465 printf(__location__ "fetch_lock failed\n");
8466 return false;
8469 if (rec->value.dptr == NULL) {
8470 initial = 0;
8471 status = rec->store(
8472 rec, make_tdb_data((uint8_t *)&initial,
8473 sizeof(initial)),
8475 if (!NT_STATUS_IS_OK(status)) {
8476 printf(__location__ "store returned %s\n",
8477 nt_errstr(status));
8478 return false;
8482 TALLOC_FREE(rec);
8484 res = db->transaction_commit(db);
8485 if (res != 0) {
8486 printf(__location__ "transaction_commit failed\n");
8487 return false;
8490 while (true) {
8491 uint32_t val, val2;
8492 int i;
8494 res = db->transaction_start(db);
8495 if (res != 0) {
8496 printf(__location__ "transaction_start failed\n");
8497 break;
8500 if (!dbwrap_fetch_uint32(db, "transtest", &val)) {
8501 printf(__location__ "dbwrap_fetch_uint32 failed\n");
8502 break;
8505 for (i=0; i<10; i++) {
8506 if (!dbtrans_inc(db)) {
8507 return false;
8511 if (!dbwrap_fetch_uint32(db, "transtest", &val2)) {
8512 printf(__location__ "dbwrap_fetch_uint32 failed\n");
8513 break;
8516 if (val2 != val + 10) {
8517 printf(__location__ "val=%d, val2=%d\n",
8518 (int)val, (int)val2);
8519 break;
8522 printf("val2=%d\r", val2);
8524 res = db->transaction_commit(db);
8525 if (res != 0) {
8526 printf(__location__ "transaction_commit failed\n");
8527 break;
8531 TALLOC_FREE(db);
8532 return true;
8536 * Just a dummy test to be run under a debugger. There's no real way
8537 * to inspect the tevent_select specific function from outside of
8538 * tevent_select.c.
8541 static bool run_local_tevent_select(int dummy)
8543 struct tevent_context *ev;
8544 struct tevent_fd *fd1, *fd2;
8545 bool result = false;
8547 ev = tevent_context_init_byname(NULL, "select");
8548 if (ev == NULL) {
8549 d_fprintf(stderr, "tevent_context_init_byname failed\n");
8550 goto fail;
8553 fd1 = tevent_add_fd(ev, ev, 2, 0, NULL, NULL);
8554 if (fd1 == NULL) {
8555 d_fprintf(stderr, "tevent_add_fd failed\n");
8556 goto fail;
8558 fd2 = tevent_add_fd(ev, ev, 3, 0, NULL, NULL);
8559 if (fd2 == NULL) {
8560 d_fprintf(stderr, "tevent_add_fd failed\n");
8561 goto fail;
8563 TALLOC_FREE(fd2);
8565 fd2 = tevent_add_fd(ev, ev, 1, 0, NULL, NULL);
8566 if (fd2 == NULL) {
8567 d_fprintf(stderr, "tevent_add_fd failed\n");
8568 goto fail;
8571 result = true;
8572 fail:
8573 TALLOC_FREE(ev);
8574 return result;
8577 static double create_procs(bool (*fn)(int), bool *result)
8579 int i, status;
8580 volatile pid_t *child_status;
8581 volatile bool *child_status_out;
8582 int synccount;
8583 int tries = 8;
8584 struct timeval start;
8586 synccount = 0;
8588 child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*nprocs);
8589 if (!child_status) {
8590 printf("Failed to setup shared memory\n");
8591 return -1;
8594 child_status_out = (volatile bool *)shm_setup(sizeof(bool)*nprocs);
8595 if (!child_status_out) {
8596 printf("Failed to setup result status shared memory\n");
8597 return -1;
8600 for (i = 0; i < nprocs; i++) {
8601 child_status[i] = 0;
8602 child_status_out[i] = True;
8605 start = timeval_current();
8607 for (i=0;i<nprocs;i++) {
8608 procnum = i;
8609 if (fork() == 0) {
8610 pid_t mypid = getpid();
8611 sys_srandom(((int)mypid) ^ ((int)time(NULL)));
8613 slprintf(myname,sizeof(myname),"CLIENT%d", i);
8615 while (1) {
8616 if (torture_open_connection(&current_cli, i)) break;
8617 if (tries-- == 0) {
8618 printf("pid %d failed to start\n", (int)getpid());
8619 _exit(1);
8621 smb_msleep(10);
8624 child_status[i] = getpid();
8626 while (child_status[i] && timeval_elapsed(&start) < 5) smb_msleep(2);
8628 child_status_out[i] = fn(i);
8629 _exit(0);
8633 do {
8634 synccount = 0;
8635 for (i=0;i<nprocs;i++) {
8636 if (child_status[i]) synccount++;
8638 if (synccount == nprocs) break;
8639 smb_msleep(10);
8640 } while (timeval_elapsed(&start) < 30);
8642 if (synccount != nprocs) {
8643 printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount);
8644 *result = False;
8645 return timeval_elapsed(&start);
8648 /* start the client load */
8649 start = timeval_current();
8651 for (i=0;i<nprocs;i++) {
8652 child_status[i] = 0;
8655 printf("%d clients started\n", nprocs);
8657 for (i=0;i<nprocs;i++) {
8658 while (waitpid(0, &status, 0) == -1 && errno == EINTR) /* noop */ ;
8661 printf("\n");
8663 for (i=0;i<nprocs;i++) {
8664 if (!child_status_out[i]) {
8665 *result = False;
8668 return timeval_elapsed(&start);
8671 #define FLAG_MULTIPROC 1
8673 static struct {
8674 const char *name;
8675 bool (*fn)(int);
8676 unsigned flags;
8677 } torture_ops[] = {
8678 {"FDPASS", run_fdpasstest, 0},
8679 {"LOCK1", run_locktest1, 0},
8680 {"LOCK2", run_locktest2, 0},
8681 {"LOCK3", run_locktest3, 0},
8682 {"LOCK4", run_locktest4, 0},
8683 {"LOCK5", run_locktest5, 0},
8684 {"LOCK6", run_locktest6, 0},
8685 {"LOCK7", run_locktest7, 0},
8686 {"LOCK8", run_locktest8, 0},
8687 {"LOCK9", run_locktest9, 0},
8688 {"UNLINK", run_unlinktest, 0},
8689 {"BROWSE", run_browsetest, 0},
8690 {"ATTR", run_attrtest, 0},
8691 {"TRANS2", run_trans2test, 0},
8692 {"MAXFID", run_maxfidtest, FLAG_MULTIPROC},
8693 {"TORTURE",run_torture, FLAG_MULTIPROC},
8694 {"RANDOMIPC", run_randomipc, 0},
8695 {"NEGNOWAIT", run_negprot_nowait, 0},
8696 {"NBENCH", run_nbench, 0},
8697 {"NBENCH2", run_nbench2, 0},
8698 {"OPLOCK1", run_oplock1, 0},
8699 {"OPLOCK2", run_oplock2, 0},
8700 {"OPLOCK4", run_oplock4, 0},
8701 {"DIR", run_dirtest, 0},
8702 {"DIR1", run_dirtest1, 0},
8703 {"DIR-CREATETIME", run_dir_createtime, 0},
8704 {"DENY1", torture_denytest1, 0},
8705 {"DENY2", torture_denytest2, 0},
8706 {"TCON", run_tcon_test, 0},
8707 {"TCONDEV", run_tcon_devtype_test, 0},
8708 {"RW1", run_readwritetest, 0},
8709 {"RW2", run_readwritemulti, FLAG_MULTIPROC},
8710 {"RW3", run_readwritelarge, 0},
8711 {"RW-SIGNING", run_readwritelarge_signtest, 0},
8712 {"OPEN", run_opentest, 0},
8713 {"POSIX", run_simple_posix_open_test, 0},
8714 {"POSIX-APPEND", run_posix_append, 0},
8715 {"CASE-INSENSITIVE-CREATE", run_case_insensitive_create, 0},
8716 {"ASYNC-ECHO", run_async_echo, 0},
8717 { "UID-REGRESSION-TEST", run_uid_regression_test, 0},
8718 { "SHORTNAME-TEST", run_shortname_test, 0},
8719 { "ADDRCHANGE", run_addrchange, 0},
8720 #if 1
8721 {"OPENATTR", run_openattrtest, 0},
8722 #endif
8723 {"XCOPY", run_xcopy, 0},
8724 {"RENAME", run_rename, 0},
8725 {"DELETE", run_deletetest, 0},
8726 {"DELETE-LN", run_deletetest_ln, 0},
8727 {"PROPERTIES", run_properties, 0},
8728 {"MANGLE", torture_mangle, 0},
8729 {"MANGLE1", run_mangle1, 0},
8730 {"W2K", run_w2ktest, 0},
8731 {"TRANS2SCAN", torture_trans2_scan, 0},
8732 {"NTTRANSSCAN", torture_nttrans_scan, 0},
8733 {"UTABLE", torture_utable, 0},
8734 {"CASETABLE", torture_casetable, 0},
8735 {"ERRMAPEXTRACT", run_error_map_extract, 0},
8736 {"PIPE_NUMBER", run_pipe_number, 0},
8737 {"TCON2", run_tcon2_test, 0},
8738 {"IOCTL", torture_ioctl_test, 0},
8739 {"CHKPATH", torture_chkpath_test, 0},
8740 {"FDSESS", run_fdsesstest, 0},
8741 { "EATEST", run_eatest, 0},
8742 { "SESSSETUP_BENCH", run_sesssetup_bench, 0},
8743 { "CHAIN1", run_chain1, 0},
8744 { "CHAIN2", run_chain2, 0},
8745 { "WINDOWS-WRITE", run_windows_write, 0},
8746 { "NTTRANS-CREATE", run_nttrans_create, 0},
8747 { "CLI_ECHO", run_cli_echo, 0},
8748 { "GETADDRINFO", run_getaddrinfo_send, 0},
8749 { "TLDAP", run_tldap },
8750 { "STREAMERROR", run_streamerror },
8751 { "NOTIFY-BENCH", run_notify_bench },
8752 { "BAD-NBT-SESSION", run_bad_nbt_session },
8753 { "SMB-ANY-CONNECT", run_smb_any_connect },
8754 { "NOTIFY-ONLINE", run_notify_online },
8755 { "SMB2-BASIC", run_smb2_basic },
8756 { "LOCAL-SUBSTITUTE", run_local_substitute, 0},
8757 { "LOCAL-GENCACHE", run_local_gencache, 0},
8758 { "LOCAL-TALLOC-DICT", run_local_talloc_dict, 0},
8759 { "LOCAL-BASE64", run_local_base64, 0},
8760 { "LOCAL-RBTREE", run_local_rbtree, 0},
8761 { "LOCAL-MEMCACHE", run_local_memcache, 0},
8762 { "LOCAL-STREAM-NAME", run_local_stream_name, 0},
8763 { "LOCAL-WBCLIENT", run_local_wbclient, 0},
8764 { "LOCAL-string_to_sid", run_local_string_to_sid, 0},
8765 { "LOCAL-binary_to_sid", run_local_binary_to_sid, 0},
8766 { "LOCAL-DBTRANS", run_local_dbtrans, 0},
8767 { "LOCAL-TEVENT-SELECT", run_local_tevent_select, 0},
8768 { "LOCAL-CONVERT-STRING", run_local_convert_string, 0},
8769 {NULL, NULL, 0}};
8773 /****************************************************************************
8774 run a specified test or "ALL"
8775 ****************************************************************************/
8776 static bool run_test(const char *name)
8778 bool ret = True;
8779 bool result = True;
8780 bool found = False;
8781 int i;
8782 double t;
8783 if (strequal(name,"ALL")) {
8784 for (i=0;torture_ops[i].name;i++) {
8785 run_test(torture_ops[i].name);
8787 found = True;
8790 for (i=0;torture_ops[i].name;i++) {
8791 fstr_sprintf(randomfname, "\\XX%x",
8792 (unsigned)random());
8794 if (strequal(name, torture_ops[i].name)) {
8795 found = True;
8796 printf("Running %s\n", name);
8797 if (torture_ops[i].flags & FLAG_MULTIPROC) {
8798 t = create_procs(torture_ops[i].fn, &result);
8799 if (!result) {
8800 ret = False;
8801 printf("TEST %s FAILED!\n", name);
8803 } else {
8804 struct timeval start;
8805 start = timeval_current();
8806 if (!torture_ops[i].fn(0)) {
8807 ret = False;
8808 printf("TEST %s FAILED!\n", name);
8810 t = timeval_elapsed(&start);
8812 printf("%s took %g secs\n\n", name, t);
8816 if (!found) {
8817 printf("Did not find a test named %s\n", name);
8818 ret = False;
8821 return ret;
8825 static void usage(void)
8827 int i;
8829 printf("WARNING samba4 test suite is much more complete nowadays.\n");
8830 printf("Please use samba4 torture.\n\n");
8832 printf("Usage: smbtorture //server/share <options> TEST1 TEST2 ...\n");
8834 printf("\t-d debuglevel\n");
8835 printf("\t-U user%%pass\n");
8836 printf("\t-k use kerberos\n");
8837 printf("\t-N numprocs\n");
8838 printf("\t-n my_netbios_name\n");
8839 printf("\t-W workgroup\n");
8840 printf("\t-o num_operations\n");
8841 printf("\t-O socket_options\n");
8842 printf("\t-m maximum protocol\n");
8843 printf("\t-L use oplocks\n");
8844 printf("\t-c CLIENT.TXT specify client load file for NBENCH\n");
8845 printf("\t-A showall\n");
8846 printf("\t-p port\n");
8847 printf("\t-s seed\n");
8848 printf("\t-b unclist_filename specify multiple shares for multiple connections\n");
8849 printf("\t-f filename filename to test\n");
8850 printf("\n\n");
8852 printf("tests are:");
8853 for (i=0;torture_ops[i].name;i++) {
8854 printf(" %s", torture_ops[i].name);
8856 printf("\n");
8858 printf("default test is ALL\n");
8860 exit(1);
8863 /****************************************************************************
8864 main program
8865 ****************************************************************************/
8866 int main(int argc,char *argv[])
8868 int opt, i;
8869 char *p;
8870 int gotuser = 0;
8871 int gotpass = 0;
8872 bool correct = True;
8873 TALLOC_CTX *frame = talloc_stackframe();
8874 int seed = time(NULL);
8876 #ifdef HAVE_SETBUFFER
8877 setbuffer(stdout, NULL, 0);
8878 #endif
8880 setup_logging("smbtorture", DEBUG_STDOUT);
8882 load_case_tables();
8884 if (is_default_dyn_CONFIGFILE()) {
8885 if(getenv("SMB_CONF_PATH")) {
8886 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
8889 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
8890 load_interfaces();
8892 if (argc < 2) {
8893 usage();
8896 for(p = argv[1]; *p; p++)
8897 if(*p == '\\')
8898 *p = '/';
8900 if (strncmp(argv[1], "//", 2)) {
8901 usage();
8904 fstrcpy(host, &argv[1][2]);
8905 p = strchr_m(&host[2],'/');
8906 if (!p) {
8907 usage();
8909 *p = 0;
8910 fstrcpy(share, p+1);
8912 fstrcpy(myname, get_myname(talloc_tos()));
8913 if (!*myname) {
8914 fprintf(stderr, "Failed to get my hostname.\n");
8915 return 1;
8918 if (*username == 0 && getenv("LOGNAME")) {
8919 fstrcpy(username,getenv("LOGNAME"));
8922 argc--;
8923 argv++;
8925 fstrcpy(workgroup, lp_workgroup());
8927 while ((opt = getopt(argc, argv, "p:hW:U:n:N:O:o:m:Ll:d:Aec:ks:b:B:f:"))
8928 != EOF) {
8929 switch (opt) {
8930 case 'p':
8931 port_to_use = atoi(optarg);
8932 break;
8933 case 's':
8934 seed = atoi(optarg);
8935 break;
8936 case 'W':
8937 fstrcpy(workgroup,optarg);
8938 break;
8939 case 'm':
8940 max_protocol = interpret_protocol(optarg, max_protocol);
8941 break;
8942 case 'N':
8943 nprocs = atoi(optarg);
8944 break;
8945 case 'o':
8946 torture_numops = atoi(optarg);
8947 break;
8948 case 'd':
8949 lp_set_cmdline("log level", optarg);
8950 break;
8951 case 'O':
8952 sockops = optarg;
8953 break;
8954 case 'L':
8955 use_oplocks = True;
8956 break;
8957 case 'l':
8958 local_path = optarg;
8959 break;
8960 case 'A':
8961 torture_showall = True;
8962 break;
8963 case 'n':
8964 fstrcpy(myname, optarg);
8965 break;
8966 case 'c':
8967 client_txt = optarg;
8968 break;
8969 case 'e':
8970 do_encrypt = true;
8971 break;
8972 case 'k':
8973 #ifdef HAVE_KRB5
8974 use_kerberos = True;
8975 #else
8976 d_printf("No kerberos support compiled in\n");
8977 exit(1);
8978 #endif
8979 break;
8980 case 'U':
8981 gotuser = 1;
8982 fstrcpy(username,optarg);
8983 p = strchr_m(username,'%');
8984 if (p) {
8985 *p = 0;
8986 fstrcpy(password, p+1);
8987 gotpass = 1;
8989 break;
8990 case 'b':
8991 fstrcpy(multishare_conn_fname, optarg);
8992 use_multishare_conn = True;
8993 break;
8994 case 'B':
8995 torture_blocksize = atoi(optarg);
8996 break;
8997 case 'f':
8998 test_filename = SMB_STRDUP(optarg);
8999 break;
9000 default:
9001 printf("Unknown option %c (%d)\n", (char)opt, opt);
9002 usage();
9006 d_printf("using seed %d\n", seed);
9008 srandom(seed);
9010 if(use_kerberos && !gotuser) gotpass = True;
9012 while (!gotpass) {
9013 p = getpass("Password:");
9014 if (p) {
9015 fstrcpy(password, p);
9016 gotpass = 1;
9020 printf("host=%s share=%s user=%s myname=%s\n",
9021 host, share, username, myname);
9023 if (argc == optind) {
9024 correct = run_test("ALL");
9025 } else {
9026 for (i=optind;i<argc;i++) {
9027 if (!run_test(argv[i])) {
9028 correct = False;
9033 TALLOC_FREE(frame);
9035 if (correct) {
9036 return(0);
9037 } else {
9038 return(1);