LCOV - code coverage report
Current view: top level - source3/torture - test_matching.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 92 106 86.8 %
Date: 2024-04-21 15:09:00 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Samba utility tests
       4             :    Copyright (C) Stefan Metzmacher 2021
       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 "lib/util_matching.h"
      22             : #include "proto.h"
      23             : 
      24           1 : bool run_str_match_mswild(int dummy)
      25             : {
      26           1 :         const char *namelist = "/abc*.txt/xyz*.dat/a0123456789Z/";
      27           1 :         name_compare_entry *name_entries = NULL;
      28           1 :         struct samba_path_matching *pmcs = NULL;
      29           1 :         struct samba_path_matching *pmci = NULL;
      30           1 :         const struct str_match_mswild_name {
      31             :                 const char *name;
      32             :                 ssize_t case_sensitive_idx;
      33             :                 ssize_t case_insensitive_idx;
      34           1 :         } names[] = {{
      35             :                 .name = "/dir/abc123.txt",
      36             :                 .case_sensitive_idx = 0,
      37             :                 .case_insensitive_idx = 0,
      38             :         },{
      39             :                 .name = "/dir/AbC123.TxT",
      40             :                 .case_sensitive_idx = -1,
      41             :                 .case_insensitive_idx = 0,
      42             :         },{
      43             :                 .name = "/dir/xyz123.dat",
      44             :                 .case_sensitive_idx = 1,
      45             :                 .case_insensitive_idx = 1,
      46             :         },{
      47             :                 .name = "/dir/XyZ123.DaT",
      48             :                 .case_sensitive_idx = -1,
      49             :                 .case_insensitive_idx = 1,
      50             :         },{
      51             :                 .name = "/dir/aaa123.jpg",
      52             :                 .case_sensitive_idx = -1,
      53             :                 .case_insensitive_idx = -1,
      54             :         },{
      55             :                 .name = "/dir/a0123456789Z",
      56             :                 .case_sensitive_idx = 2,
      57             :                 .case_insensitive_idx = 2,
      58             :         },{
      59             :                 .name = "/dir/A0123456789z",
      60             :                 .case_sensitive_idx = -1,
      61             :                 .case_insensitive_idx = 2,
      62             :         }};
      63           1 :         NTSTATUS status;
      64           1 :         size_t i;
      65           1 :         bool ret = true;
      66             : 
      67           1 :         d_fprintf(stderr, "namelist: %s\n", namelist);
      68             : 
      69           1 :         set_namearray(&name_entries, namelist);
      70           1 :         SMB_ASSERT(name_entries != NULL);
      71             : 
      72           1 :         status = samba_path_matching_mswild_create(talloc_tos(),
      73             :                                                    true, /* case_sensitive */
      74             :                                                    namelist,
      75             :                                                    &pmcs);
      76           1 :         SMB_ASSERT(NT_STATUS_IS_OK(status));
      77           1 :         status = samba_path_matching_mswild_create(talloc_tos(),
      78             :                                                    false, /* case_sensitive */
      79             :                                                    namelist,
      80             :                                                    &pmci);
      81           1 :         SMB_ASSERT(NT_STATUS_IS_OK(status));
      82             : 
      83             : 
      84           8 :         for (i = 0; i < ARRAY_SIZE(names); i++) {
      85           7 :                 const struct str_match_mswild_name *n = &names[i];
      86           7 :                 bool case_sensitive_match;
      87           7 :                 bool case_insensitive_match;
      88           7 :                 ssize_t cs_match_idx = -1;
      89           7 :                 ssize_t ci_match_idx = -1;
      90           7 :                 ssize_t replace_start = -1;
      91           7 :                 ssize_t replace_end = -1;
      92           7 :                 bool ok = true;
      93             : 
      94           7 :                 case_sensitive_match = is_in_path(n->name,
      95             :                                                   name_entries,
      96             :                                                   true);
      97           7 :                 if (n->case_sensitive_idx != -1) {
      98           7 :                         ok &= case_sensitive_match;
      99             :                 } else {
     100           4 :                         ok &= !case_sensitive_match;
     101             :                 }
     102           7 :                 status = samba_path_matching_check_last_component(pmcs,
     103           0 :                                                                   n->name,
     104             :                                                                   &cs_match_idx,
     105             :                                                                   &replace_start,
     106             :                                                                   &replace_end);
     107           7 :                 SMB_ASSERT(NT_STATUS_IS_OK(status));
     108           7 :                 SMB_ASSERT(replace_start == -1);
     109           7 :                 SMB_ASSERT(replace_end == -1);
     110           7 :                 if (n->case_sensitive_idx != cs_match_idx) {
     111           0 :                         ok = false;
     112             :                 }
     113           7 :                 case_insensitive_match = is_in_path(n->name,
     114             :                                                     name_entries,
     115             :                                                     false);
     116           7 :                 if (n->case_insensitive_idx != -1) {
     117           6 :                         ok &= case_insensitive_match;
     118             :                 } else {
     119           1 :                         ok &= !case_insensitive_match;
     120             :                 }
     121           7 :                 status = samba_path_matching_check_last_component(pmci,
     122           0 :                                                                   n->name,
     123             :                                                                   &ci_match_idx,
     124             :                                                                   &replace_start,
     125             :                                                                   &replace_end);
     126           7 :                 SMB_ASSERT(NT_STATUS_IS_OK(status));
     127           7 :                 SMB_ASSERT(replace_start == -1);
     128           7 :                 SMB_ASSERT(replace_end == -1);
     129           7 :                 if (n->case_insensitive_idx != ci_match_idx) {
     130           0 :                         ok = false;
     131             :                 }
     132             : 
     133           7 :                 d_fprintf(stderr, "name[%s] "
     134             :                           "case_sensitive[TIDX=%zd;MATCH=%u;MIDX=%zd] "
     135             :                           "case_insensitive[TIDX=%zd;MATCH=%u;MIDX=%zd] "
     136             :                           "%s\n",
     137           0 :                           n->name,
     138           0 :                           n->case_sensitive_idx,
     139             :                           case_sensitive_match,
     140             :                           cs_match_idx,
     141           0 :                           n->case_insensitive_idx,
     142             :                           case_insensitive_match,
     143             :                           ci_match_idx,
     144             :                           ok ? "OK" : "FAIL");
     145             : 
     146           7 :                 ret &= ok;
     147             :         }
     148             : 
     149           1 :         return ret;
     150             : }
     151             : 
     152           1 : bool run_str_match_regex_sub1(int dummy)
     153             : {
     154           1 :         const char *invalidlist1 = "/Re7599Ex[0-9].*\\.txt/";
     155           1 :         const char *invalidlist2 = "/Re7599Ex\\([0-9]\\).*\\.\\(txt\\)/";
     156           1 :         const char *invalidlist3 = "/Re7599Ex\\([0-9]).*\\.txt/";
     157           1 :         const char *invalidlist4 = "/Re7599Ex[0-9.*\\.txt/";
     158           1 :         const char *namelist = "/Re7599Ex\\([0-9]\\).*\\.txt/test\\(.*\\).txt/^test\\([0-9]*\\).dat/";
     159           1 :         struct samba_path_matching *pm = NULL;
     160           1 :         const struct str_match_regex_sub1 {
     161             :                 const char *name;
     162             :                 ssize_t match_idx;
     163             :                 ssize_t sub_start;
     164             :                 ssize_t sub_end;
     165           1 :         } names[] = {{
     166             :                 .name = "/dir/Re7599Ex567.txt",
     167             :                 .match_idx = 0,
     168             :                 .sub_start = 13,
     169             :                 .sub_end = 14,
     170             :         },{
     171             :                 .name = "/dir/rE7599eX567.txt",
     172             :                 .match_idx = -1,
     173             :                 .sub_start = -1,
     174             :                 .sub_end = -1,
     175             :         },{
     176             :                 .name = "/dir/Re7599Ex.txt",
     177             :                 .match_idx = -1,
     178             :                 .sub_start = -1,
     179             :                 .sub_end = -1,
     180             :         },{
     181             :                 .name = "/dir/testabc123.txt",
     182             :                 .match_idx = 1,
     183             :                 .sub_start = 9,
     184             :                 .sub_end = 15,
     185             :         },{
     186             :                 .name = "/dir/testabc123.tXt",
     187             :                 .match_idx = -1,
     188             :                 .sub_start = -1,
     189             :                 .sub_end = -1,
     190             :         },{
     191             :                 .name = "/dir/test123.dat",
     192             :                 .match_idx = 2,
     193             :                 .sub_start = 9,
     194             :                 .sub_end = 12,
     195             :         },{
     196             :                 .name = "/dir/tEst123.dat",
     197             :                 .match_idx = -1,
     198             :                 .sub_start = -1,
     199             :                 .sub_end = -1,
     200             :         }};
     201           1 :         NTSTATUS status;
     202           1 :         size_t i;
     203           1 :         bool ret = true;
     204             : 
     205           1 :         d_fprintf(stderr, "invalidlist1: %s\n", invalidlist1);
     206           1 :         status = samba_path_matching_regex_sub1_create(talloc_tos(),
     207             :                                                        invalidlist1,
     208             :                                                        &pm);
     209           1 :         SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
     210           1 :         d_fprintf(stderr, "invalidlist2: %s\n", invalidlist2);
     211           1 :         status = samba_path_matching_regex_sub1_create(talloc_tos(),
     212             :                                                        invalidlist2,
     213             :                                                        &pm);
     214           1 :         SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
     215           1 :         d_fprintf(stderr, "invalidlist3: %s\n", invalidlist3);
     216           1 :         status = samba_path_matching_regex_sub1_create(talloc_tos(),
     217             :                                                        invalidlist3,
     218             :                                                        &pm);
     219           1 :         SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
     220           1 :         d_fprintf(stderr, "invalidlist4: %s\n", invalidlist4);
     221           1 :         status = samba_path_matching_regex_sub1_create(talloc_tos(),
     222             :                                                        invalidlist4,
     223             :                                                        &pm);
     224           1 :         SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
     225             : 
     226           1 :         d_fprintf(stderr, "namelist: %s\n", namelist);
     227           1 :         status = samba_path_matching_regex_sub1_create(talloc_tos(),
     228             :                                                        namelist,
     229             :                                                        &pm);
     230           1 :         SMB_ASSERT(NT_STATUS_IS_OK(status));
     231             : 
     232           8 :         for (i = 0; i < ARRAY_SIZE(names); i++) {
     233           7 :                 const struct str_match_regex_sub1 *n = &names[i];
     234           7 :                 ssize_t match_idx = -1;
     235           7 :                 ssize_t replace_start = -1;
     236           7 :                 ssize_t replace_end = -1;
     237           7 :                 bool ok = true;
     238             : 
     239          14 :                 status = samba_path_matching_check_last_component(pm,
     240           7 :                                                                   n->name,
     241             :                                                                   &match_idx,
     242             :                                                                   &replace_start,
     243             :                                                                   &replace_end);
     244           7 :                 SMB_ASSERT(NT_STATUS_IS_OK(status));
     245           7 :                 if (match_idx == -1) {
     246           4 :                         SMB_ASSERT(replace_start == -1);
     247           4 :                         SMB_ASSERT(replace_end == -1);
     248             :                 }
     249           7 :                 if (n->match_idx != match_idx) {
     250           0 :                         ok = false;
     251             :                 }
     252           7 :                 if (n->sub_start != replace_start) {
     253           0 :                         ok = false;
     254             :                 }
     255           7 :                 if (n->sub_end != replace_end) {
     256           0 :                         ok = false;
     257             :                 }
     258             : 
     259           7 :                 d_fprintf(stderr, "name[%s] "
     260             :                           "T[IDX=%zd;START=%zd;END=%zd] "
     261             :                           "M[[IDX=%zd;START=%zd;END=%zd] "
     262             :                           "%s\n",
     263           0 :                           n->name,
     264           0 :                           n->match_idx,
     265           0 :                           n->sub_start,
     266           0 :                           n->sub_end,
     267             :                           match_idx,
     268             :                           replace_start,
     269             :                           replace_end,
     270             :                           ok ? "OK" : "FAIL");
     271             : 
     272           7 :                 ret &= ok;
     273             :         }
     274             : 
     275           1 :         return ret;
     276             : }

Generated by: LCOV version 1.14