1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2016, 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
unit_setup(void)28 static CURLcode unit_setup(void)
29 {
30 return CURLE_OK;
31 }
32
unit_stop(void)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 };
69
70 for(i=0; i < sizeof(pairs)/sizeof(pairs[0]); i++) {
71 char *out = Curl_dedotdotify((char *)pairs[i].input);
72 abort_unless(out != NULL, "returned NULL!");
73
74 if(strcmp(out, pairs[i].output)) {
75 fprintf(stderr, "Test %d: '%s' gave '%s' instead of '%s'\n",
76 i, pairs[i].input, out, pairs[i].output);
77 fail("Test case output mismatched");
78 fails++;
79 }
80 else
81 fprintf(stderr, "Test %d: OK\n", i);
82 free(out);
83 }
84
85 fail_if(fails, "output mismatched");
86
87 UNITTEST_STOP
88