unit1307.c 15 KB

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