s4:smbtorture: Fix samba3.smb.dir on btrfs
[samba4-gss.git] / source4 / torture / smb2 / samba3misc.c
blob6696c062e94ce05487a08098cdcbef7c7f327bee
1 /*
2 Unix SMB/CIFS implementation.
4 Test some misc Samba3 code paths
6 Copyright (C) Volker Lendecke 2006
7 Copyright (C) Stefan Metzmacher 2019
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "system/time.h"
25 #include "system/filesys.h"
26 #include "libcli/smb2/smb2.h"
27 #include "libcli/smb2/smb2_calls.h"
28 #include "../libcli/smb/smbXcli_base.h"
29 #include "torture/torture.h"
30 #include "torture/smb2/proto.h"
31 #include "torture/util.h"
32 #include "lib/events/events.h"
33 #include "param/param.h"
35 #define CHECK_STATUS(status, correct) do { \
36 const char *_cmt = "(" __location__ ")"; \
37 torture_assert_ntstatus_equal_goto(tctx,status,correct, \
38 ret,done,_cmt); \
39 } while (0)
41 #define BASEDIR "samba3misc.smb2"
43 #define WAIT_FOR_ASYNC_RESPONSE(req) \
44 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) { \
45 if (tevent_loop_once(tctx->ev) != 0) { \
46 break; \
47 } \
50 static void torture_smb2_tree_disconnect_timer(struct tevent_context *ev,
51 struct tevent_timer *te,
52 struct timeval now,
53 void *private_data)
55 struct smb2_tree *tree =
56 talloc_get_type_abort(private_data,
57 struct smb2_tree);
59 smbXcli_conn_disconnect(tree->session->transport->conn,
60 NT_STATUS_CTX_CLIENT_QUERY_TIMEOUT);
64 * Check that Samba3 correctly deals with conflicting local posix byte range
65 * locks on an underlying file via "normal" SMB2 (without posix extensions).
67 * Note: This test depends on "posix locking = yes".
68 * Note: To run this test, use "--option=torture:localdir=<LOCALDIR>"
70 static bool torture_samba3_localposixlock1(struct torture_context *tctx,
71 struct smb2_tree *tree)
73 NTSTATUS status;
74 bool ret = true;
75 int rc;
76 const char *fname = "posixtimedlock.dat";
77 const char *fpath;
78 const char *localdir;
79 const char *localname;
80 struct smb2_handle h = {{0}};
81 struct smb2_lock lck = {0};
82 struct smb2_lock_element el[1] = {{0}};
83 struct smb2_request *req = NULL;
84 int fd = -1;
85 struct flock posix_lock;
86 struct tevent_timer *te;
88 status = torture_smb2_testdir(tree, BASEDIR, &h);
89 CHECK_STATUS(status, NT_STATUS_OK);
90 smb2_util_close(tree, h);
92 status = torture_smb2_testfile(tree, fname, &h);
93 CHECK_STATUS(status, NT_STATUS_OK);
95 fpath = talloc_asprintf(tctx, "%s\\%s", BASEDIR, fname);
96 torture_assert(tctx, fpath != NULL, "fpath\n");
98 status = torture_smb2_testfile(tree, fpath, &h);
99 CHECK_STATUS(status, NT_STATUS_OK);
101 localdir = torture_setting_string(tctx, "localdir", NULL);
102 torture_assert(tctx, localdir != NULL,
103 "--option=torture:localdir=<LOCALDIR> required\n");
105 localname = talloc_asprintf(tctx, "%s/%s/%s",
106 localdir, BASEDIR, fname);
107 torture_assert(tctx, localname != NULL, "localname\n");
110 * Lock a byte range from posix
113 torture_comment(tctx, " local open(%s)\n", localname);
114 fd = open(localname, O_RDWR);
115 if (fd == -1) {
116 torture_warning(tctx, "open(%s) failed: %s\n",
117 localname, strerror(errno));
118 torture_assert(tctx, fd != -1, "open localname\n");
121 posix_lock.l_type = F_WRLCK;
122 posix_lock.l_whence = SEEK_SET;
123 posix_lock.l_start = 0;
124 posix_lock.l_len = 1;
126 torture_comment(tctx, " local fcntl\n");
127 rc = fcntl(fd, F_SETLK, &posix_lock);
128 if (rc == -1) {
129 torture_warning(tctx, "fcntl failed: %s\n", strerror(errno));
130 torture_assert_goto(tctx, rc != -1, ret, done,
131 "fcntl lock\n");
134 el[0].offset = 0;
135 el[0].length = 1;
136 el[0].reserved = 0x00000000;
137 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
138 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
139 lck.in.locks = el;
140 lck.in.lock_count = 0x0001;
141 lck.in.lock_sequence = 0x00000000;
142 lck.in.file.handle = h;
144 torture_comment(tctx, " remote non-blocking lock\n");
145 status = smb2_lock(tree, &lck);
146 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
148 torture_comment(tctx, " remote async blocking lock\n");
149 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
150 req = smb2_lock_send(tree, &lck);
151 torture_assert_goto(tctx, req != NULL, ret, done, "smb2_lock_send()\n");
153 te = tevent_add_timer(tctx->ev,
154 tctx, timeval_current_ofs(5, 0),
155 torture_smb2_tree_disconnect_timer,
156 tree);
157 torture_assert_goto(tctx, te != NULL, ret, done, "tevent_add_timer\n");
159 torture_comment(tctx, " remote wait for STATUS_PENDING\n");
160 WAIT_FOR_ASYNC_RESPONSE(req);
162 torture_comment(tctx, " local close file\n");
163 close(fd);
164 fd = -1;
166 torture_comment(tctx, " remote lock should now succeed\n");
167 status = smb2_lock_recv(req, &lck);
168 CHECK_STATUS(status, NT_STATUS_OK);
170 done:
171 if (fd != -1) {
172 close(fd);
174 smb2_util_close(tree, h);
175 smb2_deltree(tree, BASEDIR);
176 return ret;
179 struct torture_suite *torture_smb2_samba3misc_init(TALLOC_CTX *ctx)
181 struct torture_suite *suite = torture_suite_create(ctx, "samba3misc");
183 torture_suite_add_1smb2_test(suite, "localposixlock1",
184 torture_samba3_localposixlock1);
186 suite->description = talloc_strdup(suite, "SMB2 Samba3 MISC");
188 return suite;