diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-08-28 15:28:34 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-08-28 13:54:51 +0000 |
commit | 2a19c63448c84c1805fb1a585c3651318bb86ca7 (patch) | |
tree | eb17888e8531aa6ee5e85721bd553b832a7e5156 /chromium/services/network/session_cleanup_cookie_store_unittest.cc | |
parent | b014812705fc80bff0a5c120dfcef88f349816dc (diff) | |
download | qtwebengine-chromium-2a19c63448c84c1805fb1a585c3651318bb86ca7.tar.gz |
BASELINE: Update Chromium to 69.0.3497.70
Change-Id: I2b7b56e4e7a8b26656930def0d4575dc32b900a0
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/services/network/session_cleanup_cookie_store_unittest.cc')
-rw-r--r-- | chromium/services/network/session_cleanup_cookie_store_unittest.cc | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/chromium/services/network/session_cleanup_cookie_store_unittest.cc b/chromium/services/network/session_cleanup_cookie_store_unittest.cc new file mode 100644 index 00000000000..7c6e4b8f387 --- /dev/null +++ b/chromium/services/network/session_cleanup_cookie_store_unittest.cc @@ -0,0 +1,205 @@ +// Copyright 2018 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 "services/network/session_cleanup_cookie_store.h" +#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/run_loop.h" +#include "base/sequenced_task_runner.h" +#include "base/task_scheduler/post_task.h" +#include "base/task_scheduler/task_scheduler.h" +#include "base/test/scoped_task_environment.h" +#include "base/time/time.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "url/gurl.h" + +namespace network { +namespace { + +using CanonicalCookieVector = + std::vector<std::unique_ptr<net::CanonicalCookie>>; + +const base::FilePath::CharType kTestCookiesFilename[] = + FILE_PATH_LITERAL("Cookies"); + +class SessionCleanupCookieStoreTest : public testing::Test { + public: + SessionCleanupCookieStoreTest() {} + + void OnLoaded(base::RunLoop* run_loop, + CanonicalCookieVector* cookies_out, + CanonicalCookieVector cookies) { + cookies_out->swap(cookies); + run_loop->Quit(); + } + + CanonicalCookieVector Load() { + base::RunLoop run_loop; + CanonicalCookieVector cookies; + store_->Load(base::BindRepeating(&SessionCleanupCookieStoreTest::OnLoaded, + base::Unretained(this), &run_loop, + &cookies)); + run_loop.Run(); + return cookies; + } + + protected: + CanonicalCookieVector CreateAndLoad() { + auto sqlite_store = base::MakeRefCounted<net::SQLitePersistentCookieStore>( + temp_dir_.GetPath().Append(kTestCookiesFilename), + base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()}), + background_task_runner_, true, nullptr); + store_ = + base::MakeRefCounted<SessionCleanupCookieStore>(sqlite_store.get()); + return Load(); + } + + // Adds a persistent cookie to store_. + void AddCookie(const std::string& name, + const std::string& value, + const std::string& domain, + const std::string& path, + base::Time creation) { + store_->AddCookie(net::CanonicalCookie(name, value, domain, path, creation, + creation, base::Time(), false, false, + net::CookieSameSite::DEFAULT_MODE, + net::COOKIE_PRIORITY_DEFAULT)); + } + + void DestroyStore() { + store_ = nullptr; + // Ensure that |store_|'s destructor has run by flushing TaskScheduler. + base::TaskScheduler::GetInstance()->FlushForTesting(); + } + + void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } + + void TearDown() override { DestroyStore(); } + + base::test::ScopedTaskEnvironment scoped_task_environment_; + const scoped_refptr<base::SequencedTaskRunner> background_task_runner_ = + base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()}); + base::ScopedTempDir temp_dir_; + scoped_refptr<SessionCleanupCookieStore> store_; +}; + +TEST_F(SessionCleanupCookieStoreTest, TestPersistence) { + CanonicalCookieVector cookies = CreateAndLoad(); + ASSERT_EQ(0u, cookies.size()); + + base::Time t = base::Time::Now(); + AddCookie("A", "B", "foo.com", "/", t); + t += base::TimeDelta::FromDays(10); + AddCookie("A", "B", "persistent.com", "/", t); + + // Replace the store, which forces the current store to flush data to + // disk. Then, after reloading the store, confirm that the data was flushed by + // making sure it loads successfully. This ensures that all pending commits + // are made to the store before allowing it to be closed. + DestroyStore(); + + // Reload and test for persistence. + cookies = CreateAndLoad(); + EXPECT_EQ(2u, cookies.size()); + bool found_foo_cookie = false; + bool found_persistent_cookie = false; + for (const auto& cookie : cookies) { + if (cookie->Domain() == "foo.com") + found_foo_cookie = true; + else if (cookie->Domain() == "persistent.com") + found_persistent_cookie = true; + } + EXPECT_TRUE(found_foo_cookie); + EXPECT_TRUE(found_persistent_cookie); + + // Now delete the cookies and check persistence again. + store_->DeleteCookie(*cookies[0]); + store_->DeleteCookie(*cookies[1]); + DestroyStore(); + + // Reload and check if the cookies have been removed. + cookies = CreateAndLoad(); + EXPECT_EQ(0u, cookies.size()); + cookies.clear(); +} + +TEST_F(SessionCleanupCookieStoreTest, TestDeleteSessionCookies) { + CanonicalCookieVector cookies = CreateAndLoad(); + ASSERT_EQ(0u, cookies.size()); + + base::Time t = base::Time::Now(); + AddCookie("A", "B", "foo.com", "/", t); + t += base::TimeDelta::FromDays(10); + AddCookie("A", "B", "persistent.com", "/", t); + t += base::TimeDelta::FromDays(10); + AddCookie("A", "B", "nonpersistent.com", "/", t); + + // Replace the store, which forces the current store to flush data to + // disk. Then, after reloading the store, confirm that the data was flushed by + // making sure it loads successfully. This ensures that all pending commits + // are made to the store before allowing it to be closed. + DestroyStore(); + + // Reload and test for persistence. + cookies = CreateAndLoad(); + EXPECT_EQ(3u, cookies.size()); + + t += base::TimeDelta::FromDays(10); + AddCookie("A", "B", "nonpersistent.com", "/second", t); + + // Cookies from "nonpersistent.com" should be deleted. + store_->DeleteSessionCookies( + base::BindRepeating([](const std::string& domain, bool is_https) { + return domain == "nonpersistent.com"; + })); + scoped_task_environment_.RunUntilIdle(); + DestroyStore(); + cookies = CreateAndLoad(); + + EXPECT_EQ(2u, cookies.size()); + for (const auto& cookie : cookies) { + EXPECT_NE("nonpersistent.com", cookie->Domain()); + } + cookies.clear(); +} + +TEST_F(SessionCleanupCookieStoreTest, ForceKeepSessionState) { + CanonicalCookieVector cookies = CreateAndLoad(); + ASSERT_EQ(0u, cookies.size()); + + base::Time t = base::Time::Now(); + AddCookie("A", "B", "foo.com", "/", t); + + // Recreate |store_|, and call DeleteSessionCookies with a function that that + // makes "nonpersistent.com" session only, but then instruct the store to + // forcibly keep all cookies. + + // Reload and test for persistence + DestroyStore(); + cookies = CreateAndLoad(); + EXPECT_EQ(1u, cookies.size()); + + t += base::TimeDelta::FromDays(10); + AddCookie("A", "B", "persistent.com", "/", t); + t += base::TimeDelta::FromDays(10); + AddCookie("A", "B", "nonpersistent.com", "/", t); + + store_->SetForceKeepSessionState(); + // Cookies from "nonpersistent.com" should NOT be deleted. + store_->DeleteSessionCookies( + base::BindRepeating([](const std::string& domain, bool is_https) { + return domain == "nonpersistent.com"; + })); + scoped_task_environment_.RunUntilIdle(); + DestroyStore(); + cookies = CreateAndLoad(); + + EXPECT_EQ(3u, cookies.size()); + cookies.clear(); +} + +} // namespace +} // namespace network |