rpc_server3: Use fdopen_keepfd()
[Samba.git] / source3 / torture / test_matching.c
blob647b758a34af83e66ed1f49a949bb3bfa2c3364e
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility tests
4 Copyright (C) Stefan Metzmacher 2021
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "lib/util_matching.h"
22 #include "proto.h"
24 bool run_str_match_mswild(int dummy)
26 const char *namelist = "/abc*.txt/xyz*.dat/a0123456789Z/";
27 name_compare_entry *name_entries = NULL;
28 struct samba_path_matching *pmcs = NULL;
29 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 } 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 bool ret = true;
67 d_fprintf(stderr, "namelist: %s\n", namelist);
69 set_namearray(&name_entries, namelist);
70 SMB_ASSERT(name_entries != NULL);
72 status = samba_path_matching_mswild_create(talloc_tos(),
73 true, /* case_sensitive */
74 namelist,
75 &pmcs);
76 SMB_ASSERT(NT_STATUS_IS_OK(status));
77 status = samba_path_matching_mswild_create(talloc_tos(),
78 false, /* case_sensitive */
79 namelist,
80 &pmci);
81 SMB_ASSERT(NT_STATUS_IS_OK(status));
84 for (i = 0; i < ARRAY_SIZE(names); i++) {
85 const struct str_match_mswild_name *n = &names[i];
86 bool case_sensitive_match;
87 bool case_insensitive_match;
88 ssize_t cs_match_idx = -1;
89 ssize_t ci_match_idx = -1;
90 ssize_t replace_start = -1;
91 ssize_t replace_end = -1;
92 bool ok = true;
94 case_sensitive_match = is_in_path(n->name,
95 name_entries,
96 true);
97 if (n->case_sensitive_idx != -1) {
98 ok &= case_sensitive_match;
99 } else {
100 ok &= !case_sensitive_match;
102 status = samba_path_matching_check_last_component(pmcs,
103 n->name,
104 &cs_match_idx,
105 &replace_start,
106 &replace_end);
107 SMB_ASSERT(NT_STATUS_IS_OK(status));
108 SMB_ASSERT(replace_start == -1);
109 SMB_ASSERT(replace_end == -1);
110 if (n->case_sensitive_idx != cs_match_idx) {
111 ok = false;
113 case_insensitive_match = is_in_path(n->name,
114 name_entries,
115 false);
116 if (n->case_insensitive_idx != -1) {
117 ok &= case_insensitive_match;
118 } else {
119 ok &= !case_insensitive_match;
121 status = samba_path_matching_check_last_component(pmci,
122 n->name,
123 &ci_match_idx,
124 &replace_start,
125 &replace_end);
126 SMB_ASSERT(NT_STATUS_IS_OK(status));
127 SMB_ASSERT(replace_start == -1);
128 SMB_ASSERT(replace_end == -1);
129 if (n->case_insensitive_idx != ci_match_idx) {
130 ok = false;
133 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 n->name,
138 n->case_sensitive_idx,
139 case_sensitive_match,
140 cs_match_idx,
141 n->case_insensitive_idx,
142 case_insensitive_match,
143 ci_match_idx,
144 ok ? "OK" : "FAIL");
146 ret &= ok;
149 return ret;
152 bool run_str_match_regex_sub1(int dummy)
154 const char *invalidlist1 = "/Re7599Ex[0-9].*\\.txt/";
155 const char *invalidlist2 = "/Re7599Ex\\([0-9]\\).*\\.\\(txt\\)/";
156 const char *invalidlist3 = "/Re7599Ex\\([0-9]).*\\.txt/";
157 const char *invalidlist4 = "/Re7599Ex[0-9.*\\.txt/";
158 const char *namelist = "/Re7599Ex\\([0-9]\\).*\\.txt/test\\(.*\\).txt/^test\\([0-9]*\\).dat/";
159 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 } names[] = {{
166 .name = "/dir/Re7599Ex567.txt",
167 .match_idx = 0,
168 .sub_start = 13,
169 .sub_end = 14,
171 .name = "/dir/rE7599eX567.txt",
172 .match_idx = -1,
173 .sub_start = -1,
174 .sub_end = -1,
176 .name = "/dir/Re7599Ex.txt",
177 .match_idx = -1,
178 .sub_start = -1,
179 .sub_end = -1,
181 .name = "/dir/testabc123.txt",
182 .match_idx = 1,
183 .sub_start = 9,
184 .sub_end = 15,
186 .name = "/dir/testabc123.tXt",
187 .match_idx = -1,
188 .sub_start = -1,
189 .sub_end = -1,
191 .name = "/dir/test123.dat",
192 .match_idx = 2,
193 .sub_start = 9,
194 .sub_end = 12,
196 .name = "/dir/tEst123.dat",
197 .match_idx = -1,
198 .sub_start = -1,
199 .sub_end = -1,
201 NTSTATUS status;
202 size_t i;
203 bool ret = true;
205 d_fprintf(stderr, "invalidlist1: %s\n", invalidlist1);
206 status = samba_path_matching_regex_sub1_create(talloc_tos(),
207 invalidlist1,
208 &pm);
209 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
210 d_fprintf(stderr, "invalidlist2: %s\n", invalidlist2);
211 status = samba_path_matching_regex_sub1_create(talloc_tos(),
212 invalidlist2,
213 &pm);
214 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
215 d_fprintf(stderr, "invalidlist3: %s\n", invalidlist3);
216 status = samba_path_matching_regex_sub1_create(talloc_tos(),
217 invalidlist3,
218 &pm);
219 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
220 d_fprintf(stderr, "invalidlist4: %s\n", invalidlist4);
221 status = samba_path_matching_regex_sub1_create(talloc_tos(),
222 invalidlist4,
223 &pm);
224 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER));
226 d_fprintf(stderr, "namelist: %s\n", namelist);
227 status = samba_path_matching_regex_sub1_create(talloc_tos(),
228 namelist,
229 &pm);
230 SMB_ASSERT(NT_STATUS_IS_OK(status));
232 for (i = 0; i < ARRAY_SIZE(names); i++) {
233 const struct str_match_regex_sub1 *n = &names[i];
234 ssize_t match_idx = -1;
235 ssize_t replace_start = -1;
236 ssize_t replace_end = -1;
237 bool ok = true;
239 status = samba_path_matching_check_last_component(pm,
240 n->name,
241 &match_idx,
242 &replace_start,
243 &replace_end);
244 SMB_ASSERT(NT_STATUS_IS_OK(status));
245 if (match_idx == -1) {
246 SMB_ASSERT(replace_start == -1);
247 SMB_ASSERT(replace_end == -1);
249 if (n->match_idx != match_idx) {
250 ok = false;
252 if (n->sub_start != replace_start) {
253 ok = false;
255 if (n->sub_end != replace_end) {
256 ok = false;
259 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 n->name,
264 n->match_idx,
265 n->sub_start,
266 n->sub_end,
267 match_idx,
268 replace_start,
269 replace_end,
270 ok ? "OK" : "FAIL");
272 ret &= ok;
275 return ret;