s3:libsmb: allow store_cldap_reply() to work with a ipv6 response
[samba.git] / source4 / torture / raw / search.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RAW_SEARCH_* individual test suite
4    Copyright (C) Andrew Tridgell 2003
5    
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.
10    
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.
15    
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/>.
18 */
19
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"
28
29 #undef strncasecmp
30
31 #define BASEDIR "\\testsearch"
32
33 #define CHECK_STATUS_LEVEL(__tctx, __status, __level, __supp)           \
34         do {                                                            \
35                 if (NT_STATUS_EQUAL(__status,                           \
36                         NT_STATUS_NOT_SUPPORTED) ||                     \
37                     NT_STATUS_EQUAL(__status,                           \
38                         NT_STATUS_NOT_IMPLEMENTED)) {                   \
39                         __supp = false;                                 \
40                 } else {                                                \
41                         __supp = true;                                  \
42                 }                                                       \
43                 if (__supp) {                                           \
44                         torture_assert_ntstatus_ok_goto(__tctx,         \
45                             __status, ret, done, #__level" failed");    \
46                 } else {                                                \
47                         torture_warning(__tctx, "(%s) Info "            \
48                             "level "#__level" is %s",                   \
49                             __location__, nt_errstr(__status));         \
50                 }                                                       \
51         } while (0)
52
53 /*
54   callback function for single_search
55 */
56 static bool single_search_callback(void *private_data, const union smb_search_data *file)
57 {
58         union smb_search_data *data = (union smb_search_data *)private_data;
59
60         *data = *file;
61
62         return true;
63 }
64
65 /*
66   do a single file (non-wildcard) search 
67 */
68 NTSTATUS torture_single_search(struct smbcli_state *cli, 
69                                TALLOC_CTX *tctx,
70                                const char *pattern,
71                                enum smb_search_level level,
72                                enum smb_search_data_level data_level,
73                                uint16_t attrib,
74                                union smb_search_data *data)
75 {
76         union smb_search_first io;
77         union smb_search_close c;
78         NTSTATUS status;
79
80         switch (level) {
81         case RAW_SEARCH_SEARCH:
82         case RAW_SEARCH_FFIRST:
83         case RAW_SEARCH_FUNIQUE:
84                 io.search_first.level = level;
85                 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
86                 io.search_first.in.max_count = 1;
87                 io.search_first.in.search_attrib = attrib;
88                 io.search_first.in.pattern = pattern;
89                 break;
90
91         case RAW_SEARCH_TRANS2:
92                 io.t2ffirst.level = RAW_SEARCH_TRANS2;
93                 io.t2ffirst.data_level = data_level;
94                 io.t2ffirst.in.search_attrib = attrib;
95                 io.t2ffirst.in.max_count = 1;
96                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
97                 io.t2ffirst.in.storage_type = 0;
98                 io.t2ffirst.in.pattern = pattern;
99                 break;
100
101         case RAW_SEARCH_SMB2:
102                 return NT_STATUS_INVALID_LEVEL;
103         }
104
105         status = smb_raw_search_first(cli->tree, tctx,
106                                       &io, (void *)data, single_search_callback);
107
108         if (NT_STATUS_IS_OK(status) && level == RAW_SEARCH_FFIRST) {
109                 c.fclose.level = RAW_FINDCLOSE_FCLOSE;
110                 c.fclose.in.max_count = 1;
111                 c.fclose.in.search_attrib = 0;
112                 c.fclose.in.id = data->search.id;
113                 status = smb_raw_search_close(cli->tree, &c);
114         }
115         
116         return status;
117 }
118
119
120 static struct {
121         const char *name;
122         enum smb_search_level level;
123         enum smb_search_data_level data_level;
124         int name_offset;
125         int resume_key_offset;
126         uint32_t capability_mask;
127         NTSTATUS status;
128         union smb_search_data data;
129 } levels[] = {
130         {
131                 .name              = "FFIRST",
132                 .level             = RAW_SEARCH_FFIRST,
133                 .data_level        = RAW_SEARCH_DATA_SEARCH,
134                 .name_offset       = offsetof(union smb_search_data,
135                                               search.name),
136                 .resume_key_offset = -1,
137         },
138         {
139                 .name              = "FUNIQUE",
140                 .level             = RAW_SEARCH_FUNIQUE,
141                 .data_level        = RAW_SEARCH_DATA_SEARCH,
142                 .name_offset       = offsetof(union smb_search_data,
143                                               search.name),
144                 .resume_key_offset = -1,
145         },
146         {
147                 .name              = "SEARCH",
148                 .level             = RAW_SEARCH_SEARCH,
149                 .data_level        = RAW_SEARCH_DATA_SEARCH,
150                 .name_offset       = offsetof(union smb_search_data,
151                                               search.name),
152                 .resume_key_offset = -1,
153         },
154         {
155                 .name              = "STANDARD",
156                 .level             = RAW_SEARCH_TRANS2,
157                 .data_level        = RAW_SEARCH_DATA_STANDARD,
158                 .name_offset       =  offsetof(union smb_search_data,
159                                                standard.name.s),
160                 .resume_key_offset =  offsetof(union smb_search_data,
161                                                standard.resume_key),
162         },
163         {
164                 .name              = "EA_SIZE",
165                 .level             = RAW_SEARCH_TRANS2,
166                 .data_level        = RAW_SEARCH_DATA_EA_SIZE,
167                 .name_offset       = offsetof(union smb_search_data,
168                                               ea_size.name.s),
169                 .resume_key_offset = offsetof(union smb_search_data,
170                                               ea_size.resume_key),
171         },
172         {
173                 .name              = "DIRECTORY_INFO",
174                 .level             = RAW_SEARCH_TRANS2,
175                 .data_level        = RAW_SEARCH_DATA_DIRECTORY_INFO,
176                 .name_offset       = offsetof(union smb_search_data,
177                                               directory_info.name.s),
178                 .resume_key_offset = offsetof(union smb_search_data,
179                                               directory_info.file_index),
180         },
181         {
182                 .name              = "FULL_DIRECTORY_INFO",
183                 .level             = RAW_SEARCH_TRANS2,
184                 .data_level        = RAW_SEARCH_DATA_FULL_DIRECTORY_INFO,
185                 .name_offset       = offsetof(union smb_search_data,
186                                               full_directory_info.name.s),
187                 .resume_key_offset = offsetof(union smb_search_data,
188                                               full_directory_info.file_index),
189         },
190         {
191                 .name              = "NAME_INFO",
192                 .level             = RAW_SEARCH_TRANS2,
193                 .data_level        = RAW_SEARCH_DATA_NAME_INFO,
194                 .name_offset       =  offsetof(union smb_search_data,
195                                                name_info.name.s),
196                 .resume_key_offset =  offsetof(union smb_search_data,
197                                                name_info.file_index),
198         },
199         {
200                 .name              = "BOTH_DIRECTORY_INFO",
201                 .level             = RAW_SEARCH_TRANS2,
202                 .data_level        = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
203                 .name_offset       = offsetof(union smb_search_data,
204                                               both_directory_info.name.s),
205                 .resume_key_offset = offsetof(union smb_search_data,
206                                               both_directory_info.file_index),
207         },
208         {
209                 .name              = "ID_FULL_DIRECTORY_INFO",
210                 .level             = RAW_SEARCH_TRANS2,
211                 .data_level        = RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO,
212                 .name_offset       = offsetof(union smb_search_data,
213                                               id_full_directory_info.name.s),
214                 .resume_key_offset = offsetof(union smb_search_data,
215                                               id_full_directory_info.file_index),
216         },
217         {
218                 .name              = "ID_BOTH_DIRECTORY_INFO",
219                 .level             = RAW_SEARCH_TRANS2,
220                 .data_level        = RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO,
221                 .name_offset       = offsetof(union smb_search_data,
222                                               id_both_directory_info.name.s),
223                 .resume_key_offset = offsetof(union smb_search_data,
224                                               id_both_directory_info.file_index),
225         },
226         {
227                 .name              = "UNIX_INFO",
228                 .level             = RAW_SEARCH_TRANS2,
229                 .data_level        = RAW_SEARCH_DATA_UNIX_INFO,
230                 .name_offset       = offsetof(union smb_search_data,
231                                               unix_info.name),
232                 .resume_key_offset = offsetof(union smb_search_data,
233                                               unix_info.file_index),
234                 .capability_mask = CAP_UNIX
235         },
236 };
237
238
239 /*
240   return level name
241 */
242 static const char *level_name(enum smb_search_level level,
243                               enum smb_search_data_level data_level)
244 {
245         int i;
246         for (i=0;i<ARRAY_SIZE(levels);i++) {
247                 if (level == levels[i].level &&
248                     data_level == levels[i].data_level) {
249                         return levels[i].name;
250                 }
251         }
252         return NULL;
253 }
254
255 /*
256   extract the name from a smb_data structure and level
257 */
258 static const char *extract_name(void *data, enum smb_search_level level,
259                                 enum smb_search_data_level data_level)
260 {
261         int i;
262         for (i=0;i<ARRAY_SIZE(levels);i++) {
263                 if (level == levels[i].level &&
264                     data_level == levels[i].data_level) {
265                         return *(const char **)(levels[i].name_offset + (char *)data);
266                 }
267         }
268         return NULL;
269 }
270
271 /*
272   extract the name from a smb_data structure and level
273 */
274 static uint32_t extract_resume_key(void *data, enum smb_search_level level,
275                                    enum smb_search_data_level data_level)
276 {
277         int i;
278         for (i=0;i<ARRAY_SIZE(levels);i++) {
279                 if (level == levels[i].level &&
280                     data_level == levels[i].data_level) {
281                         return *(uint32_t *)(levels[i].resume_key_offset + (char *)data);
282                 }
283         }
284         return 0;
285 }
286
287 /* find a level in the table by name */
288 static union smb_search_data *find(const char *name)
289 {
290         int i;
291         for (i=0;i<ARRAY_SIZE(levels);i++) {
292                 if (NT_STATUS_IS_OK(levels[i].status) && 
293                     strcmp(levels[i].name, name) == 0) {
294                         return &levels[i].data;
295                 }
296         }
297         return NULL;
298 }
299
300 /*
301  * Negotiate SMB1+POSIX.
302  */
303
304 static NTSTATUS setup_smb1_posix(struct torture_context *tctx,
305                                  struct smbcli_state *cli_unix)
306 {
307         struct smb_trans2 tp;
308         uint16_t setup;
309         uint8_t data[12];
310         uint8_t params[4];
311         uint32_t cap = cli_unix->transport->negotiate.capabilities;
312
313         if ((cap & CAP_UNIX) == 0) {
314                 /*
315                  * Server doesn't support SMB1+POSIX.
316                  * The caller will skip the UNIX info
317                  * level anyway.
318                  */
319                 torture_comment(tctx,
320                         "Server doesn't support SMB1+POSIX\n");
321                 return NT_STATUS_OK;
322         }
323
324         /* Setup POSIX on this connection. */
325         SSVAL(data, 0, CIFS_UNIX_MAJOR_VERSION);
326         SSVAL(data, 2, CIFS_UNIX_MINOR_VERSION);
327         SBVAL(data,4,((uint64_t)(
328                 CIFS_UNIX_POSIX_ACLS_CAP|
329                 CIFS_UNIX_POSIX_PATHNAMES_CAP|
330                 CIFS_UNIX_FCNTL_LOCKS_CAP|
331                 CIFS_UNIX_EXTATTR_CAP|
332                 CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP)));
333         setup = TRANSACT2_SETFSINFO;
334         tp.in.max_setup = 0;
335         tp.in.flags = 0;
336         tp.in.timeout = 0;
337         tp.in.setup_count = 1;
338         tp.in.max_param = 0;
339         tp.in.max_data = 0;
340         tp.in.setup = &setup;
341         tp.in.trans_name = NULL;
342         SSVAL(params, 0, 0);
343         SSVAL(params, 2, SMB_SET_CIFS_UNIX_INFO);
344         tp.in.params = data_blob_talloc(tctx, params, 4);
345         tp.in.data = data_blob_talloc(tctx, data, 12);
346         return smb_raw_trans2(cli_unix->tree, tctx, &tp);
347 }
348
349 /* 
350    basic testing of all RAW_SEARCH_* calls using a single file
351 */
352 static bool test_one_file(struct torture_context *tctx,
353                           struct smbcli_state *cli,
354                           struct smbcli_state *cli_unix)
355 {
356         bool ret = true;
357         int fnum;
358         const char *fname = "torture_search.txt";
359         const char *fname2 = "torture_search-NOTEXIST.txt";
360         NTSTATUS status;
361         int i;
362         union smb_fileinfo all_info, alt_info, name_info, internal_info;
363         bool all_info_supported, alt_info_supported, name_info_supported,
364             internal_info_supported;
365         union smb_search_data *s;
366
367         status = setup_smb1_posix(tctx, cli_unix);
368         if (!NT_STATUS_IS_OK(status)) {
369                 torture_result(tctx,
370                         TORTURE_FAIL,
371                         __location__"setup_smb1_posix() failed (%s)\n",
372                         nt_errstr(status));
373                 ret = false;
374                 goto done;
375         }
376
377         fnum = create_complex_file(cli, tctx, fname);
378         if (fnum == -1) {
379                 torture_result(tctx,
380                         TORTURE_FAIL,
381                         __location__"ERROR: open of %s failed (%s)\n",
382                         fname,
383                         smbcli_errstr(cli->tree));
384                 ret = false;
385                 goto done;
386         }
387
388         /* call all the levels */
389         for (i=0;i<ARRAY_SIZE(levels);i++) {
390                 NTSTATUS expected_status;
391                 uint32_t cap = cli->transport->negotiate.capabilities;
392                 struct smbcli_state *cli_search = cli;
393
394                 torture_comment(tctx, "Testing %s\n", levels[i].name);
395
396                 if (levels[i].data_level == RAW_SEARCH_DATA_UNIX_INFO) {
397                         /*
398                          * For an SMB1+POSIX info level, use the cli_unix
399                          * connection.
400                          */
401                         cli_search = cli_unix;
402                 }
403
404                 levels[i].status = torture_single_search(cli_search, tctx, fname,
405                                                          levels[i].level,
406                                                          levels[i].data_level,
407                                                          0,
408                                                          &levels[i].data);
409
410                 /* see if this server claims to support this level */
411                 if (((cap & levels[i].capability_mask) != levels[i].capability_mask)
412                     || NT_STATUS_EQUAL(levels[i].status, NT_STATUS_NOT_SUPPORTED)) {
413                         printf("search level %s(%d) not supported by server\n",
414                                levels[i].name, (int)levels[i].level);
415                         continue;
416                 }
417
418                 if (!NT_STATUS_IS_OK(levels[i].status)) {
419                         torture_result(tctx,
420                                 TORTURE_FAIL,
421                                 __location__"search level %s(%d) failed - %s\n",
422                                 levels[i].name, (int)levels[i].level,
423                                 nt_errstr(levels[i].status));
424                         ret = false;
425                         continue;
426                 }
427
428                 status = torture_single_search(cli_search, tctx, fname2,
429                                                levels[i].level,
430                                                levels[i].data_level,
431                                                0,
432                                                &levels[i].data);
433                 
434                 expected_status = NT_STATUS_NO_SUCH_FILE;
435                 if (levels[i].level == RAW_SEARCH_SEARCH ||
436                     levels[i].level == RAW_SEARCH_FFIRST ||
437                     levels[i].level == RAW_SEARCH_FUNIQUE) {
438                         expected_status = STATUS_NO_MORE_FILES;
439                 }
440                 if (!NT_STATUS_EQUAL(status, expected_status)) {
441                         torture_result(tctx,
442                                 TORTURE_FAIL,
443                                 __location__"search level %s(%d) should fail with %s - %s\n",
444                                levels[i].name, (int)levels[i].level, 
445                                nt_errstr(expected_status),
446                                nt_errstr(status));
447                         ret = false;
448                 }
449         }
450
451         /* get the all_info file into to check against */
452         all_info.generic.level = RAW_FILEINFO_ALL_INFO;
453         all_info.generic.in.file.path = fname;
454         status = smb_raw_pathinfo(cli->tree, tctx, &all_info);
455         CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_ALL_INFO",
456             all_info_supported);
457
458         alt_info.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
459         alt_info.generic.in.file.path = fname;
460         status = smb_raw_pathinfo(cli->tree, tctx, &alt_info);
461         CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_ALT_NAME_INFO",
462             alt_info_supported);
463
464         internal_info.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
465         internal_info.generic.in.file.path = fname;
466         status = smb_raw_pathinfo(cli->tree, tctx, &internal_info);
467         CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_INTERNAL_INFORMATION",
468             internal_info_supported);
469
470         name_info.generic.level = RAW_FILEINFO_NAME_INFO;
471         name_info.generic.in.file.path = fname;
472         status = smb_raw_pathinfo(cli->tree, tctx, &name_info);
473         CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_NAME_INFO",
474             name_info_supported);
475
476 #define CHECK_VAL(name, sname1, field1, v, sname2, field2) do { \
477         s = find(name); \
478         if (s) { \
479                 if ((s->sname1.field1) != (v.sname2.out.field2)) { \
480                         torture_result(tctx,\
481                                 TORTURE_FAIL,\
482                                 "(%s) %s/%s [0x%x] != %s/%s [0x%x]\n", \
483                                 __location__, \
484                                 #sname1, #field1, (int)s->sname1.field1, \
485                                 #sname2, #field2, (int)v.sname2.out.field2); \
486                         ret = false; \
487                 } \
488         }} while (0)
489
490 #define CHECK_TIME(name, sname1, field1, v, sname2, field2) do { \
491         s = find(name); \
492         if (s) { \
493                 if (s->sname1.field1 != (~1 & nt_time_to_unix(v.sname2.out.field2))) { \
494                         torture_result(tctx,\
495                                 TORTURE_FAIL,\
496                                 "(%s) %s/%s [%s] != %s/%s [%s]\n", \
497                                 __location__, \
498                                 #sname1, #field1, timestring(tctx, s->sname1.field1), \
499                                 #sname2, #field2, nt_time_string(tctx, v.sname2.out.field2)); \
500                         ret = false; \
501                 } \
502         }} while (0)
503
504 #define CHECK_NTTIME(name, sname1, field1, v, sname2, field2) do { \
505         s = find(name); \
506         if (s) { \
507                 if (s->sname1.field1 != v.sname2.out.field2) { \
508                         torture_result(tctx,\
509                                 TORTURE_FAIL,\
510                                 "(%s) %s/%s [%s] != %s/%s [%s]\n", \
511                                 __location__, \
512                                 #sname1, #field1, nt_time_string(tctx, s->sname1.field1), \
513                                 #sname2, #field2, nt_time_string(tctx, v.sname2.out.field2)); \
514                         ret = false; \
515                 } \
516         }} while (0)
517
518 #define CHECK_STR(name, sname1, field1, v, sname2, field2) do { \
519         s = find(name); \
520         if (s) { \
521                 if (!s->sname1.field1 || strcmp(s->sname1.field1, v.sname2.out.field2.s)) { \
522                         torture_result(tctx,\
523                                 TORTURE_FAIL,\
524                                 "(%s) %s/%s [%s] != %s/%s [%s]\n", \
525                                 __location__, \
526                                 #sname1, #field1, s->sname1.field1, \
527                                 #sname2, #field2, v.sname2.out.field2.s); \
528                         ret = false; \
529                 } \
530         }} while (0)
531
532 #define CHECK_WSTR(name, sname1, field1, v, sname2, field2, flags) do { \
533         s = find(name); \
534         if (s) { \
535                 if (!s->sname1.field1.s || \
536                     strcmp(s->sname1.field1.s, v.sname2.out.field2.s) || \
537                     wire_bad_flags(&s->sname1.field1, flags, cli->transport)) { \
538                         torture_result(tctx,\
539                                 TORTURE_FAIL,\
540                                 "(%s) %s/%s [%s] != %s/%s [%s]\n", \
541                                 __location__, \
542                                 #sname1, #field1, s->sname1.field1.s, \
543                                 #sname2, #field2, v.sname2.out.field2.s); \
544                         ret = false; \
545                 } \
546         }} while (0)
547
548 #define CHECK_NAME(name, sname1, field1, fname, flags) do { \
549         s = find(name); \
550         if (s) { \
551                 if (!s->sname1.field1.s || \
552                     strcmp(s->sname1.field1.s, fname) || \
553                     wire_bad_flags(&s->sname1.field1, flags, cli->transport)) { \
554                         torture_result(tctx,\
555                                 TORTURE_FAIL,\
556                                 "(%s) %s/%s [%s] != %s\n", \
557                                 __location__, \
558                                 #sname1, #field1, s->sname1.field1.s, \
559                                 fname); \
560                         ret = false; \
561                 } \
562         }} while (0)
563
564 #define CHECK_UNIX_NAME(name, sname1, field1, fname, flags) do { \
565         s = find(name); \
566         if (s) { \
567                 if (!s->sname1.field1 || \
568                     strcmp(s->sname1.field1, fname)) { \
569                         torture_result(tctx,\
570                                 TORTURE_FAIL,\
571                                 "(%s) %s/%s [%s] != %s\n", \
572                                 __location__, \
573                                 #sname1, #field1, s->sname1.field1, \
574                                 fname); \
575                         ret = false; \
576                 } \
577         }} while (0)
578         
579         /* check that all the results are as expected */
580         CHECK_VAL("SEARCH",              search,              attrib, all_info, all_info, attrib&0xFFF);
581         CHECK_VAL("STANDARD",            standard,            attrib, all_info, all_info, attrib&0xFFF);
582         CHECK_VAL("EA_SIZE",             ea_size,             attrib, all_info, all_info, attrib&0xFFF);
583         CHECK_VAL("DIRECTORY_INFO",      directory_info,      attrib, all_info, all_info, attrib);
584         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, attrib, all_info, all_info, attrib);
585         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, attrib, all_info, all_info, attrib);
586         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           attrib, all_info, all_info, attrib);
587         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           attrib, all_info, all_info, attrib);
588
589         CHECK_TIME("SEARCH",             search,              write_time, all_info, all_info, write_time);
590         CHECK_TIME("STANDARD",           standard,            write_time, all_info, all_info, write_time);
591         CHECK_TIME("EA_SIZE",            ea_size,             write_time, all_info, all_info, write_time);
592         CHECK_TIME("STANDARD",           standard,            create_time, all_info, all_info, create_time);
593         CHECK_TIME("EA_SIZE",            ea_size,             create_time, all_info, all_info, create_time);
594         CHECK_TIME("STANDARD",           standard,            access_time, all_info, all_info, access_time);
595         CHECK_TIME("EA_SIZE",            ea_size,             access_time, all_info, all_info, access_time);
596
597         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      write_time, all_info, all_info, write_time);
598         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, write_time, all_info, all_info, write_time);
599         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, write_time, all_info, all_info, write_time);
600         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           write_time, all_info, all_info, write_time);
601         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           write_time, all_info, all_info, write_time);
602
603         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      create_time, all_info, all_info, create_time);
604         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
605         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
606         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           create_time, all_info, all_info, create_time);
607         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           create_time, all_info, all_info, create_time);
608
609         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      access_time, all_info, all_info, access_time);
610         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, access_time, all_info, all_info, access_time);
611         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, access_time, all_info, all_info, access_time);
612         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           access_time, all_info, all_info, access_time);
613         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           access_time, all_info, all_info, access_time);
614
615         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      change_time, all_info, all_info, change_time);
616         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, change_time, all_info, all_info, change_time);
617         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, change_time, all_info, all_info, change_time);
618         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           change_time, all_info, all_info, change_time);
619         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           change_time, all_info, all_info, change_time);
620
621         CHECK_VAL("SEARCH",              search,              size, all_info, all_info, size);
622         CHECK_VAL("STANDARD",            standard,            size, all_info, all_info, size);
623         CHECK_VAL("EA_SIZE",             ea_size,             size, all_info, all_info, size);
624         CHECK_VAL("DIRECTORY_INFO",      directory_info,      size, all_info, all_info, size);
625         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, size, all_info, all_info, size);
626         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, size, all_info, all_info, size);
627         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           size, all_info, all_info, size);
628         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           size, all_info, all_info, size);
629         CHECK_VAL("UNIX_INFO",           unix_info,           size, all_info, all_info, size);
630
631         CHECK_VAL("STANDARD",            standard,            alloc_size, all_info, all_info, alloc_size);
632         CHECK_VAL("EA_SIZE",             ea_size,             alloc_size, all_info, all_info, alloc_size);
633         CHECK_VAL("DIRECTORY_INFO",      directory_info,      alloc_size, all_info, all_info, alloc_size);
634         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, alloc_size, all_info, all_info, alloc_size);
635         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, alloc_size, all_info, all_info, alloc_size);
636         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           alloc_size, all_info, all_info, alloc_size);
637         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           alloc_size, all_info, all_info, alloc_size);
638         CHECK_VAL("UNIX_INFO",           unix_info,           alloc_size, all_info, all_info, alloc_size);
639
640         CHECK_VAL("EA_SIZE",             ea_size,             ea_size, all_info, all_info, ea_size);
641         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, ea_size, all_info, all_info, ea_size);
642         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, ea_size, all_info, all_info, ea_size);
643         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           ea_size, all_info, all_info, ea_size);
644         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           ea_size, all_info, all_info, ea_size);
645
646         if (alt_info_supported) {
647                 CHECK_STR("SEARCH", search, name, alt_info, alt_name_info,
648                     fname);
649                 CHECK_WSTR("BOTH_DIRECTORY_INFO", both_directory_info,
650                     short_name, alt_info, alt_name_info, fname, STR_UNICODE);
651         }
652
653         CHECK_NAME("STANDARD",            standard,            name, fname, 0);
654         CHECK_NAME("EA_SIZE",             ea_size,             name, fname, 0);
655         CHECK_NAME("DIRECTORY_INFO",      directory_info,      name, fname, STR_TERMINATE_ASCII);
656         CHECK_NAME("FULL_DIRECTORY_INFO", full_directory_info, name, fname, STR_TERMINATE_ASCII);
657
658         if (name_info_supported) {
659                 CHECK_NAME("NAME_INFO", name_info, name, fname,
660                     STR_TERMINATE_ASCII);
661         }
662
663         CHECK_NAME("BOTH_DIRECTORY_INFO", both_directory_info, name, fname, STR_TERMINATE_ASCII);
664         CHECK_NAME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           name, fname, STR_TERMINATE_ASCII);
665         CHECK_NAME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           name, fname, STR_TERMINATE_ASCII);
666         CHECK_UNIX_NAME("UNIX_INFO",           unix_info,           name, fname, STR_TERMINATE_ASCII);
667
668         if (internal_info_supported) {
669                 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,
670                     file_id, internal_info, internal_information, file_id);
671                 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,
672                     file_id, internal_info, internal_information, file_id);
673         }
674
675 done:
676         smb_raw_exit(cli->session);
677         smbcli_unlink(cli->tree, fname);
678
679         return ret;
680 }
681
682
683 struct multiple_result {
684         TALLOC_CTX *tctx;
685         int count;
686         union smb_search_data *list;
687 };
688
689 /*
690   callback function for multiple_search
691 */
692 static bool multiple_search_callback(void *private_data, const union smb_search_data *file)
693 {
694         struct multiple_result *data = (struct multiple_result *)private_data;
695
696
697         data->count++;
698         data->list = talloc_realloc(data->tctx,
699                                       data->list, 
700                                       union smb_search_data,
701                                       data->count);
702
703         data->list[data->count-1] = *file;
704
705         return true;
706 }
707
708 enum continue_type {CONT_FLAGS, CONT_NAME, CONT_RESUME_KEY};
709
710 /*
711   do a single file (non-wildcard) search 
712 */
713 static NTSTATUS multiple_search(struct smbcli_state *cli, 
714                                 TALLOC_CTX *tctx,
715                                 const char *pattern,
716                                 enum smb_search_data_level data_level,
717                                 enum continue_type cont_type,
718                                 void *data)
719 {
720         union smb_search_first io;
721         union smb_search_next io2;
722         NTSTATUS status;
723         const int per_search = 100;
724         struct multiple_result *result = (struct multiple_result *)data;
725
726         if (data_level == RAW_SEARCH_DATA_SEARCH) {
727                 io.search_first.level = RAW_SEARCH_SEARCH;
728                 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
729                 io.search_first.in.max_count = per_search;
730                 io.search_first.in.search_attrib = 0;
731                 io.search_first.in.pattern = pattern;
732         } else {
733                 io.t2ffirst.level = RAW_SEARCH_TRANS2;
734                 io.t2ffirst.data_level = data_level;
735                 io.t2ffirst.in.search_attrib = 0;
736                 io.t2ffirst.in.max_count = per_search;
737                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
738                 io.t2ffirst.in.storage_type = 0;
739                 io.t2ffirst.in.pattern = pattern;
740                 if (cont_type == CONT_RESUME_KEY) {
741                         io.t2ffirst.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME | 
742                                 FLAG_TRANS2_FIND_BACKUP_INTENT;
743                 }
744         }
745
746         status = smb_raw_search_first(cli->tree, tctx,
747                                       &io, data, multiple_search_callback);
748         
749
750         while (NT_STATUS_IS_OK(status)) {
751                 if (data_level == RAW_SEARCH_DATA_SEARCH) {
752                         io2.search_next.level = RAW_SEARCH_SEARCH;
753                         io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
754                         io2.search_next.in.max_count = per_search;
755                         io2.search_next.in.search_attrib = 0;
756                         io2.search_next.in.id = result->list[result->count-1].search.id;
757                 } else {
758                         io2.t2fnext.level = RAW_SEARCH_TRANS2;
759                         io2.t2fnext.data_level = data_level;
760                         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
761                         io2.t2fnext.in.max_count = per_search;
762                         io2.t2fnext.in.resume_key = 0;
763                         io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
764                         io2.t2fnext.in.last_name = "";
765                         switch (cont_type) {
766                         case CONT_RESUME_KEY:
767                                 io2.t2fnext.in.resume_key = extract_resume_key(&result->list[result->count-1],
768                                                                                io2.t2fnext.level, io2.t2fnext.data_level);
769                                 if (io2.t2fnext.in.resume_key == 0) {
770                                         printf("Server does not support resume by key for level %s\n",
771                                                level_name(io2.t2fnext.level, io2.t2fnext.data_level));
772                                         return NT_STATUS_NOT_SUPPORTED;
773                                 }
774                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
775                                         FLAG_TRANS2_FIND_BACKUP_INTENT;
776                                 break;
777                         case CONT_NAME:
778                                 io2.t2fnext.in.last_name = extract_name(&result->list[result->count-1],
779                                                                         io2.t2fnext.level, io2.t2fnext.data_level);
780                                 break;
781                         case CONT_FLAGS:
782                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_CONTINUE;
783                                 break;
784                         }
785                 }
786
787                 status = smb_raw_search_next(cli->tree, tctx,
788                                              &io2, data, multiple_search_callback);
789                 if (!NT_STATUS_IS_OK(status)) {
790                         break;
791                 }
792                 if (data_level == RAW_SEARCH_DATA_SEARCH) {
793                         if (io2.search_next.out.count == 0) {
794                                 break;
795                         }
796                 } else if (io2.t2fnext.out.count == 0 ||
797                            io2.t2fnext.out.end_of_search) {
798                         break;
799                 }
800         }
801
802         return status;
803 }
804
805 #define CHECK_STATUS(status, correct) torture_assert_ntstatus_equal(tctx, status, correct, "incorrect status")
806
807 #define CHECK_VALUE(v, correct) torture_assert_int_equal(tctx, (v), (correct), "incorrect value");
808
809 #define CHECK_STRING(v, correct) torture_assert_casestr_equal(tctx, v, correct, "incorrect value");
810
811
812 static enum smb_search_data_level compare_data_level;
813
814 static int search_compare(union smb_search_data *d1, union smb_search_data *d2)
815 {
816         const char *s1, *s2;
817         enum smb_search_level level;
818
819         if (compare_data_level == RAW_SEARCH_DATA_SEARCH) {
820                 level = RAW_SEARCH_SEARCH;
821         } else {
822                 level = RAW_SEARCH_TRANS2;
823         }
824
825         s1 = extract_name(d1, level, compare_data_level);
826         s2 = extract_name(d2, level, compare_data_level);
827         return strcmp_safe(s1, s2);
828 }
829
830
831
832 /* 
833    basic testing of search calls using many files
834 */
835 static bool test_many_files(struct torture_context *tctx, 
836                             struct smbcli_state *cli)
837 {
838         const int num_files = 700;
839         int i, fnum, t;
840         char *fname;
841         bool ret = true;
842         NTSTATUS status;
843         struct multiple_result result;
844         struct {
845                 const char *name;
846                 const char *cont_name;
847                 enum smb_search_data_level data_level;
848                 enum continue_type cont_type;
849         } search_types[] = {
850                 {"SEARCH",              "ID",    RAW_SEARCH_DATA_SEARCH,              CONT_RESUME_KEY},
851                 {"BOTH_DIRECTORY_INFO", "NAME",  RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_NAME},
852                 {"BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_FLAGS},
853                 {"BOTH_DIRECTORY_INFO", "KEY",   RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY},
854                 {"STANDARD",            "FLAGS", RAW_SEARCH_DATA_STANDARD,            CONT_FLAGS},
855                 {"STANDARD",            "KEY",   RAW_SEARCH_DATA_STANDARD,            CONT_RESUME_KEY},
856                 {"STANDARD",            "NAME",  RAW_SEARCH_DATA_STANDARD,            CONT_NAME},
857                 {"EA_SIZE",             "FLAGS", RAW_SEARCH_DATA_EA_SIZE,             CONT_FLAGS},
858                 {"EA_SIZE",             "KEY",   RAW_SEARCH_DATA_EA_SIZE,             CONT_RESUME_KEY},
859                 {"EA_SIZE",             "NAME",  RAW_SEARCH_DATA_EA_SIZE,             CONT_NAME},
860                 {"DIRECTORY_INFO",      "FLAGS", RAW_SEARCH_DATA_DIRECTORY_INFO,      CONT_FLAGS},
861                 {"DIRECTORY_INFO",      "KEY",   RAW_SEARCH_DATA_DIRECTORY_INFO,      CONT_RESUME_KEY},
862                 {"DIRECTORY_INFO",      "NAME",  RAW_SEARCH_DATA_DIRECTORY_INFO,      CONT_NAME},
863                 {"FULL_DIRECTORY_INFO",    "FLAGS", RAW_SEARCH_DATA_FULL_DIRECTORY_INFO,    CONT_FLAGS},
864                 {"FULL_DIRECTORY_INFO",    "KEY",   RAW_SEARCH_DATA_FULL_DIRECTORY_INFO,    CONT_RESUME_KEY},
865                 {"FULL_DIRECTORY_INFO",    "NAME",  RAW_SEARCH_DATA_FULL_DIRECTORY_INFO,    CONT_NAME},
866                 {"ID_FULL_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_FLAGS},
867                 {"ID_FULL_DIRECTORY_INFO", "KEY",   RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_RESUME_KEY},
868                 {"ID_FULL_DIRECTORY_INFO", "NAME",  RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO, CONT_NAME},
869                 {"ID_BOTH_DIRECTORY_INFO", "NAME",  RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_NAME},
870                 {"ID_BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_FLAGS},
871                 {"ID_BOTH_DIRECTORY_INFO", "KEY",   RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY}
872         };
873
874         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
875
876         torture_comment(tctx, "Testing with %d files\n", num_files);
877
878         for (i=0;i<num_files;i++) {
879                 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
880                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
881                 torture_assert(tctx, fnum != -1, "Failed to create");
882                 talloc_free(fname);
883                 smbcli_close(cli->tree, fnum);
884         }
885
886
887         for (t=0;t<ARRAY_SIZE(search_types);t++) {
888                 ZERO_STRUCT(result);
889
890                 if ((search_types[t].cont_type == CONT_RESUME_KEY) &&
891                     (search_types[t].data_level != RAW_SEARCH_DATA_SEARCH) &&
892                     !torture_setting_bool(tctx, "resume_key_support", true)) {
893                         torture_comment(tctx,
894                                         "SKIP: Continue %s via %s\n",
895                                         search_types[t].name, search_types[t].cont_name);
896                         continue;
897                 }
898
899                 result.tctx = talloc_new(tctx);
900         
901                 torture_comment(tctx,
902                                 "Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
903
904                 status = multiple_search(cli, tctx, BASEDIR "\\*.*", 
905                                          search_types[t].data_level,
906                                          search_types[t].cont_type,
907                                          &result);
908                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
909                         torture_warning(tctx, "search level %s not supported "
910                                         "by server",
911                                         search_types[t].name);
912                         continue;
913                 }
914                 torture_assert_ntstatus_ok(tctx, status, "search failed");
915                 CHECK_VALUE(result.count, num_files);
916
917                 compare_data_level = search_types[t].data_level;
918
919                 TYPESAFE_QSORT(result.list, result.count, search_compare);
920
921                 for (i=0;i<result.count;i++) {
922                         const char *s;
923                         enum smb_search_level level;
924                         if (compare_data_level == RAW_SEARCH_DATA_SEARCH) {
925                                 level = RAW_SEARCH_SEARCH;
926                         } else {
927                                 level = RAW_SEARCH_TRANS2;
928                         }
929                         s = extract_name(&result.list[i], level, compare_data_level);
930                         fname = talloc_asprintf(cli, "t%03d-%d.txt", i, i);
931                         torture_assert_str_equal(tctx, fname, s, "Incorrect name");
932                         talloc_free(fname);
933                 }
934                 talloc_free(result.tctx);
935         }
936
937         smb_raw_exit(cli->session);
938         smbcli_deltree(cli->tree, BASEDIR);
939
940         return ret;
941 }
942
943 /*
944   check a individual file result
945 */
946 static bool check_result(struct multiple_result *result, const char *name, bool exist, uint32_t attrib)
947 {
948         int i;
949         for (i=0;i<result->count;i++) {
950                 if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
951         }
952         if (i == result->count) {
953                 if (exist) {
954                         printf("failed: '%s' should exist with attribute %s\n", 
955                                name, attrib_string(result->list, attrib));
956                         return false;
957                 }
958                 return true;
959         }
960
961         if (!exist) {
962                 printf("failed: '%s' should NOT exist (has attribute %s)\n", 
963                        name, attrib_string(result->list, result->list[i].both_directory_info.attrib));
964                 return false;
965         }
966
967         if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
968                 printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
969                        name, 
970                        attrib, result->list[i].both_directory_info.attrib);
971                 return false;
972         }
973         return true;
974 }
975
976 /* 
977    test what happens when the directory is modified during a search
978 */
979 static bool test_modify_search(struct torture_context *tctx, 
980                                                            struct smbcli_state *cli)
981 {
982         const int num_files = 20;
983         int i, fnum;
984         char *fname;
985         bool ret = true;
986         NTSTATUS status;
987         struct multiple_result result;
988         union smb_search_first io;
989         union smb_search_next io2;
990         union smb_setfileinfo sfinfo;
991
992         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
993
994         printf("Creating %d files\n", num_files);
995
996         for (i=num_files-1;i>=0;i--) {
997                 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
998                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
999                 if (fnum == -1) {
1000                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1001                         ret = false;
1002                         goto done;
1003                 }
1004                 talloc_free(fname);
1005                 smbcli_close(cli->tree, fnum);
1006         }
1007
1008         printf("pulling the first file\n");
1009         ZERO_STRUCT(result);
1010         result.tctx = talloc_new(tctx);
1011
1012         io.t2ffirst.level = RAW_SEARCH_TRANS2;
1013         io.t2ffirst.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1014         io.t2ffirst.in.search_attrib = 0;
1015         io.t2ffirst.in.max_count = 0;
1016         io.t2ffirst.in.flags = 0;
1017         io.t2ffirst.in.storage_type = 0;
1018         io.t2ffirst.in.pattern = BASEDIR "\\*.*";
1019
1020         status = smb_raw_search_first(cli->tree, tctx,
1021                                       &io, &result, multiple_search_callback);
1022         CHECK_STATUS(status, NT_STATUS_OK);
1023         CHECK_VALUE(result.count, 1);
1024         
1025         printf("pulling the second file\n");
1026         io2.t2fnext.level = RAW_SEARCH_TRANS2;
1027         io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1028         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1029         io2.t2fnext.in.max_count = 1;
1030         io2.t2fnext.in.resume_key = 0;
1031         io2.t2fnext.in.flags = 0;
1032         io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
1033
1034         status = smb_raw_search_next(cli->tree, tctx,
1035                                      &io2, &result, multiple_search_callback);
1036         CHECK_STATUS(status, NT_STATUS_OK);
1037         CHECK_VALUE(result.count, 2);
1038
1039         result.count = 0;
1040
1041         printf("Changing attributes and deleting\n");
1042         smbcli_open(cli->tree, BASEDIR "\\T003-03.txt.2", O_CREAT|O_RDWR, DENY_NONE);
1043         smbcli_open(cli->tree, BASEDIR "\\T013-13.txt.2", O_CREAT|O_RDWR, DENY_NONE);
1044         fnum = create_complex_file(cli, tctx, BASEDIR "\\T013-13.txt.3");
1045         smbcli_unlink(cli->tree, BASEDIR "\\T014-14.txt");
1046         torture_set_file_attribute(cli->tree, BASEDIR "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
1047         torture_set_file_attribute(cli->tree, BASEDIR "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
1048         torture_set_file_attribute(cli->tree, BASEDIR "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);  
1049         torture_set_file_attribute(cli->tree, BASEDIR "\\T018-18.txt", 0);      
1050         sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
1051         sfinfo.generic.in.file.fnum = fnum;
1052         sfinfo.disposition_info.in.delete_on_close = 1;
1053         status = smb_raw_setfileinfo(cli->tree, &sfinfo);
1054         CHECK_STATUS(status, NT_STATUS_OK);
1055
1056         io2.t2fnext.level = RAW_SEARCH_TRANS2;
1057         io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1058         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1059         io2.t2fnext.in.max_count = num_files + 3;
1060         io2.t2fnext.in.resume_key = 0;
1061         io2.t2fnext.in.flags = 0;
1062         io2.t2fnext.in.last_name = ".";
1063
1064         status = smb_raw_search_next(cli->tree, tctx,
1065                                      &io2, &result, multiple_search_callback);
1066         CHECK_STATUS(status, NT_STATUS_OK);
1067         CHECK_VALUE(result.count, 20);
1068
1069         ret &= check_result(&result, "t009-9.txt", true, FILE_ATTRIBUTE_ARCHIVE);
1070         ret &= check_result(&result, "t014-14.txt", false, 0);
1071         ret &= check_result(&result, "t015-15.txt", false, 0);
1072         ret &= check_result(&result, "t016-16.txt", true, FILE_ATTRIBUTE_NORMAL);
1073         ret &= check_result(&result, "t017-17.txt", false, 0);
1074         ret &= check_result(&result, "t018-18.txt", true, FILE_ATTRIBUTE_ARCHIVE);
1075         ret &= check_result(&result, "t019-19.txt", true, FILE_ATTRIBUTE_ARCHIVE);
1076         ret &= check_result(&result, "T013-13.txt.2", true, FILE_ATTRIBUTE_ARCHIVE);
1077         ret &= check_result(&result, "T003-3.txt.2", false, 0);
1078         ret &= check_result(&result, "T013-13.txt.3", true, FILE_ATTRIBUTE_ARCHIVE);
1079
1080         if (!ret) {
1081                 for (i=0;i<result.count;i++) {
1082                         printf("%s %s (0x%x)\n", 
1083                                result.list[i].both_directory_info.name.s, 
1084                                attrib_string(tctx, result.list[i].both_directory_info.attrib),
1085                                result.list[i].both_directory_info.attrib);
1086                 }
1087         }
1088
1089 done:
1090         smb_raw_exit(cli->session);
1091         smbcli_deltree(cli->tree, BASEDIR);
1092
1093         return ret;
1094 }
1095
1096
1097 /* 
1098    testing if directories always come back sorted
1099 */
1100 static bool test_sorted(struct torture_context *tctx, struct smbcli_state *cli)
1101 {
1102         const int num_files = 700;
1103         int i, fnum;
1104         char *fname;
1105         bool ret = true;
1106         NTSTATUS status;
1107         struct multiple_result result;
1108
1109         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1110
1111         printf("Creating %d files\n", num_files);
1112
1113         for (i=0;i<num_files;i++) {
1114                 fname = talloc_asprintf(cli, BASEDIR "\\%s.txt", generate_random_str_list(tctx, 10, "abcdefgh"));
1115                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1116                 if (fnum == -1) {
1117                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1118                         ret = false;
1119                         goto done;
1120                 }
1121                 talloc_free(fname);
1122                 smbcli_close(cli->tree, fnum);
1123         }
1124
1125
1126         ZERO_STRUCT(result);
1127         result.tctx = tctx;
1128         
1129         status = multiple_search(cli, tctx, BASEDIR "\\*.*", 
1130                                  RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
1131                                  CONT_NAME, &result);   
1132         CHECK_STATUS(status, NT_STATUS_OK);
1133         CHECK_VALUE(result.count, num_files);
1134
1135         for (i=0;i<num_files-1;i++) {
1136                 const char *name1, *name2;
1137                 name1 = result.list[i].both_directory_info.name.s;
1138                 name2 = result.list[i+1].both_directory_info.name.s;
1139                 if (strcasecmp_m(name1, name2) > 0) {
1140                         printf("non-alphabetical order at entry %d  '%s' '%s'\n", 
1141                                i, name1, name2);
1142                         printf("Server does not produce sorted directory listings (not an error)\n");
1143                         goto done;
1144                 }
1145         }
1146
1147         talloc_free(result.list);
1148
1149 done:
1150         smb_raw_exit(cli->session);
1151         smbcli_deltree(cli->tree, BASEDIR);
1152
1153         return ret;
1154 }
1155
1156
1157
1158 /* 
1159    basic testing of many old style search calls using separate dirs
1160 */
1161 static bool test_many_dirs(struct torture_context *tctx, 
1162                                                    struct smbcli_state *cli)
1163 {
1164         const int num_dirs = 20;
1165         int i, fnum, n;
1166         char *fname, *dname;
1167         bool ret = true;
1168         NTSTATUS status;
1169         union smb_search_data *file, *file2, *file3;
1170
1171         if (!torture_setting_bool(tctx, "raw_search_search", true)) {
1172                 torture_comment(tctx, "Skipping these tests as the server "
1173                         "doesn't support old style search calls\n");
1174                 return true;
1175         }
1176         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1177
1178         printf("Creating %d dirs\n", num_dirs);
1179
1180         for (i=0;i<num_dirs;i++) {
1181                 dname = talloc_asprintf(cli, BASEDIR "\\d%d", i);
1182                 status = smbcli_mkdir(cli->tree, dname);
1183                 if (!NT_STATUS_IS_OK(status)) {
1184                         printf("(%s) Failed to create %s - %s\n", 
1185                                __location__, dname, nt_errstr(status));
1186                         ret = false;
1187                         goto done;
1188                 }
1189
1190                 for (n=0;n<3;n++) {
1191                         fname = talloc_asprintf(cli, BASEDIR "\\d%d\\f%d-%d.txt", i, i, n);
1192                         fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1193                         if (fnum == -1) {
1194                                 printf("(%s) Failed to create %s - %s\n", 
1195                                        __location__, fname, smbcli_errstr(cli->tree));
1196                                 ret = false;
1197                                 goto done;
1198                         }
1199                         talloc_free(fname);
1200                         smbcli_close(cli->tree, fnum);
1201                 }
1202
1203                 talloc_free(dname);
1204         }
1205
1206         file  = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1207         file2 = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1208         file3 = talloc_zero_array(tctx, union smb_search_data, num_dirs);
1209
1210         printf("Search first on %d dirs\n", num_dirs);
1211
1212         for (i=0;i<num_dirs;i++) {
1213                 union smb_search_first io;
1214                 io.search_first.level = RAW_SEARCH_SEARCH;
1215                 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
1216                 io.search_first.in.max_count = 1;
1217                 io.search_first.in.search_attrib = 0;
1218                 io.search_first.in.pattern = talloc_asprintf(tctx, BASEDIR "\\d%d\\*.txt", i);
1219                 fname = talloc_asprintf(tctx, "f%d-", i);
1220
1221                 io.search_first.out.count = 0;
1222
1223                 status = smb_raw_search_first(cli->tree, tctx,
1224                                               &io, (void *)&file[i], single_search_callback);
1225                 if (io.search_first.out.count != 1) {
1226                         printf("(%s) search first gave %d entries for dir %d - %s\n",
1227                                __location__, io.search_first.out.count, i, nt_errstr(status));
1228                         ret = false;
1229                         goto done;
1230                 }
1231                 CHECK_STATUS(status, NT_STATUS_OK);
1232                 if (strncasecmp(file[i].search.name, fname, strlen(fname)) != 0) {
1233                         printf("(%s) incorrect name '%s' expected '%s'[12].txt\n", 
1234                                __location__, file[i].search.name, fname);
1235                         ret = false;
1236                         goto done;
1237                 }
1238
1239                 talloc_free(fname);
1240         }
1241
1242         printf("Search next on %d dirs\n", num_dirs);
1243
1244         for (i=0;i<num_dirs;i++) {
1245                 union smb_search_next io2;
1246
1247                 io2.search_next.level = RAW_SEARCH_SEARCH;
1248                 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
1249                 io2.search_next.in.max_count = 1;
1250                 io2.search_next.in.search_attrib = 0;
1251                 io2.search_next.in.id = file[i].search.id;
1252                 fname = talloc_asprintf(tctx, "f%d-", i);
1253
1254                 io2.search_next.out.count = 0;
1255
1256                 status = smb_raw_search_next(cli->tree, tctx,
1257                                              &io2, (void *)&file2[i], single_search_callback);
1258                 if (io2.search_next.out.count != 1) {
1259                         printf("(%s) search next gave %d entries for dir %d - %s\n",
1260                                __location__, io2.search_next.out.count, i, nt_errstr(status));
1261                         ret = false;
1262                         goto done;
1263                 }
1264                 CHECK_STATUS(status, NT_STATUS_OK);
1265                 if (strncasecmp(file2[i].search.name, fname, strlen(fname)) != 0) {
1266                         printf("(%s) incorrect name '%s' expected '%s'[12].txt\n", 
1267                                __location__, file2[i].search.name, fname);
1268                         ret = false;
1269                         goto done;
1270                 }
1271
1272                 talloc_free(fname);
1273         }
1274
1275
1276         printf("Search next (rewind) on %d dirs\n", num_dirs);
1277
1278         for (i=0;i<num_dirs;i++) {
1279                 union smb_search_next io2;
1280
1281                 io2.search_next.level = RAW_SEARCH_SEARCH;
1282                 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
1283                 io2.search_next.in.max_count = 1;
1284                 io2.search_next.in.search_attrib = 0;
1285                 io2.search_next.in.id = file[i].search.id;
1286                 fname = talloc_asprintf(tctx, "f%d-", i);
1287                 io2.search_next.out.count = 0;
1288
1289                 status = smb_raw_search_next(cli->tree, tctx,
1290                                              &io2, (void *)&file3[i], single_search_callback);
1291                 if (io2.search_next.out.count != 1) {
1292                         printf("(%s) search next gave %d entries for dir %d - %s\n",
1293                                __location__, io2.search_next.out.count, i, nt_errstr(status));
1294                         ret = false;
1295                         goto done;
1296                 }
1297                 CHECK_STATUS(status, NT_STATUS_OK);
1298
1299                 if (strncasecmp(file3[i].search.name, file2[i].search.name, 3) != 0) {
1300                         printf("(%s) incorrect name '%s' on rewind at dir %d\n", 
1301                                __location__, file2[i].search.name, i);
1302                         ret = false;
1303                         goto done;
1304                 }
1305
1306                 if (torture_setting_bool(tctx, "rewind_support", true) &&
1307                     strcmp(file3[i].search.name, file2[i].search.name) != 0) {
1308                         printf("(%s) server did not rewind - got '%s' expected '%s'\n", 
1309                                __location__, file3[i].search.name, file2[i].search.name);
1310                         ret = false;
1311                         goto done;
1312                 }
1313
1314                 talloc_free(fname);
1315         }
1316
1317
1318 done:
1319         smb_raw_exit(cli->session);
1320         smbcli_deltree(cli->tree, BASEDIR);
1321
1322         return ret;
1323 }
1324
1325
1326 /* 
1327    testing of OS/2 style delete
1328 */
1329 static bool test_os2_delete(struct torture_context *tctx, 
1330                                                         struct smbcli_state *cli)
1331 {
1332         const int num_files = 700;
1333         const int delete_count = 4;
1334         int total_deleted = 0;
1335         int i, fnum;
1336         char *fname;
1337         bool ret = true;
1338         NTSTATUS status;
1339         union smb_search_first io;
1340         union smb_search_next io2;
1341         struct multiple_result result;
1342
1343         if (!torture_setting_bool(tctx, "search_ea_size", true)){
1344                 torture_comment(tctx,
1345                                 "Server does not support RAW_SEARCH_EA_SIZE "
1346                                 "level. Skipping this test\n");
1347                 return true;
1348         }
1349
1350         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1351
1352         printf("Testing OS/2 style delete on %d files\n", num_files);
1353
1354         for (i=0;i<num_files;i++) {
1355                 fname = talloc_asprintf(cli, BASEDIR "\\file%u.txt", i);
1356                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1357                 if (fnum == -1) {
1358                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1359                         ret = false;
1360                         goto done;
1361                 }
1362                 talloc_free(fname);
1363                 smbcli_close(cli->tree, fnum);
1364         }
1365
1366
1367         ZERO_STRUCT(result);
1368         result.tctx = tctx;
1369
1370         io.t2ffirst.level = RAW_SEARCH_TRANS2;
1371         io.t2ffirst.data_level = RAW_SEARCH_DATA_EA_SIZE;
1372         io.t2ffirst.in.search_attrib = 0;
1373         io.t2ffirst.in.max_count = 100;
1374         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1375         io.t2ffirst.in.storage_type = 0;
1376         io.t2ffirst.in.pattern = BASEDIR "\\*";
1377
1378         status = smb_raw_search_first(cli->tree, tctx,
1379                                       &io, &result, multiple_search_callback);
1380         CHECK_STATUS(status, NT_STATUS_OK);
1381
1382         for (i=0;i<MIN(result.count, delete_count);i++) {
1383                 fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
1384                 status = smbcli_unlink(cli->tree, fname);
1385                 CHECK_STATUS(status, NT_STATUS_OK);
1386                 total_deleted++;
1387                 talloc_free(fname);
1388         }
1389
1390         io2.t2fnext.level = RAW_SEARCH_TRANS2;
1391         io2.t2fnext.data_level = RAW_SEARCH_DATA_EA_SIZE;
1392         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1393         io2.t2fnext.in.max_count = 100;
1394         io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1395         io2.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1396         io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1397
1398         do {
1399                 ZERO_STRUCT(result);
1400                 result.tctx = tctx;
1401
1402                 status = smb_raw_search_next(cli->tree, tctx,
1403                                              &io2, &result, multiple_search_callback);
1404                 if (!NT_STATUS_IS_OK(status)) {
1405                         break;
1406                 }
1407
1408                 for (i=0;i<MIN(result.count, delete_count);i++) {
1409                         fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
1410                         status = smbcli_unlink(cli->tree, fname);
1411                         CHECK_STATUS(status, NT_STATUS_OK);
1412                         total_deleted++;
1413                         talloc_free(fname);
1414                 }
1415
1416                 if (i>0) {
1417                         io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
1418                         io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
1419                 }
1420         } while (NT_STATUS_IS_OK(status) && result.count != 0);
1421
1422         CHECK_STATUS(status, NT_STATUS_OK);
1423
1424         if (total_deleted != num_files) {
1425                 printf("error: deleted %d - expected to delete %d\n", 
1426                        total_deleted, num_files);
1427                 ret = false;
1428         }
1429
1430 done:
1431         smb_raw_exit(cli->session);
1432         smbcli_deltree(cli->tree, BASEDIR);
1433
1434         return ret;
1435 }
1436
1437
1438 static int ealist_cmp(union smb_search_data *r1, union smb_search_data *r2)
1439 {
1440         return strcmp(r1->ea_list.name.s, r2->ea_list.name.s);
1441 }
1442
1443 /* 
1444    testing of the rather strange ea_list level
1445 */
1446 static bool test_ea_list(struct torture_context *tctx, 
1447                                                  struct smbcli_state *cli)
1448 {
1449         int  fnum;
1450         bool ret = true;
1451         NTSTATUS status;
1452         union smb_search_first io;
1453         union smb_search_next nxt;
1454         struct multiple_result result;
1455         union smb_setfileinfo setfile;
1456
1457         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1458
1459         printf("Testing RAW_SEARCH_EA_LIST level\n");
1460
1461         if (!torture_setting_bool(tctx, "search_ea_support", true) ||
1462             !torture_setting_bool(tctx, "ea_support", true)) {
1463                 printf("..skipped per target configuration.\n");
1464                 return true;
1465         }
1466
1467         fnum = smbcli_open(cli->tree, BASEDIR "\\file1.txt", O_CREAT|O_RDWR, DENY_NONE);
1468         smbcli_close(cli->tree, fnum);
1469
1470         fnum = smbcli_open(cli->tree, BASEDIR "\\file2.txt", O_CREAT|O_RDWR, DENY_NONE);
1471         smbcli_close(cli->tree, fnum);
1472
1473         fnum = smbcli_open(cli->tree, BASEDIR "\\file3.txt", O_CREAT|O_RDWR, DENY_NONE);
1474         smbcli_close(cli->tree, fnum);
1475
1476         setfile.generic.level = RAW_SFILEINFO_EA_SET;
1477         setfile.generic.in.file.path = BASEDIR "\\file2.txt";
1478         setfile.ea_set.in.num_eas = 2;
1479         setfile.ea_set.in.eas = talloc_array(tctx, struct ea_struct, 2);
1480         setfile.ea_set.in.eas[0].flags = 0;
1481         setfile.ea_set.in.eas[0].name.s = "EA ONE";
1482         setfile.ea_set.in.eas[0].value = data_blob_string_const("VALUE 1");
1483         setfile.ea_set.in.eas[1].flags = 0;
1484         setfile.ea_set.in.eas[1].name.s = "SECOND EA";
1485         setfile.ea_set.in.eas[1].value = data_blob_string_const("Value Two");
1486
1487         status = smb_raw_setpathinfo(cli->tree, &setfile);
1488         CHECK_STATUS(status, NT_STATUS_OK);
1489
1490         setfile.generic.in.file.path = BASEDIR "\\file3.txt";
1491         status = smb_raw_setpathinfo(cli->tree, &setfile);
1492         CHECK_STATUS(status, NT_STATUS_OK);
1493         
1494         ZERO_STRUCT(result);
1495         result.tctx = tctx;
1496
1497         io.t2ffirst.level = RAW_SEARCH_TRANS2;
1498         io.t2ffirst.data_level = RAW_SEARCH_DATA_EA_LIST;
1499         io.t2ffirst.in.search_attrib = 0;
1500         io.t2ffirst.in.max_count = 2;
1501         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
1502         io.t2ffirst.in.storage_type = 0;
1503         io.t2ffirst.in.pattern = BASEDIR "\\*";
1504         io.t2ffirst.in.num_names = 2;
1505         io.t2ffirst.in.ea_names = talloc_array(tctx, struct ea_name, 2);
1506         io.t2ffirst.in.ea_names[0].name.s = "SECOND EA";
1507         io.t2ffirst.in.ea_names[1].name.s = "THIRD EA";
1508
1509         status = smb_raw_search_first(cli->tree, tctx,
1510                                       &io, &result, multiple_search_callback);
1511         CHECK_STATUS(status, NT_STATUS_OK);
1512         CHECK_VALUE(result.count, 2);
1513
1514         nxt.t2fnext.level = RAW_SEARCH_TRANS2;
1515         nxt.t2fnext.data_level = RAW_SEARCH_DATA_EA_LIST;
1516         nxt.t2fnext.in.handle = io.t2ffirst.out.handle;
1517         nxt.t2fnext.in.max_count = 2;
1518         nxt.t2fnext.in.resume_key = result.list[1].ea_list.resume_key;
1519         nxt.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME | FLAG_TRANS2_FIND_CONTINUE;
1520         nxt.t2fnext.in.last_name = result.list[1].ea_list.name.s;
1521         nxt.t2fnext.in.num_names = 2;
1522         nxt.t2fnext.in.ea_names = talloc_array(tctx, struct ea_name, 2);
1523         nxt.t2fnext.in.ea_names[0].name.s = "SECOND EA";
1524         nxt.t2fnext.in.ea_names[1].name.s = "THIRD EA";
1525
1526         status = smb_raw_search_next(cli->tree, tctx,
1527                                      &nxt, &result, multiple_search_callback);
1528         CHECK_STATUS(status, NT_STATUS_OK);
1529
1530         /* we have to sort the result as different servers can return directories
1531            in different orders */
1532         TYPESAFE_QSORT(result.list, result.count, ealist_cmp);
1533
1534         CHECK_VALUE(result.count, 3);
1535         CHECK_VALUE(result.list[0].ea_list.eas.num_eas, 2);
1536         CHECK_STRING(result.list[0].ea_list.name.s, "file1.txt");
1537         CHECK_STRING(result.list[0].ea_list.eas.eas[0].name.s, "SECOND EA");
1538         CHECK_VALUE(result.list[0].ea_list.eas.eas[0].value.length, 0);
1539         CHECK_STRING(result.list[0].ea_list.eas.eas[1].name.s, "THIRD EA");
1540         CHECK_VALUE(result.list[0].ea_list.eas.eas[1].value.length, 0);
1541
1542         CHECK_STRING(result.list[1].ea_list.name.s, "file2.txt");
1543         CHECK_STRING(result.list[1].ea_list.eas.eas[0].name.s, "SECOND EA");
1544         CHECK_VALUE(result.list[1].ea_list.eas.eas[0].value.length, 9);
1545         CHECK_STRING((const char *)result.list[1].ea_list.eas.eas[0].value.data, "Value Two");
1546         CHECK_STRING(result.list[1].ea_list.eas.eas[1].name.s, "THIRD EA");
1547         CHECK_VALUE(result.list[1].ea_list.eas.eas[1].value.length, 0);
1548
1549         CHECK_STRING(result.list[2].ea_list.name.s, "file3.txt");
1550         CHECK_STRING(result.list[2].ea_list.eas.eas[0].name.s, "SECOND EA");
1551         CHECK_VALUE(result.list[2].ea_list.eas.eas[0].value.length, 9);
1552         CHECK_STRING((const char *)result.list[2].ea_list.eas.eas[0].value.data, "Value Two");
1553         CHECK_STRING(result.list[2].ea_list.eas.eas[1].name.s, "THIRD EA");
1554         CHECK_VALUE(result.list[2].ea_list.eas.eas[1].value.length, 0);
1555
1556         smb_raw_exit(cli->session);
1557         smbcli_deltree(cli->tree, BASEDIR);
1558
1559         return ret;
1560 }
1561
1562 /*
1563  Test the behavior of max count parameter in TRANS2_FIND_FIRST2 and
1564  TRANS2_FIND_NEXT2 queries
1565 */
1566 static bool test_max_count(struct torture_context *tctx,
1567                            struct smbcli_state *cli)
1568 {
1569         const int num_files = 2;
1570         int i, fnum;
1571         char *fname;
1572         bool ret = true;
1573         NTSTATUS status;
1574         struct multiple_result result;
1575         union smb_search_first io;
1576         union smb_search_next io2;
1577
1578         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1579
1580         torture_comment(tctx, "Creating %d files\n", num_files);
1581
1582         for (i=num_files-1;i>=0;i--) {
1583                 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
1584                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
1585                 if (fnum == -1) {
1586                         torture_comment(tctx,
1587                                 "Failed to create %s - %s\n",
1588                                 fname, smbcli_errstr(cli->tree));
1589                         ret = false;
1590                         goto done;
1591                 }
1592                 talloc_free(fname);
1593                 smbcli_close(cli->tree, fnum);
1594         }
1595
1596         torture_comment(tctx, "Set max_count parameter to 0. "
1597                         "This should return 1 entry\n");
1598         ZERO_STRUCT(result);
1599         result.tctx = talloc_new(tctx);
1600
1601         io.t2ffirst.level = RAW_SEARCH_TRANS2;
1602         io.t2ffirst.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1603         io.t2ffirst.in.search_attrib = 0;
1604         io.t2ffirst.in.max_count = 0;
1605         io.t2ffirst.in.flags = 0;
1606         io.t2ffirst.in.storage_type = 0;
1607         io.t2ffirst.in.pattern = BASEDIR "\\*.*";
1608
1609         status = smb_raw_search_first(cli->tree, tctx,
1610                                       &io, &result, multiple_search_callback);
1611         CHECK_STATUS(status, NT_STATUS_OK);
1612         CHECK_VALUE(result.count, 1);
1613
1614         torture_comment(tctx, "Set max_count to 1. This should also "
1615                         "return 1 entry\n");
1616         io2.t2fnext.level = RAW_SEARCH_TRANS2;
1617         io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
1618         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
1619         io2.t2fnext.in.max_count = 1;
1620         io2.t2fnext.in.resume_key = 0;
1621         io2.t2fnext.in.flags = 0;
1622         io2.t2fnext.in.last_name =
1623                 result.list[result.count-1].both_directory_info.name.s;
1624
1625         status = smb_raw_search_next(cli->tree, tctx,
1626                                      &io2, &result, multiple_search_callback);
1627         CHECK_STATUS(status, NT_STATUS_OK);
1628         CHECK_VALUE(result.count, 2);
1629 done:
1630         smb_raw_exit(cli->session);
1631         smbcli_deltree(cli->tree, BASEDIR);
1632
1633         return ret;
1634 }
1635
1636 /* 
1637    basic testing of all RAW_SEARCH_* calls using a single file
1638 */
1639 struct torture_suite *torture_raw_search(TALLOC_CTX *mem_ctx)
1640 {
1641         struct torture_suite *suite = torture_suite_create(mem_ctx, "search");
1642
1643         torture_suite_add_2smb_test(suite, "one file search", test_one_file);
1644         torture_suite_add_1smb_test(suite, "many files", test_many_files);
1645         torture_suite_add_1smb_test(suite, "sorted", test_sorted);
1646         torture_suite_add_1smb_test(suite, "modify search", test_modify_search);
1647         torture_suite_add_1smb_test(suite, "many dirs", test_many_dirs);
1648         torture_suite_add_1smb_test(suite, "os2 delete", test_os2_delete);
1649         torture_suite_add_1smb_test(suite, "ea list", test_ea_list);
1650         torture_suite_add_1smb_test(suite, "max count", test_max_count);
1651
1652         return suite;
1653 }