Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_browsertest.cc
blob871d9f50f2f3ef1763e8eb72a22ada68d72b4997
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
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/files/file.h"
8 #include "base/files/file_enumerator.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/lazy_instance.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/test/thread_test_helper.h"
16 #include "content/browser/browser_main_loop.h"
17 #include "content/browser/indexed_db/indexed_db_class_factory.h"
18 #include "content/browser/indexed_db/indexed_db_context_impl.h"
19 #include "content/browser/indexed_db/mock_browsertest_indexed_db_class_factory.h"
20 #include "content/browser/web_contents/web_contents_impl.h"
21 #include "content/public/browser/browser_context.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/storage_partition.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/common/url_constants.h"
28 #include "content/public/test/browser_test_utils.h"
29 #include "content/public/test/content_browser_test.h"
30 #include "content/public/test/content_browser_test_utils.h"
31 #include "content/shell/browser/shell.h"
32 #include "net/base/escape.h"
33 #include "net/base/net_errors.h"
34 #include "net/test/embedded_test_server/embedded_test_server.h"
35 #include "net/test/embedded_test_server/http_request.h"
36 #include "net/test/embedded_test_server/http_response.h"
37 #include "storage/browser/database/database_util.h"
38 #include "storage/browser/quota/quota_manager.h"
40 using base::ASCIIToUTF16;
41 using storage::QuotaManager;
42 using storage::DatabaseUtil;
44 namespace content {
46 // This browser test is aimed towards exercising the IndexedDB bindings and
47 // the actual implementation that lives in the browser side.
48 class IndexedDBBrowserTest : public ContentBrowserTest {
49 public:
50 IndexedDBBrowserTest() : disk_usage_(-1) {}
52 void SetUp() override {
53 GetTestClassFactory()->Reset();
54 IndexedDBClassFactory::SetIndexedDBClassFactoryGetter(GetIDBClassFactory);
55 ContentBrowserTest::SetUp();
58 void TearDown() override {
59 IndexedDBClassFactory::SetIndexedDBClassFactoryGetter(NULL);
60 ContentBrowserTest::TearDown();
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);
71 void SimpleTest(const GURL& test_url, bool incognito = false) {
72 // The test page will perform tests on IndexedDB, then navigate to either
73 // a #pass or #fail ref.
74 Shell* the_browser = incognito ? CreateOffTheRecordBrowser() : shell();
76 VLOG(0) << "Navigating to URL and blocking.";
77 NavigateToURLBlockUntilNavigationsComplete(the_browser, test_url, 2);
78 VLOG(0) << "Navigation done.";
79 std::string result =
80 the_browser->web_contents()->GetLastCommittedURL().ref();
81 if (result != "pass") {
82 std::string js_result;
83 ASSERT_TRUE(ExecuteScriptAndExtractString(
84 the_browser->web_contents(),
85 "window.domAutomationController.send(getLog())",
86 &js_result));
87 FAIL() << "Failed: " << js_result;
91 void NavigateAndWaitForTitle(Shell* shell,
92 const char* filename,
93 const char* hash,
94 const char* expected_string) {
95 GURL url = GetTestUrl("indexeddb", filename);
96 if (hash)
97 url = GURL(url.spec() + hash);
99 base::string16 expected_title16(ASCIIToUTF16(expected_string));
100 TitleWatcher title_watcher(shell->web_contents(), expected_title16);
101 NavigateToURL(shell, url);
102 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
105 IndexedDBContextImpl* GetContext() {
106 StoragePartition* partition =
107 BrowserContext::GetDefaultStoragePartition(
108 shell()->web_contents()->GetBrowserContext());
109 return static_cast<IndexedDBContextImpl*>(partition->GetIndexedDBContext());
112 void SetQuota(int quotaKilobytes) {
113 const int kTemporaryStorageQuotaSize = quotaKilobytes
114 * 1024 * QuotaManager::kPerHostTemporaryPortion;
115 SetTempQuota(kTemporaryStorageQuotaSize,
116 BrowserContext::GetDefaultStoragePartition(
117 shell()->web_contents()->GetBrowserContext())->GetQuotaManager());
120 static void SetTempQuota(int64 bytes, scoped_refptr<QuotaManager> qm) {
121 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
122 BrowserThread::PostTask(
123 BrowserThread::IO, FROM_HERE,
124 base::Bind(&IndexedDBBrowserTest::SetTempQuota, bytes, qm));
125 return;
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
128 qm->SetTemporaryGlobalOverrideQuota(bytes, storage::QuotaCallback());
129 // Don't return until the quota has been set.
130 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper(
131 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
132 ASSERT_TRUE(helper->Run());
135 virtual int64 RequestDiskUsage() {
136 PostTaskAndReplyWithResult(
137 GetContext()->TaskRunner(),
138 FROM_HERE,
139 base::Bind(&IndexedDBContext::GetOriginDiskUsage,
140 GetContext(),
141 GURL("file:///")),
142 base::Bind(&IndexedDBBrowserTest::DidGetDiskUsage, this));
143 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper(
144 BrowserMainLoop::GetInstance()->indexed_db_thread()->
145 message_loop_proxy()));
146 EXPECT_TRUE(helper->Run());
147 // Wait for DidGetDiskUsage to be called.
148 base::MessageLoop::current()->RunUntilIdle();
149 return disk_usage_;
152 private:
153 static MockBrowserTestIndexedDBClassFactory* GetTestClassFactory() {
154 static ::base::LazyInstance<MockBrowserTestIndexedDBClassFactory>::Leaky
155 s_factory = LAZY_INSTANCE_INITIALIZER;
156 return s_factory.Pointer();
159 static IndexedDBClassFactory* GetIDBClassFactory() {
160 return GetTestClassFactory();
163 virtual void DidGetDiskUsage(int64 bytes) {
164 EXPECT_GT(bytes, 0);
165 disk_usage_ = bytes;
168 int64 disk_usage_;
170 DISALLOW_COPY_AND_ASSIGN(IndexedDBBrowserTest);
173 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CursorTest) {
174 SimpleTest(GetTestUrl("indexeddb", "cursor_test.html"));
177 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CursorTestIncognito) {
178 SimpleTest(GetTestUrl("indexeddb", "cursor_test.html"),
179 true /* incognito */);
182 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CursorPrefetch) {
183 SimpleTest(GetTestUrl("indexeddb", "cursor_prefetch.html"));
186 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, IndexTest) {
187 SimpleTest(GetTestUrl("indexeddb", "index_test.html"));
190 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, KeyPathTest) {
191 SimpleTest(GetTestUrl("indexeddb", "key_path_test.html"));
194 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, TransactionGetTest) {
195 SimpleTest(GetTestUrl("indexeddb", "transaction_get_test.html"));
198 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, KeyTypesTest) {
199 SimpleTest(GetTestUrl("indexeddb", "key_types_test.html"));
202 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, ObjectStoreTest) {
203 SimpleTest(GetTestUrl("indexeddb", "object_store_test.html"));
206 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, DatabaseTest) {
207 SimpleTest(GetTestUrl("indexeddb", "database_test.html"));
210 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, TransactionTest) {
211 SimpleTest(GetTestUrl("indexeddb", "transaction_test.html"));
214 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CallbackAccounting) {
215 SimpleTest(GetTestUrl("indexeddb", "callback_accounting.html"));
218 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, DoesntHangTest) {
219 SimpleTest(GetTestUrl("indexeddb", "transaction_run_forever.html"));
220 CrashTab(shell()->web_contents());
221 SimpleTest(GetTestUrl("indexeddb", "transaction_not_blocked.html"));
224 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, Bug84933Test) {
225 const GURL url = GetTestUrl("indexeddb", "bug_84933.html");
227 // Just navigate to the URL. Test will crash if it fails.
228 NavigateToURLBlockUntilNavigationsComplete(shell(), url, 1);
231 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, Bug106883Test) {
232 const GURL url = GetTestUrl("indexeddb", "bug_106883.html");
234 // Just navigate to the URL. Test will crash if it fails.
235 NavigateToURLBlockUntilNavigationsComplete(shell(), url, 1);
238 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, Bug109187Test) {
239 const GURL url = GetTestUrl("indexeddb", "bug_109187.html");
241 // Just navigate to the URL. Test will crash if it fails.
242 NavigateToURLBlockUntilNavigationsComplete(shell(), url, 1);
245 class IndexedDBBrowserTestWithLowQuota : public IndexedDBBrowserTest {
246 public:
247 IndexedDBBrowserTestWithLowQuota() {}
249 void SetUpOnMainThread() override {
250 const int kInitialQuotaKilobytes = 5000;
251 SetQuota(kInitialQuotaKilobytes);
254 private:
255 DISALLOW_COPY_AND_ASSIGN(IndexedDBBrowserTestWithLowQuota);
258 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithLowQuota, QuotaTest) {
259 SimpleTest(GetTestUrl("indexeddb", "quota_test.html"));
262 class IndexedDBBrowserTestWithGCExposed : public IndexedDBBrowserTest {
263 public:
264 IndexedDBBrowserTestWithGCExposed() {}
266 void SetUpCommandLine(CommandLine* command_line) override {
267 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc");
270 private:
271 DISALLOW_COPY_AND_ASSIGN(IndexedDBBrowserTestWithGCExposed);
274 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithGCExposed,
275 DatabaseCallbacksTest) {
276 SimpleTest(GetTestUrl("indexeddb", "database_callbacks_first.html"));
279 static void CopyLevelDBToProfile(Shell* shell,
280 scoped_refptr<IndexedDBContextImpl> context,
281 const std::string& test_directory) {
282 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread());
283 base::FilePath leveldb_dir(FILE_PATH_LITERAL("file__0.indexeddb.leveldb"));
284 base::FilePath test_data_dir =
285 GetTestFilePath("indexeddb", test_directory.c_str()).Append(leveldb_dir);
286 base::FilePath dest = context->data_path().Append(leveldb_dir);
287 // If we don't create the destination directory first, the contents of the
288 // leveldb directory are copied directly into profile/IndexedDB instead of
289 // profile/IndexedDB/file__0.xxx/
290 ASSERT_TRUE(base::CreateDirectory(dest));
291 const bool kRecursive = true;
292 ASSERT_TRUE(base::CopyDirectory(test_data_dir,
293 context->data_path(),
294 kRecursive));
297 class IndexedDBBrowserTestWithPreexistingLevelDB : public IndexedDBBrowserTest {
298 public:
299 IndexedDBBrowserTestWithPreexistingLevelDB() {}
300 void SetUpOnMainThread() override {
301 scoped_refptr<IndexedDBContextImpl> context = GetContext();
302 context->TaskRunner()->PostTask(
303 FROM_HERE,
304 base::Bind(
305 &CopyLevelDBToProfile, shell(), context, EnclosingLevelDBDir()));
306 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper(
307 BrowserMainLoop::GetInstance()->indexed_db_thread()->
308 message_loop_proxy()));
309 ASSERT_TRUE(helper->Run());
312 virtual std::string EnclosingLevelDBDir() = 0;
314 private:
315 DISALLOW_COPY_AND_ASSIGN(IndexedDBBrowserTestWithPreexistingLevelDB);
318 class IndexedDBBrowserTestWithVersion0Schema : public
319 IndexedDBBrowserTestWithPreexistingLevelDB {
320 std::string EnclosingLevelDBDir() override { return "migration_from_0"; }
323 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithVersion0Schema, MigrationTest) {
324 SimpleTest(GetTestUrl("indexeddb", "migration_test.html"));
327 class IndexedDBBrowserTestWithVersion123456Schema : public
328 IndexedDBBrowserTestWithPreexistingLevelDB {
329 std::string EnclosingLevelDBDir() override { return "schema_version_123456"; }
332 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithVersion123456Schema,
333 DestroyTest) {
334 int64 original_size = RequestDiskUsage();
335 EXPECT_GT(original_size, 0);
336 SimpleTest(GetTestUrl("indexeddb", "open_bad_db.html"));
337 int64 new_size = RequestDiskUsage();
338 EXPECT_NE(original_size, new_size);
341 class IndexedDBBrowserTestWithVersion987654SSVData : public
342 IndexedDBBrowserTestWithPreexistingLevelDB {
343 std::string EnclosingLevelDBDir() override { return "ssv_version_987654"; }
346 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithVersion987654SSVData,
347 DestroyTest) {
348 int64 original_size = RequestDiskUsage();
349 EXPECT_GT(original_size, 0);
350 SimpleTest(GetTestUrl("indexeddb", "open_bad_db.html"));
351 int64 new_size = RequestDiskUsage();
352 EXPECT_NE(original_size, new_size);
355 class IndexedDBBrowserTestWithCorruptLevelDB : public
356 IndexedDBBrowserTestWithPreexistingLevelDB {
357 std::string EnclosingLevelDBDir() override { return "corrupt_leveldb"; }
360 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithCorruptLevelDB,
361 DestroyTest) {
362 int64 original_size = RequestDiskUsage();
363 EXPECT_GT(original_size, 0);
364 SimpleTest(GetTestUrl("indexeddb", "open_bad_db.html"));
365 int64 new_size = RequestDiskUsage();
366 EXPECT_NE(original_size, new_size);
369 class IndexedDBBrowserTestWithMissingSSTFile : public
370 IndexedDBBrowserTestWithPreexistingLevelDB {
371 std::string EnclosingLevelDBDir() override { return "missing_sst"; }
374 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithMissingSSTFile,
375 DestroyTest) {
376 int64 original_size = RequestDiskUsage();
377 EXPECT_GT(original_size, 0);
378 SimpleTest(GetTestUrl("indexeddb", "open_missing_table.html"));
379 int64 new_size = RequestDiskUsage();
380 EXPECT_NE(original_size, new_size);
383 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, LevelDBLogFileTest) {
384 // Any page that opens an IndexedDB will work here.
385 SimpleTest(GetTestUrl("indexeddb", "database_test.html"));
386 base::FilePath leveldb_dir(FILE_PATH_LITERAL("file__0.indexeddb.leveldb"));
387 base::FilePath log_file(FILE_PATH_LITERAL("LOG"));
388 base::FilePath log_file_path =
389 GetContext()->data_path().Append(leveldb_dir).Append(log_file);
390 int64 size;
391 EXPECT_TRUE(base::GetFileSize(log_file_path, &size));
392 EXPECT_GT(size, 0);
395 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CanDeleteWhenOverQuotaTest) {
396 SimpleTest(GetTestUrl("indexeddb", "fill_up_5k.html"));
397 int64 size = RequestDiskUsage();
398 const int kQuotaKilobytes = 2;
399 EXPECT_GT(size, kQuotaKilobytes * 1024);
400 SetQuota(kQuotaKilobytes);
401 SimpleTest(GetTestUrl("indexeddb", "delete_over_quota.html"));
404 namespace {
406 static void CompactIndexedDBBackingStore(
407 scoped_refptr<IndexedDBContextImpl> context,
408 const GURL& origin_url) {
409 IndexedDBFactory* factory = context->GetIDBFactory();
411 std::pair<IndexedDBFactory::OriginDBMapIterator,
412 IndexedDBFactory::OriginDBMapIterator> range =
413 factory->GetOpenDatabasesForOrigin(origin_url);
415 if (range.first == range.second) // If no open db's for this origin
416 return;
418 // Compact the first db's backing store since all the db's are in the same
419 // backing store.
420 IndexedDBDatabase* db = range.first->second;
421 IndexedDBBackingStore* backing_store = db->backing_store();
422 backing_store->Compact();
425 static void CorruptIndexedDBDatabase(
426 IndexedDBContextImpl* context,
427 const GURL& origin_url,
428 base::WaitableEvent* signal_when_finished) {
430 CompactIndexedDBBackingStore(context, origin_url);
432 int numFiles = 0;
433 int numErrors = 0;
434 base::FilePath idb_data_path = context->GetFilePath(origin_url);
435 const bool recursive = false;
436 base::FileEnumerator enumerator(
437 idb_data_path, recursive, base::FileEnumerator::FILES);
438 for (base::FilePath idb_file = enumerator.Next(); !idb_file.empty();
439 idb_file = enumerator.Next()) {
440 int64 size(0);
441 GetFileSize(idb_file, &size);
443 if (idb_file.Extension() == FILE_PATH_LITERAL(".ldb")) {
444 numFiles++;
445 base::File file(idb_file,
446 base::File::FLAG_WRITE | base::File::FLAG_OPEN_TRUNCATED);
447 if (file.IsValid()) {
448 // Was opened truncated, expand back to the original
449 // file size and fill with zeros (corrupting the file).
450 file.SetLength(size);
451 } else {
452 numErrors++;
457 VLOG(0) << "There were " << numFiles << " in " << idb_data_path.value()
458 << " with " << numErrors << " errors";
459 signal_when_finished->Signal();
462 const std::string s_corrupt_db_test_prefix = "/corrupt/test/";
464 static scoped_ptr<net::test_server::HttpResponse> CorruptDBRequestHandler(
465 IndexedDBContextImpl* context,
466 const GURL& origin_url,
467 const std::string& path,
468 IndexedDBBrowserTest* test,
469 const net::test_server::HttpRequest& request) {
470 std::string request_path;
471 if (path.find(s_corrupt_db_test_prefix) != std::string::npos)
472 request_path = request.relative_url.substr(s_corrupt_db_test_prefix.size());
473 else
474 return scoped_ptr<net::test_server::HttpResponse>();
476 // Remove the query string if present.
477 std::string request_query;
478 size_t query_pos = request_path.find('?');
479 if (query_pos != std::string::npos) {
480 request_query = request_path.substr(query_pos + 1);
481 request_path = request_path.substr(0, query_pos);
484 if (request_path == "corruptdb" && !request_query.empty()) {
485 VLOG(0) << "Requested to corrupt IndexedDB: " << request_query;
486 base::WaitableEvent signal_when_finished(false, false);
487 context->TaskRunner()->PostTask(FROM_HERE,
488 base::Bind(&CorruptIndexedDBDatabase,
489 base::ConstRef(context),
490 origin_url,
491 &signal_when_finished));
492 signal_when_finished.Wait();
494 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
495 new net::test_server::BasicHttpResponse);
496 http_response->set_code(net::HTTP_OK);
497 return http_response.Pass();
498 } else if (request_path == "fail" && !request_query.empty()) {
499 FailClass failure_class = FAIL_CLASS_NOTHING;
500 FailMethod failure_method = FAIL_METHOD_NOTHING;
501 int instance_num = 1;
502 int call_num = 1;
503 std::string fail_class;
504 std::string fail_method;
506 url::Component query(0, request_query.length()), key_pos, value_pos;
507 while (url::ExtractQueryKeyValue(
508 request_query.c_str(), &query, &key_pos, &value_pos)) {
509 std::string escaped_key(request_query.substr(key_pos.begin, key_pos.len));
510 std::string escaped_value(
511 request_query.substr(value_pos.begin, value_pos.len));
513 std::string key = net::UnescapeURLComponent(
514 escaped_key,
515 net::UnescapeRule::NORMAL | net::UnescapeRule::SPACES |
516 net::UnescapeRule::URL_SPECIAL_CHARS);
518 std::string value = net::UnescapeURLComponent(
519 escaped_value,
520 net::UnescapeRule::NORMAL | net::UnescapeRule::SPACES |
521 net::UnescapeRule::URL_SPECIAL_CHARS);
523 if (key == "method")
524 fail_method = value;
525 else if (key == "class")
526 fail_class = value;
527 else if (key == "instNum")
528 instance_num = atoi(value.c_str());
529 else if (key == "callNum")
530 call_num = atoi(value.c_str());
531 else
532 NOTREACHED() << "Unknown param: \"" << key << "\"";
535 if (fail_class == "LevelDBTransaction") {
536 failure_class = FAIL_CLASS_LEVELDB_TRANSACTION;
537 if (fail_method == "Get")
538 failure_method = FAIL_METHOD_GET;
539 else if (fail_method == "Commit")
540 failure_method = FAIL_METHOD_COMMIT;
541 else
542 NOTREACHED() << "Unknown method: \"" << fail_method << "\"";
543 } else if (fail_class == "LevelDBIterator") {
544 failure_class = FAIL_CLASS_LEVELDB_ITERATOR;
545 if (fail_method == "Seek")
546 failure_method = FAIL_METHOD_SEEK;
547 else
548 NOTREACHED() << "Unknown method: \"" << fail_method << "\"";
549 } else {
550 NOTREACHED() << "Unknown class: \"" << fail_class << "\"";
553 DCHECK_GE(instance_num, 1);
554 DCHECK_GE(call_num, 1);
556 test->FailOperation(failure_class, failure_method, instance_num, call_num);
558 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
559 new net::test_server::BasicHttpResponse);
560 http_response->set_code(net::HTTP_OK);
561 return http_response.Pass();
564 // A request for a test resource
565 base::FilePath resourcePath =
566 content::GetTestFilePath("indexeddb", request_path.c_str());
567 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
568 new net::test_server::BasicHttpResponse);
569 http_response->set_code(net::HTTP_OK);
570 std::string file_contents;
571 if (!base::ReadFileToString(resourcePath, &file_contents))
572 return scoped_ptr<net::test_server::HttpResponse>();
573 http_response->set_content(file_contents);
574 return http_response.Pass();
577 } // namespace
579 class IndexedDBBrowserCorruptionTest
580 : public IndexedDBBrowserTest,
581 public ::testing::WithParamInterface<const char*> {};
583 IN_PROC_BROWSER_TEST_P(IndexedDBBrowserCorruptionTest,
584 OperationOnCorruptedOpenDatabase) {
585 ASSERT_TRUE(embedded_test_server()->Started() ||
586 embedded_test_server()->InitializeAndWaitUntilReady());
587 const GURL& origin_url = embedded_test_server()->base_url();
588 embedded_test_server()->RegisterRequestHandler(
589 base::Bind(&CorruptDBRequestHandler,
590 base::Unretained(GetContext()),
591 origin_url,
592 s_corrupt_db_test_prefix,
593 this));
595 std::string test_file = s_corrupt_db_test_prefix +
596 "corrupted_open_db_detection.html#" + GetParam();
597 SimpleTest(embedded_test_server()->GetURL(test_file));
599 test_file = s_corrupt_db_test_prefix + "corrupted_open_db_recovery.html";
600 SimpleTest(embedded_test_server()->GetURL(test_file));
603 INSTANTIATE_TEST_CASE_P(IndexedDBBrowserCorruptionTestInstantiation,
604 IndexedDBBrowserCorruptionTest,
605 ::testing::Values("failGetBlobJournal",
606 "get",
607 "failWebkitGetDatabaseNames",
608 "iterate",
609 "failTransactionCommit",
610 "clearObjectStore"));
612 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest,
613 DeleteCompactsBackingStore) {
614 const GURL test_url = GetTestUrl("indexeddb", "delete_compact.html");
615 SimpleTest(GURL(test_url.spec() + "#fill"));
616 int64 after_filling = RequestDiskUsage();
617 EXPECT_GT(after_filling, 0);
619 SimpleTest(GURL(test_url.spec() + "#purge"));
620 int64 after_deleting = RequestDiskUsage();
621 EXPECT_LT(after_deleting, after_filling);
623 // The above tests verify basic assertions - that filling writes data and
624 // deleting reduces the amount stored.
626 // The below tests make assumptions about implementation specifics, such as
627 // data compression, compaction efficiency, and the maximum amount of
628 // metadata and log data remains after a deletion. It is possible that
629 // changes to the implementation may require these constants to be tweaked.
631 const int kTestFillBytes = 1024 * 1024 * 5; // 5MB
632 EXPECT_GT(after_filling, kTestFillBytes);
634 const int kTestCompactBytes = 1024 * 10; // 10kB
635 EXPECT_LT(after_deleting, kTestCompactBytes);
638 // Complex multi-step (converted from pyauto) tests begin here.
640 // Verify null key path persists after restarting browser.
641 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, PRE_NullKeyPathPersistence) {
642 NavigateAndWaitForTitle(shell(), "bug_90635.html", "#part1",
643 "pass - first run");
646 // Verify null key path persists after restarting browser.
647 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, NullKeyPathPersistence) {
648 NavigateAndWaitForTitle(shell(), "bug_90635.html", "#part2",
649 "pass - second run");
652 // Verify that a VERSION_CHANGE transaction is rolled back after a
653 // renderer/browser crash
654 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest,
655 PRE_PRE_VersionChangeCrashResilience) {
656 NavigateAndWaitForTitle(shell(), "version_change_crash.html", "#part1",
657 "pass - part1 - complete");
660 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, PRE_VersionChangeCrashResilience) {
661 NavigateAndWaitForTitle(shell(), "version_change_crash.html", "#part2",
662 "pass - part2 - crash me");
663 // If we actually crash here then googletest will not run the next step
664 // (VersionChangeCrashResilience) as an optimization. googletest's
665 // ASSERT_DEATH/EXIT fails to work properly (on Windows) due to how we
666 // implement the PRE_* test mechanism.
667 exit(0);
670 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, VersionChangeCrashResilience) {
671 NavigateAndWaitForTitle(shell(), "version_change_crash.html", "#part3",
672 "pass - part3 - rolled back");
675 // Verify that open DB connections are closed when a tab is destroyed.
676 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, ConnectionsClosedOnTabClose) {
677 NavigateAndWaitForTitle(shell(), "version_change_blocked.html", "#tab1",
678 "setVersion(2) complete");
680 // Start on a different URL to force a new renderer process.
681 Shell* new_shell = CreateBrowser();
682 NavigateToURL(new_shell, GURL(url::kAboutBlankURL));
683 NavigateAndWaitForTitle(new_shell, "version_change_blocked.html", "#tab2",
684 "setVersion(3) blocked");
686 base::string16 expected_title16(ASCIIToUTF16("setVersion(3) complete"));
687 TitleWatcher title_watcher(new_shell->web_contents(), expected_title16);
689 base::KillProcess(
690 shell()->web_contents()->GetRenderProcessHost()->GetHandle(), 0, true);
691 shell()->Close();
693 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
696 // Verify that a "close" event is fired at database connections when
697 // the backing store is deleted.
698 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, ForceCloseEventTest) {
699 NavigateAndWaitForTitle(shell(), "force_close_event.html", NULL,
700 "connection ready");
702 GetContext()->TaskRunner()->PostTask(
703 FROM_HERE,
704 base::Bind(&IndexedDBContextImpl::DeleteForOrigin,
705 GetContext(),
706 GURL("file:///")));
708 base::string16 expected_title16(ASCIIToUTF16("connection closed"));
709 TitleWatcher title_watcher(shell()->web_contents(), expected_title16);
710 title_watcher.AlsoWaitForTitle(ASCIIToUTF16("connection closed with error"));
711 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
714 class IndexedDBBrowserTestSingleProcess : public IndexedDBBrowserTest {
715 public:
716 void SetUpCommandLine(CommandLine* command_line) override {
717 command_line->AppendSwitch(switches::kSingleProcess);
721 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestSingleProcess,
722 RenderThreadShutdownTest) {
723 SimpleTest(GetTestUrl("indexeddb", "shutdown_with_requests.html"));
726 } // namespace content