Debian Codegraph @

src/test/test-ether-addr-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "ether-addr-util.h"
4 #include "string-util.h"
5 #include "tests.h"
6
7 TEST(ether_addr_helpers) {
8 struct ether_addr a;
9
10 a = ETHER_ADDR_NULL;
11 assert_se(ether_addr_is_null(&a));
12 assert_se(!ether_addr_is_broadcast(&a));
13 assert_se(!ether_addr_is_multicast(&a));
14 assert_se(ether_addr_is_unicast(&a));
15 assert_se(!ether_addr_is_local(&a));
16 assert_se(ether_addr_is_global(&a));
17
18 memset(a.ether_addr_octet, 0xff, sizeof(a));
19 assert_se(!ether_addr_is_null(&a));
20 assert_se(ether_addr_is_broadcast(&a));
21 assert_se(ether_addr_is_multicast(&a));
22 assert_se(!ether_addr_is_unicast(&a));
23 assert_se(ether_addr_is_local(&a));
24 assert_se(!ether_addr_is_global(&a));
25
26 a = (struct ether_addr) { { 0x01, 0x23, 0x34, 0x56, 0x78, 0x9a } };
27 assert_se(!ether_addr_is_null(&a));
28 assert_se(!ether_addr_is_broadcast(&a));
29 assert_se(ether_addr_is_multicast(&a));
30 assert_se(!ether_addr_is_unicast(&a));
31 assert_se(!ether_addr_is_local(&a));
32 assert_se(ether_addr_is_global(&a));
33 }
34
35 #define INFINIBAD_ADDR_1 ((const struct hw_addr_data){ .length = 20, .infiniband = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20} })
36
37 TEST(HW_ADDR_TO_STRING) {
38 const char *s = HW_ADDR_TO_STR(&(const struct hw_addr_data){6});
39 log_info("null: %s", s);
40
41 log_info("null×2: %s, %s",
42 HW_ADDR_TO_STR(&(const struct hw_addr_data){6}),
43 HW_ADDR_TO_STR(&(const struct hw_addr_data){6}));
44 log_info("null×3: %s, %s, %s",
45 HW_ADDR_TO_STR(&(const struct hw_addr_data){6}),
46 s,
47 HW_ADDR_TO_STR(&(const struct hw_addr_data){6}));
48
49 log_info("infiniband: %s", HW_ADDR_TO_STR(&INFINIBAD_ADDR_1));
50
51 /* Let's nest function calls in a stupid way. */
52 _cleanup_free_ char *t = NULL;
53 log_info("infiniband×3: %s\n%14s%s\n%14s%s",
54 HW_ADDR_TO_STR(&(const struct hw_addr_data){20}), "",
55 t = strdup(HW_ADDR_TO_STR(&INFINIBAD_ADDR_1)), "",
56 HW_ADDR_TO_STR(&(const struct hw_addr_data){20}));
57
58 const char *p;
59 /* Let's use a separate selection statement */
60 if ((p = HW_ADDR_TO_STR(&(const struct hw_addr_data){6})))
61 log_info("joint: %s, %s", s, p);
62 }
63
64 static void test_parse_hw_addr_full_one(const char *in, size_t expected_len, const char *expected) {
65 struct hw_addr_data h;
66 int r;
67
68 r = parse_hw_addr_full(in, expected_len, &h);
69 log_debug_errno(r, "parse_hw_addr(\"%s\", len=%zu) → \"%s\" (expected: \"%s\") : %d/%m",
70 in, expected_len, r >= 0 ? HW_ADDR_TO_STR(&h) : "n/a", strna(expected), r);
71 assert_se((r >= 0) == !!expected);
72 if (r >= 0) {
73 if (!IN_SET(expected_len, 0, SIZE_MAX))
74 assert_se(h.length == expected_len);
75 ASSERT_STREQ(HW_ADDR_TO_STR(&h), expected);
76 }
77 }
78
79 TEST(parse_hw_addr) {
80 /* IPv4 */
81 test_parse_hw_addr_full_one("10.0.0.1", 0, "0a:00:00:01");
82 test_parse_hw_addr_full_one("10.0.0.1", 4, "0a:00:00:01");
83 test_parse_hw_addr_full_one("192.168.0.1", 0, "c0:a8:00:01");
84 test_parse_hw_addr_full_one("192.168.0.1", 4, "c0:a8:00:01");
85 /* IPv6 */
86 test_parse_hw_addr_full_one("::", 0, "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00");
87 test_parse_hw_addr_full_one("::", 16, "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00");
88 test_parse_hw_addr_full_one("::1", 0, "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01");
89 test_parse_hw_addr_full_one("::1", 16, "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01");
90 test_parse_hw_addr_full_one("1234::", 0, "12:34:00:00:00:00:00:00:00:00:00:00:00:00:00:00");
91 test_parse_hw_addr_full_one("1234::", 16, "12:34:00:00:00:00:00:00:00:00:00:00:00:00:00:00");
92 test_parse_hw_addr_full_one("12:34::56", 0, "00:12:00:34:00:00:00:00:00:00:00:00:00:00:00:56");
93 test_parse_hw_addr_full_one("12:34::56", 16, "00:12:00:34:00:00:00:00:00:00:00:00:00:00:00:56");
94 test_parse_hw_addr_full_one("12aa:34::56", 0, "12:aa:00:34:00:00:00:00:00:00:00:00:00:00:00:56");
95 test_parse_hw_addr_full_one("12aa:34::56", 16, "12:aa:00:34:00:00:00:00:00:00:00:00:00:00:00:56");
96 test_parse_hw_addr_full_one("1234:5678:90ab:cdef:1234:5678:90ab:cdef", 0, "12:34:56:78:90:ab:cd:ef:12:34:56:78:90:ab:cd:ef");
97 test_parse_hw_addr_full_one("1234:5678:90ab:cdef:1234:5678:90ab:cdef", 16, "12:34:56:78:90:ab:cd:ef:12:34:56:78:90:ab:cd:ef");
98 /* Dot */
99 test_parse_hw_addr_full_one("12.34", 0, "00:12:00:34");
100 test_parse_hw_addr_full_one("12.34", 4, "00:12:00:34");
101 test_parse_hw_addr_full_one("12.34", SIZE_MAX, "00:12:00:34");
102 test_parse_hw_addr_full_one("12.34.56", 0, "00:12:00:34:00:56");
103 test_parse_hw_addr_full_one("12.34.56", 6, "00:12:00:34:00:56");
104 test_parse_hw_addr_full_one("12.34.56", SIZE_MAX, "00:12:00:34:00:56");
105 test_parse_hw_addr_full_one("12.34.56.78", 0, "0c:22:38:4e"); /* IPv4 address */
106 test_parse_hw_addr_full_one("12.34.56.78", 4, "0c:22:38:4e"); /* IPv4 address */
107 test_parse_hw_addr_full_one("12.34.56.78", 8, "00:12:00:34:00:56:00:78");
108 test_parse_hw_addr_full_one("12.34.56.78", SIZE_MAX, "00:12:00:34:00:56:00:78");
109 test_parse_hw_addr_full_one("12.34.56.78.90", 0, NULL);
110 test_parse_hw_addr_full_one("12.34.56.78.90", 10, "00:12:00:34:00:56:00:78:00:90");
111 test_parse_hw_addr_full_one("12.34.56.78.90", SIZE_MAX, "00:12:00:34:00:56:00:78:00:90");
112 test_parse_hw_addr_full_one("aabb.ccdd", 0, "aa:bb:cc:dd");
113 test_parse_hw_addr_full_one("aabb.ccdd", 4, "aa:bb:cc:dd");
114 test_parse_hw_addr_full_one("aabb.ccdd", SIZE_MAX, "aa:bb:cc:dd");
115 test_parse_hw_addr_full_one("aabb.ccdd.eeff", 0, "aa:bb:cc:dd:ee:ff");
116 test_parse_hw_addr_full_one("aabb.ccdd.eeff", 6, "aa:bb:cc:dd:ee:ff");
117 test_parse_hw_addr_full_one("aabb.ccdd.eeff", SIZE_MAX, "aa:bb:cc:dd:ee:ff");
118 /* Colon */
119 test_parse_hw_addr_full_one("12:34", 0, NULL);
120 test_parse_hw_addr_full_one("12:34", 2, "12:34");
121 test_parse_hw_addr_full_one("12:34", SIZE_MAX, "12:34");
122 test_parse_hw_addr_full_one("12:34:56:78:90:ab", 0, "12:34:56:78:90:ab");
123 test_parse_hw_addr_full_one("12:34:56:78:90:ab", 6, "12:34:56:78:90:ab");
124 test_parse_hw_addr_full_one("12:34:56:78:90:ab", SIZE_MAX, "12:34:56:78:90:ab");
125 test_parse_hw_addr_full_one("12:34:56:78:90:ab:cd:ef", 0, "00:12:00:34:00:56:00:78:00:90:00:ab:00:cd:00:ef"); /* IPv6 */
126 test_parse_hw_addr_full_one("12:34:56:78:90:ab:cd:ef", 8, "12:34:56:78:90:ab:cd:ef");
127 test_parse_hw_addr_full_one("12:34:56:78:90:ab:cd:ef", 16, "00:12:00:34:00:56:00:78:00:90:00:ab:00:cd:00:ef"); /* IPv6 */
128 test_parse_hw_addr_full_one("12:34:56:78:90:ab:cd:ef", SIZE_MAX, "12:34:56:78:90:ab:cd:ef");
129 test_parse_hw_addr_full_one("12:34:56:78:90:AB:CD:EF", 0, "00:12:00:34:00:56:00:78:00:90:00:ab:00:cd:00:ef"); /* IPv6 */
130 test_parse_hw_addr_full_one("12:34:56:78:90:AB:CD:EF", 8, "12:34:56:78:90:ab:cd:ef");
131 test_parse_hw_addr_full_one("12:34:56:78:90:AB:CD:EF", 16, "00:12:00:34:00:56:00:78:00:90:00:ab:00:cd:00:ef"); /* IPv6 */
132 test_parse_hw_addr_full_one("12:34:56:78:90:AB:CD:EF", SIZE_MAX, "12:34:56:78:90:ab:cd:ef");
133 /* Hyphen */
134 test_parse_hw_addr_full_one("12-34", 0, NULL);
135 test_parse_hw_addr_full_one("12-34", 2, "12:34");
136 test_parse_hw_addr_full_one("12-34", SIZE_MAX, "12:34");
137 test_parse_hw_addr_full_one("12-34-56-78-90-ab-cd-ef", 0, NULL);
138 test_parse_hw_addr_full_one("12-34-56-78-90-ab-cd-ef", 8, "12:34:56:78:90:ab:cd:ef");
139 test_parse_hw_addr_full_one("12-34-56-78-90-ab-cd-ef", SIZE_MAX, "12:34:56:78:90:ab:cd:ef");
140 test_parse_hw_addr_full_one("12-34-56-78-90-AB-CD-EF", 0, NULL);
141 test_parse_hw_addr_full_one("12-34-56-78-90-AB-CD-EF", 8, "12:34:56:78:90:ab:cd:ef");
142 test_parse_hw_addr_full_one("12-34-56-78-90-AB-CD-EF", SIZE_MAX, "12:34:56:78:90:ab:cd:ef");
143
144 /* Invalid */
145 test_parse_hw_addr_full_one("", SIZE_MAX, NULL);
146 test_parse_hw_addr_full_one("12", SIZE_MAX, NULL);
147 test_parse_hw_addr_full_one("12.", SIZE_MAX, NULL);
148 test_parse_hw_addr_full_one("12.34.", SIZE_MAX, NULL);
149 test_parse_hw_addr_full_one(".12", SIZE_MAX, NULL);
150 test_parse_hw_addr_full_one(".12.34", SIZE_MAX, NULL);
151 test_parse_hw_addr_full_one("12.34:56", SIZE_MAX, NULL);
152 test_parse_hw_addr_full_one("1234:56", SIZE_MAX, NULL);
153 test_parse_hw_addr_full_one("1234:56", SIZE_MAX, NULL);
154 test_parse_hw_addr_full_one("12:34:", SIZE_MAX, NULL);
155 test_parse_hw_addr_full_one(":12:34", SIZE_MAX, NULL);
156 test_parse_hw_addr_full_one("::1", SIZE_MAX, NULL);
157 test_parse_hw_addr_full_one("aa:bb-cc", SIZE_MAX, NULL);
158 test_parse_hw_addr_full_one("aa:xx", SIZE_MAX, NULL);
159 test_parse_hw_addr_full_one("aa bb", SIZE_MAX, NULL);
160 }
161
162 DEFINE_TEST_MAIN(LOG_INFO);