LCOV - code coverage report
Current view: top level - source4/torture/raw - search.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 670 832 80.5 %
Date: 2024-04-21 15:09:00 Functions: 20 21 95.2 %

          Line data    Source code
       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        1112 : static bool single_search_callback(void *private_data, const union smb_search_data *file)
      57             : {
      58        1112 :         union smb_search_data *data = (union smb_search_data *)private_data;
      59             : 
      60        1112 :         *data = *file;
      61             : 
      62        1112 :         return true;
      63             : }
      64             : 
      65             : /*
      66             :   do a single file (non-wildcard) search 
      67             : */
      68         776 : 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          87 :         union smb_search_first io;
      77          87 :         union smb_search_close c;
      78          87 :         NTSTATUS status;
      79             : 
      80         776 :         switch (level) {
      81          42 :         case RAW_SEARCH_SEARCH:
      82             :         case RAW_SEARCH_FFIRST:
      83             :         case RAW_SEARCH_FUNIQUE:
      84          42 :                 io.search_first.level = level;
      85          42 :                 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
      86          42 :                 io.search_first.in.max_count = 1;
      87          42 :                 io.search_first.in.search_attrib = attrib;
      88          42 :                 io.search_first.in.pattern = pattern;
      89         689 :                 break;
      90             : 
      91         734 :         case RAW_SEARCH_TRANS2:
      92         734 :                 io.t2ffirst.level = RAW_SEARCH_TRANS2;
      93         734 :                 io.t2ffirst.data_level = data_level;
      94         734 :                 io.t2ffirst.in.search_attrib = attrib;
      95         734 :                 io.t2ffirst.in.max_count = 1;
      96         734 :                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
      97         734 :                 io.t2ffirst.in.storage_type = 0;
      98         734 :                 io.t2ffirst.in.pattern = pattern;
      99         734 :                 break;
     100             : 
     101           0 :         case RAW_SEARCH_SMB2:
     102           0 :                 return NT_STATUS_INVALID_LEVEL;
     103             :         }
     104             : 
     105         776 :         status = smb_raw_search_first(cli->tree, tctx,
     106             :                                       &io, (void *)data, single_search_callback);
     107             : 
     108         776 :         if (NT_STATUS_IS_OK(status) && level == RAW_SEARCH_FFIRST) {
     109           7 :                 c.fclose.level = RAW_FINDCLOSE_FCLOSE;
     110           7 :                 c.fclose.in.max_count = 1;
     111           7 :                 c.fclose.in.search_attrib = 0;
     112           7 :                 c.fclose.in.id = data->search.id;
     113           7 :                 status = smb_raw_search_close(cli->tree, &c);
     114             :         }
     115             :         
     116         776 :         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           0 : static const char *level_name(enum smb_search_level level,
     243             :                               enum smb_search_data_level data_level)
     244             : {
     245             :         int i;
     246           0 :         for (i=0;i<ARRAY_SIZE(levels);i++) {
     247           0 :                 if (level == levels[i].level &&
     248           0 :                     data_level == levels[i].data_level) {
     249           0 :                         return levels[i].name;
     250             :                 }
     251             :         }
     252           0 :         return NULL;
     253             : }
     254             : 
     255             : /*
     256             :   extract the name from a smb_data structure and level
     257             : */
     258     1206432 : static const char *extract_name(void *data, enum smb_search_level level,
     259             :                                 enum smb_search_data_level data_level)
     260             : {
     261           0 :         int i;
     262     8631143 :         for (i=0;i<ARRAY_SIZE(levels);i++) {
     263     8631143 :                 if (level == levels[i].level &&
     264     5086589 :                     data_level == levels[i].data_level) {
     265     1206432 :                         return *(const char **)(levels[i].name_offset + (char *)data);
     266             :                 }
     267             :         }
     268           0 :         return NULL;
     269             : }
     270             : 
     271             : /*
     272             :   extract the name from a smb_data structure and level
     273             : */
     274          49 : static uint32_t extract_resume_key(void *data, enum smb_search_level level,
     275             :                                    enum smb_search_data_level data_level)
     276             : {
     277           0 :         int i;
     278         364 :         for (i=0;i<ARRAY_SIZE(levels);i++) {
     279         364 :                 if (level == levels[i].level &&
     280         217 :                     data_level == levels[i].data_level) {
     281          49 :                         return *(uint32_t *)(levels[i].resume_key_offset + (char *)data);
     282             :                 }
     283             :         }
     284           0 :         return 0;
     285             : }
     286             : 
     287             : /* find a level in the table by name */
     288         490 : static union smb_search_data *find(const char *name)
     289             : {
     290           0 :         int i;
     291        3748 :         for (i=0;i<ARRAY_SIZE(levels);i++) {
     292        3745 :                 if (NT_STATUS_IS_OK(levels[i].status) && 
     293        3742 :                     strcmp(levels[i].name, name) == 0) {
     294         487 :                         return &levels[i].data;
     295             :                 }
     296             :         }
     297           3 :         return NULL;
     298             : }
     299             : 
     300             : /*
     301             :  * Negotiate SMB1+POSIX.
     302             :  */
     303             : 
     304           7 : static NTSTATUS setup_smb1_posix(struct torture_context *tctx,
     305             :                                  struct smbcli_state *cli_unix)
     306             : {
     307           0 :         struct smb_trans2 tp;
     308           0 :         uint16_t setup;
     309           0 :         uint8_t data[12];
     310           0 :         uint8_t params[4];
     311           7 :         uint32_t cap = cli_unix->transport->negotiate.capabilities;
     312             : 
     313           7 :         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           1 :                 torture_comment(tctx,
     320             :                         "Server doesn't support SMB1+POSIX\n");
     321           1 :                 return NT_STATUS_OK;
     322             :         }
     323             : 
     324             :         /* Setup POSIX on this connection. */
     325           6 :         SSVAL(data, 0, CIFS_UNIX_MAJOR_VERSION);
     326           6 :         SSVAL(data, 2, CIFS_UNIX_MINOR_VERSION);
     327           6 :         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           6 :         setup = TRANSACT2_SETFSINFO;
     334           6 :         tp.in.max_setup = 0;
     335           6 :         tp.in.flags = 0;
     336           6 :         tp.in.timeout = 0;
     337           6 :         tp.in.setup_count = 1;
     338           6 :         tp.in.max_param = 0;
     339           6 :         tp.in.max_data = 0;
     340           6 :         tp.in.setup = &setup;
     341           6 :         tp.in.trans_name = NULL;
     342           6 :         SSVAL(params, 0, 0);
     343           6 :         SSVAL(params, 2, SMB_SET_CIFS_UNIX_INFO);
     344           6 :         tp.in.params = data_blob_talloc(tctx, params, 4);
     345           6 :         tp.in.data = data_blob_talloc(tctx, data, 12);
     346           6 :         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           7 : static bool test_one_file(struct torture_context *tctx,
     353             :                           struct smbcli_state *cli,
     354             :                           struct smbcli_state *cli_unix)
     355             : {
     356           7 :         bool ret = true;
     357           0 :         int fnum;
     358           7 :         const char *fname = "torture_search.txt";
     359           7 :         const char *fname2 = "torture_search-NOTEXIST.txt";
     360           0 :         NTSTATUS status;
     361           0 :         int i;
     362           0 :         union smb_fileinfo all_info, alt_info, name_info, internal_info;
     363           0 :         bool all_info_supported, alt_info_supported, name_info_supported,
     364             :             internal_info_supported;
     365           0 :         union smb_search_data *s;
     366             : 
     367           7 :         status = setup_smb1_posix(tctx, cli_unix);
     368           7 :         if (!NT_STATUS_IS_OK(status)) {
     369           0 :                 torture_result(tctx,
     370             :                         TORTURE_FAIL,
     371             :                         __location__"setup_smb1_posix() failed (%s)\n",
     372             :                         nt_errstr(status));
     373           0 :                 ret = false;
     374           0 :                 goto done;
     375             :         }
     376             : 
     377           7 :         fnum = create_complex_file(cli, tctx, fname);
     378           7 :         if (fnum == -1) {
     379           0 :                 torture_result(tctx,
     380             :                         TORTURE_FAIL,
     381             :                         __location__"ERROR: open of %s failed (%s)\n",
     382             :                         fname,
     383             :                         smbcli_errstr(cli->tree));
     384           0 :                 ret = false;
     385           0 :                 goto done;
     386             :         }
     387             : 
     388             :         /* call all the levels */
     389          91 :         for (i=0;i<ARRAY_SIZE(levels);i++) {
     390           0 :                 NTSTATUS expected_status;
     391          84 :                 uint32_t cap = cli->transport->negotiate.capabilities;
     392          84 :                 struct smbcli_state *cli_search = cli;
     393             : 
     394          84 :                 torture_comment(tctx, "Testing %s\n", levels[i].name);
     395             : 
     396          84 :                 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           7 :                         cli_search = cli_unix;
     402             :                 }
     403             : 
     404          84 :                 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          84 :                 if (((cap & levels[i].capability_mask) != levels[i].capability_mask)
     412          83 :                     || NT_STATUS_EQUAL(levels[i].status, NT_STATUS_NOT_SUPPORTED)) {
     413           1 :                         printf("search level %s(%d) not supported by server\n",
     414           1 :                                levels[i].name, (int)levels[i].level);
     415           1 :                         continue;
     416             :                 }
     417             : 
     418          83 :                 if (!NT_STATUS_IS_OK(levels[i].status)) {
     419           0 :                         torture_result(tctx,
     420             :                                 TORTURE_FAIL,
     421             :                                 __location__"search level %s(%d) failed - %s\n",
     422           0 :                                 levels[i].name, (int)levels[i].level,
     423             :                                 nt_errstr(levels[i].status));
     424           0 :                         ret = false;
     425           0 :                         continue;
     426             :                 }
     427             : 
     428          83 :                 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          83 :                 expected_status = NT_STATUS_NO_SUCH_FILE;
     435          83 :                 if (levels[i].level == RAW_SEARCH_SEARCH ||
     436          76 :                     levels[i].level == RAW_SEARCH_FFIRST ||
     437          69 :                     levels[i].level == RAW_SEARCH_FUNIQUE) {
     438          21 :                         expected_status = STATUS_NO_MORE_FILES;
     439             :                 }
     440          83 :                 if (!NT_STATUS_EQUAL(status, expected_status)) {
     441           0 :                         torture_result(tctx,
     442             :                                 TORTURE_FAIL,
     443             :                                 __location__"search level %s(%d) should fail with %s - %s\n",
     444           0 :                                levels[i].name, (int)levels[i].level, 
     445             :                                nt_errstr(expected_status),
     446             :                                nt_errstr(status));
     447           0 :                         ret = false;
     448             :                 }
     449             :         }
     450             : 
     451             :         /* get the all_info file into to check against */
     452           7 :         all_info.generic.level = RAW_FILEINFO_ALL_INFO;
     453           7 :         all_info.generic.in.file.path = fname;
     454           7 :         status = smb_raw_pathinfo(cli->tree, tctx, &all_info);
     455           7 :         CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_ALL_INFO",
     456             :             all_info_supported);
     457             : 
     458           7 :         alt_info.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
     459           7 :         alt_info.generic.in.file.path = fname;
     460           7 :         status = smb_raw_pathinfo(cli->tree, tctx, &alt_info);
     461           7 :         CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_ALT_NAME_INFO",
     462             :             alt_info_supported);
     463             : 
     464           7 :         internal_info.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
     465           7 :         internal_info.generic.in.file.path = fname;
     466           7 :         status = smb_raw_pathinfo(cli->tree, tctx, &internal_info);
     467           7 :         CHECK_STATUS_LEVEL(tctx, status, "RAW_FILEINFO_INTERNAL_INFORMATION",
     468             :             internal_info_supported);
     469             : 
     470           7 :         name_info.generic.level = RAW_FILEINFO_NAME_INFO;
     471           7 :         name_info.generic.in.file.path = fname;
     472           7 :         status = smb_raw_pathinfo(cli->tree, tctx, &name_info);
     473           7 :         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           7 :         CHECK_VAL("SEARCH",              search,              attrib, all_info, all_info, attrib&0xFFF);
     581           7 :         CHECK_VAL("STANDARD",            standard,            attrib, all_info, all_info, attrib&0xFFF);
     582           7 :         CHECK_VAL("EA_SIZE",             ea_size,             attrib, all_info, all_info, attrib&0xFFF);
     583           7 :         CHECK_VAL("DIRECTORY_INFO",      directory_info,      attrib, all_info, all_info, attrib);
     584           7 :         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, attrib, all_info, all_info, attrib);
     585           7 :         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, attrib, all_info, all_info, attrib);
     586           7 :         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           attrib, all_info, all_info, attrib);
     587           7 :         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           attrib, all_info, all_info, attrib);
     588             : 
     589           7 :         CHECK_TIME("SEARCH",             search,              write_time, all_info, all_info, write_time);
     590           7 :         CHECK_TIME("STANDARD",           standard,            write_time, all_info, all_info, write_time);
     591           7 :         CHECK_TIME("EA_SIZE",            ea_size,             write_time, all_info, all_info, write_time);
     592           7 :         CHECK_TIME("STANDARD",           standard,            create_time, all_info, all_info, create_time);
     593           7 :         CHECK_TIME("EA_SIZE",            ea_size,             create_time, all_info, all_info, create_time);
     594           7 :         CHECK_TIME("STANDARD",           standard,            access_time, all_info, all_info, access_time);
     595           7 :         CHECK_TIME("EA_SIZE",            ea_size,             access_time, all_info, all_info, access_time);
     596             : 
     597           7 :         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      write_time, all_info, all_info, write_time);
     598           7 :         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, write_time, all_info, all_info, write_time);
     599           7 :         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, write_time, all_info, all_info, write_time);
     600           7 :         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           write_time, all_info, all_info, write_time);
     601           7 :         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           write_time, all_info, all_info, write_time);
     602             : 
     603           7 :         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      create_time, all_info, all_info, create_time);
     604           7 :         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
     605           7 :         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
     606           7 :         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           create_time, all_info, all_info, create_time);
     607           7 :         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           create_time, all_info, all_info, create_time);
     608             : 
     609           7 :         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      access_time, all_info, all_info, access_time);
     610           7 :         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, access_time, all_info, all_info, access_time);
     611           7 :         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, access_time, all_info, all_info, access_time);
     612           7 :         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           access_time, all_info, all_info, access_time);
     613           7 :         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           access_time, all_info, all_info, access_time);
     614             : 
     615           7 :         CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      change_time, all_info, all_info, change_time);
     616           7 :         CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, change_time, all_info, all_info, change_time);
     617           7 :         CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, change_time, all_info, all_info, change_time);
     618           7 :         CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           change_time, all_info, all_info, change_time);
     619           7 :         CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           change_time, all_info, all_info, change_time);
     620             : 
     621           7 :         CHECK_VAL("SEARCH",              search,              size, all_info, all_info, size);
     622           7 :         CHECK_VAL("STANDARD",            standard,            size, all_info, all_info, size);
     623           7 :         CHECK_VAL("EA_SIZE",             ea_size,             size, all_info, all_info, size);
     624           7 :         CHECK_VAL("DIRECTORY_INFO",      directory_info,      size, all_info, all_info, size);
     625           7 :         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, size, all_info, all_info, size);
     626           7 :         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, size, all_info, all_info, size);
     627           7 :         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           size, all_info, all_info, size);
     628           7 :         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           size, all_info, all_info, size);
     629           7 :         CHECK_VAL("UNIX_INFO",           unix_info,           size, all_info, all_info, size);
     630             : 
     631           7 :         CHECK_VAL("STANDARD",            standard,            alloc_size, all_info, all_info, alloc_size);
     632           7 :         CHECK_VAL("EA_SIZE",             ea_size,             alloc_size, all_info, all_info, alloc_size);
     633           7 :         CHECK_VAL("DIRECTORY_INFO",      directory_info,      alloc_size, all_info, all_info, alloc_size);
     634           7 :         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, alloc_size, all_info, all_info, alloc_size);
     635           7 :         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, alloc_size, all_info, all_info, alloc_size);
     636           7 :         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           alloc_size, all_info, all_info, alloc_size);
     637           7 :         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           alloc_size, all_info, all_info, alloc_size);
     638           7 :         CHECK_VAL("UNIX_INFO",           unix_info,           alloc_size, all_info, all_info, alloc_size);
     639             : 
     640           7 :         CHECK_VAL("EA_SIZE",             ea_size,             ea_size, all_info, all_info, ea_size);
     641           7 :         CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, ea_size, all_info, all_info, ea_size);
     642           7 :         CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, ea_size, all_info, all_info, ea_size);
     643           7 :         CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           ea_size, all_info, all_info, ea_size);
     644           7 :         CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           ea_size, all_info, all_info, ea_size);
     645             : 
     646           7 :         if (alt_info_supported) {
     647           7 :                 CHECK_STR("SEARCH", search, name, alt_info, alt_name_info,
     648             :                     fname);
     649           7 :                 CHECK_WSTR("BOTH_DIRECTORY_INFO", both_directory_info,
     650             :                     short_name, alt_info, alt_name_info, fname, STR_UNICODE);
     651             :         }
     652             : 
     653           7 :         CHECK_NAME("STANDARD",            standard,            name, fname, 0);
     654           7 :         CHECK_NAME("EA_SIZE",             ea_size,             name, fname, 0);
     655           7 :         CHECK_NAME("DIRECTORY_INFO",      directory_info,      name, fname, STR_TERMINATE_ASCII);
     656           7 :         CHECK_NAME("FULL_DIRECTORY_INFO", full_directory_info, name, fname, STR_TERMINATE_ASCII);
     657             : 
     658           7 :         if (name_info_supported) {
     659           7 :                 CHECK_NAME("NAME_INFO", name_info, name, fname,
     660             :                     STR_TERMINATE_ASCII);
     661             :         }
     662             : 
     663           7 :         CHECK_NAME("BOTH_DIRECTORY_INFO", both_directory_info, name, fname, STR_TERMINATE_ASCII);
     664           7 :         CHECK_NAME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           name, fname, STR_TERMINATE_ASCII);
     665           7 :         CHECK_NAME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           name, fname, STR_TERMINATE_ASCII);
     666           7 :         CHECK_UNIX_NAME("UNIX_INFO",           unix_info,           name, fname, STR_TERMINATE_ASCII);
     667             : 
     668           7 :         if (internal_info_supported) {
     669           7 :                 CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,
     670             :                     file_id, internal_info, internal_information, file_id);
     671           7 :                 CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,
     672             :                     file_id, internal_info, internal_information, file_id);
     673             :         }
     674             : 
     675           7 : done:
     676           7 :         smb_raw_exit(cli->session);
     677           7 :         smbcli_unlink(cli->tree, fname);
     678             : 
     679           7 :         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      197589 : static bool multiple_search_callback(void *private_data, const union smb_search_data *file)
     693             : {
     694      197589 :         struct multiple_result *data = (struct multiple_result *)private_data;
     695             : 
     696             : 
     697      197589 :         data->count++;
     698      197589 :         data->list = talloc_realloc(data->tctx,
     699             :                                       data->list, 
     700             :                                       union smb_search_data,
     701             :                                       data->count);
     702             : 
     703      197589 :         data->list[data->count-1] = *file;
     704             : 
     705      197589 :         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         119 : 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           0 :         union smb_search_first io;
     721           0 :         union smb_search_next io2;
     722           0 :         NTSTATUS status;
     723         119 :         const int per_search = 100;
     724         119 :         struct multiple_result *result = (struct multiple_result *)data;
     725             : 
     726         119 :         if (data_level == RAW_SEARCH_DATA_SEARCH) {
     727           7 :                 io.search_first.level = RAW_SEARCH_SEARCH;
     728           7 :                 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
     729           7 :                 io.search_first.in.max_count = per_search;
     730           7 :                 io.search_first.in.search_attrib = 0;
     731           7 :                 io.search_first.in.pattern = pattern;
     732             :         } else {
     733         112 :                 io.t2ffirst.level = RAW_SEARCH_TRANS2;
     734         112 :                 io.t2ffirst.data_level = data_level;
     735         112 :                 io.t2ffirst.in.search_attrib = 0;
     736         112 :                 io.t2ffirst.in.max_count = per_search;
     737         112 :                 io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
     738         112 :                 io.t2ffirst.in.storage_type = 0;
     739         112 :                 io.t2ffirst.in.pattern = pattern;
     740         112 :                 if (cont_type == CONT_RESUME_KEY) {
     741           7 :                         io.t2ffirst.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME | 
     742             :                                 FLAG_TRANS2_FIND_BACKUP_INTENT;
     743             :                 }
     744             :         }
     745             : 
     746         119 :         status = smb_raw_search_first(cli->tree, tctx,
     747             :                                       &io, data, multiple_search_callback);
     748             :         
     749             : 
     750         833 :         while (NT_STATUS_IS_OK(status)) {
     751         833 :                 if (data_level == RAW_SEARCH_DATA_SEARCH) {
     752          49 :                         io2.search_next.level = RAW_SEARCH_SEARCH;
     753          49 :                         io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
     754          49 :                         io2.search_next.in.max_count = per_search;
     755          49 :                         io2.search_next.in.search_attrib = 0;
     756          49 :                         io2.search_next.in.id = result->list[result->count-1].search.id;
     757             :                 } else {
     758         784 :                         io2.t2fnext.level = RAW_SEARCH_TRANS2;
     759         784 :                         io2.t2fnext.data_level = data_level;
     760         784 :                         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
     761         784 :                         io2.t2fnext.in.max_count = per_search;
     762         784 :                         io2.t2fnext.in.resume_key = 0;
     763         784 :                         io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CLOSE_IF_END;
     764         784 :                         io2.t2fnext.in.last_name = "";
     765         784 :                         switch (cont_type) {
     766          49 :                         case CONT_RESUME_KEY:
     767          49 :                                 io2.t2fnext.in.resume_key = extract_resume_key(&result->list[result->count-1],
     768             :                                                                                io2.t2fnext.level, io2.t2fnext.data_level);
     769          49 :                                 if (io2.t2fnext.in.resume_key == 0) {
     770           0 :                                         printf("Server does not support resume by key for level %s\n",
     771             :                                                level_name(io2.t2fnext.level, io2.t2fnext.data_level));
     772           0 :                                         return NT_STATUS_NOT_SUPPORTED;
     773             :                                 }
     774          49 :                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_REQUIRE_RESUME |
     775             :                                         FLAG_TRANS2_FIND_BACKUP_INTENT;
     776          49 :                                 break;
     777         392 :                         case CONT_NAME:
     778         392 :                                 io2.t2fnext.in.last_name = extract_name(&result->list[result->count-1],
     779             :                                                                         io2.t2fnext.level, io2.t2fnext.data_level);
     780         392 :                                 break;
     781         343 :                         case CONT_FLAGS:
     782         343 :                                 io2.t2fnext.in.flags |= FLAG_TRANS2_FIND_CONTINUE;
     783         343 :                                 break;
     784             :                         }
     785             :                 }
     786             : 
     787         833 :                 status = smb_raw_search_next(cli->tree, tctx,
     788             :                                              &io2, data, multiple_search_callback);
     789         833 :                 if (!NT_STATUS_IS_OK(status)) {
     790           0 :                         break;
     791             :                 }
     792         833 :                 if (data_level == RAW_SEARCH_DATA_SEARCH) {
     793          49 :                         if (io2.search_next.out.count == 0) {
     794           7 :                                 break;
     795             :                         }
     796         784 :                 } else if (io2.t2fnext.out.count == 0 ||
     797         672 :                            io2.t2fnext.out.end_of_search) {
     798             :                         break;
     799             :                 }
     800             :         }
     801             : 
     802         119 :         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      563820 : static int search_compare(union smb_search_data *d1, union smb_search_data *d2)
     815             : {
     816           0 :         const char *s1, *s2;
     817           0 :         enum smb_search_level level;
     818             : 
     819      563820 :         if (compare_data_level == RAW_SEARCH_DATA_SEARCH) {
     820       34921 :                 level = RAW_SEARCH_SEARCH;
     821             :         } else {
     822      528899 :                 level = RAW_SEARCH_TRANS2;
     823             :         }
     824             : 
     825      563820 :         s1 = extract_name(d1, level, compare_data_level);
     826      563820 :         s2 = extract_name(d2, level, compare_data_level);
     827      563820 :         return strcmp_safe(s1, s2);
     828             : }
     829             : 
     830             : 
     831             : 
     832             : /* 
     833             :    basic testing of search calls using many files
     834             : */
     835           7 : static bool test_many_files(struct torture_context *tctx, 
     836             :                             struct smbcli_state *cli)
     837             : {
     838           7 :         const int num_files = 700;
     839           0 :         int i, fnum, t;
     840           0 :         char *fname;
     841           7 :         bool ret = true;
     842           0 :         NTSTATUS status;
     843           0 :         struct multiple_result result;
     844           0 :         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           7 :         } 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           7 :         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
     875             : 
     876           7 :         torture_comment(tctx, "Testing with %d files\n", num_files);
     877             : 
     878        4907 :         for (i=0;i<num_files;i++) {
     879        4900 :                 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
     880        4900 :                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
     881        4900 :                 torture_assert(tctx, fnum != -1, "Failed to create");
     882        4900 :                 talloc_free(fname);
     883        4900 :                 smbcli_close(cli->tree, fnum);
     884             :         }
     885             : 
     886             : 
     887         161 :         for (t=0;t<ARRAY_SIZE(search_types);t++) {
     888         154 :                 ZERO_STRUCT(result);
     889             : 
     890         154 :                 if ((search_types[t].cont_type == CONT_RESUME_KEY) &&
     891          56 :                     (search_types[t].data_level != RAW_SEARCH_DATA_SEARCH) &&
     892          49 :                     !torture_setting_bool(tctx, "resume_key_support", true)) {
     893          42 :                         torture_comment(tctx,
     894             :                                         "SKIP: Continue %s via %s\n",
     895             :                                         search_types[t].name, search_types[t].cont_name);
     896          42 :                         continue;
     897             :                 }
     898             : 
     899         112 :                 result.tctx = talloc_new(tctx);
     900             :         
     901         112 :                 torture_comment(tctx,
     902             :                                 "Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);
     903             : 
     904         112 :                 status = multiple_search(cli, tctx, BASEDIR "\\*.*", 
     905             :                                          search_types[t].data_level,
     906             :                                          search_types[t].cont_type,
     907             :                                          &result);
     908         112 :                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
     909           0 :                         torture_warning(tctx, "search level %s not supported "
     910             :                                         "by server",
     911             :                                         search_types[t].name);
     912           0 :                         continue;
     913             :                 }
     914         112 :                 torture_assert_ntstatus_ok(tctx, status, "search failed");
     915         112 :                 CHECK_VALUE(result.count, num_files);
     916             : 
     917         112 :                 compare_data_level = search_types[t].data_level;
     918             : 
     919         112 :                 TYPESAFE_QSORT(result.list, result.count, search_compare);
     920             : 
     921       78512 :                 for (i=0;i<result.count;i++) {
     922           0 :                         const char *s;
     923           0 :                         enum smb_search_level level;
     924       78400 :                         if (compare_data_level == RAW_SEARCH_DATA_SEARCH) {
     925        4900 :                                 level = RAW_SEARCH_SEARCH;
     926             :                         } else {
     927       73500 :                                 level = RAW_SEARCH_TRANS2;
     928             :                         }
     929       78400 :                         s = extract_name(&result.list[i], level, compare_data_level);
     930       78400 :                         fname = talloc_asprintf(cli, "t%03d-%d.txt", i, i);
     931       78400 :                         torture_assert_str_equal(tctx, fname, s, "Incorrect name");
     932       78400 :                         talloc_free(fname);
     933             :                 }
     934         112 :                 talloc_free(result.tctx);
     935             :         }
     936             : 
     937           7 :         smb_raw_exit(cli->session);
     938           7 :         smbcli_deltree(cli->tree, BASEDIR);
     939             : 
     940           7 :         return ret;
     941             : }
     942             : 
     943             : /*
     944             :   check a individual file result
     945             : */
     946          70 : static bool check_result(struct multiple_result *result, const char *name, bool exist, uint32_t attrib)
     947             : {
     948           0 :         int i;
     949        1250 :         for (i=0;i<result->count;i++) {
     950        1222 :                 if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
     951             :         }
     952          70 :         if (i == result->count) {
     953          28 :                 if (exist) {
     954           0 :                         printf("failed: '%s' should exist with attribute %s\n", 
     955           0 :                                name, attrib_string(result->list, attrib));
     956           0 :                         return false;
     957             :                 }
     958          28 :                 return true;
     959             :         }
     960             : 
     961          42 :         if (!exist) {
     962           0 :                 printf("failed: '%s' should NOT exist (has attribute %s)\n", 
     963           0 :                        name, attrib_string(result->list, result->list[i].both_directory_info.attrib));
     964           0 :                 return false;
     965             :         }
     966             : 
     967          42 :         if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
     968           0 :                 printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
     969             :                        name, 
     970           0 :                        attrib, result->list[i].both_directory_info.attrib);
     971           0 :                 return false;
     972             :         }
     973          42 :         return true;
     974             : }
     975             : 
     976             : /* 
     977             :    test what happens when the directory is modified during a search
     978             : */
     979           7 : static bool test_modify_search(struct torture_context *tctx, 
     980             :                                                            struct smbcli_state *cli)
     981             : {
     982           7 :         const int num_files = 20;
     983           0 :         int i, fnum;
     984           0 :         char *fname;
     985           7 :         bool ret = true;
     986           0 :         NTSTATUS status;
     987           0 :         struct multiple_result result;
     988           0 :         union smb_search_first io;
     989           0 :         union smb_search_next io2;
     990           0 :         union smb_setfileinfo sfinfo;
     991             : 
     992           7 :         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
     993             : 
     994           7 :         printf("Creating %d files\n", num_files);
     995             : 
     996         147 :         for (i=num_files-1;i>=0;i--) {
     997         140 :                 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
     998         140 :                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
     999         140 :                 if (fnum == -1) {
    1000           0 :                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
    1001           0 :                         ret = false;
    1002           0 :                         goto done;
    1003             :                 }
    1004         140 :                 talloc_free(fname);
    1005         140 :                 smbcli_close(cli->tree, fnum);
    1006             :         }
    1007             : 
    1008           7 :         printf("pulling the first file\n");
    1009           7 :         ZERO_STRUCT(result);
    1010           7 :         result.tctx = talloc_new(tctx);
    1011             : 
    1012           7 :         io.t2ffirst.level = RAW_SEARCH_TRANS2;
    1013           7 :         io.t2ffirst.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
    1014           7 :         io.t2ffirst.in.search_attrib = 0;
    1015           7 :         io.t2ffirst.in.max_count = 0;
    1016           7 :         io.t2ffirst.in.flags = 0;
    1017           7 :         io.t2ffirst.in.storage_type = 0;
    1018           7 :         io.t2ffirst.in.pattern = BASEDIR "\\*.*";
    1019             : 
    1020           7 :         status = smb_raw_search_first(cli->tree, tctx,
    1021             :                                       &io, &result, multiple_search_callback);
    1022           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1023           7 :         CHECK_VALUE(result.count, 1);
    1024             :         
    1025           7 :         printf("pulling the second file\n");
    1026           7 :         io2.t2fnext.level = RAW_SEARCH_TRANS2;
    1027           7 :         io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
    1028           7 :         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
    1029           7 :         io2.t2fnext.in.max_count = 1;
    1030           7 :         io2.t2fnext.in.resume_key = 0;
    1031           7 :         io2.t2fnext.in.flags = 0;
    1032           7 :         io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;
    1033             : 
    1034           7 :         status = smb_raw_search_next(cli->tree, tctx,
    1035             :                                      &io2, &result, multiple_search_callback);
    1036           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1037           7 :         CHECK_VALUE(result.count, 2);
    1038             : 
    1039           7 :         result.count = 0;
    1040             : 
    1041           7 :         printf("Changing attributes and deleting\n");
    1042           7 :         smbcli_open(cli->tree, BASEDIR "\\T003-03.txt.2", O_CREAT|O_RDWR, DENY_NONE);
    1043           7 :         smbcli_open(cli->tree, BASEDIR "\\T013-13.txt.2", O_CREAT|O_RDWR, DENY_NONE);
    1044           7 :         fnum = create_complex_file(cli, tctx, BASEDIR "\\T013-13.txt.3");
    1045           7 :         smbcli_unlink(cli->tree, BASEDIR "\\T014-14.txt");
    1046           7 :         torture_set_file_attribute(cli->tree, BASEDIR "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
    1047           7 :         torture_set_file_attribute(cli->tree, BASEDIR "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
    1048           7 :         torture_set_file_attribute(cli->tree, BASEDIR "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);     
    1049           7 :         torture_set_file_attribute(cli->tree, BASEDIR "\\T018-18.txt", 0); 
    1050           7 :         sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
    1051           7 :         sfinfo.generic.in.file.fnum = fnum;
    1052           7 :         sfinfo.disposition_info.in.delete_on_close = 1;
    1053           7 :         status = smb_raw_setfileinfo(cli->tree, &sfinfo);
    1054           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1055             : 
    1056           7 :         io2.t2fnext.level = RAW_SEARCH_TRANS2;
    1057           7 :         io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
    1058           7 :         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
    1059           7 :         io2.t2fnext.in.max_count = num_files + 3;
    1060           7 :         io2.t2fnext.in.resume_key = 0;
    1061           7 :         io2.t2fnext.in.flags = 0;
    1062           7 :         io2.t2fnext.in.last_name = ".";
    1063             : 
    1064           7 :         status = smb_raw_search_next(cli->tree, tctx,
    1065             :                                      &io2, &result, multiple_search_callback);
    1066           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1067           7 :         CHECK_VALUE(result.count, 20);
    1068             : 
    1069           7 :         ret &= check_result(&result, "t009-9.txt", true, FILE_ATTRIBUTE_ARCHIVE);
    1070           7 :         ret &= check_result(&result, "t014-14.txt", false, 0);
    1071           7 :         ret &= check_result(&result, "t015-15.txt", false, 0);
    1072           7 :         ret &= check_result(&result, "t016-16.txt", true, FILE_ATTRIBUTE_NORMAL);
    1073           7 :         ret &= check_result(&result, "t017-17.txt", false, 0);
    1074           7 :         ret &= check_result(&result, "t018-18.txt", true, FILE_ATTRIBUTE_ARCHIVE);
    1075           7 :         ret &= check_result(&result, "t019-19.txt", true, FILE_ATTRIBUTE_ARCHIVE);
    1076           7 :         ret &= check_result(&result, "T013-13.txt.2", true, FILE_ATTRIBUTE_ARCHIVE);
    1077           7 :         ret &= check_result(&result, "T003-3.txt.2", false, 0);
    1078           7 :         ret &= check_result(&result, "T013-13.txt.3", true, FILE_ATTRIBUTE_ARCHIVE);
    1079             : 
    1080           7 :         if (!ret) {
    1081           0 :                 for (i=0;i<result.count;i++) {
    1082           0 :                         printf("%s %s (0x%x)\n", 
    1083           0 :                                result.list[i].both_directory_info.name.s, 
    1084           0 :                                attrib_string(tctx, result.list[i].both_directory_info.attrib),
    1085           0 :                                result.list[i].both_directory_info.attrib);
    1086             :                 }
    1087             :         }
    1088             : 
    1089           7 : done:
    1090           7 :         smb_raw_exit(cli->session);
    1091           7 :         smbcli_deltree(cli->tree, BASEDIR);
    1092             : 
    1093           7 :         return ret;
    1094             : }
    1095             : 
    1096             : 
    1097             : /* 
    1098             :    testing if directories always come back sorted
    1099             : */
    1100           7 : static bool test_sorted(struct torture_context *tctx, struct smbcli_state *cli)
    1101             : {
    1102           7 :         const int num_files = 700;
    1103           0 :         int i, fnum;
    1104           0 :         char *fname;
    1105           7 :         bool ret = true;
    1106           0 :         NTSTATUS status;
    1107           0 :         struct multiple_result result;
    1108             : 
    1109           7 :         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
    1110             : 
    1111           7 :         printf("Creating %d files\n", num_files);
    1112             : 
    1113        4907 :         for (i=0;i<num_files;i++) {
    1114        4900 :                 fname = talloc_asprintf(cli, BASEDIR "\\%s.txt", generate_random_str_list(tctx, 10, "abcdefgh"));
    1115        4900 :                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
    1116        4900 :                 if (fnum == -1) {
    1117           0 :                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
    1118           0 :                         ret = false;
    1119           0 :                         goto done;
    1120             :                 }
    1121        4900 :                 talloc_free(fname);
    1122        4900 :                 smbcli_close(cli->tree, fnum);
    1123             :         }
    1124             : 
    1125             : 
    1126           7 :         ZERO_STRUCT(result);
    1127           7 :         result.tctx = tctx;
    1128             :         
    1129           7 :         status = multiple_search(cli, tctx, BASEDIR "\\*.*", 
    1130             :                                  RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
    1131             :                                  CONT_NAME, &result);       
    1132           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1133           7 :         CHECK_VALUE(result.count, num_files);
    1134             : 
    1135        1408 :         for (i=0;i<num_files-1;i++) {
    1136           0 :                 const char *name1, *name2;
    1137        1406 :                 name1 = result.list[i].both_directory_info.name.s;
    1138        1406 :                 name2 = result.list[i+1].both_directory_info.name.s;
    1139        1406 :                 if (strcasecmp_m(name1, name2) > 0) {
    1140           5 :                         printf("non-alphabetical order at entry %d  '%s' '%s'\n", 
    1141             :                                i, name1, name2);
    1142           5 :                         printf("Server does not produce sorted directory listings (not an error)\n");
    1143           5 :                         goto done;
    1144             :                 }
    1145             :         }
    1146             : 
    1147           2 :         talloc_free(result.list);
    1148             : 
    1149           7 : done:
    1150           7 :         smb_raw_exit(cli->session);
    1151           7 :         smbcli_deltree(cli->tree, BASEDIR);
    1152             : 
    1153           7 :         return ret;
    1154             : }
    1155             : 
    1156             : 
    1157             : 
    1158             : /* 
    1159             :    basic testing of many old style search calls using separate dirs
    1160             : */
    1161           7 : static bool test_many_dirs(struct torture_context *tctx, 
    1162             :                                                    struct smbcli_state *cli)
    1163             : {
    1164           7 :         const int num_dirs = 20;
    1165           0 :         int i, fnum, n;
    1166           0 :         char *fname, *dname;
    1167           7 :         bool ret = true;
    1168           0 :         NTSTATUS status;
    1169           0 :         union smb_search_data *file, *file2, *file3;
    1170             : 
    1171           7 :         if (!torture_setting_bool(tctx, "raw_search_search", true)) {
    1172           0 :                 torture_comment(tctx, "Skipping these tests as the server "
    1173             :                         "doesn't support old style search calls\n");
    1174           0 :                 return true;
    1175             :         }
    1176           7 :         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
    1177             : 
    1178           7 :         printf("Creating %d dirs\n", num_dirs);
    1179             : 
    1180         147 :         for (i=0;i<num_dirs;i++) {
    1181         140 :                 dname = talloc_asprintf(cli, BASEDIR "\\d%d", i);
    1182         140 :                 status = smbcli_mkdir(cli->tree, dname);
    1183         140 :                 if (!NT_STATUS_IS_OK(status)) {
    1184           0 :                         printf("(%s) Failed to create %s - %s\n", 
    1185             :                                __location__, dname, nt_errstr(status));
    1186           0 :                         ret = false;
    1187           0 :                         goto done;
    1188             :                 }
    1189             : 
    1190         560 :                 for (n=0;n<3;n++) {
    1191         420 :                         fname = talloc_asprintf(cli, BASEDIR "\\d%d\\f%d-%d.txt", i, i, n);
    1192         420 :                         fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
    1193         420 :                         if (fnum == -1) {
    1194           0 :                                 printf("(%s) Failed to create %s - %s\n", 
    1195             :                                        __location__, fname, smbcli_errstr(cli->tree));
    1196           0 :                                 ret = false;
    1197           0 :                                 goto done;
    1198             :                         }
    1199         420 :                         talloc_free(fname);
    1200         420 :                         smbcli_close(cli->tree, fnum);
    1201             :                 }
    1202             : 
    1203         140 :                 talloc_free(dname);
    1204             :         }
    1205             : 
    1206           7 :         file  = talloc_zero_array(tctx, union smb_search_data, num_dirs);
    1207           7 :         file2 = talloc_zero_array(tctx, union smb_search_data, num_dirs);
    1208           7 :         file3 = talloc_zero_array(tctx, union smb_search_data, num_dirs);
    1209             : 
    1210           7 :         printf("Search first on %d dirs\n", num_dirs);
    1211             : 
    1212         147 :         for (i=0;i<num_dirs;i++) {
    1213           0 :                 union smb_search_first io;
    1214         140 :                 io.search_first.level = RAW_SEARCH_SEARCH;
    1215         140 :                 io.search_first.data_level = RAW_SEARCH_DATA_SEARCH;
    1216         140 :                 io.search_first.in.max_count = 1;
    1217         140 :                 io.search_first.in.search_attrib = 0;
    1218         140 :                 io.search_first.in.pattern = talloc_asprintf(tctx, BASEDIR "\\d%d\\*.txt", i);
    1219         140 :                 fname = talloc_asprintf(tctx, "f%d-", i);
    1220             : 
    1221         140 :                 io.search_first.out.count = 0;
    1222             : 
    1223         140 :                 status = smb_raw_search_first(cli->tree, tctx,
    1224         140 :                                               &io, (void *)&file[i], single_search_callback);
    1225         140 :                 if (io.search_first.out.count != 1) {
    1226           0 :                         printf("(%s) search first gave %d entries for dir %d - %s\n",
    1227           0 :                                __location__, io.search_first.out.count, i, nt_errstr(status));
    1228           0 :                         ret = false;
    1229           0 :                         goto done;
    1230             :                 }
    1231         140 :                 CHECK_STATUS(status, NT_STATUS_OK);
    1232         140 :                 if (strncasecmp(file[i].search.name, fname, strlen(fname)) != 0) {
    1233           0 :                         printf("(%s) incorrect name '%s' expected '%s'[12].txt\n", 
    1234           0 :                                __location__, file[i].search.name, fname);
    1235           0 :                         ret = false;
    1236           0 :                         goto done;
    1237             :                 }
    1238             : 
    1239         140 :                 talloc_free(fname);
    1240             :         }
    1241             : 
    1242           7 :         printf("Search next on %d dirs\n", num_dirs);
    1243             : 
    1244         147 :         for (i=0;i<num_dirs;i++) {
    1245           0 :                 union smb_search_next io2;
    1246             : 
    1247         140 :                 io2.search_next.level = RAW_SEARCH_SEARCH;
    1248         140 :                 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
    1249         140 :                 io2.search_next.in.max_count = 1;
    1250         140 :                 io2.search_next.in.search_attrib = 0;
    1251         140 :                 io2.search_next.in.id = file[i].search.id;
    1252         140 :                 fname = talloc_asprintf(tctx, "f%d-", i);
    1253             : 
    1254         140 :                 io2.search_next.out.count = 0;
    1255             : 
    1256         140 :                 status = smb_raw_search_next(cli->tree, tctx,
    1257         140 :                                              &io2, (void *)&file2[i], single_search_callback);
    1258         140 :                 if (io2.search_next.out.count != 1) {
    1259           0 :                         printf("(%s) search next gave %d entries for dir %d - %s\n",
    1260           0 :                                __location__, io2.search_next.out.count, i, nt_errstr(status));
    1261           0 :                         ret = false;
    1262           0 :                         goto done;
    1263             :                 }
    1264         140 :                 CHECK_STATUS(status, NT_STATUS_OK);
    1265         140 :                 if (strncasecmp(file2[i].search.name, fname, strlen(fname)) != 0) {
    1266           0 :                         printf("(%s) incorrect name '%s' expected '%s'[12].txt\n", 
    1267           0 :                                __location__, file2[i].search.name, fname);
    1268           0 :                         ret = false;
    1269           0 :                         goto done;
    1270             :                 }
    1271             : 
    1272         140 :                 talloc_free(fname);
    1273             :         }
    1274             : 
    1275             : 
    1276           7 :         printf("Search next (rewind) on %d dirs\n", num_dirs);
    1277             : 
    1278         147 :         for (i=0;i<num_dirs;i++) {
    1279           0 :                 union smb_search_next io2;
    1280             : 
    1281         140 :                 io2.search_next.level = RAW_SEARCH_SEARCH;
    1282         140 :                 io2.search_next.data_level = RAW_SEARCH_DATA_SEARCH;
    1283         140 :                 io2.search_next.in.max_count = 1;
    1284         140 :                 io2.search_next.in.search_attrib = 0;
    1285         140 :                 io2.search_next.in.id = file[i].search.id;
    1286         140 :                 fname = talloc_asprintf(tctx, "f%d-", i);
    1287         140 :                 io2.search_next.out.count = 0;
    1288             : 
    1289         140 :                 status = smb_raw_search_next(cli->tree, tctx,
    1290         140 :                                              &io2, (void *)&file3[i], single_search_callback);
    1291         140 :                 if (io2.search_next.out.count != 1) {
    1292           0 :                         printf("(%s) search next gave %d entries for dir %d - %s\n",
    1293           0 :                                __location__, io2.search_next.out.count, i, nt_errstr(status));
    1294           0 :                         ret = false;
    1295           0 :                         goto done;
    1296             :                 }
    1297         140 :                 CHECK_STATUS(status, NT_STATUS_OK);
    1298             : 
    1299         140 :                 if (strncasecmp(file3[i].search.name, file2[i].search.name, 3) != 0) {
    1300           0 :                         printf("(%s) incorrect name '%s' on rewind at dir %d\n", 
    1301           0 :                                __location__, file2[i].search.name, i);
    1302           0 :                         ret = false;
    1303           0 :                         goto done;
    1304             :                 }
    1305             : 
    1306         140 :                 if (torture_setting_bool(tctx, "rewind_support", true) &&
    1307         140 :                     strcmp(file3[i].search.name, file2[i].search.name) != 0) {
    1308           0 :                         printf("(%s) server did not rewind - got '%s' expected '%s'\n", 
    1309           0 :                                __location__, file3[i].search.name, file2[i].search.name);
    1310           0 :                         ret = false;
    1311           0 :                         goto done;
    1312             :                 }
    1313             : 
    1314         140 :                 talloc_free(fname);
    1315             :         }
    1316             : 
    1317             : 
    1318           7 : done:
    1319           7 :         smb_raw_exit(cli->session);
    1320           7 :         smbcli_deltree(cli->tree, BASEDIR);
    1321             : 
    1322           7 :         return ret;
    1323             : }
    1324             : 
    1325             : 
    1326             : /* 
    1327             :    testing of OS/2 style delete
    1328             : */
    1329           7 : static bool test_os2_delete(struct torture_context *tctx, 
    1330             :                                                         struct smbcli_state *cli)
    1331             : {
    1332           7 :         const int num_files = 700;
    1333           7 :         const int delete_count = 4;
    1334           7 :         int total_deleted = 0;
    1335           0 :         int i, fnum;
    1336           0 :         char *fname;
    1337           7 :         bool ret = true;
    1338           0 :         NTSTATUS status;
    1339           0 :         union smb_search_first io;
    1340           0 :         union smb_search_next io2;
    1341           0 :         struct multiple_result result;
    1342             : 
    1343           7 :         if (!torture_setting_bool(tctx, "search_ea_size", true)){
    1344           0 :                 torture_comment(tctx,
    1345             :                                 "Server does not support RAW_SEARCH_EA_SIZE "
    1346             :                                 "level. Skipping this test\n");
    1347           0 :                 return true;
    1348             :         }
    1349             : 
    1350           7 :         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
    1351             : 
    1352           7 :         printf("Testing OS/2 style delete on %d files\n", num_files);
    1353             : 
    1354        4907 :         for (i=0;i<num_files;i++) {
    1355        4900 :                 fname = talloc_asprintf(cli, BASEDIR "\\file%u.txt", i);
    1356        4900 :                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
    1357        4900 :                 if (fnum == -1) {
    1358           0 :                         printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
    1359           0 :                         ret = false;
    1360           0 :                         goto done;
    1361             :                 }
    1362        4900 :                 talloc_free(fname);
    1363        4900 :                 smbcli_close(cli->tree, fnum);
    1364             :         }
    1365             : 
    1366             : 
    1367           7 :         ZERO_STRUCT(result);
    1368           7 :         result.tctx = tctx;
    1369             : 
    1370           7 :         io.t2ffirst.level = RAW_SEARCH_TRANS2;
    1371           7 :         io.t2ffirst.data_level = RAW_SEARCH_DATA_EA_SIZE;
    1372           7 :         io.t2ffirst.in.search_attrib = 0;
    1373           7 :         io.t2ffirst.in.max_count = 100;
    1374           7 :         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
    1375           7 :         io.t2ffirst.in.storage_type = 0;
    1376           7 :         io.t2ffirst.in.pattern = BASEDIR "\\*";
    1377             : 
    1378           7 :         status = smb_raw_search_first(cli->tree, tctx,
    1379             :                                       &io, &result, multiple_search_callback);
    1380           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1381             : 
    1382          35 :         for (i=0;i<MIN(result.count, delete_count);i++) {
    1383          28 :                 fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
    1384          28 :                 status = smbcli_unlink(cli->tree, fname);
    1385          28 :                 CHECK_STATUS(status, NT_STATUS_OK);
    1386          28 :                 total_deleted++;
    1387          28 :                 talloc_free(fname);
    1388             :         }
    1389             : 
    1390           7 :         io2.t2fnext.level = RAW_SEARCH_TRANS2;
    1391           7 :         io2.t2fnext.data_level = RAW_SEARCH_DATA_EA_SIZE;
    1392           7 :         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
    1393           7 :         io2.t2fnext.in.max_count = 100;
    1394           7 :         io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
    1395           7 :         io2.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
    1396           7 :         io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
    1397             : 
    1398           0 :         do {
    1399        1225 :                 ZERO_STRUCT(result);
    1400        1225 :                 result.tctx = tctx;
    1401             : 
    1402        1225 :                 status = smb_raw_search_next(cli->tree, tctx,
    1403             :                                              &io2, &result, multiple_search_callback);
    1404        1225 :                 if (!NT_STATUS_IS_OK(status)) {
    1405           0 :                         break;
    1406             :                 }
    1407             : 
    1408        6097 :                 for (i=0;i<MIN(result.count, delete_count);i++) {
    1409        4872 :                         fname = talloc_asprintf(cli, BASEDIR "\\%s", result.list[i].ea_size.name.s);
    1410        4872 :                         status = smbcli_unlink(cli->tree, fname);
    1411        4872 :                         CHECK_STATUS(status, NT_STATUS_OK);
    1412        4872 :                         total_deleted++;
    1413        4872 :                         talloc_free(fname);
    1414             :                 }
    1415             : 
    1416        1225 :                 if (i>0) {
    1417        1218 :                         io2.t2fnext.in.resume_key = result.list[i-1].ea_size.resume_key;
    1418        1218 :                         io2.t2fnext.in.last_name = result.list[i-1].ea_size.name.s;
    1419             :                 }
    1420        1225 :         } while (NT_STATUS_IS_OK(status) && result.count != 0);
    1421             : 
    1422           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1423             : 
    1424           7 :         if (total_deleted != num_files) {
    1425           0 :                 printf("error: deleted %d - expected to delete %d\n", 
    1426             :                        total_deleted, num_files);
    1427           0 :                 ret = false;
    1428             :         }
    1429             : 
    1430           7 : done:
    1431           7 :         smb_raw_exit(cli->session);
    1432           7 :         smbcli_deltree(cli->tree, BASEDIR);
    1433             : 
    1434           7 :         return ret;
    1435             : }
    1436             : 
    1437             : 
    1438          26 : static int ealist_cmp(union smb_search_data *r1, union smb_search_data *r2)
    1439             : {
    1440          26 :         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           7 : static bool test_ea_list(struct torture_context *tctx, 
    1447             :                                                  struct smbcli_state *cli)
    1448             : {
    1449           0 :         int  fnum;
    1450           7 :         bool ret = true;
    1451           0 :         NTSTATUS status;
    1452           0 :         union smb_search_first io;
    1453           0 :         union smb_search_next nxt;
    1454           0 :         struct multiple_result result;
    1455           0 :         union smb_setfileinfo setfile;
    1456             : 
    1457           7 :         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
    1458             : 
    1459           7 :         printf("Testing RAW_SEARCH_EA_LIST level\n");
    1460             : 
    1461           7 :         if (!torture_setting_bool(tctx, "search_ea_support", true) ||
    1462           7 :             !torture_setting_bool(tctx, "ea_support", true)) {
    1463           0 :                 printf("..skipped per target configuration.\n");
    1464           0 :                 return true;
    1465             :         }
    1466             : 
    1467           7 :         fnum = smbcli_open(cli->tree, BASEDIR "\\file1.txt", O_CREAT|O_RDWR, DENY_NONE);
    1468           7 :         smbcli_close(cli->tree, fnum);
    1469             : 
    1470           7 :         fnum = smbcli_open(cli->tree, BASEDIR "\\file2.txt", O_CREAT|O_RDWR, DENY_NONE);
    1471           7 :         smbcli_close(cli->tree, fnum);
    1472             : 
    1473           7 :         fnum = smbcli_open(cli->tree, BASEDIR "\\file3.txt", O_CREAT|O_RDWR, DENY_NONE);
    1474           7 :         smbcli_close(cli->tree, fnum);
    1475             : 
    1476           7 :         setfile.generic.level = RAW_SFILEINFO_EA_SET;
    1477           7 :         setfile.generic.in.file.path = BASEDIR "\\file2.txt";
    1478           7 :         setfile.ea_set.in.num_eas = 2;
    1479           7 :         setfile.ea_set.in.eas = talloc_array(tctx, struct ea_struct, 2);
    1480           7 :         setfile.ea_set.in.eas[0].flags = 0;
    1481           7 :         setfile.ea_set.in.eas[0].name.s = "EA ONE";
    1482           7 :         setfile.ea_set.in.eas[0].value = data_blob_string_const("VALUE 1");
    1483           7 :         setfile.ea_set.in.eas[1].flags = 0;
    1484           7 :         setfile.ea_set.in.eas[1].name.s = "SECOND EA";
    1485           7 :         setfile.ea_set.in.eas[1].value = data_blob_string_const("Value Two");
    1486             : 
    1487           7 :         status = smb_raw_setpathinfo(cli->tree, &setfile);
    1488           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1489             : 
    1490           7 :         setfile.generic.in.file.path = BASEDIR "\\file3.txt";
    1491           7 :         status = smb_raw_setpathinfo(cli->tree, &setfile);
    1492           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1493             :         
    1494           7 :         ZERO_STRUCT(result);
    1495           7 :         result.tctx = tctx;
    1496             : 
    1497           7 :         io.t2ffirst.level = RAW_SEARCH_TRANS2;
    1498           7 :         io.t2ffirst.data_level = RAW_SEARCH_DATA_EA_LIST;
    1499           7 :         io.t2ffirst.in.search_attrib = 0;
    1500           7 :         io.t2ffirst.in.max_count = 2;
    1501           7 :         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME;
    1502           7 :         io.t2ffirst.in.storage_type = 0;
    1503           7 :         io.t2ffirst.in.pattern = BASEDIR "\\*";
    1504           7 :         io.t2ffirst.in.num_names = 2;
    1505           7 :         io.t2ffirst.in.ea_names = talloc_array(tctx, struct ea_name, 2);
    1506           7 :         io.t2ffirst.in.ea_names[0].name.s = "SECOND EA";
    1507           7 :         io.t2ffirst.in.ea_names[1].name.s = "THIRD EA";
    1508             : 
    1509           7 :         status = smb_raw_search_first(cli->tree, tctx,
    1510             :                                       &io, &result, multiple_search_callback);
    1511           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1512           7 :         CHECK_VALUE(result.count, 2);
    1513             : 
    1514           7 :         nxt.t2fnext.level = RAW_SEARCH_TRANS2;
    1515           7 :         nxt.t2fnext.data_level = RAW_SEARCH_DATA_EA_LIST;
    1516           7 :         nxt.t2fnext.in.handle = io.t2ffirst.out.handle;
    1517           7 :         nxt.t2fnext.in.max_count = 2;
    1518           7 :         nxt.t2fnext.in.resume_key = result.list[1].ea_list.resume_key;
    1519           7 :         nxt.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME | FLAG_TRANS2_FIND_CONTINUE;
    1520           7 :         nxt.t2fnext.in.last_name = result.list[1].ea_list.name.s;
    1521           7 :         nxt.t2fnext.in.num_names = 2;
    1522           7 :         nxt.t2fnext.in.ea_names = talloc_array(tctx, struct ea_name, 2);
    1523           7 :         nxt.t2fnext.in.ea_names[0].name.s = "SECOND EA";
    1524           7 :         nxt.t2fnext.in.ea_names[1].name.s = "THIRD EA";
    1525             : 
    1526           7 :         status = smb_raw_search_next(cli->tree, tctx,
    1527             :                                      &nxt, &result, multiple_search_callback);
    1528           7 :         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           7 :         TYPESAFE_QSORT(result.list, result.count, ealist_cmp);
    1533             : 
    1534           7 :         CHECK_VALUE(result.count, 3);
    1535           7 :         CHECK_VALUE(result.list[0].ea_list.eas.num_eas, 2);
    1536           7 :         CHECK_STRING(result.list[0].ea_list.name.s, "file1.txt");
    1537           7 :         CHECK_STRING(result.list[0].ea_list.eas.eas[0].name.s, "SECOND EA");
    1538           7 :         CHECK_VALUE(result.list[0].ea_list.eas.eas[0].value.length, 0);
    1539           7 :         CHECK_STRING(result.list[0].ea_list.eas.eas[1].name.s, "THIRD EA");
    1540           7 :         CHECK_VALUE(result.list[0].ea_list.eas.eas[1].value.length, 0);
    1541             : 
    1542           7 :         CHECK_STRING(result.list[1].ea_list.name.s, "file2.txt");
    1543           7 :         CHECK_STRING(result.list[1].ea_list.eas.eas[0].name.s, "SECOND EA");
    1544           7 :         CHECK_VALUE(result.list[1].ea_list.eas.eas[0].value.length, 9);
    1545           7 :         CHECK_STRING((const char *)result.list[1].ea_list.eas.eas[0].value.data, "Value Two");
    1546           7 :         CHECK_STRING(result.list[1].ea_list.eas.eas[1].name.s, "THIRD EA");
    1547           7 :         CHECK_VALUE(result.list[1].ea_list.eas.eas[1].value.length, 0);
    1548             : 
    1549           7 :         CHECK_STRING(result.list[2].ea_list.name.s, "file3.txt");
    1550           7 :         CHECK_STRING(result.list[2].ea_list.eas.eas[0].name.s, "SECOND EA");
    1551           7 :         CHECK_VALUE(result.list[2].ea_list.eas.eas[0].value.length, 9);
    1552           7 :         CHECK_STRING((const char *)result.list[2].ea_list.eas.eas[0].value.data, "Value Two");
    1553           7 :         CHECK_STRING(result.list[2].ea_list.eas.eas[1].name.s, "THIRD EA");
    1554           7 :         CHECK_VALUE(result.list[2].ea_list.eas.eas[1].value.length, 0);
    1555             : 
    1556           7 :         smb_raw_exit(cli->session);
    1557           7 :         smbcli_deltree(cli->tree, BASEDIR);
    1558             : 
    1559           7 :         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           7 : static bool test_max_count(struct torture_context *tctx,
    1567             :                            struct smbcli_state *cli)
    1568             : {
    1569           7 :         const int num_files = 2;
    1570           0 :         int i, fnum;
    1571           0 :         char *fname;
    1572           7 :         bool ret = true;
    1573           0 :         NTSTATUS status;
    1574           0 :         struct multiple_result result;
    1575           0 :         union smb_search_first io;
    1576           0 :         union smb_search_next io2;
    1577             : 
    1578           7 :         torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
    1579             : 
    1580           7 :         torture_comment(tctx, "Creating %d files\n", num_files);
    1581             : 
    1582          21 :         for (i=num_files-1;i>=0;i--) {
    1583          14 :                 fname = talloc_asprintf(cli, BASEDIR "\\t%03d-%d.txt", i, i);
    1584          14 :                 fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
    1585          14 :                 if (fnum == -1) {
    1586           0 :                         torture_comment(tctx,
    1587             :                                 "Failed to create %s - %s\n",
    1588             :                                 fname, smbcli_errstr(cli->tree));
    1589           0 :                         ret = false;
    1590           0 :                         goto done;
    1591             :                 }
    1592          14 :                 talloc_free(fname);
    1593          14 :                 smbcli_close(cli->tree, fnum);
    1594             :         }
    1595             : 
    1596           7 :         torture_comment(tctx, "Set max_count parameter to 0. "
    1597             :                         "This should return 1 entry\n");
    1598           7 :         ZERO_STRUCT(result);
    1599           7 :         result.tctx = talloc_new(tctx);
    1600             : 
    1601           7 :         io.t2ffirst.level = RAW_SEARCH_TRANS2;
    1602           7 :         io.t2ffirst.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
    1603           7 :         io.t2ffirst.in.search_attrib = 0;
    1604           7 :         io.t2ffirst.in.max_count = 0;
    1605           7 :         io.t2ffirst.in.flags = 0;
    1606           7 :         io.t2ffirst.in.storage_type = 0;
    1607           7 :         io.t2ffirst.in.pattern = BASEDIR "\\*.*";
    1608             : 
    1609           7 :         status = smb_raw_search_first(cli->tree, tctx,
    1610             :                                       &io, &result, multiple_search_callback);
    1611           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1612           7 :         CHECK_VALUE(result.count, 1);
    1613             : 
    1614           7 :         torture_comment(tctx, "Set max_count to 1. This should also "
    1615             :                         "return 1 entry\n");
    1616           7 :         io2.t2fnext.level = RAW_SEARCH_TRANS2;
    1617           7 :         io2.t2fnext.data_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
    1618           7 :         io2.t2fnext.in.handle = io.t2ffirst.out.handle;
    1619           7 :         io2.t2fnext.in.max_count = 1;
    1620           7 :         io2.t2fnext.in.resume_key = 0;
    1621           7 :         io2.t2fnext.in.flags = 0;
    1622           7 :         io2.t2fnext.in.last_name =
    1623           7 :                 result.list[result.count-1].both_directory_info.name.s;
    1624             : 
    1625           7 :         status = smb_raw_search_next(cli->tree, tctx,
    1626             :                                      &io2, &result, multiple_search_callback);
    1627           7 :         CHECK_STATUS(status, NT_STATUS_OK);
    1628           7 :         CHECK_VALUE(result.count, 2);
    1629           7 : done:
    1630           7 :         smb_raw_exit(cli->session);
    1631           7 :         smbcli_deltree(cli->tree, BASEDIR);
    1632             : 
    1633           7 :         return ret;
    1634             : }
    1635             : 
    1636             : /* 
    1637             :    basic testing of all RAW_SEARCH_* calls using a single file
    1638             : */
    1639        2354 : struct torture_suite *torture_raw_search(TALLOC_CTX *mem_ctx)
    1640             : {
    1641        2354 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "search");
    1642             : 
    1643        2354 :         torture_suite_add_2smb_test(suite, "one file search", test_one_file);
    1644        2354 :         torture_suite_add_1smb_test(suite, "many files", test_many_files);
    1645        2354 :         torture_suite_add_1smb_test(suite, "sorted", test_sorted);
    1646        2354 :         torture_suite_add_1smb_test(suite, "modify search", test_modify_search);
    1647        2354 :         torture_suite_add_1smb_test(suite, "many dirs", test_many_dirs);
    1648        2354 :         torture_suite_add_1smb_test(suite, "os2 delete", test_os2_delete);
    1649        2354 :         torture_suite_add_1smb_test(suite, "ea list", test_ea_list);
    1650        2354 :         torture_suite_add_1smb_test(suite, "max count", test_max_count);
    1651             : 
    1652        2354 :         return suite;
    1653             : }

Generated by: LCOV version 1.14