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