| 1 |
/* |
| 2 |
Unix SMB/CIFS implementation. |
| 3 |
basic raw test suite for multiplexing |
| 4 |
Copyright (C) Andrew Tridgell 2003 |
| 5 |
|
| 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. |
| 10 |
|
| 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. |
| 15 |
|
| 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/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "includes.h" |
| 21 |
#include "system/filesys.h" |
| 22 |
#include "libcli/raw/libcliraw.h" |
| 23 |
#include "libcli/raw/raw_proto.h" |
| 24 |
#include "libcli/libcli.h" |
| 25 |
#include "torture/util.h" |
| 26 |
#include "torture/raw/proto.h" |
| 27 |
|
| 28 |
#define BASEDIR "\\test_mux" |
| 29 |
|
| 30 |
/* |
| 31 |
test the delayed reply to a open that leads to a sharing violation |
| 32 |
*/ |
| 33 |
static bool test_mux_open(struct torture_context *tctx, struct smbcli_state *cli, TALLOC_CTX *mem_ctx) |
| 34 |
{ |
| 35 |
union smb_open io; |
| 36 |
NTSTATUS status; |
| 37 |
int fnum1, fnum2; |
| 38 |
bool ret = true; |
| 39 |
struct smbcli_request *req1, *req2; |
| 40 |
struct timeval tv; |
| 41 |
double d; |
| 42 |
|
| 43 |
torture_comment(tctx, "Testing multiplexed open/open/close\n"); |
| 44 |
|
| 45 |
torture_comment(tctx, "send first open\n"); |
| 46 |
io.generic.level = RAW_OPEN_NTCREATEX; |
| 47 |
io.ntcreatex.in.root_fid.fnum = 0; |
| 48 |
io.ntcreatex.in.flags = 0; |
| 49 |
io.ntcreatex.in.access_mask = SEC_FILE_READ_DATA; |
| 50 |
io.ntcreatex.in.create_options = 0; |
| 51 |
io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL; |
| 52 |
io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ; |
| 53 |
io.ntcreatex.in.alloc_size = 0; |
| 54 |
io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE; |
| 55 |
io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS; |
| 56 |
io.ntcreatex.in.security_flags = 0; |
| 57 |
io.ntcreatex.in.fname = BASEDIR "\\open.dat"; |
| 58 |
status = smb_raw_open(cli->tree, mem_ctx, &io); |
| 59 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "send first open"); |
| 60 |
fnum1 = io.ntcreatex.out.file.fnum; |
| 61 |
|
| 62 |
torture_comment(tctx, "send 2nd open, non-conflicting\n"); |
| 63 |
io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN; |
| 64 |
status = smb_raw_open(cli->tree, mem_ctx, &io); |
| 65 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "send 2nd open, non-conflicting"); |
| 66 |
fnum2 = io.ntcreatex.out.file.fnum; |
| 67 |
|
| 68 |
tv = timeval_current(); |
| 69 |
|
| 70 |
torture_comment(tctx, "send 3rd open, conflicting\n"); |
| 71 |
io.ntcreatex.in.share_access = 0; |
| 72 |
status = smb_raw_open(cli->tree, mem_ctx, &io); |
| 73 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_SHARING_VIOLATION, "send 3rd open, conflicting"); |
| 74 |
|
| 75 |
d = timeval_elapsed(&tv); |
| 76 |
if (d < 0.5 || d > 1.5) { |
| 77 |
torture_comment(tctx, "bad timeout for conflict - %.2f should be 1.0\n", d); |
| 78 |
} else { |
| 79 |
torture_comment(tctx, "open delay %.2f\n", d); |
| 80 |
} |
| 81 |
|
| 82 |
torture_comment(tctx, "send async open, conflicting\n"); |
| 83 |
tv = timeval_current(); |
| 84 |
req1 = smb_raw_open_send(cli->tree, &io); |
| 85 |
|
| 86 |
torture_comment(tctx, "send 2nd async open, conflicting\n"); |
| 87 |
tv = timeval_current(); |
| 88 |
req2 = smb_raw_open_send(cli->tree, &io); |
| 89 |
|
| 90 |
torture_comment(tctx, "close first sync open\n"); |
| 91 |
smbcli_close(cli->tree, fnum1); |
| 92 |
|
| 93 |
torture_comment(tctx, "cancel 2nd async open (should be ignored)\n"); |
| 94 |
smb_raw_ntcancel(req2); |
| 95 |
|
| 96 |
d = timeval_elapsed(&tv); |
| 97 |
if (d > 0.25) { |
| 98 |
torture_comment(tctx, "bad timeout after cancel - %.2f should be <0.25\n", d); |
| 99 |
torture_assert(tctx, d <= 0.25, "bad timeout after cancel"); |
| 100 |
} |
| 101 |
|
| 102 |
torture_comment(tctx, "close the 2nd sync open\n"); |
| 103 |
smbcli_close(cli->tree, fnum2); |
| 104 |
|
| 105 |
torture_comment(tctx, "see if the 1st async open now succeeded\n"); |
| 106 |
status = smb_raw_open_recv(req1, mem_ctx, &io); |
| 107 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "see if the 1st async open now succeeded"); |
| 108 |
|
| 109 |
d = timeval_elapsed(&tv); |
| 110 |
if (d > 0.25) { |
| 111 |
torture_comment(tctx, "bad timeout for async conflict - %.2f should be <0.25\n", d); |
| 112 |
torture_assert(tctx, d <= 0.25, "bad timeout for async conflict"); |
| 113 |
} else { |
| 114 |
torture_comment(tctx, "async open delay %.2f\n", d); |
| 115 |
} |
| 116 |
|
| 117 |
torture_comment(tctx, "2nd async open should have timed out\n"); |
| 118 |
status = smb_raw_open_recv(req2, mem_ctx, &io); |
| 119 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_SHARING_VIOLATION, "2nd async open should have timed out"); |
| 120 |
d = timeval_elapsed(&tv); |
| 121 |
if (d < 0.8) { |
| 122 |
torture_comment(tctx, "bad timeout for async conflict - %.2f should be 1.0\n", d); |
| 123 |
} |
| 124 |
|
| 125 |
torture_comment(tctx, "close the 1st async open\n"); |
| 126 |
smbcli_close(cli->tree, io.ntcreatex.out.file.fnum); |
| 127 |
|
| 128 |
return ret; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/* |
| 133 |
test a write that hits a byte range lock and send the close after the write |
| 134 |
*/ |
| 135 |
static bool test_mux_write(struct torture_context *tctx, struct smbcli_state *cli, TALLOC_CTX *mem_ctx) |
| 136 |
{ |
| 137 |
union smb_write io; |
| 138 |
NTSTATUS status; |
| 139 |
int fnum; |
| 140 |
bool ret = true; |
| 141 |
struct smbcli_request *req; |
| 142 |
|
| 143 |
torture_comment(tctx, "Testing multiplexed lock/write/close\n"); |
| 144 |
|
| 145 |
fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE); |
| 146 |
if (fnum == -1) { |
| 147 |
torture_comment(tctx, "open failed in mux_write - %s\n", smbcli_errstr(cli->tree)); |
| 148 |
torture_assert(tctx, fnum != -1, "open failed in mux_write"); |
| 149 |
} |
| 150 |
|
| 151 |
cli->session->pid = 1; |
| 152 |
|
| 153 |
status = smbcli_lock(cli->tree, fnum, 0, 4, 0, WRITE_LOCK); |
| 154 |
|
| 155 |
/* lock a range */ |
| 156 |
if (NT_STATUS_IS_ERR(status)) { |
| 157 |
torture_assert_ntstatus_ok(tctx, status, "lock failed in mux_write"); |
| 158 |
} |
| 159 |
|
| 160 |
cli->session->pid = 2; |
| 161 |
|
| 162 |
/* send an async write */ |
| 163 |
io.generic.level = RAW_WRITE_WRITEX; |
| 164 |
io.writex.in.file.fnum = fnum; |
| 165 |
io.writex.in.offset = 0; |
| 166 |
io.writex.in.wmode = 0; |
| 167 |
io.writex.in.remaining = 0; |
| 168 |
io.writex.in.count = 4; |
| 169 |
io.writex.in.data = (const uint8_t *)&fnum; |
| 170 |
req = smb_raw_write_send(cli->tree, &io); |
| 171 |
|
| 172 |
/* unlock the range */ |
| 173 |
cli->session->pid = 1; |
| 174 |
smbcli_unlock(cli->tree, fnum, 0, 4); |
| 175 |
|
| 176 |
/* and recv the async write reply */ |
| 177 |
status = smb_raw_write_recv(req, &io); |
| 178 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_FILE_LOCK_CONFLICT, "recv the async write reply"); |
| 179 |
|
| 180 |
smbcli_close(cli->tree, fnum); |
| 181 |
|
| 182 |
return ret; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
/* |
| 187 |
test a lock that conflicts with an existing lock |
| 188 |
*/ |
| 189 |
static bool test_mux_lock(struct torture_context *tctx, struct smbcli_state *cli, TALLOC_CTX *mem_ctx) |
| 190 |
{ |
| 191 |
union smb_lock io; |
| 192 |
NTSTATUS status; |
| 193 |
int fnum; |
| 194 |
bool ret = true; |
| 195 |
struct smbcli_request *req; |
| 196 |
struct smb_lock_entry lock[1]; |
| 197 |
struct timeval t; |
| 198 |
|
| 199 |
torture_comment(tctx, "TESTING MULTIPLEXED LOCK/LOCK/UNLOCK\n"); |
| 200 |
|
| 201 |
fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE); |
| 202 |
if (fnum == -1) { |
| 203 |
torture_comment(tctx, "open failed in mux_lock - %s\n", smbcli_errstr(cli->tree)); |
| 204 |
torture_assert(tctx, fnum != -1, "open failed in mux_lock"); |
| 205 |
} |
| 206 |
|
| 207 |
torture_comment(tctx, "establishing a lock\n"); |
| 208 |
io.lockx.level = RAW_LOCK_LOCKX; |
| 209 |
io.lockx.in.file.fnum = fnum; |
| 210 |
io.lockx.in.mode = 0; |
| 211 |
io.lockx.in.timeout = 0; |
| 212 |
io.lockx.in.lock_cnt = 1; |
| 213 |
io.lockx.in.ulock_cnt = 0; |
| 214 |
lock[0].pid = 1; |
| 215 |
lock[0].offset = 0; |
| 216 |
lock[0].count = 4; |
| 217 |
io.lockx.in.locks = &lock[0]; |
| 218 |
|
| 219 |
status = smb_raw_lock(cli->tree, &io); |
| 220 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "establishing a lock"); |
| 221 |
|
| 222 |
torture_comment(tctx, "the second lock will conflict with the first\n"); |
| 223 |
lock[0].pid = 2; |
| 224 |
io.lockx.in.timeout = 1000; |
| 225 |
status = smb_raw_lock(cli->tree, &io); |
| 226 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_FILE_LOCK_CONFLICT, "the second lock will conflict with the first"); |
| 227 |
|
| 228 |
torture_comment(tctx, "this will too, but we'll unlock while waiting\n"); |
| 229 |
t = timeval_current(); |
| 230 |
req = smb_raw_lock_send(cli->tree, &io); |
| 231 |
|
| 232 |
torture_comment(tctx, "unlock the first range\n"); |
| 233 |
lock[0].pid = 1; |
| 234 |
io.lockx.in.ulock_cnt = 1; |
| 235 |
io.lockx.in.lock_cnt = 0; |
| 236 |
io.lockx.in.timeout = 0; |
| 237 |
status = smb_raw_lock(cli->tree, &io); |
| 238 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "unlock the first range"); |
| 239 |
|
| 240 |
torture_comment(tctx, "recv the async reply\n"); |
| 241 |
status = smbcli_request_simple_recv(req); |
| 242 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "recv the async reply"); |
| 243 |
|
| 244 |
torture_comment(tctx, "async lock took %.2f msec\n", timeval_elapsed(&t) * 1000); |
| 245 |
torture_assert(tctx, timeval_elapsed(&t) <= 0.1, "failed to trigger early lock retry\n"); |
| 246 |
|
| 247 |
torture_comment(tctx, "reopening with an exit\n"); |
| 248 |
smb_raw_exit(cli->session); |
| 249 |
fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE); |
| 250 |
|
| 251 |
torture_comment(tctx, "Now trying with a cancel\n"); |
| 252 |
|
| 253 |
io.lockx.level = RAW_LOCK_LOCKX; |
| 254 |
io.lockx.in.file.fnum = fnum; |
| 255 |
io.lockx.in.mode = 0; |
| 256 |
io.lockx.in.timeout = 0; |
| 257 |
io.lockx.in.lock_cnt = 1; |
| 258 |
io.lockx.in.ulock_cnt = 0; |
| 259 |
lock[0].pid = 1; |
| 260 |
lock[0].offset = 0; |
| 261 |
lock[0].count = 4; |
| 262 |
io.lockx.in.locks = &lock[0]; |
| 263 |
|
| 264 |
status = smb_raw_lock(cli->tree, &io); |
| 265 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "Now trying with a cancel"); |
| 266 |
|
| 267 |
lock[0].pid = 2; |
| 268 |
io.lockx.in.timeout = 1000; |
| 269 |
status = smb_raw_lock(cli->tree, &io); |
| 270 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_FILE_LOCK_CONFLICT, "Now trying with a cancel pid 2"); |
| 271 |
|
| 272 |
req = smb_raw_lock_send(cli->tree, &io); |
| 273 |
|
| 274 |
/* cancel the blocking lock */ |
| 275 |
smb_raw_ntcancel(req); |
| 276 |
|
| 277 |
torture_comment(tctx, "sending 2nd cancel\n"); |
| 278 |
/* the 2nd cancel is totally harmless, but tests the server trying to |
| 279 |
cancel an already cancelled request */ |
| 280 |
smb_raw_ntcancel(req); |
| 281 |
|
| 282 |
torture_comment(tctx, "sent 2nd cancel\n"); |
| 283 |
|
| 284 |
lock[0].pid = 1; |
| 285 |
io.lockx.in.ulock_cnt = 1; |
| 286 |
io.lockx.in.lock_cnt = 0; |
| 287 |
io.lockx.in.timeout = 0; |
| 288 |
status = smb_raw_lock(cli->tree, &io); |
| 289 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "clear lock"); |
| 290 |
|
| 291 |
status = smbcli_request_simple_recv(req); |
| 292 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_FILE_LOCK_CONFLICT, "recv 2nd cancel"); |
| 293 |
|
| 294 |
torture_comment(tctx, "cancel a lock using exit to close file\n"); |
| 295 |
lock[0].pid = 1; |
| 296 |
io.lockx.in.ulock_cnt = 0; |
| 297 |
io.lockx.in.lock_cnt = 1; |
| 298 |
io.lockx.in.timeout = 1000; |
| 299 |
|
| 300 |
status = smb_raw_lock(cli->tree, &io); |
| 301 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "cancel a lock using exit to close file"); |
| 302 |
|
| 303 |
t = timeval_current(); |
| 304 |
lock[0].pid = 2; |
| 305 |
req = smb_raw_lock_send(cli->tree, &io); |
| 306 |
|
| 307 |
smb_raw_exit(cli->session); |
| 308 |
smb_raw_exit(cli->session); |
| 309 |
smb_raw_exit(cli->session); |
| 310 |
smb_raw_exit(cli->session); |
| 311 |
|
| 312 |
torture_comment(tctx, "recv the async reply\n"); |
| 313 |
status = smbcli_request_simple_recv(req); |
| 314 |
torture_assert_ntstatus_equal(tctx, status, NT_STATUS_RANGE_NOT_LOCKED, "recv the async reply"); |
| 315 |
torture_comment(tctx, "async lock exit took %.2f msec\n", timeval_elapsed(&t) * 1000); |
| 316 |
torture_assert(tctx, timeval_elapsed(&t) <= 0.1, "failed to trigger early lock failure\n"); |
| 317 |
|
| 318 |
return ret; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
/* |
| 324 |
basic testing of multiplexing notify |
| 325 |
*/ |
| 326 |
bool torture_raw_mux(struct torture_context *torture, struct smbcli_state *cli) |
| 327 |
{ |
| 328 |
bool ret = true; |
| 329 |
TALLOC_CTX *frame; |
| 330 |
|
| 331 |
torture_assert(torture, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR); |
| 332 |
frame = talloc_stackframe(); |
| 333 |
|
| 334 |
ret &= test_mux_open(torture, cli, frame); |
| 335 |
ret &= test_mux_write(torture, cli, frame); |
| 336 |
ret &= test_mux_lock(torture, cli, frame); |
| 337 |
|
| 338 |
smb_raw_exit(cli->session); |
| 339 |
smbcli_deltree(cli->tree, BASEDIR); |
| 340 |
TALLOC_FREE(frame); |
| 341 |
return ret; |
| 342 |
} |