blob: b2955e500a88922bb5ef6d677ca42d68a188affd [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 Olssona4d87372019-07-05 17:08:3311#include "rtc_base/win32.h"
12
henrike@webrtc.orgf0488722014-05-13 18:00:2613#include <string>
14
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 17:11:0016#include "rtc_base/net_helpers.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2617
18#if !defined(WEBRTC_WIN)
19#error Only for Windows
20#endif
21
22namespace rtc {
23
Mirko Bonadei6a489f22019-04-09 13:11:1224class Win32Test : public ::testing::Test {
henrike@webrtc.orgf0488722014-05-13 18:00:2625 public:
Yves Gerey665174f2018-06-19 13:03:0526 Win32Test() {}
henrike@webrtc.orgf0488722014-05-13 18:00:2627};
28
guoweis@webrtc.orgb08f4042015-02-17 19:00:4229TEST_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
deadbeef966963a2017-05-08 18:35:5662// Test that invalid IPv6 addresses are recognized and false is returned.
63TEST_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.orgf0488722014-05-13 18:00:2689} // namespace rtc