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

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

Issue 334303002: Using a mock LevelDBTransaction for corruption tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Factored the new browser IDB content test objects into own file. Created 6 years, 6 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.h" 8 #include "base/files/file.h"
9 #include "base/files/file_enumerator.h" 9 #include "base/files/file_enumerator.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/lazy_instance.h"
11 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/strings/string_split.h"
13 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
14 #include "base/test/thread_test_helper.h" 16 #include "base/test/thread_test_helper.h"
15 #include "content/browser/browser_main_loop.h" 17 #include "content/browser/browser_main_loop.h"
18 #include "content/browser/indexed_db/indexed_db_browsertest_mock_factory.h"
19 #include "content/browser/indexed_db/indexed_db_class_factory.h"
16 #include "content/browser/indexed_db/indexed_db_context_impl.h" 20 #include "content/browser/indexed_db/indexed_db_context_impl.h"
17 #include "content/browser/web_contents/web_contents_impl.h" 21 #include "content/browser/web_contents/web_contents_impl.h"
18 #include "content/public/browser/browser_context.h" 22 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/browser_thread.h" 23 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/render_process_host.h" 24 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/storage_partition.h" 25 #include "content/public/browser/storage_partition.h"
22 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/content_switches.h" 27 #include "content/public/common/content_switches.h"
24 #include "content/public/common/url_constants.h" 28 #include "content/public/common/url_constants.h"
25 #include "content/public/test/browser_test_utils.h" 29 #include "content/public/test/browser_test_utils.h"
(...skipping 12 matching lines...) Expand all
38 using webkit_database::DatabaseUtil; 42 using webkit_database::DatabaseUtil;
39 43
40 namespace content { 44 namespace content {
41 45
42 // This browser test is aimed towards exercising the IndexedDB bindings and 46 // This browser test is aimed towards exercising the IndexedDB bindings and
43 // the actual implementation that lives in the browser side. 47 // the actual implementation that lives in the browser side.
44 class IndexedDBBrowserTest : public ContentBrowserTest { 48 class IndexedDBBrowserTest : public ContentBrowserTest {
45 public: 49 public:
46 IndexedDBBrowserTest() : disk_usage_(-1) {} 50 IndexedDBBrowserTest() : disk_usage_(-1) {}
47 51
52 virtual void SetUp() OVERRIDE {
53 GetTestClassFactory()->Reset();
54 IndexedDBClassFactory::SetIndexedDBClassFactoryGetter(GetIDBClassFactory);
55 ContentBrowserTest::SetUp();
56 }
57
58 virtual void TearDown() OVERRIDE {
59 IndexedDBClassFactory::SetIndexedDBClassFactoryGetter(NULL);
60 ContentBrowserTest::TearDown();
61 }
62
63 void FailOperation(FailClass failure_class,
64 FailMethod failure_method,
65 int fail_on_instance_num,
66 int fail_on_call_num) {
67 GetTestClassFactory()->FailOperation(
68 failure_class, failure_method, fail_on_instance_num, fail_on_call_num);
69 }
70
48 void SimpleTest(const GURL& test_url, bool incognito = false) { 71 void SimpleTest(const GURL& test_url, bool incognito = false) {
49 // The test page will perform tests on IndexedDB, then navigate to either 72 // The test page will perform tests on IndexedDB, then navigate to either
50 // a #pass or #fail ref. 73 // a #pass or #fail ref.
51 Shell* the_browser = incognito ? CreateOffTheRecordBrowser() : shell(); 74 Shell* the_browser = incognito ? CreateOffTheRecordBrowser() : shell();
52 75
53 VLOG(0) << "Navigating to URL and blocking."; 76 VLOG(0) << "Navigating to URL and blocking.";
54 NavigateToURLBlockUntilNavigationsComplete(the_browser, test_url, 2); 77 NavigateToURLBlockUntilNavigationsComplete(the_browser, test_url, 2);
55 VLOG(0) << "Navigation done."; 78 VLOG(0) << "Navigation done.";
56 std::string result = 79 std::string result =
57 the_browser->web_contents()->GetLastCommittedURL().ref(); 80 the_browser->web_contents()->GetLastCommittedURL().ref();
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper( 143 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper(
121 BrowserMainLoop::GetInstance()->indexed_db_thread()-> 144 BrowserMainLoop::GetInstance()->indexed_db_thread()->
122 message_loop_proxy())); 145 message_loop_proxy()));
123 EXPECT_TRUE(helper->Run()); 146 EXPECT_TRUE(helper->Run());
124 // Wait for DidGetDiskUsage to be called. 147 // Wait for DidGetDiskUsage to be called.
125 base::MessageLoop::current()->RunUntilIdle(); 148 base::MessageLoop::current()->RunUntilIdle();
126 return disk_usage_; 149 return disk_usage_;
127 } 150 }
128 151
129 private: 152 private:
153 static IndexedDBBrowserTestClassFactory* GetTestClassFactory() {
154 static ::base::LazyInstance<IndexedDBBrowserTestClassFactory>::Leaky
155 s_factory = LAZY_INSTANCE_INITIALIZER;
156 return s_factory.Pointer();
157 }
158
159 static IndexedDBClassFactory* GetIDBClassFactory() {
160 return GetTestClassFactory();
161 }
162
130 virtual void DidGetDiskUsage(int64 bytes) { 163 virtual void DidGetDiskUsage(int64 bytes) {
131 EXPECT_GT(bytes, 0); 164 EXPECT_GT(bytes, 0);
132 disk_usage_ = bytes; 165 disk_usage_ = bytes;
133 } 166 }
134 167
135 int64 disk_usage_; 168 int64 disk_usage_;
136 169
137 DISALLOW_COPY_AND_ASSIGN(IndexedDBBrowserTest); 170 DISALLOW_COPY_AND_ASSIGN(IndexedDBBrowserTest);
138 }; 171 };
139 172
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 << " with " << numErrors << " errors"; 468 << " with " << numErrors << " errors";
436 signal_when_finished->Signal(); 469 signal_when_finished->Signal();
437 } 470 }
438 471
439 const std::string s_corrupt_db_test_prefix = "/corrupt/test/"; 472 const std::string s_corrupt_db_test_prefix = "/corrupt/test/";
440 473
441 static scoped_ptr<net::test_server::HttpResponse> CorruptDBRequestHandler( 474 static scoped_ptr<net::test_server::HttpResponse> CorruptDBRequestHandler(
442 IndexedDBContextImpl* context, 475 IndexedDBContextImpl* context,
443 const GURL& origin_url, 476 const GURL& origin_url,
444 const std::string& path, 477 const std::string& path,
478 IndexedDBBrowserTest* test,
445 const net::test_server::HttpRequest& request) { 479 const net::test_server::HttpRequest& request) {
446
447 std::string request_path; 480 std::string request_path;
448 if (path.find(s_corrupt_db_test_prefix) != std::string::npos) 481 if (path.find(s_corrupt_db_test_prefix) != std::string::npos)
449 request_path = request.relative_url.substr(s_corrupt_db_test_prefix.size()); 482 request_path = request.relative_url.substr(s_corrupt_db_test_prefix.size());
450 else 483 else
451 return scoped_ptr<net::test_server::HttpResponse>(); 484 return scoped_ptr<net::test_server::HttpResponse>();
452 485
453 // Remove the query string if present. 486 // Remove the query string if present.
454 std::string request_query; 487 std::string request_query;
488 std::vector<std::pair<std::string, std::string> > query_params;
455 size_t query_pos = request_path.find('?'); 489 size_t query_pos = request_path.find('?');
456 if (query_pos != std::string::npos) { 490 if (query_pos != std::string::npos) {
457 request_query = request_path.substr(query_pos + 1); 491 request_query = request_path.substr(query_pos + 1);
458 request_path = request_path.substr(0, query_pos); 492 request_path = request_path.substr(0, query_pos);
493 base::SplitStringIntoKeyValuePairs(request_query, '=', '&', &query_params);
459 } 494 }
460 495
461 if (request_path == "corruptdb" && !request_query.empty()) { 496 if (request_path == "corruptdb" && !request_query.empty()) {
462 VLOG(0) << "Requested to corrupt IndexedDB: " << request_query; 497 VLOG(0) << "Requested to corrupt IndexedDB: " << request_query;
463 base::WaitableEvent signal_when_finished(false, false); 498 base::WaitableEvent signal_when_finished(false, false);
464 context->TaskRunner()->PostTask(FROM_HERE, 499 context->TaskRunner()->PostTask(FROM_HERE,
465 base::Bind(&CorruptIndexedDBDatabase, 500 base::Bind(&CorruptIndexedDBDatabase,
466 base::ConstRef(context), 501 base::ConstRef(context),
467 origin_url, 502 origin_url,
468 &signal_when_finished)); 503 &signal_when_finished));
469 signal_when_finished.Wait(); 504 signal_when_finished.Wait();
470 505
471 scoped_ptr<net::test_server::BasicHttpResponse> http_response( 506 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
472 new net::test_server::BasicHttpResponse); 507 new net::test_server::BasicHttpResponse);
473 http_response->set_code(net::HTTP_OK); 508 http_response->set_code(net::HTTP_OK);
474 return http_response.PassAs<net::test_server::HttpResponse>(); 509 return http_response.PassAs<net::test_server::HttpResponse>();
510 } else if (request_path == "fail" && !query_params.empty()) {
511 FailClass failure_class = FAIL_CLASS_NOTHING;
512 FailMethod failure_method = FAIL_METHOD_NOTHING;
513 int instance_num = 1;
514 int call_num = 1;
515 std::string fail_class;
516 std::string fail_method;
517
518 for (std::vector<std::pair<std::string, std::string> >::iterator it =
519 query_params.begin();
520 it != query_params.end();
521 it++) {
522 if (it->first == "method")
523 fail_method = it->second;
524 else if (it->first == "class")
525 fail_class = it->second;
526 else if (it->first == "instNum")
527 instance_num = atoi(it->second.c_str());
528 else if (it->first == "callNum")
529 call_num = atoi(it->second.c_str());
530 else {
jsbell 2014/06/18 17:03:08 Nit: If any if/elseif/else branch has braces, all
531 VLOG(0) << "Unknown param: \"" << it->first << '"';
532 NOTREACHED();
jsbell 2014/06/18 17:03:08 Just `NOTREACHED() << message` ? (And then it can
cmumford 2014/06/18 18:06:44 Done.
533 }
534 }
535
536 if (fail_class == "LevelDBTransaction") {
537 failure_class = FAIL_CLASS_LEVELDB_TRANSACTION;
538 if (fail_method == "Get")
539 failure_method = FAIL_METHOD_GET;
540 else if (fail_method == "Commit")
541 failure_method = FAIL_METHOD_COMMIT;
542 else {
jsbell 2014/06/18 17:03:08 Ditto.
543 VLOG(0) << "Unknown method: \"" << fail_method << "\"";
544 NOTREACHED();
jsbell 2014/06/18 17:03:08 NOTREACHED() << ... ?
cmumford 2014/06/18 18:06:44 Done.
545 }
546 }
547
548 DCHECK(instance_num >= 1);
549 DCHECK(call_num >= 1);
550
551 test->FailOperation(failure_class, failure_method, instance_num, call_num);
552
553 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
554 new net::test_server::BasicHttpResponse);
555 http_response->set_code(net::HTTP_OK);
556 return http_response.PassAs<net::test_server::HttpResponse>();
475 } 557 }
476 558
477 // A request for a test resource 559 // A request for a test resource
478 base::FilePath resourcePath = 560 base::FilePath resourcePath =
479 content::GetTestFilePath("indexeddb", request_path.c_str()); 561 content::GetTestFilePath("indexeddb", request_path.c_str());
480 scoped_ptr<net::test_server::BasicHttpResponse> http_response( 562 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
481 new net::test_server::BasicHttpResponse); 563 new net::test_server::BasicHttpResponse);
482 http_response->set_code(net::HTTP_OK); 564 http_response->set_code(net::HTTP_OK);
483 std::string file_contents; 565 std::string file_contents;
484 if (!base::ReadFileToString(resourcePath, &file_contents)) 566 if (!base::ReadFileToString(resourcePath, &file_contents))
(...skipping 10 matching lines...) Expand all
495 577
496 IN_PROC_BROWSER_TEST_P(IndexedDBBrowserCorruptionTest, 578 IN_PROC_BROWSER_TEST_P(IndexedDBBrowserCorruptionTest,
497 OperationOnCorruptedOpenDatabase) { 579 OperationOnCorruptedOpenDatabase) {
498 ASSERT_TRUE(embedded_test_server()->Started() || 580 ASSERT_TRUE(embedded_test_server()->Started() ||
499 embedded_test_server()->InitializeAndWaitUntilReady()); 581 embedded_test_server()->InitializeAndWaitUntilReady());
500 const GURL& origin_url = embedded_test_server()->base_url(); 582 const GURL& origin_url = embedded_test_server()->base_url();
501 embedded_test_server()->RegisterRequestHandler( 583 embedded_test_server()->RegisterRequestHandler(
502 base::Bind(&CorruptDBRequestHandler, 584 base::Bind(&CorruptDBRequestHandler,
503 base::ConstRef(GetContext()), 585 base::ConstRef(GetContext()),
504 origin_url, 586 origin_url,
505 s_corrupt_db_test_prefix)); 587 s_corrupt_db_test_prefix,
588 this));
506 589
507 std::string test_file = s_corrupt_db_test_prefix + 590 std::string test_file = s_corrupt_db_test_prefix +
508 "corrupted_open_db_detection.html#" + GetParam(); 591 "corrupted_open_db_detection.html#" + GetParam();
509 SimpleTest(embedded_test_server()->GetURL(test_file)); 592 SimpleTest(embedded_test_server()->GetURL(test_file));
510 593
511 test_file = s_corrupt_db_test_prefix + "corrupted_open_db_recovery.html"; 594 test_file = s_corrupt_db_test_prefix + "corrupted_open_db_recovery.html";
512 SimpleTest(embedded_test_server()->GetURL(test_file)); 595 SimpleTest(embedded_test_server()->GetURL(test_file));
513 } 596 }
514 597
515 INSTANTIATE_TEST_CASE_P(IndexedDBBrowserCorruptionTestInstantiation, 598 INSTANTIATE_TEST_CASE_P(IndexedDBBrowserCorruptionTestInstantiation,
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 #define MAYBE_RenderThreadShutdownTest DISABLED_RenderThreadShutdownTest 712 #define MAYBE_RenderThreadShutdownTest DISABLED_RenderThreadShutdownTest
630 #else 713 #else
631 #define MAYBE_RenderThreadShutdownTest RenderThreadShutdownTest 714 #define MAYBE_RenderThreadShutdownTest RenderThreadShutdownTest
632 #endif 715 #endif
633 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestSingleProcess, 716 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestSingleProcess,
634 MAYBE_RenderThreadShutdownTest) { 717 MAYBE_RenderThreadShutdownTest) {
635 SimpleTest(GetTestUrl("indexeddb", "shutdown_with_requests.html")); 718 SimpleTest(GetTestUrl("indexeddb", "shutdown_with_requests.html"));
636 } 719 }
637 720
638 } // namespace content 721 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698