unit1395.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /* copied from urlapi.c */
  26. extern int dedotdotify(const char *input, size_t clen, char **out);
  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", NULL },
  54. { "test/this/../now", "test/now" },
  55. { "/1../moo../foo", "/1../moo../foo"},
  56. { "/../../moo", "/moo"},
  57. { "/../../moo?", "/moo?"},
  58. { "/123?", NULL},
  59. { "/../moo/..?", "/" },
  60. { "/", NULL },
  61. { "", NULL },
  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;
  73. int err = dedotdotify(pairs[i].input, strlen(pairs[i].input), &out);
  74. abort_unless(err == 0, "returned error");
  75. abort_if(err && out, "returned error with output");
  76. if(out && pairs[i].output && strcmp(out, pairs[i].output)) {
  77. fprintf(stderr, "Test %u: '%s' gave '%s' instead of '%s'\n",
  78. i, pairs[i].input, out, pairs[i].output);
  79. fail("Test case output mismatched");
  80. fails++;
  81. }
  82. else if((!out && pairs[i].output) ||
  83. (out && !pairs[i].output)) {
  84. fprintf(stderr, "Test %u: '%s' gave '%s' instead of '%s'\n",
  85. i, pairs[i].input, out ? out : "(null)",
  86. pairs[i].output ? pairs[i].output : "(null)");
  87. fail("Test case output mismatched");
  88. fails++;
  89. }
  90. else
  91. fprintf(stderr, "Test %u: OK\n", i);
  92. free(out);
  93. }
  94. fail_if(fails, "output mismatched");
  95. UNITTEST_STOP