s4-smbtorture: use standard macros in smb2.read test
[Samba.git] / source4 / torture / smb2 / read.c
blobc1105a9d26226ad3a45fd481c32a14b3c9113d88
1 /*
2 Unix SMB/CIFS implementation.
4 SMB2 read test suite
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/>.
22 #include "includes.h"
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, _expected) \
31 torture_assert_ntstatus_equal_goto(torture, _status, _expected, \
32 ret, done, "Incorrect status")
34 #define CHECK_VALUE(v, correct) \
35 torture_assert_int_equal_goto(torture, v, correct, \
36 ret, done, "Incorrect value")
38 #define FNAME "smb2_readtest.dat"
39 #define DNAME "smb2_readtest.dir"
41 static bool test_read_eof(struct torture_context *torture, struct smb2_tree *tree)
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);
50 ZERO_STRUCT(buf);
52 smb2_util_unlink(tree, FNAME);
54 status = torture_smb2_testfile(tree, FNAME, &h);
55 CHECK_STATUS(status, NT_STATUS_OK);
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);
64 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
65 CHECK_STATUS(status, NT_STATUS_OK);
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;
73 status = smb2_read(tree, tmp_ctx, &rd);
74 CHECK_STATUS(status, NT_STATUS_OK);
75 CHECK_VALUE(rd.out.data.length, 10);
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);
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);
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);
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);
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);
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);
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);
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);
127 done:
128 talloc_free(tmp_ctx);
129 return ret;
133 static bool test_read_position(struct torture_context *torture, struct smb2_tree *tree)
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;
143 ZERO_STRUCT(buf);
145 status = torture_smb2_testfile(tree, FNAME, &h);
146 CHECK_STATUS(status, NT_STATUS_OK);
148 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
149 CHECK_STATUS(status, NT_STATUS_OK);
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;
157 status = smb2_read(tree, tmp_ctx, &rd);
158 CHECK_STATUS(status, NT_STATUS_OK);
159 CHECK_VALUE(rd.out.data.length, 10);
161 info.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
162 info.generic.in.file.handle = h;
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);
173 done:
174 talloc_free(tmp_ctx);
175 return ret;
178 static bool test_read_dir(struct torture_context *torture, struct smb2_tree *tree)
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);
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;
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;
198 status = smb2_read(tree, tmp_ctx, &rd);
199 CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
201 rd.in.min_count = 11;
202 status = smb2_read(tree, tmp_ctx, &rd);
203 CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
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);
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);
224 done:
225 talloc_free(tmp_ctx);
226 return ret;
231 basic testing of SMB2 read
233 struct torture_suite *torture_smb2_read_init(void)
235 struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "read");
237 torture_suite_add_1smb2_test(suite, "eof", test_read_eof);
238 torture_suite_add_1smb2_test(suite, "position", test_read_position);
239 torture_suite_add_1smb2_test(suite, "dir", test_read_dir);
241 suite->description = talloc_strdup(suite, "SMB2-READ tests");
243 return suite;