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 0 : bool run_str_match_mswild(int dummy)
25 : {
26 0 : const char *namelist = "/abc*.txt/xyz*.dat/a0123456789Z/";
27 0 : name_compare_entry *name_entries = NULL;
28 0 : struct samba_path_matching *pmcs = NULL;
29 0 : struct samba_path_matching *pmci = NULL;
30 : const struct str_match_mswild_name {
31 : const char *name;
32 : ssize_t case_sensitive_idx;
33 : ssize_t case_insensitive_idx;
34 0 : } 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 : NTSTATUS status;
64 : size_t i;
65 0 : bool ret = true;
66 :
67 0 : d_fprintf(stderr, "namelist: %s\n", namelist);
68 :
69 0 : set_namearray(&name_entries, namelist);
70 0 : SMB_ASSERT(name_entries != NULL);
71 :
72 0 : status = samba_path_matching_mswild_create(talloc_tos(),
73 : true, /* case_sensitive */
74 : namelist,
75 : &pmcs);
76 0 : SMB_ASSERT(NT_STATUS_IS_OK(status));
77 0 : status = samba_path_matching_mswild_create(talloc_tos(),
78 : false, /* case_sensitive */
79 : namelist,
80 : &pmci);
81 0 : SMB_ASSERT(NT_STATUS_IS_OK(status));
82 :
83 :
84 0 : for (i = 0; i < ARRAY_SIZE(names); i++) {
85 0 : const struct str_match_mswild_name *n = &names[i];
86 : bool case_sensitive_match;
87 : bool case_insensitive_match;
88 0 : ssize_t cs_match_idx = -1;
89 0 : ssize_t ci_match_idx = -1;
90 0 : ssize_t replace_start = -1;
91 0 : ssize_t replace_end = -1;
92 0 : bool ok = true;
93 :
94 0 : case_sensitive_match = is_in_path(n->name,
95 : name_entries,
96 : true);
97 0 : if (n->case_sensitive_idx != -1) {
98 0 : ok &= case_sensitive_match;
99 : } else {
100 0 : ok &= !case_sensitive_match;
101 : }
102 0 : status = samba_path_matching_check_last_component(pmcs,
103 0 : n->name,
104 : &cs_match_idx,
105 : &replace_start,
106 : &replace_end);
107 0 : SMB_ASSERT(NT_STATUS_IS_OK(status));
108 0 : SMB_ASSERT(replace_start == -1);
109 0 : SMB_ASSERT(replace_end == -1);
110 0 : if (n->case_sensitive_idx != cs_match_idx) {
111 0 : ok = false;
112 : }
113 0 : case_insensitive_match = is_in_path(n->name,
114 : name_entries,
115 : false);
116 0 : if (n->case_insensitive_idx != -1) {
117 0 : ok &= case_insensitive_match;
118 : } else {
119 0 : ok &= !case_insensitive_match;
120 : }
121 0 : status = samba_path_matching_check_last_component(pmci,
122 0 : n->name,
123 : &ci_match_idx,
124 : &replace_start,
125 : &replace_end);
126 0 : SMB_ASSERT(NT_STATUS_IS_OK(status));
127 0 : SMB_ASSERT(replace_start == -1);
128 0 : SMB_ASSERT(replace_end == -1);
129 0 : if (n->case_insensitive_idx != ci_match_idx) {
130 0 : ok = false;
131 : }
132 :
133 0 : 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 0 : ret &= ok;
147 : }
148 :
149 0 : return ret;
150 : }
151 :
152 0 : bool run_str_match_regex_sub1(int dummy)
153 : {
154 0 : const char *invalidlist1 = "/Re7599Ex[0-9].*\\.txt/";
155 0 : const char *invalidlist2 = "/Re7599Ex\\([0-9]\\).*\\.\\(txt\\)/";
156 0 : const char *invalidlist3 = "/Re7599Ex\\([0-9]).*\\.txt/";
157 0 : const char *invalidlist4 = "/Re7599Ex[0-9.*\\.txt/";
158 0 : const char *namelist = "/Re7599Ex\\([0-9]\\).*\\.txt/test\\(.*\\).txt/^test\\([0-9]*\\).dat/";
159 0 : struct samba_path_matching *pm = NULL;
160 : 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 0 : } 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 : NTSTATUS status;
202 : size_t i;
203 0 : bool ret = true;
204 :
205 0 : d_fprintf(stderr, "invalidlist1: %s\n", invalidlist1);
206 0 : status = samba_path_matching_regex_sub1_create(talloc_tos(),
207 : invalidlist1,
208 : &pm);
209 0 : SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
210 0 : d_fprintf(stderr, "invalidlist2: %s\n", invalidlist2);
211 0 : status = samba_path_matching_regex_sub1_create(talloc_tos(),
212 : invalidlist2,
213 : &pm);
214 0 : SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
215 0 : d_fprintf(stderr, "invalidlist3: %s\n", invalidlist3);
216 0 : status = samba_path_matching_regex_sub1_create(talloc_tos(),
217 : invalidlist3,
218 : &pm);
219 0 : SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
220 0 : d_fprintf(stderr, "invalidlist4: %s\n", invalidlist4);
221 0 : status = samba_path_matching_regex_sub1_create(talloc_tos(),
222 : invalidlist4,
223 : &pm);
224 0 : SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
225 :
226 0 : d_fprintf(stderr, "namelist: %s\n", namelist);
227 0 : status = samba_path_matching_regex_sub1_create(talloc_tos(),
228 : namelist,
229 : &pm);
230 0 : SMB_ASSERT(NT_STATUS_IS_OK(status));
231 :
232 0 : for (i = 0; i < ARRAY_SIZE(names); i++) {
233 0 : const struct str_match_regex_sub1 *n = &names[i];
234 0 : ssize_t match_idx = -1;
235 0 : ssize_t replace_start = -1;
236 0 : ssize_t replace_end = -1;
237 0 : bool ok = true;
238 :
239 0 : status = samba_path_matching_check_last_component(pm,
240 0 : n->name,
241 : &match_idx,
242 : &replace_start,
243 : &replace_end);
244 0 : SMB_ASSERT(NT_STATUS_IS_OK(status));
245 0 : if (match_idx == -1) {
246 0 : SMB_ASSERT(replace_start == -1);
247 0 : SMB_ASSERT(replace_end == -1);
248 : }
249 0 : if (n->match_idx != match_idx) {
250 0 : ok = false;
251 : }
252 0 : if (n->sub_start != replace_start) {
253 0 : ok = false;
254 : }
255 0 : if (n->sub_end != replace_end) {
256 0 : ok = false;
257 : }
258 :
259 0 : 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 0 : ret &= ok;
273 : }
274 :
275 0 : return ret;
276 : }
|