| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <netinet/in.h> |
| 4 |
#include <sys/socket.h> |
| 5 |
|
| 6 |
#include "parse-helpers.h" |
| 7 |
#include "tests.h" |
| 8 |
|
| 9 |
static void test_valid_item( |
| 10 |
const char *str, |
| 11 |
int expected_af, |
| 12 |
int expected_ip_protocol, |
| 13 |
uint16_t expected_nr_ports, |
| 14 |
uint16_t expected_port_min) { |
| 15 |
uint16_t nr_ports, port_min; |
| 16 |
int af, ip_protocol; |
| 17 |
|
| 18 |
assert_se(parse_socket_bind_item(str, &af, &ip_protocol, &nr_ports, &port_min) >= 0); |
| 19 |
assert_se(af == expected_af); |
| 20 |
assert_se(ip_protocol == expected_ip_protocol); |
| 21 |
assert_se(nr_ports == expected_nr_ports); |
| 22 |
assert_se(port_min == expected_port_min); |
| 23 |
|
| 24 |
log_info("%s: \"%s\" ok", __func__, str); |
| 25 |
} |
| 26 |
|
| 27 |
static void test_invalid_item(const char *str) { |
| 28 |
uint16_t nr_ports, port_min; |
| 29 |
int af, ip_protocol; |
| 30 |
|
| 31 |
assert_se(parse_socket_bind_item(str, &af, &ip_protocol, &nr_ports, &port_min) == -EINVAL); |
| 32 |
|
| 33 |
log_info("%s: \"%s\" ok", __func__, str); |
| 34 |
} |
| 35 |
|
| 36 |
TEST(valid_items) { |
| 37 |
test_valid_item("any", AF_UNSPEC, 0, 0, 0); |
| 38 |
test_valid_item("0-65535", AF_UNSPEC, 0, 0, 0); |
| 39 |
test_valid_item("ipv4", AF_INET, 0, 0, 0); |
| 40 |
test_valid_item("ipv6", AF_INET6, 0, 0, 0); |
| 41 |
test_valid_item("ipv4:any", AF_INET, 0, 0, 0); |
| 42 |
test_valid_item("ipv6:any", AF_INET6, 0, 0, 0); |
| 43 |
test_valid_item("tcp", AF_UNSPEC, IPPROTO_TCP, 0, 0); |
| 44 |
test_valid_item("udp", AF_UNSPEC, IPPROTO_UDP, 0, 0); |
| 45 |
test_valid_item("tcp:any", AF_UNSPEC, IPPROTO_TCP, 0, 0); |
| 46 |
test_valid_item("udp:any", AF_UNSPEC, IPPROTO_UDP, 0, 0); |
| 47 |
test_valid_item("0", AF_UNSPEC, 0, 1, 0); |
| 48 |
test_valid_item("6666", AF_UNSPEC, 0, 1, 6666); |
| 49 |
test_valid_item("6666-6667", AF_UNSPEC, 0, 2, 6666); |
| 50 |
test_valid_item("65535", AF_UNSPEC, 0, 1, 65535); |
| 51 |
test_valid_item("1-65535", AF_UNSPEC, 0, 65535, 1); |
| 52 |
test_valid_item("ipv4:tcp", AF_INET, IPPROTO_TCP, 0, 0); |
| 53 |
test_valid_item("ipv4:udp", AF_INET, IPPROTO_UDP, 0, 0); |
| 54 |
test_valid_item("ipv6:tcp", AF_INET6, IPPROTO_TCP, 0, 0); |
| 55 |
test_valid_item("ipv6:udp", AF_INET6, IPPROTO_UDP, 0, 0); |
| 56 |
test_valid_item("ipv4:6666", AF_INET, 0, 1, 6666); |
| 57 |
test_valid_item("ipv6:6666", AF_INET6, 0, 1, 6666); |
| 58 |
test_valid_item("tcp:6666", AF_UNSPEC, IPPROTO_TCP, 1, 6666); |
| 59 |
test_valid_item("udp:6666", AF_UNSPEC, IPPROTO_UDP, 1, 6666); |
| 60 |
test_valid_item("ipv4:tcp:6666", AF_INET, IPPROTO_TCP, 1, 6666); |
| 61 |
test_valid_item("ipv6:tcp:6666", AF_INET6, IPPROTO_TCP, 1, 6666); |
| 62 |
test_valid_item("ipv6:udp:6666-6667", AF_INET6, IPPROTO_UDP, 2, 6666); |
| 63 |
test_valid_item("ipv6:tcp:any", AF_INET6, IPPROTO_TCP, 0, 0); |
| 64 |
test_valid_item("ipv6:tcp:0", AF_INET6, IPPROTO_TCP, 1, 0); |
| 65 |
} |
| 66 |
|
| 67 |
TEST(invalid_items) { |
| 68 |
test_invalid_item(""); |
| 69 |
test_invalid_item(":"); |
| 70 |
test_invalid_item("::"); |
| 71 |
test_invalid_item("any:"); |
| 72 |
test_invalid_item("meh"); |
| 73 |
test_invalid_item("zupa:meh"); |
| 74 |
test_invalid_item("zupa:meh:eh"); |
| 75 |
test_invalid_item("ip"); |
| 76 |
test_invalid_item("dccp"); |
| 77 |
test_invalid_item("ipv6meh"); |
| 78 |
test_invalid_item("ipv6::"); |
| 79 |
test_invalid_item("ipv6:ipv6"); |
| 80 |
test_invalid_item("ipv6:icmp"); |
| 81 |
test_invalid_item("65536"); |
| 82 |
test_invalid_item("ipv6:tcp:6666-6665"); |
| 83 |
test_invalid_item("ipv6:tcp:6666-100000"); |
| 84 |
test_invalid_item("ipv6::6666"); |
| 85 |
test_invalid_item("ipv6:tcp:any:"); |
| 86 |
test_invalid_item("ipv6:tcp:any:ipv6"); |
| 87 |
test_invalid_item("ipv6:tcp:6666:zupa"); |
| 88 |
test_invalid_item("ipv6:tcp:6666:any"); |
| 89 |
test_invalid_item("ipv6:tcp:6666 zupa"); |
| 90 |
test_invalid_item("ipv6:tcp:6666: zupa"); |
| 91 |
test_invalid_item("ipv6:tcp:6666\n zupa"); |
| 92 |
} |
| 93 |
|
| 94 |
static int test_path_simplify_and_warn_one(const char *p, const char *q, PathSimplifyWarnFlags f) { |
| 95 |
_cleanup_free_ char *s = ASSERT_PTR(strdup(p)); |
| 96 |
int a, b; |
| 97 |
|
| 98 |
a = path_simplify_and_warn(s, f, /* unit= */ NULL, /* filename= */ NULL, /* line= */ 0, "Foobar="); |
| 99 |
assert(streq_ptr(s, q)); |
| 100 |
|
| 101 |
free(s); |
| 102 |
s = ASSERT_PTR(strdup(p)); |
| 103 |
|
| 104 |
b = path_simplify_and_warn(s, f|PATH_CHECK_FATAL, /* unit= */ NULL, /* filename= */ NULL, /* line= */ 0, "Foobar="); |
| 105 |
assert(streq_ptr(s, q)); |
| 106 |
|
| 107 |
assert(a == b); |
| 108 |
|
| 109 |
return a; |
| 110 |
} |
| 111 |
|
| 112 |
TEST(path_simplify_and_warn) { |
| 113 |
|
| 114 |
assert_se(test_path_simplify_and_warn_one("", "", 0) == -EINVAL); |
| 115 |
assert_se(test_path_simplify_and_warn_one("/", "/", 0) == 0); |
| 116 |
assert_se(test_path_simplify_and_warn_one("/foo/../bar", "/foo/../bar", 0) == -EINVAL); |
| 117 |
assert_se(test_path_simplify_and_warn_one("/foo/./bar", "/foo/bar", 0) == 0); |
| 118 |
assert_se(test_path_simplify_and_warn_one("/proc/self///fd", "/proc/self/fd", 0) == 0); |
| 119 |
assert_se(test_path_simplify_and_warn_one("/proc/self///fd", "/proc/self/fd", PATH_CHECK_NON_API_VFS) == -EINVAL); |
| 120 |
assert_se(test_path_simplify_and_warn_one("aaaa", "aaaa", 0) == 0); |
| 121 |
assert_se(test_path_simplify_and_warn_one("aaaa", "aaaa", PATH_CHECK_ABSOLUTE) == -EINVAL); |
| 122 |
assert_se(test_path_simplify_and_warn_one("aaaa", "aaaa", PATH_CHECK_RELATIVE) == 0); |
| 123 |
assert_se(test_path_simplify_and_warn_one("/aaaa", "/aaaa", 0) == 0); |
| 124 |
assert_se(test_path_simplify_and_warn_one("/aaaa", "/aaaa", PATH_CHECK_ABSOLUTE) == 0); |
| 125 |
assert_se(test_path_simplify_and_warn_one("/aaaa", "/aaaa", PATH_CHECK_RELATIVE) == -EINVAL); |
| 126 |
} |
| 127 |
|
| 128 |
DEFINE_TEST_MAIN(LOG_INFO); |