| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" |
| 5 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 6 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 7 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 8 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 9 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/test/thread_test_helper.h" | 12 #include "base/test/thread_test_helper.h" |
| 11 #include "base/time.h" | 13 #include "base/time.h" |
| 12 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" | 14 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
| 13 #include "chrome/common/chrome_constants.h" | 15 #include "chrome/common/chrome_constants.h" |
| 14 #include "content/browser/browser_thread.h" | 16 #include "content/browser/browser_thread.h" |
| 15 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 19 |
| 18 class SQLitePersistentCookieStoreTest : public testing::Test { | 20 class SQLitePersistentCookieStoreTest : public testing::Test { |
| 19 public: | 21 public: |
| 20 SQLitePersistentCookieStoreTest() | 22 SQLitePersistentCookieStoreTest() |
| 21 : ui_thread_(BrowserThread::UI), | 23 : ui_thread_(BrowserThread::UI), |
| 22 db_thread_(BrowserThread::DB) { | 24 db_thread_(BrowserThread::DB), |
| 25 event_(false, false) { |
| 23 } | 26 } |
| 24 | 27 |
| 25 protected: | 28 protected: |
| 29 void OnLoaded( |
| 30 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { |
| 31 cookies_ = cookies; |
| 32 event_.Signal(); |
| 33 } |
| 34 |
| 35 bool Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { |
| 36 bool result = |
| 37 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 38 base::Unretained(this))); |
| 39 event_.Wait(); |
| 40 *cookies = cookies_; |
| 41 return result; |
| 42 } |
| 43 |
| 26 virtual void SetUp() { | 44 virtual void SetUp() { |
| 27 ui_thread_.Start(); | 45 ui_thread_.Start(); |
| 28 db_thread_.Start(); | 46 db_thread_.Start(); |
| 29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 47 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 30 store_ = new SQLitePersistentCookieStore( | 48 store_ = new SQLitePersistentCookieStore( |
| 31 temp_dir_.path().Append(chrome::kCookieFilename)); | 49 temp_dir_.path().Append(chrome::kCookieFilename)); |
| 32 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 50 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
| 33 ASSERT_TRUE(store_->Load(&cookies)); | 51 ASSERT_TRUE(Load(&cookies)); |
| 34 ASSERT_TRUE(0 == cookies.size()); | 52 ASSERT_TRUE(0 == cookies.size()); |
| 35 // Make sure the store gets written at least once. | 53 // Make sure the store gets written at least once. |
| 36 store_->AddCookie( | 54 store_->AddCookie( |
| 37 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", | 55 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", |
| 38 "/", std::string(), std::string(), | 56 "/", std::string(), std::string(), |
| 39 base::Time::Now(), | 57 base::Time::Now(), |
| 40 base::Time::Now(), | 58 base::Time::Now(), |
| 41 base::Time::Now(), | 59 base::Time::Now(), |
| 42 false, false, true)); | 60 false, false, true)); |
| 43 } | 61 } |
| 44 | 62 |
| 45 BrowserThread ui_thread_; | 63 BrowserThread ui_thread_; |
| 46 BrowserThread db_thread_; | 64 BrowserThread db_thread_; |
| 65 base::WaitableEvent event_; |
| 66 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; |
| 47 ScopedTempDir temp_dir_; | 67 ScopedTempDir temp_dir_; |
| 48 scoped_refptr<SQLitePersistentCookieStore> store_; | 68 scoped_refptr<SQLitePersistentCookieStore> store_; |
| 49 }; | 69 }; |
| 50 | 70 |
| 51 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { | 71 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { |
| 52 store_->SetClearLocalStateOnExit(false); | 72 store_->SetClearLocalStateOnExit(false); |
| 53 store_ = NULL; | 73 store_ = NULL; |
| 54 // Make sure we wait until the destructor has run. | 74 // Make sure we wait until the destructor has run. |
| 55 scoped_refptr<base::ThreadTestHelper> helper( | 75 scoped_refptr<base::ThreadTestHelper> helper( |
| 56 new base::ThreadTestHelper( | 76 new base::ThreadTestHelper( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 store_ = NULL; | 108 store_ = NULL; |
| 89 scoped_refptr<base::ThreadTestHelper> helper( | 109 scoped_refptr<base::ThreadTestHelper> helper( |
| 90 new base::ThreadTestHelper( | 110 new base::ThreadTestHelper( |
| 91 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 111 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
| 92 // Make sure we wait until the destructor has run. | 112 // Make sure we wait until the destructor has run. |
| 93 ASSERT_TRUE(helper->Run()); | 113 ASSERT_TRUE(helper->Run()); |
| 94 store_ = new SQLitePersistentCookieStore( | 114 store_ = new SQLitePersistentCookieStore( |
| 95 temp_dir_.path().Append(chrome::kCookieFilename)); | 115 temp_dir_.path().Append(chrome::kCookieFilename)); |
| 96 | 116 |
| 97 // Reload and test for persistence | 117 // Reload and test for persistence |
| 98 ASSERT_TRUE(store_->Load(&cookies)); | 118 ASSERT_TRUE(Load(&cookies)); |
| 99 ASSERT_EQ(1U, cookies.size()); | 119 ASSERT_EQ(1U, cookies.size()); |
| 100 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); | 120 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); |
| 101 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 121 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
| 102 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 122 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
| 103 | 123 |
| 104 // Now delete the cookie and check persistence again. | 124 // Now delete the cookie and check persistence again. |
| 105 store_->DeleteCookie(*cookies[0]); | 125 store_->DeleteCookie(*cookies[0]); |
| 106 store_ = NULL; | 126 store_ = NULL; |
| 107 // Make sure we wait until the destructor has run. | 127 // Make sure we wait until the destructor has run. |
| 108 ASSERT_TRUE(helper->Run()); | 128 ASSERT_TRUE(helper->Run()); |
| 109 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 129 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
| 110 cookies.clear(); | 130 cookies.clear(); |
| 111 store_ = new SQLitePersistentCookieStore( | 131 store_ = new SQLitePersistentCookieStore( |
| 112 temp_dir_.path().Append(chrome::kCookieFilename)); | 132 temp_dir_.path().Append(chrome::kCookieFilename)); |
| 113 | 133 |
| 114 // Reload and check if the cookie has been removed. | 134 // Reload and check if the cookie has been removed. |
| 115 ASSERT_TRUE(store_->Load(&cookies)); | 135 ASSERT_TRUE(Load(&cookies)); |
| 116 ASSERT_EQ(0U, cookies.size()); | 136 ASSERT_EQ(0U, cookies.size()); |
| 117 } | 137 } |
| 118 | 138 |
| 119 // Test that we can force the database to be written by calling Flush(). | 139 // Test that we can force the database to be written by calling Flush(). |
| 120 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { | 140 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { |
| 121 // File timestamps don't work well on all platforms, so we'll determine | 141 // File timestamps don't work well on all platforms, so we'll determine |
| 122 // whether the DB file has been modified by checking its size. | 142 // whether the DB file has been modified by checking its size. |
| 123 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); | 143 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); |
| 124 base::PlatformFileInfo info; | 144 base::PlatformFileInfo info; |
| 125 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); | 145 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 198 |
| 179 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); | 199 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); |
| 180 | 200 |
| 181 scoped_refptr<base::ThreadTestHelper> helper( | 201 scoped_refptr<base::ThreadTestHelper> helper( |
| 182 new base::ThreadTestHelper( | 202 new base::ThreadTestHelper( |
| 183 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 203 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
| 184 ASSERT_TRUE(helper->Run()); | 204 ASSERT_TRUE(helper->Run()); |
| 185 | 205 |
| 186 ASSERT_EQ(1, counter->callback_count()); | 206 ASSERT_EQ(1, counter->callback_count()); |
| 187 } | 207 } |
| OLD | NEW |