Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Unified Diff: net/cookies/cookie_monster_unittest.cc

Issue 1698693002: Make CookieStore no longer threadsafe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@getcookiemonster
Patch Set: merge Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/cookies/cookie_monster_unittest.cc
diff --git a/net/cookies/cookie_monster_unittest.cc b/net/cookies/cookie_monster_unittest.cc
index 8e84af4fe69efc87c57de43e9e9365475a57702b..4f64b2f00c23d9ff86a5526c2dffa8b40541a684 100644
--- a/net/cookies/cookie_monster_unittest.cc
+++ b/net/cookies/cookie_monster_unittest.cc
@@ -102,10 +102,6 @@ INSTANTIATE_TYPED_TEST_CASE_P(CookieMonster,
CookieStoreTest,
CookieMonsterTestTraits);
-INSTANTIATE_TYPED_TEST_CASE_P(CookieMonster,
- MultiThreadedCookieStoreTest,
- CookieMonsterTestTraits);
-
INSTANTIATE_TYPED_TEST_CASE_P(CookieMonsterStrictSecure,
CookieStoreTest,
CookieMonsterEnforcingStrictSecure);
@@ -2465,330 +2461,6 @@ TEST_F(CookieMonsterTest, HistogramCheck) {
EXPECT_EQ(samples2->TotalCount(), samples3->TotalCount());
}
-namespace {
-
-class MultiThreadedCookieMonsterTest : public CookieMonsterTest {
- public:
- MultiThreadedCookieMonsterTest() : other_thread_("CMTthread") {}
-
- // Helper methods for calling the asynchronous CookieMonster methods
- // from a different thread.
-
- void GetAllCookiesTask(CookieMonster* cm, GetCookieListCallback* callback) {
- cm->GetAllCookiesAsync(
- base::Bind(&GetCookieListCallback::Run, base::Unretained(callback)));
- }
-
- void GetAllCookiesForURLTask(CookieMonster* cm,
- const GURL& url,
- GetCookieListCallback* callback) {
- cm->GetAllCookiesForURLAsync(url, base::Bind(&GetCookieListCallback::Run,
- base::Unretained(callback)));
- }
-
- void GetAllCookiesForURLWithOptionsTask(CookieMonster* cm,
- const GURL& url,
- const CookieOptions& options,
- GetCookieListCallback* callback) {
- cm->GetCookieListWithOptionsAsync(
- url, options,
- base::Bind(&GetCookieListCallback::Run, base::Unretained(callback)));
- }
-
- void SetCookieWithDetailsTask(CookieMonster* cm,
- const GURL& url,
- ResultSavingCookieCallback<bool>* callback) {
- // Define the parameters here instead of in the calling fucntion.
- // The maximum number of parameters for Bind function is 6.
- std::string name = "A";
- std::string value = "B";
- std::string domain = std::string();
- std::string path = "/foo";
- base::Time expiration_time = base::Time();
- bool secure = false;
- bool http_only = false;
- bool same_site = false;
- CookiePriority priority = COOKIE_PRIORITY_DEFAULT;
- cm->SetCookieWithDetailsAsync(
- url, name, value, domain, path, base::Time(), expiration_time,
- base::Time(), secure, http_only, same_site,
- false /* enforces strict secure cookies */, priority,
- base::Bind(&ResultSavingCookieCallback<bool>::Run,
- base::Unretained(callback)));
- }
-
- void DeleteAllCreatedBetweenTask(CookieMonster* cm,
- const base::Time& delete_begin,
- const base::Time& delete_end,
- ResultSavingCookieCallback<int>* callback) {
- cm->DeleteAllCreatedBetweenAsync(
- delete_begin, delete_end,
- base::Bind(&ResultSavingCookieCallback<int>::Run,
- base::Unretained(callback)));
- }
-
- void DeleteAllCreatedBetweenForHostTask(
- CookieMonster* cm,
- const base::Time delete_begin,
- const base::Time delete_end,
- const GURL& url,
- ResultSavingCookieCallback<int>* callback) {
- cm->DeleteAllCreatedBetweenForHostAsync(
- delete_begin, delete_end, url,
- base::Bind(&ResultSavingCookieCallback<int>::Run,
- base::Unretained(callback)));
- }
-
- void DeleteCanonicalCookieTask(CookieMonster* cm,
- const CanonicalCookie& cookie,
- ResultSavingCookieCallback<int>* callback) {
- cm->DeleteCanonicalCookieAsync(
- cookie, base::Bind(&ResultSavingCookieCallback<int>::Run,
- base::Unretained(callback)));
- }
-
- protected:
- void RunOnOtherThread(const base::Closure& task) {
- other_thread_.Start();
- other_thread_.task_runner()->PostTask(FROM_HERE, task);
- other_thread_.Stop();
- }
-
- Thread other_thread_;
-};
-
-} // namespace
-
-TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookies) {
- scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
- EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B"));
- CookieList cookies = GetAllCookies(cm.get());
- CookieList::const_iterator it = cookies.begin();
- ASSERT_TRUE(it != cookies.end());
- EXPECT_EQ(http_www_google_.host(), it->Domain());
- EXPECT_EQ("A", it->Name());
- ASSERT_TRUE(++it == cookies.end());
- GetCookieListCallback callback(&other_thread_);
- base::Closure task =
- base::Bind(&MultiThreadedCookieMonsterTest::GetAllCookiesTask,
- base::Unretained(this), cm, &callback);
- RunOnOtherThread(task);
- callback.WaitUntilDone();
- it = callback.cookies().begin();
- ASSERT_TRUE(it != callback.cookies().end());
- EXPECT_EQ(http_www_google_.host(), it->Domain());
- EXPECT_EQ("A", it->Name());
- ASSERT_TRUE(++it == callback.cookies().end());
-}
-
-TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURL) {
- scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
- EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B"));
- CookieList cookies = GetAllCookiesForURL(cm.get(), http_www_google_.url());
- CookieList::const_iterator it = cookies.begin();
- ASSERT_TRUE(it != cookies.end());
- EXPECT_EQ(http_www_google_.host(), it->Domain());
- EXPECT_EQ("A", it->Name());
- ASSERT_TRUE(++it == cookies.end());
- GetCookieListCallback callback(&other_thread_);
- base::Closure task =
- base::Bind(&MultiThreadedCookieMonsterTest::GetAllCookiesForURLTask,
- base::Unretained(this), cm, http_www_google_.url(), &callback);
- RunOnOtherThread(task);
- callback.WaitUntilDone();
- it = callback.cookies().begin();
- ASSERT_TRUE(it != callback.cookies().end());
- EXPECT_EQ(http_www_google_.host(), it->Domain());
- EXPECT_EQ("A", it->Name());
- ASSERT_TRUE(++it == callback.cookies().end());
-}
-
-TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckGetAllCookiesForURLWithOpt) {
- scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
- EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "A=B"));
- CookieOptions options;
- CookieList cookies =
- GetAllCookiesForURLWithOptions(cm.get(), http_www_google_.url(), options);
- CookieList::const_iterator it = cookies.begin();
- ASSERT_TRUE(it != cookies.end());
- EXPECT_EQ(http_www_google_.host(), it->Domain());
- EXPECT_EQ("A", it->Name());
- ASSERT_TRUE(++it == cookies.end());
- GetCookieListCallback callback(&other_thread_);
- base::Closure task = base::Bind(
- &MultiThreadedCookieMonsterTest::GetAllCookiesForURLWithOptionsTask,
- base::Unretained(this), cm, http_www_google_.url(), options, &callback);
- RunOnOtherThread(task);
- callback.WaitUntilDone();
- it = callback.cookies().begin();
- ASSERT_TRUE(it != callback.cookies().end());
- EXPECT_EQ(http_www_google_.host(), it->Domain());
- EXPECT_EQ("A", it->Name());
- ASSERT_TRUE(++it == callback.cookies().end());
-}
-
-TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckSetCookieWithDetails) {
- scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
- EXPECT_TRUE(SetCookieWithDetails(cm.get(), www_google_foo_.url(), "A", "B",
- std::string(), "/foo", base::Time(),
- base::Time(), base::Time(), false, false,
- false, COOKIE_PRIORITY_DEFAULT));
- ResultSavingCookieCallback<bool> callback(&other_thread_);
- base::Closure task =
- base::Bind(&MultiThreadedCookieMonsterTest::SetCookieWithDetailsTask,
- base::Unretained(this), cm, www_google_foo_.url(), &callback);
- RunOnOtherThread(task);
- callback.WaitUntilDone();
- EXPECT_TRUE(callback.result());
-}
-
-TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteAllCreatedBetween) {
- scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
- CookieOptions options;
- Time now = Time::Now();
- EXPECT_TRUE(
- SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
- EXPECT_EQ(1, DeleteAllCreatedBetween(cm.get(), now - TimeDelta::FromDays(99),
- Time()));
- EXPECT_TRUE(
- SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
- ResultSavingCookieCallback<int> callback(&other_thread_);
- base::Closure task =
- base::Bind(&MultiThreadedCookieMonsterTest::DeleteAllCreatedBetweenTask,
- base::Unretained(this), cm, now - TimeDelta::FromDays(99),
- Time(), &callback);
- RunOnOtherThread(task);
- callback.WaitUntilDone();
- EXPECT_EQ(1, callback.result());
-}
-
-TEST_F(MultiThreadedCookieMonsterTest,
- ThreadCheckDeleteAllCreatedBetweenForHost) {
- scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
- GURL url_not_google("http://www.notgoogle.com");
-
- CookieOptions options;
- Time now = Time::Now();
- // ago1 < ago2 < ago3 < now.
- Time ago1 = now - TimeDelta::FromDays(101);
- Time ago2 = now - TimeDelta::FromDays(100);
- Time ago3 = now - TimeDelta::FromDays(99);
-
- // These 3 cookies match the first deletion.
- EXPECT_TRUE(
- SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
- EXPECT_TRUE(
- SetCookieWithOptions(cm.get(), http_www_google_.url(), "C=D", options));
- EXPECT_TRUE(
- SetCookieWithOptions(cm.get(), http_www_google_.url(), "Y=Z", options));
-
- // This cookie does not match host.
- EXPECT_TRUE(SetCookieWithOptions(cm.get(), url_not_google, "E=F", options));
-
- // This cookie does not match time range: [ago3, inf], for first deletion, but
- // matches for the second deletion.
- EXPECT_TRUE(
- cm->SetCookieWithCreationTime(http_www_google_.url(), "G=H", ago2));
-
- // 1. First set of deletions.
- EXPECT_EQ(3, // Deletes A=B, C=D, Y=Z
- DeleteAllCreatedBetweenForHost(cm.get(), ago3, Time::Max(),
- http_www_google_.url()));
-
- EXPECT_TRUE(
- SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
- ResultSavingCookieCallback<int> callback(&other_thread_);
-
- // 2. Second set of deletions.
- base::Closure task = base::Bind(
- &MultiThreadedCookieMonsterTest::DeleteAllCreatedBetweenForHostTask,
- base::Unretained(this), cm, ago1, Time(), http_www_google_.url(),
- &callback);
- RunOnOtherThread(task);
- callback.WaitUntilDone();
- EXPECT_EQ(2, callback.result()); // Deletes A=B, G=H.
-}
-
-TEST_F(MultiThreadedCookieMonsterTest, ThreadCheckDeleteCanonicalCookie) {
- scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
- CookieOptions options;
- EXPECT_TRUE(
- SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
- CookieList cookies = GetAllCookies(cm.get());
- CookieList::iterator it = cookies.begin();
- EXPECT_TRUE(DeleteCanonicalCookie(cm.get(), *it));
-
- EXPECT_TRUE(
- SetCookieWithOptions(cm.get(), http_www_google_.url(), "A=B", options));
- ResultSavingCookieCallback<int> callback(&other_thread_);
- cookies = GetAllCookies(cm.get());
- it = cookies.begin();
- base::Closure task =
- base::Bind(&MultiThreadedCookieMonsterTest::DeleteCanonicalCookieTask,
- base::Unretained(this), cm, *it, &callback);
- RunOnOtherThread(task);
- callback.WaitUntilDone();
- EXPECT_EQ(1, callback.result());
-}
-
-// Ensure that cookies for http, https, ws, and wss all share the same storage
-// and policies when GetAllCookiesForURLAsync is used. This test is part of
-// MultiThreadedCookieMonsterTest in order to test and use
-// GetAllCookiesForURLAsync, but it does not use any additional threads.
-TEST_F(MultiThreadedCookieMonsterTest, GetAllCookiesForURLEffectiveDomain) {
- scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create(
- http_www_google_.url(), kValidCookieLine, Time::Now(), CookieOptions()));
-
- // This cookie will be freed by the CookieMonster.
- std::vector<CanonicalCookie*> cookies = {new CanonicalCookie(*cookie)};
- scoped_refptr<NewMockPersistentCookieStore> store(
- new NewMockPersistentCookieStore);
- scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
-
- CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback;
- ::testing::StrictMock<::testing::MockFunction<void(int)>> checkpoint;
- const std::string key = cookie_util::GetEffectiveDomain(
- http_www_google_.url().scheme(), http_www_google_.url().host());
-
- ::testing::InSequence s;
- EXPECT_CALL(checkpoint, Call(0));
- EXPECT_CALL(*store, Load(::testing::_));
- EXPECT_CALL(*store, LoadCookiesForKey(key, ::testing::_))
- .WillOnce(::testing::SaveArg<1>(&loaded_callback));
- EXPECT_CALL(checkpoint, Call(1));
- // LoadCookiesForKey will never be called after checkpoint.Call(1) although
- // we will call GetAllCookiesForURLAsync again, because all URLs below share
- // the same key.
- EXPECT_CALL(*store, LoadCookiesForKey(::testing::_, ::testing::_)).Times(0);
-
- GetCookieListCallback callback;
- checkpoint.Call(0);
- GetAllCookiesForURLTask(cm.get(), http_www_google_.url(), &callback);
- checkpoint.Call(1);
- // Pass the cookies to the CookieMonster.
- loaded_callback.Run(cookies);
- // Now GetAllCookiesForURLTask is done.
- callback.WaitUntilDone();
- // See that the callback was called with the cookies.
- ASSERT_EQ(1u, callback.cookies().size());
- EXPECT_TRUE(cookie->IsEquivalent(callback.cookies()[0]));
-
- // All urls in |urls| should share the same cookie domain.
- const GURL kUrls[] = {
- http_www_google_.url(), https_www_google_.url(), ws_www_google_.url(),
- wss_www_google_.url(),
- };
- for (const GURL& url : kUrls) {
- // Call the function with |url| and verify it is done synchronously without
- // calling LoadCookiesForKey.
- GetCookieListCallback callback;
- GetAllCookiesForURLTask(cm.get(), url, &callback);
- callback.WaitUntilDone();
- ASSERT_EQ(1u, callback.cookies().size());
- EXPECT_TRUE(cookie->IsEquivalent(callback.cookies()[0]));
- }
-}
-
TEST_F(CookieMonsterTest, InvalidExpiryTime) {
std::string cookie_line =
std::string(kValidCookieLine) + "; expires=Blarg arg arg";

Powered by Google App Engine
This is Rietveld 408576698