inet_pton tests, fix regression test case
[libc-test] / src / functional / inet_pton.c
index d220118..b31faed 100644 (file)
@@ -1,4 +1,5 @@
-// inet_pton and inet_ntop tests with roundtrip check
+// inet_addr, inet_ntoa, inet_pton and inet_ntop tests with roundtrip check
+#include <errno.h>
 #include <string.h>
 #include <stdio.h>
 #include <arpa/inet.h>
@@ -11,78 +12,149 @@ static int digit(int c)
        return c;
 }
 
-static void tobin(char *d, char *s)
+static void tobin(void *d, char *s)
 {
        int i;
-       for (i=0; s[2*i]; i++) d[i] = digit(s[2*i])*16+digit(s[2*i+1]);
+       unsigned char *p = d;
+       for (i=0; s[2*i]; i++) p[i] = digit(s[2*i])*16+digit(s[2*i+1]);
 }
 
-static void tohex(char *d, char *s, int n)
+static void tohex(char *d, void *s, int n)
 {
        int i;
-       for (i=0; i<n; i++) sprintf(d+2*i, "%02x", (unsigned char)s[i]);
+       unsigned char *p = s;
+       for (i=0; i<n; i++) sprintf(d+2*i, "%02x", p[i]);
 }
 
-#define T(af,src,ret,hex) do{\
+#define V6(src,ret,hex) do{\
        int r; \
-       char binaddr[16]; \
-       char hexaddr[40]; \
-       char txtaddr[60]; \
-       r=inet_pton(af,src,binaddr); \
+       char binaddr[16]={0}; \
+       char hexaddr[40]={0}; \
+       char txtaddr[60]={0}; \
+       \
+       r=inet_pton(AF_INET6,src,binaddr); \
        if (r!=ret) \
