Change uint_t to unsigned int in source4
[garming/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 "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, 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(uint8_t *buf, unsigned int 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         unsigned int 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         unsigned int 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         unsigned int seed = time(NULL);
424         union smb_fileinfo finfo;
425
426         buf = talloc_zero_array(tctx, uint8_t, maxsize);
427
428         if (!cli->transport->negotiate.lockread_supported) {
429                 printf("Server does not support writeunlock - skipping\n");
430                 return true;
431         }
432
433         if (!torture_setup_dir(cli, BASEDIR)) {
434                 return false;
435         }
436
437         printf("Testing RAW_WRITE_WRITEUNLOCK\n");
438         io.generic.level = RAW_WRITE_WRITEUNLOCK;
439         
440         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
441         if (fnum == -1) {
442                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
443                 ret = false;
444                 goto done;
445         }
446
447         printf("Trying zero write\n");
448         io.writeunlock.in.file.fnum = fnum;
449         io.writeunlock.in.count = 0;
450         io.writeunlock.in.offset = 0;
451         io.writeunlock.in.remaining = 0;
452         io.writeunlock.in.data = buf;
453         status = smb_raw_write(cli->tree, &io);
454         CHECK_STATUS(status, NT_STATUS_OK);
455         CHECK_VALUE(io.writeunlock.out.nwritten, io.writeunlock.in.count);
456
457         setup_buffer(buf, seed, maxsize);
458
459         printf("Trying small write\n");
460         io.writeunlock.in.count = 9;
461         io.writeunlock.in.offset = 4;
462         io.writeunlock.in.data = buf;
463         status = smb_raw_write(cli->tree, &io);
464         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
465         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
466                 printf("read failed at %s\n", __location__);
467                 ret = false;
468                 goto done;
469         }
470         CHECK_BUFFER(buf+4, seed, 9);
471         CHECK_VALUE(IVAL(buf,0), 0);
472
473         setup_buffer(buf, seed, maxsize);
474         smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count, 
475                  0, WRITE_LOCK);
476         status = smb_raw_write(cli->tree, &io);
477         CHECK_STATUS(status, NT_STATUS_OK);
478         CHECK_VALUE(io.writeunlock.out.nwritten, io.writeunlock.in.count);
479
480         memset(buf, 0, maxsize);
481         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
482                 printf("read failed at %s\n", __location__);
483                 ret = false;
484                 goto done;
485         }
486         CHECK_BUFFER(buf+4, seed, 9);
487         CHECK_VALUE(IVAL(buf,0), 0);
488
489         setup_buffer(buf, seed, maxsize);
490
491         printf("Trying large write\n");
492         io.writeunlock.in.count = 4000;
493         io.writeunlock.in.offset = 0;
494         io.writeunlock.in.data = buf;
495         smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count, 
496                  0, WRITE_LOCK);
497         status = smb_raw_write(cli->tree, &io);
498         CHECK_STATUS(status, NT_STATUS_OK);
499         CHECK_VALUE(io.writeunlock.out.nwritten, 4000);
500
501         status = smb_raw_write(cli->tree, &io);
502         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
503
504         memset(buf, 0, maxsize);
505         if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
506                 printf("read failed at %s\n", __location__);
507                 ret = false;
508                 goto done;
509         }
510         CHECK_BUFFER(buf, seed, 4000);
511
512         printf("Trying bad fnum\n");
513         io.writeunlock.in.file.fnum = fnum+1;
514         io.writeunlock.in.count = 4000;
515         io.writeunlock.in.offset = 0;
516         io.writeunlock.in.data = buf;
517         status = smb_raw_write(cli->tree, &io);
518         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
519
520         printf("Setting file as sparse\n");
521         status = torture_set_sparse(cli->tree, fnum);
522         CHECK_STATUS(status, NT_STATUS_OK);
523         
524         if (!(cli->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
525                 printf("skipping large file tests - CAP_LARGE_FILES not set\n");
526                 goto done;
527         }
528
529         printf("Trying 2^32 offset\n");
530         setup_buffer(buf, seed, maxsize);
531         io.writeunlock.in.file.fnum = fnum;
532         io.writeunlock.in.count = 4000;
533         io.writeunlock.in.offset = 0xFFFFFFFF - 2000;
534         io.writeunlock.in.data = buf;
535         smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count, 
536                  0, WRITE_LOCK);
537         status = smb_raw_write(cli->tree, &io);
538         CHECK_STATUS(status, NT_STATUS_OK);
539         CHECK_VALUE(io.writeunlock.out.nwritten, 4000);
540         CHECK_ALL_INFO(io.writeunlock.in.count + (uint64_t)io.writeunlock.in.offset, size);
541
542         memset(buf, 0, maxsize);
543         if (smbcli_read(cli->tree, fnum, buf, io.writeunlock.in.offset, 4000) != 4000) {
544                 printf("read failed at %s\n", __location__);
545                 ret = false;
546                 goto done;
547         }
548         CHECK_BUFFER(buf, seed, 4000);
549
550 done:
551         smbcli_close(cli->tree, fnum);
552         smb_raw_exit(cli->session);
553         smbcli_deltree(cli->tree, BASEDIR);
554         return ret;
555 }
556
557
558 /*
559   test write close ops
560 */
561 static bool test_writeclose(struct torture_context *tctx, 
562                                                         struct smbcli_state *cli)
563 {
564         union smb_write io;
565         NTSTATUS status;
566         bool ret = true;
567         int fnum;
568         uint8_t *buf;
569         const int maxsize = 90000;
570         const char *fname = BASEDIR "\\test.txt";
571         unsigned int seed = time(NULL);
572         union smb_fileinfo finfo;
573
574         buf = talloc_zero_array(tctx, uint8_t, maxsize);
575
576         if (!torture_setting_bool(tctx, "writeclose_support", true)) {
577                 printf("Server does not support writeclose - skipping\n");
578                 return true;
579         }
580
581         if (!torture_setup_dir(cli, BASEDIR)) {
582                 return false;
583         }
584
585         printf("Testing RAW_WRITE_WRITECLOSE\n");
586         io.generic.level = RAW_WRITE_WRITECLOSE;
587         
588         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
589         if (fnum == -1) {
590                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
591                 ret = false;
592                 goto done;
593         }
594
595         printf("Trying zero write\n");
596         io.writeclose.in.file.fnum = fnum;
597         io.writeclose.in.count = 0;
598         io.writeclose.in.offset = 0;
599         io.writeclose.in.mtime = 0;
600         io.writeclose.in.data = buf;
601         status = smb_raw_write(cli->tree, &io);
602         CHECK_STATUS(status, NT_STATUS_OK);
603         CHECK_VALUE(io.writeclose.out.nwritten, io.writeclose.in.count);
604
605         status = smb_raw_write(cli->tree, &io);
606         CHECK_STATUS(status, NT_STATUS_OK);
607         CHECK_VALUE(io.writeclose.out.nwritten, io.writeclose.in.count);
608
609         setup_buffer(buf, seed, maxsize);
610
611         printf("Trying small write\n");
612         io.writeclose.in.count = 9;
613         io.writeclose.in.offset = 4;
614         io.writeclose.in.data = buf;
615         status = smb_raw_write(cli->tree, &io);
616         CHECK_STATUS(status, NT_STATUS_OK);
617
618         status = smb_raw_write(cli->tree, &io);
619         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
620
621         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
622         io.writeclose.in.file.fnum = fnum;
623
624         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
625                 printf("read failed at %s\n", __location__);
626                 ret = false;
627                 goto done;
628         }
629         CHECK_BUFFER(buf+4, seed, 9);
630         CHECK_VALUE(IVAL(buf,0), 0);
631
632         setup_buffer(buf, seed, maxsize);
633         status = smb_raw_write(cli->tree, &io);
634         CHECK_STATUS(status, NT_STATUS_OK);
635         CHECK_VALUE(io.writeclose.out.nwritten, io.writeclose.in.count);
636
637         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
638         io.writeclose.in.file.fnum = fnum;
639
640         memset(buf, 0, maxsize);
641         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
642                 printf("read failed at %s\n", __location__);
643                 ret = false;
644                 goto done;
645         }
646         CHECK_BUFFER(buf+4, seed, 9);
647         CHECK_VALUE(IVAL(buf,0), 0);
648
649         setup_buffer(buf, seed, maxsize);
650
651         printf("Trying large write\n");
652         io.writeclose.in.count = 4000;
653         io.writeclose.in.offset = 0;
654         io.writeclose.in.data = buf;
655         status = smb_raw_write(cli->tree, &io);
656         CHECK_STATUS(status, NT_STATUS_OK);
657         CHECK_VALUE(io.writeclose.out.nwritten, 4000);
658
659         status = smb_raw_write(cli->tree, &io);
660         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
661
662         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
663         io.writeclose.in.file.fnum = fnum;
664
665         memset(buf, 0, maxsize);
666         if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
667                 printf("read failed at %s\n", __location__);
668                 ret = false;
669                 goto done;
670         }
671         CHECK_BUFFER(buf, seed, 4000);
672
673         printf("Trying bad fnum\n");
674         io.writeclose.in.file.fnum = fnum+1;
675         io.writeclose.in.count = 4000;
676         io.writeclose.in.offset = 0;
677         io.writeclose.in.data = buf;
678         status = smb_raw_write(cli->tree, &io);
679         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
680
681         printf("Setting file as sparse\n");
682         status = torture_set_sparse(cli->tree, fnum);
683         CHECK_STATUS(status, NT_STATUS_OK);
684         
685         if (!(cli->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
686                 printf("skipping large file tests - CAP_LARGE_FILES not set\n");
687                 goto done;
688         }
689
690         printf("Trying 2^32 offset\n");
691         setup_buffer(buf, seed, maxsize);
692         io.writeclose.in.file.fnum = fnum;
693         io.writeclose.in.count = 4000;
694         io.writeclose.in.offset = 0xFFFFFFFF - 2000;
695         io.writeclose.in.data = buf;
696         status = smb_raw_write(cli->tree, &io);
697         CHECK_STATUS(status, NT_STATUS_OK);
698         CHECK_VALUE(io.writeclose.out.nwritten, 4000);
699         CHECK_ALL_INFO(io.writeclose.in.count + (uint64_t)io.writeclose.in.offset, size);
700
701         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
702         io.writeclose.in.file.fnum = fnum;
703
704         memset(buf, 0, maxsize);
705         if (smbcli_read(cli->tree, fnum, buf, io.writeclose.in.offset, 4000) != 4000) {
706                 printf("read failed at %s\n", __location__);
707                 ret = false;
708                 goto done;
709         }
710         CHECK_BUFFER(buf, seed, 4000);
711
712 done:
713         smbcli_close(cli->tree, fnum);
714         smb_raw_exit(cli->session);
715         smbcli_deltree(cli->tree, BASEDIR);
716         return ret;
717 }
718
719 /* 
720    basic testing of write calls
721 */
722 struct torture_suite *torture_raw_write(TALLOC_CTX *mem_ctx)
723 {
724         struct torture_suite *suite = torture_suite_create(mem_ctx, "WRITE");
725
726         torture_suite_add_1smb_test(suite, "write", test_write);
727         torture_suite_add_1smb_test(suite, "write unlock", test_writeunlock);
728         torture_suite_add_1smb_test(suite, "write close", test_writeclose);
729         torture_suite_add_1smb_test(suite, "writex", test_writex);
730
731         return suite;
732 }