s4:torture/smb2: we only support 64KB reads/writes for now
[kai/samba-autobuild/.git] / source4 / torture / smb2 / read.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 read test suite
5
6    Copyright (C) Andrew Tridgell 2008
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25
26 #include "torture/torture.h"
27 #include "torture/smb2/proto.h"
28
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=%u - should be %u\n", \
41                        __location__, #v, (unsigned)v, (unsigned)correct); \
42                 ret = false; \
43                 goto done; \
44         }} while (0)
45
46 #define FNAME "smb2_readtest.dat"
47 #define DNAME "smb2_readtest.dir"
48
49 static bool test_read_eof(struct torture_context *torture, struct smb2_tree *tree)
50 {
51         bool ret = true;
52         NTSTATUS status;
53         struct smb2_handle h;
54         uint8_t buf[64*1024];
55         struct smb2_read rd;
56         TALLOC_CTX *tmp_ctx = talloc_new(tree);
57
58         ZERO_STRUCT(buf);
59
60         smb2_util_unlink(tree, FNAME);
61
62         status = torture_smb2_testfile(tree, FNAME, &h);
63         CHECK_STATUS(status, NT_STATUS_OK);
64
65         ZERO_STRUCT(rd);
66         rd.in.file.handle = h;
67         rd.in.length      = 5;
68         rd.in.offset      = 0;
69         status = smb2_read(tree, tree, &rd);
70         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
71
72         status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
73         CHECK_STATUS(status, NT_STATUS_OK);
74
75         ZERO_STRUCT(rd);
76         rd.in.file.handle = h;
77         rd.in.length = 10;
78         rd.in.offset = 0;
79         rd.in.min_count = 1;
80
81         status = smb2_read(tree, tmp_ctx, &rd);
82         CHECK_STATUS(status, NT_STATUS_OK);
83         CHECK_VALUE(rd.out.data.length, 10);
84
85         rd.in.min_count = 0;
86         rd.in.length = 10;
87         rd.in.offset = sizeof(buf);
88         status = smb2_read(tree, tmp_ctx, &rd);
89         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
90
91         rd.in.min_count = 0;
92         rd.in.length = 0;
93         rd.in.offset = sizeof(buf);
94         status = smb2_read(tree, tmp_ctx, &rd);
95         CHECK_STATUS(status, NT_STATUS_OK);
96         CHECK_VALUE(rd.out.data.length, 0);
97
98         rd.in.min_count = 1;
99         rd.in.length = 0;
100         rd.in.offset = sizeof(buf);
101         status = smb2_read(tree, tmp_ctx, &rd);
102         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
103
104         rd.in.min_count = 0;
105         rd.in.length = 2;
106         rd.in.offset = sizeof(buf) - 1;
107         status = smb2_read(tree, tmp_ctx, &rd);
108         CHECK_STATUS(status, NT_STATUS_OK);
109         CHECK_VALUE(rd.out.data.length, 1);
110
111         rd.in.min_count = 2;
112         rd.in.length = 1;
113         rd.in.offset = sizeof(buf) - 1;
114         status = smb2_read(tree, tmp_ctx, &rd);
115         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
116
117         rd.in.min_count = 0x10000;
118         rd.in.length = 1;
119         rd.in.offset = 0;
120         status = smb2_read(tree, tmp_ctx, &rd);
121         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
122
123         rd.in.min_count = 0x10000 - 2;
124         rd.in.length = 1;
125         rd.in.offset = 0;
126         status = smb2_read(tree, tmp_ctx, &rd);
127         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
128
129         rd.in.min_count = 10;
130         rd.in.length = 5;
131         rd.in.offset = 0;
132         status = smb2_read(tree, tmp_ctx, &rd);
133         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
134
135 done:
136         talloc_free(tmp_ctx);
137         return ret;
138 }
139
140
141 static bool test_read_position(struct torture_context *torture, struct smb2_tree *tree)
142 {
143         bool ret = true;
144         NTSTATUS status;
145         struct smb2_handle h;
146         uint8_t buf[64*1024];
147         struct smb2_read rd;
148         TALLOC_CTX *tmp_ctx = talloc_new(tree);
149         union smb_fileinfo info;
150
151         ZERO_STRUCT(buf);
152
153         status = torture_smb2_testfile(tree, FNAME, &h);
154         CHECK_STATUS(status, NT_STATUS_OK);
155
156         status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
157         CHECK_STATUS(status, NT_STATUS_OK);
158
159         ZERO_STRUCT(rd);
160         rd.in.file.handle = h;
161         rd.in.length = 10;
162         rd.in.offset = 0;
163         rd.in.min_count = 1;
164
165         status = smb2_read(tree, tmp_ctx, &rd);
166         CHECK_STATUS(status, NT_STATUS_OK);
167         CHECK_VALUE(rd.out.data.length, 10);
168
169         info.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
170         info.generic.in.file.handle = h;
171
172         status = smb2_getinfo_file(tree, tmp_ctx, &info);
173         CHECK_STATUS(status, NT_STATUS_OK);
174         if (torture_setting_bool(torture, "windows", false)) {
175                 CHECK_VALUE(info.all_info2.out.position, 0);
176         } else {
177                 CHECK_VALUE(info.all_info2.out.position, 10);
178         }
179
180         
181 done:
182         talloc_free(tmp_ctx);
183         return ret;
184 }
185
186 static bool test_read_dir(struct torture_context *torture, struct smb2_tree *tree)
187 {
188         bool ret = true;
189         NTSTATUS status;
190         struct smb2_handle h;
191         struct smb2_read rd;
192         TALLOC_CTX *tmp_ctx = talloc_new(tree);
193
194         status = torture_smb2_testdir(tree, DNAME, &h);
195         if (!NT_STATUS_IS_OK(status)) {
196                 printf(__location__ " Unable to create test directory '%s' - %s\n", DNAME, nt_errstr(status));
197                 return false;
198         }
199
200         ZERO_STRUCT(rd);
201         rd.in.file.handle = h;
202         rd.in.length = 10;
203         rd.in.offset = 0;
204         rd.in.min_count = 1;
205
206         status = smb2_read(tree, tmp_ctx, &rd);
207         CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
208         
209         rd.in.min_count = 11;
210         status = smb2_read(tree, tmp_ctx, &rd);
211         CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
212
213         rd.in.length = 0;
214         rd.in.min_count = 2592;
215         status = smb2_read(tree, tmp_ctx, &rd);
216         if (torture_setting_bool(torture, "windows", false)) {
217                 CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
218         } else {
219                 CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
220         }
221
222         rd.in.length = 0;
223         rd.in.min_count = 0;
224         rd.in.channel = 0;
225         status = smb2_read(tree, tmp_ctx, &rd);
226         if (torture_setting_bool(torture, "windows", false)) {
227                 CHECK_STATUS(status, NT_STATUS_OK);
228         } else {
229                 CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
230         }
231         
232 done:
233         talloc_free(tmp_ctx);
234         return ret;
235 }
236
237
238 /* 
239    basic testing of SMB2 read
240 */
241 struct torture_suite *torture_smb2_read_init(void)
242 {
243         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "read");
244
245         torture_suite_add_1smb2_test(suite, "eof", test_read_eof);
246         torture_suite_add_1smb2_test(suite, "position", test_read_position);
247         torture_suite_add_1smb2_test(suite, "dir", test_read_dir);
248
249         suite->description = talloc_strdup(suite, "SMB2-READ tests");
250
251         return suite;
252 }
253