1 // Copyright 2020 The Chromium Authors
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/scheme_host_port_matcher.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace net {
10
11 namespace {
12
TEST(SchemeHostPortMatcherTest,ParseMultipleRules)13 TEST(SchemeHostPortMatcherTest, ParseMultipleRules) {
14 SchemeHostPortMatcher matcher =
15 SchemeHostPortMatcher::FromRawString(".google.com , .foobar.com:30");
16 EXPECT_EQ(2u, matcher.rules().size());
17
18 EXPECT_TRUE(matcher.Includes(GURL("http://baz.google.com:40")));
19 EXPECT_FALSE(matcher.Includes(GURL("http://google.com:40")));
20 EXPECT_TRUE(matcher.Includes(GURL("http://bar.foobar.com:30")));
21 EXPECT_FALSE(matcher.Includes(GURL("http://bar.foobar.com")));
22 EXPECT_FALSE(matcher.Includes(GURL("http://bar.foobar.com:33")));
23 }
24
TEST(SchemeHostPortMatcherTest,WithBadInputs)25 TEST(SchemeHostPortMatcherTest, WithBadInputs) {
26 SchemeHostPortMatcher matcher = SchemeHostPortMatcher::FromRawString(
27 ":// , , .google.com , , http://baz");
28
29 EXPECT_EQ(2u, matcher.rules().size());
30 EXPECT_EQ("*.google.com", matcher.rules()[0]->ToString());
31 EXPECT_EQ("http://baz", matcher.rules()[1]->ToString());
32
33 EXPECT_TRUE(matcher.Includes(GURL("http://baz.google.com:40")));
34 EXPECT_TRUE(matcher.Includes(GURL("http://baz")));
35 EXPECT_FALSE(matcher.Includes(GURL("http://google.com")));
36 }
37
38 // Tests that URLMatcher does not include logic specific to ProxyBypassRules.
39 // * Should not implicitly bypass localhost or link-local addresses
40 // * Should not match proxy bypass specific rules like <-loopback> and <local>
41 //
42 // Historically, SchemeHostPortMatcher was refactored out of ProxyBypassRules.
43 // This test confirms that the layering separation is as expected.
TEST(SchemeHostPortMatcherTest,DoesNotMimicProxyBypassRules)44 TEST(SchemeHostPortMatcherTest, DoesNotMimicProxyBypassRules) {
45 // Should not parse <-loopback> as its own rule (will treat it as a hostname
46 // rule).
47 SchemeHostPortMatcher matcher =
48 SchemeHostPortMatcher::FromRawString("<-loopback>");
49 EXPECT_EQ(1u, matcher.rules().size());
50 EXPECT_EQ("<-loopback>", matcher.rules().front()->ToString());
51
52 // Should not parse <local> as its own rule (will treat it as a hostname
53 // rule).
54 matcher = SchemeHostPortMatcher::FromRawString("<local>");
55 EXPECT_EQ(1u, matcher.rules().size());
56 EXPECT_EQ("<local>", matcher.rules().front()->ToString());
57
58 // Should not implicitly match localhost or link-local addresses.
59 matcher = SchemeHostPortMatcher::FromRawString("www.example.com");
60 EXPECT_EQ(SchemeHostPortMatcherResult::kNoMatch,
61 matcher.Evaluate(GURL("http://localhost")));
62 EXPECT_EQ(SchemeHostPortMatcherResult::kNoMatch,
63 matcher.Evaluate(GURL("http://169.254.1.1")));
64 }
65
66 } // namespace
67
68 } // namespace net
69