]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-ip-protocol-list.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <netinet/in.h>
6 #include "ip-protocol-list.h"
7 #include "stdio-util.h"
8 #include "string-util.h"
11 static void test_int(int i
) {
12 char str
[DECIMAL_STR_MAX(int)];
14 assert_se(ip_protocol_from_name(ip_protocol_to_name(i
)) == i
);
16 xsprintf(str
, "%i", i
);
17 assert_se(ip_protocol_from_name(ip_protocol_to_name(parse_ip_protocol(str
))) == i
);
20 static void test_int_fail(int i
, int error
) {
21 char str
[DECIMAL_STR_MAX(int)];
23 assert_se(!ip_protocol_to_name(i
));
25 xsprintf(str
, "%i", i
);
26 assert_se(parse_ip_protocol(str
) == error
);
29 static void test_str(const char *s
) {
30 ASSERT_STREQ(ip_protocol_to_name(ip_protocol_from_name(s
)), s
);
31 ASSERT_STREQ(ip_protocol_to_name(parse_ip_protocol(s
)), s
);
34 static void test_str_fail(const char *s
, int error
) {
35 assert_se(ip_protocol_from_name(s
) == -EINVAL
);
36 assert_se(parse_ip_protocol(s
) == error
);
40 test_int(IPPROTO_TCP
);
41 test_int(IPPROTO_DCCP
);
42 test_int_fail(-1, -ERANGE
);
43 test_int_fail(1024 * 1024, -EPROTONOSUPPORT
);
49 test_str_fail("hoge", -EINVAL
);
50 test_str_fail("-1", -ERANGE
);
51 test_str_fail("1000000000", -EPROTONOSUPPORT
);
54 TEST(parse_ip_protocol
) {
55 assert_se(parse_ip_protocol("sctp") == IPPROTO_SCTP
);
56 assert_se(parse_ip_protocol("ScTp") == IPPROTO_SCTP
);
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
);
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);
71 DEFINE_TEST_MAIN(LOG_INFO
);