xref: /curl/tests/unit/unit1307.c (revision 329a8e9d)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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.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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "curlcheck.h"
25 
26 #include "curl_fnmatch.h"
27 
unit_setup(void)28 static CURLcode unit_setup(void)
29 {
30   return CURLE_OK;
31 }
32 
unit_stop(void)33 static void unit_stop(void)
34 {
35 }
36 
37 #ifndef CURL_DISABLE_FTP
38 
39 /*
40    CURL_FNMATCH_MATCH    0
41    CURL_FNMATCH_NOMATCH  1
42    CURL_FNMATCH_FAIL     2
43  */
44 
45 #define MATCH   CURL_FNMATCH_MATCH
46 #define NOMATCH CURL_FNMATCH_NOMATCH
47 
48 #define LINUX_DIFFER 0x80
49 #define LINUX_SHIFT 8
50 #define LINUX_MATCH ((CURL_FNMATCH_MATCH << LINUX_SHIFT) | LINUX_DIFFER)
51 #define LINUX_NOMATCH ((CURL_FNMATCH_NOMATCH << LINUX_SHIFT) | LINUX_DIFFER)
52 #define LINUX_FAIL ((CURL_FNMATCH_FAIL << LINUX_SHIFT) | LINUX_DIFFER)
53 
54 #define MAC_DIFFER 0x40
55 #define MAC_SHIFT 16
56 #define MAC_MATCH ((CURL_FNMATCH_MATCH << MAC_SHIFT) | MAC_DIFFER)
57 #define MAC_NOMATCH ((CURL_FNMATCH_NOMATCH << MAC_SHIFT) | MAC_DIFFER)
58 #define MAC_FAIL ((CURL_FNMATCH_FAIL << MAC_SHIFT) | MAC_DIFFER)
59 
60 struct testcase {
61   const char *pattern;
62   const char *string;
63   int result;
64 };
65 
66 static const struct testcase tests[] = {
67   /* brackets syntax */
68   {"*[*[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
69    "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
70    "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\001\177[[[[[[[[[[[[[[[[[[[[[",
71    "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
72    "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
73    "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[",
74    NOMATCH|MAC_FAIL},
75 
76   { "\\[",                      "[",                      MATCH },
77   { "[",                        "[",             NOMATCH|LINUX_MATCH|MAC_FAIL},
78   { "[]",                       "[]",            NOMATCH|LINUX_MATCH|MAC_FAIL},
79   { "[][]",                     "[",                      MATCH },
80   { "[][]",                     "]",                      MATCH },
81   { "[[]",                      "[",                      MATCH },
82   { "[[[]",                     "[",                      MATCH },
83   { "[[[[]",                    "[",                      MATCH },
84   { "[[[[]",                    "[",                      MATCH },
85 
86   { "[][[]",                    "]",                      MATCH },
87   { "[][[[]",                   "[",                      MATCH },
88   { "[[]",                      "]",                      NOMATCH },
89 
90   { "[a@]",                     "a",                      MATCH },
91 
92   { "[a-z]",                    "a",                      MATCH },
93   { "[a-z]",                    "A",                      NOMATCH },
94   { "?[a-z]",                   "?Z",                     NOMATCH },
95   { "[A-Z]",                    "C",                      MATCH },
96   { "[A-Z]",                    "c",                      NOMATCH },
97   { "[0-9]",                    "7",                      MATCH },
98   { "[7-8]",                    "7",                      MATCH },
99   { "[7-]",                     "7",                      MATCH },
100   { "[7-]",                     "-",                      MATCH },
101   { "[7-]",                     "[",                      NOMATCH },
102   { "[a-bA-F]",                 "F",                      MATCH },
103   { "[a-bA-B9]",                "9",                      MATCH },
104   { "[a-bA-B98]",               "8",                      MATCH },
105   { "[a-bA-B98]",               "C",                      NOMATCH },
106   { "[a-bA-Z9]",                "F",                      MATCH },
107   { "[a-bA-Z9]ero*",            "Zero chance.",           MATCH },
108   { "S[a-][x]opho*",            "Saxophone",              MATCH },
109   { "S[a-][x]opho*",            "SaXophone",              NOMATCH },
110   { "S[a-][x]*.txt",            "S-x.txt",                MATCH },
111   { "[\\a-\\b]",                "a",                      MATCH },
112   { "[\\a-\\b]",                "b",                      MATCH },
113   { "[?*[][?*[][?*[]",          "?*[",                    MATCH },
114   { "[][?*-]",                  "]",                      MATCH },
115   { "[][?*-]",                  "[",                      MATCH },
116   { "[][?*-]",                  "?",                      MATCH },
117   { "[][?*-]",                  "*",                      MATCH },
118   { "[][?*-]",                  "-",                      MATCH },
119   { "[]?*-]",                   "-",                      MATCH },
120   { "[\xFF]",                   "\xFF", MATCH|LINUX_FAIL|MAC_FAIL},
121   { "?/b/c",                    "a/b/c",                  MATCH },
122   { "^_{}~",                    "^_{}~",                  MATCH },
123   { "!#%+,-./01234567889",      "!#%+,-./01234567889",    MATCH },
124   { "PQRSTUVWXYZ]abcdefg",      "PQRSTUVWXYZ]abcdefg",    MATCH },
125   { ":;=@ABCDEFGHIJKLMNO",      ":;=@ABCDEFGHIJKLMNO",    MATCH },
126 
127   /* negate */
128   { "[!a]",                     "b",                      MATCH },
129   { "[!a]",                     "a",                      NOMATCH },
130   { "[^a]",                     "b",                      MATCH },
131   { "[^a]",                     "a",                      NOMATCH },
132   { "[^a-z0-9A-Z]",             "a",                      NOMATCH },
133   { "[^a-z0-9A-Z]",             "-",                      MATCH },
134   { "curl[!a-z]lib",            "curl lib",               MATCH },
135   { "curl[! ]lib",              "curl lib",               NOMATCH },
136   { "[! ][ ]",                  "  ",                     NOMATCH },
137   { "[! ][ ]",                  "a ",                     MATCH },
138   { "*[^a].t?t",                "a.txt",                  NOMATCH },
139   { "*[^a].t?t",                "ba.txt",                 NOMATCH },
140   { "*[^a].t?t",                "ab.txt",                 MATCH },
141   { "*[^a]",                    "",                       NOMATCH },
142   { "[!\xFF]",                  "",             NOMATCH|LINUX_FAIL},
143   { "[!\xFF]",                  "\xFF",  NOMATCH|LINUX_FAIL|MAC_FAIL},
144   { "[!\xFF]",                  "a",      MATCH|LINUX_FAIL|MAC_FAIL},
145   { "[!?*[]",                   "?",                      NOMATCH },
146   { "[!!]",                     "!",                      NOMATCH },
147   { "[!!]",                     "x",                      MATCH },
148 
149   { "[[:alpha:]]",              "a",                      MATCH },
150   { "[[:alpha:]]",              "9",                      NOMATCH },
151   { "[[:alnum:]]",              "a",                      MATCH },
152   { "[[:alnum:]]",              "[",                      NOMATCH },
153   { "[[:alnum:]]",              "]",                      NOMATCH },
154   { "[[:alnum:]]",              "9",                      MATCH },
155   { "[[:digit:]]",              "9",                      MATCH },
156   { "[[:xdigit:]]",             "9",                      MATCH },
157   { "[[:xdigit:]]",             "F",                      MATCH },
158   { "[[:xdigit:]]",             "G",                      NOMATCH },
159   { "[[:upper:]]",              "U",                      MATCH },
160   { "[[:upper:]]",              "u",                      NOMATCH },
161   { "[[:lower:]]",              "l",                      MATCH },
162   { "[[:lower:]]",              "L",                      NOMATCH },
163   { "[[:print:]]",              "L",                      MATCH },
164   { "[[:print:]]",              "\10",                    NOMATCH },
165   { "[[:print:]]",              "\10",                    NOMATCH },
166   { "[[:space:]]",              " ",                      MATCH },
167   { "[[:space:]]",              "x",                      NOMATCH },
168   { "[[:graph:]]",              " ",                      NOMATCH },
169   { "[[:graph:]]",              "x",                      MATCH },
170   { "[[:blank:]]",              "\t",                     MATCH },
171   { "[[:blank:]]",              " ",                      MATCH },
172   { "[[:blank:]]",              "\r",                     NOMATCH },
173   { "[^[:blank:]]",             "\t",                     NOMATCH },
174   { "[^[:print:]]",             "\10",                    MATCH },
175   { "[[:lower:]][[:lower:]]",   "ll",                     MATCH },
176   { "[[:foo:]]",                "bar",                    NOMATCH|MAC_FAIL},
177   { "[[:foo:]]",                "f]",         MATCH|LINUX_NOMATCH|MAC_FAIL},
178 
179   { "Curl[[:blank:]];-)",       "Curl ;-)",               MATCH },
180   { "*[[:blank:]]*",            " ",                      MATCH },
181   { "*[[:blank:]]*",            "",                       NOMATCH },
182   { "*[[:blank:]]*",            "hi, im_Pavel",           MATCH },
183 
184   /* common using */
185   { "filename.dat",             "filename.dat",           MATCH },
186   { "*curl*",                   "lets use curl!!",        MATCH },
187   { "filename.txt",             "filename.dat",           NOMATCH },
188   { "*.txt",                    "text.txt",               MATCH },
189   { "*.txt",                    "a.txt",                  MATCH },
190   { "*.txt",                    ".txt",                   MATCH },
191   { "*.txt",                    "txt",                    NOMATCH },
192   { "??.txt",                   "99.txt",                 MATCH },
193   { "??.txt",                   "a99.txt",                NOMATCH },
194   { "?.???",                    "a.txt",                  MATCH },
195   { "*.???",                    "somefile.dat",           MATCH },
196   { "*.???",                    "photo.jpeg",             NOMATCH },
197   { ".*",                       ".htaccess",              MATCH },
198   { ".*",                       ".",                      MATCH },
199   { ".*",                       "..",                     MATCH },
200 
201   /* many stars => one star */
202   { "**.txt",                   "text.txt",               MATCH },
203   { "***.txt",                  "t.txt",                  MATCH },
204   { "****.txt",                 ".txt",                   MATCH },
205 
206   /* empty string or pattern */
207   { "",                         "",                       MATCH },
208   { "",                         "hello",                  NOMATCH },
209   { "file",                     "",                       NOMATCH  },
210   { "?",                        "",                       NOMATCH },
211   { "*",                        "",                       MATCH },
212   { "x",                        "",                       NOMATCH },
213 
214   /* backslash */
215   { "\\",                       "\\",                     MATCH|LINUX_NOMATCH},
216   { "\\\\",                     "\\",                     MATCH },
217   { "\\\\",                     "\\\\",                   NOMATCH },
218   { "\\?",                      "?",                      MATCH },
219   { "\\*",                      "*",                      MATCH },
220   { "?.txt",                    "?.txt",                  MATCH },
221   { "*.txt",                    "*.txt",                  MATCH },
222   { "\\?.txt",                  "?.txt",                  MATCH },
223   { "\\*.txt",                  "*.txt",                  MATCH },
224   { "\\?.txt",                  "x.txt",                  NOMATCH },
225   { "\\*.txt",                  "x.txt",                  NOMATCH },
226   { "\\*\\\\.txt",              "*\\.txt",                MATCH },
227   { "*\\**\\?*\\\\*",           "cc*cc?cccc",             NOMATCH },
228   { "*\\?*\\**",                "cc?cc",                  NOMATCH },
229   { "\\\"\\$\\&\\'\\(\\)",      "\"$&'()",                MATCH },
230   { "\\*\\?\\[\\\\\\`\\|",      "*?[\\`|",                MATCH },
231   { "[\\a\\b]c",                "ac",                     MATCH },
232   { "[\\a\\b]c",                "bc",                     MATCH },
233   { "[\\a\\b]d",                "bc",                     NOMATCH },
234   { "[a-bA-B\\?]",              "?",                      MATCH },
235   { "cu[a-ab-b\\r]l",           "curl",                   MATCH },
236   { "[\\a-z]",                  "c",                      MATCH },
237 
238   { "?*?*?.*?*",                "abc.c",                  MATCH },
239   { "?*?*?.*?*",                "abcc",                   NOMATCH },
240   { "?*?*?.*?*",                "abc.",                   NOMATCH },
241   { "?*?*?.*?*",                "abc.c++",                MATCH },
242   { "?*?*?.*?*",                "abcdef.c++",             MATCH },
243   { "?*?*?.?",                  "abcdef.c",               MATCH },
244   { "?*?*?.?",                  "abcdef.cd",              NOMATCH },
245 
246   { "Lindmätarv",               "Lindmätarv",             MATCH },
247 
248   { "",                         "",                       MATCH},
249   {"**]*[*[\x13]**[*\x13)]*]*[**[*\x13~r-]*]**[.*]*[\xe3\xe3\xe3\xe3\xe3\xe3"
250    "\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3"
251    "\xe3\xe3\xe3\xe3\xe3*[\x13]**[*\x13)]*]*[*[\x13]*[~r]*]*\xba\x13\xa6~b-]*",
252                                 "a",                      NOMATCH|LINUX_FAIL}
253 };
254 
ret2name(int i)255 static const char *ret2name(int i)
256 {
257   switch(i) {
258   case 0:
259     return "MATCH";
260   case 1:
261     return "NOMATCH";
262   case 2:
263     return "FAIL";
264   default:
265     return "unknown";
266   }
267   /* not reached */
268 }
269 
270 enum system {
271   SYSTEM_CUSTOM,
272   SYSTEM_LINUX,
273   SYSTEM_MACOS
274 };
275 
276 UNITTEST_START
277 {
278   int testnum = sizeof(tests) / sizeof(struct testcase);
279   int i;
280   enum system machine;
281 
282 #ifdef HAVE_FNMATCH
283 #ifdef __APPLE__
284   machine = SYSTEM_MACOS;
285 #else
286   machine = SYSTEM_LINUX;
287 #endif
288   printf("Tested with system fnmatch(), %s-style\n",
289          machine == SYSTEM_LINUX ? "linux" : "mac");
290 #else
291   printf("Tested with custom fnmatch()\n");
292   machine = SYSTEM_CUSTOM;
293 #endif
294 
295   for(i = 0; i < testnum; i++) {
296     int result = tests[i].result;
297     int rc = Curl_fnmatch(NULL, tests[i].pattern, tests[i].string);
298     if(result & (LINUX_DIFFER|MAC_DIFFER)) {
299       if((result & LINUX_DIFFER) && (machine == SYSTEM_LINUX))
300         result >>= LINUX_SHIFT;
301       else if((result & MAC_DIFFER) && (machine == SYSTEM_MACOS))
302         result >>= MAC_SHIFT;
303       result &= 0x03; /* filter off all high bits */
304     }
305     if(rc != result) {
306       printf("Curl_fnmatch(\"%s\", \"%s\") should return %s (returns %s)"
307              " [%d]\n",
308              tests[i].pattern, tests[i].string, ret2name(result),
309              ret2name(rc), i);
310       fail("pattern mismatch");
311     }
312   }
313 }
314 UNITTEST_STOP
315 
316 #else
317 
318 UNITTEST_START
319 UNITTEST_STOP
320 
321 #endif
322