pkalinnikov | 15cf724 | 2016-07-13 08:57:34 | [diff] [blame] | 1 | // Copyright 2016 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 | |
Pavel Kalinnikov | d797063 | 2017-06-20 09:07:34 | [diff] [blame] | 5 | #include "components/url_pattern_index/url_pattern.h" |
pkalinnikov | 35d1881 | 2017-04-05 17:28:18 | [diff] [blame] | 6 | |
pkalinnikov | 15cf724 | 2016-07-13 08:57:34 | [diff] [blame] | 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | #include "url/gurl.h" |
| 9 | |
Pavel Kalinnikov | d797063 | 2017-06-20 09:07:34 | [diff] [blame] | 10 | namespace url_pattern_index { |
pkalinnikov | 15cf724 | 2016-07-13 08:57:34 | [diff] [blame] | 11 | |
| 12 | namespace { |
| 13 | |
| 14 | constexpr proto::AnchorType kAnchorNone = proto::ANCHOR_TYPE_NONE; |
| 15 | constexpr proto::AnchorType kBoundary = proto::ANCHOR_TYPE_BOUNDARY; |
| 16 | constexpr proto::AnchorType kSubdomain = proto::ANCHOR_TYPE_SUBDOMAIN; |
| 17 | |
| 18 | } // namespace |
| 19 | |
pkalinnikov | 35d1881 | 2017-04-05 17:28:18 | [diff] [blame] | 20 | TEST(SubresourceFilterUrlPatternTest, MatchesUrl) { |
pkalinnikov | 15cf724 | 2016-07-13 08:57:34 | [diff] [blame] | 21 | const struct { |
| 22 | UrlPattern url_pattern; |
| 23 | const char* url; |
| 24 | bool expect_match; |
| 25 | } kTestCases[] = { |
pkalinnikov | 35d1881 | 2017-04-05 17:28:18 | [diff] [blame] | 26 | {{"", proto::URL_PATTERN_TYPE_SUBSTRING}, "http://ex.com/", true}, |
| 27 | {{"", proto::URL_PATTERN_TYPE_WILDCARDED}, "http://ex.com/", true}, |
| 28 | {{"", kBoundary, kAnchorNone}, "http://ex.com/", true}, |
| 29 | {{"", kSubdomain, kAnchorNone}, "http://ex.com/", true}, |
| 30 | {{"", kSubdomain, kAnchorNone}, "http://ex.com/", true}, |
| 31 | {{"^", kSubdomain, kAnchorNone}, "http://ex.com/", false}, |
| 32 | {{".", kSubdomain, kAnchorNone}, "http://ex.com/", false}, |
| 33 | {{"", kAnchorNone, kBoundary}, "http://ex.com/", true}, |
| 34 | {{"^", kAnchorNone, kBoundary}, "http://ex.com/", true}, |
| 35 | {{".", kAnchorNone, kBoundary}, "http://ex.com/", false}, |
| 36 | {{"", kBoundary, kBoundary}, "http://ex.com/", false}, |
| 37 | {{"", kSubdomain, kBoundary}, "http://ex.com/", false}, |
| 38 | {{"com/", kSubdomain, kBoundary}, "http://ex.com/", true}, |
| 39 | |
pkalinnikov | 15cf724 | 2016-07-13 08:57:34 | [diff] [blame] | 40 | {{"xampl", proto::URL_PATTERN_TYPE_SUBSTRING}, |
| 41 | "http://example.com", |
| 42 | true}, |
| 43 | {{"example", proto::URL_PATTERN_TYPE_SUBSTRING}, |
| 44 | "http://example.com", |
| 45 | true}, |
| 46 | {{"/a?a"}, "http://ex.com/a?a", true}, |
| 47 | {{"^abc"}, "http://ex.com/abc?a", true}, |
| 48 | {{"^abc"}, "http://ex.com/a?abc", true}, |
| 49 | {{"^abc"}, "http://ex.com/abc?abc", true}, |
pkalinnikov | 35d1881 | 2017-04-05 17:28:18 | [diff] [blame] | 50 | {{"^abc^abc"}, "http://ex.com/abc?abc", true}, |
| 51 | {{"^com^abc^abc"}, "http://ex.com/abc?abc", false}, |
pkalinnikov | 15cf724 | 2016-07-13 08:57:34 | [diff] [blame] | 52 | |
| 53 | {{"http://ex", kBoundary, kAnchorNone}, "http://example.com", true}, |
pkalinnikov | 35d1881 | 2017-04-05 17:28:18 | [diff] [blame] | 54 | {{"http://ex", kAnchorNone, kAnchorNone}, "http://example.com", true}, |
pkalinnikov | 15cf724 | 2016-07-13 08:57:34 | [diff] [blame] | 55 | {{"mple.com/", kAnchorNone, kBoundary}, "http://example.com", true}, |
pkalinnikov | 35d1881 | 2017-04-05 17:28:18 | [diff] [blame] | 56 | {{"mple.com/", kAnchorNone, kAnchorNone}, "http://example.com", true}, |
| 57 | {{"mple.com/", kSubdomain, kAnchorNone}, "http://example.com", false}, |
| 58 | {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.com", false}, |
| 59 | {{"ex.com", kSubdomain, kAnchorNone}, "http://ex.com", true}, |
| 60 | {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.ex.com", true}, |
| 61 | {{"ex.com", kSubdomain, kAnchorNone}, "http://hex.hex.com", false}, |
pkalinnikov | 15cf724 | 2016-07-13 08:57:34 | [diff] [blame] | 62 | |
| 63 | // Note: "example.com" will be normalized into "example.com/". |
| 64 | {{"http://*mpl", kBoundary, kAnchorNone}, "http://example.com", true}, |
| 65 | {{"mpl*com/", kAnchorNone, kBoundary}, "http://example.com", true}, |
| 66 | {{"example^com"}, "http://example.com", false}, |
| 67 | {{"example^com"}, "http://example/com", true}, |
| 68 | {{"example.com^"}, "http://example.com:8080", true}, |
| 69 | {{"http*.com/", kBoundary, kBoundary}, "http://example.com", true}, |
| 70 | {{"http*.org/", kBoundary, kBoundary}, "http://example.com", false}, |
| 71 | |
| 72 | {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path/bbb?k=v&p1=0&p2=1", false}, |
| 73 | {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path?k=v&p1=0&p2=1", true}, |
| 74 | {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path?k=v&k=v&p1=0&p2=1", true}, |
| 75 | {{"/path?*&p1=*&p2="}, |
| 76 | "http://ex.com/aaa/path?k=v&p1=0&p3=10&p2=1", |
| 77 | true}, |
| 78 | {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path&p1=0&p2=1", false}, |
| 79 | {{"/path?*&p1=*&p2="}, "http://ex.com/aaa/path?k=v&p2=0&p1=1", false}, |
| 80 | |
| 81 | {{"abc*def*ghijk*xyz"}, |
| 82 | "http://example.com/abcdeffffghijkmmmxyzzz", |
| 83 | true}, |
| 84 | {{"abc*cdef"}, "http://example.com/abcdef", false}, |
| 85 | |
| 86 | {{"^^a^^"}, "http://ex.com/?a=/", true}, |
| 87 | {{"^^a^^"}, "http://ex.com/?a=/&b=0", true}, |
| 88 | {{"^^a^^"}, "http://ex.com/?a=", false}, |
| 89 | |
| 90 | {{"ex.com^path^*k=v^"}, "http://ex.com/path/?k1=v1&ak=v&kk=vv", true}, |
| 91 | {{"ex.com^path^*k=v^"}, "http://ex.com/p/path/?k1=v1&ak=v&kk=vv", false}, |
| 92 | {{"a^a&a^a&"}, "http://ex.com/a/a/a/a/?a&a&a&a&a", true}, |
| 93 | |
| 94 | {{"abc*def^"}, "http://ex.com/abc/a/ddef/", true}, |
pkalinnikov | 854818d6 | 2016-07-22 11:55:10 | [diff] [blame] | 95 | |
| 96 | {{"https://example.com/"}, "http://example.com/", false}, |
| 97 | {{"example.com/", kSubdomain, kAnchorNone}, "http://example.com/", true}, |
| 98 | {{"examp", kSubdomain, kAnchorNone}, "http://example.com/", true}, |
| 99 | {{"xamp", kSubdomain, kAnchorNone}, "http://example.com/", false}, |
| 100 | {{"examp", kSubdomain, kAnchorNone}, "http://test.example.com/", true}, |
| 101 | {{"t.examp", kSubdomain, kAnchorNone}, "http://test.example.com/", false}, |
| 102 | {{"com^", kSubdomain, kAnchorNone}, "http://test.example.com/", true}, |
| 103 | {{"x.com", kSubdomain, kAnchorNone}, "http://ex.com/?url=x.com", false}, |
| 104 | {{"ex.com/", kSubdomain, kBoundary}, "http://ex.com/", true}, |
| 105 | {{"ex.com^", kSubdomain, kBoundary}, "http://ex.com/", true}, |
| 106 | {{"ex.co", kSubdomain, kBoundary}, "http://ex.com/", false}, |
| 107 | {{"ex.com", kSubdomain, kBoundary}, "http://rex.com.ex.com/", false}, |
| 108 | {{"ex.com/", kSubdomain, kBoundary}, "http://rex.com.ex.com/", true}, |
| 109 | {{"http", kSubdomain, kBoundary}, "http://http.com/", false}, |
| 110 | {{"http", kSubdomain, kAnchorNone}, "http://http.com/", true}, |
| 111 | {{"/example.com", kSubdomain, kBoundary}, "http://example.com/", false}, |
| 112 | {{"/example.com/", kSubdomain, kBoundary}, "http://example.com/", false}, |
pkalinnikov | 15cf724 | 2016-07-13 08:57:34 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | for (const auto& test_case : kTestCases) { |
pkalinnikov | 35d1881 | 2017-04-05 17:28:18 | [diff] [blame] | 116 | SCOPED_TRACE(testing::Message() << "Rule: " << test_case.url_pattern |
| 117 | << "; URL: " << GURL(test_case.url)); |
pkalinnikov | 15cf724 | 2016-07-13 08:57:34 | [diff] [blame] | 118 | |
pkalinnikov | 35d1881 | 2017-04-05 17:28:18 | [diff] [blame] | 119 | const bool is_match = test_case.url_pattern.MatchesUrl(GURL(test_case.url)); |
pkalinnikov | 15cf724 | 2016-07-13 08:57:34 | [diff] [blame] | 120 | EXPECT_EQ(test_case.expect_match, is_match); |
| 121 | } |
| 122 | } |
| 123 | |
Pavel Kalinnikov | d797063 | 2017-06-20 09:07:34 | [diff] [blame] | 124 | } // namespace url_pattern_index |