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

Side by Side Diff: chrome/browser/net/sqlite_persistent_cookie_store_unittest.cc

Issue 9602001: Reinitialize the cookie database if the meta table gets corrupted. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/net/sqlite_persistent_cookie_store.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 InitializeStore(false); 132 InitializeStore(false);
131 // Put some data - any data - on disk, to have something to remove. 133 // Put some data - any data - on disk, to have something to remove.
132 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); 134 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now());
133 store_->SetClearLocalStateOnExit(true); 135 store_->SetClearLocalStateOnExit(true);
134 DestroyStore(); 136 DestroyStore();
135 137
136 ASSERT_FALSE(file_util::PathExists( 138 ASSERT_FALSE(file_util::PathExists(
137 temp_dir_.path().Append(chrome::kCookieFilename))); 139 temp_dir_.path().Append(chrome::kCookieFilename)));
138 } 140 }
139 141
142 TEST_F(SQLitePersistentCookieStoreTest, TestInvalidMetaTableRecovery) {
143 InitializeStore(false);
144 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now());
145 DestroyStore();
146
147 // Load up the store and verify that it has good data in it.
148 std::vector<net::CookieMonster::CanonicalCookie*> cookies;
149 CreateAndLoad(false, &cookies);
150 ASSERT_EQ(1U, cookies.size());
151 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str());
152 ASSERT_STREQ("A", cookies[0]->Name().c_str());
153 ASSERT_STREQ("B", cookies[0]->Value().c_str());
154 DestroyStore();
155 STLDeleteContainerPointers(cookies.begin(), cookies.end());
156 cookies.clear();
157
158 // Now corrupt the meta table.
159 {
160 sql::Connection db;
161 ASSERT_TRUE(db.Open(temp_dir_.path().Append(chrome::kCookieFilename)));
162 sql::MetaTable meta_table_;
163 meta_table_.Init(&db, 0, 0);
164 meta_table_.SetVersionNumber(0);
165 meta_table_.SetCompatibleVersionNumber(0);
166 db.Close();
167 }
168
169 // Upon loading, the database should be reset to a good, blank state.
170 CreateAndLoad(false, &cookies);
171 ASSERT_EQ(0U, cookies.size());
172
173 // Verify that, after, recovery, the database persists properly.
174 AddCookie("X", "Y", "http://foo.bar", "/", base::Time::Now());
175 DestroyStore();
176 CreateAndLoad(false, &cookies);
177 ASSERT_EQ(1U, cookies.size());
178 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str());
179 ASSERT_STREQ("X", cookies[0]->Name().c_str());
180 ASSERT_STREQ("Y", cookies[0]->Value().c_str());
181 STLDeleteContainerPointers(cookies.begin(), cookies.end());
182 cookies.clear();
183 }
184
140 // Test if data is stored as expected in the SQLite database. 185 // Test if data is stored as expected in the SQLite database.
141 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) { 186 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) {
142 InitializeStore(false); 187 InitializeStore(false);
143 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); 188 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now());
144 // Replace the store effectively destroying the current one and forcing it 189 // Replace the store effectively destroying the current one and forcing it
145 // to write it's data to disk. Then we can see if after loading it again it 190 // to write it's data to disk. Then we can see if after loading it again it
146 // is still there. 191 // is still there.
147 DestroyStore(); 192 DestroyStore();
148 // Reload and test for persistence 193 // Reload and test for persistence
149 std::vector<net::CookieMonster::CanonicalCookie*> cookies; 194 std::vector<net::CookieMonster::CanonicalCookie*> cookies;
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 446
402 EXPECT_FALSE(cookie_map["session-noexpires"]->DoesExpire()); 447 EXPECT_FALSE(cookie_map["session-noexpires"]->DoesExpire());
403 EXPECT_FALSE(cookie_map["session-noexpires"]->IsPersistent()); 448 EXPECT_FALSE(cookie_map["session-noexpires"]->IsPersistent());
404 449
405 EXPECT_TRUE(cookie_map["persistent"]->DoesExpire()); 450 EXPECT_TRUE(cookie_map["persistent"]->DoesExpire());
406 EXPECT_TRUE(cookie_map["persistent"]->IsPersistent()); 451 EXPECT_TRUE(cookie_map["persistent"]->IsPersistent());
407 452
408 STLDeleteContainerPointers(cookies.begin(), cookies.end()); 453 STLDeleteContainerPointers(cookies.begin(), cookies.end());
409 cookies.clear(); 454 cookies.clear();
410 } 455 }
OLDNEW
« no previous file with comments | « chrome/browser/net/sqlite_persistent_cookie_store.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698