r16907: Add an index parameter to torture_open_connection. Next step is to enable the
[gd/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 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         printf("Trying 2^32 offset\n");
187         setup_buffer(buf, seed, maxsize);
188         io.write.in.file.fnum = fnum;
189         io.write.in.count = 4000;
190         io.write.in.offset = 0xFFFFFFFF - 2000;
191         io.write.in.data = buf;
192         status = smb_raw_write(cli->tree, &io);
193         CHECK_STATUS(status, NT_STATUS_OK);
194         CHECK_VALUE(io.write.out.nwritten, 4000);
195         CHECK_ALL_INFO(io.write.in.count + (uint64_t)io.write.in.offset, size);
196
197         memset(buf, 0, maxsize);
198         if (smbcli_read(cli->tree, fnum, buf, io.write.in.offset, 4000) != 4000) {
199                 printf("read failed at %s\n", __location__);
200                 ret = False;
201                 goto done;
202         }
203         CHECK_BUFFER(buf, seed, 4000);
204
205 done:
206         smbcli_close(cli->tree, fnum);
207         smb_raw_exit(cli->session);
208         smbcli_deltree(cli->tree, BASEDIR);
209         return ret;
210 }
211
212
213 /*
214   test writex ops
215 */
216 static BOOL test_writex(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
217 {
218         union smb_write io;
219         NTSTATUS status;
220         BOOL ret = True;
221         int fnum, i;
222         uint8_t *buf;
223         const int maxsize = 90000;
224         const char *fname = BASEDIR "\\test.txt";
225         uint_t seed = time(NULL);
226         union smb_fileinfo finfo;
227
228         buf = talloc_zero_size(mem_ctx, maxsize);
229
230         if (!torture_setup_dir(cli, BASEDIR)) {
231                 return False;
232         }
233
234         printf("Testing RAW_WRITE_WRITEX\n");
235         io.generic.level = RAW_WRITE_WRITEX;
236         
237         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
238         if (fnum == -1) {
239                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
240                 ret = False;
241                 goto done;
242         }
243
244         printf("Trying zero write\n");
245         io.writex.in.file.fnum = fnum;
246         io.writex.in.offset = 0;
247         io.writex.in.wmode = 0;
248         io.writex.in.remaining = 0;
249         io.writex.in.count = 0;
250         io.writex.in.data = buf;
251         status = smb_raw_write(cli->tree, &io);
252         CHECK_STATUS(status, NT_STATUS_OK);
253         CHECK_VALUE(io.writex.out.nwritten, 0);
254
255         setup_buffer(buf, seed, maxsize);
256
257         printf("Trying small write\n");
258         io.writex.in.count = 9;
259         io.writex.in.offset = 4;
260         io.writex.in.data = buf;
261         status = smb_raw_write(cli->tree, &io);
262         CHECK_STATUS(status, NT_STATUS_OK);
263         CHECK_VALUE(io.writex.out.nwritten, io.writex.in.count);
264
265         memset(buf, 0, maxsize);
266         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
267                 printf("read failed at %s\n", __location__);
268                 ret = False;
269                 goto done;
270         }
271         CHECK_BUFFER(buf+4, seed, 9);
272         CHECK_VALUE(IVAL(buf,0), 0);
273
274         setup_buffer(buf, seed, maxsize);
275
276         printf("Trying large write\n");
277         io.writex.in.count = 4000;
278         io.writex.in.offset = 0;
279         io.writex.in.data = buf;
280         status = smb_raw_write(cli->tree, &io);
281         CHECK_STATUS(status, NT_STATUS_OK);
282         CHECK_VALUE(io.writex.out.nwritten, 4000);
283
284         memset(buf, 0, maxsize);
285         if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
286                 printf("read failed at %s\n", __location__);
287                 ret = False;
288                 goto done;
289         }
290         CHECK_BUFFER(buf, seed, 4000);
291
292         printf("Trying bad fnum\n");
293         io.writex.in.file.fnum = fnum+1;
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_INVALID_HANDLE);
299
300         printf("Testing wmode\n");
301         io.writex.in.file.fnum = fnum;
302         io.writex.in.count = 1;
303         io.writex.in.offset = 0;
304         io.writex.in.wmode = 1;
305         io.writex.in.data = buf;
306         status = smb_raw_write(cli->tree, &io);
307         CHECK_STATUS(status, NT_STATUS_OK);
308         CHECK_VALUE(io.writex.out.nwritten, io.writex.in.count);
309
310         io.writex.in.wmode = 2;
311         status = smb_raw_write(cli->tree, &io);
312         CHECK_STATUS(status, NT_STATUS_OK);
313         CHECK_VALUE(io.writex.out.nwritten, io.writex.in.count);
314
315
316         printf("Trying locked region\n");
317         cli->session->pid++;
318         if (NT_STATUS_IS_ERR(smbcli_lock(cli->tree, fnum, 3, 1, 0, WRITE_LOCK))) {
319                 printf("Failed to lock file at %s\n", __location__);
320                 ret = False;
321                 goto done;
322         }
323         cli->session->pid--;
324         io.writex.in.wmode = 0;
325         io.writex.in.count = 4;
326         io.writex.in.offset = 0;
327         status = smb_raw_write(cli->tree, &io);
328         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
329
330         printf("Setting file as sparse\n");
331         status = torture_set_sparse(cli->tree, fnum);
332         CHECK_STATUS(status, NT_STATUS_OK);
333         
334         printf("Trying 2^32 offset\n");
335         setup_buffer(buf, seed, maxsize);
336         io.writex.in.file.fnum = fnum;
337         io.writex.in.count = 4000;
338         io.writex.in.offset = 0xFFFFFFFF - 2000;
339         io.writex.in.data = buf;
340         status = smb_raw_write(cli->tree, &io);
341         CHECK_STATUS(status, NT_STATUS_OK);
342         CHECK_VALUE(io.writex.out.nwritten, 4000);
343         CHECK_ALL_INFO(io.writex.in.count + (uint64_t)io.writex.in.offset, size);
344
345         memset(buf, 0, maxsize);
346         if (smbcli_read(cli->tree, fnum, buf, io.writex.in.offset, 4000) != 4000) {
347                 printf("read failed at %s\n", __location__);
348                 ret = False;
349                 goto done;
350         }
351         CHECK_BUFFER(buf, seed, 4000);
352
353         for (i=33;i<64;i++) {
354                 printf("Trying 2^%d offset\n", i);
355                 setup_buffer(buf, seed+1, maxsize);
356                 io.writex.in.file.fnum = fnum;
357                 io.writex.in.count = 4000;
358                 io.writex.in.offset = ((uint64_t)1) << i;
359                 io.writex.in.data = buf;
360                 status = smb_raw_write(cli->tree, &io);
361                 if (i>33 &&
362                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
363                         break;
364                 }
365                 CHECK_STATUS(status, NT_STATUS_OK);
366                 CHECK_VALUE(io.writex.out.nwritten, 4000);
367                 CHECK_ALL_INFO(io.writex.in.count + (uint64_t)io.writex.in.offset, size);
368
369                 memset(buf, 0, maxsize);
370                 if (smbcli_read(cli->tree, fnum, buf, io.writex.in.offset, 4000) != 4000) {
371                         printf("read failed at %s\n", __location__);
372                         ret = False;
373                         goto done;
374                 }
375                 CHECK_BUFFER(buf, seed+1, 4000);
376         }
377         printf("limit is 2^%d\n", i);
378
379         setup_buffer(buf, seed, maxsize);
380
381 done:
382         smbcli_close(cli->tree, fnum);
383         smb_raw_exit(cli->session);
384         smbcli_deltree(cli->tree, BASEDIR);
385         return ret;
386 }
387
388
389 /*
390   test write unlock ops
391 */
392 static BOOL test_writeunlock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
393 {
394         union smb_write io;
395         NTSTATUS status;
396         BOOL ret = True;
397         int fnum;
398         uint8_t *buf;
399         const int maxsize = 90000;
400         const char *fname = BASEDIR "\\test.txt";
401         uint_t seed = time(NULL);
402         union smb_fileinfo finfo;
403
404         buf = talloc_zero_size(mem_ctx, maxsize);
405
406         if (!torture_setup_dir(cli, BASEDIR)) {
407                 return False;
408         }
409
410         printf("Testing RAW_WRITE_WRITEUNLOCK\n");
411         io.generic.level = RAW_WRITE_WRITEUNLOCK;
412         
413         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
414         if (fnum == -1) {
415                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
416                 ret = False;
417                 goto done;
418         }
419
420         printf("Trying zero write\n");
421         io.writeunlock.in.file.fnum = fnum;
422         io.writeunlock.in.count = 0;
423         io.writeunlock.in.offset = 0;
424         io.writeunlock.in.remaining = 0;
425         io.writeunlock.in.data = buf;
426         status = smb_raw_write(cli->tree, &io);
427         CHECK_STATUS(status, NT_STATUS_OK);
428         CHECK_VALUE(io.writeunlock.out.nwritten, io.writeunlock.in.count);
429
430         setup_buffer(buf, seed, maxsize);
431
432         printf("Trying small write\n");
433         io.writeunlock.in.count = 9;
434         io.writeunlock.in.offset = 4;
435         io.writeunlock.in.data = buf;
436         status = smb_raw_write(cli->tree, &io);
437         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
438         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
439                 printf("read failed at %s\n", __location__);
440                 ret = False;
441                 goto done;
442         }
443         CHECK_BUFFER(buf+4, seed, 9);
444         CHECK_VALUE(IVAL(buf,0), 0);
445
446         setup_buffer(buf, seed, maxsize);
447         smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count, 
448                  0, WRITE_LOCK);
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         memset(buf, 0, maxsize);
454         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
455                 printf("read failed at %s\n", __location__);
456                 ret = False;
457                 goto done;
458         }
459         CHECK_BUFFER(buf+4, seed, 9);
460         CHECK_VALUE(IVAL(buf,0), 0);
461
462         setup_buffer(buf, seed, maxsize);
463
464         printf("Trying large write\n");
465         io.writeunlock.in.count = 4000;
466         io.writeunlock.in.offset = 0;
467         io.writeunlock.in.data = buf;
468         smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count, 
469                  0, WRITE_LOCK);
470         status = smb_raw_write(cli->tree, &io);
471         CHECK_STATUS(status, NT_STATUS_OK);
472         CHECK_VALUE(io.writeunlock.out.nwritten, 4000);
473
474         status = smb_raw_write(cli->tree, &io);
475         CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
476
477         memset(buf, 0, maxsize);
478         if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
479                 printf("read failed at %s\n", __location__);
480                 ret = False;
481                 goto done;
482         }
483         CHECK_BUFFER(buf, seed, 4000);
484
485         printf("Trying bad fnum\n");
486         io.writeunlock.in.file.fnum = fnum+1;
487         io.writeunlock.in.count = 4000;
488         io.writeunlock.in.offset = 0;
489         io.writeunlock.in.data = buf;
490         status = smb_raw_write(cli->tree, &io);
491         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
492
493         printf("Setting file as sparse\n");
494         status = torture_set_sparse(cli->tree, fnum);
495         CHECK_STATUS(status, NT_STATUS_OK);
496         
497         printf("Trying 2^32 offset\n");
498         setup_buffer(buf, seed, maxsize);
499         io.writeunlock.in.file.fnum = fnum;
500         io.writeunlock.in.count = 4000;
501         io.writeunlock.in.offset = 0xFFFFFFFF - 2000;
502         io.writeunlock.in.data = buf;
503         smbcli_lock(cli->tree, fnum, io.writeunlock.in.offset, io.writeunlock.in.count, 
504                  0, WRITE_LOCK);
505         status = smb_raw_write(cli->tree, &io);
506         CHECK_STATUS(status, NT_STATUS_OK);
507         CHECK_VALUE(io.writeunlock.out.nwritten, 4000);
508         CHECK_ALL_INFO(io.writeunlock.in.count + (uint64_t)io.writeunlock.in.offset, size);
509
510         memset(buf, 0, maxsize);
511         if (smbcli_read(cli->tree, fnum, buf, io.writeunlock.in.offset, 4000) != 4000) {
512                 printf("read failed at %s\n", __location__);
513                 ret = False;
514                 goto done;
515         }
516         CHECK_BUFFER(buf, seed, 4000);
517
518 done:
519         smbcli_close(cli->tree, fnum);
520         smb_raw_exit(cli->session);
521         smbcli_deltree(cli->tree, BASEDIR);
522         return ret;
523 }
524
525
526 /*
527   test write close ops
528 */
529 static BOOL test_writeclose(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
530 {
531         union smb_write io;
532         NTSTATUS status;
533         BOOL ret = True;
534         int fnum;
535         uint8_t *buf;
536         const int maxsize = 90000;
537         const char *fname = BASEDIR "\\test.txt";
538         uint_t seed = time(NULL);
539         union smb_fileinfo finfo;
540
541         buf = talloc_zero_size(mem_ctx, maxsize);
542
543         if (!torture_setup_dir(cli, BASEDIR)) {
544                 return False;
545         }
546
547         printf("Testing RAW_WRITE_WRITECLOSE\n");
548         io.generic.level = RAW_WRITE_WRITECLOSE;
549         
550         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
551         if (fnum == -1) {
552                 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
553                 ret = False;
554                 goto done;
555         }
556
557         printf("Trying zero write\n");
558         io.writeclose.in.file.fnum = fnum;
559         io.writeclose.in.count = 0;
560         io.writeclose.in.offset = 0;
561         io.writeclose.in.mtime = 0;
562         io.writeclose.in.data = buf;
563         status = smb_raw_write(cli->tree, &io);
564         CHECK_STATUS(status, NT_STATUS_OK);
565         CHECK_VALUE(io.writeclose.out.nwritten, io.writeclose.in.count);
566
567         status = smb_raw_write(cli->tree, &io);
568         CHECK_STATUS(status, NT_STATUS_OK);
569         CHECK_VALUE(io.writeclose.out.nwritten, io.writeclose.in.count);
570
571         setup_buffer(buf, seed, maxsize);
572
573         printf("Trying small write\n");
574         io.writeclose.in.count = 9;
575         io.writeclose.in.offset = 4;
576         io.writeclose.in.data = buf;
577         status = smb_raw_write(cli->tree, &io);
578         CHECK_STATUS(status, NT_STATUS_OK);
579
580         status = smb_raw_write(cli->tree, &io);
581         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
582
583         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
584         io.writeclose.in.file.fnum = fnum;
585
586         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
587                 printf("read failed at %s\n", __location__);
588                 ret = False;
589                 goto done;
590         }
591         CHECK_BUFFER(buf+4, seed, 9);
592         CHECK_VALUE(IVAL(buf,0), 0);
593
594         setup_buffer(buf, seed, maxsize);
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         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
600         io.writeclose.in.file.fnum = fnum;
601
602         memset(buf, 0, maxsize);
603         if (smbcli_read(cli->tree, fnum, buf, 0, 13) != 13) {
604                 printf("read failed at %s\n", __location__);
605                 ret = False;
606                 goto done;
607         }
608         CHECK_BUFFER(buf+4, seed, 9);
609         CHECK_VALUE(IVAL(buf,0), 0);
610
611         setup_buffer(buf, seed, maxsize);
612
613         printf("Trying large write\n");
614         io.writeclose.in.count = 4000;
615         io.writeclose.in.offset = 0;
616         io.writeclose.in.data = buf;
617         status = smb_raw_write(cli->tree, &io);
618         CHECK_STATUS(status, NT_STATUS_OK);
619         CHECK_VALUE(io.writeclose.out.nwritten, 4000);
620
621         status = smb_raw_write(cli->tree, &io);
622         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
623
624         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
625         io.writeclose.in.file.fnum = fnum;
626
627         memset(buf, 0, maxsize);
628         if (smbcli_read(cli->tree, fnum, buf, 0, 4000) != 4000) {
629                 printf("read failed at %s\n", __location__);
630                 ret = False;
631                 goto done;
632         }
633         CHECK_BUFFER(buf, seed, 4000);
634
635         printf("Trying bad fnum\n");
636         io.writeclose.in.file.fnum = fnum+1;
637         io.writeclose.in.count = 4000;
638         io.writeclose.in.offset = 0;
639         io.writeclose.in.data = buf;
640         status = smb_raw_write(cli->tree, &io);
641         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
642
643         printf("Setting file as sparse\n");
644         status = torture_set_sparse(cli->tree, fnum);
645         CHECK_STATUS(status, NT_STATUS_OK);
646         
647         printf("Trying 2^32 offset\n");
648         setup_buffer(buf, seed, maxsize);
649         io.writeclose.in.file.fnum = fnum;
650         io.writeclose.in.count = 4000;
651         io.writeclose.in.offset = 0xFFFFFFFF - 2000;
652         io.writeclose.in.data = buf;
653         status = smb_raw_write(cli->tree, &io);
654         CHECK_STATUS(status, NT_STATUS_OK);
655         CHECK_VALUE(io.writeclose.out.nwritten, 4000);
656         CHECK_ALL_INFO(io.writeclose.in.count + (uint64_t)io.writeclose.in.offset, size);
657
658         fnum = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
659         io.writeclose.in.file.fnum = fnum;
660
661         memset(buf, 0, maxsize);
662         if (smbcli_read(cli->tree, fnum, buf, io.writeclose.in.offset, 4000) != 4000) {
663                 printf("read failed at %s\n", __location__);
664                 ret = False;
665                 goto done;
666         }
667         CHECK_BUFFER(buf, seed, 4000);
668
669 done:
670         smbcli_close(cli->tree, fnum);
671         smb_raw_exit(cli->session);
672         smbcli_deltree(cli->tree, BASEDIR);
673         return ret;
674 }
675
676 /* 
677    basic testing of write calls
678 */
679 BOOL torture_raw_write(struct torture_context *torture)
680 {
681         struct smbcli_state *cli;
682         BOOL ret = True;
683         TALLOC_CTX *mem_ctx;
684
685         if (!torture_open_connection(&cli, 0)) {
686                 return False;
687         }
688
689         mem_ctx = talloc_init("torture_raw_write");
690
691         ret &= test_write(cli, mem_ctx);
692         ret &= test_writeunlock(cli, mem_ctx);
693         ret &= test_writeclose(cli, mem_ctx);
694         ret &= test_writex(cli, mem_ctx);
695
696         torture_close_connection(cli);
697         talloc_free(mem_ctx);
698         return ret;
699 }