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
diff --git a/webrtc/base/win32.cc b/webrtc/base/win32.cc
index 8f56612..c1b55bf 100644
--- a/webrtc/base/win32.cc
+++ b/webrtc/base/win32.cc
@@ -86,7 +86,7 @@
       reinterpret_cast<const uint16*>(src);
   int runpos[8];
   int current = 1;
-  int max = 1;
+  int max = 0;
   int maxpos = -1;
   int run_array_size = ARRAY_SIZE(runpos);
   // Run over the address marking runs of 0s.
@@ -100,11 +100,11 @@
       ++current;
     } else {
       runpos[i] = -1;
-      current =1;
+      current = 1;
     }
   }
 
-  if (max > 1) {
+  if (max > 0) {
     int tmpmax = maxpos;
     // Run back through, setting -1 for all but the longest run.
     for (int i = run_array_size - 1; i >= 0; i--) {
diff --git a/webrtc/base/win32_unittest.cc b/webrtc/base/win32_unittest.cc
index 0050c77..2bd93ac 100644
--- a/webrtc/base/win32_unittest.cc
+++ b/webrtc/base/win32_unittest.cc
@@ -59,4 +59,37 @@
             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