123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- /***************************************************************************
- * _ _ ____ _
- * Project ___| | | | _ \| |
- * / __| | | | |_) | |
- * | (__| |_| | _ <| |___
- * \___|\___/|_| \_\_____|
- *
- * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
- *
- * This software is licensed as described in the file COPYING, which
- * you should have received as part of this distribution. The terms
- * are also available at https://curl.se/docs/copyright.html.
- *
- * You may opt to use, copy, modify, merge, publish, distribute and/or sell
- * copies of the Software, and permit persons to whom the Software is
- * furnished to do so, under the terms of the COPYING file.
- *
- * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
- * KIND, either express or implied.
- *
- * SPDX-License-Identifier: curl
- *
- ***************************************************************************/
- #include "curlcheck.h"
- #include "curl_fnmatch.h"
- static CURLcode unit_setup(void)
- {
- return CURLE_OK;
- }
- static void unit_stop(void)
- {
- }
- #ifndef CURL_DISABLE_FTP
- /*
- CURL_FNMATCH_MATCH 0
- CURL_FNMATCH_NOMATCH 1
- CURL_FNMATCH_FAIL 2
- */
- #define MATCH CURL_FNMATCH_MATCH
- #define NOMATCH CURL_FNMATCH_NOMATCH
- #define LINUX_DIFFER 0x80
- #define LINUX_SHIFT 8
- #define LINUX_MATCH ((CURL_FNMATCH_MATCH << LINUX_SHIFT) | LINUX_DIFFER)
- #define LINUX_NOMATCH ((CURL_FNMATCH_NOMATCH << LINUX_SHIFT) | LINUX_DIFFER)
- #define LINUX_FAIL ((CURL_FNMATCH_FAIL << LINUX_SHIFT) | LINUX_DIFFER)
- #define MAC_DIFFER 0x40
- #define MAC_SHIFT 16
- #define MAC_MATCH ((CURL_FNMATCH_MATCH << MAC_SHIFT) | MAC_DIFFER)
- #define MAC_NOMATCH ((CURL_FNMATCH_NOMATCH << MAC_SHIFT) | MAC_DIFFER)
- #define MAC_FAIL ((CURL_FNMATCH_FAIL << MAC_SHIFT) | MAC_DIFFER)
- struct testcase {
- const char *pattern;
- const char *string;
- int result;
- };
- static const struct testcase tests[] = {
- /* brackets syntax */
- {"*[*[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
- "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
- "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\001\177[[[[[[[[[[[[[[[[[[[[[",
- "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
- "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
- "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[",
- NOMATCH|MAC_FAIL},
- { "\\[", "[", MATCH },
- { "[", "[", NOMATCH|LINUX_MATCH|MAC_FAIL},
- { "[]", "[]", NOMATCH|LINUX_MATCH|MAC_FAIL},
- { "[][]", "[", MATCH },
- { "[][]", "]", MATCH },
- { "[[]", "[", MATCH },
- { "[[[]", "[", MATCH },
- { "[[[[]", "[", MATCH },
- { "[[[[]", "[", MATCH },
- { "[][[]", "]", MATCH },
- { "[][[[]", "[", MATCH },
- { "[[]", "]", NOMATCH },
- { "[a@]", "a", MATCH },
- { "[a-z]", "a", MATCH },
- { "[a-z]", "A", NOMATCH },
- { "?[a-z]", "?Z", NOMATCH },
- { "[A-Z]", "C", MATCH },
- { "[A-Z]", "c", NOMATCH },
- { "[0-9]", "7", MATCH },
- { "[7-8]", "7", MATCH },
- { "[7-]", "7", MATCH },
- { "[7-]", "-", MATCH },
- { "[7-]", "[", NOMATCH },
- { "[a-bA-F]", "F", MATCH },
- { "[a-bA-B9]", "9", MATCH },
- { "[a-bA-B98]", "8", MATCH },
- { "[a-bA-B98]", "C", NOMATCH },
- { "[a-bA-Z9]", "F", MATCH },
- { "[a-bA-Z9]ero*", "Zero chance.", MATCH },
- { "S[a-][x]opho*", "Saxophone", MATCH },
- { "S[a-][x]opho*", "SaXophone", NOMATCH },
- { "S[a-][x]*.txt", "S-x.txt", MATCH },
- { "[\\a-\\b]", "a", MATCH },
- { "[\\a-\\b]", "b", MATCH },
- { "[?*[][?*[][?*[]", "?*[", MATCH },
- { "[][?*-]", "]", MATCH },
- { "[][?*-]", "[", MATCH },
- { "[][?*-]", "?", MATCH },
- { "[][?*-]", "*", MATCH },
- { "[][?*-]", "-", MATCH },
- { "[]?*-]", "-", MATCH },
- { "[\xFF]", "\xFF", MATCH|LINUX_FAIL|MAC_FAIL},
- { "?/b/c", "a/b/c", MATCH },
- { "^_{}~", "^_{}~", MATCH },
- { "!#%+,-./01234567889", "!#%+,-./01234567889", MATCH },
- { "PQRSTUVWXYZ]abcdefg", "PQRSTUVWXYZ]abcdefg", MATCH },
- { ":;=@ABCDEFGHIJKLMNO", ":;=@ABCDEFGHIJKLMNO", MATCH },
- /* negate */
- { "[!a]", "b", MATCH },
- { "[!a]", "a", NOMATCH },
- { "[^a]", "b", MATCH },
- { "[^a]", "a", NOMATCH },
- { "[^a-z0-9A-Z]", "a", NOMATCH },
- { "[^a-z0-9A-Z]", "-", MATCH },
- { "curl[!a-z]lib", "curl lib", MATCH },
- { "curl[! ]lib", "curl lib", NOMATCH },
- { "[! ][ ]", " ", NOMATCH },
- { "[! ][ ]", "a ", MATCH },
- { "*[^a].t?t", "a.txt", NOMATCH },
- { "*[^a].t?t", "ba.txt", NOMATCH },
- { "*[^a].t?t", "ab.txt", MATCH },
- { "*[^a]", "", NOMATCH },
- { "[!\xFF]", "", NOMATCH|LINUX_FAIL},
- { "[!\xFF]", "\xFF", NOMATCH|LINUX_FAIL|MAC_FAIL},
- { "[!\xFF]", "a", MATCH|LINUX_FAIL|MAC_FAIL},
- { "[!?*[]", "?", NOMATCH },
- { "[!!]", "!", NOMATCH },
- { "[!!]", "x", MATCH },
- { "[[:alpha:]]", "a", MATCH },
- { "[[:alpha:]]", "9", NOMATCH },
- { "[[:alnum:]]", "a", MATCH },
- { "[[:alnum:]]", "[", NOMATCH },
- { "[[:alnum:]]", "]", NOMATCH },
- { "[[:alnum:]]", "9", MATCH },
- { "[[:digit:]]", "9", MATCH },
- { "[[:xdigit:]]", "9", MATCH },
- { "[[:xdigit:]]", "F", MATCH },
- { "[[:xdigit:]]", "G", NOMATCH },
- { "[[:upper:]]", "U", MATCH },
- { "[[:upper:]]", "u", NOMATCH },
- { "[[:lower:]]", "l", MATCH },
- { "[[:lower:]]", "L", NOMATCH },
- { "[[:print:]]", "L", MATCH },
- { "[[:print:]]", "\10", NOMATCH },
- { "[[:print:]]", "\10", NOMATCH },
- { "[[:space:]]", " ", MATCH },
- { "[[:space:]]", "x", NOMATCH },
- { "[[:graph:]]", " ", NOMATCH },
- { "[[:graph:]]", "x", MATCH },
- { "[[:blank:]]", "\t", MATCH },
- { "[[:blank:]]", " ", MATCH },
- { "[[:blank:]]", "\r", NOMATCH },
- { "[^[:blank:]]", "\t", NOMATCH },
- { "[^[:print:]]", "\10", MATCH },
- { "[[:lower:]][[:lower:]]", "ll", MATCH },
- { "[[:foo:]]", "bar", NOMATCH|MAC_FAIL},
- { "[[:foo:]]", "f]", MATCH|LINUX_NOMATCH|MAC_FAIL},
- { "Curl[[:blank:]];-)", "Curl ;-)", MATCH },
- { "*[[:blank:]]*", " ", MATCH },
- { "*[[:blank:]]*", "", NOMATCH },
- { "*[[:blank:]]*", "hi, im_Pavel", MATCH },
- /* common using */
- { "filename.dat", "filename.dat", MATCH },
- { "*curl*", "lets use curl!!", MATCH },
- { "filename.txt", "filename.dat", NOMATCH },
- { "*.txt", "text.txt", MATCH },
- { "*.txt", "a.txt", MATCH },
- { "*.txt", ".txt", MATCH },
- { "*.txt", "txt", NOMATCH },
- { "??.txt", "99.txt", MATCH },
- { "??.txt", "a99.txt", NOMATCH },
- { "?.???", "a.txt", MATCH },
- { "*.???", "somefile.dat", MATCH },
- { "*.???", "photo.jpeg", NOMATCH },
- { ".*", ".htaccess", MATCH },
- { ".*", ".", MATCH },
- { ".*", "..", MATCH },
- /* many stars => one star */
- { "**.txt", "text.txt", MATCH },
- { "***.txt", "t.txt", MATCH },
- { "****.txt", ".txt", MATCH },
- /* empty string or pattern */
- { "", "", MATCH },
- { "", "hello", NOMATCH },
- { "file", "", NOMATCH },
- { "?", "", NOMATCH },
- { "*", "", MATCH },
- { "x", "", NOMATCH },
- /* backslash */
- { "\\", "\\", MATCH|LINUX_NOMATCH},
- { "\\\\", "\\", MATCH },
- { "\\\\", "\\\\", NOMATCH },
- { "\\?", "?", MATCH },
- { "\\*", "*", MATCH },
- { "?.txt", "?.txt", MATCH },
- { "*.txt", "*.txt", MATCH },
- { "\\?.txt", "?.txt", MATCH },
- { "\\*.txt", "*.txt", MATCH },
- { "\\?.txt", "x.txt", NOMATCH },
- { "\\*.txt", "x.txt", NOMATCH },
- { "\\*\\\\.txt", "*\\.txt", MATCH },
- { "*\\**\\?*\\\\*", "cc*cc?cccc", NOMATCH },
- { "*\\?*\\**", "cc?cc", NOMATCH },
- { "\\\"\\$\\&\\'\\(\\)", "\"$&'()", MATCH },
- { "\\*\\?\\[\\\\\\`\\|", "*?[\\`|", MATCH },
- { "[\\a\\b]c", "ac", MATCH },
- { "[\\a\\b]c", "bc", MATCH },
- { "[\\a\\b]d", "bc", NOMATCH },
- { "[a-bA-B\\?]", "?", MATCH },
- { "cu[a-ab-b\\r]l", "curl", MATCH },
- { "[\\a-z]", "c", MATCH },
- { "?*?*?.*?*", "abc.c", MATCH },
- { "?*?*?.*?*", "abcc", NOMATCH },
- { "?*?*?.*?*", "abc.", NOMATCH },
- { "?*?*?.*?*", "abc.c++", MATCH },
- { "?*?*?.*?*", "abcdef.c++", MATCH },
- { "?*?*?.?", "abcdef.c", MATCH },
- { "?*?*?.?", "abcdef.cd", NOMATCH },
- { "Lindmätarv", "Lindmätarv", MATCH },
- { "", "", MATCH},
- {"**]*[*[\x13]**[*\x13)]*]*[**[*\x13~r-]*]**[.*]*[\xe3\xe3\xe3\xe3\xe3\xe3"
- "\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3"
- "\xe3\xe3\xe3\xe3\xe3*[\x13]**[*\x13)]*]*[*[\x13]*[~r]*]*\xba\x13\xa6~b-]*",
- "a", NOMATCH|LINUX_FAIL}
- };
- static const char *ret2name(int i)
- {
- switch(i) {
- case 0:
- return "MATCH";
- case 1:
- return "NOMATCH";
- case 2:
- return "FAIL";
- default:
- return "unknown";
- }
- /* not reached */
- }
- enum system {
- SYSTEM_CUSTOM,
- SYSTEM_LINUX,
- SYSTEM_MACOS
- };
- UNITTEST_START
- {
- int testnum = sizeof(tests) / sizeof(struct testcase);
- int i;
- enum system machine;
- #ifdef HAVE_FNMATCH
- if(strstr(OS, "apple") || strstr(OS, "darwin")) {
- machine = SYSTEM_MACOS;
- }
- else
- machine = SYSTEM_LINUX;
- printf("Tested with system fnmatch(), %s-style\n",
- machine == SYSTEM_LINUX ? "linux" : "mac");
- #else
- printf("Tested with custom fnmatch()\n");
- machine = SYSTEM_CUSTOM;
- #endif
- for(i = 0; i < testnum; i++) {
- int result = tests[i].result;
- int rc = Curl_fnmatch(NULL, tests[i].pattern, tests[i].string);
- if(result & (LINUX_DIFFER|MAC_DIFFER)) {
- if((result & LINUX_DIFFER) && (machine == SYSTEM_LINUX))
- result >>= LINUX_SHIFT;
- else if((result & MAC_DIFFER) && (machine == SYSTEM_MACOS))
- result >>= MAC_SHIFT;
- result &= 0x03; /* filter off all high bits */
- }
- if(rc != result) {
- printf("Curl_fnmatch(\"%s\", \"%s\") should return %s (returns %s)"
- " [%d]\n",
- tests[i].pattern, tests[i].string, ret2name(result),
- ret2name(rc), i);
- fail("pattern mismatch");
- }
- }
- }
- UNITTEST_STOP
- #else
- UNITTEST_START
- {
- /* nothing to do, just fail */
- return 1;
- }
- UNITTEST_STOP
- #endif
|