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