blob: 09e7f4539f3b4dd05eed204bca5e4406fb8edcdb [file] [log] [blame]
[email protected]b6a50182010-05-12 22:47:141// 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
10namespace net {
11
12namespace {
13
14TEST(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]2fbaecf22010-07-22 22:20:3521 EXPECT_EQ("test", host_port.host());
22 EXPECT_EQ(1234u, host_port.port());
[email protected]b6a50182010-05-12 22:47:1423
24 host_port = HostPortPair("chrome.net", 80);
25 EXPECT_TRUE(rules.RewriteHost(&host_port));
[email protected]2fbaecf22010-07-22 22:20:3526 EXPECT_EQ("bar", host_port.host());
27 EXPECT_EQ(60u, host_port.port());
[email protected]b6a50182010-05-12 22:47:1428
29 host_port = HostPortPair("crack.com", 80);
30 EXPECT_TRUE(rules.RewriteHost(&host_port));
[email protected]2fbaecf22010-07-22 22:20:3531 EXPECT_EQ("baz", host_port.host());
32 EXPECT_EQ(80u, host_port.port());
[email protected]b6a50182010-05-12 22:47:1433
34 host_port = HostPortPair("wtf.foo.com", 666);
35 EXPECT_FALSE(rules.RewriteHost(&host_port));
[email protected]2fbaecf22010-07-22 22:20:3536 EXPECT_EQ("wtf.foo.com", host_port.host());
37 EXPECT_EQ(666u, host_port.port());
[email protected]b6a50182010-05-12 22:47:1438}
39
40// Parsing bad rules should silently discard the rule (and never crash).
41TEST(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