This commit was generated by cvs2svn to compensate for changes in r30,
[kai/samba-autobuild/.git] / source4 / torture / raw / read.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for various read operations
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #define CHECK_STATUS(status, correct) do { \
24         if (!NT_STATUS_EQUAL(status, correct)) { \
25                 printf("(%d) Incorrect status %s - should be %s\n", \
26                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
27                 ret = False; \
28                 goto done; \
29         }} while (0)
30
31 #define CHECK_VALUE(v, correct) do { \
32         if ((v) != (correct)) { \
33                 printf("(%d) Incorrect value %s=%d - should be %d\n", \
34                        __LINE__, #v, v, correct); \
35                 ret = False; \
36                 goto done; \
37         }} while (0)
38
39 #define CHECK_BUFFER(buf, seed, len) do { \
40         if (!check_buffer(buf, seed, len, __LINE__)) { \
41                 ret = False; \
42                 goto done; \
43         }} while (0)
44
45 #define BASEDIR "\\testread"
46
47
48 /*
49   setup a random buffer based on a seed
50 */
51 static void setup_buffer(char *buf, unsigned seed, int len)
52 {
53         int i;
54         srandom(seed);
55         for (i=0;i<len;i++) buf[i] = random();
56 }
57
58 /*
59   check a random buffer based on a seed
60 */
61 static BOOL check_buffer(char *buf, unsigned seed, int len, int line)
62 {
63         int i;
64         srandom(seed);
65         for (i=0;i<len;i++) {
66                 char v = random();
67                 if (buf[i] != v) {
68                         printf("Buffer incorrect at line %d! ofs=%d v1=0x%x v2=0x%x\n", 
69                                line, i, buf[i], v);
70                         return False;
71                 }
72         }
73         return True;
74 }
75
76 /*
77   test read ops
78 */
79 static BOOL test_read(struct cli_state *cli, TALLOC_CTX *mem_ctx)
80 {
81         union smb_read io;
82         NTSTATUS status;
83         BOOL ret = True;
84         int fnum;
85         char *buf;
86         const int maxsize = 90000;
87         const char *fname = BASEDIR "\\test.txt";
88         const char *test_data = "TEST DATA";
89         unsigned seed = time(NULL);
90
91         buf = talloc_zero(mem_ctx, maxsize);
92
93         if (cli_deltree(cli, BASEDIR) == -1 ||
94             !cli_mkdir(cli, BASEDIR)) {
95                 printf("Unable to setup %s - %s\n", BASEDIR, cli_errstr(cli));
96                 return False;
97         }
98
99         printf("Testing RAW_READ_READ\n");
100         io.generic.level = RAW_READ_READ;
101         
102         fnum = cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE);
103         if (fnum == -1) {
104                 printf("Failed to create %s - %s\n", fname, cli_errstr(cli));
105                 ret = False;
106                 goto done;
107         }
108
109         printf("Trying empty file read\n");
110         io.read.in.fnum = fnum;
111         io.read.in.count = 1;
112         io.read.in.offset = 0;
113         io.read.in.remaining = 0;
114         io.read.out.data = buf;
115         status = smb_raw_read(cli->tree, &io);
116
117         CHECK_STATUS(status, NT_STATUS_OK);
118         CHECK_VALUE(io.read.out.nread, 0);
119
120         printf("Trying zero file read\n");
121         io.read.in.count = 0;
122         status = smb_raw_read(cli->tree, &io);
123         CHECK_STATUS(status, NT_STATUS_OK);
124         CHECK_VALUE(io.read.out.nread, 0);
125
126         printf("Trying bad fnum\n");
127         io.read.in.fnum = fnum+1;
128         status = smb_raw_read(cli->tree, &io);
129         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
130         io.read.in.fnum = fnum;
131
132         cli_write(cli, fnum, 0, test_data, 0, strlen(test_data));
133
134         printf("Trying small read\n");
135         io.read.in.fnum = fnum;
136         io.read.in.offset = 0;
137         io.read.in.remaining = 0;
138         io.read.in.count = strlen(test_data);
139         status = smb_raw_read(cli->tree, &io);
140         CHECK_STATUS(status, NT_STATUS_OK);
141         CHECK_VALUE(io.read.out.nread, strlen(test_data));
142         if (memcmp(buf, test_data, strlen(test_data)) != 0) {
143                 ret = False;
144                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data, buf);
145                 goto done;
146         }
147
148         printf("Trying short read\n");
149         io.read.in.offset = 1;
150         io.read.in.count = strlen(test_data);
151         status = smb_raw_read(cli->tree, &io);
152         CHECK_STATUS(status, NT_STATUS_OK);
153         CHECK_VALUE(io.read.out.nread, strlen(test_data)-1);
154         if (memcmp(buf, test_data+1, strlen(test_data)-1) != 0) {
155                 ret = False;
156                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data+1, buf);
157                 goto done;
158         }
159
160         printf("Trying max offset\n");
161         io.read.in.offset = ~0;
162         io.read.in.count = strlen(test_data);
163         status = smb_raw_read(cli->tree, &io);
164         CHECK_STATUS(status, NT_STATUS_OK);
165         CHECK_VALUE(io.read.out.nread, 0);
166
167         setup_buffer(buf, seed, maxsize);
168         cli_write(cli, fnum, 0, buf, 0, maxsize);
169         memset(buf, 0, maxsize);
170
171         printf("Trying large read\n");
172         io.read.in.offset = 0;
173         io.read.in.count = ~0;
174         status = smb_raw_read(cli->tree, &io);
175         CHECK_STATUS(status, NT_STATUS_OK);
176         CHECK_BUFFER(buf, seed, io.read.out.nread);
177
178
179         printf("Trying locked region\n");
180         cli->session->pid++;
181         if (!cli_lock(cli, fnum, 103, 1, 0, WRITE_LOCK)) {
182                 printf("Failed to lock file at %d\n", __LINE__);
183                 ret = False;
184                 goto done;
185         }
186         cli->session->pid--;
187         memset(buf, 0, maxsize);
188         io.read.in.offset = 0;
189         io.read.in.count = ~0;
190         status = smb_raw_read(cli->tree, &io);
191         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
192         
193
194 done:
195         smb_raw_exit(cli->session);
196         cli_deltree(cli, BASEDIR);
197         return ret;
198 }
199
200
201 /*
202   test lockread ops
203 */
204 static BOOL test_lockread(struct cli_state *cli, TALLOC_CTX *mem_ctx)
205 {
206         union smb_read io;
207         NTSTATUS status;
208         BOOL ret = True;
209         int fnum;
210         char *buf;
211         const int maxsize = 90000;
212         const char *fname = BASEDIR "\\test.txt";
213         const char *test_data = "TEST DATA";
214         unsigned seed = time(NULL);
215
216         buf = talloc_zero(mem_ctx, maxsize);
217
218         if (cli_deltree(cli, BASEDIR) == -1 ||
219             !cli_mkdir(cli, BASEDIR)) {
220                 printf("Unable to setup %s - %s\n", BASEDIR, cli_errstr(cli));
221                 return False;
222         }
223
224         printf("Testing RAW_READ_LOCKREAD\n");
225         io.generic.level = RAW_READ_LOCKREAD;
226         
227         fnum = cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE);
228         if (fnum == -1) {
229                 printf("Failed to create %s - %s\n", fname, cli_errstr(cli));
230                 ret = False;
231                 goto done;
232         }
233
234         printf("Trying empty file read\n");
235         io.lockread.in.fnum = fnum;
236         io.lockread.in.count = 1;
237         io.lockread.in.offset = 0;
238         io.lockread.in.remaining = 0;
239         io.lockread.out.data = buf;
240         status = smb_raw_read(cli->tree, &io);
241
242         CHECK_STATUS(status, NT_STATUS_OK);
243         CHECK_VALUE(io.lockread.out.nread, 0);
244
245         printf("Trying zero file read\n");
246         io.lockread.in.count = 0;
247         status = smb_raw_read(cli->tree, &io);
248         CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
249
250         printf("Trying bad fnum\n");
251         io.lockread.in.fnum = fnum+1;
252         status = smb_raw_read(cli->tree, &io);
253         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
254         io.lockread.in.fnum = fnum;
255
256         cli_write(cli, fnum, 0, test_data, 0, strlen(test_data));
257
258         printf("Trying small read\n");
259         io.lockread.in.fnum = fnum;
260         io.lockread.in.offset = 0;
261         io.lockread.in.remaining = 0;
262         io.lockread.in.count = strlen(test_data);
263         status = smb_raw_read(cli->tree, &io);
264         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
265
266         cli_unlock(cli, fnum, 0, 1);
267
268         status = smb_raw_read(cli->tree, &io);
269         CHECK_STATUS(status, NT_STATUS_OK);
270         CHECK_VALUE(io.lockread.out.nread, strlen(test_data));
271         if (memcmp(buf, test_data, strlen(test_data)) != 0) {
272                 ret = False;
273                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data, buf);
274                 goto done;
275         }
276
277         printf("Trying short read\n");
278         io.lockread.in.offset = 1;
279         io.lockread.in.count = strlen(test_data);
280         status = smb_raw_read(cli->tree, &io);
281         CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
282         cli_unlock(cli, fnum, 0, strlen(test_data));
283         status = smb_raw_read(cli->tree, &io);
284         CHECK_STATUS(status, NT_STATUS_OK);
285
286         CHECK_VALUE(io.lockread.out.nread, strlen(test_data)-1);
287         if (memcmp(buf, test_data+1, strlen(test_data)-1) != 0) {
288                 ret = False;
289                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data+1, buf);
290                 goto done;
291         }
292
293         printf("Trying max offset\n");
294         io.lockread.in.offset = ~0;
295         io.lockread.in.count = strlen(test_data);
296         status = smb_raw_read(cli->tree, &io);
297         CHECK_STATUS(status, NT_STATUS_OK);
298         CHECK_VALUE(io.lockread.out.nread, 0);
299
300         setup_buffer(buf, seed, maxsize);
301         cli_write(cli, fnum, 0, buf, 0, maxsize);
302         memset(buf, 0, maxsize);
303
304         printf("Trying large read\n");
305         io.lockread.in.offset = 0;
306         io.lockread.in.count = ~0;
307         status = smb_raw_read(cli->tree, &io);
308         CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
309         cli_unlock(cli, fnum, 1, strlen(test_data));
310         status = smb_raw_read(cli->tree, &io);
311         CHECK_STATUS(status, NT_STATUS_OK);
312         CHECK_BUFFER(buf, seed, io.lockread.out.nread);
313         cli_unlock(cli, fnum, 0, 0xFFFF);
314
315
316         printf("Trying locked region\n");
317         cli->session->pid++;
318         if (!cli_lock(cli, fnum, 103, 1, 0, WRITE_LOCK)) {
319                 printf("Failed to lock file at %d\n", __LINE__);
320                 ret = False;
321                 goto done;
322         }
323         cli->session->pid--;
324         memset(buf, 0, maxsize);
325         io.lockread.in.offset = 0;
326         io.lockread.in.count = ~0;
327         status = smb_raw_read(cli->tree, &io);
328         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
329         
330
331 done:
332         cli_close(cli, fnum);
333         cli_deltree(cli, BASEDIR);
334         return ret;
335 }
336
337
338 /*
339   test readx ops
340 */
341 static BOOL test_readx(struct cli_state *cli, TALLOC_CTX *mem_ctx)
342 {
343         union smb_read io;
344         NTSTATUS status;
345         BOOL ret = True;
346         int fnum;
347         char *buf;
348         const int maxsize = 90000;
349         const char *fname = BASEDIR "\\test.txt";
350         const char *test_data = "TEST DATA";
351         unsigned seed = time(NULL);
352
353         buf = talloc_zero(mem_ctx, maxsize);
354
355         if (cli_deltree(cli, BASEDIR) == -1 ||
356             !cli_mkdir(cli, BASEDIR)) {
357                 printf("Unable to setup %s - %s\n", BASEDIR, cli_errstr(cli));
358                 return False;
359         }
360
361         printf("Testing RAW_READ_READX\n");
362         
363         fnum = cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE);
364         if (fnum == -1) {
365                 printf("Failed to create %s - %s\n", fname, cli_errstr(cli));
366                 ret = False;
367                 goto done;
368         }
369
370         printf("Trying empty file read\n");
371         io.generic.level = RAW_READ_READX;
372         io.readx.in.fnum = fnum;
373         io.readx.in.mincnt = 1;
374         io.readx.in.maxcnt = 1;
375         io.readx.in.offset = 0;
376         io.readx.in.remaining = 0;
377         io.readx.out.data = buf;
378         status = smb_raw_read(cli->tree, &io);
379
380         CHECK_STATUS(status, NT_STATUS_OK);
381         CHECK_VALUE(io.readx.out.nread, 0);
382         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
383         CHECK_VALUE(io.readx.out.compaction_mode, 0);
384
385         printf("Trying zero file read\n");
386         io.readx.in.mincnt = 0;
387         io.readx.in.maxcnt = 0;
388         status = smb_raw_read(cli->tree, &io);
389         CHECK_STATUS(status, NT_STATUS_OK);
390         CHECK_VALUE(io.readx.out.nread, 0);
391         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
392         CHECK_VALUE(io.readx.out.compaction_mode, 0);
393
394         printf("Trying bad fnum\n");
395         io.readx.in.fnum = fnum+1;
396         status = smb_raw_read(cli->tree, &io);
397         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
398         io.readx.in.fnum = fnum;
399
400         cli_write(cli, fnum, 0, test_data, 0, strlen(test_data));
401
402         printf("Trying small read\n");
403         io.readx.in.fnum = fnum;
404         io.readx.in.offset = 0;
405         io.readx.in.remaining = 0;
406         io.readx.in.mincnt = strlen(test_data);
407         io.readx.in.maxcnt = strlen(test_data);
408         status = smb_raw_read(cli->tree, &io);
409         CHECK_STATUS(status, NT_STATUS_OK);
410         CHECK_VALUE(io.readx.out.nread, strlen(test_data));
411         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
412         CHECK_VALUE(io.readx.out.compaction_mode, 0);
413         if (memcmp(buf, test_data, strlen(test_data)) != 0) {
414                 ret = False;
415                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data, buf);
416                 goto done;
417         }
418
419         printf("Trying short read\n");
420         io.readx.in.offset = 1;
421         io.readx.in.mincnt = strlen(test_data);
422         io.readx.in.maxcnt = strlen(test_data);
423         status = smb_raw_read(cli->tree, &io);
424         CHECK_STATUS(status, NT_STATUS_OK);
425         CHECK_VALUE(io.readx.out.nread, strlen(test_data)-1);
426         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
427         CHECK_VALUE(io.readx.out.compaction_mode, 0);
428         if (memcmp(buf, test_data+1, strlen(test_data)-1) != 0) {
429                 ret = False;
430                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data+1, buf);
431                 goto done;
432         }
433
434         printf("Trying max offset\n");
435         io.readx.in.offset = 0xffffffff;
436         io.readx.in.mincnt = strlen(test_data);
437         io.readx.in.maxcnt = strlen(test_data);
438         status = smb_raw_read(cli->tree, &io);
439         CHECK_STATUS(status, NT_STATUS_OK);
440         CHECK_VALUE(io.readx.out.nread, 0);
441         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
442         CHECK_VALUE(io.readx.out.compaction_mode, 0);
443
444         setup_buffer(buf, seed, maxsize);
445         cli_write(cli, fnum, 0, buf, 0, maxsize);
446         memset(buf, 0, maxsize);
447
448         printf("Trying large read\n");
449         io.readx.in.offset = 0;
450         io.readx.in.mincnt = ~0;
451         io.readx.in.maxcnt = ~0;
452         status = smb_raw_read(cli->tree, &io);
453         CHECK_STATUS(status, NT_STATUS_OK);
454         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
455         CHECK_VALUE(io.readx.out.compaction_mode, 0);
456         CHECK_VALUE(io.readx.out.nread, io.readx.in.maxcnt);
457         CHECK_BUFFER(buf, seed, io.readx.out.nread);
458
459         printf("Trying mincnt > maxcnt\n");
460         memset(buf, 0, maxsize);
461         io.readx.in.offset = 0;
462         io.readx.in.mincnt = 30000;
463         io.readx.in.maxcnt = 20000;
464         status = smb_raw_read(cli->tree, &io);
465         CHECK_STATUS(status, NT_STATUS_OK);
466         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
467         CHECK_VALUE(io.readx.out.compaction_mode, 0);
468         CHECK_VALUE(io.readx.out.nread, io.readx.in.maxcnt);
469         CHECK_BUFFER(buf, seed, io.readx.out.nread);
470
471         printf("Trying mincnt < maxcnt\n");
472         memset(buf, 0, maxsize);
473         io.readx.in.offset = 0;
474         io.readx.in.mincnt = 20000;
475         io.readx.in.maxcnt = 30000;
476         status = smb_raw_read(cli->tree, &io);
477         CHECK_STATUS(status, NT_STATUS_OK);
478         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
479         CHECK_VALUE(io.readx.out.compaction_mode, 0);
480         CHECK_VALUE(io.readx.out.nread, io.readx.in.maxcnt);
481         CHECK_BUFFER(buf, seed, io.readx.out.nread);
482
483         printf("Trying locked region\n");
484         cli->session->pid++;
485         if (!cli_lock(cli, fnum, 103, 1, 0, WRITE_LOCK)) {
486                 printf("Failed to lock file at %d\n", __LINE__);
487                 ret = False;
488                 goto done;
489         }
490         cli->session->pid--;
491         memset(buf, 0, maxsize);
492         io.readx.in.offset = 0;
493         io.readx.in.mincnt = 100;
494         io.readx.in.maxcnt = 200;
495         status = smb_raw_read(cli->tree, &io);
496         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);     
497
498 #ifdef LARGE_SMB_OFF_T
499         printf("Trying large offset read\n");
500         io.readx.in.offset = ((SMB_BIG_UINT)0x2) << 32;
501         io.readx.in.mincnt = 10;
502         io.readx.in.maxcnt = 10;
503         status = smb_raw_read(cli->tree, &io);
504         CHECK_STATUS(status, NT_STATUS_OK);
505         CHECK_VALUE(io.readx.out.nread, 0);
506
507         if (!cli_lock64(cli, fnum, io.readx.in.offset, 1, 0, WRITE_LOCK)) {
508                 printf("Failed to lock file at %d\n", __LINE__);
509                 ret = False;
510                 goto done;
511         }
512
513         status = smb_raw_read(cli->tree, &io);
514         CHECK_STATUS(status, NT_STATUS_OK);
515         CHECK_VALUE(io.readx.out.nread, 0);
516 #endif
517
518 done:
519         cli_close(cli, fnum);
520         cli_deltree(cli, BASEDIR);
521         return ret;
522 }
523
524
525 /*
526   test readbraw ops
527 */
528 static BOOL test_readbraw(struct cli_state *cli, TALLOC_CTX *mem_ctx)
529 {
530         union smb_read io;
531         NTSTATUS status;
532         BOOL ret = True;
533         int fnum;
534         char *buf;
535         const int maxsize = 90000;
536         const char *fname = BASEDIR "\\test.txt";
537         const char *test_data = "TEST DATA";
538         unsigned seed = time(NULL);
539
540         buf = talloc_zero(mem_ctx, maxsize);
541
542         if (cli_deltree(cli, BASEDIR) == -1 ||
543             !cli_mkdir(cli, BASEDIR)) {
544                 printf("Unable to setup %s - %s\n", BASEDIR, cli_errstr(cli));
545                 return False;
546         }
547
548         printf("Testing RAW_READ_READBRAW\n");
549         
550         fnum = cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE);
551         if (fnum == -1) {
552                 printf("Failed to create %s - %s\n", fname, cli_errstr(cli));
553                 ret = False;
554                 goto done;
555         }
556
557         printf("Trying empty file read\n");
558         io.generic.level = RAW_READ_READBRAW;
559         io.readbraw.in.fnum = fnum;
560         io.readbraw.in.mincnt = 1;
561         io.readbraw.in.maxcnt = 1;
562         io.readbraw.in.offset = 0;
563         io.readbraw.in.timeout = 0;
564         io.readbraw.out.data = buf;
565         status = smb_raw_read(cli->tree, &io);
566
567         CHECK_STATUS(status, NT_STATUS_OK);
568         CHECK_VALUE(io.readbraw.out.nread, 0);
569
570         printf("Trying zero file read\n");
571         io.readbraw.in.mincnt = 0;
572         io.readbraw.in.maxcnt = 0;
573         status = smb_raw_read(cli->tree, &io);
574         CHECK_STATUS(status, NT_STATUS_OK);
575         CHECK_VALUE(io.readbraw.out.nread, 0);
576
577         printf("Trying bad fnum\n");
578         io.readbraw.in.fnum = fnum+1;
579         status = smb_raw_read(cli->tree, &io);
580         CHECK_STATUS(status, NT_STATUS_OK);
581         CHECK_VALUE(io.readbraw.out.nread, 0);
582         io.readbraw.in.fnum = fnum;
583
584         cli_write(cli, fnum, 0, test_data, 0, strlen(test_data));
585
586         printf("Trying small read\n");
587         io.readbraw.in.fnum = fnum;
588         io.readbraw.in.offset = 0;
589         io.readbraw.in.mincnt = strlen(test_data);
590         io.readbraw.in.maxcnt = strlen(test_data);
591         status = smb_raw_read(cli->tree, &io);
592         CHECK_STATUS(status, NT_STATUS_OK);
593         CHECK_VALUE(io.readbraw.out.nread, strlen(test_data));
594         if (memcmp(buf, test_data, strlen(test_data)) != 0) {
595                 ret = False;
596                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data, buf);
597                 goto done;
598         }
599
600         printf("Trying short read\n");
601         io.readbraw.in.offset = 1;
602         io.readbraw.in.mincnt = strlen(test_data);
603         io.readbraw.in.maxcnt = strlen(test_data);
604         status = smb_raw_read(cli->tree, &io);
605         CHECK_STATUS(status, NT_STATUS_OK);
606         CHECK_VALUE(io.readbraw.out.nread, strlen(test_data)-1);
607         if (memcmp(buf, test_data+1, strlen(test_data)-1) != 0) {
608                 ret = False;
609                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data+1, buf);
610                 goto done;
611         }
612
613         printf("Trying max offset\n");
614         io.readbraw.in.offset = ~0;
615         io.readbraw.in.mincnt = strlen(test_data);
616         io.readbraw.in.maxcnt = strlen(test_data);
617         status = smb_raw_read(cli->tree, &io);
618         CHECK_STATUS(status, NT_STATUS_OK);
619         CHECK_VALUE(io.readbraw.out.nread, 0);
620
621         setup_buffer(buf, seed, maxsize);
622         cli_write(cli, fnum, 0, buf, 0, maxsize);
623         memset(buf, 0, maxsize);
624
625         printf("Trying large read\n");
626         io.readbraw.in.offset = 0;
627         io.readbraw.in.mincnt = ~0;
628         io.readbraw.in.maxcnt = ~0;
629         status = smb_raw_read(cli->tree, &io);
630         CHECK_STATUS(status, NT_STATUS_OK);
631         CHECK_VALUE(io.readbraw.out.nread, 0xFFFF);
632         CHECK_BUFFER(buf, seed, io.readbraw.out.nread);
633
634         printf("Trying mincnt > maxcnt\n");
635         memset(buf, 0, maxsize);
636         io.readbraw.in.offset = 0;
637         io.readbraw.in.mincnt = 30000;
638         io.readbraw.in.maxcnt = 20000;
639         status = smb_raw_read(cli->tree, &io);
640         CHECK_STATUS(status, NT_STATUS_OK);
641         CHECK_VALUE(io.readbraw.out.nread, io.readbraw.in.maxcnt);
642         CHECK_BUFFER(buf, seed, io.readbraw.out.nread);
643
644         printf("Trying mincnt < maxcnt\n");
645         memset(buf, 0, maxsize);
646         io.readbraw.in.offset = 0;
647         io.readbraw.in.mincnt = 20000;
648         io.readbraw.in.maxcnt = 30000;
649         status = smb_raw_read(cli->tree, &io);
650         CHECK_STATUS(status, NT_STATUS_OK);
651         CHECK_VALUE(io.readbraw.out.nread, io.readbraw.in.maxcnt);
652         CHECK_BUFFER(buf, seed, io.readbraw.out.nread);
653
654         printf("Trying locked region\n");
655         cli->session->pid++;
656         if (!cli_lock(cli, fnum, 103, 1, 0, WRITE_LOCK)) {
657                 printf("Failed to lock file at %d\n", __LINE__);
658                 ret = False;
659                 goto done;
660         }
661         cli->session->pid--;
662         memset(buf, 0, maxsize);
663         io.readbraw.in.offset = 0;
664         io.readbraw.in.mincnt = 100;
665         io.readbraw.in.maxcnt = 200;
666         status = smb_raw_read(cli->tree, &io);
667         CHECK_STATUS(status, NT_STATUS_OK);
668         CHECK_VALUE(io.readbraw.out.nread, 0);
669
670         printf("Trying locked region with timeout\n");
671         memset(buf, 0, maxsize);
672         io.readbraw.in.offset = 0;
673         io.readbraw.in.mincnt = 100;
674         io.readbraw.in.maxcnt = 200;
675         io.readbraw.in.timeout = 10000;
676         status = smb_raw_read(cli->tree, &io);
677         CHECK_STATUS(status, NT_STATUS_OK);
678         CHECK_VALUE(io.readbraw.out.nread, 0);
679
680 #ifdef LARGE_SMB_OFF_T
681         printf("Trying large offset read\n");
682         io.readbraw.in.offset = ((SMB_BIG_UINT)0x2) << 32;
683         io.readbraw.in.mincnt = 10;
684         io.readbraw.in.maxcnt = 10;
685         io.readbraw.in.timeout = 0;
686         status = smb_raw_read(cli->tree, &io);
687         CHECK_STATUS(status, NT_STATUS_OK);
688         CHECK_VALUE(io.readbraw.out.nread, 0);
689 #endif
690
691 done:
692         cli_close(cli, fnum);
693         cli_deltree(cli, BASEDIR);
694         return ret;
695 }
696
697
698 /* 
699    basic testing of read calls
700 */
701 BOOL torture_raw_read(int dummy)
702 {
703         struct cli_state *cli;
704         BOOL ret = True;
705         TALLOC_CTX *mem_ctx;
706
707         if (!torture_open_connection(&cli)) {
708                 return False;
709         }
710
711         mem_ctx = talloc_init("torture_raw_read");
712
713         if (!test_read(cli, mem_ctx)) {
714                 ret = False;
715         }
716
717         if (!test_readx(cli, mem_ctx)) {
718                 ret = False;
719         }
720
721         if (!test_lockread(cli, mem_ctx)) {
722                 ret = False;
723         }
724
725         if (!test_readbraw(cli, mem_ctx)) {
726                 ret = False;
727         }
728
729         torture_close_connection(cli);
730         talloc_destroy(mem_ctx);
731         return ret;
732 }