r2520: - finished implementing the server side of the old style search requests
[gd/samba-autobuild/.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("(%d) %s/%s [0x%x] != %s/%s [0x%x]\n", \
223                                __LINE__, \
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("(%d) %s/%s [%s] != %s/%s [%s]\n", \
235                                __LINE__, \
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("(%d) %s/%s [%s] != %s/%s [%s]\n", \
247                                __LINE__, \
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("(%d) %s/%s [%s] != %s/%s [%s]\n", \
259                                __LINE__, \
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("(%d) %s/%s [%s] != %s/%s [%s]\n", \
273                                __LINE__, \
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("(%d) %s/%s [%s] != %s\n", \
287                                __LINE__, \
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("(%d) %s/%s [%s] != %s\n", \
300                                __LINE__, \
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(data->list, 
414                                     data->count * (sizeof(data->list[0])));
415
416         data->list[data->count-1] = *file;
417
418         return True;
419 }
420
421 enum continue_type {CONT_FLAGS, CONT_NAME, CONT_RESUME_KEY};
422
423 /*
424   do a single file (non-wildcard) search 
425 */
426 static NTSTATUS multiple_search(struct smbcli_state *cli, 
427                                 TALLOC_CTX *mem_ctx,
428                                 const char *pattern,
429                                 enum smb_search_level level,
430                                 enum continue_type cont_type,
431                                 void *data)
432 {
433         union smb_search_first io;
434         union smb_search_next io2;
435         NTSTATUS status;
436         const int per_search = 300;
437         struct multiple_result *result = data;
438
439         io.generic.level = level;
440         if (level == RAW_SEARCH_SEARCH) {
441                 io.search_first.in.max_count = per_search;
442                 io.search_first.in.search_attrib = 0;
443                 io.search_first.in.pattern = pattern;
444         } else {
445                 io.t2ffirst.in.search_attrib = 0;
446                 io.t2ffirst.in.max_count = per_search;
447                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
448                 io.t2ffirst.in.storage_type = 0;
449                 io.t2ffirst.in.pattern = pattern;
450                 if (cont_type == CONT_RESUME_KEY) {
451                         io.t2ffirst.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME | 
452                                 FLAG_TRANS2_FIND_BACKUP_INTENT;
453                 }
454         }
455
456         status = smb_raw_search_first(cli->tree, mem_ctx,
457                                       &io, data, multiple_search_callback);
458         
459
460         while (NT_STATUS_IS_OK(status)) {
461                 io2.generic.level = level;
462                 if (level == RAW_SEARCH_SEARCH) {
463                         io2.search_next.in.max_count = per_search;
464                         io2.search_next.in.search_attrib = 0;
465                         io2.search_next.in.id = result->list[result->count-1].search.id;
466                 } else {
467                         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
468                         io2.t2fnext.in.max_count = per_search;
469                         io2.t2fnext.in.resume_key = 0;
470                         io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
471                         io2.t2fnext.in.last_name = "";
472                         switch (cont_type) {
473                         case CONT_RESUME_KEY:
474                                 if (level == RAW_SEARCH_STANDARD) {
475                                         io2.t2fnext.in.resume_key = 
476                                                 result->list[result->count-1].standard.resume_key;
477                                 } else if (level == RAW_SEARCH_EA_SIZE) {
478                                         io2.t2fnext.in.resume_key = 
479                                                 result->list[result->count-1].ea_size.resume_key;
480                                 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
481                                         io2.t2fnext.in.resume_key = 
482                                                 result->list[result->count-1].directory_info.file_index;
483                                 } else {
484                                         io2.t2fnext.in.resume_key = 
485                                                 result->list[result->count-1].both_directory_info.file_index;
486                                 }
487                                 if (io2.t2fnext.in.resume_key == 0) {
488                                         printf("Server does not support resume by key\n");
489                                         return NT_STATUS_NOT_SUPPORTED;
490                                 }
491                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
492                                         FLAG_TRANS2_FIND_BACKUP_INTENT;
493                                 break;
494                         case CONT_NAME:
495                                 if (level == RAW_SEARCH_STANDARD) {
496                                         io2.t2fnext.in.last_name = 
497                                                 result->list[result->count-1].standard.name.s;
498                                 } else if (level == RAW_SEARCH_EA_SIZE) {
499                                         io2.t2fnext.in.last_name = 
500                                                 result->list[result->count-1].ea_size.name.s;
501                                 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
502                                         io2.t2fnext.in.last_name = 
503                                                 result->list[result->count-1].directory_info.name.s;
504                                 } else {
505                                         io2.t2fnext.in.last_name = 
506                                                 result->list[result->count-1].both_directory_info.name.s;
507                                 }
508                                 break;
509                         case CONT_FLAGS:
510                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_CONTINUE;
511                                 break;
512                         }
513                 }
514
515                 status = smb_raw_search_next(cli->tree, mem_ctx,
516                                              &io2, data, multiple_search_callback);
517                 if (!NT_STATUS_IS_OK(status)) {
518                         break;
519                 }
520                 if (level == RAW_SEARCH_SEARCH) {
521                         if (io2.search_next.out.count == 0) {
522                                 break;
523                         }
524                 } else if (io2.t2fnext.out.count == 0 ||
525                            io2.t2fnext.out.end_of_search) {
526                         break;
527                 }
528         }
529
530         return status;
531 }
532
533 #define CHECK_STATUS(status, correct) do { \
534         if (!NT_STATUS_EQUAL(status, correct)) { \
535                 printf("(%d) Incorrect status %s - should be %s\n", \
536                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
537                 ret = False; \
538                 goto done; \
539         }} while (0)
540
541 #define CHECK_VALUE(v, correct) do { \
542         if ((v) != (correct)) { \
543                 printf("(%d) Incorrect value %s=%d - should be %d\n", \
544                        __LINE__, #v, v, correct); \
545                 ret = False; \
546         }} while (0)
547
548
549 static int search_both_compare(union smb_search_data *d1, union smb_search_data *d2)
550 {
551         return strcmp_safe(d1->both_directory_info.name.s, d2->both_directory_info.name.s);
552 }
553
554 static int search_standard_compare(union smb_search_data *d1, union smb_search_data *d2)
555 {
556         return strcmp_safe(d1->standard.name.s, d2->standard.name.s);
557 }
558
559 static int search_ea_size_compare(union smb_search_data *d1, union smb_search_data *d2)
560 {
561         return strcmp_safe(d1->ea_size.name.s, d2->ea_size.name.s);
562 }
563
564 static int search_directory_info_compare(union smb_search_data *d1, union smb_search_data *d2)
565 {
566         return strcmp_safe(d1->directory_info.name.s, d2->directory_info.name.s);
567 }
568
569 static int search_old_compare(union smb_search_data *d1, union smb_search_data *d2)
570 {
571         return strcmp_safe(d1->search.name, d2->search.name);
572 }
573
574
575 /* 
576    basic testing of search calls using many files
577 */
578 static BOOL test_many_files(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
579 {
580         const int num_files = 700;
581         int i, fnum, t;
582         char *fname;
583         BOOL ret = True;
584         NTSTATUS status;
585         struct multiple_result result;
586         struct {
587                 const char *name;
588                 const char *cont_name;
589                 enum smb_search_level level;
590                 enum continue_type cont_type;
591         } search_types[] = {
592                 {"SEARCH",              "ID",    RAW_SEARCH_SEARCH,              CONT_RESUME_KEY},
593                 {"BOTH_DIRECTORY_INFO", "NAME",  RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_NAME},
594                 {"BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_FLAGS},
595                 {"BOTH_DIRECTORY_INFO", "KEY",   RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY},
596                 {"STANDARD",            "FLAGS", RAW_SEARCH_STANDARD,            CONT_FLAGS},
597                 {"STANDARD",            "KEY",   RAW_SEARCH_STANDARD,            CONT_RESUME_KEY},
598                 {"STANDARD",            "NAME",  RAW_SEARCH_STANDARD,            CONT_NAME},
599                 {"EA_SIZE",             "FLAGS", RAW_SEARCH_EA_SIZE,             CONT_FLAGS},
600                 {"EA_SIZE",             "KEY",   RAW_SEARCH_EA_SIZE,             CONT_RESUME_KEY},
601                 {"EA_SIZE",             "NAME",  RAW_SEARCH_EA_SIZE,             CONT_NAME},
602                 {"DIRECTORY_INFO",      "FLAGS", RAW_SEARCH_DIRECTORY_INFO,      CONT_FLAGS},
603                 {"DIRECTORY_INFO",      "KEY",   RAW_SEARCH_DIRECTORY_INFO,      CONT_RESUME_KEY},
604                 {"DIRECTORY_INFO",      "NAME",  RAW_SEARCH_DIRECTORY_INFO,      CONT_NAME}
605         };
606
607         if (smbcli_deltree(cli->tree, BASEDIR) == -1 || 
608             NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
609                 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
610                 return False;
611         }
612
613         printf("Creating %d files\n", num_files);
614
615         for (i=0;i<num_files;i++) {
616                 asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
617                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
618                 if (fnum == -1) {
619                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
620                         ret = False;
621                         goto done;
622                 }
623                 free(fname);
624                 smbcli_close(cli->tree, fnum);
625         }
626
627
628         for (t=0;t<ARRAY_SIZE(search_types);t++) {
629                 ZERO_STRUCT(result);
630                 result.mem_ctx = mem_ctx;
631         
632                 printf("Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
633
634                 status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*", 
635                                          search_types[t].level,
636                                          search_types[t].cont_type,
637                                          &result);
638         
639                 if (!NT_STATUS_IS_OK(status)) {
640                         printf("search failed - %s\n", nt_errstr(status));
641                         ret = False;
642                         continue;
643                 }
644                 CHECK_VALUE(result.count, num_files);
645
646                 if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
647                         qsort(result.list, result.count, sizeof(result.list[0]), 
648                               QSORT_CAST  search_both_compare);
649                 } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
650                         qsort(result.list, result.count, sizeof(result.list[0]), 
651                               QSORT_CAST search_standard_compare);
652                 } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
653                         qsort(result.list, result.count, sizeof(result.list[0]), 
654                               QSORT_CAST search_ea_size_compare);
655                 } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
656                         qsort(result.list, result.count, sizeof(result.list[0]), 
657                               QSORT_CAST search_directory_info_compare);
658                 } else {
659                         qsort(result.list, result.count, sizeof(result.list[0]), 
660                               QSORT_CAST search_old_compare);
661                 }
662
663                 for (i=0;i<num_files;i++) {
664                         const char *s;
665                         if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
666                                 s = result.list[i].both_directory_info.name.s;
667                         } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
668                                 s = result.list[i].standard.name.s;
669                         } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
670                                 s = result.list[i].ea_size.name.s;
671                         } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
672                                 s = result.list[i].directory_info.name.s;
673                         } else {
674                                 s = result.list[i].search.name;
675                         }
676                         asprintf(&fname, "t%03d-%d.txt", i, i);
677                         if (strcmp(fname, s)) {
678                                 printf("Incorrect name %s at entry %d\n", s, i);
679                                 ret = False;
680                                 break;
681                         }
682                         free(fname);
683                 }
684         }
685
686 done:
687         smb_raw_exit(cli->session);
688         smbcli_deltree(cli->tree, BASEDIR);
689
690         return ret;
691 }
692
693 /*
694   check a individual file result
695 */
696 static BOOL check_result(struct multiple_result *result, const char *name, BOOL exist, uint32_t attrib)
697 {
698         int i;
699         for (i=0;i<result->count;i++) {
700                 if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
701         }
702         if (i == result->count) {
703                 if (exist) {
704                         printf("failed: '%s' should exist with attribute %s\n", 
705                                name, attrib_string(NULL, attrib));
706                         return False;
707                 }
708                 return True;
709         }
710
711         if (!exist) {
712                 printf("failed: '%s' should NOT exist (has attribute %s)\n", 
713                        name, attrib_string(NULL, result->list[i].both_directory_info.attrib));
714                 return False;
715         }
716
717         if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
718                 printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
719                        name, 
720                        attrib, result->list[i].both_directory_info.attrib);
721                 return False;
722         }
723         return True;
724 }
725
726 /* 
727    test what happens when the directory is modified during a search
728 */
729 static BOOL test_modify_search(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
730 {
731         const int num_files = 20;
732         int i, fnum;
733         char *fname;
734         BOOL ret = True;
735         NTSTATUS status;
736         struct multiple_result result;
737         union smb_search_first io;
738         union smb_search_next io2;
739         union smb_setfileinfo sfinfo;
740
741         if (smbcli_deltree(cli->tree, BASEDIR) == -1 || 
742             NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
743                 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
744                 return False;
745         }
746
747         printf("Creating %d files\n", num_files);
748
749         for (i=0;i<num_files;i++) {
750                 asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
751                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
752                 if (fnum == -1) {
753                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
754                         ret = False;
755                         goto done;
756                 }
757                 free(fname);
758                 smbcli_close(cli->tree, fnum);
759         }
760
761         printf("pulling the first 10 files\n");
762         ZERO_STRUCT(result);
763
764         io.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
765         io.t2ffirst.in.search_attrib = 0;
766         io.t2ffirst.in.max_count = 0;
767         io.t2ffirst.in.flags = 0;
768         io.t2ffirst.in.storage_type = 0;
769         io.t2ffirst.in.pattern = BASEDIR "\\*.*";
770
771         status = smb_raw_search_first(cli->tree, mem_ctx,
772                                       &io, &result, multiple_search_callback);
773         CHECK_STATUS(status, NT_STATUS_OK);
774         CHECK_VALUE(result.count, 1);
775
776         io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
777         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
778         io2.t2fnext.in.max_count = num_files/2 - 1;
779         io2.t2fnext.in.resume_key = 0;
780         io2.t2fnext.in.flags = 0;
781         io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
782
783         status = smb_raw_search_next(cli->tree, mem_ctx,
784                                      &io2, &result, multiple_search_callback);
785         CHECK_STATUS(status, NT_STATUS_OK);
786         CHECK_VALUE(result.count, num_files/2);
787
788         printf("Changing attributes and deleting\n");
789         smbcli_open(cli->tree, BASEDIR "\\T003-03.txt.2", O_CREAT|O_RDWR, DENY_NONE);
790         smbcli_open(cli->tree, BASEDIR "\\T013-13.txt.2", O_CREAT|O_RDWR, DENY_NONE);
791         fnum = create_complex_file(cli, mem_ctx, BASEDIR "\\T013-13.txt.3");
792         smbcli_unlink(cli->tree, BASEDIR "\\T014-14.txt");
793         torture_set_file_attribute(cli->tree, BASEDIR "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
794         torture_set_file_attribute(cli->tree, BASEDIR "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
795         torture_set_file_attribute(cli->tree, BASEDIR "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);  
796         sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
797         sfinfo.generic.file.fnum = fnum;
798         sfinfo.disposition_info.in.delete_on_close = 1;
799         status = smb_raw_setfileinfo(cli->tree, &sfinfo);
800         CHECK_STATUS(status, NT_STATUS_OK);
801
802         io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
803         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
804         io2.t2fnext.in.max_count = num_files/2;
805         io2.t2fnext.in.resume_key = 0;
806         io2.t2fnext.in.flags = 0;
807         io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
808
809         status = smb_raw_search_next(cli->tree, mem_ctx,
810                                      &io2, &result, multiple_search_callback);
811         CHECK_STATUS(status, NT_STATUS_OK);
812         CHECK_VALUE(result.count, 19);
813
814         ret &= check_result(&result, "t009-9.txt", True, FILE_ATTRIBUTE_ARCHIVE);
815         ret &= check_result(&result, "t014-14.txt", False, 0);
816         ret &= check_result(&result, "t015-15.txt", False, 0);
817         ret &= check_result(&result, "t016-16.txt", True, FILE_ATTRIBUTE_NORMAL);
818         ret &= check_result(&result, "t017-17.txt", False, 0);
819         ret &= check_result(&result, "t018-18.txt", True, FILE_ATTRIBUTE_ARCHIVE);
820         ret &= check_result(&result, "t019-19.txt", True, FILE_ATTRIBUTE_ARCHIVE);
821         ret &= check_result(&result, "T013-13.txt.2", True, FILE_ATTRIBUTE_ARCHIVE);
822         ret &= check_result(&result, "T003-3.txt.2", False, 0);
823         ret &= check_result(&result, "T013-13.txt.3", True, FILE_ATTRIBUTE_ARCHIVE);
824
825         if (!ret) {
826                 for (i=0;i<result.count;i++) {
827                         printf("%s %s (0x%x)\n", 
828                                result.list[i].both_directory_info.name.s, 
829                                attrib_string(mem_ctx, result.list[i].both_directory_info.attrib),
830                                result.list[i].both_directory_info.attrib);
831                 }
832         }
833
834 done:
835         smb_raw_exit(cli->session);
836         smbcli_deltree(cli->tree, BASEDIR);
837
838         return ret;
839 }
840
841
842 /* 
843    testing if directories always come back sorted
844 */
845 static BOOL test_sorted(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
846 {
847         const int num_files = 700;
848         int i, fnum;
849         char *fname;
850         BOOL ret = True;
851         NTSTATUS status;
852         struct multiple_result result;
853
854         if (smbcli_deltree(cli->tree, BASEDIR) == -1 || 
855             NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
856                 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
857                 return False;
858         }
859
860         printf("Creating %d files\n", num_files);
861
862         for (i=0;i<num_files;i++) {
863                 asprintf(&fname, BASEDIR "\\%s.txt", generate_random_str(mem_ctx, 10));
864                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
865                 if (fnum == -1) {
866                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
867                         ret = False;
868                         goto done;
869                 }
870                 free(fname);
871                 smbcli_close(cli->tree, fnum);
872         }
873
874
875         ZERO_STRUCT(result);
876         result.mem_ctx = mem_ctx;
877         
878         status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*", 
879                                  RAW_SEARCH_BOTH_DIRECTORY_INFO,
880                                  CONT_NAME, &result);   
881         CHECK_STATUS(status, NT_STATUS_OK);
882         CHECK_VALUE(result.count, num_files);
883
884         for (i=0;i<num_files-1;i++) {
885                 const char *name1, *name2;
886                 name1 = result.list[i].both_directory_info.name.s;
887                 name2 = result.list[i+1].both_directory_info.name.s;
888                 if (StrCaseCmp(name1, name2) > 0) {
889                         printf("non-alphabetical order at entry %d  '%s' '%s'\n", 
890                                i, name1, name2);
891                         printf("Server does not produce sorted directory listings\n");
892                         ret = False;
893                         goto done;
894                 }
895         }
896
897 done:
898         smb_raw_exit(cli->session);
899         smbcli_deltree(cli->tree, BASEDIR);
900
901         return ret;
902 }
903
904
905 /* 
906    basic testing of all RAW_SEARCH_* calls using a single file
907 */
908 BOOL torture_raw_search(int dummy)
909 {
910         struct smbcli_state *cli;
911         BOOL ret = True;
912         TALLOC_CTX *mem_ctx;
913
914         if (!torture_open_connection(&cli)) {
915                 return False;
916         }
917
918         mem_ctx = talloc_init("torture_search");
919
920         if (!test_one_file(cli, mem_ctx)) {
921                 ret = False;
922         }
923
924         if (!test_many_files(cli, mem_ctx)) {
925                 ret = False;
926         }
927
928         if (!test_sorted(cli, mem_ctx)) {
929                 ret = False;
930         }
931
932         if (!test_modify_search(cli, mem_ctx)) {
933                 ret = False;
934         }
935
936         torture_close_connection(cli);
937         talloc_destroy(mem_ctx);
938         
939         return ret;
940 }