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::"; 31 const std::string kTestV6AddrFullString = 32 "[2001:db8:1020:3040:5060:7080:90a0:b0c0]:5678"; 33 const std::string kTestV6AddrFullAnonymizedString = "[2001:db8:1020::]:5678"; 34 35 TEST(SocketAddressTest, TestDefaultCtor) { 36 SocketAddress addr; 37 EXPECT_FALSE(addr.IsUnresolvedIP()); 38 EXPECT_EQ(IPAddress(), addr.ipaddr()); 39 EXPECT_EQ(0, addr.port()); 40 EXPECT_EQ("", addr.hostname()); 41 } 42 43 TEST(SocketAddressTest, TestIPPortCtor) { 44 SocketAddress addr(IPAddress(0x01020304), 5678); 45 EXPECT_FALSE(addr.IsUnresolvedIP()); 46 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr()); 47 EXPECT_EQ(5678, addr.port()); 48 EXPECT_EQ("", addr.hostname()); 49 EXPECT_EQ("1.2.3.4:5678", addr.ToString()); 50 } 51 52 TEST(SocketAddressTest, TestIPv4StringPortCtor) { 53 SocketAddress addr("1.2.3.4", 5678); 54 EXPECT_FALSE(addr.IsUnresolvedIP()); 55 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr()); 56 EXPECT_EQ(5678, addr.port()); 57 EXPECT_EQ("1.2.3.4", addr.hostname()); 58 EXPECT_EQ("1.2.3.4:5678", addr.ToString()); 59 } 60 61 TEST(SocketAddressTest, TestIPv6StringPortCtor) { 62 SocketAddress addr2(kTestV6AddrString, 1234); 63 IPAddress tocheck(kTestV6Addr); 64 65 EXPECT_FALSE(addr2.IsUnresolvedIP()); 66 EXPECT_EQ(tocheck, addr2.ipaddr()); 67 EXPECT_EQ(1234, addr2.port()); 68 EXPECT_EQ(kTestV6AddrString, addr2.hostname()); 69 EXPECT_EQ("[" + kTestV6AddrString + "]:1234", addr2.ToString()); 70 } 71 72 TEST(SocketAddressTest, TestSpecialStringPortCtor) { 73 // inet_addr doesn't handle this address properly. 74 SocketAddress addr("255.255.255.255", 5678); 75 EXPECT_FALSE(addr.IsUnresolvedIP()); 76 EXPECT_EQ(IPAddress(0xFFFFFFFFU), addr.ipaddr()); 77 EXPECT_EQ(5678, addr.port()); 78 EXPECT_EQ("255.255.255.255", addr.hostname()); 79 EXPECT_EQ("255.255.255.255:5678", addr.ToString()); 80 } 81 82 TEST(SocketAddressTest, TestHostnamePortCtor) { 83 SocketAddress addr("a.b.com", 5678); 84 EXPECT_TRUE(addr.IsUnresolvedIP()); 85 EXPECT_EQ(IPAddress(), addr.ipaddr()); 86 EXPECT_EQ(5678, addr.port()); 87 EXPECT_EQ("a.b.com", addr.hostname()); 88 EXPECT_EQ("a.b.com:5678", addr.ToString()); 89 } 90 91 TEST(SocketAddressTest, TestCopyCtor) { 92 SocketAddress from("1.2.3.4", 5678); 93 SocketAddress addr(from); 94 EXPECT_FALSE(addr.IsUnresolvedIP()); 95 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr()); 96 EXPECT_EQ(5678, addr.port()); 97 EXPECT_EQ("1.2.3.4", addr.hostname()); 98 EXPECT_EQ("1.2.3.4:5678", addr.ToString()); 99 } 100 101 TEST(SocketAddressTest, TestAssign) { 102 SocketAddress from("1.2.3.4", 5678); 103 SocketAddress addr(IPAddress(0x88888888), 9999); 104 addr = from; 105 EXPECT_FALSE(addr.IsUnresolvedIP()); 106 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr()); 107 EXPECT_EQ(5678, addr.port()); 108 EXPECT_EQ("1.2.3.4", addr.hostname()); 109 EXPECT_EQ("1.2.3.4:5678", addr.ToString()); 110 } 111 112 TEST(SocketAddressTest, TestSetIPPort) { 113 SocketAddress addr(IPAddress(0x88888888), 9999); 114 addr.SetIP(IPAddress(0x01020304)); 115 addr.SetPort(5678); 116 EXPECT_FALSE(addr.IsUnresolvedIP()); 117 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr()); 118 EXPECT_EQ(5678, addr.port()); 119 EXPECT_EQ("", addr.hostname()); 120 EXPECT_EQ("1.2.3.4:5678", addr.ToString()); 121 } 122 123 TEST(SocketAddressTest, TestSetIPFromString) { 124 SocketAddress addr(IPAddress(0x88888888), 9999); 125 addr.SetIP("1.2.3.4"); 126 addr.SetPort(5678); 127 EXPECT_FALSE(addr.IsUnresolvedIP()); 128 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr()); 129 EXPECT_EQ(5678, addr.port()); 130 EXPECT_EQ("1.2.3.4", addr.hostname()); 131 EXPECT_EQ("1.2.3.4:5678", addr.ToString()); 132 } 133 134 TEST(SocketAddressTest, TestSetIPFromHostname) { 135 SocketAddress addr(IPAddress(0x88888888), 9999); 136 addr.SetIP("a.b.com"); 137 addr.SetPort(5678); 138 EXPECT_TRUE(addr.IsUnresolvedIP()); 139 EXPECT_EQ(IPAddress(), addr.ipaddr()); 140 EXPECT_EQ(5678, addr.port()); 141 EXPECT_EQ("a.b.com", addr.hostname()); 142 EXPECT_EQ("a.b.com:5678", addr.ToString()); 143 addr.SetResolvedIP(IPAddress(0x01020304)); 144 EXPECT_FALSE(addr.IsUnresolvedIP()); 145 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr()); 146 EXPECT_EQ("a.b.com", addr.hostname()); 147 EXPECT_EQ("a.b.com:5678", addr.ToString()); 148 } 149 150 TEST(SocketAddressTest, TestFromIPv4String) { 151 SocketAddress addr; 152 EXPECT_TRUE(addr.FromString("1.2.3.4:5678")); 153 EXPECT_FALSE(addr.IsUnresolvedIP()); 154 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr()); 155 EXPECT_EQ(5678, addr.port()); 156 EXPECT_EQ("1.2.3.4", addr.hostname()); 157 EXPECT_EQ("1.2.3.4:5678", addr.ToString()); 158 } 159 160 TEST(SocketAddressTest, TestFromIPv6String) { 161 SocketAddress addr; 162 EXPECT_TRUE(addr.FromString(kTestV6AddrFullString)); 163 EXPECT_FALSE(addr.IsUnresolvedIP()); 164 EXPECT_EQ(5678, addr.port()); 165 EXPECT_EQ(kTestV6AddrString, addr.hostname()); 166 EXPECT_EQ(kTestV6AddrFullString, addr.ToString()); 167 } 168 169 TEST(SocketAddressTest, TestFromHostname) { 170 SocketAddress addr; 171 EXPECT_TRUE(addr.FromString("a.b.com:5678")); 172 EXPECT_TRUE(addr.IsUnresolvedIP()); 173 EXPECT_EQ(IPAddress(), addr.ipaddr()); 174 EXPECT_EQ(5678, addr.port()); 175 EXPECT_EQ("a.b.com", addr.hostname()); 176 EXPECT_EQ("a.b.com:5678", addr.ToString()); 177 } 178 179 TEST(SocketAddressTest, TestToFromSockAddr) { 180 SocketAddress from("1.2.3.4", 5678), addr; 181 sockaddr_in addr_in; 182 from.ToSockAddr(&addr_in); 183 EXPECT_TRUE(addr.FromSockAddr(addr_in)); 184 EXPECT_FALSE(addr.IsUnresolvedIP()); 185 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr()); 186 EXPECT_EQ(5678, addr.port()); 187 EXPECT_EQ("", addr.hostname()); 188 EXPECT_EQ("1.2.3.4:5678", addr.ToString()); 189 } 190 191 TEST(SocketAddressTest, TestToFromSockAddrStorage) { 192 SocketAddress from("1.2.3.4", 5678), addr; 193 sockaddr_storage addr_storage; 194 from.ToSockAddrStorage(&addr_storage); 195 EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr)); 196 EXPECT_FALSE(addr.IsUnresolvedIP()); 197 EXPECT_EQ(IPAddress(0x01020304U), addr.ipaddr()); 198 EXPECT_EQ(5678, addr.port()); 199 EXPECT_EQ("", addr.hostname()); 200 EXPECT_EQ("1.2.3.4:5678", addr.ToString()); 201 202 addr.Clear(); 203 from.ToDualStackSockAddrStorage(&addr_storage); 204 EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr)); 205 EXPECT_FALSE(addr.IsUnresolvedIP()); 206 EXPECT_EQ(IPAddress(kMappedV4Addr), addr.ipaddr()); 207 EXPECT_EQ(5678, addr.port()); 208 EXPECT_EQ("", addr.hostname()); 209 EXPECT_EQ("[::ffff:1.2.3.4]:5678", addr.ToString()); 210 211 addr.Clear(); 212 memset(&addr_storage, 0, sizeof(sockaddr_storage)); 213 from = SocketAddress(kTestV6AddrString, 5678); 214 from.SetScopeID(6); 215 from.ToSockAddrStorage(&addr_storage); 216 EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr)); 217 EXPECT_FALSE(addr.IsUnresolvedIP()); 218 EXPECT_EQ(IPAddress(kTestV6Addr), addr.ipaddr()); 219 EXPECT_EQ(5678, addr.port()); 220 EXPECT_EQ("", addr.hostname()); 221 EXPECT_EQ(kTestV6AddrFullString, addr.ToString()); 222 EXPECT_EQ(6, addr.scope_id()); 223 224 addr.Clear(); 225 from.ToDualStackSockAddrStorage(&addr_storage); 226 EXPECT_TRUE(SocketAddressFromSockAddrStorage(addr_storage, &addr)); 227 EXPECT_FALSE(addr.IsUnresolvedIP()); 228 EXPECT_EQ(IPAddress(kTestV6Addr), addr.ipaddr()); 229 EXPECT_EQ(5678, addr.port()); 230 EXPECT_EQ("", addr.hostname()); 231 EXPECT_EQ(kTestV6AddrFullString, addr.ToString()); 232 EXPECT_EQ(6, addr.scope_id()); 233 234 addr = from; 235 addr_storage.ss_family = AF_UNSPEC; 236 EXPECT_FALSE(SocketAddressFromSockAddrStorage(addr_storage, &addr)); 237 EXPECT_EQ(from, addr); 238 239 EXPECT_FALSE(SocketAddressFromSockAddrStorage(addr_storage, NULL)); 240 } 241 242 bool AreEqual(const SocketAddress& addr1, 243 const SocketAddress& addr2) { 244 return addr1 == addr2 && addr2 == addr1 && 245 !(addr1 != addr2) && !(addr2 != addr1); 246 } 247 248 bool AreUnequal(const SocketAddress& addr1, 249 const SocketAddress& addr2) { 250 return !(addr1 == addr2) && !(addr2 == addr1) && 251 addr1 != addr2 && addr2 != addr1; 252 } 253 254 TEST(SocketAddressTest, TestEqualityOperators) { 255 SocketAddress addr1("1.2.3.4", 5678); 256 SocketAddress addr2("1.2.3.4", 5678); 257 EXPECT_PRED2(AreEqual, addr1, addr2); 258 259 addr2 = SocketAddress("0.0.0.1", 5678); 260 EXPECT_PRED2(AreUnequal, addr1, addr2); 261 262 addr2 = SocketAddress("1.2.3.4", 1234); 263 EXPECT_PRED2(AreUnequal, addr1, addr2); 264 265 addr2 = SocketAddress(kTestV6AddrString, 5678); 266 EXPECT_PRED2(AreUnequal, addr1, addr2); 267 268 addr1 = SocketAddress(kTestV6AddrString, 5678); 269 EXPECT_PRED2(AreEqual, addr1, addr2); 270 271 addr2 = SocketAddress(kTestV6AddrString, 1234); 272 EXPECT_PRED2(AreUnequal, addr1, addr2); 273 274 addr2 = SocketAddress("fe80::1", 5678); 275 EXPECT_PRED2(AreUnequal, addr1, addr2); 276 } 277 278 bool IsLessThan(const SocketAddress& addr1, 279 const SocketAddress& addr2) { 280 return addr1 < addr2 && 281 !(addr2 < addr1) && 282 !(addr1 == addr2); 283 } 284 285 TEST(SocketAddressTest, TestComparisonOperator) { 286 SocketAddress addr1("1.2.3.4", 5678); 287 SocketAddress addr2("1.2.3.4", 5678); 288 289 EXPECT_FALSE(addr1 < addr2); 290 EXPECT_FALSE(addr2 < addr1); 291 292 addr2 = SocketAddress("1.2.3.4", 5679); 293 EXPECT_PRED2(IsLessThan, addr1, addr2); 294 295 addr2 = SocketAddress("2.2.3.4", 49152); 296 EXPECT_PRED2(IsLessThan, addr1, addr2); 297 298 addr2 = SocketAddress(kTestV6AddrString, 5678); 299 EXPECT_PRED2(IsLessThan, addr1, addr2); 300 301 addr1 = SocketAddress("fe80::1", 5678); 302 EXPECT_PRED2(IsLessThan, addr2, addr1); 303 304 addr2 = SocketAddress("fe80::1", 5679); 305 EXPECT_PRED2(IsLessThan, addr1, addr2); 306 307 addr2 = SocketAddress("fe80::1", 5678); 308 EXPECT_FALSE(addr1 < addr2); 309 EXPECT_FALSE(addr2 < addr1); 310 } 311 312 TEST(SocketAddressTest, TestToSensitiveString) { 313 SocketAddress addr_v4("1.2.3.4", 5678); 314 EXPECT_EQ("1.2.3.4", addr_v4.HostAsURIString()); 315 EXPECT_EQ("1.2.3.4:5678", addr_v4.ToString()); 316 EXPECT_EQ("1.2.3.4", addr_v4.HostAsSensitiveURIString()); 317 EXPECT_EQ("1.2.3.4:5678", addr_v4.ToSensitiveString()); 318 IPAddress::set_strip_sensitive(true); 319 EXPECT_EQ("1.2.3.x", addr_v4.HostAsSensitiveURIString()); 320 EXPECT_EQ("1.2.3.x:5678", addr_v4.ToSensitiveString()); 321 IPAddress::set_strip_sensitive(false); 322 323 SocketAddress addr_v6(kTestV6AddrString, 5678); 324 EXPECT_EQ("[" + kTestV6AddrString + "]", addr_v6.HostAsURIString()); 325 EXPECT_EQ(kTestV6AddrFullString, addr_v6.ToString()); 326 EXPECT_EQ("[" + kTestV6AddrString + "]", addr_v6.HostAsSensitiveURIString()); 327 EXPECT_EQ(kTestV6AddrFullString, addr_v6.ToSensitiveString()); 328 IPAddress::set_strip_sensitive(true); 329 EXPECT_EQ("[" + kTestV6AddrAnonymizedString + "]", 330 addr_v6.HostAsSensitiveURIString()); 331 EXPECT_EQ(kTestV6AddrFullAnonymizedString, addr_v6.ToSensitiveString()); 332 IPAddress::set_strip_sensitive(false); 333 } 334 335 } // namespace rtc 336