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