xref: /aosp_15_r20/external/webrtc/rtc_base/win32_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "rtc_base/win32.h"
12 
13 #include <string>
14 
15 #include "rtc_base/gunit.h"
16 #include "rtc_base/net_helpers.h"
17 
18 #if !defined(WEBRTC_WIN)
19 #error Only for Windows
20 #endif
21 
22 namespace rtc {
23 
24 class Win32Test : public ::testing::Test {
25  public:
Win32Test()26   Win32Test() {}
27 };
28 
TEST_F(Win32Test,IPv6AddressCompression)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 
62 // Test that invalid IPv6 addresses are recognized and false is returned.
TEST_F(Win32Test,InvalidIPv6AddressParsing)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 
89 }  // namespace rtc
90