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