113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci#include "curlcheck.h"
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci/* copied from urlapi.c */
2713498266Sopenharmony_ciextern int dedotdotify(const char *input, size_t clen, char **out);
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci#include "memdebug.h"
3013498266Sopenharmony_ci
3113498266Sopenharmony_cistatic CURLcode unit_setup(void)
3213498266Sopenharmony_ci{
3313498266Sopenharmony_ci  return CURLE_OK;
3413498266Sopenharmony_ci}
3513498266Sopenharmony_ci
3613498266Sopenharmony_cistatic void unit_stop(void)
3713498266Sopenharmony_ci{
3813498266Sopenharmony_ci
3913498266Sopenharmony_ci}
4013498266Sopenharmony_ci
4113498266Sopenharmony_cistruct dotdot {
4213498266Sopenharmony_ci  const char *input;
4313498266Sopenharmony_ci  const char *output;
4413498266Sopenharmony_ci};
4513498266Sopenharmony_ci
4613498266Sopenharmony_ciUNITTEST_START
4713498266Sopenharmony_ci
4813498266Sopenharmony_ci  unsigned int i;
4913498266Sopenharmony_ci  int fails = 0;
5013498266Sopenharmony_ci  const struct dotdot pairs[] = {
5113498266Sopenharmony_ci    { "/a/b/c/./../../g", "/a/g" },
5213498266Sopenharmony_ci    { "mid/content=5/../6", "mid/6" },
5313498266Sopenharmony_ci    { "/hello/../moo", "/moo" },
5413498266Sopenharmony_ci    { "/1/../1", "/1" },
5513498266Sopenharmony_ci    { "/1/./1", "/1/1" },
5613498266Sopenharmony_ci    { "/1/..", "/" },
5713498266Sopenharmony_ci    { "/1/.", "/1/" },
5813498266Sopenharmony_ci    { "/1/./..", "/" },
5913498266Sopenharmony_ci    { "/1/./../2", "/2" },
6013498266Sopenharmony_ci    { "/hello/1/./../2", "/hello/2" },
6113498266Sopenharmony_ci    { "test/this", NULL },
6213498266Sopenharmony_ci    { "test/this/../now", "test/now" },
6313498266Sopenharmony_ci    { "/1../moo../foo", "/1../moo../foo"},
6413498266Sopenharmony_ci    { "/../../moo", "/moo"},
6513498266Sopenharmony_ci    { "/../../moo?", "/moo?"},
6613498266Sopenharmony_ci    { "/123?", NULL},
6713498266Sopenharmony_ci    { "/../moo/..?", "/" },
6813498266Sopenharmony_ci    { "/", NULL },
6913498266Sopenharmony_ci    { "", NULL },
7013498266Sopenharmony_ci    { "/.../", "/.../" },
7113498266Sopenharmony_ci    { "./moo", "moo" },
7213498266Sopenharmony_ci    { "../moo", "moo" },
7313498266Sopenharmony_ci    { "/.", "/" },
7413498266Sopenharmony_ci    { "/..", "/" },
7513498266Sopenharmony_ci    { "/moo/..", "/" },
7613498266Sopenharmony_ci    { "/..", "/" },
7713498266Sopenharmony_ci    { "/.", "/" },
7813498266Sopenharmony_ci  };
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci  for(i = 0; i < sizeof(pairs)/sizeof(pairs[0]); i++) {
8113498266Sopenharmony_ci    char *out;
8213498266Sopenharmony_ci    int err = dedotdotify(pairs[i].input, strlen(pairs[i].input), &out);
8313498266Sopenharmony_ci    abort_unless(err == 0, "returned error");
8413498266Sopenharmony_ci    abort_if(err && out, "returned error with output");
8513498266Sopenharmony_ci
8613498266Sopenharmony_ci    if(out && pairs[i].output && strcmp(out, pairs[i].output)) {
8713498266Sopenharmony_ci      fprintf(stderr, "Test %u: '%s' gave '%s' instead of '%s'\n",
8813498266Sopenharmony_ci              i, pairs[i].input, out, pairs[i].output);
8913498266Sopenharmony_ci      fail("Test case output mismatched");
9013498266Sopenharmony_ci      fails++;
9113498266Sopenharmony_ci    }
9213498266Sopenharmony_ci    else if((!out && pairs[i].output) ||
9313498266Sopenharmony_ci            (out && !pairs[i].output)) {
9413498266Sopenharmony_ci      fprintf(stderr, "Test %u: '%s' gave '%s' instead of '%s'\n",
9513498266Sopenharmony_ci              i, pairs[i].input, out ? out : "(null)",
9613498266Sopenharmony_ci              pairs[i].output ? pairs[i].output : "(null)");
9713498266Sopenharmony_ci      fail("Test case output mismatched");
9813498266Sopenharmony_ci      fails++;
9913498266Sopenharmony_ci    }
10013498266Sopenharmony_ci    else
10113498266Sopenharmony_ci      fprintf(stderr, "Test %u: OK\n", i);
10213498266Sopenharmony_ci    free(out);
10313498266Sopenharmony_ci  }
10413498266Sopenharmony_ci
10513498266Sopenharmony_ci  fail_if(fails, "output mismatched");
10613498266Sopenharmony_ci
10713498266Sopenharmony_ciUNITTEST_STOP
108