• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, 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 #define MAX_PATTERN_L 100
31 #define MAX_STRING_L  100
32 
33 struct testcase {
34   char pattern[MAX_PATTERN_L];
35   char string[MAX_STRING_L];
36   int  result;
37 };
38 
39 static const struct testcase tests[] = {
40   /* brackets syntax */
41   { "\\[",                      "[",                      MATCH },
42   { "[",                        "[",                      RE_ERR },
43   { "[]",                       "[]",                     RE_ERR },
44   { "[][]",                     "[",                      MATCH },
45   { "[][]",                     "]",                      MATCH },
46   { "[[]",                      "[",                      MATCH },
47   { "[[[]",                     "[",                      MATCH },
48   { "[[[[]",                    "[",                      MATCH },
49   { "[[[[]",                    "[",                      MATCH },
50 
51   { "[][[]",                    "]",                      MATCH },
52   { "[][[[]",                   "[",                      MATCH },
53   { "[[]",                      "]",                      NOMATCH },
54 
55   { "[a-z]",                    "a",                      MATCH },
56   { "[a-z]",                    "A",                      NOMATCH },
57   { "?[a-z]",                   "?Z",                     NOMATCH },
58   { "[A-Z]",                    "C",                      MATCH },
59   { "[A-Z]",                    "c",                      NOMATCH },
60   { "[0-9]",                    "7",                      MATCH },
61   { "[7-8]",                    "7",                      MATCH },
62   { "[7-]",                     "7",                      MATCH },
63   { "[7-]",                     "-",                      MATCH },
64   { "[7-]",                     "[",                      NOMATCH },
65   { "[a-bA-F]",                 "F",                      MATCH },
66   { "[a-bA-B9]",                "9",                      MATCH },
67   { "[a-bA-B98]",               "8",                      MATCH },
68   { "[a-bA-B98]",               "C",                      NOMATCH },
69   { "[a-bA-Z9]",                "F",                      MATCH },
70   { "[a-bA-Z9]ero*",            "Zero chance.",           MATCH },
71   { "S[a-][x]opho*",            "Saxophone",              MATCH },
72   { "S[a-][x]opho*",            "SaXophone",              NOMATCH },
73   { "S[a-][x]*.txt",            "S-x.txt",                MATCH },
74   { "[\\a-\\b]",                "a",                      MATCH },
75   { "[\\a-\\b]",                "b",                      MATCH },
76   { "[?*[][?*[][?*[]",          "?*[",                    MATCH },
77   { "[][?*-]",                  "]",                      MATCH },
78   { "[][?*-]",                  "[",                      MATCH },
79   { "[][?*-]",                  "?",                      MATCH },
80   { "[][?*-]",                  "*",                      MATCH },
81   { "[][?*-]",                  "-",                      MATCH },
82   { "[]?*-]",                   "-",                      MATCH },
83   { "?/b/c",                    "a/b/c",                  MATCH },
84   { "^_{}~",                    "^_{}~",                  MATCH },
85   { "!#%+,-./01234567889",      "!#%+,-./01234567889",    MATCH },
86   { "PQRSTUVWXYZ]abcdefg",      "PQRSTUVWXYZ]abcdefg",    MATCH },
87   { ":;=@ABCDEFGHIJKLMNO",      ":;=@ABCDEFGHIJKLMNO",    MATCH },
88 
89   /* negate */
90   { "[!a]",                     "b",                      MATCH },
91   { "[!a]",                     "a",                      NOMATCH },
92   { "[^a]",                     "b",                      MATCH },
93   { "[^a]",                     "a",                      NOMATCH },
94   { "[^a-z0-9A-Z]",             "a",                      NOMATCH },
95   { "[^a-z0-9A-Z]",             "-",                      MATCH },
96   { "curl[!a-z]lib",            "curl lib",               MATCH },
97   { "curl[! ]lib",              "curl lib",               NOMATCH },
98   { "[! ][ ]",                  "  ",                     NOMATCH },
99   { "[! ][ ]",                  "a ",                     MATCH },
100   { "*[^a].t?t",                "a.txt",                  NOMATCH },
101   { "*[^a].t?t",                "ba.txt",                 NOMATCH },
102   { "*[^a].t?t",                "ab.txt",                 MATCH },
103   { "[!?*[]",                   "?",                      NOMATCH },
104   { "[!!]",                     "!",                      NOMATCH },
105   { "[!!]",                     "x",                      MATCH },
106 
107   { "[[:alpha:]]",              "a",                      MATCH },
108   { "[[:alpha:]]",              "9",                      NOMATCH },
109   { "[[:alnum:]]",              "a",                      MATCH },
110   { "[[:alnum:]]",              "[",                      NOMATCH },
111   { "[[:alnum:]]",              "]",                      NOMATCH },
112   { "[[:alnum:]]",              "9",                      MATCH },
113   { "[[:digit:]]",              "9",                      MATCH },
114   { "[[:xdigit:]]",             "9",                      MATCH },
115   { "[[:xdigit:]]",             "F",                      MATCH },
116   { "[[:xdigit:]]",             "G",                      NOMATCH },
117   { "[[:upper:]]",              "U",                      MATCH },
118   { "[[:upper:]]",              "u",                      NOMATCH },
119   { "[[:lower:]]",              "l",                      MATCH },
120   { "[[:lower:]]",              "L",                      NOMATCH },
121   { "[[:print:]]",              "L",                      MATCH },
122   { "[[:print:]]",              {'\10'},                  NOMATCH },
123   { "[[:print:]]",              {'\10'},                  NOMATCH },
124   { "[[:space:]]",              " ",                      MATCH },
125   { "[[:space:]]",              "x",                      NOMATCH },
126   { "[[:graph:]]",              " ",                      NOMATCH },
127   { "[[:graph:]]",              "x",                      MATCH },
128   { "[[:blank:]]",              {'\t'},                   MATCH },
129   { "[[:blank:]]",              {' '},                    MATCH },
130   { "[[:blank:]]",              {'\r'},                   NOMATCH },
131   { "[^[:blank:]]",             {'\t'},                   NOMATCH },
132   { "[^[:print:]]",             {'\10'},                  MATCH },
133   { "[[:lower:]][[:lower:]]",   "ll",                     MATCH },
134 
135   { "Curl[[:blank:]];-)",       "Curl ;-)",               MATCH },
136   { "*[[:blank:]]*",            " ",                      MATCH },
137   { "*[[:blank:]]*",            "",                       NOMATCH },
138   { "*[[:blank:]]*",            "hi, im_Pavel",           MATCH },
139 
140   /* common using */
141   { "filename.dat",             "filename.dat",           MATCH },
142   { "*curl*",                   "lets use curl!!",        MATCH },
143   { "filename.txt",             "filename.dat",           NOMATCH },
144   { "*.txt",                    "text.txt",               MATCH },
145   { "*.txt",                    "a.txt",                  MATCH },
146   { "*.txt",                    ".txt",                   MATCH },
147   { "*.txt",                    "txt",                    NOMATCH },
148   { "??.txt",                   "99.txt",                 MATCH },
149   { "??.txt",                   "a99.txt",                NOMATCH },
150   { "?.???",                    "a.txt",                  MATCH },
151   { "*.???",                    "somefile.dat",           MATCH },
152   { "*.???",                    "photo.jpeg",             NOMATCH },
153   { ".*",                       ".htaccess",              MATCH },
154   { ".*",                       ".",                      MATCH },
155   { ".*",                       "..",                     MATCH },
156 
157   /* many stars => one star */
158   { "**.txt",                   "text.txt",               MATCH },
159   { "***.txt",                  "t.txt",                  MATCH },
160   { "****.txt",                 ".txt",                   MATCH },
161 
162   /* empty string or pattern */
163   { "",                         "",                       MATCH },
164   { "",                         "hello",                  NOMATCH },
165   { "file",                     "",                       NOMATCH  },
166   { "?",                        "",                       NOMATCH },
167   { "*",                        "",                       MATCH },
168   { "x",                        "",                       NOMATCH },
169 
170   /* backslash */
171   { "\\",                       "\\",                     RE_ERR },
172   { "\\\\",                     "\\",                     MATCH },
173   { "\\\\",                     "\\\\",                   NOMATCH },
174   { "\\?",                      "?",                      MATCH },
175   { "\\*",                      "*",                      MATCH },
176   { "?.txt",                    "?.txt",                  MATCH },
177   { "*.txt",                    "*.txt",                  MATCH },
178   { "\\?.txt",                  "?.txt",                  MATCH },
179   { "\\*.txt",                  "*.txt",                  MATCH },
180   { "\\?.txt",                  "x.txt",                  NOMATCH },
181   { "\\*.txt",                  "x.txt",                  NOMATCH },
182   { "\\*\\\\.txt",              "*\\.txt",                MATCH },
183   { "*\\**\\?*\\\\*",           "cc*cc?cc\\cc*cc",        MATCH },
184   { "*\\**\\?*\\\\*",           "cc*cc?cccc",             NOMATCH },
185   { "*\\**\\?*\\\\*",           "cc*cc?cc\\cc*cc",        MATCH },
186   { "*\\?*\\**",                "cc?c*c",                 MATCH },
187   { "*\\?*\\**curl*",           "cc?c*curl",              MATCH },
188   { "*\\?*\\**",                "cc?cc",                  NOMATCH },
189   { "\\\"\\$\\&\\'\\(\\)",      "\"$&'()",                MATCH },
190   { "\\*\\?\\[\\\\\\`\\|",      "*?[\\`|",                MATCH },
191   { "[\\a\\b]c",                "ac",                     MATCH },
192   { "[\\a\\b]c",                "bc",                     MATCH },
193   { "[\\a\\b]d",                "bc",                     NOMATCH },
194   { "[a-bA-B\\?]",              "?",                      MATCH },
195   { "cu[a-ab-b\\r]l",           "curl",                   MATCH },
196   { "[\\a-z]",                  "c",                      MATCH },
197 
198   { "?*?*?.*?*",                "abc.c",                  MATCH },
199   { "?*?*?.*?*",                "abcc",                   NOMATCH },
200   { "?*?*?.*?*",                "abc.",                   NOMATCH },
201   { "?*?*?.*?*",                "abc.c++",                MATCH },
202   { "?*?*?.*?*",                "abcdef.c++",             MATCH },
203   { "?*?*?.?",                  "abcdef.c",               MATCH },
204   { "?*?*?.?",                  "abcdef.cd",              NOMATCH },
205 
206   { "Lindmätarv",               "Lindmätarv",             MATCH },
207 
208   { "",                         "",                       MATCH }
209 };
210 
unit_setup(void)211 static CURLcode unit_setup(void)
212 {
213   return CURLE_OK;
214 }
215 
unit_stop(void)216 static void unit_stop(void)
217 {
218 }
219 
220 UNITTEST_START
221 
222   int testnum = sizeof(tests) / sizeof(struct testcase);
223   int i, rc;
224 
225   for(i = 0; i < testnum; i++) {
226     rc = Curl_fnmatch(NULL, tests[i].pattern, tests[i].string);
227     if(rc != tests[i].result) {
228       printf("Curl_fnmatch(\"%s\", \"%s\") should return %d (returns %d)\n",
229              tests[i].pattern, tests[i].string, tests[i].result, rc);
230       fail("pattern mismatch");
231     }
232   }
233 
234 UNITTEST_STOP
235