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/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
13 #include "base/test/thread_test_helper.h" | 13 #include "base/test/thread_test_helper.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" | 15 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
16 #include "chrome/common/chrome_constants.h" | 16 #include "chrome/common/chrome_constants.h" |
17 #include "content/test/test_browser_thread.h" | 17 #include "content/test/test_browser_thread.h" |
18 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 19 #include "sql/connection.h" |
| 20 #include "sql/meta_table.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 | 22 |
21 using content::BrowserThread; | 23 using content::BrowserThread; |
22 | 24 |
23 class SQLitePersistentCookieStoreTest : public testing::Test { | 25 class SQLitePersistentCookieStoreTest : public testing::Test { |
24 public: | 26 public: |
25 SQLitePersistentCookieStoreTest() | 27 SQLitePersistentCookieStoreTest() |
26 : ui_thread_(BrowserThread::UI), | 28 : ui_thread_(BrowserThread::UI), |
27 db_thread_(BrowserThread::DB), | 29 db_thread_(BrowserThread::DB), |
28 io_thread_(BrowserThread::IO), | 30 io_thread_(BrowserThread::IO), |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 TEST_F(SQLitePersistentCookieStoreTest, RemoveOnDestruction) { | 129 TEST_F(SQLitePersistentCookieStoreTest, RemoveOnDestruction) { |
128 InitializeStore(false); | 130 InitializeStore(false); |
129 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); | 131 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); |
130 store_->SetClearLocalStateOnExit(true); | 132 store_->SetClearLocalStateOnExit(true); |
131 DestroyStore(); | 133 DestroyStore(); |
132 | 134 |
133 ASSERT_FALSE(file_util::PathExists( | 135 ASSERT_FALSE(file_util::PathExists( |
134 temp_dir_.path().Append(chrome::kCookieFilename))); | 136 temp_dir_.path().Append(chrome::kCookieFilename))); |
135 } | 137 } |
136 | 138 |
| 139 TEST_F(SQLitePersistentCookieStoreTest, TestInvalidMetaTableRecovery) { |
| 140 InitializeStore(false); |
| 141 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); |
| 142 DestroyStore(); |
| 143 |
| 144 // Load up the store and verify that it has good data in it. |
| 145 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
| 146 CreateAndLoad(false, &cookies); |
| 147 ASSERT_EQ(1U, cookies.size()); |
| 148 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); |
| 149 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
| 150 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
| 151 DestroyStore(); |
| 152 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
| 153 cookies.clear(); |
| 154 |
| 155 // Now corrupt the meta table. |
| 156 { |
| 157 sql::Connection db; |
| 158 ASSERT_TRUE(db.Open(temp_dir_.path().Append(chrome::kCookieFilename))); |
| 159 sql::MetaTable meta_table_; |
| 160 meta_table_.Init(&db, 0, 0); |
| 161 meta_table_.SetVersionNumber(0); |
| 162 meta_table_.SetCompatibleVersionNumber(0); |
| 163 db.Close(); |
| 164 } |
| 165 |
| 166 // Upon loading, the database should be reset to a good, blank state. |
| 167 CreateAndLoad(false, &cookies); |
| 168 ASSERT_EQ(0U, cookies.size()); |
| 169 |
| 170 // Verify that, after, recovery, the database persists properly. |
| 171 AddCookie("X", "Y", "http://foo.bar", "/", base::Time::Now()); |
| 172 DestroyStore(); |
| 173 CreateAndLoad(false, &cookies); |
| 174 ASSERT_EQ(1U, cookies.size()); |
| 175 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); |
| 176 ASSERT_STREQ("X", cookies[0]->Name().c_str()); |
| 177 ASSERT_STREQ("Y", cookies[0]->Value().c_str()); |
| 178 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
| 179 cookies.clear(); |
| 180 } |
| 181 |
137 // Test if data is stored as expected in the SQLite database. | 182 // Test if data is stored as expected in the SQLite database. |
138 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) { | 183 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) { |
139 InitializeStore(false); | 184 InitializeStore(false); |
140 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); | 185 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); |
141 // Replace the store effectively destroying the current one and forcing it | 186 // Replace the store effectively destroying the current one and forcing it |
142 // to write it's data to disk. Then we can see if after loading it again it | 187 // to write it's data to disk. Then we can see if after loading it again it |
143 // is still there. | 188 // is still there. |
144 DestroyStore(); | 189 DestroyStore(); |
145 // Reload and test for persistence | 190 // Reload and test for persistence |
146 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 191 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 | 442 |
398 EXPECT_FALSE(cookie_map["session-noexpires"]->DoesExpire()); | 443 EXPECT_FALSE(cookie_map["session-noexpires"]->DoesExpire()); |
399 EXPECT_FALSE(cookie_map["session-noexpires"]->IsPersistent()); | 444 EXPECT_FALSE(cookie_map["session-noexpires"]->IsPersistent()); |
400 | 445 |
401 EXPECT_TRUE(cookie_map["persistent"]->DoesExpire()); | 446 EXPECT_TRUE(cookie_map["persistent"]->DoesExpire()); |
402 EXPECT_TRUE(cookie_map["persistent"]->IsPersistent()); | 447 EXPECT_TRUE(cookie_map["persistent"]->IsPersistent()); |
403 | 448 |
404 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 449 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
405 cookies.clear(); | 450 cookies.clear(); |
406 } | 451 } |
OLD | NEW |