-               t_error("inet_pton("#af", "#src", addr) returned %d, want %d\n", r, ret); \
+               t_error("inet_pton(AF_INET6, "#src", addr) returned %d, want %d\n", r, ret); \
        if (ret!=1) break; \
-       tohex(hexaddr,binaddr,af==AF_INET?4:16); \
+       tohex(hexaddr,binaddr,16); \
        if (strcmp(hexaddr,hex)) \
-               t_error("inet_pton("#af", "#src", addr) got addr %s, want %s\n", hexaddr, hex); \
+               t_error("inet_pton(AF_INET6, "#src", addr) got addr %s, want %s\n", hexaddr, hex); \
+       \
        tobin(binaddr,hex); \
-       if (inet_ntop(af,binaddr,txtaddr,sizeof txtaddr)!=txtaddr) \
-               t_error("inet_ntop("#af", <"#hex">, buf, size) did not return buf\n"); \
-       if (af==AF_INET && strcmp(txtaddr,src)) \
-               t_error("inet_ntop("#af", <"#hex">, buf, size) got %s, want %s\n", txtaddr, src); \
-       if (af!=AF_INET6) break; \
-       if (inet_pton(af,txtaddr,binaddr)!=1) \
-               t_error("inet_ntop("#af", <"#hex">, buf, size) got %s, it is rejected by inet_pton\n", txtaddr); \
+       if (inet_ntop(AF_INET6,binaddr,txtaddr,sizeof txtaddr)!=txtaddr) \
+               t_error("inet_ntop(AF_INET6, <"#hex">, buf, size) did not return buf\n"); \
+       if (inet_pton(AF_INET6,txtaddr,binaddr)!=1) \
+               t_error("inet_ntop(AF_INET6, <"#hex">, buf, size) got %s, it is rejected by inet_pton\n", txtaddr); \
        tohex(hexaddr,binaddr,16); \
        if (strcmp(hexaddr,hex)) \
-               t_error("inet_ntop("#af", <"#hex">, buf, size) got %s that is %s, want %s\n", txtaddr, hexaddr, hex); \
+               t_error("inet_ntop(AF_INET6, <"#hex">, buf, size) got %s that is %s, want %s\n", txtaddr, hexaddr, hex); \
        if (strncmp(hex,"00000000000000000000ffff",24)==0 && !strchr(txtaddr,'.')) \
-               t_error("inet_ntop("#af", <"#hex">, buf, size) got %s, should be ipv4 mapped\n", txtaddr); \
+               t_error("inet_ntop(AF_INET6, <"#hex">, buf, size) got %s, should be ipv4 mapped\n", txtaddr); \
+}while(0);
+
+// ret and hex are the results of inet_pton and inet_addr respectively
+#define V4(src,ret,hex) do{\
+       int r; \
+       uint32_t a; \
+       struct in_addr in; \
+       char buf[20]={0}; \
+       char *p; \
+       \
+       a=inet_addr(src); \
+       tohex(buf,&a,4); \
+       if (strcmp(buf,hex)) \
+               t_error("inet_addr("#src") returned %s, want %s\n", buf, hex); \
+       \
+       r=inet_pton(AF_INET,src,&a); \
+       if (r!=ret) \
+               t_error("inet_pton(AF_INET, "#src", addr) returned %d, want %d\n", r, ret); \
+       \
+       if (ret!=1) break; \
+       \
+       tohex(buf,&a,4); \
+       if (strcmp(buf,hex)) \
+               t_error("inet_pton(AF_INET, "#src", addr) got addr %s, want %s\n", buf, hex); \
+       \
+       tobin(&a,hex); \
+       if (inet_ntop(AF_INET,&a,buf,sizeof buf)!=buf) \
+               t_error("inet_ntop(AF_INET, <"#hex">, buf, size) did not return buf\n"); \
+       if (strcmp(buf,src)) \
+               t_error("inet_ntop(AF_INET, <"#hex">, buf, size) got %s, want %s\n", buf, src); \
+       \
+       in.s_addr = a; \
+       p=inet_ntoa(in); \
+       if (strcmp(p,src)) \
+               t_error("inet_ntoa(<"#hex">) returned %s, want %s\n", p, src); \
 }while(0);
 
 int main(void)
 {
-T(AF_INET+AF_INET6, "0.0.0.0", -1, "")
-T(AF_INET, "0.0.0.0", 1, "00000000")
-T(AF_INET, "127.0.0.1", 1, "7f000001")
-T(AF_INET, "255.255.255.255", 1, "ffffffff")
-T(AF_INET, "1.2.3.", 0, "")
-T(AF_INET, "1.2.3.4.5", 0, "")
-T(AF_INET, ".1.2.3", 0, "")
-T(AF_INET, "1.2.03.4", 0, "")
-T(AF_INET, "1.2.3.a", 0, "")
-T(AF_INET, "1.256.2.3", 0, "")
-T(AF_INET, "1.2.4294967296.3", 0, "")
-T(AF_INET, "1.2.-4294967295.3", 0, "")
-T(AF_INET, "1.2. 3.4", 0, "")
-T(AF_INET6, ":", 0, "")
-T(AF_INET6, "::", 1, "00000000000000000000000000000000")
-T(AF_INET6, "::1", 1, "00000000000000000000000000000001")
-T(AF_INET6, ":::", 0, "")
-T(AF_INET6, "192.168.1.1", 0, "")
-T(AF_INET6, ":192.168.1.1", 0, "")
-T(AF_INET6, "::192.168.1.1", 1, "000000000000000000000000c0a80101")
-T(AF_INET6, ":ffff:192.168.1.1", 0, "")
-T(AF_INET6, "::ffff:192.168.1.1", 1, "00000000000000000000ffffc0a80101")
-T(AF_INET6, ".192.168.1.1", 0, "")
-T(AF_INET6, ":.192.168.1.1", 0, "")
-T(AF_INET6, "a:0b:00c:000d:0000e:f::", 1, "000a000b000c000d000e000f00000000")
-T(AF_INET6, "ffff:c0a8:5e4", 0, "")
-T(AF_INET6, ":ffff:c0a8:5e4", 0, "")
-T(AF_INET6, "0:0:0:0:0:ffff:c0a8:5e4", 1, "00000000000000000000ffffc0a805e4")
-T(AF_INET6, "0:0:0:0:ffff:c0a8:5e4", 0, "")
-T(AF_INET6, "0::ffff:c0a8:5e4", 1, "00000000000000000000ffffc0a805e4")
-T(AF_INET6, "::0::ffff:c0a8:5e4", 0, "")
-T(AF_INET6, "c0a8", 0, "")
+
+// errors
+if (inet_pton(12345, "", 0) != -1 || errno != EAFNOSUPPORT)
+       t_error("inet_pton(12345,,) should fail with EAFNOSUPPORT, got %s\n", strerror(errno));
+errno=0;
+if (inet_ntop(AF_INET,"xxxx","",0) != 0 || errno != ENOSPC)
+       t_error("inet_ntop(,,0,0) should fail with ENOSPC, got %s\n", strerror(errno));
+errno=0;
+
+// dotted-decimal notation
+V4("0.0.0.0", 1, "00000000")
+V4("127.0.0.1", 1, "7f000001")
+V4("10.0.128.31", 1, "0a00801f")
+V4("255.255.255.255", 1, "ffffffff")
+
+// numbers-and-dots notation, but not dotted-decimal
+V4("1.2.03.4", 0, "01020304")
+V4("1.2.0x33.4", 0, "01023304")
+V4("1.2.0XAB.4", 0, "0102ab04")
+V4("1.2.0xabcd", 0, "0102abcd")
+V4("1.0xabcdef", 0, "01abcdef")
+V4("00377.0x0ff.65534", 0, "fffffffe")
+
+// invalid
+V4(".1.2.3", 0, "ffffffff")
+V4("1..2.3", 0, "ffffffff")
+V4("1.2.3.", 0, "ffffffff")
+V4("1.2.3.4.5", 0, "ffffffff")
+V4("1.2.3.a", 0, "ffffffff")
+V4("1.256.2.3", 0, "ffffffff")
+V4("1.2.4294967296.3", 0, "ffffffff")
+V4("1.2.-4294967295.3", 0, "ffffffff")
+V4("1.2. 3.4", 0, "ffffffff")
+
+// ipv6
+V6(":", 0, "")
+V6("::", 1, "00000000000000000000000000000000")
+V6("::1", 1, "00000000000000000000000000000001")
+V6(":::", 0, "")
+V6("192.168.1.1", 0, "")
+V6(":192.168.1.1", 0, "")
+V6("::192.168.1.1", 1, "000000000000000000000000c0a80101")
+V6("0:0:0:0:0:0:192.168.1.1", 1, "000000000000000000000000c0a80101")
+V6("0:0::0:0:0:192.168.1.1", 1, "000000000000000000000000c0a80101")
+V6("::012.34.56.78", 0, "")
+V6(":ffff:192.168.1.1", 0, "")
+V6("::ffff:192.168.1.1", 1, "00000000000000000000ffffc0a80101")
+V6(".192.168.1.1", 0, "")
+V6(":.192.168.1.1", 0, "")
+V6("a:0b:00c:000d:E:F::", 1, "000a000b000c000d000e000f00000000")
+V6("a:0b:00c:000d:0000e:f::", 0, "")
+V6("1:2:3:4:5:6::", 1, "00010002000300040005000600000000")
+V6("1:2:3:4:5:6:7::", 1, "00010002000300040005000600070000")
+V6("1:2:3:4:5:6:7:8::", 0, "")
+V6("1:2:3:4:5:6:7::9", 0, "")
+V6("::1:2:3:4:5:6", 1, "00000000000100020003000400050006")
+V6("::1:2:3:4:5:6:7", 1, "00000001000200030004000500060007")
+V6("::1:2:3:4:5:6:7:8", 0, "")
+V6("a:b::c:d:e:f", 1, "000a000b00000000000c000d000e000f")
+V6("ffff:c0a8:5e4", 0, "")
+V6(":ffff:c0a8:5e4", 0, "")
+V6("0:0:0:0:0:ffff:c0a8:5e4", 1, "00000000000000000000ffffc0a805e4")
+V6("0:0:0:0:ffff:c0a8:5e4", 0, "")
+V6("0::ffff:c0a8:5e4", 1, "00000000000000000000ffffc0a805e4")
+V6("::0::ffff:c0a8:5e4", 0, "")
+V6("c0a8", 0, "")
+
 return t_status;
 }