2 Unix SMB/CIFS implementation.
3 RAW_SEARCH_* individual test suite
4 Copyright (C) Andrew Tridgell 2003
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.
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.
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.
24 #define BASEDIR "\\testsearch"
27 callback function for single_search
29 static BOOL single_search_callback(void *private, union smb_search_data *file)
31 union smb_search_data *data = private;
39 do a single file (non-wildcard) search
41 static NTSTATUS single_search(struct smbcli_state *cli,
44 enum smb_search_level level,
45 union smb_search_data *data)
47 union smb_search_first io;
48 union smb_search_close c;
51 io.generic.level = level;
52 if (level == RAW_SEARCH_SEARCH ||
53 level == RAW_SEARCH_FFIRST ||
54 level == RAW_SEARCH_FUNIQUE) {
55 io.search_first.in.max_count = 1;
56 io.search_first.in.search_attrib = 0;
57 io.search_first.in.pattern = pattern;
59 io.t2ffirst.in.search_attrib = 0;
60 io.t2ffirst.in.max_count = 1;
61 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
62 io.t2ffirst.in.storage_type = 0;
63 io.t2ffirst.in.pattern = pattern;
66 status = smb_raw_search_first(cli->tree, mem_ctx,
67 &io, (void *)data, single_search_callback);
69 if (NT_STATUS_IS_OK(status) && level == RAW_SEARCH_FFIRST) {
70 c.fclose.level = RAW_FINDCLOSE_FCLOSE;
71 c.fclose.in.max_count = 1;
72 c.fclose.in.search_attrib = 0;
73 c.fclose.in.id = data->search.id;
74 status = smb_raw_search_close(cli->tree, &c);
83 enum smb_search_level level;
84 uint32_t capability_mask;
86 union smb_search_data data;
88 {"FFIRST", RAW_SEARCH_FFIRST, },
89 {"FUNIQUE", RAW_SEARCH_FUNIQUE, },
90 {"SEARCH", RAW_SEARCH_SEARCH, },
91 {"STANDARD", RAW_SEARCH_STANDARD, },
92 {"EA_SIZE", RAW_SEARCH_EA_SIZE, },
93 {"DIRECTORY_INFO", RAW_SEARCH_DIRECTORY_INFO, },
94 {"FULL_DIRECTORY_INFO", RAW_SEARCH_FULL_DIRECTORY_INFO, },
95 {"NAME_INFO", RAW_SEARCH_NAME_INFO, },
96 {"BOTH_DIRECTORY_INFO", RAW_SEARCH_BOTH_DIRECTORY_INFO, },
97 {"ID_FULL_DIRECTORY_INFO", RAW_SEARCH_ID_FULL_DIRECTORY_INFO, },
98 {"ID_BOTH_DIRECTORY_INFO", RAW_SEARCH_ID_BOTH_DIRECTORY_INFO, },
99 {"UNIX_INFO", RAW_SEARCH_UNIX_INFO, CAP_UNIX}
102 /* find a level in the table by name */
103 static union smb_search_data *find(const char *name)
106 for (i=0;i<ARRAY_SIZE(levels);i++) {
107 if (NT_STATUS_IS_OK(levels[i].status) &&
108 strcmp(levels[i].name, name) == 0) {
109 return &levels[i].data;
116 basic testing of all RAW_SEARCH_* calls using a single file
118 static BOOL test_one_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
122 const char *fname = "\\torture_search.txt";
123 const char *fname2 = "\\torture_search-NOTEXIST.txt";
126 union smb_fileinfo all_info, alt_info, name_info, internal_info;
127 union smb_search_data *s;
129 printf("Testing one file searches\n");
131 fnum = create_complex_file(cli, mem_ctx, fname);
133 printf("ERROR: open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
138 /* call all the levels */
139 for (i=0;i<ARRAY_SIZE(levels);i++) {
140 NTSTATUS expected_status;
141 uint32_t cap = cli->transport->negotiate.capabilities;
143 printf("testing %s\n", levels[i].name);
145 levels[i].status = single_search(cli, mem_ctx, fname,
146 levels[i].level, &levels[i].data);
148 /* see if this server claims to support this level */
149 if ((cap & levels[i].capability_mask) != levels[i].capability_mask) {
150 printf("search level %s(%d) not supported by server\n",
151 levels[i].name, (int)levels[i].level);
155 if (!NT_STATUS_IS_OK(levels[i].status)) {
156 printf("search level %s(%d) failed - %s\n",
157 levels[i].name, (int)levels[i].level,
158 nt_errstr(levels[i].status));
163 status = single_search(cli, mem_ctx, fname2,
164 levels[i].level, &levels[i].data);
166 expected_status = NT_STATUS_NO_SUCH_FILE;
167 if (levels[i].level == RAW_SEARCH_SEARCH ||
168 levels[i].level == RAW_SEARCH_FFIRST ||
169 levels[i].level == RAW_SEARCH_FUNIQUE) {
170 expected_status = STATUS_NO_MORE_FILES;
172 if (!NT_STATUS_EQUAL(status, expected_status)) {
173 printf("search level %s(%d) should fail with %s - %s\n",
174 levels[i].name, (int)levels[i].level,
175 nt_errstr(expected_status),
181 /* get the all_info file into to check against */
182 all_info.generic.level = RAW_FILEINFO_ALL_INFO;
183 all_info.generic.in.fname = fname;
184 status = smb_raw_pathinfo(cli->tree, mem_ctx, &all_info);
185 if (!NT_STATUS_IS_OK(status)) {
186 printf("RAW_FILEINFO_ALL_INFO failed - %s\n", nt_errstr(status));
191 alt_info.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
192 alt_info.generic.in.fname = fname;
193 status = smb_raw_pathinfo(cli->tree, mem_ctx, &alt_info);
194 if (!NT_STATUS_IS_OK(status)) {
195 printf("RAW_FILEINFO_ALT_NAME_INFO failed - %s\n", nt_errstr(status));
200 internal_info.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
201 internal_info.generic.in.fname = fname;
202 status = smb_raw_pathinfo(cli->tree, mem_ctx, &internal_info);
203 if (!NT_STATUS_IS_OK(status)) {
204 printf("RAW_FILEINFO_INTERNAL_INFORMATION failed - %s\n", nt_errstr(status));
209 name_info.generic.level = RAW_FILEINFO_NAME_INFO;
210 name_info.generic.in.fname = fname;
211 status = smb_raw_pathinfo(cli->tree, mem_ctx, &name_info);
212 if (!NT_STATUS_IS_OK(status)) {
213 printf("RAW_FILEINFO_NAME_INFO failed - %s\n", nt_errstr(status));
218 #define CHECK_VAL(name, sname1, field1, v, sname2, field2) do { \
221 if ((s->sname1.field1) != (v.sname2.out.field2)) { \
222 printf("(%s) %s/%s [0x%x] != %s/%s [0x%x]\n", \
224 #sname1, #field1, (int)s->sname1.field1, \
225 #sname2, #field2, (int)v.sname2.out.field2); \
230 #define CHECK_TIME(name, sname1, field1, v, sname2, field2) do { \
233 if (s->sname1.field1 != (~1 & nt_time_to_unix(v.sname2.out.field2))) { \
234 printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
236 #sname1, #field1, timestring(mem_ctx, s->sname1.field1), \
237 #sname2, #field2, nt_time_string(mem_ctx, v.sname2.out.field2)); \
242 #define CHECK_NTTIME(name, sname1, field1, v, sname2, field2) do { \
245 if (s->sname1.field1 != v.sname2.out.field2) { \
246 printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
248 #sname1, #field1, nt_time_string(mem_ctx, s->sname1.field1), \
249 #sname2, #field2, nt_time_string(mem_ctx, v.sname2.out.field2)); \
254 #define CHECK_STR(name, sname1, field1, v, sname2, field2) do { \
257 if (!s->sname1.field1 || strcmp(s->sname1.field1, v.sname2.out.field2.s)) { \
258 printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
260 #sname1, #field1, s->sname1.field1, \
261 #sname2, #field2, v.sname2.out.field2.s); \
266 #define CHECK_WSTR(name, sname1, field1, v, sname2, field2, flags) do { \
269 if (!s->sname1.field1.s || \
270 strcmp(s->sname1.field1.s, v.sname2.out.field2.s) || \
271 wire_bad_flags(&s->sname1.field1, flags, cli)) { \
272 printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
274 #sname1, #field1, s->sname1.field1.s, \
275 #sname2, #field2, v.sname2.out.field2.s); \
280 #define CHECK_NAME(name, sname1, field1, fname, flags) do { \
283 if (!s->sname1.field1.s || \
284 strcmp(s->sname1.field1.s, fname) || \
285 wire_bad_flags(&s->sname1.field1, flags, cli)) { \
286 printf("(%s) %s/%s [%s] != %s\n", \
288 #sname1, #field1, s->sname1.field1.s, \
294 #define CHECK_UNIX_NAME(name, sname1, field1, fname, flags) do { \
297 if (!s->sname1.field1 || \
298 strcmp(s->sname1.field1, fname)) { \
299 printf("(%s) %s/%s [%s] != %s\n", \
301 #sname1, #field1, s->sname1.field1, \
307 /* check that all the results are as expected */
308 CHECK_VAL("SEARCH", search, attrib, all_info, all_info, attrib&0xFFF);
309 CHECK_VAL("STANDARD", standard, attrib, all_info, all_info, attrib&0xFFF);
310 CHECK_VAL("EA_SIZE", ea_size, attrib, all_info, all_info, attrib&0xFFF);
311 CHECK_VAL("DIRECTORY_INFO", directory_info, attrib, all_info, all_info, attrib);
312 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, attrib, all_info, all_info, attrib);
313 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, attrib, all_info, all_info, attrib);
314 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, attrib, all_info, all_info, attrib);
315 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, attrib, all_info, all_info, attrib);
317 CHECK_TIME("SEARCH", search, write_time, all_info, all_info, write_time);
318 CHECK_TIME("STANDARD", standard, write_time, all_info, all_info, write_time);
319 CHECK_TIME("EA_SIZE", ea_size, write_time, all_info, all_info, write_time);
320 CHECK_TIME("STANDARD", standard, create_time, all_info, all_info, create_time);
321 CHECK_TIME("EA_SIZE", ea_size, create_time, all_info, all_info, create_time);
322 CHECK_TIME("STANDARD", standard, access_time, all_info, all_info, access_time);
323 CHECK_TIME("EA_SIZE", ea_size, access_time, all_info, all_info, access_time);
325 CHECK_NTTIME("DIRECTORY_INFO", directory_info, write_time, all_info, all_info, write_time);
326 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, write_time, all_info, all_info, write_time);
327 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, write_time, all_info, all_info, write_time);
328 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, write_time, all_info, all_info, write_time);
329 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, write_time, all_info, all_info, write_time);
331 CHECK_NTTIME("DIRECTORY_INFO", directory_info, create_time, all_info, all_info, create_time);
332 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
333 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
334 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, create_time, all_info, all_info, create_time);
335 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, create_time, all_info, all_info, create_time);
337 CHECK_NTTIME("DIRECTORY_INFO", directory_info, access_time, all_info, all_info, access_time);
338 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, access_time, all_info, all_info, access_time);
339 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, access_time, all_info, all_info, access_time);
340 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, access_time, all_info, all_info, access_time);
341 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, access_time, all_info, all_info, access_time);
343 CHECK_NTTIME("DIRECTORY_INFO", directory_info, create_time, all_info, all_info, create_time);
344 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
345 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
346 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, create_time, all_info, all_info, create_time);
347 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, create_time, all_info, all_info, create_time);
349 CHECK_VAL("SEARCH", search, size, all_info, all_info, size);
350 CHECK_VAL("STANDARD", standard, size, all_info, all_info, size);
351 CHECK_VAL("EA_SIZE", ea_size, size, all_info, all_info, size);
352 CHECK_VAL("DIRECTORY_INFO", directory_info, size, all_info, all_info, size);
353 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, size, all_info, all_info, size);
354 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, size, all_info, all_info, size);
355 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, size, all_info, all_info, size);
356 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, size, all_info, all_info, size);
357 CHECK_VAL("UNIX_INFO", unix_info, size, all_info, all_info, size);
359 CHECK_VAL("STANDARD", standard, alloc_size, all_info, all_info, alloc_size);
360 CHECK_VAL("EA_SIZE", ea_size, alloc_size, all_info, all_info, alloc_size);
361 CHECK_VAL("DIRECTORY_INFO", directory_info, alloc_size, all_info, all_info, alloc_size);
362 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, alloc_size, all_info, all_info, alloc_size);
363 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, alloc_size, all_info, all_info, alloc_size);
364 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, alloc_size, all_info, all_info, alloc_size);
365 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, alloc_size, all_info, all_info, alloc_size);
366 CHECK_VAL("UNIX_INFO", unix_info, alloc_size, all_info, all_info, alloc_size);
368 CHECK_VAL("EA_SIZE", ea_size, ea_size, all_info, all_info, ea_size);
369 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, ea_size, all_info, all_info, ea_size);
370 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, ea_size, all_info, all_info, ea_size);
371 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, ea_size, all_info, all_info, ea_size);
372 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, ea_size, all_info, all_info, ea_size);
374 CHECK_STR("SEARCH", search, name, alt_info, alt_name_info, fname);
375 CHECK_WSTR("BOTH_DIRECTORY_INFO", both_directory_info, short_name, alt_info, alt_name_info, fname, STR_UNICODE);
377 CHECK_NAME("STANDARD", standard, name, fname+1, 0);
378 CHECK_NAME("EA_SIZE", ea_size, name, fname+1, 0);
379 CHECK_NAME("DIRECTORY_INFO", directory_info, name, fname+1, STR_TERMINATE_ASCII);
380 CHECK_NAME("FULL_DIRECTORY_INFO", full_directory_info, name, fname+1, STR_TERMINATE_ASCII);
381 CHECK_NAME("NAME_INFO", name_info, name, fname+1, STR_TERMINATE_ASCII);
382 CHECK_NAME("BOTH_DIRECTORY_INFO", both_directory_info, name, fname+1, STR_TERMINATE_ASCII);
383 CHECK_NAME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, name, fname+1, STR_TERMINATE_ASCII);
384 CHECK_NAME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, name, fname+1, STR_TERMINATE_ASCII);
385 CHECK_UNIX_NAME("UNIX_INFO", unix_info, name, fname+1, STR_TERMINATE_ASCII);
387 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, file_id, internal_info, internal_information, file_id);
388 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, file_id, internal_info, internal_information, file_id);
391 smb_raw_exit(cli->session);
392 smbcli_unlink(cli->tree, fname);
398 struct multiple_result {
401 union smb_search_data *list;
405 callback function for multiple_search
407 static BOOL multiple_search_callback(void *private, union smb_search_data *file)
409 struct multiple_result *data = private;
413 data->list = talloc_realloc_p(data->mem_ctx,
415 union smb_search_data,
418 data->list[data->count-1] = *file;
423 enum continue_type {CONT_FLAGS, CONT_NAME, CONT_RESUME_KEY};
426 do a single file (non-wildcard) search
428 static NTSTATUS multiple_search(struct smbcli_state *cli,
431 enum smb_search_level level,
432 enum continue_type cont_type,
435 union smb_search_first io;
436 union smb_search_next io2;
438 const int per_search = 300;
439 struct multiple_result *result = data;
441 io.generic.level = level;
442 if (level == RAW_SEARCH_SEARCH) {
443 io.search_first.in.max_count = per_search;
444 io.search_first.in.search_attrib = 0;
445 io.search_first.in.pattern = pattern;
447 io.t2ffirst.in.search_attrib = 0;
448 io.t2ffirst.in.max_count = per_search;
449 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
450 io.t2ffirst.in.storage_type = 0;
451 io.t2ffirst.in.pattern = pattern;
452 if (cont_type == CONT_RESUME_KEY) {
453 io.t2ffirst.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
454 FLAG_TRANS2_FIND_BACKUP_INTENT;
458 status = smb_raw_search_first(cli->tree, mem_ctx,
459 &io, data, multiple_search_callback);
462 while (NT_STATUS_IS_OK(status)) {
463 io2.generic.level = level;
464 if (level == RAW_SEARCH_SEARCH) {
465 io2.search_next.in.max_count = per_search;
466 io2.search_next.in.search_attrib = 0;
467 io2.search_next.in.id = result->list[result->count-1].search.id;
469 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
470 io2.t2fnext.in.max_count = per_search;
471 io2.t2fnext.in.resume_key = 0;
472 io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
473 io2.t2fnext.in.last_name = "";
475 case CONT_RESUME_KEY:
476 if (level == RAW_SEARCH_STANDARD) {
477 io2.t2fnext.in.resume_key =
478 result->list[result->count-1].standard.resume_key;
479 } else if (level == RAW_SEARCH_EA_SIZE) {
480 io2.t2fnext.in.resume_key =
481 result->list[result->count-1].ea_size.resume_key;
482 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
483 io2.t2fnext.in.resume_key =
484 result->list[result->count-1].directory_info.file_index;
486 io2.t2fnext.in.resume_key =
487 result->list[result->count-1].both_directory_info.file_index;
489 if (io2.t2fnext.in.resume_key == 0) {
490 printf("Server does not support resume by key\n");
491 return NT_STATUS_NOT_SUPPORTED;
493 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
494 FLAG_TRANS2_FIND_BACKUP_INTENT;
497 if (level == RAW_SEARCH_STANDARD) {
498 io2.t2fnext.in.last_name =
499 result->list[result->count-1].standard.name.s;
500 } else if (level == RAW_SEARCH_EA_SIZE) {
501 io2.t2fnext.in.last_name =
502 result->list[result->count-1].ea_size.name.s;
503 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
504 io2.t2fnext.in.last_name =
505 result->list[result->count-1].directory_info.name.s;
507 io2.t2fnext.in.last_name =
508 result->list[result->count-1].both_directory_info.name.s;
512 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_CONTINUE;
517 status = smb_raw_search_next(cli->tree, mem_ctx,
518 &io2, data, multiple_search_callback);
519 if (!NT_STATUS_IS_OK(status)) {
522 if (level == RAW_SEARCH_SEARCH) {
523 if (io2.search_next.out.count == 0) {
526 } else if (io2.t2fnext.out.count == 0 ||
527 io2.t2fnext.out.end_of_search) {
535 #define CHECK_STATUS(status, correct) do { \
536 if (!NT_STATUS_EQUAL(status, correct)) { \
537 printf("(%s) Incorrect status %s - should be %s\n", \
538 __location__, nt_errstr(status), nt_errstr(correct)); \
543 #define CHECK_VALUE(v, correct) do { \
544 if ((v) != (correct)) { \
545 printf("(%s) Incorrect value %s=%d - should be %d\n", \
546 __location__, #v, v, correct); \
551 static int search_both_compare(union smb_search_data *d1, union smb_search_data *d2)
553 return strcmp_safe(d1->both_directory_info.name.s, d2->both_directory_info.name.s);
556 static int search_standard_compare(union smb_search_data *d1, union smb_search_data *d2)
558 return strcmp_safe(d1->standard.name.s, d2->standard.name.s);
561 static int search_ea_size_compare(union smb_search_data *d1, union smb_search_data *d2)
563 return strcmp_safe(d1->ea_size.name.s, d2->ea_size.name.s);
566 static int search_directory_info_compare(union smb_search_data *d1, union smb_search_data *d2)
568 return strcmp_safe(d1->directory_info.name.s, d2->directory_info.name.s);
571 static int search_old_compare(union smb_search_data *d1, union smb_search_data *d2)
573 return strcmp_safe(d1->search.name, d2->search.name);
578 basic testing of search calls using many files
580 static BOOL test_many_files(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
582 const int num_files = 700;
587 struct multiple_result result;
590 const char *cont_name;
591 enum smb_search_level level;
592 enum continue_type cont_type;
594 {"SEARCH", "ID", RAW_SEARCH_SEARCH, CONT_RESUME_KEY},
595 {"BOTH_DIRECTORY_INFO", "NAME", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_NAME},
596 {"BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_FLAGS},
597 {"BOTH_DIRECTORY_INFO", "KEY", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY},
598 {"STANDARD", "FLAGS", RAW_SEARCH_STANDARD, CONT_FLAGS},
599 {"STANDARD", "KEY", RAW_SEARCH_STANDARD, CONT_RESUME_KEY},
600 {"STANDARD", "NAME", RAW_SEARCH_STANDARD, CONT_NAME},
601 {"EA_SIZE", "FLAGS", RAW_SEARCH_EA_SIZE, CONT_FLAGS},
602 {"EA_SIZE", "KEY", RAW_SEARCH_EA_SIZE, CONT_RESUME_KEY},
603 {"EA_SIZE", "NAME", RAW_SEARCH_EA_SIZE, CONT_NAME},
604 {"DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DIRECTORY_INFO, CONT_FLAGS},
605 {"DIRECTORY_INFO", "KEY", RAW_SEARCH_DIRECTORY_INFO, CONT_RESUME_KEY},
606 {"DIRECTORY_INFO", "NAME", RAW_SEARCH_DIRECTORY_INFO, CONT_NAME}
609 if (smbcli_deltree(cli->tree, BASEDIR) == -1 ||
610 NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
611 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
615 printf("Creating %d files\n", num_files);
617 for (i=0;i<num_files;i++) {
618 asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
619 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
621 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
626 smbcli_close(cli->tree, fnum);
630 for (t=0;t<ARRAY_SIZE(search_types);t++) {
632 result.mem_ctx = talloc(mem_ctx, 0);
634 printf("Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
636 status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*",
637 search_types[t].level,
638 search_types[t].cont_type,
641 if (!NT_STATUS_IS_OK(status)) {
642 printf("search failed - %s\n", nt_errstr(status));
646 CHECK_VALUE(result.count, num_files);
648 if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
649 qsort(result.list, result.count, sizeof(result.list[0]),
650 QSORT_CAST search_both_compare);
651 } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
652 qsort(result.list, result.count, sizeof(result.list[0]),
653 QSORT_CAST search_standard_compare);
654 } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
655 qsort(result.list, result.count, sizeof(result.list[0]),
656 QSORT_CAST search_ea_size_compare);
657 } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
658 qsort(result.list, result.count, sizeof(result.list[0]),
659 QSORT_CAST search_directory_info_compare);
661 qsort(result.list, result.count, sizeof(result.list[0]),
662 QSORT_CAST search_old_compare);
665 for (i=0;i<result.count;i++) {
667 if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
668 s = result.list[i].both_directory_info.name.s;
669 } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
670 s = result.list[i].standard.name.s;
671 } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
672 s = result.list[i].ea_size.name.s;
673 } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
674 s = result.list[i].directory_info.name.s;
676 s = result.list[i].search.name;
678 asprintf(&fname, "t%03d-%d.txt", i, i);
679 if (strcmp(fname, s)) {
680 printf("Incorrect name %s at entry %d\n", s, i);
686 talloc_free(result.mem_ctx);
690 smb_raw_exit(cli->session);
691 smbcli_deltree(cli->tree, BASEDIR);
697 check a individual file result
699 static BOOL check_result(struct multiple_result *result, const char *name, BOOL exist, uint32_t attrib)
702 for (i=0;i<result->count;i++) {
703 if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
705 if (i == result->count) {
707 printf("failed: '%s' should exist with attribute %s\n",
708 name, attrib_string(result->list, attrib));
715 printf("failed: '%s' should NOT exist (has attribute %s)\n",
716 name, attrib_string(result->list, result->list[i].both_directory_info.attrib));
720 if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
721 printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
723 attrib, result->list[i].both_directory_info.attrib);
730 test what happens when the directory is modified during a search
732 static BOOL test_modify_search(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
734 const int num_files = 20;
739 struct multiple_result result;
740 union smb_search_first io;
741 union smb_search_next io2;
742 union smb_setfileinfo sfinfo;
744 if (smbcli_deltree(cli->tree, BASEDIR) == -1 ||
745 NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
746 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
750 printf("Creating %d files\n", num_files);
752 for (i=num_files-1;i>=0;i--) {
753 asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
754 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
756 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
761 smbcli_close(cli->tree, fnum);
764 printf("pulling the first file\n");
766 result.mem_ctx = talloc(mem_ctx, 0);
768 io.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
769 io.t2ffirst.in.search_attrib = 0;
770 io.t2ffirst.in.max_count = 0;
771 io.t2ffirst.in.flags = 0;
772 io.t2ffirst.in.storage_type = 0;
773 io.t2ffirst.in.pattern = BASEDIR "\\*.*";
775 status = smb_raw_search_first(cli->tree, mem_ctx,
776 &io, &result, multiple_search_callback);
777 CHECK_STATUS(status, NT_STATUS_OK);
778 CHECK_VALUE(result.count, 1);
780 printf("pulling the second file\n");
781 io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
782 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
783 io2.t2fnext.in.max_count = 1;
784 io2.t2fnext.in.resume_key = 0;
785 io2.t2fnext.in.flags = 0;
786 if (result.count == 0) {
787 io2.t2fnext.in.last_name = "";
789 io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
792 status = smb_raw_search_next(cli->tree, mem_ctx,
793 &io2, &result, multiple_search_callback);
794 CHECK_STATUS(status, NT_STATUS_OK);
795 CHECK_VALUE(result.count, 2);
797 printf("Changing attributes and deleting\n");
798 smbcli_open(cli->tree, BASEDIR "\\T003-03.txt.2", O_CREAT|O_RDWR, DENY_NONE);
799 smbcli_open(cli->tree, BASEDIR "\\T013-13.txt.2", O_CREAT|O_RDWR, DENY_NONE);
800 fnum = create_complex_file(cli, mem_ctx, BASEDIR "\\T013-13.txt.3");
801 smbcli_unlink(cli->tree, BASEDIR "\\T014-14.txt");
802 torture_set_file_attribute(cli->tree, BASEDIR "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
803 torture_set_file_attribute(cli->tree, BASEDIR "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
804 torture_set_file_attribute(cli->tree, BASEDIR "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);
805 torture_set_file_attribute(cli->tree, BASEDIR "\\T018-18.txt", 0);
806 sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
807 sfinfo.generic.file.fnum = fnum;
808 sfinfo.disposition_info.in.delete_on_close = 1;
809 status = smb_raw_setfileinfo(cli->tree, &sfinfo);
810 CHECK_STATUS(status, NT_STATUS_OK);
812 io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
813 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
814 io2.t2fnext.in.max_count = num_files - 1;
815 io2.t2fnext.in.resume_key = 0;
816 io2.t2fnext.in.flags = 0;
817 io2.t2fnext.in.last_name = result.list[result.count-2].both_directory_info.name.s;
819 status = smb_raw_search_next(cli->tree, mem_ctx,
820 &io2, &result, multiple_search_callback);
821 CHECK_STATUS(status, NT_STATUS_OK);
822 CHECK_VALUE(result.count, 21);
824 ret &= check_result(&result, "t009-9.txt", True, FILE_ATTRIBUTE_ARCHIVE);
825 ret &= check_result(&result, "t014-14.txt", False, 0);
826 ret &= check_result(&result, "t015-15.txt", False, 0);
827 ret &= check_result(&result, "t016-16.txt", True, FILE_ATTRIBUTE_NORMAL);
828 ret &= check_result(&result, "t017-17.txt", False, 0);
829 ret &= check_result(&result, "t018-18.txt", True, FILE_ATTRIBUTE_ARCHIVE);
830 ret &= check_result(&result, "t019-19.txt", True, FILE_ATTRIBUTE_ARCHIVE);
831 ret &= check_result(&result, "T013-13.txt.2", True, FILE_ATTRIBUTE_ARCHIVE);
832 ret &= check_result(&result, "T003-3.txt.2", False, 0);
833 ret &= check_result(&result, "T013-13.txt.3", True, FILE_ATTRIBUTE_ARCHIVE);
836 for (i=0;i<result.count;i++) {
837 printf("%s %s (0x%x)\n",
838 result.list[i].both_directory_info.name.s,
839 attrib_string(mem_ctx, result.list[i].both_directory_info.attrib),
840 result.list[i].both_directory_info.attrib);
845 smb_raw_exit(cli->session);
846 smbcli_deltree(cli->tree, BASEDIR);
853 testing if directories always come back sorted
855 static BOOL test_sorted(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
857 const int num_files = 700;
862 struct multiple_result result;
864 if (smbcli_deltree(cli->tree, BASEDIR) == -1 ||
865 NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
866 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
870 printf("Creating %d files\n", num_files);
872 for (i=0;i<num_files;i++) {
873 asprintf(&fname, BASEDIR "\\%s.txt", generate_random_str_list(mem_ctx, 10, "abcdefgh"));
874 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
876 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
881 smbcli_close(cli->tree, fnum);
886 result.mem_ctx = mem_ctx;
888 status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*",
889 RAW_SEARCH_BOTH_DIRECTORY_INFO,
891 CHECK_STATUS(status, NT_STATUS_OK);
892 CHECK_VALUE(result.count, num_files);
894 for (i=0;i<num_files-1;i++) {
895 const char *name1, *name2;
896 name1 = result.list[i].both_directory_info.name.s;
897 name2 = result.list[i+1].both_directory_info.name.s;
898 if (StrCaseCmp(name1, name2) > 0) {
899 printf("non-alphabetical order at entry %d '%s' '%s'\n",
901 printf("Server does not produce sorted directory listings (not an error)\n");
906 talloc_free(result.list);
909 smb_raw_exit(cli->session);
910 smbcli_deltree(cli->tree, BASEDIR);
917 basic testing of all RAW_SEARCH_* calls using a single file
919 BOOL torture_raw_search(int dummy)
921 struct smbcli_state *cli;
925 if (!torture_open_connection(&cli)) {
929 mem_ctx = talloc_init("torture_search");
931 if (!test_one_file(cli, mem_ctx)) {
935 if (!test_many_files(cli, mem_ctx)) {
939 if (!test_sorted(cli, mem_ctx)) {
943 if (!test_modify_search(cli, mem_ctx)) {
947 torture_close_connection(cli);
948 talloc_destroy(mem_ctx);