| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "import-util.h" |
| 5 |
#include "tests.h" |
| 6 |
|
| 7 |
static void test_import_url_last_component_one(const char *input, const char *output, int ret) { |
| 8 |
_cleanup_free_ char *s = NULL; |
| 9 |
|
| 10 |
assert_se(import_url_last_component(input, &s) == ret); |
| 11 |
ASSERT_STREQ(output, s); |
| 12 |
} |
| 13 |
|
| 14 |
TEST(import_url_last_component) { |
| 15 |
test_import_url_last_component_one("https://foobar/waldo/quux", "quux", 0); |
| 16 |
test_import_url_last_component_one("https://foobar/waldo/quux/", "quux", 0); |
| 17 |
test_import_url_last_component_one("https://foobar/waldo/", "waldo", 0); |
| 18 |
test_import_url_last_component_one("https://foobar/", NULL, -EADDRNOTAVAIL); |
| 19 |
test_import_url_last_component_one("https://foobar", NULL, -EADDRNOTAVAIL); |
| 20 |
test_import_url_last_component_one("https://foobar/waldo/quux?foo=bar", "quux", 0); |
| 21 |
test_import_url_last_component_one("https://foobar/waldo/quux/?foo=bar", "quux", 0); |
| 22 |
test_import_url_last_component_one("https://foobar/waldo/quux/?foo=bar#piep", "quux", 0); |
| 23 |
test_import_url_last_component_one("https://foobar/waldo/quux/#piep", "quux", 0); |
| 24 |
test_import_url_last_component_one("https://foobar/waldo/quux#piep", "quux", 0); |
| 25 |
test_import_url_last_component_one("https://", NULL, -EINVAL); |
| 26 |
test_import_url_last_component_one("", NULL, -EINVAL); |
| 27 |
test_import_url_last_component_one(":", NULL, -EINVAL); |
| 28 |
test_import_url_last_component_one(":/", NULL, -EINVAL); |
| 29 |
test_import_url_last_component_one("x:/", NULL, -EINVAL); |
| 30 |
test_import_url_last_component_one("x:y", NULL, -EADDRNOTAVAIL); |
| 31 |
test_import_url_last_component_one("x:y/z", "z", 0); |
| 32 |
} |
| 33 |
|
| 34 |
static void test_import_url_change_suffix_one(const char *input, size_t n, const char *suffix, const char *output, int ret) { |
| 35 |
_cleanup_free_ char *s = NULL; |
| 36 |
|
| 37 |
assert_se(import_url_change_suffix(input, n, suffix, &s) == ret); |
| 38 |
ASSERT_STREQ(output, s); |
| 39 |
} |
| 40 |
|
| 41 |
TEST(import_url_change_suffix) { |
| 42 |
test_import_url_change_suffix_one("https://foobar/waldo/quux", 1, "wuff", "https://foobar/waldo/wuff", 0); |
| 43 |
test_import_url_change_suffix_one("https://foobar/waldo/quux/", 1, "wuff", "https://foobar/waldo/wuff", 0); |
| 44 |
test_import_url_change_suffix_one("https://foobar/waldo/quux///?mief", 1, "wuff", "https://foobar/waldo/wuff", 0); |
| 45 |
test_import_url_change_suffix_one("https://foobar/waldo/quux///?mief#opopo", 1, "wuff", "https://foobar/waldo/wuff", 0); |
| 46 |
test_import_url_change_suffix_one("https://foobar/waldo/quux/quff", 2, "wuff", "https://foobar/waldo/wuff", 0); |
| 47 |
test_import_url_change_suffix_one("https://foobar/waldo/quux/quff/", 2, "wuff", "https://foobar/waldo/wuff", 0); |
| 48 |
test_import_url_change_suffix_one("https://foobar/waldo/quux/quff", 0, "wuff", "https://foobar/waldo/quux/quff/wuff", 0); |
| 49 |
test_import_url_change_suffix_one("https://foobar/waldo/quux/quff?aa?bb##4", 0, "wuff", "https://foobar/waldo/quux/quff/wuff", 0); |
| 50 |
test_import_url_change_suffix_one("https://", 0, "wuff", NULL, -EINVAL); |
| 51 |
test_import_url_change_suffix_one("", 0, "wuff", NULL, -EINVAL); |
| 52 |
test_import_url_change_suffix_one(":", 0, "wuff", NULL, -EINVAL); |
| 53 |
test_import_url_change_suffix_one(":/", 0, "wuff", NULL, -EINVAL); |
| 54 |
test_import_url_change_suffix_one("x:/", 0, "wuff", NULL, -EINVAL); |
| 55 |
test_import_url_change_suffix_one("x:y", 0, "wuff", "x:y/wuff", 0); |
| 56 |
test_import_url_change_suffix_one("x:y/z", 0, "wuff", "x:y/z/wuff", 0); |
| 57 |
test_import_url_change_suffix_one("x:y/z/", 0, "wuff", "x:y/z/wuff", 0); |
| 58 |
test_import_url_change_suffix_one("x:y/z/", 1, "wuff", "x:y/wuff", 0); |
| 59 |
test_import_url_change_suffix_one("x:y/z/", 2, "wuff", "x:y/wuff", 0); |
| 60 |
} |
| 61 |
|
| 62 |
DEFINE_TEST_MAIN(LOG_INFO); |