1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #if defined(WEBRTC_POSIX)
12 #include <netinet/in.h> // for sockaddr_in
13 #endif
14
15 #include <string.h>
16
17 #include "rtc_base/ip_address.h"
18 #include "rtc_base/socket_address.h"
19 #include "test/gtest.h"
20
21 namespace rtc {
22
23 const in6_addr kTestV6Addr = {
24 {{0x20, 0x01, 0x0d, 0xb8, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
25 0x90, 0xA0, 0xB0, 0xC0}}};
26 const in6_addr kMappedV4Addr = {
27 {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
28 0x01, 0x02, 0x03, 0x04}}};
29 const std::string kTestV6AddrString = "2001:db8:1020:3040:5060:7080:90a0:b0c0";
30 const std::string kTestV6AddrAnonymizedString = "2001:db8:1020:x:x:x:x:x";
31 const std::string kTestV6AddrFullString =
32 "[2001:db8:1020:3040:5060:7080:90a0:b0c0]:5678";
33 const std::string kTestV6AddrFullAnonymizedString =
34 "[2001:db8:1020:x:x:x:x:x]:5678";
35
TEST(SocketAddressTest,TestDefaultCtor)36 TEST(SocketAddressTest, TestDefaultCtor) {
37 SocketAddress addr;
38 EXPECT_FALSE(addr.IsUnresolvedIP());
39 EXPECT_EQ(IPAddress(), addr.ipaddr());
40 EXPECT_EQ(0, addr.port());
41 EXPECT_EQ("", addr.hostname());
42 }
43
TEST(SocketAddressTest,TestIPPortCtor)44 TEST(SocketAddressTest, TestIPPortCtor) {
45 SocketAddress addr(IPAddress(0x01020304), 5678);
46 EXPECT_FALSE(addr.IsUnresolvedIP());
47 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
48 EXPECT_EQ(5678, addr.port());
49 EXPECT_EQ("", addr.hostname());
50 EXPECT_EQ("1.2.3.4:5678", addr.ToString());
51 }
52
TEST(SocketAddressTest,TestIPv4StringPortCtor)53 TEST(SocketAddressTest, TestIPv4StringPortCtor) {
54 SocketAddress addr("1.2.3.4", 5678);
55 EXPECT_FALSE(addr.IsUnresolvedIP());
56 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
57 EXPECT_EQ(5678, addr.port());
58 EXPECT_EQ("1.2.3.4", addr.hostname());
59 EXPECT_EQ("1.2.3.4:5678", addr.ToString());
60 }
61
TEST(SocketAddressTest,TestIPv6StringPortCtor)62 TEST(SocketAddressTest, TestIPv6StringPortCtor) {
63 SocketAddress addr2(kTestV6AddrString, 1234);
64 IPAddress tocheck(kTestV6Addr);
65
66 EXPECT_FALSE(addr2.IsUnresolvedIP());
67 EXPECT_EQ(tocheck, addr2.ipaddr());
68 EXPECT_EQ(1234, addr2.port());
69 EXPECT_EQ(kTestV6AddrString, addr2.hostname());
70 EXPECT_EQ("[" + kTestV6AddrString + "]:1234", addr2.ToString());
71 }
72
TEST(SocketAddressTest,TestSpecialStringPortCtor)73 TEST(SocketAddressTest, TestSpecialStringPortCtor) {
74 // inet_addr doesn't handle this address properly.
75 SocketAddress addr("255.255.255.255", 5678);
76 EXPECT_FALSE(addr.IsUnresolvedIP());
77 EXPECT_EQ(IPAddress(0xFFFFFFFFU), addr.ipaddr());
78 EXPECT_EQ(5678, addr.port());
79 EXPECT_EQ("255.255.255.255", addr.hostname());
80 EXPECT_EQ("255.255.255.255:5678", addr.ToString());
81 }
82
TEST(SocketAddressTest,TestHostnamePortCtor)83 TEST(SocketAddressTest, TestHostnamePortCtor) {
84 SocketAddress addr("a.b.com", 5678);
85 EXPECT_TRUE(addr.IsUnresolvedIP());
86 EXPECT_EQ(IPAddress(), addr.ipaddr());
87 EXPECT_EQ(5678, addr.port());
88 EXPECT_EQ("a.b.com", addr.hostname());
89 EXPECT_EQ("a.b.com:5678", addr.ToString());
90 }
91
TEST(SocketAddressTest,TestCopyCtor)92 TEST(SocketAddressTest, TestCopyCtor) {
93 SocketAddress from("1.2.3.4", 5678);
94 SocketAddress addr(from);
95 EXPECT_FALSE(addr.IsUnresolvedIP());
96 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
97 EXPECT_EQ(5678, addr.port());
98 EXPECT_EQ("1.2.3.4", addr.hostname());
99 EXPECT_EQ("1.2.3.4:5678", addr.ToString());
100 }
101
TEST(SocketAddressTest,TestAssign)102 TEST(SocketAddressTest, TestAssign) {
103 SocketAddress from("1.2.3.4", 5678);
104 SocketAddress addr(IPAddress(0x88888888), 9999);
105 addr = from;
106 EXPECT_FALSE(addr.IsUnresolvedIP());
107 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
108 EXPECT_EQ(5678, addr.port());
109 EXPECT_EQ("1.2.3.4", addr.hostname());
110 EXPECT_EQ("1.2.3.4:5678", addr.ToString());
111 }
112
TEST(SocketAddressTest,TestSetIPPort)113 TEST(SocketAddressTest, TestSetIPPort) {
114 SocketAddress addr(IPAddress(0x88888888), 9999);
115 addr.SetIP(IPAddress(0x01020304));
116 addr.SetPort(5678);
117 EXPECT_FALSE(addr.IsUnresolvedIP());
118 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
119 EXPECT_EQ(5678, addr.port());
120 EXPECT_EQ("", addr.hostname());
121 EXPECT_EQ("1.2.3.4:5678", addr.ToString());
122 }
123
TEST(SocketAddressTest,TestSetIPFromString)124 TEST(SocketAddressTest, TestSetIPFromString) {
125 SocketAddress addr(IPAddress(0x88888888), 9999);
126 addr.SetIP("1.2.3.4");
127 addr.SetPort(5678);
128 EXPECT_FALSE(addr.IsUnresolvedIP());
129 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
130 EXPECT_EQ(5678, addr.port());
131 EXPECT_EQ("1.2.3.4", addr.hostname());
132 EXPECT_EQ("1.2.3.4:5678", addr.ToString());
133 }
134
TEST(SocketAddressTest,TestSetIPFromHostname)135 TEST(SocketAddressTest, TestSetIPFromHostname) {
136 SocketAddress addr(IPAddress(0x88888888), 9999);
137 addr.SetIP("a.b.com");
138 addr.SetPort(5678);
139 EXPECT_TRUE(addr.IsUnresolvedIP());
140 EXPECT_EQ(IPAddress(), addr.ipaddr());
141 EXPECT_EQ(5678, addr.port());
142 EXPECT_EQ("a.b.com", addr.hostname());
143 EXPECT_EQ("a.b.com:5678", addr.ToString());
144 addr.SetResolvedIP(IPAddress(0x01020304));
145 EXPECT_FALSE(addr.IsUnresolvedIP());
146 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
147 EXPECT_EQ("a.b.com", addr.hostname());
148 EXPECT_EQ("a.b.com:5678", addr.ToString());
149 }
150
TEST(SocketAddressTest,TestFromIPv4String)151 TEST(SocketAddressTest, TestFromIPv4String) {
152 SocketAddress addr;
153 EXPECT_TRUE(addr.FromString("1.2.3.4:5678"));
154 EXPECT_FALSE(addr.IsUnresolvedIP());
155 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
156 EXPECT_EQ(5678, addr.port());
157 EXPECT_EQ("1.2.3.4", addr.hostname());
158 EXPECT_EQ("1.2.3.4:5678", addr.ToString());
159 }
160
TEST(SocketAddressTest,TestFromIPv6String)161 TEST(SocketAddressTest, TestFromIPv6String) {
162 SocketAddress addr;
163 EXPECT_TRUE(addr.FromString(kTestV6AddrFullString));
164 EXPECT_FALSE(addr.IsUnresolvedIP());
165 EXPECT_EQ(5678, addr.port());
166 EXPECT_EQ(kTestV6AddrString, addr.hostname());
167 EXPECT_EQ(kTestV6AddrFullString, addr.ToString());
168 }
169
TEST(SocketAddressTest,TestFromHostname)170 TEST(SocketAddressTest, TestFromHostname) {
171 SocketAddress addr;
172 EXPECT_TRUE(addr.FromString("a.b.com:5678"));
173 EXPECT_TRUE(addr.IsUnresolvedIP());
174 EXPECT_EQ(IPAddress(), addr.ipaddr());
175 EXPECT_EQ(5678, addr.port());
176 EXPECT_EQ("a.b.com", addr.hostname());
177 EXPECT_EQ("a.b.com:5678", addr.ToString());
178 }
179
TEST(SocketAddressTest,TestToFromSockAddr)180 TEST(SocketAddressTest, TestToFromSockAddr) {
181 SocketAddress from("1.2.3.4", 5678), addr;
182 sockaddr_in addr_in;
183 from.ToSockAddr(&addr_in);
184 EXPECT_TRUE(addr.FromSockAddr(addr_in));
185 EXPECT_FALSE(addr.IsUnresolvedIP());
186 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
187 EXPECT_EQ(5678, addr.port());
188 EXPECT_EQ("", addr.hostname());
189 EXPECT_EQ("1.2.3.4:5678", addr.ToString());
190 }
191
TEST(SocketAddressTest,TestToFromSockAddrStorage)192 TEST(SocketAddressTest, TestToFromSockAddrStorage) {
193 SocketAddress from("1.2.3.4", 5678), addr;
194 sockaddr_storage addr_storage;
195 from.ToSockAddrStorage(&addr_storage);
196 EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr));
197 EXPECT_FALSE(addr.IsUnresolvedIP());
198 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr());
199 EXPECT_EQ(5678, addr.port());
200 EXPECT_EQ("", addr.hostname());
201 EXPECT_EQ("1.2.3.4:5678", addr.ToString());
202
203 addr.Clear();
204 from.ToDualStackSockAddrStorage(&addr_storage);
205 EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr));
206 EXPECT_FALSE(addr.IsUnresolvedIP());
207 EXPECT_EQ(IPAddress(kMappedV4Addr), addr.ipaddr());
208 EXPECT_EQ(5678, addr.port());
209 EXPECT_EQ("", addr.hostname());
210 EXPECT_EQ("[::ffff:1.2.3.4]:5678", addr.ToString());
211
212 addr.Clear();
213 memset(&addr_storage, 0, sizeof(sockaddr_storage));
214 from = SocketAddress(kTestV6AddrString, 5678);
215 from.SetScopeID(6);
216 from.ToSockAddrStorage(&addr_storage);
217 EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr));
218 EXPECT_FALSE(addr.IsUnresolvedIP());
219 EXPECT_EQ(IPAddress(kTestV6Addr), addr.ipaddr());
220 EXPECT_EQ(5678, addr.port());
221 EXPECT_EQ("", addr.hostname());
222 EXPECT_EQ(kTestV6AddrFullString, addr.ToString());
223 EXPECT_EQ(6, addr.scope_id());
224
225 addr.Clear();
226 from.ToDualStackSockAddrStorage(&addr_storage);
227 EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr));
228 EXPECT_FALSE(addr.IsUnresolvedIP());
229 EXPECT_EQ(IPAddress(kTestV6Addr), addr.ipaddr());
230 EXPECT_EQ(5678, addr.port());
231 EXPECT_EQ("", addr.hostname());
232 EXPECT_EQ(kTestV6AddrFullString, addr.ToString());
233 EXPECT_EQ(6, addr.scope_id());
234
235 addr = from;
236 addr_storage.ss_family = AF_UNSPEC;
237 EXPECT_FALSE(SocketAddressFromSockAddrStorage(addr_storage, &addr));
238 EXPECT_EQ(from, addr);
239
240 EXPECT_FALSE(SocketAddressFromSockAddrStorage(addr_storage, nullptr));
241 }
242
AreEqual(const SocketAddress & addr1,const SocketAddress & addr2)243 bool AreEqual(const SocketAddress& addr1, const SocketAddress& addr2) {
244 return addr1 == addr2 && addr2 == addr1 && !(addr1 != addr2) &&
245 !(addr2 != addr1);
246 }
247
AreUnequal(const SocketAddress & addr1,const SocketAddress & addr2)248 bool AreUnequal(const SocketAddress& addr1, const SocketAddress& addr2) {
249 return !(addr1 == addr2) && !(addr2 == addr1) && addr1 != addr2 &&
250 addr2 != addr1;
251 }
252
TEST(SocketAddressTest,TestEqualityOperators)253 TEST(SocketAddressTest, TestEqualityOperators) {
254 SocketAddress addr1("1.2.3.4", 5678);
255 SocketAddress addr2("1.2.3.4", 5678);
256 EXPECT_TRUE(AreEqual(addr1, addr2));
257
258 addr2 = SocketAddress("0.0.0.1", 5678);
259 EXPECT_TRUE(AreUnequal(addr1, addr2));
260
261 addr2 = SocketAddress("1.2.3.4", 1234);
262 EXPECT_TRUE(AreUnequal(addr1, addr2));
263
264 addr2 = SocketAddress(kTestV6AddrString, 5678);
265 EXPECT_TRUE(AreUnequal(addr1, addr2));
266
267 addr1 = SocketAddress(kTestV6AddrString, 5678);
268 EXPECT_TRUE(AreEqual(addr1, addr2));
269
270 addr2 = SocketAddress(kTestV6AddrString, 1234);
271 EXPECT_TRUE(AreUnequal(addr1, addr2));
272
273 addr2 = SocketAddress("fe80::1", 5678);
274 EXPECT_TRUE(AreUnequal(addr1, addr2));
275
276 SocketAddress addr3("a.b.c.d", 1);
277 SocketAddress addr4("b.b.c.d", 1);
278 EXPECT_TRUE(AreUnequal(addr3, addr4));
279 EXPECT_TRUE(AreEqual(addr3, addr3));
280
281 addr3.SetIP(addr1.ip());
282 addr4.SetIP(addr1.ip());
283 EXPECT_TRUE(AreEqual(addr3, addr4));
284 }
285
IsLessThan(const SocketAddress & addr1,const SocketAddress & addr2)286 bool IsLessThan(const SocketAddress& addr1, const SocketAddress& addr2) {
287 return addr1 < addr2 && !(addr2 < addr1) && !(addr1 == addr2);
288 }
289
TEST(SocketAddressTest,TestComparisonOperator)290 TEST(SocketAddressTest, TestComparisonOperator) {
291 SocketAddress addr1("1.2.3.4", 5678);
292 SocketAddress addr2("1.2.3.4", 5678);
293
294 EXPECT_FALSE(addr1 < addr2);
295 EXPECT_FALSE(addr2 < addr1);
296
297 addr2 = SocketAddress("1.2.3.4", 5679);
298 EXPECT_TRUE(IsLessThan(addr1, addr2));
299
300 addr2 = SocketAddress("2.2.3.4", 49152);
301 EXPECT_TRUE(IsLessThan(addr1, addr2));
302
303 addr2 = SocketAddress(kTestV6AddrString, 5678);
304 EXPECT_TRUE(IsLessThan(addr1, addr2));
305
306 addr1 = SocketAddress("fe80::1", 5678);
307 EXPECT_TRUE(IsLessThan(addr2, addr1));
308
309 addr2 = SocketAddress("fe80::1", 5679);
310 EXPECT_TRUE(IsLessThan(addr1, addr2));
311
312 addr2 = SocketAddress("fe80::1", 5678);
313 EXPECT_FALSE(addr1 < addr2);
314 EXPECT_FALSE(addr2 < addr1);
315
316 SocketAddress addr3("a.b.c.d", 1);
317 SocketAddress addr4("b.b.c.d", 1);
318 EXPECT_TRUE(IsLessThan(addr3, addr4));
319 }
320
TEST(SocketAddressTest,TestToSensitiveString)321 TEST(SocketAddressTest, TestToSensitiveString) {
322 SocketAddress addr_v4("1.2.3.4", 5678);
323 EXPECT_EQ("1.2.3.4", addr_v4.HostAsURIString());
324 EXPECT_EQ("1.2.3.4:5678", addr_v4.ToString());
325
326 #if defined(NDEBUG)
327 EXPECT_EQ("1.2.3.x", addr_v4.HostAsSensitiveURIString());
328 EXPECT_EQ("1.2.3.x:5678", addr_v4.ToSensitiveString());
329 #else
330 EXPECT_EQ("1.2.3.4", addr_v4.HostAsSensitiveURIString());
331 EXPECT_EQ("1.2.3.4:5678", addr_v4.ToSensitiveString());
332 #endif // defined(NDEBUG)
333
334 SocketAddress addr_v6(kTestV6AddrString, 5678);
335 EXPECT_EQ("[" + kTestV6AddrString + "]", addr_v6.HostAsURIString());
336 EXPECT_EQ(kTestV6AddrFullString, addr_v6.ToString());
337 #if defined(NDEBUG)
338 EXPECT_EQ("[" + kTestV6AddrAnonymizedString + "]",
339 addr_v6.HostAsSensitiveURIString());
340 EXPECT_EQ(kTestV6AddrFullAnonymizedString, addr_v6.ToSensitiveString());
341 #else
342 EXPECT_EQ("[" + kTestV6AddrString + "]", addr_v6.HostAsSensitiveURIString());
343 EXPECT_EQ(kTestV6AddrFullString, addr_v6.ToSensitiveString());
344 #endif // defined(NDEBUG)
345 }
346
347 } // namespace rtc
348