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

Side by Side Diff: chrome/browser/history/history_unittest.cc

Issue 10823203: Fix downloads db state=3 corruption using version=23 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: version=23 Created 8 years, 4 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
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 // History unit tests come in two flavors: 5 // History unit tests come in two flavors:
6 // 6 //
7 // 1. The more complicated style is that the unit test creates a full history 7 // 1. The more complicated style is that the unit test creates a full history
8 // service. This spawns a background thread for the history backend, and 8 // service. This spawns a background thread for the history backend, and
9 // all communication is asynchronous. This is useful for testing more 9 // all communication is asynchronous. This is useful for testing more
10 // complicated things or end-to-end behavior. 10 // complicated things or end-to-end behavior.
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 history_service_->Cleanup(); 178 history_service_->Cleanup();
179 history_service_ = NULL; 179 history_service_ = NULL;
180 180
181 // Wait for the backend class to terminate before deleting the files and 181 // Wait for the backend class to terminate before deleting the files and
182 // moving to the next test. Note: if this never terminates, somebody is 182 // moving to the next test. Note: if this never terminates, somebody is
183 // probably leaking a reference to the history backend, so it never calls 183 // probably leaking a reference to the history backend, so it never calls
184 // our destroy task. 184 // our destroy task.
185 MessageLoop::current()->Run(); 185 MessageLoop::current()->Run();
186 } 186 }
187 187
188 int64 AddDownload(int32 state, const Time& time) { 188 int64 AddDownload(DownloadItem::DownloadState state, const Time& time) {
189 DownloadPersistentStoreInfo download( 189 DownloadPersistentStoreInfo download(
190 FilePath(FILE_PATH_LITERAL("foo-path")), 190 FilePath(FILE_PATH_LITERAL("foo-path")),
191 GURL("foo-url"), 191 GURL("foo-url"),
192 GURL(""), 192 GURL(""),
193 time, 193 time,
194 time, 194 time,
195 0, 195 0,
196 512, 196 512,
197 state, 197 state,
198 0, 198 0,
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 Time now = Time::Now(); 316 Time now = Time::Now();
317 TimeDelta one_day = TimeDelta::FromDays(1); 317 TimeDelta one_day = TimeDelta::FromDays(1);
318 Time month_ago = now - TimeDelta::FromDays(30); 318 Time month_ago = now - TimeDelta::FromDays(30);
319 319
320 // Initially there should be nothing in the downloads database. 320 // Initially there should be nothing in the downloads database.
321 std::vector<DownloadPersistentStoreInfo> downloads; 321 std::vector<DownloadPersistentStoreInfo> downloads;
322 db_->QueryDownloads(&downloads); 322 db_->QueryDownloads(&downloads);
323 EXPECT_EQ(0U, downloads.size()); 323 EXPECT_EQ(0U, downloads.size());
324 324
325 // Keep track of these as we need to update them later during the test. 325 // Keep track of these as we need to update them later during the test.
326 DownloadID in_progress, removing; 326 DownloadID in_progress;
327 327
328 // Create one with a 0 time. 328 // Create one with a 0 time.
329 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, Time())); 329 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, Time()));
330 // Create one for now and +/- 1 day. 330 // Create one for now and +/- 1 day.
331 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now - one_day)); 331 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now - one_day));
332 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now)); 332 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now));
333 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now + one_day)); 333 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, now + one_day));
334 // Try the other four states. 334 // Try the other four states.
335 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago)); 335 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago));
336 EXPECT_NE(0, in_progress = AddDownload(DownloadItem::IN_PROGRESS, month_ago)); 336 EXPECT_NE(0, in_progress = AddDownload(DownloadItem::IN_PROGRESS, month_ago));
337 EXPECT_NE(0, AddDownload(DownloadItem::CANCELLED, month_ago)); 337 EXPECT_NE(0, AddDownload(DownloadItem::CANCELLED, month_ago));
338 EXPECT_NE(0, AddDownload(DownloadItem::INTERRUPTED, month_ago)); 338 EXPECT_NE(0, AddDownload(DownloadItem::INTERRUPTED, month_ago));
339 EXPECT_NE(0, removing = AddDownload(DownloadItem::REMOVING, month_ago)); 339 EXPECT_EQ(0, AddDownload(DownloadItem::REMOVING, month_ago));
340 340
341 // Test to see if inserts worked. 341 // Test to see if inserts worked.
342 db_->QueryDownloads(&downloads); 342 db_->QueryDownloads(&downloads);
343 EXPECT_EQ(9U, downloads.size()); 343 EXPECT_EQ(8U, downloads.size());
344 344
345 // Try removing from current timestamp. This should delete the one in the 345 // Try removing from current timestamp. This should delete the one in the
346 // future and one very recent one. 346 // future and one very recent one.
347 db_->RemoveDownloadsBetween(now, Time()); 347 db_->RemoveDownloadsBetween(now, Time());
348 db_->QueryDownloads(&downloads); 348 db_->QueryDownloads(&downloads);
349 EXPECT_EQ(7U, downloads.size()); 349 EXPECT_EQ(6U, downloads.size());
350 350
351 // Try removing from two months ago. This should not delete items that are 351 // Try removing from two months ago. This should not delete items that are
352 // 'in progress' or in 'removing' state. 352 // 'in progress' or in 'removing' state.
353 db_->RemoveDownloadsBetween(now - TimeDelta::FromDays(60), Time()); 353 db_->RemoveDownloadsBetween(now - TimeDelta::FromDays(60), Time());
354 db_->QueryDownloads(&downloads); 354 db_->QueryDownloads(&downloads);
355 EXPECT_EQ(3U, downloads.size()); 355 EXPECT_EQ(2U, downloads.size());
356 356
357 // Download manager converts to TimeT, which is lossy, so we do the same 357 // Download manager converts to TimeT, which is lossy, so we do the same
358 // for comparison. 358 // for comparison.
359 Time month_ago_lossy = Time::FromTimeT(month_ago.ToTimeT()); 359 Time month_ago_lossy = Time::FromTimeT(month_ago.ToTimeT());
360 360
361 // Make sure the right values remain. 361 // Make sure the right values remain.
362 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0].state); 362 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0].state);
363 EXPECT_EQ(0, downloads[0].start_time.ToInternalValue()); 363 EXPECT_EQ(0, downloads[0].start_time.ToInternalValue());
364 EXPECT_EQ(DownloadItem::IN_PROGRESS, downloads[1].state); 364 EXPECT_EQ(DownloadItem::IN_PROGRESS, downloads[1].state);
365 EXPECT_EQ(month_ago_lossy.ToInternalValue(), 365 EXPECT_EQ(month_ago_lossy.ToInternalValue(),
366 downloads[1].start_time.ToInternalValue()); 366 downloads[1].start_time.ToInternalValue());
367 EXPECT_EQ(DownloadItem::REMOVING, downloads[2].state);
368 EXPECT_EQ(month_ago_lossy.ToInternalValue(),
369 downloads[2].start_time.ToInternalValue());
370 367
371 // Change state so we can delete the downloads. 368 // Change state so we can delete the downloads.
372 DownloadPersistentStoreInfo data; 369 DownloadPersistentStoreInfo data;
373 data.received_bytes = 512; 370 data.received_bytes = 512;
374 data.state = DownloadItem::COMPLETE; 371 data.state = DownloadItem::COMPLETE;
375 data.end_time = base::Time::Now(); 372 data.end_time = base::Time::Now();
376 data.opened = false; 373 data.opened = false;
377 data.db_handle = in_progress; 374 data.db_handle = in_progress;
378 EXPECT_TRUE(db_->UpdateDownload(data)); 375 EXPECT_TRUE(db_->UpdateDownload(data));
379 data.state = DownloadItem::CANCELLED;
380 data.db_handle = removing;
381 EXPECT_TRUE(db_->UpdateDownload(data));
382 376
383 // Try removing from Time=0. This should delete all. 377 // Try removing from Time=0. This should delete all.
384 db_->RemoveDownloadsBetween(Time(), Time()); 378 db_->RemoveDownloadsBetween(Time(), Time());
385 db_->QueryDownloads(&downloads); 379 db_->QueryDownloads(&downloads);
386 EXPECT_EQ(0U, downloads.size()); 380 EXPECT_EQ(0U, downloads.size());
387 381
388 // Check removal of downloads stuck in IN_PROGRESS state. 382 // Check removal of downloads stuck in IN_PROGRESS state.
389 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago)); 383 EXPECT_NE(0, AddDownload(DownloadItem::COMPLETE, month_ago));
390 EXPECT_NE(0, AddDownload(DownloadItem::IN_PROGRESS, month_ago)); 384 EXPECT_NE(0, AddDownload(DownloadItem::IN_PROGRESS, month_ago));
391 db_->QueryDownloads(&downloads); 385 db_->QueryDownloads(&downloads);
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
956 history_service_ = history; 950 history_service_ = history;
957 history->ScheduleDBTask(task.get(), &request_consumer); 951 history->ScheduleDBTask(task.get(), &request_consumer);
958 request_consumer.CancelAllRequests(); 952 request_consumer.CancelAllRequests();
959 CleanupHistoryService(); 953 CleanupHistoryService();
960 // WARNING: history has now been deleted. 954 // WARNING: history has now been deleted.
961 history = NULL; 955 history = NULL;
962 ASSERT_FALSE(task->done_invoked); 956 ASSERT_FALSE(task->done_invoked);
963 } 957 }
964 958
965 } // namespace history 959 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698