blob: 8084540aa1fd41e9e53802a31968e05e44bc6cf8 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/gunit.h"
14#include "rtc_base/nethelpers.h"
15#include "rtc_base/win32.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
17#if !defined(WEBRTC_WIN)
18#error Only for Windows
19#endif
20
21namespace rtc {
22
23class Win32Test : public testing::Test {
24 public:
25 Win32Test() {
26 }
27};
28
29TEST_F(Win32Test, FileTimeToUInt64Test) {
30 FILETIME ft;
31 ft.dwHighDateTime = 0xBAADF00D;
32 ft.dwLowDateTime = 0xFEED3456;
33
Peter Boström0c4e06b2015-10-07 12:23:21 +020034 uint64_t expected = 0xBAADF00DFEED3456;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035 EXPECT_EQ(expected, ToUInt64(ft));
36}
37
guoweis@webrtc.orgb08f4042015-02-17 19:00:42 +000038TEST_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
deadbeef966963a2017-05-08 11:35:56 -070071// Test that invalid IPv6 addresses are recognized and false is returned.
72TEST_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.orgf0488722014-05-13 18:00:26 +000098} // namespace rtc