r8176: Exploring the share mode database...
[samba.git] / source / 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                                union smb_search_data *data)
48 {
49         union smb_search_first io;
50         union smb_search_close c;
51         NTSTATUS status;
52
53         io.generic.level = level;
54         if (level == RAW_SEARCH_SEARCH ||
55             level == RAW_SEARCH_FFIRST ||
56             level == RAW_SEARCH_FUNIQUE) {
57                 io.search_first.in.max_count = 1;
58                 io.search_first.in.search_attrib = 0;
59                 io.search_first.in.pattern = pattern;
60         } else {
61                 io.t2ffirst.in.search_attrib = 0;
62                 io.t2ffirst.in.max_count = 1;
63                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
64                 io.t2ffirst.in.storage_type = 0;
65                 io.t2ffirst.in.pattern = pattern;
66         }
67
68         status = smb_raw_search_first(cli->tree, mem_ctx,
69                                       &io, (void *)data, single_search_callback);
70
71         if (NT_STATUS_IS_OK(status) && level == RAW_SEARCH_FFIRST) {
72                 c.fclose.level = RAW_FINDCLOSE_FCLOSE;
73                 c.fclose.in.max_count = 1;
74                 c.fclose.in.search_attrib = 0;
75                 c.fclose.in.id = data->search.id;
76                 status = smb_raw_search_close(cli->tree, &c);
77         }
78         
79         return status;
80 }
81
82
83 static struct {
84         const char *name;
85         enum smb_search_level level;
86         uint32_t capability_mask;
87         NTSTATUS status;
88         union smb_search_data data;
89 } levels[] = {
90         {"FFIRST",                 RAW_SEARCH_FFIRST, },
91         {"FUNIQUE",                RAW_SEARCH_FUNIQUE, },
92         {"SEARCH",                 RAW_SEARCH_SEARCH, },
93         {"STANDARD",               RAW_SEARCH_STANDARD, },
94         {"EA_SIZE",                RAW_SEARCH_EA_SIZE, },
95         {"DIRECTORY_INFO",         RAW_SEARCH_DIRECTORY_INFO, },
96         {"FULL_DIRECTORY_INFO",    RAW_SEARCH_FULL_DIRECTORY_INFO, },
97         {"NAME_INFO",              RAW_SEARCH_NAME_INFO, },
98         {"BOTH_DIRECTORY_INFO",    RAW_SEARCH_BOTH_DIRECTORY_INFO, },
99         {"ID_FULL_DIRECTORY_INFO", RAW_SEARCH_ID_FULL_DIRECTORY_INFO, },
100         {"ID_BOTH_DIRECTORY_INFO", RAW_SEARCH_ID_BOTH_DIRECTORY_INFO, },
101         {"UNIX_INFO",              RAW_SEARCH_UNIX_INFO, CAP_UNIX}
102 };
103
104 /* find a level in the table by name */
105 static union smb_search_data *find(const char *name)
106 {
107         int i;
108         for (i=0;i<ARRAY_SIZE(levels);i++) {
109                 if (NT_STATUS_IS_OK(levels[i].status) && 
110                     strcmp(levels[i].name, name) == 0) {
111                         return &levels[i].data;
112                 }
113         }
114         return NULL;
115 }
116
117 /* 
118    basic testing of all RAW_SEARCH_* calls using a single file
119 */
120 static BOOL test_one_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
121 {
122         BOOL ret = True;
123         int fnum;
124         const char *fname = "\\torture_search.txt";
125         const char *fname2 = "\\torture_search-NOTEXIST.txt";
126         NTSTATUS status;
127         int i;
128         union smb_fileinfo all_info, alt_info, name_info, internal_info;
129         union smb_search_data *s;
130
131         printf("Testing one file searches\n");
132
133         fnum = create_complex_file(cli, mem_ctx, fname);
134         if (fnum == -1) {
135                 printf("ERROR: open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
136                 ret = False;
137                 goto done;
138         }
139
140         /* call all the levels */
141         for (i=0;i<ARRAY_SIZE(levels);i++) {
142                 NTSTATUS expected_status;
143                 uint32_t cap = cli->transport->negotiate.capabilities;
144
145                 printf("testing %s\n", levels[i].name);
146
147                 levels[i].status = torture_single_search(cli, mem_ctx, fname, 
148                                                          levels[i].level,
149                                                          &levels[i].data);
150
151                 /* see if this server claims to support this level */
152                 if ((cap & levels[i].capability_mask) != levels[i].capability_mask) {
153                         printf("search level %s(%d) not supported by server\n",
154                                levels[i].name, (int)levels[i].level);
155                         continue;
156                 }
157
158                 if (!NT_STATUS_IS_OK(levels[i].status)) {
159                         printf("search level %s(%d) failed - %s\n",
160                                levels[i].name, (int)levels[i].level, 
161                                nt_errstr(levels[i].status));
162                         ret = False;
163                         continue;
164                 }
165
166                 status = torture_single_search(cli, mem_ctx, fname2, 
167                                                levels[i].level,
168                                                &levels[i].data);
169                 
170                 expected_status = NT_STATUS_NO_SUCH_FILE;
171                 if (levels[i].level == RAW_SEARCH_SEARCH ||
172                     levels[i].level == RAW_SEARCH_FFIRST ||
173                     levels[i].level == RAW_SEARCH_FUNIQUE) {
174                         expected_status = STATUS_NO_MORE_FILES;
175                 }
176                 if (!NT_STATUS_EQUAL(status, expected_status)) {
177                         printf("search level %s(%d) should fail with %s - %s\n",
178                                levels[i].name, (int)levels[i].level, 
179                                nt_errstr(expected_status),
180                                nt_errstr(status));
181                         ret = False;
182                 }
183         }
184
185         /* get the all_info file into to check against */
186         all_info.generic.level = RAW_FILEINFO_ALL_INFO;
187         all_info.generic.in.fname = fname;
188         status = smb_raw_pathinfo(cli->tree, mem_ctx, &all_info);
189         if (!NT_STATUS_IS_OK(status)) {
190                 printf("RAW_FILEINFO_ALL_INFO failed - %s\n", nt_errstr(status));
191                 ret = False;
192                 goto done;
193         }
194
195         alt_info.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
196         alt_info.generic.in.fname = fname;
197         status = smb_raw_pathinfo(cli->tree, mem_ctx, &alt_info);
198         if (!NT_STATUS_IS_OK(status)) {
199                 printf("RAW_FILEINFO_ALT_NAME_INFO failed - %s\n", nt_errstr(status));
200                 ret = False;
201                 goto done;
202         }
203
204         internal_info.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
205         internal_info.generic.in.fname = fname;
206         status = smb_raw_pathinfo(cli->tree, mem_ctx, &internal_info);
207         if (!NT_STATUS_IS_OK(status)) {
208                 printf("RAW_FILEINFO_INTERNAL_INFORMATION failed - %s\n", nt_errstr(status));
209                 ret = False;
210                 goto done;
211         }
212
213         name_info.generic.level = RAW_FILEINFO_NAME_INFO;
214         name_info.generic.in.fname = fname;
215         status = smb_raw_pathinfo(cli->tree, mem_ctx, &name_info);
216         if (!NT_STATUS_IS_OK(status)) {
217                 printf("RAW_FILEINFO_NAME_INFO failed - %s\n", nt_errstr(status));
218                 ret = False;
219                 goto done;
220         }
221
222 #define CHECK_VAL(name, sname1, field1, v, sname2, field2) do { \
223         s = find(name); \
224         if (s) { \
225                 if ((s->sname1.field1) != (v.sname2.out.field2)) { \
226                         printf("(%s) %s/%s [0x%x] != %s/%s [0x%x]\n", \
227                                __location__, \
228                                 #sname1, #field1, (int)s->sname1.field1, \
229                                 #sname2, #field2, (int)v.sname2.out.field2); \
230                         ret = False; \
231                 } \
232         }} while (0)
233
234 #define CHECK_TIME(name, sname1, field1, v, sname2, field2) do { \
235         s = find(name); \
236         if (s) { \
237                 if (s->sname1.field1 != (~1 & nt_time_to_unix(v.sname2.out.field2))) { \
238                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
239                                __location__, \
240                                 #sname1, #field1, timestring(mem_ctx, s->sname1.field1), \
241                                 #sname2, #field2, nt_time_string(mem_ctx, v.sname2.out.field2)); \
242                         ret = False; \
243                 } \
244         }} while (0)
245
246 #define CHECK_NTTIME(name, sname1, field1, v, sname2, field2) do { \
247         s = find(name); \
248         if (s) { \
249                 if (s->sname1.field1 != v.sname2.out.field2) { \
250                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
251                                __location__, \
252                                 #sname1, #field1, nt_time_string(mem_ctx, s->sname1.field1), \
253                                 #sname2, #field2, nt_time_string(mem_ctx, v.sname2.out.field2)); \
254                         ret = False; \
255                 } \
256         }} while (0)
257
258 #define CHECK_STR(name, sname1, field1, v, sname2, field2) do { \
259         s = find(name); \
260         if (s) { \
261                 if (!s->sname1.field1 || strcmp(s->sname1.field1, v.sname2.out.field2.s)) { \
262                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
263                                __location__, \
264                                 #sname1, #field1, s->sname1.field1, \
265                                 #sname2, #field2, v.sname2.out.field2.s); \
266                         ret = False; \
267                 } \
268         }} while (0)
269
270 #define CHECK_WSTR(name, sname1, field1, v, sname2, field2, flags) do { \
271         s = find(name); \
272         if (s) { \
273                 if (!s->sname1.field1.s || \
274                     strcmp(s->sname1.field1.s, v.sname2.out.field2.s) || \
275                     wire_bad_flags(&s->sname1.field1, flags, cli)) { \
276                         printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
277                                __location__, \
278                                 #sname1, #field1, s->sname1.field1.s, \
279                                 #sname2, #field2, v.sname2.out.field2.s); \
280                         ret = False; \
281                 } \
282         }} while (0)
283
284 #define CHECK_NAME(name, sname1, field1, fname, flags) do { \
285         s = find(name); \
286         if (s) { \
287                 if (!s->sname1.field1.s || \
288                     strcmp(s->sname1.field1.s, fname) || \
289                     wire_bad_flags(&s->sname1.field1, flags, cli)) { \
290                         printf("(%s) %s/%s [%s] != %s\n", \
291                                __location__, \
292                                 #sname1, #field1, s->sname1.field1.s, \
293                                 fname); \
294                         ret = False; \
295                 } \
296         }} while (0)
297
298 #define CHECK_UNIX_NAME(name, sname1, field1, fname, flags) do { \
299         s = find(name); \
300         if (s) { \
301                 if (!s->sname1.field1 || \
302                     strcmp(s->sname1.field1, fname)) { \
303                         printf("(%s) %s/%s [%s] != %s\n", \
304                                __location__, \
305                                 #sname1, #field1, s->sname1.field1, \
306                                 fname); \
307                         ret = False; \
308                 } \
309         }} while (0)
310         
311         /* check that all the results are as expected */
312         CHECK_VAL("SEARCH",              search,              attrib, all_info, all_info, attrib&0xFFF);
313         CHECK_VAL("STANDARD",            standard,            attrib, all_info, all_info, attrib&0xFFF);
314         CHECK_VAL("EA_SIZE",             ea_size,             attrib, all_info, all_info, attrib&0xFFF);
315         CHECK_VAL("DIRECTORY_INFO",      directory_info,      attrib, all_info, all_info, attrib);
316         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, attrib, all_info, all_info, attrib);
317         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, attrib, all_info, all_info, attrib);
318         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           attrib, all_info, all_info, attrib);
319         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           attrib, all_info, all_info, attrib);
320
321         CHECK_TIME("SEARCH",             search,              write_time, all_info, all_info, write_time);
322         CHECK_TIME("STANDARD",           standard,            write_time, all_info, all_info, write_time);
323         CHECK_TIME("EA_SIZE",            ea_size,             write_time, all_info, all_info, write_time);
324         CHECK_TIME("STANDARD",           standard,            create_time, all_info, all_info, create_time);
325         CHECK_TIME("EA_SIZE",            ea_size,             create_time, all_info, all_info, create_time);
326         CHECK_TIME("STANDARD",           standard,            access_time, all_info, all_info, access_time);
327         CHECK_TIME("EA_SIZE",            ea_size,             access_time, all_info, all_info, access_time);
328
329         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      write_time, all_info, all_info, write_time);
330         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, write_time, all_info, all_info, write_time);
331         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, write_time, all_info, all_info, write_time);
332         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           write_time, all_info, all_info, write_time);
333         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           write_time, all_info, all_info, write_time);
334
335         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      create_time, all_info, all_info, create_time);
336         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
337         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
338         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           create_time, all_info, all_info, create_time);
339         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           create_time, all_info, all_info, create_time);
340
341         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      access_time, all_info, all_info, access_time);
342         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, access_time, all_info, all_info, access_time);
343         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, access_time, all_info, all_info, access_time);
344         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           access_time, all_info, all_info, access_time);
345         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           access_time, all_info, all_info, access_time);
346
347         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      create_time, all_info, all_info, create_time);
348         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
349         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
350         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           create_time, all_info, all_info, create_time);
351         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           create_time, all_info, all_info, create_time);
352
353         CHECK_VAL("SEARCH",              search,              size, all_info, all_info, size);
354         CHECK_VAL("STANDARD",            standard,            size, all_info, all_info, size);
355         CHECK_VAL("EA_SIZE",             ea_size,             size, all_info, all_info, size);
356         CHECK_VAL("DIRECTORY_INFO",      directory_info,      size, all_info, all_info, size);
357         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, size, all_info, all_info, size);
358         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, size, all_info, all_info, size);
359         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           size, all_info, all_info, size);
360         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           size, all_info, all_info, size);
361         CHECK_VAL("UNIX_INFO",           unix_info,           size, all_info, all_info, size);
362
363         CHECK_VAL("STANDARD",            standard,            alloc_size, all_info, all_info, alloc_size);
364         CHECK_VAL("EA_SIZE",             ea_size,             alloc_size, all_info, all_info, alloc_size);
365         CHECK_VAL("DIRECTORY_INFO",      directory_info,      alloc_size, all_info, all_info, alloc_size);
366         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, alloc_size, all_info, all_info, alloc_size);
367         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, alloc_size, all_info, all_info, alloc_size);
368         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           alloc_size, all_info, all_info, alloc_size);
369         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           alloc_size, all_info, all_info, alloc_size);
370         CHECK_VAL("UNIX_INFO",           unix_info,           alloc_size, all_info, all_info, alloc_size);
371
372         CHECK_VAL("EA_SIZE",             ea_size,             ea_size, all_info, all_info, ea_size);
373         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, ea_size, all_info, all_info, ea_size);
374         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, ea_size, all_info, all_info, ea_size);
375         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           ea_size, all_info, all_info, ea_size);
376         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           ea_size, all_info, all_info, ea_size);
377
378         CHECK_STR("SEARCH", search, name, alt_info, alt_name_info, fname);
379         CHECK_WSTR("BOTH_DIRECTORY_INFO", both_directory_info, short_name, alt_info, alt_name_info, fname, STR_UNICODE);
380
381         CHECK_NAME("STANDARD",            standard,            name, fname+1, 0);
382         CHECK_NAME("EA_SIZE",             ea_size,             name, fname+1, 0);
383         CHECK_NAME("DIRECTORY_INFO",      directory_info,      name, fname+1, STR_TERMINATE_ASCII);
384         CHECK_NAME("FULL_DIRECTORY_INFO", full_directory_info, name, fname+1, STR_TERMINATE_ASCII);
385         CHECK_NAME("NAME_INFO",           name_info,           name, fname+1, STR_TERMINATE_ASCII);
386         CHECK_NAME("BOTH_DIRECTORY_INFO", both_directory_info, name, fname+1, STR_TERMINATE_ASCII);
387         CHECK_NAME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           name, fname+1, STR_TERMINATE_ASCII);
388         CHECK_NAME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           name, fname+1, STR_TERMINATE_ASCII);
389         CHECK_UNIX_NAME("UNIX_INFO",           unix_info,           name, fname+1, STR_TERMINATE_ASCII);
390
391         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, file_id, internal_info, internal_information, file_id);
392         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, file_id, internal_info, internal_information, file_id);
393
394 done:
395         smb_raw_exit(cli->session);
396         smbcli_unlink(cli->tree, fname);
397
398         return ret;
399 }
400
401
402 struct multiple_result {
403         TALLOC_CTX *mem_ctx;
404         int count;
405         union smb_search_data *list;
406 };
407
408 /*
409   callback function for multiple_search
410 */
411 static BOOL multiple_search_callback(void *private, union smb_search_data *file)
412 {
413         struct multiple_result *data = private;
414
415
416         data->count++;
417         data->list = talloc_realloc(data->mem_ctx,
418                                       data->list, 
419                                       union smb_search_data,
420                                       data->count);
421
422         data->list[data->count-1] = *file;
423
424         return True;
425 }
426
427 enum continue_type {CONT_FLAGS, CONT_NAME, CONT_RESUME_KEY};
428
429 /*
430   do a single file (non-wildcard) search 
431 */
432 static NTSTATUS multiple_search(struct smbcli_state *cli, 
433                                 TALLOC_CTX *mem_ctx,
434                                 const char *pattern,
435                                 enum smb_search_level level,
436                                 enum continue_type cont_type,
437                                 void *data)
438 {
439         union smb_search_first io;
440         union smb_search_next io2;
441         NTSTATUS status;
442         const int per_search = 300;
443         struct multiple_result *result = data;
444
445         io.generic.level = level;
446         if (level == RAW_SEARCH_SEARCH) {
447                 io.search_first.in.max_count = per_search;
448                 io.search_first.in.search_attrib = 0;
449                 io.search_first.in.pattern = pattern;
450         } else {
451                 io.t2ffirst.in.search_attrib = 0;
452                 io.t2ffirst.in.max_count = per_search;
453                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
454                 io.t2ffirst.in.storage_type = 0;
455                 io.t2ffirst.in.pattern = pattern;
456                 if (cont_type == CONT_RESUME_KEY) {
457                         io.t2ffirst.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME | 
458                                 FLAG_TRANS2_FIND_BACKUP_INTENT;
459                 }
460         }
461
462         status = smb_raw_search_first(cli->tree, mem_ctx,
463                                       &io, data, multiple_search_callback);
464         
465
466         while (NT_STATUS_IS_OK(status)) {
467                 io2.generic.level = level;
468                 if (level == RAW_SEARCH_SEARCH) {
469                         io2.search_next.in.max_count = per_search;
470                         io2.search_next.in.search_attrib = 0;
471                         io2.search_next.in.id = result->list[result->count-1].search.id;
472                 } else {
473                         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
474                         io2.t2fnext.in.max_count = per_search;
475                         io2.t2fnext.in.resume_key = 0;
476                         io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
477                         io2.t2fnext.in.last_name = "";
478                         switch (cont_type) {
479                         case CONT_RESUME_KEY:
480                                 if (level == RAW_SEARCH_STANDARD) {
481                                         io2.t2fnext.in.resume_key = 
482                                                 result->list[result->count-1].standard.resume_key;
483                                 } else if (level == RAW_SEARCH_EA_SIZE) {
484                                         io2.t2fnext.in.resume_key = 
485                                                 result->list[result->count-1].ea_size.resume_key;
486                                 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
487                                         io2.t2fnext.in.resume_key = 
488                                                 result->list[result->count-1].directory_info.file_index;
489                                 } else {
490                                         io2.t2fnext.in.resume_key = 
491                                                 result->list[result->count-1].both_directory_info.file_index;
492                                 }
493                                 if (io2.t2fnext.in.resume_key == 0) {
494                                         printf("Server does not support resume by key\n");
495                                         return NT_STATUS_NOT_SUPPORTED;
496                                 }
497                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
498                                         FLAG_TRANS2_FIND_BACKUP_INTENT;
499                                 break;
500                         case CONT_NAME:
501                                 if (level == RAW_SEARCH_STANDARD) {
502                                         io2.t2fnext.in.last_name = 
503                                                 result->list[result->count-1].standard.name.s;
504                                 } else if (level == RAW_SEARCH_EA_SIZE) {
505                                         io2.t2fnext.in.last_name = 
506                                                 result->list[result->count-1].ea_size.name.s;
507                                 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
508                                         io2.t2fnext.in.last_name = 
509                                                 result->list[result->count-1].directory_info.name.s;
510                                 } else {
511                                         io2.t2fnext.in.last_name = 
512                                                 result->list[result->count-1].both_directory_info.name.s;
513                                 }
514                                 break;
515                         case CONT_FLAGS:
516                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_CONTINUE;
517                                 break;
518                         }
519                 }
520
521                 status = smb_raw_search_next(cli->tree, mem_ctx,
522                                              &io2, data, multiple_search_callback);
523                 if (!NT_STATUS_IS_OK(status)) {
524                         break;
525                 }
526                 if (level == RAW_SEARCH_SEARCH) {
527                         if (io2.search_next.out.count == 0) {
528                                 break;
529                         }
530                 } else if (io2.t2fnext.out.count == 0 ||
531                            io2.t2fnext.out.end_of_search) {
532                         break;
533                 }
534         }
535
536         return status;
537 }
538
539 #define CHECK_STATUS(status, correct) do { \
540         if (!NT_STATUS_EQUAL(status, correct)) { \
541                 printf("(%s) Incorrect status %s - should be %s\n", \
542                        __location__, nt_errstr(status), nt_errstr(correct)); \
543                 ret = False; \
544                 goto done; \
545         }} while (0)
546
547 #define CHECK_VALUE(v, correct) do { \
548         if ((v) != (correct)) { \
549                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
550                        __location__, #v, v, correct); \
551                 ret = False; \
552         }} while (0)
553
554 #define CHECK_STRING(v, correct) do { \
555         if (StrCaseCmp(v, correct) != 0) { \
556                 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
557                        __location__, #v, v, correct); \
558                 ret = False; \
559         }} while (0)
560
561
562 static int search_both_compare(union smb_search_data *d1, union smb_search_data *d2)
563 {
564         return strcmp_safe(d1->both_directory_info.name.s, d2->both_directory_info.name.s);
565 }
566
567 static int search_standard_compare(union smb_search_data *d1, union smb_search_data *d2)
568 {
569         return strcmp_safe(d1->standard.name.s, d2->standard.name.s);
570 }
571
572 static int search_ea_size_compare(union smb_search_data *d1, union smb_search_data *d2)
573 {
574         return strcmp_safe(d1->ea_size.name.s, d2->ea_size.name.s);
575 }
576
577 static int search_directory_info_compare(union smb_search_data *d1, union smb_search_data *d2)
578 {
579         return strcmp_safe(d1->directory_info.name.s, d2->directory_info.name.s);
580 }
581
582 static int search_old_compare(union smb_search_data *d1, union smb_search_data *d2)
583 {
584         return strcmp_safe(d1->search.name, d2->search.name);
585 }
586
587
588 /* 
589    basic testing of search calls using many files
590 */
591 static BOOL test_many_files(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
592 {
593         const int num_files = 700;
594         int i, fnum, t;
595         char *fname;
596         BOOL ret = True;
597         NTSTATUS status;
598         struct multiple_result result;
599         struct {
600                 const char *name;
601                 const char *cont_name;
602                 enum smb_search_level level;
603                 enum continue_type cont_type;
604         } search_types[] = {
605                 {"SEARCH",              "ID",    RAW_SEARCH_SEARCH,              CONT_RESUME_KEY},
606                 {"BOTH_DIRECTORY_INFO", "NAME",  RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_NAME},
607                 {"BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_FLAGS},
608                 {"BOTH_DIRECTORY_INFO", "KEY",   RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY},
609                 {"STANDARD",            "FLAGS", RAW_SEARCH_STANDARD,            CONT_FLAGS},
610                 {"STANDARD",            "KEY",   RAW_SEARCH_STANDARD,            CONT_RESUME_KEY},
611                 {"STANDARD",            "NAME",  RAW_SEARCH_STANDARD,            CONT_NAME},
612                 {"EA_SIZE",             "FLAGS", RAW_SEARCH_EA_SIZE,             CONT_FLAGS},
613                 {"EA_SIZE",             "KEY",   RAW_SEARCH_EA_SIZE,             CONT_RESUME_KEY},
614                 {"EA_SIZE",             "NAME",  RAW_SEARCH_EA_SIZE,             CONT_NAME},
615                 {"DIRECTORY_INFO",      "FLAGS", RAW_SEARCH_DIRECTORY_INFO,      CONT_FLAGS},
616                 {"DIRECTORY_INFO",      "KEY",   RAW_SEARCH_DIRECTORY_INFO,      CONT_RESUME_KEY},
617                 {"DIRECTORY_INFO",      "NAME",  RAW_SEARCH_DIRECTORY_INFO,      CONT_NAME}
618         };
619
620         if (!torture_setup_dir(cli, BASEDIR)) {
621                 return False;
622         }
623
624         printf("Creating %d files\n", num_files);
625
626         for (i=0;i<num_files;i++) {
627                 asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
628                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
629                 if (fnum == -1) {
630                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
631                         ret = False;
632                         goto done;
633                 }
634                 free(fname);
635                 smbcli_close(cli->tree, fnum);
636         }
637
638
639         for (t=0;t<ARRAY_SIZE(search_types);t++) {
640                 ZERO_STRUCT(result);
641                 result.mem_ctx = talloc_new(mem_ctx);
642         
643                 printf("Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
644
645                 status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*", 
646                                          search_types[t].level,
647                                          search_types[t].cont_type,
648                                          &result);
649         
650                 if (!NT_STATUS_IS_OK(status)) {
651                         printf("search failed - %s\n", nt_errstr(status));
652                         ret = False;
653                         continue;
654                 }
655                 CHECK_VALUE(result.count, num_files);
656
657                 if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
658                         qsort(result.list, result.count, sizeof(result.list[0]), 
659                               QSORT_CAST  search_both_compare);
660                 } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
661                         qsort(result.list, result.count, sizeof(result.list[0]), 
662                               QSORT_CAST search_standard_compare);
663                 } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
664                         qsort(result.list, result.count, sizeof(result.list[0]), 
665                               QSORT_CAST search_ea_size_compare);
666                 } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
667                         qsort(result.list, result.count, sizeof(result.list[0]), 
668                               QSORT_CAST search_directory_info_compare);
669                 } else {
670                         qsort(result.list, result.count, sizeof(result.list[0]), 
671                               QSORT_CAST search_old_compare);
672                 }
673
674                 for (i=0;i<result.count;i++) {
675                         const char *s;
676                         if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
677                                 s = result.list[i].both_directory_info.name.s;
678                         } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
679                                 s = result.list[i].standard.name.s;
680                         } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
681                                 s = result.list[i].ea_size.name.s;
682                         } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
683                                 s = result.list[i].directory_info.name.s;
684                         } else {
685                                 s = result.list[i].search.name;
686                         }
687                         asprintf(&fname, "t%03d-%d.txt", i, i);
688                         if (strcmp(fname, s)) {
689                                 printf("Incorrect name %s at entry %d\n", s, i);
690                                 ret = False;
691                                 break;
692                         }
693                         free(fname);
694                 }
695                 talloc_free(result.mem_ctx);
696         }
697
698 done:
699         smb_raw_exit(cli->session);
700         smbcli_deltree(cli->tree, BASEDIR);
701
702         return ret;
703 }
704
705 /*
706   check a individual file result
707 */
708 static BOOL check_result(struct multiple_result *result, const char *name, BOOL exist, uint32_t attrib)
709 {
710         int i;
711         for (i=0;i<result->count;i++) {
712                 if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
713         }
714         if (i == result->count) {
715                 if (exist) {
716                         printf("failed: '%s' should exist with attribute %s\n", 
717                                name, attrib_string(result->list, attrib));
718                         return False;
719                 }
720                 return True;
721         }
722
723         if (!exist) {
724                 printf("failed: '%s' should NOT exist (has attribute %s)\n", 
725                        name, attrib_string(result->list, result->list[i].both_directory_info.attrib));
726                 return False;
727         }
728
729         if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
730                 printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
731                        name, 
732                        attrib, result->list[i].both_directory_info.attrib);
733                 return False;
734         }
735         return True;
736 }
737
738 /* 
739    test what happens when the directory is modified during a search
740 */
741 static BOOL test_modify_search(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
742 {
743         const int num_files = 20;
744         int i, fnum;
745         char *fname;
746         BOOL ret = True;
747         NTSTATUS status;
748         struct multiple_result result;
749         union smb_search_first io;
750         union smb_search_next io2;
751         union smb_setfileinfo sfinfo;
752
753         if (!torture_setup_dir(cli, BASEDIR)) {
754                 return False;
755         }
756
757         printf("Creating %d files\n", num_files);
758
759         for (i=num_files-1;i>=0;i--) {
760                 asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
761                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
762                 if (fnum == -1) {
763                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
764                         ret = False;
765                         goto done;
766                 }
767                 free(fname);
768                 smbcli_close(cli->tree, fnum);
769         }
770
771         printf("pulling the first file\n");
772         ZERO_STRUCT(result);
773         result.mem_ctx = talloc_new(mem_ctx);
774
775         io.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
776         io.t2ffirst.in.search_attrib = 0;
777         io.t2ffirst.in.max_count = 0;
778         io.t2ffirst.in.flags = 0;
779         io.t2ffirst.in.storage_type = 0;
780         io.t2ffirst.in.pattern = BASEDIR "\\*.*";
781
782         status = smb_raw_search_first(cli->tree, mem_ctx,
783                                       &io, &result, multiple_search_callback);
784         CHECK_STATUS(status, NT_STATUS_OK);
785         CHECK_VALUE(result.count, 1);
786         
787         printf("pulling the second file\n");
788         io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
789         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
790         io2.t2fnext.in.max_count = 1;
791         io2.t2fnext.in.resume_key = 0;
792         io2.t2fnext.in.flags = 0;
793         if (result.count == 0) {
794                 io2.t2fnext.in.last_name = "";
795         } else {
796                 io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
797         }
798
799         status = smb_raw_search_next(cli->tree, mem_ctx,
800                                      &io2, &result, multiple_search_callback);
801         CHECK_STATUS(status, NT_STATUS_OK);
802         CHECK_VALUE(result.count, 2);
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 - 1;
822         io2.t2fnext.in.resume_key = 0;
823         io2.t2fnext.in.flags = 0;
824         io2.t2fnext.in.last_name = result.list[result.count-2].both_directory_info.name.s;
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, 21);
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                 asprintf(&fname, 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                 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(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 = 300;
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                 asprintf(&dname, 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                         asprintf(&fname, 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                         free(fname);
960                 }
961
962                 free(dname);
963                 smbcli_close(cli->tree, fnum);
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                 asprintf(&fname, 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                 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                 asprintf(&fname, 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         }
1137
1138         io2.t2fnext.level = RAW_SEARCH_EA_SIZE;
1139         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1140         io2.t2fnext.in.max_count = 100;
1141         io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1142         io2.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1143         io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1144
1145         do {
1146                 ZERO_STRUCT(result);
1147                 result.mem_ctx = mem_ctx;
1148
1149                 status = smb_raw_search_next(cli->tree, mem_ctx,
1150                                              &io2, &result, multiple_search_callback);
1151                 if (!NT_STATUS_IS_OK(status)) {
1152                         break;
1153                 }
1154
1155                 for (i=0;i<MIN(result.count, delete_count);i++) {
1156                         asprintf(&fname, BASEDIR "\\%s", result.list[i].ea_size.name.s);
1157                         status = smbcli_unlink(cli->tree, fname);
1158                         CHECK_STATUS(status, NT_STATUS_OK);
1159                         total_deleted++;
1160                 }
1161
1162                 if (i>0) {
1163                         io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1164                         io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1165                 }
1166         } while (NT_STATUS_IS_OK(status) && result.count != 0);
1167
1168         CHECK_STATUS(status, NT_STATUS_OK);
1169
1170         if (total_deleted != num_files) {
1171                 printf("error: deleted %d - expected to delete %d\n", 
1172                        total_deleted, num_files);
1173                 ret = False;
1174         }
1175
1176 done:
1177         smb_raw_exit(cli->session);
1178         smbcli_deltree(cli->tree, BASEDIR);
1179
1180         return ret;
1181 }
1182
1183
1184 /* 
1185    testing of the rather strange ea_list level
1186 */
1187 static BOOL test_ea_list(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
1188 {
1189         int  fnum;
1190         BOOL ret = True;
1191         NTSTATUS status;
1192         union smb_search_first io;
1193         union smb_search_next nxt;
1194         struct multiple_result result;
1195         union smb_setfileinfo setfile;
1196
1197         if (!torture_setup_dir(cli, BASEDIR)) {
1198                 return False;
1199         }
1200
1201         printf("Testing RAW_SEARCH_EA_LIST level\n");
1202
1203         fnum = smbcli_open(cli->tree, BASEDIR "\\file1.txt", O_CREAT|O_RDWR, DENY_NONE);
1204         smbcli_close(cli->tree, fnum);
1205
1206         fnum = smbcli_open(cli->tree, BASEDIR "\\file2.txt", O_CREAT|O_RDWR, DENY_NONE);
1207         smbcli_close(cli->tree, fnum);
1208
1209         fnum = smbcli_open(cli->tree, BASEDIR "\\file3.txt", O_CREAT|O_RDWR, DENY_NONE);
1210         smbcli_close(cli->tree, fnum);
1211
1212         setfile.generic.level = RAW_SFILEINFO_EA_SET;
1213         setfile.generic.file.fname = BASEDIR "\\file2.txt";
1214         setfile.ea_set.in.num_eas = 2;
1215         setfile.ea_set.in.eas = talloc_array(mem_ctx, struct ea_struct, 2);
1216         setfile.ea_set.in.eas[0].flags = 0;
1217         setfile.ea_set.in.eas[0].name.s = "EA ONE";
1218         setfile.ea_set.in.eas[0].value = data_blob_string_const("VALUE 1");
1219         setfile.ea_set.in.eas[1].flags = 0;
1220         setfile.ea_set.in.eas[1].name.s = "SECOND EA";
1221         setfile.ea_set.in.eas[1].value = data_blob_string_const("Value Two");
1222
1223         status = smb_raw_setpathinfo(cli->tree, &setfile);
1224         CHECK_STATUS(status, NT_STATUS_OK);
1225
1226         setfile.generic.file.fname = BASEDIR "\\file3.txt";
1227         status = smb_raw_setpathinfo(cli->tree, &setfile);
1228         CHECK_STATUS(status, NT_STATUS_OK);
1229         
1230         ZERO_STRUCT(result);
1231         result.mem_ctx = mem_ctx;
1232
1233         io.t2ffirst.level = RAW_SEARCH_EA_LIST;
1234         io.t2ffirst.in.search_attrib = 0;
1235         io.t2ffirst.in.max_count = 2;
1236         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1237         io.t2ffirst.in.storage_type = 0;
1238         io.t2ffirst.in.pattern = BASEDIR "\\*";
1239         io.t2ffirst.in.num_names = 2;
1240         io.t2ffirst.in.ea_names = talloc_array(mem_ctx, struct ea_name, 2);
1241         io.t2ffirst.in.ea_names[0].name.s = "SECOND EA";
1242         io.t2ffirst.in.ea_names[1].name.s = "THIRD EA";
1243
1244         status = smb_raw_search_first(cli->tree, mem_ctx,
1245                                       &io, &result, multiple_search_callback);
1246         CHECK_STATUS(status, NT_STATUS_OK);
1247         CHECK_VALUE(result.count, 2);
1248
1249         nxt.t2fnext.level = RAW_SEARCH_EA_LIST;
1250         nxt.t2fnext.in.handle = io.t2ffirst.out.handle;
1251         nxt.t2fnext.in.max_count = 2;
1252         nxt.t2fnext.in.resume_key = result.list[1].ea_list.resume_key;
1253         nxt.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME | FLAG_TRANS2_FIND_CONTINUE;
1254         nxt.t2fnext.in.last_name = "file2.txt";
1255         nxt.t2fnext.in.num_names = 2;
1256         nxt.t2fnext.in.ea_names = talloc_array(mem_ctx, struct ea_name, 2);
1257         nxt.t2fnext.in.ea_names[0].name.s = "SECOND EA";
1258         nxt.t2fnext.in.ea_names[1].name.s = "THIRD EA";
1259
1260         status = smb_raw_search_next(cli->tree, mem_ctx,
1261                                      &nxt, &result, multiple_search_callback);
1262         CHECK_STATUS(status, NT_STATUS_OK);
1263
1264
1265         CHECK_VALUE(result.count, 3);
1266         CHECK_VALUE(result.list[0].ea_list.eas.num_eas, 2);
1267         CHECK_STRING(result.list[0].ea_list.name.s, "file1.txt");
1268         CHECK_STRING(result.list[0].ea_list.eas.eas[0].name.s, "SECOND EA");
1269         CHECK_VALUE(result.list[0].ea_list.eas.eas[0].value.length, 0);
1270         CHECK_STRING(result.list[0].ea_list.eas.eas[1].name.s, "THIRD EA");
1271         CHECK_VALUE(result.list[0].ea_list.eas.eas[1].value.length, 0);
1272
1273         CHECK_STRING(result.list[1].ea_list.name.s, "file2.txt");
1274         CHECK_STRING(result.list[1].ea_list.eas.eas[0].name.s, "SECOND EA");
1275         CHECK_VALUE(result.list[1].ea_list.eas.eas[0].value.length, 9);
1276         CHECK_STRING(result.list[1].ea_list.eas.eas[0].value.data, "Value Two");
1277         CHECK_STRING(result.list[1].ea_list.eas.eas[1].name.s, "THIRD EA");
1278         CHECK_VALUE(result.list[1].ea_list.eas.eas[1].value.length, 0);
1279
1280         CHECK_STRING(result.list[2].ea_list.name.s, "file3.txt");
1281         CHECK_STRING(result.list[2].ea_list.eas.eas[0].name.s, "SECOND EA");
1282         CHECK_VALUE(result.list[2].ea_list.eas.eas[0].value.length, 9);
1283         CHECK_STRING(result.list[2].ea_list.eas.eas[0].value.data, "Value Two");
1284         CHECK_STRING(result.list[2].ea_list.eas.eas[1].name.s, "THIRD EA");
1285         CHECK_VALUE(result.list[2].ea_list.eas.eas[1].value.length, 0);
1286
1287 done:
1288         smb_raw_exit(cli->session);
1289         smbcli_deltree(cli->tree, BASEDIR);
1290
1291         return ret;
1292 }
1293
1294
1295
1296 /* 
1297    basic testing of all RAW_SEARCH_* calls using a single file
1298 */
1299 BOOL torture_raw_search(void)
1300 {
1301         struct smbcli_state *cli;
1302         BOOL ret = True;
1303         TALLOC_CTX *mem_ctx;
1304
1305         if (!torture_open_connection(&cli)) {
1306                 return False;
1307         }
1308
1309         mem_ctx = talloc_init("torture_search");
1310
1311         ret &= test_one_file(cli, mem_ctx);
1312         ret &= test_many_files(cli, mem_ctx);
1313         ret &= test_sorted(cli, mem_ctx);
1314         ret &= test_modify_search(cli, mem_ctx);
1315         ret &= test_many_dirs(cli, mem_ctx);
1316         ret &= test_os2_delete(cli, mem_ctx);
1317         ret &= test_ea_list(cli, mem_ctx);
1318
1319         torture_close_connection(cli);
1320         talloc_free(mem_ctx);
1321         
1322         return ret;
1323 }