xref: /third_party/curl/tests/unit/unit1395.c (revision 13498266)
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
26/* copied from urlapi.c */
27extern int dedotdotify(const char *input, size_t clen, char **out);
28
29#include "memdebug.h"
30
31static CURLcode unit_setup(void)
32{
33  return CURLE_OK;
34}
35
36static void unit_stop(void)
37{
38
39}
40
41struct dotdot {
42  const char *input;
43  const char *output;
44};
45
46UNITTEST_START
47
48  unsigned int i;
49  int fails = 0;
50  const struct dotdot pairs[] = {
51    { "/a/b/c/./../../g", "/a/g" },
52    { "mid/content=5/../6", "mid/6" },
53    { "/hello/../moo", "/moo" },
54    { "/1/../1", "/1" },
55    { "/1/./1", "/1/1" },
56    { "/1/..", "/" },
57    { "/1/.", "/1/" },
58    { "/1/./..", "/" },
59    { "/1/./../2", "/2" },
60    { "/hello/1/./../2", "/hello/2" },
61    { "test/this", NULL },
62    { "test/this/../now", "test/now" },
63    { "/1../moo../foo", "/1../moo../foo"},
64    { "/../../moo", "/moo"},
65    { "/../../moo?", "/moo?"},
66    { "/123?", NULL},
67    { "/../moo/..?", "/" },
68    { "/", NULL },
69    { "", NULL },
70    { "/.../", "/.../" },
71    { "./moo", "moo" },
72    { "../moo", "moo" },
73    { "/.", "/" },
74    { "/..", "/" },
75    { "/moo/..", "/" },
76    { "/..", "/" },
77    { "/.", "/" },
78  };
79
80  for(i = 0; i < sizeof(pairs)/sizeof(pairs[0]); i++) {
81    char *out;
82    int err = dedotdotify(pairs[i].input, strlen(pairs[i].input), &out);
83    abort_unless(err == 0, "returned error");
84    abort_if(err && out, "returned error with output");
85
86    if(out && pairs[i].output && strcmp(out, pairs[i].output)) {
87      fprintf(stderr, "Test %u: '%s' gave '%s' instead of '%s'\n",
88              i, pairs[i].input, out, pairs[i].output);
89      fail("Test case output mismatched");
90      fails++;
91    }
92    else if((!out && pairs[i].output) ||
93            (out && !pairs[i].output)) {
94      fprintf(stderr, "Test %u: '%s' gave '%s' instead of '%s'\n",
95              i, pairs[i].input, out ? out : "(null)",
96              pairs[i].output ? pairs[i].output : "(null)");
97      fail("Test case output mismatched");
98      fails++;
99    }
100    else
101      fprintf(stderr, "Test %u: OK\n", i);
102    free(out);
103  }
104
105  fail_if(fails, "output mismatched");
106
107UNITTEST_STOP
108