unit1604.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 "tool_cfgable.h"
  26. #include "tool_doswin.h"
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "memdebug.h" /* LAST include file */
  31. static CURLcode unit_setup(void)
  32. {
  33. return CURLE_OK;
  34. }
  35. static void unit_stop(void)
  36. {
  37. }
  38. #if defined(_WIN32) || defined(MSDOS)
  39. static char *getflagstr(int flags)
  40. {
  41. char *buf = malloc(256);
  42. if(buf) {
  43. msnprintf(buf, 256, "%s,%s",
  44. ((flags & SANITIZE_ALLOW_PATH) ?
  45. "SANITIZE_ALLOW_PATH" : ""),
  46. ((flags & SANITIZE_ALLOW_RESERVED) ?
  47. "SANITIZE_ALLOW_RESERVED" : ""));
  48. }
  49. return buf;
  50. }
  51. static char *getcurlcodestr(int cc)
  52. {
  53. char *buf = malloc(256);
  54. if(buf) {
  55. msnprintf(buf, 256, "%s (%d)",
  56. (cc == SANITIZE_ERR_OK ? "SANITIZE_ERR_OK" :
  57. cc == SANITIZE_ERR_BAD_ARGUMENT ? "SANITIZE_ERR_BAD_ARGUMENT" :
  58. cc == SANITIZE_ERR_INVALID_PATH ? "SANITIZE_ERR_INVALID_PATH" :
  59. cc == SANITIZE_ERR_OUT_OF_MEMORY ? "SANITIZE_ERR_OUT_OF_MEMORY":
  60. "unexpected error code - add name"),
  61. cc);
  62. }
  63. return buf;
  64. }
  65. struct data {
  66. const char *input;
  67. int flags;
  68. const char *expected_output;
  69. SANITIZEcode expected_result;
  70. };
  71. UNITTEST_START
  72. { /* START sanitize_file_name */
  73. struct data data[] = {
  74. { "", 0,
  75. "", SANITIZE_ERR_OK
  76. },
  77. { "normal filename", 0,
  78. "normal filename", SANITIZE_ERR_OK
  79. },
  80. { "control\tchar", 0,
  81. "control_char", SANITIZE_ERR_OK
  82. },
  83. { "banned*char", 0,
  84. "banned_char", SANITIZE_ERR_OK
  85. },
  86. { "f:foo", 0,
  87. "f_foo", SANITIZE_ERR_OK
  88. },
  89. { "f:foo", SANITIZE_ALLOW_PATH,
  90. "f:foo", SANITIZE_ERR_OK
  91. },
  92. { "f:\\foo", 0,
  93. "f__foo", SANITIZE_ERR_OK
  94. },
  95. { "f:\\foo", SANITIZE_ALLOW_PATH,
  96. "f:\\foo", SANITIZE_ERR_OK
  97. },
  98. { "f:/foo", 0,
  99. "f__foo", SANITIZE_ERR_OK
  100. },
  101. { "f:/foo", SANITIZE_ALLOW_PATH,
  102. "f:/foo", SANITIZE_ERR_OK
  103. },
  104. #ifndef MSDOS
  105. { "\\\\?\\C:\\foo", SANITIZE_ALLOW_PATH,
  106. "\\\\?\\C:\\foo", SANITIZE_ERR_OK
  107. },
  108. { "\\\\?\\C:\\foo", 0,
  109. "____C__foo", SANITIZE_ERR_OK
  110. },
  111. #endif
  112. { "foo:bar", 0,
  113. "foo_bar", SANITIZE_ERR_OK
  114. },
  115. { "foo|<>/bar\\\":?*baz", 0,
  116. "foo____bar_____baz", SANITIZE_ERR_OK
  117. },
  118. { "f:foo::$DATA", 0,
  119. "f_foo__$DATA", SANITIZE_ERR_OK
  120. },
  121. { "con . air", 0,
  122. "con _ air", SANITIZE_ERR_OK
  123. },
  124. { "con.air", 0,
  125. "con_air", SANITIZE_ERR_OK
  126. },
  127. { "con:/x", 0,
  128. "con__x", SANITIZE_ERR_OK
  129. },
  130. { "file . . . . .. .", 0,
  131. "file", SANITIZE_ERR_OK
  132. },
  133. { "foo . . ? . . ", 0,
  134. "foo . . _", SANITIZE_ERR_OK
  135. },
  136. { "com1", 0,
  137. "_com1", SANITIZE_ERR_OK
  138. },
  139. { "com1", SANITIZE_ALLOW_RESERVED,
  140. "com1", SANITIZE_ERR_OK
  141. },
  142. { "f:\\com1", 0,
  143. "f__com1", SANITIZE_ERR_OK
  144. },
  145. { "f:\\com1", SANITIZE_ALLOW_PATH,
  146. "f:\\_com1", SANITIZE_ERR_OK
  147. },
  148. { "f:\\com1", SANITIZE_ALLOW_RESERVED,
  149. "f__com1", SANITIZE_ERR_OK
  150. },
  151. { "f:\\com1", SANITIZE_ALLOW_RESERVED | SANITIZE_ALLOW_PATH,
  152. "f:\\com1", SANITIZE_ERR_OK
  153. },
  154. { "com1:\\com1", SANITIZE_ALLOW_PATH,
  155. "_com1:\\_com1", SANITIZE_ERR_OK
  156. },
  157. { "com1:\\com1", SANITIZE_ALLOW_RESERVED | SANITIZE_ALLOW_PATH,
  158. "com1:\\com1", SANITIZE_ERR_OK
  159. },
  160. { "com1:\\com1", SANITIZE_ALLOW_RESERVED,
  161. "com1__com1", SANITIZE_ERR_OK
  162. },
  163. #ifndef MSDOS
  164. { "\\com1", SANITIZE_ALLOW_PATH,
  165. "\\_com1", SANITIZE_ERR_OK
  166. },
  167. { "\\\\com1", SANITIZE_ALLOW_PATH,
  168. "\\\\com1", SANITIZE_ERR_OK
  169. },
  170. { "\\\\?\\C:\\com1", SANITIZE_ALLOW_PATH,
  171. "\\\\?\\C:\\com1", SANITIZE_ERR_OK
  172. },
  173. #endif
  174. { "CoM1", 0,
  175. "_CoM1", SANITIZE_ERR_OK
  176. },
  177. { "CoM1", SANITIZE_ALLOW_RESERVED,
  178. "CoM1", SANITIZE_ERR_OK
  179. },
  180. { "COM56", 0,
  181. "COM56", SANITIZE_ERR_OK
  182. },
  183. /* At the moment we expect a maximum path length of 259. I assume MS-DOS
  184. has variable max path lengths depending on compiler that are shorter
  185. so currently these "good" truncate tests will not run on MS-DOS */
  186. { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  187. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  188. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  189. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  190. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  191. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  192. 0,
  193. NULL, SANITIZE_ERR_INVALID_PATH
  194. },
  195. { NULL, 0,
  196. NULL, SANITIZE_ERR_BAD_ARGUMENT
  197. },
  198. };
  199. size_t i;
  200. for(i = 0; i < sizeof(data) / sizeof(data[0]); ++i) {
  201. char *output = NULL;
  202. char *flagstr = NULL;
  203. char *received_ccstr = NULL;
  204. char *expected_ccstr = NULL;
  205. SANITIZEcode res;
  206. res = sanitize_file_name(&output, data[i].input, data[i].flags);
  207. if(res == data[i].expected_result &&
  208. ((!output && !data[i].expected_output) ||
  209. (output && data[i].expected_output &&
  210. !strcmp(output, data[i].expected_output)))) { /* OK */
  211. free(output);
  212. continue;
  213. }
  214. flagstr = getflagstr(data[i].flags);
  215. abort_unless(flagstr, "out of memory");
  216. received_ccstr = getcurlcodestr(res);
  217. abort_unless(received_ccstr, "out of memory");
  218. expected_ccstr = getcurlcodestr(data[i].expected_result);
  219. abort_unless(expected_ccstr, "out of memory");
  220. unitfail++;
  221. fprintf(stderr, "\n"
  222. "%s:%d sanitize_file_name failed.\n"
  223. "input: %s\n"
  224. "flags: %s\n"
  225. "output: %s\n"
  226. "result: %s\n"
  227. "expected output: %s\n"
  228. "expected result: %s\n",
  229. __FILE__, __LINE__,
  230. data[i].input,
  231. flagstr,
  232. (output ? output : "(null)"),
  233. received_ccstr,
  234. (data[i].expected_output ? data[i].expected_output : "(null)"),
  235. expected_ccstr);
  236. free(output);
  237. free(flagstr);
  238. free(received_ccstr);
  239. free(expected_ccstr);
  240. }
  241. } /* END sanitize_file_name */
  242. #else
  243. UNITTEST_START
  244. {
  245. fprintf(stderr, "Skipped test not for this platform\n");
  246. }
  247. #endif /* _WIN32 || MSDOS */
  248. UNITTEST_STOP