2 Unix SMB/CIFS implementation.
4 SMB2 setinfo individual test suite
6 Copyright (C) Andrew Tridgell 2005
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 "system/time.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
27 #include "torture/torture.h"
28 #include "torture/smb2/proto.h"
30 #include "libcli/security/security.h"
31 #include "librpc/gen_ndr/ndr_security.h"
35 /* basic testing of all SMB2 setinfo calls
36 for each call we test that it succeeds, and where possible test
37 for consistency between the calls.
39 BOOL torture_smb2_setinfo(struct torture_context *torture)
41 struct smb2_tree *tree;
43 TALLOC_CTX *mem_ctx = talloc_new(NULL);
44 struct smb2_handle handle;
47 union smb_fileinfo finfo2;
48 union smb_setfileinfo sfinfo;
49 struct security_ace ace;
50 struct security_descriptor *sd;
51 struct dom_sid *test_sid;
52 NTSTATUS status, status2=NT_STATUS_OK;
53 const char *call_name;
54 time_t basetime = (time(NULL) - 86400) & ~1;
55 int n = time(NULL) % 100;
59 fname = talloc_asprintf(mem_ctx, BASEDIR "fnum_test_%d.txt", n);
60 fname_new = talloc_asprintf(mem_ctx, BASEDIR "fnum_test_new_%d.txt", n);
62 if (!torture_smb2_connection(mem_ctx, &tree)) {
66 #define RECREATE_FILE(fname) do { \
67 smb2_util_close(tree, handle); \
68 status = smb2_create_complex_file(tree, fname, &handle); \
69 if (!NT_STATUS_IS_OK(status)) { \
70 printf("(%s) ERROR: open of %s failed (%s)\n", \
71 __location__, fname, nt_errstr(status)); \
76 #define RECREATE_BOTH do { \
77 RECREATE_FILE(fname); \
82 #define CHECK_CALL(call, rightstatus) do { \
84 sfinfo.generic.level = RAW_SFILEINFO_ ## call; \
85 sfinfo.generic.in.file.handle = handle; \
86 status = smb2_setinfo_file(tree, &sfinfo); \
87 if (!NT_STATUS_EQUAL(status, rightstatus)) { \
88 printf("(%s) %s - %s (should be %s)\n", __location__, #call, \
89 nt_errstr(status), nt_errstr(rightstatus)); \
95 #define CHECK1(call) \
96 do { if (NT_STATUS_IS_OK(status)) { \
97 finfo2.generic.level = RAW_FILEINFO_ ## call; \
98 finfo2.generic.in.file.handle = handle; \
99 status2 = smb2_getinfo_file(tree, mem_ctx, &finfo2); \
100 if (!NT_STATUS_IS_OK(status2)) { \
101 printf("(%s) %s - %s\n", __location__, #call, nt_errstr(status2)); \
107 #define CHECK_VALUE(call, stype, field, value) do { \
109 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && finfo2.stype.out.field != value) { \
110 printf("(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
111 call_name, #stype, #field, \
112 (uint_t)value, (uint_t)finfo2.stype.out.field); \
113 torture_smb2_all_info(tree, handle); \
118 #define CHECK_TIME(call, stype, field, value) do { \
120 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && nt_time_to_unix(finfo2.stype.out.field) != value) { \
121 printf("(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
122 call_name, #stype, #field, \
124 (uint_t)nt_time_to_unix(finfo2.stype.out.field)); \
125 printf("\t%s", timestring(mem_ctx, value)); \
126 printf("\t%s\n", nt_time_string(mem_ctx, finfo2.stype.out.field)); \
127 torture_smb2_all_info(tree, handle); \
132 #define CHECK_STATUS(status, correct) do { \
133 if (!NT_STATUS_EQUAL(status, correct)) { \
134 printf("(%s) Incorrect status %s - should be %s\n", \
135 __location__, nt_errstr(status), nt_errstr(correct)); \
140 torture_smb2_all_info(tree, handle);
142 printf("test basic_information level\n");
144 unix_to_nt_time(&sfinfo.basic_info.in.create_time, basetime + 100);
145 unix_to_nt_time(&sfinfo.basic_info.in.access_time, basetime + 200);
146 unix_to_nt_time(&sfinfo.basic_info.in.write_time, basetime + 300);
147 unix_to_nt_time(&sfinfo.basic_info.in.change_time, basetime + 400);
148 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_READONLY;
149 CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
150 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, create_time, basetime + 100);
151 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, access_time, basetime + 200);
152 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, write_time, basetime + 300);
153 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, change_time, basetime + 400);
154 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_READONLY);
156 printf("a zero time means don't change\n");
157 unix_to_nt_time(&sfinfo.basic_info.in.create_time, 0);
158 unix_to_nt_time(&sfinfo.basic_info.in.access_time, 0);
159 unix_to_nt_time(&sfinfo.basic_info.in.write_time, 0);
160 unix_to_nt_time(&sfinfo.basic_info.in.change_time, 0);
161 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
162 CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
163 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, create_time, basetime + 100);
164 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, access_time, basetime + 200);
165 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, write_time, basetime + 300);
166 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, change_time, basetime + 400);
167 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_NORMAL);
169 printf("change the attribute\n");
170 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_HIDDEN;
171 CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
172 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_HIDDEN);
174 printf("zero attrib means don't change\n");
175 sfinfo.basic_info.in.attrib = 0;
176 CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
177 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_HIDDEN);
179 printf("restore attribute\n");
180 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
181 CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
182 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_NORMAL);
184 printf("test disposition_information level\n");
185 sfinfo.disposition_info.in.delete_on_close = 1;
186 CHECK_CALL(DISPOSITION_INFORMATION, NT_STATUS_OK);
187 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, delete_pending, 1);
188 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, nlink, 0);
190 sfinfo.disposition_info.in.delete_on_close = 0;
191 CHECK_CALL(DISPOSITION_INFORMATION, NT_STATUS_OK);
192 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, delete_pending, 0);
193 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, nlink, 1);
195 printf("test allocation_information level\n");
196 sfinfo.allocation_info.in.alloc_size = 0;
197 CHECK_CALL(ALLOCATION_INFORMATION, NT_STATUS_OK);
198 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 0);
199 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, alloc_size, 0);
201 sfinfo.allocation_info.in.alloc_size = 4096;
202 CHECK_CALL(ALLOCATION_INFORMATION, NT_STATUS_OK);
203 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, alloc_size, 4096);
204 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 0);
206 printf("test end_of_file_info level\n");
207 sfinfo.end_of_file_info.in.size = 37;
208 CHECK_CALL(END_OF_FILE_INFORMATION, NT_STATUS_OK);
209 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 37);
211 sfinfo.end_of_file_info.in.size = 7;
212 CHECK_CALL(END_OF_FILE_INFORMATION, NT_STATUS_OK);
213 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 7);
215 printf("test position_information level\n");
216 sfinfo.position_information.in.position = 123456;
217 CHECK_CALL(POSITION_INFORMATION, NT_STATUS_OK);
218 CHECK_VALUE(POSITION_INFORMATION, position_information, position, 123456);
219 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, position, 123456);
221 printf("test mode_information level\n");
222 sfinfo.mode_information.in.mode = 2;
223 CHECK_CALL(MODE_INFORMATION, NT_STATUS_OK);
224 CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 2);
225 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, mode, 2);
227 sfinfo.mode_information.in.mode = 1;
228 CHECK_CALL(MODE_INFORMATION, NT_STATUS_INVALID_PARAMETER);
230 sfinfo.mode_information.in.mode = 0;
231 CHECK_CALL(MODE_INFORMATION, NT_STATUS_OK);
232 CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 0);
234 printf("test sec_desc level\n");
236 finfo2.query_secdesc.in.secinfo_flags =
241 sd = finfo2.query_secdesc.out.sd;
243 test_sid = dom_sid_parse_talloc(mem_ctx, "S-1-5-32-1234-5432");
245 ace.type = SEC_ACE_TYPE_ACCESS_ALLOWED;
247 ace.access_mask = SEC_STD_ALL;
248 ace.trustee = *test_sid;
249 status = security_descriptor_dacl_add(sd, &ace);
250 CHECK_STATUS(status, NT_STATUS_OK);
252 printf("add a new ACE to the DACL\n");
254 sfinfo.set_secdesc.in.secinfo_flags = finfo2.query_secdesc.in.secinfo_flags;
255 sfinfo.set_secdesc.in.sd = sd;
256 CHECK_CALL(SEC_DESC, NT_STATUS_OK);
259 if (!security_acl_equal(finfo2.query_secdesc.out.sd->dacl, sd->dacl)) {
260 printf("%s: security descriptors don't match!\n", __location__);
262 NDR_PRINT_DEBUG(security_descriptor, finfo2.query_secdesc.out.sd);
263 printf("expected:\n");
264 NDR_PRINT_DEBUG(security_descriptor, sd);
268 printf("remove it again\n");
270 status = security_descriptor_dacl_del(sd, test_sid);
271 CHECK_STATUS(status, NT_STATUS_OK);
273 sfinfo.set_secdesc.in.secinfo_flags = finfo2.query_secdesc.in.secinfo_flags;
274 sfinfo.set_secdesc.in.sd = sd;
275 CHECK_CALL(SEC_DESC, NT_STATUS_OK);
278 if (!security_acl_equal(finfo2.query_secdesc.out.sd->dacl, sd->dacl)) {
279 printf("%s: security descriptors don't match!\n", __location__);
281 NDR_PRINT_DEBUG(security_descriptor, finfo2.query_secdesc.out.sd);
282 printf("expected:\n");
283 NDR_PRINT_DEBUG(security_descriptor, sd);
288 status = smb2_util_close(tree, handle);
289 if (NT_STATUS_IS_ERR(status)) {
290 printf("Failed to delete %s - %s\n", fname, nt_errstr(status));
292 smb2_util_unlink(tree, fname);
294 talloc_free(mem_ctx);