| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <netinet/in.h> |
| 4 |
|
| 5 |
#include "ip-protocol-list.h" |
| 6 |
#include "stdio-util.h" |
| 7 |
#include "tests.h" |
| 8 |
|
| 9 |
static void test_int(int i) { |
| 10 |
char str[DECIMAL_STR_MAX(int)]; |
| 11 |
|
| 12 |
assert_se(ip_protocol_from_name(ip_protocol_to_name(i)) == i); |
| 13 |
|
| 14 |
xsprintf(str, "%i", i); |
| 15 |
assert_se(ip_protocol_from_name(ip_protocol_to_name(parse_ip_protocol(str))) == i); |
| 16 |
} |
| 17 |
|
| 18 |
static void test_int_fail(int i, int error) { |
| 19 |
char str[DECIMAL_STR_MAX(int)]; |
| 20 |
|
| 21 |
assert_se(!ip_protocol_to_name(i)); |
| 22 |
|
| 23 |
xsprintf(str, "%i", i); |
| 24 |
assert_se(parse_ip_protocol(str) == error); |
| 25 |
} |
| 26 |
|
| 27 |
static void test_str(const char *s) { |
| 28 |
ASSERT_STREQ(ip_protocol_to_name(ip_protocol_from_name(s)), s); |
| 29 |
ASSERT_STREQ(ip_protocol_to_name(parse_ip_protocol(s)), s); |
| 30 |
} |
| 31 |
|
| 32 |
static void test_str_fail(const char *s, int error) { |
| 33 |
assert_se(ip_protocol_from_name(s) == -EINVAL); |
| 34 |
assert_se(parse_ip_protocol(s) == error); |
| 35 |
} |
| 36 |
|
| 37 |
TEST(integer) { |
| 38 |
test_int(IPPROTO_TCP); |
| 39 |
test_int(IPPROTO_DCCP); |
| 40 |
test_int_fail(-1, -ERANGE); |
| 41 |
test_int_fail(1024 * 1024, -EPROTONOSUPPORT); |
| 42 |
} |
| 43 |
|
| 44 |
TEST(string) { |
| 45 |
test_str("sctp"); |
| 46 |
test_str("udp"); |
| 47 |
test_str_fail("hoge", -EINVAL); |
| 48 |
test_str_fail("-1", -ERANGE); |
| 49 |
test_str_fail("1000000000", -EPROTONOSUPPORT); |
| 50 |
} |
| 51 |
|
| 52 |
TEST(parse_ip_protocol) { |
| 53 |
assert_se(parse_ip_protocol("sctp") == IPPROTO_SCTP); |
| 54 |
assert_se(parse_ip_protocol("ScTp") == IPPROTO_SCTP); |
| 55 |
assert_se(parse_ip_protocol("mptcp") == IPPROTO_MPTCP); |
| 56 |
assert_se(parse_ip_protocol("MPTCP") == IPPROTO_MPTCP); |
| 57 |
assert_se(parse_ip_protocol("ip") == IPPROTO_IP); |
| 58 |
assert_se(parse_ip_protocol("") == IPPROTO_IP); |
| 59 |
assert_se(parse_ip_protocol("1") == 1); |
| 60 |
assert_se(parse_ip_protocol("0") == 0); |
| 61 |
assert_se(parse_ip_protocol("-10") == -ERANGE); |
| 62 |
assert_se(parse_ip_protocol("100000000") == -EPROTONOSUPPORT); |
| 63 |
} |
| 64 |
|
| 65 |
TEST(parse_ip_protocol_full) { |
| 66 |
assert_se(parse_ip_protocol_full("-1", true) == -ERANGE); |
| 67 |
assert_se(parse_ip_protocol_full("0", true) == 0); |
| 68 |
assert_se(parse_ip_protocol_full("11", true) == 11); |
| 69 |
} |
| 70 |
|
| 71 |
DEFINE_TEST_MAIN(LOG_INFO); |