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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/time.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
28 #include "torture/torture.h"
29 #include "torture/smb2/proto.h"
31 #include "libcli/security/security.h"
32 #include "librpc/gen_ndr/ndr_security.h"
36 /* basic testing of all SMB2 setinfo calls
37 for each call we test that it succeeds, and where possible test
38 for consistency between the calls.
40 BOOL torture_smb2_setinfo(struct torture_context *torture)
42 struct smb2_tree *tree;
44 TALLOC_CTX *mem_ctx = talloc_new(NULL);
45 struct smb2_handle handle;
48 union smb_fileinfo finfo2;
49 union smb_setfileinfo sfinfo;
50 struct security_ace ace;
51 struct security_descriptor *sd;
52 struct dom_sid *test_sid;
53 NTSTATUS status, status2=NT_STATUS_OK;
54 const char *call_name;
55 time_t basetime = (time(NULL) - 86400) & ~1;
56 int n = time(NULL) % 100;
60 fname = talloc_asprintf(mem_ctx, BASEDIR "fnum_test_%d.txt", n);
61 fname_new = talloc_asprintf(mem_ctx, BASEDIR "fnum_test_new_%d.txt", n);
63 if (!torture_smb2_connection(mem_ctx, &tree)) {
67 #define RECREATE_FILE(fname) do { \
68 smb2_util_close(tree, handle); \
69 status = smb2_create_complex_file(tree, fname, &handle); \
70 if (!NT_STATUS_IS_OK(status)) { \
71 printf("(%s) ERROR: open of %s failed (%s)\n", \
72 __location__, fname, nt_errstr(status)); \
77 #define RECREATE_BOTH do { \
78 RECREATE_FILE(fname); \
83 #define CHECK_CALL(call, rightstatus) do { \
85 sfinfo.generic.level = RAW_SFILEINFO_ ## call; \
86 sfinfo.generic.in.file.handle = handle; \
87 status = smb2_setinfo_file(tree, &sfinfo); \
88 if (!NT_STATUS_EQUAL(status, rightstatus)) { \
89 printf("(%s) %s - %s (should be %s)\n", __location__, #call, \
90 nt_errstr(status), nt_errstr(rightstatus)); \
96 #define CHECK1(call) \
97 do { if (NT_STATUS_IS_OK(status)) { \
98 finfo2.generic.level = RAW_FILEINFO_ ## call; \
99 finfo2.generic.in.file.handle = handle; \
100 status2 = smb2_getinfo_file(tree, mem_ctx, &finfo2); \
101 if (!NT_STATUS_IS_OK(status2)) { \
102 printf("(%s) %s - %s\n", __location__, #call, nt_errstr(status2)); \
108 #define CHECK_VALUE(call, stype, field, value) do { \
110 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && finfo2.stype.out.field != value) { \
111 printf("(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
112 call_name, #stype, #field, \
113 (uint_t)value, (uint_t)finfo2.stype.out.field); \
114 torture_smb2_all_info(tree, handle); \
119 #define CHECK_TIME(call, stype, field, value) do { \
121 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && nt_time_to_unix(finfo2.stype.out.field) != value) { \
122 printf("(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
123 call_name, #stype, #field, \
125 (uint_t)nt_time_to_unix(finfo2.stype.out.field)); \
126 printf("\t%s", timestring(mem_ctx, value)); \
127 printf("\t%s\n", nt_time_string(mem_ctx, finfo2.stype.out.field)); \
128 torture_smb2_all_info(tree, handle); \
133 #define CHECK_STATUS(status, correct) do { \
134 if (!NT_STATUS_EQUAL(status, correct)) { \
135 printf("(%s) Incorrect status %s - should be %s\n", \
136 __location__, nt_errstr(status), nt_errstr(correct)); \
141 torture_smb2_all_info(tree, handle);
143 printf("test basic_information level\n");
145 unix_to_nt_time(&sfinfo.basic_info.in.create_time, basetime + 100);
146 unix_to_nt_time(&sfinfo.basic_info.in.access_time, basetime + 200);
147 unix_to_nt_time(&sfinfo.basic_info.in.write_time, basetime + 300);
148 unix_to_nt_time(&sfinfo.basic_info.in.change_time, basetime + 400);
149 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_READONLY;
150 CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
151 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, create_time, basetime + 100);
152 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, access_time, basetime + 200);
153 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, write_time, basetime + 300);
154 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, change_time, basetime + 400);
155 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_READONLY);
157 printf("a zero time means don't change\n");
158 unix_to_nt_time(&sfinfo.basic_info.in.create_time, 0);
159 unix_to_nt_time(&sfinfo.basic_info.in.access_time, 0);
160 unix_to_nt_time(&sfinfo.basic_info.in.write_time, 0);
161 unix_to_nt_time(&sfinfo.basic_info.in.change_time, 0);
162 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
163 CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
164 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, create_time, basetime + 100);
165 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, access_time, basetime + 200);
166 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, write_time, basetime + 300);
167 CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, change_time, basetime + 400);
168 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_NORMAL);
170 printf("change the attribute\n");
171 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_HIDDEN;
172 CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
173 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_HIDDEN);
175 printf("zero attrib means don't change\n");
176 sfinfo.basic_info.in.attrib = 0;
177 CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
178 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_HIDDEN);
180 printf("restore attribute\n");
181 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
182 CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
183 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_NORMAL);
185 printf("test disposition_information level\n");
186 sfinfo.disposition_info.in.delete_on_close = 1;
187 CHECK_CALL(DISPOSITION_INFORMATION, NT_STATUS_OK);
188 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, delete_pending, 1);
189 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, nlink, 0);
191 sfinfo.disposition_info.in.delete_on_close = 0;
192 CHECK_CALL(DISPOSITION_INFORMATION, NT_STATUS_OK);
193 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, delete_pending, 0);
194 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, nlink, 1);
196 printf("test allocation_information level\n");
197 sfinfo.allocation_info.in.alloc_size = 0;
198 CHECK_CALL(ALLOCATION_INFORMATION, NT_STATUS_OK);
199 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 0);
200 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, alloc_size, 0);
202 sfinfo.allocation_info.in.alloc_size = 4096;
203 CHECK_CALL(ALLOCATION_INFORMATION, NT_STATUS_OK);
204 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, alloc_size, 4096);
205 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 0);
207 printf("test end_of_file_info level\n");
208 sfinfo.end_of_file_info.in.size = 37;
209 CHECK_CALL(END_OF_FILE_INFORMATION, NT_STATUS_OK);
210 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 37);
212 sfinfo.end_of_file_info.in.size = 7;
213 CHECK_CALL(END_OF_FILE_INFORMATION, NT_STATUS_OK);
214 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 7);
216 printf("test position_information level\n");
217 sfinfo.position_information.in.position = 123456;
218 CHECK_CALL(POSITION_INFORMATION, NT_STATUS_OK);
219 CHECK_VALUE(POSITION_INFORMATION, position_information, position, 123456);
220 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, position, 123456);
222 printf("test mode_information level\n");
223 sfinfo.mode_information.in.mode = 2;
224 CHECK_CALL(MODE_INFORMATION, NT_STATUS_OK);
225 CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 2);
226 CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, mode, 2);
228 sfinfo.mode_information.in.mode = 1;
229 CHECK_CALL(MODE_INFORMATION, NT_STATUS_INVALID_PARAMETER);
231 sfinfo.mode_information.in.mode = 0;
232 CHECK_CALL(MODE_INFORMATION, NT_STATUS_OK);
233 CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 0);
235 printf("test sec_desc level\n");
237 finfo2.query_secdesc.in.secinfo_flags =
242 sd = finfo2.query_secdesc.out.sd;
244 test_sid = dom_sid_parse_talloc(mem_ctx, "S-1-5-32-1234-5432");
246 ace.type = SEC_ACE_TYPE_ACCESS_ALLOWED;
248 ace.access_mask = SEC_STD_ALL;
249 ace.trustee = *test_sid;
250 status = security_descriptor_dacl_add(sd, &ace);
251 CHECK_STATUS(status, NT_STATUS_OK);
253 printf("add a new ACE to the DACL\n");
255 sfinfo.set_secdesc.in.secinfo_flags = finfo2.query_secdesc.in.secinfo_flags;
256 sfinfo.set_secdesc.in.sd = sd;
257 CHECK_CALL(SEC_DESC, NT_STATUS_OK);
260 if (!security_acl_equal(finfo2.query_secdesc.out.sd->dacl, sd->dacl)) {
261 printf("%s: security descriptors don't match!\n", __location__);
263 NDR_PRINT_DEBUG(security_descriptor, finfo2.query_secdesc.out.sd);
264 printf("expected:\n");
265 NDR_PRINT_DEBUG(security_descriptor, sd);
269 printf("remove it again\n");
271 status = security_descriptor_dacl_del(sd, test_sid);
272 CHECK_STATUS(status, NT_STATUS_OK);
274 sfinfo.set_secdesc.in.secinfo_flags = finfo2.query_secdesc.in.secinfo_flags;
275 sfinfo.set_secdesc.in.sd = sd;
276 CHECK_CALL(SEC_DESC, NT_STATUS_OK);
279 if (!security_acl_equal(finfo2.query_secdesc.out.sd->dacl, sd->dacl)) {
280 printf("%s: security descriptors don't match!\n", __location__);
282 NDR_PRINT_DEBUG(security_descriptor, finfo2.query_secdesc.out.sd);
283 printf("expected:\n");
284 NDR_PRINT_DEBUG(security_descriptor, sd);
289 status = smb2_util_close(tree, handle);
290 if (NT_STATUS_IS_ERR(status)) {
291 printf("Failed to delete %s - %s\n", fname, nt_errstr(status));
293 smb2_util_unlink(tree, fname);
295 talloc_free(mem_ctx);