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

Unified 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, 11 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 side-by-side diff with in-line comments
Download patch
« url/origin_filter.cc ('K') | « url/origin_filter.cc ('k') | url/url.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: url/origin_filter_unittest.cc
diff --git a/url/origin_filter_unittest.cc b/url/origin_filter_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8cadd0c48622fb9f2b89c75791347711a4e41176
--- /dev/null
+++ b/url/origin_filter_unittest.cc
@@ -0,0 +1,140 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "url/origin_filter.h"
+
+#include <algorithm>
+#include <vector>
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "url/gurl.h"
+#include "url/origin.h"
+
+namespace url {
+
+TEST(OriginFilterTest, Empty) {
+ // An empty filter matches everything.
+ scoped_ptr<OriginFilter> filter = OriginFilter::Empty();
+ EXPECT_TRUE(filter->MatchesURL(GURL("https://www.google.com")));
+ EXPECT_TRUE(filter->MatchesURL(GURL("https://www.chrome.com")));
+ EXPECT_TRUE(filter->MatchesURL(GURL("invalid url spec")));
+}
+
+TEST(OriginFilterTest, Whitelist) {
+ std::vector<Origin> whitelist;
+ whitelist.push_back(Origin(GURL("https://www.google.com")));
+ whitelist.push_back(Origin(GURL("http://www.example.com")));
+ scoped_ptr<OriginFilter> filter = OriginFilter::AsWhitelist(whitelist);
+
+ // Whitelist matches any URL on the specified origins.
+ EXPECT_TRUE(filter->MatchesURL(GURL("https://www.google.com")));
+ EXPECT_TRUE(filter->MatchesURL(GURL("https://www.google.com/?q=test")));
+ EXPECT_TRUE(filter->MatchesURL(GURL("http://www.example.com")));
+ EXPECT_TRUE(filter->MatchesURL(GURL("http://www.example.com/index.html")));
+ EXPECT_TRUE(filter->MatchesURL(GURL("http://www.example.com/foo/bar")));
+
+ // Subdomains are different origins.
+ EXPECT_FALSE(filter->MatchesURL(GURL("https://test.www.google.com")));
+
+ // Different scheme or port is a different origin.
+ EXPECT_FALSE(filter->MatchesURL(GURL("https://www.google.com:8000")));
+ EXPECT_FALSE(filter->MatchesURL(GURL("https://www.example.com/index.html")));
+
+ // Different host is a different origin.
+ EXPECT_FALSE(filter->MatchesURL(GURL("https://www.youtube.com")));
+ EXPECT_FALSE(filter->MatchesURL(GURL("https://www.chromium.org")));
+}
+
+TEST(OriginFilterTest, Blacklist) {
+ std::vector<Origin> blacklist;
+ blacklist.push_back(Origin(GURL("https://www.google.com")));
+ blacklist.push_back(Origin(GURL("http://www.example.com")));
+ scoped_ptr<OriginFilter> filter = OriginFilter::AsBlacklist(blacklist);
+
+ // URLS on explicitly specified origins are not matched.
+ EXPECT_FALSE(filter->MatchesURL(GURL("https://www.google.com")));
+ EXPECT_FALSE(filter->MatchesURL(GURL("https://www.google.com/?q=test")));
+ EXPECT_FALSE(filter->MatchesURL(GURL("http://www.example.com")));
+ EXPECT_FALSE(filter->MatchesURL(GURL("http://www.example.com/index.html")));
+ EXPECT_FALSE(filter->MatchesURL(GURL("http://www.example.com/foo/bar")));
+
+ // Subdomains are different origins.
+ EXPECT_TRUE(filter->MatchesURL(GURL("https://test.www.google.com")));
+
+ // The same hosts but with different schemes and hosts are not blacklisted.
+ EXPECT_TRUE(filter->MatchesURL(GURL("https://www.google.com:8000")));
+ EXPECT_TRUE(filter->MatchesURL(GURL("https://www.example.com/index.html")));
+
+ // Different hosts are not blacklisted.
+ EXPECT_TRUE(filter->MatchesURL(GURL("https://www.chrome.com")));
+ EXPECT_TRUE(filter->MatchesURL(GURL("https://www.youtube.com")));
+}
+
+TEST(OriginFilterTest, MatchesURLWithSubdomain) {
+ std::vector<Origin> whitelist;
+ whitelist.push_back(Origin(GURL("https://www.google.com")));
+ scoped_ptr<OriginFilter> filter = OriginFilter::AsWhitelist(whitelist);
+
+ // Any URL on the specified origin is matched.
+ EXPECT_TRUE(filter->MatchesURLWithSubdomains(GURL("https://www.google.com")));
+ EXPECT_TRUE(filter->MatchesURLWithSubdomains(
+ GURL("https://www.google.com/test.html")));
+
+ // Subdomains are also matched...
+ EXPECT_TRUE(filter->MatchesURLWithSubdomains(
+ GURL("https://foo.www.google.com")));
+ EXPECT_TRUE(filter->MatchesURLWithSubdomains(
+ GURL("https://foo.www.google.com/?q=test")));
+ EXPECT_TRUE(filter->MatchesURLWithSubdomains(
+ GURL("https://foo.bar.www.google.com")));
+ EXPECT_TRUE(filter->MatchesURLWithSubdomains(
+ GURL("https://foo.bar.www.google.com/test.html")));
+ EXPECT_TRUE(filter->MatchesURLWithSubdomains(
+ GURL("https://foo.bar.baz.www.google.com")));
+
+ // Superdomains are not matched.
+ EXPECT_FALSE(filter->MatchesURLWithSubdomains(GURL("https://google.com")));
+
+ // Different hosts are not blacklisted.
+ EXPECT_FALSE(filter->MatchesURL(GURL("https://www.chrome.com")));
+ EXPECT_FALSE(filter->MatchesURL(GURL("https://www.youtube.com")));
+}
+
+TEST(OriginFilterTest, Equivalence) {
+ std::vector<Origin> origins;
+ origins.push_back(Origin(GURL("https://www.google.com")));
+ scoped_ptr<OriginFilter> filter1 = OriginFilter::AsWhitelist(origins);
+
+ origins.push_back(Origin(GURL("https://www.chrome.com")));
+ scoped_ptr<OriginFilter> filter2 = OriginFilter::AsWhitelist(origins);
+ scoped_ptr<OriginFilter> filter3 = OriginFilter::AsBlacklist(origins);
+
+ // Two whitelists with different sets of origins are not equivalent.
+ EXPECT_NE(*filter1, *filter2);
+
+ // Whitelist and blacklist are not equivalent.
+ EXPECT_NE(*filter1, *filter3);
+ EXPECT_NE(*filter2, *filter3);
+
+ // Unique origins are ignored, so adding them to the list does not change
+ // the filter.
+ origins.push_back(Origin(GURL("unique origin")));
+ scoped_ptr<OriginFilter> filter4 = OriginFilter::AsBlacklist(origins);
+ EXPECT_EQ(*filter3, *filter4);
+
+ // Operator== is symmetric...
+ EXPECT_EQ(*filter4, *filter3);
+
+ // ...and reflexive.
+ EXPECT_EQ(filter1, filter1);
+
+ // The order of the list does not matter.
+ origins.push_back(Origin(GURL("https://www.youtube.com")));
+ scoped_ptr<OriginFilter> filter5 = OriginFilter::AsBlacklist(origins);
+ std::reverse(origins.begin(), origins.end());
+ scoped_ptr<OriginFilter> filter6 = OriginFilter::AsBlacklist(origins);
+ EXPECT_EQ(*filter5, *filter6);
+}
+
+} // namespace url
« 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