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..17cddc53711d271c6056a3fd1a6e29df44c5ccfc |
--- /dev/null |
+++ b/chrome/browser/browsing_data/origin_filter_builder_unittest.cc |
@@ -0,0 +1,104 @@ |
+// 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 <vector> |
+ |
+#include "base/callback.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "url/gurl.h" |
+#include "url/origin.h" |
+ |
+namespace url { |
+ |
+TEST(OriginFilterBuilderTest, Empty) { |
+ // An empty blacklist matches everything. |
+ OriginFilterBuilder builder(OriginFilterBuilder::BLACKLIST); |
+ base::Callback<bool(const GURL&)> filter = builder.BuildSameOriginFilter(); |
+ |
+ EXPECT_TRUE(filter.Run(GURL("https://www.google.com"))); |
+ EXPECT_TRUE(filter.Run(GURL("https://www.chrome.com"))); |
+ EXPECT_TRUE(filter.Run(GURL("invalid url spec"))); |
+} |
+ |
+TEST(OriginFilterBuilderTest, Whitelist) { |
+ OriginFilterBuilder builder(OriginFilterBuilder::WHITELIST); |
+ base::Callback<bool(const GURL&)> filter = |
+ builder.AddOrigin(Origin(GURL("https://www.google.com"))) |
+ ->AddOrigin(Origin(GURL("http://www.example.com"))) |
+ ->BuildSameOriginFilter(); |
+ |
+ // Whitelist matches any URL on the specified origins. |
+ EXPECT_TRUE(filter.Run(GURL("https://www.google.com"))); |
+ EXPECT_TRUE(filter.Run(GURL("https://www.google.com/?q=test"))); |
+ EXPECT_TRUE(filter.Run(GURL("http://www.example.com"))); |
+ EXPECT_TRUE(filter.Run(GURL("http://www.example.com/index.html"))); |
+ EXPECT_TRUE(filter.Run(GURL("http://www.example.com/foo/bar"))); |
+ |
+ // Subdomains are different origins. |
+ EXPECT_FALSE(filter.Run(GURL("https://test.www.google.com"))); |
+ |
+ // Different scheme or port is a different origin. |
+ EXPECT_FALSE(filter.Run(GURL("https://www.google.com:8000"))); |
+ EXPECT_FALSE(filter.Run(GURL("https://www.example.com/index.html"))); |
+ |
+ // Different host is a different origin. |
+ EXPECT_FALSE(filter.Run(GURL("https://www.youtube.com"))); |
+ EXPECT_FALSE(filter.Run(GURL("https://www.chromium.org"))); |
+} |
+ |
+TEST(OriginFilterBuilderTest, Blacklist) { |
+ OriginFilterBuilder builder(OriginFilterBuilder::BLACKLIST); |
+ base::Callback<bool(const GURL&)> filter = |
+ builder.AddOrigin(Origin(GURL("https://www.google.com"))) |
+ ->AddOrigin(Origin(GURL("http://www.example.com"))) |
+ ->BuildSameOriginFilter(); |
+ |
+ // URLS on explicitly specified origins are not matched. |
+ EXPECT_FALSE(filter.Run(GURL("https://www.google.com"))); |
+ EXPECT_FALSE(filter.Run(GURL("https://www.google.com/?q=test"))); |
+ EXPECT_FALSE(filter.Run(GURL("http://www.example.com"))); |
+ EXPECT_FALSE(filter.Run(GURL("http://www.example.com/index.html"))); |
+ EXPECT_FALSE(filter.Run(GURL("http://www.example.com/foo/bar"))); |
+ |
+ // Subdomains are different origins. |
+ EXPECT_TRUE(filter.Run(GURL("https://test.www.google.com"))); |
+ |
+ // The same hosts but with different schemes and hosts are not blacklisted. |
+ EXPECT_TRUE(filter.Run(GURL("https://www.google.com:8000"))); |
+ EXPECT_TRUE(filter.Run(GURL("https://www.example.com/index.html"))); |
+ |
+ // Different hosts are not blacklisted. |
+ EXPECT_TRUE(filter.Run(GURL("https://www.chrome.com"))); |
+ EXPECT_TRUE(filter.Run(GURL("https://www.youtube.com"))); |
+} |
+ |
+TEST(OriginFilterBuilderTest, MatchesURLWithSubdomain) { |
+ OriginFilterBuilder builder(OriginFilterBuilder::WHITELIST); |
+ base::Callback<bool(const GURL&)> filter = |
+ builder.AddOrigin(Origin(GURL("https://www.google.com"))) |
+ ->BuildDomainFilter(); |
+ |
+ // Any URL on the specified origin is matched. |
+ EXPECT_TRUE(filter.Run(GURL("https://www.google.com"))); |
+ EXPECT_TRUE(filter.Run(GURL("https://www.google.com/test.html"))); |
+ |
+ // Subdomains are also matched... |
+ EXPECT_TRUE(filter.Run(GURL("https://foo.www.google.com"))); |
+ EXPECT_TRUE(filter.Run(GURL("https://foo.www.google.com/?q=test"))); |
+ EXPECT_TRUE(filter.Run(GURL("https://foo.bar.www.google.com"))); |
+ EXPECT_TRUE(filter.Run(GURL("https://foo.bar.www.google.com/test.html"))); |
+ EXPECT_TRUE(filter.Run(GURL("https://foo.bar.baz.www.google.com"))); |
+ |
+ // Superdomains are not matched. |
+ EXPECT_FALSE(filter.Run(GURL("https://google.com"))); |
+ |
+ // Different hosts are not matched. |
+ EXPECT_FALSE(filter.Run(GURL("https://www.chrome.com"))); |
+ EXPECT_FALSE(filter.Run(GURL("https://www.youtube.com"))); |
Mike West
2016/02/17 15:22:10
Nit: You could reduce the boilerplate in all of th
msramek
2016/02/18 12:48:43
Done.
|
+} |
+ |
+} // namespace url |