]>
git.saurik.com Git - apple/libc.git/blob - tests/net.c
758ee488aa553dec81705bb93ea8d7a106853d71
2 #include <sys/socket.h>
4 #include <netinet/in.h>
8 #include <darwintest.h>
10 T_DECL(link_ntoa_basic
, "link_ntoa converts to proper string")
12 // u_char sdl_len; /* Total length of sockaddr */
13 // u_char sdl_family; /* AF_LINK */
14 // u_short sdl_index; /* if != 0, system given index for interface */
15 // u_char sdl_type; /* interface type */
16 // u_char sdl_nlen; /* interface name length, no trailing 0 reqd. */
17 // u_char sdl_alen; /* link level address length */
18 // u_char sdl_slen; /* link layer selector length */
19 // char sdl_data[12]; /* minimum work area, can be larger;
20 // contains both if name and ll address */
22 struct sockaddr_dl sad
;
23 bzero(&sad
, sizeof(sad
));
26 sad
.sdl_data
[0] = 'l';
27 sad
.sdl_data
[1] = 'e';
28 sad
.sdl_data
[2] = '0';
29 sad
.sdl_data
[3] = 0x01;
30 sad
.sdl_data
[4] = 0x80;
31 sad
.sdl_data
[5] = 0xc2;
32 sad
.sdl_data
[6] = 0x00;
33 sad
.sdl_data
[7] = 0x00;
34 sad
.sdl_data
[8] = 0x02;
35 sad
.sdl_data
[9] = 0xaa;
36 sad
.sdl_data
[10] = 0xbb;
37 sad
.sdl_data
[11] = 0xcc;
40 char *foo
= link_ntoa(&sad
);
42 T_EXPECT_EQ_STR("le0:1.80.c2.0.0.2", foo
, NULL
);
45 T_DECL(link_ntoa_overflow
, "link_ntoa try to overflow")
48 struct sockaddr_dl
*sad
;
49 sad
= (struct sockaddr_dl
*)&sockraw
;
50 bzero(sad
, sizeof(*sad
));
53 sad
->sdl_data
[0] = 'l';
54 sad
->sdl_data
[1] = 'e';
55 sad
->sdl_data
[2] = '0';
56 sad
->sdl_data
[3] = 0x11;
57 sad
->sdl_data
[4] = 0x80;
58 sad
->sdl_data
[5] = 0xc2;
59 sad
->sdl_data
[6] = 0x11;
60 sad
->sdl_data
[7] = 0x11;
61 sad
->sdl_data
[8] = 0xa2;
62 sad
->sdl_data
[9] = 0xaa;
63 sad
->sdl_data
[10] = 0xbb;
64 sad
->sdl_data
[11] = 0xcc;
78 /* set the length to something that will fit in the buffer */
81 char *foo
= link_ntoa(sad
);
86 /* this string should be 66 bytes long and exceed the buffer */
91 T_EXPECT_EQ_STR("", foo
, NULL
);
93 T_EXPECT_EQ(over
, foo
[64], "did not overflow");
94 T_EXPECT_EQ(over2
, foo
[65], "did not overflow either");
97 T_DECL(inet_ntop
, "inet_ntop")
99 char *addresses4
[] = { "1.2.3.4", "10.0.0.1", "2.2.2.2" };
100 char *addresses6
[] = { "2001:db8:85a3::8a2e:370:7334", "::1", "::" };
101 for (int i
= 0; i
< sizeof(addresses4
)/sizeof(addresses4
[0]); i
++){
102 struct in_addr addr4
;
104 T_EXPECT_EQ(inet_pton(AF_INET
, addresses4
[i
], &addr4
), 1, "inet_pton(AF_INET, %s)", addresses4
[i
]);
105 char *str
= inet_ntop(AF_INET
, &addr4
, buf
, sizeof(buf
));
106 T_EXPECT_NOTNULL(str
, "inet_ntop(AF_INET) of %s", addresses4
[i
]);
107 T_EXPECT_EQ_STR(str
, addresses4
[i
], "round-trip of %s", addresses4
[i
]);
109 for (int i
= 0; i
< sizeof(addresses6
)/sizeof(addresses6
[0]); i
++){
110 struct in6_addr addr6
;
112 T_EXPECT_EQ(inet_pton(AF_INET6
, addresses6
[i
], &addr6
), 1, "inet_pton(AF_INET6, %s)", addresses6
[i
]);
113 char *str
= inet_ntop(AF_INET6
, &addr6
, buf
, sizeof(buf
));
114 T_EXPECT_NOTNULL(str
, "inet_ntop(AF_INET6) of %s", addresses6
[i
]);
115 T_EXPECT_EQ_STR(str
, addresses6
[i
], "round-trip of %s", addresses6
[i
]);
121 const char *expected_out_addr
;
124 static const struct testcase test_addrs
[] = {
125 { "1:2:3:4:5::1.2.3.4", "1:2:3:4:5:0:102:304" },
126 { "1:0:3:0:5:0:7:8", "1:0:3:0:5:0:7:8" },
127 { "0:0:3:0:0:0:7:8", "0:0:3::7:8" },
128 { "0:0:3:0:5:0:7:8", "::3:0:5:0:7:8" },
129 { "0:0:0:0:0:0:0:0", "::" },
130 { "0:0:0:0:0:1:0:0", "::1:0:0" },
131 { "1:0:0:0:0:0:0:0", "1::" },
132 { "0:0:0:1:0:0:0:0", "0:0:0:1::" },
133 { "1:0:0:0:0:0:0:1", "1::1" },
134 { "1:2:3:4:5:6:0:0", "1:2:3:4:5:6::" },
135 { "1:2:3:4:5:0:0:0", "1:2:3:4:5::" },
138 T_DECL(inet_ntop_resolve_zeroes
, "Check for proper behavior when shortening zeroes w/ inet_ntop")
140 // Take ip addrs as text, convert to binary and back.
141 // Upon converting back, they should adhere to the IPv6 guidelines.
142 for (int i
= 0; i
< sizeof(test_addrs
)/sizeof(struct testcase
); ++i
) {
143 struct in6_addr addr6
;
145 char *in_addr
= test_addrs
[i
].in_addr
;
146 char *expected_out_addr
= test_addrs
[i
].expected_out_addr
;
147 T_EXPECT_EQ(inet_pton(AF_INET6
, in_addr
, &addr6
), 1, "inet_pton(AF_INET6, %s)", in_addr
);
148 char *str
= inet_ntop(AF_INET6
, &addr6
, buf
, sizeof(buf
));
149 T_EXPECT_NOTNULL(str
, "inet_ntop(AF_INET6) of %s", in_addr
);
150 // <rdar://problem/32825795> Single-zero tests will fail until change
155 T_EXPECT_EQ_STR(str
, expected_out_addr
, NULL
);
158 // Same test, but step through the possible range of ipv6 values.
159 for (int i
= 0x0; i
< 0x10000; ++i
) {
160 struct in6_addr addr6
;
163 sprintf(in_addr
, "1:1:1:1:1:1:1:%x", i
);
164 char *expected_out_addr
= in_addr
;
166 T_EXPECT_EQ(inet_pton(AF_INET6
, in_addr
, &addr6
), 1, "inet_pton(AF_INET6, %s)", in_addr
);
167 char *str
= inet_ntop(AF_INET6
, &addr6
, buf
, sizeof(buf
));
169 T_EXPECT_NOTNULL(str
, "inet_ntop(AF_INET6) of %s", in_addr
);
171 // <rdar://problem/32825795>
173 T_PASS("Never displayed"); // Cancel out the T_QUIET
176 T_EXPECT_EQ_STR(str
, expected_out_addr
, NULL
);
178 T_PASS("Passed ipv6 value testing");