2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2008
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.
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.
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/>.
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
26 #include "torture/torture.h"
27 #include "torture/smb2/proto.h"
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)); \
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); \
46 #define FNAME "smb2_readtest.dat"
47 #define DNAME "smb2_readtest.dir"
49 static bool test_read_eof(struct torture_context *torture, struct smb2_tree *tree)
56 TALLOC_CTX *tmp_ctx = talloc_new(tree);
60 smb2_util_unlink(tree, FNAME);
62 status = torture_smb2_testfile(tree, FNAME, &h);
63 CHECK_STATUS(status, NT_STATUS_OK);
66 rd.in.file.handle = h;
69 status = smb2_read(tree, tree, &rd);
70 CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
72 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
73 CHECK_STATUS(status, NT_STATUS_OK);
76 rd.in.file.handle = h;
81 status = smb2_read(tree, tmp_ctx, &rd);
82 CHECK_STATUS(status, NT_STATUS_OK);
83 CHECK_VALUE(rd.out.data.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);
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);
100 rd.in.offset = sizeof(buf);
101 status = smb2_read(tree, tmp_ctx, &rd);
102 CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
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);
113 rd.in.offset = sizeof(buf) - 1;
114 status = smb2_read(tree, tmp_ctx, &rd);
115 CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
117 rd.in.min_count = 0x10000;
120 status = smb2_read(tree, tmp_ctx, &rd);
121 CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
123 rd.in.min_count = 0x10000 - 2;
126 status = smb2_read(tree, tmp_ctx, &rd);
127 CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
129 rd.in.min_count = 10;
132 status = smb2_read(tree, tmp_ctx, &rd);
133 CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
136 talloc_free(tmp_ctx);
141 static bool test_read_position(struct torture_context *torture, struct smb2_tree *tree)
145 struct smb2_handle h;
146 uint8_t buf[64*1024];
148 TALLOC_CTX *tmp_ctx = talloc_new(tree);
149 union smb_fileinfo info;
153 status = torture_smb2_testfile(tree, FNAME, &h);
154 CHECK_STATUS(status, NT_STATUS_OK);
156 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
157 CHECK_STATUS(status, NT_STATUS_OK);
160 rd.in.file.handle = h;
165 status = smb2_read(tree, tmp_ctx, &rd);
166 CHECK_STATUS(status, NT_STATUS_OK);
167 CHECK_VALUE(rd.out.data.length, 10);
169 info.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
170 info.generic.in.file.handle = h;
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);
177 CHECK_VALUE(info.all_info2.out.position, 10);
182 talloc_free(tmp_ctx);
186 static bool test_read_dir(struct torture_context *torture, struct smb2_tree *tree)
190 struct smb2_handle h;
192 TALLOC_CTX *tmp_ctx = talloc_new(tree);
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));
201 rd.in.file.handle = h;
206 status = smb2_read(tree, tmp_ctx, &rd);
207 CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
209 rd.in.min_count = 11;
210 status = smb2_read(tree, tmp_ctx, &rd);
211 CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
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);
219 CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
225 status = smb2_read(tree, tmp_ctx, &rd);
226 if (torture_setting_bool(torture, "windows", false)) {
227 CHECK_STATUS(status, NT_STATUS_OK);
229 CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
233 talloc_free(tmp_ctx);
239 basic testing of SMB2 read
241 struct torture_suite *torture_smb2_read_init(void)
243 struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "read");
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);
249 suite->description = talloc_strdup(suite, "SMB2-READ tests");