2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1997-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "wbc_async.h"
26 static fstring host, workgroup, share, password, username, myname;
27 static int max_protocol = PROTOCOL_NT1;
28 static const char *sockops="TCP_NODELAY";
30 static int port_to_use=0;
31 int torture_numops=100;
32 int torture_blocksize=1024*1024;
33 static int procnum; /* records process count number when forking */
34 static struct cli_state *current_cli;
35 static fstring randomfname;
36 static bool use_oplocks;
37 static bool use_level_II_oplocks;
38 static const char *client_txt = "client_oplocks.txt";
39 static bool use_kerberos;
40 static fstring multishare_conn_fname;
41 static bool use_multishare_conn = False;
42 static bool do_encrypt;
44 bool torture_showall = False;
46 static double create_procs(bool (*fn)(int), bool *result);
49 static struct timeval tp1,tp2;
52 void start_timer(void)
57 double end_timer(void)
60 return((tp2.tv_sec - tp1.tv_sec) +
61 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
65 /* return a pointer to a anonymous shared memory segment of size "size"
66 which will persist across fork() but will disappear when all processes
69 The memory is not zeroed
71 This function uses system5 shared memory. It takes advantage of a property
72 that the memory is not destroyed if it is attached when the id is removed
74 void *shm_setup(int size)
79 shmid = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
81 printf("can't get shared memory\n");
84 ret = (void *)shmat(shmid, 0, 0);
85 if (!ret || ret == (void *)-1) {
86 printf("can't attach to shared memory\n");
89 /* the following releases the ipc, but note that this process
90 and all its children will still have access to the memory, its
91 just that the shmid is no longer valid for other shm calls. This
92 means we don't leave behind lots of shm segments after we exit
94 See Stevens "advanced programming in unix env" for details
96 shmctl(shmid, IPC_RMID, 0);
101 /********************************************************************
102 Ensure a connection is encrypted.
103 ********************************************************************/
105 static bool force_cli_encryption(struct cli_state *c,
106 const char *sharename)
109 uint32 caplow, caphigh;
112 if (!SERVER_HAS_UNIX_CIFS(c)) {
113 d_printf("Encryption required and "
114 "server that doesn't support "
115 "UNIX extensions - failing connect\n");
119 if (!cli_unix_extensions_version(c, &major, &minor, &caplow, &caphigh)) {
120 d_printf("Encryption required and "
121 "can't get UNIX CIFS extensions "
122 "version from server.\n");
126 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
127 d_printf("Encryption required and "
128 "share %s doesn't support "
129 "encryption.\n", sharename);
133 if (c->use_kerberos) {
134 status = cli_gss_smb_encryption_start(c);
136 status = cli_raw_ntlm_smb_encryption_start(c,
142 if (!NT_STATUS_IS_OK(status)) {
143 d_printf("Encryption required and "
144 "setup failed with error %s.\n",
153 static struct cli_state *open_nbt_connection(void)
155 struct nmb_name called, calling;
156 struct sockaddr_storage ss;
160 make_nmb_name(&calling, myname, 0x0);
161 make_nmb_name(&called , host, 0x20);
165 if (!(c = cli_initialise())) {
166 printf("Failed initialize cli_struct to connect with %s\n", host);
170 c->port = port_to_use;
172 status = cli_connect(c, host, &ss);
173 if (!NT_STATUS_IS_OK(status)) {
174 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
178 c->use_kerberos = use_kerberos;
180 c->timeout = 120000; /* set a really long timeout (2 minutes) */
181 if (use_oplocks) c->use_oplocks = True;
182 if (use_level_II_oplocks) c->use_level_II_oplocks = True;
184 if (!cli_session_request(c, &calling, &called)) {
186 * Well, that failed, try *SMBSERVER ...
187 * However, we must reconnect as well ...
189 status = cli_connect(c, host, &ss);
190 if (!NT_STATUS_IS_OK(status)) {
191 printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
195 make_nmb_name(&called, "*SMBSERVER", 0x20);
196 if (!cli_session_request(c, &calling, &called)) {
197 printf("%s rejected the session\n",host);
198 printf("We tried with a called name of %s & %s\n",
208 /* Insert a NULL at the first separator of the given path and return a pointer
209 * to the remainder of the string.
212 terminate_path_at_separator(char * path)
220 if ((p = strchr_m(path, '/'))) {
225 if ((p = strchr_m(path, '\\'))) {
235 parse a //server/share type UNC name
237 bool smbcli_parse_unc(const char *unc_name, TALLOC_CTX *mem_ctx,
238 char **hostname, char **sharename)
242 *hostname = *sharename = NULL;
244 if (strncmp(unc_name, "\\\\", 2) &&
245 strncmp(unc_name, "//", 2)) {
249 *hostname = talloc_strdup(mem_ctx, &unc_name[2]);
250 p = terminate_path_at_separator(*hostname);
253 *sharename = talloc_strdup(mem_ctx, p);
254 terminate_path_at_separator(*sharename);
257 if (*hostname && *sharename) {
261 TALLOC_FREE(*hostname);
262 TALLOC_FREE(*sharename);
266 static bool torture_open_connection_share(struct cli_state **c,
267 const char *hostname,
268 const char *sharename)
275 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
277 status = cli_full_connection(c, myname,
278 hostname, NULL, port_to_use,
281 password, flags, Undefined, &retry);
282 if (!NT_STATUS_IS_OK(status)) {
283 printf("failed to open share connection: //%s/%s port:%d - %s\n",
284 hostname, sharename, port_to_use, nt_errstr(status));
288 if (use_oplocks) (*c)->use_oplocks = True;
289 if (use_level_II_oplocks) (*c)->use_level_II_oplocks = True;
290 (*c)->timeout = 120000; /* set a really long timeout (2 minutes) */
293 return force_cli_encryption(*c,
299 bool torture_open_connection(struct cli_state **c, int conn_index)
301 char **unc_list = NULL;
302 int num_unc_names = 0;
305 if (use_multishare_conn==True) {
307 unc_list = file_lines_load(multishare_conn_fname, &num_unc_names, 0, NULL);
308 if (!unc_list || num_unc_names <= 0) {
309 printf("Failed to load unc names list from '%s'\n", multishare_conn_fname);
313 if (!smbcli_parse_unc(unc_list[conn_index % num_unc_names],
315 printf("Failed to parse UNC name %s\n",
316 unc_list[conn_index % num_unc_names]);
317 TALLOC_FREE(unc_list);
321 result = torture_open_connection_share(c, h, s);
323 /* h, s were copied earlier */
324 TALLOC_FREE(unc_list);
328 return torture_open_connection_share(c, host, share);
331 bool torture_cli_session_setup2(struct cli_state *cli, uint16 *new_vuid)
333 uint16 old_vuid = cli->vuid;
334 fstring old_user_name;
335 size_t passlen = strlen(password);
339 fstrcpy(old_user_name, cli->user_name);
341 ret = NT_STATUS_IS_OK(cli_session_setup(cli, username,
345 *new_vuid = cli->vuid;
346 cli->vuid = old_vuid;
347 status = cli_set_username(cli, old_user_name);
348 if (!NT_STATUS_IS_OK(status)) {
355 bool torture_close_connection(struct cli_state *c)
359 printf("tdis failed (%s)\n", cli_errstr(c));
369 /* check if the server produced the expected error code */
370 static bool check_error(int line, struct cli_state *c,
371 uint8 eclass, uint32 ecode, NTSTATUS nterr)
373 if (cli_is_dos_error(c)) {
377 /* Check DOS error */
379 cli_dos_error(c, &cclass, &num);
381 if (eclass != cclass || ecode != num) {
382 printf("unexpected error code class=%d code=%d\n",
383 (int)cclass, (int)num);
384 printf(" expected %d/%d %s (line=%d)\n",
385 (int)eclass, (int)ecode, nt_errstr(nterr), line);
394 status = cli_nt_error(c);
396 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
397 printf("unexpected error code %s\n", nt_errstr(status));
398 printf(" expected %s (line=%d)\n", nt_errstr(nterr), line);
407 static bool wait_lock(struct cli_state *c, int fnum, uint32 offset, uint32 len)
409 while (!cli_lock(c, fnum, offset, len, -1, WRITE_LOCK)) {
410 if (!check_error(__LINE__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
416 static bool rw_torture(struct cli_state *c)
418 const char *lockfname = "\\torture.lck";
422 pid_t pid2, pid = getpid();
427 memset(buf, '\0', sizeof(buf));
429 fnum2 = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
432 fnum2 = cli_open(c, lockfname, O_RDWR, DENY_NONE);
434 printf("open of %s failed (%s)\n", lockfname, cli_errstr(c));
439 for (i=0;i<torture_numops;i++) {
440 unsigned n = (unsigned)sys_random()%10;
442 printf("%d\r", i); fflush(stdout);
444 slprintf(fname, sizeof(fstring) - 1, "\\torture.%u", n);
446 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
450 fnum = cli_open(c, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
452 printf("open failed (%s)\n", cli_errstr(c));
457 if (cli_write(c, fnum, 0, (char *)&pid, 0, sizeof(pid)) != sizeof(pid)) {
458 printf("write failed (%s)\n", cli_errstr(c));
463 if (cli_write(c, fnum, 0, (char *)buf,
464 sizeof(pid)+(j*sizeof(buf)),
465 sizeof(buf)) != sizeof(buf)) {
466 printf("write failed (%s)\n", cli_errstr(c));
473 if (cli_read(c, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
474 printf("read failed (%s)\n", cli_errstr(c));
479 printf("data corruption!\n");
483 if (!cli_close(c, fnum)) {
484 printf("close failed (%s)\n", cli_errstr(c));
488 if (!cli_unlink(c, fname)) {
489 printf("unlink failed (%s)\n", cli_errstr(c));
493 if (!cli_unlock(c, fnum2, n*sizeof(int), sizeof(int))) {
494 printf("unlock failed (%s)\n", cli_errstr(c));
500 cli_unlink(c, lockfname);
507 static bool run_torture(int dummy)
509 struct cli_state *cli;
514 cli_sockopt(cli, sockops);
516 ret = rw_torture(cli);
518 if (!torture_close_connection(cli)) {
525 static bool rw_torture3(struct cli_state *c, char *lockfname)
532 unsigned countprev = 0;
537 for (i = 0; i < sizeof(buf); i += sizeof(uint32))
539 SIVAL(buf, i, sys_random());
544 fnum = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
547 printf("first open read/write of %s failed (%s)\n",
548 lockfname, cli_errstr(c));
554 for (i = 0; i < 500 && fnum == -1; i++)
556 fnum = cli_open(c, lockfname, O_RDONLY,
561 printf("second open read-only of %s failed (%s)\n",
562 lockfname, cli_errstr(c));
568 for (count = 0; count < sizeof(buf); count += sent)
570 if (count >= countprev) {
571 printf("%d %8d\r", i, count);
574 countprev += (sizeof(buf) / 20);
579 sent = ((unsigned)sys_random()%(20))+ 1;
580 if (sent > sizeof(buf) - count)
582 sent = sizeof(buf) - count;
585 if (cli_write(c, fnum, 0, buf+count, count, (size_t)sent) != sent) {
586 printf("write failed (%s)\n", cli_errstr(c));
592 sent = cli_read(c, fnum, buf_rd+count, count,
596 printf("read failed offset:%d size:%ld (%s)\n",
597 count, (unsigned long)sizeof(buf)-count,
604 if (memcmp(buf_rd+count, buf+count, sent) != 0)
606 printf("read/write compare failed\n");
607 printf("offset: %d req %ld recvd %ld\n", count, (unsigned long)sizeof(buf)-count, (unsigned long)sent);
616 if (!cli_close(c, fnum)) {
617 printf("close failed (%s)\n", cli_errstr(c));
624 static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
626 const char *lockfname = "\\torture2.lck";
635 if (!cli_unlink(c1, lockfname)) {
636 printf("unlink failed (%s) (normal, this file should not exist)\n", cli_errstr(c1));
639 fnum1 = cli_open(c1, lockfname, O_RDWR | O_CREAT | O_EXCL,
642 printf("first open read/write of %s failed (%s)\n",
643 lockfname, cli_errstr(c1));
646 fnum2 = cli_open(c2, lockfname, O_RDONLY,
649 printf("second open read-only of %s failed (%s)\n",
650 lockfname, cli_errstr(c2));
651 cli_close(c1, fnum1);
655 for (i=0;i<torture_numops;i++)
657 size_t buf_size = ((unsigned)sys_random()%(sizeof(buf)-1))+ 1;
659 printf("%d\r", i); fflush(stdout);
662 generate_random_buffer((unsigned char *)buf, buf_size);
664 if (cli_write(c1, fnum1, 0, buf, 0, buf_size) != buf_size) {
665 printf("write failed (%s)\n", cli_errstr(c1));
670 if ((bytes_read = cli_read(c2, fnum2, buf_rd, 0, buf_size)) != buf_size) {
671 printf("read failed (%s)\n", cli_errstr(c2));
672 printf("read %d, expected %ld\n", (int)bytes_read,
673 (unsigned long)buf_size);
678 if (memcmp(buf_rd, buf, buf_size) != 0)
680 printf("read/write compare failed\n");
686 if (!cli_close(c2, fnum2)) {
687 printf("close failed (%s)\n", cli_errstr(c2));
690 if (!cli_close(c1, fnum1)) {
691 printf("close failed (%s)\n", cli_errstr(c1));
695 if (!cli_unlink(c1, lockfname)) {
696 printf("unlink failed (%s)\n", cli_errstr(c1));
703 static bool run_readwritetest(int dummy)
705 static struct cli_state *cli1, *cli2;
706 bool test1, test2 = False;
708 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
711 cli_sockopt(cli1, sockops);
712 cli_sockopt(cli2, sockops);
714 printf("starting readwritetest\n");
716 test1 = rw_torture2(cli1, cli2);
717 printf("Passed readwritetest v1: %s\n", BOOLSTR(test1));
720 test2 = rw_torture2(cli1, cli1);
721 printf("Passed readwritetest v2: %s\n", BOOLSTR(test2));
724 if (!torture_close_connection(cli1)) {
728 if (!torture_close_connection(cli2)) {
732 return (test1 && test2);
735 static bool run_readwritemulti(int dummy)
737 struct cli_state *cli;
742 cli_sockopt(cli, sockops);
744 printf("run_readwritemulti: fname %s\n", randomfname);
745 test = rw_torture3(cli, randomfname);
747 if (!torture_close_connection(cli)) {
754 static bool run_readwritelarge(int dummy)
756 static struct cli_state *cli1;
758 const char *lockfname = "\\large.dat";
763 if (!torture_open_connection(&cli1, 0)) {
766 cli_sockopt(cli1, sockops);
767 memset(buf,'\0',sizeof(buf));
769 cli1->max_xmit = 128*1024;
771 printf("starting readwritelarge\n");
773 cli_unlink(cli1, lockfname);
775 fnum1 = cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE);
777 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
781 cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf));
783 if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
784 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
788 if (fsize == sizeof(buf))
789 printf("readwritelarge test 1 succeeded (size = %lx)\n",
790 (unsigned long)fsize);
792 printf("readwritelarge test 1 failed (size = %lx)\n",
793 (unsigned long)fsize);
797 if (!cli_close(cli1, fnum1)) {
798 printf("close failed (%s)\n", cli_errstr(cli1));
802 if (!cli_unlink(cli1, lockfname)) {
803 printf("unlink failed (%s)\n", cli_errstr(cli1));
807 fnum1 = cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE);
809 printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
813 cli1->max_xmit = 4*1024;
815 cli_smbwrite(cli1, fnum1, buf, 0, sizeof(buf));
817 if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
818 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
822 if (fsize == sizeof(buf))
823 printf("readwritelarge test 2 succeeded (size = %lx)\n",
824 (unsigned long)fsize);
826 printf("readwritelarge test 2 failed (size = %lx)\n",
827 (unsigned long)fsize);
832 /* ToDo - set allocation. JRA */
833 if(!cli_set_allocation_size(cli1, fnum1, 0)) {
834 printf("set allocation size to zero failed (%s)\n", cli_errstr(&cli1));
837 if (!cli_qfileinfo(cli1, fnum1, NULL, &fsize, NULL, NULL, NULL, NULL, NULL)) {
838 printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
842 printf("readwritelarge test 3 (truncate test) succeeded (size = %x)\n", fsize);
845 if (!cli_close(cli1, fnum1)) {
846 printf("close failed (%s)\n", cli_errstr(cli1));
850 if (!torture_close_connection(cli1)) {
859 #define ival(s) strtol(s, NULL, 0)
861 /* run a test that simulates an approximate netbench client load */
862 static bool run_netbench(int client)
864 struct cli_state *cli;
869 const char *params[20];
876 cli_sockopt(cli, sockops);
880 slprintf(cname,sizeof(cname)-1, "client%d", client);
882 f = fopen(client_txt, "r");
889 while (fgets(line, sizeof(line)-1, f)) {
893 line[strlen(line)-1] = 0;
895 /* printf("[%d] %s\n", line_count, line); */
897 all_string_sub(line,"client1", cname, sizeof(line));
899 /* parse the command parameters */
900 params[0] = strtok_r(line, " ", &saveptr);
902 while (params[i]) params[++i] = strtok_r(NULL, " ", &saveptr);
908 if (!strncmp(params[0],"SMB", 3)) {
909 printf("ERROR: You are using a dbench 1 load file\n");
913 if (!strcmp(params[0],"NTCreateX")) {
914 nb_createx(params[1], ival(params[2]), ival(params[3]),
916 } else if (!strcmp(params[0],"Close")) {
917 nb_close(ival(params[1]));
918 } else if (!strcmp(params[0],"Rename")) {
919 nb_rename(params[1], params[2]);
920 } else if (!strcmp(params[0],"Unlink")) {
921 nb_unlink(params[1]);
922 } else if (!strcmp(params[0],"Deltree")) {
923 nb_deltree(params[1]);
924 } else if (!strcmp(params[0],"Rmdir")) {
926 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
927 nb_qpathinfo(params[1]);
928 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
929 nb_qfileinfo(ival(params[1]));
930 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
931 nb_qfsinfo(ival(params[1]));
932 } else if (!strcmp(params[0],"FIND_FIRST")) {
933 nb_findfirst(params[1]);
934 } else if (!strcmp(params[0],"WriteX")) {
935 nb_writex(ival(params[1]),
936 ival(params[2]), ival(params[3]), ival(params[4]));
937 } else if (!strcmp(params[0],"ReadX")) {
938 nb_readx(ival(params[1]),
939 ival(params[2]), ival(params[3]), ival(params[4]));
940 } else if (!strcmp(params[0],"Flush")) {
941 nb_flush(ival(params[1]));
943 printf("Unknown operation %s\n", params[0]);
951 if (!torture_close_connection(cli)) {
959 /* run a test that simulates an approximate netbench client load */
960 static bool run_nbench(int dummy)
969 signal(SIGALRM, nb_alarm);
971 t = create_procs(run_netbench, &correct);
974 printf("\nThroughput %g MB/sec\n",
975 1.0e-6 * nbio_total() / t);
981 This test checks for two things:
983 1) correct support for retaining locks over a close (ie. the server
984 must not use posix semantics)
985 2) support for lock timeouts
987 static bool run_locktest1(int dummy)
989 struct cli_state *cli1, *cli2;
990 const char *fname = "\\lockt1.lck";
991 int fnum1, fnum2, fnum3;
993 unsigned lock_timeout;
995 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
998 cli_sockopt(cli1, sockops);
999 cli_sockopt(cli2, sockops);
1001 printf("starting locktest1\n");
1003 cli_unlink(cli1, fname);
1005 fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1007 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1010 fnum2 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1012 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli1));
1015 fnum3 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
1017 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli2));
1021 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
1022 printf("lock1 failed (%s)\n", cli_errstr(cli1));
1027 if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1028 printf("lock2 succeeded! This is a locking bug\n");
1031 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1032 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1036 lock_timeout = (1 + (random() % 20));
1037 printf("Testing lock timeout with timeout=%u\n", lock_timeout);
1039 if (cli_lock(cli2, fnum3, 0, 4, lock_timeout * 1000, WRITE_LOCK)) {
1040 printf("lock3 succeeded! This is a locking bug\n");
1043 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1044 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1048 if (ABS(t2 - t1) < lock_timeout-1) {
1049 printf("error: This server appears not to support timed lock requests\n");
1052 printf("server slept for %u seconds for a %u second timeout\n",
1053 (unsigned int)(t2-t1), lock_timeout);
1055 if (!cli_close(cli1, fnum2)) {
1056 printf("close1 failed (%s)\n", cli_errstr(cli1));
1060 if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
1061 printf("lock4 succeeded! This is a locking bug\n");
1064 if (!check_error(__LINE__, cli2, ERRDOS, ERRlock,
1065 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1068 if (!cli_close(cli1, fnum1)) {
1069 printf("close2 failed (%s)\n", cli_errstr(cli1));
1073 if (!cli_close(cli2, fnum3)) {
1074 printf("close3 failed (%s)\n", cli_errstr(cli2));
1078 if (!cli_unlink(cli1, fname)) {
1079 printf("unlink failed (%s)\n", cli_errstr(cli1));
1084 if (!torture_close_connection(cli1)) {
1088 if (!torture_close_connection(cli2)) {
1092 printf("Passed locktest1\n");
1097 this checks to see if a secondary tconx can use open files from an
1100 static bool run_tcon_test(int dummy)
1102 static struct cli_state *cli;
1103 const char *fname = "\\tcontest.tmp";
1105 uint16 cnum1, cnum2, cnum3;
1106 uint16 vuid1, vuid2;
1111 memset(buf, '\0', sizeof(buf));
1113 if (!torture_open_connection(&cli, 0)) {
1116 cli_sockopt(cli, sockops);
1118 printf("starting tcontest\n");
1120 cli_unlink(cli, fname);
1122 fnum1 = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1124 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1131 if (cli_write(cli, fnum1, 0, buf, 130, 4) != 4) {
1132 printf("initial write failed (%s)", cli_errstr(cli));
1136 status = cli_tcon_andx(cli, share, "?????",
1137 password, strlen(password)+1);
1138 if (!NT_STATUS_IS_OK(status)) {
1139 printf("%s refused 2nd tree connect (%s)\n", host,
1146 cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
1147 vuid2 = cli->vuid + 1;
1149 /* try a write with the wrong tid */
1152 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1153 printf("* server allows write with wrong TID\n");
1156 printf("server fails write with wrong TID : %s\n", cli_errstr(cli));
1160 /* try a write with an invalid tid */
1163 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1164 printf("* server allows write with invalid TID\n");
1167 printf("server fails write with invalid TID : %s\n", cli_errstr(cli));
1170 /* try a write with an invalid vuid */
1174 if (cli_write(cli, fnum1, 0, buf, 130, 4) == 4) {
1175 printf("* server allows write with invalid VUID\n");
1178 printf("server fails write with invalid VUID : %s\n", cli_errstr(cli));
1184 if (!cli_close(cli, fnum1)) {
1185 printf("close failed (%s)\n", cli_errstr(cli));
1191 if (!cli_tdis(cli)) {
1192 printf("secondary tdis failed (%s)\n", cli_errstr(cli));
1198 if (!torture_close_connection(cli)) {
1207 checks for old style tcon support
1209 static bool run_tcon2_test(int dummy)
1211 static struct cli_state *cli;
1212 uint16 cnum, max_xmit;
1216 if (!torture_open_connection(&cli, 0)) {
1219 cli_sockopt(cli, sockops);
1221 printf("starting tcon2 test\n");
1223 if (asprintf(&service, "\\\\%s\\%s", host, share) == -1) {
1227 status = cli_raw_tcon(cli, service, password, "?????", &max_xmit, &cnum);
1229 if (!NT_STATUS_IS_OK(status)) {
1230 printf("tcon2 failed : %s\n", cli_errstr(cli));
1232 printf("tcon OK : max_xmit=%d cnum=%d tid=%d\n",
1233 (int)max_xmit, (int)cnum, SVAL(cli->inbuf, smb_tid));
1236 if (!torture_close_connection(cli)) {
1240 printf("Passed tcon2 test\n");
1244 static bool tcon_devtest(struct cli_state *cli,
1245 const char *myshare, const char *devtype,
1246 const char *return_devtype,
1247 NTSTATUS expected_error)
1252 status = cli_tcon_andx(cli, myshare, devtype,
1253 password, strlen(password)+1);
1255 if (NT_STATUS_IS_OK(expected_error)) {
1256 if (NT_STATUS_IS_OK(status)) {
1257 if (strcmp(cli->dev, return_devtype) == 0) {
1260 printf("tconX to share %s with type %s "
1261 "succeeded but returned the wrong "
1262 "device type (got [%s] but should have got [%s])\n",
1263 myshare, devtype, cli->dev, return_devtype);
1267 printf("tconX to share %s with type %s "
1268 "should have succeeded but failed\n",
1274 if (NT_STATUS_IS_OK(status)) {
1275 printf("tconx to share %s with type %s "
1276 "should have failed but succeeded\n",
1280 if (NT_STATUS_EQUAL(cli_nt_error(cli),
1284 printf("Returned unexpected error\n");
1293 checks for correct tconX support
1295 static bool run_tcon_devtype_test(int dummy)
1297 static struct cli_state *cli1 = NULL;
1303 status = cli_full_connection(&cli1, myname,
1304 host, NULL, port_to_use,
1306 username, workgroup,
1307 password, flags, Undefined, &retry);
1309 if (!NT_STATUS_IS_OK(status)) {
1310 printf("could not open connection\n");
1314 if (!tcon_devtest(cli1, "IPC$", "A:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1317 if (!tcon_devtest(cli1, "IPC$", "?????", "IPC", NT_STATUS_OK))
1320 if (!tcon_devtest(cli1, "IPC$", "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1323 if (!tcon_devtest(cli1, "IPC$", "IPC", "IPC", NT_STATUS_OK))
1326 if (!tcon_devtest(cli1, "IPC$", "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1329 if (!tcon_devtest(cli1, share, "A:", "A:", NT_STATUS_OK))
1332 if (!tcon_devtest(cli1, share, "?????", "A:", NT_STATUS_OK))
1335 if (!tcon_devtest(cli1, share, "LPT:", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1338 if (!tcon_devtest(cli1, share, "IPC", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1341 if (!tcon_devtest(cli1, share, "FOOBA", NULL, NT_STATUS_BAD_DEVICE_TYPE))
1347 printf("Passed tcondevtest\n");
1354 This test checks that
1356 1) the server supports multiple locking contexts on the one SMB
1357 connection, distinguished by PID.
1359 2) the server correctly fails overlapping locks made by the same PID (this
1360 goes against POSIX behaviour, which is why it is tricky to implement)
1362 3) the server denies unlock requests by an incorrect client PID
1364 static bool run_locktest2(int dummy)
1366 static struct cli_state *cli;
1367 const char *fname = "\\lockt2.lck";
1368 int fnum1, fnum2, fnum3;
1369 bool correct = True;
1371 if (!torture_open_connection(&cli, 0)) {
1375 cli_sockopt(cli, sockops);
1377 printf("starting locktest2\n");
1379 cli_unlink(cli, fname);
1383 fnum1 = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1385 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
1389 fnum2 = cli_open(cli, fname, O_RDWR, DENY_NONE);
1391 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli));
1397 fnum3 = cli_open(cli, fname, O_RDWR, DENY_NONE);
1399 printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli));
1405 if (!cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1406 printf("lock1 failed (%s)\n", cli_errstr(cli));
1410 if (cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
1411 printf("WRITE lock1 succeeded! This is a locking bug\n");
1414 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1415 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1418 if (cli_lock(cli, fnum2, 0, 4, 0, WRITE_LOCK)) {
1419 printf("WRITE lock2 succeeded! This is a locking bug\n");
1422 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1423 NT_STATUS_LOCK_NOT_GRANTED)) return False;
1426 if (cli_lock(cli, fnum2, 0, 4, 0, READ_LOCK)) {
1427 printf("READ lock2 succeeded! This is a locking bug\n");
1430 if (!check_error(__LINE__, cli, ERRDOS, ERRlock,
1431 NT_STATUS_FILE_LOCK_CONFLICT)) return False;
1434 if (!cli_lock(cli, fnum1, 100, 4, 0, WRITE_LOCK)) {
1435 printf("lock at 100 failed (%s)\n", cli_errstr(cli));
1438 if (cli_unlock(cli, fnum1, 100, 4)) {
1439 printf("unlock at 100 succeeded! This is a locking bug\n");
1443 if (cli_unlock(cli, fnum1, 0, 4)) {
1444 printf("unlock1 succeeded! This is a locking bug\n");
1447 if (!check_error(__LINE__, cli,
1449 NT_STATUS_RANGE_NOT_LOCKED)) return False;
1452 if (cli_unlock(cli, fnum1, 0, 8)) {
1453 printf("unlock2 succeeded! This is a locking bug\n");
1456 if (!check_error(__LINE__, cli,
1458 NT_STATUS_RANGE_NOT_LOCKED)) return False;
1461 if (cli_lock(cli, fnum3, 0, 4, 0, WRITE_LOCK)) {
1462 printf("lock3 succeeded! This is a locking bug\n");
1465 if (!check_error(__LINE__, cli, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
1470 if (!cli_close(cli, fnum1)) {
1471 printf("close1 failed (%s)\n", cli_errstr(cli));
1475 if (!cli_close(cli, fnum2)) {
1476 printf("close2 failed (%s)\n", cli_errstr(cli));
1480 if (!cli_close(cli, fnum3)) {
1481 printf("close3 failed (%s)\n", cli_errstr(cli));
1485 if (!torture_close_connection(cli)) {
1489 printf("locktest2 finished\n");
1496 This test checks that
1498 1) the server supports the full offset range in lock requests
1500 static bool run_locktest3(int dummy)
1502 static struct cli_state *cli1, *cli2;
1503 const char *fname = "\\lockt3.lck";
1504 int fnum1, fnum2, i;
1506 bool correct = True;
1508 #define NEXT_OFFSET offset += (~(uint32)0) / torture_numops
1510 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1513 cli_sockopt(cli1, sockops);
1514 cli_sockopt(cli2, sockops);
1516 printf("starting locktest3\n");
1518 cli_unlink(cli1, fname);
1520 fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1522 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
1525 fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
1527 printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli2));
1531 for (offset=i=0;i<torture_numops;i++) {
1533 if (!cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1534 printf("lock1 %d failed (%s)\n",
1540 if (!cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1541 printf("lock2 %d failed (%s)\n",
1548 for (offset=i=0;i<torture_numops;i++) {
1551 if (cli_lock(cli1, fnum1, offset-2, 1, 0, WRITE_LOCK)) {
1552 printf("error: lock1 %d succeeded!\n", i);
1556 if (cli_lock(cli2, fnum2, offset-1, 1, 0, WRITE_LOCK)) {
1557 printf("error: lock2 %d succeeded!\n", i);
1561 if (cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
1562 printf("error: lock3 %d succeeded!\n", i);
1566 if (cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
1567 printf("error: lock4 %d succeeded!\n", i);
1572 for (offset=i=0;i<torture_numops;i++) {
1575 if (!cli_unlock(cli1, fnum1, offset-1, 1)) {
1576 printf("unlock1 %d failed (%s)\n",
1582 if (!cli_unlock(cli2, fnum2, offset-2, 1)) {
1583 printf("unlock2 %d failed (%s)\n",
1590 if (!cli_close(cli1, fnum1)) {
1591 printf("close1 failed (%s)\n", cli_errstr(cli1));
1595 if (!cli_close(cli2, fnum2)) {
1596 printf("close2 failed (%s)\n", cli_errstr(cli2));
1600 if (!cli_unlink(cli1, fname)) {
1601 printf("unlink failed (%s)\n", cli_errstr(cli1));
1605 if (!torture_close_connection(cli1)) {
1609 if (!torture_close_connection(cli2)) {
1613 printf("finished locktest3\n");
1618 #define EXPECTED(ret, v) if ((ret) != (v)) { \
1619 printf("** "); correct = False; \
1623 looks at overlapping locks
1625 static bool run_locktest4(int dummy)
1627 static struct cli_state *cli1, *cli2;
1628 const char *fname = "\\lockt4.lck";
1629 int fnum1, fnum2, f;
1632 bool correct = True;
1634 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1638 cli_sockopt(cli1, sockops);
1639 cli_sockopt(cli2, sockops);
1641 printf("starting locktest4\n");
1643 cli_unlink(cli1, fname);
1645 fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1646 fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
1648 memset(buf, 0, sizeof(buf));
1650 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1651 printf("Failed to create file\n");
1656 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1657 cli_lock(cli1, fnum1, 2, 4, 0, WRITE_LOCK);
1658 EXPECTED(ret, False);
1659 printf("the same process %s set overlapping write locks\n", ret?"can":"cannot");
1661 ret = cli_lock(cli1, fnum1, 10, 4, 0, READ_LOCK) &&
1662 cli_lock(cli1, fnum1, 12, 4, 0, READ_LOCK);
1663 EXPECTED(ret, True);
1664 printf("the same process %s set overlapping read locks\n", ret?"can":"cannot");
1666 ret = cli_lock(cli1, fnum1, 20, 4, 0, WRITE_LOCK) &&
1667 cli_lock(cli2, fnum2, 22, 4, 0, WRITE_LOCK);
1668 EXPECTED(ret, False);
1669 printf("a different connection %s set overlapping write locks\n", ret?"can":"cannot");
1671 ret = cli_lock(cli1, fnum1, 30, 4, 0, READ_LOCK) &&
1672 cli_lock(cli2, fnum2, 32, 4, 0, READ_LOCK);
1673 EXPECTED(ret, True);
1674 printf("a different connection %s set overlapping read locks\n", ret?"can":"cannot");
1676 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 40, 4, 0, WRITE_LOCK)) &&
1677 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 42, 4, 0, WRITE_LOCK));
1678 EXPECTED(ret, False);
1679 printf("a different pid %s set overlapping write locks\n", ret?"can":"cannot");
1681 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 50, 4, 0, READ_LOCK)) &&
1682 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 52, 4, 0, READ_LOCK));
1683 EXPECTED(ret, True);
1684 printf("a different pid %s set overlapping read locks\n", ret?"can":"cannot");
1686 ret = cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK) &&
1687 cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK);
1688 EXPECTED(ret, True);
1689 printf("the same process %s set the same read lock twice\n", ret?"can":"cannot");
1691 ret = cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK) &&
1692 cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK);
1693 EXPECTED(ret, False);
1694 printf("the same process %s set the same write lock twice\n", ret?"can":"cannot");
1696 ret = cli_lock(cli1, fnum1, 80, 4, 0, READ_LOCK) &&
1697 cli_lock(cli1, fnum1, 80, 4, 0, WRITE_LOCK);
1698 EXPECTED(ret, False);
1699 printf("the same process %s overlay a read lock with a write lock\n", ret?"can":"cannot");
1701 ret = cli_lock(cli1, fnum1, 90, 4, 0, WRITE_LOCK) &&
1702 cli_lock(cli1, fnum1, 90, 4, 0, READ_LOCK);
1703 EXPECTED(ret, True);
1704 printf("the same process %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1706 ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 100, 4, 0, WRITE_LOCK)) &&
1707 (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 100, 4, 0, READ_LOCK));
1708 EXPECTED(ret, False);
1709 printf("a different pid %s overlay a write lock with a read lock\n", ret?"can":"cannot");
1711 ret = cli_lock(cli1, fnum1, 110, 4, 0, READ_LOCK) &&
1712 cli_lock(cli1, fnum1, 112, 4, 0, READ_LOCK) &&
1713 cli_unlock(cli1, fnum1, 110, 6);
1714 EXPECTED(ret, False);
1715 printf("the same process %s coalesce read locks\n", ret?"can":"cannot");
1718 ret = cli_lock(cli1, fnum1, 120, 4, 0, WRITE_LOCK) &&
1719 (cli_read(cli2, fnum2, buf, 120, 4) == 4);
1720 EXPECTED(ret, False);
1721 printf("this server %s strict write locking\n", ret?"doesn't do":"does");
1723 ret = cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK) &&
1724 (cli_write(cli2, fnum2, 0, buf, 130, 4) == 4);
1725 EXPECTED(ret, False);
1726 printf("this server %s strict read locking\n", ret?"doesn't do":"does");
1729 ret = cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1730 cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
1731 cli_unlock(cli1, fnum1, 140, 4) &&
1732 cli_unlock(cli1, fnum1, 140, 4);
1733 EXPECTED(ret, True);
1734 printf("this server %s do recursive read locking\n", ret?"does":"doesn't");
1737 ret = cli_lock(cli1, fnum1, 150, 4, 0, WRITE_LOCK) &&
1738 cli_lock(cli1, fnum1, 150, 4, 0, READ_LOCK) &&
1739 cli_unlock(cli1, fnum1, 150, 4) &&
1740 (cli_read(cli2, fnum2, buf, 150, 4) == 4) &&
1741 !(cli_write(cli2, fnum2, 0, buf, 150, 4) == 4) &&
1742 cli_unlock(cli1, fnum1, 150, 4);
1743 EXPECTED(ret, True);
1744 printf("this server %s do recursive lock overlays\n", ret?"does":"doesn't");
1746 ret = cli_lock(cli1, fnum1, 160, 4, 0, READ_LOCK) &&
1747 cli_unlock(cli1, fnum1, 160, 4) &&
1748 (cli_write(cli2, fnum2, 0, buf, 160, 4) == 4) &&
1749 (cli_read(cli2, fnum2, buf, 160, 4) == 4);
1750 EXPECTED(ret, True);
1751 printf("the same process %s remove a read lock using write locking\n", ret?"can":"cannot");
1753 ret = cli_lock(cli1, fnum1, 170, 4, 0, WRITE_LOCK) &&
1754 cli_unlock(cli1, fnum1, 170, 4) &&
1755 (cli_write(cli2, fnum2, 0, buf, 170, 4) == 4) &&
1756 (cli_read(cli2, fnum2, buf, 170, 4) == 4);
1757 EXPECTED(ret, True);
1758 printf("the same process %s remove a write lock using read locking\n", ret?"can":"cannot");
1760 ret = cli_lock(cli1, fnum1, 190, 4, 0, WRITE_LOCK) &&
1761 cli_lock(cli1, fnum1, 190, 4, 0, READ_LOCK) &&
1762 cli_unlock(cli1, fnum1, 190, 4) &&
1763 !(cli_write(cli2, fnum2, 0, buf, 190, 4) == 4) &&
1764 (cli_read(cli2, fnum2, buf, 190, 4) == 4);
1765 EXPECTED(ret, True);
1766 printf("the same process %s remove the first lock first\n", ret?"does":"doesn't");
1768 cli_close(cli1, fnum1);
1769 cli_close(cli2, fnum2);
1770 fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1771 f = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1772 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1773 cli_lock(cli1, f, 0, 1, 0, READ_LOCK) &&
1774 cli_close(cli1, fnum1) &&
1775 ((fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE)) != -1) &&
1776 cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1778 cli_close(cli1, fnum1);
1779 EXPECTED(ret, True);
1780 printf("the server %s have the NT byte range lock bug\n", !ret?"does":"doesn't");
1783 cli_close(cli1, fnum1);
1784 cli_close(cli2, fnum2);
1785 cli_unlink(cli1, fname);
1786 torture_close_connection(cli1);
1787 torture_close_connection(cli2);
1789 printf("finished locktest4\n");
1794 looks at lock upgrade/downgrade.
1796 static bool run_locktest5(int dummy)
1798 static struct cli_state *cli1, *cli2;
1799 const char *fname = "\\lockt5.lck";
1800 int fnum1, fnum2, fnum3;
1803 bool correct = True;
1805 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
1809 cli_sockopt(cli1, sockops);
1810 cli_sockopt(cli2, sockops);
1812 printf("starting locktest5\n");
1814 cli_unlink(cli1, fname);
1816 fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1817 fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
1818 fnum3 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1820 memset(buf, 0, sizeof(buf));
1822 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1823 printf("Failed to create file\n");
1828 /* Check for NT bug... */
1829 ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
1830 cli_lock(cli1, fnum3, 0, 1, 0, READ_LOCK);
1831 cli_close(cli1, fnum1);
1832 fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1833 ret = cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
1834 EXPECTED(ret, True);
1835 printf("this server %s the NT locking bug\n", ret ? "doesn't have" : "has");
1836 cli_close(cli1, fnum1);
1837 fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
1838 cli_unlock(cli1, fnum3, 0, 1);
1840 ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
1841 cli_lock(cli1, fnum1, 1, 1, 0, READ_LOCK);
1842 EXPECTED(ret, True);
1843 printf("the same process %s overlay a write with a read lock\n", ret?"can":"cannot");
1845 ret = cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
1846 EXPECTED(ret, False);
1848 printf("a different processs %s get a read lock on the first process lock stack\n", ret?"can":"cannot");
1850 /* Unlock the process 2 lock. */
1851 cli_unlock(cli2, fnum2, 0, 4);
1853 ret = cli_lock(cli1, fnum3, 0, 4, 0, READ_LOCK);
1854 EXPECTED(ret, False);
1856 printf("the same processs on a different fnum %s get a read lock\n", ret?"can":"cannot");
1858 /* Unlock the process 1 fnum3 lock. */
1859 cli_unlock(cli1, fnum3, 0, 4);
1861 /* Stack 2 more locks here. */
1862 ret = cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK) &&
1863 cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK);
1865 EXPECTED(ret, True);
1866 printf("the same process %s stack read locks\n", ret?"can":"cannot");
1868 /* Unlock the first process lock, then check this was the WRITE lock that was
1871 ret = cli_unlock(cli1, fnum1, 0, 4) &&
1872 cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
1874 EXPECTED(ret, True);
1875 printf("the first unlock removes the %s lock\n", ret?"WRITE":"READ");
1877 /* Unlock the process 2 lock. */
1878 cli_unlock(cli2, fnum2, 0, 4);
1880 /* We should have 3 stacked locks here. Ensure we need to do 3 unlocks. */
1882 ret = cli_unlock(cli1, fnum1, 1, 1) &&
1883 cli_unlock(cli1, fnum1, 0, 4) &&
1884 cli_unlock(cli1, fnum1, 0, 4);
1886 EXPECTED(ret, True);
1887 printf("the same process %s unlock the stack of 4 locks\n", ret?"can":"cannot");
1889 /* Ensure the next unlock fails. */
1890 ret = cli_unlock(cli1, fnum1, 0, 4);
1891 EXPECTED(ret, False);
1892 printf("the same process %s count the lock stack\n", !ret?"can":"cannot");
1894 /* Ensure connection 2 can get a write lock. */
1895 ret = cli_lock(cli2, fnum2, 0, 4, 0, WRITE_LOCK);
1896 EXPECTED(ret, True);
1898 printf("a different processs %s get a write lock on the unlocked stack\n", ret?"can":"cannot");
1902 cli_close(cli1, fnum1);
1903 cli_close(cli2, fnum2);
1904 cli_unlink(cli1, fname);
1905 if (!torture_close_connection(cli1)) {
1908 if (!torture_close_connection(cli2)) {
1912 printf("finished locktest5\n");
1918 tries the unusual lockingX locktype bits
1920 static bool run_locktest6(int dummy)
1922 static struct cli_state *cli;
1923 const char *fname[1] = { "\\lock6.txt" };
1928 if (!torture_open_connection(&cli, 0)) {
1932 cli_sockopt(cli, sockops);
1934 printf("starting locktest6\n");
1937 printf("Testing %s\n", fname[i]);
1939 cli_unlink(cli, fname[i]);
1941 fnum = cli_open(cli, fname[i], O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1942 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CHANGE_LOCKTYPE);
1943 cli_close(cli, fnum);
1944 printf("CHANGE_LOCKTYPE gave %s\n", nt_errstr(status));
1946 fnum = cli_open(cli, fname[i], O_RDWR, DENY_NONE);
1947 status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CANCEL_LOCK);
1948 cli_close(cli, fnum);
1949 printf("CANCEL_LOCK gave %s\n", nt_errstr(status));
1951 cli_unlink(cli, fname[i]);
1954 torture_close_connection(cli);
1956 printf("finished locktest6\n");
1960 static bool run_locktest7(int dummy)
1962 struct cli_state *cli1;
1963 const char *fname = "\\lockt7.lck";
1966 bool correct = False;
1968 if (!torture_open_connection(&cli1, 0)) {
1972 cli_sockopt(cli1, sockops);
1974 printf("starting locktest7\n");
1976 cli_unlink(cli1, fname);
1978 fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
1980 memset(buf, 0, sizeof(buf));
1982 if (cli_write(cli1, fnum1, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
1983 printf("Failed to create file\n");
1987 cli_setpid(cli1, 1);
1989 if (!cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK)) {
1990 printf("Unable to apply read lock on range 130:4, error was %s\n", cli_errstr(cli1));
1993 printf("pid1 successfully locked range 130:4 for READ\n");
1996 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
1997 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2000 printf("pid1 successfully read the range 130:4\n");
2003 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2004 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2005 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2006 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2010 printf("pid1 successfully wrote to the range 130:4 (should be denied)\n");
2014 cli_setpid(cli1, 2);
2016 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2017 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2019 printf("pid2 successfully read the range 130:4\n");
2022 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2023 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2024 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2025 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2029 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2033 cli_setpid(cli1, 1);
2034 cli_unlock(cli1, fnum1, 130, 4);
2036 if (!cli_lock(cli1, fnum1, 130, 4, 0, WRITE_LOCK)) {
2037 printf("Unable to apply write lock on range 130:4, error was %s\n", cli_errstr(cli1));
2040 printf("pid1 successfully locked range 130:4 for WRITE\n");
2043 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2044 printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2047 printf("pid1 successfully read the range 130:4\n");
2050 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2051 printf("pid1 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2054 printf("pid1 successfully wrote to the range 130:4\n");
2057 cli_setpid(cli1, 2);
2059 if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
2060 printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
2061 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2062 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2066 printf("pid2 successfully read the range 130:4 (should be denied)\n");
2070 if (cli_write(cli1, fnum1, 0, buf, 130, 4) != 4) {
2071 printf("pid2 unable to write to the range 130:4, error was %s\n", cli_errstr(cli1));
2072 if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
2073 printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
2077 printf("pid2 successfully wrote to the range 130:4 (should be denied)\n");
2081 cli_unlock(cli1, fnum1, 130, 0);
2085 cli_close(cli1, fnum1);
2086 cli_unlink(cli1, fname);
2087 torture_close_connection(cli1);
2089 printf("finished locktest7\n");
2094 test whether fnums and tids open on one VC are available on another (a major
2097 static bool run_fdpasstest(int dummy)
2099 struct cli_state *cli1, *cli2;
2100 const char *fname = "\\fdpass.tst";
2104 if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
2107 cli_sockopt(cli1, sockops);
2108 cli_sockopt(cli2, sockops);
2110 printf("starting fdpasstest\n");
2112 cli_unlink(cli1, fname);
2114 fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2116 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2120 if (cli_write(cli1, fnum1, 0, "hello world\n", 0, 13) != 13) {
2121 printf("write failed (%s)\n", cli_errstr(cli1));
2125 cli2->vuid = cli1->vuid;
2126 cli2->cnum = cli1->cnum;
2127 cli2->pid = cli1->pid;
2129 if (cli_read(cli2, fnum1, buf, 0, 13) == 13) {
2130 printf("read succeeded! nasty security hole [%s]\n",
2135 cli_close(cli1, fnum1);
2136 cli_unlink(cli1, fname);
2138 torture_close_connection(cli1);
2139 torture_close_connection(cli2);
2141 printf("finished fdpasstest\n");
2145 static bool run_fdsesstest(int dummy)
2147 struct cli_state *cli;
2152 const char *fname = "\\fdsess.tst";
2153 const char *fname1 = "\\fdsess1.tst";
2159 if (!torture_open_connection(&cli, 0))
2161 cli_sockopt(cli, sockops);
2163 if (!torture_cli_session_setup2(cli, &new_vuid))
2166 saved_cnum = cli->cnum;
2167 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, share, "?????", "", 1)))
2169 new_cnum = cli->cnum;
2170 cli->cnum = saved_cnum;
2172 printf("starting fdsesstest\n");
2174 cli_unlink(cli, fname);
2175 cli_unlink(cli, fname1);
2177 fnum1 = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2179 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2183 if (cli_write(cli, fnum1, 0, "hello world\n", 0, 13) != 13) {
2184 printf("write failed (%s)\n", cli_errstr(cli));
2188 saved_vuid = cli->vuid;
2189 cli->vuid = new_vuid;
2191 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2192 printf("read succeeded with different vuid! nasty security hole [%s]\n",
2196 /* Try to open a file with different vuid, samba cnum. */
2197 fnum2 = cli_open(cli, fname1, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2199 printf("create with different vuid, same cnum succeeded.\n");
2200 cli_close(cli, fnum2);
2201 cli_unlink(cli, fname1);
2203 printf("create with different vuid, same cnum failed.\n");
2204 printf("This will cause problems with service clients.\n");
2208 cli->vuid = saved_vuid;
2210 /* Try with same vuid, different cnum. */
2211 cli->cnum = new_cnum;
2213 if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
2214 printf("read succeeded with different cnum![%s]\n",
2219 cli->cnum = saved_cnum;
2220 cli_close(cli, fnum1);
2221 cli_unlink(cli, fname);
2223 torture_close_connection(cli);
2225 printf("finished fdsesstest\n");
2230 This test checks that
2232 1) the server does not allow an unlink on a file that is open
2234 static bool run_unlinktest(int dummy)
2236 struct cli_state *cli;
2237 const char *fname = "\\unlink.tst";
2239 bool correct = True;
2241 if (!torture_open_connection(&cli, 0)) {
2245 cli_sockopt(cli, sockops);
2247 printf("starting unlink test\n");
2249 cli_unlink(cli, fname);
2253 fnum = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2255 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2259 if (cli_unlink(cli, fname)) {
2260 printf("error: server allowed unlink on an open file\n");
2263 correct = check_error(__LINE__, cli, ERRDOS, ERRbadshare,
2264 NT_STATUS_SHARING_VIOLATION);
2267 cli_close(cli, fnum);
2268 cli_unlink(cli, fname);
2270 if (!torture_close_connection(cli)) {
2274 printf("unlink test finished\n");
2281 test how many open files this server supports on the one socket
2283 static bool run_maxfidtest(int dummy)
2285 struct cli_state *cli;
2286 const char *ftemplate = "\\maxfid.%d.%d";
2288 int fnums[0x11000], i;
2290 bool correct = True;
2295 printf("failed to connect\n");
2299 cli_sockopt(cli, sockops);
2301 for (i=0; i<0x11000; i++) {
2302 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2303 if ((fnums[i] = cli_open(cli, fname,
2304 O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
2306 printf("open of %s failed (%s)\n",
2307 fname, cli_errstr(cli));
2308 printf("maximum fnum is %d\n", i);
2316 printf("cleaning up\n");
2318 slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
2319 cli_close(cli, fnums[i]);
2320 if (!cli_unlink(cli, fname)) {
2321 printf("unlink of %s failed (%s)\n",
2322 fname, cli_errstr(cli));
2329 printf("maxfid test finished\n");
2330 if (!torture_close_connection(cli)) {
2336 /* generate a random buffer */
2337 static void rand_buf(char *buf, int len)
2340 *buf = (char)sys_random();
2345 /* send smb negprot commands, not reading the response */
2346 static bool run_negprot_nowait(int dummy)
2349 static struct cli_state *cli;
2350 bool correct = True;
2352 printf("starting negprot nowait test\n");
2354 if (!(cli = open_nbt_connection())) {
2358 for (i=0;i<50000;i++) {
2359 cli_negprot_sendsync(cli);
2362 if (!torture_close_connection(cli)) {
2366 printf("finished negprot nowait test\n");
2372 /* send random IPC commands */
2373 static bool run_randomipc(int dummy)
2375 char *rparam = NULL;
2377 unsigned int rdrcnt,rprcnt;
2379 int api, param_len, i;
2380 struct cli_state *cli;
2381 bool correct = True;
2384 printf("starting random ipc test\n");
2386 if (!torture_open_connection(&cli, 0)) {
2390 for (i=0;i<count;i++) {
2391 api = sys_random() % 500;
2392 param_len = (sys_random() % 64);
2394 rand_buf(param, param_len);
2399 param, param_len, 8,
2400 NULL, 0, BUFFER_SIZE,
2404 printf("%d/%d\r", i,count);
2407 printf("%d/%d\n", i, count);
2409 if (!torture_close_connection(cli)) {
2413 printf("finished random ipc test\n");
2420 static void browse_callback(const char *sname, uint32 stype,
2421 const char *comment, void *state)
2423 printf("\t%20.20s %08x %s\n", sname, stype, comment);
2429 This test checks the browse list code
2432 static bool run_browsetest(int dummy)
2434 static struct cli_state *cli;
2435 bool correct = True;
2437 printf("starting browse test\n");
2439 if (!torture_open_connection(&cli, 0)) {
2443 printf("domain list:\n");
2444 cli_NetServerEnum(cli, cli->server_domain,
2445 SV_TYPE_DOMAIN_ENUM,
2446 browse_callback, NULL);
2448 printf("machine list:\n");
2449 cli_NetServerEnum(cli, cli->server_domain,
2451 browse_callback, NULL);
2453 if (!torture_close_connection(cli)) {
2457 printf("browse test finished\n");
2465 This checks how the getatr calls works
2467 static bool run_attrtest(int dummy)
2469 struct cli_state *cli;
2472 const char *fname = "\\attrib123456789.tst";
2473 bool correct = True;
2475 printf("starting attrib test\n");
2477 if (!torture_open_connection(&cli, 0)) {
2481 cli_unlink(cli, fname);
2482 fnum = cli_open(cli, fname,
2483 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2484 cli_close(cli, fnum);
2485 if (!cli_getatr(cli, fname, NULL, NULL, &t)) {
2486 printf("getatr failed (%s)\n", cli_errstr(cli));
2490 if (abs(t - time(NULL)) > 60*60*24*10) {
2491 printf("ERROR: SMBgetatr bug. time is %s",
2497 t2 = t-60*60*24; /* 1 day ago */
2499 if (!cli_setatr(cli, fname, 0, t2)) {
2500 printf("setatr failed (%s)\n", cli_errstr(cli));
2504 if (!cli_getatr(cli, fname, NULL, NULL, &t)) {
2505 printf("getatr failed (%s)\n", cli_errstr(cli));
2510 printf("ERROR: getatr/setatr bug. times are\n%s",
2512 printf("%s", ctime(&t2));
2516 cli_unlink(cli, fname);
2518 if (!torture_close_connection(cli)) {
2522 printf("attrib test finished\n");
2529 This checks a couple of trans2 calls
2531 static bool run_trans2test(int dummy)
2533 struct cli_state *cli;
2536 time_t c_time, a_time, m_time;
2537 struct timespec c_time_ts, a_time_ts, m_time_ts, w_time_ts, m_time2_ts;
2538 const char *fname = "\\trans2.tst";
2539 const char *dname = "\\trans2";
2540 const char *fname2 = "\\trans2\\trans2.tst";
2542 bool correct = True;
2544 printf("starting trans2 test\n");
2546 if (!torture_open_connection(&cli, 0)) {
2550 cli_unlink(cli, fname);
2551 fnum = cli_open(cli, fname,
2552 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2553 if (!cli_qfileinfo(cli, fnum, NULL, &size, &c_time_ts, &a_time_ts, &w_time_ts,
2554 &m_time_ts, NULL)) {
2555 printf("ERROR: qfileinfo failed (%s)\n", cli_errstr(cli));
2559 if (!cli_qfilename(cli, fnum, pname, sizeof(pname))) {
2560 printf("ERROR: qfilename failed (%s)\n", cli_errstr(cli));
2564 if (strcmp(pname, fname)) {
2565 printf("qfilename gave different name? [%s] [%s]\n",
2570 cli_close(cli, fnum);
2574 cli_unlink(cli, fname);
2575 fnum = cli_open(cli, fname,
2576 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2578 printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
2581 cli_close(cli, fnum);
2583 if (!cli_qpathinfo(cli, fname, &c_time, &a_time, &m_time, &size, NULL)) {
2584 printf("ERROR: qpathinfo failed (%s)\n", cli_errstr(cli));
2587 if (c_time != m_time) {
2588 printf("create time=%s", ctime(&c_time));
2589 printf("modify time=%s", ctime(&m_time));
2590 printf("This system appears to have sticky create times\n");
2592 if (a_time % (60*60) == 0) {
2593 printf("access time=%s", ctime(&a_time));
2594 printf("This system appears to set a midnight access time\n");
2598 if (abs(m_time - time(NULL)) > 60*60*24*7) {
2599 printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
2605 cli_unlink(cli, fname);
2606 fnum = cli_open(cli, fname,
2607 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2608 cli_close(cli, fnum);
2609 if (!cli_qpathinfo2(cli, fname, &c_time_ts, &a_time_ts, &w_time_ts,
2610 &m_time_ts, &size, NULL, NULL)) {
2611 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2614 if (w_time_ts.tv_sec < 60*60*24*2) {
2615 printf("write time=%s", ctime(&w_time_ts.tv_sec));
2616 printf("This system appears to set a initial 0 write time\n");
2621 cli_unlink(cli, fname);
2624 /* check if the server updates the directory modification time
2625 when creating a new file */
2626 if (!cli_mkdir(cli, dname)) {
2627 printf("ERROR: mkdir failed (%s)\n", cli_errstr(cli));
2631 if (!cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts, &w_time_ts,
2632 &m_time_ts, &size, NULL, NULL)) {
2633 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2637 fnum = cli_open(cli, fname2,
2638 O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
2639 cli_write(cli, fnum, 0, (char *)&fnum, 0, sizeof(fnum));
2640 cli_close(cli, fnum);
2641 if (!cli_qpathinfo2(cli, "\\trans2\\", &c_time_ts, &a_time_ts, &w_time_ts,
2642 &m_time2_ts, &size, NULL, NULL)) {
2643 printf("ERROR: qpathinfo2 failed (%s)\n", cli_errstr(cli));
2646 if (memcmp(&m_time_ts, &m_time2_ts, sizeof(struct timespec))
2648 printf("This system does not update directory modification times\n");
2652 cli_unlink(cli, fname2);
2653 cli_rmdir(cli, dname);
2655 if (!torture_close_connection(cli)) {
2659 printf("trans2 test finished\n");
2665 This checks new W2K calls.
2668 static bool new_trans(struct cli_state *pcli, int fnum, int level)
2672 bool correct = True;
2674 if (!cli_qfileinfo_test(pcli, fnum, level, &buf, &len)) {
2675 printf("ERROR: qfileinfo (%d) failed (%s)\n", level, cli_errstr(pcli));
2678 printf("qfileinfo: level %d, len = %u\n", level, len);
2679 dump_data(0, (uint8 *)buf, len);
2686 static bool run_w2ktest(int dummy)
2688 struct cli_state *cli;
2690 const char *fname = "\\w2ktest\\w2k.tst";
2692 bool correct = True;
2694 printf("starting w2k test\n");
2696 if (!torture_open_connection(&cli, 0)) {
2700 fnum = cli_open(cli, fname,
2701 O_RDWR | O_CREAT , DENY_NONE);
2703 for (level = 1004; level < 1040; level++) {
2704 new_trans(cli, fnum, level);
2707 cli_close(cli, fnum);
2709 if (!torture_close_connection(cli)) {
2713 printf("w2k test finished\n");
2720 this is a harness for some oplock tests
2722 static bool run_oplock1(int dummy)
2724 struct cli_state *cli1;
2725 const char *fname = "\\lockt1.lck";
2727 bool correct = True;
2729 printf("starting oplock test 1\n");
2731 if (!torture_open_connection(&cli1, 0)) {
2735 cli_unlink(cli1, fname);
2737 cli_sockopt(cli1, sockops);
2739 cli1->use_oplocks = True;
2741 fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2743 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2747 cli1->use_oplocks = False;
2749 cli_unlink(cli1, fname);
2750 cli_unlink(cli1, fname);
2752 if (!cli_close(cli1, fnum1)) {
2753 printf("close2 failed (%s)\n", cli_errstr(cli1));
2757 if (!cli_unlink(cli1, fname)) {
2758 printf("unlink failed (%s)\n", cli_errstr(cli1));
2762 if (!torture_close_connection(cli1)) {
2766 printf("finished oplock test 1\n");
2771 static bool run_oplock2(int dummy)
2773 struct cli_state *cli1, *cli2;
2774 const char *fname = "\\lockt2.lck";
2776 int saved_use_oplocks = use_oplocks;
2778 bool correct = True;
2779 volatile bool *shared_correct;
2781 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
2782 *shared_correct = True;
2784 use_level_II_oplocks = True;
2787 printf("starting oplock test 2\n");
2789 if (!torture_open_connection(&cli1, 0)) {
2790 use_level_II_oplocks = False;
2791 use_oplocks = saved_use_oplocks;
2795 cli1->use_oplocks = True;
2796 cli1->use_level_II_oplocks = True;
2798 if (!torture_open_connection(&cli2, 1)) {
2799 use_level_II_oplocks = False;
2800 use_oplocks = saved_use_oplocks;
2804 cli2->use_oplocks = True;
2805 cli2->use_level_II_oplocks = True;
2807 cli_unlink(cli1, fname);
2809 cli_sockopt(cli1, sockops);
2810 cli_sockopt(cli2, sockops);
2812 fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
2814 printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
2818 /* Don't need the globals any more. */
2819 use_level_II_oplocks = False;
2820 use_oplocks = saved_use_oplocks;
2824 fnum2 = cli_open(cli2, fname, O_RDWR, DENY_NONE);
2826 printf("second open of %s failed (%s)\n", fname, cli_errstr(cli1));
2827 *shared_correct = False;
2833 if (!cli_close(cli2, fnum2)) {
2834 printf("close2 failed (%s)\n", cli_errstr(cli1));
2835 *shared_correct = False;
2843 /* Ensure cli1 processes the break. Empty file should always return 0
2846 if (cli_read(cli1, fnum1, buf, 0, 4) != 0) {
2847 printf("read on fnum1 failed (%s)\n", cli_errstr(cli1));
2851 /* Should now be at level II. */
2852 /* Test if sending a write locks causes a break to none. */
2854 if (!cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK)) {
2855 printf("lock failed (%s)\n", cli_errstr(cli1));
2859 cli_unlock(cli1, fnum1, 0, 4);
2863 if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
2864 printf("lock failed (%s)\n", cli_errstr(cli1));
2868 cli_unlock(cli1, fnum1, 0, 4);
2872 cli_read(cli1, fnum1, buf, 0, 4);
2875 if (cli_write(cli1, fnum1, 0, buf, 0, 4) != 4) {
2876 printf("write on fnum1 failed (%s)\n", cli_errstr(cli1));
2881 if (!cli_close(cli1, fnum1)) {
2882 printf("close1 failed (%s)\n", cli_errstr(cli1));
2888 if (!cli_unlink(cli1, fname)) {
2889 printf("unlink failed (%s)\n", cli_errstr(cli1));
2893 if (!torture_close_connection(cli1)) {
2897 if (!*shared_correct) {
2901 printf("finished oplock test 2\n");
2906 /* handler for oplock 3 tests */
2907 static bool oplock3_handler(struct cli_state *cli, int fnum, unsigned char level)
2909 printf("got oplock break fnum=%d level=%d\n",
2911 return cli_oplock_ack(cli, fnum, level);
2914 static bool run_oplock3(int dummy)
2916 struct cli_state *cli;
2917 const char *fname = "\\oplockt3.dat";
2919 char buf[4] = "abcd";
2920 bool correct = True;
2921 volatile bool *shared_correct;
2923 shared_correct = (volatile bool *)shm_setup(sizeof(bool));
2924 *shared_correct = True;
2926 printf("starting oplock test 3\n");
2931 use_level_II_oplocks = True;
2932 if (!torture_open_connection(&cli, 0)) {
2933 *shared_correct = False;
2937 /* try to trigger a oplock break in parent */
2938 fnum = cli_open(cli, fname, O_RDWR, DENY_NONE);
2939 cli_write(cli, fnum, 0, buf, 0, 4);
2945 use_level_II_oplocks = True;
2946 if (!torture_open_connection(&cli, 1)) { /* other is forked */
2949 cli_oplock_handler(cli, oplock3_handler);
2950 fnum = cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE);
2951 cli_write(cli, fnum, 0, buf, 0, 4);
2952 cli_close(cli, fnum);
2953 fnum = cli_open(cli, fname, O_RDWR, DENY_NONE);
2954 cli->timeout = 20000;
2955 cli_receive_smb(cli);
2956 printf("finished oplock test 3\n");
2958 return (correct && *shared_correct);
2960 /* What are we looking for here? What's sucess and what's FAILURE? */
2966 Test delete on close semantics.
2968 static bool run_deletetest(int dummy)
2970 struct cli_state *cli1 = NULL;
2971 struct cli_state *cli2 = NULL;
2972 const char *fname = "\\delete.file";
2975 bool correct = True;
2977 printf("starting delete test\n");
2979 if (!torture_open_connection(&cli1, 0)) {
2983 cli_sockopt(cli1, sockops);
2985 /* Test 1 - this should delete the file on close. */
2987 cli_setatr(cli1, fname, 0, 0);
2988 cli_unlink(cli1, fname);
2990 fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
2991 0, FILE_OVERWRITE_IF,
2992 FILE_DELETE_ON_CLOSE, 0);
2995 printf("[1] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3002 uint32 *accinfo = NULL;
3004 cli_qfileinfo_test(cli1, fnum1, SMB_FILE_ACCESS_INFORMATION, (char **)&accinfo, &len);
3006 printf("access mode = 0x%lx\n", *accinfo);
3011 if (!cli_close(cli1, fnum1)) {
3012 printf("[1] close failed (%s)\n", cli_errstr(cli1));
3017 fnum1 = cli_open(cli1, fname, O_RDWR, DENY_NONE);
3019 printf("[1] open of %s succeeded (should fail)\n", fname);
3024 printf("first delete on close test succeeded.\n");
3026 /* Test 2 - this should delete the file on close. */
3028 cli_setatr(cli1, fname, 0, 0);
3029 cli_unlink(cli1, fname);
3031 fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_ALL_ACCESS,
3032 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
3033 FILE_OVERWRITE_IF, 0, 0);
3036 printf("[2] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3041 if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3042 printf("[2] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3047 if (!cli_close(cli1, fnum1)) {
3048 printf("[2] close failed (%s)\n", cli_errstr(cli1));
3053 fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
3055 printf("[2] open of %s succeeded should have been deleted on close !\n", fname);
3056 if (!cli_close(cli1, fnum1)) {
3057 printf("[2] close failed (%s)\n", cli_errstr(cli1));
3061 cli_unlink(cli1, fname);
3063 printf("second delete on close test succeeded.\n");
3066 cli_setatr(cli1, fname, 0, 0);
3067 cli_unlink(cli1, fname);
3069 fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
3070 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0);
3073 printf("[3] open - 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3078 /* This should fail with a sharing violation - open for delete is only compatible
3079 with SHARE_DELETE. */
3081 fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3082 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0, 0);
3085 printf("[3] open - 2 of %s succeeded - should have failed.\n", fname);
3090 /* This should succeed. */
3092 fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
3093 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0);
3096 printf("[3] open - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3101 if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3102 printf("[3] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3107 if (!cli_close(cli1, fnum1)) {
3108 printf("[3] close 1 failed (%s)\n", cli_errstr(cli1));
3113 if (!cli_close(cli1, fnum2)) {
3114 printf("[3] close 2 failed (%s)\n", cli_errstr(cli1));
3119 /* This should fail - file should no longer be there. */
3121 fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
3123 printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
3124 if (!cli_close(cli1, fnum1)) {
3125 printf("[3] close failed (%s)\n", cli_errstr(cli1));
3127 cli_unlink(cli1, fname);
3131 printf("third delete on close test succeeded.\n");
3134 cli_setatr(cli1, fname, 0, 0);
3135 cli_unlink(cli1, fname);
3137 fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3138 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0);
3141 printf("[4] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3146 /* This should succeed. */
3147 fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS,
3148 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0);
3150 printf("[4] open - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
3155 if (!cli_close(cli1, fnum2)) {
3156 printf("[4] close - 1 failed (%s)\n", cli_errstr(cli1));
3161 if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3162 printf("[4] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
3167 /* This should fail - no more opens once delete on close set. */
3168 fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS,
3169 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3172 printf("[4] open - 3 of %s succeeded ! Should have failed.\n", fname );
3176 printf("fourth delete on close test succeeded.\n");
3178 if (!cli_close(cli1, fnum1)) {
3179 printf("[4] close - 2 failed (%s)\n", cli_errstr(cli1));
3185 cli_setatr(cli1, fname, 0, 0);
3186 cli_unlink(cli1, fname);
3188 fnum1 = cli_open(cli1, fname, O_RDWR|O_CREAT, DENY_NONE);
3190 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3195 /* This should fail - only allowed on NT opens with DELETE access. */
3197 if (cli_nt_delete_on_close(cli1, fnum1, True)) {
3198 printf("[5] setting delete_on_close on OpenX file succeeded - should fail !\n");
3203 if (!cli_close(cli1, fnum1)) {
3204 printf("[5] close - 2 failed (%s)\n", cli_errstr(cli1));
3209 printf("fifth delete on close test succeeded.\n");
3212 cli_setatr(cli1, fname, 0, 0);
3213 cli_unlink(cli1, fname);
3215 fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
3216 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3217 FILE_OVERWRITE_IF, 0, 0);
3220 printf("[6] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3225 /* This should fail - only allowed on NT opens with DELETE access. */
3227 if (cli_nt_delete_on_close(cli1, fnum1, True)) {
3228 printf("[6] setting delete_on_close on file with no delete access succeeded - should fail !\n");
3233 if (!cli_close(cli1, fnum1)) {
3234 printf("[6] close - 2 failed (%s)\n", cli_errstr(cli1));
3239 printf("sixth delete on close test succeeded.\n");
3242 cli_setatr(cli1, fname, 0, 0);
3243 cli_unlink(cli1, fname);
3245 fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3246 FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0);
3249 printf("[7] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3254 if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3255 printf("[7] setting delete_on_close on file failed !\n");
3260 if (!cli_nt_delete_on_close(cli1, fnum1, False)) {
3261 printf("[7] unsetting delete_on_close on file failed !\n");
3266 if (!cli_close(cli1, fnum1)) {
3267 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3272 /* This next open should succeed - we reset the flag. */
3274 fnum1 = cli_open(cli1, fname, O_RDONLY, DENY_NONE);
3276 printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
3281 if (!cli_close(cli1, fnum1)) {
3282 printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
3287 printf("seventh delete on close test succeeded.\n");
3290 cli_setatr(cli1, fname, 0, 0);
3291 cli_unlink(cli1, fname);
3293 if (!torture_open_connection(&cli2, 1)) {
3294 printf("[8] failed to open second connection.\n");
3299 cli_sockopt(cli1, sockops);
3301 fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3302 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3303 FILE_OVERWRITE_IF, 0, 0);
3306 printf("[8] open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
3311 fnum2 = cli_nt_create_full(cli2, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
3312 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3316 printf("[8] open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
3321 if (!cli_nt_delete_on_close(cli1, fnum1, True)) {
3322 printf("[8] setting delete_on_close on file failed !\n");