aw: Remove aw_switches
[chromium-blink-merge.git] / net / base / ip_pattern_unittest.cc
blobd1bbdb286ada87bb5b551235519802b60dc8dc07
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 "testing/gtest/include/gtest/gtest.h"
9 namespace net {
11 namespace {
13 bool IsValidPattern(const std::string& pattern_text) {
14 IPPattern pattern;
15 return pattern.ParsePattern(pattern_text);
18 bool CheckForMatch(const IPPattern& pattern, std::string address_text) {
19 IPAddressNumber address;
20 EXPECT_TRUE(ParseIPLiteralToNumber(address_text, &address));
21 return pattern.Match(address);
24 TEST(IPPatternTest, EmptyPattern) {
25 IPPattern pattern;
26 IPAddressNumber ipv4_address1;
27 EXPECT_TRUE(ParseIPLiteralToNumber("1.2.3.4", &ipv4_address1));
28 IPAddressNumber ipv6_address1;
29 EXPECT_TRUE(ParseIPLiteralToNumber("1:2:3:4:5:6:7:8", &ipv6_address1));
31 EXPECT_FALSE(pattern.Match(ipv4_address1));
32 EXPECT_FALSE(pattern.Match(ipv6_address1));
35 TEST(IPPatternTest, PerfectMatchPattern) {
36 IPPattern pattern_v4;
37 std::string ipv4_text1("1.2.3.4");
38 EXPECT_TRUE(pattern_v4.ParsePattern(ipv4_text1));
39 EXPECT_TRUE(pattern_v4.is_ipv4());
40 EXPECT_TRUE(CheckForMatch(pattern_v4, ipv4_text1));
42 IPPattern pattern_v6;
43 std::string ipv6_text1("1:2:3:4:5:6:7:8");
44 EXPECT_TRUE(pattern_v6.ParsePattern(ipv6_text1));
45 EXPECT_FALSE(pattern_v6.is_ipv4());
46 EXPECT_TRUE(CheckForMatch(pattern_v6, ipv6_text1));
48 // Also check that there is no confusion betwene v6 and v4, despite having
49 // similar values in some sense.
50 EXPECT_FALSE(CheckForMatch(pattern_v4, ipv6_text1));
51 EXPECT_FALSE(CheckForMatch(pattern_v6, ipv4_text1));
54 TEST(IPPatternTest, AlternativeMatchPattern) {
55 IPPattern pattern_v4;
56 EXPECT_TRUE(pattern_v4.ParsePattern("1.2.[3,5].4"));
57 EXPECT_TRUE(pattern_v4.is_ipv4());
58 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.2.4"));
59 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.3.4"));
60 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.4.4"));
61 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.5.4"));
62 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.6.4"));
64 IPPattern pattern_v6;
65 EXPECT_TRUE(pattern_v6.ParsePattern("1:2fab:3:4:5:[6,8]:7:8"));
66 EXPECT_FALSE(pattern_v6.is_ipv4());
67 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:5:7:8"));
68 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:6:7:8"));
69 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:7:7:8"));
70 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:8:7:8"));
71 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:9:7:8"));
74 TEST(IPPatternTest, RangeMatchPattern) {
75 IPPattern pattern_v4;
76 EXPECT_TRUE(pattern_v4.ParsePattern("1.2.[3-5].4"));
77 EXPECT_TRUE(pattern_v4.is_ipv4());
78 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.2.4"));
79 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.3.4"));
80 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.4.4"));
81 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.5.4"));
82 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.6.4"));
84 IPPattern pattern_v6;
85 EXPECT_TRUE(pattern_v6.ParsePattern("1:2fab:3:4:5:[6-8]:7:8"));
86 EXPECT_FALSE(pattern_v6.is_ipv4());
87 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:5:7:8"));
88 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:6:7:8"));
89 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:7:7:8"));
90 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:8:7:8"));
91 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:9:7:8"));
94 TEST(IPPatternTest, WildCardMatchPattern) {
95 // Use two ranges, and check that only getting both right is a match.
96 IPPattern pattern_v4;
97 EXPECT_TRUE(pattern_v4.ParsePattern("1.2.*.4"));
98 EXPECT_TRUE(pattern_v4.is_ipv4());
99 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.2.255"));
100 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.255.4"));
101 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.0.4"));
103 IPPattern pattern_v6;
104 EXPECT_TRUE(pattern_v6.ParsePattern("1:2fab:3:4:5:*:7:8"));
105 EXPECT_FALSE(pattern_v6.is_ipv4());
106 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:5:7:8888"));
107 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:FFFF:7:8"));
108 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:9999:7:8"));
111 TEST(IPPatternTest, MultiRangeMatchPattern) {
112 // Use two ranges, and check that only getting both right is a match.
113 // This ensures that the right component range is matched against the desired
114 // component.
115 IPPattern pattern_v4;
116 EXPECT_TRUE(pattern_v4.ParsePattern("1.[2-3].3.[4-5]"));
117 EXPECT_TRUE(pattern_v4.is_ipv4());
118 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.4.3.6"));
119 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.2.3.6"));
120 EXPECT_FALSE(CheckForMatch(pattern_v4, "1.4.3.4"));
121 EXPECT_TRUE(CheckForMatch(pattern_v4, "1.2.3.4"));
123 IPPattern pattern_v6;
124 EXPECT_TRUE(pattern_v6.ParsePattern("1:2fab:3:4:[5-7]:6:7:[8-A]"));
125 EXPECT_FALSE(pattern_v6.is_ipv4());
126 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:4:5:7:F"));
127 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:5:7:F"));
128 EXPECT_FALSE(CheckForMatch(pattern_v6, "1:2fab:3:4:4:6:7:A"));
129 EXPECT_TRUE(CheckForMatch(pattern_v6, "1:2fab:3:4:5:6:7:A"));
132 TEST(IPPatternTest, BytoOrderInIPv6Ranges) {
133 IPPattern pattern_v6_low_byte;
134 EXPECT_TRUE(pattern_v6_low_byte.ParsePattern("1:2:3:4:5:6:7:[0-FF]"));
135 EXPECT_TRUE(CheckForMatch(pattern_v6_low_byte, "1:2:3:4:5:6:7:0088"));
136 EXPECT_FALSE(CheckForMatch(pattern_v6_low_byte, "1:2:3:4:5:6:7:8800"));
138 IPPattern pattern_v6_high_byte;
139 EXPECT_TRUE(pattern_v6_high_byte.ParsePattern("1:2:3:4:5:6:7:[0-FF00]"));
140 EXPECT_TRUE(CheckForMatch(pattern_v6_high_byte, "1:2:3:4:5:6:7:0088"));
141 EXPECT_TRUE(CheckForMatch(pattern_v6_high_byte, "1:2:3:4:5:6:7:FF00"));
142 EXPECT_FALSE(CheckForMatch(pattern_v6_high_byte, "1:2:3:4:5:6:7:FF01"));
145 TEST(IPPatternTest, InvalidPatterns) {
146 EXPECT_FALSE(IsValidPattern("1:2:3:4:5:6:7:8:9")); // Too long.
147 EXPECT_FALSE(IsValidPattern("1:2:3:4:5:6:7")); // Too Short
148 EXPECT_FALSE(IsValidPattern("1:2:3:4:5:6:7:H")); // Non-hex.
149 EXPECT_FALSE(IsValidPattern("1:G:3:4:5:6:7:8")); // Non-hex.
151 EXPECT_FALSE(IsValidPattern("1.2.3.4.5")); // Too long
152 EXPECT_FALSE(IsValidPattern("1.2.3")); // Too short
153 EXPECT_FALSE(IsValidPattern("1.2.3.A")); // Non-decimal.
154 EXPECT_FALSE(IsValidPattern("1.A.3.4")); // Non-decimal
155 EXPECT_FALSE(IsValidPattern("1.256.3.4")); // Out of range
158 } // namespace
160 } // namespace net