Index: content/browser/net/quota_policy_cookie_store_unittest.cc |
diff --git a/content/browser/net/quota_policy_cookie_store_unittest.cc b/content/browser/net/quota_policy_cookie_store_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e68595d0614b0730aa6de2fe4235ccd9e30e97ec |
--- /dev/null |
+++ b/content/browser/net/quota_policy_cookie_store_unittest.cc |
@@ -0,0 +1,191 @@ |
+// 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 "base/bind.h" |
+#include "base/files/file_util.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_vector.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
+#include "base/stl_util.h" |
+#include "base/synchronization/waitable_event.h" |
+#include "base/test/sequenced_worker_pool_owner.h" |
+#include "base/threading/sequenced_worker_pool.h" |
+#include "base/time/time.h" |
+#include "content/browser/net/quota_policy_cookie_store.h" |
+#include "content/public/test/mock_special_storage_policy.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
+#include "net/base/test_data_directory.h" |
+#include "net/cookies/cookie_util.h" |
+#include "net/ssl/ssl_client_cert_type.h" |
+#include "net/test/cert_test_util.h" |
+#include "sql/statement.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+const base::FilePath::CharType kTestCookiesFilename[] = |
+ FILE_PATH_LITERAL("Cookies"); |
+} |
+ |
+namespace content { |
+ |
+class QuotaPolicyCookieStoreTest : public testing::Test { |
+ public: |
+ QuotaPolicyCookieStoreTest() |
+ : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")), |
+ loaded_event_(false, false) { |
+ } |
+ |
+ void OnLoaded(const std::vector<net::CanonicalCookie*>& cookies) { |
+ cookies_ = cookies; |
+ loaded_event_.Signal(); |
+ } |
+ |
+ void Load(std::vector<net::CanonicalCookie*>* cookies) { |
+ EXPECT_FALSE(loaded_event_.IsSignaled()); |
+ store_->Load(base::Bind(&QuotaPolicyCookieStoreTest::OnLoaded, |
+ base::Unretained(this))); |
+ loaded_event_.Wait(); |
+ *cookies = cookies_; |
+ } |
+ |
+ protected: |
+ scoped_refptr<base::SequencedTaskRunner> background_task_runner() { |
+ return pool_owner_->pool()->GetSequencedTaskRunner( |
+ pool_owner_->pool()->GetNamedSequenceToken("background")); |
+ } |
+ |
+ scoped_refptr<base::SequencedTaskRunner> client_task_runner() { |
+ return pool_owner_->pool()->GetSequencedTaskRunner( |
+ pool_owner_->pool()->GetNamedSequenceToken("client")); |
+ } |
+ |
+ // Adds a persistent cookie to store_. |
+ void AddCookie(const std::string& name, |
+ const std::string& value, |
+ const std::string& domain, |
+ const std::string& path, |
+ const base::Time& creation) { |
+ store_->AddCookie( |
+ net::CanonicalCookie( |
+ GURL(), name, value, domain, path, creation, creation, creation, |
+ false, false, false, net::COOKIE_PRIORITY_DEFAULT)); |
+ } |
+ |
+ void DestroyStore() { |
+ store_ = NULL; |
+ // Make sure we wait until the destructor has run by shutting down the pool |
+ // resetting the owner (whose destructor blocks on the pool completion). |
+ pool_owner_->pool()->Shutdown(3); |
rohitrao (ping after 24h)
2015/03/18 14:37:34
@rsleevi For some reason, the indirection through
|
+ pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); |
+ } |
+ |
+ void SetUp() override { |
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
+ store_ = new QuotaPolicyCookieStore( |
+ temp_dir_.path().Append(kTestCookiesFilename), |
+ client_task_runner(), |
+ background_task_runner(), |
+ true, NULL, NULL); |
+ std::vector<net::CanonicalCookie*> cookies; |
+ Load(&cookies); |
+ ASSERT_EQ(0u, cookies.size()); |
+ // Make sure the store gets written at least once. |
+ AddCookie("A", "B", "foo.com", "/", base::Time::Now()); |
+ } |
+ |
+ void TearDown() override { |
+ DestroyStore(); |
+ pool_owner_->pool()->Shutdown(); |
+ } |
+ |
+ base::MessageLoop main_loop_; |
+ scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; |
+ base::WaitableEvent loaded_event_; |
+ base::ScopedTempDir temp_dir_; |
+ scoped_refptr<QuotaPolicyCookieStore> store_; |
+ std::vector<net::CanonicalCookie*> cookies_; |
+}; |
+ |
+// Test if data is stored as expected in the QuotaPolicy database. |
+TEST_F(QuotaPolicyCookieStoreTest, TestPersistence) { |
+ AddCookie("A", "B", "persistent.com", "/", base::Time::Now()); |
+ |
+ // Replace the store effectively destroying the current one and forcing it |
+ // to write its data to disk. Then we can see if after loading it again it |
+ // is still there. |
+ DestroyStore(); |
+ store_ = new QuotaPolicyCookieStore( |
+ temp_dir_.path().Append(kTestCookiesFilename), |
+ client_task_runner(), |
+ background_task_runner(), |
+ true, NULL, NULL); |
+ |
+ // Reload and test for persistence |
+ std::vector<net::CanonicalCookie*> cookies; |
+ Load(&cookies); |
+ ASSERT_EQ(2U, cookies.size()); |
+ EXPECT_EQ("foo.com", cookies[0]->Domain()); |
+ EXPECT_EQ("persistent.com", cookies[1]->Domain()); |
+ |
+ // Now delete the cookies and check persistence again. |
+ store_->DeleteCookie(*cookies[0]); |
+ store_->DeleteCookie(*cookies[1]); |
+ DestroyStore(); |
+ |
+ STLDeleteElements(&cookies); |
+ cookies.clear(); |
+ store_ = new QuotaPolicyCookieStore( |
+ temp_dir_.path().Append(kTestCookiesFilename), |
+ client_task_runner(), |
+ background_task_runner(), |
+ true, NULL, NULL); |
+ |
+ // Reload and check if the cert has been removed. |
+ Load(&cookies); |
+ ASSERT_EQ(0U, cookies.size()); |
+} |
+ |
+// Test if data is stored as expected in the QuotaPolicy database. |
+TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) { |
+ AddCookie("A", "B", "persistent.com", "/", base::Time::Now()); |
+ AddCookie("A", "B", "nonpersistent.com", "/", base::Time::Now()); |
+ |
+ // Replace the store effectively destroying the current one and forcing it |
+ // to write its data to disk. Then we can see if after loading it again it |
+ // is still there. |
+ DestroyStore(); |
+ // Specify storage policy that makes "nonpersistent.com" session only. |
+ scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
+ new content::MockSpecialStoragePolicy(); |
+ storage_policy->AddSessionOnly( |
+ net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); |
+ store_ = new QuotaPolicyCookieStore( |
+ temp_dir_.path().Append(kTestCookiesFilename), |
+ client_task_runner(), |
+ background_task_runner(), |
+ true, storage_policy.get(), NULL); |
+ |
+ // Reload and test for persistence |
+ std::vector<net::CanonicalCookie*> cookies; |
+ Load(&cookies); |
+ ASSERT_EQ(3U, cookies.size()); |
+ |
+ // Now close the store, and "nonpersistent.com" should be deleted according to |
+ // policy. |
+ DestroyStore(); |
+ store_ = new QuotaPolicyCookieStore( |
+ temp_dir_.path().Append(kTestCookiesFilename), |
+ client_task_runner(), |
+ background_task_runner(), |
+ true, NULL, NULL); |
+ |
+ Load(&cookies); |
+ ASSERT_EQ(2U, cookies.size()); |
+ EXPECT_EQ("foo.com", cookies[0]->Domain()); |
+ EXPECT_EQ("persistent.com", cookies[1]->Domain()); |
+} |
+ |
+} // namespace content |