548bd1ce610f7eff738d8805cb9cb192fb80b3e0
[samba.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 #include "librpc/gen_ndr/ndr_security.h"
30
31 #define CHECK_STATUS(status, correct) do { \
32         if (!NT_STATUS_EQUAL(status, correct)) { \
33                 printf("(%s) Incorrect status %s - should be %s\n", \
34                        __location__, nt_errstr(status), nt_errstr(correct)); \
35                 ret = false; \
36                 goto done; \
37         }} while (0)
38
39 #define CHECK_VALUE(v, correct) do { \
40         if ((v) != (correct)) { \
41                 printf("(%s) Incorrect value %s=%u - should be %u\n", \
42                        __location__, #v, (unsigned)v, (unsigned)correct); \
43                 ret = false; \
44                 goto done; \
45         }} while (0)
46
47 #define FNAME "smb2_readtest.dat"
48 #define DNAME "smb2_readtest.dir"
49
50 static bool test_read_eof(struct torture_context *torture, struct smb2_tree *tree)
51 {
52         bool ret = true;
53         NTSTATUS status;
54         struct smb2_handle h;
55         uint8_t buf[70000];
56         struct smb2_read rd;
57         TALLOC_CTX *tmp_ctx = talloc_new(tree);
58
59         ZERO_STRUCT(buf);
60
61         status = torture_smb2_testfile(tree, FNAME, &h);
62         CHECK_STATUS(status, NT_STATUS_OK);
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[70000];
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
230 /* 
231    basic testing of SMB2 read
232 */
233 struct torture_suite *torture_smb2_read_init(void)
234 {
235         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "READ");
236
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);
240
241         suite->description = talloc_strdup(suite, "SMB2-READ tests");
242
243         return suite;
244 }
245