henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 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 | |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 11 | #include "rtc_base/win32.h" |
| 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | #include <string> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 15 | #include "rtc_base/gunit.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 16 | #include "rtc_base/net_helpers.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 17 | |
| 18 | #if !defined(WEBRTC_WIN) |
| 19 | #error Only for Windows |
| 20 | #endif |
| 21 | |
| 22 | namespace rtc { |
| 23 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 13:11:12 | [diff] [blame] | 24 | class Win32Test : public ::testing::Test { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 25 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 26 | Win32Test() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 27 | }; |
| 28 | |
guoweis@webrtc.org | b08f404 | 2015-02-17 19:00:42 | [diff] [blame] | 29 | TEST_F(Win32Test, IPv6AddressCompression) { |
| 30 | IPAddress ipv6; |
| 31 | |
| 32 | // Zero compression should be done on the leftmost 0s when there are |
| 33 | // multiple longest series. |
| 34 | ASSERT_TRUE(IPFromString("2a00:8a00:a000:1190:0000:0001:000:252", &ipv6)); |
| 35 | EXPECT_EQ("2a00:8a00:a000:1190::1:0:252", ipv6.ToString()); |
| 36 | |
| 37 | // Ensure the zero compression could handle multiple octects. |
| 38 | ASSERT_TRUE(IPFromString("0:0:0:0:0:0:0:1", &ipv6)); |
| 39 | EXPECT_EQ("::1", ipv6.ToString()); |
| 40 | |
| 41 | // Make sure multiple 0 octects compressed. |
| 42 | ASSERT_TRUE(IPFromString("fe80:0:0:0:2aa:ff:fe9a:4ca2", &ipv6)); |
| 43 | EXPECT_EQ("fe80::2aa:ff:fe9a:4ca2", ipv6.ToString()); |
| 44 | |
| 45 | // Test zero compression at the end of string. |
| 46 | ASSERT_TRUE(IPFromString("2a00:8a00:a000:1190:0000:0001:000:00", &ipv6)); |
| 47 | EXPECT_EQ("2a00:8a00:a000:1190:0:1::", ipv6.ToString()); |
| 48 | |
| 49 | // Test zero compression at the beginning of string. |
| 50 | ASSERT_TRUE(IPFromString("0:0:000:1190:0000:0001:000:00", &ipv6)); |
| 51 | EXPECT_EQ("::1190:0:1:0:0", ipv6.ToString()); |
| 52 | |
| 53 | // Test zero compression only done once. |
| 54 | ASSERT_TRUE(IPFromString("0:1:000:1190:0000:0001:000:01", &ipv6)); |
| 55 | EXPECT_EQ("::1:0:1190:0:1:0:1", ipv6.ToString()); |
| 56 | |
| 57 | // Make sure noncompressable IPv6 is the same. |
| 58 | ASSERT_TRUE(IPFromString("1234:5678:abcd:1234:5678:abcd:1234:5678", &ipv6)); |
| 59 | EXPECT_EQ("1234:5678:abcd:1234:5678:abcd:1234:5678", ipv6.ToString()); |
| 60 | } |
| 61 | |
deadbeef | 966963a | 2017-05-08 18:35:56 | [diff] [blame] | 62 | // Test that invalid IPv6 addresses are recognized and false is returned. |
| 63 | TEST_F(Win32Test, InvalidIPv6AddressParsing) { |
| 64 | IPAddress ipv6; |
| 65 | |
| 66 | // More than 1 run of "::"s. |
| 67 | EXPECT_FALSE(IPFromString("1::2::3", &ipv6)); |
| 68 | |
| 69 | // More than 1 run of "::"s in a longer address. |
| 70 | // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7592 |
| 71 | EXPECT_FALSE(IPFromString("1::2::3::4::5::6::7::8", &ipv6)); |
| 72 | |
| 73 | // Three ':'s in a row. |
| 74 | EXPECT_FALSE(IPFromString("1:::2", &ipv6)); |
| 75 | |
| 76 | // Non-hex character. |
| 77 | EXPECT_FALSE(IPFromString("test::1", &ipv6)); |
| 78 | |
| 79 | // More than 4 hex digits per group. |
| 80 | EXPECT_FALSE(IPFromString("abcde::1", &ipv6)); |
| 81 | |
| 82 | // More than 8 groups. |
| 83 | EXPECT_FALSE(IPFromString("1:2:3:4:5:6:7:8:9", &ipv6)); |
| 84 | |
| 85 | // Less than 8 groups. |
| 86 | EXPECT_FALSE(IPFromString("1:2:3:4:5:6:7", &ipv6)); |
| 87 | } |
| 88 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 89 | } // namespace rtc |