unit1395.c (4370B)
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 "unitcheck.h" 25 #include "memdebug.h" 26 #include "unitprotos.h" 27 28 static CURLcode test_unit1395(char *arg) 29 { 30 UNITTEST_BEGIN_SIMPLE 31 32 unsigned int i; 33 int fails = 0; 34 35 struct dotdot { 36 const char *input; 37 const char *output; 38 }; 39 40 const struct dotdot pairs[] = { 41 { "%2f%2e%2e%2f/../a", "%2f%2e%2e%2f/a" }, 42 { "%2f%2e%2e%2f/../", "%2f%2e%2e%2f/" }, 43 { "%2f%2e%2e%2f/.", "%2f%2e%2e%2f/" }, 44 { "%2f%2e%2e%2f/", "%2f%2e%2e%2f/" }, 45 { "%2f%2e%2e%2f", "%2f%2e%2e%2f" }, 46 { "%2f%2e%2e%2", "%2f%2e%2e%2" }, 47 { "%2f%2e%2e%", "%2f%2e%2e%" }, 48 { "%2f%2e%2e", "%2f%2e%2e" }, 49 { "%2f%2e%2", "%2f%2e%2" }, 50 { "%2f%2e%", "%2f%2e%" }, 51 { "%2f%2e", "%2f%2e" }, 52 { "%2f%2", "%2f%2" }, 53 { "%2f%", "%2f%" }, 54 { "%2f", "%2f" }, 55 { "%2", "%2" }, 56 { "%", NULL }, 57 { "2", NULL }, 58 { "e", NULL }, 59 { ".", NULL }, 60 { "./", "" }, 61 { "..", "" }, 62 { "../", "" }, 63 { "../a", "a" }, 64 { "///moo.", "///moo." }, 65 { ".///moo.", "//moo." }, 66 { "./moo..", "moo.." }, 67 { "./moo../", "moo../" }, 68 { "./moo../.m", "moo../.m" }, 69 { "./moo", "moo" }, 70 { "../moo", "moo" }, 71 { "../moo?", "moo?" }, 72 { "../moo?#", "moo?#" }, 73 { "../moo?#?..", "moo?#?.." }, 74 { "/../moo/..", "/" }, 75 { "/a/c/%2e%2E/b", "/a/b" }, 76 { "/a/%2e/g", "/a/g" }, 77 { "/a/b/c/./g", "/a/b/c/g" }, 78 { "/a/c/../b", "/a/b" }, 79 { "/a/b/c/./../../g", "/a/g" }, 80 { "/a/b/c/./%2e%2E/../g", "/a/g" }, 81 { "/a/b/c/./../%2e%2E/g", "/a/g" }, 82 { "/a/b/c/%2E/%2e%2E/%2e%2E/g", "/a/g" }, 83 { "mid/content=5/../6", "mid/6" }, 84 { "/hello/../moo", "/moo" }, 85 { "/1/../1", "/1" }, 86 { "/1/./1", "/1/1" }, 87 { "/1/%2e/1", "/1/1" }, 88 { "/1/%2E/1", "/1/1" }, 89 { "/1/..", "/" }, 90 { "/1/.", "/1/" }, 91 { "/1/%2e", "/1/" }, 92 { "/1/%2E", "/1/" }, 93 { "/1/./..", "/" }, 94 { "/1/%2e/.%2E", "/" }, 95 { "/1/./%2e.", "/" }, 96 { "/1/./../2", "/2" }, 97 { "/hello/1/./../2", "/hello/2" }, 98 { "test/this", "test/this" }, 99 { "test/this/../now", "test/now" }, 100 { "/1../moo../foo", "/1../moo../foo"}, 101 { "/../../moo", "/moo"}, 102 { "/../../moo?", "/moo?"}, 103 { "/123?", "/123?" }, 104 { "/", NULL }, 105 { "", NULL }, 106 { "/.../", "/.../" }, 107 { "/.", "/" }, 108 { "/..", "/" }, 109 { "/moo/..", "/" }, 110 { "/..", "/" }, 111 { "/.", "/" }, 112 }; 113 114 for(i = 0; i < CURL_ARRAYSIZE(pairs); i++) { 115 char *out; 116 int err = dedotdotify(pairs[i].input, strlen(pairs[i].input), &out); 117 abort_unless(err == 0, "returned error"); 118 abort_if(err && out, "returned error with output"); 119 120 if(out && pairs[i].output && strcmp(out, pairs[i].output)) { 121 curl_mfprintf(stderr, "Test %u: '%s' gave '%s' instead of '%s'\n", 122 i, pairs[i].input, out, pairs[i].output); 123 fail("Test case output mismatched"); 124 fails++; 125 } 126 else if((!out && pairs[i].output) || 127 (out && !pairs[i].output)) { 128 curl_mfprintf(stderr, "Test %u: '%s' gave '%s' instead of '%s'\n", 129 i, pairs[i].input, out ? out : "(null)", 130 pairs[i].output ? pairs[i].output : "(null)"); 131 fail("Test case output mismatched"); 132 fails++; 133 } 134 else 135 curl_mfprintf(stderr, "Test %u: OK\n", i); 136 free(out); 137 } 138 139 fail_if(fails, "output mismatched"); 140 141 UNITTEST_END_SIMPLE 142 }