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