diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-05 23:10:10 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-05 23:10:10 +0000 |
commit | 17e06f15fdce9c0523f17405e983551976170bd7 (patch) | |
tree | ec2a31c0a873679cbb1d0d9ad3b421d7630d21a0 /net/base | |
parent | f3aa4f7127a4da46ecd1d20cb757340de21d8d63 (diff) | |
download | chromium_src-17e06f15fdce9c0523f17405e983551976170bd7.zip chromium_src-17e06f15fdce9c0523f17405e983551976170bd7.tar.gz chromium_src-17e06f15fdce9c0523f17405e983551976170bd7.tar.bz2 |
- Make IPEndPoint::ToString() surround IPv6 addresses with square brackets.
- Use a strict weak ordering for operator<
BUG=75858
TEST=net_unittest
Review URL: http://codereview.chromium.org/6793011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80541 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/ip_endpoint.cc | 11 | ||||
-rw-r--r-- | net/base/ip_endpoint_unittest.cc | 31 |
2 files changed, 32 insertions, 10 deletions
diff --git a/net/base/ip_endpoint.cc b/net/base/ip_endpoint.cc index 3d776b1..ff8258a 100644 --- a/net/base/ip_endpoint.cc +++ b/net/base/ip_endpoint.cc @@ -113,11 +113,18 @@ std::string IPEndPoint::ToString() const { if (!ToSockAddr(addr, &addr_len)) { return ""; } - return NetAddressToString(addr, addr_len) + ":" + base::IntToString(port_); + return NetAddressToStringWithPort(addr, addr_len); } bool IPEndPoint::operator<(const IPEndPoint& that) const { - return address_ < that.address_ || port_ < that.port_; + // Sort IPv4 before IPv6. + if (address_.size() != that.address_.size()) { + return address_.size() < that.address_.size(); + } + if (address_ != that.address_) { + return address_ < that.address_; + } + return port_ < that.port_; } bool IPEndPoint::operator==(const IPEndPoint& that) const { diff --git a/net/base/ip_endpoint_unittest.cc b/net/base/ip_endpoint_unittest.cc index 6058cc0..3808b707 100644 --- a/net/base/ip_endpoint_unittest.cc +++ b/net/base/ip_endpoint_unittest.cc @@ -26,8 +26,8 @@ struct TestData { } tests[] = { { "127.0.00.1", "127.0.0.1", false}, { "192.168.1.1", "192.168.1.1", false }, - { "::1", "::1", true }, - { "2001:db8:0::42", "2001:db8::42", true }, + { "::1", "[::1]", true }, + { "2001:db8:0::42", "[2001:db8::42]", true }, }; int test_count = ARRAYSIZE_UNSAFE(tests); @@ -121,32 +121,47 @@ TEST_F(IPEndPointTest, LessThan) { IPEndPoint ip_endpoint1(tests[0].ip_address, 100); IPEndPoint ip_endpoint2(tests[0].ip_address, 1000); EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); + EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); // IPv4 vs IPv6 - ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); + ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81); ip_endpoint2 = IPEndPoint(tests[2].ip_address, 80); - EXPECT_FALSE(ip_endpoint1 < ip_endpoint2); + EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); + EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); // IPv4 vs IPv4 - ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); + ip_endpoint1 = IPEndPoint(tests[0].ip_address, 81); ip_endpoint2 = IPEndPoint(tests[1].ip_address, 80); EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); + EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); // IPv6 vs IPv6 - ip_endpoint1 = IPEndPoint(tests[2].ip_address, 80); + ip_endpoint1 = IPEndPoint(tests[2].ip_address, 81); ip_endpoint2 = IPEndPoint(tests[3].ip_address, 80); EXPECT_TRUE(ip_endpoint1 < ip_endpoint2); + EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); + + // Compare equivalent endpoints. + ip_endpoint1 = IPEndPoint(tests[0].ip_address, 80); + ip_endpoint2 = IPEndPoint(tests[0].ip_address, 80); + EXPECT_FALSE(ip_endpoint1 < ip_endpoint2); + EXPECT_FALSE(ip_endpoint2 < ip_endpoint1); } -TEST_F(IPEndPointTest, DISABLED_ToString) { +TEST_F(IPEndPointTest, ToString) { IPEndPoint endpoint; EXPECT_EQ(0, endpoint.port()); for (int index = 0; index < test_count; ++index) { int port = 100 + index; IPEndPoint endpoint(tests[index].ip_address, port); + const std::string result = endpoint.ToString(); + if (tests[index].ipv6 && result.empty()) { + // NetAddressToStringWithPort may fail on systems without IPv6. + continue; + } EXPECT_EQ(tests[index].host_normalized + ":" + base::IntToString(port), - endpoint.ToString()); + result); } } |