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("(%d) %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("(%d) %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("(%d) %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("(%d) %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("(%d) %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("(%d) %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("(%d) %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(data->list,
414 data->count * (sizeof(data->list[0])));
416 data->list[data->count-1] = *file;
421 enum continue_type {CONT_FLAGS, CONT_NAME, CONT_RESUME_KEY};
424 do a single file (non-wildcard) search
426 static NTSTATUS multiple_search(struct smbcli_state *cli,
429 enum smb_search_level level,
430 enum continue_type cont_type,
433 union smb_search_first io;
434 union smb_search_next io2;
436 const int per_search = 300;
437 struct multiple_result *result = data;
439 io.generic.level = level;
440 if (level == RAW_SEARCH_SEARCH) {
441 io.search_first.in.max_count = per_search;
442 io.search_first.in.search_attrib = 0;
443 io.search_first.in.pattern = pattern;
445 io.t2ffirst.in.search_attrib = 0;
446 io.t2ffirst.in.max_count = per_search;
447 io.t2ffirst.in.flags = 0;
448 io.t2ffirst.in.storage_type = 0;
449 io.t2ffirst.in.pattern = pattern;
450 if (cont_type == CONT_RESUME_KEY) {
451 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME |
452 FLAG_TRANS2_FIND_BACKUP_INTENT;
456 status = smb_raw_search_first(cli->tree, mem_ctx,
457 &io, data, multiple_search_callback);
460 while (NT_STATUS_IS_OK(status)) {
461 io2.generic.level = level;
462 if (level == RAW_SEARCH_SEARCH) {
463 io2.search_next.in.max_count = per_search;
464 io2.search_next.in.search_attrib = 0;
465 io2.search_next.in.id = result->list[result->count-1].search.id;
467 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
468 io2.t2fnext.in.max_count = per_search;
469 io2.t2fnext.in.resume_key = 0;
470 io2.t2fnext.in.flags = 0;
471 io2.t2fnext.in.last_name = "";
473 case CONT_RESUME_KEY:
474 if (level == RAW_SEARCH_STANDARD) {
475 io2.t2fnext.in.resume_key =
476 result->list[result->count-1].standard.resume_key;
477 } else if (level == RAW_SEARCH_EA_SIZE) {
478 io2.t2fnext.in.resume_key =
479 result->list[result->count-1].ea_size.resume_key;
480 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
481 io2.t2fnext.in.resume_key =
482 result->list[result->count-1].directory_info.file_index;
484 io2.t2fnext.in.resume_key =
485 result->list[result->count-1].both_directory_info.file_index;
487 if (io2.t2fnext.in.resume_key == 0) {
488 printf("Server does not support resume by key\n");
489 return NT_STATUS_NOT_SUPPORTED;
491 io2.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME |
492 FLAG_TRANS2_FIND_BACKUP_INTENT;
495 if (level == RAW_SEARCH_STANDARD) {
496 io2.t2fnext.in.last_name =
497 result->list[result->count-1].standard.name.s;
498 } else if (level == RAW_SEARCH_EA_SIZE) {
499 io2.t2fnext.in.last_name =
500 result->list[result->count-1].ea_size.name.s;
501 } else if (level == RAW_SEARCH_DIRECTORY_INFO) {
502 io2.t2fnext.in.last_name =
503 result->list[result->count-1].directory_info.name.s;
505 io2.t2fnext.in.last_name =
506 result->list[result->count-1].both_directory_info.name.s;
510 io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CONTINUE;
515 status = smb_raw_search_next(cli->tree, mem_ctx,
516 &io2, data, multiple_search_callback);
517 if (!NT_STATUS_IS_OK(status)) {
520 if (level == RAW_SEARCH_SEARCH) {
521 if (io2.search_next.out.count == 0) {
524 } else if (io2.t2fnext.out.count == 0 ||
525 io2.t2fnext.out.end_of_search) {
533 #define CHECK_STATUS(status, correct) do { \
534 if (!NT_STATUS_EQUAL(status, correct)) { \
535 printf("(%d) Incorrect status %s - should be %s\n", \
536 __LINE__, nt_errstr(status), nt_errstr(correct)); \
541 #define CHECK_VALUE(v, correct) do { \
542 if ((v) != (correct)) { \
543 printf("(%d) Incorrect value %s=%d - should be %d\n", \
544 __LINE__, #v, v, correct); \
549 static int search_both_compare(union smb_search_data *d1, union smb_search_data *d2)
551 return strcmp_safe(d1->both_directory_info.name.s, d2->both_directory_info.name.s);
554 static int search_standard_compare(union smb_search_data *d1, union smb_search_data *d2)
556 return strcmp_safe(d1->standard.name.s, d2->standard.name.s);
559 static int search_ea_size_compare(union smb_search_data *d1, union smb_search_data *d2)
561 return strcmp_safe(d1->ea_size.name.s, d2->ea_size.name.s);
564 static int search_directory_info_compare(union smb_search_data *d1, union smb_search_data *d2)
566 return strcmp_safe(d1->directory_info.name.s, d2->directory_info.name.s);
569 static int search_old_compare(union smb_search_data *d1, union smb_search_data *d2)
571 return strcmp_safe(d1->search.name, d2->search.name);
576 basic testing of search calls using many files
578 static BOOL test_many_files(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
580 const int num_files = 700;
585 struct multiple_result result;
588 const char *cont_name;
589 enum smb_search_level level;
590 enum continue_type cont_type;
592 {"BOTH_DIRECTORY_INFO", "NAME", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_NAME},
593 {"BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_FLAGS},
594 {"BOTH_DIRECTORY_INFO", "KEY", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY},
595 {"STANDARD", "FLAGS", RAW_SEARCH_STANDARD, CONT_FLAGS},
596 {"STANDARD", "KEY", RAW_SEARCH_STANDARD, CONT_RESUME_KEY},
597 {"STANDARD", "NAME", RAW_SEARCH_STANDARD, CONT_NAME},
598 {"EA_SIZE", "FLAGS", RAW_SEARCH_EA_SIZE, CONT_FLAGS},
599 {"EA_SIZE", "KEY", RAW_SEARCH_EA_SIZE, CONT_RESUME_KEY},
600 {"EA_SIZE", "NAME", RAW_SEARCH_EA_SIZE, CONT_NAME},
601 {"DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DIRECTORY_INFO, CONT_FLAGS},
602 {"DIRECTORY_INFO", "KEY", RAW_SEARCH_DIRECTORY_INFO, CONT_RESUME_KEY},
603 {"DIRECTORY_INFO", "NAME", RAW_SEARCH_DIRECTORY_INFO, CONT_NAME},
604 {"SEARCH", "ID", RAW_SEARCH_SEARCH, CONT_RESUME_KEY}
607 if (smbcli_deltree(cli->tree, BASEDIR) == -1 ||
608 NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
609 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
613 printf("Creating %d files\n", num_files);
615 for (i=0;i<num_files;i++) {
616 asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
617 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
619 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
624 smbcli_close(cli->tree, fnum);
628 for (t=0;t<ARRAY_SIZE(search_types);t++) {
630 result.mem_ctx = mem_ctx;
632 printf("Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
634 status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*",
635 search_types[t].level,
636 search_types[t].cont_type,
639 if (!NT_STATUS_IS_OK(status)) {
640 printf("search failed - %s\n", nt_errstr(status));
644 CHECK_VALUE(result.count, num_files);
646 if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
647 qsort(result.list, result.count, sizeof(result.list[0]),
648 QSORT_CAST search_both_compare);
649 } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
650 qsort(result.list, result.count, sizeof(result.list[0]),
651 QSORT_CAST search_standard_compare);
652 } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
653 qsort(result.list, result.count, sizeof(result.list[0]),
654 QSORT_CAST search_ea_size_compare);
655 } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
656 qsort(result.list, result.count, sizeof(result.list[0]),
657 QSORT_CAST search_directory_info_compare);
659 qsort(result.list, result.count, sizeof(result.list[0]),
660 QSORT_CAST search_old_compare);
663 for (i=0;i<num_files;i++) {
665 if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
666 s = result.list[i].both_directory_info.name.s;
667 } else if (search_types[t].level == RAW_SEARCH_STANDARD) {
668 s = result.list[i].standard.name.s;
669 } else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
670 s = result.list[i].ea_size.name.s;
671 } else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
672 s = result.list[i].directory_info.name.s;
674 s = result.list[i].search.name;
676 asprintf(&fname, "t%03d-%d.txt", i, i);
677 if (strcmp(fname, s)) {
678 printf("Incorrect name %s at entry %d\n", s, i);
687 smb_raw_exit(cli->session);
688 smbcli_deltree(cli->tree, BASEDIR);
694 check a individual file result
696 static BOOL check_result(struct multiple_result *result, const char *name, BOOL exist, uint32_t attrib)
699 for (i=0;i<result->count;i++) {
700 if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
702 if (i == result->count) {
704 printf("failed: '%s' should exist with attribute %s\n",
705 name, attrib_string(NULL, attrib));
712 printf("failed: '%s' should NOT exist (has attribute %s)\n",
713 name, attrib_string(NULL, result->list[i].both_directory_info.attrib));
717 if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
718 printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
720 attrib, result->list[i].both_directory_info.attrib);
727 test what happens when the directory is modified during a search
729 static BOOL test_modify_search(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
731 const int num_files = 20;
736 struct multiple_result result;
737 union smb_search_first io;
738 union smb_search_next io2;
739 union smb_setfileinfo sfinfo;
741 if (smbcli_deltree(cli->tree, BASEDIR) == -1 ||
742 NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
743 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
747 printf("Creating %d files\n", num_files);
749 for (i=0;i<num_files;i++) {
750 asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
751 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
753 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
758 smbcli_close(cli->tree, fnum);
761 printf("pulling the first 10 files\n");
764 io.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
765 io.t2ffirst.in.search_attrib = 0;
766 io.t2ffirst.in.max_count = 0;
767 io.t2ffirst.in.flags = 0;
768 io.t2ffirst.in.storage_type = 0;
769 io.t2ffirst.in.pattern = BASEDIR "\\*.*";
771 status = smb_raw_search_first(cli->tree, mem_ctx,
772 &io, &result, multiple_search_callback);
773 CHECK_STATUS(status, NT_STATUS_OK);
774 CHECK_VALUE(result.count, 1);
776 io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
777 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
778 io2.t2fnext.in.max_count = num_files/2 - 1;
779 io2.t2fnext.in.resume_key = 0;
780 io2.t2fnext.in.flags = 0;
781 io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
783 status = smb_raw_search_next(cli->tree, mem_ctx,
784 &io2, &result, multiple_search_callback);
785 CHECK_STATUS(status, NT_STATUS_OK);
786 CHECK_VALUE(result.count, num_files/2);
788 printf("Changing attributes and deleting\n");
789 smbcli_open(cli->tree, BASEDIR "\\T003-03.txt.2", O_CREAT|O_RDWR, DENY_NONE);
790 smbcli_open(cli->tree, BASEDIR "\\T013-13.txt.2", O_CREAT|O_RDWR, DENY_NONE);
791 fnum = create_complex_file(cli, mem_ctx, BASEDIR "\\T013-13.txt.3");
792 smbcli_unlink(cli->tree, BASEDIR "\\T014-14.txt");
793 torture_set_file_attribute(cli->tree, BASEDIR "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
794 torture_set_file_attribute(cli->tree, BASEDIR "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
795 torture_set_file_attribute(cli->tree, BASEDIR "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);
796 sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
797 sfinfo.generic.file.fnum = fnum;
798 sfinfo.disposition_info.in.delete_on_close = 1;
799 status = smb_raw_setfileinfo(cli->tree, &sfinfo);
800 CHECK_STATUS(status, NT_STATUS_OK);
802 io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
803 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
804 io2.t2fnext.in.max_count = num_files/2;
805 io2.t2fnext.in.resume_key = 0;
806 io2.t2fnext.in.flags = 0;
807 io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
809 status = smb_raw_search_next(cli->tree, mem_ctx,
810 &io2, &result, multiple_search_callback);
811 CHECK_STATUS(status, NT_STATUS_OK);
812 CHECK_VALUE(result.count, 19);
814 ret &= check_result(&result, "t009-9.txt", True, FILE_ATTRIBUTE_ARCHIVE);
815 ret &= check_result(&result, "t014-14.txt", False, 0);
816 ret &= check_result(&result, "t015-15.txt", False, 0);
817 ret &= check_result(&result, "t016-16.txt", True, FILE_ATTRIBUTE_NORMAL);
818 ret &= check_result(&result, "t017-17.txt", False, 0);
819 ret &= check_result(&result, "t018-18.txt", True, FILE_ATTRIBUTE_ARCHIVE);
820 ret &= check_result(&result, "t019-19.txt", True, FILE_ATTRIBUTE_ARCHIVE);
821 ret &= check_result(&result, "T013-13.txt.2", True, FILE_ATTRIBUTE_ARCHIVE);
822 ret &= check_result(&result, "T003-3.txt.2", False, 0);
823 ret &= check_result(&result, "T013-13.txt.3", True, FILE_ATTRIBUTE_ARCHIVE);
826 for (i=0;i<result.count;i++) {
827 printf("%s %s (0x%x)\n",
828 result.list[i].both_directory_info.name.s,
829 attrib_string(mem_ctx, result.list[i].both_directory_info.attrib),
830 result.list[i].both_directory_info.attrib);
835 smb_raw_exit(cli->session);
836 smbcli_deltree(cli->tree, BASEDIR);
843 testing if directories always come back sorted
845 static BOOL test_sorted(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
847 const int num_files = 700;
852 struct multiple_result result;
854 if (smbcli_deltree(cli->tree, BASEDIR) == -1 ||
855 NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
856 printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
860 printf("Creating %d files\n", num_files);
862 for (i=0;i<num_files;i++) {
863 asprintf(&fname, BASEDIR "\\%s.txt", generate_random_str(mem_ctx, 10));
864 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
866 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
871 smbcli_close(cli->tree, fnum);
876 result.mem_ctx = mem_ctx;
878 status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*",
879 RAW_SEARCH_BOTH_DIRECTORY_INFO,
881 CHECK_STATUS(status, NT_STATUS_OK);
882 CHECK_VALUE(result.count, num_files);
884 for (i=0;i<num_files-1;i++) {
885 const char *name1, *name2;
886 name1 = result.list[i].both_directory_info.name.s;
887 name2 = result.list[i+1].both_directory_info.name.s;
888 if (StrCaseCmp(name1, name2) > 0) {
889 printf("non-alphabetical order at entry %d '%s' '%s'\n",
891 printf("Server does not produce sorted directory listings\n");
898 smb_raw_exit(cli->session);
899 smbcli_deltree(cli->tree, BASEDIR);
906 basic testing of all RAW_SEARCH_* calls using a single file
908 BOOL torture_raw_search(int dummy)
910 struct smbcli_state *cli;
914 if (!torture_open_connection(&cli)) {
918 mem_ctx = talloc_init("torture_search");
920 if (!test_one_file(cli, mem_ctx)) {
924 if (!test_many_files(cli, mem_ctx)) {
928 if (!test_sorted(cli, mem_ctx)) {
932 if (!test_modify_search(cli, mem_ctx)) {
936 torture_close_connection(cli);
937 talloc_destroy(mem_ctx);