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