r3257: make the RAW-SEARCH test less sensitive to the servers directory ordering
[kamenim/samba.git] / source4 / torture / raw / search.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RAW_SEARCH_* individual test suite
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23
24 #define BASEDIR "\\testsearch"
25
26 /*
27   callback function for single_search
28 */
29 static BOOL single_search_callback(void *private, union smb_search_data *file)
30 {
31         union smb_search_data *data = private;
32
33         *data = *file;
34
35         return True;
36 }
37
38 /*
39   do a single file (non-wildcard) search 
40 */
41 static NTSTATUS single_search(struct smbcli_state *cli, 
42                               TALLOC_CTX *mem_ctx,
43                               const char *pattern,
44                               enum smb_search_level level,
45                               union smb_search_data *data)
46 {
47         union smb_search_first io;
48         union smb_search_close c;
49         NTSTATUS status;
50
51         io.generic.level = level;
52         if (level == RAW_SEARCH_SEARCH ||
53             level == RAW_SEARCH_FFIRST ||
54             level == RAW_SEARCH_FUNIQUE) {
55                 io.search_first.in.max_count = 1;
56                 io.search_first.in.search_attrib = 0;
57                 io.search_first.in.pattern = pattern;
58         } else {
59                 io.t2ffirst.in.search_attrib = 0;
60                 io.t2ffirst.in.max_count = 1;
61                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
62                 io.t2ffirst.in.storage_type = 0;
63                 io.t2ffirst.in.pattern = pattern;
64         }
65
66         status = smb_raw_search_first(cli->tree, mem_ctx,
67                                       &io, (void *)data, single_search_callback);
68
69         if (NT_STATUS_IS_OK(status) && level == RAW_SEARCH_FFIRST) {
70                 c.fclose.level = RAW_FINDCLOSE_FCLOSE;
71                 c.fclose.in.max_count = 1;
72                 c.fclose.in.search_attrib = 0;
73                 c.fclose.in.id = data->search.id;
74                 status = smb_raw_search_close(cli->tree, &c);
75         }
76         
77         return status;
78 }
79
80
81 static struct {
82         const char *name;
83         enum smb_search_level level;
84         uint32_t capability_mask;
85         NTSTATUS status;
86         union smb_search_data data;
87 } levels[] = {
88         {"FFIRST",                 RAW_SEARCH_FFIRST, },
89         {"FUNIQUE",                RAW_SEARCH_FUNIQUE, },
90         {"SEARCH",                 RAW_SEARCH_SEARCH, },
91         {"STANDARD",               RAW_SEARCH_STANDARD, },
92         {"EA_SIZE",                RAW_SEARCH_EA_SIZE, },
93         {"DIRECTORY_INFO",         RAW_SEARCH_DIRECTORY_INFO, },
94         {"FULL_DIRECTORY_INFO",    RAW_SEARCH_FULL_DIRECTORY_INFO, },
95         {"NAME_INFO",              RAW_SEARCH_NAME_INFO, },
96         {"BOTH_DIRECTORY_INFO",    RAW_SEARCH_BOTH_DIRECTORY_INFO, },
97         {"ID_FULL_DIRECTORY_INFO", RAW_SEARCH_ID_FULL_DIRECTORY_INFO, },
98         {"ID_BOTH_DIRECTORY_INFO", RAW_SEARCH_ID_BOTH_DIRECTORY_INFO, },
99         {"UNIX_INFO",              RAW_SEARCH_UNIX_INFO, CAP_UNIX}
100 };
101
102 /* find a level in the table by name */
103 static union smb_search_data *find(const char *name)
104 {
105         int i;
106         for (i=0;i<ARRAY_SIZE(levels);i++) {
107                 if (NT_STATUS_IS_OK(levels[i].status) && 
108                     strcmp(levels[i].name, name) == 0) {
109                         return &levels[i].data;
110                 }
111         }
112         return NULL;
113 }
114
115 /* 
116    basic testing of all RAW_SEARCH_* calls using a single file
117 */
118 static BOOL test_one_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
119 {
120         BOOL ret = True;
121         int fnum;
122         const char *fname = "\\torture_search.txt";
123         const char *fname2 = "\\torture_search-NOTEXIST.txt";
124         NTSTATUS status;
125         int i;
126         union smb_fileinfo all_info, alt_info, name_info, internal_info;
127         union smb_search_data *s;
128
129         printf("Testing one file searches\n");
130
131         fnum = create_complex_file(cli, mem_ctx, fname);
132         if (fnum == -1) {
133                 printf("ERROR: open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
134                 ret = False;
135                 goto done;
136         }
137
138         /* call all the levels */
139         for (i=0;i<ARRAY_SIZE(levels);i++) {
140                 NTSTATUS expected_status;
141                 uint32_t cap = cli->transport->negotiate.capabilities;
142
143                 printf("testing %s\n", levels[i].name);
144
145                 levels[i].status = single_search(cli, mem_ctx, fname, 
146                                                  levels[i].level, &levels[i].data);
147
148                 /* see if this server claims to support this level */
149                 if ((cap & levels[i].capability_mask) != levels[i].capability_mask) {
150                         printf("search level %s(%d) not supported by server\n",
151                                levels[i].name, (int)levels[i].level);
152                         continue;
153                 }
154
155                 if (!NT_STATUS_IS_OK(levels[i].status)) {
156                         printf("search level %s(%d) failed - %s\n",
157                                levels[i].name, (int)levels[i].level, 
158                                nt_errstr(levels[i].status));
159                         ret = False;
160                         continue;
161                 }
162
163                 status = single_search(cli, mem_ctx, fname2, 
164                                        levels[i].level, &levels[i].data);
165                 
166                 expected_status = NT_STATUS_NO_SUCH_FILE;
167                 if (levels[i].level == RAW_SEARCH_SEARCH ||
168                     levels[i].level == RAW_SEARCH_FFIRST ||
169                     levels[i].level == RAW_SEARCH_FUNIQUE) {
170                         expected_status = STATUS_NO_MORE_FILES;
171                 }
172                 if (!NT_STATUS_EQUAL(status, expected_status)) {
173                         printf("search level %s(%d) should fail with %s - %s\n",
174                                levels[i].name, (int)levels[i].level, 
175                                nt_errstr(expected_status),
176                                nt_errstr(status));
177                         ret = False;
178                 }
179         }
180
181         /* get the all_info file into to check against */
182         all_info.generic.level = RAW_FILEINFO_ALL_INFO;
183         all_info.generic.in.fname = fname;
184         status = smb_raw_pathinfo(cli->tree, mem_ctx, &all_info);
185         if (!NT_STATUS_IS_OK(status)) {
186                 printf("RAW_FILEINFO_ALL_INFO failed - %s\n", nt_errstr(status));
187                 ret = False;
188                 goto done;
189         }
190
191         alt_info.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
192         alt_info.generic.in.fname = fname;
193         status = smb_raw_pathinfo(cli->tree, mem_ctx, &alt_info);
194         if (!NT_STATUS_IS_OK(status)) {
195                 printf("RAW_FILEINFO_ALT_NAME_INFO failed - %s\n", nt_errstr(status));
196                 ret = False;
197                 goto done;
198         }
199
200         internal_info.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
201         internal_info.generic.in.fname = fname;
202         status = smb_raw_pathinfo(cli->tree, mem_ctx, &internal_info);
203         if (!NT_STATUS_IS_OK(status)) {
204                 printf("RAW_FILEINFO_INTERNAL_INFORMATION failed - %s\n", nt_errstr(status));
205                 ret = False;
206                 goto done;
207         }
208
209         name_info.generic.level = RAW_FILEINFO_NAME_INFO;
210         name_info.generic.in.fname = fname;
211         status = smb_raw_pathinfo(cli->tree, mem_ctx, &name_info);
212         if (!NT_STATUS_IS_OK(status)) {
213                 printf("RAW_FILEINFO_NAME_INFO failed - %s\n", nt_errstr(status));
214                 ret = False;
215                 goto done;
216         }
217
218 #define CHECK_VAL(name, sname1, field1, v, sname2, field2) do { \
219         s = find(name); \
220         if (s) { \
221                 if ((s->sname1.field1) != (v.sname2.out.field2)) { \
222                         printf("(%s) %s/%s [0x%x] != %s/%s [0x%x]\n", \
223                                __location__, \
224                                 #sname1, #field1, (int)s->sname1.field1, \
225                                 #sname2, #field2, (int)v.sname2.out.field2); \
226                         ret = False; \
227                 } \
228         }} while (0)
229
230 #define CHECK_TIME(name, sname1, field1, v, sname2, field2) do { \
231         s = find(name); \
232         if (s) { \
233                 if (s->sname1.field1 != (~1 & nt_time_to_unix(v.sname2.out.field2))) { \
234                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
235                                __location__, \
236                                 #sname1, #field1, timestring(mem_ctx, s->sname1.field1), \
237                                 #sname2, #field2, nt_time_string(mem_ctx, v.sname2.out.field2)); \
238                         ret = False; \
239                 } \
240         }} while (0)
241
242 #define CHECK_NTTIME(name, sname1, field1, v, sname2, field2) do { \
243         s = find(name); \
244         if (s) { \
245                 if (s->sname1.field1 != v.sname2.out.field2) { \
246                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
247                                __location__, \
248                                 #sname1, #field1, nt_time_string(mem_ctx, s->sname1.field1), \
249                                 #sname2, #field2, nt_time_string(mem_ctx, v.sname2.out.field2)); \
250                         ret = False; \
251                 } \
252         }} while (0)
253
254 #define CHECK_STR(name, sname1, field1, v, sname2, field2) do { \
255         s = find(name); \
256         if (s) { \
257                 if (!s->sname1.field1 || strcmp(s->sname1.field1, v.sname2.out.field2.s)) { \
258                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
259                                __location__, \
260                                 #sname1, #field1, s->sname1.field1, \
261                                 #sname2, #field2, v.sname2.out.field2.s); \
262                         ret = False; \
263                 } \
264         }} while (0)
265
266 #define CHECK_WSTR(name, sname1, field1, v, sname2, field2, flags) do { \
267         s = find(name); \
268         if (s) { \
269                 if (!s->sname1.field1.s || \
270                     strcmp(s->sname1.field1.s, v.sname2.out.field2.s) || \
271                     wire_bad_flags(&s->sname1.field1, flags, cli)) { \
272                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
273                                __location__, \
274                                 #sname1, #field1, s->sname1.field1.s, \
275                                 #sname2, #field2, v.sname2.out.field2.s); \
276                         ret = False; \
277                 } \
278         }} while (0)
279
280 #define CHECK_NAME(name, sname1, field1, fname, flags) do { \
281         s = find(name); \
282         if (s) { \
283                 if (!s->sname1.field1.s || \
284                     strcmp(s->sname1.field1.s, fname) || \
285                     wire_bad_flags(&s->sname1.field1, flags, cli)) { \
286                         printf("(%s) %s/%s [%s] != %s\n", \
287                                __location__, \
288                                 #sname1, #field1, s->sname1.field1.s, \
289                                 fname); \
290                         ret = False; \
291                 } \
292         }} while (0)
293
294 #define CHECK_UNIX_NAME(name, sname1, field1, fname, flags) do { \
295         s = find(name); \
296         if (s) { \
297                 if (!s->sname1.field1 || \
298                     strcmp(s->sname1.field1, fname)) { \
299                         printf("(%s) %s/%s [%s] != %s\n", \
300                                __location__, \
301                                 #sname1, #field1, s->sname1.field1, \
302                                 fname); \
303                         ret = False; \
304                 } \
305         }} while (0)
306         
307         /* check that all the results are as expected */
308         CHECK_VAL("SEARCH",              search,              attrib, all_info, all_info, attrib&0xFFF);
309         CHECK_VAL("STANDARD",            standard,            attrib, all_info, all_info, attrib&0xFFF);
310         CHECK_VAL("EA_SIZE",             ea_size,             attrib, all_info, all_info, attrib&0xFFF);
311         CHECK_VAL("DIRECTORY_INFO",      directory_info,      attrib, all_info, all_info, attrib);
312         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, attrib, all_info, all_info, attrib);
313         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, attrib, all_info, all_info, attrib);
314         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           attrib, all_info, all_info, attrib);
315         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           attrib, all_info, all_info, attrib);
316
317         CHECK_TIME("SEARCH",             search,              write_time, all_info, all_info, write_time);
318         CHECK_TIME("STANDARD",           standard,            write_time, all_info, all_info, write_time);
319         CHECK_TIME("EA_SIZE",            ea_size,             write_time, all_info, all_info, write_time);
320         CHECK_TIME("STANDARD",           standard,            create_time, all_info, all_info, create_time);
321         CHECK_TIME("EA_SIZE",            ea_size,             create_time, all_info, all_info, create_time);
322         CHECK_TIME("STANDARD",           standard,            access_time, all_info, all_info, access_time);
323         CHECK_TIME("EA_SIZE",            ea_size,             access_time, all_info, all_info, access_time);
324
325         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      write_time, all_info, all_info, write_time);
326         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, write_time, all_info, all_info, write_time);
327         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, write_time, all_info, all_info, write_time);
328         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           write_time, all_info, all_info, write_time);
329         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           write_time, all_info, all_info, write_time);
330
331         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      create_time, all_info, all_info, create_time);
332         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
333         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
334         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           create_time, all_info, all_info, create_time);
335         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           create_time, all_info, all_info, create_time);
336
337         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      access_time, all_info, all_info, access_time);
338         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, access_time, all_info, all_info, access_time);
339         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, access_time, all_info, all_info, access_time);
340         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           access_time, all_info, all_info, access_time);
341         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           access_time, all_info, all_info, access_time);
342
343         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      create_time, all_info, all_info, create_time);
344         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
345         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
346         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           create_time, all_info, all_info, create_time);
347         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           create_time, all_info, all_info, create_time);
348
349         CHECK_VAL("SEARCH",              search,              size, all_info, all_info, size);
350         CHECK_VAL("STANDARD",            standard,            size, all_info, all_info, size);
351         CHECK_VAL("EA_SIZE",             ea_size,             size, all_info, all_info, size);
352         CHECK_VAL("DIRECTORY_INFO",      directory_info,      size, all_info, all_info, size);
353         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, size, all_info, all_info, size);
354         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, size, all_info, all_info, size);
355         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           size, all_info, all_info, size);
356         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           size, all_info, all_info, size);
357         CHECK_VAL("UNIX_INFO",           unix_info,           size, all_info, all_info, size);
358
359         CHECK_VAL("STANDARD",            standard,            alloc_size, all_info, all_info, alloc_size);
360         CHECK_VAL("EA_SIZE",             ea_size,             alloc_size, all_info, all_info, alloc_size);
361         CHECK_VAL("DIRECTORY_INFO",      directory_info,      alloc_size, all_info, all_info, alloc_size);
362         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, alloc_size, all_info, all_info, alloc_size);
363         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, alloc_size, all_info, all_info, alloc_size);
364         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           alloc_size, all_info, all_info, alloc_size);
365         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           alloc_size, all_info, all_info, alloc_size);
366         CHECK_VAL("UNIX_INFO",           unix_info,           alloc_size, all_info, all_info, alloc_size);
367
368         CHECK_VAL("EA_SIZE",             ea_size,             ea_size, all_info, all_info, ea_size);
369         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, ea_size, all_info, all_info, ea_size);
370         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, ea_size, all_info, all_info, ea_size);
371         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           ea_size, all_info, all_info, ea_size);
372         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           ea_size, all_info, all_info, ea_size);
373
374         CHECK_STR("SEARCH", search, name, alt_info, alt_name_info, fname);
375         CHECK_WSTR("BOTH_DIRECTORY_INFO", both_directory_info, short_name, alt_info, alt_name_info, fname, STR_UNICODE);
376
377         CHECK_NAME("STANDARD",            standard,            name, fname+1, 0);
378         CHECK_NAME("EA_SIZE",             ea_size,             name, fname+1, 0);
379         CHECK_NAME("DIRECTORY_INFO",      directory_info,      name, fname+1, STR_TERMINATE_ASCII);
380         CHECK_NAME("FULL_DIRECTORY_INFO", full_directory_info, name, fname+1, STR_TERMINATE_ASCII);
381         CHECK_NAME("NAME_INFO",           name_info,           name, fname+1, STR_TERMINATE_ASCII);
382         CHECK_NAME("BOTH_DIRECTORY_INFO", both_directory_info, name, fname+1, STR_TERMINATE_ASCII);
383         CHECK_NAME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           name, fname+1, STR_TERMINATE_ASCII);
384         CHECK_NAME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           name, fname+1, STR_TERMINATE_ASCII);
385         CHECK_UNIX_NAME("UNIX_INFO",           unix_info,           name, fname+1, STR_TERMINATE_ASCII);
386
387         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, file_id, internal_info, internal_information, file_id);
388         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, file_id, internal_info, internal_information, file_id);
389
390 done:
391         smb_raw_exit(cli->session);
392         smbcli_unlink(cli->tree, fname);
393
394         return ret;
395 }
396
397
398 struct multiple_result {
399         TALLOC_CTX *mem_ctx;
400         int count;
401         union smb_search_data *list;
402 };
403
404 /*
405   callback function for multiple_search
406 */
407 static BOOL multiple_search_callback(void *private, union smb_search_data *file)
408 {
409         struct multiple_result *data = private;
410
411
412         data->count++;
413         data->list = talloc_realloc_p(data->mem_ctx,
414                                       data->list, 
415                                       union smb_search_data,
416                                       data->count);
417
418         data->list[data->count-1] = *file;
419
420         return True;
421 }
422
423 enum continue_type {CONT_FLAGS, CONT_NAME, CONT_RESUME_KEY};
424
425 /*
426   do a single file (non-wildcard) search 
427 */
428 static NTSTATUS multiple_search(struct smbcli_state *cli, 
429                                 TALLOC_CTX *mem_ctx,
430                                 const char *pattern,
431                                 enum smb_search_level level,
432                                 enum continue_type cont_type,
433                                 void *data)
434 {
435         union smb_search_first io;
436         union smb_search_next io2;
437         NTSTATUS status;
438         const int per_search = 300;
439         struct multiple_result *result = data;
440
441         io.generic.level = level;
442         if (level == RAW_SEARCH_SEARCH) {
443                 io.search_first.in.max_count = per_search;
444                 io.search_first.in.search_attrib = 0;
445                 io.search_first.in.pattern = pattern;
446         } else {
447                 io.t2ffirst.in.search_attrib = 0;
448                 io.t2ffirst.in.max_count = per_search;
449                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
450                 io.t2ffirst.in.storage_type = 0;
451                 io.t2ffirst.in.pattern = pattern;
452                 if (cont_type == CONT_RESUME_KEY) {
453                         io.t2ffirst.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME | 
454                                 FLAG_TRANS2_FIND_BACKUP_INTENT;
455                 }
456         }
457
458         status = smb_raw_search_first(cli->tree, mem_ctx,
459                                       &io, data, multiple_search_callback);
460         
461
462         while (NT_STATUS_IS_OK(status)) {
463                 io2.generic.level = level;
464                 if (level == RAW_SEARCH_SEARCH) {
465                         io2.search_next.in.max_count = per_search;
466                         io2.search_next.in.search_attrib = 0;
467                         io2.search_next.in.id = result->list[result->count-1].search.id;
468                 } else {
469                         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
470                         io2.t2fnext.in.max_count = per_search;
471                         io2.t2fnext.in.resume_key = 0;
472                         io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
473                         io2.t2fnext.in.last_name = "";
474                         switch (cont_type) {
475                         case CONT_RESUME_KEY:
476                                 if (level == RAW_SEARCH_STANDARD) {
477                                         io2.t2fnext.in.resume_key = 
478                                                 result->list[result->count-1].standard.resume_key;
479                                 } else if (level == RAW_SEARCH_EA_SIZE) {
480                                         io2.t2fnext.in.resume_key = 
481                                                 result->list[result->count-1].ea_size.resume_key;
482                                 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
483                                         io2.t2fnext.in.resume_key = 
484                                                 result->list[result->count-1].directory_info.file_index;
485                                 } else {
486                                         io2.t2fnext.in.resume_key = 
487                                                 result->list[result->count-1].both_directory_info.file_index;
488                                 }
489                                 if (io2.t2fnext.in.resume_key == 0) {
490                                         printf("Server does not support resume by key\n");
491                                         return NT_STATUS_NOT_SUPPORTED;
492                                 }
493                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
494                                         FLAG_TRANS2_FIND_BACKUP_INTENT;
495                                 break;
496                         case CONT_NAME:
497                                 if (level == RAW_SEARCH_STANDARD) {
498                                         io2.t2fnext.in.last_name = 
499                                                 result->list[result->count-1].standard.name.s;
500                                 } else if (level == RAW_SEARCH_EA_SIZE) {
501                                         io2.t2fnext.in.last_name = 
502                                                 result->list[result->count-1].ea_size.name.s;
503                                 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
504                                         io2.t2fnext.in.last_name = 
505                                                 result->list[result->count-1].directory_info.name.s;
506                                 } else {
507                                         io2.t2fnext.in.last_name = 
508                                                 result->list[result->count-1].both_directory_info.name.s;
509                                 }
510                                 break;
511                         case CONT_FLAGS:
512                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_CONTINUE;
513                                 break;
514                         }
515                 }
516
517                 status = smb_raw_search_next(cli->tree, mem_ctx,
518                                              &io2, data, multiple_search_callback);
519                 if (!NT_STATUS_IS_OK(status)) {
520                         break;
521                 }
522                 if (level == RAW_SEARCH_SEARCH) {
523                         if (io2.search_next.out.count == 0) {
524                                 break;
525                         }
526                 } else if (io2.t2fnext.out.count == 0 ||
527                            io2.t2fnext.out.end_of_search) {
528                         break;
529                 }
530         }
531
532         return status;
533 }
534
535 #define CHECK_STATUS(status, correct) do { \
536         if (!NT_STATUS_EQUAL(status, correct)) { \
537                 printf("(%s) Incorrect status %s - should be %s\n", \
538                        __location__, nt_errstr(status), nt_errstr(correct)); \
539                 ret = False; \
540                 goto done; \
541         }} while (0)
542
543 #define CHECK_VALUE(v, correct) do { \
544         if ((v) != (correct)) { \
545                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
546                        __location__, #v, v, correct); \
547                 ret = False; \
548         }} while (0)
549
550
551 static int search_both_compare(union smb_search_data *d1, union smb_search_data *d2)
552 {
553         return strcmp_safe(d1->both_directory_info.name.s, d2->both_directory_info.name.s);
554 }
555
556 static int search_standard_compare(union smb_search_data *d1, union smb_search_data *d2)
557 {
558         return strcmp_safe(d1->standard.name.s, d2->standard.name.s);
559 }
560
561 static int search_ea_size_compare(union smb_search_data *d1, union smb_search_data *d2)
562 {
563         return strcmp_safe(d1->ea_size.name.s, d2->ea_size.name.s);
564 }
565
566 static int search_directory_info_compare(union smb_search_data *d1, union smb_search_data *d2)
567 {
568         return strcmp_safe(d1->directory_info.name.s, d2->directory_info.name.s);
569 }
570
571 static int search_old_compare(union smb_search_data *d1, union smb_search_data *d2)
572 {
573         return strcmp_safe(d1->search.name, d2->search.name);
574 }
575
576
577 /* 
578    basic testing of search calls using many files
579 */
580 static BOOL test_many_files(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
581 {
582         const int num_files = 700;
583         int i, fnum, t;
584         char *fname;
585         BOOL ret = True;
586         NTSTATUS status;
587         struct multiple_result result;
588         struct {
589                 const char *name;
590                 const char *cont_name;
591                 enum smb_search_level level;
592                 enum continue_type cont_type;
593         } search_types[] = {
594                 {"SEARCH",              "ID",    RAW_SEARCH_SEARCH,              CONT_RESUME_KEY},
595                 {"BOTH_DIRECTORY_INFO", "NAME",  RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_NAME},
596                 {"BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_FLAGS},
597                 {"BOTH_DIRECTORY_INFO", "KEY",   RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY},
598                 {"STANDARD",            "FLAGS", RAW_SEARCH_STANDARD,            CONT_FLAGS},
599                 {"STANDARD",            "KEY",   RAW_SEARCH_STANDARD,            CONT_RESUME_KEY},
600                 {"STANDARD",            "NAME",  RAW_SEARCH_STANDARD,            CONT_NAME},
601                 {"EA_SIZE",             "FLAGS", RAW_SEARCH_EA_SIZE,             CONT_FLAGS},
602                 {"EA_SIZE",             "KEY",   RAW_SEARCH_EA_SIZE,             CONT_RESUME_KEY},
603                 {"EA_SIZE",             "NAME",  RAW_SEARCH_EA_SIZE,             CONT_NAME},
604                 {"DIRECTORY_INFO",      "FLAGS", RAW_SEARCH_DIRECTORY_INFO,      CONT_FLAGS},
605                 {"DIRECTORY_INFO",      "KEY",   RAW_SEARCH_DIRECTORY_INFO,      CONT_RESUME_KEY},
606                 {"DIRECTORY_INFO",      "NAME",  RAW_SEARCH_DIRECTORY_INFO,      CONT_NAME}
607         };
608
609         if (smbcli_deltree(cli->tree, BASEDIR) == -1 || 
610             NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
611                 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
612                 return False;
613         }
614
615         printf("Creating %d files\n", num_files);
616
617         for (i=0;i<num_files;i++) {
618                 asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
619                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
620                 if (fnum == -1) {
621                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
622                         ret = False;
623                         goto done;
624                 }
625                 free(fname);
626                 smbcli_close(cli->tree, fnum);
627         }
628
629
630         for (t=0;t<ARRAY_SIZE(search_types);t++) {
631                 ZERO_STRUCT(result);
632                 result.mem_ctx = talloc(mem_ctx, 0);
633         
634                 printf("Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
635
636                 status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*", 
637                                          search_types[t].level,
638                                          search_types[t].cont_type,
639                                          &result);
640         
641                 if (!NT_STATUS_IS_OK(status)) {
642                         printf("search failed - %s\n", nt_errstr(status));
643                         ret = False;
644                         continue;
645                 }
646                 CHECK_VALUE(result.count, num_files);
647
648                 if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
649                         qsort(result.list, result.count, sizeof(result.list[0]), 
650                               QSORT_CAST  search_both_compare);
651                 } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
652                         qsort(result.list, result.count, sizeof(result.list[0]), 
653                               QSORT_CAST search_standard_compare);
654                 } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
655                         qsort(result.list, result.count, sizeof(result.list[0]), 
656                               QSORT_CAST search_ea_size_compare);
657                 } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
658                         qsort(result.list, result.count, sizeof(result.list[0]), 
659                               QSORT_CAST search_directory_info_compare);
660                 } else {
661                         qsort(result.list, result.count, sizeof(result.list[0]), 
662                               QSORT_CAST search_old_compare);
663                 }
664
665                 for (i=0;i<num_files;i++) {
666                         const char *s;
667                         if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
668                                 s = result.list[i].both_directory_info.name.s;
669                         } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
670                                 s = result.list[i].standard.name.s;
671                         } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
672                                 s = result.list[i].ea_size.name.s;
673                         } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
674                                 s = result.list[i].directory_info.name.s;
675                         } else {
676                                 s = result.list[i].search.name;
677                         }
678                         asprintf(&fname, "t%03d-%d.txt", i, i);
679                         if (strcmp(fname, s)) {
680                                 printf("Incorrect name %s at entry %d\n", s, i);
681                                 ret = False;
682                                 break;
683                         }
684                         free(fname);
685                 }
686                 talloc_free(result.mem_ctx);
687         }
688
689 done:
690         smb_raw_exit(cli->session);
691         smbcli_deltree(cli->tree, BASEDIR);
692
693         return ret;
694 }
695
696 /*
697   check a individual file result
698 */
699 static BOOL check_result(struct multiple_result *result, const char *name, BOOL exist, uint32_t attrib)
700 {
701         int i;
702         for (i=0;i<result->count;i++) {
703                 if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
704         }
705         if (i == result->count) {
706                 if (exist) {
707                         printf("failed: '%s' should exist with attribute %s\n", 
708                                name, attrib_string(result->list, attrib));
709                         return False;
710                 }
711                 return True;
712         }
713
714         if (!exist) {
715                 printf("failed: '%s' should NOT exist (has attribute %s)\n", 
716                        name, attrib_string(result->list, result->list[i].both_directory_info.attrib));
717                 return False;
718         }
719
720         if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
721                 printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
722                        name, 
723                        attrib, result->list[i].both_directory_info.attrib);
724                 return False;
725         }
726         return True;
727 }
728
729 /* 
730    test what happens when the directory is modified during a search
731 */
732 static BOOL test_modify_search(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
733 {
734         const int num_files = 20;
735         int i, fnum;
736         char *fname;
737         BOOL ret = True;
738         NTSTATUS status;
739         struct multiple_result result;
740         union smb_search_first io;
741         union smb_search_next io2;
742         union smb_setfileinfo sfinfo;
743
744         if (smbcli_deltree(cli->tree, BASEDIR) == -1 || 
745             NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
746                 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
747                 return False;
748         }
749
750         printf("Creating %d files\n", num_files);
751
752         for (i=num_files-1;i>=0;i--) {
753                 asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
754                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
755                 if (fnum == -1) {
756                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
757                         ret = False;
758                         goto done;
759                 }
760                 free(fname);
761                 smbcli_close(cli->tree, fnum);
762         }
763
764         printf("pulling the first 2 files\n");
765         ZERO_STRUCT(result);
766         result.mem_ctx = talloc(mem_ctx, 0);
767
768         io.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
769         io.t2ffirst.in.search_attrib = 0;
770         io.t2ffirst.in.max_count = 0;
771         io.t2ffirst.in.flags = 0;
772         io.t2ffirst.in.storage_type = 0;
773         io.t2ffirst.in.pattern = BASEDIR "\\*.*";
774
775         status = smb_raw_search_first(cli->tree, mem_ctx,
776                                       &io, &result, multiple_search_callback);
777         CHECK_STATUS(status, NT_STATUS_OK);
778         CHECK_VALUE(result.count, 1);
779         
780         io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
781         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
782         io2.t2fnext.in.max_count = 1;
783         io2.t2fnext.in.resume_key = 0;
784         io2.t2fnext.in.flags = 0;
785         if (result.count == 0) {
786                 io2.t2fnext.in.last_name = "";
787         } else {
788                 io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
789         }
790
791         status = smb_raw_search_next(cli->tree, mem_ctx,
792                                      &io2, &result, multiple_search_callback);
793         CHECK_STATUS(status, NT_STATUS_OK);
794         CHECK_VALUE(result.count, 2);
795
796         printf("Changing attributes and deleting\n");
797         smbcli_open(cli->tree, BASEDIR "\\T003-03.txt.2", O_CREAT|O_RDWR, DENY_NONE);
798         smbcli_open(cli->tree, BASEDIR "\\T013-13.txt.2", O_CREAT|O_RDWR, DENY_NONE);
799         fnum = create_complex_file(cli, mem_ctx, BASEDIR "\\T013-13.txt.3");
800         smbcli_unlink(cli->tree, BASEDIR "\\T014-14.txt");
801         torture_set_file_attribute(cli->tree, BASEDIR "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
802         torture_set_file_attribute(cli->tree, BASEDIR "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
803         torture_set_file_attribute(cli->tree, BASEDIR "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);  
804         sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
805         sfinfo.generic.file.fnum = fnum;
806         sfinfo.disposition_info.in.delete_on_close = 1;
807         status = smb_raw_setfileinfo(cli->tree, &sfinfo);
808         CHECK_STATUS(status, NT_STATUS_OK);
809
810         io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
811         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
812         io2.t2fnext.in.max_count = num_files - 2;
813         io2.t2fnext.in.resume_key = 0;
814         io2.t2fnext.in.flags = 0;
815         io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
816
817         status = smb_raw_search_next(cli->tree, mem_ctx,
818                                      &io2, &result, multiple_search_callback);
819         CHECK_STATUS(status, NT_STATUS_OK);
820         CHECK_VALUE(result.count, 20);
821
822         ret &= check_result(&result, "t009-9.txt", True, FILE_ATTRIBUTE_ARCHIVE);
823         ret &= check_result(&result, "t014-14.txt", False, 0);
824         ret &= check_result(&result, "t015-15.txt", False, 0);
825         ret &= check_result(&result, "t016-16.txt", True, FILE_ATTRIBUTE_NORMAL);
826         ret &= check_result(&result, "t017-17.txt", False, 0);
827         ret &= check_result(&result, "t018-18.txt", True, FILE_ATTRIBUTE_ARCHIVE);
828         ret &= check_result(&result, "t019-19.txt", True, FILE_ATTRIBUTE_ARCHIVE);
829         ret &= check_result(&result, "T013-13.txt.2", True, FILE_ATTRIBUTE_ARCHIVE);
830         ret &= check_result(&result, "T003-3.txt.2", False, 0);
831         ret &= check_result(&result, "T013-13.txt.3", True, FILE_ATTRIBUTE_ARCHIVE);
832
833         if (!ret) {
834                 for (i=0;i<result.count;i++) {
835                         printf("%s %s (0x%x)\n", 
836                                result.list[i].both_directory_info.name.s, 
837                                attrib_string(mem_ctx, result.list[i].both_directory_info.attrib),
838                                result.list[i].both_directory_info.attrib);
839                 }
840         }
841         exit(1);
842
843 done:
844         smb_raw_exit(cli->session);
845         smbcli_deltree(cli->tree, BASEDIR);
846
847         return ret;
848 }
849
850
851 /* 
852    testing if directories always come back sorted
853 */
854 static BOOL test_sorted(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
855 {
856         const int num_files = 700;
857         int i, fnum;
858         char *fname;
859         BOOL ret = True;
860         NTSTATUS status;
861         struct multiple_result result;
862
863         if (smbcli_deltree(cli->tree, BASEDIR) == -1 || 
864             NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
865                 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
866                 return False;
867         }
868
869         printf("Creating %d files\n", num_files);
870
871         for (i=0;i<num_files;i++) {
872                 asprintf(&fname, BASEDIR "\\%s.txt", generate_random_str(mem_ctx, 10));
873                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
874                 if (fnum == -1) {
875                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
876                         ret = False;
877                         goto done;
878                 }
879                 free(fname);
880                 smbcli_close(cli->tree, fnum);
881         }
882
883
884         ZERO_STRUCT(result);
885         result.mem_ctx = mem_ctx;
886         
887         status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*", 
888                                  RAW_SEARCH_BOTH_DIRECTORY_INFO,
889                                  CONT_NAME, &result);   
890         CHECK_STATUS(status, NT_STATUS_OK);
891         CHECK_VALUE(result.count, num_files);
892
893         for (i=0;i<num_files-1;i++) {
894                 const char *name1, *name2;
895                 name1 = result.list[i].both_directory_info.name.s;
896                 name2 = result.list[i+1].both_directory_info.name.s;
897                 if (StrCaseCmp(name1, name2) > 0) {
898                         printf("non-alphabetical order at entry %d  '%s' '%s'\n", 
899                                i, name1, name2);
900                         printf("Server does not produce sorted directory listings (not an error)\n");
901                         goto done;
902                 }
903         }
904
905         talloc_free(result.list);
906
907 done:
908         smb_raw_exit(cli->session);
909         smbcli_deltree(cli->tree, BASEDIR);
910
911         return ret;
912 }
913
914
915 /* 
916    basic testing of all RAW_SEARCH_* calls using a single file
917 */
918 BOOL torture_raw_search(int dummy)
919 {
920         struct smbcli_state *cli;
921         BOOL ret = True;
922         TALLOC_CTX *mem_ctx;
923
924         if (!torture_open_connection(&cli)) {
925                 return False;
926         }
927
928         mem_ctx = talloc_init("torture_search");
929
930         if (!test_one_file(cli, mem_ctx)) {
931                 ret = False;
932         }
933
934         if (!test_many_files(cli, mem_ctx)) {
935                 ret = False;
936         }
937
938         if (!test_sorted(cli, mem_ctx)) {
939                 ret = False;
940         }
941
942         if (!test_modify_search(cli, mem_ctx)) {
943                 ret = False;
944         }
945
946         torture_close_connection(cli);
947         talloc_destroy(mem_ctx);
948         
949         return ret;
950 }