unit1604.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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,%s,%s",
  44. ((flags & SANITIZE_ALLOW_COLONS) ?
  45. "SANITIZE_ALLOW_COLONS" : ""),
  46. ((flags & SANITIZE_ALLOW_PATH) ?
  47. "SANITIZE_ALLOW_PATH" : ""),
  48. ((flags & SANITIZE_ALLOW_RESERVED) ?
  49. "SANITIZE_ALLOW_RESERVED" : ""),
  50. ((flags & SANITIZE_ALLOW_TRUNCATE) ?
  51. "SANITIZE_ALLOW_TRUNCATE" : ""));
  52. }
  53. return buf;
  54. }
  55. static char *getcurlcodestr(int cc)
  56. {
  57. char *buf = malloc(256);
  58. if(buf) {
  59. msnprintf(buf, 256, "%s (%d)",
  60. (cc == SANITIZE_ERR_OK ? "SANITIZE_ERR_OK" :
  61. cc == SANITIZE_ERR_BAD_ARGUMENT ? "SANITIZE_ERR_BAD_ARGUMENT" :
  62. cc == SANITIZE_ERR_INVALID_PATH ? "SANITIZE_ERR_INVALID_PATH" :
  63. cc == SANITIZE_ERR_OUT_OF_MEMORY ? "SANITIZE_ERR_OUT_OF_MEMORY":
  64. "unexpected error code - add name"),
  65. cc);
  66. }
  67. return buf;
  68. }
  69. struct data {
  70. const char *input;
  71. int flags;
  72. const char *expected_output;
  73. SANITIZEcode expected_result;
  74. };
  75. UNITTEST_START
  76. { /* START sanitize_file_name */
  77. struct data data[] = {
  78. { "", 0,
  79. "", SANITIZE_ERR_OK
  80. },
  81. { "normal filename", 0,
  82. "normal filename", SANITIZE_ERR_OK
  83. },
  84. { "control\tchar", 0,
  85. "control_char", SANITIZE_ERR_OK
  86. },
  87. { "banned*char", 0,
  88. "banned_char", SANITIZE_ERR_OK
  89. },
  90. { "f:foo", 0,
  91. "f_foo", SANITIZE_ERR_OK
  92. },
  93. { "f:foo", SANITIZE_ALLOW_COLONS,
  94. "f:foo", SANITIZE_ERR_OK
  95. },
  96. { "f:foo", SANITIZE_ALLOW_PATH,
  97. "f:foo", SANITIZE_ERR_OK
  98. },
  99. { "f:\\foo", 0,
  100. "f__foo", SANITIZE_ERR_OK
  101. },
  102. { "f:\\foo", SANITIZE_ALLOW_PATH,
  103. "f:\\foo", SANITIZE_ERR_OK
  104. },
  105. { "f:/foo", 0,
  106. "f__foo", SANITIZE_ERR_OK
  107. },
  108. { "f:/foo", SANITIZE_ALLOW_PATH,
  109. "f:/foo", SANITIZE_ERR_OK
  110. },
  111. #ifndef MSDOS
  112. { "\\\\?\\C:\\foo", SANITIZE_ALLOW_PATH,
  113. "\\\\?\\C:\\foo", SANITIZE_ERR_OK
  114. },
  115. { "\\\\?\\C:\\foo", 0,
  116. "____C__foo", SANITIZE_ERR_OK
  117. },
  118. #endif
  119. { "foo:bar", 0,
  120. "foo_bar", SANITIZE_ERR_OK
  121. },
  122. { "foo|<>/bar\\\":?*baz", 0,
  123. "foo____bar_____baz", SANITIZE_ERR_OK
  124. },
  125. { "f:foo::$DATA", 0,
  126. "f_foo__$DATA", SANITIZE_ERR_OK
  127. },
  128. { "con . air", 0,
  129. "con _ air", SANITIZE_ERR_OK
  130. },
  131. { "con.air", 0,
  132. "con_air", SANITIZE_ERR_OK
  133. },
  134. { "con:/x", 0,
  135. "con__x", SANITIZE_ERR_OK
  136. },
  137. { "file . . . . .. .", 0,
  138. "file", SANITIZE_ERR_OK
  139. },
  140. { "foo . . ? . . ", 0,
  141. "foo . . _", SANITIZE_ERR_OK
  142. },
  143. { "com1", 0,
  144. "_com1", SANITIZE_ERR_OK
  145. },
  146. { "com1", SANITIZE_ALLOW_RESERVED,
  147. "com1", SANITIZE_ERR_OK
  148. },
  149. { "f:\\com1", 0,
  150. "f__com1", SANITIZE_ERR_OK
  151. },
  152. { "f:\\com1", SANITIZE_ALLOW_PATH,
  153. "f:\\_com1", SANITIZE_ERR_OK
  154. },
  155. { "f:\\com1", SANITIZE_ALLOW_RESERVED,
  156. "f__com1", SANITIZE_ERR_OK
  157. },
  158. { "f:\\com1", SANITIZE_ALLOW_RESERVED | SANITIZE_ALLOW_COLONS,
  159. "f:_com1", SANITIZE_ERR_OK
  160. },
  161. { "f:\\com1", SANITIZE_ALLOW_RESERVED | SANITIZE_ALLOW_PATH,
  162. "f:\\com1", SANITIZE_ERR_OK
  163. },
  164. { "com1:\\com1", SANITIZE_ALLOW_PATH,
  165. "_com1:\\_com1", SANITIZE_ERR_OK
  166. },
  167. { "com1:\\com1", SANITIZE_ALLOW_RESERVED | SANITIZE_ALLOW_PATH,
  168. "com1:\\com1", SANITIZE_ERR_OK
  169. },
  170. { "com1:\\com1", SANITIZE_ALLOW_RESERVED,
  171. "com1__com1", SANITIZE_ERR_OK
  172. },
  173. #ifndef MSDOS
  174. { "\\com1", SANITIZE_ALLOW_PATH,
  175. "\\_com1", SANITIZE_ERR_OK
  176. },
  177. { "\\\\com1", SANITIZE_ALLOW_PATH,
  178. "\\\\com1", SANITIZE_ERR_OK
  179. },
  180. { "\\\\?\\C:\\com1", SANITIZE_ALLOW_PATH,
  181. "\\\\?\\C:\\com1", SANITIZE_ERR_OK
  182. },
  183. #endif
  184. { "CoM1", 0,
  185. "_CoM1", SANITIZE_ERR_OK
  186. },
  187. { "CoM1", SANITIZE_ALLOW_RESERVED,
  188. "CoM1", SANITIZE_ERR_OK
  189. },
  190. { "COM56", 0,
  191. "COM56", SANITIZE_ERR_OK
  192. },
  193. /* At the moment we expect a maximum path length of 259. I assume MSDOS
  194. has variable max path lengths depending on compiler that are shorter
  195. so currently these "good" truncate tests won't run on MSDOS */
  196. #ifndef MSDOS
  197. { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  198. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  199. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  200. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  201. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  202. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  203. SANITIZE_ALLOW_TRUNCATE,
  204. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  205. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  206. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  207. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  208. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  209. "FFFFF", SANITIZE_ERR_OK
  210. },
  211. { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  212. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  213. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  214. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  215. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  216. "FFF\\FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  217. SANITIZE_ALLOW_TRUNCATE | SANITIZE_ALLOW_PATH,
  218. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  219. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  220. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  221. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  222. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  223. "FFF\\FFFFF", SANITIZE_ERR_OK
  224. },
  225. { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  226. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  227. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  228. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  229. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  230. "FFF\\FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  231. SANITIZE_ALLOW_TRUNCATE,
  232. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  233. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  234. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  235. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  236. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  237. "FFF_F", SANITIZE_ERR_OK
  238. },
  239. #endif /* !MSDOS */
  240. { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  241. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  242. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  243. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  244. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  245. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  246. 0,
  247. NULL, SANITIZE_ERR_INVALID_PATH
  248. },
  249. { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  250. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  251. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  252. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  253. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  254. "FFFF\\FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  255. SANITIZE_ALLOW_TRUNCATE,
  256. NULL, SANITIZE_ERR_INVALID_PATH
  257. },
  258. { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  259. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  260. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  261. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  262. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  263. "FFFFFFFFFFFFFFFFFFFFFFFFF\\FFFFFFFFFFFFFFFFFFFFFFFF",
  264. SANITIZE_ALLOW_TRUNCATE | SANITIZE_ALLOW_PATH,
  265. NULL, SANITIZE_ERR_INVALID_PATH
  266. },
  267. { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  268. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  269. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  270. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  271. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  272. "FFF\\FFFFFFFFFFFFFFFFFFFFF:FFFFFFFFFFFFFFFFFFFFFFFF",
  273. SANITIZE_ALLOW_TRUNCATE | SANITIZE_ALLOW_PATH,
  274. NULL, SANITIZE_ERR_INVALID_PATH
  275. },
  276. { "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  277. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  278. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  279. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  280. "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
  281. "FF\\F:FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  282. SANITIZE_ALLOW_TRUNCATE | SANITIZE_ALLOW_PATH,
  283. NULL, SANITIZE_ERR_INVALID_PATH
  284. },
  285. { NULL, 0,
  286. NULL, SANITIZE_ERR_BAD_ARGUMENT
  287. },
  288. };
  289. size_t i;
  290. for(i = 0; i < sizeof(data) / sizeof(data[0]); ++i) {
  291. char *output = NULL;
  292. char *flagstr = NULL;
  293. char *received_ccstr = NULL;
  294. char *expected_ccstr = NULL;
  295. SANITIZEcode res;
  296. res = sanitize_file_name(&output, data[i].input, data[i].flags);
  297. if(res == data[i].expected_result &&
  298. ((!output && !data[i].expected_output) ||
  299. (output && data[i].expected_output &&
  300. !strcmp(output, data[i].expected_output)))) { /* OK */
  301. free(output);
  302. continue;
  303. }
  304. flagstr = getflagstr(data[i].flags);
  305. abort_unless(flagstr, "out of memory");
  306. received_ccstr = getcurlcodestr(res);
  307. abort_unless(received_ccstr, "out of memory");
  308. expected_ccstr = getcurlcodestr(data[i].expected_result);
  309. abort_unless(expected_ccstr, "out of memory");
  310. unitfail++;
  311. fprintf(stderr, "\n"
  312. "%s:%d sanitize_file_name failed.\n"
  313. "input: %s\n"
  314. "flags: %s\n"
  315. "output: %s\n"
  316. "result: %s\n"
  317. "expected output: %s\n"
  318. "expected result: %s\n",
  319. __FILE__, __LINE__,
  320. data[i].input,
  321. flagstr,
  322. (output ? output : "(null)"),
  323. received_ccstr,
  324. (data[i].expected_output ? data[i].expected_output : "(null)"),
  325. expected_ccstr);
  326. free(output);
  327. free(flagstr);
  328. free(received_ccstr);
  329. free(expected_ccstr);
  330. }
  331. } /* END sanitize_file_name */
  332. #else
  333. UNITTEST_START
  334. {
  335. fprintf(stderr, "Skipped test not for this platform\n");
  336. }
  337. #endif /* _WIN32 || MSDOS */
  338. UNITTEST_STOP