first public release of samba4 code
[samba.git] / source / torture / raw / search.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RAW_SEARCH_* individual test suite
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23
24 #define BASEDIR "\\testsearch"
25
26 /*
27   callback function for single_search
28 */
29 static BOOL single_search_callback(void *private, union smb_search_data *file)
30 {
31         union smb_search_data *data = private;
32
33         *data = *file;
34
35         return True;
36 }
37
38 /*
39   do a single file (non-wildcard) search 
40 */
41 static NTSTATUS single_search(struct cli_state *cli, 
42                               TALLOC_CTX *mem_ctx,
43                               const char *pattern,
44                               enum search_level level,
45                               union smb_search_data *data)
46 {
47         union smb_search_first io;
48         NTSTATUS status;
49
50         io.generic.level = level;
51         if (level == RAW_SEARCH_SEARCH) {
52                 io.search_first.in.max_count = 1;
53                 io.search_first.in.search_attrib = 0;
54                 io.search_first.in.pattern = pattern;
55         } else {
56                 io.t2ffirst.in.search_attrib = 0;
57                 io.t2ffirst.in.max_count = 1;
58                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
59                 io.t2ffirst.in.storage_type = 0;
60                 io.t2ffirst.in.pattern = pattern;
61         }
62
63         status = smb_raw_search_first(cli->tree, mem_ctx,
64                                       &io, (void *)data, single_search_callback);
65         
66         return status;
67 }
68
69
70 static struct {
71         const char *name;
72         enum search_level level;
73         uint32 capability_mask;
74         NTSTATUS status;
75         union smb_search_data data;
76 } levels[] = {
77         {"SEARCH",              RAW_SEARCH_SEARCH, },
78         {"STANDARD",            RAW_SEARCH_STANDARD, },
79         {"EA_SIZE",             RAW_SEARCH_EA_SIZE, },
80         {"DIRECTORY_INFO",      RAW_SEARCH_DIRECTORY_INFO, },
81         {"FULL_DIRECTORY_INFO", RAW_SEARCH_FULL_DIRECTORY_INFO, },
82         {"NAME_INFO",           RAW_SEARCH_NAME_INFO, },
83         {"BOTH_DIRECTORY_INFO", RAW_SEARCH_BOTH_DIRECTORY_INFO, },
84         {"LEVEL_261",           RAW_SEARCH_261, },
85         {"LEVEL_262",           RAW_SEARCH_262, },
86         {"UNIX_INFO",           RAW_SEARCH_UNIX_INFO, CAP_UNIX}
87 };
88
89 /* find a level in the table by name */
90 static union smb_search_data *find(const char *name)
91 {
92         int i;
93         for (i=0;i<ARRAY_SIZE(levels);i++) {
94                 if (NT_STATUS_IS_OK(levels[i].status) && 
95                     strcmp(levels[i].name, name) == 0) {
96                         return &levels[i].data;
97                 }
98         }
99         return NULL;
100 }
101
102 /* 
103    basic testing of all RAW_SEARCH_* calls using a single file
104 */
105 static BOOL test_one_file(struct cli_state *cli, TALLOC_CTX *mem_ctx)
106 {
107         BOOL ret = True;
108         int fnum;
109         const char *fname = "\\torture_search.txt";
110         NTSTATUS status;
111         int i;
112         union smb_fileinfo all_info, alt_info, name_info;
113         union smb_search_data *s;
114
115         printf("Testing one file searches\n");
116
117         fnum = create_complex_file(cli, mem_ctx, fname);
118         if (fnum == -1) {
119                 printf("ERROR: open of %s failed (%s)\n", fname, cli_errstr(cli));
120                 ret = False;
121                 goto done;
122         }
123
124         /* call all the levels */
125         for (i=0;i<ARRAY_SIZE(levels);i++) {
126                 uint32 cap = cli->transport->negotiate.capabilities;
127
128                 levels[i].status = single_search(cli, mem_ctx, fname, 
129                                                  levels[i].level, &levels[i].data);
130
131                 /* see if this server claims to support this level */
132                 if ((cap & levels[i].capability_mask) != levels[i].capability_mask) {
133                         continue;
134                 }
135
136                 printf("testing %s\n", levels[i].name);
137
138                 if (!NT_STATUS_IS_OK(levels[i].status)) {
139                         printf("search level %s(%d) failed - %s\n",
140                                levels[i].name, (int)levels[i].level, 
141                                nt_errstr(levels[i].status));
142                         ret = False;
143                 }
144         }
145
146         /* get the all_info file into to check against */
147         all_info.generic.level = RAW_FILEINFO_ALL_INFO;
148         all_info.generic.in.fname = fname;
149         status = smb_raw_pathinfo(cli->tree, mem_ctx, &all_info);
150         if (!NT_STATUS_IS_OK(status)) {
151                 printf("RAW_FILEINFO_ALL_INFO failed - %s\n", nt_errstr(status));
152                 ret = False;
153                 goto done;
154         }
155
156         alt_info.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
157         alt_info.generic.in.fname = fname;
158         status = smb_raw_pathinfo(cli->tree, mem_ctx, &alt_info);
159         if (!NT_STATUS_IS_OK(status)) {
160                 printf("RAW_FILEINFO_ALT_NAME_INFO failed - %s\n", nt_errstr(status));
161                 ret = False;
162                 goto done;
163         }
164
165         name_info.generic.level = RAW_FILEINFO_NAME_INFO;
166         name_info.generic.in.fname = fname;
167         status = smb_raw_pathinfo(cli->tree, mem_ctx, &name_info);
168         if (!NT_STATUS_IS_OK(status)) {
169                 printf("RAW_FILEINFO_NAME_INFO failed - %s\n", nt_errstr(status));
170                 ret = False;
171                 goto done;
172         }
173
174 #define CHECK_VAL(name, sname1, field1, v, sname2, field2) do { \
175         s = find(name); \
176         if (s) { \
177                 if (s->sname1.field1 != v.sname2.out.field2) { \
178                         printf("(%d) %s/%s [%d] != %s/%s [%d]\n", \
179                                __LINE__, \
180                                 #sname1, #field1, (int)s->sname1.field1, \
181                                 #sname2, #field2, (int)v.sname2.out.field2); \
182                         ret = False; \
183                 } \
184         }} while (0)
185
186 #define CHECK_TIME(name, sname1, field1, v, sname2, field2) do { \
187         s = find(name); \
188         if (s) { \
189                 if (s->sname1.field1 != (~1 & nt_time_to_unix(&v.sname2.out.field2))) { \
190                         printf("(%d) %s/%s [%s] != %s/%s [%s]\n", \
191                                __LINE__, \
192                                 #sname1, #field1, time_string(mem_ctx, s->sname1.field1), \
193                                 #sname2, #field2, nt_time_string(mem_ctx, &v.sname2.out.field2)); \
194                         ret = False; \
195                 } \
196         }} while (0)
197
198 #define CHECK_NTTIME(name, sname1, field1, v, sname2, field2) do { \
199         s = find(name); \
200         if (s) { \
201                 if (memcmp(&s->sname1.field1, &v.sname2.out.field2, sizeof(NTTIME))) { \
202                         printf("(%d) %s/%s [%s] != %s/%s [%s]\n", \
203                                __LINE__, \
204                                 #sname1, #field1, nt_time_string(mem_ctx, &s->sname1.field1), \
205                                 #sname2, #field2, nt_time_string(mem_ctx, &v.sname2.out.field2)); \
206                         ret = False; \
207                 } \
208         }} while (0)
209
210 #define CHECK_STR(name, sname1, field1, v, sname2, field2) do { \
211         s = find(name); \
212         if (s) { \
213                 if (!s->sname1.field1 || strcmp(s->sname1.field1, v.sname2.out.field2.s)) { \
214                         printf("(%d) %s/%s [%s] != %s/%s [%s]\n", \
215                                __LINE__, \
216                                 #sname1, #field1, s->sname1.field1, \
217                                 #sname2, #field2, v.sname2.out.field2.s); \
218                         ret = False; \
219                 } \
220         }} while (0)
221
222 #define CHECK_WSTR(name, sname1, field1, v, sname2, field2, flags) do { \
223         s = find(name); \
224         if (s) { \
225                 if (!s->sname1.field1.s || \
226                     strcmp(s->sname1.field1.s, v.sname2.out.field2.s) || \
227                     wire_bad_flags(&s->sname1.field1, flags)) { \
228                         printf("(%d) %s/%s [%s] != %s/%s [%s]\n", \
229                                __LINE__, \
230                                 #sname1, #field1, s->sname1.field1.s, \
231                                 #sname2, #field2, v.sname2.out.field2.s); \
232                         ret = False; \
233                 } \
234         }} while (0)
235
236 #define CHECK_NAME(name, sname1, field1, fname, flags) do { \
237         s = find(name); \
238         if (s) { \
239                 if (!s->sname1.field1.s || \
240                     strcmp(s->sname1.field1.s, fname) || \
241                     wire_bad_flags(&s->sname1.field1, flags)) { \
242                         printf("(%d) %s/%s [%s] != %s\n", \
243                                __LINE__, \
244                                 #sname1, #field1, s->sname1.field1.s, \
245                                 fname); \
246                         ret = False; \
247                 } \
248         }} while (0)
249         
250         /* check that all the results are as expected */
251         CHECK_VAL("SEARCH",              search,              attrib, all_info, all_info, attrib);
252         CHECK_VAL("STANDARD",            standard,            attrib, all_info, all_info, attrib);
253         CHECK_VAL("EA_SIZE",             ea_size,             attrib, all_info, all_info, attrib);
254         CHECK_VAL("DIRECTORY_INFO",      directory_info,      attrib, all_info, all_info, attrib);
255         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, attrib, all_info, all_info, attrib);
256         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, attrib, all_info, all_info, attrib);
257         CHECK_VAL("LEVEL_261",           level_261,           attrib, all_info, all_info, attrib);
258         CHECK_VAL("LEVEL_262",           level_262,           attrib, all_info, all_info, attrib);
259
260         CHECK_TIME("SEARCH",             search,              write_time, all_info, all_info, write_time);
261         CHECK_TIME("STANDARD",           standard,            write_time, all_info, all_info, write_time);
262         CHECK_TIME("EA_SIZE",            ea_size,             write_time, all_info, all_info, write_time);
263         CHECK_TIME("STANDARD",           standard,            create_time, all_info, all_info, create_time);
264         CHECK_TIME("EA_SIZE",            ea_size,             create_time, all_info, all_info, create_time);
265         CHECK_TIME("STANDARD",           standard,            access_time, all_info, all_info, access_time);
266         CHECK_TIME("EA_SIZE",            ea_size,             access_time, all_info, all_info, access_time);
267
268         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      write_time, all_info, all_info, write_time);
269         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, write_time, all_info, all_info, write_time);
270         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, write_time, all_info, all_info, write_time);
271         CHECK_NTTIME("LEVEL_261",           level_261,           write_time, all_info, all_info, write_time);
272         CHECK_NTTIME("LEVEL_262",           level_262,           write_time, all_info, all_info, write_time);
273
274         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      create_time, all_info, all_info, create_time);
275         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
276         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
277         CHECK_NTTIME("LEVEL_261",           level_261,           create_time, all_info, all_info, create_time);
278         CHECK_NTTIME("LEVEL_262",           level_262,           create_time, all_info, all_info, create_time);
279
280         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      access_time, all_info, all_info, access_time);
281         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, access_time, all_info, all_info, access_time);
282         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, access_time, all_info, all_info, access_time);
283         CHECK_NTTIME("LEVEL_261",           level_261,           access_time, all_info, all_info, access_time);
284         CHECK_NTTIME("LEVEL_262",           level_262,           access_time, all_info, all_info, access_time);
285
286         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      create_time, all_info, all_info, create_time);
287         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
288         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
289         CHECK_NTTIME("LEVEL_261",           level_261,           create_time, all_info, all_info, create_time);
290         CHECK_NTTIME("LEVEL_262",           level_262,           create_time, all_info, all_info, create_time);
291
292         CHECK_VAL("SEARCH",              search,              size, all_info, all_info, size);
293         CHECK_VAL("STANDARD",            standard,            size, all_info, all_info, size);
294         CHECK_VAL("EA_SIZE",             ea_size,             size, all_info, all_info, size);
295         CHECK_VAL("DIRECTORY_INFO",      directory_info,      size, all_info, all_info, size);
296         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, size, all_info, all_info, size);
297         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, size, all_info, all_info, size);
298         CHECK_VAL("LEVEL_261",           level_261,           size, all_info, all_info, size);
299         CHECK_VAL("LEVEL_262",           level_262,           size, all_info, all_info, size);
300
301         CHECK_VAL("STANDARD",            standard,            alloc_size, all_info, all_info, alloc_size);
302         CHECK_VAL("EA_SIZE",             ea_size,             alloc_size, all_info, all_info, alloc_size);
303         CHECK_VAL("DIRECTORY_INFO",      directory_info,      alloc_size, all_info, all_info, alloc_size);
304         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, alloc_size, all_info, all_info, alloc_size);
305         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, alloc_size, all_info, all_info, alloc_size);
306         CHECK_VAL("LEVEL_261",           level_261,           alloc_size, all_info, all_info, alloc_size);
307         CHECK_VAL("LEVEL_262",           level_262,           alloc_size, all_info, all_info, alloc_size);
308
309         CHECK_VAL("EA_SIZE",             ea_size,             ea_size, all_info, all_info, ea_size);
310         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, ea_size, all_info, all_info, ea_size);
311         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, ea_size, all_info, all_info, ea_size);
312         CHECK_VAL("LEVEL_261",           level_261,           ea_size, all_info, all_info, ea_size);
313         CHECK_VAL("LEVEL_262",           level_262,           ea_size, all_info, all_info, ea_size);
314
315         CHECK_STR("SEARCH", search, name, alt_info, alt_name_info, fname);
316         CHECK_WSTR("BOTH_DIRECTORY_INFO", both_directory_info, short_name, alt_info, alt_name_info, fname, STR_UNICODE);
317
318         CHECK_NAME("STANDARD",            standard,            name, fname+1, 0);
319         CHECK_NAME("EA_SIZE",             ea_size,             name, fname+1, 0);
320         CHECK_NAME("DIRECTORY_INFO",      directory_info,      name, fname+1, STR_TERMINATE_ASCII);
321         CHECK_NAME("FULL_DIRECTORY_INFO", full_directory_info, name, fname+1, STR_TERMINATE_ASCII);
322         CHECK_NAME("NAME_INFO",           name_info,           name, fname+1, STR_TERMINATE_ASCII);
323         CHECK_NAME("BOTH_DIRECTORY_INFO", both_directory_info, name, fname+1, STR_TERMINATE_ASCII);
324         CHECK_NAME("LEVEL_261",           level_261,           name, fname+1, STR_TERMINATE_ASCII);
325         CHECK_NAME("LEVEL_262",           level_262,           name, fname+1, STR_TERMINATE_ASCII);
326
327 done:
328         smb_raw_exit(cli->session);
329         cli_unlink(cli, fname);
330
331         return ret;
332 }
333
334
335 struct multiple_result {
336         TALLOC_CTX *mem_ctx;
337         int count;
338         union smb_search_data *list;
339 };
340
341 /*
342   callback function for multiple_search
343 */
344 static BOOL multiple_search_callback(void *private, union smb_search_data *file)
345 {
346         struct multiple_result *data = private;
347
348
349         data->count++;
350         data->list = talloc_realloc(data->mem_ctx, 
351                                     data->list, 
352                                     data->count * (sizeof(data->list[0])));
353
354         data->list[data->count-1] = *file;
355
356         return True;
357 }
358
359 enum continue_type {CONT_FLAGS, CONT_NAME, CONT_RESUME_KEY};
360
361 /*
362   do a single file (non-wildcard) search 
363 */
364 static NTSTATUS multiple_search(struct cli_state *cli, 
365                                 TALLOC_CTX *mem_ctx,
366                                 const char *pattern,
367                                 enum search_level level,
368                                 enum continue_type cont_type,
369                                 void *data)
370 {
371         union smb_search_first io;
372         union smb_search_next io2;
373         NTSTATUS status;
374         const int per_search = 300;
375         struct multiple_result *result = data;
376
377         io.generic.level = level;
378         if (level == RAW_SEARCH_SEARCH) {
379                 io.search_first.in.max_count = per_search;
380                 io.search_first.in.search_attrib = 0;
381                 io.search_first.in.pattern = pattern;
382         } else {
383                 io.t2ffirst.in.search_attrib = 0;
384                 io.t2ffirst.in.max_count = per_search;
385                 io.t2ffirst.in.flags = 0;
386                 io.t2ffirst.in.storage_type = 0;
387                 io.t2ffirst.in.pattern = pattern;
388                 if (cont_type == CONT_RESUME_KEY) {
389                         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME | 
390                                 FLAG_TRANS2_FIND_BACKUP_INTENT;
391                 }
392         }
393
394         status = smb_raw_search_first(cli->tree, mem_ctx,
395                                       &io, data, multiple_search_callback);
396         
397
398         while (NT_STATUS_IS_OK(status)) {
399                 io2.generic.level = level;
400                 if (level == RAW_SEARCH_SEARCH) {
401                         io2.search_next.in.max_count = per_search;
402                         io2.search_next.in.search_attrib = 0;
403                         io2.search_next.in.search_id = result->list[result->count-1].search.search_id;
404                 } else {
405                         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
406                         io2.t2fnext.in.max_count = per_search;
407                         io2.t2fnext.in.resume_key = 0;
408                         io2.t2fnext.in.flags = 0;
409                         io2.t2fnext.in.last_name = "";
410                         switch (cont_type) {
411                         case CONT_RESUME_KEY:
412                                 if (level == RAW_SEARCH_STANDARD) {
413                                         io2.t2fnext.in.resume_key = 
414                                                 result->list[result->count-1].standard.resume_key;
415                                 } else {
416                                         io2.t2fnext.in.resume_key = 
417                                                 result->list[result->count-1].both_directory_info.file_index;
418                                 }
419                                 io2.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME |
420                                         FLAG_TRANS2_FIND_BACKUP_INTENT;
421                                 break;
422                         case CONT_NAME:
423                                 if (level == RAW_SEARCH_STANDARD) {
424                                         io2.t2fnext.in.last_name = 
425                                                 result->list[result->count-1].standard.name.s;
426                                 } else {
427                                         io2.t2fnext.in.last_name = 
428                                                 result->list[result->count-1].both_directory_info.name.s;
429                                 }
430                                 break;
431                         case CONT_FLAGS:
432                                 io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CONTINUE;
433                                 break;
434                         }
435                 }
436
437                 status = smb_raw_search_next(cli->tree, mem_ctx,
438                                              &io2, data, multiple_search_callback);
439                 if (!NT_STATUS_IS_OK(status)) {
440                         break;
441                 }
442                 if (level == RAW_SEARCH_SEARCH) {
443                         if (io2.search_next.out.count == 0) {
444                                 break;
445                         }
446                 } else if (io2.t2fnext.out.count == 0 ||
447                            io2.t2fnext.out.end_of_search) {
448                         break;
449                 }
450         }
451
452         return status;
453 }
454
455 #define CHECK_STATUS(status, correct) do { \
456         if (!NT_STATUS_EQUAL(status, correct)) { \
457                 printf("(%d) Incorrect status %s - should be %s\n", \
458                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
459                 ret = False; \
460                 goto done; \
461         }} while (0)
462
463 #define CHECK_VALUE(v, correct) do { \
464         if ((v) != (correct)) { \
465                 printf("(%d) Incorrect value %s=%d - should be %d\n", \
466                        __LINE__, #v, v, correct); \
467                 ret = False; \
468         }} while (0)
469
470
471 static int search_both_compare(union smb_search_data *d1, union smb_search_data *d2)
472 {
473         return strcmp(d1->both_directory_info.name.s, d2->both_directory_info.name.s);
474 }
475
476 static int search_standard_compare(union smb_search_data *d1, union smb_search_data *d2)
477 {
478         return strcmp(d1->standard.name.s, d2->standard.name.s);
479 }
480
481 static int search_old_compare(union smb_search_data *d1, union smb_search_data *d2)
482 {
483         return strcmp(d1->search.name, d2->search.name);
484 }
485
486
487 /* 
488    basic testing of search calls using many files
489 */
490 static BOOL test_many_files(struct cli_state *cli, TALLOC_CTX *mem_ctx)
491 {
492         const int num_files = 700;
493         int i, fnum, t;
494         char *fname;
495         BOOL ret = True;
496         NTSTATUS status;
497         struct multiple_result result;
498         struct {
499                 const char *name;
500                 const char *cont_name;
501                 enum search_level level;
502                 enum continue_type cont_type;
503         } search_types[] = {
504                 {"BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_FLAGS},
505                 {"BOTH_DIRECTORY_INFO", "KEY",   RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY},
506                 {"BOTH_DIRECTORY_INFO", "NAME",  RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_NAME},
507                 {"STANDARD",            "FLAGS", RAW_SEARCH_STANDARD,            CONT_FLAGS},
508                 {"STANDARD",            "KEY",   RAW_SEARCH_STANDARD,            CONT_RESUME_KEY},
509                 {"STANDARD",            "NAME",  RAW_SEARCH_STANDARD,            CONT_NAME},
510                 {"SEARCH",              "ID",    RAW_SEARCH_SEARCH,              CONT_RESUME_KEY}
511         };
512
513         if (cli_deltree(cli, BASEDIR) == -1 || 
514             !cli_mkdir(cli, BASEDIR)) {
515                 printf("Failed to create " BASEDIR " - %s\n", cli_errstr(cli));
516                 return False;
517         }
518
519         printf("Creating %d files\n", num_files);
520
521         for (i=0;i<num_files;i++) {
522                 asprintf(&fname, BASEDIR "\\test%03d.txt", i);
523                 fnum = cli_open(cli, fname, O_CREAT|O_RDWR, DENY_NONE);
524                 if (fnum == -1) {
525                         printf("Failed to create %s - %s\n", fname, cli_errstr(cli));
526                         ret = False;
527                         goto done;
528                 }
529                 free(fname);
530                 cli_close(cli, fnum);
531         }
532
533
534         for (t=0;t<ARRAY_SIZE(search_types);t++) {
535                 ZERO_STRUCT(result);
536                 result.mem_ctx = mem_ctx;
537         
538                 printf("Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
539
540                 status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*", 
541                                          search_types[t].level,
542                                          search_types[t].cont_type,
543                                          &result);
544         
545                 CHECK_STATUS(status, NT_STATUS_OK);
546                 CHECK_VALUE(result.count, num_files);
547
548                 if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
549                         qsort(result.list, result.count, sizeof(result.list[0]), search_both_compare);
550                 } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
551                         qsort(result.list, result.count, sizeof(result.list[0]), search_standard_compare);
552                 } else {
553                         qsort(result.list, result.count, sizeof(result.list[0]), search_old_compare);
554                 }
555
556                 for (i=0;i<num_files;i++) {
557                         const char *s;
558                         if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
559                                 s = result.list[i].both_directory_info.name.s;
560                         } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
561                                 s = result.list[i].standard.name.s;
562                         } else {
563                                 s = result.list[i].search.name;
564                         }
565                         asprintf(&fname, "test%03d.txt", i);
566                         if (strcmp(fname, s)) {
567                                 printf("Incorrect name %s at entry %d\n", s, i);
568                                 ret = False;
569                                 break;
570                         }
571                         free(fname);
572                 }
573         }
574
575 done:
576         smb_raw_exit(cli->session);
577         cli_deltree(cli, BASEDIR);
578
579         return ret;
580 }
581
582
583 /* 
584    basic testing of all RAW_SEARCH_* calls using a single file
585 */
586 BOOL torture_raw_search(int dummy)
587 {
588         struct cli_state *cli;
589         BOOL ret = True;
590         TALLOC_CTX *mem_ctx;
591
592         if (!torture_open_connection(&cli)) {
593                 return False;
594         }
595
596         mem_ctx = talloc_init("torture_search");
597
598         if (!test_one_file(cli, mem_ctx)) {
599                 ret = False;
600         }
601
602         if (!test_many_files(cli, mem_ctx)) {
603                 ret = False;
604         }
605
606         torture_close_connection(cli);
607         talloc_destroy(mem_ctx);
608         
609         return ret;
610 }