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