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