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