| 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..3bd41578976bafe58cd0841bd5d9bf835e100541
|
| --- /dev/null
|
| +++ b/chrome/browser/browsing_data/origin_filter_builder_unittest.cc
|
| @@ -0,0 +1,107 @@
|
| +// 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 filter matches everything.
|
| + scoped_ptr<OriginFilterBuilder> builder = OriginFilterBuilder::Empty();
|
| + base::Callback<bool(const GURL&)> filter = builder->GetSameOriginFilter();
|
| +
|
| + 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) {
|
| + std::vector<Origin> whitelist;
|
| + whitelist.push_back(Origin(GURL("https://www.google.com")));
|
| + whitelist.push_back(Origin(GURL("http://www.example.com")));
|
| + scoped_ptr<OriginFilterBuilder> builder =
|
| + OriginFilterBuilder::AsWhitelist(whitelist);
|
| + base::Callback<bool(const GURL&)> filter = builder->GetSameOriginFilter();
|
| +
|
| + // 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) {
|
| + std::vector<Origin> blacklist;
|
| + blacklist.push_back(Origin(GURL("https://www.google.com")));
|
| + blacklist.push_back(Origin(GURL("http://www.example.com")));
|
| + scoped_ptr<OriginFilterBuilder> builder =
|
| + OriginFilterBuilder::AsBlacklist(blacklist);
|
| + base::Callback<bool(const GURL&)> filter = builder->GetSameOriginFilter();
|
| +
|
| + // 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) {
|
| + std::vector<Origin> whitelist;
|
| + whitelist.push_back(Origin(GURL("https://www.google.com")));
|
| + scoped_ptr<OriginFilterBuilder> builder =
|
| + OriginFilterBuilder::AsWhitelist(whitelist);
|
| + base::Callback<bool(const GURL&)> filter = builder->GetSubdomainFilter();
|
| +
|
| + // 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")));
|
| +}
|
| +
|
| +} // namespace url
|
|
|