OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 Loading... | |
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. |
jsbell
2014/06/19 21:08:47
Wasn't there a thread about a utility for query st
| |
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 = | |
jsbell
2014/06/19 21:08:47
Might be marginally more readable if the vector<>
cmumford
2014/06/19 22:45:58
I switched to url::ExtractQueryKeyValue, and altho
| |
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 | |
531 NOTREACHED() << "Unknown param: \"" << it->first << "\""; | |
532 } | |
533 | |
534 if (fail_class == "LevelDBTransaction") { | |
535 failure_class = FAIL_CLASS_LEVELDB_TRANSACTION; | |
536 if (fail_method == "Get") | |
537 failure_method = FAIL_METHOD_GET; | |
538 else if (fail_method == "Commit") | |
539 failure_method = FAIL_METHOD_COMMIT; | |
540 else { | |
541 NOTREACHED() << "Unknown method: \"" << fail_method << "\""; | |
542 } | |
543 } | |
544 | |
545 DCHECK(instance_num >= 1); | |
jsbell
2014/06/19 21:08:47
DCHECK_GE
cmumford
2014/06/19 22:17:11
Done.
| |
546 DCHECK(call_num >= 1); | |
jsbell
2014/06/19 21:08:47
DCHECK_GE
cmumford
2014/06/19 22:17:11
Done.
| |
547 | |
548 test->FailOperation(failure_class, failure_method, instance_num, call_num); | |
549 | |
550 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | |
551 new net::test_server::BasicHttpResponse); | |
552 http_response->set_code(net::HTTP_OK); | |
553 return http_response.PassAs<net::test_server::HttpResponse>(); | |
475 } | 554 } |
476 | 555 |
477 // A request for a test resource | 556 // A request for a test resource |
478 base::FilePath resourcePath = | 557 base::FilePath resourcePath = |
479 content::GetTestFilePath("indexeddb", request_path.c_str()); | 558 content::GetTestFilePath("indexeddb", request_path.c_str()); |
480 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | 559 scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
481 new net::test_server::BasicHttpResponse); | 560 new net::test_server::BasicHttpResponse); |
482 http_response->set_code(net::HTTP_OK); | 561 http_response->set_code(net::HTTP_OK); |
483 std::string file_contents; | 562 std::string file_contents; |
484 if (!base::ReadFileToString(resourcePath, &file_contents)) | 563 if (!base::ReadFileToString(resourcePath, &file_contents)) |
(...skipping 10 matching lines...) Expand all Loading... | |
495 | 574 |
496 IN_PROC_BROWSER_TEST_P(IndexedDBBrowserCorruptionTest, | 575 IN_PROC_BROWSER_TEST_P(IndexedDBBrowserCorruptionTest, |
497 OperationOnCorruptedOpenDatabase) { | 576 OperationOnCorruptedOpenDatabase) { |
498 ASSERT_TRUE(embedded_test_server()->Started() || | 577 ASSERT_TRUE(embedded_test_server()->Started() || |
499 embedded_test_server()->InitializeAndWaitUntilReady()); | 578 embedded_test_server()->InitializeAndWaitUntilReady()); |
500 const GURL& origin_url = embedded_test_server()->base_url(); | 579 const GURL& origin_url = embedded_test_server()->base_url(); |
501 embedded_test_server()->RegisterRequestHandler( | 580 embedded_test_server()->RegisterRequestHandler( |
502 base::Bind(&CorruptDBRequestHandler, | 581 base::Bind(&CorruptDBRequestHandler, |
503 base::ConstRef(GetContext()), | 582 base::ConstRef(GetContext()), |
504 origin_url, | 583 origin_url, |
505 s_corrupt_db_test_prefix)); | 584 s_corrupt_db_test_prefix, |
585 this)); | |
506 | 586 |
507 std::string test_file = s_corrupt_db_test_prefix + | 587 std::string test_file = s_corrupt_db_test_prefix + |
508 "corrupted_open_db_detection.html#" + GetParam(); | 588 "corrupted_open_db_detection.html#" + GetParam(); |
509 SimpleTest(embedded_test_server()->GetURL(test_file)); | 589 SimpleTest(embedded_test_server()->GetURL(test_file)); |
510 | 590 |
511 test_file = s_corrupt_db_test_prefix + "corrupted_open_db_recovery.html"; | 591 test_file = s_corrupt_db_test_prefix + "corrupted_open_db_recovery.html"; |
512 SimpleTest(embedded_test_server()->GetURL(test_file)); | 592 SimpleTest(embedded_test_server()->GetURL(test_file)); |
513 } | 593 } |
514 | 594 |
515 INSTANTIATE_TEST_CASE_P(IndexedDBBrowserCorruptionTestInstantiation, | 595 INSTANTIATE_TEST_CASE_P(IndexedDBBrowserCorruptionTestInstantiation, |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
629 #define MAYBE_RenderThreadShutdownTest DISABLED_RenderThreadShutdownTest | 709 #define MAYBE_RenderThreadShutdownTest DISABLED_RenderThreadShutdownTest |
630 #else | 710 #else |
631 #define MAYBE_RenderThreadShutdownTest RenderThreadShutdownTest | 711 #define MAYBE_RenderThreadShutdownTest RenderThreadShutdownTest |
632 #endif | 712 #endif |
633 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestSingleProcess, | 713 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestSingleProcess, |
634 MAYBE_RenderThreadShutdownTest) { | 714 MAYBE_RenderThreadShutdownTest) { |
635 SimpleTest(GetTestUrl("indexeddb", "shutdown_with_requests.html")); | 715 SimpleTest(GetTestUrl("indexeddb", "shutdown_with_requests.html")); |
636 } | 716 } |
637 | 717 |
638 } // namespace content | 718 } // namespace content |
OLD | NEW |