06222e8e9e928eef2044dd73c70ac832fed2e8c2
[ira/wip.git] / source4 / torture / smb2 / find.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 find 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 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 static struct {
30         const char *name;
31         uint16_t level;
32         NTSTATUS status;
33         union smb_search_data data;
34 } levels[] = {
35 #define LEVEL(x) #x, x
36  { LEVEL(SMB2_FIND_ID_BOTH_DIRECTORY_INFO) },
37  { LEVEL(SMB2_FIND_DIRECTORY_INFO) },
38  { LEVEL(SMB2_FIND_FULL_DIRECTORY_INFO) },
39  { LEVEL(SMB2_FIND_NAME_INFO) },
40  { LEVEL(SMB2_FIND_BOTH_DIRECTORY_INFO) },
41  { LEVEL(SMB2_FIND_ID_FULL_DIRECTORY_INFO) },
42 };
43
44 #define FNAME "smb2-find.dat"
45
46 #define CHECK_VALUE(call_name, stype, field) do { \
47         union smb_search_data *d = find_level("SMB2_FIND_" #call_name); \
48         if (io.all_info2.out.field != d->stype.field) { \
49                 printf("(%s) %s/%s should be 0x%llx - 0x%llx\n", __location__, \
50                        #call_name, #field, \
51                        (long long)io.all_info2.out.field, (long long)d->stype.field); \
52                 ret = false; \
53         }} while (0)
54
55 #define CHECK_CONST_STRING(call_name, stype, field, str) do { \
56         union smb_search_data *d = find_level("SMB2_FIND_" #call_name); \
57         if (strcmp(str, d->stype.field.s) != 0) { \
58                 printf("(%s) %s/%s should be '%s' - '%s'\n", __location__, \
59                        #call_name, #field, \
60                        str, d->stype.field.s); \
61                 ret = false; \
62         }} while (0)
63
64 static union smb_search_data *find_level(const char *name)
65 {
66         int i;
67         for (i=0;i<ARRAY_SIZE(levels);i++) {
68                 if (strcmp(name, levels[i].name) == 0) {
69                         return &levels[i].data;
70                 }
71         }
72         return NULL;
73 }
74
75 /*
76   test find levels
77 */
78 static bool torture_smb2_find_levels(struct smb2_tree *tree)
79 {
80         struct smb2_handle handle;
81         NTSTATUS status;
82         int i;
83         struct smb2_find f;
84         bool ret = true;
85         union smb_fileinfo io;
86         const char *alt_name;
87
88         status = smb2_create_complex_file(tree, FNAME, &handle);
89         if (!NT_STATUS_IS_OK(status)) {
90                 return false;
91         }
92
93         io.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
94         io.generic.in.file.handle = handle;
95         status = smb2_getinfo_file(tree, tree, &io);
96         if (!NT_STATUS_IS_OK(status)) {
97                 return false;
98         }
99         alt_name = talloc_strdup(tree, io.alt_name_info.out.fname.s);   
100
101         io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
102         io.generic.in.file.handle = handle;
103         status = smb2_getinfo_file(tree, tree, &io);
104         if (!NT_STATUS_IS_OK(status)) {
105                 return false;
106         }
107
108         status = smb2_util_roothandle(tree, &handle);
109         if (!NT_STATUS_IS_OK(status)) {
110                 return false;
111         }
112
113         ZERO_STRUCT(f);
114         f.in.file.handle        = handle;
115         f.in.pattern            = FNAME;
116         f.in.continue_flags     = SMB2_CONTINUE_FLAG_RESTART;
117         f.in.max_response_size  = 0x10000;
118
119         for (i=0;i<ARRAY_SIZE(levels);i++) {
120                 union smb_search_data *d;
121                 uint_t count;
122
123                 f.in.level = levels[i].level - 0x100;
124
125                 levels[i].status = smb2_find_level(tree, tree, &f, &count, &d);
126                 if (!NT_STATUS_IS_OK(levels[i].status)) {
127                         printf("%s failed - %s\n", levels[i].name, 
128                                nt_errstr(levels[i].status));
129                 }
130
131                 if (count != 1) {
132                         printf("Expected count 1 - got %d in %s\n", count, levels[i].name);
133                         ret = false;
134                 }
135
136                 levels[i].data = d[0];
137         }
138
139         CHECK_VALUE(DIRECTORY_INFO, directory_info, create_time);
140         CHECK_VALUE(DIRECTORY_INFO, directory_info, access_time);
141         CHECK_VALUE(DIRECTORY_INFO, directory_info, write_time);
142         CHECK_VALUE(DIRECTORY_INFO, directory_info, change_time);
143         CHECK_VALUE(DIRECTORY_INFO, directory_info, size);
144         CHECK_VALUE(DIRECTORY_INFO, directory_info, alloc_size);
145         CHECK_VALUE(DIRECTORY_INFO, directory_info, attrib);
146         CHECK_CONST_STRING(DIRECTORY_INFO, directory_info, name, FNAME);
147
148         CHECK_VALUE(FULL_DIRECTORY_INFO, full_directory_info, create_time);
149         CHECK_VALUE(FULL_DIRECTORY_INFO, full_directory_info, access_time);
150         CHECK_VALUE(FULL_DIRECTORY_INFO, full_directory_info, write_time);
151         CHECK_VALUE(FULL_DIRECTORY_INFO, full_directory_info, change_time);
152         CHECK_VALUE(FULL_DIRECTORY_INFO, full_directory_info, size);
153         CHECK_VALUE(FULL_DIRECTORY_INFO, full_directory_info, alloc_size);
154         CHECK_VALUE(FULL_DIRECTORY_INFO, full_directory_info, attrib);
155         CHECK_VALUE(FULL_DIRECTORY_INFO, full_directory_info, ea_size);
156         CHECK_CONST_STRING(FULL_DIRECTORY_INFO, full_directory_info, name, FNAME);
157
158         CHECK_VALUE(BOTH_DIRECTORY_INFO, both_directory_info, create_time);
159         CHECK_VALUE(BOTH_DIRECTORY_INFO, both_directory_info, access_time);
160         CHECK_VALUE(BOTH_DIRECTORY_INFO, both_directory_info, write_time);
161         CHECK_VALUE(BOTH_DIRECTORY_INFO, both_directory_info, change_time);
162         CHECK_VALUE(BOTH_DIRECTORY_INFO, both_directory_info, size);
163         CHECK_VALUE(BOTH_DIRECTORY_INFO, both_directory_info, alloc_size);
164         CHECK_VALUE(BOTH_DIRECTORY_INFO, both_directory_info, attrib);
165         CHECK_VALUE(BOTH_DIRECTORY_INFO, both_directory_info, ea_size);
166         CHECK_CONST_STRING(BOTH_DIRECTORY_INFO, both_directory_info, short_name, alt_name);
167         CHECK_CONST_STRING(BOTH_DIRECTORY_INFO, both_directory_info, name, FNAME);
168
169         CHECK_VALUE(ID_FULL_DIRECTORY_INFO, id_full_directory_info, create_time);
170         CHECK_VALUE(ID_FULL_DIRECTORY_INFO, id_full_directory_info, access_time);
171         CHECK_VALUE(ID_FULL_DIRECTORY_INFO, id_full_directory_info, write_time);
172         CHECK_VALUE(ID_FULL_DIRECTORY_INFO, id_full_directory_info, change_time);
173         CHECK_VALUE(ID_FULL_DIRECTORY_INFO, id_full_directory_info, size);
174         CHECK_VALUE(ID_FULL_DIRECTORY_INFO, id_full_directory_info, alloc_size);
175         CHECK_VALUE(ID_FULL_DIRECTORY_INFO, id_full_directory_info, attrib);
176         CHECK_VALUE(ID_FULL_DIRECTORY_INFO, id_full_directory_info, ea_size);
177         CHECK_VALUE(ID_FULL_DIRECTORY_INFO, id_full_directory_info, file_id);
178         CHECK_CONST_STRING(ID_FULL_DIRECTORY_INFO, id_full_directory_info, name, FNAME);
179
180         CHECK_VALUE(ID_BOTH_DIRECTORY_INFO, id_both_directory_info, create_time);
181         CHECK_VALUE(ID_BOTH_DIRECTORY_INFO, id_both_directory_info, access_time);
182         CHECK_VALUE(ID_BOTH_DIRECTORY_INFO, id_both_directory_info, write_time);
183         CHECK_VALUE(ID_BOTH_DIRECTORY_INFO, id_both_directory_info, change_time);
184         CHECK_VALUE(ID_BOTH_DIRECTORY_INFO, id_both_directory_info, size);
185         CHECK_VALUE(ID_BOTH_DIRECTORY_INFO, id_both_directory_info, alloc_size);
186         CHECK_VALUE(ID_BOTH_DIRECTORY_INFO, id_both_directory_info, attrib);
187         CHECK_VALUE(ID_BOTH_DIRECTORY_INFO, id_both_directory_info, ea_size);
188         CHECK_VALUE(ID_BOTH_DIRECTORY_INFO, id_both_directory_info, file_id);
189         CHECK_CONST_STRING(ID_BOTH_DIRECTORY_INFO, id_both_directory_info, name, FNAME);
190
191
192         return ret;
193 }
194
195
196 /* basic testing of all SMB2 find levels
197 */
198 bool torture_smb2_find(struct torture_context *torture)
199 {
200         TALLOC_CTX *mem_ctx = talloc_new(NULL);
201         struct smb2_tree *tree;
202         bool ret = true;
203         NTSTATUS status;
204
205         if (!torture_smb2_connection(torture, &tree)) {
206                 return false;
207         }
208
209         status = torture_setup_complex_file(tree, FNAME);
210         if (!NT_STATUS_IS_OK(status)) {
211                 return false;
212         }
213         torture_setup_complex_file(tree, FNAME ":streamtwo");
214
215         ret &= torture_smb2_find_levels(tree);
216
217         talloc_free(mem_ctx);
218
219         return ret;
220 }