diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2023-09-16 12:43:16 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2023-09-22 08:17:42 +0200 |
commit | 1b2733b4129c132d598ec96a07b4f93386ad4e0f (patch) | |
tree | a87cc1c467153990ca437d035e57f2570bb2b53d /src/test/test-ip-protocol-list.c | |
parent | network/vxlan: avoid unneccesary temporary variables (diff) | |
download | systemd-1b2733b4129c132d598ec96a07b4f93386ad4e0f.tar.xz systemd-1b2733b4129c132d598ec96a07b4f93386ad4e0f.zip |
shared/ip-procotol-list: generalize and rework parse_ip_protocol()
Optionally, accept protocols that don't have a known name.
Avoid any allocations in the common case.
Return more granular error codes: -ERANGE for negative values,
-EOPNOTSUPP if the protocol is a valid number, but we don't know
the protocol, and -EINVAL only if it's not a numerical string.
Diffstat (limited to '')
-rw-r--r-- | src/test/test-ip-protocol-list.c | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/src/test/test-ip-protocol-list.c b/src/test/test-ip-protocol-list.c index 018441d497..dfff015f53 100644 --- a/src/test/test-ip-protocol-list.c +++ b/src/test/test-ip-protocol-list.c @@ -17,13 +17,13 @@ static void test_int(int i) { assert_se(ip_protocol_from_name(ip_protocol_to_name(parse_ip_protocol(str))) == i); } -static void test_int_fail(int i) { +static void test_int_fail(int i, int error) { char str[DECIMAL_STR_MAX(int)]; assert_se(!ip_protocol_to_name(i)); xsprintf(str, "%i", i); - assert_se(parse_ip_protocol(str) == -EINVAL); + assert_se(parse_ip_protocol(str) == error); } static void test_str(const char *s) { @@ -31,39 +31,41 @@ static void test_str(const char *s) { assert_se(streq(ip_protocol_to_name(parse_ip_protocol(s)), s)); } -static void test_str_fail(const char *s) { +static void test_str_fail(const char *s, int error) { assert_se(ip_protocol_from_name(s) == -EINVAL); - assert_se(parse_ip_protocol(s) == -EINVAL); -} - -static void test_parse_ip_protocol_one(const char *s, int expected) { - assert_se(parse_ip_protocol(s) == expected); + assert_se(parse_ip_protocol(s) == error); } TEST(integer) { test_int(IPPROTO_TCP); test_int(IPPROTO_DCCP); - test_int_fail(-1); - test_int_fail(1024 * 1024); + test_int_fail(-1, -ERANGE); + test_int_fail(1024 * 1024, -EPROTONOSUPPORT); } TEST(string) { test_str("sctp"); test_str("udp"); - test_str_fail("hoge"); - test_str_fail("-1"); - test_str_fail("1000000000"); + test_str_fail("hoge", -EINVAL); + test_str_fail("-1", -ERANGE); + test_str_fail("1000000000", -EPROTONOSUPPORT); } TEST(parse_ip_protocol) { - test_parse_ip_protocol_one("sctp", IPPROTO_SCTP); - test_parse_ip_protocol_one("ScTp", IPPROTO_SCTP); - test_parse_ip_protocol_one("ip", IPPROTO_IP); - test_parse_ip_protocol_one("", IPPROTO_IP); - test_parse_ip_protocol_one("1", 1); - test_parse_ip_protocol_one("0", 0); - test_parse_ip_protocol_one("-10", -EINVAL); - test_parse_ip_protocol_one("100000000", -EINVAL); + assert_se(parse_ip_protocol("sctp") == IPPROTO_SCTP); + assert_se(parse_ip_protocol("ScTp") == IPPROTO_SCTP); + assert_se(parse_ip_protocol("ip") == IPPROTO_IP); + assert_se(parse_ip_protocol("") == IPPROTO_IP); + assert_se(parse_ip_protocol("1") == 1); + assert_se(parse_ip_protocol("0") == 0); + assert_se(parse_ip_protocol("-10") == -ERANGE); + assert_se(parse_ip_protocol("100000000") == -EPROTONOSUPPORT); +} + +TEST(parse_ip_protocol_full) { + assert_se(parse_ip_protocol_full("-1", true) == -ERANGE); + assert_se(parse_ip_protocol_full("0", true) == 0); + assert_se(parse_ip_protocol_full("11", true) == 11); } DEFINE_TEST_MAIN(LOG_INFO); |