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

Unified Diff: chrome/browser/browsing_data/origin_filter_builder_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: Rebase over 1246583002 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/browsing_data/origin_filter_builder.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/browsing_data/origin_filter_builder_unittest.cc
diff --git a/chrome/browser/browsing_data/origin_filter_builder_unittest.cc b/chrome/browser/browsing_data/origin_filter_builder_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..32fef25dadf3028c49a8e48852343732e3b5e3fd
--- /dev/null
+++ b/chrome/browser/browsing_data/origin_filter_builder_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 "chrome/browser/browsing_data/origin_filter_builder.h"
+
+#include <algorithm>
+#include <string>
+#include <vector>
+
+#include "base/callback.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "url/gurl.h"
+#include "url/origin.h"
+
+namespace url {
+
+namespace {
+
+struct TestCase {
+ std::string url;
+ bool should_match;
+};
+
+void RunTestCase(
+ TestCase test_case, const base::Callback<bool(const GURL&)>& filter) {
+ if (test_case.should_match)
+ EXPECT_TRUE(filter.Run(GURL(test_case.url)));
+ else
+ EXPECT_FALSE(filter.Run(GURL(test_case.url)));
+}
+
+} // namespace
+
+TEST(OriginFilterBuilderTest, Noop) {
+ // An no-op filter matches everything.
+ base::Callback<bool(const GURL&)> filter =
+ OriginFilterBuilder::BuildNoopFilter();
+
+ TestCase test_cases[] = {
+ { "https://www.google.com", true },
+ { "https://www.chrome.com", true },
+ { "invalid url spec", true }
+ };
+
+ for (TestCase test_case : test_cases)
+ RunTestCase(test_case, filter);
+}
+
+TEST(OriginFilterBuilderTest, Whitelist) {
+ OriginFilterBuilder builder(OriginFilterBuilder::WHITELIST);
+ builder.AddOrigin(Origin(GURL("https://www.google.com")));
+ builder.AddOrigin(Origin(GURL("http://www.example.com")));
+ base::Callback<bool(const GURL&)> filter = builder.BuildSameOriginFilter();
+
+ TestCase test_cases[] = {
+ // Whitelist matches any URL on the specified origins.
+ { "https://www.google.com", true },
+ { "https://www.google.com/?q=test", true },
+ { "http://www.example.com", true },
+ { "http://www.example.com/index.html", true },
+ { "http://www.example.com/foo/bar", true },
+
+ // Subdomains are different origins.
+ { "https://test.www.google.com", false },
+
+ // Different scheme or port is a different origin.
+ { "https://www.google.com:8000", false },
+ { "https://www.example.com/index.html", false },
+
+ // Different host is a different origin.
+ { "https://www.youtube.com", false },
+ { "https://www.chromium.org", false },
+ };
+
+ for (TestCase test_case : test_cases)
+ RunTestCase(test_case, filter);
+}
+
+TEST(OriginFilterBuilderTest, Blacklist) {
+ OriginFilterBuilder builder(OriginFilterBuilder::BLACKLIST);
+ builder.AddOrigin(Origin(GURL("https://www.google.com")));
+ builder.AddOrigin(Origin(GURL("http://www.example.com")));
+ base::Callback<bool(const GURL&)> filter = builder.BuildSameOriginFilter();
+
+ TestCase test_cases[] = {
+ // URLS on explicitly specified origins are not matched.
+ { "https://www.google.com", false },
+ { "https://www.google.com/?q=test", false },
+ { "http://www.example.com", false },
+ { "http://www.example.com/index.html", false },
+ { "http://www.example.com/foo/bar", false },
+
+ // Subdomains are different origins.
+ { "https://test.www.google.com", true },
+
+ // The same hosts but with different schemes and ports
+ // are not blacklisted.
+ { "https://www.google.com:8000", true },
+ { "https://www.example.com/index.html", true },
+
+ // Different hosts are not blacklisted.
+ { "https://www.chrome.com", true },
+ { "https://www.youtube.com", true },
+ };
+
+ for (TestCase test_case : test_cases)
+ RunTestCase(test_case, filter);
+}
+
+TEST(OriginFilterBuilderTest, MatchesURLWithSubdomain) {
+ OriginFilterBuilder builder(OriginFilterBuilder::WHITELIST);
+ builder.AddOrigin(Origin(GURL("https://www.google.com")));
+ base::Callback<bool(const GURL&)> filter = builder.BuildDomainFilter();
+
+ TestCase test_cases[] = {
+ // Any URL on the specified origin is matched.
+ { "https://www.google.com", true },
+ { "https://www.google.com/test.html", true },
+
+ // Subdomains are also matched.
+ { "https://foo.www.google.com", true },
+ { "https://foo.www.google.com/?q=test", true },
+ { "https://foo.bar.www.google.com", true },
+ { "https://foo.bar.www.google.com/test.html", true },
+ { "https://foo.bar.baz.www.google.com", true },
+
+ // Superdomains are not matched.
+ { "https://google.com", false },
+
+ // Different hosts are not matched.
+ { "https://www.chrome.com", false },
+ { "https://www.youtube.com", false },
+ };
+
+ for (TestCase test_case : test_cases)
+ RunTestCase(test_case, filter);
+}
+
+} // namespace url
« no previous file with comments | « chrome/browser/browsing_data/origin_filter_builder.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698