/* Check if inet_ntop is according to RFC 5952 */ #include #include #include #include #include #include #include /* Section 4.1: Handling Leading Zeros in a 16-Bit Field */ struct in6_addr s41_input = { { 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } }; char *s41_str = "2001:db8::1"; /* Section 4.2.1: Shorten as Much as Possible */ struct in6_addr s421_input = { { 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01 } }; char *s421_str = "2001:db8::2:1"; /* Section 4.2.2: Handling One 16-Bit 0 Field */ struct in6_addr s422_input = { { 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01 } }; char *s422_str = "2001:db8:0:1:1:1:1:1"; /* Section 4.2.3a: Choice in Placement of "::" */ struct in6_addr s423a_input = { { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } }; char *s423a_str = "2001:0:0:1::1"; /* Section 4.2.3b: Choice in Placement of "::" */ struct in6_addr s423b_input = { { 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } }; char *s423b_str = "2001:db8::1:0:0:1"; struct tests { char *label; struct in6_addr *addrp; char **strp; } tests[] = { { "Section 4.1", &s41_input, &s41_str }, { "Section 4.2.1", &s421_input, &s421_str }, { "Section 4.2.2", &s422_input, &s422_str }, { "Section 4.2.3a", &s423a_input, &s423a_str }, { "Section 4.2.3b", &s423b_input, &s423b_str }, { NULL, NULL, NULL } }; int main(void) { int i, errors; char buf[INET6_ADDRSTRLEN]; errors= 0; for (i= 0; tests[i].label != NULL; i++) { if (inet_ntop(AF_INET6, tests[i].addrp, buf, sizeof(buf)) == NULL) { fprintf(stderr, "inet_ntop failed for test %s: %s\n", tests[i].label, strerror(errno)); exit(1); } if (strcmp(buf, *tests[i].strp) != 0) { fprintf(stderr, "%s test failed: got %s, expected %s\n", tests[i].label, buf, *tests[i].strp); errors++; } } if (errors) { fprintf(stderr, "Found %d error%s.\n", errors, errors == 1 ? "" : "s"); exit(1); } exit(0); }