dns: Do as BIND and MS DNS and drop quries with qdcount != 1
[Samba.git] / source4 / torture / raw / search.c
blobe93d2a10f71cb6f5cd01f207bf76afc87c50dfbd
1 /*
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/>.
20 #include "includes.h"
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"
30 #define BASEDIR "\\testsearch"
32 #define CHECK_STATUS_LEVEL(__tctx, __status, __level, __supp) \
33 do { \
34 if (NT_STATUS_EQUAL(__status, \
35 NT_STATUS_NOT_SUPPORTED) || \
36 NT_STATUS_EQUAL(__status, \
37 NT_STATUS_NOT_IMPLEMENTED)) { \
38 torture_warning(__tctx, "(%s) Info " \
39 "level "#__level" is %s", \
40 __location__, nt_errstr(__status)); \
41 __supp = false; \
42 } else { \
43 torture_assert_ntstatus_ok_goto(__tctx, \
44 __status, ret, done, #__level" failed"); \
45 __supp = true; \
46 } \
47 } while (0)
50 callback function for single_search
52 static bool single_search_callback(void *private_data, const union smb_search_data *file)
54 union smb_search_data *data = (union smb_search_data *)private_data;
56 *data = *file;
58 return true;
62 do a single file (non-wildcard) search
64 NTSTATUS torture_single_search(struct smbcli_state *cli,
65 TALLOC_CTX *tctx,
66 const char *pattern,
67 enum smb_search_level level,
68 enum smb_search_data_level data_level,
69 uint16_t attrib,
70 union smb_search_data *data)
72 union smb_search_first io;
73 union smb_search_close c;
74 NTSTATUS status;
76 switch (level) {
77 case RAW_SEARCH_SEARCH:
78 case RAW_SEARCH_FFIRST:
79 case RAW_SEARCH_FUNIQUE:
80 io.search_first.level = level;
81 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
82 io.search_first.in.max_count = 1;
83 io.search_first.in.search_attrib = attrib;
84 io.search_first.in.pattern = pattern;
85 break;
87 case RAW_SEARCH_TRANS2:
88 io.t2ffirst.level = RAW_SEARCH_TRANS2;
89 io.t2ffirst.data_level = data_level;
90 io.t2ffirst.in.search_attrib = attrib;
91 io.t2ffirst.in.max_count = 1;
92 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
93 io.t2ffirst.in.storage_type = 0;
94 io.t2ffirst.in.pattern = pattern;
95 break;
97 case RAW_SEARCH_SMB2:
98 return NT_STATUS_INVALID_LEVEL;
101 status = smb_raw_search_first(cli->tree, tctx,
102 &io, (void *)data, single_search_callback);
104 if (NT_STATUS_IS_OK(status) && level == RAW_SEARCH_FFIRST) {
105 c.fclose.level = RAW_FINDCLOSE_FCLOSE;
106 c.fclose.in.max_count = 1;
107 c.fclose.in.search_attrib = 0;
108 c.fclose.in.id = data->search.id;
109 status = smb_raw_search_close(cli->tree, &c);
112 return status;
116 static struct {
117 const char *name;
118 enum smb_search_level level;
119 enum smb_search_data_level data_level;
120 int name_offset;
121 int resume_key_offset;
122 uint32_t capability_mask;
123 NTSTATUS status;
124 union smb_search_data data;
125 } levels[] = {
126 {"FFIRST",
127 RAW_SEARCH_FFIRST, RAW_SEARCH_DATA_SEARCH,
128 offsetof(union smb_search_data, search.name),
131 {"FUNIQUE",
132 RAW_SEARCH_FUNIQUE, RAW_SEARCH_DATA_SEARCH,
133 offsetof(union smb_search_data, search.name),
136 {"SEARCH",
137 RAW_SEARCH_SEARCH, RAW_SEARCH_DATA_SEARCH,
138 offsetof(union smb_search_data, search.name),
141 {"STANDARD",
142 RAW_SEARCH_TRANS2, RAW_SEARCH_DATA_STANDARD,
143 offsetof(union smb_search_data, standard.name.s),
144 offsetof(union smb_search_data, standard.resume_key),
146 {"EA_SIZE",
147 RAW_SEARCH_TRANS2, RAW_SEARCH_DATA_EA_SIZE,
148 offsetof(union smb_search_data, ea_size.name.s),
149 offsetof(union smb_search_data, ea_size.resume_key),
151 {"DIRECTORY_INFO",
152 RAW_SEARCH_TRANS2, RAW_SEARCH_DATA_DIRECTORY_INFO,
153 offsetof(union smb_search_data, directory_info.name.s),
154 offsetof(union smb_search_data, directory_info.file_index),
156 {"FULL_DIRECTORY_INFO",
157 RAW_SEARCH_TRANS2, RAW_SEARCH_DATA_FULL_DIRECTORY_INFO,
158 offsetof(union smb_search_data, full_directory_info.name.s),
159 offsetof(union smb_search_data, full_directory_info.file_index),
161 {"NAME_INFO",
162 RAW_SEARCH_TRANS2, RAW_SEARCH_DATA_NAME_INFO,
163 offsetof(union smb_search_data, name_info.name.s),
164 offsetof(union smb_search_data, name_info.file_index),
166 {"BOTH_DIRECTORY_INFO",
167 RAW_SEARCH_TRANS2, RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
168 offsetof(union smb_search_data, both_directory_info.name.s),
169 offsetof(union smb_search_data, both_directory_info.file_index),
171 {"ID_FULL_DIRECTORY_INFO",
172 RAW_SEARCH_TRANS2, RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO,
173 offsetof(union smb_search_data, id_full_directory_info.name.s),
174 offsetof(union smb_search_data, id_full_directory_info.file_index),
176 {"ID_BOTH_DIRECTORY_INFO",
177 RAW_SEARCH_TRANS2, RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO,
178 offsetof(union smb_search_data, id_both_directory_info.name.s),
179 offsetof(union smb_search_data, id_both_directory_info.file_index),
181 {"UNIX_INFO",
182 RAW_SEARCH_TRANS2, RAW_SEARCH_DATA_UNIX_INFO,
183 offsetof(union smb_search_data, unix_info.name),
184 offsetof(union smb_search_data, unix_info.file_index),
185 CAP_UNIX}
190 return level name
192 static const char *level_name(enum smb_search_level level,
193 enum smb_search_data_level data_level)
195 int i;
196 for (i=0;i<ARRAY_SIZE(levels);i++) {
197 if (level == levels[i].level &&
198 data_level == levels[i].data_level) {
199 return levels[i].name;
202 return NULL;
206 extract the name from a smb_data structure and level
208 static const char *extract_name(void *data, enum smb_search_level level,
209 enum smb_search_data_level data_level)
211 int i;
212 for (i=0;i<ARRAY_SIZE(levels);i++) {
213 if (level == levels[i].level &&
214 data_level == levels[i].data_level) {
215 return *(const char **)(levels[i].name_offset + (char *)data);
218 return NULL;
222 extract the name from a smb_data structure and level
224 static uint32_t extract_resume_key(void *data, enum smb_search_level level,
225 enum smb_search_data_level data_level)
227 int i;
228 for (i=0;i<ARRAY_SIZE(levels);i++) {
229 if (level == levels[i].level &&
230 data_level == levels[i].data_level) {
231 return *(uint32_t *)(levels[i].resume_key_offset + (char *)data);
234 return 0;
237 /* find a level in the table by name */
238 static union smb_search_data *find(const char *name)
240 int i;
241 for (i=0;i<ARRAY_SIZE(levels);i++) {
242 if (NT_STATUS_IS_OK(levels[i].status) &&
243 strcmp(levels[i].name, name) == 0) {
244 return &levels[i].data;
247 return NULL;
251 basic testing of all RAW_SEARCH_* calls using a single file
253 static bool test_one_file(struct torture_context *tctx,
254 struct smbcli_state *cli)
256 bool ret = true;
257 int fnum;
258 const char *fname = "\\torture_search.txt";
259 const char *fname2 = "\\torture_search-NOTEXIST.txt";
260 NTSTATUS status;
261 int i;
262 union smb_fileinfo all_info, alt_info, name_info, internal_info;
263 bool all_info_supported, alt_info_supported, name_info_supported,
264 internal_info_supported;
265 union smb_search_data *s;
267 fnum = create_complex_file(cli, tctx, fname);
268 if (fnum == -1) {
269 printf("ERROR: open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
270 ret = false;
271 goto done;
274 /* call all the levels */
275 for (i=0;i<ARRAY_SIZE(levels);i++) {
276 NTSTATUS expected_status;
277 uint32_t cap = cli->transport->negotiate.capabilities;
279 torture_comment(tctx, "Testing %s\n", levels[i].name);
281 levels[i].status = torture_single_search(cli, tctx, fname,
282 levels[i].level,
283 levels[i].data_level,
285 &levels[i].data);
287 /* see if this server claims to support this level */
288 if (((cap & levels[i].capability_mask) != levels[i].capability_mask)
289 || NT_STATUS_EQUAL(levels[i].status, NT_STATUS_NOT_SUPPORTED)) {
290 printf("search level %s(%d) not supported by server\n",
291 levels[i].name, (int)levels[i].level);
292 continue;
295 if (!NT_STATUS_IS_OK(levels[i].status)) {
296 printf("search level %s(%d) failed - %s\n",
297 levels[i].name, (int)levels[i].level,
298 nt_errstr(levels[i].status));
299 ret = false;
300 continue;
303 status = torture_single_search(cli, tctx, fname2,
304 levels[i].level,
305 levels[i].data_level,
307 &levels[i].data);
309 expected_status = NT_STATUS_NO_SUCH_FILE;
310 if (levels[i].level == RAW_SEARCH_SEARCH ||
311 levels[i].level == RAW_SEARCH_FFIRST ||
312 levels[i].level == RAW_SEARCH_FUNIQUE) {
313 expected_status = STATUS_NO_MORE_FILES;
315 if (!NT_STATUS_EQUAL(status, expected_status)) {
316 printf("search level %s(%d) should fail with %s - %s\n",
317 levels[i].name, (int)levels[i].level,
318 nt_errstr(expected_status),
319 nt_errstr(status));
320 ret = false;
324 /* get the all_info file into to check against */
325 all_info.generic.level = RAW_FILEINFO_ALL_INFO;
326 all_info.generic.in.file.path = fname;
327 status = smb_raw_pathinfo(cli->tree, tctx, &all_info);
328 CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_ALL_INFO",
329 all_info_supported);
331 alt_info.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
332 alt_info.generic.in.file.path = fname;
333 status = smb_raw_pathinfo(cli->tree, tctx, &alt_info);
334 CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_ALT_NAME_INFO",
335 alt_info_supported);
337 internal_info.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
338 internal_info.generic.in.file.path = fname;
339 status = smb_raw_pathinfo(cli->tree, tctx, &internal_info);
340 CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_INTERNAL_INFORMATION",
341 internal_info_supported);
343 name_info.generic.level = RAW_FILEINFO_NAME_INFO;
344 name_info.generic.in.file.path = fname;
345 status = smb_raw_pathinfo(cli->tree, tctx, &name_info);
346 CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_NAME_INFO",
347 name_info_supported);
349 #define CHECK_VAL(name, sname1, field1, v, sname2, field2) do { \
350 s = find(name); \
351 if (s) { \
352 if ((s->sname1.field1) != (v.sname2.out.field2)) { \
353 printf("(%s) %s/%s [0x%x] != %s/%s [0x%x]\n", \
354 __location__, \
355 #sname1, #field1, (int)s->sname1.field1, \
356 #sname2, #field2, (int)v.sname2.out.field2); \
357 ret = false; \
359 }} while (0)
361 #define CHECK_TIME(name, sname1, field1, v, sname2, field2) do { \
362 s = find(name); \
363 if (s) { \
364 if (s->sname1.field1 != (~1 & nt_time_to_unix(v.sname2.out.field2))) { \
365 printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
366 __location__, \
367 #sname1, #field1, timestring(tctx, s->sname1.field1), \
368 #sname2, #field2, nt_time_string(tctx, v.sname2.out.field2)); \
369 ret = false; \
371 }} while (0)
373 #define CHECK_NTTIME(name, sname1, field1, v, sname2, field2) do { \
374 s = find(name); \
375 if (s) { \
376 if (s->sname1.field1 != v.sname2.out.field2) { \
377 printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
378 __location__, \
379 #sname1, #field1, nt_time_string(tctx, s->sname1.field1), \
380 #sname2, #field2, nt_time_string(tctx, v.sname2.out.field2)); \
381 ret = false; \
383 }} while (0)
385 #define CHECK_STR(name, sname1, field1, v, sname2, field2) do { \
386 s = find(name); \
387 if (s) { \
388 if (!s->sname1.field1 || strcmp(s->sname1.field1, v.sname2.out.field2.s)) { \
389 printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
390 __location__, \
391 #sname1, #field1, s->sname1.field1, \
392 #sname2, #field2, v.sname2.out.field2.s); \
393 ret = false; \
395 }} while (0)
397 #define CHECK_WSTR(name, sname1, field1, v, sname2, field2, flags) do { \
398 s = find(name); \
399 if (s) { \
400 if (!s->sname1.field1.s || \
401 strcmp(s->sname1.field1.s, v.sname2.out.field2.s) || \
402 wire_bad_flags(&s->sname1.field1, flags, cli->transport)) { \
403 printf("(%s) %s/%s [%s] != %s/%s [%s]\n", \
404 __location__, \
405 #sname1, #field1, s->sname1.field1.s, \
406 #sname2, #field2, v.sname2.out.field2.s); \
407 ret = false; \
409 }} while (0)
411 #define CHECK_NAME(name, sname1, field1, fname, flags) do { \
412 s = find(name); \
413 if (s) { \
414 if (!s->sname1.field1.s || \
415 strcmp(s->sname1.field1.s, fname) || \
416 wire_bad_flags(&s->sname1.field1, flags, cli->transport)) { \
417 printf("(%s) %s/%s [%s] != %s\n", \
418 __location__, \
419 #sname1, #field1, s->sname1.field1.s, \
420 fname); \
421 ret = false; \
423 }} while (0)
425 #define CHECK_UNIX_NAME(name, sname1, field1, fname, flags) do { \
426 s = find(name); \
427 if (s) { \
428 if (!s->sname1.field1 || \
429 strcmp(s->sname1.field1, fname)) { \
430 printf("(%s) %s/%s [%s] != %s\n", \
431 __location__, \
432 #sname1, #field1, s->sname1.field1, \
433 fname); \
434 ret = false; \
436 }} while (0)
438 /* check that all the results are as expected */
439 CHECK_VAL("SEARCH", search, attrib, all_info, all_info, attrib&0xFFF);
440 CHECK_VAL("STANDARD", standard, attrib, all_info, all_info, attrib&0xFFF);
441 CHECK_VAL("EA_SIZE", ea_size, attrib, all_info, all_info, attrib&0xFFF);
442 CHECK_VAL("DIRECTORY_INFO", directory_info, attrib, all_info, all_info, attrib);
443 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, attrib, all_info, all_info, attrib);
444 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, attrib, all_info, all_info, attrib);
445 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, attrib, all_info, all_info, attrib);
446 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, attrib, all_info, all_info, attrib);
448 CHECK_TIME("SEARCH", search, write_time, all_info, all_info, write_time);
449 CHECK_TIME("STANDARD", standard, write_time, all_info, all_info, write_time);
450 CHECK_TIME("EA_SIZE", ea_size, write_time, all_info, all_info, write_time);
451 CHECK_TIME("STANDARD", standard, create_time, all_info, all_info, create_time);
452 CHECK_TIME("EA_SIZE", ea_size, create_time, all_info, all_info, create_time);
453 CHECK_TIME("STANDARD", standard, access_time, all_info, all_info, access_time);
454 CHECK_TIME("EA_SIZE", ea_size, access_time, all_info, all_info, access_time);
456 CHECK_NTTIME("DIRECTORY_INFO", directory_info, write_time, all_info, all_info, write_time);
457 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, write_time, all_info, all_info, write_time);
458 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, write_time, all_info, all_info, write_time);
459 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, write_time, all_info, all_info, write_time);
460 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, write_time, all_info, all_info, write_time);
462 CHECK_NTTIME("DIRECTORY_INFO", directory_info, create_time, all_info, all_info, create_time);
463 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
464 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
465 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, create_time, all_info, all_info, create_time);
466 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, create_time, all_info, all_info, create_time);
468 CHECK_NTTIME("DIRECTORY_INFO", directory_info, access_time, all_info, all_info, access_time);
469 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, access_time, all_info, all_info, access_time);
470 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, access_time, all_info, all_info, access_time);
471 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, access_time, all_info, all_info, access_time);
472 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, access_time, all_info, all_info, access_time);
474 CHECK_NTTIME("DIRECTORY_INFO", directory_info, change_time, all_info, all_info, change_time);
475 CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, change_time, all_info, all_info, change_time);
476 CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, change_time, all_info, all_info, change_time);
477 CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, change_time, all_info, all_info, change_time);
478 CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, change_time, all_info, all_info, change_time);
480 CHECK_VAL("SEARCH", search, size, all_info, all_info, size);
481 CHECK_VAL("STANDARD", standard, size, all_info, all_info, size);
482 CHECK_VAL("EA_SIZE", ea_size, size, all_info, all_info, size);
483 CHECK_VAL("DIRECTORY_INFO", directory_info, size, all_info, all_info, size);
484 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, size, all_info, all_info, size);
485 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, size, all_info, all_info, size);
486 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, size, all_info, all_info, size);
487 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, size, all_info, all_info, size);
488 CHECK_VAL("UNIX_INFO", unix_info, size, all_info, all_info, size);
490 CHECK_VAL("STANDARD", standard, alloc_size, all_info, all_info, alloc_size);
491 CHECK_VAL("EA_SIZE", ea_size, alloc_size, all_info, all_info, alloc_size);
492 CHECK_VAL("DIRECTORY_INFO", directory_info, alloc_size, all_info, all_info, alloc_size);
493 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, alloc_size, all_info, all_info, alloc_size);
494 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, alloc_size, all_info, all_info, alloc_size);
495 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, alloc_size, all_info, all_info, alloc_size);
496 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, alloc_size, all_info, all_info, alloc_size);
497 CHECK_VAL("UNIX_INFO", unix_info, alloc_size, all_info, all_info, alloc_size);
499 CHECK_VAL("EA_SIZE", ea_size, ea_size, all_info, all_info, ea_size);
500 CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, ea_size, all_info, all_info, ea_size);
501 CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, ea_size, all_info, all_info, ea_size);
502 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, ea_size, all_info, all_info, ea_size);
503 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, ea_size, all_info, all_info, ea_size);
505 if (alt_info_supported) {
506 CHECK_STR("SEARCH", search, name, alt_info, alt_name_info,
507 fname);
508 CHECK_WSTR("BOTH_DIRECTORY_INFO", both_directory_info,
509 short_name, alt_info, alt_name_info, fname, STR_UNICODE);
512 CHECK_NAME("STANDARD", standard, name, fname+1, 0);
513 CHECK_NAME("EA_SIZE", ea_size, name, fname+1, 0);
514 CHECK_NAME("DIRECTORY_INFO", directory_info, name, fname+1, STR_TERMINATE_ASCII);
515 CHECK_NAME("FULL_DIRECTORY_INFO", full_directory_info, name, fname+1, STR_TERMINATE_ASCII);
517 if (name_info_supported) {
518 CHECK_NAME("NAME_INFO", name_info, name, fname+1,
519 STR_TERMINATE_ASCII);
522 CHECK_NAME("BOTH_DIRECTORY_INFO", both_directory_info, name, fname+1, STR_TERMINATE_ASCII);
523 CHECK_NAME("ID_FULL_DIRECTORY_INFO", id_full_directory_info, name, fname+1, STR_TERMINATE_ASCII);
524 CHECK_NAME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, name, fname+1, STR_TERMINATE_ASCII);
525 CHECK_UNIX_NAME("UNIX_INFO", unix_info, name, fname+1, STR_TERMINATE_ASCII);
527 if (internal_info_supported) {
528 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,
529 file_id, internal_info, internal_information, file_id);
530 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,
531 file_id, internal_info, internal_information, file_id);
534 done:
535 smb_raw_exit(cli->session);
536 smbcli_unlink(cli->tree, fname);
538 return ret;
542 struct multiple_result {
543 TALLOC_CTX *tctx;
544 int count;
545 union smb_search_data *list;
549 callback function for multiple_search
551 static bool multiple_search_callback(void *private_data, const union smb_search_data *file)
553 struct multiple_result *data = (struct multiple_result *)private_data;
556 data->count++;
557 data->list = talloc_realloc(data->tctx,
558 data->list,
559 union smb_search_data,
560 data->count);
562 data->list[data->count-1] = *file;
564 return true;
567 enum continue_type {CONT_FLAGS, CONT_NAME, CONT_RESUME_KEY};
570 do a single file (non-wildcard) search
572 static NTSTATUS multiple_search(struct smbcli_state *cli,
573 TALLOC_CTX *tctx,
574 const char *pattern,
575 enum smb_search_data_level data_level,
576 enum continue_type cont_type,
577 void *data)
579 union smb_search_first io;
580 union smb_search_next io2;
581 NTSTATUS status;
582 const int per_search = 100;
583 struct multiple_result *result = (struct multiple_result *)data;
585 if (data_level == RAW_SEARCH_DATA_SEARCH) {
586 io.search_first.level = RAW_SEARCH_SEARCH;
587 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
588 io.search_first.in.max_count = per_search;
589 io.search_first.in.search_attrib = 0;
590 io.search_first.in.pattern = pattern;
591 } else {
592 io.t2ffirst.level = RAW_SEARCH_TRANS2;
593 io.t2ffirst.data_level = data_level;
594 io.t2ffirst.in.search_attrib = 0;
595 io.t2ffirst.in.max_count = per_search;
596 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
597 io.t2ffirst.in.storage_type = 0;
598 io.t2ffirst.in.pattern = pattern;
599 if (cont_type == CONT_RESUME_KEY) {
600 io.t2ffirst.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
601 FLAG_TRANS2_FIND_BACKUP_INTENT;
605 status = smb_raw_search_first(cli->tree, tctx,
606 &io, data, multiple_search_callback);
609 while (NT_STATUS_IS_OK(status)) {
610 if (data_level == RAW_SEARCH_DATA_SEARCH) {
611 io2.search_next.level = RAW_SEARCH_SEARCH;
612 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
613 io2.search_next.in.max_count = per_search;
614 io2.search_next.in.search_attrib = 0;
615 io2.search_next.in.id = result->list[result->count-1].search.id;
616 } else {
617 io2.t2fnext.level = RAW_SEARCH_TRANS2;
618 io2.t2fnext.data_level = data_level;
619 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
620 io2.t2fnext.in.max_count = per_search;
621 io2.t2fnext.in.resume_key = 0;
622 io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
623 io2.t2fnext.in.last_name = "";
624 switch (cont_type) {
625 case CONT_RESUME_KEY:
626 io2.t2fnext.in.resume_key = extract_resume_key(&result->list[result->count-1],
627 io2.t2fnext.level, io2.t2fnext.data_level);
628 if (io2.t2fnext.in.resume_key == 0) {
629 printf("Server does not support resume by key for level %s\n",
630 level_name(io2.t2fnext.level, io2.t2fnext.data_level));
631 return NT_STATUS_NOT_SUPPORTED;
633 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
634 FLAG_TRANS2_FIND_BACKUP_INTENT;
635 break;
636 case CONT_NAME:
637 io2.t2fnext.in.last_name = extract_name(&result->list[result->count-1],
638 io2.t2fnext.level, io2.t2fnext.data_level);
639 break;
640 case CONT_FLAGS:
641 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_CONTINUE;
642 break;
646 status = smb_raw_search_next(cli->tree, tctx,
647 &io2, data, multiple_search_callback);
648 if (!NT_STATUS_IS_OK(status)) {
649 break;
651 if (data_level == RAW_SEARCH_DATA_SEARCH) {
652 if (io2.search_next.out.count == 0) {
653 break;
655 } else if (io2.t2fnext.out.count == 0 ||
656 io2.t2fnext.out.end_of_search) {
657 break;
661 return status;
664 #define CHECK_STATUS(status, correct) torture_assert_ntstatus_equal(tctx, status, correct, "incorrect status")
666 #define CHECK_VALUE(v, correct) torture_assert_int_equal(tctx, (v), (correct), "incorrect value");
668 #define CHECK_STRING(v, correct) torture_assert_casestr_equal(tctx, v, correct, "incorrect value");
671 static enum smb_search_data_level compare_data_level;
673 static int search_compare(union smb_search_data *d1, union smb_search_data *d2)
675 const char *s1, *s2;
676 enum smb_search_level level;
678 if (compare_data_level == RAW_SEARCH_DATA_SEARCH) {
679 level = RAW_SEARCH_SEARCH;
680 } else {
681 level = RAW_SEARCH_TRANS2;
684 s1 = extract_name(d1, level, compare_data_level);
685 s2 = extract_name(d2, level, compare_data_level);
686 return strcmp_safe(s1, s2);
692 basic testing of search calls using many files
694 static bool test_many_files(struct torture_context *tctx,
695 struct smbcli_state *cli)
697 const int num_files = 700;
698 int i, fnum, t;
699 char *fname;
700 bool ret = true;
701 NTSTATUS status;
702 struct multiple_result result;
703 struct {
704 const char *name;
705 const char *cont_name;
706 enum smb_search_data_level data_level;
707 enum continue_type cont_type;
708 } search_types[] = {
709 {"SEARCH", "ID", RAW_SEARCH_DATA_SEARCH, CONT_RESUME_KEY},
710 {"BOTH_DIRECTORY_INFO", "NAME", RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_NAME},
711 {"BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_FLAGS},
712 {"BOTH_DIRECTORY_INFO", "KEY", RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY},
713 {"STANDARD", "FLAGS", RAW_SEARCH_DATA_STANDARD, CONT_FLAGS},
714 {"STANDARD", "KEY", RAW_SEARCH_DATA_STANDARD, CONT_RESUME_KEY},
715 {"STANDARD", "NAME", RAW_SEARCH_DATA_STANDARD, CONT_NAME},
716 {"EA_SIZE", "FLAGS", RAW_SEARCH_DATA_EA_SIZE, CONT_FLAGS},
717 {"EA_SIZE", "KEY", RAW_SEARCH_DATA_EA_SIZE, CONT_RESUME_KEY},
718 {"EA_SIZE", "NAME", RAW_SEARCH_DATA_EA_SIZE, CONT_NAME},
719 {"DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_DIRECTORY_INFO, CONT_FLAGS},
720 {"DIRECTORY_INFO", "KEY", RAW_SEARCH_DATA_DIRECTORY_INFO, CONT_RESUME_KEY},
721 {"DIRECTORY_INFO", "NAME", RAW_SEARCH_DATA_DIRECTORY_INFO, CONT_NAME},
722 {"FULL_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_FULL_DIRECTORY_INFO, CONT_FLAGS},
723 {"FULL_DIRECTORY_INFO", "KEY", RAW_SEARCH_DATA_FULL_DIRECTORY_INFO, CONT_RESUME_KEY},
724 {"FULL_DIRECTORY_INFO", "NAME", RAW_SEARCH_DATA_FULL_DIRECTORY_INFO, CONT_NAME},
725 {"ID_FULL_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_FLAGS},
726 {"ID_FULL_DIRECTORY_INFO", "KEY", RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_RESUME_KEY},
727 {"ID_FULL_DIRECTORY_INFO", "NAME", RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_NAME},
728 {"ID_BOTH_DIRECTORY_INFO", "NAME", RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_NAME},
729 {"ID_BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_FLAGS},
730 {"ID_BOTH_DIRECTORY_INFO", "KEY", RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY}
733 if (!torture_setup_dir(cli, BASEDIR)) {
734 return false;
737 torture_comment(tctx, "Testing with %d files\n", num_files);
739 for (i=0;i<num_files;i++) {
740 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
741 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
742 torture_assert(tctx, fnum != -1, "Failed to create");
743 talloc_free(fname);
744 smbcli_close(cli->tree, fnum);
748 for (t=0;t<ARRAY_SIZE(search_types);t++) {
749 ZERO_STRUCT(result);
751 if ((search_types[t].cont_type == CONT_RESUME_KEY) &&
752 (search_types[t].data_level != RAW_SEARCH_DATA_SEARCH) &&
753 !torture_setting_bool(tctx, "resume_key_support", true)) {
754 torture_comment(tctx,
755 "SKIP: Continue %s via %s\n",
756 search_types[t].name, search_types[t].cont_name);
757 continue;
760 result.tctx = talloc_new(tctx);
762 torture_comment(tctx,
763 "Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
765 status = multiple_search(cli, tctx, BASEDIR "\\*.*",
766 search_types[t].data_level,
767 search_types[t].cont_type,
768 &result);
769 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
770 torture_warning(tctx, "search level %s not supported "
771 "by server",
772 search_types[t].name);
773 continue;
775 torture_assert_ntstatus_ok(tctx, status, "search failed");
776 CHECK_VALUE(result.count, num_files);
778 compare_data_level = search_types[t].data_level;
780 TYPESAFE_QSORT(result.list, result.count, search_compare);
782 for (i=0;i<result.count;i++) {
783 const char *s;
784 enum smb_search_level level;
785 if (compare_data_level == RAW_SEARCH_DATA_SEARCH) {
786 level = RAW_SEARCH_SEARCH;
787 } else {
788 level = RAW_SEARCH_TRANS2;
790 s = extract_name(&result.list[i], level, compare_data_level);
791 fname = talloc_asprintf(cli, "t%03d-%d.txt", i, i);
792 torture_assert_str_equal(tctx, fname, s, "Incorrect name");
793 talloc_free(fname);
795 talloc_free(result.tctx);
798 smb_raw_exit(cli->session);
799 smbcli_deltree(cli->tree, BASEDIR);
801 return ret;
805 check a individual file result
807 static bool check_result(struct multiple_result *result, const char *name, bool exist, uint32_t attrib)
809 int i;
810 for (i=0;i<result->count;i++) {
811 if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
813 if (i == result->count) {
814 if (exist) {
815 printf("failed: '%s' should exist with attribute %s\n",
816 name, attrib_string(result->list, attrib));
817 return false;
819 return true;
822 if (!exist) {
823 printf("failed: '%s' should NOT exist (has attribute %s)\n",
824 name, attrib_string(result->list, result->list[i].both_directory_info.attrib));
825 return false;
828 if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
829 printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
830 name,
831 attrib, result->list[i].both_directory_info.attrib);
832 return false;
834 return true;
838 test what happens when the directory is modified during a search
840 static bool test_modify_search(struct torture_context *tctx,
841 struct smbcli_state *cli)
843 const int num_files = 20;
844 int i, fnum;
845 char *fname;
846 bool ret = true;
847 NTSTATUS status;
848 struct multiple_result result;
849 union smb_search_first io;
850 union smb_search_next io2;
851 union smb_setfileinfo sfinfo;
853 if (!torture_setup_dir(cli, BASEDIR)) {
854 return false;
857 printf("Creating %d files\n", num_files);
859 for (i=num_files-1;i>=0;i--) {
860 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
861 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
862 if (fnum == -1) {
863 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
864 ret = false;
865 goto done;
867 talloc_free(fname);
868 smbcli_close(cli->tree, fnum);
871 printf("pulling the first file\n");
872 ZERO_STRUCT(result);
873 result.tctx = talloc_new(tctx);
875 io.t2ffirst.level = RAW_SEARCH_TRANS2;
876 io.t2ffirst.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
877 io.t2ffirst.in.search_attrib = 0;
878 io.t2ffirst.in.max_count = 0;
879 io.t2ffirst.in.flags = 0;
880 io.t2ffirst.in.storage_type = 0;
881 io.t2ffirst.in.pattern = BASEDIR "\\*.*";
883 status = smb_raw_search_first(cli->tree, tctx,
884 &io, &result, multiple_search_callback);
885 CHECK_STATUS(status, NT_STATUS_OK);
886 CHECK_VALUE(result.count, 1);
888 printf("pulling the second file\n");
889 io2.t2fnext.level = RAW_SEARCH_TRANS2;
890 io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
891 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
892 io2.t2fnext.in.max_count = 1;
893 io2.t2fnext.in.resume_key = 0;
894 io2.t2fnext.in.flags = 0;
895 io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
897 status = smb_raw_search_next(cli->tree, tctx,
898 &io2, &result, multiple_search_callback);
899 CHECK_STATUS(status, NT_STATUS_OK);
900 CHECK_VALUE(result.count, 2);
902 result.count = 0;
904 printf("Changing attributes and deleting\n");
905 smbcli_open(cli->tree, BASEDIR "\\T003-03.txt.2", O_CREAT|O_RDWR, DENY_NONE);
906 smbcli_open(cli->tree, BASEDIR "\\T013-13.txt.2", O_CREAT|O_RDWR, DENY_NONE);
907 fnum = create_complex_file(cli, tctx, BASEDIR "\\T013-13.txt.3");
908 smbcli_unlink(cli->tree, BASEDIR "\\T014-14.txt");
909 torture_set_file_attribute(cli->tree, BASEDIR "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
910 torture_set_file_attribute(cli->tree, BASEDIR "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
911 torture_set_file_attribute(cli->tree, BASEDIR "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);
912 torture_set_file_attribute(cli->tree, BASEDIR "\\T018-18.txt", 0);
913 sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
914 sfinfo.generic.in.file.fnum = fnum;
915 sfinfo.disposition_info.in.delete_on_close = 1;
916 status = smb_raw_setfileinfo(cli->tree, &sfinfo);
917 CHECK_STATUS(status, NT_STATUS_OK);
919 io2.t2fnext.level = RAW_SEARCH_TRANS2;
920 io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
921 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
922 io2.t2fnext.in.max_count = num_files + 3;
923 io2.t2fnext.in.resume_key = 0;
924 io2.t2fnext.in.flags = 0;
925 io2.t2fnext.in.last_name = ".";
927 status = smb_raw_search_next(cli->tree, tctx,
928 &io2, &result, multiple_search_callback);
929 CHECK_STATUS(status, NT_STATUS_OK);
930 CHECK_VALUE(result.count, 20);
932 ret &= check_result(&result, "t009-9.txt", true, FILE_ATTRIBUTE_ARCHIVE);
933 ret &= check_result(&result, "t014-14.txt", false, 0);
934 ret &= check_result(&result, "t015-15.txt", false, 0);
935 ret &= check_result(&result, "t016-16.txt", true, FILE_ATTRIBUTE_NORMAL);
936 ret &= check_result(&result, "t017-17.txt", false, 0);
937 ret &= check_result(&result, "t018-18.txt", true, FILE_ATTRIBUTE_ARCHIVE);
938 ret &= check_result(&result, "t019-19.txt", true, FILE_ATTRIBUTE_ARCHIVE);
939 ret &= check_result(&result, "T013-13.txt.2", true, FILE_ATTRIBUTE_ARCHIVE);
940 ret &= check_result(&result, "T003-3.txt.2", false, 0);
941 ret &= check_result(&result, "T013-13.txt.3", true, FILE_ATTRIBUTE_ARCHIVE);
943 if (!ret) {
944 for (i=0;i<result.count;i++) {
945 printf("%s %s (0x%x)\n",
946 result.list[i].both_directory_info.name.s,
947 attrib_string(tctx, result.list[i].both_directory_info.attrib),
948 result.list[i].both_directory_info.attrib);
952 done:
953 smb_raw_exit(cli->session);
954 smbcli_deltree(cli->tree, BASEDIR);
956 return ret;
961 testing if directories always come back sorted
963 static bool test_sorted(struct torture_context *tctx, struct smbcli_state *cli)
965 const int num_files = 700;
966 int i, fnum;
967 char *fname;
968 bool ret = true;
969 NTSTATUS status;
970 struct multiple_result result;
972 if (!torture_setup_dir(cli, BASEDIR)) {
973 return false;
976 printf("Creating %d files\n", num_files);
978 for (i=0;i<num_files;i++) {
979 fname = talloc_asprintf(cli, BASEDIR "\\%s.txt", generate_random_str_list(tctx, 10, "abcdefgh"));
980 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
981 if (fnum == -1) {
982 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
983 ret = false;
984 goto done;
986 talloc_free(fname);
987 smbcli_close(cli->tree, fnum);
991 ZERO_STRUCT(result);
992 result.tctx = tctx;
994 status = multiple_search(cli, tctx, BASEDIR "\\*.*",
995 RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
996 CONT_NAME, &result);
997 CHECK_STATUS(status, NT_STATUS_OK);
998 CHECK_VALUE(result.count, num_files);
1000 for (i=0;i<num_files-1;i++) {
1001 const char *name1, *name2;
1002 name1 = result.list[i].both_directory_info.name.s;
1003 name2 = result.list[i+1].both_directory_info.name.s;
1004 if (strcasecmp_m(name1, name2) > 0) {
1005 printf("non-alphabetical order at entry %d '%s' '%s'\n",
1006 i, name1, name2);
1007 printf("Server does not produce sorted directory listings (not an error)\n");
1008 goto done;
1012 talloc_free(result.list);
1014 done:
1015 smb_raw_exit(cli->session);
1016 smbcli_deltree(cli->tree, BASEDIR);
1018 return ret;
1024 basic testing of many old style search calls using separate dirs
1026 static bool test_many_dirs(struct torture_context *tctx,
1027 struct smbcli_state *cli)
1029 const int num_dirs = 20;
1030 int i, fnum, n;
1031 char *fname, *dname;
1032 bool ret = true;
1033 NTSTATUS status;
1034 union smb_search_data *file, *file2, *file3;
1036 if (!torture_setting_bool(tctx, "raw_search_search", true)) {
1037 torture_comment(tctx, "Skipping these tests as the server "
1038 "doesn't support old style search calls\n");
1039 return true;
1041 if (!torture_setup_dir(cli, BASEDIR)) {
1042 return false;
1045 printf("Creating %d dirs\n", num_dirs);
1047 for (i=0;i<num_dirs;i++) {
1048 dname = talloc_asprintf(cli, BASEDIR "\\d%d", i);
1049 status = smbcli_mkdir(cli->tree, dname);
1050 if (!NT_STATUS_IS_OK(status)) {
1051 printf("(%s) Failed to create %s - %s\n",
1052 __location__, dname, nt_errstr(status));
1053 ret = false;
1054 goto done;
1057 for (n=0;n<3;n++) {
1058 fname = talloc_asprintf(cli, BASEDIR "\\d%d\\f%d-%d.txt", i, i, n);
1059 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1060 if (fnum == -1) {
1061 printf("(%s) Failed to create %s - %s\n",
1062 __location__, fname, smbcli_errstr(cli->tree));
1063 ret = false;
1064 goto done;
1066 talloc_free(fname);
1067 smbcli_close(cli->tree, fnum);
1070 talloc_free(dname);
1073 file = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1074 file2 = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1075 file3 = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1077 printf("Search first on %d dirs\n", num_dirs);
1079 for (i=0;i<num_dirs;i++) {
1080 union smb_search_first io;
1081 io.search_first.level = RAW_SEARCH_SEARCH;
1082 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
1083 io.search_first.in.max_count = 1;
1084 io.search_first.in.search_attrib = 0;
1085 io.search_first.in.pattern = talloc_asprintf(tctx, BASEDIR "\\d%d\\*.txt", i);
1086 fname = talloc_asprintf(tctx, "f%d-", i);
1088 io.search_first.out.count = 0;
1090 status = smb_raw_search_first(cli->tree, tctx,
1091 &io, (void *)&file[i], single_search_callback);
1092 if (io.search_first.out.count != 1) {
1093 printf("(%s) search first gave %d entries for dir %d - %s\n",
1094 __location__, io.search_first.out.count, i, nt_errstr(status));
1095 ret = false;
1096 goto done;
1098 CHECK_STATUS(status, NT_STATUS_OK);
1099 if (strncasecmp(file[i].search.name, fname, strlen(fname)) != 0) {
1100 printf("(%s) incorrect name '%s' expected '%s'[12].txt\n",
1101 __location__, file[i].search.name, fname);
1102 ret = false;
1103 goto done;
1106 talloc_free(fname);
1109 printf("Search next on %d dirs\n", num_dirs);
1111 for (i=0;i<num_dirs;i++) {
1112 union smb_search_next io2;
1114 io2.search_next.level = RAW_SEARCH_SEARCH;
1115 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
1116 io2.search_next.in.max_count = 1;
1117 io2.search_next.in.search_attrib = 0;
1118 io2.search_next.in.id = file[i].search.id;
1119 fname = talloc_asprintf(tctx, "f%d-", i);
1121 io2.search_next.out.count = 0;
1123 status = smb_raw_search_next(cli->tree, tctx,
1124 &io2, (void *)&file2[i], single_search_callback);
1125 if (io2.search_next.out.count != 1) {
1126 printf("(%s) search next gave %d entries for dir %d - %s\n",
1127 __location__, io2.search_next.out.count, i, nt_errstr(status));
1128 ret = false;
1129 goto done;
1131 CHECK_STATUS(status, NT_STATUS_OK);
1132 if (strncasecmp(file2[i].search.name, fname, strlen(fname)) != 0) {
1133 printf("(%s) incorrect name '%s' expected '%s'[12].txt\n",
1134 __location__, file2[i].search.name, fname);
1135 ret = false;
1136 goto done;
1139 talloc_free(fname);
1143 printf("Search next (rewind) on %d dirs\n", num_dirs);
1145 for (i=0;i<num_dirs;i++) {
1146 union smb_search_next io2;
1148 io2.search_next.level = RAW_SEARCH_SEARCH;
1149 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
1150 io2.search_next.in.max_count = 1;
1151 io2.search_next.in.search_attrib = 0;
1152 io2.search_next.in.id = file[i].search.id;
1153 fname = talloc_asprintf(tctx, "f%d-", i);
1154 io2.search_next.out.count = 0;
1156 status = smb_raw_search_next(cli->tree, tctx,
1157 &io2, (void *)&file3[i], single_search_callback);
1158 if (io2.search_next.out.count != 1) {
1159 printf("(%s) search next gave %d entries for dir %d - %s\n",
1160 __location__, io2.search_next.out.count, i, nt_errstr(status));
1161 ret = false;
1162 goto done;
1164 CHECK_STATUS(status, NT_STATUS_OK);
1166 if (strncasecmp(file3[i].search.name, file2[i].search.name, 3) != 0) {
1167 printf("(%s) incorrect name '%s' on rewind at dir %d\n",
1168 __location__, file2[i].search.name, i);
1169 ret = false;
1170 goto done;
1173 if (torture_setting_bool(tctx, "rewind_support", true) &&
1174 strcmp(file3[i].search.name, file2[i].search.name) != 0) {
1175 printf("(%s) server did not rewind - got '%s' expected '%s'\n",
1176 __location__, file3[i].search.name, file2[i].search.name);
1177 ret = false;
1178 goto done;
1181 talloc_free(fname);
1185 done:
1186 smb_raw_exit(cli->session);
1187 smbcli_deltree(cli->tree, BASEDIR);
1189 return ret;
1194 testing of OS/2 style delete
1196 static bool test_os2_delete(struct torture_context *tctx,
1197 struct smbcli_state *cli)
1199 const int num_files = 700;
1200 const int delete_count = 4;
1201 int total_deleted = 0;
1202 int i, fnum;
1203 char *fname;
1204 bool ret = true;
1205 NTSTATUS status;
1206 union smb_search_first io;
1207 union smb_search_next io2;
1208 struct multiple_result result;
1210 if (!torture_setting_bool(tctx, "search_ea_size", true)){
1211 torture_comment(tctx,
1212 "Server does not support RAW_SEARCH_EA_SIZE "
1213 "level. Skipping this test\n");
1214 return true;
1217 if (!torture_setup_dir(cli, BASEDIR)) {
1218 return false;
1221 printf("Testing OS/2 style delete on %d files\n", num_files);
1223 for (i=0;i<num_files;i++) {
1224 fname = talloc_asprintf(cli, BASEDIR "\\file%u.txt", i);
1225 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1226 if (fnum == -1) {
1227 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1228 ret = false;
1229 goto done;
1231 talloc_free(fname);
1232 smbcli_close(cli->tree, fnum);
1236 ZERO_STRUCT(result);
1237 result.tctx = tctx;
1239 io.t2ffirst.level = RAW_SEARCH_TRANS2;
1240 io.t2ffirst.data_level = RAW_SEARCH_DATA_EA_SIZE;
1241 io.t2ffirst.in.search_attrib = 0;
1242 io.t2ffirst.in.max_count = 100;
1243 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1244 io.t2ffirst.in.storage_type = 0;
1245 io.t2ffirst.in.pattern = BASEDIR "\\*";
1247 status = smb_raw_search_first(cli->tree, tctx,
1248 &io, &result, multiple_search_callback);
1249 CHECK_STATUS(status, NT_STATUS_OK);
1251 for (i=0;i<MIN(result.count, delete_count);i++) {
1252 fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
1253 status = smbcli_unlink(cli->tree, fname);
1254 CHECK_STATUS(status, NT_STATUS_OK);
1255 total_deleted++;
1256 talloc_free(fname);
1259 io2.t2fnext.level = RAW_SEARCH_TRANS2;
1260 io2.t2fnext.data_level = RAW_SEARCH_DATA_EA_SIZE;
1261 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1262 io2.t2fnext.in.max_count = 100;
1263 io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1264 io2.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1265 io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1267 do {
1268 ZERO_STRUCT(result);
1269 result.tctx = tctx;
1271 status = smb_raw_search_next(cli->tree, tctx,
1272 &io2, &result, multiple_search_callback);
1273 if (!NT_STATUS_IS_OK(status)) {
1274 break;
1277 for (i=0;i<MIN(result.count, delete_count);i++) {
1278 fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
1279 status = smbcli_unlink(cli->tree, fname);
1280 CHECK_STATUS(status, NT_STATUS_OK);
1281 total_deleted++;
1282 talloc_free(fname);
1285 if (i>0) {
1286 io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1287 io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1289 } while (NT_STATUS_IS_OK(status) && result.count != 0);
1291 CHECK_STATUS(status, NT_STATUS_OK);
1293 if (total_deleted != num_files) {
1294 printf("error: deleted %d - expected to delete %d\n",
1295 total_deleted, num_files);
1296 ret = false;
1299 done:
1300 smb_raw_exit(cli->session);
1301 smbcli_deltree(cli->tree, BASEDIR);
1303 return ret;
1307 static int ealist_cmp(union smb_search_data *r1, union smb_search_data *r2)
1309 return strcmp(r1->ea_list.name.s, r2->ea_list.name.s);
1313 testing of the rather strange ea_list level
1315 static bool test_ea_list(struct torture_context *tctx,
1316 struct smbcli_state *cli)
1318 int fnum;
1319 bool ret = true;
1320 NTSTATUS status;
1321 union smb_search_first io;
1322 union smb_search_next nxt;
1323 struct multiple_result result;
1324 union smb_setfileinfo setfile;
1326 if (!torture_setup_dir(cli, BASEDIR)) {
1327 return false;
1330 printf("Testing RAW_SEARCH_EA_LIST level\n");
1332 if (!torture_setting_bool(tctx, "search_ea_support", true) ||
1333 !torture_setting_bool(tctx, "ea_support", true)) {
1334 printf("..skipped per target configuration.\n");
1335 return true;
1338 fnum = smbcli_open(cli->tree, BASEDIR "\\file1.txt", O_CREAT|O_RDWR, DENY_NONE);
1339 smbcli_close(cli->tree, fnum);
1341 fnum = smbcli_open(cli->tree, BASEDIR "\\file2.txt", O_CREAT|O_RDWR, DENY_NONE);
1342 smbcli_close(cli->tree, fnum);
1344 fnum = smbcli_open(cli->tree, BASEDIR "\\file3.txt", O_CREAT|O_RDWR, DENY_NONE);
1345 smbcli_close(cli->tree, fnum);
1347 setfile.generic.level = RAW_SFILEINFO_EA_SET;
1348 setfile.generic.in.file.path = BASEDIR "\\file2.txt";
1349 setfile.ea_set.in.num_eas = 2;
1350 setfile.ea_set.in.eas = talloc_array(tctx, struct ea_struct, 2);
1351 setfile.ea_set.in.eas[0].flags = 0;
1352 setfile.ea_set.in.eas[0].name.s = "EA ONE";
1353 setfile.ea_set.in.eas[0].value = data_blob_string_const("VALUE 1");
1354 setfile.ea_set.in.eas[1].flags = 0;
1355 setfile.ea_set.in.eas[1].name.s = "SECOND EA";
1356 setfile.ea_set.in.eas[1].value = data_blob_string_const("Value Two");
1358 status = smb_raw_setpathinfo(cli->tree, &setfile);
1359 CHECK_STATUS(status, NT_STATUS_OK);
1361 setfile.generic.in.file.path = BASEDIR "\\file3.txt";
1362 status = smb_raw_setpathinfo(cli->tree, &setfile);
1363 CHECK_STATUS(status, NT_STATUS_OK);
1365 ZERO_STRUCT(result);
1366 result.tctx = tctx;
1368 io.t2ffirst.level = RAW_SEARCH_TRANS2;
1369 io.t2ffirst.data_level = RAW_SEARCH_DATA_EA_LIST;
1370 io.t2ffirst.in.search_attrib = 0;
1371 io.t2ffirst.in.max_count = 2;
1372 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1373 io.t2ffirst.in.storage_type = 0;
1374 io.t2ffirst.in.pattern = BASEDIR "\\*";
1375 io.t2ffirst.in.num_names = 2;
1376 io.t2ffirst.in.ea_names = talloc_array(tctx, struct ea_name, 2);
1377 io.t2ffirst.in.ea_names[0].name.s = "SECOND EA";
1378 io.t2ffirst.in.ea_names[1].name.s = "THIRD EA";
1380 status = smb_raw_search_first(cli->tree, tctx,
1381 &io, &result, multiple_search_callback);
1382 CHECK_STATUS(status, NT_STATUS_OK);
1383 CHECK_VALUE(result.count, 2);
1385 nxt.t2fnext.level = RAW_SEARCH_TRANS2;
1386 nxt.t2fnext.data_level = RAW_SEARCH_DATA_EA_LIST;
1387 nxt.t2fnext.in.handle = io.t2ffirst.out.handle;
1388 nxt.t2fnext.in.max_count = 2;
1389 nxt.t2fnext.in.resume_key = result.list[1].ea_list.resume_key;
1390 nxt.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME | FLAG_TRANS2_FIND_CONTINUE;
1391 nxt.t2fnext.in.last_name = result.list[1].ea_list.name.s;
1392 nxt.t2fnext.in.num_names = 2;
1393 nxt.t2fnext.in.ea_names = talloc_array(tctx, struct ea_name, 2);
1394 nxt.t2fnext.in.ea_names[0].name.s = "SECOND EA";
1395 nxt.t2fnext.in.ea_names[1].name.s = "THIRD EA";
1397 status = smb_raw_search_next(cli->tree, tctx,
1398 &nxt, &result, multiple_search_callback);
1399 CHECK_STATUS(status, NT_STATUS_OK);
1401 /* we have to sort the result as different servers can return directories
1402 in different orders */
1403 TYPESAFE_QSORT(result.list, result.count, ealist_cmp);
1405 CHECK_VALUE(result.count, 3);
1406 CHECK_VALUE(result.list[0].ea_list.eas.num_eas, 2);
1407 CHECK_STRING(result.list[0].ea_list.name.s, "file1.txt");
1408 CHECK_STRING(result.list[0].ea_list.eas.eas[0].name.s, "SECOND EA");
1409 CHECK_VALUE(result.list[0].ea_list.eas.eas[0].value.length, 0);
1410 CHECK_STRING(result.list[0].ea_list.eas.eas[1].name.s, "THIRD EA");
1411 CHECK_VALUE(result.list[0].ea_list.eas.eas[1].value.length, 0);
1413 CHECK_STRING(result.list[1].ea_list.name.s, "file2.txt");
1414 CHECK_STRING(result.list[1].ea_list.eas.eas[0].name.s, "SECOND EA");
1415 CHECK_VALUE(result.list[1].ea_list.eas.eas[0].value.length, 9);
1416 CHECK_STRING((const char *)result.list[1].ea_list.eas.eas[0].value.data, "Value Two");
1417 CHECK_STRING(result.list[1].ea_list.eas.eas[1].name.s, "THIRD EA");
1418 CHECK_VALUE(result.list[1].ea_list.eas.eas[1].value.length, 0);
1420 CHECK_STRING(result.list[2].ea_list.name.s, "file3.txt");
1421 CHECK_STRING(result.list[2].ea_list.eas.eas[0].name.s, "SECOND EA");
1422 CHECK_VALUE(result.list[2].ea_list.eas.eas[0].value.length, 9);
1423 CHECK_STRING((const char *)result.list[2].ea_list.eas.eas[0].value.data, "Value Two");
1424 CHECK_STRING(result.list[2].ea_list.eas.eas[1].name.s, "THIRD EA");
1425 CHECK_VALUE(result.list[2].ea_list.eas.eas[1].value.length, 0);
1427 smb_raw_exit(cli->session);
1428 smbcli_deltree(cli->tree, BASEDIR);
1430 return ret;
1434 Test the behavior of max count parameter in TRANS2_FIND_FIRST2 and
1435 TRANS2_FIND_NEXT2 queries
1437 static bool test_max_count(struct torture_context *tctx,
1438 struct smbcli_state *cli)
1440 const int num_files = 2;
1441 int i, fnum;
1442 char *fname;
1443 bool ret = true;
1444 NTSTATUS status;
1445 struct multiple_result result;
1446 union smb_search_first io;
1447 union smb_search_next io2;
1449 if (!torture_setup_dir(cli, BASEDIR)) {
1450 return false;
1453 torture_comment(tctx, "Creating %d files\n", num_files);
1455 for (i=num_files-1;i>=0;i--) {
1456 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
1457 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1458 if (fnum == -1) {
1459 torture_comment(tctx,
1460 "Failed to create %s - %s\n",
1461 fname, smbcli_errstr(cli->tree));
1462 ret = false;
1463 goto done;
1465 talloc_free(fname);
1466 smbcli_close(cli->tree, fnum);
1469 torture_comment(tctx, "Set max_count parameter to 0. "
1470 "This should return 1 entry\n");
1471 ZERO_STRUCT(result);
1472 result.tctx = talloc_new(tctx);
1474 io.t2ffirst.level = RAW_SEARCH_TRANS2;
1475 io.t2ffirst.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1476 io.t2ffirst.in.search_attrib = 0;
1477 io.t2ffirst.in.max_count = 0;
1478 io.t2ffirst.in.flags = 0;
1479 io.t2ffirst.in.storage_type = 0;
1480 io.t2ffirst.in.pattern = BASEDIR "\\*.*";
1482 status = smb_raw_search_first(cli->tree, tctx,
1483 &io, &result, multiple_search_callback);
1484 CHECK_STATUS(status, NT_STATUS_OK);
1485 CHECK_VALUE(result.count, 1);
1487 torture_comment(tctx, "Set max_count to 1. This should also "
1488 "return 1 entry\n");
1489 io2.t2fnext.level = RAW_SEARCH_TRANS2;
1490 io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1491 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1492 io2.t2fnext.in.max_count = 1;
1493 io2.t2fnext.in.resume_key = 0;
1494 io2.t2fnext.in.flags = 0;
1495 io2.t2fnext.in.last_name =
1496 result.list[result.count-1].both_directory_info.name.s;
1498 status = smb_raw_search_next(cli->tree, tctx,
1499 &io2, &result, multiple_search_callback);
1500 CHECK_STATUS(status, NT_STATUS_OK);
1501 CHECK_VALUE(result.count, 2);
1502 done:
1503 smb_raw_exit(cli->session);
1504 smbcli_deltree(cli->tree, BASEDIR);
1506 return ret;
1510 basic testing of all RAW_SEARCH_* calls using a single file
1512 struct torture_suite *torture_raw_search(TALLOC_CTX *mem_ctx)
1514 struct torture_suite *suite = torture_suite_create(mem_ctx, "search");
1516 torture_suite_add_1smb_test(suite, "one file search", test_one_file);
1517 torture_suite_add_1smb_test(suite, "many files", test_many_files);
1518 torture_suite_add_1smb_test(suite, "sorted", test_sorted);
1519 torture_suite_add_1smb_test(suite, "modify search", test_modify_search);
1520 torture_suite_add_1smb_test(suite, "many dirs", test_many_dirs);
1521 torture_suite_add_1smb_test(suite, "os2 delete", test_os2_delete);
1522 torture_suite_add_1smb_test(suite, "ea list", test_ea_list);
1523 torture_suite_add_1smb_test(suite, "max count", test_max_count);
1525 return suite;