1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sys/cdefs.h>
18
19 #include <gtest/gtest.h>
20
21 #include <arpa/inet.h>
22
TEST(arpa_inet,inet_addr)23 TEST(arpa_inet, inet_addr) {
24 ASSERT_EQ((htonl)(0x7f000001), inet_addr("127.0.0.1"));
25 }
26
TEST(arpa_inet,inet_aton)27 TEST(arpa_inet, inet_aton) {
28 in_addr a;
29
30 // a.b.c.d
31 a.s_addr = 0;
32 ASSERT_EQ(1, inet_aton("127.1.2.3", &a));
33 ASSERT_EQ((htonl)(0x7f010203), a.s_addr);
34
35 // a.b.c
36 a.s_addr = 0;
37 ASSERT_EQ(1, inet_aton("127.1.2", &a));
38 ASSERT_EQ((htonl)(0x7f010002), a.s_addr);
39
40 // a.b
41 a.s_addr = 0;
42 ASSERT_EQ(1, inet_aton("127.1", &a));
43 ASSERT_EQ((htonl)(0x7f000001), a.s_addr);
44
45 // a
46 a.s_addr = 0;
47 ASSERT_EQ(1, inet_aton("0x7f000001", &a));
48 ASSERT_EQ((htonl)(0x7f000001), a.s_addr);
49
50 // Hex (0x) and mixed-case hex digits.
51 a.s_addr = 0;
52 ASSERT_EQ(1, inet_aton("0xFf.0.0.1", &a));
53 ASSERT_EQ((htonl)(0xff000001), a.s_addr);
54
55 // Hex (0X) and mixed-case hex digits.
56 a.s_addr = 0;
57 ASSERT_EQ(1, inet_aton("0XfF.0.0.1", &a));
58 ASSERT_EQ((htonl)(0xff000001), a.s_addr);
59
60 // Octal.
61 a.s_addr = 0;
62 ASSERT_EQ(1, inet_aton("0177.0.0.1", &a));
63 ASSERT_EQ((htonl)(0x7f000001), a.s_addr);
64
65 a.s_addr = 0;
66 ASSERT_EQ(1, inet_aton("036", &a));
67 ASSERT_EQ((htonl)(036U), a.s_addr);
68 }
69
TEST(arpa_inet,inet_aton_nullptr)70 TEST(arpa_inet, inet_aton_nullptr) {
71 ASSERT_EQ(0, inet_aton("", nullptr));
72 ASSERT_EQ(1, inet_aton("127.0.0.1", nullptr));
73 }
74
TEST(arpa_inet,inet_aton_invalid)75 TEST(arpa_inet, inet_aton_invalid) {
76 ASSERT_EQ(0, inet_aton("", nullptr)); // Empty.
77 ASSERT_EQ(0, inet_aton("x", nullptr)); // Leading junk.
78 ASSERT_EQ(0, inet_aton("127.0.0.1x", nullptr)); // Trailing junk.
79 ASSERT_EQ(0, inet_aton("09.0.0.1", nullptr)); // Invalid octal.
80 ASSERT_EQ(0, inet_aton("0xg.0.0.1", nullptr)); // Invalid hex.
81
82 ASSERT_EQ(0, inet_aton("1.2.3.4.5", nullptr)); // Too many dots.
83 ASSERT_EQ(0, inet_aton("1.2.3.4.", nullptr)); // Trailing dot.
84
85 // Out of range a.b.c.d form.
86 ASSERT_EQ(0, inet_aton("999.0.0.1", nullptr));
87 ASSERT_EQ(0, inet_aton("0.999.0.1", nullptr));
88 ASSERT_EQ(0, inet_aton("0.0.999.1", nullptr));
89 ASSERT_EQ(0, inet_aton("0.0.0.999", nullptr));
90
91 // Out of range a.b.c form.
92 ASSERT_EQ(0, inet_aton("256.0.0", nullptr));
93 ASSERT_EQ(0, inet_aton("0.256.0", nullptr));
94 ASSERT_EQ(0, inet_aton("0.0.0x10000", nullptr));
95
96 // Out of range a.b form.
97 ASSERT_EQ(0, inet_aton("256.0", nullptr));
98 ASSERT_EQ(0, inet_aton("0.0x1000000", nullptr));
99
100 // Out of range a form.
101 ASSERT_EQ(0, inet_aton("0x100000000", nullptr));
102
103 // 64-bit overflow.
104 ASSERT_EQ(0, inet_aton("0x10000000000000000", nullptr));
105
106 // Out of range octal.
107 ASSERT_EQ(0, inet_aton("0400.0.0.1", nullptr));
108 }
109
TEST(arpa_inet,inet_lnaof)110 TEST(arpa_inet, inet_lnaof) {
111 in_addr a = { htonl(0x12345678) };
112 ASSERT_EQ(0x00345678U, inet_lnaof(a));
113 }
114
TEST(arpa_inet,inet_makeaddr)115 TEST(arpa_inet, inet_makeaddr) {
116 in_addr a = inet_makeaddr(0x12U, 0x345678);
117 ASSERT_EQ((htonl)(0x12345678), a.s_addr);
118 }
119
TEST(arpa_inet,inet_netof)120 TEST(arpa_inet, inet_netof) {
121 in_addr a = { htonl(0x12345678) };
122 ASSERT_EQ(0x12U, inet_netof(a));
123 }
124
TEST(arpa_inet,inet_network)125 TEST(arpa_inet, inet_network) {
126 ASSERT_EQ(0x7f000001U, inet_network("127.0.0.1"));
127 ASSERT_EQ(0x7fU, inet_network("0x7f"));
128 ASSERT_EQ(~0U, inet_network(""));
129 }
130
TEST(arpa_inet,inet_ntoa)131 TEST(arpa_inet, inet_ntoa) {
132 in_addr a = { (htonl)(0x7f000001) };
133 ASSERT_STREQ("127.0.0.1", inet_ntoa(a));
134 }
135
TEST(arpa_inet,inet_pton__inet_ntop)136 TEST(arpa_inet, inet_pton__inet_ntop) {
137 sockaddr_storage ss;
138 ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss));
139
140 char s[INET_ADDRSTRLEN];
141 ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss, s, INET_ADDRSTRLEN));
142 }
143
TEST(arpa_inet,inet_ntop_overflow)144 TEST(arpa_inet, inet_ntop_overflow) {
145 // OpenBSD's inet_ntop had a bug where passing a 'size' larger than INET_ADDRSTRLEN
146 // for AF_INET or INET6_ADDRSTRLEN for AF_INET6 would cause inet_ntop to overflow an
147 // internal buffer.
148
149 sockaddr_storage ss4;
150 ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &ss4));
151
152 sockaddr_storage ss6;
153 ASSERT_EQ(1, inet_pton(AF_INET6, "::1", &ss6));
154
155 char s4[INET_ADDRSTRLEN];
156 char s6[INET6_ADDRSTRLEN];
157 ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss4, s4, INET_ADDRSTRLEN));
158 ASSERT_STREQ("127.0.0.1", inet_ntop(AF_INET, &ss4, s4, 2*INET_ADDRSTRLEN));
159 ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, INET_ADDRSTRLEN));
160 ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, INET6_ADDRSTRLEN));
161 ASSERT_STREQ("::1", inet_ntop(AF_INET6, &ss6, s6, 2*INET6_ADDRSTRLEN));
162 }
163
TEST(arpa_inet,inet_nsap_addr)164 TEST(arpa_inet, inet_nsap_addr) {
165 #if !defined(ANDROID_HOST_MUSL)
166 // inet_nsap_addr() doesn't seem to be documented anywhere, but it's basically
167 // text to binary for arbitrarily-long strings like "0xdeadbeef". Any
168 // '.', '+', or '/' characters are ignored as punctuation. The return value is
169 // the length in bytes, or 0 for all errors.
170 u_char buf[32];
171
172 // Missing "0x" prefix.
173 ASSERT_EQ(0U, inet_nsap_addr("123", buf, sizeof(buf)));
174 ASSERT_EQ(0U, inet_nsap_addr("012", buf, sizeof(buf)));
175
176 // 1 byte.
177 ASSERT_EQ(1U, inet_nsap_addr("0x12", buf, sizeof(buf)));
178 ASSERT_EQ(0x12, buf[0]);
179
180 // 10 bytes.
181 ASSERT_EQ(10U, inet_nsap_addr("0x1234567890abcdef0011", buf, sizeof(buf)));
182 ASSERT_EQ(0x12, buf[0]);
183 ASSERT_EQ(0x34, buf[1]);
184 ASSERT_EQ(0x56, buf[2]);
185 ASSERT_EQ(0x78, buf[3]);
186 ASSERT_EQ(0x90, buf[4]);
187 ASSERT_EQ(0xab, buf[5]);
188 ASSERT_EQ(0xcd, buf[6]);
189 ASSERT_EQ(0xef, buf[7]);
190 ASSERT_EQ(0x00, buf[8]);
191 ASSERT_EQ(0x11, buf[9]);
192
193 // Ignored punctuation.
194 ASSERT_EQ(10U, inet_nsap_addr("0x1122.3344+5566/7788/99aa", buf, sizeof(buf)));
195 ASSERT_EQ(0x11, buf[0]);
196 ASSERT_EQ(0x22, buf[1]);
197 ASSERT_EQ(0x33, buf[2]);
198 ASSERT_EQ(0x44, buf[3]);
199 ASSERT_EQ(0x55, buf[4]);
200 ASSERT_EQ(0x66, buf[5]);
201 ASSERT_EQ(0x77, buf[6]);
202 ASSERT_EQ(0x88, buf[7]);
203 ASSERT_EQ(0x99, buf[8]);
204 ASSERT_EQ(0xaa, buf[9]);
205
206 // Truncated.
207 ASSERT_EQ(4U, inet_nsap_addr("0xdeadbeef666666666666", buf, 4));
208 // Overwritten...
209 ASSERT_EQ(0xde, buf[0]);
210 ASSERT_EQ(0xad, buf[1]);
211 ASSERT_EQ(0xbe, buf[2]);
212 ASSERT_EQ(0xef, buf[3]);
213 // Same as before...
214 ASSERT_EQ(0x55, buf[4]);
215 ASSERT_EQ(0x66, buf[5]);
216 ASSERT_EQ(0x77, buf[6]);
217 ASSERT_EQ(0x88, buf[7]);
218 ASSERT_EQ(0x99, buf[8]);
219 ASSERT_EQ(0xaa, buf[9]);
220
221 // Case insensitivity.
222 ASSERT_EQ(6U, inet_nsap_addr("0xaAbBcCdDeEfF", buf, 6));
223 ASSERT_EQ(0xaa, buf[0]);
224 ASSERT_EQ(0xbb, buf[1]);
225 ASSERT_EQ(0xcc, buf[2]);
226 ASSERT_EQ(0xdd, buf[3]);
227 ASSERT_EQ(0xee, buf[4]);
228 ASSERT_EQ(0xff, buf[5]);
229
230 // Punctuation isn't allowed within a byte.
231 ASSERT_EQ(0U, inet_nsap_addr("0x1.122", buf, sizeof(buf)));
232 // Invalid punctuation.
233 ASSERT_EQ(0U, inet_nsap_addr("0x11,22", buf, sizeof(buf)));
234 // Invalid hex digit.
235 ASSERT_EQ(0U, inet_nsap_addr("0x11.g2", buf, sizeof(buf)));
236 ASSERT_EQ(0U, inet_nsap_addr("0x11.2g", buf, sizeof(buf)));
237 // Invalid half-byte.
238 ASSERT_EQ(0U, inet_nsap_addr("0x11.2", buf, sizeof(buf)));
239 #else
240 GTEST_SKIP() << "musl doesn't have inet_nsap_addr";
241 #endif
242 }
243
TEST(arpa_inet,inet_nsap_ntoa)244 TEST(arpa_inet, inet_nsap_ntoa) {
245 #if !defined(ANDROID_HOST_MUSL)
246 // inet_nsap_ntoa() doesn't seem to be documented anywhere, but it's basically
247 // binary to text for arbitrarily-long byte buffers.
248 // The return value is a pointer to the buffer. No errors are possible.
249 const unsigned char bytes[] = {0x01, 0x00, 0x02, 0x0e, 0xf0, 0x20};
250 char dst[32];
251 ASSERT_EQ(dst, inet_nsap_ntoa(6, bytes, dst));
252 ASSERT_STREQ(dst, "0x01.0002.0EF0.20");
253 #else
254 GTEST_SKIP() << "musl doesn't have inet_nsap_ntoa";
255 #endif
256 }
257
TEST(arpa_inet,inet_nsap_ntoa__nullptr)258 TEST(arpa_inet, inet_nsap_ntoa__nullptr) {
259 #if !defined(ANDROID_HOST_MUSL)
260 // If you don't provide a destination, a static buffer is provided for you.
261 const unsigned char bytes[] = {0x01, 0x00, 0x02, 0x0e, 0xf0, 0x20};
262 ASSERT_STREQ("0x01.0002.0EF0.20", inet_nsap_ntoa(6, bytes, nullptr));
263 #else
264 GTEST_SKIP() << "musl doesn't have inet_nsap_ntoa";
265 #endif
266 }
267