r11800: - filled in unknown fields in SMB2 all_info level
[garming/samba-autobuild/.git] / source4 / torture / smb2 / setinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 setinfo individual test suite
5
6    Copyright (C) Andrew Tridgell 2005
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "system/time.h"
25
26 #define BASEDIR ""
27
28 /* basic testing of all SMB2 setinfo calls 
29    for each call we test that it succeeds, and where possible test 
30    for consistency between the calls. 
31 */
32 BOOL torture_smb2_setinfo(void)
33 {
34         struct smb2_tree *tree;
35         BOOL ret = True;
36         TALLOC_CTX *mem_ctx = talloc_new(NULL);
37         struct smb2_handle handle;
38         char *fname;
39         char *fname_new;
40         union smb_fileinfo finfo2;
41         union smb_setfileinfo sfinfo;
42         NTSTATUS status, status2;
43         const char *call_name;
44         time_t basetime = (time(NULL) - 86400) & ~1;
45         int n = time(NULL) % 100;
46         
47         fname = talloc_asprintf(mem_ctx, BASEDIR "fnum_test_%d.txt", n);
48         fname_new = talloc_asprintf(mem_ctx, BASEDIR "fnum_test_new_%d.txt", n);
49
50         if (!torture_smb2_connection(mem_ctx, &tree)) {
51                 return False;
52         }
53
54 #define RECREATE_FILE(fname) do { \
55         smb2_util_close(tree, handle); \
56         status = smb2_create_complex_file(tree, fname, &handle); \
57         if (!NT_STATUS_IS_OK(status)) { \
58                 printf("(%s) ERROR: open of %s failed (%s)\n", \
59                        __location__, fname, nt_errstr(status)); \
60                 ret = False; \
61                 goto done; \
62         }} while (0)
63
64 #define RECREATE_BOTH do { \
65                 RECREATE_FILE(fname); \
66         } while (0)
67
68         RECREATE_BOTH;
69         
70 #define CHECK_CALL(call, rightstatus) do { \
71         call_name = #call; \
72         sfinfo.generic.level = RAW_SFILEINFO_ ## call; \
73         sfinfo.generic.file.handle = handle; \
74         status = smb2_setinfo_file(tree, &sfinfo); \
75         if (!NT_STATUS_EQUAL(status, rightstatus)) { \
76                 printf("(%s) %s - %s (should be %s)\n", __location__, #call, \
77                         nt_errstr(status), nt_errstr(rightstatus)); \
78                 ret = False; \
79         } \
80         } while (0)
81
82 #define CHECK1(call) \
83         do { if (NT_STATUS_IS_OK(status)) { \
84                 finfo2.generic.level = RAW_FILEINFO_ ## call; \
85                 finfo2.generic.in.handle = handle; \
86                 status2 = smb2_getinfo_file(tree, mem_ctx, &finfo2); \
87                 if (!NT_STATUS_IS_OK(status2)) { \
88                         printf("%s - %s\n", #call, nt_errstr(status2)); \
89                 } \
90         }} while (0)
91
92 #define CHECK_VALUE(call, stype, field, value) do { \
93         CHECK1(call); \
94         if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && finfo2.stype.out.field != value) { \
95                 printf("(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
96                        call_name, #stype, #field, \
97                        (uint_t)value, (uint_t)finfo2.stype.out.field); \
98                 torture_smb2_all_info(tree, handle); \
99         }} while (0)
100
101 #define CHECK_TIME(call, stype, field, value) do { \
102         CHECK1(call); \
103         if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && nt_time_to_unix(finfo2.stype.out.field) != value) { \
104                 printf("(%s) %s - %s/%s should be 0x%x - 0x%x\n", __location__, \
105                         call_name, #stype, #field, \
106                         (uint_t)value, \
107                         (uint_t)nt_time_to_unix(finfo2.stype.out.field)); \
108                 printf("\t%s", timestring(mem_ctx, value)); \
109                 printf("\t%s\n", nt_time_string(mem_ctx, finfo2.stype.out.field)); \
110                 torture_smb2_all_info(tree, handle); \
111         }} while (0)
112
113 #define CHECK_STR(call, stype, field, value) do { \
114         CHECK1(call); \
115         if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(status2) && strcmp(finfo2.stype.out.field, value) != 0) { \
116                 printf("(%s) %s - %s/%s should be '%s' - '%s'\n", __location__, \
117                         call_name, #stype, #field, \
118                         value, \
119                         finfo2.stype.out.field); \
120                 torture_smb2_all_info(tree, handle); \
121         }} while (0)
122
123 #define CHECK_STATUS(status, correct) do { \
124         if (!NT_STATUS_EQUAL(status, correct)) { \
125                 printf("(%s) Incorrect status %s - should be %s\n", \
126                        __location__, nt_errstr(status), nt_errstr(correct)); \
127                 ret = False; \
128                 goto done; \
129         }} while (0)
130
131
132         torture_smb2_all_info(tree, handle);
133         
134         printf("test basic_information level\n");
135         basetime += 86400;
136         unix_to_nt_time(&sfinfo.basic_info.in.create_time, basetime + 100);
137         unix_to_nt_time(&sfinfo.basic_info.in.access_time, basetime + 200);
138         unix_to_nt_time(&sfinfo.basic_info.in.write_time,  basetime + 300);
139         unix_to_nt_time(&sfinfo.basic_info.in.change_time, basetime + 400);
140         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_READONLY;
141         CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
142         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, create_time, basetime + 100);
143         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, access_time, basetime + 200);
144         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, write_time,  basetime + 300);
145         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, change_time, basetime + 400);
146         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib,     FILE_ATTRIBUTE_READONLY);
147
148         printf("a zero time means don't change\n");
149         unix_to_nt_time(&sfinfo.basic_info.in.create_time, 0);
150         unix_to_nt_time(&sfinfo.basic_info.in.access_time, 0);
151         unix_to_nt_time(&sfinfo.basic_info.in.write_time,  0);
152         unix_to_nt_time(&sfinfo.basic_info.in.change_time, 0);
153         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
154         CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
155         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, create_time, basetime + 100);
156         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, access_time, basetime + 200);
157         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, write_time,  basetime + 300);
158         CHECK_TIME(SMB2_ALL_INFORMATION, all_info2, change_time, basetime + 400);
159         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib,     FILE_ATTRIBUTE_NORMAL);
160
161         printf("change the attribute\n");
162         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_HIDDEN;
163         CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
164         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_HIDDEN);
165
166         printf("zero attrib means don't change\n");
167         sfinfo.basic_info.in.attrib = 0;
168         CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
169         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_HIDDEN);
170
171         printf("restore attribute\n");
172         sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
173         CHECK_CALL(BASIC_INFORMATION, NT_STATUS_OK);
174         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, attrib, FILE_ATTRIBUTE_NORMAL);
175
176         printf("test disposition_information level\n");
177         sfinfo.disposition_info.in.delete_on_close = 1;
178         CHECK_CALL(DISPOSITION_INFORMATION, NT_STATUS_OK);
179         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, delete_pending, 1);
180         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, nlink, 0);
181
182         sfinfo.disposition_info.in.delete_on_close = 0;
183         CHECK_CALL(DISPOSITION_INFORMATION, NT_STATUS_OK);
184         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, delete_pending, 0);
185         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, nlink, 1);
186
187         printf("test allocation_information level\n");
188         sfinfo.allocation_info.in.alloc_size = 0;
189         CHECK_CALL(ALLOCATION_INFORMATION, NT_STATUS_OK);
190         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 0);
191         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, alloc_size, 0);
192
193         sfinfo.allocation_info.in.alloc_size = 4096;
194         CHECK_CALL(ALLOCATION_INFORMATION, NT_STATUS_OK);
195         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, alloc_size, 4096);
196         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 0);
197
198         printf("test end_of_file_info level\n");
199         sfinfo.end_of_file_info.in.size = 37;
200         CHECK_CALL(END_OF_FILE_INFORMATION, NT_STATUS_OK);
201         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 37);
202
203         sfinfo.end_of_file_info.in.size = 7;
204         CHECK_CALL(END_OF_FILE_INFORMATION, NT_STATUS_OK);
205         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, size, 7);
206
207         printf("test position_information level\n");
208         sfinfo.position_information.in.position = 123456;
209         CHECK_CALL(POSITION_INFORMATION, NT_STATUS_OK);
210         CHECK_VALUE(POSITION_INFORMATION, position_information, position, 123456);
211         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, position, 123456);
212
213         printf("test mode_information level\n");
214         sfinfo.mode_information.in.mode = 2;
215         CHECK_CALL(MODE_INFORMATION, NT_STATUS_OK);
216         CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 2);
217         CHECK_VALUE(SMB2_ALL_INFORMATION, all_info2, mode, 2);
218
219         sfinfo.mode_information.in.mode = 1;
220         CHECK_CALL(MODE_INFORMATION, NT_STATUS_INVALID_PARAMETER);
221
222         sfinfo.mode_information.in.mode = 0;
223         CHECK_CALL(MODE_INFORMATION, NT_STATUS_OK);
224         CHECK_VALUE(MODE_INFORMATION, mode_information, mode, 0);
225
226 done:
227         status = smb2_util_close(tree, handle);
228         if (NT_STATUS_IS_ERR(status)) {
229                 printf("Failed to delete %s - %s\n", fname, nt_errstr(status));
230         }
231         smb2_util_unlink(tree, fname);
232
233         talloc_free(mem_ctx);
234         return ret;
235 }
236
237