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 #define FAIL_UNLESS(__cond) \
37 if (__cond) {} else { \
38 torture_result(tctx, TORTURE_FAIL, "%s) condition violated: %s\n", \
39 __location__, #__cond); \
40 ret = false; goto done; \
44 /* basic testing of all SMB2 setinfo calls
45 for each call we test that it succeeds, and where possible test
46 for consistency between the calls.
48 bool torture_smb2_setinfo(struct torture_context
*tctx
)
50 struct smb2_tree
*tree
;
52 struct smb2_handle handle
;
55 union smb_fileinfo finfo2
;
56 union smb_setfileinfo sfinfo
;
57 struct security_ace ace
;
58 struct security_descriptor
*sd
;
59 struct dom_sid
*test_sid
;
60 NTSTATUS status
, status2
=NT_STATUS_OK
;
61 const char *call_name
;
62 time_t basetime
= (time(NULL
) - 86400) & ~1;
63 int n
= time(NULL
) % 100;
67 fname
= talloc_asprintf(tctx
, BASEDIR
"fnum_test_%d.txt", n
);
68 fname_new
= talloc_asprintf(tctx
, BASEDIR
"fnum_test_new_%d.txt", n
);
70 if (!torture_smb2_connection(tctx
, &tree
)) {
74 #define RECREATE_FILE(fname) do { \
75 smb2_util_close(tree, handle); \
76 status = smb2_create_complex_file(tree, fname, &handle); \
77 if (!NT_STATUS_IS_OK(status)) { \
78 torture_result(tctx, TORTURE_ERROR, "(%s) ERROR: open of %s failed (%s)\n", \
79 __location__, fname, nt_errstr(status)); \
84 #define RECREATE_BOTH do { \
85 RECREATE_FILE(fname); \
90 #define CHECK_CALL(call, rightstatus) do { \
92 sfinfo.generic.level = RAW_SFILEINFO_ ## call; \
93 sfinfo.generic.in.file.handle = handle; \
94 status = smb2_setinfo_file(tree, &sfinfo); \
95 if (!NT_STATUS_EQUAL(status, rightstatus)) { \
96 torture_result(tctx, TORTURE_FAIL, "(%s) %s - %s (should be %s)\n", __location__, #call, \
97 nt_errstr(status), nt_errstr(rightstatus)); \
103 #define CHECK1(call) \
104 do { if (NT_STATUS_IS_OK(status)) { \
105 finfo2.generic.level = RAW_FILEINFO_ ## call; \
106 finfo2.generic.in.file.handle = handle; \
107 status2 = smb2_getinfo_file(tree, tctx, &finfo2); \
108 if (!NT_STATUS_IS_OK(status2)) { \
109 torture_result(tctx, TORTURE_FAIL, "(%s) %s - %s\n", __location__, #call, nt_errstr(status2)); \
115 #define CHECK_VALUE(call, stype, field, value) do { \
117 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && finfo2.stype.out.field != value) { \
118 torture_result(tctx, TORTURE_FAIL, "(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
119 call_name, #stype, #field, \
120 (unsigned int)value, (unsigned int)finfo2.stype.out.field); \
121 torture_smb2_all_info(tree, handle); \
126 #define CHECK_TIME(call, stype, field, value) do { \
128 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && nt_time_to_unix(finfo2.stype.out.field) != value) { \
129 torture_result(tctx, TORTURE_FAIL, "(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
130 call_name, #stype, #field, \
131 (unsigned int)value, \
132 (unsigned int)nt_time_to_unix(finfo2.stype.out.field)); \
133 torture_warning(tctx, "\t%s", timestring(tctx, value)); \
134 torture_warning(tctx, "\t%s\n", nt_time_string(tctx, finfo2.stype.out.field)); \
135 torture_smb2_all_info(tree, handle); \
140 #define CHECK_STATUS(status, correct) do { \
141 if (!NT_STATUS_EQUAL(status, correct)) { \
142 torture_result(tctx, TORTURE_FAIL, "(%s) Incorrect status %s - should be %s\n", \
143 __location__, nt_errstr(status), nt_errstr(correct)); \
148 torture_smb2_all_info(tree
, handle
);
150 torture_comment(tctx
, "Test basic_information level\n");
152 unix_to_nt_time(&sfinfo
.basic_info
.in
.create_time
, basetime
+ 100);
153 unix_to_nt_time(&sfinfo
.basic_info
.in
.access_time
, basetime
+ 200);
154 unix_to_nt_time(&sfinfo
.basic_info
.in
.write_time
, basetime
+ 300);
155 unix_to_nt_time(&sfinfo
.basic_info
.in
.change_time
, basetime
+ 400);
156 sfinfo
.basic_info
.in
.attrib
= FILE_ATTRIBUTE_READONLY
;
157 CHECK_CALL(BASIC_INFORMATION
, NT_STATUS_OK
);
158 CHECK_TIME(SMB2_ALL_INFORMATION
, all_info2
, create_time
, basetime
+ 100);
159 CHECK_TIME(SMB2_ALL_INFORMATION
, all_info2
, access_time
, basetime
+ 200);
160 CHECK_TIME(SMB2_ALL_INFORMATION
, all_info2
, write_time
, basetime
+ 300);
161 CHECK_TIME(SMB2_ALL_INFORMATION
, all_info2
, change_time
, basetime
+ 400);
162 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, attrib
, FILE_ATTRIBUTE_READONLY
);
164 torture_comment(tctx
, "a zero time means don't change\n");
165 unix_to_nt_time(&sfinfo
.basic_info
.in
.create_time
, 0);
166 unix_to_nt_time(&sfinfo
.basic_info
.in
.access_time
, 0);
167 unix_to_nt_time(&sfinfo
.basic_info
.in
.write_time
, 0);
168 unix_to_nt_time(&sfinfo
.basic_info
.in
.change_time
, 0);
169 sfinfo
.basic_info
.in
.attrib
= FILE_ATTRIBUTE_NORMAL
;
170 CHECK_CALL(BASIC_INFORMATION
, NT_STATUS_OK
);
171 CHECK_TIME(SMB2_ALL_INFORMATION
, all_info2
, create_time
, basetime
+ 100);
172 CHECK_TIME(SMB2_ALL_INFORMATION
, all_info2
, access_time
, basetime
+ 200);
173 CHECK_TIME(SMB2_ALL_INFORMATION
, all_info2
, write_time
, basetime
+ 300);
174 CHECK_TIME(SMB2_ALL_INFORMATION
, all_info2
, change_time
, basetime
+ 400);
175 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, attrib
, FILE_ATTRIBUTE_NORMAL
);
177 torture_comment(tctx
, "change the attribute\n");
178 sfinfo
.basic_info
.in
.attrib
= FILE_ATTRIBUTE_HIDDEN
;
179 CHECK_CALL(BASIC_INFORMATION
, NT_STATUS_OK
);
180 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, attrib
, FILE_ATTRIBUTE_HIDDEN
);
182 torture_comment(tctx
, "zero attrib means don't change\n");
183 sfinfo
.basic_info
.in
.attrib
= 0;
184 CHECK_CALL(BASIC_INFORMATION
, NT_STATUS_OK
);
185 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, attrib
, FILE_ATTRIBUTE_HIDDEN
);
187 torture_comment(tctx
, "can't change a file to a directory\n");
188 sfinfo
.basic_info
.in
.attrib
= FILE_ATTRIBUTE_DIRECTORY
;
189 CHECK_CALL(BASIC_INFORMATION
, NT_STATUS_INVALID_PARAMETER
);
191 torture_comment(tctx
, "restore attribute\n");
192 sfinfo
.basic_info
.in
.attrib
= FILE_ATTRIBUTE_NORMAL
;
193 CHECK_CALL(BASIC_INFORMATION
, NT_STATUS_OK
);
194 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, attrib
, FILE_ATTRIBUTE_NORMAL
);
196 torture_comment(tctx
, "Test disposition_information level\n");
197 sfinfo
.disposition_info
.in
.delete_on_close
= 1;
198 CHECK_CALL(DISPOSITION_INFORMATION
, NT_STATUS_OK
);
199 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, delete_pending
, 1);
200 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, nlink
, 0);
202 sfinfo
.disposition_info
.in
.delete_on_close
= 0;
203 CHECK_CALL(DISPOSITION_INFORMATION
, NT_STATUS_OK
);
204 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, delete_pending
, 0);
205 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, nlink
, 1);
207 torture_comment(tctx
, "Test allocation_information level\n");
208 sfinfo
.allocation_info
.in
.alloc_size
= 0;
209 CHECK_CALL(ALLOCATION_INFORMATION
, NT_STATUS_OK
);
210 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, size
, 0);
211 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, alloc_size
, 0);
213 sfinfo
.allocation_info
.in
.alloc_size
= 4096;
214 CHECK_CALL(ALLOCATION_INFORMATION
, NT_STATUS_OK
);
215 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, alloc_size
, 4096);
216 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, size
, 0);
218 torture_comment(tctx
, "Test end_of_file_info level\n");
219 sfinfo
.end_of_file_info
.in
.size
= 37;
220 CHECK_CALL(END_OF_FILE_INFORMATION
, NT_STATUS_OK
);
221 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, size
, 37);
223 sfinfo
.end_of_file_info
.in
.size
= 7;
224 CHECK_CALL(END_OF_FILE_INFORMATION
, NT_STATUS_OK
);
225 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, size
, 7);
227 torture_comment(tctx
, "Test position_information level\n");
228 sfinfo
.position_information
.in
.position
= 123456;
229 CHECK_CALL(POSITION_INFORMATION
, NT_STATUS_OK
);
230 CHECK_VALUE(POSITION_INFORMATION
, position_information
, position
, 123456);
231 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, position
, 123456);
233 torture_comment(tctx
, "Test mode_information level\n");
234 sfinfo
.mode_information
.in
.mode
= 2;
235 CHECK_CALL(MODE_INFORMATION
, NT_STATUS_OK
);
236 CHECK_VALUE(MODE_INFORMATION
, mode_information
, mode
, 2);
237 CHECK_VALUE(SMB2_ALL_INFORMATION
, all_info2
, mode
, 2);
239 sfinfo
.mode_information
.in
.mode
= 1;
240 CHECK_CALL(MODE_INFORMATION
, NT_STATUS_INVALID_PARAMETER
);
242 sfinfo
.mode_information
.in
.mode
= 0;
243 CHECK_CALL(MODE_INFORMATION
, NT_STATUS_OK
);
244 CHECK_VALUE(MODE_INFORMATION
, mode_information
, mode
, 0);
246 torture_comment(tctx
, "Test sec_desc level\n");
248 finfo2
.query_secdesc
.in
.secinfo_flags
=
253 sd
= finfo2
.query_secdesc
.out
.sd
;
255 test_sid
= dom_sid_parse_talloc(tctx
, SID_NT_AUTHENTICATED_USERS
);
257 ace
.type
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
259 ace
.access_mask
= SEC_STD_ALL
;
260 ace
.trustee
= *test_sid
;
261 status
= security_descriptor_dacl_add(sd
, &ace
);
262 CHECK_STATUS(status
, NT_STATUS_OK
);
264 torture_comment(tctx
, "add a new ACE to the DACL\n");
266 sfinfo
.set_secdesc
.in
.secinfo_flags
= finfo2
.query_secdesc
.in
.secinfo_flags
;
267 sfinfo
.set_secdesc
.in
.sd
= sd
;
268 CHECK_CALL(SEC_DESC
, NT_STATUS_OK
);
269 FAIL_UNLESS(smb2_util_verify_sd(tctx
, tree
, handle
, sd
));
271 torture_comment(tctx
, "remove it again\n");
273 status
= security_descriptor_dacl_del(sd
, test_sid
);
274 CHECK_STATUS(status
, NT_STATUS_OK
);
276 sfinfo
.set_secdesc
.in
.secinfo_flags
= finfo2
.query_secdesc
.in
.secinfo_flags
;
277 sfinfo
.set_secdesc
.in
.sd
= sd
;
278 CHECK_CALL(SEC_DESC
, NT_STATUS_OK
);
279 FAIL_UNLESS(smb2_util_verify_sd(tctx
, tree
, handle
, sd
));
282 status
= smb2_util_close(tree
, handle
);
283 if (NT_STATUS_IS_ERR(status
)) {
284 torture_warning(tctx
, "Failed to delete %s - %s\n", fname
, nt_errstr(status
));
286 smb2_util_unlink(tree
, fname
);