[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "net/base/host_mapping_rules.h" |
| 6 | |
| 7 | #include "net/base/host_port_pair.h" |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
| 10 | namespace net { |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | TEST(HostMappingRulesTest, SetRulesFromString) { |
| 15 | HostMappingRules rules; |
| 16 | rules.SetRulesFromString( |
| 17 | "map *.com baz , map *.net bar:60, EXCLUDE *.foo.com"); |
| 18 | |
| 19 | HostPortPair host_port("test", 1234); |
| 20 | EXPECT_FALSE(rules.RewriteHost(&host_port)); |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame^] | 21 | EXPECT_EQ("test", host_port.host()); |
| 22 | EXPECT_EQ(1234u, host_port.port()); |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 23 | |
| 24 | host_port = HostPortPair("chrome.net", 80); |
| 25 | EXPECT_TRUE(rules.RewriteHost(&host_port)); |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame^] | 26 | EXPECT_EQ("bar", host_port.host()); |
| 27 | EXPECT_EQ(60u, host_port.port()); |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 28 | |
| 29 | host_port = HostPortPair("crack.com", 80); |
| 30 | EXPECT_TRUE(rules.RewriteHost(&host_port)); |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame^] | 31 | EXPECT_EQ("baz", host_port.host()); |
| 32 | EXPECT_EQ(80u, host_port.port()); |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 33 | |
| 34 | host_port = HostPortPair("wtf.foo.com", 666); |
| 35 | EXPECT_FALSE(rules.RewriteHost(&host_port)); |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame^] | 36 | EXPECT_EQ("wtf.foo.com", host_port.host()); |
| 37 | EXPECT_EQ(666u, host_port.port()); |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Parsing bad rules should silently discard the rule (and never crash). |
| 41 | TEST(HostMappingRulesTest, ParseInvalidRules) { |
| 42 | HostMappingRules rules; |
| 43 | |
| 44 | EXPECT_FALSE(rules.AddRuleFromString("xyz")); |
| 45 | EXPECT_FALSE(rules.AddRuleFromString("")); |
| 46 | EXPECT_FALSE(rules.AddRuleFromString(" ")); |
| 47 | EXPECT_FALSE(rules.AddRuleFromString("EXCLUDE")); |
| 48 | EXPECT_FALSE(rules.AddRuleFromString("EXCLUDE foo bar")); |
| 49 | EXPECT_FALSE(rules.AddRuleFromString("INCLUDE")); |
| 50 | EXPECT_FALSE(rules.AddRuleFromString("INCLUDE x")); |
| 51 | EXPECT_FALSE(rules.AddRuleFromString("INCLUDE x :10")); |
| 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | |
| 56 | } // namespace net |