1 // Copyright (c) 2010 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/host_mapping_rules.h"
7 #include "net/base/host_port_pair.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(HostMappingRulesTest
, SetRulesFromString
) {
15 HostMappingRules rules
;
16 rules
.SetRulesFromString(
17 "map *.com baz , map *.net bar:60, EXCLUDE *.foo.com");
19 HostPortPair
host_port("test", 1234);
20 EXPECT_FALSE(rules
.RewriteHost(&host_port
));
21 EXPECT_EQ("test", host_port
.host());
22 EXPECT_EQ(1234u, host_port
.port());
24 host_port
= HostPortPair("chrome.net", 80);
25 EXPECT_TRUE(rules
.RewriteHost(&host_port
));
26 EXPECT_EQ("bar", host_port
.host());
27 EXPECT_EQ(60u, host_port
.port());
29 host_port
= HostPortPair("crack.com", 80);
30 EXPECT_TRUE(rules
.RewriteHost(&host_port
));
31 EXPECT_EQ("baz", host_port
.host());
32 EXPECT_EQ(80u, host_port
.port());
34 host_port
= HostPortPair("wtf.foo.com", 666);
35 EXPECT_FALSE(rules
.RewriteHost(&host_port
));
36 EXPECT_EQ("wtf.foo.com", host_port
.host());
37 EXPECT_EQ(666u, host_port
.port());
40 TEST(HostMappingRulesTest
, PortSpecificMatching
) {
41 HostMappingRules rules
;
42 rules
.SetRulesFromString(
43 "map *.com:80 baz:111 , map *.com:443 blat:333, EXCLUDE *.foo.com");
46 HostPortPair
host_port("test.com", 1234);
47 EXPECT_FALSE(rules
.RewriteHost(&host_port
));
48 EXPECT_EQ("test.com", host_port
.host());
49 EXPECT_EQ(1234u, host_port
.port());
52 host_port
= HostPortPair("crack.com", 80);
53 EXPECT_TRUE(rules
.RewriteHost(&host_port
));
54 EXPECT_EQ("baz", host_port
.host());
55 EXPECT_EQ(111u, host_port
.port());
58 host_port
= HostPortPair("wtf.com", 443);
59 EXPECT_TRUE(rules
.RewriteHost(&host_port
));
60 EXPECT_EQ("blat", host_port
.host());
61 EXPECT_EQ(333u, host_port
.port());
63 // Match port 443, but excluded.
64 host_port
= HostPortPair("wtf.foo.com", 443);
65 EXPECT_FALSE(rules
.RewriteHost(&host_port
));
66 EXPECT_EQ("wtf.foo.com", host_port
.host());
67 EXPECT_EQ(443u, host_port
.port());
70 // Parsing bad rules should silently discard the rule (and never crash).
71 TEST(HostMappingRulesTest
, ParseInvalidRules
) {
72 HostMappingRules rules
;
74 EXPECT_FALSE(rules
.AddRuleFromString("xyz"));
75 EXPECT_FALSE(rules
.AddRuleFromString(std::string()));
76 EXPECT_FALSE(rules
.AddRuleFromString(" "));
77 EXPECT_FALSE(rules
.AddRuleFromString("EXCLUDE"));
78 EXPECT_FALSE(rules
.AddRuleFromString("EXCLUDE foo bar"));
79 EXPECT_FALSE(rules
.AddRuleFromString("INCLUDE"));
80 EXPECT_FALSE(rules
.AddRuleFromString("INCLUDE x"));
81 EXPECT_FALSE(rules
.AddRuleFromString("INCLUDE x :10"));