Skip to content

Commit a31feaf

Browse files
authored
Merge pull request #801 from mzpqnxow/ipv6-tests
add suite of ipv6 formatting tests, related to #796
2 parents dfd2001 + 331b562 commit a31feaf

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,7 @@ int main(int argc, char *argv[])
18221822
x += ranges6_selftest();
18231823
x += dedup_selftest();
18241824
x += checksum_selftest();
1825+
x += ipv4address_selftest();
18251826
x += ipv6address_selftest();
18261827
x += proto_coap_selftest();
18271828
x += smack_selftest();

src/massip-addr.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,37 @@ ipv6address_t ipv6address_add(ipv6address_t lhs, ipv6address_t rhs) {
274274

275275

276276
int ipv6address_selftest(void)
277+
{
278+
struct test_pair {
279+
const char *name; // Human-readable IPv6 address string
280+
struct ipaddress ip_addr; // IP address (union)
281+
};
282+
/* Probably overkill, added while investigating issue #796 */
283+
struct test_pair tests[] = {
284+
{"2001:db8:ac10:fe01::2", {.ipv6 = {0x20010db8ac10fe01, 0x0000000000000002}, .version = 6}},
285+
{"2607:f8b0:4000::1", {.ipv6 = {0x2607f8b040000000, 0x0000000000000001}, .version = 6}},
286+
{"fd12:3456:7890:abcd:ef00::1", {.ipv6 = {0xfd1234567890abcd, 0xef00000000000001}, .version = 6}},
287+
{"::1", {.ipv6 = {0x0000000000000000, 0x0000000000000001}, .version = 6}},
288+
{"1::", {.ipv6 = {0x0001000000000000, 0x0000000000000000}, .version = 6}},
289+
{"1::2", {.ipv6 = {0x0001000000000000, 0x0000000000000002}, .version = 6}},
290+
{"2::1", {.ipv6 = {0x0002000000000000, 0x0000000000000001}, .version = 6}},
291+
{"1:2::", {.ipv6 = {0x0001000200000000, 0x0000000000000000}, .version = 6}},
292+
{NULL, {{0, 0}, 0}}
293+
};
294+
295+
int x = 0;
296+
ipaddress ip;
297+
struct ipaddress_formatted fmt;
298+
299+
for (int i = 0; tests[i].name != NULL; i++) {
300+
fmt = ipaddress_fmt(tests[i].ip_addr);
301+
if (strcmp(fmt.string, tests[i].name) != 0)
302+
x++;
303+
}
304+
return x;
305+
}
306+
307+
int ipv4address_selftest(void)
277308
{
278309
int x = 0;
279310
ipaddress ip;

src/massip-addr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,6 @@ unsigned massint128_bitcount(massint128_t num);
194194
* @return 0 on success, 1 on failure
195195
*/
196196
int ipv6address_selftest(void);
197+
int ipv4address_selftest(void);
197198

198199
#endif

0 commit comments

Comments
 (0)