80d1bb5b3f5eea9e870863ecafec075e94345b46
[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 #include "torture/torture.h"
23 #include "system/filesys.h"
24 #include "libcli/raw/libcliraw.h"
25
26
27 #define BASEDIR "\\testsearch"
28
29 /*
30   callback function for single_search
31 */
32 static BOOL single_search_callback(void *private, union smb_search_data *file)
33 {
34         union smb_search_data *data = private;
35
36         *data = *file;
37
38         return True;
39 }
40
41 /*
42   do a single file (non-wildcard) search 
43 */
44 NTSTATUS torture_single_search(struct smbcli_state *cli, 
45                                TALLOC_CTX *mem_ctx,
46                                const char *pattern,
47                                enum smb_search_level level,
48                                uint16_t attrib,
49                                union smb_search_data *data)
50 {
51         union smb_search_first io;
52         union smb_search_close c;
53         NTSTATUS status;
54
55         io.generic.level = level;
56         if (level == RAW_SEARCH_SEARCH ||
57             level == RAW_SEARCH_FFIRST ||
58             level == RAW_SEARCH_FUNIQUE) {
59                 io.search_first.in.max_count = 1;
60                 io.search_first.in.search_attrib = attrib;
61                 io.search_first.in.pattern = pattern;
62         } else {
63                 io.t2ffirst.in.search_attrib = attrib;
64                 io.t2ffirst.in.max_count = 1;
65                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
66                 io.t2ffirst.in.storage_type = 0;
67                 io.t2ffirst.in.pattern = pattern;
68         }
69
70         status = smb_raw_search_first(cli->tree, mem_ctx,
71                                       &io, (void *)data, single_search_callback);
72
73         if (NT_STATUS_IS_OK(status) && level == RAW_SEARCH_FFIRST) {
74                 c.fclose.level = RAW_FINDCLOSE_FCLOSE;
75                 c.fclose.in.max_count = 1;
76                 c.fclose.in.search_attrib = 0;
77                 c.fclose.in.id = data->search.id;
78                 status = smb_raw_search_close(cli->tree, &c);
79         }
80         
81         return status;
82 }
83
84
85 static struct {
86         const char *name;
87         enum smb_search_level level;
88         uint32_t capability_mask;
89         NTSTATUS status;
90         union smb_search_data data;
91 } levels[] = {
92         {"FFIRST",                 RAW_SEARCH_FFIRST, },
93         {"FUNIQUE",                RAW_SEARCH_FUNIQUE, },
94         {"SEARCH",                 RAW_SEARCH_SEARCH, },
95         {"STANDARD",               RAW_SEARCH_STANDARD, },
96         {"EA_SIZE",                RAW_SEARCH_EA_SIZE, },
97         {"DIRECTORY_INFO",         RAW_SEARCH_DIRECTORY_INFO, },
98         {"FULL_DIRECTORY_INFO",    RAW_SEARCH_FULL_DIRECTORY_INFO, },
99         {"NAME_INFO",              RAW_SEARCH_NAME_INFO, },
100         {"BOTH_DIRECTORY_INFO",    RAW_SEARCH_BOTH_DIRECTORY_INFO, },
101         {"ID_FULL_DIRECTORY_INFO", RAW_SEARCH_ID_FULL_DIRECTORY_INFO, },
102         {"ID_BOTH_DIRECTORY_INFO", RAW_SEARCH_ID_BOTH_DIRECTORY_INFO, },
103         {"UNIX_INFO",              RAW_SEARCH_UNIX_INFO, CAP_UNIX}
104 };
105
106 /* find a level in the table by name */
107 static union smb_search_data *find(const char *name)
108 {
109         int i;
110         for (i=0;i<ARRAY_SIZE(levels);i++) {
111                 if (NT_STATUS_IS_OK(levels[i].status) && 
112                     strcmp(levels[i].name, name) == 0) {
113                         return &levels[i].data;
114                 }
115         }
116         return NULL;
117 }
118
119 /* 
120    basic testing of all RAW_SEARCH_* calls using a single file
121 */
122 static BOOL test_one_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
123 {
124         BOOL ret = True;
125         int fnum;
126         const char *fname = "\\torture_search.txt";
127         const char *fname2 = "\\torture_search-NOTEXIST.txt";
128         NTSTATUS status;
129         int i;
130         union smb_fileinfo all_info, alt_info, name_info, internal_info;
131         union smb_search_data *s;
132
133         printf("Testing one file searches\n");
134
135         fnum = create_complex_file(cli, mem_ctx, fname);
136         if (fnum == -1) {
137                 printf("ERROR: open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
138                 ret = False;
139                 goto done;
140         }
141
142         /* call all the levels */
143         for (i=0;i<ARRAY_SIZE(levels);i++) {
144                 NTSTATUS expected_status;
145                 uint32_t cap = cli->transport->negotiate.capabilities;
146
147                 printf("testing %s\n", levels[i].name);
148
149                 levels[i].status = torture_single_search(cli, mem_ctx, fname, 
150                                                          levels[i].level, 0,
151                                                          &levels[i].data);
152
153                 /* see if this server claims to support this level */
154                 if ((cap & levels[i].capability_mask) != levels[i].capability_mask) {
155                         printf("search level %s(%d) not supported by server\n",
156                                levels[i].name, (int)levels[i].level);
157                         continue;
158                 }
159
160                 if (!NT_STATUS_IS_OK(levels[i].status)) {
161                         printf("search level %s(%d) failed - %s\n",
162                                levels[i].name, (int)levels[i].level, 
163                                nt_errstr(levels[i].status));
164                         ret = False;
165                         continue;
166                 }
167
168                 status = torture_single_search(cli, mem_ctx, fname2, 
169                                                levels[i].level, 0,
170                                                &levels[i].data);
171                 
172                 expected_status = NT_STATUS_NO_SUCH_FILE;
173                 if (levels[i].level == RAW_SEARCH_SEARCH ||
174                     levels[i].level == RAW_SEARCH_FFIRST ||
175                     levels[i].level == RAW_SEARCH_FUNIQUE) {
176                         expected_status = STATUS_NO_MORE_FILES;
177                 }
178                 if (!NT_STATUS_EQUAL(status, expected_status)) {
179                         printf("search level %s(%d) should fail with %s - %s\n",
180                                levels[i].name, (int)levels[i].level, 
181                                nt_errstr(expected_status),
182                                nt_errstr(status));
183                         ret = False;
184                 }
185         }
186
187         /* get the all_info file into to check against */
188         all_info.generic.level = RAW_FILEINFO_ALL_INFO;
189         all_info.generic.in.fname = fname;
190         status = smb_raw_pathinfo(cli->tree, mem_ctx, &all_info);
191         if (!NT_STATUS_IS_OK(status)) {
192                 printf("RAW_FILEINFO_ALL_INFO failed - %s\n", nt_errstr(status));
193                 ret = False;
194                 goto done;
195         }
196
197         alt_info.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
198         alt_info.generic.in.fname = fname;
199         status = smb_raw_pathinfo(cli->tree, mem_ctx, &alt_info);
200         if (!NT_STATUS_IS_OK(status)) {
201                 printf("RAW_FILEINFO_ALT_NAME_INFO failed - %s\n", nt_errstr(status));
202                 ret = False;
203                 goto done;
204         }
205
206         internal_info.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
207         internal_info.generic.in.fname = fname;
208         status = smb_raw_pathinfo(cli->tree, mem_ctx, &internal_info);
209         if (!NT_STATUS_IS_OK(status)) {
210                 printf("RAW_FILEINFO_INTERNAL_INFORMATION failed - %s\n", nt_errstr(status));
211                 ret = False;
212                 goto done;
213         }
214
215         name_info.generic.level = RAW_FILEINFO_NAME_INFO;
216         name_info.generic.in.fname = fname;
217         status = smb_raw_pathinfo(cli->tree, mem_ctx, &name_info);
218         if (!NT_STATUS_IS_OK(status)) {
219                 printf("RAW_FILEINFO_NAME_INFO failed - %s\n", nt_errstr(status));
220                 ret = False;
221                 goto done;
222         }
223
224 #define CHECK_VAL(name, sname1, field1, v, sname2, field2) do { \
225         s = find(name); \
226         if (s) { \
227                 if ((s->sname1.field1) != (v.sname2.out.field2)) { \
228                         printf("(%s) %s/%s [0x%x] != %s/%s [0x%x]\n", \
229                                __location__, \
230                                 #sname1, #field1, (int)s->sname1.field1, \
231                                 #sname2, #field2, (int)v.sname2.out.field2); \
232                         ret = False; \
233                 } \
234         }} while (0)
235
236 #define CHECK_TIME(name, sname1, field1, v, sname2, field2) do { \
237         s = find(name); \
238         if (s) { \
239                 if (s->sname1.field1 != (~1 & nt_time_to_unix(v.sname2.out.field2))) { \
240                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
241                                __location__, \
242                                 #sname1, #field1, timestring(mem_ctx, s->sname1.field1), \
243                                 #sname2, #field2, nt_time_string(mem_ctx, v.sname2.out.field2)); \
244                         ret = False; \
245                 } \
246         }} while (0)
247
248 #define CHECK_NTTIME(name, sname1, field1, v, sname2, field2) do { \
249         s = find(name); \
250         if (s) { \
251                 if (s->sname1.field1 != v.sname2.out.field2) { \
252                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
253                                __location__, \
254                                 #sname1, #field1, nt_time_string(mem_ctx, s->sname1.field1), \
255                                 #sname2, #field2, nt_time_string(mem_ctx, v.sname2.out.field2)); \
256                         ret = False; \
257                 } \
258         }} while (0)
259
260 #define CHECK_STR(name, sname1, field1, v, sname2, field2) do { \
261         s = find(name); \
262         if (s) { \
263                 if (!s->sname1.field1 || strcmp(s->sname1.field1, v.sname2.out.field2.s)) { \
264                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
265                                __location__, \
266                                 #sname1, #field1, s->sname1.field1, \
267                                 #sname2, #field2, v.sname2.out.field2.s); \
268                         ret = False; \
269                 } \
270         }} while (0)
271
272 #define CHECK_WSTR(name, sname1, field1, v, sname2, field2, flags) do { \
273         s = find(name); \
274         if (s) { \
275                 if (!s->sname1.field1.s || \
276                     strcmp(s->sname1.field1.s, v.sname2.out.field2.s) || \
277                     wire_bad_flags(&s->sname1.field1, flags, cli)) { \
278                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
279                                __location__, \
280                                 #sname1, #field1, s->sname1.field1.s, \
281                                 #sname2, #field2, v.sname2.out.field2.s); \
282                         ret = False; \
283                 } \
284         }} while (0)
285
286 #define CHECK_NAME(name, sname1, field1, fname, flags) do { \
287         s = find(name); \
288         if (s) { \
289                 if (!s->sname1.field1.s || \
290                     strcmp(s->sname1.field1.s, fname) || \
291                     wire_bad_flags(&s->sname1.field1, flags, cli)) { \
292                         printf("(%s) %s/%s [%s] != %s\n", \
293                                __location__, \
294                                 #sname1, #field1, s->sname1.field1.s, \
295                                 fname); \
296                         ret = False; \
297                 } \
298         }} while (0)
299
300 #define CHECK_UNIX_NAME(name, sname1, field1, fname, flags) do { \
301         s = find(name); \
302         if (s) { \
303                 if (!s->sname1.field1 || \
304                     strcmp(s->sname1.field1, fname)) { \
305                         printf("(%s) %s/%s [%s] != %s\n", \
306                                __location__, \
307                                 #sname1, #field1, s->sname1.field1, \
308                                 fname); \
309                         ret = False; \
310                 } \
311         }} while (0)
312         
313         /* check that all the results are as expected */
314         CHECK_VAL("SEARCH",              search,              attrib, all_info, all_info, attrib&0xFFF);
315         CHECK_VAL("STANDARD",            standard,            attrib, all_info, all_info, attrib&0xFFF);
316         CHECK_VAL("EA_SIZE",             ea_size,             attrib, all_info, all_info, attrib&0xFFF);
317         CHECK_VAL("DIRECTORY_INFO",      directory_info,      attrib, all_info, all_info, attrib);
318         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, attrib, all_info, all_info, attrib);
319         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, attrib, all_info, all_info, attrib);
320         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           attrib, all_info, all_info, attrib);
321         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           attrib, all_info, all_info, attrib);
322
323         CHECK_TIME("SEARCH",             search,              write_time, all_info, all_info, write_time);
324         CHECK_TIME("STANDARD",           standard,            write_time, all_info, all_info, write_time);
325         CHECK_TIME("EA_SIZE",            ea_size,             write_time, all_info, all_info, write_time);
326         CHECK_TIME("STANDARD",           standard,            create_time, all_info, all_info, create_time);
327         CHECK_TIME("EA_SIZE",            ea_size,             create_time, all_info, all_info, create_time);
328         CHECK_TIME("STANDARD",           standard,            access_time, all_info, all_info, access_time);
329         CHECK_TIME("EA_SIZE",            ea_size,             access_time, all_info, all_info, access_time);
330
331         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      write_time, all_info, all_info, write_time);
332         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, write_time, all_info, all_info, write_time);
333         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, write_time, all_info, all_info, write_time);
334         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           write_time, all_info, all_info, write_time);
335         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           write_time, all_info, all_info, write_time);
336
337         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      create_time, all_info, all_info, create_time);
338         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
339         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
340         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           create_time, all_info, all_info, create_time);
341         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           create_time, all_info, all_info, create_time);
342
343         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      access_time, all_info, all_info, access_time);
344         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, access_time, all_info, all_info, access_time);
345         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, access_time, all_info, all_info, access_time);
346         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           access_time, all_info, all_info, access_time);
347         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           access_time, all_info, all_info, access_time);
348
349         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      change_time, all_info, all_info, change_time);
350         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, change_time, all_info, all_info, change_time);
351         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, change_time, all_info, all_info, change_time);
352         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           change_time, all_info, all_info, change_time);
353         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           change_time, all_info, all_info, change_time);
354
355         CHECK_VAL("SEARCH",              search,              size, all_info, all_info, size);
356         CHECK_VAL("STANDARD",            standard,            size, all_info, all_info, size);
357         CHECK_VAL("EA_SIZE",             ea_size,             size, all_info, all_info, size);
358         CHECK_VAL("DIRECTORY_INFO",      directory_info,      size, all_info, all_info, size);
359         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, size, all_info, all_info, size);
360         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, size, all_info, all_info, size);
361         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           size, all_info, all_info, size);
362         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           size, all_info, all_info, size);
363         CHECK_VAL("UNIX_INFO",           unix_info,           size, all_info, all_info, size);
364
365         CHECK_VAL("STANDARD",            standard,            alloc_size, all_info, all_info, alloc_size);
366         CHECK_VAL("EA_SIZE",             ea_size,             alloc_size, all_info, all_info, alloc_size);
367         CHECK_VAL("DIRECTORY_INFO",      directory_info,      alloc_size, all_info, all_info, alloc_size);
368         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, alloc_size, all_info, all_info, alloc_size);
369         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, alloc_size, all_info, all_info, alloc_size);
370         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           alloc_size, all_info, all_info, alloc_size);
371         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           alloc_size, all_info, all_info, alloc_size);
372         CHECK_VAL("UNIX_INFO",           unix_info,           alloc_size, all_info, all_info, alloc_size);
373
374         CHECK_VAL("EA_SIZE",             ea_size,             ea_size, all_info, all_info, ea_size);
375         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, ea_size, all_info, all_info, ea_size);
376         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, ea_size, all_info, all_info, ea_size);
377         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           ea_size, all_info, all_info, ea_size);
378         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           ea_size, all_info, all_info, ea_size);
379
380         CHECK_STR("SEARCH", search, name, alt_info, alt_name_info, fname);
381         CHECK_WSTR("BOTH_DIRECTORY_INFO", both_directory_info, short_name, alt_info, alt_name_info, fname, STR_UNICODE);
382
383         CHECK_NAME("STANDARD",            standard,            name, fname+1, 0);
384         CHECK_NAME("EA_SIZE",             ea_size,             name, fname+1, 0);
385         CHECK_NAME("DIRECTORY_INFO",      directory_info,      name, fname+1, STR_TERMINATE_ASCII);
386         CHECK_NAME("FULL_DIRECTORY_INFO", full_directory_info, name, fname+1, STR_TERMINATE_ASCII);
387         CHECK_NAME("NAME_INFO",           name_info,           name, fname+1, STR_TERMINATE_ASCII);
388         CHECK_NAME("BOTH_DIRECTORY_INFO", both_directory_info, name, fname+1, STR_TERMINATE_ASCII);
389         CHECK_NAME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           name, fname+1, STR_TERMINATE_ASCII);
390         CHECK_NAME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           name, fname+1, STR_TERMINATE_ASCII);
391         CHECK_UNIX_NAME("UNIX_INFO",           unix_info,           name, fname+1, STR_TERMINATE_ASCII);
392
393         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, file_id, internal_info, internal_information, file_id);
394         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, file_id, internal_info, internal_information, file_id);
395
396 done:
397         smb_raw_exit(cli->session);
398         smbcli_unlink(cli->tree, fname);
399
400         return ret;
401 }
402
403
404 struct multiple_result {
405         TALLOC_CTX *mem_ctx;
406         int count;
407         union smb_search_data *list;
408 };
409
410 /*
411   callback function for multiple_search
412 */
413 static BOOL multiple_search_callback(void *private, union smb_search_data *file)
414 {
415         struct multiple_result *data = private;
416
417
418         data->count++;
419         data->list = talloc_realloc(data->mem_ctx,
420                                       data->list, 
421                                       union smb_search_data,
422                                       data->count);
423
424         data->list[data->count-1] = *file;
425
426         return True;
427 }
428
429 enum continue_type {CONT_FLAGS, CONT_NAME, CONT_RESUME_KEY};
430
431 /*
432   do a single file (non-wildcard) search 
433 */
434 static NTSTATUS multiple_search(struct smbcli_state *cli, 
435                                 TALLOC_CTX *mem_ctx,
436                                 const char *pattern,
437                                 enum smb_search_level level,
438                                 enum continue_type cont_type,
439                                 void *data)
440 {
441         union smb_search_first io;
442         union smb_search_next io2;
443         NTSTATUS status;
444         const int per_search = 100;
445         struct multiple_result *result = data;
446
447         io.generic.level = level;
448         if (level == RAW_SEARCH_SEARCH) {
449                 io.search_first.in.max_count = per_search;
450                 io.search_first.in.search_attrib = 0;
451                 io.search_first.in.pattern = pattern;
452         } else {
453                 io.t2ffirst.in.search_attrib = 0;
454                 io.t2ffirst.in.max_count = per_search;
455                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
456                 io.t2ffirst.in.storage_type = 0;
457                 io.t2ffirst.in.pattern = pattern;
458                 if (cont_type == CONT_RESUME_KEY) {
459                         io.t2ffirst.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME | 
460                                 FLAG_TRANS2_FIND_BACKUP_INTENT;
461                 }
462         }
463
464         status = smb_raw_search_first(cli->tree, mem_ctx,
465                                       &io, data, multiple_search_callback);
466         
467
468         while (NT_STATUS_IS_OK(status)) {
469                 io2.generic.level = level;
470                 if (level == RAW_SEARCH_SEARCH) {
471                         io2.search_next.in.max_count = per_search;
472                         io2.search_next.in.search_attrib = 0;
473                         io2.search_next.in.id = result->list[result->count-1].search.id;
474                 } else {
475                         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
476                         io2.t2fnext.in.max_count = per_search;
477                         io2.t2fnext.in.resume_key = 0;
478                         io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
479                         io2.t2fnext.in.last_name = "";
480                         switch (cont_type) {
481                         case CONT_RESUME_KEY:
482                                 if (level == RAW_SEARCH_STANDARD) {
483                                         io2.t2fnext.in.resume_key = 
484                                                 result->list[result->count-1].standard.resume_key;
485                                 } else if (level == RAW_SEARCH_EA_SIZE) {
486                                         io2.t2fnext.in.resume_key = 
487                                                 result->list[result->count-1].ea_size.resume_key;
488                                 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
489                                         io2.t2fnext.in.resume_key = 
490                                                 result->list[result->count-1].directory_info.file_index;
491                                 } else {
492                                         io2.t2fnext.in.resume_key = 
493                                                 result->list[result->count-1].both_directory_info.file_index;
494                                 }
495                                 if (io2.t2fnext.in.resume_key == 0) {
496                                         printf("Server does not support resume by key\n");
497                                         return NT_STATUS_NOT_SUPPORTED;
498                                 }
499                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
500                                         FLAG_TRANS2_FIND_BACKUP_INTENT;
501                                 break;
502                         case CONT_NAME:
503                                 if (level == RAW_SEARCH_STANDARD) {
504                                         io2.t2fnext.in.last_name = 
505                                                 result->list[result->count-1].standard.name.s;
506                                 } else if (level == RAW_SEARCH_EA_SIZE) {
507                                         io2.t2fnext.in.last_name = 
508                                                 result->list[result->count-1].ea_size.name.s;
509                                 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
510                                         io2.t2fnext.in.last_name = 
511                                                 result->list[result->count-1].directory_info.name.s;
512                                 } else {
513                                         io2.t2fnext.in.last_name = 
514                                                 result->list[result->count-1].both_directory_info.name.s;
515                                 }
516                                 break;
517                         case CONT_FLAGS:
518                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_CONTINUE;
519                                 break;
520                         }
521                 }
522
523                 status = smb_raw_search_next(cli->tree, mem_ctx,
524                                              &io2, data, multiple_search_callback);
525                 if (!NT_STATUS_IS_OK(status)) {
526                         break;
527                 }
528                 if (level == RAW_SEARCH_SEARCH) {
529                         if (io2.search_next.out.count == 0) {
530                                 break;
531                         }
532                 } else if (io2.t2fnext.out.count == 0 ||
533                            io2.t2fnext.out.end_of_search) {
534                         break;
535                 }
536         }
537
538         return status;
539 }
540
541 #define CHECK_STATUS(status, correct) do { \
542         if (!NT_STATUS_EQUAL(status, correct)) { \
543                 printf("(%s) Incorrect status %s - should be %s\n", \
544                        __location__, nt_errstr(status), nt_errstr(correct)); \
545                 ret = False; \
546                 goto done; \
547         }} while (0)
548
549 #define CHECK_VALUE(v, correct) do { \
550         if ((v) != (correct)) { \
551                 printf("(%s) Incorrect value %s=%ld - should be %ld\n", \
552                        __location__, #v, (long)v, (long)correct); \
553                 ret = False; \
554                 goto done; \
555         }} while (0)
556
557 #define CHECK_STRING(v, correct) do { \
558         if (strcasecmp_m(v, correct) != 0) { \
559                 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
560                        __location__, #v, v, correct); \
561                 ret = False; \
562         }} while (0)
563
564
565 static int search_both_compare(union smb_search_data *d1, union smb_search_data *d2)
566 {
567         return strcmp_safe(d1->both_directory_info.name.s, d2->both_directory_info.name.s);
568 }
569
570 static int search_standard_compare(union smb_search_data *d1, union smb_search_data *d2)
571 {
572         return strcmp_safe(d1->standard.name.s, d2->standard.name.s);
573 }
574
575 static int search_ea_size_compare(union smb_search_data *d1, union smb_search_data *d2)
576 {
577         return strcmp_safe(d1->ea_size.name.s, d2->ea_size.name.s);
578 }
579
580 static int search_directory_info_compare(union smb_search_data *d1, union smb_search_data *d2)
581 {
582         return strcmp_safe(d1->directory_info.name.s, d2->directory_info.name.s);
583 }
584
585 static int search_old_compare(union smb_search_data *d1, union smb_search_data *d2)
586 {
587         return strcmp_safe(d1->search.name, d2->search.name);
588 }
589
590
591 /* 
592    basic testing of search calls using many files
593 */
594 static BOOL test_many_files(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
595 {
596         const int num_files = 700;
597         int i, fnum, t;
598         char *fname;
599         BOOL ret = True;
600         NTSTATUS status;
601         struct multiple_result result;
602         struct {
603                 const char *name;
604                 const char *cont_name;
605                 enum smb_search_level level;
606                 enum continue_type cont_type;
607         } search_types[] = {
608                 {"SEARCH",              "ID",    RAW_SEARCH_SEARCH,              CONT_RESUME_KEY},
609                 {"BOTH_DIRECTORY_INFO", "NAME",  RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_NAME},
610                 {"BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_FLAGS},
611                 {"BOTH_DIRECTORY_INFO", "KEY",   RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY},
612                 {"STANDARD",            "FLAGS", RAW_SEARCH_STANDARD,            CONT_FLAGS},
613                 {"STANDARD",            "KEY",   RAW_SEARCH_STANDARD,            CONT_RESUME_KEY},
614                 {"STANDARD",            "NAME",  RAW_SEARCH_STANDARD,            CONT_NAME},
615                 {"EA_SIZE",             "FLAGS", RAW_SEARCH_EA_SIZE,             CONT_FLAGS},
616                 {"EA_SIZE",             "KEY",   RAW_SEARCH_EA_SIZE,             CONT_RESUME_KEY},
617                 {"EA_SIZE",             "NAME",  RAW_SEARCH_EA_SIZE,             CONT_NAME},
618                 {"DIRECTORY_INFO",      "FLAGS", RAW_SEARCH_DIRECTORY_INFO,      CONT_FLAGS},
619                 {"DIRECTORY_INFO",      "KEY",   RAW_SEARCH_DIRECTORY_INFO,      CONT_RESUME_KEY},
620                 {"DIRECTORY_INFO",      "NAME",  RAW_SEARCH_DIRECTORY_INFO,      CONT_NAME}
621         };
622
623         if (!torture_setup_dir(cli, BASEDIR)) {
624                 return False;
625         }
626
627         printf("Creating %d files\n", num_files);
628
629         for (i=0;i<num_files;i++) {
630                 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
631                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
632                 if (fnum == -1) {
633                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
634                         ret = False;
635                         goto done;
636                 }
637                 talloc_free(fname);
638                 smbcli_close(cli->tree, fnum);
639         }
640
641
642         for (t=0;t<ARRAY_SIZE(search_types);t++) {
643                 ZERO_STRUCT(result);
644                 result.mem_ctx = talloc_new(mem_ctx);
645         
646                 printf("Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
647
648                 status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*", 
649                                          search_types[t].level,
650                                          search_types[t].cont_type,
651                                          &result);
652         
653                 if (!NT_STATUS_IS_OK(status)) {
654                         printf("search failed - %s\n", nt_errstr(status));
655                         ret = False;
656                         continue;
657                 }
658                 CHECK_VALUE(result.count, num_files);
659
660                 if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
661                         qsort(result.list, result.count, sizeof(result.list[0]), 
662                               QSORT_CAST  search_both_compare);
663                 } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
664                         qsort(result.list, result.count, sizeof(result.list[0]), 
665                               QSORT_CAST search_standard_compare);
666                 } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
667                         qsort(result.list, result.count, sizeof(result.list[0]), 
668                               QSORT_CAST search_ea_size_compare);
669                 } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
670                         qsort(result.list, result.count, sizeof(result.list[0]), 
671                               QSORT_CAST search_directory_info_compare);
672                 } else {
673                         qsort(result.list, result.count, sizeof(result.list[0]), 
674                               QSORT_CAST search_old_compare);
675                 }
676
677                 for (i=0;i<result.count;i++) {
678                         const char *s;
679                         if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
680                                 s = result.list[i].both_directory_info.name.s;
681                         } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
682                                 s = result.list[i].standard.name.s;
683                         } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
684                                 s = result.list[i].ea_size.name.s;
685                         } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
686                                 s = result.list[i].directory_info.name.s;
687                         } else {
688                                 s = result.list[i].search.name;
689                         }
690                         fname = talloc_asprintf(cli, "t%03d-%d.txt", i, i);
691                         if (strcmp(fname, s)) {
692                                 printf("Incorrect name %s at entry %d\n", s, i);
693                                 ret = False;
694                                 break;
695                         }
696                         talloc_free(fname);
697                 }
698                 talloc_free(result.mem_ctx);
699         }
700
701 done:
702         smb_raw_exit(cli->session);
703         smbcli_deltree(cli->tree, BASEDIR);
704
705         return ret;
706 }
707
708 /*
709   check a individual file result
710 */
711 static BOOL check_result(struct multiple_result *result, const char *name, BOOL exist, uint32_t attrib)
712 {
713         int i;
714         for (i=0;i<result->count;i++) {
715                 if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
716         }
717         if (i == result->count) {
718                 if (exist) {
719                         printf("failed: '%s' should exist with attribute %s\n", 
720                                name, attrib_string(result->list, attrib));
721                         return False;
722                 }
723                 return True;
724         }
725
726         if (!exist) {
727                 printf("failed: '%s' should NOT exist (has attribute %s)\n", 
728                        name, attrib_string(result->list, result->list[i].both_directory_info.attrib));
729                 return False;
730         }
731
732         if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
733                 printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
734                        name, 
735                        attrib, result->list[i].both_directory_info.attrib);
736                 return False;
737         }
738         return True;
739 }
740
741 /* 
742    test what happens when the directory is modified during a search
743 */
744 static BOOL test_modify_search(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
745 {
746         const int num_files = 20;
747         int i, fnum;
748         char *fname;
749         BOOL ret = True;
750         NTSTATUS status;
751         struct multiple_result result;
752         union smb_search_first io;
753         union smb_search_next io2;
754         union smb_setfileinfo sfinfo;
755
756         if (!torture_setup_dir(cli, BASEDIR)) {
757                 return False;
758         }
759
760         printf("Creating %d files\n", num_files);
761
762         for (i=num_files-1;i>=0;i--) {
763                 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
764                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
765                 if (fnum == -1) {
766                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
767                         ret = False;
768                         goto done;
769                 }
770                 talloc_free(fname);
771                 smbcli_close(cli->tree, fnum);
772         }
773
774         printf("pulling the first file\n");
775         ZERO_STRUCT(result);
776         result.mem_ctx = talloc_new(mem_ctx);
777
778         io.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
779         io.t2ffirst.in.search_attrib = 0;
780         io.t2ffirst.in.max_count = 0;
781         io.t2ffirst.in.flags = 0;
782         io.t2ffirst.in.storage_type = 0;
783         io.t2ffirst.in.pattern = BASEDIR "\\*.*";
784
785         status = smb_raw_search_first(cli->tree, mem_ctx,
786                                       &io, &result, multiple_search_callback);
787         CHECK_STATUS(status, NT_STATUS_OK);
788         CHECK_VALUE(result.count, 1);
789         
790         printf("pulling the second file\n");
791         io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
792         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
793         io2.t2fnext.in.max_count = 1;
794         io2.t2fnext.in.resume_key = 0;
795         io2.t2fnext.in.flags = 0;
796         io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
797
798         status = smb_raw_search_next(cli->tree, mem_ctx,
799                                      &io2, &result, multiple_search_callback);
800         CHECK_STATUS(status, NT_STATUS_OK);
801         CHECK_VALUE(result.count, 2);
802
803         result.count = 0;
804
805         printf("Changing attributes and deleting\n");
806         smbcli_open(cli->tree, BASEDIR "\\T003-03.txt.2", O_CREAT|O_RDWR, DENY_NONE);
807         smbcli_open(cli->tree, BASEDIR "\\T013-13.txt.2", O_CREAT|O_RDWR, DENY_NONE);
808         fnum = create_complex_file(cli, mem_ctx, BASEDIR "\\T013-13.txt.3");
809         smbcli_unlink(cli->tree, BASEDIR "\\T014-14.txt");
810         torture_set_file_attribute(cli->tree, BASEDIR "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
811         torture_set_file_attribute(cli->tree, BASEDIR "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
812         torture_set_file_attribute(cli->tree, BASEDIR "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);  
813         torture_set_file_attribute(cli->tree, BASEDIR "\\T018-18.txt", 0);      
814         sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
815         sfinfo.generic.file.fnum = fnum;
816         sfinfo.disposition_info.in.delete_on_close = 1;
817         status = smb_raw_setfileinfo(cli->tree, &sfinfo);
818         CHECK_STATUS(status, NT_STATUS_OK);
819
820         io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
821         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
822         io2.t2fnext.in.max_count = num_files + 3;
823         io2.t2fnext.in.resume_key = 0;
824         io2.t2fnext.in.flags = 0;
825         io2.t2fnext.in.last_name = ".";
826
827         status = smb_raw_search_next(cli->tree, mem_ctx,
828                                      &io2, &result, multiple_search_callback);
829         CHECK_STATUS(status, NT_STATUS_OK);
830         CHECK_VALUE(result.count, 20);
831
832         ret &= check_result(&result, "t009-9.txt", True, FILE_ATTRIBUTE_ARCHIVE);
833         ret &= check_result(&result, "t014-14.txt", False, 0);
834         ret &= check_result(&result, "t015-15.txt", False, 0);
835         ret &= check_result(&result, "t016-16.txt", True, FILE_ATTRIBUTE_NORMAL);
836         ret &= check_result(&result, "t017-17.txt", False, 0);
837         ret &= check_result(&result, "t018-18.txt", True, FILE_ATTRIBUTE_ARCHIVE);
838         ret &= check_result(&result, "t019-19.txt", True, FILE_ATTRIBUTE_ARCHIVE);
839         ret &= check_result(&result, "T013-13.txt.2", True, FILE_ATTRIBUTE_ARCHIVE);
840         ret &= check_result(&result, "T003-3.txt.2", False, 0);
841         ret &= check_result(&result, "T013-13.txt.3", True, FILE_ATTRIBUTE_ARCHIVE);
842
843         if (!ret) {
844                 for (i=0;i<result.count;i++) {
845                         printf("%s %s (0x%x)\n", 
846                                result.list[i].both_directory_info.name.s, 
847                                attrib_string(mem_ctx, result.list[i].both_directory_info.attrib),
848                                result.list[i].both_directory_info.attrib);
849                 }
850         }
851
852 done:
853         smb_raw_exit(cli->session);
854         smbcli_deltree(cli->tree, BASEDIR);
855
856         return ret;
857 }
858
859
860 /* 
861    testing if directories always come back sorted
862 */
863 static BOOL test_sorted(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
864 {
865         const int num_files = 700;
866         int i, fnum;
867         char *fname;
868         BOOL ret = True;
869         NTSTATUS status;
870         struct multiple_result result;
871
872         if (!torture_setup_dir(cli, BASEDIR)) {
873                 return False;
874         }
875
876         printf("Creating %d files\n", num_files);
877
878         for (i=0;i<num_files;i++) {
879                 fname = talloc_asprintf(cli, BASEDIR "\\%s.txt", generate_random_str_list(mem_ctx, 10, "abcdefgh"));
880                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
881                 if (fnum == -1) {
882                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
883                         ret = False;
884                         goto done;
885                 }
886                 talloc_free(fname);
887                 smbcli_close(cli->tree, fnum);
888         }
889
890
891         ZERO_STRUCT(result);
892         result.mem_ctx = mem_ctx;
893         
894         status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*", 
895                                  RAW_SEARCH_BOTH_DIRECTORY_INFO,
896                                  CONT_NAME, &result);   
897         CHECK_STATUS(status, NT_STATUS_OK);
898         CHECK_VALUE(result.count, num_files);
899
900         for (i=0;i<num_files-1;i++) {
901                 const char *name1, *name2;
902                 name1 = result.list[i].both_directory_info.name.s;
903                 name2 = result.list[i+1].both_directory_info.name.s;
904                 if (strcasecmp_m(name1, name2) > 0) {
905                         printf("non-alphabetical order at entry %d  '%s' '%s'\n", 
906                                i, name1, name2);
907                         printf("Server does not produce sorted directory listings (not an error)\n");
908                         goto done;
909                 }
910         }
911
912         talloc_free(result.list);
913
914 done:
915         smb_raw_exit(cli->session);
916         smbcli_deltree(cli->tree, BASEDIR);
917
918         return ret;
919 }
920
921
922
923 /* 
924    basic testing of many old style search calls using separate dirs
925 */
926 static BOOL test_many_dirs(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
927 {
928         const int num_dirs = 100;
929         int i, fnum, n;
930         char *fname, *dname;
931         BOOL ret = True;
932         NTSTATUS status;
933         union smb_search_data *file, *file2, *file3;
934
935         if (!torture_setup_dir(cli, BASEDIR)) {
936                 return False;
937         }
938
939         printf("Creating %d dirs\n", num_dirs);
940
941         for (i=0;i<num_dirs;i++) {
942                 dname = talloc_asprintf(cli, BASEDIR "\\d%d", i);
943                 status = smbcli_mkdir(cli->tree, dname);
944                 if (!NT_STATUS_IS_OK(status)) {
945                         printf("(%s) Failed to create %s - %s\n", 
946                                __location__, dname, nt_errstr(status));
947                         ret = False;
948                         goto done;
949                 }
950
951                 for (n=0;n<3;n++) {
952                         fname = talloc_asprintf(cli, BASEDIR "\\d%d\\f%d-%d.txt", i, i, n);
953                         fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
954                         if (fnum == -1) {
955                                 printf("(%s) Failed to create %s - %s\n", 
956                                        __location__, fname, smbcli_errstr(cli->tree));
957                                 ret = False;
958                                 goto done;
959                         }
960                         talloc_free(fname);
961                         smbcli_close(cli->tree, fnum);
962                 }
963
964                 talloc_free(dname);
965         }
966
967         file  = talloc_zero_array(mem_ctx, union smb_search_data, num_dirs);
968         file2 = talloc_zero_array(mem_ctx, union smb_search_data, num_dirs);
969         file3 = talloc_zero_array(mem_ctx, union smb_search_data, num_dirs);
970
971         printf("Search first on %d dirs\n", num_dirs);
972
973         for (i=0;i<num_dirs;i++) {
974                 union smb_search_first io;
975                 io.generic.level = RAW_SEARCH_SEARCH;
976                 io.search_first.in.max_count = 1;
977                 io.search_first.in.search_attrib = 0;
978                 io.search_first.in.pattern = talloc_asprintf(mem_ctx, BASEDIR "\\d%d\\*.txt", i);
979                 fname = talloc_asprintf(mem_ctx, "f%d-", i);
980
981                 io.search_first.out.count = 0;
982
983                 status = smb_raw_search_first(cli->tree, mem_ctx,
984                                               &io, (void *)&file[i], single_search_callback);
985                 if (io.search_first.out.count != 1) {
986                         printf("(%s) search first gave %d entries for dir %d - %s\n",
987                                __location__, io.search_first.out.count, i, nt_errstr(status));
988                         ret = False;
989                         goto done;
990                 }
991                 CHECK_STATUS(status, NT_STATUS_OK);
992                 if (strncasecmp(file[i].search.name, fname, strlen(fname)) != 0) {
993                         printf("(%s) incorrect name '%s' expected '%s'[12].txt\n", 
994                                __location__, file[i].search.name, fname);
995                         ret = False;
996                         goto done;
997                 }
998
999                 talloc_free(fname);
1000         }
1001
1002         printf("Search next on %d dirs\n", num_dirs);
1003
1004         for (i=0;i<num_dirs;i++) {
1005                 union smb_search_next io2;
1006
1007                 io2.generic.level = RAW_SEARCH_SEARCH;
1008                 io2.search_next.in.max_count = 1;
1009                 io2.search_next.in.search_attrib = 0;
1010                 io2.search_next.in.id = file[i].search.id;
1011                 fname = talloc_asprintf(mem_ctx, "f%d-", i);
1012
1013                 io2.search_next.out.count = 0;
1014
1015                 status = smb_raw_search_next(cli->tree, mem_ctx,
1016                                              &io2, (void *)&file2[i], single_search_callback);
1017                 if (io2.search_next.out.count != 1) {
1018                         printf("(%s) search next gave %d entries for dir %d - %s\n",
1019                                __location__, io2.search_next.out.count, i, nt_errstr(status));
1020                         ret = False;
1021                         goto done;
1022                 }
1023                 CHECK_STATUS(status, NT_STATUS_OK);
1024                 if (strncasecmp(file2[i].search.name, fname, strlen(fname)) != 0) {
1025                         printf("(%s) incorrect name '%s' expected '%s'[12].txt\n", 
1026                                __location__, file2[i].search.name, fname);
1027                         ret = False;
1028                         goto done;
1029                 }
1030
1031                 talloc_free(fname);
1032         }
1033
1034
1035         printf("Search next (rewind) on %d dirs\n", num_dirs);
1036
1037         for (i=0;i<num_dirs;i++) {
1038                 union smb_search_next io2;
1039
1040                 io2.generic.level = RAW_SEARCH_SEARCH;
1041                 io2.search_next.in.max_count = 1;
1042                 io2.search_next.in.search_attrib = 0;
1043                 io2.search_next.in.id = file[i].search.id;
1044                 fname = talloc_asprintf(mem_ctx, "f%d-", i);
1045                 io2.search_next.out.count = 0;
1046
1047                 status = smb_raw_search_next(cli->tree, mem_ctx,
1048                                              &io2, (void *)&file3[i], single_search_callback);
1049                 if (io2.search_next.out.count != 1) {
1050                         printf("(%s) search next gave %d entries for dir %d - %s\n",
1051                                __location__, io2.search_next.out.count, i, nt_errstr(status));
1052                         ret = False;
1053                         goto done;
1054                 }
1055                 CHECK_STATUS(status, NT_STATUS_OK);
1056
1057                 if (strncasecmp(file3[i].search.name, file2[i].search.name, 3) != 0) {
1058                         printf("(%s) incorrect name '%s' on rewind at dir %d\n", 
1059                                __location__, file2[i].search.name, i);
1060                         ret = False;
1061                         goto done;
1062                 }
1063
1064                 if (strcmp(file3[i].search.name, file2[i].search.name) != 0) {
1065                         printf("(%s) server did not rewind - got '%s' expected '%s'\n", 
1066                                __location__, file3[i].search.name, file2[i].search.name);
1067                         ret = False;
1068                         goto done;
1069                 }
1070
1071                 talloc_free(fname);
1072         }
1073
1074
1075 done:
1076         smb_raw_exit(cli->session);
1077         smbcli_deltree(cli->tree, BASEDIR);
1078
1079         return ret;
1080 }
1081
1082
1083 /* 
1084    testing of OS/2 style delete
1085 */
1086 static BOOL test_os2_delete(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1087 {
1088         const int num_files = 700;
1089         const int delete_count = 4;
1090         int total_deleted = 0;
1091         int i, fnum;
1092         char *fname;
1093         BOOL ret = True;
1094         NTSTATUS status;
1095         union smb_search_first io;
1096         union smb_search_next io2;
1097         struct multiple_result result;
1098
1099         if (!torture_setup_dir(cli, BASEDIR)) {
1100                 return False;
1101         }
1102
1103         printf("Testing OS/2 style delete on %d files\n", num_files);
1104
1105         for (i=0;i<num_files;i++) {
1106                 fname = talloc_asprintf(cli, BASEDIR "\\file%u.txt", i);
1107                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1108                 if (fnum == -1) {
1109                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1110                         ret = False;
1111                         goto done;
1112                 }
1113                 talloc_free(fname);
1114                 smbcli_close(cli->tree, fnum);
1115         }
1116
1117
1118         ZERO_STRUCT(result);
1119         result.mem_ctx = mem_ctx;
1120
1121         io.t2ffirst.level = RAW_SEARCH_EA_SIZE;
1122         io.t2ffirst.in.search_attrib = 0;
1123         io.t2ffirst.in.max_count = 100;
1124         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1125         io.t2ffirst.in.storage_type = 0;
1126         io.t2ffirst.in.pattern = BASEDIR "\\*";
1127
1128         status = smb_raw_search_first(cli->tree, mem_ctx,
1129                                       &io, &result, multiple_search_callback);
1130         CHECK_STATUS(status, NT_STATUS_OK);
1131
1132         for (i=0;i<MIN(result.count, delete_count);i++) {
1133                 fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
1134                 status = smbcli_unlink(cli->tree, fname);
1135                 CHECK_STATUS(status, NT_STATUS_OK);
1136                 total_deleted++;
1137                 talloc_free(fname);
1138         }
1139
1140         io2.t2fnext.level = RAW_SEARCH_EA_SIZE;
1141         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1142         io2.t2fnext.in.max_count = 100;
1143         io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1144         io2.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1145         io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1146
1147         do {
1148                 ZERO_STRUCT(result);
1149                 result.mem_ctx = mem_ctx;
1150
1151                 status = smb_raw_search_next(cli->tree, mem_ctx,
1152                                              &io2, &result, multiple_search_callback);
1153                 if (!NT_STATUS_IS_OK(status)) {
1154                         break;
1155                 }
1156
1157                 for (i=0;i<MIN(result.count, delete_count);i++) {
1158                         fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
1159                         status = smbcli_unlink(cli->tree, fname);
1160                         CHECK_STATUS(status, NT_STATUS_OK);
1161                         total_deleted++;
1162                         talloc_free(fname);
1163                 }
1164
1165                 if (i>0) {
1166                         io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1167                         io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1168                 }
1169         } while (NT_STATUS_IS_OK(status) && result.count != 0);
1170
1171         CHECK_STATUS(status, NT_STATUS_OK);
1172
1173         if (total_deleted != num_files) {
1174                 printf("error: deleted %d - expected to delete %d\n", 
1175                        total_deleted, num_files);
1176                 ret = False;
1177         }
1178
1179 done:
1180         smb_raw_exit(cli->session);
1181         smbcli_deltree(cli->tree, BASEDIR);
1182
1183         return ret;
1184 }
1185
1186
1187 static int ealist_cmp(union smb_search_data *r1, union smb_search_data *r2)
1188 {
1189         return strcmp(r1->ea_list.name.s, r2->ea_list.name.s);
1190 }
1191
1192 /* 
1193    testing of the rather strange ea_list level
1194 */
1195 static BOOL test_ea_list(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1196 {
1197         int  fnum;
1198         BOOL ret = True;
1199         NTSTATUS status;
1200         union smb_search_first io;
1201         union smb_search_next nxt;
1202         struct multiple_result result;
1203         union smb_setfileinfo setfile;
1204
1205         if (!torture_setup_dir(cli, BASEDIR)) {
1206                 return False;
1207         }
1208
1209         printf("Testing RAW_SEARCH_EA_LIST level\n");
1210
1211         fnum = smbcli_open(cli->tree, BASEDIR "\\file1.txt", O_CREAT|O_RDWR, DENY_NONE);
1212         smbcli_close(cli->tree, fnum);
1213
1214         fnum = smbcli_open(cli->tree, BASEDIR "\\file2.txt", O_CREAT|O_RDWR, DENY_NONE);
1215         smbcli_close(cli->tree, fnum);
1216
1217         fnum = smbcli_open(cli->tree, BASEDIR "\\file3.txt", O_CREAT|O_RDWR, DENY_NONE);
1218         smbcli_close(cli->tree, fnum);
1219
1220         setfile.generic.level = RAW_SFILEINFO_EA_SET;
1221         setfile.generic.file.fname = BASEDIR "\\file2.txt";
1222         setfile.ea_set.in.num_eas = 2;
1223         setfile.ea_set.in.eas = talloc_array(mem_ctx, struct ea_struct, 2);
1224         setfile.ea_set.in.eas[0].flags = 0;
1225         setfile.ea_set.in.eas[0].name.s = "EA ONE";
1226         setfile.ea_set.in.eas[0].value = data_blob_string_const("VALUE 1");
1227         setfile.ea_set.in.eas[1].flags = 0;
1228         setfile.ea_set.in.eas[1].name.s = "SECOND EA";
1229         setfile.ea_set.in.eas[1].value = data_blob_string_const("Value Two");
1230
1231         status = smb_raw_setpathinfo(cli->tree, &setfile);
1232         CHECK_STATUS(status, NT_STATUS_OK);
1233
1234         setfile.generic.file.fname = BASEDIR "\\file3.txt";
1235         status = smb_raw_setpathinfo(cli->tree, &setfile);
1236         CHECK_STATUS(status, NT_STATUS_OK);
1237         
1238         ZERO_STRUCT(result);
1239         result.mem_ctx = mem_ctx;
1240
1241         io.t2ffirst.level = RAW_SEARCH_EA_LIST;
1242         io.t2ffirst.in.search_attrib = 0;
1243         io.t2ffirst.in.max_count = 2;
1244         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1245         io.t2ffirst.in.storage_type = 0;
1246         io.t2ffirst.in.pattern = BASEDIR "\\*";
1247         io.t2ffirst.in.num_names = 2;
1248         io.t2ffirst.in.ea_names = talloc_array(mem_ctx, struct ea_name, 2);
1249         io.t2ffirst.in.ea_names[0].name.s = "SECOND EA";
1250         io.t2ffirst.in.ea_names[1].name.s = "THIRD EA";
1251
1252         status = smb_raw_search_first(cli->tree, mem_ctx,
1253                                       &io, &result, multiple_search_callback);
1254         CHECK_STATUS(status, NT_STATUS_OK);
1255         CHECK_VALUE(result.count, 2);
1256
1257         nxt.t2fnext.level = RAW_SEARCH_EA_LIST;
1258         nxt.t2fnext.in.handle = io.t2ffirst.out.handle;
1259         nxt.t2fnext.in.max_count = 2;
1260         nxt.t2fnext.in.resume_key = result.list[1].ea_list.resume_key;
1261         nxt.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME | FLAG_TRANS2_FIND_CONTINUE;
1262         nxt.t2fnext.in.last_name = result.list[1].ea_list.name.s;
1263         nxt.t2fnext.in.num_names = 2;
1264         nxt.t2fnext.in.ea_names = talloc_array(mem_ctx, struct ea_name, 2);
1265         nxt.t2fnext.in.ea_names[0].name.s = "SECOND EA";
1266         nxt.t2fnext.in.ea_names[1].name.s = "THIRD EA";
1267
1268         status = smb_raw_search_next(cli->tree, mem_ctx,
1269                                      &nxt, &result, multiple_search_callback);
1270         CHECK_STATUS(status, NT_STATUS_OK);
1271
1272         /* we have to sort the result as different servers can return directories
1273            in different orders */
1274         qsort(result.list, result.count, sizeof(result.list[0]), 
1275               (comparison_fn_t)ealist_cmp);
1276
1277         CHECK_VALUE(result.count, 3);
1278         CHECK_VALUE(result.list[0].ea_list.eas.num_eas, 2);
1279         CHECK_STRING(result.list[0].ea_list.name.s, "file1.txt");
1280         CHECK_STRING(result.list[0].ea_list.eas.eas[0].name.s, "SECOND EA");
1281         CHECK_VALUE(result.list[0].ea_list.eas.eas[0].value.length, 0);
1282         CHECK_STRING(result.list[0].ea_list.eas.eas[1].name.s, "THIRD EA");
1283         CHECK_VALUE(result.list[0].ea_list.eas.eas[1].value.length, 0);
1284
1285         CHECK_STRING(result.list[1].ea_list.name.s, "file2.txt");
1286         CHECK_STRING(result.list[1].ea_list.eas.eas[0].name.s, "SECOND EA");
1287         CHECK_VALUE(result.list[1].ea_list.eas.eas[0].value.length, 9);
1288         CHECK_STRING(result.list[1].ea_list.eas.eas[0].value.data, "Value Two");
1289         CHECK_STRING(result.list[1].ea_list.eas.eas[1].name.s, "THIRD EA");
1290         CHECK_VALUE(result.list[1].ea_list.eas.eas[1].value.length, 0);
1291
1292         CHECK_STRING(result.list[2].ea_list.name.s, "file3.txt");
1293         CHECK_STRING(result.list[2].ea_list.eas.eas[0].name.s, "SECOND EA");
1294         CHECK_VALUE(result.list[2].ea_list.eas.eas[0].value.length, 9);
1295         CHECK_STRING(result.list[2].ea_list.eas.eas[0].value.data, "Value Two");
1296         CHECK_STRING(result.list[2].ea_list.eas.eas[1].name.s, "THIRD EA");
1297         CHECK_VALUE(result.list[2].ea_list.eas.eas[1].value.length, 0);
1298
1299 done:
1300         smb_raw_exit(cli->session);
1301         smbcli_deltree(cli->tree, BASEDIR);
1302
1303         return ret;
1304 }
1305
1306
1307
1308 /* 
1309    basic testing of all RAW_SEARCH_* calls using a single file
1310 */
1311 BOOL torture_raw_search(void)
1312 {
1313         struct smbcli_state *cli;
1314         BOOL ret = True;
1315         TALLOC_CTX *mem_ctx;
1316
1317         if (!torture_open_connection(&cli)) {
1318                 return False;
1319         }
1320
1321         mem_ctx = talloc_init("torture_search");
1322
1323         ret &= test_one_file(cli, mem_ctx);
1324         ret &= test_many_files(cli, mem_ctx);
1325         ret &= test_sorted(cli, mem_ctx);
1326         ret &= test_modify_search(cli, mem_ctx);
1327         ret &= test_many_dirs(cli, mem_ctx);
1328         ret &= test_os2_delete(cli, mem_ctx);
1329         ret &= test_ea_list(cli, mem_ctx);
1330
1331         torture_close_connection(cli);
1332         talloc_free(mem_ctx);
1333         
1334         return ret;
1335 }