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