unit1395.c 2.6 KB

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