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