dsdb: Add more tests for DN+String and DN+Binary comparisons
[Samba.git] / source4 / torture / raw / search.c
blob0736c41d0cca38dc76d53c2e28928e3d63326c5d
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 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
735 torture_comment(tctx, "Testing with %d files\n", num_files);
737 for (i=0;i<num_files;i++) {
738 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
739 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
740 torture_assert(tctx, fnum != -1, "Failed to create");
741 talloc_free(fname);
742 smbcli_close(cli->tree, fnum);
746 for (t=0;t<ARRAY_SIZE(search_types);t++) {
747 ZERO_STRUCT(result);
749 if ((search_types[t].cont_type == CONT_RESUME_KEY) &&
750 (search_types[t].data_level != RAW_SEARCH_DATA_SEARCH) &&
751 !torture_setting_bool(tctx, "resume_key_support", true)) {
752 torture_comment(tctx,
753 "SKIP: Continue %s via %s\n",
754 search_types[t].name, search_types[t].cont_name);
755 continue;
758 result.tctx = talloc_new(tctx);
760 torture_comment(tctx,
761 "Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
763 status = multiple_search(cli, tctx, BASEDIR "\\*.*",
764 search_types[t].data_level,
765 search_types[t].cont_type,
766 &result);
767 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
768 torture_warning(tctx, "search level %s not supported "
769 "by server",
770 search_types[t].name);
771 continue;
773 torture_assert_ntstatus_ok(tctx, status, "search failed");
774 CHECK_VALUE(result.count, num_files);
776 compare_data_level = search_types[t].data_level;
778 TYPESAFE_QSORT(result.list, result.count, search_compare);
780 for (i=0;i<result.count;i++) {
781 const char *s;
782 enum smb_search_level level;
783 if (compare_data_level == RAW_SEARCH_DATA_SEARCH) {
784 level = RAW_SEARCH_SEARCH;
785 } else {
786 level = RAW_SEARCH_TRANS2;
788 s = extract_name(&result.list[i], level, compare_data_level);
789 fname = talloc_asprintf(cli, "t%03d-%d.txt", i, i);
790 torture_assert_str_equal(tctx, fname, s, "Incorrect name");
791 talloc_free(fname);
793 talloc_free(result.tctx);
796 smb_raw_exit(cli->session);
797 smbcli_deltree(cli->tree, BASEDIR);
799 return ret;
803 check a individual file result
805 static bool check_result(struct multiple_result *result, const char *name, bool exist, uint32_t attrib)
807 int i;
808 for (i=0;i<result->count;i++) {
809 if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
811 if (i == result->count) {
812 if (exist) {
813 printf("failed: '%s' should exist with attribute %s\n",
814 name, attrib_string(result->list, attrib));
815 return false;
817 return true;
820 if (!exist) {
821 printf("failed: '%s' should NOT exist (has attribute %s)\n",
822 name, attrib_string(result->list, result->list[i].both_directory_info.attrib));
823 return false;
826 if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
827 printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
828 name,
829 attrib, result->list[i].both_directory_info.attrib);
830 return false;
832 return true;
836 test what happens when the directory is modified during a search
838 static bool test_modify_search(struct torture_context *tctx,
839 struct smbcli_state *cli)
841 const int num_files = 20;
842 int i, fnum;
843 char *fname;
844 bool ret = true;
845 NTSTATUS status;
846 struct multiple_result result;
847 union smb_search_first io;
848 union smb_search_next io2;
849 union smb_setfileinfo sfinfo;
851 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
853 printf("Creating %d files\n", num_files);
855 for (i=num_files-1;i>=0;i--) {
856 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
857 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
858 if (fnum == -1) {
859 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
860 ret = false;
861 goto done;
863 talloc_free(fname);
864 smbcli_close(cli->tree, fnum);
867 printf("pulling the first file\n");
868 ZERO_STRUCT(result);
869 result.tctx = talloc_new(tctx);
871 io.t2ffirst.level = RAW_SEARCH_TRANS2;
872 io.t2ffirst.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
873 io.t2ffirst.in.search_attrib = 0;
874 io.t2ffirst.in.max_count = 0;
875 io.t2ffirst.in.flags = 0;
876 io.t2ffirst.in.storage_type = 0;
877 io.t2ffirst.in.pattern = BASEDIR "\\*.*";
879 status = smb_raw_search_first(cli->tree, tctx,
880 &io, &result, multiple_search_callback);
881 CHECK_STATUS(status, NT_STATUS_OK);
882 CHECK_VALUE(result.count, 1);
884 printf("pulling the second file\n");
885 io2.t2fnext.level = RAW_SEARCH_TRANS2;
886 io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
887 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
888 io2.t2fnext.in.max_count = 1;
889 io2.t2fnext.in.resume_key = 0;
890 io2.t2fnext.in.flags = 0;
891 io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
893 status = smb_raw_search_next(cli->tree, tctx,
894 &io2, &result, multiple_search_callback);
895 CHECK_STATUS(status, NT_STATUS_OK);
896 CHECK_VALUE(result.count, 2);
898 result.count = 0;
900 printf("Changing attributes and deleting\n");
901 smbcli_open(cli->tree, BASEDIR "\\T003-03.txt.2", O_CREAT|O_RDWR, DENY_NONE);
902 smbcli_open(cli->tree, BASEDIR "\\T013-13.txt.2", O_CREAT|O_RDWR, DENY_NONE);
903 fnum = create_complex_file(cli, tctx, BASEDIR "\\T013-13.txt.3");
904 smbcli_unlink(cli->tree, BASEDIR "\\T014-14.txt");
905 torture_set_file_attribute(cli->tree, BASEDIR "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
906 torture_set_file_attribute(cli->tree, BASEDIR "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
907 torture_set_file_attribute(cli->tree, BASEDIR "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);
908 torture_set_file_attribute(cli->tree, BASEDIR "\\T018-18.txt", 0);
909 sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
910 sfinfo.generic.in.file.fnum = fnum;
911 sfinfo.disposition_info.in.delete_on_close = 1;
912 status = smb_raw_setfileinfo(cli->tree, &sfinfo);
913 CHECK_STATUS(status, NT_STATUS_OK);
915 io2.t2fnext.level = RAW_SEARCH_TRANS2;
916 io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
917 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
918 io2.t2fnext.in.max_count = num_files + 3;
919 io2.t2fnext.in.resume_key = 0;
920 io2.t2fnext.in.flags = 0;
921 io2.t2fnext.in.last_name = ".";
923 status = smb_raw_search_next(cli->tree, tctx,
924 &io2, &result, multiple_search_callback);
925 CHECK_STATUS(status, NT_STATUS_OK);
926 CHECK_VALUE(result.count, 20);
928 ret &= check_result(&result, "t009-9.txt", true, FILE_ATTRIBUTE_ARCHIVE);
929 ret &= check_result(&result, "t014-14.txt", false, 0);
930 ret &= check_result(&result, "t015-15.txt", false, 0);
931 ret &= check_result(&result, "t016-16.txt", true, FILE_ATTRIBUTE_NORMAL);
932 ret &= check_result(&result, "t017-17.txt", false, 0);
933 ret &= check_result(&result, "t018-18.txt", true, FILE_ATTRIBUTE_ARCHIVE);
934 ret &= check_result(&result, "t019-19.txt", true, FILE_ATTRIBUTE_ARCHIVE);
935 ret &= check_result(&result, "T013-13.txt.2", true, FILE_ATTRIBUTE_ARCHIVE);
936 ret &= check_result(&result, "T003-3.txt.2", false, 0);
937 ret &= check_result(&result, "T013-13.txt.3", true, FILE_ATTRIBUTE_ARCHIVE);
939 if (!ret) {
940 for (i=0;i<result.count;i++) {
941 printf("%s %s (0x%x)\n",
942 result.list[i].both_directory_info.name.s,
943 attrib_string(tctx, result.list[i].both_directory_info.attrib),
944 result.list[i].both_directory_info.attrib);
948 done:
949 smb_raw_exit(cli->session);
950 smbcli_deltree(cli->tree, BASEDIR);
952 return ret;
957 testing if directories always come back sorted
959 static bool test_sorted(struct torture_context *tctx, struct smbcli_state *cli)
961 const int num_files = 700;
962 int i, fnum;
963 char *fname;
964 bool ret = true;
965 NTSTATUS status;
966 struct multiple_result result;
968 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
970 printf("Creating %d files\n", num_files);
972 for (i=0;i<num_files;i++) {
973 fname = talloc_asprintf(cli, BASEDIR "\\%s.txt", generate_random_str_list(tctx, 10, "abcdefgh"));
974 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
975 if (fnum == -1) {
976 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
977 ret = false;
978 goto done;
980 talloc_free(fname);
981 smbcli_close(cli->tree, fnum);
985 ZERO_STRUCT(result);
986 result.tctx = tctx;
988 status = multiple_search(cli, tctx, BASEDIR "\\*.*",
989 RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
990 CONT_NAME, &result);
991 CHECK_STATUS(status, NT_STATUS_OK);
992 CHECK_VALUE(result.count, num_files);
994 for (i=0;i<num_files-1;i++) {
995 const char *name1, *name2;
996 name1 = result.list[i].both_directory_info.name.s;
997 name2 = result.list[i+1].both_directory_info.name.s;
998 if (strcasecmp_m(name1, name2) > 0) {
999 printf("non-alphabetical order at entry %d '%s' '%s'\n",
1000 i, name1, name2);
1001 printf("Server does not produce sorted directory listings (not an error)\n");
1002 goto done;
1006 talloc_free(result.list);
1008 done:
1009 smb_raw_exit(cli->session);
1010 smbcli_deltree(cli->tree, BASEDIR);
1012 return ret;
1018 basic testing of many old style search calls using separate dirs
1020 static bool test_many_dirs(struct torture_context *tctx,
1021 struct smbcli_state *cli)
1023 const int num_dirs = 20;
1024 int i, fnum, n;
1025 char *fname, *dname;
1026 bool ret = true;
1027 NTSTATUS status;
1028 union smb_search_data *file, *file2, *file3;
1030 if (!torture_setting_bool(tctx, "raw_search_search", true)) {
1031 torture_comment(tctx, "Skipping these tests as the server "
1032 "doesn't support old style search calls\n");
1033 return true;
1035 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1037 printf("Creating %d dirs\n", num_dirs);
1039 for (i=0;i<num_dirs;i++) {
1040 dname = talloc_asprintf(cli, BASEDIR "\\d%d", i);
1041 status = smbcli_mkdir(cli->tree, dname);
1042 if (!NT_STATUS_IS_OK(status)) {
1043 printf("(%s) Failed to create %s - %s\n",
1044 __location__, dname, nt_errstr(status));
1045 ret = false;
1046 goto done;
1049 for (n=0;n<3;n++) {
1050 fname = talloc_asprintf(cli, BASEDIR "\\d%d\\f%d-%d.txt", i, i, n);
1051 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1052 if (fnum == -1) {
1053 printf("(%s) Failed to create %s - %s\n",
1054 __location__, fname, smbcli_errstr(cli->tree));
1055 ret = false;
1056 goto done;
1058 talloc_free(fname);
1059 smbcli_close(cli->tree, fnum);
1062 talloc_free(dname);
1065 file = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1066 file2 = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1067 file3 = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1069 printf("Search first on %d dirs\n", num_dirs);
1071 for (i=0;i<num_dirs;i++) {
1072 union smb_search_first io;
1073 io.search_first.level = RAW_SEARCH_SEARCH;
1074 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
1075 io.search_first.in.max_count = 1;
1076 io.search_first.in.search_attrib = 0;
1077 io.search_first.in.pattern = talloc_asprintf(tctx, BASEDIR "\\d%d\\*.txt", i);
1078 fname = talloc_asprintf(tctx, "f%d-", i);
1080 io.search_first.out.count = 0;
1082 status = smb_raw_search_first(cli->tree, tctx,
1083 &io, (void *)&file[i], single_search_callback);
1084 if (io.search_first.out.count != 1) {
1085 printf("(%s) search first gave %d entries for dir %d - %s\n",
1086 __location__, io.search_first.out.count, i, nt_errstr(status));
1087 ret = false;
1088 goto done;
1090 CHECK_STATUS(status, NT_STATUS_OK);
1091 if (strncasecmp(file[i].search.name, fname, strlen(fname)) != 0) {
1092 printf("(%s) incorrect name '%s' expected '%s'[12].txt\n",
1093 __location__, file[i].search.name, fname);
1094 ret = false;
1095 goto done;
1098 talloc_free(fname);
1101 printf("Search next on %d dirs\n", num_dirs);
1103 for (i=0;i<num_dirs;i++) {
1104 union smb_search_next io2;
1106 io2.search_next.level = RAW_SEARCH_SEARCH;
1107 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
1108 io2.search_next.in.max_count = 1;
1109 io2.search_next.in.search_attrib = 0;
1110 io2.search_next.in.id = file[i].search.id;
1111 fname = talloc_asprintf(tctx, "f%d-", i);
1113 io2.search_next.out.count = 0;
1115 status = smb_raw_search_next(cli->tree, tctx,
1116 &io2, (void *)&file2[i], single_search_callback);
1117 if (io2.search_next.out.count != 1) {
1118 printf("(%s) search next gave %d entries for dir %d - %s\n",
1119 __location__, io2.search_next.out.count, i, nt_errstr(status));
1120 ret = false;
1121 goto done;
1123 CHECK_STATUS(status, NT_STATUS_OK);
1124 if (strncasecmp(file2[i].search.name, fname, strlen(fname)) != 0) {
1125 printf("(%s) incorrect name '%s' expected '%s'[12].txt\n",
1126 __location__, file2[i].search.name, fname);
1127 ret = false;
1128 goto done;
1131 talloc_free(fname);
1135 printf("Search next (rewind) on %d dirs\n", num_dirs);
1137 for (i=0;i<num_dirs;i++) {
1138 union smb_search_next io2;
1140 io2.search_next.level = RAW_SEARCH_SEARCH;
1141 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
1142 io2.search_next.in.max_count = 1;
1143 io2.search_next.in.search_attrib = 0;
1144 io2.search_next.in.id = file[i].search.id;
1145 fname = talloc_asprintf(tctx, "f%d-", i);
1146 io2.search_next.out.count = 0;
1148 status = smb_raw_search_next(cli->tree, tctx,
1149 &io2, (void *)&file3[i], single_search_callback);
1150 if (io2.search_next.out.count != 1) {
1151 printf("(%s) search next gave %d entries for dir %d - %s\n",
1152 __location__, io2.search_next.out.count, i, nt_errstr(status));
1153 ret = false;
1154 goto done;
1156 CHECK_STATUS(status, NT_STATUS_OK);
1158 if (strncasecmp(file3[i].search.name, file2[i].search.name, 3) != 0) {
1159 printf("(%s) incorrect name '%s' on rewind at dir %d\n",
1160 __location__, file2[i].search.name, i);
1161 ret = false;
1162 goto done;
1165 if (torture_setting_bool(tctx, "rewind_support", true) &&
1166 strcmp(file3[i].search.name, file2[i].search.name) != 0) {
1167 printf("(%s) server did not rewind - got '%s' expected '%s'\n",
1168 __location__, file3[i].search.name, file2[i].search.name);
1169 ret = false;
1170 goto done;
1173 talloc_free(fname);
1177 done:
1178 smb_raw_exit(cli->session);
1179 smbcli_deltree(cli->tree, BASEDIR);
1181 return ret;
1186 testing of OS/2 style delete
1188 static bool test_os2_delete(struct torture_context *tctx,
1189 struct smbcli_state *cli)
1191 const int num_files = 700;
1192 const int delete_count = 4;
1193 int total_deleted = 0;
1194 int i, fnum;
1195 char *fname;
1196 bool ret = true;
1197 NTSTATUS status;
1198 union smb_search_first io;
1199 union smb_search_next io2;
1200 struct multiple_result result;
1202 if (!torture_setting_bool(tctx, "search_ea_size", true)){
1203 torture_comment(tctx,
1204 "Server does not support RAW_SEARCH_EA_SIZE "
1205 "level. Skipping this test\n");
1206 return true;
1209 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1211 printf("Testing OS/2 style delete on %d files\n", num_files);
1213 for (i=0;i<num_files;i++) {
1214 fname = talloc_asprintf(cli, BASEDIR "\\file%u.txt", i);
1215 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1216 if (fnum == -1) {
1217 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1218 ret = false;
1219 goto done;
1221 talloc_free(fname);
1222 smbcli_close(cli->tree, fnum);
1226 ZERO_STRUCT(result);
1227 result.tctx = tctx;
1229 io.t2ffirst.level = RAW_SEARCH_TRANS2;
1230 io.t2ffirst.data_level = RAW_SEARCH_DATA_EA_SIZE;
1231 io.t2ffirst.in.search_attrib = 0;
1232 io.t2ffirst.in.max_count = 100;
1233 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1234 io.t2ffirst.in.storage_type = 0;
1235 io.t2ffirst.in.pattern = BASEDIR "\\*";
1237 status = smb_raw_search_first(cli->tree, tctx,
1238 &io, &result, multiple_search_callback);
1239 CHECK_STATUS(status, NT_STATUS_OK);
1241 for (i=0;i<MIN(result.count, delete_count);i++) {
1242 fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
1243 status = smbcli_unlink(cli->tree, fname);
1244 CHECK_STATUS(status, NT_STATUS_OK);
1245 total_deleted++;
1246 talloc_free(fname);
1249 io2.t2fnext.level = RAW_SEARCH_TRANS2;
1250 io2.t2fnext.data_level = RAW_SEARCH_DATA_EA_SIZE;
1251 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1252 io2.t2fnext.in.max_count = 100;
1253 io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1254 io2.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1255 io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1257 do {
1258 ZERO_STRUCT(result);
1259 result.tctx = tctx;
1261 status = smb_raw_search_next(cli->tree, tctx,
1262 &io2, &result, multiple_search_callback);
1263 if (!NT_STATUS_IS_OK(status)) {
1264 break;
1267 for (i=0;i<MIN(result.count, delete_count);i++) {
1268 fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
1269 status = smbcli_unlink(cli->tree, fname);
1270 CHECK_STATUS(status, NT_STATUS_OK);
1271 total_deleted++;
1272 talloc_free(fname);
1275 if (i>0) {
1276 io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1277 io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1279 } while (NT_STATUS_IS_OK(status) && result.count != 0);
1281 CHECK_STATUS(status, NT_STATUS_OK);
1283 if (total_deleted != num_files) {
1284 printf("error: deleted %d - expected to delete %d\n",
1285 total_deleted, num_files);
1286 ret = false;
1289 done:
1290 smb_raw_exit(cli->session);
1291 smbcli_deltree(cli->tree, BASEDIR);
1293 return ret;
1297 static int ealist_cmp(union smb_search_data *r1, union smb_search_data *r2)
1299 return strcmp(r1->ea_list.name.s, r2->ea_list.name.s);
1303 testing of the rather strange ea_list level
1305 static bool test_ea_list(struct torture_context *tctx,
1306 struct smbcli_state *cli)
1308 int fnum;
1309 bool ret = true;
1310 NTSTATUS status;
1311 union smb_search_first io;
1312 union smb_search_next nxt;
1313 struct multiple_result result;
1314 union smb_setfileinfo setfile;
1316 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1318 printf("Testing RAW_SEARCH_EA_LIST level\n");
1320 if (!torture_setting_bool(tctx, "search_ea_support", true) ||
1321 !torture_setting_bool(tctx, "ea_support", true)) {
1322 printf("..skipped per target configuration.\n");
1323 return true;
1326 fnum = smbcli_open(cli->tree, BASEDIR "\\file1.txt", O_CREAT|O_RDWR, DENY_NONE);
1327 smbcli_close(cli->tree, fnum);
1329 fnum = smbcli_open(cli->tree, BASEDIR "\\file2.txt", O_CREAT|O_RDWR, DENY_NONE);
1330 smbcli_close(cli->tree, fnum);
1332 fnum = smbcli_open(cli->tree, BASEDIR "\\file3.txt", O_CREAT|O_RDWR, DENY_NONE);
1333 smbcli_close(cli->tree, fnum);
1335 setfile.generic.level = RAW_SFILEINFO_EA_SET;
1336 setfile.generic.in.file.path = BASEDIR "\\file2.txt";
1337 setfile.ea_set.in.num_eas = 2;
1338 setfile.ea_set.in.eas = talloc_array(tctx, struct ea_struct, 2);
1339 setfile.ea_set.in.eas[0].flags = 0;
1340 setfile.ea_set.in.eas[0].name.s = "EA ONE";
1341 setfile.ea_set.in.eas[0].value = data_blob_string_const("VALUE 1");
1342 setfile.ea_set.in.eas[1].flags = 0;
1343 setfile.ea_set.in.eas[1].name.s = "SECOND EA";
1344 setfile.ea_set.in.eas[1].value = data_blob_string_const("Value Two");
1346 status = smb_raw_setpathinfo(cli->tree, &setfile);
1347 CHECK_STATUS(status, NT_STATUS_OK);
1349 setfile.generic.in.file.path = BASEDIR "\\file3.txt";
1350 status = smb_raw_setpathinfo(cli->tree, &setfile);
1351 CHECK_STATUS(status, NT_STATUS_OK);
1353 ZERO_STRUCT(result);
1354 result.tctx = tctx;
1356 io.t2ffirst.level = RAW_SEARCH_TRANS2;
1357 io.t2ffirst.data_level = RAW_SEARCH_DATA_EA_LIST;
1358 io.t2ffirst.in.search_attrib = 0;
1359 io.t2ffirst.in.max_count = 2;
1360 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1361 io.t2ffirst.in.storage_type = 0;
1362 io.t2ffirst.in.pattern = BASEDIR "\\*";
1363 io.t2ffirst.in.num_names = 2;
1364 io.t2ffirst.in.ea_names = talloc_array(tctx, struct ea_name, 2);
1365 io.t2ffirst.in.ea_names[0].name.s = "SECOND EA";
1366 io.t2ffirst.in.ea_names[1].name.s = "THIRD EA";
1368 status = smb_raw_search_first(cli->tree, tctx,
1369 &io, &result, multiple_search_callback);
1370 CHECK_STATUS(status, NT_STATUS_OK);
1371 CHECK_VALUE(result.count, 2);
1373 nxt.t2fnext.level = RAW_SEARCH_TRANS2;
1374 nxt.t2fnext.data_level = RAW_SEARCH_DATA_EA_LIST;
1375 nxt.t2fnext.in.handle = io.t2ffirst.out.handle;
1376 nxt.t2fnext.in.max_count = 2;
1377 nxt.t2fnext.in.resume_key = result.list[1].ea_list.resume_key;
1378 nxt.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME | FLAG_TRANS2_FIND_CONTINUE;
1379 nxt.t2fnext.in.last_name = result.list[1].ea_list.name.s;
1380 nxt.t2fnext.in.num_names = 2;
1381 nxt.t2fnext.in.ea_names = talloc_array(tctx, struct ea_name, 2);
1382 nxt.t2fnext.in.ea_names[0].name.s = "SECOND EA";
1383 nxt.t2fnext.in.ea_names[1].name.s = "THIRD EA";
1385 status = smb_raw_search_next(cli->tree, tctx,
1386 &nxt, &result, multiple_search_callback);
1387 CHECK_STATUS(status, NT_STATUS_OK);
1389 /* we have to sort the result as different servers can return directories
1390 in different orders */
1391 TYPESAFE_QSORT(result.list, result.count, ealist_cmp);
1393 CHECK_VALUE(result.count, 3);
1394 CHECK_VALUE(result.list[0].ea_list.eas.num_eas, 2);
1395 CHECK_STRING(result.list[0].ea_list.name.s, "file1.txt");
1396 CHECK_STRING(result.list[0].ea_list.eas.eas[0].name.s, "SECOND EA");
1397 CHECK_VALUE(result.list[0].ea_list.eas.eas[0].value.length, 0);
1398 CHECK_STRING(result.list[0].ea_list.eas.eas[1].name.s, "THIRD EA");
1399 CHECK_VALUE(result.list[0].ea_list.eas.eas[1].value.length, 0);
1401 CHECK_STRING(result.list[1].ea_list.name.s, "file2.txt");
1402 CHECK_STRING(result.list[1].ea_list.eas.eas[0].name.s, "SECOND EA");
1403 CHECK_VALUE(result.list[1].ea_list.eas.eas[0].value.length, 9);
1404 CHECK_STRING((const char *)result.list[1].ea_list.eas.eas[0].value.data, "Value Two");
1405 CHECK_STRING(result.list[1].ea_list.eas.eas[1].name.s, "THIRD EA");
1406 CHECK_VALUE(result.list[1].ea_list.eas.eas[1].value.length, 0);
1408 CHECK_STRING(result.list[2].ea_list.name.s, "file3.txt");
1409 CHECK_STRING(result.list[2].ea_list.eas.eas[0].name.s, "SECOND EA");
1410 CHECK_VALUE(result.list[2].ea_list.eas.eas[0].value.length, 9);
1411 CHECK_STRING((const char *)result.list[2].ea_list.eas.eas[0].value.data, "Value Two");
1412 CHECK_STRING(result.list[2].ea_list.eas.eas[1].name.s, "THIRD EA");
1413 CHECK_VALUE(result.list[2].ea_list.eas.eas[1].value.length, 0);
1415 smb_raw_exit(cli->session);
1416 smbcli_deltree(cli->tree, BASEDIR);
1418 return ret;
1422 Test the behavior of max count parameter in TRANS2_FIND_FIRST2 and
1423 TRANS2_FIND_NEXT2 queries
1425 static bool test_max_count(struct torture_context *tctx,
1426 struct smbcli_state *cli)
1428 const int num_files = 2;
1429 int i, fnum;
1430 char *fname;
1431 bool ret = true;
1432 NTSTATUS status;
1433 struct multiple_result result;
1434 union smb_search_first io;
1435 union smb_search_next io2;
1437 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1439 torture_comment(tctx, "Creating %d files\n", num_files);
1441 for (i=num_files-1;i>=0;i--) {
1442 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
1443 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1444 if (fnum == -1) {
1445 torture_comment(tctx,
1446 "Failed to create %s - %s\n",
1447 fname, smbcli_errstr(cli->tree));
1448 ret = false;
1449 goto done;
1451 talloc_free(fname);
1452 smbcli_close(cli->tree, fnum);
1455 torture_comment(tctx, "Set max_count parameter to 0. "
1456 "This should return 1 entry\n");
1457 ZERO_STRUCT(result);
1458 result.tctx = talloc_new(tctx);
1460 io.t2ffirst.level = RAW_SEARCH_TRANS2;
1461 io.t2ffirst.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1462 io.t2ffirst.in.search_attrib = 0;
1463 io.t2ffirst.in.max_count = 0;
1464 io.t2ffirst.in.flags = 0;
1465 io.t2ffirst.in.storage_type = 0;
1466 io.t2ffirst.in.pattern = BASEDIR "\\*.*";
1468 status = smb_raw_search_first(cli->tree, tctx,
1469 &io, &result, multiple_search_callback);
1470 CHECK_STATUS(status, NT_STATUS_OK);
1471 CHECK_VALUE(result.count, 1);
1473 torture_comment(tctx, "Set max_count to 1. This should also "
1474 "return 1 entry\n");
1475 io2.t2fnext.level = RAW_SEARCH_TRANS2;
1476 io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1477 io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1478 io2.t2fnext.in.max_count = 1;
1479 io2.t2fnext.in.resume_key = 0;
1480 io2.t2fnext.in.flags = 0;
1481 io2.t2fnext.in.last_name =
1482 result.list[result.count-1].both_directory_info.name.s;
1484 status = smb_raw_search_next(cli->tree, tctx,
1485 &io2, &result, multiple_search_callback);
1486 CHECK_STATUS(status, NT_STATUS_OK);
1487 CHECK_VALUE(result.count, 2);
1488 done:
1489 smb_raw_exit(cli->session);
1490 smbcli_deltree(cli->tree, BASEDIR);
1492 return ret;
1496 basic testing of all RAW_SEARCH_* calls using a single file
1498 struct torture_suite *torture_raw_search(TALLOC_CTX *mem_ctx)
1500 struct torture_suite *suite = torture_suite_create(mem_ctx, "search");
1502 torture_suite_add_1smb_test(suite, "one file search", test_one_file);
1503 torture_suite_add_1smb_test(suite, "many files", test_many_files);
1504 torture_suite_add_1smb_test(suite, "sorted", test_sorted);
1505 torture_suite_add_1smb_test(suite, "modify search", test_modify_search);
1506 torture_suite_add_1smb_test(suite, "many dirs", test_many_dirs);
1507 torture_suite_add_1smb_test(suite, "os2 delete", test_os2_delete);
1508 torture_suite_add_1smb_test(suite, "ea list", test_ea_list);
1509 torture_suite_add_1smb_test(suite, "max count", test_max_count);
1511 return suite;