Add "x"s in the end of a stripped IPv6 address string.

This makes it clearer that the IP address has been stripped.

BUG=chromium:254329

Review URL: https://codereview.webrtc.org/1516163003

Cr-Commit-Position: refs/heads/master@{#11005}
This commit is contained in:
henrikg
2015-12-14 02:07:03 -08:00
committed by Commit bot
parent 38bb8ad1ca
commit ac921d7365
3 changed files with 31 additions and 6 deletions

View File

@ -27,6 +27,7 @@
#include "webrtc/base/ipaddress.h"
#include "webrtc/base/byteorder.h"
#include "webrtc/base/checks.h"
#include "webrtc/base/nethelpers.h"
#include "webrtc/base/logging.h"
#include "webrtc/base/win32.h"
@ -157,9 +158,19 @@ std::string IPAddress::ToSensitiveString() const {
return address;
}
case AF_INET6: {
// TODO(grunell): Return a string of format 1:2:3:x:x:x:x:x or such
// instead of zeroing out.
return TruncateIP(*this, 128 - 80).ToString();
// Remove the last 5 groups (80 bits).
std::string address = TruncateIP(*this, 128 - 80).ToString();
// If all three remaining groups are written out explicitly in the string,
// remove one of the two trailing colons before appending the stripped
// groups as "x"s. There should be max 4 colons (2 between the 3 groups +
// 2 trailing) in the truncated address string.
size_t number_of_colons = std::count(address.begin(), address.end(), ':');
RTC_CHECK_LE(number_of_colons, 4u);
if (number_of_colons > 3)
address.resize(address.length() - 1);
return address + "x:x:x:x:x";
}
}
return std::string();

View File

@ -25,6 +25,10 @@ static const in6_addr kIPv6PublicAddr = {{{0x24, 0x01, 0xfa, 0x00,
0x00, 0x04, 0x10, 0x00,
0xbe, 0x30, 0x5b, 0xff,
0xfe, 0xe5, 0x00, 0xc3}}};
static const in6_addr kIPv6PublicAddr2 = {{{0x24, 0x01, 0x00, 0x00,
0x00, 0x00, 0x10, 0x00,
0xbe, 0x30, 0x5b, 0xff,
0xfe, 0xe5, 0x00, 0xc3}}};
static const in6_addr kIPv4MappedAnyAddr = {{{0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff,
@ -52,7 +56,12 @@ static const std::string kIPv6TemporaryAddrString =
"2620:0:1008:1201:2089:6dda:385e:80c0";
static const std::string kIPv6PublicAddrString =
"2401:fa00:4:1000:be30:5bff:fee5:c3";
static const std::string kIPv6PublicAddrAnonymizedString = "2401:fa00:4::";
static const std::string kIPv6PublicAddr2String =
"2401::1000:be30:5bff:fee5:c3";
static const std::string kIPv6PublicAddrAnonymizedString =
"2401:fa00:4:x:x:x:x:x";
static const std::string kIPv6PublicAddr2AnonymizedString =
"2401::x:x:x:x:x";
static const std::string kIPv4MappedAnyAddrString = "::ffff:0:0";
static const std::string kIPv4MappedRFC1918AddrString = "::ffff:c0a8:701";
static const std::string kIPv4MappedLoopbackAddrString = "::ffff:7f00:1";
@ -889,14 +898,18 @@ TEST(IPAddressTest, TestCategorizeIPv6) {
TEST(IPAddressTest, TestToSensitiveString) {
IPAddress addr_v4 = IPAddress(kIPv4PublicAddr);
IPAddress addr_v6 = IPAddress(kIPv6PublicAddr);
IPAddress addr_v6_2 = IPAddress(kIPv6PublicAddr2);
EXPECT_EQ(kIPv4PublicAddrString, addr_v4.ToString());
EXPECT_EQ(kIPv6PublicAddrString, addr_v6.ToString());
EXPECT_EQ(kIPv6PublicAddr2String, addr_v6_2.ToString());
#if defined(NDEBUG)
EXPECT_EQ(kIPv4PublicAddrAnonymizedString, addr_v4.ToSensitiveString());
EXPECT_EQ(kIPv6PublicAddrAnonymizedString, addr_v6.ToSensitiveString());
EXPECT_EQ(kIPv6PublicAddr2AnonymizedString, addr_v6_2.ToSensitiveString());
#else
EXPECT_EQ(kIPv4PublicAddrString, addr_v4.ToSensitiveString());
EXPECT_EQ(kIPv6PublicAddrString, addr_v6.ToSensitiveString());
EXPECT_EQ(kIPv6PublicAddr2String, addr_v6_2.ToSensitiveString());
#endif // defined(NDEBUG)
}

View File

@ -27,10 +27,11 @@ const in6_addr kMappedV4Addr = { { {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0xFF,
0x01, 0x02, 0x03, 0x04} } };
const std::string kTestV6AddrString = "2001:db8:1020:3040:5060:7080:90a0:b0c0";
const std::string kTestV6AddrAnonymizedString = "2001:db8:1020::";
const std::string kTestV6AddrAnonymizedString = "2001:db8:1020:x:x:x:x:x";
const std::string kTestV6AddrFullString =
"[2001:db8:1020:3040:5060:7080:90a0:b0c0]:5678";
const std::string kTestV6AddrFullAnonymizedString = "[2001:db8:1020::]:5678";
const std::string kTestV6AddrFullAnonymizedString =
"[2001:db8:1020:x:x:x:x:x]:5678";
TEST(SocketAddressTest, TestDefaultCtor) {
SocketAddress addr;