Fix issue 4061.

Issue was that the longest 0 detection wasn't done when there is only one 0 octet. The purpose of this detection is to make sure we don't also compression 0 octet sequences which are not longest.

BUG=4061
R=juberti@webrtc.org, pthatcher@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/33309004

Cr-Commit-Position: refs/heads/master@{#8397}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8397 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
guoweis@webrtc.org 2015-02-17 19:00:42 +00:00
parent 0abc6011b9
commit b08f4045ec
2 changed files with 36 additions and 3 deletions

View File

@ -86,7 +86,7 @@ const char* inet_ntop_v6(const void* src, char* dst, socklen_t size) {
reinterpret_cast<const uint16*>(src); reinterpret_cast<const uint16*>(src);
int runpos[8]; int runpos[8];
int current = 1; int current = 1;
int max = 1; int max = 0;
int maxpos = -1; int maxpos = -1;
int run_array_size = ARRAY_SIZE(runpos); int run_array_size = ARRAY_SIZE(runpos);
// Run over the address marking runs of 0s. // Run over the address marking runs of 0s.
@ -100,11 +100,11 @@ const char* inet_ntop_v6(const void* src, char* dst, socklen_t size) {
++current; ++current;
} else { } else {
runpos[i] = -1; runpos[i] = -1;
current =1; current = 1;
} }
} }
if (max > 1) { if (max > 0) {
int tmpmax = maxpos; int tmpmax = maxpos;
// Run back through, setting -1 for all but the longest run. // Run back through, setting -1 for all but the longest run.
for (int i = run_array_size - 1; i >= 0; i--) { for (int i = run_array_size - 1; i >= 0; i--) {

View File

@ -59,4 +59,37 @@ TEST_F(Win32Test, WinPingTest) {
IPAddress(INADDR_LOOPBACK), 20, 50, 0, false)); IPAddress(INADDR_LOOPBACK), 20, 50, 0, false));
} }
TEST_F(Win32Test, IPv6AddressCompression) {
IPAddress ipv6;
// Zero compression should be done on the leftmost 0s when there are
// multiple longest series.
ASSERT_TRUE(IPFromString("2a00:8a00:a000:1190:0000:0001:000:252", &ipv6));
EXPECT_EQ("2a00:8a00:a000:1190::1:0:252", ipv6.ToString());
// Ensure the zero compression could handle multiple octects.
ASSERT_TRUE(IPFromString("0:0:0:0:0:0:0:1", &ipv6));
EXPECT_EQ("::1", ipv6.ToString());
// Make sure multiple 0 octects compressed.
ASSERT_TRUE(IPFromString("fe80:0:0:0:2aa:ff:fe9a:4ca2", &ipv6));
EXPECT_EQ("fe80::2aa:ff:fe9a:4ca2", ipv6.ToString());
// Test zero compression at the end of string.
ASSERT_TRUE(IPFromString("2a00:8a00:a000:1190:0000:0001:000:00", &ipv6));
EXPECT_EQ("2a00:8a00:a000:1190:0:1::", ipv6.ToString());
// Test zero compression at the beginning of string.
ASSERT_TRUE(IPFromString("0:0:000:1190:0000:0001:000:00", &ipv6));
EXPECT_EQ("::1190:0:1:0:0", ipv6.ToString());
// Test zero compression only done once.
ASSERT_TRUE(IPFromString("0:1:000:1190:0000:0001:000:01", &ipv6));
EXPECT_EQ("::1:0:1190:0:1:0:1", ipv6.ToString());
// Make sure noncompressable IPv6 is the same.
ASSERT_TRUE(IPFromString("1234:5678:abcd:1234:5678:abcd:1234:5678", &ipv6));
EXPECT_EQ("1234:5678:abcd:1234:5678:abcd:1234:5678", ipv6.ToString());
}
} // namespace rtc } // namespace rtc