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