s4: torture: Add a TALLOC_CTX * to torture_smb2_read_init().
[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, _expected) \
31         torture_assert_ntstatus_equal_goto(torture, _status, _expected, \
32                  ret, done, "Incorrect status")
33
34 #define CHECK_VALUE(v, correct) \
35         torture_assert_int_equal_goto(torture, v, correct, \
36                  ret, done, "Incorrect value")
37
38 #define FNAME "smb2_readtest.dat"
39 #define DNAME "smb2_readtest.dir"
40
41 static bool test_read_eof(struct torture_context *torture, struct smb2_tree *tree)
42 {
43         bool ret = true;
44         NTSTATUS status;
45         struct smb2_handle h;
46         uint8_t buf[64*1024];
47         struct smb2_read rd;
48         TALLOC_CTX *tmp_ctx = talloc_new(tree);
49
50         ZERO_STRUCT(buf);
51
52         smb2_util_unlink(tree, FNAME);
53
54         status = torture_smb2_testfile(tree, FNAME, &h);
55         CHECK_STATUS(status, NT_STATUS_OK);
56
57         ZERO_STRUCT(rd);
58         rd.in.file.handle = h;
59         rd.in.length      = 5;
60         rd.in.offset      = 0;
61         status = smb2_read(tree, tree, &rd);
62         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
63
64         status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
65         CHECK_STATUS(status, NT_STATUS_OK);
66
67         ZERO_STRUCT(rd);
68         rd.in.file.handle = h;
69         rd.in.length = 10;
70         rd.in.offset = 0;
71         rd.in.min_count = 1;
72
73         status = smb2_read(tree, tmp_ctx, &rd);
74         CHECK_STATUS(status, NT_STATUS_OK);
75         CHECK_VALUE(rd.out.data.length, 10);
76
77         rd.in.min_count = 0;
78         rd.in.length = 10;
79         rd.in.offset = sizeof(buf);
80         status = smb2_read(tree, tmp_ctx, &rd);
81         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
82
83         rd.in.min_count = 0;
84         rd.in.length = 0;
85         rd.in.offset = sizeof(buf);
86         status = smb2_read(tree, tmp_ctx, &rd);
87         CHECK_STATUS(status, NT_STATUS_OK);
88         CHECK_VALUE(rd.out.data.length, 0);
89
90         rd.in.min_count = 1;
91         rd.in.length = 0;
92         rd.in.offset = sizeof(buf);
93         status = smb2_read(tree, tmp_ctx, &rd);
94         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
95
96         rd.in.min_count = 0;
97         rd.in.length = 2;
98         rd.in.offset = sizeof(buf) - 1;
99         status = smb2_read(tree, tmp_ctx, &rd);
100         CHECK_STATUS(status, NT_STATUS_OK);
101         CHECK_VALUE(rd.out.data.length, 1);
102
103         rd.in.min_count = 2;
104         rd.in.length = 1;
105         rd.in.offset = sizeof(buf) - 1;
106         status = smb2_read(tree, tmp_ctx, &rd);
107         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
108
109         rd.in.min_count = 0x10000;
110         rd.in.length = 1;
111         rd.in.offset = 0;
112         status = smb2_read(tree, tmp_ctx, &rd);
113         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
114
115         rd.in.min_count = 0x10000 - 2;
116         rd.in.length = 1;
117         rd.in.offset = 0;
118         status = smb2_read(tree, tmp_ctx, &rd);
119         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
120
121         rd.in.min_count = 10;
122         rd.in.length = 5;
123         rd.in.offset = 0;
124         status = smb2_read(tree, tmp_ctx, &rd);
125         CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
126
127 done:
128         talloc_free(tmp_ctx);
129         return ret;
130 }
131
132
133 static bool test_read_position(struct torture_context *torture, struct smb2_tree *tree)
134 {
135         bool ret = true;
136         NTSTATUS status;
137         struct smb2_handle h;
138         uint8_t buf[64*1024];
139         struct smb2_read rd;
140         TALLOC_CTX *tmp_ctx = talloc_new(tree);
141         union smb_fileinfo info;
142
143         ZERO_STRUCT(buf);
144
145         status = torture_smb2_testfile(tree, FNAME, &h);
146         CHECK_STATUS(status, NT_STATUS_OK);
147
148         status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
149         CHECK_STATUS(status, NT_STATUS_OK);
150
151         ZERO_STRUCT(rd);
152         rd.in.file.handle = h;
153         rd.in.length = 10;
154         rd.in.offset = 0;
155         rd.in.min_count = 1;
156
157         status = smb2_read(tree, tmp_ctx, &rd);
158         CHECK_STATUS(status, NT_STATUS_OK);
159         CHECK_VALUE(rd.out.data.length, 10);
160
161         info.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
162         info.generic.in.file.handle = h;
163
164         status = smb2_getinfo_file(tree, tmp_ctx, &info);
165         CHECK_STATUS(status, NT_STATUS_OK);
166         if (torture_setting_bool(torture, "windows", false)) {
167                 CHECK_VALUE(info.all_info2.out.position, 0);
168         } else {
169                 CHECK_VALUE(info.all_info2.out.position, 10);
170         }
171
172         
173 done:
174         talloc_free(tmp_ctx);
175         return ret;
176 }
177
178 static bool test_read_dir(struct torture_context *torture, struct smb2_tree *tree)
179 {
180         bool ret = true;
181         NTSTATUS status;
182         struct smb2_handle h;
183         struct smb2_read rd;
184         TALLOC_CTX *tmp_ctx = talloc_new(tree);
185
186         status = torture_smb2_testdir(tree, DNAME, &h);
187         if (!NT_STATUS_IS_OK(status)) {
188                 printf(__location__ " Unable to create test directory '%s' - %s\n", DNAME, nt_errstr(status));
189                 return false;
190         }
191
192         ZERO_STRUCT(rd);
193         rd.in.file.handle = h;
194         rd.in.length = 10;
195         rd.in.offset = 0;
196         rd.in.min_count = 1;
197
198         status = smb2_read(tree, tmp_ctx, &rd);
199         CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
200         
201         rd.in.min_count = 11;
202         status = smb2_read(tree, tmp_ctx, &rd);
203         CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
204
205         rd.in.length = 0;
206         rd.in.min_count = 2592;
207         status = smb2_read(tree, tmp_ctx, &rd);
208         if (torture_setting_bool(torture, "windows", false)) {
209                 CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
210         } else {
211                 CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
212         }
213
214         rd.in.length = 0;
215         rd.in.min_count = 0;
216         rd.in.channel = 0;
217         status = smb2_read(tree, tmp_ctx, &rd);
218         if (torture_setting_bool(torture, "windows", false)) {
219                 CHECK_STATUS(status, NT_STATUS_OK);
220         } else {
221                 CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
222         }
223         
224 done:
225         talloc_free(tmp_ctx);
226         return ret;
227 }
228
229 static bool test_read_access(struct torture_context *torture,
230                              struct smb2_tree *tree)
231 {
232         bool ret = true;
233         NTSTATUS status;
234         struct smb2_handle h;
235         uint8_t buf[64 * 1024];
236         struct smb2_read rd;
237         TALLOC_CTX *tmp_ctx = talloc_new(tree);
238
239         ZERO_STRUCT(buf);
240
241         /* create a file */
242         smb2_util_unlink(tree, FNAME);
243
244         status = torture_smb2_testfile(tree, FNAME, &h);
245         CHECK_STATUS(status, NT_STATUS_OK);
246
247         status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
248         CHECK_STATUS(status, NT_STATUS_OK);
249
250         status = smb2_util_close(tree, h);
251         CHECK_STATUS(status, NT_STATUS_OK);
252
253         /* open w/ READ access - success */
254         status = torture_smb2_testfile_access(
255             tree, FNAME, &h, SEC_FILE_READ_ATTRIBUTE | SEC_FILE_READ_DATA);
256         CHECK_STATUS(status, NT_STATUS_OK);
257
258         ZERO_STRUCT(rd);
259         rd.in.file.handle = h;
260         rd.in.length = 5;
261         rd.in.offset = 0;
262         status = smb2_read(tree, tree, &rd);
263         CHECK_STATUS(status, NT_STATUS_OK);
264
265         status = smb2_util_close(tree, h);
266         CHECK_STATUS(status, NT_STATUS_OK);
267
268         /* open w/ EXECUTE access - success */
269         status = torture_smb2_testfile_access(
270             tree, FNAME, &h, SEC_FILE_READ_ATTRIBUTE | SEC_FILE_EXECUTE);
271         CHECK_STATUS(status, NT_STATUS_OK);
272
273         ZERO_STRUCT(rd);
274         rd.in.file.handle = h;
275         rd.in.length = 5;
276         rd.in.offset = 0;
277         status = smb2_read(tree, tree, &rd);
278         CHECK_STATUS(status, NT_STATUS_OK);
279
280         status = smb2_util_close(tree, h);
281         CHECK_STATUS(status, NT_STATUS_OK);
282
283         /* open without READ or EXECUTE access - access denied */
284         status = torture_smb2_testfile_access(tree, FNAME, &h,
285                                               SEC_FILE_READ_ATTRIBUTE);
286         CHECK_STATUS(status, NT_STATUS_OK);
287
288         ZERO_STRUCT(rd);
289         rd.in.file.handle = h;
290         rd.in.length = 5;
291         rd.in.offset = 0;
292         status = smb2_read(tree, tree, &rd);
293         CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
294
295         status = smb2_util_close(tree, h);
296         CHECK_STATUS(status, NT_STATUS_OK);
297
298 done:
299         talloc_free(tmp_ctx);
300         return ret;
301 }
302
303 /* 
304    basic testing of SMB2 read
305 */
306 struct torture_suite *torture_smb2_read_init(TALLOC_CTX *ctx)
307 {
308         struct torture_suite *suite = torture_suite_create(ctx, "read");
309
310         torture_suite_add_1smb2_test(suite, "eof", test_read_eof);
311         torture_suite_add_1smb2_test(suite, "position", test_read_position);
312         torture_suite_add_1smb2_test(suite, "dir", test_read_dir);
313         torture_suite_add_1smb2_test(suite, "access", test_read_access);
314
315         suite->description = talloc_strdup(suite, "SMB2-READ tests");
316
317         return suite;
318 }
319