unit1307.c 15 KB

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