unit1395.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /* copied from urlapi.c */
  26. extern char *dedotdotify(const char *input, size_t clen);
  27. #include "memdebug.h"
  28. static CURLcode unit_setup(void)
  29. {
  30. return CURLE_OK;
  31. }
  32. static void unit_stop(void)
  33. {
  34. }
  35. struct dotdot {
  36. const char *input;
  37. const char *output;
  38. };
  39. UNITTEST_START
  40. unsigned int i;
  41. int fails = 0;
  42. const struct dotdot pairs[] = {
  43. { "/a/b/c/./../../g", "/a/g" },
  44. { "mid/content=5/../6", "mid/6" },
  45. { "/hello/../moo", "/moo" },
  46. { "/1/../1", "/1" },
  47. { "/1/./1", "/1/1" },
  48. { "/1/..", "/" },
  49. { "/1/.", "/1/" },
  50. { "/1/./..", "/" },
  51. { "/1/./../2", "/2" },
  52. { "/hello/1/./../2", "/hello/2" },
  53. { "test/this", "test/this" },
  54. { "test/this/../now", "test/now" },
  55. { "/1../moo../foo", "/1../moo../foo"},
  56. { "/../../moo", "/moo"},
  57. { "/../../moo?andnot/../yay", "/moo?andnot/../yay"},
  58. { "/123?foo=/./&bar=/../", "/123?foo=/./&bar=/../"},
  59. { "/../moo/..?what", "/?what" },
  60. { "/", "/" },
  61. { "", "" },
  62. { "/.../", "/.../" },
  63. { "./moo", "moo" },
  64. { "../moo", "moo" },
  65. { "/.", "/" },
  66. { "/..", "/" },
  67. { "/moo/..", "/" },
  68. { "..", "" },
  69. { ".", "" },
  70. };
  71. for(i = 0; i < sizeof(pairs)/sizeof(pairs[0]); i++) {
  72. char *out = dedotdotify(pairs[i].input, strlen(pairs[i].input));
  73. abort_unless(out != NULL, "returned NULL!");
  74. if(strcmp(out, pairs[i].output)) {
  75. fprintf(stderr, "Test %u: '%s' gave '%s' instead of '%s'\n",
  76. i, pairs[i].input, out, pairs[i].output);
  77. fail("Test case output mismatched");
  78. fails++;
  79. }
  80. else
  81. fprintf(stderr, "Test %u: OK\n", i);
  82. free(out);
  83. }
  84. fail_if(fails, "output mismatched");
  85. UNITTEST_STOP