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 io_thread_(BrowserThread::IO), |
| 26 event_(false, false) { |
23 } | 27 } |
24 | 28 |
25 protected: | 29 protected: |
| 30 void OnLoaded( |
| 31 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { |
| 32 cookies_ = cookies; |
| 33 event_.Signal(); |
| 34 } |
| 35 |
| 36 bool Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { |
| 37 bool result = |
| 38 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 39 base::Unretained(this))); |
| 40 event_.Wait(); |
| 41 *cookies = cookies_; |
| 42 return result; |
| 43 } |
| 44 |
26 virtual void SetUp() { | 45 virtual void SetUp() { |
27 ui_thread_.Start(); | 46 ui_thread_.Start(); |
28 db_thread_.Start(); | 47 db_thread_.Start(); |
| 48 io_thread_.Start(); |
29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
30 store_ = new SQLitePersistentCookieStore( | 50 store_ = new SQLitePersistentCookieStore( |
31 temp_dir_.path().Append(chrome::kCookieFilename)); | 51 temp_dir_.path().Append(chrome::kCookieFilename)); |
32 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 52 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
33 ASSERT_TRUE(store_->Load(&cookies)); | 53 ASSERT_TRUE(Load(&cookies)); |
34 ASSERT_TRUE(0 == cookies.size()); | 54 ASSERT_TRUE(0 == cookies.size()); |
35 // Make sure the store gets written at least once. | 55 // Make sure the store gets written at least once. |
36 store_->AddCookie( | 56 store_->AddCookie( |
37 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", | 57 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", |
38 "/", std::string(), std::string(), | 58 "/", std::string(), std::string(), |
39 base::Time::Now(), | 59 base::Time::Now(), |
40 base::Time::Now(), | 60 base::Time::Now(), |
41 base::Time::Now(), | 61 base::Time::Now(), |
42 false, false, true)); | 62 false, false, true)); |
43 } | 63 } |
44 | 64 |
45 BrowserThread ui_thread_; | 65 BrowserThread ui_thread_; |
46 BrowserThread db_thread_; | 66 BrowserThread db_thread_; |
| 67 BrowserThread io_thread_; |
| 68 base::WaitableEvent event_; |
| 69 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; |
47 ScopedTempDir temp_dir_; | 70 ScopedTempDir temp_dir_; |
48 scoped_refptr<SQLitePersistentCookieStore> store_; | 71 scoped_refptr<SQLitePersistentCookieStore> store_; |
49 }; | 72 }; |
50 | 73 |
51 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { | 74 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { |
52 store_->SetClearLocalStateOnExit(false); | 75 store_->SetClearLocalStateOnExit(false); |
53 store_ = NULL; | 76 store_ = NULL; |
54 // Make sure we wait until the destructor has run. | 77 // Make sure we wait until the destructor has run. |
55 scoped_refptr<base::ThreadTestHelper> helper( | 78 scoped_refptr<base::ThreadTestHelper> helper( |
56 new base::ThreadTestHelper( | 79 new base::ThreadTestHelper( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 store_ = NULL; | 111 store_ = NULL; |
89 scoped_refptr<base::ThreadTestHelper> helper( | 112 scoped_refptr<base::ThreadTestHelper> helper( |
90 new base::ThreadTestHelper( | 113 new base::ThreadTestHelper( |
91 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 114 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
92 // Make sure we wait until the destructor has run. | 115 // Make sure we wait until the destructor has run. |
93 ASSERT_TRUE(helper->Run()); | 116 ASSERT_TRUE(helper->Run()); |
94 store_ = new SQLitePersistentCookieStore( | 117 store_ = new SQLitePersistentCookieStore( |
95 temp_dir_.path().Append(chrome::kCookieFilename)); | 118 temp_dir_.path().Append(chrome::kCookieFilename)); |
96 | 119 |
97 // Reload and test for persistence | 120 // Reload and test for persistence |
98 ASSERT_TRUE(store_->Load(&cookies)); | 121 ASSERT_TRUE(Load(&cookies)); |
99 ASSERT_EQ(1U, cookies.size()); | 122 ASSERT_EQ(1U, cookies.size()); |
100 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); | 123 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); |
101 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 124 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
102 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 125 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
103 | 126 |
104 // Now delete the cookie and check persistence again. | 127 // Now delete the cookie and check persistence again. |
105 store_->DeleteCookie(*cookies[0]); | 128 store_->DeleteCookie(*cookies[0]); |
106 store_ = NULL; | 129 store_ = NULL; |
107 // Make sure we wait until the destructor has run. | 130 // Make sure we wait until the destructor has run. |
108 ASSERT_TRUE(helper->Run()); | 131 ASSERT_TRUE(helper->Run()); |
109 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 132 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
110 cookies.clear(); | 133 cookies.clear(); |
111 store_ = new SQLitePersistentCookieStore( | 134 store_ = new SQLitePersistentCookieStore( |
112 temp_dir_.path().Append(chrome::kCookieFilename)); | 135 temp_dir_.path().Append(chrome::kCookieFilename)); |
113 | 136 |
114 // Reload and check if the cookie has been removed. | 137 // Reload and check if the cookie has been removed. |
115 ASSERT_TRUE(store_->Load(&cookies)); | 138 ASSERT_TRUE(Load(&cookies)); |
116 ASSERT_EQ(0U, cookies.size()); | 139 ASSERT_EQ(0U, cookies.size()); |
117 } | 140 } |
118 | 141 |
119 // Test that we can force the database to be written by calling Flush(). | 142 // Test that we can force the database to be written by calling Flush(). |
120 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { | 143 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { |
121 // File timestamps don't work well on all platforms, so we'll determine | 144 // 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. | 145 // whether the DB file has been modified by checking its size. |
123 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); | 146 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); |
124 base::PlatformFileInfo info; | 147 base::PlatformFileInfo info; |
125 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); | 148 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 | 201 |
179 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); | 202 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); |
180 | 203 |
181 scoped_refptr<base::ThreadTestHelper> helper( | 204 scoped_refptr<base::ThreadTestHelper> helper( |
182 new base::ThreadTestHelper( | 205 new base::ThreadTestHelper( |
183 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 206 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
184 ASSERT_TRUE(helper->Run()); | 207 ASSERT_TRUE(helper->Run()); |
185 | 208 |
186 ASSERT_EQ(1, counter->callback_count()); | 209 ASSERT_EQ(1, counter->callback_count()); |
187 } | 210 } |
OLD | NEW |