1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22 #include "curlcheck.h"
23
24 #include "curl_fnmatch.h"
25
26 #define MATCH CURL_FNMATCH_MATCH
27 #define NOMATCH CURL_FNMATCH_NOMATCH
28 #define RE_ERR CURL_FNMATCH_FAIL
29
30 struct testcase {
31 const char *pattern;
32 const char *string;
33 int result;
34 };
35
36 static const struct testcase tests[] = {
37 /* brackets syntax */
38 { "\\[", "[", MATCH },
39 { "[", "[", RE_ERR },
40 { "[]", "[]", RE_ERR },
41 { "[][]", "[", MATCH },
42 { "[][]", "]", MATCH },
43 { "[[]", "[", MATCH },
44 { "[[[]", "[", MATCH },
45 { "[[[[]", "[", MATCH },
46 { "[[[[]", "[", MATCH },
47
48 { "[][[]", "]", MATCH },
49 { "[][[[]", "[", MATCH },
50 { "[[]", "]", NOMATCH },
51
52 { "[a-z]", "a", MATCH },
53 { "[a-z]", "A", NOMATCH },
54 { "?[a-z]", "?Z", NOMATCH },
55 { "[A-Z]", "C", MATCH },
56 { "[A-Z]", "c", NOMATCH },
57 { "[0-9]", "7", MATCH },
58 { "[7-8]", "7", MATCH },
59 { "[7-]", "7", MATCH },
60 { "[7-]", "-", MATCH },
61 { "[7-]", "[", NOMATCH },
62 { "[a-bA-F]", "F", MATCH },
63 { "[a-bA-B9]", "9", MATCH },
64 { "[a-bA-B98]", "8", MATCH },
65 { "[a-bA-B98]", "C", NOMATCH },
66 { "[a-bA-Z9]", "F", MATCH },
67 { "[a-bA-Z9]ero*", "Zero chance.", MATCH },
68 { "S[a-][x]opho*", "Saxophone", MATCH },
69 { "S[a-][x]opho*", "SaXophone", NOMATCH },
70 { "S[a-][x]*.txt", "S-x.txt", MATCH },
71 { "[\\a-\\b]", "a", MATCH },
72 { "[\\a-\\b]", "b", MATCH },
73 { "[?*[][?*[][?*[]", "?*[", MATCH },
74 { "[][?*-]", "]", MATCH },
75 { "[][?*-]", "[", MATCH },
76 { "[][?*-]", "?", MATCH },
77 { "[][?*-]", "*", MATCH },
78 { "[][?*-]", "-", MATCH },
79 { "[]?*-]", "-", MATCH },
80 { "?/b/c", "a/b/c", MATCH },
81 { "^_{}~", "^_{}~", MATCH },
82 { "!#%+,-./01234567889", "!#%+,-./01234567889", MATCH },
83 { "PQRSTUVWXYZ]abcdefg", "PQRSTUVWXYZ]abcdefg", MATCH },
84 { ":;=@ABCDEFGHIJKLMNO", ":;=@ABCDEFGHIJKLMNO", MATCH },
85
86 /* negate */
87 { "[!a]", "b", MATCH },
88 { "[!a]", "a", NOMATCH },
89 { "[^a]", "b", MATCH },
90 { "[^a]", "a", NOMATCH },
91 { "[^a-z0-9A-Z]", "a", NOMATCH },
92 { "[^a-z0-9A-Z]", "-", MATCH },
93 { "curl[!a-z]lib", "curl lib", MATCH },
94 { "curl[! ]lib", "curl lib", NOMATCH },
95 { "[! ][ ]", " ", NOMATCH },
96 { "[! ][ ]", "a ", MATCH },
97 { "*[^a].t?t", "a.txt", NOMATCH },
98 { "*[^a].t?t", "ba.txt", NOMATCH },
99 { "*[^a].t?t", "ab.txt", MATCH },
100 { "*[^a]", "", MATCH },
101 { "[!ÿ]", "", MATCH },
102 { "[!?*[]", "?", NOMATCH },
103 { "[!!]", "!", NOMATCH },
104 { "[!!]", "x", MATCH },
105
106 { "[[:alpha:]]", "a", MATCH },
107 { "[[:alpha:]]", "9", NOMATCH },
108 { "[[:alnum:]]", "a", MATCH },
109 { "[[:alnum:]]", "[", NOMATCH },
110 { "[[:alnum:]]", "]", NOMATCH },
111 { "[[:alnum:]]", "9", MATCH },
112 { "[[:digit:]]", "9", MATCH },
113 { "[[:xdigit:]]", "9", MATCH },
114 { "[[:xdigit:]]", "F", MATCH },
115 { "[[:xdigit:]]", "G", NOMATCH },
116 { "[[:upper:]]", "U", MATCH },
117 { "[[:upper:]]", "u", NOMATCH },
118 { "[[:lower:]]", "l", MATCH },
119 { "[[:lower:]]", "L", NOMATCH },
120 { "[[:print:]]", "L", MATCH },
121 { "[[:print:]]", "\10", NOMATCH },
122 { "[[:print:]]", "\10", NOMATCH },
123 { "[[:space:]]", " ", MATCH },
124 { "[[:space:]]", "x", NOMATCH },
125 { "[[:graph:]]", " ", NOMATCH },
126 { "[[:graph:]]", "x", MATCH },
127 { "[[:blank:]]", "\t", MATCH },
128 { "[[:blank:]]", " ", MATCH },
129 { "[[:blank:]]", "\r", NOMATCH },
130 { "[^[:blank:]]", "\t", NOMATCH },
131 { "[^[:print:]]", "\10", MATCH },
132 { "[[:lower:]][[:lower:]]", "ll", MATCH },
133
134 { "Curl[[:blank:]];-)", "Curl ;-)", MATCH },
135 { "*[[:blank:]]*", " ", MATCH },
136 { "*[[:blank:]]*", "", NOMATCH },
137 { "*[[:blank:]]*", "hi, im_Pavel", MATCH },
138
139 /* common using */
140 { "filename.dat", "filename.dat", MATCH },
141 { "*curl*", "lets use curl!!", MATCH },
142 { "filename.txt", "filename.dat", NOMATCH },
143 { "*.txt", "text.txt", MATCH },
144 { "*.txt", "a.txt", MATCH },
145 { "*.txt", ".txt", MATCH },
146 { "*.txt", "txt", NOMATCH },
147 { "??.txt", "99.txt", MATCH },
148 { "??.txt", "a99.txt", NOMATCH },
149 { "?.???", "a.txt", MATCH },
150 { "*.???", "somefile.dat", MATCH },
151 { "*.???", "photo.jpeg", NOMATCH },
152 { ".*", ".htaccess", MATCH },
153 { ".*", ".", MATCH },
154 { ".*", "..", MATCH },
155
156 /* many stars => one star */
157 { "**.txt", "text.txt", MATCH },
158 { "***.txt", "t.txt", MATCH },
159 { "****.txt", ".txt", MATCH },
160
161 /* empty string or pattern */
162 { "", "", MATCH },
163 { "", "hello", NOMATCH },
164 { "file", "", NOMATCH },
165 { "?", "", NOMATCH },
166 { "*", "", MATCH },
167 { "x", "", NOMATCH },
168
169 /* backslash */
170 { "\\", "\\", RE_ERR },
171 { "\\\\", "\\", MATCH },
172 { "\\\\", "\\\\", NOMATCH },
173 { "\\?", "?", MATCH },
174 { "\\*", "*", MATCH },
175 { "?.txt", "?.txt", MATCH },
176 { "*.txt", "*.txt", MATCH },
177 { "\\?.txt", "?.txt", MATCH },
178 { "\\*.txt", "*.txt", MATCH },
179 { "\\?.txt", "x.txt", NOMATCH },
180 { "\\*.txt", "x.txt", NOMATCH },
181 { "\\*\\\\.txt", "*\\.txt", MATCH },
182 { "*\\**\\?*\\\\*", "cc*cc?cc\\cc*cc", MATCH },
183 { "*\\**\\?*\\\\*", "cc*cc?cccc", NOMATCH },
184 { "*\\**\\?*\\\\*", "cc*cc?cc\\cc*cc", MATCH },
185 { "*\\?*\\**", "cc?c*c", MATCH },
186 { "*\\?*\\**curl*", "cc?c*curl", MATCH },
187 { "*\\?*\\**", "cc?cc", NOMATCH },
188 { "\\\"\\$\\&\\'\\(\\)", "\"$&'()", MATCH },
189 { "\\*\\?\\[\\\\\\`\\|", "*?[\\`|", MATCH },
190 { "[\\a\\b]c", "ac", MATCH },
191 { "[\\a\\b]c", "bc", MATCH },
192 { "[\\a\\b]d", "bc", NOMATCH },
193 { "[a-bA-B\\?]", "?", MATCH },
194 { "cu[a-ab-b\\r]l", "curl", MATCH },
195 { "[\\a-z]", "c", MATCH },
196
197 { "?*?*?.*?*", "abc.c", MATCH },
198 { "?*?*?.*?*", "abcc", NOMATCH },
199 { "?*?*?.*?*", "abc.", NOMATCH },
200 { "?*?*?.*?*", "abc.c++", MATCH },
201 { "?*?*?.*?*", "abcdef.c++", MATCH },
202 { "?*?*?.?", "abcdef.c", MATCH },
203 { "?*?*?.?", "abcdef.cd", NOMATCH },
204
205 { "Lindmätarv", "Lindmätarv", MATCH },
206
207 { "", "", MATCH },
208 {"**]*[*[\x13]**[*\x13)]*]*[**[*\x13~r-]*]**[.*]*[\xe3\xe3\xe3\xe3\xe3\xe3"
209 "\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3"
210 "\xe3\xe3\xe3\xe3\xe3*[\x13]**[*\x13)]*]*[*[\x13]*[~r]*]*\xba\x13\xa6~b-]*",
211 "a", NOMATCH }
212 };
213
unit_setup(void)214 static CURLcode unit_setup(void)
215 {
216 return CURLE_OK;
217 }
218
unit_stop(void)219 static void unit_stop(void)
220 {
221 }
222
223 UNITTEST_START
224
225 int testnum = sizeof(tests) / sizeof(struct testcase);
226 int i, rc;
227
228 for(i = 0; i < testnum; i++) {
229 rc = Curl_fnmatch(NULL, tests[i].pattern, tests[i].string);
230 if(rc != tests[i].result) {
231 printf("Curl_fnmatch(\"%s\", \"%s\") should return %d (returns %d)\n",
232 tests[i].pattern, tests[i].string, tests[i].result, rc);
233 fail("pattern mismatch");
234 }
235 }
236
237 UNITTEST_STOP
238