- cope with servers that don't properly implement SMBexit
[samba.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         cli_close(cli, fnum);
196         smb_raw_exit(cli->session);
197         cli_deltree(cli, BASEDIR);
198         return ret;
199 }
200
201
202 /*
203   test lockread ops
204 */
205 static BOOL test_lockread(struct cli_state *cli, TALLOC_CTX *mem_ctx)
206 {
207         union smb_read io;
208         NTSTATUS status;
209         BOOL ret = True;
210         int fnum;
211         char *buf;
212         const int maxsize = 90000;
213         const char *fname = BASEDIR "\\test.txt";
214         const char *test_data = "TEST DATA";
215         unsigned seed = time(NULL);
216
217         buf = talloc_zero(mem_ctx, maxsize);
218
219         if (cli_deltree(cli, BASEDIR) == -1 ||
220             !cli_mkdir(cli, BASEDIR)) {
221                 printf("Unable to setup %s - %s\n", BASEDIR, cli_errstr(cli));
222                 return False;
223         }
224
225         printf("Testing RAW_READ_LOCKREAD\n");
226         io.generic.level = RAW_READ_LOCKREAD;
227         
228         fnum = cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE);
229         if (fnum == -1) {
230                 printf("Failed to create %s - %s\n", fname, cli_errstr(cli));
231                 ret = False;
232                 goto done;
233         }
234
235         printf("Trying empty file read\n");
236         io.lockread.in.fnum = fnum;
237         io.lockread.in.count = 1;
238         io.lockread.in.offset = 0;
239         io.lockread.in.remaining = 0;
240         io.lockread.out.data = buf;
241         status = smb_raw_read(cli->tree, &io);
242
243         CHECK_STATUS(status, NT_STATUS_OK);
244         CHECK_VALUE(io.lockread.out.nread, 0);
245
246         printf("Trying zero file read\n");
247         io.lockread.in.count = 0;
248         status = smb_raw_read(cli->tree, &io);
249         CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
250
251         printf("Trying bad fnum\n");
252         io.lockread.in.fnum = fnum+1;
253         status = smb_raw_read(cli->tree, &io);
254         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
255         io.lockread.in.fnum = fnum;
256
257         cli_write(cli, fnum, 0, test_data, 0, strlen(test_data));
258
259         printf("Trying small read\n");
260         io.lockread.in.fnum = fnum;
261         io.lockread.in.offset = 0;
262         io.lockread.in.remaining = 0;
263         io.lockread.in.count = strlen(test_data);
264         status = smb_raw_read(cli->tree, &io);
265         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
266
267         cli_unlock(cli, fnum, 0, 1);
268
269         status = smb_raw_read(cli->tree, &io);
270         CHECK_STATUS(status, NT_STATUS_OK);
271         CHECK_VALUE(io.lockread.out.nread, strlen(test_data));
272         if (memcmp(buf, test_data, strlen(test_data)) != 0) {
273                 ret = False;
274                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data, buf);
275                 goto done;
276         }
277
278         printf("Trying short read\n");
279         io.lockread.in.offset = 1;
280         io.lockread.in.count = strlen(test_data);
281         status = smb_raw_read(cli->tree, &io);
282         CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
283         cli_unlock(cli, fnum, 0, strlen(test_data));
284         status = smb_raw_read(cli->tree, &io);
285         CHECK_STATUS(status, NT_STATUS_OK);
286
287         CHECK_VALUE(io.lockread.out.nread, strlen(test_data)-1);
288         if (memcmp(buf, test_data+1, strlen(test_data)-1) != 0) {
289                 ret = False;
290                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data+1, buf);
291                 goto done;
292         }
293
294         printf("Trying max offset\n");
295         io.lockread.in.offset = ~0;
296         io.lockread.in.count = strlen(test_data);
297         status = smb_raw_read(cli->tree, &io);
298         CHECK_STATUS(status, NT_STATUS_OK);
299         CHECK_VALUE(io.lockread.out.nread, 0);
300
301         setup_buffer(buf, seed, maxsize);
302         cli_write(cli, fnum, 0, buf, 0, maxsize);
303         memset(buf, 0, maxsize);
304
305         printf("Trying large read\n");
306         io.lockread.in.offset = 0;
307         io.lockread.in.count = ~0;
308         status = smb_raw_read(cli->tree, &io);
309         CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
310         cli_unlock(cli, fnum, 1, strlen(test_data));
311         status = smb_raw_read(cli->tree, &io);
312         CHECK_STATUS(status, NT_STATUS_OK);
313         CHECK_BUFFER(buf, seed, io.lockread.out.nread);
314         cli_unlock(cli, fnum, 0, 0xFFFF);
315
316
317         printf("Trying locked region\n");
318         cli->session->pid++;
319         if (!cli_lock(cli, fnum, 103, 1, 0, WRITE_LOCK)) {
320                 printf("Failed to lock file at %d\n", __LINE__);
321                 ret = False;
322                 goto done;
323         }
324         cli->session->pid--;
325         memset(buf, 0, maxsize);
326         io.lockread.in.offset = 0;
327         io.lockread.in.count = ~0;
328         status = smb_raw_read(cli->tree, &io);
329         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
330         
331
332 done:
333         cli_close(cli, fnum);
334         cli_deltree(cli, BASEDIR);
335         return ret;
336 }
337
338
339 /*
340   test readx ops
341 */
342 static BOOL test_readx(struct cli_state *cli, TALLOC_CTX *mem_ctx)
343 {
344         union smb_read io;
345         NTSTATUS status;
346         BOOL ret = True;
347         int fnum;
348         char *buf;
349         const int maxsize = 90000;
350         const char *fname = BASEDIR "\\test.txt";
351         const char *test_data = "TEST DATA";
352         unsigned seed = time(NULL);
353
354         buf = talloc_zero(mem_ctx, maxsize);
355
356         if (cli_deltree(cli, BASEDIR) == -1 ||
357             !cli_mkdir(cli, BASEDIR)) {
358                 printf("Unable to setup %s - %s\n", BASEDIR, cli_errstr(cli));
359                 return False;
360         }
361
362         printf("Testing RAW_READ_READX\n");
363         
364         fnum = cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE);
365         if (fnum == -1) {
366                 printf("Failed to create %s - %s\n", fname, cli_errstr(cli));
367                 ret = False;
368                 goto done;
369         }
370
371         printf("Trying empty file read\n");
372         io.generic.level = RAW_READ_READX;
373         io.readx.in.fnum = fnum;
374         io.readx.in.mincnt = 1;
375         io.readx.in.maxcnt = 1;
376         io.readx.in.offset = 0;
377         io.readx.in.remaining = 0;
378         io.readx.out.data = buf;
379         status = smb_raw_read(cli->tree, &io);
380
381         CHECK_STATUS(status, NT_STATUS_OK);
382         CHECK_VALUE(io.readx.out.nread, 0);
383         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
384         CHECK_VALUE(io.readx.out.compaction_mode, 0);
385
386         printf("Trying zero file read\n");
387         io.readx.in.mincnt = 0;
388         io.readx.in.maxcnt = 0;
389         status = smb_raw_read(cli->tree, &io);
390         CHECK_STATUS(status, NT_STATUS_OK);
391         CHECK_VALUE(io.readx.out.nread, 0);
392         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
393         CHECK_VALUE(io.readx.out.compaction_mode, 0);
394
395         printf("Trying bad fnum\n");
396         io.readx.in.fnum = fnum+1;
397         status = smb_raw_read(cli->tree, &io);
398         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
399         io.readx.in.fnum = fnum;
400
401         cli_write(cli, fnum, 0, test_data, 0, strlen(test_data));
402
403         printf("Trying small read\n");
404         io.readx.in.fnum = fnum;
405         io.readx.in.offset = 0;
406         io.readx.in.remaining = 0;
407         io.readx.in.mincnt = strlen(test_data);
408         io.readx.in.maxcnt = strlen(test_data);
409         status = smb_raw_read(cli->tree, &io);
410         CHECK_STATUS(status, NT_STATUS_OK);
411         CHECK_VALUE(io.readx.out.nread, strlen(test_data));
412         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
413         CHECK_VALUE(io.readx.out.compaction_mode, 0);
414         if (memcmp(buf, test_data, strlen(test_data)) != 0) {
415                 ret = False;
416                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data, buf);
417                 goto done;
418         }
419
420         printf("Trying short read\n");
421         io.readx.in.offset = 1;
422         io.readx.in.mincnt = strlen(test_data);
423         io.readx.in.maxcnt = strlen(test_data);
424         status = smb_raw_read(cli->tree, &io);
425         CHECK_STATUS(status, NT_STATUS_OK);
426         CHECK_VALUE(io.readx.out.nread, strlen(test_data)-1);
427         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
428         CHECK_VALUE(io.readx.out.compaction_mode, 0);
429         if (memcmp(buf, test_data+1, strlen(test_data)-1) != 0) {
430                 ret = False;
431                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data+1, buf);
432                 goto done;
433         }
434
435         printf("Trying max offset\n");
436         io.readx.in.offset = 0xffffffff;
437         io.readx.in.mincnt = strlen(test_data);
438         io.readx.in.maxcnt = strlen(test_data);
439         status = smb_raw_read(cli->tree, &io);
440         CHECK_STATUS(status, NT_STATUS_OK);
441         CHECK_VALUE(io.readx.out.nread, 0);
442         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
443         CHECK_VALUE(io.readx.out.compaction_mode, 0);
444
445         setup_buffer(buf, seed, maxsize);
446         cli_write(cli, fnum, 0, buf, 0, maxsize);
447         memset(buf, 0, maxsize);
448
449         printf("Trying large read\n");
450         io.readx.in.offset = 0;
451         io.readx.in.mincnt = ~0;
452         io.readx.in.maxcnt = ~0;
453         status = smb_raw_read(cli->tree, &io);
454         CHECK_STATUS(status, NT_STATUS_OK);
455         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
456         CHECK_VALUE(io.readx.out.compaction_mode, 0);
457         CHECK_VALUE(io.readx.out.nread, io.readx.in.maxcnt);
458         CHECK_BUFFER(buf, seed, io.readx.out.nread);
459
460         printf("Trying mincnt > maxcnt\n");
461         memset(buf, 0, maxsize);
462         io.readx.in.offset = 0;
463         io.readx.in.mincnt = 30000;
464         io.readx.in.maxcnt = 20000;
465         status = smb_raw_read(cli->tree, &io);
466         CHECK_STATUS(status, NT_STATUS_OK);
467         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
468         CHECK_VALUE(io.readx.out.compaction_mode, 0);
469         CHECK_VALUE(io.readx.out.nread, io.readx.in.maxcnt);
470         CHECK_BUFFER(buf, seed, io.readx.out.nread);
471
472         printf("Trying mincnt < maxcnt\n");
473         memset(buf, 0, maxsize);
474         io.readx.in.offset = 0;
475         io.readx.in.mincnt = 20000;
476         io.readx.in.maxcnt = 30000;
477         status = smb_raw_read(cli->tree, &io);
478         CHECK_STATUS(status, NT_STATUS_OK);
479         CHECK_VALUE(io.readx.out.remaining, 0xFFFF);
480         CHECK_VALUE(io.readx.out.compaction_mode, 0);
481         CHECK_VALUE(io.readx.out.nread, io.readx.in.maxcnt);
482         CHECK_BUFFER(buf, seed, io.readx.out.nread);
483
484         printf("Trying locked region\n");
485         cli->session->pid++;
486         if (!cli_lock(cli, fnum, 103, 1, 0, WRITE_LOCK)) {
487                 printf("Failed to lock file at %d\n", __LINE__);
488                 ret = False;
489                 goto done;
490         }
491         cli->session->pid--;
492         memset(buf, 0, maxsize);
493         io.readx.in.offset = 0;
494         io.readx.in.mincnt = 100;
495         io.readx.in.maxcnt = 200;
496         status = smb_raw_read(cli->tree, &io);
497         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);     
498
499 #ifdef LARGE_SMB_OFF_T
500         printf("Trying large offset read\n");
501         io.readx.in.offset = ((SMB_BIG_UINT)0x2) << 32;
502         io.readx.in.mincnt = 10;
503         io.readx.in.maxcnt = 10;
504         status = smb_raw_read(cli->tree, &io);
505         CHECK_STATUS(status, NT_STATUS_OK);
506         CHECK_VALUE(io.readx.out.nread, 0);
507
508         if (!cli_lock64(cli, fnum, io.readx.in.offset, 1, 0, WRITE_LOCK)) {
509                 printf("Failed to lock file at %d\n", __LINE__);
510                 ret = False;
511                 goto done;
512         }
513
514         status = smb_raw_read(cli->tree, &io);
515         CHECK_STATUS(status, NT_STATUS_OK);
516         CHECK_VALUE(io.readx.out.nread, 0);
517 #endif
518
519 done:
520         cli_close(cli, fnum);
521         cli_deltree(cli, BASEDIR);
522         return ret;
523 }
524
525
526 /*
527   test readbraw ops
528 */
529 static BOOL test_readbraw(struct cli_state *cli, TALLOC_CTX *mem_ctx)
530 {
531         union smb_read io;
532         NTSTATUS status;
533         BOOL ret = True;
534         int fnum;
535         char *buf;
536         const int maxsize = 90000;
537         const char *fname = BASEDIR "\\test.txt";
538         const char *test_data = "TEST DATA";
539         unsigned seed = time(NULL);
540
541         buf = talloc_zero(mem_ctx, maxsize);
542
543         if (cli_deltree(cli, BASEDIR) == -1 ||
544             !cli_mkdir(cli, BASEDIR)) {
545                 printf("Unable to setup %s - %s\n", BASEDIR, cli_errstr(cli));
546                 return False;
547         }
548
549         printf("Testing RAW_READ_READBRAW\n");
550         
551         fnum = cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE);
552         if (fnum == -1) {
553                 printf("Failed to create %s - %s\n", fname, cli_errstr(cli));
554                 ret = False;
555                 goto done;
556         }
557
558         printf("Trying empty file read\n");
559         io.generic.level = RAW_READ_READBRAW;
560         io.readbraw.in.fnum = fnum;
561         io.readbraw.in.mincnt = 1;
562         io.readbraw.in.maxcnt = 1;
563         io.readbraw.in.offset = 0;
564         io.readbraw.in.timeout = 0;
565         io.readbraw.out.data = buf;
566         status = smb_raw_read(cli->tree, &io);
567
568         CHECK_STATUS(status, NT_STATUS_OK);
569         CHECK_VALUE(io.readbraw.out.nread, 0);
570
571         printf("Trying zero file read\n");
572         io.readbraw.in.mincnt = 0;
573         io.readbraw.in.maxcnt = 0;
574         status = smb_raw_read(cli->tree, &io);
575         CHECK_STATUS(status, NT_STATUS_OK);
576         CHECK_VALUE(io.readbraw.out.nread, 0);
577
578         printf("Trying bad fnum\n");
579         io.readbraw.in.fnum = fnum+1;
580         status = smb_raw_read(cli->tree, &io);
581         CHECK_STATUS(status, NT_STATUS_OK);
582         CHECK_VALUE(io.readbraw.out.nread, 0);
583         io.readbraw.in.fnum = fnum;
584
585         cli_write(cli, fnum, 0, test_data, 0, strlen(test_data));
586
587         printf("Trying small read\n");
588         io.readbraw.in.fnum = fnum;
589         io.readbraw.in.offset = 0;
590         io.readbraw.in.mincnt = strlen(test_data);
591         io.readbraw.in.maxcnt = strlen(test_data);
592         status = smb_raw_read(cli->tree, &io);
593         CHECK_STATUS(status, NT_STATUS_OK);
594         CHECK_VALUE(io.readbraw.out.nread, strlen(test_data));
595         if (memcmp(buf, test_data, strlen(test_data)) != 0) {
596                 ret = False;
597                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data, buf);
598                 goto done;
599         }
600
601         printf("Trying short read\n");
602         io.readbraw.in.offset = 1;
603         io.readbraw.in.mincnt = strlen(test_data);
604         io.readbraw.in.maxcnt = strlen(test_data);
605         status = smb_raw_read(cli->tree, &io);
606         CHECK_STATUS(status, NT_STATUS_OK);
607         CHECK_VALUE(io.readbraw.out.nread, strlen(test_data)-1);
608         if (memcmp(buf, test_data+1, strlen(test_data)-1) != 0) {
609                 ret = False;
610                 printf("incorrect data at %d!? (%s:%s)\n", __LINE__, test_data+1, buf);
611                 goto done;
612         }
613
614         printf("Trying max offset\n");
615         io.readbraw.in.offset = ~0;
616         io.readbraw.in.mincnt = strlen(test_data);
617         io.readbraw.in.maxcnt = strlen(test_data);
618         status = smb_raw_read(cli->tree, &io);
619         CHECK_STATUS(status, NT_STATUS_OK);
620         CHECK_VALUE(io.readbraw.out.nread, 0);
621
622         setup_buffer(buf, seed, maxsize);
623         cli_write(cli, fnum, 0, buf, 0, maxsize);
624         memset(buf, 0, maxsize);
625
626         printf("Trying large read\n");
627         io.readbraw.in.offset = 0;
628         io.readbraw.in.mincnt = ~0;
629         io.readbraw.in.maxcnt = ~0;
630         status = smb_raw_read(cli->tree, &io);
631         CHECK_STATUS(status, NT_STATUS_OK);
632         CHECK_VALUE(io.readbraw.out.nread, 0xFFFF);
633         CHECK_BUFFER(buf, seed, io.readbraw.out.nread);
634
635         printf("Trying mincnt > maxcnt\n");
636         memset(buf, 0, maxsize);
637         io.readbraw.in.offset = 0;
638         io.readbraw.in.mincnt = 30000;
639         io.readbraw.in.maxcnt = 20000;
640         status = smb_raw_read(cli->tree, &io);
641         CHECK_STATUS(status, NT_STATUS_OK);
642         CHECK_VALUE(io.readbraw.out.nread, io.readbraw.in.maxcnt);
643         CHECK_BUFFER(buf, seed, io.readbraw.out.nread);
644
645         printf("Trying mincnt < maxcnt\n");
646         memset(buf, 0, maxsize);
647         io.readbraw.in.offset = 0;
648         io.readbraw.in.mincnt = 20000;
649         io.readbraw.in.maxcnt = 30000;
650         status = smb_raw_read(cli->tree, &io);
651         CHECK_STATUS(status, NT_STATUS_OK);
652         CHECK_VALUE(io.readbraw.out.nread, io.readbraw.in.maxcnt);
653         CHECK_BUFFER(buf, seed, io.readbraw.out.nread);
654
655         printf("Trying locked region\n");
656         cli->session->pid++;
657         if (!cli_lock(cli, fnum, 103, 1, 0, WRITE_LOCK)) {
658                 printf("Failed to lock file at %d\n", __LINE__);
659                 ret = False;
660                 goto done;
661         }
662         cli->session->pid--;
663         memset(buf, 0, maxsize);
664         io.readbraw.in.offset = 0;
665         io.readbraw.in.mincnt = 100;
666         io.readbraw.in.maxcnt = 200;
667         status = smb_raw_read(cli->tree, &io);
668         CHECK_STATUS(status, NT_STATUS_OK);
669         CHECK_VALUE(io.readbraw.out.nread, 0);
670
671         printf("Trying locked region with timeout\n");
672         memset(buf, 0, maxsize);
673         io.readbraw.in.offset = 0;
674         io.readbraw.in.mincnt = 100;
675         io.readbraw.in.maxcnt = 200;
676         io.readbraw.in.timeout = 10000;
677         status = smb_raw_read(cli->tree, &io);
678         CHECK_STATUS(status, NT_STATUS_OK);
679         CHECK_VALUE(io.readbraw.out.nread, 0);
680
681 #ifdef LARGE_SMB_OFF_T
682         printf("Trying large offset read\n");
683         io.readbraw.in.offset = ((SMB_BIG_UINT)0x2) << 32;
684         io.readbraw.in.mincnt = 10;
685         io.readbraw.in.maxcnt = 10;
686         io.readbraw.in.timeout = 0;
687         status = smb_raw_read(cli->tree, &io);
688         CHECK_STATUS(status, NT_STATUS_OK);
689         CHECK_VALUE(io.readbraw.out.nread, 0);
690 #endif
691
692 done:
693         cli_close(cli, fnum);
694         cli_deltree(cli, BASEDIR);
695         return ret;
696 }
697
698
699 /* 
700    basic testing of read calls
701 */
702 BOOL torture_raw_read(int dummy)
703 {
704         struct cli_state *cli;
705         BOOL ret = True;
706         TALLOC_CTX *mem_ctx;
707
708         if (!torture_open_connection(&cli)) {
709                 return False;
710         }
711
712         mem_ctx = talloc_init("torture_raw_read");
713
714         if (!test_read(cli, mem_ctx)) {
715                 ret = False;
716         }
717
718         if (!test_readx(cli, mem_ctx)) {
719                 ret = False;
720         }
721
722         if (!test_lockread(cli, mem_ctx)) {
723                 ret = False;
724         }
725
726         if (!test_readbraw(cli, mem_ctx)) {
727                 ret = False;
728         }
729
730         torture_close_connection(cli);
731         talloc_destroy(mem_ctx);
732         return ret;
733 }