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