OLD | NEW |
| (Empty) |
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 "webrtc/base/gunit.h" | |
16 #include "webrtc/base/socketaddress.h" | |
17 #include "webrtc/base/ipaddress.h" | |
18 | |
19 namespace rtc { | |
20 | |
21 const in6_addr kTestV6Addr = { { {0x20, 0x01, 0x0d, 0xb8, | |
22 0x10, 0x20, 0x30, 0x40, | |
23 0x50, 0x60, 0x70, 0x80, | |
24 0x90, 0xA0, 0xB0, 0xC0} } }; | |
25 const in6_addr kMappedV4Addr = { { {0x00, 0x00, 0x00, 0x00, | |
26 0x00, 0x00, 0x00, 0x00, | |
27 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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
243 bool AreEqual(const SocketAddress& addr1, | |
244 const SocketAddress& addr2) { | |
245 return addr1 == addr2 && addr2 == addr1 && | |
246 !(addr1 != addr2) && !(addr2 != addr1); | |
247 } | |
248 | |
249 bool AreUnequal(const SocketAddress& addr1, | |
250 const SocketAddress& addr2) { | |
251 return !(addr1 == addr2) && !(addr2 == addr1) && | |
252 addr1 != addr2 && addr2 != addr1; | |
253 } | |
254 | |
255 TEST(SocketAddressTest, TestEqualityOperators) { | |
256 SocketAddress addr1("1.2.3.4", 5678); | |
257 SocketAddress addr2("1.2.3.4", 5678); | |
258 EXPECT_PRED2(AreEqual, addr1, addr2); | |
259 | |
260 addr2 = SocketAddress("0.0.0.1", 5678); | |
261 EXPECT_PRED2(AreUnequal, addr1, addr2); | |
262 | |
263 addr2 = SocketAddress("1.2.3.4", 1234); | |
264 EXPECT_PRED2(AreUnequal, addr1, addr2); | |
265 | |
266 addr2 = SocketAddress(kTestV6AddrString, 5678); | |
267 EXPECT_PRED2(AreUnequal, addr1, addr2); | |
268 | |
269 addr1 = SocketAddress(kTestV6AddrString, 5678); | |
270 EXPECT_PRED2(AreEqual, addr1, addr2); | |
271 | |
272 addr2 = SocketAddress(kTestV6AddrString, 1234); | |
273 EXPECT_PRED2(AreUnequal, addr1, addr2); | |
274 | |
275 addr2 = SocketAddress("fe80::1", 5678); | |
276 EXPECT_PRED2(AreUnequal, addr1, addr2); | |
277 | |
278 SocketAddress addr3("a.b.c.d", 1); | |
279 SocketAddress addr4("b.b.c.d", 1); | |
280 EXPECT_PRED2(AreUnequal, addr3, addr4); | |
281 EXPECT_PRED2(AreEqual, addr3, addr3); | |
282 | |
283 addr3.SetIP(addr1.ip()); | |
284 addr4.SetIP(addr1.ip()); | |
285 EXPECT_PRED2(AreEqual,addr3, addr4); | |
286 } | |
287 | |
288 bool IsLessThan(const SocketAddress& addr1, const SocketAddress& addr2) { | |
289 return addr1 < addr2 && | |
290 !(addr2 < addr1) && | |
291 !(addr1 == addr2); | |
292 } | |
293 | |
294 TEST(SocketAddressTest, TestComparisonOperator) { | |
295 SocketAddress addr1("1.2.3.4", 5678); | |
296 SocketAddress addr2("1.2.3.4", 5678); | |
297 | |
298 EXPECT_FALSE(addr1 < addr2); | |
299 EXPECT_FALSE(addr2 < addr1); | |
300 | |
301 addr2 = SocketAddress("1.2.3.4", 5679); | |
302 EXPECT_PRED2(IsLessThan, addr1, addr2); | |
303 | |
304 addr2 = SocketAddress("2.2.3.4", 49152); | |
305 EXPECT_PRED2(IsLessThan, addr1, addr2); | |
306 | |
307 addr2 = SocketAddress(kTestV6AddrString, 5678); | |
308 EXPECT_PRED2(IsLessThan, addr1, addr2); | |
309 | |
310 addr1 = SocketAddress("fe80::1", 5678); | |
311 EXPECT_PRED2(IsLessThan, addr2, addr1); | |
312 | |
313 addr2 = SocketAddress("fe80::1", 5679); | |
314 EXPECT_PRED2(IsLessThan, addr1, addr2); | |
315 | |
316 addr2 = SocketAddress("fe80::1", 5678); | |
317 EXPECT_FALSE(addr1 < addr2); | |
318 EXPECT_FALSE(addr2 < addr1); | |
319 | |
320 SocketAddress addr3("a.b.c.d", 1); | |
321 SocketAddress addr4("b.b.c.d", 1); | |
322 EXPECT_PRED2(IsLessThan, addr3, addr4); | |
323 } | |
324 | |
325 TEST(SocketAddressTest, TestToSensitiveString) { | |
326 SocketAddress addr_v4("1.2.3.4", 5678); | |
327 EXPECT_EQ("1.2.3.4", addr_v4.HostAsURIString()); | |
328 EXPECT_EQ("1.2.3.4:5678", addr_v4.ToString()); | |
329 | |
330 #if defined(NDEBUG) | |
331 EXPECT_EQ("1.2.3.x", addr_v4.HostAsSensitiveURIString()); | |
332 EXPECT_EQ("1.2.3.x:5678", addr_v4.ToSensitiveString()); | |
333 #else | |
334 EXPECT_EQ("1.2.3.4", addr_v4.HostAsSensitiveURIString()); | |
335 EXPECT_EQ("1.2.3.4:5678", addr_v4.ToSensitiveString()); | |
336 #endif // defined(NDEBUG) | |
337 | |
338 SocketAddress addr_v6(kTestV6AddrString, 5678); | |
339 EXPECT_EQ("[" + kTestV6AddrString + "]", addr_v6.HostAsURIString()); | |
340 EXPECT_EQ(kTestV6AddrFullString, addr_v6.ToString()); | |
341 #if defined(NDEBUG) | |
342 EXPECT_EQ("[" + kTestV6AddrAnonymizedString + "]", | |
343 addr_v6.HostAsSensitiveURIString()); | |
344 EXPECT_EQ(kTestV6AddrFullAnonymizedString, addr_v6.ToSensitiveString()); | |
345 #else | |
346 EXPECT_EQ("[" + kTestV6AddrString + "]", addr_v6.HostAsSensitiveURIString()); | |
347 EXPECT_EQ(kTestV6AddrFullString, addr_v6.ToSensitiveString()); | |
348 #endif // defined(NDEBUG) | |
349 } | |
350 | |
351 } // namespace rtc | |
OLD | NEW |