scheme_host_port_matcher_unittest.cc 2.7 KB

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