1// zero compression for the last field in an ipv6 address is (probably) allowed
2// https://tools.ietf.org/html/rfc4291#section-2.2
3// but further fields shouldnt buffer overflow
4#include <stdio.h>
5#include <string.h>
6#include <arpa/inet.h>
7#include "test.h"
8
9static void txt(char *s, unsigned char *buf)
10{
11	int i;
12	sprintf(s, "%04x", buf[0]<<8 | buf[1]);
13	for (i=1; i<8; i++)
14		sprintf(s+5*i, ":%04x", buf[2*i]<<8 | buf[2*i+1]);
15}
16
17int main(void)
18{
19	char s[50], sw[50];
20	unsigned char buf[16];
21	unsigned char want[16] = {0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,0};
22	char *addr;
23
24	addr = "1:2:3:4:5:6:7::";
25	if (inet_pton(AF_INET6, addr, buf)!=1 || memcmp(buf, want, 16)!=0) {
26		txt(s, buf);
27		txt(sw, want);
28		t_error("inet_pton(%s) returned %s, wanted %s\n",
29			addr, s, sw);
30	}
31
32	addr = "1:2:3:4:5:6:7::9:10:11:12:13:14:15:16:17:18:19:20";
33	if (inet_pton(AF_INET6, addr, buf)!=0) {
34		txt(s, buf);
35		t_error("inet_pton(%s) returned %s, wanted a failure\n",
36			addr, s);
37	}
38	return t_status;
39}
40