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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "libcli/raw/raw_proto.h"
24 #include "libcli/libcli.h"
25 #include "torture/util.h"
26 #include "lib/util/tsort.h"
27 #include "torture/raw/proto.h"
31 #define BASEDIR "\\testsearch"
33 #define CHECK_STATUS_LEVEL(__tctx, __status, __level, __supp) \
35 if (NT_STATUS_EQUAL(__status, \
36 NT_STATUS_NOT_SUPPORTED) || \
37 NT_STATUS_EQUAL(__status, \
38 NT_STATUS_NOT_IMPLEMENTED)) { \
44 torture_assert_ntstatus_ok_goto(__tctx, \
45 __status, ret, done, #__level" failed"); \
47 torture_warning(__tctx, "(%s) Info " \
48 "level "#__level" is %s", \
49 __location__, nt_errstr(__status)); \
54 callback function for single_search
56 static bool single_search_callback(void *private_data, const union smb_search_data *file)
58 union smb_search_data *data = (union smb_search_data *)private_data;
66 do a single file (non-wildcard) search
68 NTSTATUS torture_single_search(struct smbcli_state *cli,
71 enum smb_search_level level,
72 enum smb_search_data_level data_level,
74 union smb_search_data *data)
76 union smb_search_first io;
77 union smb_search_close c;
81 case RAW_SEARCH_SEARCH:
82 case RAW_SEARCH_FFIRST:
83 case RAW_SEARCH_FUNIQUE:
84 io.search_first.level = level;
85 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
86 io.search_first.in.max_count = 1;
87 io.search_first.in.search_attrib = attrib;
88 io.search_first.in.pattern = pattern;
91 case RAW_SEARCH_TRANS2:
92 io.t2ffirst.level = RAW_SEARCH_TRANS2;
93 io.t2ffirst.data_level = data_level;
94 io.t2ffirst.in.search_attrib = attrib;
95 io.t2ffirst.in.max_count = 1;
96 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
97 io.t2ffirst.in.storage_type = 0;
98 io.t2ffirst.in.pattern = pattern;
101 case RAW_SEARCH_SMB2:
102 return NT_STATUS_INVALID_LEVEL;
105 status = smb_raw_search_first(cli->tree, tctx,
106 &io, (void *)data, single_search_callback);
108 if (NT_STATUS_IS_OK(status) && level == RAW_SEARCH_FFIRST) {
109 c.fclose.level = RAW_FINDCLOSE_FCLOSE;
110 c.fclose.in.max_count = 1;
111 c.fclose.in.search_attrib = 0;
112 c.fclose.in.id = data->search.id;
113 status = smb_raw_search_close(cli->tree, &c);
122 enum smb_search_level level;
123 enum smb_search_data_level data_level;
125 int resume_key_offset;
126 uint32_t capability_mask;
128 union smb_search_data data;
132 .level = RAW_SEARCH_FFIRST,
133 .data_level = RAW_SEARCH_DATA_SEARCH,
134 .name_offset = offsetof(union smb_search_data,
136 .resume_key_offset = -1,
140 .level = RAW_SEARCH_FUNIQUE,
141 .data_level = RAW_SEARCH_DATA_SEARCH,
142 .name_offset = offsetof(union smb_search_data,
144 .resume_key_offset = -1,
148 .level = RAW_SEARCH_SEARCH,
149 .data_level = RAW_SEARCH_DATA_SEARCH,
150 .name_offset = offsetof(union smb_search_data,
152 .resume_key_offset = -1,
156 .level = RAW_SEARCH_TRANS2,
157 .data_level = RAW_SEARCH_DATA_STANDARD,
158 .name_offset = offsetof(union smb_search_data,
160 .resume_key_offset = offsetof(union smb_search_data,
161 standard.resume_key),
165 .level = RAW_SEARCH_TRANS2,
166 .data_level = RAW_SEARCH_DATA_EA_SIZE,
167 .name_offset = offsetof(union smb_search_data,
169 .resume_key_offset = offsetof(union smb_search_data,
173 .name = "DIRECTORY_INFO",
174 .level = RAW_SEARCH_TRANS2,
175 .data_level = RAW_SEARCH_DATA_DIRECTORY_INFO,
176 .name_offset = offsetof(union smb_search_data,
177 directory_info.name.s),
178 .resume_key_offset = offsetof(union smb_search_data,
179 directory_info.file_index),
182 .name = "FULL_DIRECTORY_INFO",
183 .level = RAW_SEARCH_TRANS2,
184 .data_level = RAW_SEARCH_DATA_FULL_DIRECTORY_INFO,
185 .name_offset = offsetof(union smb_search_data,
186 full_directory_info.name.s),
187 .resume_key_offset = offsetof(union smb_search_data,
188 full_directory_info.file_index),
192 .level = RAW_SEARCH_TRANS2,
193 .data_level = RAW_SEARCH_DATA_NAME_INFO,
194 .name_offset = offsetof(union smb_search_data,
196 .resume_key_offset = offsetof(union smb_search_data,
197 name_info.file_index),
200 .name = "BOTH_DIRECTORY_INFO",
201 .level = RAW_SEARCH_TRANS2,
202 .data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
203 .name_offset = offsetof(union smb_search_data,
204 both_directory_info.name.s),
205 .resume_key_offset = offsetof(union smb_search_data,
206 both_directory_info.file_index),
209 .name = "ID_FULL_DIRECTORY_INFO",
210 .level = RAW_SEARCH_TRANS2,
211 .data_level = RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO,
212 .name_offset = offsetof(union smb_search_data,
213 id_full_directory_info.name.s),
214 .resume_key_offset = offsetof(union smb_search_data,
215 id_full_directory_info.file_index),
218 .name = "ID_BOTH_DIRECTORY_INFO",
219 .level = RAW_SEARCH_TRANS2,
220 .data_level = RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO,
221 .name_offset = offsetof(union smb_search_data,
222 id_both_directory_info.name.s),
223 .resume_key_offset = offsetof(union smb_search_data,
224 id_both_directory_info.file_index),
228 .level = RAW_SEARCH_TRANS2,
229 .data_level = RAW_SEARCH_DATA_UNIX_INFO,
230 .name_offset = offsetof(union smb_search_data,
232 .resume_key_offset = offsetof(union smb_search_data,
233 unix_info.file_index),
234 .capability_mask = CAP_UNIX
242 static const char *level_name(enum smb_search_level level,
243 enum smb_search_data_level data_level)
246 for (i=0;i<ARRAY_SIZE(levels);i++) {
247 if (level == levels[i].level &&
248 data_level == levels[i].data_level) {
249 return levels[i].name;
256 extract the name from a smb_data structure and level
258 static const char *extract_name(void *data, enum smb_search_level level,
259 enum smb_search_data_level data_level)
262 for (i=0;i<ARRAY_SIZE(levels);i++) {
263 if (level == levels[i].level &&
264 data_level == levels[i].data_level) {
265 return *(const char **)(levels[i].name_offset + (char *)data);
272 extract the name from a smb_data structure and level
274 static uint32_t extract_resume_key(void *data, enum smb_search_level level,
275 enum smb_search_data_level data_level)
278 for (i=0;i<ARRAY_SIZE(levels);i++) {
279 if (level == levels[i].level &&
280 data_level == levels[i].data_level) {
281 return *(uint32_t *)(levels[i].resume_key_offset + (char *)data);
287 /* find a level in the table by name */
288 static union smb_search_data *find(const char *name)
291 for (i=0;i<ARRAY_SIZE(levels);i++) {
292 if (NT_STATUS_IS_OK(levels[i].status) &&
293 strcmp(levels[i].name, name) == 0) {
294 return &levels[i].data;
301 * Negotiate SMB1+POSIX.
304 static NTSTATUS setup_smb1_posix(struct torture_context *tctx,
305 struct smbcli_state *cli_unix)
307 struct smb_trans2 tp;
311 uint32_t cap = cli_unix->transport->negotiate.capabilities;
313 if ((cap & CAP_UNIX) == 0) {
315 * Server doesn't support SMB1+POSIX.
316 * The caller will skip the UNIX info
319 torture_comment(tctx,
320 "Server doesn't support SMB1+POSIX\n");
324 /* Setup POSIX on this connection. */
325 SSVAL(data, 0, CIFS_UNIX_MAJOR_VERSION);
326 SSVAL(data, 2, CIFS_UNIX_MINOR_VERSION);
327 SBVAL(data,4,((uint64_t)(
328 CIFS_UNIX_POSIX_ACLS_CAP|
329 CIFS_UNIX_POSIX_PATHNAMES_CAP|
330 CIFS_UNIX_FCNTL_LOCKS_CAP|
331 CIFS_UNIX_EXTATTR_CAP|
332 CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP)));
333 setup = TRANSACT2_SETFSINFO;
337 tp.in.setup_count = 1;
340 tp.in.setup = &setup;
341 tp.in.trans_name = NULL;
343 SSVAL(params, 2, SMB_SET_CIFS_UNIX_INFO);
344 tp.in.params = data_blob_talloc(tctx, params, 4);
345 tp.in.data = data_blob_talloc(tctx, data, 12);
346 return smb_raw_trans2(cli_unix->tree, tctx, &tp);
350 basic testing of all RAW_SEARCH_* calls using a single file
352 static bool test_one_file(struct torture_context *tctx,
353 struct smbcli_state *cli,
354 struct smbcli_state *cli_unix)
358 const char *fname = "torture_search.txt";
359 const char *fname2 = "torture_search-NOTEXIST.txt";
362 union smb_fileinfo all_info, alt_info, name_info, internal_info;
363 bool all_info_supported, alt_info_supported, name_info_supported,
364 internal_info_supported;
365 union smb_search_data *s;
367 status = setup_smb1_posix(tctx, cli_unix);
368 if (!NT_STATUS_IS_OK(status)) {
371 __location__"setup_smb1_posix() failed (%s)\n",
377 fnum = create_complex_file(cli, tctx, fname);
381 __location__"ERROR: open of %s failed (%s)\n",
383 smbcli_errstr(cli->tree));
388 /* call all the levels */
389 for (i=0;i<ARRAY_SIZE(levels);i++) {
390 NTSTATUS expected_status;
391 uint32_t cap = cli->transport->negotiate.capabilities;
392 struct smbcli_state *cli_search = cli;
394 torture_comment(tctx, "Testing %s\n", levels[i].name);
396 if (levels[i].data_level == RAW_SEARCH_DATA_UNIX_INFO) {
398 * For an SMB1+POSIX info level, use the cli_unix
401 cli_search = cli_unix;
404 levels[i].status = torture_single_search(cli_search, tctx, fname,
406 levels[i].data_level,
410 /* see if this server claims to support this level */
411 if (((cap & levels[i].capability_mask) != levels[i].capability_mask)
412 || NT_STATUS_EQUAL(levels[i].status, NT_STATUS_NOT_SUPPORTED)) {
413 printf("search level %s(%d) not supported by server\n",
414 levels[i].name, (int)levels[i].level);
418 if (!NT_STATUS_IS_OK(levels[i].status)) {
421 __location__"search level %s(%d) failed - %s\n",
422 levels[i].name, (int)levels[i].level,
423 nt_errstr(levels[i].status));
428 status = torture_single_search(cli_search, tctx, fname2,
430 levels[i].data_level,
434 expected_status = NT_STATUS_NO_SUCH_FILE;
435 if (levels[i].level == RAW_SEARCH_SEARCH ||
436 levels[i].level == RAW_SEARCH_FFIRST ||
437 levels[i].level == RAW_SEARCH_FUNIQUE) {
438 expected_status = STATUS_NO_MORE_FILES;
440 if (!NT_STATUS_EQUAL(status, expected_status)) {
443 __location__"search level %s(%d) should fail with %s - %s\n",
444 levels[i].name, (int)levels[i].level,
445 nt_errstr(expected_status),
451 /* get the all_info file into to check against */
452 all_info.generic.level = RAW_FILEINFO_ALL_INFO;
453 all_info.generic.in.file.path = fname;
454 status = smb_raw_pathinfo(cli->tree, tctx, &all_info);
455 CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_ALL_INFO",
458 alt_info.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
459 alt_info.generic.in.file.path = fname;
460 status = smb_raw_pathinfo(cli->tree, tctx, &alt_info);
461 CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_ALT_NAME_INFO",
464 internal_info.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
465 internal_info.generic.in.file.path = fname;
466 status = smb_raw_pathinfo(cli->tree, tctx, &internal_info);
467 CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_INTERNAL_INFORMATION",
468 internal_info_supported);
470 name_info.generic.level = RAW_FILEINFO_NAME_INFO;
471 name_info.generic.in.file.path = fname;
472 status = smb_raw_pathinfo(cli->tree, tctx, &name_info);
473 CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_NAME_INFO",
474 name_info_supported);
476 #define CHECK_VAL(name, sname1, field1, v, sname2, field2) do { \
479 if ((s->sname1.field1) != (v.sname2.out.field2)) { \
480 torture_result(tctx,\
482 "(%s) %s/%s [0x%x] != %s/%s [0x%x]\n", \
484 #sname1, #field1, (int)s->sname1.field1, \
485 #sname2, #field2, (int)v.sname2.out.field2); \
490 #define CHECK_TIME(name, sname1, field1, v, sname2, field2) do { \
493 if (s->sname1.field1 != (~1 & nt_time_to_unix(v.sname2.out.field2))) { \
494 torture_result(tctx,\
496 "(%s) %s/%s [%s] != %s/%s [%s]\n", \
498 #sname1, #field1, timestring(tctx, s->sname1.field1), \
499 #sname2, #field2, nt_time_string(tctx, v.sname2.out.field2)); \
504 #define CHECK_NTTIME(name, sname1, field1, v, sname2, field2) do { \
507 if (s->sname1.field1 != v.sname2.out.field2) { \
508 torture_result(tctx,\
510 "(%s) %s/%s [%s] != %s/%s [%s]\n", \
512 #sname1, #field1, nt_time_string(tctx, s->sname1.field1), \
513 #sname2, #field2, nt_time_string(tctx, v.sname2.out.field2)); \
518 #define CHECK_STR(name, sname1, field1, v, sname2, field2) do { \
521 if (!s->sname1.field1 || strcmp(s->sname1.field1, v.sname2.out.field2.s)) { \
522 torture_result(tctx,\
524 "(%s) %s/%s [%s] != %s/%s [%s]\n", \
526 #sname1, #field1, s->sname1.field1, \
527 #sname2, #field2, v.sname2.out.field2.s); \
532 #define CHECK_WSTR(name, sname1, field1, v, sname2, field2, flags) do { \
535 if (!s->sname1.field1.s || \
536 strcmp(s->sname1.field1.s, v.sname2.out.field2.s) || \
537 wire_bad_flags(&s->sname1.field1, flags, cli->transport)) { \
538 torture_result(tctx,\
540 "(%s) %s/%s [%s] != %s/%s [%s]\n", \
542 #sname1, #field1, s->sname1.field1.s, \
543 #sname2, #field2, v.sname2.out.field2.s); \
548 #define CHECK_NAME(name, sname1, field1, fname, flags) do { \
551 if (!s->sname1.field1.s || \
552 strcmp(s->sname1.field1.s, fname) || \
553 wire_bad_flags(&s->sname1.field1, flags, cli->transport)) { \
554 torture_result(tctx,\
556 "(%s) %s/%s [%s] != %s\n", \
558 #sname1, #field1, s->sname1.field1.s, \
564 #define CHECK_UNIX_NAME(name, sname1, field1, fname, flags) do { \
567 if (!s->sname1.field1 || \
568 strcmp(s->sname1.field1, fname)) { \
569 torture_result(tctx,\
571 "(%s) %s/%s [%s] != %s\n", \
573 #sname1, #field1, s->sname1.field1, \
579 /* check that all the results are as expected */
580 CHECK_VAL("SEARCH", search, attrib, all_info, all_info, attrib&0xFFF);
581 CHECK_VAL("STANDARD", standard, attrib, all_info, all_info, attrib&0xFFF);
582 CHECK_VAL("EA_SIZE", ea_size, attrib, all_info, all_info, attrib&0xFFF);
583 CHECK_VAL("DIRECTORY_INFO", directory_info, attrib, all_info, all_info, attrib);
584 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, attrib, all_info, all_info, attrib);
585 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, attrib, all_info, all_info, attrib);
586 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, attrib, all_info, all_info, attrib);
587 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, attrib, all_info, all_info, attrib);
589 CHECK_TIME("SEARCH", search, write_time, all_info, all_info, write_time);
590 CHECK_TIME("STANDARD", standard, write_time, all_info, all_info, write_time);
591 CHECK_TIME("EA_SIZE", ea_size, write_time, all_info, all_info, write_time);
592 CHECK_TIME("STANDARD", standard, create_time, all_info, all_info, create_time);
593 CHECK_TIME("EA_SIZE", ea_size, create_time, all_info, all_info, create_time);
594 CHECK_TIME("STANDARD", standard, access_time, all_info, all_info, access_time);
595 CHECK_TIME("EA_SIZE", ea_size, access_time, all_info, all_info, access_time);
597 CHECK_NTTIME("DIRECTORY_INFO", directory_info, write_time, all_info, all_info, write_time);
598 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, write_time, all_info, all_info, write_time);
599 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, write_time, all_info, all_info, write_time);
600 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, write_time, all_info, all_info, write_time);
601 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, write_time, all_info, all_info, write_time);
603 CHECK_NTTIME("DIRECTORY_INFO", directory_info, create_time, all_info, all_info, create_time);
604 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
605 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
606 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, create_time, all_info, all_info, create_time);
607 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, create_time, all_info, all_info, create_time);
609 CHECK_NTTIME("DIRECTORY_INFO", directory_info, access_time, all_info, all_info, access_time);
610 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, access_time, all_info, all_info, access_time);
611 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, access_time, all_info, all_info, access_time);
612 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, access_time, all_info, all_info, access_time);
613 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, access_time, all_info, all_info, access_time);
615 CHECK_NTTIME("DIRECTORY_INFO", directory_info, change_time, all_info, all_info, change_time);
616 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, change_time, all_info, all_info, change_time);
617 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, change_time, all_info, all_info, change_time);
618 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, change_time, all_info, all_info, change_time);
619 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, change_time, all_info, all_info, change_time);
621 CHECK_VAL("SEARCH", search, size, all_info, all_info, size);
622 CHECK_VAL("STANDARD", standard, size, all_info, all_info, size);
623 CHECK_VAL("EA_SIZE", ea_size, size, all_info, all_info, size);
624 CHECK_VAL("DIRECTORY_INFO", directory_info, size, all_info, all_info, size);
625 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, size, all_info, all_info, size);
626 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, size, all_info, all_info, size);
627 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, size, all_info, all_info, size);
628 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, size, all_info, all_info, size);
629 CHECK_VAL("UNIX_INFO", unix_info, size, all_info, all_info, size);
631 CHECK_VAL("STANDARD", standard, alloc_size, all_info, all_info, alloc_size);
632 CHECK_VAL("EA_SIZE", ea_size, alloc_size, all_info, all_info, alloc_size);
633 CHECK_VAL("DIRECTORY_INFO", directory_info, alloc_size, all_info, all_info, alloc_size);
634 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, alloc_size, all_info, all_info, alloc_size);
635 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, alloc_size, all_info, all_info, alloc_size);
636 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, alloc_size, all_info, all_info, alloc_size);
637 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, alloc_size, all_info, all_info, alloc_size);
638 CHECK_VAL("UNIX_INFO", unix_info, alloc_size, all_info, all_info, alloc_size);
640 CHECK_VAL("EA_SIZE", ea_size, ea_size, all_info, all_info, ea_size);
641 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, ea_size, all_info, all_info, ea_size);
642 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, ea_size, all_info, all_info, ea_size);
643 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, ea_size, all_info, all_info, ea_size);
644 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, ea_size, all_info, all_info, ea_size);
646 if (alt_info_supported) {
647 CHECK_STR("SEARCH", search, name, alt_info, alt_name_info,
649 CHECK_WSTR("BOTH_DIRECTORY_INFO", both_directory_info,
650 short_name, alt_info, alt_name_info, fname, STR_UNICODE);
653 CHECK_NAME("STANDARD", standard, name, fname, 0);
654 CHECK_NAME("EA_SIZE", ea_size, name, fname, 0);
655 CHECK_NAME("DIRECTORY_INFO", directory_info, name, fname, STR_TERMINATE_ASCII);
656 CHECK_NAME("FULL_DIRECTORY_INFO", full_directory_info, name, fname, STR_TERMINATE_ASCII);
658 if (name_info_supported) {
659 CHECK_NAME("NAME_INFO", name_info, name, fname,
660 STR_TERMINATE_ASCII);
663 CHECK_NAME("BOTH_DIRECTORY_INFO", both_directory_info, name, fname, STR_TERMINATE_ASCII);
664 CHECK_NAME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, name, fname, STR_TERMINATE_ASCII);
665 CHECK_NAME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, name, fname, STR_TERMINATE_ASCII);
666 CHECK_UNIX_NAME("UNIX_INFO", unix_info, name, fname, STR_TERMINATE_ASCII);
668 if (internal_info_supported) {
669 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,
670 file_id, internal_info, internal_information, file_id);
671 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,
672 file_id, internal_info, internal_information, file_id);
676 smb_raw_exit(cli->session);
677 smbcli_unlink(cli->tree, fname);
683 struct multiple_result {
686 union smb_search_data *list;
690 callback function for multiple_search
692 static bool multiple_search_callback(void *private_data, const union smb_search_data *file)
694 struct multiple_result *data = (struct multiple_result *)private_data;
698 data->list = talloc_realloc(data->tctx,
700 union smb_search_data,
703 data->list[data->count-1] = *file;
708 enum continue_type {CONT_FLAGS, CONT_NAME, CONT_RESUME_KEY};
711 do a single file (non-wildcard) search
713 static NTSTATUS multiple_search(struct smbcli_state *cli,
716 enum smb_search_data_level data_level,
717 enum continue_type cont_type,
720 union smb_search_first io;
721 union smb_search_next io2;
723 const int per_search = 100;
724 struct multiple_result *result = (struct multiple_result *)data;
726 if (data_level == RAW_SEARCH_DATA_SEARCH) {
727 io.search_first.level = RAW_SEARCH_SEARCH;
728 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
729 io.search_first.in.max_count = per_search;
730 io.search_first.in.search_attrib = 0;
731 io.search_first.in.pattern = pattern;
733 io.t2ffirst.level = RAW_SEARCH_TRANS2;
734 io.t2ffirst.data_level = data_level;
735 io.t2ffirst.in.search_attrib = 0;
736 io.t2ffirst.in.max_count = per_search;
737 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
738 io.t2ffirst.in.storage_type = 0;
739 io.t2ffirst.in.pattern = pattern;
740 if (cont_type == CONT_RESUME_KEY) {
741 io.t2ffirst.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
742 FLAG_TRANS2_FIND_BACKUP_INTENT;
746 status = smb_raw_search_first(cli->tree, tctx,
747 &io, data, multiple_search_callback);
750 while (NT_STATUS_IS_OK(status)) {
751 if (data_level == RAW_SEARCH_DATA_SEARCH) {
752 io2.search_next.level = RAW_SEARCH_SEARCH;
753 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
754 io2.search_next.in.max_count = per_search;
755 io2.search_next.in.search_attrib = 0;
756 io2.search_next.in.id = result->list[result->count-1].search.id;
758 io2.t2fnext.level = RAW_SEARCH_TRANS2;
759 io2.t2fnext.data_level = data_level;
760 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
761 io2.t2fnext.in.max_count = per_search;
762 io2.t2fnext.in.resume_key = 0;
763 io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
764 io2.t2fnext.in.last_name = "";
766 case CONT_RESUME_KEY:
767 io2.t2fnext.in.resume_key = extract_resume_key(&result->list[result->count-1],
768 io2.t2fnext.level, io2.t2fnext.data_level);
769 if (io2.t2fnext.in.resume_key == 0) {
770 printf("Server does not support resume by key for level %s\n",
771 level_name(io2.t2fnext.level, io2.t2fnext.data_level));
772 return NT_STATUS_NOT_SUPPORTED;
774 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
775 FLAG_TRANS2_FIND_BACKUP_INTENT;
778 io2.t2fnext.in.last_name = extract_name(&result->list[result->count-1],
779 io2.t2fnext.level, io2.t2fnext.data_level);
782 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_CONTINUE;
787 status = smb_raw_search_next(cli->tree, tctx,
788 &io2, data, multiple_search_callback);
789 if (!NT_STATUS_IS_OK(status)) {
792 if (data_level == RAW_SEARCH_DATA_SEARCH) {
793 if (io2.search_next.out.count == 0) {
796 } else if (io2.t2fnext.out.count == 0 ||
797 io2.t2fnext.out.end_of_search) {
805 #define CHECK_STATUS(status, correct) torture_assert_ntstatus_equal(tctx, status, correct, "incorrect status")
807 #define CHECK_VALUE(v, correct) torture_assert_int_equal(tctx, (v), (correct), "incorrect value");
809 #define CHECK_STRING(v, correct) torture_assert_casestr_equal(tctx, v, correct, "incorrect value");
812 static enum smb_search_data_level compare_data_level;
814 static int search_compare(union smb_search_data *d1, union smb_search_data *d2)
817 enum smb_search_level level;
819 if (compare_data_level == RAW_SEARCH_DATA_SEARCH) {
820 level = RAW_SEARCH_SEARCH;
822 level = RAW_SEARCH_TRANS2;
825 s1 = extract_name(d1, level, compare_data_level);
826 s2 = extract_name(d2, level, compare_data_level);
827 return strcmp_safe(s1, s2);
833 basic testing of search calls using many files
835 static bool test_many_files(struct torture_context *tctx,
836 struct smbcli_state *cli)
838 const int num_files = 700;
843 struct multiple_result result;
846 const char *cont_name;
847 enum smb_search_data_level data_level;
848 enum continue_type cont_type;
850 {"SEARCH", "ID", RAW_SEARCH_DATA_SEARCH, CONT_RESUME_KEY},
851 {"BOTH_DIRECTORY_INFO", "NAME", RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_NAME},
852 {"BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_FLAGS},
853 {"BOTH_DIRECTORY_INFO", "KEY", RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY},
854 {"STANDARD", "FLAGS", RAW_SEARCH_DATA_STANDARD, CONT_FLAGS},
855 {"STANDARD", "KEY", RAW_SEARCH_DATA_STANDARD, CONT_RESUME_KEY},
856 {"STANDARD", "NAME", RAW_SEARCH_DATA_STANDARD, CONT_NAME},
857 {"EA_SIZE", "FLAGS", RAW_SEARCH_DATA_EA_SIZE, CONT_FLAGS},
858 {"EA_SIZE", "KEY", RAW_SEARCH_DATA_EA_SIZE, CONT_RESUME_KEY},
859 {"EA_SIZE", "NAME", RAW_SEARCH_DATA_EA_SIZE, CONT_NAME},
860 {"DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_DIRECTORY_INFO, CONT_FLAGS},
861 {"DIRECTORY_INFO", "KEY", RAW_SEARCH_DATA_DIRECTORY_INFO, CONT_RESUME_KEY},
862 {"DIRECTORY_INFO", "NAME", RAW_SEARCH_DATA_DIRECTORY_INFO, CONT_NAME},
863 {"FULL_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_FULL_DIRECTORY_INFO, CONT_FLAGS},
864 {"FULL_DIRECTORY_INFO", "KEY", RAW_SEARCH_DATA_FULL_DIRECTORY_INFO, CONT_RESUME_KEY},
865 {"FULL_DIRECTORY_INFO", "NAME", RAW_SEARCH_DATA_FULL_DIRECTORY_INFO, CONT_NAME},
866 {"ID_FULL_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_FLAGS},
867 {"ID_FULL_DIRECTORY_INFO", "KEY", RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_RESUME_KEY},
868 {"ID_FULL_DIRECTORY_INFO", "NAME", RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_NAME},
869 {"ID_BOTH_DIRECTORY_INFO", "NAME", RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_NAME},
870 {"ID_BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_FLAGS},
871 {"ID_BOTH_DIRECTORY_INFO", "KEY", RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY}
874 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
876 torture_comment(tctx, "Testing with %d files\n", num_files);
878 for (i=0;i<num_files;i++) {
879 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
880 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
881 torture_assert(tctx, fnum != -1, "Failed to create");
883 smbcli_close(cli->tree, fnum);
887 for (t=0;t<ARRAY_SIZE(search_types);t++) {
890 if ((search_types[t].cont_type == CONT_RESUME_KEY) &&
891 (search_types[t].data_level != RAW_SEARCH_DATA_SEARCH) &&
892 !torture_setting_bool(tctx, "resume_key_support", true)) {
893 torture_comment(tctx,
894 "SKIP: Continue %s via %s\n",
895 search_types[t].name, search_types[t].cont_name);
899 result.tctx = talloc_new(tctx);
901 torture_comment(tctx,
902 "Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
904 status = multiple_search(cli, tctx, BASEDIR "\\*.*",
905 search_types[t].data_level,
906 search_types[t].cont_type,
908 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
909 torture_warning(tctx, "search level %s not supported "
911 search_types[t].name);
914 torture_assert_ntstatus_ok(tctx, status, "search failed");
915 CHECK_VALUE(result.count, num_files);
917 compare_data_level = search_types[t].data_level;
919 TYPESAFE_QSORT(result.list, result.count, search_compare);
921 for (i=0;i<result.count;i++) {
923 enum smb_search_level level;
924 if (compare_data_level == RAW_SEARCH_DATA_SEARCH) {
925 level = RAW_SEARCH_SEARCH;
927 level = RAW_SEARCH_TRANS2;
929 s = extract_name(&result.list[i], level, compare_data_level);
930 fname = talloc_asprintf(cli, "t%03d-%d.txt", i, i);
931 torture_assert_str_equal(tctx, fname, s, "Incorrect name");
934 talloc_free(result.tctx);
937 smb_raw_exit(cli->session);
938 smbcli_deltree(cli->tree, BASEDIR);
944 check a individual file result
946 static bool check_result(struct multiple_result *result, const char *name, bool exist, uint32_t attrib)
949 for (i=0;i<result->count;i++) {
950 if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
952 if (i == result->count) {
954 printf("failed: '%s' should exist with attribute %s\n",
955 name, attrib_string(result->list, attrib));
962 printf("failed: '%s' should NOT exist (has attribute %s)\n",
963 name, attrib_string(result->list, result->list[i].both_directory_info.attrib));
967 if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
968 printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
970 attrib, result->list[i].both_directory_info.attrib);
977 test what happens when the directory is modified during a search
979 static bool test_modify_search(struct torture_context *tctx,
980 struct smbcli_state *cli)
982 const int num_files = 20;
987 struct multiple_result result;
988 union smb_search_first io;
989 union smb_search_next io2;
990 union smb_setfileinfo sfinfo;
992 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
994 printf("Creating %d files\n", num_files);
996 for (i=num_files-1;i>=0;i--) {
997 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
998 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1000 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1005 smbcli_close(cli->tree, fnum);
1008 printf("pulling the first file\n");
1009 ZERO_STRUCT(result);
1010 result.tctx = talloc_new(tctx);
1012 io.t2ffirst.level = RAW_SEARCH_TRANS2;
1013 io.t2ffirst.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1014 io.t2ffirst.in.search_attrib = 0;
1015 io.t2ffirst.in.max_count = 0;
1016 io.t2ffirst.in.flags = 0;
1017 io.t2ffirst.in.storage_type = 0;
1018 io.t2ffirst.in.pattern = BASEDIR "\\*.*";
1020 status = smb_raw_search_first(cli->tree, tctx,
1021 &io, &result, multiple_search_callback);
1022 CHECK_STATUS(status, NT_STATUS_OK);
1023 CHECK_VALUE(result.count, 1);
1025 printf("pulling the second file\n");
1026 io2.t2fnext.level = RAW_SEARCH_TRANS2;
1027 io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1028 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1029 io2.t2fnext.in.max_count = 1;
1030 io2.t2fnext.in.resume_key = 0;
1031 io2.t2fnext.in.flags = 0;
1032 io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
1034 status = smb_raw_search_next(cli->tree, tctx,
1035 &io2, &result, multiple_search_callback);
1036 CHECK_STATUS(status, NT_STATUS_OK);
1037 CHECK_VALUE(result.count, 2);
1041 printf("Changing attributes and deleting\n");
1042 smbcli_open(cli->tree, BASEDIR "\\T003-03.txt.2", O_CREAT|O_RDWR, DENY_NONE);
1043 smbcli_open(cli->tree, BASEDIR "\\T013-13.txt.2", O_CREAT|O_RDWR, DENY_NONE);
1044 fnum = create_complex_file(cli, tctx, BASEDIR "\\T013-13.txt.3");
1045 smbcli_unlink(cli->tree, BASEDIR "\\T014-14.txt");
1046 torture_set_file_attribute(cli->tree, BASEDIR "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
1047 torture_set_file_attribute(cli->tree, BASEDIR "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
1048 torture_set_file_attribute(cli->tree, BASEDIR "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);
1049 torture_set_file_attribute(cli->tree, BASEDIR "\\T018-18.txt", 0);
1050 sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
1051 sfinfo.generic.in.file.fnum = fnum;
1052 sfinfo.disposition_info.in.delete_on_close = 1;
1053 status = smb_raw_setfileinfo(cli->tree, &sfinfo);
1054 CHECK_STATUS(status, NT_STATUS_OK);
1056 io2.t2fnext.level = RAW_SEARCH_TRANS2;
1057 io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1058 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1059 io2.t2fnext.in.max_count = num_files + 3;
1060 io2.t2fnext.in.resume_key = 0;
1061 io2.t2fnext.in.flags = 0;
1062 io2.t2fnext.in.last_name = ".";
1064 status = smb_raw_search_next(cli->tree, tctx,
1065 &io2, &result, multiple_search_callback);
1066 CHECK_STATUS(status, NT_STATUS_OK);
1067 CHECK_VALUE(result.count, 20);
1069 ret &= check_result(&result, "t009-9.txt", true, FILE_ATTRIBUTE_ARCHIVE);
1070 ret &= check_result(&result, "t014-14.txt", false, 0);
1071 ret &= check_result(&result, "t015-15.txt", false, 0);
1072 ret &= check_result(&result, "t016-16.txt", true, FILE_ATTRIBUTE_NORMAL);
1073 ret &= check_result(&result, "t017-17.txt", false, 0);
1074 ret &= check_result(&result, "t018-18.txt", true, FILE_ATTRIBUTE_ARCHIVE);
1075 ret &= check_result(&result, "t019-19.txt", true, FILE_ATTRIBUTE_ARCHIVE);
1076 ret &= check_result(&result, "T013-13.txt.2", true, FILE_ATTRIBUTE_ARCHIVE);
1077 ret &= check_result(&result, "T003-3.txt.2", false, 0);
1078 ret &= check_result(&result, "T013-13.txt.3", true, FILE_ATTRIBUTE_ARCHIVE);
1081 for (i=0;i<result.count;i++) {
1082 printf("%s %s (0x%x)\n",
1083 result.list[i].both_directory_info.name.s,
1084 attrib_string(tctx, result.list[i].both_directory_info.attrib),
1085 result.list[i].both_directory_info.attrib);
1090 smb_raw_exit(cli->session);
1091 smbcli_deltree(cli->tree, BASEDIR);
1098 testing if directories always come back sorted
1100 static bool test_sorted(struct torture_context *tctx, struct smbcli_state *cli)
1102 const int num_files = 700;
1107 struct multiple_result result;
1109 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1111 printf("Creating %d files\n", num_files);
1113 for (i=0;i<num_files;i++) {
1114 fname = talloc_asprintf(cli, BASEDIR "\\%s.txt", generate_random_str_list(tctx, 10, "abcdefgh"));
1115 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1117 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1122 smbcli_close(cli->tree, fnum);
1126 ZERO_STRUCT(result);
1129 status = multiple_search(cli, tctx, BASEDIR "\\*.*",
1130 RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
1131 CONT_NAME, &result);
1132 CHECK_STATUS(status, NT_STATUS_OK);
1133 CHECK_VALUE(result.count, num_files);
1135 for (i=0;i<num_files-1;i++) {
1136 const char *name1, *name2;
1137 name1 = result.list[i].both_directory_info.name.s;
1138 name2 = result.list[i+1].both_directory_info.name.s;
1139 if (strcasecmp_m(name1, name2) > 0) {
1140 printf("non-alphabetical order at entry %d '%s' '%s'\n",
1142 printf("Server does not produce sorted directory listings (not an error)\n");
1147 talloc_free(result.list);
1150 smb_raw_exit(cli->session);
1151 smbcli_deltree(cli->tree, BASEDIR);
1159 basic testing of many old style search calls using separate dirs
1161 static bool test_many_dirs(struct torture_context *tctx,
1162 struct smbcli_state *cli)
1164 const int num_dirs = 20;
1166 char *fname, *dname;
1169 union smb_search_data *file, *file2, *file3;
1171 if (!torture_setting_bool(tctx, "raw_search_search", true)) {
1172 torture_comment(tctx, "Skipping these tests as the server "
1173 "doesn't support old style search calls\n");
1176 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1178 printf("Creating %d dirs\n", num_dirs);
1180 for (i=0;i<num_dirs;i++) {
1181 dname = talloc_asprintf(cli, BASEDIR "\\d%d", i);
1182 status = smbcli_mkdir(cli->tree, dname);
1183 if (!NT_STATUS_IS_OK(status)) {
1184 printf("(%s) Failed to create %s - %s\n",
1185 __location__, dname, nt_errstr(status));
1191 fname = talloc_asprintf(cli, BASEDIR "\\d%d\\f%d-%d.txt", i, i, n);
1192 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1194 printf("(%s) Failed to create %s - %s\n",
1195 __location__, fname, smbcli_errstr(cli->tree));
1200 smbcli_close(cli->tree, fnum);
1206 file = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1207 file2 = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1208 file3 = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1210 printf("Search first on %d dirs\n", num_dirs);
1212 for (i=0;i<num_dirs;i++) {
1213 union smb_search_first io;
1214 io.search_first.level = RAW_SEARCH_SEARCH;
1215 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
1216 io.search_first.in.max_count = 1;
1217 io.search_first.in.search_attrib = 0;
1218 io.search_first.in.pattern = talloc_asprintf(tctx, BASEDIR "\\d%d\\*.txt", i);
1219 fname = talloc_asprintf(tctx, "f%d-", i);
1221 io.search_first.out.count = 0;
1223 status = smb_raw_search_first(cli->tree, tctx,
1224 &io, (void *)&file[i], single_search_callback);
1225 if (io.search_first.out.count != 1) {
1226 printf("(%s) search first gave %d entries for dir %d - %s\n",
1227 __location__, io.search_first.out.count, i, nt_errstr(status));
1231 CHECK_STATUS(status, NT_STATUS_OK);
1232 if (strncasecmp(file[i].search.name, fname, strlen(fname)) != 0) {
1233 printf("(%s) incorrect name '%s' expected '%s'[12].txt\n",
1234 __location__, file[i].search.name, fname);
1242 printf("Search next on %d dirs\n", num_dirs);
1244 for (i=0;i<num_dirs;i++) {
1245 union smb_search_next io2;
1247 io2.search_next.level = RAW_SEARCH_SEARCH;
1248 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
1249 io2.search_next.in.max_count = 1;
1250 io2.search_next.in.search_attrib = 0;
1251 io2.search_next.in.id = file[i].search.id;
1252 fname = talloc_asprintf(tctx, "f%d-", i);
1254 io2.search_next.out.count = 0;
1256 status = smb_raw_search_next(cli->tree, tctx,
1257 &io2, (void *)&file2[i], single_search_callback);
1258 if (io2.search_next.out.count != 1) {
1259 printf("(%s) search next gave %d entries for dir %d - %s\n",
1260 __location__, io2.search_next.out.count, i, nt_errstr(status));
1264 CHECK_STATUS(status, NT_STATUS_OK);
1265 if (strncasecmp(file2[i].search.name, fname, strlen(fname)) != 0) {
1266 printf("(%s) incorrect name '%s' expected '%s'[12].txt\n",
1267 __location__, file2[i].search.name, fname);
1276 printf("Search next (rewind) on %d dirs\n", num_dirs);
1278 for (i=0;i<num_dirs;i++) {
1279 union smb_search_next io2;
1281 io2.search_next.level = RAW_SEARCH_SEARCH;
1282 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
1283 io2.search_next.in.max_count = 1;
1284 io2.search_next.in.search_attrib = 0;
1285 io2.search_next.in.id = file[i].search.id;
1286 fname = talloc_asprintf(tctx, "f%d-", i);
1287 io2.search_next.out.count = 0;
1289 status = smb_raw_search_next(cli->tree, tctx,
1290 &io2, (void *)&file3[i], single_search_callback);
1291 if (io2.search_next.out.count != 1) {
1292 printf("(%s) search next gave %d entries for dir %d - %s\n",
1293 __location__, io2.search_next.out.count, i, nt_errstr(status));
1297 CHECK_STATUS(status, NT_STATUS_OK);
1299 if (strncasecmp(file3[i].search.name, file2[i].search.name, 3) != 0) {
1300 printf("(%s) incorrect name '%s' on rewind at dir %d\n",
1301 __location__, file2[i].search.name, i);
1306 if (torture_setting_bool(tctx, "rewind_support", true) &&
1307 strcmp(file3[i].search.name, file2[i].search.name) != 0) {
1308 printf("(%s) server did not rewind - got '%s' expected '%s'\n",
1309 __location__, file3[i].search.name, file2[i].search.name);
1319 smb_raw_exit(cli->session);
1320 smbcli_deltree(cli->tree, BASEDIR);
1327 testing of OS/2 style delete
1329 static bool test_os2_delete(struct torture_context *tctx,
1330 struct smbcli_state *cli)
1332 const int num_files = 700;
1333 const int delete_count = 4;
1334 int total_deleted = 0;
1339 union smb_search_first io;
1340 union smb_search_next io2;
1341 struct multiple_result result;
1343 if (!torture_setting_bool(tctx, "search_ea_size", true)){
1344 torture_comment(tctx,
1345 "Server does not support RAW_SEARCH_EA_SIZE "
1346 "level. Skipping this test\n");
1350 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1352 printf("Testing OS/2 style delete on %d files\n", num_files);
1354 for (i=0;i<num_files;i++) {
1355 fname = talloc_asprintf(cli, BASEDIR "\\file%u.txt", i);
1356 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1358 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1363 smbcli_close(cli->tree, fnum);
1367 ZERO_STRUCT(result);
1370 io.t2ffirst.level = RAW_SEARCH_TRANS2;
1371 io.t2ffirst.data_level = RAW_SEARCH_DATA_EA_SIZE;
1372 io.t2ffirst.in.search_attrib = 0;
1373 io.t2ffirst.in.max_count = 100;
1374 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1375 io.t2ffirst.in.storage_type = 0;
1376 io.t2ffirst.in.pattern = BASEDIR "\\*";
1378 status = smb_raw_search_first(cli->tree, tctx,
1379 &io, &result, multiple_search_callback);
1380 CHECK_STATUS(status, NT_STATUS_OK);
1382 for (i=0;i<MIN(result.count, delete_count);i++) {
1383 fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
1384 status = smbcli_unlink(cli->tree, fname);
1385 CHECK_STATUS(status, NT_STATUS_OK);
1390 io2.t2fnext.level = RAW_SEARCH_TRANS2;
1391 io2.t2fnext.data_level = RAW_SEARCH_DATA_EA_SIZE;
1392 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1393 io2.t2fnext.in.max_count = 100;
1394 io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1395 io2.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1396 io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1399 ZERO_STRUCT(result);
1402 status = smb_raw_search_next(cli->tree, tctx,
1403 &io2, &result, multiple_search_callback);
1404 if (!NT_STATUS_IS_OK(status)) {
1408 for (i=0;i<MIN(result.count, delete_count);i++) {
1409 fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
1410 status = smbcli_unlink(cli->tree, fname);
1411 CHECK_STATUS(status, NT_STATUS_OK);
1417 io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1418 io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1420 } while (NT_STATUS_IS_OK(status) && result.count != 0);
1422 CHECK_STATUS(status, NT_STATUS_OK);
1424 if (total_deleted != num_files) {
1425 printf("error: deleted %d - expected to delete %d\n",
1426 total_deleted, num_files);
1431 smb_raw_exit(cli->session);
1432 smbcli_deltree(cli->tree, BASEDIR);
1438 static int ealist_cmp(union smb_search_data *r1, union smb_search_data *r2)
1440 return strcmp(r1->ea_list.name.s, r2->ea_list.name.s);
1444 testing of the rather strange ea_list level
1446 static bool test_ea_list(struct torture_context *tctx,
1447 struct smbcli_state *cli)
1452 union smb_search_first io;
1453 union smb_search_next nxt;
1454 struct multiple_result result;
1455 union smb_setfileinfo setfile;
1457 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1459 printf("Testing RAW_SEARCH_EA_LIST level\n");
1461 if (!torture_setting_bool(tctx, "search_ea_support", true) ||
1462 !torture_setting_bool(tctx, "ea_support", true)) {
1463 printf("..skipped per target configuration.\n");
1467 fnum = smbcli_open(cli->tree, BASEDIR "\\file1.txt", O_CREAT|O_RDWR, DENY_NONE);
1468 smbcli_close(cli->tree, fnum);
1470 fnum = smbcli_open(cli->tree, BASEDIR "\\file2.txt", O_CREAT|O_RDWR, DENY_NONE);
1471 smbcli_close(cli->tree, fnum);
1473 fnum = smbcli_open(cli->tree, BASEDIR "\\file3.txt", O_CREAT|O_RDWR, DENY_NONE);
1474 smbcli_close(cli->tree, fnum);
1476 setfile.generic.level = RAW_SFILEINFO_EA_SET;
1477 setfile.generic.in.file.path = BASEDIR "\\file2.txt";
1478 setfile.ea_set.in.num_eas = 2;
1479 setfile.ea_set.in.eas = talloc_array(tctx, struct ea_struct, 2);
1480 setfile.ea_set.in.eas[0].flags = 0;
1481 setfile.ea_set.in.eas[0].name.s = "EA ONE";
1482 setfile.ea_set.in.eas[0].value = data_blob_string_const("VALUE 1");
1483 setfile.ea_set.in.eas[1].flags = 0;
1484 setfile.ea_set.in.eas[1].name.s = "SECOND EA";
1485 setfile.ea_set.in.eas[1].value = data_blob_string_const("Value Two");
1487 status = smb_raw_setpathinfo(cli->tree, &setfile);
1488 CHECK_STATUS(status, NT_STATUS_OK);
1490 setfile.generic.in.file.path = BASEDIR "\\file3.txt";
1491 status = smb_raw_setpathinfo(cli->tree, &setfile);
1492 CHECK_STATUS(status, NT_STATUS_OK);
1494 ZERO_STRUCT(result);
1497 io.t2ffirst.level = RAW_SEARCH_TRANS2;
1498 io.t2ffirst.data_level = RAW_SEARCH_DATA_EA_LIST;
1499 io.t2ffirst.in.search_attrib = 0;
1500 io.t2ffirst.in.max_count = 2;
1501 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1502 io.t2ffirst.in.storage_type = 0;
1503 io.t2ffirst.in.pattern = BASEDIR "\\*";
1504 io.t2ffirst.in.num_names = 2;
1505 io.t2ffirst.in.ea_names = talloc_array(tctx, struct ea_name, 2);
1506 io.t2ffirst.in.ea_names[0].name.s = "SECOND EA";
1507 io.t2ffirst.in.ea_names[1].name.s = "THIRD EA";
1509 status = smb_raw_search_first(cli->tree, tctx,
1510 &io, &result, multiple_search_callback);
1511 CHECK_STATUS(status, NT_STATUS_OK);
1512 CHECK_VALUE(result.count, 2);
1514 nxt.t2fnext.level = RAW_SEARCH_TRANS2;
1515 nxt.t2fnext.data_level = RAW_SEARCH_DATA_EA_LIST;
1516 nxt.t2fnext.in.handle = io.t2ffirst.out.handle;
1517 nxt.t2fnext.in.max_count = 2;
1518 nxt.t2fnext.in.resume_key = result.list[1].ea_list.resume_key;
1519 nxt.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME | FLAG_TRANS2_FIND_CONTINUE;
1520 nxt.t2fnext.in.last_name = result.list[1].ea_list.name.s;
1521 nxt.t2fnext.in.num_names = 2;
1522 nxt.t2fnext.in.ea_names = talloc_array(tctx, struct ea_name, 2);
1523 nxt.t2fnext.in.ea_names[0].name.s = "SECOND EA";
1524 nxt.t2fnext.in.ea_names[1].name.s = "THIRD EA";
1526 status = smb_raw_search_next(cli->tree, tctx,
1527 &nxt, &result, multiple_search_callback);
1528 CHECK_STATUS(status, NT_STATUS_OK);
1530 /* we have to sort the result as different servers can return directories
1531 in different orders */
1532 TYPESAFE_QSORT(result.list, result.count, ealist_cmp);
1534 CHECK_VALUE(result.count, 3);
1535 CHECK_VALUE(result.list[0].ea_list.eas.num_eas, 2);
1536 CHECK_STRING(result.list[0].ea_list.name.s, "file1.txt");
1537 CHECK_STRING(result.list[0].ea_list.eas.eas[0].name.s, "SECOND EA");
1538 CHECK_VALUE(result.list[0].ea_list.eas.eas[0].value.length, 0);
1539 CHECK_STRING(result.list[0].ea_list.eas.eas[1].name.s, "THIRD EA");
1540 CHECK_VALUE(result.list[0].ea_list.eas.eas[1].value.length, 0);
1542 CHECK_STRING(result.list[1].ea_list.name.s, "file2.txt");
1543 CHECK_STRING(result.list[1].ea_list.eas.eas[0].name.s, "SECOND EA");
1544 CHECK_VALUE(result.list[1].ea_list.eas.eas[0].value.length, 9);
1545 CHECK_STRING((const char *)result.list[1].ea_list.eas.eas[0].value.data, "Value Two");
1546 CHECK_STRING(result.list[1].ea_list.eas.eas[1].name.s, "THIRD EA");
1547 CHECK_VALUE(result.list[1].ea_list.eas.eas[1].value.length, 0);
1549 CHECK_STRING(result.list[2].ea_list.name.s, "file3.txt");
1550 CHECK_STRING(result.list[2].ea_list.eas.eas[0].name.s, "SECOND EA");
1551 CHECK_VALUE(result.list[2].ea_list.eas.eas[0].value.length, 9);
1552 CHECK_STRING((const char *)result.list[2].ea_list.eas.eas[0].value.data, "Value Two");
1553 CHECK_STRING(result.list[2].ea_list.eas.eas[1].name.s, "THIRD EA");
1554 CHECK_VALUE(result.list[2].ea_list.eas.eas[1].value.length, 0);
1556 smb_raw_exit(cli->session);
1557 smbcli_deltree(cli->tree, BASEDIR);
1563 Test the behavior of max count parameter in TRANS2_FIND_FIRST2 and
1564 TRANS2_FIND_NEXT2 queries
1566 static bool test_max_count(struct torture_context *tctx,
1567 struct smbcli_state *cli)
1569 const int num_files = 2;
1574 struct multiple_result result;
1575 union smb_search_first io;
1576 union smb_search_next io2;
1578 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1580 torture_comment(tctx, "Creating %d files\n", num_files);
1582 for (i=num_files-1;i>=0;i--) {
1583 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
1584 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1586 torture_comment(tctx,
1587 "Failed to create %s - %s\n",
1588 fname, smbcli_errstr(cli->tree));
1593 smbcli_close(cli->tree, fnum);
1596 torture_comment(tctx, "Set max_count parameter to 0. "
1597 "This should return 1 entry\n");
1598 ZERO_STRUCT(result);
1599 result.tctx = talloc_new(tctx);
1601 io.t2ffirst.level = RAW_SEARCH_TRANS2;
1602 io.t2ffirst.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1603 io.t2ffirst.in.search_attrib = 0;
1604 io.t2ffirst.in.max_count = 0;
1605 io.t2ffirst.in.flags = 0;
1606 io.t2ffirst.in.storage_type = 0;
1607 io.t2ffirst.in.pattern = BASEDIR "\\*.*";
1609 status = smb_raw_search_first(cli->tree, tctx,
1610 &io, &result, multiple_search_callback);
1611 CHECK_STATUS(status, NT_STATUS_OK);
1612 CHECK_VALUE(result.count, 1);
1614 torture_comment(tctx, "Set max_count to 1. This should also "
1615 "return 1 entry\n");
1616 io2.t2fnext.level = RAW_SEARCH_TRANS2;
1617 io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1618 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1619 io2.t2fnext.in.max_count = 1;
1620 io2.t2fnext.in.resume_key = 0;
1621 io2.t2fnext.in.flags = 0;
1622 io2.t2fnext.in.last_name =
1623 result.list[result.count-1].both_directory_info.name.s;
1625 status = smb_raw_search_next(cli->tree, tctx,
1626 &io2, &result, multiple_search_callback);
1627 CHECK_STATUS(status, NT_STATUS_OK);
1628 CHECK_VALUE(result.count, 2);
1630 smb_raw_exit(cli->session);
1631 smbcli_deltree(cli->tree, BASEDIR);
1637 basic testing of all RAW_SEARCH_* calls using a single file
1639 struct torture_suite *torture_raw_search(TALLOC_CTX *mem_ctx)
1641 struct torture_suite *suite = torture_suite_create(mem_ctx, "search");
1643 torture_suite_add_2smb_test(suite, "one file search", test_one_file);
1644 torture_suite_add_1smb_test(suite, "many files", test_many_files);
1645 torture_suite_add_1smb_test(suite, "sorted", test_sorted);
1646 torture_suite_add_1smb_test(suite, "modify search", test_modify_search);
1647 torture_suite_add_1smb_test(suite, "many dirs", test_many_dirs);
1648 torture_suite_add_1smb_test(suite, "os2 delete", test_os2_delete);
1649 torture_suite_add_1smb_test(suite, "ea list", test_ea_list);
1650 torture_suite_add_1smb_test(suite, "max count", test_max_count);