]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-ip-protocol-list.c
ASSERT_STREQ for simple cases
[thirdparty/systemd.git] / src / test / test-ip-protocol-list.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <netinet/in.h>
4
5 #include "macro.h"
6 #include "ip-protocol-list.h"
7 #include "stdio-util.h"
8 #include "string-util.h"
9 #include "tests.h"
10
11 static void test_int(int i) {
12 char str[DECIMAL_STR_MAX(int)];
13
14 assert_se(ip_protocol_from_name(ip_protocol_to_name(i)) == i);
15
16 xsprintf(str, "%i", i);
17 assert_se(ip_protocol_from_name(ip_protocol_to_name(parse_ip_protocol(str))) == i);
18 }
19
20 static void test_int_fail(int i, int error) {
21 char str[DECIMAL_STR_MAX(int)];
22
23 assert_se(!ip_protocol_to_name(i));
24
25 xsprintf(str, "%i", i);
26 assert_se(parse_ip_protocol(str) == error);
27 }
28
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);
32 }
33
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);
37 }
38
39 TEST(integer) {
40 test_int(IPPROTO_TCP);
41 test_int(IPPROTO_DCCP);
42 test_int_fail(-1, -ERANGE);
43 test_int_fail(1024 * 1024, -EPROTONOSUPPORT);
44 }
45
46 TEST(string) {
47 test_str("sctp");
48 test_str("udp");
49 test_str_fail("hoge", -EINVAL);
50 test_str_fail("-1", -ERANGE);
51 test_str_fail("1000000000", -EPROTONOSUPPORT);
52 }
53
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);
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);