components: Fix 'gn check' errors for user_prefs.
[chromium-blink-merge.git] / net / base / ip_pattern_unittest.cc
blob5e06cd69a1c709928c2a2e73899bf3915723df88
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/ip_pattern.h"
7 #include "net/base/ip_address_number.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace net {
12 namespace {
14 bool IsValidPattern(const std::string& pattern_text) {
15 IPPattern pattern;
16 return pattern.ParsePattern(pattern_text);
19 bool CheckForMatch(const IPPattern& pattern, std::string address_text) {
20 IPAddressNumber address;
21 EXPECT_TRUE(ParseIPLiteralToNumber(address_text, &address));
22 return pattern.Match(address);
25 TEST(IPPatternTest, EmptyPattern) {
26 IPPattern pattern;
27 IPAddressNumber ipv4_address1;
28 EXPECT_TRUE(ParseIPLiteralToNumber("1.2.3.4", &ipv4_address1));
29 IPAddressNumber ipv6_address1;
30 EXPECT_TRUE(ParseIPLiteralToNumber("1:2:3:4:5:6:7:8", &ipv6_address1));
32 EXPECT_FALSE(pattern.Match(ipv4_address1));
33 EXPECT_FALSE(pattern.Match(ipv6_address1));
36 TEST(IPPatternTest, PerfectMatchPattern) {
37 IPPattern pattern_v4;
38 std::string ipv4_text1("1.2.3.4");
39 EXPECT_TRUE(pattern_v4.ParsePattern(ipv4_text1));
40 EXPECT_TRUE(pattern_v4.is_ipv4());
41 EXPECT_TRUE(CheckForMatch(pattern_v4, ipv4_text1));
43 IPPattern pattern_v6;
44 std::string ipv6_text1("1:2:3:4:5:6:7:8");
45 EXPECT_TRUE(pattern_v6.ParsePattern(ipv6_text1));
46 EXPECT_FALSE(pattern_v6.is_ipv4());
47 EXPECT_TRUE(CheckForMatch(pattern_v6, ipv6_text1));
49 // Also check that there is no confusion betwene v6 and v4, despite having
50 // similar values in some sense.
51 EXPECT_FALSE(CheckForMatch(pattern_v4, ipv6_text1));
52 EXPECT_FALSE(CheckForMatch(pattern_v6, ipv4_text1));
55 TEST(IPPatternTest, AlternativeMatchPattern) {
56 IPPattern pattern_v4;
57 EXPECT_TRUE(pattern_v4.ParsePattern("1.2.[3,5].4"));
58 EXPECT_TRUE(pattern_v4.is_ipv4());
59 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.2.4"));
60 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.3.4"));
61 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.4.4"));
62 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.5.4"));
63 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.6.4"));
65 IPPattern pattern_v6;
66 EXPECT_TRUE(pattern_v6.ParsePattern("1:2fab:3:4:5:[6,8]:7:8"));
67 EXPECT_FALSE(pattern_v6.is_ipv4());
68 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:5:7:8"));
69 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:6:7:8"));
70 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:7:7:8"));
71 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:8:7:8"));
72 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:9:7:8"));
75 TEST(IPPatternTest, RangeMatchPattern) {
76 IPPattern pattern_v4;
77 EXPECT_TRUE(pattern_v4.ParsePattern("1.2.[3-5].4"));
78 EXPECT_TRUE(pattern_v4.is_ipv4());
79 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.2.4"));
80 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.3.4"));
81 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.4.4"));
82 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.5.4"));
83 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.6.4"));
85 IPPattern pattern_v6;
86 EXPECT_TRUE(pattern_v6.ParsePattern("1:2fab:3:4:5:[6-8]:7:8"));
87 EXPECT_FALSE(pattern_v6.is_ipv4());
88 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:5:7:8"));
89 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:6:7:8"));
90 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:7:7:8"));
91 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:8:7:8"));
92 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:9:7:8"));
95 TEST(IPPatternTest, WildCardMatchPattern) {
96 // Use two ranges, and check that only getting both right is a match.
97 IPPattern pattern_v4;
98 EXPECT_TRUE(pattern_v4.ParsePattern("1.2.*.4"));
99 EXPECT_TRUE(pattern_v4.is_ipv4());
100 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.2.255"));
101 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.255.4"));
102 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.0.4"));
104 IPPattern pattern_v6;
105 EXPECT_TRUE(pattern_v6.ParsePattern("1:2fab:3:4:5:*:7:8"));
106 EXPECT_FALSE(pattern_v6.is_ipv4());
107 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:5:7:8888"));
108 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:FFFF:7:8"));
109 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:9999:7:8"));
112 TEST(IPPatternTest, MultiRangeMatchPattern) {
113 // Use two ranges, and check that only getting both right is a match.
114 // This ensures that the right component range is matched against the desired
115 // component.
116 IPPattern pattern_v4;
117 EXPECT_TRUE(pattern_v4.ParsePattern("1.[2-3].3.[4-5]"));
118 EXPECT_TRUE(pattern_v4.is_ipv4());
119 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.4.3.6"));
120 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.3.6"));
121 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.4.3.4"));
122 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.3.4"));
124 IPPattern pattern_v6;
125 EXPECT_TRUE(pattern_v6.ParsePattern("1:2fab:3:4:[5-7]:6:7:[8-A]"));
126 EXPECT_FALSE(pattern_v6.is_ipv4());
127 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:4:5:7:F"));
128 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:5:7:F"));
129 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:4:6:7:A"));
130 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:6:7:A"));
133 TEST(IPPatternTest, BytoOrderInIPv6Ranges) {
134 IPPattern pattern_v6_low_byte;
135 EXPECT_TRUE(pattern_v6_low_byte.ParsePattern("1:2:3:4:5:6:7:[0-FF]"));
136 EXPECT_TRUE(CheckForMatch(pattern_v6_low_byte, "1:2:3:4:5:6:7:0088"));
137 EXPECT_FALSE(CheckForMatch(pattern_v6_low_byte, "1:2:3:4:5:6:7:8800"));
139 IPPattern pattern_v6_high_byte;
140 EXPECT_TRUE(pattern_v6_high_byte.ParsePattern("1:2:3:4:5:6:7:[0-FF00]"));
141 EXPECT_TRUE(CheckForMatch(pattern_v6_high_byte, "1:2:3:4:5:6:7:0088"));
142 EXPECT_TRUE(CheckForMatch(pattern_v6_high_byte, "1:2:3:4:5:6:7:FF00"));
143 EXPECT_FALSE(CheckForMatch(pattern_v6_high_byte, "1:2:3:4:5:6:7:FF01"));
146 TEST(IPPatternTest, InvalidPatterns) {
147 EXPECT_FALSE(IsValidPattern("1:2:3:4:5:6:7:8:9")); // Too long.
148 EXPECT_FALSE(IsValidPattern("1:2:3:4:5:6:7")); // Too Short
149 EXPECT_FALSE(IsValidPattern("1:2:3:4:5:6:7:H")); // Non-hex.
150 EXPECT_FALSE(IsValidPattern("1:G:3:4:5:6:7:8")); // Non-hex.
152 EXPECT_FALSE(IsValidPattern("1.2.3.4.5")); // Too long
153 EXPECT_FALSE(IsValidPattern("1.2.3")); // Too short
154 EXPECT_FALSE(IsValidPattern("1.2.3.A")); // Non-decimal.
155 EXPECT_FALSE(IsValidPattern("1.A.3.4")); // Non-decimal
156 EXPECT_FALSE(IsValidPattern("1.256.3.4")); // Out of range
159 } // namespace
161 } // namespace net