1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | test suite for various write operations
|
---|
4 |
|
---|
5 | Copyright (C) Andrew Tridgell 2003
|
---|
6 |
|
---|
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.
|
---|
11 |
|
---|
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.
|
---|
16 |
|
---|
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/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 | #include "libcli/raw/libcliraw.h"
|
---|
23 | #include "system/time.h"
|
---|
24 | #include "system/filesys.h"
|
---|
25 | #include "libcli/libcli.h"
|
---|
26 | #include "torture/util.h"
|
---|
27 |
|
---|
28 | #define CHECK_STATUS(status, correct) do { \
|
---|
29 | if (!NT_STATUS_EQUAL(status, correct)) { \
|
---|
30 | torture_fail(tctx, talloc_asprintf(tctx, "(%s) Incorrect status %s - should be %s\n", \
|
---|
31 | __location__, nt_errstr(status), nt_errstr(correct))); \
|
---|
32 | ret = false; \
|
---|
33 | goto done; \
|
---|
34 | }} while (0)
|
---|
35 |
|
---|
36 | #define CHECK_VALUE(v, correct) do { \
|
---|
37 | if ((v) != (correct)) { \
|
---|
38 | torture_fail(tctx, talloc_asprintf(tctx, "(%s) Incorrect value %s=%d - should be %d\n", \
|
---|
39 | __location__, #v, v, correct)); \
|
---|
40 | ret = false; \
|
---|
41 | goto done; \
|
---|
42 | }} while (0)
|
---|
43 |
|
---|
44 | #define CHECK_BUFFER(buf, seed, len) do { \
|
---|
45 | if (!check_buffer(tctx, buf, seed, len, __location__)) { \
|
---|
46 | ret = false; \
|
---|
47 | goto done; \
|
---|
48 | }} while (0)
|
---|
49 |
|
---|
50 | #define CHECK_ALL_INFO(v, field) do { \
|
---|
51 | finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
|
---|
52 | finfo.all_info.in.file.path = fname; \
|
---|
53 | status = smb_raw_pathinfo(cli->tree, tctx, &finfo); \
|
---|
54 | CHECK_STATUS(status, NT_STATUS_OK); \
|
---|
55 | if ((v) != finfo.all_info.out.field) { \
|
---|
56 | torture_comment(tctx, "(%s) wrong value for field %s %.0f - %.0f\n", \
|
---|
57 | __location__, #field, (double)v, (double)finfo.all_info.out.field); \
|
---|
58 | dump_all_info(tctx, &finfo); \
|
---|
59 | ret = false; \
|
---|
60 | }} while (0)
|
---|
61 |
|
---|
62 |
|
---|
63 | #define BASEDIR "\\testwrite"
|
---|
64 |
|
---|
65 |
|
---|
66 | /*
|
---|
67 | setup a random buffer based on a seed
|
---|
68 | */
|
---|
69 | static void setup_buffer(uint8_t *buf, unsigned int seed, int len)
|
---|
70 | {
|
---|
71 | int i;
|
---|
72 | srandom(seed);
|
---|
73 | for (i=0;i<len;i++) buf[i] = random();
|
---|
74 | }
|
---|
75 |
|
---|
76 | /*
|
---|
77 | check a random buffer based on a seed
|
---|
78 | */
|
---|
79 | static bool check_buffer(struct torture_context *tctx,
|
---|
80 | uint8_t *buf, unsigned int seed, int len, const char *location)
|
---|
81 | {
|
---|
82 | int i;
|
---|
83 | srandom(seed);
|
---|
84 | for (i=0;i<len;i++) {
|
---|
85 | uint8_t v = random();
|
---|
86 | if (buf[i] != v) {
|
---|
87 | torture_fail(tctx, talloc_asprintf(tctx, "Buffer incorrect at %s! ofs=%d buf=0x%x correct=0x%x\n",
|
---|
88 | location, i, buf[i], v));
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /*
|
---|
96 | test write ops
|
---|
97 | */
|
---|
98 | static bool test_write(struct torture_context *tctx,
|
---|
99 | struct smbcli_state *cli)
|
---|
100 | {
|
---|
101 | union smb_write io;
|
---|
102 | NTSTATUS status;
|
---|
103 | bool ret = true;
|
---|
104 | int fnum;
|
---|
105 | uint8_t *buf;
|
---|
106 | const int maxsize = 90000;
|
---|
107 | const char *fname = BASEDIR "\\test.txt";
|
---|
108 | unsigned int seed = time(NULL);
|
---|
109 | union smb_fileinfo finfo;
|
---|
110 |
|
---|
111 | buf = talloc_zero_array(tctx, uint8_t, maxsize);
|
---|
112 |
|
---|
113 | if (!torture_setup_dir(cli, BASEDIR)) {
|
---|
114 | torture_fail(tctx, "failed to setup basedir");
|
---|
115 | }
|
---|
116 |
|
---|
117 | torture_comment(tctx, "Testing RAW_WRITE_WRITE\n");
|
---|
118 | io.generic.level = RAW_WRITE_WRITE;
|
---|
119 |
|
---|
120 | fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
|
---|
121 | if (fnum == -1) {
|
---|
122 | ret = false;
|
---|
123 | torture_fail_goto(tctx, done,
|
---|
124 | talloc_asprintf(tctx, "Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree)));
|
---|
125 | }
|
---|
126 |
|
---|
127 | torture_comment(tctx, "Trying zero write\n");
|
---|
128 | io.write.in.file.fnum = fnum;
|
---|
129 | io.write.in.count = 0;
|
---|
130 | io.write.in.offset = 0;
|
---|
131 | io.write.in.remaining = 0;
|
---|
132 | io.write.in.data = buf;
|
---|
133 | status = smb_raw_write(cli->tree, &io);
|
---|
134 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
135 | CHECK_VALUE(io.write.out.nwritten, 0);
|
---|
136 |
|
---|
137 | setup_buffer(buf, seed, maxsize);
|
---|
138 |
|
---|
139 | torture_comment(tctx, "Trying small write\n");
|
---|
140 | io.write.in.count = 9;
|
---|
141 | io.write.in.offset = 4;
|
---|
142 | io.write.in.data = buf;
|
---|
143 | status = smb_raw_write(cli->tree, &io);
|
---|
144 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
145 | CHECK_VALUE(io.write.out.nwritten, io.write.in.count);
|
---|
146 |
|
---|
147 | memset(buf, 0, maxsize);
|
---|
148 | if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
|
---|
149 | ret = false;
|
---|
150 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
151 | }
|
---|
152 | CHECK_BUFFER(buf+4, seed, 9);
|
---|
153 | CHECK_VALUE(IVAL(buf,0), 0);
|
---|
154 |
|
---|
155 | setup_buffer(buf, seed, maxsize);
|
---|
156 |
|
---|
157 | torture_comment(tctx, "Trying large write\n");
|
---|
158 | io.write.in.count = 4000;
|
---|
159 | io.write.in.offset = 0;
|
---|
160 | io.write.in.data = buf;
|
---|
161 | status = smb_raw_write(cli->tree, &io);
|
---|
162 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
163 | CHECK_VALUE(io.write.out.nwritten, 4000);
|
---|
164 |
|
---|
165 | memset(buf, 0, maxsize);
|
---|
166 | if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
|
---|
167 | ret = false;
|
---|
168 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
169 | }
|
---|
170 | CHECK_BUFFER(buf, seed, 4000);
|
---|
171 |
|
---|
172 | torture_comment(tctx, "Trying bad fnum\n");
|
---|
173 | io.write.in.file.fnum = fnum+1;
|
---|
174 | io.write.in.count = 4000;
|
---|
175 | io.write.in.offset = 0;
|
---|
176 | io.write.in.data = buf;
|
---|
177 | status = smb_raw_write(cli->tree, &io);
|
---|
178 | CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
|
---|
179 |
|
---|
180 | torture_comment(tctx, "Setting file as sparse\n");
|
---|
181 | status = torture_set_sparse(cli->tree, fnum);
|
---|
182 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
183 |
|
---|
184 | if (!(cli->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
|
---|
185 | torture_comment(tctx, "skipping large file tests - CAP_LARGE_FILES not set\n");
|
---|
186 | goto done;
|
---|
187 | }
|
---|
188 |
|
---|
189 | torture_comment(tctx, "Trying 2^32 offset\n");
|
---|
190 | setup_buffer(buf, seed, maxsize);
|
---|
191 | io.write.in.file.fnum = fnum;
|
---|
192 | io.write.in.count = 4000;
|
---|
193 | io.write.in.offset = 0xFFFFFFFF - 2000;
|
---|
194 | io.write.in.data = buf;
|
---|
195 | status = smb_raw_write(cli->tree, &io);
|
---|
196 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
197 | CHECK_VALUE(io.write.out.nwritten, 4000);
|
---|
198 | CHECK_ALL_INFO(io.write.in.count + (uint64_t)io.write.in.offset, size);
|
---|
199 |
|
---|
200 | memset(buf, 0, maxsize);
|
---|
201 | if (smbcli_read(cli->tree, fnum, buf, io.write.in.offset, 4000) != 4000) {
|
---|
202 | ret = false;
|
---|
203 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
204 | }
|
---|
205 | CHECK_BUFFER(buf, seed, 4000);
|
---|
206 |
|
---|
207 | done:
|
---|
208 | smbcli_close(cli->tree, fnum);
|
---|
209 | smb_raw_exit(cli->session);
|
---|
210 | smbcli_deltree(cli->tree, BASEDIR);
|
---|
211 | return ret;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /*
|
---|
216 | test writex ops
|
---|
217 | */
|
---|
218 | static bool test_writex(struct torture_context *tctx,
|
---|
219 | struct smbcli_state *cli)
|
---|
220 | {
|
---|
221 | union smb_write io;
|
---|
222 | NTSTATUS status;
|
---|
223 | bool ret = true;
|
---|
224 | int fnum, i;
|
---|
225 | uint8_t *buf;
|
---|
226 | const int maxsize = 90000;
|
---|
227 | const char *fname = BASEDIR "\\test.txt";
|
---|
228 | unsigned int seed = time(NULL);
|
---|
229 | union smb_fileinfo finfo;
|
---|
230 | int max_bits=63;
|
---|
231 |
|
---|
232 | if (!torture_setting_bool(tctx, "dangerous", false)) {
|
---|
233 | max_bits=33;
|
---|
234 | torture_comment(tctx, "dangerous not set - limiting range of test to 2^%d\n", max_bits);
|
---|
235 | }
|
---|
236 |
|
---|
237 | buf = talloc_zero_array(tctx, uint8_t, maxsize);
|
---|
238 |
|
---|
239 | if (!cli->transport->negotiate.lockread_supported) {
|
---|
240 | torture_comment(tctx, "Server does not support writeunlock - skipping\n");
|
---|
241 | return true;
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (!torture_setup_dir(cli, BASEDIR)) {
|
---|
245 | torture_fail(tctx, "failed to setup basedir");
|
---|
246 | }
|
---|
247 |
|
---|
248 | torture_comment(tctx, "Testing RAW_WRITE_WRITEX\n");
|
---|
249 | io.generic.level = RAW_WRITE_WRITEX;
|
---|
250 |
|
---|
251 | fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
|
---|
252 | if (fnum == -1) {
|
---|
253 | ret = false;
|
---|
254 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree)));
|
---|
255 | }
|
---|
256 |
|
---|
257 | torture_comment(tctx, "Trying zero write\n");
|
---|
258 | io.writex.in.file.fnum = fnum;
|
---|
259 | io.writex.in.offset = 0;
|
---|
260 | io.writex.in.wmode = 0;
|
---|
261 | io.writex.in.remaining = 0;
|
---|
262 | io.writex.in.count = 0;
|
---|
263 | io.writex.in.data = buf;
|
---|
264 | status = smb_raw_write(cli->tree, &io);
|
---|
265 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
266 | CHECK_VALUE(io.writex.out.nwritten, 0);
|
---|
267 |
|
---|
268 | setup_buffer(buf, seed, maxsize);
|
---|
269 |
|
---|
270 | torture_comment(tctx, "Trying small write\n");
|
---|
271 | io.writex.in.count = 9;
|
---|
272 | io.writex.in.offset = 4;
|
---|
273 | io.writex.in.data = buf;
|
---|
274 | status = smb_raw_write(cli->tree, &io);
|
---|
275 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
276 | CHECK_VALUE(io.writex.out.nwritten, io.writex.in.count);
|
---|
277 |
|
---|
278 | memset(buf, 0, maxsize);
|
---|
279 | if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
|
---|
280 | ret = false;
|
---|
281 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
282 | }
|
---|
283 | CHECK_BUFFER(buf+4, seed, 9);
|
---|
284 | CHECK_VALUE(IVAL(buf,0), 0);
|
---|
285 |
|
---|
286 | setup_buffer(buf, seed, maxsize);
|
---|
287 |
|
---|
288 | torture_comment(tctx, "Trying large write\n");
|
---|
289 | io.writex.in.count = 4000;
|
---|
290 | io.writex.in.offset = 0;
|
---|
291 | io.writex.in.data = buf;
|
---|
292 | status = smb_raw_write(cli->tree, &io);
|
---|
293 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
294 | CHECK_VALUE(io.writex.out.nwritten, 4000);
|
---|
295 |
|
---|
296 | memset(buf, 0, maxsize);
|
---|
297 | if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
|
---|
298 | ret = false;
|
---|
299 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
300 | }
|
---|
301 | CHECK_BUFFER(buf, seed, 4000);
|
---|
302 |
|
---|
303 | torture_comment(tctx, "Trying bad fnum\n");
|
---|
304 | io.writex.in.file.fnum = fnum+1;
|
---|
305 | io.writex.in.count = 4000;
|
---|
306 | io.writex.in.offset = 0;
|
---|
307 | io.writex.in.data = buf;
|
---|
308 | status = smb_raw_write(cli->tree, &io);
|
---|
309 | CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
|
---|
310 |
|
---|
311 | torture_comment(tctx, "Testing wmode\n");
|
---|
312 | io.writex.in.file.fnum = fnum;
|
---|
313 | io.writex.in.count = 1;
|
---|
314 | io.writex.in.offset = 0;
|
---|
315 | io.writex.in.wmode = 1;
|
---|
316 | io.writex.in.data = buf;
|
---|
317 | status = smb_raw_write(cli->tree, &io);
|
---|
318 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
319 | CHECK_VALUE(io.writex.out.nwritten, io.writex.in.count);
|
---|
320 |
|
---|
321 | io.writex.in.wmode = 2;
|
---|
322 | status = smb_raw_write(cli->tree, &io);
|
---|
323 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
324 | CHECK_VALUE(io.writex.out.nwritten, io.writex.in.count);
|
---|
325 |
|
---|
326 |
|
---|
327 | torture_comment(tctx, "Trying locked region\n");
|
---|
328 | cli->session->pid++;
|
---|
329 | if (NT_STATUS_IS_ERR(smbcli_lock(cli->tree, fnum, 3, 1, 0, WRITE_LOCK))) {
|
---|
330 | ret = false;
|
---|
331 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "Failed to lock file at %s\n", __location__));
|
---|
332 | }
|
---|
333 | cli->session->pid--;
|
---|
334 | io.writex.in.wmode = 0;
|
---|
335 | io.writex.in.count = 4;
|
---|
336 | io.writex.in.offset = 0;
|
---|
337 | status = smb_raw_write(cli->tree, &io);
|
---|
338 | CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
|
---|
339 |
|
---|
340 | torture_comment(tctx, "Setting file as sparse\n");
|
---|
341 | status = torture_set_sparse(cli->tree, fnum);
|
---|
342 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
343 |
|
---|
344 | if (!(cli->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
|
---|
345 | torture_skip(tctx, "skipping large file tests - CAP_LARGE_FILES not set\n");
|
---|
346 | }
|
---|
347 |
|
---|
348 | torture_comment(tctx, "Trying 2^32 offset\n");
|
---|
349 | setup_buffer(buf, seed, maxsize);
|
---|
350 | io.writex.in.file.fnum = fnum;
|
---|
351 | io.writex.in.count = 4000;
|
---|
352 | io.writex.in.offset = 0xFFFFFFFF - 2000;
|
---|
353 | io.writex.in.data = buf;
|
---|
354 | status = smb_raw_write(cli->tree, &io);
|
---|
355 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
356 | CHECK_VALUE(io.writex.out.nwritten, 4000);
|
---|
357 | CHECK_ALL_INFO(io.writex.in.count + (uint64_t)io.writex.in.offset, size);
|
---|
358 |
|
---|
359 | memset(buf, 0, maxsize);
|
---|
360 | if (smbcli_read(cli->tree, fnum, buf, io.writex.in.offset, 4000) != 4000) {
|
---|
361 | ret = false;
|
---|
362 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
363 | }
|
---|
364 | CHECK_BUFFER(buf, seed, 4000);
|
---|
365 |
|
---|
366 | for (i=33;i<max_bits;i++) {
|
---|
367 | torture_comment(tctx, "Trying 2^%d offset\n", i);
|
---|
368 | setup_buffer(buf, seed+1, maxsize);
|
---|
369 | io.writex.in.file.fnum = fnum;
|
---|
370 | io.writex.in.count = 4000;
|
---|
371 | io.writex.in.offset = ((uint64_t)1) << i;
|
---|
372 | io.writex.in.data = buf;
|
---|
373 | status = smb_raw_write(cli->tree, &io);
|
---|
374 | if (i>33 &&
|
---|
375 | NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
|
---|
376 | break;
|
---|
377 | }
|
---|
378 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
379 | CHECK_VALUE(io.writex.out.nwritten, 4000);
|
---|
380 | CHECK_ALL_INFO(io.writex.in.count + (uint64_t)io.writex.in.offset, size);
|
---|
381 |
|
---|
382 | memset(buf, 0, maxsize);
|
---|
383 | if (smbcli_read(cli->tree, fnum, buf, io.writex.in.offset, 4000) != 4000) {
|
---|
384 | ret = false;
|
---|
385 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
386 | }
|
---|
387 | CHECK_BUFFER(buf, seed+1, 4000);
|
---|
388 | }
|
---|
389 | torture_comment(tctx, "limit is 2^%d\n", i);
|
---|
390 |
|
---|
391 | setup_buffer(buf, seed, maxsize);
|
---|
392 |
|
---|
393 | done:
|
---|
394 | smbcli_close(cli->tree, fnum);
|
---|
395 | smb_raw_exit(cli->session);
|
---|
396 | smbcli_deltree(cli->tree, BASEDIR);
|
---|
397 | return ret;
|
---|
398 | }
|
---|
399 |
|
---|
400 |
|
---|
401 | /*
|
---|
402 | test write unlock ops
|
---|
403 | */
|
---|
404 | static bool test_writeunlock(struct torture_context *tctx,
|
---|
405 | struct smbcli_state *cli)
|
---|
406 | {
|
---|
407 | union smb_write io;
|
---|
408 | NTSTATUS status;
|
---|
409 | bool ret = true;
|
---|
410 | int fnum;
|
---|
411 | uint8_t *buf;
|
---|
412 | const int maxsize = 90000;
|
---|
413 | const char *fname = BASEDIR "\\test.txt";
|
---|
414 | unsigned int seed = time(NULL);
|
---|
415 | union smb_fileinfo finfo;
|
---|
416 |
|
---|
417 | buf = talloc_zero_array(tctx, uint8_t, maxsize);
|
---|
418 |
|
---|
419 | if (!cli->transport->negotiate.lockread_supported) {
|
---|
420 | torture_skip(tctx, "Server does not support writeunlock - skipping\n");
|
---|
421 | }
|
---|
422 |
|
---|
423 | if (!torture_setup_dir(cli, BASEDIR)) {
|
---|
424 | torture_fail(tctx, "failed to setup basedir");
|
---|
425 | }
|
---|
426 |
|
---|
427 | torture_comment(tctx, "Testing RAW_WRITE_WRITEUNLOCK\n");
|
---|
428 | io.generic.level = RAW_WRITE_WRITEUNLOCK;
|
---|
429 |
|
---|
430 | fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
|
---|
431 | if (fnum == -1) {
|
---|
432 | ret = false;
|
---|
433 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree)));
|
---|
434 | }
|
---|
435 |
|
---|
436 | torture_comment(tctx, "Trying zero write\n");
|
---|
437 | io.writeunlock.in.file.fnum = fnum;
|
---|
438 | io.writeunlock.in.count = 0;
|
---|
439 | io.writeunlock.in.offset = 0;
|
---|
440 | io.writeunlock.in.remaining = 0;
|
---|
441 | io.writeunlock.in.data = buf;
|
---|
442 | status = smb_raw_write(cli->tree, &io);
|
---|
443 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
444 | CHECK_VALUE(io.writeunlock.out.nwritten, io.writeunlock.in.count);
|
---|
445 |
|
---|
446 | setup_buffer(buf, seed, maxsize);
|
---|
447 |
|
---|
448 | torture_comment(tctx, "Trying small write\n");
|
---|
449 | io.writeunlock.in.count = 9;
|
---|
450 | io.writeunlock.in.offset = 4;
|
---|
451 | io.writeunlock.in.data = buf;
|
---|
452 | status = smb_raw_write(cli->tree, &io);
|
---|
453 | CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
|
---|
454 | if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
|
---|
455 | ret = false;
|
---|
456 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
457 | }
|
---|
458 | CHECK_BUFFER(buf+4, seed, 9);
|
---|
459 | CHECK_VALUE(IVAL(buf,0), 0);
|
---|
460 |
|
---|
461 | setup_buffer(buf, seed, maxsize);
|
---|
462 | smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count,
|
---|
463 | 0, WRITE_LOCK);
|
---|
464 | status = smb_raw_write(cli->tree, &io);
|
---|
465 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
466 | CHECK_VALUE(io.writeunlock.out.nwritten, io.writeunlock.in.count);
|
---|
467 |
|
---|
468 | memset(buf, 0, maxsize);
|
---|
469 | if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
|
---|
470 | ret = false;
|
---|
471 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
472 | }
|
---|
473 | CHECK_BUFFER(buf+4, seed, 9);
|
---|
474 | CHECK_VALUE(IVAL(buf,0), 0);
|
---|
475 |
|
---|
476 | setup_buffer(buf, seed, maxsize);
|
---|
477 |
|
---|
478 | torture_comment(tctx, "Trying large write\n");
|
---|
479 | io.writeunlock.in.count = 4000;
|
---|
480 | io.writeunlock.in.offset = 0;
|
---|
481 | io.writeunlock.in.data = buf;
|
---|
482 | smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count,
|
---|
483 | 0, WRITE_LOCK);
|
---|
484 | status = smb_raw_write(cli->tree, &io);
|
---|
485 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
486 | CHECK_VALUE(io.writeunlock.out.nwritten, 4000);
|
---|
487 |
|
---|
488 | status = smb_raw_write(cli->tree, &io);
|
---|
489 | CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
|
---|
490 |
|
---|
491 | memset(buf, 0, maxsize);
|
---|
492 | if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
|
---|
493 | ret = false;
|
---|
494 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
495 | }
|
---|
496 | CHECK_BUFFER(buf, seed, 4000);
|
---|
497 |
|
---|
498 | torture_comment(tctx, "Trying bad fnum\n");
|
---|
499 | io.writeunlock.in.file.fnum = fnum+1;
|
---|
500 | io.writeunlock.in.count = 4000;
|
---|
501 | io.writeunlock.in.offset = 0;
|
---|
502 | io.writeunlock.in.data = buf;
|
---|
503 | status = smb_raw_write(cli->tree, &io);
|
---|
504 | CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
|
---|
505 |
|
---|
506 | torture_comment(tctx, "Setting file as sparse\n");
|
---|
507 | status = torture_set_sparse(cli->tree, fnum);
|
---|
508 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
509 |
|
---|
510 | if (!(cli->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
|
---|
511 | torture_skip(tctx, "skipping large file tests - CAP_LARGE_FILES not set\n");
|
---|
512 | }
|
---|
513 |
|
---|
514 | torture_comment(tctx, "Trying 2^32 offset\n");
|
---|
515 | setup_buffer(buf, seed, maxsize);
|
---|
516 | io.writeunlock.in.file.fnum = fnum;
|
---|
517 | io.writeunlock.in.count = 4000;
|
---|
518 | io.writeunlock.in.offset = 0xFFFFFFFF - 2000;
|
---|
519 | io.writeunlock.in.data = buf;
|
---|
520 | smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count,
|
---|
521 | 0, WRITE_LOCK);
|
---|
522 | status = smb_raw_write(cli->tree, &io);
|
---|
523 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
524 | CHECK_VALUE(io.writeunlock.out.nwritten, 4000);
|
---|
525 | CHECK_ALL_INFO(io.writeunlock.in.count + (uint64_t)io.writeunlock.in.offset, size);
|
---|
526 |
|
---|
527 | memset(buf, 0, maxsize);
|
---|
528 | if (smbcli_read(cli->tree, fnum, buf, io.writeunlock.in.offset, 4000) != 4000) {
|
---|
529 | ret = false;
|
---|
530 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
531 | }
|
---|
532 | CHECK_BUFFER(buf, seed, 4000);
|
---|
533 |
|
---|
534 | done:
|
---|
535 | smbcli_close(cli->tree, fnum);
|
---|
536 | smb_raw_exit(cli->session);
|
---|
537 | smbcli_deltree(cli->tree, BASEDIR);
|
---|
538 | return ret;
|
---|
539 | }
|
---|
540 |
|
---|
541 |
|
---|
542 | /*
|
---|
543 | test write close ops
|
---|
544 | */
|
---|
545 | static bool test_writeclose(struct torture_context *tctx,
|
---|
546 | struct smbcli_state *cli)
|
---|
547 | {
|
---|
548 | union smb_write io;
|
---|
549 | NTSTATUS status;
|
---|
550 | bool ret = true;
|
---|
551 | int fnum;
|
---|
552 | uint8_t *buf;
|
---|
553 | const int maxsize = 90000;
|
---|
554 | const char *fname = BASEDIR "\\test.txt";
|
---|
555 | unsigned int seed = time(NULL);
|
---|
556 | union smb_fileinfo finfo;
|
---|
557 |
|
---|
558 | buf = talloc_zero_array(tctx, uint8_t, maxsize);
|
---|
559 |
|
---|
560 | if (!torture_setting_bool(tctx, "writeclose_support", true)) {
|
---|
561 | torture_skip(tctx, "Server does not support writeclose - skipping\n");
|
---|
562 | }
|
---|
563 |
|
---|
564 | if (!torture_setup_dir(cli, BASEDIR)) {
|
---|
565 | torture_fail(tctx, "failed to setup basedir");
|
---|
566 | }
|
---|
567 |
|
---|
568 | torture_comment(tctx, "Testing RAW_WRITE_WRITECLOSE\n");
|
---|
569 | io.generic.level = RAW_WRITE_WRITECLOSE;
|
---|
570 |
|
---|
571 | fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
|
---|
572 | if (fnum == -1) {
|
---|
573 | ret = false;
|
---|
574 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree)));
|
---|
575 | }
|
---|
576 |
|
---|
577 | torture_comment(tctx, "Trying zero write\n");
|
---|
578 | io.writeclose.in.file.fnum = fnum;
|
---|
579 | io.writeclose.in.count = 0;
|
---|
580 | io.writeclose.in.offset = 0;
|
---|
581 | io.writeclose.in.mtime = 0;
|
---|
582 | io.writeclose.in.data = buf;
|
---|
583 | status = smb_raw_write(cli->tree, &io);
|
---|
584 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
585 | CHECK_VALUE(io.writeclose.out.nwritten, io.writeclose.in.count);
|
---|
586 |
|
---|
587 | status = smb_raw_write(cli->tree, &io);
|
---|
588 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
589 | CHECK_VALUE(io.writeclose.out.nwritten, io.writeclose.in.count);
|
---|
590 |
|
---|
591 | setup_buffer(buf, seed, maxsize);
|
---|
592 |
|
---|
593 | torture_comment(tctx, "Trying small write\n");
|
---|
594 | io.writeclose.in.count = 9;
|
---|
595 | io.writeclose.in.offset = 4;
|
---|
596 | io.writeclose.in.data = buf;
|
---|
597 | status = smb_raw_write(cli->tree, &io);
|
---|
598 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
599 |
|
---|
600 | status = smb_raw_write(cli->tree, &io);
|
---|
601 | CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
|
---|
602 |
|
---|
603 | fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
|
---|
604 | io.writeclose.in.file.fnum = fnum;
|
---|
605 |
|
---|
606 | if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
|
---|
607 | ret = false;
|
---|
608 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
609 | }
|
---|
610 | CHECK_BUFFER(buf+4, seed, 9);
|
---|
611 | CHECK_VALUE(IVAL(buf,0), 0);
|
---|
612 |
|
---|
613 | setup_buffer(buf, seed, maxsize);
|
---|
614 | status = smb_raw_write(cli->tree, &io);
|
---|
615 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
616 | CHECK_VALUE(io.writeclose.out.nwritten, io.writeclose.in.count);
|
---|
617 |
|
---|
618 | fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
|
---|
619 | io.writeclose.in.file.fnum = fnum;
|
---|
620 |
|
---|
621 | memset(buf, 0, maxsize);
|
---|
622 | if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
|
---|
623 | ret = false;
|
---|
624 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
625 | }
|
---|
626 | CHECK_BUFFER(buf+4, seed, 9);
|
---|
627 | CHECK_VALUE(IVAL(buf,0), 0);
|
---|
628 |
|
---|
629 | setup_buffer(buf, seed, maxsize);
|
---|
630 |
|
---|
631 | torture_comment(tctx, "Trying large write\n");
|
---|
632 | io.writeclose.in.count = 4000;
|
---|
633 | io.writeclose.in.offset = 0;
|
---|
634 | io.writeclose.in.data = buf;
|
---|
635 | status = smb_raw_write(cli->tree, &io);
|
---|
636 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
637 | CHECK_VALUE(io.writeclose.out.nwritten, 4000);
|
---|
638 |
|
---|
639 | status = smb_raw_write(cli->tree, &io);
|
---|
640 | CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
|
---|
641 |
|
---|
642 | fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
|
---|
643 | io.writeclose.in.file.fnum = fnum;
|
---|
644 |
|
---|
645 | memset(buf, 0, maxsize);
|
---|
646 | if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
|
---|
647 | ret = false;
|
---|
648 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
649 | }
|
---|
650 | CHECK_BUFFER(buf, seed, 4000);
|
---|
651 |
|
---|
652 | torture_comment(tctx, "Trying bad fnum\n");
|
---|
653 | io.writeclose.in.file.fnum = fnum+1;
|
---|
654 | io.writeclose.in.count = 4000;
|
---|
655 | io.writeclose.in.offset = 0;
|
---|
656 | io.writeclose.in.data = buf;
|
---|
657 | status = smb_raw_write(cli->tree, &io);
|
---|
658 | CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
|
---|
659 |
|
---|
660 | torture_comment(tctx, "Setting file as sparse\n");
|
---|
661 | status = torture_set_sparse(cli->tree, fnum);
|
---|
662 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
663 |
|
---|
664 | if (!(cli->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
|
---|
665 | torture_skip(tctx, "skipping large file tests - CAP_LARGE_FILES not set\n");
|
---|
666 | }
|
---|
667 |
|
---|
668 | torture_comment(tctx, "Trying 2^32 offset\n");
|
---|
669 | setup_buffer(buf, seed, maxsize);
|
---|
670 | io.writeclose.in.file.fnum = fnum;
|
---|
671 | io.writeclose.in.count = 4000;
|
---|
672 | io.writeclose.in.offset = 0xFFFFFFFF - 2000;
|
---|
673 | io.writeclose.in.data = buf;
|
---|
674 | status = smb_raw_write(cli->tree, &io);
|
---|
675 | CHECK_STATUS(status, NT_STATUS_OK);
|
---|
676 | CHECK_VALUE(io.writeclose.out.nwritten, 4000);
|
---|
677 | CHECK_ALL_INFO(io.writeclose.in.count + (uint64_t)io.writeclose.in.offset, size);
|
---|
678 |
|
---|
679 | fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
|
---|
680 | io.writeclose.in.file.fnum = fnum;
|
---|
681 |
|
---|
682 | memset(buf, 0, maxsize);
|
---|
683 | if (smbcli_read(cli->tree, fnum, buf, io.writeclose.in.offset, 4000) != 4000) {
|
---|
684 | ret = false;
|
---|
685 | torture_fail_goto(tctx, done, talloc_asprintf(tctx, "read failed at %s\n", __location__));
|
---|
686 | }
|
---|
687 | CHECK_BUFFER(buf, seed, 4000);
|
---|
688 |
|
---|
689 | done:
|
---|
690 | smbcli_close(cli->tree, fnum);
|
---|
691 | smb_raw_exit(cli->session);
|
---|
692 | smbcli_deltree(cli->tree, BASEDIR);
|
---|
693 | return ret;
|
---|
694 | }
|
---|
695 |
|
---|
696 | /*
|
---|
697 | basic testing of write calls
|
---|
698 | */
|
---|
699 | struct torture_suite *torture_raw_write(TALLOC_CTX *mem_ctx)
|
---|
700 | {
|
---|
701 | struct torture_suite *suite = torture_suite_create(mem_ctx, "write");
|
---|
702 |
|
---|
703 | torture_suite_add_1smb_test(suite, "write", test_write);
|
---|
704 | torture_suite_add_1smb_test(suite, "write unlock", test_writeunlock);
|
---|
705 | torture_suite_add_1smb_test(suite, "write close", test_writeclose);
|
---|
706 | torture_suite_add_1smb_test(suite, "writex", test_writex);
|
---|
707 |
|
---|
708 | return suite;
|
---|
709 | }
|
---|