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 status
= torture_smb2_testfile(tree
, FNAME
, &h
);
61 CHECK_STATUS(status
, NT_STATUS_OK
);
63 status
= smb2_util_write(tree
, h
, buf
, 0, ARRAY_SIZE(buf
));
64 CHECK_STATUS(status
, NT_STATUS_OK
);
67 rd
.in
.file
.handle
= h
;
72 status
= smb2_read(tree
, tmp_ctx
, &rd
);
73 CHECK_STATUS(status
, NT_STATUS_OK
);
74 CHECK_VALUE(rd
.out
.data
.length
, 10);
78 rd
.in
.offset
= sizeof(buf
);
79 status
= smb2_read(tree
, tmp_ctx
, &rd
);
80 CHECK_STATUS(status
, NT_STATUS_END_OF_FILE
);
84 rd
.in
.offset
= sizeof(buf
);
85 status
= smb2_read(tree
, tmp_ctx
, &rd
);
86 CHECK_STATUS(status
, NT_STATUS_OK
);
87 CHECK_VALUE(rd
.out
.data
.length
, 0);
91 rd
.in
.offset
= sizeof(buf
);
92 status
= smb2_read(tree
, tmp_ctx
, &rd
);
93 CHECK_STATUS(status
, NT_STATUS_END_OF_FILE
);
97 rd
.in
.offset
= sizeof(buf
) - 1;
98 status
= smb2_read(tree
, tmp_ctx
, &rd
);
99 CHECK_STATUS(status
, NT_STATUS_OK
);
100 CHECK_VALUE(rd
.out
.data
.length
, 1);
104 rd
.in
.offset
= sizeof(buf
) - 1;
105 status
= smb2_read(tree
, tmp_ctx
, &rd
);
106 CHECK_STATUS(status
, NT_STATUS_END_OF_FILE
);
108 rd
.in
.min_count
= 0x10000;
111 status
= smb2_read(tree
, tmp_ctx
, &rd
);
112 CHECK_STATUS(status
, NT_STATUS_END_OF_FILE
);
114 rd
.in
.min_count
= 0x10000 - 2;
117 status
= smb2_read(tree
, tmp_ctx
, &rd
);
118 CHECK_STATUS(status
, NT_STATUS_END_OF_FILE
);
120 rd
.in
.min_count
= 10;
123 status
= smb2_read(tree
, tmp_ctx
, &rd
);
124 CHECK_STATUS(status
, NT_STATUS_END_OF_FILE
);
127 talloc_free(tmp_ctx
);
132 static bool test_read_position(struct torture_context
*torture
, struct smb2_tree
*tree
)
136 struct smb2_handle h
;
139 TALLOC_CTX
*tmp_ctx
= talloc_new(tree
);
140 union smb_fileinfo info
;
144 status
= torture_smb2_testfile(tree
, FNAME
, &h
);
145 CHECK_STATUS(status
, NT_STATUS_OK
);
147 status
= smb2_util_write(tree
, h
, buf
, 0, ARRAY_SIZE(buf
));
148 CHECK_STATUS(status
, NT_STATUS_OK
);
151 rd
.in
.file
.handle
= h
;
156 status
= smb2_read(tree
, tmp_ctx
, &rd
);
157 CHECK_STATUS(status
, NT_STATUS_OK
);
158 CHECK_VALUE(rd
.out
.data
.length
, 10);
160 info
.generic
.level
= RAW_FILEINFO_SMB2_ALL_INFORMATION
;
161 info
.generic
.in
.file
.handle
= h
;
163 status
= smb2_getinfo_file(tree
, tmp_ctx
, &info
);
164 CHECK_STATUS(status
, NT_STATUS_OK
);
165 if (torture_setting_bool(torture
, "windows", false)) {
166 CHECK_VALUE(info
.all_info2
.out
.position
, 0);
168 CHECK_VALUE(info
.all_info2
.out
.position
, 10);
173 talloc_free(tmp_ctx
);
177 static bool test_read_dir(struct torture_context
*torture
, struct smb2_tree
*tree
)
181 struct smb2_handle h
;
183 TALLOC_CTX
*tmp_ctx
= talloc_new(tree
);
185 status
= torture_smb2_testdir(tree
, DNAME
, &h
);
186 if (!NT_STATUS_IS_OK(status
)) {
187 printf(__location__
" Unable to create test directory '%s' - %s\n", DNAME
, nt_errstr(status
));
192 rd
.in
.file
.handle
= h
;
197 status
= smb2_read(tree
, tmp_ctx
, &rd
);
198 CHECK_STATUS(status
, NT_STATUS_INVALID_DEVICE_REQUEST
);
200 rd
.in
.min_count
= 11;
201 status
= smb2_read(tree
, tmp_ctx
, &rd
);
202 CHECK_STATUS(status
, NT_STATUS_INVALID_DEVICE_REQUEST
);
205 rd
.in
.min_count
= 2592;
206 status
= smb2_read(tree
, tmp_ctx
, &rd
);
207 if (torture_setting_bool(torture
, "windows", false)) {
208 CHECK_STATUS(status
, NT_STATUS_END_OF_FILE
);
210 CHECK_STATUS(status
, NT_STATUS_INVALID_DEVICE_REQUEST
);
216 status
= smb2_read(tree
, tmp_ctx
, &rd
);
217 if (torture_setting_bool(torture
, "windows", false)) {
218 CHECK_STATUS(status
, NT_STATUS_OK
);
220 CHECK_STATUS(status
, NT_STATUS_INVALID_DEVICE_REQUEST
);
224 talloc_free(tmp_ctx
);
230 basic testing of SMB2 read
232 struct torture_suite
*torture_smb2_read_init(void)
234 struct torture_suite
*suite
= torture_suite_create(talloc_autofree_context(), "READ");
236 torture_suite_add_1smb2_test(suite
, "EOF", test_read_eof
);
237 torture_suite_add_1smb2_test(suite
, "POSITION", test_read_position
);
238 torture_suite_add_1smb2_test(suite
, "DIR", test_read_dir
);
240 suite
->description
= talloc_strdup(suite
, "SMB2-READ tests");