blob: 5691be46c86eedfb2d3d3fbaa2ce05b042bddac5 [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:
Yves Gerey665174f2018-06-19 15:03:05 +020025 Win32Test() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026};
27
guoweis@webrtc.orgb08f4042015-02-17 19:00:42 +000028TEST_F(Win32Test, IPv6AddressCompression) {
29 IPAddress ipv6;
30
31 // Zero compression should be done on the leftmost 0s when there are
32 // multiple longest series.
33 ASSERT_TRUE(IPFromString("2a00:8a00:a000:1190:0000:0001:000:252", &ipv6));
34 EXPECT_EQ("2a00:8a00:a000:1190::1:0:252", ipv6.ToString());
35
36 // Ensure the zero compression could handle multiple octects.
37 ASSERT_TRUE(IPFromString("0:0:0:0:0:0:0:1", &ipv6));
38 EXPECT_EQ("::1", ipv6.ToString());
39
40 // Make sure multiple 0 octects compressed.
41 ASSERT_TRUE(IPFromString("fe80:0:0:0:2aa:ff:fe9a:4ca2", &ipv6));
42 EXPECT_EQ("fe80::2aa:ff:fe9a:4ca2", ipv6.ToString());
43
44 // Test zero compression at the end of string.
45 ASSERT_TRUE(IPFromString("2a00:8a00:a000:1190:0000:0001:000:00", &ipv6));
46 EXPECT_EQ("2a00:8a00:a000:1190:0:1::", ipv6.ToString());
47
48 // Test zero compression at the beginning of string.
49 ASSERT_TRUE(IPFromString("0:0:000:1190:0000:0001:000:00", &ipv6));
50 EXPECT_EQ("::1190:0:1:0:0", ipv6.ToString());
51
52 // Test zero compression only done once.
53 ASSERT_TRUE(IPFromString("0:1:000:1190:0000:0001:000:01", &ipv6));
54 EXPECT_EQ("::1:0:1190:0:1:0:1", ipv6.ToString());
55
56 // Make sure noncompressable IPv6 is the same.
57 ASSERT_TRUE(IPFromString("1234:5678:abcd:1234:5678:abcd:1234:5678", &ipv6));
58 EXPECT_EQ("1234:5678:abcd:1234:5678:abcd:1234:5678", ipv6.ToString());
59}
60
deadbeef966963a2017-05-08 11:35:56 -070061// Test that invalid IPv6 addresses are recognized and false is returned.
62TEST_F(Win32Test, InvalidIPv6AddressParsing) {
63 IPAddress ipv6;
64
65 // More than 1 run of "::"s.
66 EXPECT_FALSE(IPFromString("1::2::3", &ipv6));
67
68 // More than 1 run of "::"s in a longer address.
69 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7592
70 EXPECT_FALSE(IPFromString("1::2::3::4::5::6::7::8", &ipv6));
71
72 // Three ':'s in a row.
73 EXPECT_FALSE(IPFromString("1:::2", &ipv6));
74
75 // Non-hex character.
76 EXPECT_FALSE(IPFromString("test::1", &ipv6));
77
78 // More than 4 hex digits per group.
79 EXPECT_FALSE(IPFromString("abcde::1", &ipv6));
80
81 // More than 8 groups.
82 EXPECT_FALSE(IPFromString("1:2:3:4:5:6:7:8:9", &ipv6));
83
84 // Less than 8 groups.
85 EXPECT_FALSE(IPFromString("1:2:3:4:5:6:7", &ipv6));
86}
87
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088} // namespace rtc