Fix comments on new test.
[Samba/fernandojvsilva.git] / source3 / torture / torture.c
blobca499a9fcc8326f141a2e7bc2ee7491ceb30a99e
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
4 Copyright (C) Andrew Tridgell 1997-1998
5 Copyright (C) Jeremy Allison 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "nsswitch/libwbclient/wbc_async.h"
24 extern char *optarg;
25 extern int optind;
27 static fstring host, workgroup, share, password, username, myname;
28 static int max_protocol = PROTOCOL_NT1;
29 static const char *sockops="TCP_NODELAY";
30 static int nprocs=1;
31 static int port_to_use=0;
32 int torture_numops=100;
33 int torture_blocksize=1024*1024;
34 static int procnum; /* records process count number when forking */
35 static struct cli_state *current_cli;
36 static fstring randomfname;
37 static bool use_oplocks;
38 static bool use_level_II_oplocks;
39 static const char *client_txt = "client_oplocks.txt";
40 static bool use_kerberos;
41 static fstring multishare_conn_fname;
42 static bool use_multishare_conn = False;
43 static bool do_encrypt;
44 static const char *local_path = NULL;
46 bool torture_showall = False;
48 static double create_procs(bool (*fn)(int), bool *result);
51 static struct timeval tp1,tp2;
54 void start_timer(void)
56 GetTimeOfDay(&tp1);
59 double end_timer(void)
61 GetTimeOfDay(&tp2);
62 return((tp2.tv_sec - tp1.tv_sec) +
63 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
67 /* return a pointer to a anonymous shared memory segment of size "size"
68 which will persist across fork() but will disappear when all processes
69 exit
71 The memory is not zeroed
73 This function uses system5 shared memory. It takes advantage of a property
74 that the memory is not destroyed if it is attached when the id is removed
76 void *shm_setup(int size)
78 int shmid;
79 void *ret;
81 #ifdef __QNXNTO__
82 shmid = shm_open("private", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
83 if (shmid == -1) {
84 printf("can't get shared memory\n");
85 exit(1);
87 shm_unlink("private");
88 if (ftruncate(shmid, size) == -1) {
89 printf("can't set shared memory size\n");
90 exit(1);
92 ret = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0);
93 if (ret == MAP_FAILED) {
94 printf("can't map shared memory\n");
95 exit(1);
97 #else
98 shmid = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
99 if (shmid == -1) {
100 printf("can't get shared memory\n");
101 exit(1);
103 ret = (void *)shmat(shmid, 0, 0);
104 if (!ret || ret == (void *)-1) {
105 printf("can't attach to shared memory\n");
106 return NULL;
108 /* the following releases the ipc, but note that this process
109 and all its children will still have access to the memory, its
110 just that the shmid is no longer valid for other shm calls. This
111 means we don't leave behind lots of shm segments after we exit
113 See Stevens "advanced programming in unix env" for details
115 shmctl(shmid, IPC_RMID, 0);
116 #endif
118 return ret;
121 /********************************************************************
122 Ensure a connection is encrypted.
123 ********************************************************************/
125 static bool force_cli_encryption(struct cli_state *c,
126 const char *sharename)
128 uint16 major, minor;
129 uint32 caplow, caphigh;
130 NTSTATUS status;
132 if (!SERVER_HAS_UNIX_CIFS(c)) {
133 d_printf("Encryption required and "
134 "server that doesn't support "
135 "UNIX extensions - failing connect\n");
136 return false;
139 if (!cli_unix_extensions_version(c, &major, &minor, &caplow, &caphigh)) {
140 d_printf("Encryption required and "
141 "can't get UNIX CIFS extensions "
142 "version from server.\n");
143 return false;
146 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
147 d_printf("Encryption required and "
148 "share %s doesn't support "
149 "encryption.\n", sharename);
150 return false;
153 if (c->use_kerberos) {
154 status = cli_gss_smb_encryption_start(c);
155 } else {
156 status = cli_raw_ntlm_smb_encryption_start(c,
157 username,
158 password,
159 workgroup);
162 if (!NT_STATUS_IS_OK(status)) {
163 d_printf("Encryption required and "
164 "setup failed with error %s.\n",
165 nt_errstr(status));
166 return false;
169 return true;
173 static struct cli_state *open_nbt_connection(void)
175 struct nmb_name called, calling;
176 struct sockaddr_storage ss;
177 struct cli_state *c;
178 NTSTATUS status;
180 make_nmb_name(&calling, myname, 0x0);
181 make_nmb_name(&called , host, 0x20);
183 zero_sockaddr(&ss);
185 if (!(c = cli_initialise())) {
186 printf("Failed initialize cli_struct to connect with %s\n", host);
187 return NULL;
190 c->port = port_to_use;
192 status = cli_connect(c, host, &ss);
193 if (!NT_STATUS_IS_OK(status)) {
194 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
195 return NULL;
198 c->use_kerberos = use_kerberos;
200 c->timeout = 120000; /* set a really long timeout (2 minutes) */
201 if (use_oplocks) c->use_oplocks = True;
202 if (use_level_II_oplocks) c->use_level_II_oplocks = True;
204 if (!cli_session_request(c, &calling, &called)) {
206 * Well, that failed, try *SMBSERVER ...
207 * However, we must reconnect as well ...
209 status = cli_connect(c, host, &ss);
210 if (!NT_STATUS_IS_OK(status)) {
211 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
212 return NULL;
215 make_nmb_name(&called, "*SMBSERVER", 0x20);
216 if (!cli_session_request(c, &calling, &called)) {
217 printf("%s rejected the session\n",host);
218 printf("We tried with a called name of %s & %s\n",
219 host, "*SMBSERVER");
220 cli_shutdown(c);
221 return NULL;
225 return c;
228 /* Insert a NULL at the first separator of the given path and return a pointer
229 * to the remainder of the string.
231 static char *
232 terminate_path_at_separator(char * path)
234 char * p;
236 if (!path) {
237 return NULL;
240 if ((p = strchr_m(path, '/'))) {
241 *p = '\0';
242 return p + 1;
245 if ((p = strchr_m(path, '\\'))) {
246 *p = '\0';
247 return p + 1;
250 /* No separator. */
251 return NULL;
255 parse a //server/share type UNC name
257 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
258 char **hostname, char **sharename)
260 char *p;
262 *hostname = *sharename = NULL;
264 if (strncmp(unc_name, "\\\\", 2) &&
265 strncmp(unc_name, "//", 2)) {
266 return False;
269 *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
270 p = terminate_path_at_separator(*hostname);
272 if (p && *p) {
273 *sharename = talloc_strdup(mem_ctx, p);
274 terminate_path_at_separator(*sharename);
277 if (*hostname && *sharename) {
278 return True;
281 TALLOC_FREE(*hostname);
282 TALLOC_FREE(*sharename);
283 return False;
286 static bool torture_open_connection_share(struct cli_state **c,
287 const char *hostname,
288 const char *sharename)
290 bool retry;
291 int flags = 0;
292 NTSTATUS status;
294 if (use_kerberos)
295 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
296 if (use_oplocks)
297 flags |= CLI_FULL_CONNECTION_OPLOCKS;
298 if (use_level_II_oplocks)
299 flags |= CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS;
301 status = cli_full_connection(c, myname,
302 hostname, NULL, port_to_use,
303 sharename, "?????",
304 username, workgroup,
305 password, flags, Undefined, &retry);
306 if (!NT_STATUS_IS_OK(status)) {
307 printf("failed to open share connection: //%s/%s port:%d - %s\n",
308 hostname, sharename, port_to_use, nt_errstr(status));
309 return False;
312 (*c)->timeout = 120000; /* set a really long timeout (2 minutes) */
314 if (do_encrypt) {
315 return force_cli_encryption(*c,
316 sharename);
318 return True;
321 bool torture_open_connection(struct cli_state **c, int conn_index)
323 char **unc_list = NULL;
324 int num_unc_names = 0;
325 bool result;
327 if (use_multishare_conn==True) {
328 char *h, *s;
329 unc_list = file_lines_load(multishare_conn_fname, &num_unc_names, 0, NULL);
330 if (!unc_list || num_unc_names <= 0) {
331 printf("Failed to load unc names list from '%s'\n", multishare_conn_fname);
332 exit(1);
335 if (!smbcli_parse_unc(unc_list[conn_index % num_unc_names],
336 NULL, &h, &s)) {
337 printf("Failed to parse UNC name %s\n",
338 unc_list[conn_index % num_unc_names]);
339 TALLOC_FREE(unc_list);
340 exit(1);
343 result = torture_open_connection_share(c, h, s);
345 /* h, s were copied earlier */
346 TALLOC_FREE(unc_list);
347 return result;
350 return torture_open_connection_share(c, host, share);
353 bool torture_cli_session_setup2(struct cli_state *cli, uint16 *new_vuid)
355 uint16 old_vuid = cli->vuid;
356 fstring old_user_name;
357 size_t passlen = strlen(password);
358 NTSTATUS status;
359 bool ret;
361 fstrcpy(old_user_name, cli->user_name);
362 cli->vuid = 0;
363 ret = NT_STATUS_IS_OK(cli_session_setup(cli, username,
364 password, passlen,
365 password, passlen,
366 workgroup));
367 *new_vuid = cli->vuid;
368 cli->vuid = old_vuid;
369 status = cli_set_username(cli, old_user_name);
370 if (!NT_STATUS_IS_OK(status)) {
371 return false;
373 return ret;
377 bool torture_close_connection(struct cli_state *c)
379 bool ret = True;
380 if (!cli_tdis(c)) {
381 printf("tdis failed (%s)\n", cli_errstr(c));
382 ret = False;
385 cli_shutdown(c);
387 return ret;
391 /* check if the server produced the expected error code */
392 static bool check_error(int line, struct cli_state *c,
393 uint8 eclass, uint32 ecode, NTSTATUS nterr)
395 if (cli_is_dos_error(c)) {
396 uint8 cclass;
397 uint32 num;
399 /* Check DOS error */
401 cli_dos_error(c, &cclass, &num);
403 if (eclass != cclass || ecode != num) {
404 printf("unexpected error code class=%d code=%d\n",
405 (int)cclass, (int)num);
406 printf(" expected %d/%d %s (line=%d)\n",
407 (int)eclass, (int)ecode, nt_errstr(nterr), line);
408 return False;
411 } else {
412 NTSTATUS status;
414 /* Check NT error */
416 status = cli_nt_error(c);
418 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
419 printf("unexpected error code %s\n", nt_errstr(status));
420 printf(" expected %s (line=%d)\n", nt_errstr(nterr), line);
421 return False;
425 return True;
429 static bool wait_lock(struct cli_state *c, int fnum, uint32 offset, uint32 len)
431 while (!cli_lock(c, fnum, offset, len, -1, WRITE_LOCK)) {
432 if (!check_error(__LINE__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
434 return True;
438 static bool rw_torture(struct cli_state *c)
440 const char *lockfname = "\\torture.lck";
441 fstring fname;
442 uint16_t fnum;
443 uint16_t fnum2;
444 pid_t pid2, pid = getpid();
445 int i, j;
446 char buf[1024];
447 bool correct = True;
448 NTSTATUS status;
450 memset(buf, '\0', sizeof(buf));
452 status = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
453 DENY_NONE, &fnum2);
454 if (!NT_STATUS_IS_OK(status)) {
455 status = cli_open(c, lockfname, O_RDWR, DENY_NONE, &fnum2);
457 if (!NT_STATUS_IS_OK(status)) {
458 printf("open of %s failed (%s)\n", lockfname, cli_errstr(c));
459 return False;
462 for (i=0;i<torture_numops;i++) {
463 unsigned n = (unsigned)sys_random()%10;
464 if (i % 10 == 0) {
465 printf("%d\r", i); fflush(stdout);
467 slprintf(fname, sizeof(fstring) - 1, "\\torture.%u", n);
469 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
470 return False;
473 if (!NT_STATUS_IS_OK(cli_open(c, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL, &fnum))) {
474 printf("open failed (%s)\n", cli_errstr(c));
475 correct = False;
476 break;
479 if (cli_write(c, fnum, 0, (char *)&pid, 0, sizeof(pid)) != sizeof(pid)) {
480 printf("write failed (%s)\n", cli_errstr(c));
481 correct = False;
484 for (j=0;j<50;j++) {
485 if (cli_write(c, fnum, 0, (char *)buf,
486 sizeof(pid)+(j*sizeof(buf)),
487 sizeof(buf)) != sizeof(buf)) {
488 printf("write failed (%s)\n", cli_errstr(c));
489 correct = False;
493 pid2 = 0;
495 if (cli_read(c, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
496 printf("read failed (%s)\n", cli_errstr(c));
497 correct = False;
500 if (pid2 != pid) {
501 printf("data corruption!\n");
502 correct = False;
505 if (!NT_STATUS_IS_OK(cli_close(c, fnum))) {
506 printf("close failed (%s)\n", cli_errstr(c));
507 correct = False;
510 if (!NT_STATUS_IS_OK(cli_unlink(c, fname, aSYSTEM | aHIDDEN))) {
511 printf("unlink failed (%s)\n", cli_errstr(c));
512 correct = False;
515 if (!NT_STATUS_IS_OK(cli_unlock(c, fnum2, n*sizeof(int), sizeof(int)))) {
516 printf("unlock failed (%s)\n", cli_errstr(c));
517 correct = False;
521 cli_close(c, fnum2);
522 cli_unlink(c, lockfname, aSYSTEM | aHIDDEN);
524 printf("%d\n", i);
526 return correct;
529 static bool run_torture(int dummy)
531 struct cli_state *cli;
532 bool ret;
534 cli = current_cli;
536 cli_sockopt(cli, sockops);
538 ret = rw_torture(cli);
540 if (!torture_close_connection(cli)) {
541 ret = False;
544 return ret;
547 static bool rw_torture3(struct cli_state *c, char *lockfname)
549 uint16_t fnum = (uint16_t)-1;
550 unsigned int i = 0;
551 char buf[131072];
552 char buf_rd[131072];
553 unsigned count;
554 unsigned countprev = 0;
555 ssize_t sent = 0;
556 bool correct = True;
557 NTSTATUS status;
559 srandom(1);
560 for (i = 0; i < sizeof(buf); i += sizeof(uint32))
562 SIVAL(buf, i, sys_random());
565 if (procnum == 0)
567 if (!NT_STATUS_IS_OK(cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
568 DENY_NONE, &fnum))) {
569 printf("first open read/write of %s failed (%s)\n",
570 lockfname, cli_errstr(c));
571 return False;
574 else
576 for (i = 0; i < 500 && fnum == (uint16_t)-1; i++)
578 status = cli_open(c, lockfname, O_RDONLY,
579 DENY_NONE, &fnum);
580 if (!NT_STATUS_IS_OK(status)) {
581 break;
583 smb_msleep(10);
585 if (!NT_STATUS_IS_OK(status)) {
586 printf("second open read-only of %s failed (%s)\n",
587 lockfname, cli_errstr(c));
588 return False;
592 i = 0;
593 for (count = 0; count < sizeof(buf); count += sent)
595 if (count >= countprev) {
596 printf("%d %8d\r", i, count);
597 fflush(stdout);
598 i++;
599 countprev += (sizeof(buf) / 20);
602 if (procnum == 0)
604 sent = ((unsigned)sys_random()%(20))+ 1;
605 if (sent > sizeof(buf) - count)
607 sent = sizeof(buf) - count;
610 if (cli_write(c, fnum, 0, buf+count, count, (size_t)sent) != sent) {
611 printf("write failed (%s)\n", cli_errstr(c));
612 correct = False;
615 else
617 sent = cli_read(c, fnum, buf_rd+count, count,
618 sizeof(buf)-count);
619 if (sent < 0)
621 printf("read failed offset:%d size:%ld (%s)\n",
622 count, (unsigned long)sizeof(buf)-count,
623 cli_errstr(c));
624 correct = False;
625 sent = 0;
627 if (sent > 0)
629 if (memcmp(buf_rd+count, buf+count, sent) != 0)
631 printf("read/write compare failed\n");
632 printf("offset: %d req %ld recvd %ld\n", count, (unsigned long)sizeof(buf)-count, (unsigned long)sent);
633 correct = False;
634 break;
641 if (!NT_STATUS_IS_OK(cli_close(c, fnum))) {
642 printf("close failed (%s)\n", cli_errstr(c));
643 correct = False;
646 return correct;
649 static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
651 const char *lockfname = "\\torture2.lck";
652 uint16_t fnum1;
653 uint16_t fnum2;
654 int i;
655 char buf[131072];
656 char buf_rd[131072];
657 bool correct = True;
658 ssize_t bytes_read;
660 if (!NT_STATUS_IS_OK(cli_unlink(c1, lockfname, aSYSTEM | aHIDDEN))) {
661 printf("unlink failed (%s) (normal, this file should not exist)\n", cli_errstr(c1));
664 if (!NT_STATUS_IS_OK(cli_open(c1, lockfname, O_RDWR | O_CREAT | O_EXCL,
665 DENY_NONE, &fnum1))) {
666 printf("first open read/write of %s failed (%s)\n",
667 lockfname, cli_errstr(c1));
668 return False;
670 if (!NT_STATUS_IS_OK(cli_open(c2, lockfname, O_RDONLY,
671 DENY_NONE, &fnum2))) {
672 printf("second open read-only of %s failed (%s)\n",
673 lockfname, cli_errstr(c2));
674 cli_close(c1, fnum1);
675 return False;
678 for (i=0;i<torture_numops;i++)
680 size_t buf_size = ((unsigned)sys_random()%(sizeof(buf)-1))+ 1;
681 if (i % 10 == 0) {
682 printf("%d\r", i); fflush(stdout);
685 generate_random_buffer((unsigned char *)buf, buf_size);
687 if (cli_write(c1, fnum1, 0, buf, 0, buf_size) != buf_size) {
688 printf("write failed (%s)\n", cli_errstr(c1));
689 correct = False;
690 break;
693 if ((bytes_read = cli_read(c2, fnum2, buf_rd, 0, buf_size)) != buf_size) {
694 printf("read failed (%s)\n", cli_errstr(c2));
695 printf("read %d, expected %ld\n", (int)bytes_read,
696 (unsigned long)buf_size);
697 correct = False;
698 break;
701 if (memcmp(buf_rd, buf, buf_size) != 0)
703 printf("read/write compare failed\n");
704 correct = False;
705 break;
709 if (!NT_STATUS_IS_OK(cli_close(c2, fnum2))) {
710 printf("close failed (%s)\n", cli_errstr(c2));
711 correct = False;
713 if (!NT_STATUS_IS_OK(cli_close(c1, fnum1))) {
714 printf("close failed (%s)\n", cli_errstr(c1));
715 correct = False;
718 if (!NT_STATUS_IS_OK(cli_unlink(c1, lockfname, aSYSTEM | aHIDDEN))) {
719 printf("unlink failed (%s)\n", cli_errstr(c1));
720 correct = False;
723 return correct;
726 static bool run_readwritetest(int dummy)
728 struct cli_state *cli1, *cli2;
729 bool test1, test2 = False;
731 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
732 return False;
734 cli_sockopt(cli1, sockops);
735 cli_sockopt(cli2, sockops);
737 printf("starting readwritetest\n");
739 test1 = rw_torture2(cli1, cli2);
740 printf("Passed readwritetest v1: %s\n", BOOLSTR(test1));
742 if (test1) {
743 test2 = rw_torture2(cli1, cli1);
744 printf("Passed readwritetest v2: %s\n", BOOLSTR(test2));
747 if (!torture_close_connection(cli1)) {
748 test1 = False;
751 if (!torture_close_connection(cli2)) {
752 test2 = False;
755 return (test1 && test2);
758 static bool run_readwritemulti(int dummy)
760 struct cli_state *cli;
761 bool test;
763 cli = current_cli;
765 cli_sockopt(cli, sockops);
767 printf("run_readwritemulti: fname %s\n", randomfname);
768 test = rw_torture3(cli, randomfname);
770 if (!torture_close_connection(cli)) {
771 test = False;
774 return test;
777 static bool run_readwritelarge(int dummy)
779 static struct cli_state *cli1;
780 uint16_t fnum1;
781 const char *lockfname = "\\large.dat";
782 SMB_OFF_T fsize;
783 char buf[126*1024];
784 bool correct = True;
786 if (!torture_open_connection(&cli1, 0)) {
787 return False;
789 cli_sockopt(cli1, sockops);
790 memset(buf,'\0',sizeof(buf));
792 cli1->max_xmit = 128*1024;
794 printf("starting readwritelarge\n");
796 cli_unlink(cli1, lockfname, aSYSTEM | aHIDDEN);
798 if (!NT_STATUS_IS_OK(cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE, &fnum1))) {
799 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
800 return False;
803 cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf));
805 if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
806 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
807 correct = False;
810 if (fsize == sizeof(buf))
811 printf("readwritelarge test 1 succeeded (size = %lx)\n",
812 (unsigned long)fsize);
813 else {
814 printf("readwritelarge test 1 failed (size = %lx)\n",
815 (unsigned long)fsize);
816 correct = False;
819 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
820 printf("close failed (%s)\n", cli_errstr(cli1));
821 correct = False;
824 if (!NT_STATUS_IS_OK(cli_unlink(cli1, lockfname, aSYSTEM | aHIDDEN))) {
825 printf("unlink failed (%s)\n", cli_errstr(cli1));
826 correct = False;
829 if (!NT_STATUS_IS_OK(cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE, &fnum1))) {
830 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
831 return False;
834 cli1->max_xmit = 4*1024;
836 cli_smbwrite(cli1, fnum1, buf, 0, sizeof(buf));
838 if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
839 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
840 correct = False;
843 if (fsize == sizeof(buf))
844 printf("readwritelarge test 2 succeeded (size = %lx)\n",
845 (unsigned long)fsize);
846 else {
847 printf("readwritelarge test 2 failed (size = %lx)\n",
848 (unsigned long)fsize);
849 correct = False;
852 #if 0
853 /* ToDo - set allocation. JRA */
854 if(!cli_set_allocation_size(cli1, fnum1, 0)) {
855 printf("set allocation size to zero failed (%s)\n", cli_errstr(&cli1));
856 return False;
858 if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
859 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
860 correct = False;
862 if (fsize != 0)
863 printf("readwritelarge test 3 (truncate test) succeeded (size = %x)\n", fsize);
864 #endif
866 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
867 printf("close failed (%s)\n", cli_errstr(cli1));
868 correct = False;
871 if (!torture_close_connection(cli1)) {
872 correct = False;
874 return correct;
877 int line_count = 0;
878 int nbio_id;
880 #define ival(s) strtol(s, NULL, 0)
882 /* run a test that simulates an approximate netbench client load */
883 static bool run_netbench(int client)
885 struct cli_state *cli;
886 int i;
887 char line[1024];
888 char cname[20];
889 FILE *f;
890 const char *params[20];
891 bool correct = True;
893 cli = current_cli;
895 nbio_id = client;
897 cli_sockopt(cli, sockops);
899 nb_setup(cli);
901 slprintf(cname,sizeof(cname)-1, "client%d", client);
903 f = fopen(client_txt, "r");
905 if (!f) {
906 perror(client_txt);
907 return False;
910 while (fgets(line, sizeof(line)-1, f)) {
911 char *saveptr;
912 line_count++;
914 line[strlen(line)-1] = 0;
916 /* printf("[%d] %s\n", line_count, line); */
918 all_string_sub(line,"client1", cname, sizeof(line));
920 /* parse the command parameters */
921 params[0] = strtok_r(line, " ", &saveptr);
922 i = 0;
923 while (params[i]) params[++i] = strtok_r(NULL, " ", &saveptr);
925 params[i] = "";
927 if (i < 2) continue;
929 if (!strncmp(params[0],"SMB", 3)) {
930 printf("ERROR: You are using a dbench 1 load file\n");
931 exit(1);
934 if (!strcmp(params[0],"NTCreateX")) {
935 nb_createx(params[1], ival(params[2]), ival(params[3]),
936 ival(params[4]));
937 } else if (!strcmp(params[0],"Close")) {
938 nb_close(ival(params[1]));
939 } else if (!strcmp(params[0],"Rename")) {
940 nb_rename(params[1], params[2]);
941 } else if (!strcmp(params[0],"Unlink")) {
942 nb_unlink(params[1]);
943 } else if (!strcmp(params[0],"Deltree")) {
944 nb_deltree(params[1]);
945 } else if (!strcmp(params[0],"Rmdir")) {
946 nb_rmdir(params[1]);
947 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
948 nb_qpathinfo(params[1]);
949 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
950 nb_qfileinfo(ival(params[1]));
951 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
952 nb_qfsinfo(ival(params[1]));
953 } else if (!strcmp(params[0],"FIND_FIRST")) {
954 nb_findfirst(params[1]);
955 } else if (!strcmp(params[0],"WriteX")) {
956 nb_writex(ival(params[1]),
957 ival(params[2]), ival(params[3]), ival(params[4]));
958 } else if (!strcmp(params[0],"ReadX")) {
959 nb_readx(ival(params[1]),
960 ival(params[2]), ival(params[3]), ival(params[4]));
961 } else if (!strcmp(params[0],"Flush")) {
962 nb_flush(ival(params[1]));
963 } else {
964 printf("Unknown operation %s\n", params[0]);
965 exit(1);
968 fclose(f);
970 nb_cleanup();
972 if (!torture_close_connection(cli)) {
973 correct = False;
976 return correct;
980 /* run a test that simulates an approximate netbench client load */
981 static bool run_nbench(int dummy)
983 double t;
984 bool correct = True;
986 nbio_shmem(nprocs);
988 nbio_id = -1;
990 signal(SIGALRM, nb_alarm);
991 alarm(1);
992 t = create_procs(run_netbench, &correct);
993 alarm(0);
995 printf("\nThroughput %g MB/sec\n",
996 1.0e-6 * nbio_total() / t);
997 return correct;
1002 This test checks for two things:
1004 1) correct support for retaining locks over a close (ie. the server
1005 must not use posix semantics)
1006 2) support for lock timeouts
1008 static bool run_locktest1(int dummy)
1010 struct cli_state *cli1, *cli2;
1011 const char *fname = "\\lockt1.lck";
1012 uint16_t fnum1, fnum2, fnum3;
1013 time_t t1, t2;
1014 unsigned lock_timeout;
1016 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1017 return False;
1019 cli_sockopt(cli1, sockops);
1020 cli_sockopt(cli2, sockops);
1022 printf("starting locktest1\n");
1024 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1026 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1027 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1028 return False;
1030 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum2))) {
1031 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli1));
1032 return False;
1034 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum3))) {
1035 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli2));
1036 return False;
1039 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
1040 printf("lock1 failed (%s)\n", cli_errstr(cli1));
1041 return False;
1045 if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1046 printf("lock2 succeeded! This is a locking bug\n");
1047 return False;
1048 } else {
1049 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1050 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1054 lock_timeout = (1 + (random() % 20));
1055 printf("Testing lock timeout with timeout=%u\n", lock_timeout);
1056 t1 = time(NULL);
1057 if (cli_lock(cli2, fnum3, 0, 4, lock_timeout * 1000, WRITE_LOCK)) {
1058 printf("lock3 succeeded! This is a locking bug\n");
1059 return False;
1060 } else {
1061 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1062 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1064 t2 = time(NULL);
1066 if (ABS(t2 - t1) < lock_timeout-1) {
1067 printf("error: This server appears not to support timed lock requests\n");
1070 printf("server slept for %u seconds for a %u second timeout\n",
1071 (unsigned int)(t2-t1), lock_timeout);
1073 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
1074 printf("close1 failed (%s)\n", cli_errstr(cli1));
1075 return False;
1078 if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1079 printf("lock4 succeeded! This is a locking bug\n");
1080 return False;
1081 } else {
1082 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1083 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1086 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
1087 printf("close2 failed (%s)\n", cli_errstr(cli1));
1088 return False;
1091 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum3))) {
1092 printf("close3 failed (%s)\n", cli_errstr(cli2));
1093 return False;
1096 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
1097 printf("unlink failed (%s)\n", cli_errstr(cli1));
1098 return False;
1102 if (!torture_close_connection(cli1)) {
1103 return False;
1106 if (!torture_close_connection(cli2)) {
1107 return False;
1110 printf("Passed locktest1\n");
1111 return True;
1115 this checks to see if a secondary tconx can use open files from an
1116 earlier tconx
1118 static bool run_tcon_test(int dummy)
1120 static struct cli_state *cli;
1121 const char *fname = "\\tcontest.tmp";
1122 uint16 fnum1;
1123 uint16 cnum1, cnum2, cnum3;
1124 uint16 vuid1, vuid2;
1125 char buf[4];
1126 bool ret = True;
1127 NTSTATUS status;
1129 memset(buf, '\0', sizeof(buf));
1131 if (!torture_open_connection(&cli, 0)) {
1132 return False;
1134 cli_sockopt(cli, sockops);
1136 printf("starting tcontest\n");
1138 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
1140 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1141 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1142 return False;
1145 cnum1 = cli->cnum;
1146 vuid1 = cli->vuid;
1148 if (cli_write(cli, fnum1, 0, buf, 130, 4) != 4) {
1149 printf("initial write failed (%s)", cli_errstr(cli));
1150 return False;
1153 status = cli_tcon_andx(cli, share, "?????",
1154 password, strlen(password)+1);
1155 if (!NT_STATUS_IS_OK(status)) {
1156 printf("%s refused 2nd tree connect (%s)\n", host,
1157 nt_errstr(status));
1158 cli_shutdown(cli);
1159 return False;
1162 cnum2 = cli->cnum;
1163 cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
1164 vuid2 = cli->vuid + 1;
1166 /* try a write with the wrong tid */
1167 cli->cnum = cnum2;
1169 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1170 printf("* server allows write with wrong TID\n");
1171 ret = False;
1172 } else {
1173 printf("server fails write with wrong TID : %s\n", cli_errstr(cli));
1177 /* try a write with an invalid tid */
1178 cli->cnum = cnum3;
1180 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1181 printf("* server allows write with invalid TID\n");
1182 ret = False;
1183 } else {
1184 printf("server fails write with invalid TID : %s\n", cli_errstr(cli));
1187 /* try a write with an invalid vuid */
1188 cli->vuid = vuid2;
1189 cli->cnum = cnum1;
1191 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1192 printf("* server allows write with invalid VUID\n");
1193 ret = False;
1194 } else {
1195 printf("server fails write with invalid VUID : %s\n", cli_errstr(cli));
1198 cli->cnum = cnum1;
1199 cli->vuid = vuid1;
1201 if (!NT_STATUS_IS_OK(cli_close(cli, fnum1))) {
1202 printf("close failed (%s)\n", cli_errstr(cli));
1203 return False;
1206 cli->cnum = cnum2;
1208 if (!cli_tdis(cli)) {
1209 printf("secondary tdis failed (%s)\n", cli_errstr(cli));
1210 return False;
1213 cli->cnum = cnum1;
1215 if (!torture_close_connection(cli)) {
1216 return False;
1219 return ret;
1224 checks for old style tcon support
1226 static bool run_tcon2_test(int dummy)
1228 static struct cli_state *cli;
1229 uint16 cnum, max_xmit;
1230 char *service;
1231 NTSTATUS status;
1233 if (!torture_open_connection(&cli, 0)) {
1234 return False;
1236 cli_sockopt(cli, sockops);
1238 printf("starting tcon2 test\n");
1240 if (asprintf(&service, "\\\\%s\\%s", host, share) == -1) {
1241 return false;
1244 status = cli_raw_tcon(cli, service, password, "?????", &max_xmit, &cnum);
1246 if (!NT_STATUS_IS_OK(status)) {
1247 printf("tcon2 failed : %s\n", cli_errstr(cli));
1248 } else {
1249 printf("tcon OK : max_xmit=%d cnum=%d tid=%d\n",
1250 (int)max_xmit, (int)cnum, SVAL(cli->inbuf, smb_tid));
1253 if (!torture_close_connection(cli)) {
1254 return False;
1257 printf("Passed tcon2 test\n");
1258 return True;
1261 static bool tcon_devtest(struct cli_state *cli,
1262 const char *myshare, const char *devtype,
1263 const char *return_devtype,
1264 NTSTATUS expected_error)
1266 NTSTATUS status;
1267 bool ret;
1269 status = cli_tcon_andx(cli, myshare, devtype,
1270 password, strlen(password)+1);
1272 if (NT_STATUS_IS_OK(expected_error)) {
1273 if (NT_STATUS_IS_OK(status)) {
1274 if (strcmp(cli->dev, return_devtype) == 0) {
1275 ret = True;
1276 } else {
1277 printf("tconX to share %s with type %s "
1278 "succeeded but returned the wrong "
1279 "device type (got [%s] but should have got [%s])\n",
1280 myshare, devtype, cli->dev, return_devtype);
1281 ret = False;
1283 } else {
1284 printf("tconX to share %s with type %s "
1285 "should have succeeded but failed\n",
1286 myshare, devtype);
1287 ret = False;
1289 cli_tdis(cli);
1290 } else {
1291 if (NT_STATUS_IS_OK(status)) {
1292 printf("tconx to share %s with type %s "
1293 "should have failed but succeeded\n",
1294 myshare, devtype);
1295 ret = False;
1296 } else {
1297 if (NT_STATUS_EQUAL(cli_nt_error(cli),
1298 expected_error)) {
1299 ret = True;
1300 } else {
1301 printf("Returned unexpected error\n");
1302 ret = False;
1306 return ret;
1310 checks for correct tconX support
1312 static bool run_tcon_devtype_test(int dummy)
1314 static struct cli_state *cli1 = NULL;
1315 bool retry;
1316 int flags = 0;
1317 NTSTATUS status;
1318 bool ret = True;
1320 status = cli_full_connection(&cli1, myname,
1321 host, NULL, port_to_use,
1322 NULL, NULL,
1323 username, workgroup,
1324 password, flags, Undefined, &retry);
1326 if (!NT_STATUS_IS_OK(status)) {
1327 printf("could not open connection\n");
1328 return False;
1331 if (!tcon_devtest(cli1, "IPC$", "A:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1332 ret = False;
1334 if (!tcon_devtest(cli1, "IPC$", "?????", "IPC", NT_STATUS_OK))
1335 ret = False;
1337 if (!tcon_devtest(cli1, "IPC$", "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1338 ret = False;
1340 if (!tcon_devtest(cli1, "IPC$", "IPC", "IPC", NT_STATUS_OK))
1341 ret = False;
1343 if (!tcon_devtest(cli1, "IPC$", "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1344 ret = False;
1346 if (!tcon_devtest(cli1, share, "A:", "A:", NT_STATUS_OK))
1347 ret = False;
1349 if (!tcon_devtest(cli1, share, "?????", "A:", NT_STATUS_OK))
1350 ret = False;
1352 if (!tcon_devtest(cli1, share, "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1353 ret = False;
1355 if (!tcon_devtest(cli1, share, "IPC", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1356 ret = False;
1358 if (!tcon_devtest(cli1, share, "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1359 ret = False;
1361 cli_shutdown(cli1);
1363 if (ret)
1364 printf("Passed tcondevtest\n");
1366 return ret;
1371 This test checks that
1373 1) the server supports multiple locking contexts on the one SMB
1374 connection, distinguished by PID.
1376 2) the server correctly fails overlapping locks made by the same PID (this
1377 goes against POSIX behaviour, which is why it is tricky to implement)
1379 3) the server denies unlock requests by an incorrect client PID
1381 static bool run_locktest2(int dummy)
1383 static struct cli_state *cli;
1384 const char *fname = "\\lockt2.lck";
1385 uint16_t fnum1, fnum2, fnum3;
1386 bool correct = True;
1388 if (!torture_open_connection(&cli, 0)) {
1389 return False;
1392 cli_sockopt(cli, sockops);
1394 printf("starting locktest2\n");
1396 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
1398 cli_setpid(cli, 1);
1400 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1401 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1402 return False;
1405 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum2))) {
1406 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli));
1407 return False;
1410 cli_setpid(cli, 2);
1412 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum3))) {
1413 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli));
1414 return False;
1417 cli_setpid(cli, 1);
1419 if (!cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1420 printf("lock1 failed (%s)\n", cli_errstr(cli));
1421 return False;
1424 if (cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1425 printf("WRITE lock1 succeeded! This is a locking bug\n");
1426 correct = False;
1427 } else {
1428 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1429 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1432 if (cli_lock(cli, fnum2, 0, 4, 0, WRITE_LOCK)) {
1433 printf("WRITE lock2 succeeded! This is a locking bug\n");
1434 correct = False;
1435 } else {
1436 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1437 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1440 if (cli_lock(cli, fnum2, 0, 4, 0, READ_LOCK)) {
1441 printf("READ lock2 succeeded! This is a locking bug\n");
1442 correct = False;
1443 } else {
1444 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1445 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1448 if (!cli_lock(cli, fnum1, 100, 4, 0, WRITE_LOCK)) {
1449 printf("lock at 100 failed (%s)\n", cli_errstr(cli));
1451 cli_setpid(cli, 2);
1452 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 100, 4))) {
1453 printf("unlock at 100 succeeded! This is a locking bug\n");
1454 correct = False;
1457 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 0, 4))) {
1458 printf("unlock1 succeeded! This is a locking bug\n");
1459 correct = False;
1460 } else {
1461 if (!check_error(__LINE__, cli,
1462 ERRDOS, ERRlock,
1463 NT_STATUS_RANGE_NOT_LOCKED)) return False;
1466 if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 0, 8))) {
1467 printf("unlock2 succeeded! This is a locking bug\n");
1468 correct = False;
1469 } else {
1470 if (!check_error(__LINE__, cli,
1471 ERRDOS, ERRlock,
1472 NT_STATUS_RANGE_NOT_LOCKED)) return False;
1475 if (cli_lock(cli, fnum3, 0, 4, 0, WRITE_LOCK)) {
1476 printf("lock3 succeeded! This is a locking bug\n");
1477 correct = False;
1478 } else {
1479 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
1482 cli_setpid(cli, 1);
1484 if (!NT_STATUS_IS_OK(cli_close(cli, fnum1))) {
1485 printf("close1 failed (%s)\n", cli_errstr(cli));
1486 return False;
1489 if (!NT_STATUS_IS_OK(cli_close(cli, fnum2))) {
1490 printf("close2 failed (%s)\n", cli_errstr(cli));
1491 return False;
1494 if (!NT_STATUS_IS_OK(cli_close(cli, fnum3))) {
1495 printf("close3 failed (%s)\n", cli_errstr(cli));
1496 return False;
1499 if (!torture_close_connection(cli)) {
1500 correct = False;
1503 printf("locktest2 finished\n");
1505 return correct;
1510 This test checks that
1512 1) the server supports the full offset range in lock requests
1514 static bool run_locktest3(int dummy)
1516 static struct cli_state *cli1, *cli2;
1517 const char *fname = "\\lockt3.lck";
1518 uint16_t fnum1, fnum2;
1519 int i;
1520 uint32 offset;
1521 bool correct = True;
1523 #define NEXT_OFFSET offset += (~(uint32)0) / torture_numops
1525 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1526 return False;
1528 cli_sockopt(cli1, sockops);
1529 cli_sockopt(cli2, sockops);
1531 printf("starting locktest3\n");
1533 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1535 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
1536 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1537 return False;
1539 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2))) {
1540 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli2));
1541 return False;
1544 for (offset=i=0;i<torture_numops;i++) {
1545 NEXT_OFFSET;
1546 if (!cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1547 printf("lock1 %d failed (%s)\n",
1549 cli_errstr(cli1));
1550 return False;
1553 if (!cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1554 printf("lock2 %d failed (%s)\n",
1556 cli_errstr(cli1));
1557 return False;
1561 for (offset=i=0;i<torture_numops;i++) {
1562 NEXT_OFFSET;
1564 if (cli_lock(cli1, fnum1, offset-2, 1, 0, WRITE_LOCK)) {
1565 printf("error: lock1 %d succeeded!\n", i);
1566 return False;
1569 if (cli_lock(cli2, fnum2, offset-1, 1, 0, WRITE_LOCK)) {
1570 printf("error: lock2 %d succeeded!\n", i);
1571 return False;
1574 if (cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1575 printf("error: lock3 %d succeeded!\n", i);
1576 return False;
1579 if (cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1580 printf("error: lock4 %d succeeded!\n", i);
1581 return False;
1585 for (offset=i=0;i<torture_numops;i++) {
1586 NEXT_OFFSET;
1588 if (!NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, offset-1, 1))) {
1589 printf("unlock1 %d failed (%s)\n",
1591 cli_errstr(cli1));
1592 return False;
1595 if (!NT_STATUS_IS_OK(cli_unlock(cli2, fnum2, offset-2, 1))) {
1596 printf("unlock2 %d failed (%s)\n",
1598 cli_errstr(cli1));
1599 return False;
1603 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
1604 printf("close1 failed (%s)\n", cli_errstr(cli1));
1605 return False;
1608 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
1609 printf("close2 failed (%s)\n", cli_errstr(cli2));
1610 return False;
1613 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
1614 printf("unlink failed (%s)\n", cli_errstr(cli1));
1615 return False;
1618 if (!torture_close_connection(cli1)) {
1619 correct = False;
1622 if (!torture_close_connection(cli2)) {
1623 correct = False;
1626 printf("finished locktest3\n");
1628 return correct;
1631 #define EXPECTED(ret, v) if ((ret) != (v)) { \
1632 printf("** "); correct = False; \
1636 looks at overlapping locks
1638 static bool run_locktest4(int dummy)
1640 static struct cli_state *cli1, *cli2;
1641 const char *fname = "\\lockt4.lck";
1642 uint16_t fnum1, fnum2, f;
1643 bool ret;
1644 char buf[1000];
1645 bool correct = True;
1647 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1648 return False;
1651 cli_sockopt(cli1, sockops);
1652 cli_sockopt(cli2, sockops);
1654 printf("starting locktest4\n");
1656 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1658 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1659 cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
1661 memset(buf, 0, sizeof(buf));
1663 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1664 printf("Failed to create file\n");
1665 correct = False;
1666 goto fail;
1669 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1670 cli_lock(cli1, fnum1, 2, 4, 0, WRITE_LOCK);
1671 EXPECTED(ret, False);
1672 printf("the same process %s set overlapping write locks\n", ret?"can":"cannot");
1674 ret = cli_lock(cli1, fnum1, 10, 4, 0, READ_LOCK) &&
1675 cli_lock(cli1, fnum1, 12, 4, 0, READ_LOCK);
1676 EXPECTED(ret, True);
1677 printf("the same process %s set overlapping read locks\n", ret?"can":"cannot");
1679 ret = cli_lock(cli1, fnum1, 20, 4, 0, WRITE_LOCK) &&
1680 cli_lock(cli2, fnum2, 22, 4, 0, WRITE_LOCK);
1681 EXPECTED(ret, False);
1682 printf("a different connection %s set overlapping write locks\n", ret?"can":"cannot");
1684 ret = cli_lock(cli1, fnum1, 30, 4, 0, READ_LOCK) &&
1685 cli_lock(cli2, fnum2, 32, 4, 0, READ_LOCK);
1686 EXPECTED(ret, True);
1687 printf("a different connection %s set overlapping read locks\n", ret?"can":"cannot");
1689 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 40, 4, 0, WRITE_LOCK)) &&
1690 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 42, 4, 0, WRITE_LOCK));
1691 EXPECTED(ret, False);
1692 printf("a different pid %s set overlapping write locks\n", ret?"can":"cannot");
1694 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 50, 4, 0, READ_LOCK)) &&
1695 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 52, 4, 0, READ_LOCK));
1696 EXPECTED(ret, True);
1697 printf("a different pid %s set overlapping read locks\n", ret?"can":"cannot");
1699 ret = cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK) &&
1700 cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK);
1701 EXPECTED(ret, True);
1702 printf("the same process %s set the same read lock twice\n", ret?"can":"cannot");
1704 ret = cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK) &&
1705 cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK);
1706 EXPECTED(ret, False);
1707 printf("the same process %s set the same write lock twice\n", ret?"can":"cannot");
1709 ret = cli_lock(cli1, fnum1, 80, 4, 0, READ_LOCK) &&
1710 cli_lock(cli1, fnum1, 80, 4, 0, WRITE_LOCK);
1711 EXPECTED(ret, False);
1712 printf("the same process %s overlay a read lock with a write lock\n", ret?"can":"cannot");
1714 ret = cli_lock(cli1, fnum1, 90, 4, 0, WRITE_LOCK) &&
1715 cli_lock(cli1, fnum1, 90, 4, 0, READ_LOCK);
1716 EXPECTED(ret, True);
1717 printf("the same process %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1719 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 100, 4, 0, WRITE_LOCK)) &&
1720 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 100, 4, 0, READ_LOCK));
1721 EXPECTED(ret, False);
1722 printf("a different pid %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1724 ret = cli_lock(cli1, fnum1, 110, 4, 0, READ_LOCK) &&
1725 cli_lock(cli1, fnum1, 112, 4, 0, READ_LOCK) &&
1726 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 110, 6));
1727 EXPECTED(ret, False);
1728 printf("the same process %s coalesce read locks\n", ret?"can":"cannot");
1731 ret = cli_lock(cli1, fnum1, 120, 4, 0, WRITE_LOCK) &&
1732 (cli_read(cli2, fnum2, buf, 120, 4) == 4);
1733 EXPECTED(ret, False);
1734 printf("this server %s strict write locking\n", ret?"doesn't do":"does");
1736 ret = cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK) &&
1737 (cli_write(cli2, fnum2, 0, buf, 130, 4) == 4);
1738 EXPECTED(ret, False);
1739 printf("this server %s strict read locking\n", ret?"doesn't do":"does");
1742 ret = cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1743 cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1744 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4)) &&
1745 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4));
1746 EXPECTED(ret, True);
1747 printf("this server %s do recursive read locking\n", ret?"does":"doesn't");
1750 ret = cli_lock(cli1, fnum1, 150, 4, 0, WRITE_LOCK) &&
1751 cli_lock(cli1, fnum1, 150, 4, 0, READ_LOCK) &&
1752 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4)) &&
1753 (cli_read(cli2, fnum2, buf, 150, 4) == 4) &&
1754 !(cli_write(cli2, fnum2, 0, buf, 150, 4) == 4) &&
1755 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4));
1756 EXPECTED(ret, True);
1757 printf("this server %s do recursive lock overlays\n", ret?"does":"doesn't");
1759 ret = cli_lock(cli1, fnum1, 160, 4, 0, READ_LOCK) &&
1760 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 160, 4)) &&
1761 (cli_write(cli2, fnum2, 0, buf, 160, 4) == 4) &&
1762 (cli_read(cli2, fnum2, buf, 160, 4) == 4);
1763 EXPECTED(ret, True);
1764 printf("the same process %s remove a read lock using write locking\n", ret?"can":"cannot");
1766 ret = cli_lock(cli1, fnum1, 170, 4, 0, WRITE_LOCK) &&
1767 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 170, 4)) &&
1768 (cli_write(cli2, fnum2, 0, buf, 170, 4) == 4) &&
1769 (cli_read(cli2, fnum2, buf, 170, 4) == 4);
1770 EXPECTED(ret, True);
1771 printf("the same process %s remove a write lock using read locking\n", ret?"can":"cannot");
1773 ret = cli_lock(cli1, fnum1, 190, 4, 0, WRITE_LOCK) &&
1774 cli_lock(cli1, fnum1, 190, 4, 0, READ_LOCK) &&
1775 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 190, 4)) &&
1776 !(cli_write(cli2, fnum2, 0, buf, 190, 4) == 4) &&
1777 (cli_read(cli2, fnum2, buf, 190, 4) == 4);
1778 EXPECTED(ret, True);
1779 printf("the same process %s remove the first lock first\n", ret?"does":"doesn't");
1781 cli_close(cli1, fnum1);
1782 cli_close(cli2, fnum2);
1783 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
1784 cli_open(cli1, fname, O_RDWR, DENY_NONE, &f);
1785 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1786 cli_lock(cli1, f, 0, 1, 0, READ_LOCK) &&
1787 NT_STATUS_IS_OK(cli_close(cli1, fnum1)) &&
1788 NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1)) &&
1789 cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1790 cli_close(cli1, f);
1791 cli_close(cli1, fnum1);
1792 EXPECTED(ret, True);
1793 printf("the server %s have the NT byte range lock bug\n", !ret?"does":"doesn't");
1795 fail:
1796 cli_close(cli1, fnum1);
1797 cli_close(cli2, fnum2);
1798 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1799 torture_close_connection(cli1);
1800 torture_close_connection(cli2);
1802 printf("finished locktest4\n");
1803 return correct;
1807 looks at lock upgrade/downgrade.
1809 static bool run_locktest5(int dummy)
1811 static struct cli_state *cli1, *cli2;
1812 const char *fname = "\\lockt5.lck";
1813 uint16_t fnum1, fnum2, fnum3;
1814 bool ret;
1815 char buf[1000];
1816 bool correct = True;
1818 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1819 return False;
1822 cli_sockopt(cli1, sockops);
1823 cli_sockopt(cli2, sockops);
1825 printf("starting locktest5\n");
1827 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1829 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1830 cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
1831 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum3);
1833 memset(buf, 0, sizeof(buf));
1835 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1836 printf("Failed to create file\n");
1837 correct = False;
1838 goto fail;
1841 /* Check for NT bug... */
1842 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1843 cli_lock(cli1, fnum3, 0, 1, 0, READ_LOCK);
1844 cli_close(cli1, fnum1);
1845 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
1846 ret = cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1847 EXPECTED(ret, True);
1848 printf("this server %s the NT locking bug\n", ret ? "doesn't have" : "has");
1849 cli_close(cli1, fnum1);
1850 cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
1851 cli_unlock(cli1, fnum3, 0, 1);
1853 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1854 cli_lock(cli1, fnum1, 1, 1, 0, READ_LOCK);
1855 EXPECTED(ret, True);
1856 printf("the same process %s overlay a write with a read lock\n", ret?"can":"cannot");
1858 ret = cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
1859 EXPECTED(ret, False);
1861 printf("a different processs %s get a read lock on the first process lock stack\n", ret?"can":"cannot");
1863 /* Unlock the process 2 lock. */
1864 cli_unlock(cli2, fnum2, 0, 4);
1866 ret = cli_lock(cli1, fnum3, 0, 4, 0, READ_LOCK);
1867 EXPECTED(ret, False);
1869 printf("the same processs on a different fnum %s get a read lock\n", ret?"can":"cannot");
1871 /* Unlock the process 1 fnum3 lock. */
1872 cli_unlock(cli1, fnum3, 0, 4);
1874 /* Stack 2 more locks here. */
1875 ret = cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK) &&
1876 cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK);
1878 EXPECTED(ret, True);
1879 printf("the same process %s stack read locks\n", ret?"can":"cannot");
1881 /* Unlock the first process lock, then check this was the WRITE lock that was
1882 removed. */
1884 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4)) &&
1885 cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
1887 EXPECTED(ret, True);
1888 printf("the first unlock removes the %s lock\n", ret?"WRITE":"READ");
1890 /* Unlock the process 2 lock. */
1891 cli_unlock(cli2, fnum2, 0, 4);
1893 /* We should have 3 stacked locks here. Ensure we need to do 3 unlocks. */
1895 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 1, 1)) &&
1896 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4)) &&
1897 NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4));
1899 EXPECTED(ret, True);
1900 printf("the same process %s unlock the stack of 4 locks\n", ret?"can":"cannot");
1902 /* Ensure the next unlock fails. */
1903 ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4));
1904 EXPECTED(ret, False);
1905 printf("the same process %s count the lock stack\n", !ret?"can":"cannot");
1907 /* Ensure connection 2 can get a write lock. */
1908 ret = cli_lock(cli2, fnum2, 0, 4, 0, WRITE_LOCK);
1909 EXPECTED(ret, True);
1911 printf("a different processs %s get a write lock on the unlocked stack\n", ret?"can":"cannot");
1914 fail:
1915 cli_close(cli1, fnum1);
1916 cli_close(cli2, fnum2);
1917 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1918 if (!torture_close_connection(cli1)) {
1919 correct = False;
1921 if (!torture_close_connection(cli2)) {
1922 correct = False;
1925 printf("finished locktest5\n");
1927 return correct;
1931 tries the unusual lockingX locktype bits
1933 static bool run_locktest6(int dummy)
1935 static struct cli_state *cli;
1936 const char *fname[1] = { "\\lock6.txt" };
1937 int i;
1938 uint16_t fnum;
1939 NTSTATUS status;
1941 if (!torture_open_connection(&cli, 0)) {
1942 return False;
1945 cli_sockopt(cli, sockops);
1947 printf("starting locktest6\n");
1949 for (i=0;i<1;i++) {
1950 printf("Testing %s\n", fname[i]);
1952 cli_unlink(cli, fname[i], aSYSTEM | aHIDDEN);
1954 cli_open(cli, fname[i], O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
1955 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CHANGE_LOCKTYPE);
1956 cli_close(cli, fnum);
1957 printf("CHANGE_LOCKTYPE gave %s\n", nt_errstr(status));
1959 cli_open(cli, fname[i], O_RDWR, DENY_NONE, &fnum);
1960 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CANCEL_LOCK);
1961 cli_close(cli, fnum);
1962 printf("CANCEL_LOCK gave %s\n", nt_errstr(status));
1964 cli_unlink(cli, fname[i], aSYSTEM | aHIDDEN);
1967 torture_close_connection(cli);
1969 printf("finished locktest6\n");
1970 return True;
1973 static bool run_locktest7(int dummy)
1975 struct cli_state *cli1;
1976 const char *fname = "\\lockt7.lck";
1977 uint16_t fnum1;
1978 char buf[200];
1979 bool correct = False;
1981 if (!torture_open_connection(&cli1, 0)) {
1982 return False;
1985 cli_sockopt(cli1, sockops);
1987 printf("starting locktest7\n");
1989 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
1991 cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
1993 memset(buf, 0, sizeof(buf));
1995 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1996 printf("Failed to create file\n");
1997 goto fail;
2000 cli_setpid(cli1, 1);
2002 if (!cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK)) {
2003 printf("Unable to apply read lock on range 130:4, error was %s\n", cli_errstr(cli1));
2004 goto fail;
2005 } else {
2006 printf("pid1 successfully locked range 130:4 for READ\n");
2009 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2010 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2011 goto fail;
2012 } else {
2013 printf("pid1 successfully read the range 130:4\n");
2016 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2017 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2018 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2019 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2020 goto fail;
2022 } else {
2023 printf("pid1 successfully wrote to the range 130:4 (should be denied)\n");
2024 goto fail;
2027 cli_setpid(cli1, 2);
2029 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2030 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2031 } else {
2032 printf("pid2 successfully read the range 130:4\n");
2035 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2036 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2037 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2038 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2039 goto fail;
2041 } else {
2042 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2043 goto fail;
2046 cli_setpid(cli1, 1);
2047 cli_unlock(cli1, fnum1, 130, 4);
2049 if (!cli_lock(cli1, fnum1, 130, 4, 0, WRITE_LOCK)) {
2050 printf("Unable to apply write lock on range 130:4, error was %s\n", cli_errstr(cli1));
2051 goto fail;
2052 } else {
2053 printf("pid1 successfully locked range 130:4 for WRITE\n");
2056 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2057 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2058 goto fail;
2059 } else {
2060 printf("pid1 successfully read the range 130:4\n");
2063 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2064 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2065 goto fail;
2066 } else {
2067 printf("pid1 successfully wrote to the range 130:4\n");
2070 cli_setpid(cli1, 2);
2072 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2073 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2074 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2075 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2076 goto fail;
2078 } else {
2079 printf("pid2 successfully read the range 130:4 (should be denied)\n");
2080 goto fail;
2083 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2084 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2085 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2086 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2087 goto fail;
2089 } else {
2090 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2091 goto fail;
2094 cli_unlock(cli1, fnum1, 130, 0);
2095 correct = True;
2097 fail:
2098 cli_close(cli1, fnum1);
2099 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2100 torture_close_connection(cli1);
2102 printf("finished locktest7\n");
2103 return correct;
2107 * This demonstrates a problem with our use of GPFS share modes: A file
2108 * descriptor sitting in the pending close queue holding a GPFS share mode
2109 * blocks opening a file another time. Happens with Word 2007 temp files.
2110 * With "posix locking = yes" and "gpfs:sharemodes = yes" enabled, the third
2111 * open is denied with NT_STATUS_SHARING_VIOLATION.
2114 static bool run_locktest8(int dummy)
2116 struct cli_state *cli1;
2117 const char *fname = "\\lockt8.lck";
2118 uint16_t fnum1, fnum2;
2119 char buf[200];
2120 bool correct = False;
2121 NTSTATUS status;
2123 if (!torture_open_connection(&cli1, 0)) {
2124 return False;
2127 cli_sockopt(cli1, sockops);
2129 printf("starting locktest8\n");
2131 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2133 status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_WRITE,
2134 &fnum1);
2135 if (!NT_STATUS_IS_OK(status)) {
2136 d_fprintf(stderr, "cli_open returned %s\n", cli_errstr(cli1));
2137 return false;
2140 memset(buf, 0, sizeof(buf));
2142 status = cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum2);
2143 if (!NT_STATUS_IS_OK(status)) {
2144 d_fprintf(stderr, "cli_open second time returned %s\n",
2145 cli_errstr(cli1));
2146 goto fail;
2149 if (!cli_lock(cli1, fnum2, 1, 1, 0, READ_LOCK)) {
2150 printf("Unable to apply read lock on range 1:1, error was "
2151 "%s\n", cli_errstr(cli1));
2152 goto fail;
2155 status = cli_close(cli1, fnum1);
2156 if (!NT_STATUS_IS_OK(status)) {
2157 d_fprintf(stderr, "cli_close(fnum1) %s\n", cli_errstr(cli1));
2158 goto fail;
2161 status = cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
2162 if (!NT_STATUS_IS_OK(status)) {
2163 d_fprintf(stderr, "cli_open third time returned %s\n",
2164 cli_errstr(cli1));
2165 goto fail;
2168 correct = true;
2170 fail:
2171 cli_close(cli1, fnum1);
2172 cli_close(cli1, fnum2);
2173 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2174 torture_close_connection(cli1);
2176 printf("finished locktest8\n");
2177 return correct;
2181 * This test is designed to be run in conjunction with
2182 * external NFS or POSIX locks taken in the filesystem.
2183 * It checks that the smbd server will block until the
2184 * lock is released and then acquire it. JRA.
2187 static bool got_alarm;
2188 static int alarm_fd;
2190 static void alarm_handler(int dummy)
2192 got_alarm = True;
2195 static void alarm_handler_parent(int dummy)
2197 close(alarm_fd);
2200 static void do_local_lock(int read_fd, int write_fd)
2202 int fd;
2203 char c = '\0';
2204 struct flock lock;
2205 const char *local_pathname = NULL;
2206 int ret;
2208 local_pathname = talloc_asprintf(talloc_tos(),
2209 "%s/lockt9.lck", local_path);
2210 if (!local_pathname) {
2211 printf("child: alloc fail\n");
2212 exit(1);
2215 unlink(local_pathname);
2216 fd = open(local_pathname, O_RDWR|O_CREAT, 0666);
2217 if (fd == -1) {
2218 printf("child: open of %s failed %s.\n",
2219 local_pathname, strerror(errno));
2220 exit(1);
2223 /* Now take a fcntl lock. */
2224 lock.l_type = F_WRLCK;
2225 lock.l_whence = SEEK_SET;
2226 lock.l_start = 0;
2227 lock.l_len = 4;
2228 lock.l_pid = getpid();
2230 ret = fcntl(fd,F_SETLK,&lock);
2231 if (ret == -1) {
2232 printf("child: failed to get lock 0:4 on file %s. Error %s\n",
2233 local_pathname, strerror(errno));
2234 exit(1);
2235 } else {
2236 printf("child: got lock 0:4 on file %s.\n",
2237 local_pathname );
2238 fflush(stdout);
2241 CatchSignal(SIGALRM, alarm_handler);
2242 alarm(5);
2243 /* Signal the parent. */
2244 if (write(write_fd, &c, 1) != 1) {
2245 printf("child: start signal fail %s.\n",
2246 strerror(errno));
2247 exit(1);
2249 alarm(0);
2251 alarm(10);
2252 /* Wait for the parent to be ready. */
2253 if (read(read_fd, &c, 1) != 1) {
2254 printf("child: reply signal fail %s.\n",
2255 strerror(errno));
2256 exit(1);
2258 alarm(0);
2260 sleep(5);
2261 close(fd);
2262 printf("child: released lock 0:4 on file %s.\n",
2263 local_pathname );
2264 fflush(stdout);
2265 exit(0);
2268 static bool run_locktest9(int dummy)
2270 struct cli_state *cli1;
2271 const char *fname = "\\lockt9.lck";
2272 uint16_t fnum;
2273 bool correct = False;
2274 int pipe_in[2], pipe_out[2];
2275 pid_t child_pid;
2276 char c = '\0';
2277 int ret;
2278 double seconds;
2279 NTSTATUS status;
2281 printf("starting locktest9\n");
2283 if (local_path == NULL) {
2284 d_fprintf(stderr, "locktest9 must be given a local path via -l <localpath>\n");
2285 return false;
2288 if (pipe(pipe_in) == -1 || pipe(pipe_out) == -1) {
2289 return false;
2292 child_pid = fork();
2293 if (child_pid == -1) {
2294 return false;
2297 if (child_pid == 0) {
2298 /* Child. */
2299 do_local_lock(pipe_out[0], pipe_in[1]);
2300 exit(0);
2303 close(pipe_out[0]);
2304 close(pipe_in[1]);
2305 pipe_out[0] = -1;
2306 pipe_in[1] = -1;
2308 /* Parent. */
2309 ret = read(pipe_in[0], &c, 1);
2310 if (ret != 1) {
2311 d_fprintf(stderr, "failed to read start signal from child. %s\n",
2312 strerror(errno));
2313 return false;
2316 if (!torture_open_connection(&cli1, 0)) {
2317 return false;
2320 cli_sockopt(cli1, sockops);
2322 status = cli_open(cli1, fname, O_RDWR, DENY_NONE,
2323 &fnum);
2324 if (!NT_STATUS_IS_OK(status)) {
2325 d_fprintf(stderr, "cli_open returned %s\n", cli_errstr(cli1));
2326 return false;
2329 /* Ensure the child has the lock. */
2330 if (cli_lock(cli1, fnum, 0, 4, 0, WRITE_LOCK)) {
2331 d_fprintf(stderr, "Got the lock on range 0:4 - this should not happen !\n");
2332 goto fail;
2333 } else {
2334 d_printf("Child has the lock.\n");
2337 /* Tell the child to wait 5 seconds then exit. */
2338 ret = write(pipe_out[1], &c, 1);
2339 if (ret != 1) {
2340 d_fprintf(stderr, "failed to send exit signal to child. %s\n",
2341 strerror(errno));
2342 goto fail;
2345 /* Wait 20 seconds for the lock. */
2346 alarm_fd = cli1->fd;
2347 CatchSignal(SIGALRM, alarm_handler_parent);
2348 alarm(20);
2350 start_timer();
2352 if (!cli_lock(cli1, fnum, 0, 4, -1, WRITE_LOCK)) {
2353 d_fprintf(stderr, "Unable to apply write lock on range 0:4, error was "
2354 "%s\n", cli_errstr(cli1));
2355 goto fail_nofd;
2357 alarm(0);
2359 seconds = end_timer();
2361 printf("Parent got the lock after %.2f seconds.\n",
2362 seconds);
2364 status = cli_close(cli1, fnum);
2365 if (!NT_STATUS_IS_OK(status)) {
2366 d_fprintf(stderr, "cli_close(fnum1) %s\n", cli_errstr(cli1));
2367 goto fail;
2370 correct = true;
2372 fail:
2373 cli_close(cli1, fnum);
2374 torture_close_connection(cli1);
2376 fail_nofd:
2378 printf("finished locktest9\n");
2379 return correct;
2383 test whether fnums and tids open on one VC are available on another (a major
2384 security hole)
2386 static bool run_fdpasstest(int dummy)
2388 struct cli_state *cli1, *cli2;
2389 const char *fname = "\\fdpass.tst";
2390 uint16_t fnum1;
2391 char buf[1024];
2393 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
2394 return False;
2396 cli_sockopt(cli1, sockops);
2397 cli_sockopt(cli2, sockops);
2399 printf("starting fdpasstest\n");
2401 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2403 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
2404 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2405 return False;
2408 if (cli_write(cli1, fnum1, 0, "hello world\n", 0, 13) != 13) {
2409 printf("write failed (%s)\n", cli_errstr(cli1));
2410 return False;
2413 cli2->vuid = cli1->vuid;
2414 cli2->cnum = cli1->cnum;
2415 cli2->pid = cli1->pid;
2417 if (cli_read(cli2, fnum1, buf, 0, 13) == 13) {
2418 printf("read succeeded! nasty security hole [%s]\n",
2419 buf);
2420 return False;
2423 cli_close(cli1, fnum1);
2424 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
2426 torture_close_connection(cli1);
2427 torture_close_connection(cli2);
2429 printf("finished fdpasstest\n");
2430 return True;
2433 static bool run_fdsesstest(int dummy)
2435 struct cli_state *cli;
2436 uint16 new_vuid;
2437 uint16 saved_vuid;
2438 uint16 new_cnum;
2439 uint16 saved_cnum;
2440 const char *fname = "\\fdsess.tst";
2441 const char *fname1 = "\\fdsess1.tst";
2442 uint16_t fnum1;
2443 uint16_t fnum2;
2444 char buf[1024];
2445 bool ret = True;
2447 if (!torture_open_connection(&cli, 0))
2448 return False;
2449 cli_sockopt(cli, sockops);
2451 if (!torture_cli_session_setup2(cli, &new_vuid))
2452 return False;
2454 saved_cnum = cli->cnum;
2455 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, share, "?????", "", 1)))
2456 return False;
2457 new_cnum = cli->cnum;
2458 cli->cnum = saved_cnum;
2460 printf("starting fdsesstest\n");
2462 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2463 cli_unlink(cli, fname1, aSYSTEM | aHIDDEN);
2465 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
2466 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2467 return False;
2470 if (cli_write(cli, fnum1, 0, "hello world\n", 0, 13) != 13) {
2471 printf("write failed (%s)\n", cli_errstr(cli));
2472 return False;
2475 saved_vuid = cli->vuid;
2476 cli->vuid = new_vuid;
2478 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2479 printf("read succeeded with different vuid! nasty security hole [%s]\n",
2480 buf);
2481 ret = False;
2483 /* Try to open a file with different vuid, samba cnum. */
2484 if (NT_STATUS_IS_OK(cli_open(cli, fname1, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum2))) {
2485 printf("create with different vuid, same cnum succeeded.\n");
2486 cli_close(cli, fnum2);
2487 cli_unlink(cli, fname1, aSYSTEM | aHIDDEN);
2488 } else {
2489 printf("create with different vuid, same cnum failed.\n");
2490 printf("This will cause problems with service clients.\n");
2491 ret = False;
2494 cli->vuid = saved_vuid;
2496 /* Try with same vuid, different cnum. */
2497 cli->cnum = new_cnum;
2499 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2500 printf("read succeeded with different cnum![%s]\n",
2501 buf);
2502 ret = False;
2505 cli->cnum = saved_cnum;
2506 cli_close(cli, fnum1);
2507 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2509 torture_close_connection(cli);
2511 printf("finished fdsesstest\n");
2512 return ret;
2516 This test checks that
2518 1) the server does not allow an unlink on a file that is open
2520 static bool run_unlinktest(int dummy)
2522 struct cli_state *cli;
2523 const char *fname = "\\unlink.tst";
2524 uint16_t fnum;
2525 bool correct = True;
2527 if (!torture_open_connection(&cli, 0)) {
2528 return False;
2531 cli_sockopt(cli, sockops);
2533 printf("starting unlink test\n");
2535 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2537 cli_setpid(cli, 1);
2539 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
2540 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2541 return False;
2544 if (NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
2545 printf("error: server allowed unlink on an open file\n");
2546 correct = False;
2547 } else {
2548 correct = check_error(__LINE__, cli, ERRDOS, ERRbadshare,
2549 NT_STATUS_SHARING_VIOLATION);
2552 cli_close(cli, fnum);
2553 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2555 if (!torture_close_connection(cli)) {
2556 correct = False;
2559 printf("unlink test finished\n");
2561 return correct;
2566 test how many open files this server supports on the one socket
2568 static bool run_maxfidtest(int dummy)
2570 struct cli_state *cli;
2571 const char *ftemplate = "\\maxfid.%d.%d";
2572 fstring fname;
2573 uint16_t fnums[0x11000];
2574 int i;
2575 int retries=4;
2576 bool correct = True;
2578 cli = current_cli;
2580 if (retries <= 0) {
2581 printf("failed to connect\n");
2582 return False;
2585 cli_sockopt(cli, sockops);
2587 for (i=0; i<0x11000; i++) {
2588 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2589 if (!NT_STATUS_IS_OK(cli_open(cli, fname,
2590 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnums[i]))) {
2591 printf("open of %s failed (%s)\n",
2592 fname, cli_errstr(cli));
2593 printf("maximum fnum is %d\n", i);
2594 break;
2596 printf("%6d\r", i);
2598 printf("%6d\n", i);
2599 i--;
2601 printf("cleaning up\n");
2602 for (;i>=0;i--) {
2603 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2604 cli_close(cli, fnums[i]);
2605 if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
2606 printf("unlink of %s failed (%s)\n",
2607 fname, cli_errstr(cli));
2608 correct = False;
2610 printf("%6d\r", i);
2612 printf("%6d\n", 0);
2614 printf("maxfid test finished\n");
2615 if (!torture_close_connection(cli)) {
2616 correct = False;
2618 return correct;
2621 /* generate a random buffer */
2622 static void rand_buf(char *buf, int len)
2624 while (len--) {
2625 *buf = (char)sys_random();
2626 buf++;
2630 /* send smb negprot commands, not reading the response */
2631 static bool run_negprot_nowait(int dummy)
2633 int i;
2634 static struct cli_state *cli;
2635 bool correct = True;
2637 printf("starting negprot nowait test\n");
2639 if (!(cli = open_nbt_connection())) {
2640 return False;
2643 for (i=0;i<50000;i++) {
2644 cli_negprot_sendsync(cli);
2647 if (!torture_close_connection(cli)) {
2648 correct = False;
2651 printf("finished negprot nowait test\n");
2653 return correct;
2657 /* send random IPC commands */
2658 static bool run_randomipc(int dummy)
2660 char *rparam = NULL;
2661 char *rdata = NULL;
2662 unsigned int rdrcnt,rprcnt;
2663 char param[1024];
2664 int api, param_len, i;
2665 struct cli_state *cli;
2666 bool correct = True;
2667 int count = 50000;
2669 printf("starting random ipc test\n");
2671 if (!torture_open_connection(&cli, 0)) {
2672 return False;
2675 for (i=0;i<count;i++) {
2676 api = sys_random() % 500;
2677 param_len = (sys_random() % 64);
2679 rand_buf(param, param_len);
2681 SSVAL(param,0,api);
2683 cli_api(cli,
2684 param, param_len, 8,
2685 NULL, 0, BUFFER_SIZE,
2686 &rparam, &rprcnt,
2687 &rdata, &rdrcnt);
2688 if (i % 100 == 0) {
2689 printf("%d/%d\r", i,count);
2692 printf("%d/%d\n", i, count);
2694 if (!torture_close_connection(cli)) {
2695 correct = False;
2698 printf("finished random ipc test\n");
2700 return correct;
2705 static void browse_callback(const char *sname, uint32 stype,
2706 const char *comment, void *state)
2708 printf("\t%20.20s %08x %s\n", sname, stype, comment);
2714 This test checks the browse list code
2717 static bool run_browsetest(int dummy)
2719 static struct cli_state *cli;
2720 bool correct = True;
2722 printf("starting browse test\n");
2724 if (!torture_open_connection(&cli, 0)) {
2725 return False;
2728 printf("domain list:\n");
2729 cli_NetServerEnum(cli, cli->server_domain,
2730 SV_TYPE_DOMAIN_ENUM,
2731 browse_callback, NULL);
2733 printf("machine list:\n");
2734 cli_NetServerEnum(cli, cli->server_domain,
2735 SV_TYPE_ALL,
2736 browse_callback, NULL);
2738 if (!torture_close_connection(cli)) {
2739 correct = False;
2742 printf("browse test finished\n");
2744 return correct;
2750 This checks how the getatr calls works
2752 static bool run_attrtest(int dummy)
2754 struct cli_state *cli;
2755 uint16_t fnum;
2756 time_t t, t2;
2757 const char *fname = "\\attrib123456789.tst";
2758 bool correct = True;
2760 printf("starting attrib test\n");
2762 if (!torture_open_connection(&cli, 0)) {
2763 return False;
2766 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2767 cli_open(cli, fname,
2768 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
2769 cli_close(cli, fnum);
2770 if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
2771 printf("getatr failed (%s)\n", cli_errstr(cli));
2772 correct = False;
2775 if (abs(t - time(NULL)) > 60*60*24*10) {
2776 printf("ERROR: SMBgetatr bug. time is %s",
2777 ctime(&t));
2778 t = time(NULL);
2779 correct = True;
2782 t2 = t-60*60*24; /* 1 day ago */
2784 if (!NT_STATUS_IS_OK(cli_setatr(cli, fname, 0, t2))) {
2785 printf("setatr failed (%s)\n", cli_errstr(cli));
2786 correct = True;
2789 if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
2790 printf("getatr failed (%s)\n", cli_errstr(cli));
2791 correct = True;
2794 if (t != t2) {
2795 printf("ERROR: getatr/setatr bug. times are\n%s",
2796 ctime(&t));
2797 printf("%s", ctime(&t2));
2798 correct = True;
2801 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2803 if (!torture_close_connection(cli)) {
2804 correct = False;
2807 printf("attrib test finished\n");
2809 return correct;
2814 This checks a couple of trans2 calls
2816 static bool run_trans2test(int dummy)
2818 struct cli_state *cli;
2819 uint16_t fnum;
2820 SMB_OFF_T size;
2821 time_t c_time, a_time, m_time;
2822 struct timespec c_time_ts, a_time_ts, m_time_ts, w_time_ts, m_time2_ts;
2823 const char *fname = "\\trans2.tst";
2824 const char *dname = "\\trans2";
2825 const char *fname2 = "\\trans2\\trans2.tst";
2826 char pname[1024];
2827 bool correct = True;
2829 printf("starting trans2 test\n");
2831 if (!torture_open_connection(&cli, 0)) {
2832 return False;
2835 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2836 cli_open(cli, fname,
2837 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
2838 if (!cli_qfileinfo(cli, fnum, NULL, &size, &c_time_ts, &a_time_ts, &w_time_ts,
2839 &m_time_ts, NULL)) {
2840 printf("ERROR: qfileinfo failed (%s)\n", cli_errstr(cli));
2841 correct = False;
2844 if (!cli_qfilename(cli, fnum, pname, sizeof(pname))) {
2845 printf("ERROR: qfilename failed (%s)\n", cli_errstr(cli));
2846 correct = False;
2849 if (strcmp(pname, fname)) {
2850 printf("qfilename gave different name? [%s] [%s]\n",
2851 fname, pname);
2852 correct = False;
2855 cli_close(cli, fnum);
2857 sleep(2);
2859 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2860 if (!NT_STATUS_IS_OK(cli_open(cli, fname,
2861 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum))) {
2862 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2863 return False;
2865 cli_close(cli, fnum);
2867 if (!cli_qpathinfo(cli, fname, &c_time, &a_time, &m_time, &size, NULL)) {
2868 printf("ERROR: qpathinfo failed (%s)\n", cli_errstr(cli));
2869 correct = False;
2870 } else {
2871 if (c_time != m_time) {
2872 printf("create time=%s", ctime(&c_time));
2873 printf("modify time=%s", ctime(&m_time));
2874 printf("This system appears to have sticky create times\n");
2876 if (a_time % (60*60) == 0) {
2877 printf("access time=%s", ctime(&a_time));
2878 printf("This system appears to set a midnight access time\n");
2879 correct = False;
2882 if (abs(m_time - time(NULL)) > 60*60*24*7) {
2883 printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
2884 correct = False;
2889 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2890 cli_open(cli, fname,
2891 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
2892 cli_close(cli, fnum);
2893 if (!cli_qpathinfo2(cli, fname, &c_time_ts, &a_time_ts, &w_time_ts,
2894 &m_time_ts, &size, NULL, NULL)) {
2895 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2896 correct = False;
2897 } else {
2898 if (w_time_ts.tv_sec < 60*60*24*2) {
2899 printf("write time=%s", ctime(&w_time_ts.tv_sec));
2900 printf("This system appears to set a initial 0 write time\n");
2901 correct = False;
2905 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
2908 /* check if the server updates the directory modification time
2909 when creating a new file */
2910 if (!NT_STATUS_IS_OK(cli_mkdir(cli, dname))) {
2911 printf("ERROR: mkdir failed (%s)\n", cli_errstr(cli));
2912 correct = False;
2914 sleep(3);
2915 if (!cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts, &w_time_ts,
2916 &m_time_ts, &size, NULL, NULL)) {
2917 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2918 correct = False;
2921 cli_open(cli, fname2,
2922 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
2923 cli_write(cli, fnum, 0, (char *)&fnum, 0, sizeof(fnum));
2924 cli_close(cli, fnum);
2925 if (!cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts, &w_time_ts,
2926 &m_time2_ts, &size, NULL, NULL)) {
2927 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2928 correct = False;
2929 } else {
2930 if (memcmp(&m_time_ts, &m_time2_ts, sizeof(struct timespec))
2931 == 0) {
2932 printf("This system does not update directory modification times\n");
2933 correct = False;
2936 cli_unlink(cli, fname2, aSYSTEM | aHIDDEN);
2937 cli_rmdir(cli, dname);
2939 if (!torture_close_connection(cli)) {
2940 correct = False;
2943 printf("trans2 test finished\n");
2945 return correct;
2949 This checks new W2K calls.
2952 static bool new_trans(struct cli_state *pcli, int fnum, int level)
2954 char *buf = NULL;
2955 uint32 len;
2956 bool correct = True;
2958 if (!cli_qfileinfo_test(pcli, fnum, level, &buf, &len)) {
2959 printf("ERROR: qfileinfo (%d) failed (%s)\n", level, cli_errstr(pcli));
2960 correct = False;
2961 } else {
2962 printf("qfileinfo: level %d, len = %u\n", level, len);
2963 dump_data(0, (uint8 *)buf, len);
2964 printf("\n");
2966 SAFE_FREE(buf);
2967 return correct;
2970 static bool run_w2ktest(int dummy)
2972 struct cli_state *cli;
2973 uint16_t fnum;
2974 const char *fname = "\\w2ktest\\w2k.tst";
2975 int level;
2976 bool correct = True;
2978 printf("starting w2k test\n");
2980 if (!torture_open_connection(&cli, 0)) {
2981 return False;
2984 cli_open(cli, fname,
2985 O_RDWR | O_CREAT , DENY_NONE, &fnum);
2987 for (level = 1004; level < 1040; level++) {
2988 new_trans(cli, fnum, level);
2991 cli_close(cli, fnum);
2993 if (!torture_close_connection(cli)) {
2994 correct = False;
2997 printf("w2k test finished\n");
2999 return correct;
3004 this is a harness for some oplock tests
3006 static bool run_oplock1(int dummy)
3008 struct cli_state *cli1;
3009 const char *fname = "\\lockt1.lck";
3010 uint16_t fnum1;
3011 bool correct = True;
3013 printf("starting oplock test 1\n");
3015 if (!torture_open_connection(&cli1, 0)) {
3016 return False;
3019 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3021 cli_sockopt(cli1, sockops);
3023 cli1->use_oplocks = True;
3025 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
3026 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3027 return False;
3030 cli1->use_oplocks = False;
3032 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3033 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3035 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3036 printf("close2 failed (%s)\n", cli_errstr(cli1));
3037 return False;
3040 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
3041 printf("unlink failed (%s)\n", cli_errstr(cli1));
3042 return False;
3045 if (!torture_close_connection(cli1)) {
3046 correct = False;
3049 printf("finished oplock test 1\n");
3051 return correct;
3054 static bool run_oplock2(int dummy)
3056 struct cli_state *cli1, *cli2;
3057 const char *fname = "\\lockt2.lck";
3058 uint16_t fnum1, fnum2;
3059 int saved_use_oplocks = use_oplocks;
3060 char buf[4];
3061 bool correct = True;
3062 volatile bool *shared_correct;
3064 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
3065 *shared_correct = True;
3067 use_level_II_oplocks = True;
3068 use_oplocks = True;
3070 printf("starting oplock test 2\n");
3072 if (!torture_open_connection(&cli1, 0)) {
3073 use_level_II_oplocks = False;
3074 use_oplocks = saved_use_oplocks;
3075 return False;
3078 cli1->use_oplocks = True;
3079 cli1->use_level_II_oplocks = True;
3081 if (!torture_open_connection(&cli2, 1)) {
3082 use_level_II_oplocks = False;
3083 use_oplocks = saved_use_oplocks;
3084 return False;
3087 cli2->use_oplocks = True;
3088 cli2->use_level_II_oplocks = True;
3090 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3092 cli_sockopt(cli1, sockops);
3093 cli_sockopt(cli2, sockops);
3095 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
3096 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
3097 return False;
3100 /* Don't need the globals any more. */
3101 use_level_II_oplocks = False;
3102 use_oplocks = saved_use_oplocks;
3104 if (fork() == 0) {
3105 /* Child code */
3106 if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2))) {
3107 printf("second open of %s failed (%s)\n", fname, cli_errstr(cli1));
3108 *shared_correct = False;
3109 exit(0);
3112 sleep(2);
3114 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
3115 printf("close2 failed (%s)\n", cli_errstr(cli1));
3116 *shared_correct = False;
3119 exit(0);
3122 sleep(2);
3124 /* Ensure cli1 processes the break. Empty file should always return 0
3125 * bytes. */
3127 if (cli_read(cli1, fnum1, buf, 0, 4) != 0) {
3128 printf("read on fnum1 failed (%s)\n", cli_errstr(cli1));
3129 correct = False;
3132 /* Should now be at level II. */
3133 /* Test if sending a write locks causes a break to none. */
3135 if (!cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK)) {
3136 printf("lock failed (%s)\n", cli_errstr(cli1));
3137 correct = False;
3140 cli_unlock(cli1, fnum1, 0, 4);
3142 sleep(2);
3144 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
3145 printf("lock failed (%s)\n", cli_errstr(cli1));
3146 correct = False;
3149 cli_unlock(cli1, fnum1, 0, 4);
3151 sleep(2);
3153 cli_read(cli1, fnum1, buf, 0, 4);
3155 #if 0
3156 if (cli_write(cli1, fnum1, 0, buf, 0, 4) != 4) {
3157 printf("write on fnum1 failed (%s)\n", cli_errstr(cli1));
3158 correct = False;
3160 #endif
3162 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3163 printf("close1 failed (%s)\n", cli_errstr(cli1));
3164 correct = False;
3167 sleep(4);
3169 if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
3170 printf("unlink failed (%s)\n", cli_errstr(cli1));
3171 correct = False;
3174 if (!torture_close_connection(cli1)) {
3175 correct = False;
3178 if (!*shared_correct) {
3179 correct = False;
3182 printf("finished oplock test 2\n");
3184 return correct;
3187 /* handler for oplock 3 tests */
3188 static NTSTATUS oplock3_handler(struct cli_state *cli, uint16_t fnum, unsigned char level)
3190 printf("got oplock break fnum=%d level=%d\n",
3191 fnum, level);
3192 return cli_oplock_ack(cli, fnum, level);
3195 static bool run_oplock3(int dummy)
3197 struct cli_state *cli;
3198 const char *fname = "\\oplockt3.dat";
3199 uint16_t fnum;
3200 char buf[4] = "abcd";
3201 bool correct = True;
3202 volatile bool *shared_correct;
3204 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
3205 *shared_correct = True;
3207 printf("starting oplock test 3\n");
3209 if (fork() == 0) {
3210 /* Child code */
3211 use_oplocks = True;
3212 use_level_II_oplocks = True;
3213 if (!torture_open_connection(&cli, 0)) {
3214 *shared_correct = False;
3215 exit(0);
3217 sleep(2);
3218 /* try to trigger a oplock break in parent */
3219 cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum);
3220 cli_write(cli, fnum, 0, buf, 0, 4);
3221 exit(0);
3224 /* parent code */
3225 use_oplocks = True;
3226 use_level_II_oplocks = True;
3227 if (!torture_open_connection(&cli, 1)) { /* other is forked */
3228 return False;
3230 cli_oplock_handler(cli, oplock3_handler);
3231 cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
3232 cli_write(cli, fnum, 0, buf, 0, 4);
3233 cli_close(cli, fnum);
3234 cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum);
3235 cli->timeout = 20000;
3236 cli_receive_smb(cli);
3237 printf("finished oplock test 3\n");
3239 return (correct && *shared_correct);
3241 /* What are we looking for here? What's sucess and what's FAILURE? */
3247 Test delete on close semantics.
3249 static bool run_deletetest(int dummy)
3251 struct cli_state *cli1 = NULL;
3252 struct cli_state *cli2 = NULL;
3253 const char *fname = "\\delete.file";
3254 uint16_t fnum1 = (uint16_t)-1;
3255 uint16_t fnum2 = (uint16_t)-1;
3256 bool correct = True;
3258 printf("starting delete test\n");
3260 if (!torture_open_connection(&cli1, 0)) {
3261 return False;
3264 cli_sockopt(cli1, sockops);
3266 /* Test 1 - this should delete the file on close. */
3268 cli_setatr(cli1, fname, 0, 0);
3269 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3271 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
3272 0, FILE_OVERWRITE_IF,
3273 FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3274 printf("[1] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3275 correct = False;
3276 goto fail;
3279 #if 0 /* JRATEST */
3281 uint32 *accinfo = NULL;
3282 uint32 len;
3283 cli_qfileinfo_test(cli1, fnum1, SMB_FILE_ACCESS_INFORMATION, (char **)&accinfo, &len);
3284 if (accinfo)
3285 printf("access mode = 0x%lx\n", *accinfo);
3286 SAFE_FREE(accinfo);
3288 #endif
3290 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3291 printf("[1] close failed (%s)\n", cli_errstr(cli1));
3292 correct = False;
3293 goto fail;
3296 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
3297 printf("[1] open of %s succeeded (should fail)\n", fname);
3298 correct = False;
3299 goto fail;
3302 printf("first delete on close test succeeded.\n");
3304 /* Test 2 - this should delete the file on close. */
3306 cli_setatr(cli1, fname, 0, 0);
3307 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3309 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
3310 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
3311 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3312 printf("[2] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3313 correct = False;
3314 goto fail;
3317 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3318 printf("[2] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3319 correct = False;
3320 goto fail;
3323 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3324 printf("[2] close failed (%s)\n", cli_errstr(cli1));
3325 correct = False;
3326 goto fail;
3329 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3330 printf("[2] open of %s succeeded should have been deleted on close !\n", fname);
3331 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3332 printf("[2] close failed (%s)\n", cli_errstr(cli1));
3333 correct = False;
3334 goto fail;
3336 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3337 } else
3338 printf("second delete on close test succeeded.\n");
3340 /* Test 3 - ... */
3341 cli_setatr(cli1, fname, 0, 0);
3342 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3344 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
3345 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3346 printf("[3] open - 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3347 correct = False;
3348 goto fail;
3351 /* This should fail with a sharing violation - open for delete is only compatible
3352 with SHARE_DELETE. */
3354 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3355 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0, 0, &fnum2))) {
3356 printf("[3] open - 2 of %s succeeded - should have failed.\n", fname);
3357 correct = False;
3358 goto fail;
3361 /* This should succeed. */
3363 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3364 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0, &fnum2))) {
3365 printf("[3] open - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3366 correct = False;
3367 goto fail;
3370 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3371 printf("[3] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3372 correct = False;
3373 goto fail;
3376 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3377 printf("[3] close 1 failed (%s)\n", cli_errstr(cli1));
3378 correct = False;
3379 goto fail;
3382 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
3383 printf("[3] close 2 failed (%s)\n", cli_errstr(cli1));
3384 correct = False;
3385 goto fail;
3388 /* This should fail - file should no longer be there. */
3390 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3391 printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
3392 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3393 printf("[3] close failed (%s)\n", cli_errstr(cli1));
3395 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3396 correct = False;
3397 goto fail;
3398 } else
3399 printf("third delete on close test succeeded.\n");
3401 /* Test 4 ... */
3402 cli_setatr(cli1, fname, 0, 0);
3403 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3405 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3406 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3407 printf("[4] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3408 correct = False;
3409 goto fail;
3412 /* This should succeed. */
3413 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3414 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0, &fnum2))) {
3415 printf("[4] open - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3416 correct = False;
3417 goto fail;
3420 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
3421 printf("[4] close - 1 failed (%s)\n", cli_errstr(cli1));
3422 correct = False;
3423 goto fail;
3426 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3427 printf("[4] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3428 correct = False;
3429 goto fail;
3432 /* This should fail - no more opens once delete on close set. */
3433 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
3434 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3435 FILE_OPEN, 0, 0, &fnum2))) {
3436 printf("[4] open - 3 of %s succeeded ! Should have failed.\n", fname );
3437 correct = False;
3438 goto fail;
3439 } else
3440 printf("fourth delete on close test succeeded.\n");
3442 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3443 printf("[4] close - 2 failed (%s)\n", cli_errstr(cli1));
3444 correct = False;
3445 goto fail;
3448 /* Test 5 ... */
3449 cli_setatr(cli1, fname, 0, 0);
3450 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3452 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum1))) {
3453 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3454 correct = False;
3455 goto fail;
3458 /* This should fail - only allowed on NT opens with DELETE access. */
3460 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3461 printf("[5] setting delete_on_close on OpenX file succeeded - should fail !\n");
3462 correct = False;
3463 goto fail;
3466 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3467 printf("[5] close - 2 failed (%s)\n", cli_errstr(cli1));
3468 correct = False;
3469 goto fail;
3472 printf("fifth delete on close test succeeded.\n");
3474 /* Test 6 ... */
3475 cli_setatr(cli1, fname, 0, 0);
3476 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3478 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
3479 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3480 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3481 printf("[6] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3482 correct = False;
3483 goto fail;
3486 /* This should fail - only allowed on NT opens with DELETE access. */
3488 if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3489 printf("[6] setting delete_on_close on file with no delete access succeeded - should fail !\n");
3490 correct = False;
3491 goto fail;
3494 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3495 printf("[6] close - 2 failed (%s)\n", cli_errstr(cli1));
3496 correct = False;
3497 goto fail;
3500 printf("sixth delete on close test succeeded.\n");
3502 /* Test 7 ... */
3503 cli_setatr(cli1, fname, 0, 0);
3504 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3506 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3507 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3508 printf("[7] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3509 correct = False;
3510 goto fail;
3513 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3514 printf("[7] setting delete_on_close on file failed !\n");
3515 correct = False;
3516 goto fail;
3519 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, false))) {
3520 printf("[7] unsetting delete_on_close on file failed !\n");
3521 correct = False;
3522 goto fail;
3525 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3526 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3527 correct = False;
3528 goto fail;
3531 /* This next open should succeed - we reset the flag. */
3533 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3534 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3535 correct = False;
3536 goto fail;
3539 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3540 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3541 correct = False;
3542 goto fail;
3545 printf("seventh delete on close test succeeded.\n");
3547 /* Test 7 ... */
3548 cli_setatr(cli1, fname, 0, 0);
3549 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3551 if (!torture_open_connection(&cli2, 1)) {
3552 printf("[8] failed to open second connection.\n");
3553 correct = False;
3554 goto fail;
3557 cli_sockopt(cli1, sockops);
3559 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3560 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3561 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3562 printf("[8] open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3563 correct = False;
3564 goto fail;
3567 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3568 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3569 FILE_OPEN, 0, 0, &fnum2))) {
3570 printf("[8] open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3571 correct = False;
3572 goto fail;
3575 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
3576 printf("[8] setting delete_on_close on file failed !\n");
3577 correct = False;
3578 goto fail;
3581 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3582 printf("[8] close - 1 failed (%s)\n", cli_errstr(cli1));
3583 correct = False;
3584 goto fail;
3587 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
3588 printf("[8] close - 2 failed (%s)\n", cli_errstr(cli2));
3589 correct = False;
3590 goto fail;
3593 /* This should fail.. */
3594 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3595 printf("[8] open of %s succeeded should have been deleted on close !\n", fname);
3596 goto fail;
3597 correct = False;
3598 } else
3599 printf("eighth delete on close test succeeded.\n");
3601 /* This should fail - we need to set DELETE_ACCESS. */
3602 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,FILE_READ_DATA|FILE_WRITE_DATA,
3603 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3604 printf("[9] open of %s succeeded should have failed!\n", fname);
3605 correct = False;
3606 goto fail;
3609 printf("ninth delete on close test succeeded.\n");
3611 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3612 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
3613 printf("[10] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3614 correct = False;
3615 goto fail;
3618 /* This should delete the file. */
3619 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3620 printf("[10] close failed (%s)\n", cli_errstr(cli1));
3621 correct = False;
3622 goto fail;
3625 /* This should fail.. */
3626 if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
3627 printf("[10] open of %s succeeded should have been deleted on close !\n", fname);
3628 goto fail;
3629 correct = False;
3630 } else
3631 printf("tenth delete on close test succeeded.\n");
3633 cli_setatr(cli1, fname, 0, 0);
3634 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3636 /* What error do we get when attempting to open a read-only file with
3637 delete access ? */
3639 /* Create a readonly file. */
3640 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
3641 FILE_ATTRIBUTE_READONLY, FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3642 printf("[11] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3643 correct = False;
3644 goto fail;
3647 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3648 printf("[11] close failed (%s)\n", cli_errstr(cli1));
3649 correct = False;
3650 goto fail;
3653 /* Now try open for delete access. */
3654 if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES|DELETE_ACCESS,
3655 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3656 FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3657 printf("[11] open of %s succeeded should have been denied with ACCESS_DENIED!\n", fname);
3658 cli_close(cli1, fnum1);
3659 goto fail;
3660 correct = False;
3661 } else {
3662 NTSTATUS nterr = cli_nt_error(cli1);
3663 if (!NT_STATUS_EQUAL(nterr,NT_STATUS_ACCESS_DENIED)) {
3664 printf("[11] open of %s should have been denied with ACCESS_DENIED! Got error %s\n", fname, nt_errstr(nterr));
3665 goto fail;
3666 correct = False;
3667 } else {
3668 printf("eleventh delete on close test succeeded.\n");
3672 printf("finished delete test\n");
3674 fail:
3675 /* FIXME: This will crash if we aborted before cli2 got
3676 * intialized, because these functions don't handle
3677 * uninitialized connections. */
3679 if (fnum1 != (uint16_t)-1) cli_close(cli1, fnum1);
3680 if (fnum2 != (uint16_t)-1) cli_close(cli1, fnum2);
3681 cli_setatr(cli1, fname, 0, 0);
3682 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3684 if (cli1 && !torture_close_connection(cli1)) {
3685 correct = False;
3687 if (cli2 && !torture_close_connection(cli2)) {
3688 correct = False;
3690 return correct;
3695 print out server properties
3697 static bool run_properties(int dummy)
3699 struct cli_state *cli;
3700 bool correct = True;
3702 printf("starting properties test\n");
3704 ZERO_STRUCT(cli);
3706 if (!torture_open_connection(&cli, 0)) {
3707 return False;
3710 cli_sockopt(cli, sockops);
3712 d_printf("Capabilities 0x%08x\n", cli->capabilities);
3714 if (!torture_close_connection(cli)) {
3715 correct = False;
3718 return correct;
3723 /* FIRST_DESIRED_ACCESS 0xf019f */
3724 #define FIRST_DESIRED_ACCESS FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|\
3725 FILE_READ_EA| /* 0xf */ \
3726 FILE_WRITE_EA|FILE_READ_ATTRIBUTES| /* 0x90 */ \
3727 FILE_WRITE_ATTRIBUTES| /* 0x100 */ \
3728 DELETE_ACCESS|READ_CONTROL_ACCESS|\
3729 WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS /* 0xf0000 */
3730 /* SECOND_DESIRED_ACCESS 0xe0080 */
3731 #define SECOND_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
3732 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
3733 WRITE_OWNER_ACCESS /* 0xe0000 */
3735 #if 0
3736 #define THIRD_DESIRED_ACCESS FILE_READ_ATTRIBUTES| /* 0x80 */ \
3737 READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|\
3738 FILE_READ_DATA|\
3739 WRITE_OWNER_ACCESS /* */
3740 #endif
3743 Test ntcreate calls made by xcopy
3745 static bool run_xcopy(int dummy)
3747 static struct cli_state *cli1;
3748 const char *fname = "\\test.txt";
3749 bool correct = True;
3750 uint16_t fnum1, fnum2;
3752 printf("starting xcopy test\n");
3754 if (!torture_open_connection(&cli1, 0)) {
3755 return False;
3758 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,
3759 FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
3760 FILE_SHARE_NONE, FILE_OVERWRITE_IF,
3761 0x4044, 0, &fnum1))) {
3762 printf("First open failed - %s\n", cli_errstr(cli1));
3763 return False;
3766 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,
3767 SECOND_DESIRED_ACCESS, 0,
3768 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN,
3769 0x200000, 0, &fnum2))) {
3770 printf("second open failed - %s\n", cli_errstr(cli1));
3771 return False;
3774 if (!torture_close_connection(cli1)) {
3775 correct = False;
3778 return correct;
3782 Test rename on files open with share delete and no share delete.
3784 static bool run_rename(int dummy)
3786 static struct cli_state *cli1;
3787 const char *fname = "\\test.txt";
3788 const char *fname1 = "\\test1.txt";
3789 bool correct = True;
3790 uint16_t fnum1;
3791 NTSTATUS status;
3793 printf("starting rename test\n");
3795 if (!torture_open_connection(&cli1, 0)) {
3796 return False;
3799 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3800 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3801 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3802 FILE_SHARE_READ, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3803 printf("First open failed - %s\n", cli_errstr(cli1));
3804 return False;
3807 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
3808 printf("First rename failed (SHARE_READ) (this is correct) - %s\n", cli_errstr(cli1));
3809 } else {
3810 printf("First rename succeeded (SHARE_READ) - this should have failed !\n");
3811 correct = False;
3814 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3815 printf("close - 1 failed (%s)\n", cli_errstr(cli1));
3816 return False;
3819 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3820 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3821 status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3822 #if 0
3823 FILE_SHARE_DELETE|FILE_SHARE_NONE,
3824 #else
3825 FILE_SHARE_DELETE|FILE_SHARE_READ,
3826 #endif
3827 FILE_OVERWRITE_IF, 0, 0, &fnum1);
3828 if (!NT_STATUS_IS_OK(status)) {
3829 printf("Second open failed - %s\n", cli_errstr(cli1));
3830 return False;
3833 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
3834 printf("Second rename failed (SHARE_DELETE | SHARE_READ) - this should have succeeded - %s\n", cli_errstr(cli1));
3835 correct = False;
3836 } else {
3837 printf("Second rename succeeded (SHARE_DELETE | SHARE_READ)\n");
3840 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3841 printf("close - 2 failed (%s)\n", cli_errstr(cli1));
3842 return False;
3845 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3846 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3848 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, READ_CONTROL_ACCESS, FILE_ATTRIBUTE_NORMAL,
3849 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3850 printf("Third open failed - %s\n", cli_errstr(cli1));
3851 return False;
3855 #if 0
3857 uint16_t fnum2;
3859 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
3860 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
3861 printf("Fourth open failed - %s\n", cli_errstr(cli1));
3862 return False;
3864 if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum2, true))) {
3865 printf("[8] setting delete_on_close on file failed !\n");
3866 return False;
3869 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
3870 printf("close - 4 failed (%s)\n", cli_errstr(cli1));
3871 return False;
3874 #endif
3876 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
3877 printf("Third rename failed (SHARE_NONE) - this should have succeeded - %s\n", cli_errstr(cli1));
3878 correct = False;
3879 } else {
3880 printf("Third rename succeeded (SHARE_NONE)\n");
3883 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3884 printf("close - 3 failed (%s)\n", cli_errstr(cli1));
3885 return False;
3888 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3889 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3891 /*----*/
3893 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3894 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3895 printf("Fourth open failed - %s\n", cli_errstr(cli1));
3896 return False;
3899 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
3900 printf("Fourth rename failed (SHARE_READ | SHARE_WRITE) (this is correct) - %s\n", cli_errstr(cli1));
3901 } else {
3902 printf("Fourth rename succeeded (SHARE_READ | SHARE_WRITE) - this should have failed !\n");
3903 correct = False;
3906 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3907 printf("close - 4 failed (%s)\n", cli_errstr(cli1));
3908 return False;
3911 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3912 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3914 /*--*/
3916 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3917 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
3918 printf("Fifth open failed - %s\n", cli_errstr(cli1));
3919 return False;
3922 if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
3923 printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have succeeded - %s ! \n",
3924 cli_errstr(cli1));
3925 correct = False;
3926 } else {
3927 printf("Fifth rename succeeded (SHARE_READ | SHARE_WRITE | SHARE_DELETE) (this is correct) - %s\n", cli_errstr(cli1));
3931 * Now check if the first name still exists ...
3934 /* if (!NT_STATUS_OP(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3935 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
3936 printf("Opening original file after rename of open file fails: %s\n",
3937 cli_errstr(cli1));
3939 else {
3940 printf("Opening original file after rename of open file works ...\n");
3941 (void)cli_close(cli1, fnum2);
3942 } */
3944 /*--*/
3947 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
3948 printf("close - 5 failed (%s)\n", cli_errstr(cli1));
3949 return False;
3952 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
3953 cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
3955 if (!torture_close_connection(cli1)) {
3956 correct = False;
3959 return correct;
3962 static bool run_pipe_number(int dummy)
3964 struct cli_state *cli1;
3965 const char *pipe_name = "\\SPOOLSS";
3966 uint16_t fnum;
3967 int num_pipes = 0;
3969 printf("starting pipenumber test\n");
3970 if (!torture_open_connection(&cli1, 0)) {
3971 return False;
3974 cli_sockopt(cli1, sockops);
3975 while(1) {
3976 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, pipe_name, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
3977 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF, 0, 0, &fnum))) {
3978 printf("Open of pipe %s failed with error (%s)\n", pipe_name, cli_errstr(cli1));
3979 break;
3981 num_pipes++;
3982 printf("\r%6d", num_pipes);
3985 printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
3986 torture_close_connection(cli1);
3987 return True;
3991 Test open mode returns on read-only files.
3993 static bool run_opentest(int dummy)
3995 static struct cli_state *cli1;
3996 static struct cli_state *cli2;
3997 const char *fname = "\\readonly.file";
3998 uint16_t fnum1, fnum2;
3999 char buf[20];
4000 SMB_OFF_T fsize;
4001 bool correct = True;
4002 char *tmp_path;
4004 printf("starting open test\n");
4006 if (!torture_open_connection(&cli1, 0)) {
4007 return False;
4010 cli_setatr(cli1, fname, 0, 0);
4011 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4013 cli_sockopt(cli1, sockops);
4015 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
4016 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4017 return False;
4020 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4021 printf("close2 failed (%s)\n", cli_errstr(cli1));
4022 return False;
4025 if (!NT_STATUS_IS_OK(cli_setatr(cli1, fname, aRONLY, 0))) {
4026 printf("cli_setatr failed (%s)\n", cli_errstr(cli1));
4027 return False;
4030 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1))) {
4031 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4032 return False;
4035 /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
4036 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4038 if (check_error(__LINE__, cli1, ERRDOS, ERRnoaccess,
4039 NT_STATUS_ACCESS_DENIED)) {
4040 printf("correct error code ERRDOS/ERRnoaccess returned\n");
4043 printf("finished open test 1\n");
4045 cli_close(cli1, fnum1);
4047 /* Now try not readonly and ensure ERRbadshare is returned. */
4049 cli_setatr(cli1, fname, 0, 0);
4051 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1))) {
4052 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
4053 return False;
4056 /* This will fail - but the error should be ERRshare. */
4057 cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
4059 if (check_error(__LINE__, cli1, ERRDOS, ERRbadshare,
4060 NT_STATUS_SHARING_VIOLATION)) {
4061 printf("correct error code ERRDOS/ERRbadshare returned\n");
4064 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4065 printf("close2 failed (%s)\n", cli_errstr(cli1));
4066 return False;
4069 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4071 printf("finished open test 2\n");
4073 /* Test truncate open disposition on file opened for read. */
4075 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
4076 printf("(3) open (1) of %s failed (%s)\n", fname, cli_errstr(cli1));
4077 return False;
4080 /* write 20 bytes. */
4082 memset(buf, '\0', 20);
4084 if (cli_write(cli1, fnum1, 0, buf, 0, 20) != 20) {
4085 printf("write failed (%s)\n", cli_errstr(cli1));
4086 correct = False;
4089 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4090 printf("(3) close1 failed (%s)\n", cli_errstr(cli1));
4091 return False;
4094 /* Ensure size == 20. */
4095 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
4096 printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
4097 return False;
4100 if (fsize != 20) {
4101 printf("(3) file size != 20\n");
4102 return False;
4105 /* Now test if we can truncate a file opened for readonly. */
4107 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY|O_TRUNC, DENY_NONE, &fnum1))) {
4108 printf("(3) open (2) of %s failed (%s)\n", fname, cli_errstr(cli1));
4109 return False;
4112 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4113 printf("close2 failed (%s)\n", cli_errstr(cli1));
4114 return False;
4117 /* Ensure size == 0. */
4118 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
4119 printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
4120 return False;
4123 if (fsize != 0) {
4124 printf("(3) file size != 0\n");
4125 return False;
4127 printf("finished open test 3\n");
4129 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4132 printf("testing ctemp\n");
4133 if (!NT_STATUS_IS_OK(cli_ctemp(cli1, talloc_tos(), "\\", &fnum1, &tmp_path))) {
4134 printf("ctemp failed (%s)\n", cli_errstr(cli1));
4135 return False;
4137 printf("ctemp gave path %s\n", tmp_path);
4138 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4139 printf("close of temp failed (%s)\n", cli_errstr(cli1));
4141 if (!NT_STATUS_IS_OK(cli_unlink(cli1, tmp_path, aSYSTEM | aHIDDEN))) {
4142 printf("unlink of temp failed (%s)\n", cli_errstr(cli1));
4145 /* Test the non-io opens... */
4147 if (!torture_open_connection(&cli2, 1)) {
4148 return False;
4151 cli_setatr(cli2, fname, 0, 0);
4152 cli_unlink(cli2, fname, aSYSTEM | aHIDDEN);
4154 cli_sockopt(cli2, sockops);
4156 printf("TEST #1 testing 2 non-io opens (no delete)\n");
4158 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4159 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4160 printf("test 1 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4161 return False;
4164 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4165 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4166 printf("test 1 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4167 return False;
4170 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4171 printf("test 1 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4172 return False;
4174 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4175 printf("test 1 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4176 return False;
4179 printf("non-io open test #1 passed.\n");
4181 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4183 printf("TEST #2 testing 2 non-io opens (first with delete)\n");
4185 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4186 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4187 printf("test 2 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4188 return False;
4191 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4192 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4193 printf("test 2 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4194 return False;
4197 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4198 printf("test 1 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4199 return False;
4201 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4202 printf("test 1 close 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
4203 return False;
4206 printf("non-io open test #2 passed.\n");
4208 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4210 printf("TEST #3 testing 2 non-io opens (second with delete)\n");
4212 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4213 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4214 printf("test 3 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4215 return False;
4218 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4219 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4220 printf("test 3 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4221 return False;
4224 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4225 printf("test 3 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4226 return False;
4228 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4229 printf("test 3 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4230 return False;
4233 printf("non-io open test #3 passed.\n");
4235 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4237 printf("TEST #4 testing 2 non-io opens (both with delete)\n");
4239 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4240 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4241 printf("test 4 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4242 return False;
4245 if (NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4246 FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4247 printf("test 4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
4248 return False;
4251 printf("test 3 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
4253 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4254 printf("test 4 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4255 return False;
4258 printf("non-io open test #4 passed.\n");
4260 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4262 printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
4264 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4265 FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4266 printf("test 5 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4267 return False;
4270 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4271 FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4272 printf("test 5 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4273 return False;
4276 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4277 printf("test 5 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4278 return False;
4281 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4282 printf("test 5 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4283 return False;
4286 printf("non-io open test #5 passed.\n");
4288 printf("TEST #6 testing 1 non-io open, one io open\n");
4290 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4292 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
4293 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4294 printf("test 6 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4295 return False;
4298 if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4299 FILE_SHARE_READ, FILE_OPEN_IF, 0, 0, &fnum2))) {
4300 printf("test 6 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4301 return False;
4304 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4305 printf("test 6 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4306 return False;
4309 if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
4310 printf("test 6 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
4311 return False;
4314 printf("non-io open test #6 passed.\n");
4316 printf("TEST #7 testing 1 non-io open, one io open with delete\n");
4318 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4320 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
4321 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4322 printf("test 7 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4323 return False;
4326 if (NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
4327 FILE_SHARE_READ|FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0, &fnum2))) {
4328 printf("test 7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
4329 return False;
4332 printf("test 7 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
4334 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4335 printf("test 7 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
4336 return False;
4339 printf("non-io open test #7 passed.\n");
4341 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4343 if (!torture_close_connection(cli1)) {
4344 correct = False;
4346 if (!torture_close_connection(cli2)) {
4347 correct = False;
4350 return correct;
4354 Test POSIX open /mkdir calls.
4356 static bool run_simple_posix_open_test(int dummy)
4358 static struct cli_state *cli1;
4359 const char *fname = "posix:file";
4360 const char *hname = "posix:hlink";
4361 const char *sname = "posix:symlink";
4362 const char *dname = "posix:dir";
4363 char buf[10];
4364 char namebuf[11];
4365 uint16 major, minor;
4366 uint32 caplow, caphigh;
4367 uint16_t fnum1 = (uint16_t)-1;
4368 SMB_STRUCT_STAT sbuf;
4369 bool correct = false;
4371 printf("Starting simple POSIX open test\n");
4373 if (!torture_open_connection(&cli1, 0)) {
4374 return false;
4377 cli_sockopt(cli1, sockops);
4379 if (!SERVER_HAS_UNIX_CIFS(cli1)) {
4380 printf("Server doesn't support UNIX CIFS extensions.\n");
4381 return false;
4384 if (!cli_unix_extensions_version(cli1, &major,
4385 &minor, &caplow, &caphigh)) {
4386 printf("Server didn't return UNIX CIFS extensions.\n");
4387 return false;
4390 if (!cli_set_unix_extensions_capabilities(cli1,
4391 major, minor, caplow, caphigh)) {
4392 printf("Server doesn't support setting UNIX CIFS extensions.\n");
4393 return false;
4396 cli_setatr(cli1, fname, 0, 0);
4397 cli_posix_unlink(cli1, fname);
4398 cli_setatr(cli1, dname, 0, 0);
4399 cli_posix_rmdir(cli1, dname);
4400 cli_setatr(cli1, hname, 0, 0);
4401 cli_posix_unlink(cli1, hname);
4402 cli_setatr(cli1, sname, 0, 0);
4403 cli_posix_unlink(cli1, sname);
4405 /* Create a directory. */
4406 if (!NT_STATUS_IS_OK(cli_posix_mkdir(cli1, dname, 0777))) {
4407 printf("POSIX mkdir of %s failed (%s)\n", dname, cli_errstr(cli1));
4408 goto out;
4411 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1))) {
4412 printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
4413 goto out;
4416 /* Test ftruncate - set file size. */
4417 if (!NT_STATUS_IS_OK(cli_ftruncate(cli1, fnum1, 1000))) {
4418 printf("ftruncate failed (%s)\n", cli_errstr(cli1));
4419 goto out;
4422 /* Ensure st_size == 1000 */
4423 if (!NT_STATUS_IS_OK(cli_posix_stat(cli1, fname, &sbuf))) {
4424 printf("stat failed (%s)\n", cli_errstr(cli1));
4425 goto out;
4428 if (sbuf.st_ex_size != 1000) {
4429 printf("ftruncate - stat size (%u) != 1000\n", (unsigned int)sbuf.st_ex_size);
4430 goto out;
4433 /* Test ftruncate - set file size back to zero. */
4434 if (!NT_STATUS_IS_OK(cli_ftruncate(cli1, fnum1, 0))) {
4435 printf("ftruncate failed (%s)\n", cli_errstr(cli1));
4436 goto out;
4439 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4440 printf("close failed (%s)\n", cli_errstr(cli1));
4441 goto out;
4444 /* Now open the file again for read only. */
4445 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1))) {
4446 printf("POSIX open of %s failed (%s)\n", fname, cli_errstr(cli1));
4447 goto out;
4450 /* Now unlink while open. */
4451 if (!NT_STATUS_IS_OK(cli_posix_unlink(cli1, fname))) {
4452 printf("POSIX unlink of %s failed (%s)\n", fname, cli_errstr(cli1));
4453 goto out;
4456 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4457 printf("close(2) failed (%s)\n", cli_errstr(cli1));
4458 goto out;
4461 /* Ensure the file has gone. */
4462 if (NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1))) {
4463 printf("POSIX open of %s succeeded, should have been deleted.\n", fname);
4464 goto out;
4467 /* What happens when we try and POSIX open a directory ? */
4468 if (NT_STATUS_IS_OK(cli_posix_open(cli1, dname, O_RDONLY, 0, &fnum1))) {
4469 printf("POSIX open of directory %s succeeded, should have failed.\n", fname);
4470 goto out;
4471 } else {
4472 if (!check_error(__LINE__, cli1, ERRDOS, EISDIR,
4473 NT_STATUS_FILE_IS_A_DIRECTORY)) {
4474 goto out;
4478 /* Create the file. */
4479 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1))) {
4480 printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
4481 goto out;
4484 /* Write some data into it. */
4485 if (cli_write(cli1, fnum1, 0, "TEST DATA\n", 0, 10) != 10) {
4486 printf("cli_write failed: %s\n", cli_errstr(cli1));
4487 goto out;
4490 cli_close(cli1, fnum1);
4492 /* Now create a hardlink. */
4493 if (!NT_STATUS_IS_OK(cli_posix_hardlink(cli1, fname, hname))) {
4494 printf("POSIX hardlink of %s failed (%s)\n", hname, cli_errstr(cli1));
4495 goto out;
4498 /* Now create a symlink. */
4499 if (!NT_STATUS_IS_OK(cli_posix_symlink(cli1, fname, sname))) {
4500 printf("POSIX symlink of %s failed (%s)\n", sname, cli_errstr(cli1));
4501 goto out;
4504 /* Open the hardlink for read. */
4505 if (!NT_STATUS_IS_OK(cli_posix_open(cli1, hname, O_RDONLY, 0, &fnum1))) {
4506 printf("POSIX open of %s failed (%s)\n", hname, cli_errstr(cli1));
4507 goto out;
4510 if (cli_read(cli1, fnum1, buf, 0, 10) != 10) {
4511 printf("POSIX read of %s failed (%s)\n", hname, cli_errstr(cli1));
4512 goto out;
4515 if (memcmp(buf, "TEST DATA\n", 10)) {
4516 printf("invalid data read from hardlink\n");
4517 goto out;
4520 /* Do a POSIX lock/unlock. */
4521 if (!NT_STATUS_IS_OK(cli_posix_lock(cli1, fnum1, 0, 100, true, READ_LOCK))) {
4522 printf("POSIX lock failed %s\n", cli_errstr(cli1));
4523 goto out;
4526 /* Punch a hole in the locked area. */
4527 if (!NT_STATUS_IS_OK(cli_posix_unlock(cli1, fnum1, 10, 80))) {
4528 printf("POSIX unlock failed %s\n", cli_errstr(cli1));
4529 goto out;
4532 cli_close(cli1, fnum1);
4534 /* Open the symlink for read - this should fail. A POSIX
4535 client should not be doing opens on a symlink. */
4536 if (NT_STATUS_IS_OK(cli_posix_open(cli1, sname, O_RDONLY, 0, &fnum1))) {
4537 printf("POSIX open of %s succeeded (should have failed)\n", sname);
4538 goto out;
4539 } else {
4540 if (!check_error(__LINE__, cli1, ERRDOS, ERRbadpath,
4541 NT_STATUS_OBJECT_PATH_NOT_FOUND)) {
4542 printf("POSIX open of %s should have failed "
4543 "with NT_STATUS_OBJECT_PATH_NOT_FOUND, "
4544 "failed with %s instead.\n",
4545 sname, cli_errstr(cli1));
4546 goto out;
4550 if (!NT_STATUS_IS_OK(cli_posix_readlink(cli1, sname, namebuf, sizeof(namebuf)))) {
4551 printf("POSIX readlink on %s failed (%s)\n", sname, cli_errstr(cli1));
4552 goto out;
4555 if (strcmp(namebuf, fname) != 0) {
4556 printf("POSIX readlink on %s failed to match name %s (read %s)\n",
4557 sname, fname, namebuf);
4558 goto out;
4561 if (!NT_STATUS_IS_OK(cli_posix_rmdir(cli1, dname))) {
4562 printf("POSIX rmdir failed (%s)\n", cli_errstr(cli1));
4563 goto out;
4566 printf("Simple POSIX open test passed\n");
4567 correct = true;
4569 out:
4571 if (fnum1 != (uint16_t)-1) {
4572 cli_close(cli1, fnum1);
4573 fnum1 = (uint16_t)-1;
4576 cli_setatr(cli1, sname, 0, 0);
4577 cli_posix_unlink(cli1, sname);
4578 cli_setatr(cli1, hname, 0, 0);
4579 cli_posix_unlink(cli1, hname);
4580 cli_setatr(cli1, fname, 0, 0);
4581 cli_posix_unlink(cli1, fname);
4582 cli_setatr(cli1, dname, 0, 0);
4583 cli_posix_rmdir(cli1, dname);
4585 if (!torture_close_connection(cli1)) {
4586 correct = false;
4589 return correct;
4593 static uint32 open_attrs_table[] = {
4594 FILE_ATTRIBUTE_NORMAL,
4595 FILE_ATTRIBUTE_ARCHIVE,
4596 FILE_ATTRIBUTE_READONLY,
4597 FILE_ATTRIBUTE_HIDDEN,
4598 FILE_ATTRIBUTE_SYSTEM,
4600 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY,
4601 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN,
4602 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,
4603 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
4604 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
4605 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
4607 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
4608 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
4609 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
4610 FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_SYSTEM,
4613 struct trunc_open_results {
4614 unsigned int num;
4615 uint32 init_attr;
4616 uint32 trunc_attr;
4617 uint32 result_attr;
4620 static struct trunc_open_results attr_results[] = {
4621 { 0, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
4622 { 1, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
4623 { 2, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
4624 { 16, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
4625 { 17, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
4626 { 18, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
4627 { 51, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4628 { 54, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4629 { 56, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
4630 { 68, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4631 { 71, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4632 { 73, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
4633 { 99, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4634 { 102, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4635 { 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 },
4636 { 116, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4637 { 119, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4638 { 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 },
4639 { 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 },
4640 { 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 },
4641 { 227, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4642 { 230, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
4643 { 232, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
4644 { 244, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4645 { 247, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
4646 { 249, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM }
4649 static bool run_openattrtest(int dummy)
4651 static struct cli_state *cli1;
4652 const char *fname = "\\openattr.file";
4653 uint16_t fnum1;
4654 bool correct = True;
4655 uint16 attr;
4656 unsigned int i, j, k, l;
4658 printf("starting open attr test\n");
4660 if (!torture_open_connection(&cli1, 0)) {
4661 return False;
4664 cli_sockopt(cli1, sockops);
4666 for (k = 0, i = 0; i < sizeof(open_attrs_table)/sizeof(uint32); i++) {
4667 cli_setatr(cli1, fname, 0, 0);
4668 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4669 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA, open_attrs_table[i],
4670 FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
4671 printf("open %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
4672 return False;
4675 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4676 printf("close %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
4677 return False;
4680 for (j = 0; j < sizeof(open_attrs_table)/sizeof(uint32); j++) {
4681 if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA, open_attrs_table[j],
4682 FILE_SHARE_NONE, FILE_OVERWRITE, 0, 0, &fnum1))) {
4683 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
4684 if (attr_results[l].num == k) {
4685 printf("[%d] trunc open 0x%x -> 0x%x of %s failed - should have succeeded !(0x%x:%s)\n",
4686 k, open_attrs_table[i],
4687 open_attrs_table[j],
4688 fname, NT_STATUS_V(cli_nt_error(cli1)), cli_errstr(cli1));
4689 correct = False;
4692 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
4693 printf("[%d] trunc open 0x%x -> 0x%x failed with wrong error code %s\n",
4694 k, open_attrs_table[i], open_attrs_table[j],
4695 cli_errstr(cli1));
4696 correct = False;
4698 #if 0
4699 printf("[%d] trunc open 0x%x -> 0x%x failed\n", k, open_attrs_table[i], open_attrs_table[j]);
4700 #endif
4701 k++;
4702 continue;
4705 if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
4706 printf("close %d (2) of %s failed (%s)\n", j, fname, cli_errstr(cli1));
4707 return False;
4710 if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, &attr, NULL, NULL))) {
4711 printf("getatr(2) failed (%s)\n", cli_errstr(cli1));
4712 return False;
4715 #if 0
4716 printf("[%d] getatr check [0x%x] trunc [0x%x] got attr 0x%x\n",
4717 k, open_attrs_table[i], open_attrs_table[j], attr );
4718 #endif
4720 for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
4721 if (attr_results[l].num == k) {
4722 if (attr != attr_results[l].result_attr ||
4723 open_attrs_table[i] != attr_results[l].init_attr ||
4724 open_attrs_table[j] != attr_results[l].trunc_attr) {
4725 printf("getatr check failed. [0x%x] trunc [0x%x] got attr 0x%x, should be 0x%x\n",
4726 open_attrs_table[i],
4727 open_attrs_table[j],
4728 (unsigned int)attr,
4729 attr_results[l].result_attr);
4730 correct = False;
4732 break;
4735 k++;
4739 cli_setatr(cli1, fname, 0, 0);
4740 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
4742 printf("open attr test %s.\n", correct ? "passed" : "failed");
4744 if (!torture_close_connection(cli1)) {
4745 correct = False;
4747 return correct;
4750 static void list_fn(const char *mnt, file_info *finfo, const char *name, void *state)
4756 test directory listing speed
4758 static bool run_dirtest(int dummy)
4760 int i;
4761 static struct cli_state *cli;
4762 uint16_t fnum;
4763 double t1;
4764 bool correct = True;
4766 printf("starting directory test\n");
4768 if (!torture_open_connection(&cli, 0)) {
4769 return False;
4772 cli_sockopt(cli, sockops);
4774 srandom(0);
4775 for (i=0;i<torture_numops;i++) {
4776 fstring fname;
4777 slprintf(fname, sizeof(fname), "\\%x", (int)random());
4778 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum))) {
4779 fprintf(stderr,"Failed to open %s\n", fname);
4780 return False;
4782 cli_close(cli, fnum);
4785 t1 = end_timer();
4787 printf("Matched %d\n", cli_list(cli, "a*.*", 0, list_fn, NULL));
4788 printf("Matched %d\n", cli_list(cli, "b*.*", 0, list_fn, NULL));
4789 printf("Matched %d\n", cli_list(cli, "xyzabc", 0, list_fn, NULL));
4791 printf("dirtest core %g seconds\n", end_timer() - t1);
4793 srandom(0);
4794 for (i=0;i<torture_numops;i++) {
4795 fstring fname;
4796 slprintf(fname, sizeof(fname), "\\%x", (int)random());
4797 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
4800 if (!torture_close_connection(cli)) {
4801 correct = False;
4804 printf("finished dirtest\n");
4806 return correct;
4809 static void del_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
4811 struct cli_state *pcli = (struct cli_state *)state;
4812 fstring fname;
4813 slprintf(fname, sizeof(fname), "\\LISTDIR\\%s", finfo->name);
4815 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
4816 return;
4818 if (finfo->mode & aDIR) {
4819 if (!NT_STATUS_IS_OK(cli_rmdir(pcli, fname)))
4820 printf("del_fn: failed to rmdir %s\n,", fname );
4821 } else {
4822 if (!NT_STATUS_IS_OK(cli_unlink(pcli, fname, aSYSTEM | aHIDDEN)))
4823 printf("del_fn: failed to unlink %s\n,", fname );
4829 sees what IOCTLs are supported
4831 bool torture_ioctl_test(int dummy)
4833 static struct cli_state *cli;
4834 uint16_t device, function;
4835 uint16_t fnum;
4836 const char *fname = "\\ioctl.dat";
4837 DATA_BLOB blob;
4838 NTSTATUS status;
4840 if (!torture_open_connection(&cli, 0)) {
4841 return False;
4844 printf("starting ioctl test\n");
4846 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
4848 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
4849 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
4850 return False;
4853 status = cli_raw_ioctl(cli, fnum, 0x2d0000 | (0x0420<<2), &blob);
4854 printf("ioctl device info: %s\n", cli_errstr(cli));
4856 status = cli_raw_ioctl(cli, fnum, IOCTL_QUERY_JOB_INFO, &blob);
4857 printf("ioctl job info: %s\n", cli_errstr(cli));
4859 for (device=0;device<0x100;device++) {
4860 printf("testing device=0x%x\n", device);
4861 for (function=0;function<0x100;function++) {
4862 uint32 code = (device<<16) | function;
4864 status = cli_raw_ioctl(cli, fnum, code, &blob);
4866 if (NT_STATUS_IS_OK(status)) {
4867 printf("ioctl 0x%x OK : %d bytes\n", (int)code,
4868 (int)blob.length);
4869 data_blob_free(&blob);
4874 if (!torture_close_connection(cli)) {
4875 return False;
4878 return True;
4883 tries varients of chkpath
4885 bool torture_chkpath_test(int dummy)
4887 static struct cli_state *cli;
4888 uint16_t fnum;
4889 bool ret;
4891 if (!torture_open_connection(&cli, 0)) {
4892 return False;
4895 printf("starting chkpath test\n");
4897 /* cleanup from an old run */
4898 cli_rmdir(cli, "\\chkpath.dir\\dir2");
4899 cli_unlink(cli, "\\chkpath.dir\\*", aSYSTEM | aHIDDEN);
4900 cli_rmdir(cli, "\\chkpath.dir");
4902 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\chkpath.dir"))) {
4903 printf("mkdir1 failed : %s\n", cli_errstr(cli));
4904 return False;
4907 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\chkpath.dir\\dir2"))) {
4908 printf("mkdir2 failed : %s\n", cli_errstr(cli));
4909 return False;
4912 if (!NT_STATUS_IS_OK(cli_open(cli, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
4913 printf("open1 failed (%s)\n", cli_errstr(cli));
4914 return False;
4916 cli_close(cli, fnum);
4918 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir"))) {
4919 printf("chkpath1 failed: %s\n", cli_errstr(cli));
4920 ret = False;
4923 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dir2"))) {
4924 printf("chkpath2 failed: %s\n", cli_errstr(cli));
4925 ret = False;
4928 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\foo.txt"))) {
4929 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
4930 NT_STATUS_NOT_A_DIRECTORY);
4931 } else {
4932 printf("* chkpath on a file should fail\n");
4933 ret = False;
4936 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\bar.txt"))) {
4937 ret = check_error(__LINE__, cli, ERRDOS, ERRbadfile,
4938 NT_STATUS_OBJECT_NAME_NOT_FOUND);
4939 } else {
4940 printf("* chkpath on a non existant file should fail\n");
4941 ret = False;
4944 if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dirxx\\bar.txt"))) {
4945 ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath,
4946 NT_STATUS_OBJECT_PATH_NOT_FOUND);
4947 } else {
4948 printf("* chkpath on a non existent component should fail\n");
4949 ret = False;
4952 cli_rmdir(cli, "\\chkpath.dir\\dir2");
4953 cli_unlink(cli, "\\chkpath.dir\\*", aSYSTEM | aHIDDEN);
4954 cli_rmdir(cli, "\\chkpath.dir");
4956 if (!torture_close_connection(cli)) {
4957 return False;
4960 return ret;
4963 static bool run_eatest(int dummy)
4965 static struct cli_state *cli;
4966 const char *fname = "\\eatest.txt";
4967 bool correct = True;
4968 uint16_t fnum;
4969 int i;
4970 size_t num_eas;
4971 struct ea_struct *ea_list = NULL;
4972 TALLOC_CTX *mem_ctx = talloc_init("eatest");
4974 printf("starting eatest\n");
4976 if (!torture_open_connection(&cli, 0)) {
4977 talloc_destroy(mem_ctx);
4978 return False;
4981 cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
4982 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0,
4983 FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
4984 FILE_SHARE_NONE, FILE_OVERWRITE_IF,
4985 0x4044, 0, &fnum))) {
4986 printf("open failed - %s\n", cli_errstr(cli));
4987 talloc_destroy(mem_ctx);
4988 return False;
4991 for (i = 0; i < 10; i++) {
4992 fstring ea_name, ea_val;
4994 slprintf(ea_name, sizeof(ea_name), "EA_%d", i);
4995 memset(ea_val, (char)i+1, i+1);
4996 if (!cli_set_ea_fnum(cli, fnum, ea_name, ea_val, i+1)) {
4997 printf("ea_set of name %s failed - %s\n", ea_name, cli_errstr(cli));
4998 talloc_destroy(mem_ctx);
4999 return False;
5003 cli_close(cli, fnum);
5004 for (i = 0; i < 10; i++) {
5005 fstring ea_name, ea_val;
5007 slprintf(ea_name, sizeof(ea_name), "EA_%d", i+10);
5008 memset(ea_val, (char)i+1, i+1);
5009 if (!cli_set_ea_path(cli, fname, ea_name, ea_val, i+1)) {
5010 printf("ea_set of name %s failed - %s\n", ea_name, cli_errstr(cli));
5011 talloc_destroy(mem_ctx);
5012 return False;
5016 if (!cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list)) {
5017 printf("ea_get list failed - %s\n", cli_errstr(cli));
5018 correct = False;
5021 printf("num_eas = %d\n", (int)num_eas);
5023 if (num_eas != 20) {
5024 printf("Should be 20 EA's stored... failing.\n");
5025 correct = False;
5028 for (i = 0; i < num_eas; i++) {
5029 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
5030 dump_data(0, ea_list[i].value.data,
5031 ea_list[i].value.length);
5034 /* Setting EA's to zero length deletes them. Test this */
5035 printf("Now deleting all EA's - case indepenent....\n");
5037 #if 1
5038 cli_set_ea_path(cli, fname, "", "", 0);
5039 #else
5040 for (i = 0; i < 20; i++) {
5041 fstring ea_name;
5042 slprintf(ea_name, sizeof(ea_name), "ea_%d", i);
5043 if (!cli_set_ea_path(cli, fname, ea_name, "", 0)) {
5044 printf("ea_set of name %s failed - %s\n", ea_name, cli_errstr(cli));
5045 talloc_destroy(mem_ctx);
5046 return False;
5049 #endif
5051 if (!cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list)) {
5052 printf("ea_get list failed - %s\n", cli_errstr(cli));
5053 correct = False;
5056 printf("num_eas = %d\n", (int)num_eas);
5057 for (i = 0; i < num_eas; i++) {
5058 printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
5059 dump_data(0, ea_list[i].value.data,
5060 ea_list[i].value.length);
5063 if (num_eas != 0) {
5064 printf("deleting EA's failed.\n");
5065 correct = False;
5068 /* Try and delete a non existant EA. */
5069 if (!cli_set_ea_path(cli, fname, "foo", "", 0)) {
5070 printf("deleting non-existant EA 'foo' should succeed. %s\n", cli_errstr(cli));
5071 correct = False;
5074 talloc_destroy(mem_ctx);
5075 if (!torture_close_connection(cli)) {
5076 correct = False;
5079 return correct;
5082 static bool run_dirtest1(int dummy)
5084 int i;
5085 static struct cli_state *cli;
5086 uint16_t fnum;
5087 int num_seen;
5088 bool correct = True;
5090 printf("starting directory test\n");
5092 if (!torture_open_connection(&cli, 0)) {
5093 return False;
5096 cli_sockopt(cli, sockops);
5098 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
5099 cli_list(cli, "\\LISTDIR\\*", aDIR, del_fn, cli);
5100 cli_rmdir(cli, "\\LISTDIR");
5101 cli_mkdir(cli, "\\LISTDIR");
5103 /* Create 1000 files and 1000 directories. */
5104 for (i=0;i<1000;i++) {
5105 fstring fname;
5106 slprintf(fname, sizeof(fname), "\\LISTDIR\\f%d", i);
5107 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
5108 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
5109 fprintf(stderr,"Failed to open %s\n", fname);
5110 return False;
5112 cli_close(cli, fnum);
5114 for (i=0;i<1000;i++) {
5115 fstring fname;
5116 slprintf(fname, sizeof(fname), "\\LISTDIR\\d%d", i);
5117 if (!NT_STATUS_IS_OK(cli_mkdir(cli, fname))) {
5118 fprintf(stderr,"Failed to open %s\n", fname);
5119 return False;
5123 /* Now ensure that doing an old list sees both files and directories. */
5124 num_seen = cli_list_old(cli, "\\LISTDIR\\*", aDIR, list_fn, NULL);
5125 printf("num_seen = %d\n", num_seen );
5126 /* We should see 100 files + 1000 directories + . and .. */
5127 if (num_seen != 2002)
5128 correct = False;
5130 /* Ensure if we have the "must have" bits we only see the
5131 * relevent entries.
5133 num_seen = cli_list_old(cli, "\\LISTDIR\\*", (aDIR<<8)|aDIR, list_fn, NULL);
5134 printf("num_seen = %d\n", num_seen );
5135 if (num_seen != 1002)
5136 correct = False;
5138 num_seen = cli_list_old(cli, "\\LISTDIR\\*", (aARCH<<8)|aDIR, list_fn, NULL);
5139 printf("num_seen = %d\n", num_seen );
5140 if (num_seen != 1000)
5141 correct = False;
5143 /* Delete everything. */
5144 cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
5145 cli_list(cli, "\\LISTDIR\\*", aDIR, del_fn, cli);
5146 cli_rmdir(cli, "\\LISTDIR");
5148 #if 0
5149 printf("Matched %d\n", cli_list(cli, "a*.*", 0, list_fn, NULL));
5150 printf("Matched %d\n", cli_list(cli, "b*.*", 0, list_fn, NULL));
5151 printf("Matched %d\n", cli_list(cli, "xyzabc", 0, list_fn, NULL));
5152 #endif
5154 if (!torture_close_connection(cli)) {
5155 correct = False;
5158 printf("finished dirtest1\n");
5160 return correct;
5163 static bool run_error_map_extract(int dummy) {
5165 static struct cli_state *c_dos;
5166 static struct cli_state *c_nt;
5167 NTSTATUS status;
5169 uint32 error;
5171 uint32 flgs2, errnum;
5172 uint8 errclass;
5174 NTSTATUS nt_status;
5176 fstring user;
5178 /* NT-Error connection */
5180 if (!(c_nt = open_nbt_connection())) {
5181 return False;
5184 c_nt->use_spnego = False;
5186 status = cli_negprot(c_nt);
5188 if (!NT_STATUS_IS_OK(status)) {
5189 printf("%s rejected the NT-error negprot (%s)\n", host,
5190 nt_errstr(status));
5191 cli_shutdown(c_nt);
5192 return False;
5195 if (!NT_STATUS_IS_OK(cli_session_setup(c_nt, "", "", 0, "", 0,
5196 workgroup))) {
5197 printf("%s rejected the NT-error initial session setup (%s)\n",host, cli_errstr(c_nt));
5198 return False;
5201 /* DOS-Error connection */
5203 if (!(c_dos = open_nbt_connection())) {
5204 return False;
5207 c_dos->use_spnego = False;
5208 c_dos->force_dos_errors = True;
5210 status = cli_negprot(c_dos);
5211 if (!NT_STATUS_IS_OK(status)) {
5212 printf("%s rejected the DOS-error negprot (%s)\n", host,
5213 nt_errstr(status));
5214 cli_shutdown(c_dos);
5215 return False;
5218 if (!NT_STATUS_IS_OK(cli_session_setup(c_dos, "", "", 0, "", 0,
5219 workgroup))) {
5220 printf("%s rejected the DOS-error initial session setup (%s)\n",host, cli_errstr(c_dos));
5221 return False;
5224 for (error=(0xc0000000 | 0x1); error < (0xc0000000| 0xFFF); error++) {
5225 fstr_sprintf(user, "%X", error);
5227 if (NT_STATUS_IS_OK(cli_session_setup(c_nt, user,
5228 password, strlen(password),
5229 password, strlen(password),
5230 workgroup))) {
5231 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
5234 flgs2 = SVAL(c_nt->inbuf,smb_flg2);
5236 /* Case #1: 32-bit NT errors */
5237 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
5238 nt_status = NT_STATUS(IVAL(c_nt->inbuf,smb_rcls));
5239 } else {
5240 printf("/** Dos error on NT connection! (%s) */\n",
5241 cli_errstr(c_nt));
5242 nt_status = NT_STATUS(0xc0000000);
5245 if (NT_STATUS_IS_OK(cli_session_setup(c_dos, user,
5246 password, strlen(password),
5247 password, strlen(password),
5248 workgroup))) {
5249 printf("/** Session setup succeeded. This shouldn't happen...*/\n");
5251 flgs2 = SVAL(c_dos->inbuf,smb_flg2), errnum;
5253 /* Case #1: 32-bit NT errors */
5254 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
5255 printf("/** NT error on DOS connection! (%s) */\n",
5256 cli_errstr(c_nt));
5257 errnum = errclass = 0;
5258 } else {
5259 cli_dos_error(c_dos, &errclass, &errnum);
5262 if (NT_STATUS_V(nt_status) != error) {
5263 printf("/*\t{ This NT error code was 'sqashed'\n\t from %s to %s \n\t during the session setup }\n*/\n",
5264 get_nt_error_c_code(NT_STATUS(error)),
5265 get_nt_error_c_code(nt_status));
5268 printf("\t{%s,\t%s,\t%s},\n",
5269 smb_dos_err_class(errclass),
5270 smb_dos_err_name(errclass, errnum),
5271 get_nt_error_c_code(NT_STATUS(error)));
5273 return True;
5276 static bool run_sesssetup_bench(int dummy)
5278 static struct cli_state *c;
5279 const char *fname = "\\file.dat";
5280 uint16_t fnum;
5281 NTSTATUS status;
5282 int i;
5284 if (!torture_open_connection(&c, 0)) {
5285 return false;
5288 if (!NT_STATUS_IS_OK(cli_ntcreate(
5289 c, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
5290 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
5291 FILE_DELETE_ON_CLOSE, 0, &fnum))) {
5292 d_printf("open %s failed: %s\n", fname, cli_errstr(c));
5293 return false;
5296 for (i=0; i<torture_numops; i++) {
5297 status = cli_session_setup(
5298 c, username,
5299 password, strlen(password),
5300 password, strlen(password),
5301 workgroup);
5302 if (!NT_STATUS_IS_OK(status)) {
5303 d_printf("(%s) cli_session_setup failed: %s\n",
5304 __location__, nt_errstr(status));
5305 return false;
5308 d_printf("\r%d ", (int)c->vuid);
5310 if (!cli_ulogoff(c)) {
5311 d_printf("(%s) cli_ulogoff failed: %s\n",
5312 __location__, cli_errstr(c));
5313 return false;
5315 c->vuid = 0;
5318 return true;
5321 static bool subst_test(const char *str, const char *user, const char *domain,
5322 uid_t uid, gid_t gid, const char *expected)
5324 char *subst;
5325 bool result = true;
5327 subst = talloc_sub_specified(talloc_tos(), str, user, domain, uid, gid);
5329 if (strcmp(subst, expected) != 0) {
5330 printf("sub_specified(%s, %s, %s, %d, %d) returned [%s], expected "
5331 "[%s]\n", str, user, domain, (int)uid, (int)gid, subst,
5332 expected);
5333 result = false;
5336 TALLOC_FREE(subst);
5337 return result;
5340 static void chain1_open_completion(struct tevent_req *req)
5342 uint16_t fnum;
5343 NTSTATUS status;
5344 status = cli_open_recv(req, &fnum);
5345 TALLOC_FREE(req);
5347 d_printf("cli_open_recv returned %s: %d\n",
5348 nt_errstr(status),
5349 NT_STATUS_IS_OK(status) ? fnum : -1);
5352 static void chain1_write_completion(struct tevent_req *req)
5354 size_t written;
5355 NTSTATUS status;
5356 status = cli_write_andx_recv(req, &written);
5357 TALLOC_FREE(req);
5359 d_printf("cli_write_andx_recv returned %s: %d\n",
5360 nt_errstr(status),
5361 NT_STATUS_IS_OK(status) ? (int)written : -1);
5364 static void chain1_close_completion(struct tevent_req *req)
5366 NTSTATUS status;
5367 bool *done = (bool *)tevent_req_callback_data_void(req);
5369 status = cli_close_recv(req);
5370 *done = true;
5372 TALLOC_FREE(req);
5374 d_printf("cli_close returned %s\n", nt_errstr(status));
5377 static bool run_chain1(int dummy)
5379 struct cli_state *cli1;
5380 struct event_context *evt = event_context_init(NULL);
5381 struct tevent_req *reqs[3], *smbreqs[3];
5382 bool done = false;
5383 const char *str = "foobar";
5384 NTSTATUS status;
5386 printf("starting chain1 test\n");
5387 if (!torture_open_connection(&cli1, 0)) {
5388 return False;
5391 cli_sockopt(cli1, sockops);
5393 reqs[0] = cli_open_create(talloc_tos(), evt, cli1, "\\test",
5394 O_CREAT|O_RDWR, 0, &smbreqs[0]);
5395 if (reqs[0] == NULL) return false;
5396 tevent_req_set_callback(reqs[0], chain1_open_completion, NULL);
5399 reqs[1] = cli_write_andx_create(talloc_tos(), evt, cli1, 0, 0,
5400 (uint8_t *)str, 0, strlen(str)+1,
5401 smbreqs, 1, &smbreqs[1]);
5402 if (reqs[1] == NULL) return false;
5403 tevent_req_set_callback(reqs[1], chain1_write_completion, NULL);
5405 reqs[2] = cli_close_create(talloc_tos(), evt, cli1, 0, &smbreqs[2]);
5406 if (reqs[2] == NULL) return false;
5407 tevent_req_set_callback(reqs[2], chain1_close_completion, &done);
5409 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
5410 if (!NT_STATUS_IS_OK(status)) {
5411 return false;
5414 while (!done) {
5415 event_loop_once(evt);
5418 torture_close_connection(cli1);
5419 return True;
5422 static void chain2_sesssetup_completion(struct tevent_req *req)
5424 NTSTATUS status;
5425 status = cli_session_setup_guest_recv(req);
5426 d_printf("sesssetup returned %s\n", nt_errstr(status));
5429 static void chain2_tcon_completion(struct tevent_req *req)
5431 bool *done = (bool *)tevent_req_callback_data_void(req);
5432 NTSTATUS status;
5433 status = cli_tcon_andx_recv(req);
5434 d_printf("tcon_and_x returned %s\n", nt_errstr(status));
5435 *done = true;
5438 static bool run_chain2(int dummy)
5440 struct cli_state *cli1;
5441 struct event_context *evt = event_context_init(NULL);
5442 struct tevent_req *reqs[2], *smbreqs[2];
5443 bool done = false;
5444 NTSTATUS status;
5446 printf("starting chain2 test\n");
5447 status = cli_start_connection(&cli1, global_myname(), host, NULL,
5448 port_to_use, Undefined, 0, NULL);
5449 if (!NT_STATUS_IS_OK(status)) {
5450 return False;
5453 cli_sockopt(cli1, sockops);
5455 reqs[0] = cli_session_setup_guest_create(talloc_tos(), evt, cli1,
5456 &smbreqs[0]);
5457 if (reqs[0] == NULL) return false;
5458 tevent_req_set_callback(reqs[0], chain2_sesssetup_completion, NULL);
5460 reqs[1] = cli_tcon_andx_create(talloc_tos(), evt, cli1, "IPC$",
5461 "?????", NULL, 0, &smbreqs[1]);
5462 if (reqs[1] == NULL) return false;
5463 tevent_req_set_callback(reqs[1], chain2_tcon_completion, &done);
5465 status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
5466 if (!NT_STATUS_IS_OK(status)) {
5467 return false;
5470 while (!done) {
5471 event_loop_once(evt);
5474 torture_close_connection(cli1);
5475 return True;
5479 struct torture_createdel_state {
5480 struct tevent_context *ev;
5481 struct cli_state *cli;
5484 static void torture_createdel_created(struct tevent_req *subreq);
5485 static void torture_createdel_closed(struct tevent_req *subreq);
5487 static struct tevent_req *torture_createdel_send(TALLOC_CTX *mem_ctx,
5488 struct tevent_context *ev,
5489 struct cli_state *cli,
5490 const char *name)
5492 struct tevent_req *req, *subreq;
5493 struct torture_createdel_state *state;
5495 req = tevent_req_create(mem_ctx, &state,
5496 struct torture_createdel_state);
5497 if (req == NULL) {
5498 return NULL;
5500 state->ev = ev;
5501 state->cli = cli;
5503 subreq = cli_ntcreate_send(
5504 state, ev, cli, name, 0,
5505 FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
5506 FILE_ATTRIBUTE_NORMAL,
5507 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
5508 FILE_OPEN_IF, FILE_DELETE_ON_CLOSE, 0);
5510 if (tevent_req_nomem(subreq, req)) {
5511 return tevent_req_post(req, ev);
5513 tevent_req_set_callback(subreq, torture_createdel_created, req);
5514 return req;
5517 static void torture_createdel_created(struct tevent_req *subreq)
5519 struct tevent_req *req = tevent_req_callback_data(
5520 subreq, struct tevent_req);
5521 struct torture_createdel_state *state = tevent_req_data(
5522 req, struct torture_createdel_state);
5523 NTSTATUS status;
5524 uint16_t fnum;
5526 status = cli_ntcreate_recv(subreq, &fnum);
5527 TALLOC_FREE(subreq);
5528 if (!NT_STATUS_IS_OK(status)) {
5529 DEBUG(10, ("cli_ntcreate_recv returned %s\n",
5530 nt_errstr(status)));
5531 tevent_req_nterror(req, status);
5532 return;
5535 subreq = cli_close_send(state, state->ev, state->cli, fnum);
5536 if (tevent_req_nomem(subreq, req)) {
5537 return;
5539 tevent_req_set_callback(subreq, torture_createdel_closed, req);
5542 static void torture_createdel_closed(struct tevent_req *subreq)
5544 struct tevent_req *req = tevent_req_callback_data(
5545 subreq, struct tevent_req);
5546 NTSTATUS status;
5548 status = cli_close_recv(subreq);
5549 if (!NT_STATUS_IS_OK(status)) {
5550 DEBUG(10, ("cli_close_recv returned %s\n", nt_errstr(status)));
5551 tevent_req_nterror(req, status);
5552 return;
5554 tevent_req_done(req);
5557 static NTSTATUS torture_createdel_recv(struct tevent_req *req)
5559 return tevent_req_simple_recv_ntstatus(req);
5562 struct torture_createdels_state {
5563 struct tevent_context *ev;
5564 struct cli_state *cli;
5565 const char *base_name;
5566 int sent;
5567 int received;
5568 int num_files;
5569 struct tevent_req **reqs;
5572 static void torture_createdels_done(struct tevent_req *subreq);
5574 static struct tevent_req *torture_createdels_send(TALLOC_CTX *mem_ctx,
5575 struct tevent_context *ev,
5576 struct cli_state *cli,
5577 const char *base_name,
5578 int num_parallel,
5579 int num_files)
5581 struct tevent_req *req;
5582 struct torture_createdels_state *state;
5583 int i;
5585 req = tevent_req_create(mem_ctx, &state,
5586 struct torture_createdels_state);
5587 if (req == NULL) {
5588 return NULL;
5590 state->ev = ev;
5591 state->cli = cli;
5592 state->base_name = talloc_strdup(state, base_name);
5593 if (tevent_req_nomem(state->base_name, req)) {
5594 return tevent_req_post(req, ev);
5596 state->num_files = MAX(num_parallel, num_files);
5597 state->sent = 0;
5598 state->received = 0;
5600 state->reqs = talloc_array(state, struct tevent_req *, num_parallel);
5601 if (tevent_req_nomem(state->reqs, req)) {
5602 return tevent_req_post(req, ev);
5605 for (i=0; i<num_parallel; i++) {
5606 char *name;
5608 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
5609 state->sent);
5610 if (tevent_req_nomem(name, req)) {
5611 return tevent_req_post(req, ev);
5613 state->reqs[i] = torture_createdel_send(
5614 state->reqs, state->ev, state->cli, name);
5615 if (tevent_req_nomem(state->reqs[i], req)) {
5616 return tevent_req_post(req, ev);
5618 name = talloc_move(state->reqs[i], &name);
5619 tevent_req_set_callback(state->reqs[i],
5620 torture_createdels_done, req);
5621 state->sent += 1;
5623 return req;
5626 static void torture_createdels_done(struct tevent_req *subreq)
5628 struct tevent_req *req = tevent_req_callback_data(
5629 subreq, struct tevent_req);
5630 struct torture_createdels_state *state = tevent_req_data(
5631 req, struct torture_createdels_state);
5632 size_t num_parallel = talloc_array_length(state->reqs);
5633 NTSTATUS status;
5634 char *name;
5635 int i;
5637 status = torture_createdel_recv(subreq);
5638 if (!NT_STATUS_IS_OK(status)){
5639 DEBUG(10, ("torture_createdel_recv returned %s\n",
5640 nt_errstr(status)));
5641 TALLOC_FREE(subreq);
5642 tevent_req_nterror(req, status);
5643 return;
5646 for (i=0; i<num_parallel; i++) {
5647 if (subreq == state->reqs[i]) {
5648 break;
5651 if (i == num_parallel) {
5652 DEBUG(10, ("received something we did not send\n"));
5653 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5654 return;
5656 TALLOC_FREE(state->reqs[i]);
5658 if (state->sent >= state->num_files) {
5659 tevent_req_done(req);
5660 return;
5663 name = talloc_asprintf(state, "%s%8.8d", state->base_name,
5664 state->sent);
5665 if (tevent_req_nomem(name, req)) {
5666 return;
5668 state->reqs[i] = torture_createdel_send(state->reqs, state->ev,
5669 state->cli, name);
5670 if (tevent_req_nomem(state->reqs[i], req)) {
5671 return;
5673 name = talloc_move(state->reqs[i], &name);
5674 tevent_req_set_callback(state->reqs[i], torture_createdels_done, req);
5675 state->sent += 1;
5678 static NTSTATUS torture_createdels_recv(struct tevent_req *req)
5680 return tevent_req_simple_recv_ntstatus(req);
5683 struct swallow_notify_state {
5684 struct tevent_context *ev;
5685 struct cli_state *cli;
5686 uint16_t fnum;
5687 uint32_t completion_filter;
5688 bool recursive;
5689 bool (*fn)(uint32_t action, const char *name, void *priv);
5690 void *priv;
5693 static void swallow_notify_done(struct tevent_req *subreq);
5695 static struct tevent_req *swallow_notify_send(TALLOC_CTX *mem_ctx,
5696 struct tevent_context *ev,
5697 struct cli_state *cli,
5698 uint16_t fnum,
5699 uint32_t completion_filter,
5700 bool recursive,
5701 bool (*fn)(uint32_t action,
5702 const char *name,
5703 void *priv),
5704 void *priv)
5706 struct tevent_req *req, *subreq;
5707 struct swallow_notify_state *state;
5709 req = tevent_req_create(mem_ctx, &state,
5710 struct swallow_notify_state);
5711 if (req == NULL) {
5712 return NULL;
5714 state->ev = ev;
5715 state->cli = cli;
5716 state->fnum = fnum;
5717 state->completion_filter = completion_filter;
5718 state->recursive = recursive;
5719 state->fn = fn;
5720 state->priv = priv;
5722 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
5723 0xffff, state->completion_filter,
5724 state->recursive);
5725 if (tevent_req_nomem(subreq, req)) {
5726 return tevent_req_post(req, ev);
5728 tevent_req_set_callback(subreq, swallow_notify_done, req);
5729 return req;
5732 static void swallow_notify_done(struct tevent_req *subreq)
5734 struct tevent_req *req = tevent_req_callback_data(
5735 subreq, struct tevent_req);
5736 struct swallow_notify_state *state = tevent_req_data(
5737 req, struct swallow_notify_state);
5738 NTSTATUS status;
5739 uint32_t i, num_changes;
5740 struct notify_change *changes;
5742 status = cli_notify_recv(subreq, state, &num_changes, &changes);
5743 TALLOC_FREE(subreq);
5744 if (!NT_STATUS_IS_OK(status)) {
5745 DEBUG(10, ("cli_notify_recv returned %s\n",
5746 nt_errstr(status)));
5747 tevent_req_nterror(req, status);
5748 return;
5751 for (i=0; i<num_changes; i++) {
5752 state->fn(changes[i].action, changes[i].name, state->priv);
5754 TALLOC_FREE(changes);
5756 subreq = cli_notify_send(state, state->ev, state->cli, state->fnum,
5757 0xffff, state->completion_filter,
5758 state->recursive);
5759 if (tevent_req_nomem(subreq, req)) {
5760 return;
5762 tevent_req_set_callback(subreq, swallow_notify_done, req);
5765 static bool print_notifies(uint32_t action, const char *name, void *priv)
5767 if (DEBUGLEVEL > 5) {
5768 d_printf("%d %s\n", (int)action, name);
5770 return true;
5773 static void notify_bench_done(struct tevent_req *req)
5775 int *num_finished = (int *)tevent_req_callback_data_void(req);
5776 *num_finished += 1;
5779 static bool run_notify_bench(int dummy)
5781 const char *dname = "\\notify-bench";
5782 struct tevent_context *ev;
5783 NTSTATUS status;
5784 uint16_t dnum;
5785 struct tevent_req *req1, *req2;
5786 int i, num_unc_names;
5787 int num_finished = 0;
5789 printf("starting notify-bench test\n");
5791 if (use_multishare_conn) {
5792 char **unc_list;
5793 unc_list = file_lines_load(multishare_conn_fname,
5794 &num_unc_names, 0, NULL);
5795 if (!unc_list || num_unc_names <= 0) {
5796 d_printf("Failed to load unc names list from '%s'\n",
5797 multishare_conn_fname);
5798 return false;
5800 TALLOC_FREE(unc_list);
5801 } else {
5802 num_unc_names = 1;
5805 ev = tevent_context_init(talloc_tos());
5806 if (ev == NULL) {
5807 d_printf("tevent_context_init failed\n");
5808 return false;
5811 for (i=0; i<num_unc_names; i++) {
5812 struct cli_state *cli;
5813 char *base_fname;
5815 base_fname = talloc_asprintf(talloc_tos(), "%s\\file%3.3d.",
5816 dname, i);
5817 if (base_fname == NULL) {
5818 return false;
5821 if (!torture_open_connection(&cli, i)) {
5822 return false;
5825 status = cli_ntcreate(cli, dname, 0,
5826 MAXIMUM_ALLOWED_ACCESS,
5827 0, FILE_SHARE_READ|FILE_SHARE_WRITE|
5828 FILE_SHARE_DELETE,
5829 FILE_OPEN_IF, FILE_DIRECTORY_FILE, 0,
5830 &dnum);
5832 if (!NT_STATUS_IS_OK(status)) {
5833 d_printf("Could not create %s: %s\n", dname,
5834 nt_errstr(status));
5835 return false;
5838 req1 = swallow_notify_send(talloc_tos(), ev, cli, dnum,
5839 FILE_NOTIFY_CHANGE_FILE_NAME |
5840 FILE_NOTIFY_CHANGE_DIR_NAME |
5841 FILE_NOTIFY_CHANGE_ATTRIBUTES |
5842 FILE_NOTIFY_CHANGE_LAST_WRITE,
5843 false, print_notifies, NULL);
5844 if (req1 == NULL) {
5845 d_printf("Could not create notify request\n");
5846 return false;
5849 req2 = torture_createdels_send(talloc_tos(), ev, cli,
5850 base_fname, 10, torture_numops);
5851 if (req2 == NULL) {
5852 d_printf("Could not create createdels request\n");
5853 return false;
5855 TALLOC_FREE(base_fname);
5857 tevent_req_set_callback(req2, notify_bench_done,
5858 &num_finished);
5861 while (num_finished < num_unc_names) {
5862 int ret;
5863 ret = tevent_loop_once(ev);
5864 if (ret != 0) {
5865 d_printf("tevent_loop_once failed\n");
5866 return false;
5870 if (!tevent_req_poll(req2, ev)) {
5871 d_printf("tevent_req_poll failed\n");
5874 status = torture_createdels_recv(req2);
5875 d_printf("torture_createdels_recv returned %s\n", nt_errstr(status));
5877 return true;
5880 static bool run_mangle1(int dummy)
5882 struct cli_state *cli;
5883 const char *fname = "this_is_a_long_fname_to_be_mangled.txt";
5884 uint16_t fnum;
5885 fstring alt_name;
5886 NTSTATUS status;
5887 time_t change_time, access_time, write_time;
5888 SMB_OFF_T size;
5889 uint16_t mode;
5891 printf("starting mangle1 test\n");
5892 if (!torture_open_connection(&cli, 0)) {
5893 return False;
5896 cli_sockopt(cli, sockops);
5898 if (!NT_STATUS_IS_OK(cli_ntcreate(
5899 cli, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
5900 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
5901 d_printf("open %s failed: %s\n", fname, cli_errstr(cli));
5902 return false;
5904 cli_close(cli, fnum);
5906 status = cli_qpathinfo_alt_name(cli, fname, alt_name);
5907 if (!NT_STATUS_IS_OK(status)) {
5908 d_printf("cli_qpathinfo_alt_name failed: %s\n",
5909 nt_errstr(status));
5910 return false;
5912 d_printf("alt_name: %s\n", alt_name);
5914 if (!NT_STATUS_IS_OK(cli_open(cli, alt_name, O_RDONLY, DENY_NONE, &fnum))) {
5915 d_printf("cli_open(%s) failed: %s\n", alt_name,
5916 cli_errstr(cli));
5917 return false;
5919 cli_close(cli, fnum);
5921 if (!cli_qpathinfo(cli, alt_name, &change_time, &access_time,
5922 &write_time, &size, &mode)) {
5923 d_printf("cli_qpathinfo(%s) failed: %s\n", alt_name,
5924 cli_errstr(cli));
5925 return false;
5928 return true;
5931 static size_t null_source(uint8_t *buf, size_t n, void *priv)
5933 size_t *to_pull = (size_t *)priv;
5934 size_t thistime = *to_pull;
5936 thistime = MIN(thistime, n);
5937 if (thistime == 0) {
5938 return 0;
5941 memset(buf, 0, thistime);
5942 *to_pull -= thistime;
5943 return thistime;
5946 static bool run_windows_write(int dummy)
5948 struct cli_state *cli1;
5949 uint16_t fnum;
5950 int i;
5951 bool ret = false;
5952 const char *fname = "\\writetest.txt";
5953 double seconds;
5954 double kbytes;
5956 printf("starting windows_write test\n");
5957 if (!torture_open_connection(&cli1, 0)) {
5958 return False;
5961 if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
5962 printf("open failed (%s)\n", cli_errstr(cli1));
5963 return False;
5966 cli_sockopt(cli1, sockops);
5968 start_timer();
5970 for (i=0; i<torture_numops; i++) {
5971 char c = 0;
5972 off_t start = i * torture_blocksize;
5973 NTSTATUS status;
5974 size_t to_pull = torture_blocksize - 1;
5976 if (cli_write(cli1, fnum, 0, &c,
5977 start + torture_blocksize - 1, 1) != 1) {
5978 printf("cli_write failed: %s\n", cli_errstr(cli1));
5979 goto fail;
5982 status = cli_push(cli1, fnum, 0, i * torture_blocksize, torture_blocksize,
5983 null_source, &to_pull);
5984 if (!NT_STATUS_IS_OK(status)) {
5985 printf("cli_push returned: %s\n", nt_errstr(status));
5986 goto fail;
5990 seconds = end_timer();
5991 kbytes = (double)torture_blocksize * torture_numops;
5992 kbytes /= 1024;
5994 printf("Wrote %d kbytes in %.2f seconds: %d kb/sec\n", (int)kbytes,
5995 (double)seconds, (int)(kbytes/seconds));
5997 ret = true;
5998 fail:
5999 cli_close(cli1, fnum);
6000 cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
6001 torture_close_connection(cli1);
6002 return ret;
6005 static bool run_cli_echo(int dummy)
6007 struct cli_state *cli;
6008 NTSTATUS status;
6010 printf("starting cli_echo test\n");
6011 if (!torture_open_connection(&cli, 0)) {
6012 return false;
6014 cli_sockopt(cli, sockops);
6016 status = cli_echo(cli, 5, data_blob_const("hello", 5));
6018 d_printf("cli_echo returned %s\n", nt_errstr(status));
6020 torture_close_connection(cli);
6021 return NT_STATUS_IS_OK(status);
6024 static bool run_uid_regression_test(int dummy)
6026 static struct cli_state *cli;
6027 int16_t old_vuid;
6028 int16_t old_cnum;
6029 bool correct = True;
6031 printf("starting uid regression test\n");
6033 if (!torture_open_connection(&cli, 0)) {
6034 return False;
6037 cli_sockopt(cli, sockops);
6039 /* Ok - now save then logoff our current user. */
6040 old_vuid = cli->vuid;
6042 if (!cli_ulogoff(cli)) {
6043 d_printf("(%s) cli_ulogoff failed: %s\n",
6044 __location__, cli_errstr(cli));
6045 correct = false;
6046 goto out;
6049 cli->vuid = old_vuid;
6051 /* Try an operation. */
6052 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\uid_reg_test"))) {
6053 /* We expect bad uid. */
6054 if (!check_error(__LINE__, cli, ERRSRV, ERRbaduid,
6055 NT_STATUS_NO_SUCH_USER)) {
6056 return False;
6060 old_cnum = cli->cnum;
6062 /* Now try a SMBtdis with the invald vuid set to zero. */
6063 cli->vuid = 0;
6065 /* This should succeed. */
6066 if (cli_tdis(cli)) {
6067 printf("First tdis with invalid vuid should succeed.\n");
6068 } else {
6069 printf("First tdis failed (%s)\n", cli_errstr(cli));
6072 cli->vuid = old_vuid;
6073 cli->cnum = old_cnum;
6075 /* This should fail. */
6076 if (cli_tdis(cli)) {
6077 printf("Second tdis with invalid vuid should fail - succeeded instead !.\n");
6078 } else {
6079 /* Should be bad tid. */
6080 if (!check_error(__LINE__, cli, ERRSRV, ERRinvnid,
6081 NT_STATUS_NETWORK_NAME_DELETED)) {
6082 return False;
6086 cli_rmdir(cli, "\\uid_reg_test");
6088 out:
6090 cli_shutdown(cli);
6091 return correct;
6095 static const char *illegal_chars = "*\\/?<>|\":";
6096 static char force_shortname_chars[] = " +,.[];=\177";
6098 static void shortname_del_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
6100 struct cli_state *pcli = (struct cli_state *)state;
6101 fstring fname;
6102 slprintf(fname, sizeof(fname), "\\shortname\\%s", finfo->name);
6104 if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
6105 return;
6107 if (finfo->mode & aDIR) {
6108 if (!NT_STATUS_IS_OK(cli_rmdir(pcli, fname)))
6109 printf("del_fn: failed to rmdir %s\n,", fname );
6110 } else {
6111 if (!NT_STATUS_IS_OK(cli_unlink(pcli, fname, aSYSTEM | aHIDDEN)))
6112 printf("del_fn: failed to unlink %s\n,", fname );
6116 struct sn_state {
6117 int i;
6118 bool val;
6121 static void shortname_list_fn(const char *mnt, file_info *finfo, const char *name, void *state)
6123 struct sn_state *s = (struct sn_state *)state;
6124 int i = s->i;
6126 #if 0
6127 printf("shortname list: i = %d, name = |%s|, shortname = |%s|\n",
6128 i, finfo->name, finfo->short_name);
6129 #endif
6131 if (strchr(force_shortname_chars, i)) {
6132 if (!finfo->short_name[0]) {
6133 /* Shortname not created when it should be. */
6134 d_printf("(%s) ERROR: Shortname was not created for file %s containing %d\n",
6135 __location__, finfo->name, i);
6136 s->val = true;
6138 } else if (finfo->short_name[0]){
6139 /* Shortname created when it should not be. */
6140 d_printf("(%s) ERROR: Shortname %s was created for file %s\n",
6141 __location__, finfo->short_name, finfo->name);
6142 s->val = true;
6146 static bool run_shortname_test(int dummy)
6148 static struct cli_state *cli;
6149 bool correct = True;
6150 int i;
6151 struct sn_state s;
6152 char fname[20];
6154 printf("starting shortname test\n");
6156 if (!torture_open_connection(&cli, 0)) {
6157 return False;
6160 cli_sockopt(cli, sockops);
6162 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
6163 cli_list(cli, "\\shortname\\*", aDIR, shortname_del_fn, cli);
6164 cli_rmdir(cli, "\\shortname");
6166 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\shortname"))) {
6167 d_printf("(%s) cli_mkdir of \\shortname failed: %s\n",
6168 __location__, cli_errstr(cli));
6169 correct = false;
6170 goto out;
6173 strlcpy(fname, "\\shortname\\", sizeof(fname));
6174 strlcat(fname, "test .txt", sizeof(fname));
6176 s.val = false;
6178 for (i = 32; i < 128; i++) {
6179 NTSTATUS status;
6180 uint16_t fnum = (uint16_t)-1;
6182 s.i = i;
6184 if (strchr(illegal_chars, i)) {
6185 continue;
6187 fname[15] = i;
6189 status = cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
6190 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum);
6191 if (!NT_STATUS_IS_OK(status)) {
6192 d_printf("(%s) cli_nt_create of %s failed: %s\n",
6193 __location__, fname, cli_errstr(cli));
6194 correct = false;
6195 goto out;
6197 cli_close(cli, fnum);
6198 if (cli_list(cli, "\\shortname\\test*.*", 0, shortname_list_fn, &s) != 1) {
6199 d_printf("(%s) failed to list %s: %s\n",
6200 __location__, fname, cli_errstr(cli));
6201 correct = false;
6202 goto out;
6204 if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
6205 d_printf("(%s) failed to delete %s: %s\n",
6206 __location__, fname, cli_errstr(cli));
6207 correct = false;
6208 goto out;
6211 if (s.val) {
6212 correct = false;
6213 goto out;
6217 out:
6219 cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
6220 cli_list(cli, "\\shortname\\*", aDIR, shortname_del_fn, cli);
6221 cli_rmdir(cli, "\\shortname");
6222 torture_close_connection(cli);
6223 return correct;
6226 static void pagedsearch_cb(struct tevent_req *req)
6228 int rc;
6229 struct tldap_message *msg;
6230 char *dn;
6232 rc = tldap_search_paged_recv(req, talloc_tos(), &msg);
6233 if (rc != TLDAP_SUCCESS) {
6234 d_printf("tldap_search_paged_recv failed: %s\n",
6235 tldap_err2string(rc));
6236 return;
6238 if (tldap_msg_type(msg) != TLDAP_RES_SEARCH_ENTRY) {
6239 TALLOC_FREE(msg);
6240 return;
6242 if (!tldap_entry_dn(msg, &dn)) {
6243 d_printf("tldap_entry_dn failed\n");
6244 return;
6246 d_printf("%s\n", dn);
6247 TALLOC_FREE(msg);
6250 static bool run_tldap(int dummy)
6252 struct tldap_context *ld;
6253 int fd, rc;
6254 NTSTATUS status;
6255 struct sockaddr_storage addr;
6256 struct tevent_context *ev;
6257 struct tevent_req *req;
6258 char *basedn;
6260 if (!resolve_name(host, &addr, 0, false)) {
6261 d_printf("could not find host %s\n", host);
6262 return false;
6264 status = open_socket_out(&addr, 389, 9999, &fd);
6265 if (!NT_STATUS_IS_OK(status)) {
6266 d_printf("open_socket_out failed: %s\n", nt_errstr(status));
6267 return false;
6270 ld = tldap_context_create(talloc_tos(), fd);
6271 if (ld == NULL) {
6272 close(fd);
6273 d_printf("tldap_context_create failed\n");
6274 return false;
6277 rc = tldap_fetch_rootdse(ld);
6278 if (rc != TLDAP_SUCCESS) {
6279 d_printf("tldap_fetch_rootdse failed: %s\n",
6280 tldap_errstr(talloc_tos(), ld, rc));
6281 return false;
6284 basedn = tldap_talloc_single_attribute(
6285 tldap_rootdse(ld), "defaultNamingContext", talloc_tos());
6286 if (basedn == NULL) {
6287 d_printf("no defaultNamingContext\n");
6288 return false;
6290 d_printf("defaultNamingContext: %s\n", basedn);
6292 ev = tevent_context_init(talloc_tos());
6293 if (ev == NULL) {
6294 d_printf("tevent_context_init failed\n");
6295 return false;
6298 req = tldap_search_paged_send(talloc_tos(), ev, ld, basedn,
6299 TLDAP_SCOPE_SUB, "(objectclass=*)",
6300 NULL, 0, 0,
6301 NULL, 0, NULL, 0, 0, 0, 0, 5);
6302 if (req == NULL) {
6303 d_printf("tldap_search_paged_send failed\n");
6304 return false;
6306 tevent_req_set_callback(req, pagedsearch_cb, NULL);
6308 tevent_req_poll(req, ev);
6310 TALLOC_FREE(req);
6312 TALLOC_FREE(ld);
6313 return true;
6316 static bool run_streamerror(int dummy)
6318 struct cli_state *cli;
6319 const char *dname = "\\testdir";
6320 const char *streamname =
6321 "testdir:{4c8cc155-6c1e-11d1-8e41-00c04fb9386d}:$DATA";
6322 NTSTATUS status;
6323 time_t change_time, access_time, write_time;
6324 SMB_OFF_T size;
6325 uint16_t mode, fnum;
6326 bool ret = true;
6328 if (!torture_open_connection(&cli, 0)) {
6329 return false;
6332 cli_rmdir(cli, dname);
6334 status = cli_mkdir(cli, dname);
6335 if (!NT_STATUS_IS_OK(status)) {
6336 printf("mkdir failed: %s\n", nt_errstr(status));
6337 return false;
6340 cli_qpathinfo(cli, streamname, &change_time, &access_time, &write_time,
6341 &size, &mode);
6342 status = cli_nt_error(cli);
6344 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
6345 printf("pathinfo returned %s, expected "
6346 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
6347 nt_errstr(status));
6348 ret = false;
6351 status = cli_ntcreate(cli, streamname, 0x16,
6352 FILE_READ_DATA|FILE_READ_EA|
6353 FILE_READ_ATTRIBUTES|READ_CONTROL_ACCESS,
6354 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
6355 FILE_OPEN, 0, 0, &fnum);
6357 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
6358 printf("ntcreate returned %s, expected "
6359 "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
6360 nt_errstr(status));
6361 ret = false;
6365 cli_rmdir(cli, dname);
6366 return ret;
6369 static bool run_local_substitute(int dummy)
6371 bool ok = true;
6373 ok &= subst_test("%U", "bla", "", -1, -1, "bla");
6374 ok &= subst_test("%u%U", "bla", "", -1, -1, "blabla");
6375 ok &= subst_test("%g", "", "", -1, -1, "NO_GROUP");
6376 ok &= subst_test("%G", "", "", -1, -1, "NO_GROUP");
6377 ok &= subst_test("%g", "", "", -1, 0, gidtoname(0));
6378 ok &= subst_test("%G", "", "", -1, 0, gidtoname(0));
6379 ok &= subst_test("%D%u", "u", "dom", -1, 0, "domu");
6380 ok &= subst_test("%i %I", "", "", -1, -1, "0.0.0.0 0.0.0.0");
6382 /* Different captialization rules in sub_basic... */
6384 ok &= (strcmp(talloc_sub_basic(talloc_tos(), "BLA", "dom", "%U%D"),
6385 "blaDOM") == 0);
6387 return ok;
6390 static bool run_local_base64(int dummy)
6392 int i;
6393 bool ret = true;
6395 for (i=1; i<2000; i++) {
6396 DATA_BLOB blob1, blob2;
6397 char *b64;
6399 blob1.data = talloc_array(talloc_tos(), uint8_t, i);
6400 blob1.length = i;
6401 generate_random_buffer(blob1.data, blob1.length);
6403 b64 = base64_encode_data_blob(talloc_tos(), blob1);
6404 if (b64 == NULL) {
6405 d_fprintf(stderr, "base64_encode_data_blob failed "
6406 "for %d bytes\n", i);
6407 ret = false;
6409 blob2 = base64_decode_data_blob(b64);
6410 TALLOC_FREE(b64);
6412 if (data_blob_cmp(&blob1, &blob2)) {
6413 d_fprintf(stderr, "data_blob_cmp failed for %d "
6414 "bytes\n", i);
6415 ret = false;
6417 TALLOC_FREE(blob1.data);
6418 data_blob_free(&blob2);
6420 return ret;
6423 static bool run_local_gencache(int dummy)
6425 char *val;
6426 time_t tm;
6427 DATA_BLOB blob;
6429 if (!gencache_set("foo", "bar", time(NULL) + 1000)) {
6430 d_printf("%s: gencache_set() failed\n", __location__);
6431 return False;
6434 if (!gencache_get("foo", &val, &tm)) {
6435 d_printf("%s: gencache_get() failed\n", __location__);
6436 return False;
6439 if (strcmp(val, "bar") != 0) {
6440 d_printf("%s: gencache_get() returned %s, expected %s\n",
6441 __location__, val, "bar");
6442 SAFE_FREE(val);
6443 return False;
6446 SAFE_FREE(val);
6448 if (!gencache_del("foo")) {
6449 d_printf("%s: gencache_del() failed\n", __location__);
6450 return False;
6452 if (gencache_del("foo")) {
6453 d_printf("%s: second gencache_del() succeeded\n",
6454 __location__);
6455 return False;
6458 if (gencache_get("foo", &val, &tm)) {
6459 d_printf("%s: gencache_get() on deleted entry "
6460 "succeeded\n", __location__);
6461 return False;
6464 blob = data_blob_string_const_null("bar");
6465 tm = time(NULL) + 60;
6467 if (!gencache_set_data_blob("foo", &blob, tm)) {
6468 d_printf("%s: gencache_set_data_blob() failed\n", __location__);
6469 return False;
6472 if (!gencache_get_data_blob("foo", &blob, NULL, NULL)) {
6473 d_printf("%s: gencache_get_data_blob() failed\n", __location__);
6474 return False;
6477 if (strcmp((const char *)blob.data, "bar") != 0) {
6478 d_printf("%s: gencache_get_data_blob() returned %s, expected %s\n",
6479 __location__, (const char *)blob.data, "bar");
6480 data_blob_free(&blob);
6481 return False;
6484 data_blob_free(&blob);
6486 if (!gencache_del("foo")) {
6487 d_printf("%s: gencache_del() failed\n", __location__);
6488 return False;
6490 if (gencache_del("foo")) {
6491 d_printf("%s: second gencache_del() succeeded\n",
6492 __location__);
6493 return False;
6496 if (gencache_get_data_blob("foo", &blob, NULL, NULL)) {
6497 d_printf("%s: gencache_get_data_blob() on deleted entry "
6498 "succeeded\n", __location__);
6499 return False;
6502 return True;
6505 static bool rbt_testval(struct db_context *db, const char *key,
6506 const char *value)
6508 struct db_record *rec;
6509 TDB_DATA data = string_tdb_data(value);
6510 bool ret = false;
6511 NTSTATUS status;
6513 rec = db->fetch_locked(db, db, string_tdb_data(key));
6514 if (rec == NULL) {
6515 d_fprintf(stderr, "fetch_locked failed\n");
6516 goto done;
6518 status = rec->store(rec, data, 0);
6519 if (!NT_STATUS_IS_OK(status)) {
6520 d_fprintf(stderr, "store failed: %s\n", nt_errstr(status));
6521 goto done;
6523 TALLOC_FREE(rec);
6525 rec = db->fetch_locked(db, db, string_tdb_data(key));
6526 if (rec == NULL) {
6527 d_fprintf(stderr, "second fetch_locked failed\n");
6528 goto done;
6530 if ((rec->value.dsize != data.dsize)
6531 || (memcmp(rec->value.dptr, data.dptr, data.dsize) != 0)) {
6532 d_fprintf(stderr, "Got wrong data back\n");
6533 goto done;
6536 ret = true;
6537 done:
6538 TALLOC_FREE(rec);
6539 return ret;
6542 static bool run_local_rbtree(int dummy)
6544 struct db_context *db;
6545 bool ret = false;
6546 int i;
6548 db = db_open_rbt(NULL);
6550 if (db == NULL) {
6551 d_fprintf(stderr, "db_open_rbt failed\n");
6552 return false;
6555 for (i=0; i<1000; i++) {
6556 char *key, *value;
6558 if (asprintf(&key, "key%ld", random()) == -1) {
6559 goto done;
6561 if (asprintf(&value, "value%ld", random()) == -1) {
6562 SAFE_FREE(key);
6563 goto done;
6566 if (!rbt_testval(db, key, value)) {
6567 SAFE_FREE(key);
6568 SAFE_FREE(value);
6569 goto done;
6572 SAFE_FREE(value);
6573 if (asprintf(&value, "value%ld", random()) == -1) {
6574 SAFE_FREE(key);
6575 goto done;
6578 if (!rbt_testval(db, key, value)) {
6579 SAFE_FREE(key);
6580 SAFE_FREE(value);
6581 goto done;
6584 SAFE_FREE(key);
6585 SAFE_FREE(value);
6588 ret = true;
6590 done:
6591 TALLOC_FREE(db);
6592 return ret;
6595 struct talloc_dict_test {
6596 int content;
6599 static int talloc_dict_traverse_fn(DATA_BLOB key, void *data, void *priv)
6601 int *count = (int *)priv;
6602 *count += 1;
6603 return 0;
6606 static bool run_local_talloc_dict(int dummy)
6608 struct talloc_dict *dict;
6609 struct talloc_dict_test *t;
6610 int key, count;
6612 dict = talloc_dict_init(talloc_tos());
6613 if (dict == NULL) {
6614 return false;
6617 t = talloc(talloc_tos(), struct talloc_dict_test);
6618 if (t == NULL) {
6619 return false;
6622 key = 1;
6623 t->content = 1;
6624 if (!talloc_dict_set(dict, data_blob_const(&key, sizeof(key)), t)) {
6625 return false;
6628 count = 0;
6629 if (talloc_dict_traverse(dict, talloc_dict_traverse_fn, &count) != 0) {
6630 return false;
6633 if (count != 1) {
6634 return false;
6637 TALLOC_FREE(dict);
6639 return true;
6642 /* Split a path name into filename and stream name components. Canonicalise
6643 * such that an implicit $DATA token is always explicit.
6645 * The "specification" of this function can be found in the
6646 * run_local_stream_name() function in torture.c, I've tried those
6647 * combinations against a W2k3 server.
6650 static NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
6651 char **pbase, char **pstream)
6653 char *base = NULL;
6654 char *stream = NULL;
6655 char *sname; /* stream name */
6656 const char *stype; /* stream type */
6658 DEBUG(10, ("split_ntfs_stream_name called for [%s]\n", fname));
6660 sname = strchr_m(fname, ':');
6662 if (lp_posix_pathnames() || (sname == NULL)) {
6663 if (pbase != NULL) {
6664 base = talloc_strdup(mem_ctx, fname);
6665 NT_STATUS_HAVE_NO_MEMORY(base);
6667 goto done;
6670 if (pbase != NULL) {
6671 base = talloc_strndup(mem_ctx, fname, PTR_DIFF(sname, fname));
6672 NT_STATUS_HAVE_NO_MEMORY(base);
6675 sname += 1;
6677 stype = strchr_m(sname, ':');
6679 if (stype == NULL) {
6680 sname = talloc_strdup(mem_ctx, sname);
6681 stype = "$DATA";
6683 else {
6684 if (StrCaseCmp(stype, ":$DATA") != 0) {
6686 * If there is an explicit stream type, so far we only
6687 * allow $DATA. Is there anything else allowed? -- vl
6689 DEBUG(10, ("[%s] is an invalid stream type\n", stype));
6690 TALLOC_FREE(base);
6691 return NT_STATUS_OBJECT_NAME_INVALID;
6693 sname = talloc_strndup(mem_ctx, sname, PTR_DIFF(stype, sname));
6694 stype += 1;
6697 if (sname == NULL) {
6698 TALLOC_FREE(base);
6699 return NT_STATUS_NO_MEMORY;
6702 if (sname[0] == '\0') {
6704 * no stream name, so no stream
6706 goto done;
6709 if (pstream != NULL) {
6710 stream = talloc_asprintf(mem_ctx, "%s:%s", sname, stype);
6711 if (stream == NULL) {
6712 TALLOC_FREE(sname);
6713 TALLOC_FREE(base);
6714 return NT_STATUS_NO_MEMORY;
6717 * upper-case the type field
6719 strupper_m(strchr_m(stream, ':')+1);
6722 done:
6723 if (pbase != NULL) {
6724 *pbase = base;
6726 if (pstream != NULL) {
6727 *pstream = stream;
6729 return NT_STATUS_OK;
6732 static bool test_stream_name(const char *fname, const char *expected_base,
6733 const char *expected_stream,
6734 NTSTATUS expected_status)
6736 NTSTATUS status;
6737 char *base = NULL;
6738 char *stream = NULL;
6740 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &stream);
6741 if (!NT_STATUS_EQUAL(status, expected_status)) {
6742 goto error;
6745 if (!NT_STATUS_IS_OK(status)) {
6746 return true;
6749 if (base == NULL) goto error;
6751 if (strcmp(expected_base, base) != 0) goto error;
6753 if ((expected_stream != NULL) && (stream == NULL)) goto error;
6754 if ((expected_stream == NULL) && (stream != NULL)) goto error;
6756 if ((stream != NULL) && (strcmp(expected_stream, stream) != 0))
6757 goto error;
6759 TALLOC_FREE(base);
6760 TALLOC_FREE(stream);
6761 return true;
6763 error:
6764 d_fprintf(stderr, "test_stream(%s, %s, %s, %s)\n",
6765 fname, expected_base ? expected_base : "<NULL>",
6766 expected_stream ? expected_stream : "<NULL>",
6767 nt_errstr(expected_status));
6768 d_fprintf(stderr, "-> base=%s, stream=%s, status=%s\n",
6769 base ? base : "<NULL>", stream ? stream : "<NULL>",
6770 nt_errstr(status));
6771 TALLOC_FREE(base);
6772 TALLOC_FREE(stream);
6773 return false;
6776 static bool run_local_stream_name(int dummy)
6778 bool ret = true;
6780 ret &= test_stream_name(
6781 "bla", "bla", NULL, NT_STATUS_OK);
6782 ret &= test_stream_name(
6783 "bla::$DATA", "bla", NULL, NT_STATUS_OK);
6784 ret &= test_stream_name(
6785 "bla:blub:", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
6786 ret &= test_stream_name(
6787 "bla::", NULL, NULL, NT_STATUS_OBJECT_NAME_INVALID);
6788 ret &= test_stream_name(
6789 "bla::123", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
6790 ret &= test_stream_name(
6791 "bla:$DATA", "bla", "$DATA:$DATA", NT_STATUS_OK);
6792 ret &= test_stream_name(
6793 "bla:x:$DATA", "bla", "x:$DATA", NT_STATUS_OK);
6794 ret &= test_stream_name(
6795 "bla:x", "bla", "x:$DATA", NT_STATUS_OK);
6797 return ret;
6800 static bool data_blob_equal(DATA_BLOB a, DATA_BLOB b)
6802 if (a.length != b.length) {
6803 printf("a.length=%d != b.length=%d\n",
6804 (int)a.length, (int)b.length);
6805 return false;
6807 if (memcmp(a.data, b.data, a.length) != 0) {
6808 printf("a.data and b.data differ\n");
6809 return false;
6811 return true;
6814 static bool run_local_memcache(int dummy)
6816 struct memcache *cache;
6817 DATA_BLOB k1, k2;
6818 DATA_BLOB d1, d2, d3;
6819 DATA_BLOB v1, v2, v3;
6821 TALLOC_CTX *mem_ctx;
6822 char *str1, *str2;
6823 size_t size1, size2;
6824 bool ret = false;
6826 cache = memcache_init(NULL, 100);
6828 if (cache == NULL) {
6829 printf("memcache_init failed\n");
6830 return false;
6833 d1 = data_blob_const("d1", 2);
6834 d2 = data_blob_const("d2", 2);
6835 d3 = data_blob_const("d3", 2);
6837 k1 = data_blob_const("d1", 2);
6838 k2 = data_blob_const("d2", 2);
6840 memcache_add(cache, STAT_CACHE, k1, d1);
6841 memcache_add(cache, GETWD_CACHE, k2, d2);
6843 if (!memcache_lookup(cache, STAT_CACHE, k1, &v1)) {
6844 printf("could not find k1\n");
6845 return false;
6847 if (!data_blob_equal(d1, v1)) {
6848 return false;
6851 if (!memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
6852 printf("could not find k2\n");
6853 return false;
6855 if (!data_blob_equal(d2, v2)) {
6856 return false;
6859 memcache_add(cache, STAT_CACHE, k1, d3);
6861 if (!memcache_lookup(cache, STAT_CACHE, k1, &v3)) {
6862 printf("could not find replaced k1\n");
6863 return false;
6865 if (!data_blob_equal(d3, v3)) {
6866 return false;
6869 memcache_add(cache, GETWD_CACHE, k1, d1);
6871 if (memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
6872 printf("Did find k2, should have been purged\n");
6873 return false;
6876 TALLOC_FREE(cache);
6878 cache = memcache_init(NULL, 0);
6880 mem_ctx = talloc_init("foo");
6882 str1 = talloc_strdup(mem_ctx, "string1");
6883 str2 = talloc_strdup(mem_ctx, "string2");
6885 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
6886 data_blob_string_const("torture"), &str1);
6887 size1 = talloc_total_size(cache);
6889 memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
6890 data_blob_string_const("torture"), &str2);
6891 size2 = talloc_total_size(cache);
6893 printf("size1=%d, size2=%d\n", (int)size1, (int)size2);
6895 if (size2 > size1) {
6896 printf("memcache leaks memory!\n");
6897 goto fail;
6900 ret = true;
6901 fail:
6902 TALLOC_FREE(cache);
6903 return ret;
6906 static void wbclient_done(struct tevent_req *req)
6908 wbcErr wbc_err;
6909 struct winbindd_response *wb_resp;
6910 int *i = (int *)tevent_req_callback_data_void(req);
6912 wbc_err = wb_trans_recv(req, req, &wb_resp);
6913 TALLOC_FREE(req);
6914 *i += 1;
6915 d_printf("wb_trans_recv %d returned %s\n", *i, wbcErrorString(wbc_err));
6918 static bool run_local_wbclient(int dummy)
6920 struct event_context *ev;
6921 struct wb_context **wb_ctx;
6922 struct winbindd_request wb_req;
6923 bool result = false;
6924 int i, j;
6926 BlockSignals(True, SIGPIPE);
6928 ev = tevent_context_init_byname(talloc_tos(), "epoll");
6929 if (ev == NULL) {
6930 goto fail;
6933 wb_ctx = TALLOC_ARRAY(ev, struct wb_context *, nprocs);
6934 if (wb_ctx == NULL) {
6935 goto fail;
6938 ZERO_STRUCT(wb_req);
6939 wb_req.cmd = WINBINDD_PING;
6941 d_printf("nprocs=%d, numops=%d\n", (int)nprocs, (int)torture_numops);
6943 for (i=0; i<nprocs; i++) {
6944 wb_ctx[i] = wb_context_init(ev, NULL);
6945 if (wb_ctx[i] == NULL) {
6946 goto fail;
6948 for (j=0; j<torture_numops; j++) {
6949 struct tevent_req *req;
6950 req = wb_trans_send(ev, ev, wb_ctx[i],
6951 (j % 2) == 0, &wb_req);
6952 if (req == NULL) {
6953 goto fail;
6955 tevent_req_set_callback(req, wbclient_done, &i);
6959 i = 0;
6961 while (i < nprocs * torture_numops) {
6962 event_loop_once(ev);
6965 result = true;
6966 fail:
6967 TALLOC_FREE(ev);
6968 return result;
6971 static void getaddrinfo_finished(struct tevent_req *req)
6973 char *name = (char *)tevent_req_callback_data_void(req);
6974 struct addrinfo *ainfo;
6975 int res;
6977 res = getaddrinfo_recv(req, &ainfo);
6978 if (res != 0) {
6979 d_printf("gai(%s) returned %s\n", name, gai_strerror(res));
6980 return;
6982 d_printf("gai(%s) succeeded\n", name);
6983 freeaddrinfo(ainfo);
6986 static bool run_getaddrinfo_send(int dummy)
6988 TALLOC_CTX *frame = talloc_stackframe();
6989 struct fncall_context *ctx;
6990 struct tevent_context *ev;
6991 bool result = false;
6992 const char *names[4] = { "www.samba.org", "notfound.samba.org",
6993 "www.slashdot.org", "heise.de" };
6994 struct tevent_req *reqs[4];
6995 int i;
6997 ev = event_context_init(frame);
6998 if (ev == NULL) {
6999 goto fail;
7002 ctx = fncall_context_init(frame, 4);
7004 for (i=0; i<ARRAY_SIZE(names); i++) {
7005 reqs[i] = getaddrinfo_send(frame, ev, ctx, names[i], NULL,
7006 NULL);
7007 if (reqs[i] == NULL) {
7008 goto fail;
7010 tevent_req_set_callback(reqs[i], getaddrinfo_finished,
7011 (void *)names[i]);
7014 for (i=0; i<ARRAY_SIZE(reqs); i++) {
7015 tevent_loop_once(ev);
7018 result = true;
7019 fail:
7020 TALLOC_FREE(frame);
7021 return result;
7025 static double create_procs(bool (*fn)(int), bool *result)
7027 int i, status;
7028 volatile pid_t *child_status;
7029 volatile bool *child_status_out;
7030 int synccount;
7031 int tries = 8;
7033 synccount = 0;
7035 child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*nprocs);
7036 if (!child_status) {
7037 printf("Failed to setup shared memory\n");
7038 return -1;
7041 child_status_out = (volatile bool *)shm_setup(sizeof(bool)*nprocs);
7042 if (!child_status_out) {
7043 printf("Failed to setup result status shared memory\n");
7044 return -1;
7047 for (i = 0; i < nprocs; i++) {
7048 child_status[i] = 0;
7049 child_status_out[i] = True;
7052 start_timer();
7054 for (i=0;i<nprocs;i++) {
7055 procnum = i;
7056 if (fork() == 0) {
7057 pid_t mypid = getpid();
7058 sys_srandom(((int)mypid) ^ ((int)time(NULL)));
7060 slprintf(myname,sizeof(myname),"CLIENT%d", i);
7062 while (1) {
7063 if (torture_open_connection(&current_cli, i)) break;
7064 if (tries-- == 0) {
7065 printf("pid %d failed to start\n", (int)getpid());
7066 _exit(1);
7068 smb_msleep(10);
7071 child_status[i] = getpid();
7073 while (child_status[i] && end_timer() < 5) smb_msleep(2);
7075 child_status_out[i] = fn(i);
7076 _exit(0);
7080 do {
7081 synccount = 0;
7082 for (i=0;i<nprocs;i++) {
7083 if (child_status[i]) synccount++;
7085 if (synccount == nprocs) break;
7086 smb_msleep(10);
7087 } while (end_timer() < 30);
7089 if (synccount != nprocs) {
7090 printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount);
7091 *result = False;
7092 return end_timer();
7095 /* start the client load */
7096 start_timer();
7098 for (i=0;i<nprocs;i++) {
7099 child_status[i] = 0;
7102 printf("%d clients started\n", nprocs);
7104 for (i=0;i<nprocs;i++) {
7105 while (waitpid(0, &status, 0) == -1 && errno == EINTR) /* noop */ ;
7108 printf("\n");
7110 for (i=0;i<nprocs;i++) {
7111 if (!child_status_out[i]) {
7112 *result = False;
7115 return end_timer();
7118 #define FLAG_MULTIPROC 1
7120 static struct {
7121 const char *name;
7122 bool (*fn)(int);
7123 unsigned flags;
7124 } torture_ops[] = {
7125 {"FDPASS", run_fdpasstest, 0},
7126 {"LOCK1", run_locktest1, 0},
7127 {"LOCK2", run_locktest2, 0},
7128 {"LOCK3", run_locktest3, 0},
7129 {"LOCK4", run_locktest4, 0},
7130 {"LOCK5", run_locktest5, 0},
7131 {"LOCK6", run_locktest6, 0},
7132 {"LOCK7", run_locktest7, 0},
7133 {"LOCK8", run_locktest8, 0},
7134 {"LOCK9", run_locktest9, 0},
7135 {"UNLINK", run_unlinktest, 0},
7136 {"BROWSE", run_browsetest, 0},
7137 {"ATTR", run_attrtest, 0},
7138 {"TRANS2", run_trans2test, 0},
7139 {"MAXFID", run_maxfidtest, FLAG_MULTIPROC},
7140 {"TORTURE",run_torture, FLAG_MULTIPROC},
7141 {"RANDOMIPC", run_randomipc, 0},
7142 {"NEGNOWAIT", run_negprot_nowait, 0},
7143 {"NBENCH", run_nbench, 0},
7144 {"OPLOCK1", run_oplock1, 0},
7145 {"OPLOCK2", run_oplock2, 0},
7146 {"OPLOCK3", run_oplock3, 0},
7147 {"DIR", run_dirtest, 0},
7148 {"DIR1", run_dirtest1, 0},
7149 {"DENY1", torture_denytest1, 0},
7150 {"DENY2", torture_denytest2, 0},
7151 {"TCON", run_tcon_test, 0},
7152 {"TCONDEV", run_tcon_devtype_test, 0},
7153 {"RW1", run_readwritetest, 0},
7154 {"RW2", run_readwritemulti, FLAG_MULTIPROC},
7155 {"RW3", run_readwritelarge, 0},
7156 {"OPEN", run_opentest, 0},
7157 {"POSIX", run_simple_posix_open_test, 0},
7158 { "UID-REGRESSION-TEST", run_uid_regression_test, 0},
7159 { "SHORTNAME-TEST", run_shortname_test, 0},
7160 #if 1
7161 {"OPENATTR", run_openattrtest, 0},
7162 #endif
7163 {"XCOPY", run_xcopy, 0},
7164 {"RENAME", run_rename, 0},
7165 {"DELETE", run_deletetest, 0},
7166 {"PROPERTIES", run_properties, 0},
7167 {"MANGLE", torture_mangle, 0},
7168 {"MANGLE1", run_mangle1, 0},
7169 {"W2K", run_w2ktest, 0},
7170 {"TRANS2SCAN", torture_trans2_scan, 0},
7171 {"NTTRANSSCAN", torture_nttrans_scan, 0},
7172 {"UTABLE", torture_utable, 0},
7173 {"CASETABLE", torture_casetable, 0},
7174 {"ERRMAPEXTRACT", run_error_map_extract, 0},
7175 {"PIPE_NUMBER", run_pipe_number, 0},
7176 {"TCON2", run_tcon2_test, 0},
7177 {"IOCTL", torture_ioctl_test, 0},
7178 {"CHKPATH", torture_chkpath_test, 0},
7179 {"FDSESS", run_fdsesstest, 0},
7180 { "EATEST", run_eatest, 0},
7181 { "SESSSETUP_BENCH", run_sesssetup_bench, 0},
7182 { "CHAIN1", run_chain1, 0},
7183 { "CHAIN2", run_chain2, 0},
7184 { "WINDOWS-WRITE", run_windows_write, 0},
7185 { "CLI_ECHO", run_cli_echo, 0},
7186 { "GETADDRINFO", run_getaddrinfo_send, 0},
7187 { "TLDAP", run_tldap },
7188 { "STREAMERROR", run_streamerror },
7189 { "NOTIFY-BENCH", run_notify_bench },
7190 { "LOCAL-SUBSTITUTE", run_local_substitute, 0},
7191 { "LOCAL-GENCACHE", run_local_gencache, 0},
7192 { "LOCAL-TALLOC-DICT", run_local_talloc_dict, 0},
7193 { "LOCAL-BASE64", run_local_base64, 0},
7194 { "LOCAL-RBTREE", run_local_rbtree, 0},
7195 { "LOCAL-MEMCACHE", run_local_memcache, 0},
7196 { "LOCAL-STREAM-NAME", run_local_stream_name, 0},
7197 { "LOCAL-WBCLIENT", run_local_wbclient, 0},
7198 {NULL, NULL, 0}};
7202 /****************************************************************************
7203 run a specified test or "ALL"
7204 ****************************************************************************/
7205 static bool run_test(const char *name)
7207 bool ret = True;
7208 bool result = True;
7209 bool found = False;
7210 int i;
7211 double t;
7212 if (strequal(name,"ALL")) {
7213 for (i=0;torture_ops[i].name;i++) {
7214 run_test(torture_ops[i].name);
7216 found = True;
7219 for (i=0;torture_ops[i].name;i++) {
7220 fstr_sprintf(randomfname, "\\XX%x",
7221 (unsigned)random());
7223 if (strequal(name, torture_ops[i].name)) {
7224 found = True;
7225 printf("Running %s\n", name);
7226 if (torture_ops[i].flags & FLAG_MULTIPROC) {
7227 t = create_procs(torture_ops[i].fn, &result);
7228 if (!result) {
7229 ret = False;
7230 printf("TEST %s FAILED!\n", name);
7232 } else {
7233 start_timer();
7234 if (!torture_ops[i].fn(0)) {
7235 ret = False;
7236 printf("TEST %s FAILED!\n", name);
7238 t = end_timer();
7240 printf("%s took %g secs\n\n", name, t);
7244 if (!found) {
7245 printf("Did not find a test named %s\n", name);
7246 ret = False;
7249 return ret;
7253 static void usage(void)
7255 int i;
7257 printf("WARNING samba4 test suite is much more complete nowadays.\n");
7258 printf("Please use samba4 torture.\n\n");
7260 printf("Usage: smbtorture //server/share <options> TEST1 TEST2 ...\n");
7262 printf("\t-d debuglevel\n");
7263 printf("\t-U user%%pass\n");
7264 printf("\t-k use kerberos\n");
7265 printf("\t-N numprocs\n");
7266 printf("\t-n my_netbios_name\n");
7267 printf("\t-W workgroup\n");
7268 printf("\t-o num_operations\n");
7269 printf("\t-O socket_options\n");
7270 printf("\t-m maximum protocol\n");
7271 printf("\t-L use oplocks\n");
7272 printf("\t-c CLIENT.TXT specify client load file for NBENCH\n");
7273 printf("\t-A showall\n");
7274 printf("\t-p port\n");
7275 printf("\t-s seed\n");
7276 printf("\t-b unclist_filename specify multiple shares for multiple connections\n");
7277 printf("\n\n");
7279 printf("tests are:");
7280 for (i=0;torture_ops[i].name;i++) {
7281 printf(" %s", torture_ops[i].name);
7283 printf("\n");
7285 printf("default test is ALL\n");
7287 exit(1);
7290 /****************************************************************************
7291 main program
7292 ****************************************************************************/
7293 int main(int argc,char *argv[])
7295 int opt, i;
7296 char *p;
7297 int gotuser = 0;
7298 int gotpass = 0;
7299 bool correct = True;
7300 TALLOC_CTX *frame = talloc_stackframe();
7301 int seed = time(NULL);
7303 dbf = x_stdout;
7305 #ifdef HAVE_SETBUFFER
7306 setbuffer(stdout, NULL, 0);
7307 #endif
7309 load_case_tables();
7311 if (is_default_dyn_CONFIGFILE()) {
7312 if(getenv("SMB_CONF_PATH")) {
7313 set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
7316 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
7317 load_interfaces();
7319 if (argc < 2) {
7320 usage();
7323 for(p = argv[1]; *p; p++)
7324 if(*p == '\\')
7325 *p = '/';
7327 if (strncmp(argv[1], "//", 2)) {
7328 usage();
7331 fstrcpy(host, &argv[1][2]);
7332 p = strchr_m(&host[2],'/');
7333 if (!p) {
7334 usage();
7336 *p = 0;
7337 fstrcpy(share, p+1);
7339 fstrcpy(myname, get_myname(talloc_tos()));
7340 if (!*myname) {
7341 fprintf(stderr, "Failed to get my hostname.\n");
7342 return 1;
7345 if (*username == 0 && getenv("LOGNAME")) {
7346 fstrcpy(username,getenv("LOGNAME"));
7349 argc--;
7350 argv++;
7352 fstrcpy(workgroup, lp_workgroup());
7354 while ((opt = getopt(argc, argv, "p:hW:U:n:N:O:o:m:Ll:d:Aec:ks:b:B:")) != EOF) {
7355 switch (opt) {
7356 case 'p':
7357 port_to_use = atoi(optarg);
7358 break;
7359 case 's':
7360 seed = atoi(optarg);
7361 break;
7362 case 'W':
7363 fstrcpy(workgroup,optarg);
7364 break;
7365 case 'm':
7366 max_protocol = interpret_protocol(optarg, max_protocol);
7367 break;
7368 case 'N':
7369 nprocs = atoi(optarg);
7370 break;
7371 case 'o':
7372 torture_numops = atoi(optarg);
7373 break;
7374 case 'd':
7375 DEBUGLEVEL = atoi(optarg);
7376 break;
7377 case 'O':
7378 sockops = optarg;
7379 break;
7380 case 'L':
7381 use_oplocks = True;
7382 break;
7383 case 'l':
7384 local_path = optarg;
7385 break;
7386 case 'A':
7387 torture_showall = True;
7388 break;
7389 case 'n':
7390 fstrcpy(myname, optarg);
7391 break;
7392 case 'c':
7393 client_txt = optarg;
7394 break;
7395 case 'e':
7396 do_encrypt = true;
7397 break;
7398 case 'k':
7399 #ifdef HAVE_KRB5
7400 use_kerberos = True;
7401 #else
7402 d_printf("No kerberos support compiled in\n");
7403 exit(1);
7404 #endif
7405 break;
7406 case 'U':
7407 gotuser = 1;
7408 fstrcpy(username,optarg);
7409 p = strchr_m(username,'%');
7410 if (p) {
7411 *p = 0;
7412 fstrcpy(password, p+1);
7413 gotpass = 1;
7415 break;
7416 case 'b':
7417 fstrcpy(multishare_conn_fname, optarg);
7418 use_multishare_conn = True;
7419 break;
7420 case 'B':
7421 torture_blocksize = atoi(optarg);
7422 break;
7423 default:
7424 printf("Unknown option %c (%d)\n", (char)opt, opt);
7425 usage();
7429 d_printf("using seed %d\n", seed);
7431 srandom(seed);
7433 if(use_kerberos && !gotuser) gotpass = True;
7435 while (!gotpass) {
7436 p = getpass("Password:");
7437 if (p) {
7438 fstrcpy(password, p);
7439 gotpass = 1;
7443 printf("host=%s share=%s user=%s myname=%s\n",
7444 host, share, username, myname);
7446 if (argc == optind) {
7447 correct = run_test("ALL");
7448 } else {
7449 for (i=optind;i<argc;i++) {
7450 if (!run_test(argv[i])) {
7451 correct = False;
7456 TALLOC_FREE(frame);
7458 if (correct) {
7459 return(0);
7460 } else {
7461 return(1);