r19504: - fixed a free error in file_lines_parse
[kai/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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
27 #include "libcli/libcli.h"
28 #include "torture/util.h"
29
30 #define CHECK_STATUS(status, correct) do { \
31         if (!NT_STATUS_EQUAL(status, correct)) { \
32                 printf("(%s) Incorrect status %s - should be %s\n", \
33                        __location__, nt_errstr(status), nt_errstr(correct)); \
34                 ret = False; \
35                 goto done; \
36         }} while (0)
37
38 #define CHECK_VALUE(v, correct) do { \
39         if ((v) != (correct)) { \
40                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
41                        __location__, #v, v, correct); \
42                 ret = False; \
43                 goto done; \
44         }} while (0)
45
46 #define CHECK_BUFFER(buf, seed, len) do { \
47         if (!check_buffer(buf, seed, len, __location__)) { \
48                 ret = False; \
49                 goto done; \
50         }} while (0)
51
52 #define CHECK_ALL_INFO(v, field) do { \
53         finfo.all_info.level = RAW_FILEINFO_ALL_INFO; \
54         finfo.all_info.in.file.path = fname; \
55         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo); \
56         CHECK_STATUS(status, NT_STATUS_OK); \
57         if ((v) != finfo.all_info.out.field) { \
58                 printf("(%s) wrong value for field %s  %.0f - %.0f\n", \
59                        __location__, #field, (double)v, (double)finfo.all_info.out.field); \
60                 dump_all_info(mem_ctx, &finfo); \
61                 ret = False; \
62         }} while (0)
63
64
65 #define BASEDIR "\\testwrite"
66
67
68 /*
69   setup a random buffer based on a seed
70 */
71 static void setup_buffer(uint8_t *buf, uint_t seed, int len)
72 {
73         int i;
74         srandom(seed);
75         for (i=0;i<len;i++) buf[i] = random();
76 }
77
78 /*
79   check a random buffer based on a seed
80 */
81 static BOOL check_buffer(uint8_t *buf, uint_t seed, int len, const char *location)
82 {
83         int i;
84         srandom(seed);
85         for (i=0;i<len;i++) {
86                 uint8_t v = random();
87                 if (buf[i] != v) {
88                         printf("Buffer incorrect at %s! ofs=%d buf=0x%x correct=0x%x\n", 
89                                location, i, buf[i], v);
90                         return False;
91                 }
92         }
93         return True;
94 }
95
96 /*
97   test write ops
98 */
99 static BOOL test_write(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
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_size(mem_ctx, 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         printf("Trying 2^32 offset\n");
192         setup_buffer(buf, seed, maxsize);
193         io.write.in.file.fnum = fnum;
194         io.write.in.count = 4000;
195         io.write.in.offset = 0xFFFFFFFF - 2000;
196         io.write.in.data = buf;
197         status = smb_raw_write(cli->tree, &io);
198         CHECK_STATUS(status, NT_STATUS_OK);
199         CHECK_VALUE(io.write.out.nwritten, 4000);
200         CHECK_ALL_INFO(io.write.in.count + (uint64_t)io.write.in.offset, size);
201
202         memset(buf, 0, maxsize);
203         if (smbcli_read(cli->tree, fnum, buf, io.write.in.offset, 4000) != 4000) {
204                 printf("read failed at %s\n", __location__);
205                 ret = False;
206                 goto done;
207         }
208         CHECK_BUFFER(buf, seed, 4000);
209
210 done:
211         smbcli_close(cli->tree, fnum);
212         smb_raw_exit(cli->session);
213         smbcli_deltree(cli->tree, BASEDIR);
214         return ret;
215 }
216
217
218 /*
219   test writex ops
220 */
221 static BOOL test_writex(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
222 {
223         union smb_write io;
224         NTSTATUS status;
225         BOOL ret = True;
226         int fnum, i;
227         uint8_t *buf;
228         const int maxsize = 90000;
229         const char *fname = BASEDIR "\\test.txt";
230         uint_t seed = time(NULL);
231         union smb_fileinfo finfo;
232         int max_bits=63;
233
234         if (!lp_parm_bool(-1, "torture", "dangerous", False)) {
235                 max_bits=33;
236                 printf("dangerous not set - limiting range of test to 2^%d\n", max_bits);
237         }
238
239         buf = talloc_zero_size(mem_ctx, maxsize);
240
241         if (!torture_setup_dir(cli, BASEDIR)) {
242                 return False;
243         }
244
245         printf("Testing RAW_WRITE_WRITEX\n");
246         io.generic.level = RAW_WRITE_WRITEX;
247         
248         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
249         if (fnum == -1) {
250                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
251                 ret = False;
252                 goto done;
253         }
254
255         printf("Trying zero write\n");
256         io.writex.in.file.fnum = fnum;
257         io.writex.in.offset = 0;
258         io.writex.in.wmode = 0;
259         io.writex.in.remaining = 0;
260         io.writex.in.count = 0;
261         io.writex.in.data = buf;
262         status = smb_raw_write(cli->tree, &io);
263         CHECK_STATUS(status, NT_STATUS_OK);
264         CHECK_VALUE(io.writex.out.nwritten, 0);
265
266         setup_buffer(buf, seed, maxsize);
267
268         printf("Trying small write\n");
269         io.writex.in.count = 9;
270         io.writex.in.offset = 4;
271         io.writex.in.data = buf;
272         status = smb_raw_write(cli->tree, &io);
273         CHECK_STATUS(status, NT_STATUS_OK);
274         CHECK_VALUE(io.writex.out.nwritten, io.writex.in.count);
275
276         memset(buf, 0, maxsize);
277         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
278                 printf("read failed at %s\n", __location__);
279                 ret = False;
280                 goto done;
281         }
282         CHECK_BUFFER(buf+4, seed, 9);
283         CHECK_VALUE(IVAL(buf,0), 0);
284
285         setup_buffer(buf, seed, maxsize);
286
287         printf("Trying large write\n");
288         io.writex.in.count = 4000;
289         io.writex.in.offset = 0;
290         io.writex.in.data = buf;
291         status = smb_raw_write(cli->tree, &io);
292         CHECK_STATUS(status, NT_STATUS_OK);
293         CHECK_VALUE(io.writex.out.nwritten, 4000);
294
295         memset(buf, 0, maxsize);
296         if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
297                 printf("read failed at %s\n", __location__);
298                 ret = False;
299                 goto done;
300         }
301         CHECK_BUFFER(buf, seed, 4000);
302
303         printf("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         printf("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         printf("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                 printf("Failed to lock file at %s\n", __location__);
331                 ret = False;
332                 goto done;
333         }
334         cli->session->pid--;
335         io.writex.in.wmode = 0;
336         io.writex.in.count = 4;
337         io.writex.in.offset = 0;
338         status = smb_raw_write(cli->tree, &io);
339         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
340
341         printf("Setting file as sparse\n");
342         status = torture_set_sparse(cli->tree, fnum);
343         CHECK_STATUS(status, NT_STATUS_OK);
344         
345         printf("Trying 2^32 offset\n");
346         setup_buffer(buf, seed, maxsize);
347         io.writex.in.file.fnum = fnum;
348         io.writex.in.count = 4000;
349         io.writex.in.offset = 0xFFFFFFFF - 2000;
350         io.writex.in.data = buf;
351         status = smb_raw_write(cli->tree, &io);
352         CHECK_STATUS(status, NT_STATUS_OK);
353         CHECK_VALUE(io.writex.out.nwritten, 4000);
354         CHECK_ALL_INFO(io.writex.in.count + (uint64_t)io.writex.in.offset, size);
355
356         memset(buf, 0, maxsize);
357         if (smbcli_read(cli->tree, fnum, buf, io.writex.in.offset, 4000) != 4000) {
358                 printf("read failed at %s\n", __location__);
359                 ret = False;
360                 goto done;
361         }
362         CHECK_BUFFER(buf, seed, 4000);
363
364         for (i=33;i<max_bits;i++) {
365                 printf("Trying 2^%d offset\n", i);
366                 setup_buffer(buf, seed+1, maxsize);
367                 io.writex.in.file.fnum = fnum;
368                 io.writex.in.count = 4000;
369                 io.writex.in.offset = ((uint64_t)1) << i;
370                 io.writex.in.data = buf;
371                 status = smb_raw_write(cli->tree, &io);
372                 if (i>33 &&
373                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
374                         break;
375                 }
376                 CHECK_STATUS(status, NT_STATUS_OK);
377                 CHECK_VALUE(io.writex.out.nwritten, 4000);
378                 CHECK_ALL_INFO(io.writex.in.count + (uint64_t)io.writex.in.offset, size);
379
380                 memset(buf, 0, maxsize);
381                 if (smbcli_read(cli->tree, fnum, buf, io.writex.in.offset, 4000) != 4000) {
382                         printf("read failed at %s\n", __location__);
383                         ret = False;
384                         goto done;
385                 }
386                 CHECK_BUFFER(buf, seed+1, 4000);
387         }
388         printf("limit is 2^%d\n", i);
389
390         setup_buffer(buf, seed, maxsize);
391
392 done:
393         smbcli_close(cli->tree, fnum);
394         smb_raw_exit(cli->session);
395         smbcli_deltree(cli->tree, BASEDIR);
396         return ret;
397 }
398
399
400 /*
401   test write unlock ops
402 */
403 static BOOL test_writeunlock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
404 {
405         union smb_write io;
406         NTSTATUS status;
407         BOOL ret = True;
408         int fnum;
409         uint8_t *buf;
410         const int maxsize = 90000;
411         const char *fname = BASEDIR "\\test.txt";
412         uint_t seed = time(NULL);
413         union smb_fileinfo finfo;
414
415         buf = talloc_zero_size(mem_ctx, maxsize);
416
417         if (!torture_setup_dir(cli, BASEDIR)) {
418                 return False;
419         }
420
421         printf("Testing RAW_WRITE_WRITEUNLOCK\n");
422         io.generic.level = RAW_WRITE_WRITEUNLOCK;
423         
424         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
425         if (fnum == -1) {
426                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
427                 ret = False;
428                 goto done;
429         }
430
431         printf("Trying zero write\n");
432         io.writeunlock.in.file.fnum = fnum;
433         io.writeunlock.in.count = 0;
434         io.writeunlock.in.offset = 0;
435         io.writeunlock.in.remaining = 0;
436         io.writeunlock.in.data = buf;
437         status = smb_raw_write(cli->tree, &io);
438         CHECK_STATUS(status, NT_STATUS_OK);
439         CHECK_VALUE(io.writeunlock.out.nwritten, io.writeunlock.in.count);
440
441         setup_buffer(buf, seed, maxsize);
442
443         printf("Trying small write\n");
444         io.writeunlock.in.count = 9;
445         io.writeunlock.in.offset = 4;
446         io.writeunlock.in.data = buf;
447         status = smb_raw_write(cli->tree, &io);
448         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
449         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
450                 printf("read failed at %s\n", __location__);
451                 ret = False;
452                 goto done;
453         }
454         CHECK_BUFFER(buf+4, seed, 9);
455         CHECK_VALUE(IVAL(buf,0), 0);
456
457         setup_buffer(buf, seed, maxsize);
458         smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count, 
459                  0, WRITE_LOCK);
460         status = smb_raw_write(cli->tree, &io);
461         CHECK_STATUS(status, NT_STATUS_OK);
462         CHECK_VALUE(io.writeunlock.out.nwritten, io.writeunlock.in.count);
463
464         memset(buf, 0, maxsize);
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
475         printf("Trying large write\n");
476         io.writeunlock.in.count = 4000;
477         io.writeunlock.in.offset = 0;
478         io.writeunlock.in.data = buf;
479         smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count, 
480                  0, WRITE_LOCK);
481         status = smb_raw_write(cli->tree, &io);
482         CHECK_STATUS(status, NT_STATUS_OK);
483         CHECK_VALUE(io.writeunlock.out.nwritten, 4000);
484
485         status = smb_raw_write(cli->tree, &io);
486         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
487
488         memset(buf, 0, maxsize);
489         if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
490                 printf("read failed at %s\n", __location__);
491                 ret = False;
492                 goto done;
493         }
494         CHECK_BUFFER(buf, seed, 4000);
495
496         printf("Trying bad fnum\n");
497         io.writeunlock.in.file.fnum = fnum+1;
498         io.writeunlock.in.count = 4000;
499         io.writeunlock.in.offset = 0;
500         io.writeunlock.in.data = buf;
501         status = smb_raw_write(cli->tree, &io);
502         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
503
504         printf("Setting file as sparse\n");
505         status = torture_set_sparse(cli->tree, fnum);
506         CHECK_STATUS(status, NT_STATUS_OK);
507         
508         printf("Trying 2^32 offset\n");
509         setup_buffer(buf, seed, maxsize);
510         io.writeunlock.in.file.fnum = fnum;
511         io.writeunlock.in.count = 4000;
512         io.writeunlock.in.offset = 0xFFFFFFFF - 2000;
513         io.writeunlock.in.data = buf;
514         smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count, 
515                  0, WRITE_LOCK);
516         status = smb_raw_write(cli->tree, &io);
517         CHECK_STATUS(status, NT_STATUS_OK);
518         CHECK_VALUE(io.writeunlock.out.nwritten, 4000);
519         CHECK_ALL_INFO(io.writeunlock.in.count + (uint64_t)io.writeunlock.in.offset, size);
520
521         memset(buf, 0, maxsize);
522         if (smbcli_read(cli->tree, fnum, buf, io.writeunlock.in.offset, 4000) != 4000) {
523                 printf("read failed at %s\n", __location__);
524                 ret = False;
525                 goto done;
526         }
527         CHECK_BUFFER(buf, seed, 4000);
528
529 done:
530         smbcli_close(cli->tree, fnum);
531         smb_raw_exit(cli->session);
532         smbcli_deltree(cli->tree, BASEDIR);
533         return ret;
534 }
535
536
537 /*
538   test write close ops
539 */
540 static BOOL test_writeclose(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
541 {
542         union smb_write io;
543         NTSTATUS status;
544         BOOL ret = True;
545         int fnum;
546         uint8_t *buf;
547         const int maxsize = 90000;
548         const char *fname = BASEDIR "\\test.txt";
549         uint_t seed = time(NULL);
550         union smb_fileinfo finfo;
551
552         buf = talloc_zero_size(mem_ctx, maxsize);
553
554         if (!torture_setup_dir(cli, BASEDIR)) {
555                 return False;
556         }
557
558         printf("Testing RAW_WRITE_WRITECLOSE\n");
559         io.generic.level = RAW_WRITE_WRITECLOSE;
560         
561         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
562         if (fnum == -1) {
563                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
564                 ret = False;
565                 goto done;
566         }
567
568         printf("Trying zero write\n");
569         io.writeclose.in.file.fnum = fnum;
570         io.writeclose.in.count = 0;
571         io.writeclose.in.offset = 0;
572         io.writeclose.in.mtime = 0;
573         io.writeclose.in.data = buf;
574         status = smb_raw_write(cli->tree, &io);
575         CHECK_STATUS(status, NT_STATUS_OK);
576         CHECK_VALUE(io.writeclose.out.nwritten, io.writeclose.in.count);
577
578         status = smb_raw_write(cli->tree, &io);
579         CHECK_STATUS(status, NT_STATUS_OK);
580         CHECK_VALUE(io.writeclose.out.nwritten, io.writeclose.in.count);
581
582         setup_buffer(buf, seed, maxsize);
583
584         printf("Trying small write\n");
585         io.writeclose.in.count = 9;
586         io.writeclose.in.offset = 4;
587         io.writeclose.in.data = buf;
588         status = smb_raw_write(cli->tree, &io);
589         CHECK_STATUS(status, NT_STATUS_OK);
590
591         status = smb_raw_write(cli->tree, &io);
592         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
593
594         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
595         io.writeclose.in.file.fnum = fnum;
596
597         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
598                 printf("read failed at %s\n", __location__);
599                 ret = False;
600                 goto done;
601         }
602         CHECK_BUFFER(buf+4, seed, 9);
603         CHECK_VALUE(IVAL(buf,0), 0);
604
605         setup_buffer(buf, seed, maxsize);
606         status = smb_raw_write(cli->tree, &io);
607         CHECK_STATUS(status, NT_STATUS_OK);
608         CHECK_VALUE(io.writeclose.out.nwritten, io.writeclose.in.count);
609
610         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
611         io.writeclose.in.file.fnum = fnum;
612
613         memset(buf, 0, maxsize);
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
624         printf("Trying large write\n");
625         io.writeclose.in.count = 4000;
626         io.writeclose.in.offset = 0;
627         io.writeclose.in.data = buf;
628         status = smb_raw_write(cli->tree, &io);
629         CHECK_STATUS(status, NT_STATUS_OK);
630         CHECK_VALUE(io.writeclose.out.nwritten, 4000);
631
632         status = smb_raw_write(cli->tree, &io);
633         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
634
635         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
636         io.writeclose.in.file.fnum = fnum;
637
638         memset(buf, 0, maxsize);
639         if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
640                 printf("read failed at %s\n", __location__);
641                 ret = False;
642                 goto done;
643         }
644         CHECK_BUFFER(buf, seed, 4000);
645
646         printf("Trying bad fnum\n");
647         io.writeclose.in.file.fnum = fnum+1;
648         io.writeclose.in.count = 4000;
649         io.writeclose.in.offset = 0;
650         io.writeclose.in.data = buf;
651         status = smb_raw_write(cli->tree, &io);
652         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
653
654         printf("Setting file as sparse\n");
655         status = torture_set_sparse(cli->tree, fnum);
656         CHECK_STATUS(status, NT_STATUS_OK);
657         
658         printf("Trying 2^32 offset\n");
659         setup_buffer(buf, seed, maxsize);
660         io.writeclose.in.file.fnum = fnum;
661         io.writeclose.in.count = 4000;
662         io.writeclose.in.offset = 0xFFFFFFFF - 2000;
663         io.writeclose.in.data = buf;
664         status = smb_raw_write(cli->tree, &io);
665         CHECK_STATUS(status, NT_STATUS_OK);
666         CHECK_VALUE(io.writeclose.out.nwritten, 4000);
667         CHECK_ALL_INFO(io.writeclose.in.count + (uint64_t)io.writeclose.in.offset, size);
668
669         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
670         io.writeclose.in.file.fnum = fnum;
671
672         memset(buf, 0, maxsize);
673         if (smbcli_read(cli->tree, fnum, buf, io.writeclose.in.offset, 4000) != 4000) {
674                 printf("read failed at %s\n", __location__);
675                 ret = False;
676                 goto done;
677         }
678         CHECK_BUFFER(buf, seed, 4000);
679
680 done:
681         smbcli_close(cli->tree, fnum);
682         smb_raw_exit(cli->session);
683         smbcli_deltree(cli->tree, BASEDIR);
684         return ret;
685 }
686
687 /* 
688    basic testing of write calls
689 */
690 BOOL torture_raw_write(struct torture_context *torture)
691 {
692         struct smbcli_state *cli;
693         BOOL ret = True;
694         TALLOC_CTX *mem_ctx;
695
696         if (!torture_open_connection(&cli, 0)) {
697                 return False;
698         }
699
700         mem_ctx = talloc_init("torture_raw_write");
701
702         ret &= test_write(cli, mem_ctx);
703         ret &= test_writeunlock(cli, mem_ctx);
704         ret &= test_writeclose(cli, mem_ctx);
705         ret &= test_writex(cli, mem_ctx);
706
707         torture_close_connection(cli);
708         talloc_free(mem_ctx);
709         return ret;
710 }