Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: url/origin_filter_unittest.cc

Issue 1603903002: Add an OriginFilterBuilder class for [white|black]listing origins. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "url/origin_filter.h"
6
7 #include <algorithm>
8 #include <vector>
9
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "url/gurl.h"
12 #include "url/origin.h"
13
14 namespace url {
15
16 TEST(OriginFilterTest, Empty) {
17 // An empty filter matches everything.
18 scoped_ptr<OriginFilter> filter = OriginFilter::Empty();
19 EXPECT_TRUE(filter->MatchesURL(GURL("https://www.google.com")));
20 EXPECT_TRUE(filter->MatchesURL(GURL("https://www.chrome.com")));
21 EXPECT_TRUE(filter->MatchesURL(GURL("invalid url spec")));
22 }
23
24 TEST(OriginFilterTest, Whitelist) {
25 std::vector<Origin> whitelist;
26 whitelist.push_back(Origin(GURL("https://www.google.com")));
27 whitelist.push_back(Origin(GURL("http://www.example.com")));
28 scoped_ptr<OriginFilter> filter = OriginFilter::AsWhitelist(whitelist);
29
30 // Whitelist matches any URL on the specified origins.
31 EXPECT_TRUE(filter->MatchesURL(GURL("https://www.google.com")));
32 EXPECT_TRUE(filter->MatchesURL(GURL("https://www.google.com/?q=test")));
33 EXPECT_TRUE(filter->MatchesURL(GURL("http://www.example.com")));
34 EXPECT_TRUE(filter->MatchesURL(GURL("http://www.example.com/index.html")));
35 EXPECT_TRUE(filter->MatchesURL(GURL("http://www.example.com/foo/bar")));
36
37 // Subdomains are different origins.
38 EXPECT_FALSE(filter->MatchesURL(GURL("https://test.www.google.com")));
39
40 // Different scheme or port is a different origin.
41 EXPECT_FALSE(filter->MatchesURL(GURL("https://www.google.com:8000")));
42 EXPECT_FALSE(filter->MatchesURL(GURL("https://www.example.com/index.html")));
43
44 // Different host is a different origin.
45 EXPECT_FALSE(filter->MatchesURL(GURL("https://www.youtube.com")));
46 EXPECT_FALSE(filter->MatchesURL(GURL("https://www.chromium.org")));
47 }
48
49 TEST(OriginFilterTest, Blacklist) {
50 std::vector<Origin> blacklist;
51 blacklist.push_back(Origin(GURL("https://www.google.com")));
52 blacklist.push_back(Origin(GURL("http://www.example.com")));
53 scoped_ptr<OriginFilter> filter = OriginFilter::AsBlacklist(blacklist);
54
55 // URLS on explicitly specified origins are not matched.
56 EXPECT_FALSE(filter->MatchesURL(GURL("https://www.google.com")));
57 EXPECT_FALSE(filter->MatchesURL(GURL("https://www.google.com/?q=test")));
58 EXPECT_FALSE(filter->MatchesURL(GURL("http://www.example.com")));
59 EXPECT_FALSE(filter->MatchesURL(GURL("http://www.example.com/index.html")));
60 EXPECT_FALSE(filter->MatchesURL(GURL("http://www.example.com/foo/bar")));
61
62 // Subdomains are different origins.
63 EXPECT_TRUE(filter->MatchesURL(GURL("https://test.www.google.com")));
64
65 // The same hosts but with different schemes and hosts are not blacklisted.
66 EXPECT_TRUE(filter->MatchesURL(GURL("https://www.google.com:8000")));
67 EXPECT_TRUE(filter->MatchesURL(GURL("https://www.example.com/index.html")));
68
69 // Different hosts are not blacklisted.
70 EXPECT_TRUE(filter->MatchesURL(GURL("https://www.chrome.com")));
71 EXPECT_TRUE(filter->MatchesURL(GURL("https://www.youtube.com")));
72 }
73
74 TEST(OriginFilterTest, MatchesURLWithSubdomain) {
75 std::vector<Origin> whitelist;
76 whitelist.push_back(Origin(GURL("https://www.google.com")));
77 scoped_ptr<OriginFilter> filter = OriginFilter::AsWhitelist(whitelist);
78
79 // Any URL on the specified origin is matched.
80 EXPECT_TRUE(filter->MatchesURLWithSubdomains(GURL("https://www.google.com")));
81 EXPECT_TRUE(filter->MatchesURLWithSubdomains(
82 GURL("https://www.google.com/test.html")));
83
84 // Subdomains are also matched...
85 EXPECT_TRUE(filter->MatchesURLWithSubdomains(
86 GURL("https://foo.www.google.com")));
87 EXPECT_TRUE(filter->MatchesURLWithSubdomains(
88 GURL("https://foo.www.google.com/?q=test")));
89 EXPECT_TRUE(filter->MatchesURLWithSubdomains(
90 GURL("https://foo.bar.www.google.com")));
91 EXPECT_TRUE(filter->MatchesURLWithSubdomains(
92 GURL("https://foo.bar.www.google.com/test.html")));
93 EXPECT_TRUE(filter->MatchesURLWithSubdomains(
94 GURL("https://foo.bar.baz.www.google.com")));
95
96 // Superdomains are not matched.
97 EXPECT_FALSE(filter->MatchesURLWithSubdomains(GURL("https://google.com")));
98
99 // Different hosts are not blacklisted.
100 EXPECT_FALSE(filter->MatchesURL(GURL("https://www.chrome.com")));
101 EXPECT_FALSE(filter->MatchesURL(GURL("https://www.youtube.com")));
102 }
103
104 TEST(OriginFilterTest, Equivalence) {
105 std::vector<Origin> origins;
106 origins.push_back(Origin(GURL("https://www.google.com")));
107 scoped_ptr<OriginFilter> filter1 = OriginFilter::AsWhitelist(origins);
108
109 origins.push_back(Origin(GURL("https://www.chrome.com")));
110 scoped_ptr<OriginFilter> filter2 = OriginFilter::AsWhitelist(origins);
111 scoped_ptr<OriginFilter> filter3 = OriginFilter::AsBlacklist(origins);
112
113 // Two whitelists with different sets of origins are not equivalent.
114 EXPECT_NE(*filter1, *filter2);
115
116 // Whitelist and blacklist are not equivalent.
117 EXPECT_NE(*filter1, *filter3);
118 EXPECT_NE(*filter2, *filter3);
119
120 // Unique origins are ignored, so adding them to the list does not change
121 // the filter.
122 origins.push_back(Origin(GURL("unique origin")));
123 scoped_ptr<OriginFilter> filter4 = OriginFilter::AsBlacklist(origins);
124 EXPECT_EQ(*filter3, *filter4);
125
126 // Operator== is symmetric...
127 EXPECT_EQ(*filter4, *filter3);
128
129 // ...and reflexive.
130 EXPECT_EQ(filter1, filter1);
131
132 // The order of the list does not matter.
133 origins.push_back(Origin(GURL("https://www.youtube.com")));
134 scoped_ptr<OriginFilter> filter5 = OriginFilter::AsBlacklist(origins);
135 std::reverse(origins.begin(), origins.end());
136 scoped_ptr<OriginFilter> filter6 = OriginFilter::AsBlacklist(origins);
137 EXPECT_EQ(*filter5, *filter6);
138 }
139
140 } // namespace url
OLDNEW
« url/origin_filter.cc ('K') | « url/origin_filter.cc ('k') | url/url.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698