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_mapping_rules.h"
7 #include "net/base/address_list.h"
8 #include "net/base/ip_endpoint.h"
9 #include "net/base/net_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
16 TEST(IPMappingRulesTest
, EmptyRules
) {
17 AddressList addresses
;
18 // Null rule does nothing to null list.
19 EXPECT_EQ(0u, addresses
.size());
21 EXPECT_FALSE(rules
.RewriteAddresses(&addresses
));
22 EXPECT_EQ(0u, addresses
.size());
24 IPAddressNumber address
;
25 EXPECT_TRUE(ParseIPLiteralToNumber("1.2.3.4", &address
));
27 IPEndPoint
endpoint(address
, port
);
28 addresses
.push_back(endpoint
);
29 // Null rule does nothing to a simple list.
30 ASSERT_EQ(1u, addresses
.size());
31 EXPECT_EQ(addresses
[0], endpoint
);
32 EXPECT_FALSE(rules
.RewriteAddresses(&addresses
));
33 ASSERT_EQ(1u, addresses
.size());
34 EXPECT_EQ(addresses
[0], endpoint
);
36 EXPECT_TRUE(ParseIPLiteralToNumber("1:2:3:4:a:b:c:def", &address
));
37 IPEndPoint
endpoint2(address
, port
);
38 addresses
.push_back(endpoint2
);
39 // Null rule does nothing to a simple list.
40 ASSERT_EQ(2u, addresses
.size());
41 EXPECT_EQ(addresses
[0], endpoint
);
42 EXPECT_EQ(addresses
[1], endpoint2
);
43 EXPECT_FALSE(rules
.RewriteAddresses(&addresses
));
44 ASSERT_EQ(2u, addresses
.size());
45 EXPECT_EQ(addresses
[0], endpoint
);
46 EXPECT_EQ(addresses
[1], endpoint2
);
49 // The |inserted_ip| must have a short final component, so that we can just
50 // append a digit, and still be a valid (but different) ip address.
51 void MatchingInsertionHelper(const std::string
& matchable_ip
,
52 const std::string
& matching_ip_pattern
,
53 const std::string
& inserted_ip
,
55 AddressList addresses
;
57 IPAddressNumber address
;
58 EXPECT_TRUE(ParseIPLiteralToNumber(matchable_ip
, &address
));
60 IPEndPoint
endpoint(address
, port
);
61 addresses
.push_back(endpoint
);
62 // Match the string with a precisely crafted pattern.
63 std::string
rule_text("Preface ");
64 rule_text
+= matching_ip_pattern
;
66 rule_text
+= inserted_ip
;
68 EXPECT_TRUE(rules
.AddRuleFromString(rule_text
));
70 ASSERT_EQ(1u, addresses
.size());
71 EXPECT_EQ(addresses
[0], endpoint
);
72 EXPECT_EQ(should_match
, rules
.RewriteAddresses(&addresses
));
74 ASSERT_EQ(1u, addresses
.size());
75 EXPECT_EQ(addresses
[0], endpoint
);
76 return; // Don't bother with the rest of the test.
79 ASSERT_EQ(2u, addresses
.size());
80 EXPECT_EQ(addresses
[1], endpoint
);
81 IPAddressNumber inserted_ip_adress_number
;
82 EXPECT_TRUE(ParseIPLiteralToNumber(inserted_ip
, &inserted_ip_adress_number
));
83 EXPECT_EQ(inserted_ip_adress_number
, addresses
[0].address());
85 // Now we have two addresses. We'll use new rules, to match the second
86 // address (the original address shifted over) in the list of addresses.
87 rule_text
= "Preface "; // Build up a new rule.
88 rule_text
+= matching_ip_pattern
;
90 // Change the inserted IP mildly.
91 std::string different_inserted_ip
= inserted_ip
+ "3";
92 rule_text
+= different_inserted_ip
;
93 // If we don't overwrite the old rule, it will fire first!
94 EXPECT_TRUE(rules
.SetRulesFromString(rule_text
)); // Overwrite old rules.
96 ASSERT_EQ(2u, addresses
.size());
97 EXPECT_EQ(addresses
[1], endpoint
);
98 EXPECT_TRUE(rules
.RewriteAddresses(&addresses
));
99 ASSERT_EQ(3u, addresses
.size());
100 EXPECT_EQ(addresses
[2], endpoint
);
101 EXPECT_TRUE(ParseIPLiteralToNumber(different_inserted_ip
,
102 &inserted_ip_adress_number
));
103 EXPECT_EQ(inserted_ip_adress_number
, addresses
[0].address());
106 TEST(IPMappingRulesTest
, SimpleMatchingInsertionIPV4
) {
107 std::string
matchable_ip("1.2.3.4");
108 std::string
matching_ip_pattern(matchable_ip
);
109 std::string
new_preface_ip("7.8.9.24");
110 bool should_match
= true;
111 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
115 TEST(IPMappingRulesTest
, SimpleMatchingInsertionIPV6
) {
116 std::string
matchable_ip("1:2:3:4:5:6:7:8");
117 std::string
matching_ip_pattern(matchable_ip
);
118 std::string
new_preface_ip("A:B:C:D:1:2:3:4");
119 bool should_match
= true;
120 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
124 TEST(IPMappingRulesTest
, SimpleMissatchingInsertionIPV4
) {
125 std::string
matchable_ip("1.2.3.4");
126 std::string
matching_ip_pattern(matchable_ip
+ "7");
127 std::string
new_preface_ip("7.8.9.24");
128 bool should_match
= false;
129 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
133 TEST(IPMappingRulesTest
, SimpleMisatchingInsertionIPV6
) {
134 std::string
matchable_ip("1:2:3:4:5:6:7:8");
135 std::string
matching_ip_pattern(matchable_ip
+ "e");
136 std::string
new_preface_ip("A:B:C:D:1:2:3:4");
137 bool should_match
= false;
138 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
142 TEST(IPMappingRulesTest
, AlternativeMatchingInsertionIPV4
) {
143 std::string
matchable_ip("1.2.3.4");
144 std::string
matching_ip_pattern("1.2.3.[2,4,6]");
145 std::string
new_preface_ip("7.8.9.24");
146 bool should_match
= true;
147 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
151 TEST(IPMappingRulesTest
, AlternativeMatchingInsertionIPV6
) {
152 std::string
matchable_ip("1:2:3:4:5:6:7:abcd");
153 std::string
matching_ip_pattern("1:2:3:4:5:6:7:[abc2,abc9,abcd,cdef]");
154 std::string
new_preface_ip("A:B:C:D:1:2:3:4");
155 bool should_match
= true;
156 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
160 TEST(IPMappingRulesTest
, RangeMatchingInsertionIPV4
) {
161 std::string
matchable_ip("1.2.3.4");
162 std::string
matching_ip_pattern("1.2.3.[2-6,22]");
163 std::string
new_preface_ip("7.8.9.24");
164 bool should_match
= true;
165 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
169 TEST(IPMappingRulesTest
, RandgeMatchingInsertionIPV6
) {
170 std::string
matchable_ip("1:2:3:4:5:6:7:abcd");
171 // Note: This test can detect confusion over high vs low-order bytes in an
172 // IPv6 component. If the code confused them, then this range would not
173 // include the matchable_ip.
174 std::string
matching_ip_pattern("1:2:3:4:5:6:7:[abc2-cdc2]");
175 std::string
new_preface_ip("A:B:C:D:1:2:3:4");
176 bool should_match
= true;
177 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
181 TEST(IPMappingRulesTest
, WildMatchingInsertionIPV4
) {
182 std::string
matchable_ip("1.2.3.4");
183 std::string
matching_ip_pattern("1.2.3.*");
184 std::string
new_preface_ip("7.8.9.24");
185 bool should_match
= true;
186 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
190 TEST(IPMappingRulesTest
, WildMatchingInsertionIPV6
) {
191 std::string
matchable_ip("1:2:3:4:5:6:7:abcd");
192 // Note: This test can detect confusion over high vs low-order bytes in an
193 // IPv6 component. If the code confused them, then this range would not
194 // include the matchable_ip.
195 std::string
matching_ip_pattern("1:2:3:4:5:6:7:*");
196 std::string
new_preface_ip("A:B:C:D:1:2:3:4");
197 bool should_match
= true;
198 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
202 TEST(IPMappingRulesTest
, WildNotMatchingInsertionIPV4
) {
203 std::string
matchable_ip("1.2.3.4");
204 std::string
matching_ip_pattern("*.200.*.*");
205 std::string
new_preface_ip("7.8.9.24");
206 bool should_match
= false;
207 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
211 TEST(IPMappingRulesTest
, WildNotMatchingInsertionIPV6
) {
212 std::string
matchable_ip("1:2:3:4:5:6:7:8");
213 // Note: This test can detect confusion over high vs low-order bytes in an
214 // IPv6 component. If the code confused them, then this range would not
215 // include the matchable_ip.
216 std::string
matching_ip_pattern("*:*:37af:*:*:*:*:*");
217 std::string
new_preface_ip("A:B:C:D:1:2:3:4");
218 bool should_match
= false;
219 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
223 TEST(IPMappingRulesTest
, IPv4NotMatchForIPV6
) {
224 std::string
matchable_ip("1:2:3:4:5:6:7:abcd");
225 // Note: This test can detect confusion over high vs low-order bytes in an
226 // IPv6 component. If the code confused them, then this range would not
227 // include the matchable_ip.
228 std::string
matching_ip_pattern("1.2.3.4");
229 std::string
new_preface_ip("10.20.30.40"); // Compatible with pattern.
230 bool should_match
= false;
231 MatchingInsertionHelper(matchable_ip
, matching_ip_pattern
, new_preface_ip
,
235 // Parsing bad rules should silently discard the rule (and never crash).
236 TEST(IPMappingRulesTest
, ParseInvalidRules
) {
237 IPMappingRules rules
;
239 EXPECT_FALSE(rules
.AddRuleFromString("Too short"));
240 EXPECT_FALSE(rules
.AddRuleFromString("Preface much too long"));
241 EXPECT_FALSE(rules
.AddRuleFromString("PrefaceNotSpelled 1.2.3.4 1.2.3.4"));
244 EXPECT_FALSE(rules
.AddRuleFromString("Preface 1.2.a.4 1.2.3.4"));
245 EXPECT_FALSE(rules
.AddRuleFromString("Preface 1.2.3.4.5 1.2.3.4"));
246 EXPECT_FALSE(rules
.AddRuleFromString("Preface 1.2.3.4 1.2.3.4.5"));
247 EXPECT_FALSE(rules
.AddRuleFromString("Preface 1.2.3.4 1.2.3.4-5"));
248 EXPECT_FALSE(rules
.AddRuleFromString("Preface 1.2.3.4-5-6 1.2.3.4"));
251 EXPECT_FALSE(rules
.AddRuleFromString(
252 "Preface 1:2:3:4:5:6:7:g 1:2:3:4:5:6:7:8"));
253 EXPECT_FALSE(rules
.AddRuleFromString(
254 "Preface 1:2:3:4:5:6:7:8 1:g:3:4:5:6:7:8"));
255 EXPECT_FALSE(rules
.AddRuleFromString(
256 "Preface 1:2:3:4:5:6:7:8:9 1:2:3:4:5:6:7:8"));
257 EXPECT_FALSE(rules
.AddRuleFromString(
258 "Preface 1:2:3:4:5:6:7:8 1:2:3:4:5:6:7:8:0"));
259 EXPECT_FALSE(rules
.AddRuleFromString(
260 "Preface 1:2:3:4:5:6:7:8 1:2:3:4:5:6:7:8-A"));
261 EXPECT_FALSE(rules
.AddRuleFromString(
262 "Preface 1:2:3:4:5:6:7:8-A-B 1:2:3:4:5:6:7:8"));
264 // Don't mix ipv4 and ipv6.
265 EXPECT_FALSE(rules
.AddRuleFromString("Preface 1.2.3.4 1:2:3:4:5:6:7:8"));
267 EXPECT_FALSE(rules
.SetRulesFromString("Preface 1.2.3.4 5.6.7.8; bad"));
271 TEST(IPMappingRulesTest
, FirstRuleToMatch
) {
272 std::string
first_replacement("1.1.1.1");
273 IPAddressNumber first_replacement_address
;
274 EXPECT_TRUE(ParseIPLiteralToNumber(first_replacement
,
275 &first_replacement_address
));
277 std::string
second_replacement("2.2.2.2");
278 IPAddressNumber second_replacement_address
;
279 EXPECT_TRUE(ParseIPLiteralToNumber(second_replacement
,
280 &second_replacement_address
));
282 IPMappingRules rules
;
284 rules
.SetRulesFromString("Preface *.*.*.* " + first_replacement
+
285 ";Preface *.*.*.* " + second_replacement
));
287 IPAddressNumber address
;
288 EXPECT_TRUE(ParseIPLiteralToNumber("7.7.7.7", &address
));
290 IPEndPoint
endpoint(address
, port
);
291 AddressList addresses
;
292 addresses
.push_back(endpoint
);
294 ASSERT_EQ(1u, addresses
.size());
295 EXPECT_EQ(addresses
[0], endpoint
);
296 EXPECT_TRUE(rules
.RewriteAddresses(&addresses
));
297 ASSERT_EQ(2u, addresses
.size());
298 EXPECT_EQ(addresses
[1], endpoint
);
299 // The last rule added to the list has the highest priority (overriding
300 // previous rules, and matching first).
301 EXPECT_NE(addresses
[0].address(), first_replacement_address
);
302 EXPECT_EQ(addresses
[0].address(), second_replacement_address
);