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