unit1307.c 12 KB

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