Chromium Code Reviews| Index: chrome/browser/managed_mode_url_filter_unittest.cc |
| diff --git a/chrome/browser/managed_mode_url_filter_unittest.cc b/chrome/browser/managed_mode_url_filter_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5b7446d00546c2be04f6b925b2948394b9c38067 |
| --- /dev/null |
| +++ b/chrome/browser/managed_mode_url_filter_unittest.cc |
| @@ -0,0 +1,172 @@ |
| +// Copyright (c) 2012 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 "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/managed_mode_url_filter.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +class FailClosureHelper : public base::RefCountedThreadSafe<FailClosureHelper> { |
| + public: |
| + explicit FailClosureHelper(const base::Closure& cb) : closure_runner_(cb) {} |
| + |
| + void Fail() { |
| + FAIL(); |
| + } |
| + |
| + private: |
| + friend class base::RefCountedThreadSafe<FailClosureHelper>; |
| + |
| + virtual ~FailClosureHelper() {} |
| + |
| + base::ScopedClosureRunner closure_runner_; |
| +}; |
| + |
| +// Returns a closure that FAILs when it is called. As soon as the closure is |
| +// destroyed (because the last reference to it is dropped), |continuation| is |
| +// called. |
| +base::Closure FailClosure(const base::Closure& continuation) { |
| + scoped_refptr<FailClosureHelper> helper = new FailClosureHelper(continuation); |
| + return base::Bind(&FailClosureHelper::Fail, helper); |
| +} |
| + |
| +} // namespace |
| + |
| +class ManagedModeURLFilterTest : public ::testing::Test { |
| + public: |
| + ManagedModeURLFilterTest() {} |
| + virtual ~ManagedModeURLFilterTest() {} |
| + |
| + virtual void SetUp() OVERRIDE { |
| + filter_.reset(new ManagedModeURLFilter); |
| + filter_->SetActive(true); |
| + } |
| + |
| + protected: |
| + MessageLoop message_loop_; |
| + base::RunLoop run_loop_; |
| + scoped_ptr<ManagedModeURLFilter> filter_; |
| +}; |
| + |
| +TEST_F(ManagedModeURLFilterTest, Basic) { |
| + scoped_ptr<base::ListValue> list(new base::ListValue); |
| + // Block domain and all subdomains, for any filtered scheme. |
|
Joao da Silva
2012/07/13 12:32:11
Did you mean "Allow domain ..." ?
Bernhard Bauer
2012/07/13 14:21:07
Yep. Fixed.
|
| + list->Append(new base::StringValue("google.com")); |
| + filter_->SetWhitelist(list.Pass(), run_loop_.QuitClosure()); |
| + run_loop_.Run(); |
| + |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://google.com"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://google.com/"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://google.com/whatever"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("https://google.com/"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("http://notgoogle.com/"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://mail.google.com"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://x.mail.google.com"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("https://x.mail.google.com/"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://x.y.google.com/a/b"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("http://youtube.com/"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("bogus://youtube.com/"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("chrome://youtube.com/"))); |
| +} |
| + |
| +TEST_F(ManagedModeURLFilterTest, Inactive) { |
| + filter_->SetActive(false); |
| + |
| + scoped_ptr<base::ListValue> list(new base::ListValue); |
| + list->Append(new base::StringValue("google.com")); |
| + filter_->SetWhitelist(list.Pass(), run_loop_.QuitClosure()); |
| + run_loop_.Run(); |
| + |
| + // If the filter is inactive, every URL should be whitelisted. |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://google.com"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("https://www.example.com"))); |
| +} |
| + |
| +TEST_F(ManagedModeURLFilterTest, Shutdown) { |
| + scoped_ptr<base::ListValue> list(new base::ListValue); |
| + list->Append(new base::StringValue("google.com")); |
| + filter_->SetWhitelist(list.Pass(), FailClosure(run_loop_.QuitClosure())); |
| + // Destroy the filter before we set the URLMatcher. |
| + filter_.reset(); |
| + run_loop_.Run(); |
| +} |
| + |
| +TEST_F(ManagedModeURLFilterTest, Scheme) { |
| + scoped_ptr<base::ListValue> list(new base::ListValue); |
| + // Filter only http, ftp and ws schemes. |
| + list->Append(new base::StringValue("http://secure.com")); |
| + list->Append(new base::StringValue("ftp://secure.com")); |
| + list->Append(new base::StringValue("ws://secure.com")); |
| + filter_->SetWhitelist(list.Pass(), run_loop_.QuitClosure()); |
| + run_loop_.Run(); |
| + |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://secure.com"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://secure.com/whatever"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("ftp://secure.com/"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("ws://secure.com"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("https://secure.com/"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("wss://secure.com"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://www.secure.com"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("https://www.secure.com"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("wss://www.secure.com"))); |
| +} |
| + |
| +TEST_F(ManagedModeURLFilterTest, Path) { |
| + scoped_ptr<base::ListValue> list(new base::ListValue); |
| + // Filter only a certain path prefix. |
| + list->Append(new base::StringValue("path.to/ruin")); |
| + filter_->SetWhitelist(list.Pass(), run_loop_.QuitClosure()); |
| + run_loop_.Run(); |
| + |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://path.to/ruin"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("https://path.to/ruin"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://path.to/ruins"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://path.to/ruin/signup"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://www.path.to/ruin"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("http://path.to/fortune"))); |
| +} |
| + |
| +TEST_F(ManagedModeURLFilterTest, PathAndScheme) { |
| + scoped_ptr<base::ListValue> list(new base::ListValue); |
| + // Filter only a certain path prefix and scheme. |
| + list->Append(new base::StringValue("https://s.aaa.com/path")); |
| + filter_->SetWhitelist(list.Pass(), run_loop_.QuitClosure()); |
| + run_loop_.Run(); |
| + |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("https://s.aaa.com/path"))); |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("https://s.aaa.com/path/bbb"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("http://s.aaa.com/path"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("https://aaa.com/path"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("https://x.aaa.com/path"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("https://s.aaa.com/bbb"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("https://s.aaa.com/"))); |
| +} |
| + |
| +TEST_F(ManagedModeURLFilterTest, Host) { |
| + scoped_ptr<base::ListValue> list(new base::ListValue); |
| + // Filter only a certain hostname, without subdomains. |
| + list->Append(new base::StringValue(".www.example.com")); |
| + filter_->SetWhitelist(list.Pass(), run_loop_.QuitClosure()); |
| + run_loop_.Run(); |
| + |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://www.example.com"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("http://example.com"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("http://subdomain.example.com"))); |
| +} |
| + |
| +TEST_F(ManagedModeURLFilterTest, IPAddress) { |
| + scoped_ptr<base::ListValue> list(new base::ListValue); |
| + // Filter an ip address. |
| + list->Append(new base::StringValue("123.123.123.123")); |
| + filter_->SetWhitelist(list.Pass(), run_loop_.QuitClosure()); |
| + run_loop_.Run(); |
| + |
| + EXPECT_TRUE(filter_->IsURLWhitelisted(GURL("http://123.123.123.123/"))); |
| + EXPECT_FALSE(filter_->IsURLWhitelisted(GURL("http://123.123.123.124/"))); |
| +} |