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

Side by Side Diff: content/browser/indexed_db/indexed_db_browsertest.cc

Issue 197333009: Handling LevelDB errors encountered after open. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Commented that DestroyBackingStore leaves non LevelDB files alone Created 6 years, 8 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
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/command_line.h" 6 #include "base/command_line.h"
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/files/file_enumerator.h"
8 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
9 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
10 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/thread_test_helper.h" 13 #include "base/test/thread_test_helper.h"
13 #include "content/browser/browser_main_loop.h" 14 #include "content/browser/browser_main_loop.h"
14 #include "content/browser/indexed_db/indexed_db_context_impl.h" 15 #include "content/browser/indexed_db/indexed_db_context_impl.h"
15 #include "content/browser/web_contents/web_contents_impl.h" 16 #include "content/browser/web_contents/web_contents_impl.h"
16 #include "content/public/browser/browser_context.h" 17 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/storage_partition.h" 20 #include "content/public/browser/storage_partition.h"
20 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/content_switches.h" 22 #include "content/public/common/content_switches.h"
22 #include "content/public/common/url_constants.h" 23 #include "content/public/common/url_constants.h"
23 #include "content/public/test/browser_test_utils.h" 24 #include "content/public/test/browser_test_utils.h"
24 #include "content/shell/browser/shell.h" 25 #include "content/shell/browser/shell.h"
25 #include "content/test/content_browser_test.h" 26 #include "content/test/content_browser_test.h"
26 #include "content/test/content_browser_test_utils.h" 27 #include "content/test/content_browser_test_utils.h"
28 #include "net/base/net_errors.h"
29 #include "net/test/embedded_test_server/embedded_test_server.h"
30 #include "net/test/embedded_test_server/http_request.h"
31 #include "net/test/embedded_test_server/http_response.h"
27 #include "webkit/browser/database/database_util.h" 32 #include "webkit/browser/database/database_util.h"
28 #include "webkit/browser/quota/quota_manager.h" 33 #include "webkit/browser/quota/quota_manager.h"
29 34
30 using base::ASCIIToUTF16; 35 using base::ASCIIToUTF16;
31 using quota::QuotaManager; 36 using quota::QuotaManager;
32 using webkit_database::DatabaseUtil; 37 using webkit_database::DatabaseUtil;
33 38
34 namespace content { 39 namespace content {
35 40
36 // This browser test is aimed towards exercising the IndexedDB bindings and 41 // This browser test is aimed towards exercising the IndexedDB bindings and
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 354
350 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CanDeleteWhenOverQuotaTest) { 355 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CanDeleteWhenOverQuotaTest) {
351 SimpleTest(GetTestUrl("indexeddb", "fill_up_5k.html")); 356 SimpleTest(GetTestUrl("indexeddb", "fill_up_5k.html"));
352 int64 size = RequestDiskUsage(); 357 int64 size = RequestDiskUsage();
353 const int kQuotaKilobytes = 2; 358 const int kQuotaKilobytes = 2;
354 EXPECT_GT(size, kQuotaKilobytes * 1024); 359 EXPECT_GT(size, kQuotaKilobytes * 1024);
355 SetQuota(kQuotaKilobytes); 360 SetQuota(kQuotaKilobytes);
356 SimpleTest(GetTestUrl("indexeddb", "delete_over_quota.html")); 361 SimpleTest(GetTestUrl("indexeddb", "delete_over_quota.html"));
357 } 362 }
358 363
364 namespace {
365
366 static void CompactIndexedDBBackingStore(
367 scoped_refptr<IndexedDBContextImpl> context,
368 const GURL& origin_url) {
369 IndexedDBFactory* factory = context->GetIDBFactory();
370
371 std::pair<IndexedDBFactory::OriginDBMapIterator,
372 IndexedDBFactory::OriginDBMapIterator> range =
373 factory->GetOpenDatabasesForOrigin(origin_url);
374
375 if (range.first == range.second) // If no open db's for this origin
376 return;
377
378 // Compact the first db's backing store since all the db's are in the same
379 // backing store.
380 IndexedDBDatabase* db = range.first->second;
381 IndexedDBBackingStore* backing_store = db->backing_store();
382 backing_store->Compact();
383 }
384
385 static void CorruptIndexedDBDatabase(
386 IndexedDBContextImpl* context,
387 const GURL& origin_url,
388 base::WaitableEvent* signal_when_finished) {
389
390 CompactIndexedDBBackingStore(context, origin_url);
391
392 int numFiles = 0;
393 int numErrors = 0;
394 base::FilePath idb_data_path = context->GetFilePath(origin_url);
395 const bool recursive = false;
396 base::FileEnumerator enumerator(
397 idb_data_path, recursive, base::FileEnumerator::FILES);
398 for (base::FilePath idb_file = enumerator.Next(); !idb_file.empty();
399 idb_file = enumerator.Next()) {
400 int64 size(0);
401 GetFileSize(idb_file, &size);
402
403 if (idb_file.Extension() == ".ldb") {
404 numFiles++;
405 base::ScopedFILE f(base::OpenFile(idb_file, "w"));
406 if (f) {
407 char zero(0);
408 if (size != (int64)fwrite(&zero, sizeof(zero), size, f.get()))
409 numErrors++;
410 } else {
411 numErrors++;
412 }
413 }
414 }
415
416 VLOG(0) << "There were " << numFiles << " in " << idb_data_path.value()
417 << " with " << numErrors << " errors";
418 signal_when_finished->Signal();
419 }
420
421 const std::string s_corrupt_db_test_prefix = "/corrupt/test/";
422
423 static scoped_ptr<net::test_server::HttpResponse> CorruptDBRequestHandler(
424 IndexedDBContextImpl* context,
425 const GURL& origin_url,
426 const std::string& path,
427 const net::test_server::HttpRequest& request) {
428
429 std::string request_path;
430 if (path.find(s_corrupt_db_test_prefix) != std::string::npos)
431 request_path = request.relative_url.substr(s_corrupt_db_test_prefix.size());
432 else
433 return scoped_ptr<net::test_server::HttpResponse>();
434
435 // Remove the query string if present.
436 std::string request_query;
437 size_t query_pos = request_path.find('?');
438 if (query_pos != std::string::npos) {
439 request_query = request_path.substr(query_pos + 1);
440 request_path = request_path.substr(0, query_pos);
441 }
442
443 if (request_path == "corruptdb" && !request_query.empty()) {
444 VLOG(0) << "Requested to corrupt IndexedDB: " << request_query;
445 base::WaitableEvent signal_when_finished(false, false);
446 context->TaskRunner()->PostTask(FROM_HERE,
447 base::Bind(&CorruptIndexedDBDatabase,
448 base::ConstRef(context),
449 origin_url,
450 &signal_when_finished));
451 signal_when_finished.Wait();
452
453 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
454 new net::test_server::BasicHttpResponse);
455 http_response->set_code(net::HTTP_OK);
456 return http_response.PassAs<net::test_server::HttpResponse>();
457 }
458
459 // A request for a test resource
460 base::FilePath resourcePath =
461 content::GetTestFilePath("indexeddb", request_path.c_str());
462 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
463 new net::test_server::BasicHttpResponse);
464 http_response->set_code(net::HTTP_OK);
465 std::string file_contents;
466 if (!base::ReadFileToString(resourcePath, &file_contents))
467 return scoped_ptr<net::test_server::HttpResponse>();
468 http_response->set_content(file_contents);
469 return http_response.PassAs<net::test_server::HttpResponse>();
470 }
471
472 } // namespace
473
474 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CorruptedOpenDatabase) {
475 ASSERT_TRUE(embedded_test_server()->Started() ||
476 embedded_test_server()->InitializeAndWaitUntilReady());
477 const GURL& origin_url = embedded_test_server()->base_url();
478 embedded_test_server()->RegisterRequestHandler(
479 base::Bind(&CorruptDBRequestHandler,
480 base::ConstRef(GetContext()),
481 origin_url,
482 s_corrupt_db_test_prefix));
483
484 std::string test_file =
485 s_corrupt_db_test_prefix + "corrupted_open_db_detection.html";
486 SimpleTest(embedded_test_server()->GetURL(test_file));
487
488 test_file = s_corrupt_db_test_prefix + "corrupted_open_db_recovery.html";
489 SimpleTest(embedded_test_server()->GetURL(test_file));
490 }
491
359 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, DeleteCompactsBackingStore) { 492 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, DeleteCompactsBackingStore) {
360 const GURL test_url = GetTestUrl("indexeddb", "delete_compact.html"); 493 const GURL test_url = GetTestUrl("indexeddb", "delete_compact.html");
361 SimpleTest(GURL(test_url.spec() + "#fill")); 494 SimpleTest(GURL(test_url.spec() + "#fill"));
362 int64 after_filling = RequestDiskUsage(); 495 int64 after_filling = RequestDiskUsage();
363 EXPECT_GT(after_filling, 0); 496 EXPECT_GT(after_filling, 0);
364 497
365 SimpleTest(GURL(test_url.spec() + "#purge")); 498 SimpleTest(GURL(test_url.spec() + "#purge"));
366 int64 after_deleting = RequestDiskUsage(); 499 int64 after_deleting = RequestDiskUsage();
367 EXPECT_LT(after_deleting, after_filling); 500 EXPECT_LT(after_deleting, after_filling);
368 501
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 "connection ready"); 575 "connection ready");
443 576
444 GetContext()->TaskRunner()->PostTask( 577 GetContext()->TaskRunner()->PostTask(
445 FROM_HERE, 578 FROM_HERE,
446 base::Bind(&IndexedDBContextImpl::DeleteForOrigin, 579 base::Bind(&IndexedDBContextImpl::DeleteForOrigin,
447 GetContext(), 580 GetContext(),
448 GURL("file:///"))); 581 GURL("file:///")));
449 582
450 base::string16 expected_title16(ASCIIToUTF16("connection closed")); 583 base::string16 expected_title16(ASCIIToUTF16("connection closed"));
451 TitleWatcher title_watcher(shell()->web_contents(), expected_title16); 584 TitleWatcher title_watcher(shell()->web_contents(), expected_title16);
585 title_watcher.AlsoWaitForTitle(ASCIIToUTF16("connection closed with error"));
452 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); 586 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
453 } 587 }
454 588
455 class IndexedDBBrowserTestSingleProcess : public IndexedDBBrowserTest { 589 class IndexedDBBrowserTestSingleProcess : public IndexedDBBrowserTest {
456 public: 590 public:
457 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 591 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
458 command_line->AppendSwitch(switches::kSingleProcess); 592 command_line->AppendSwitch(switches::kSingleProcess);
459 } 593 }
460 }; 594 };
461 595
462 // Crashing on Android due to kSingleProcess flag: http://crbug.com/342525 596 // Crashing on Android due to kSingleProcess flag: http://crbug.com/342525
463 #if defined(OS_ANDROID) 597 #if defined(OS_ANDROID)
464 #define MAYBE_RenderThreadShutdownTest DISABLED_RenderThreadShutdownTest 598 #define MAYBE_RenderThreadShutdownTest DISABLED_RenderThreadShutdownTest
465 #else 599 #else
466 #define MAYBE_RenderThreadShutdownTest RenderThreadShutdownTest 600 #define MAYBE_RenderThreadShutdownTest RenderThreadShutdownTest
467 #endif 601 #endif
468 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestSingleProcess, 602 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestSingleProcess,
469 MAYBE_RenderThreadShutdownTest) { 603 MAYBE_RenderThreadShutdownTest) {
470 SimpleTest(GetTestUrl("indexeddb", "shutdown_with_requests.html")); 604 SimpleTest(GetTestUrl("indexeddb", "shutdown_with_requests.html"));
471 } 605 }
472 606
473 } // namespace content 607 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698