unit1395.c 2.6 KB

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