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.
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/blob/blob_storage_context.h"
38 #include "storage/browser/database/database_util.h"
39 #include "storage/browser/quota/quota_manager.h"
41 using base::ASCIIToUTF16
;
42 using storage::QuotaManager
;
43 using storage::DatabaseUtil
;
47 // This browser test is aimed towards exercising the IndexedDB bindings and
48 // the actual implementation that lives in the browser side.
49 class IndexedDBBrowserTest
: public ContentBrowserTest
{
51 IndexedDBBrowserTest() : disk_usage_(-1) {}
53 void SetUp() override
{
54 GetTestClassFactory()->Reset();
55 IndexedDBClassFactory::SetIndexedDBClassFactoryGetter(GetIDBClassFactory
);
56 ContentBrowserTest::SetUp();
59 void TearDown() override
{
60 IndexedDBClassFactory::SetIndexedDBClassFactoryGetter(NULL
);
61 ContentBrowserTest::TearDown();
64 void FailOperation(FailClass failure_class
,
65 FailMethod failure_method
,
66 int fail_on_instance_num
,
67 int fail_on_call_num
) {
68 GetTestClassFactory()->FailOperation(
69 failure_class
, failure_method
, fail_on_instance_num
, fail_on_call_num
);
72 void SimpleTest(const GURL
& test_url
, bool incognito
= false) {
73 // The test page will perform tests on IndexedDB, then navigate to either
74 // a #pass or #fail ref.
75 Shell
* the_browser
= incognito
? CreateOffTheRecordBrowser() : shell();
77 VLOG(0) << "Navigating to URL and blocking.";
78 NavigateToURLBlockUntilNavigationsComplete(the_browser
, test_url
, 2);
79 VLOG(0) << "Navigation done.";
81 the_browser
->web_contents()->GetLastCommittedURL().ref();
82 if (result
!= "pass") {
83 std::string js_result
;
84 ASSERT_TRUE(ExecuteScriptAndExtractString(
85 the_browser
->web_contents(),
86 "window.domAutomationController.send(getLog())",
88 FAIL() << "Failed: " << js_result
;
92 void NavigateAndWaitForTitle(Shell
* shell
,
95 const char* expected_string
) {
96 GURL url
= GetTestUrl("indexeddb", filename
);
98 url
= GURL(url
.spec() + hash
);
100 base::string16
expected_title16(ASCIIToUTF16(expected_string
));
101 TitleWatcher
title_watcher(shell
->web_contents(), expected_title16
);
102 NavigateToURL(shell
, url
);
103 EXPECT_EQ(expected_title16
, title_watcher
.WaitAndGetTitle());
106 IndexedDBContextImpl
* GetContext() {
107 StoragePartition
* partition
=
108 BrowserContext::GetDefaultStoragePartition(
109 shell()->web_contents()->GetBrowserContext());
110 return static_cast<IndexedDBContextImpl
*>(partition
->GetIndexedDBContext());
113 void SetQuota(int quota_kilobytes
) {
114 const int kTemporaryStorageQuotaSize
=
115 quota_kilobytes
* 1024 * QuotaManager::kPerHostTemporaryPortion
;
116 SetTempQuota(kTemporaryStorageQuotaSize
,
117 BrowserContext::GetDefaultStoragePartition(
118 shell()->web_contents()->GetBrowserContext())->GetQuotaManager());
121 static void SetTempQuota(int64 bytes
, scoped_refptr
<QuotaManager
> qm
) {
122 if (!BrowserThread::CurrentlyOn(BrowserThread::IO
)) {
123 BrowserThread::PostTask(
124 BrowserThread::IO
, FROM_HERE
,
125 base::Bind(&IndexedDBBrowserTest::SetTempQuota
, bytes
, qm
));
128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
129 qm
->SetTemporaryGlobalOverrideQuota(bytes
, storage::QuotaCallback());
130 // Don't return until the quota has been set.
131 scoped_refptr
<base::ThreadTestHelper
> helper(new base::ThreadTestHelper(
132 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
)));
133 ASSERT_TRUE(helper
->Run());
136 virtual int64
RequestDiskUsage() {
137 PostTaskAndReplyWithResult(
138 GetContext()->TaskRunner(),
140 base::Bind(&IndexedDBContext::GetOriginDiskUsage
,
143 base::Bind(&IndexedDBBrowserTest::DidGetDiskUsage
, this));
144 scoped_refptr
<base::ThreadTestHelper
> helper(new base::ThreadTestHelper(
145 BrowserMainLoop::GetInstance()->indexed_db_thread()->
146 message_loop_proxy()));
147 EXPECT_TRUE(helper
->Run());
148 // Wait for DidGetDiskUsage to be called.
149 base::MessageLoop::current()->RunUntilIdle();
154 static MockBrowserTestIndexedDBClassFactory
* GetTestClassFactory() {
155 static ::base::LazyInstance
<MockBrowserTestIndexedDBClassFactory
>::Leaky
156 s_factory
= LAZY_INSTANCE_INITIALIZER
;
157 return s_factory
.Pointer();
160 static IndexedDBClassFactory
* GetIDBClassFactory() {
161 return GetTestClassFactory();
164 virtual void DidGetDiskUsage(int64 bytes
) {
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
{
247 IndexedDBBrowserTestWithLowQuota() {}
249 void SetUpOnMainThread() override
{
250 const int kInitialQuotaKilobytes
= 5000;
251 SetQuota(kInitialQuotaKilobytes
);
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
{
264 IndexedDBBrowserTestWithGCExposed() {}
266 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
267 command_line
->AppendSwitchASCII(switches::kJavaScriptFlags
, "--expose-gc");
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(),
297 class IndexedDBBrowserTestWithPreexistingLevelDB
: public IndexedDBBrowserTest
{
299 IndexedDBBrowserTestWithPreexistingLevelDB() {}
300 void SetUpOnMainThread() override
{
301 scoped_refptr
<IndexedDBContextImpl
> context
= GetContext();
302 context
->TaskRunner()->PostTask(
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;
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
,
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_GT(new_size
, 0);
339 EXPECT_NE(original_size
, new_size
);
342 class IndexedDBBrowserTestWithVersion987654SSVData
: public
343 IndexedDBBrowserTestWithPreexistingLevelDB
{
344 std::string
EnclosingLevelDBDir() override
{ return "ssv_version_987654"; }
347 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithVersion987654SSVData
,
349 int64 original_size
= RequestDiskUsage();
350 EXPECT_GT(original_size
, 0);
351 SimpleTest(GetTestUrl("indexeddb", "open_bad_db.html"));
352 int64 new_size
= RequestDiskUsage();
353 EXPECT_GT(new_size
, 0);
354 EXPECT_NE(original_size
, new_size
);
357 class IndexedDBBrowserTestWithCorruptLevelDB
: public
358 IndexedDBBrowserTestWithPreexistingLevelDB
{
359 std::string
EnclosingLevelDBDir() override
{ return "corrupt_leveldb"; }
362 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithCorruptLevelDB
,
364 int64 original_size
= RequestDiskUsage();
365 EXPECT_GT(original_size
, 0);
366 SimpleTest(GetTestUrl("indexeddb", "open_bad_db.html"));
367 int64 new_size
= RequestDiskUsage();
368 EXPECT_GT(new_size
, 0);
369 EXPECT_NE(original_size
, new_size
);
372 class IndexedDBBrowserTestWithMissingSSTFile
: public
373 IndexedDBBrowserTestWithPreexistingLevelDB
{
374 std::string
EnclosingLevelDBDir() override
{ return "missing_sst"; }
377 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithMissingSSTFile
,
379 int64 original_size
= RequestDiskUsage();
380 EXPECT_GT(original_size
, 0);
381 SimpleTest(GetTestUrl("indexeddb", "open_missing_table.html"));
382 int64 new_size
= RequestDiskUsage();
383 EXPECT_GT(new_size
, 0);
384 EXPECT_NE(original_size
, new_size
);
387 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
, LevelDBLogFileTest
) {
388 // Any page that opens an IndexedDB will work here.
389 SimpleTest(GetTestUrl("indexeddb", "database_test.html"));
390 base::FilePath
leveldb_dir(FILE_PATH_LITERAL("file__0.indexeddb.leveldb"));
391 base::FilePath
log_file(FILE_PATH_LITERAL("LOG"));
392 base::FilePath log_file_path
=
393 GetContext()->data_path().Append(leveldb_dir
).Append(log_file
);
395 EXPECT_TRUE(base::GetFileSize(log_file_path
, &size
));
399 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
, CanDeleteWhenOverQuotaTest
) {
400 SimpleTest(GetTestUrl("indexeddb", "fill_up_5k.html"));
401 int64 size
= RequestDiskUsage();
402 const int kQuotaKilobytes
= 2;
403 EXPECT_GT(size
, kQuotaKilobytes
* 1024);
404 SetQuota(kQuotaKilobytes
);
405 SimpleTest(GetTestUrl("indexeddb", "delete_over_quota.html"));
408 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithGCExposed
, BlobDidAck
) {
409 SimpleTest(GetTestUrl("indexeddb", "blob_did_ack.html"));
410 // Wait for idle so that the blob ack has time to be received/processed by
411 // the browser process.
412 base::MessageLoop::current()->RunUntilIdle();
413 content::ChromeBlobStorageContext
* blob_context
=
414 ChromeBlobStorageContext::GetFor(
415 shell()->web_contents()->GetBrowserContext());
416 EXPECT_EQ(0UL, blob_context
->context()->blob_count());
419 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithGCExposed
, BlobDidAckPrefetch
) {
420 SimpleTest(GetTestUrl("indexeddb", "blob_did_ack_prefetch.html"));
421 // Wait for idle so that the blob ack has time to be received/processed by
422 // the browser process.
423 base::MessageLoop::current()->RunUntilIdle();
424 content::ChromeBlobStorageContext
* blob_context
=
425 ChromeBlobStorageContext::GetFor(
426 shell()->web_contents()->GetBrowserContext());
427 EXPECT_EQ(0UL, blob_context
->context()->blob_count());
430 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
, BlobsCountAgainstQuota
) {
431 SimpleTest(GetTestUrl("indexeddb", "blobs_use_quota.html"));
434 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
, DeleteForOriginDeletesBlobs
) {
435 SimpleTest(GetTestUrl("indexeddb", "write_20mb_blob.html"));
436 int64 size
= RequestDiskUsage();
437 // This assertion assumes that we do not compress blobs.
438 EXPECT_GT(size
, 20 << 20 /* 20 MB */);
439 GetContext()->TaskRunner()->PostTask(
440 FROM_HERE
, base::Bind(&IndexedDBContextImpl::DeleteForOrigin
,
441 GetContext(), GURL("file:///")));
442 scoped_refptr
<base::ThreadTestHelper
> helper(
443 new base::ThreadTestHelper(BrowserMainLoop::GetInstance()
444 ->indexed_db_thread()
445 ->message_loop_proxy()));
446 ASSERT_TRUE(helper
->Run());
447 EXPECT_EQ(0, RequestDiskUsage());
450 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
, DiskFullOnCommit
) {
451 // Ignore several preceding transactions:
452 // * The test calls deleteDatabase() which opens the backing store:
453 // #1: IndexedDBBackingStore::OpenBackingStore
454 // => IndexedDBBackingStore::SetUpMetadata
455 // #2: IndexedDBBackingStore::OpenBackingStore
456 // => IndexedDBBackingStore::CleanUpBlobJournal (no-op)
457 // * Then deletes the database:
458 // #3: IndexedDBFactoryImpl::DeleteDatabase
459 // => IndexedDBDatabase::Create
460 // => IndexedDBBackingStore::CreateIDBDatabaseMetaData
461 // #4: IndexedDBFactoryImpl::DeleteDatabase
462 // => IndexedDBDatabase::DeleteDatabase
463 // => IndexedDBBackingStore::DeleteDatabase
464 // => IndexedDBBackingStore::CleanUpBlobJournal (no-op)
465 // * The test calls open(), to create a new database:
466 // #5: IndexedDBFactoryImpl::Open
467 // => IndexedDBDatabase::Create
468 // => IndexedDBBackingStore::CreateIDBDatabaseMetaData
469 // #6: IndexedDBTransaction::Commit - initial "versionchange" transaction
470 // * Once the connection is opened, the test runs:
471 // #7: IndexedDBTransaction::Commit - the test's "readwrite" transaction)
472 const int instance_num
= 7;
473 const int call_num
= 1;
474 FailOperation(FAIL_CLASS_LEVELDB_TRANSACTION
, FAIL_METHOD_COMMIT_DISK_FULL
,
475 instance_num
, call_num
);
476 SimpleTest(GetTestUrl("indexeddb", "disk_full_on_commit.html"));
481 static void CompactIndexedDBBackingStore(
482 scoped_refptr
<IndexedDBContextImpl
> context
,
483 const GURL
& origin_url
) {
484 IndexedDBFactory
* factory
= context
->GetIDBFactory();
486 std::pair
<IndexedDBFactory::OriginDBMapIterator
,
487 IndexedDBFactory::OriginDBMapIterator
> range
=
488 factory
->GetOpenDatabasesForOrigin(origin_url
);
490 if (range
.first
== range
.second
) // If no open db's for this origin
493 // Compact the first db's backing store since all the db's are in the same
495 IndexedDBDatabase
* db
= range
.first
->second
;
496 IndexedDBBackingStore
* backing_store
= db
->backing_store();
497 backing_store
->Compact();
500 static void CorruptIndexedDBDatabase(
501 IndexedDBContextImpl
* context
,
502 const GURL
& origin_url
,
503 base::WaitableEvent
* signal_when_finished
) {
505 CompactIndexedDBBackingStore(context
, origin_url
);
509 const bool recursive
= false;
510 for (const base::FilePath
& idb_data_path
:
511 context
->GetStoragePaths(origin_url
)) {
512 base::FileEnumerator
enumerator(
513 idb_data_path
, recursive
, base::FileEnumerator::FILES
);
514 for (base::FilePath idb_file
= enumerator
.Next(); !idb_file
.empty();
515 idb_file
= enumerator
.Next()) {
517 GetFileSize(idb_file
, &size
);
519 if (idb_file
.Extension() == FILE_PATH_LITERAL(".ldb")) {
522 idb_file
, base::File::FLAG_WRITE
| base::File::FLAG_OPEN_TRUNCATED
);
523 if (file
.IsValid()) {
524 // Was opened truncated, expand back to the original
525 // file size and fill with zeros (corrupting the file).
526 file
.SetLength(size
);
532 VLOG(0) << "There were " << num_files
<< " in " << idb_data_path
.value()
533 << " with " << num_errors
<< " errors";
536 signal_when_finished
->Signal();
539 const std::string s_corrupt_db_test_prefix
= "/corrupt/test/";
541 static scoped_ptr
<net::test_server::HttpResponse
> CorruptDBRequestHandler(
542 IndexedDBContextImpl
* context
,
543 const GURL
& origin_url
,
544 const std::string
& path
,
545 IndexedDBBrowserTest
* test
,
546 const net::test_server::HttpRequest
& request
) {
547 std::string request_path
;
548 if (path
.find(s_corrupt_db_test_prefix
) != std::string::npos
)
549 request_path
= request
.relative_url
.substr(s_corrupt_db_test_prefix
.size());
551 return scoped_ptr
<net::test_server::HttpResponse
>();
553 // Remove the query string if present.
554 std::string request_query
;
555 size_t query_pos
= request_path
.find('?');
556 if (query_pos
!= std::string::npos
) {
557 request_query
= request_path
.substr(query_pos
+ 1);
558 request_path
= request_path
.substr(0, query_pos
);
561 if (request_path
== "corruptdb" && !request_query
.empty()) {
562 VLOG(0) << "Requested to corrupt IndexedDB: " << request_query
;
563 base::WaitableEvent
signal_when_finished(false, false);
564 context
->TaskRunner()->PostTask(FROM_HERE
,
565 base::Bind(&CorruptIndexedDBDatabase
,
566 base::ConstRef(context
),
568 &signal_when_finished
));
569 signal_when_finished
.Wait();
571 scoped_ptr
<net::test_server::BasicHttpResponse
> http_response(
572 new net::test_server::BasicHttpResponse
);
573 http_response
->set_code(net::HTTP_OK
);
574 return http_response
.Pass();
575 } else if (request_path
== "fail" && !request_query
.empty()) {
576 FailClass failure_class
= FAIL_CLASS_NOTHING
;
577 FailMethod failure_method
= FAIL_METHOD_NOTHING
;
578 int instance_num
= 1;
580 std::string fail_class
;
581 std::string fail_method
;
583 url::Component
query(0, request_query
.length()), key_pos
, value_pos
;
584 while (url::ExtractQueryKeyValue(
585 request_query
.c_str(), &query
, &key_pos
, &value_pos
)) {
586 std::string
escaped_key(request_query
.substr(key_pos
.begin
, key_pos
.len
));
587 std::string
escaped_value(
588 request_query
.substr(value_pos
.begin
, value_pos
.len
));
590 std::string key
= net::UnescapeURLComponent(
592 net::UnescapeRule::NORMAL
| net::UnescapeRule::SPACES
|
593 net::UnescapeRule::URL_SPECIAL_CHARS
);
595 std::string value
= net::UnescapeURLComponent(
597 net::UnescapeRule::NORMAL
| net::UnescapeRule::SPACES
|
598 net::UnescapeRule::URL_SPECIAL_CHARS
);
602 else if (key
== "class")
604 else if (key
== "instNum")
605 instance_num
= atoi(value
.c_str());
606 else if (key
== "callNum")
607 call_num
= atoi(value
.c_str());
609 NOTREACHED() << "Unknown param: \"" << key
<< "\"";
612 if (fail_class
== "LevelDBTransaction") {
613 failure_class
= FAIL_CLASS_LEVELDB_TRANSACTION
;
614 if (fail_method
== "Get")
615 failure_method
= FAIL_METHOD_GET
;
616 else if (fail_method
== "Commit")
617 failure_method
= FAIL_METHOD_COMMIT
;
619 NOTREACHED() << "Unknown method: \"" << fail_method
<< "\"";
620 } else if (fail_class
== "LevelDBIterator") {
621 failure_class
= FAIL_CLASS_LEVELDB_ITERATOR
;
622 if (fail_method
== "Seek")
623 failure_method
= FAIL_METHOD_SEEK
;
625 NOTREACHED() << "Unknown method: \"" << fail_method
<< "\"";
627 NOTREACHED() << "Unknown class: \"" << fail_class
<< "\"";
630 DCHECK_GE(instance_num
, 1);
631 DCHECK_GE(call_num
, 1);
633 test
->FailOperation(failure_class
, failure_method
, instance_num
, call_num
);
635 scoped_ptr
<net::test_server::BasicHttpResponse
> http_response(
636 new net::test_server::BasicHttpResponse
);
637 http_response
->set_code(net::HTTP_OK
);
638 return http_response
.Pass();
641 // A request for a test resource
642 base::FilePath resource_path
=
643 content::GetTestFilePath("indexeddb", request_path
.c_str());
644 scoped_ptr
<net::test_server::BasicHttpResponse
> http_response(
645 new net::test_server::BasicHttpResponse
);
646 http_response
->set_code(net::HTTP_OK
);
647 std::string file_contents
;
648 if (!base::ReadFileToString(resource_path
, &file_contents
))
649 return scoped_ptr
<net::test_server::HttpResponse
>();
650 http_response
->set_content(file_contents
);
651 return http_response
.Pass();
656 class IndexedDBBrowserCorruptionTest
657 : public IndexedDBBrowserTest
,
658 public ::testing::WithParamInterface
<const char*> {};
660 IN_PROC_BROWSER_TEST_P(IndexedDBBrowserCorruptionTest
,
661 OperationOnCorruptedOpenDatabase
) {
662 ASSERT_TRUE(embedded_test_server()->Started() ||
663 embedded_test_server()->InitializeAndWaitUntilReady());
664 const GURL
& origin_url
= embedded_test_server()->base_url();
665 embedded_test_server()->RegisterRequestHandler(
666 base::Bind(&CorruptDBRequestHandler
,
667 base::Unretained(GetContext()),
669 s_corrupt_db_test_prefix
,
672 std::string test_file
= s_corrupt_db_test_prefix
+
673 "corrupted_open_db_detection.html#" + GetParam();
674 SimpleTest(embedded_test_server()->GetURL(test_file
));
676 test_file
= s_corrupt_db_test_prefix
+ "corrupted_open_db_recovery.html";
677 SimpleTest(embedded_test_server()->GetURL(test_file
));
680 INSTANTIATE_TEST_CASE_P(IndexedDBBrowserCorruptionTestInstantiation
,
681 IndexedDBBrowserCorruptionTest
,
682 ::testing::Values("failGetBlobJournal",
684 "failWebkitGetDatabaseNames",
686 "failTransactionCommit",
687 "clearObjectStore"));
689 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
,
690 DeleteCompactsBackingStore
) {
691 const GURL test_url
= GetTestUrl("indexeddb", "delete_compact.html");
692 SimpleTest(GURL(test_url
.spec() + "#fill"));
693 int64 after_filling
= RequestDiskUsage();
694 EXPECT_GT(after_filling
, 0);
696 SimpleTest(GURL(test_url
.spec() + "#purge"));
697 int64 after_deleting
= RequestDiskUsage();
698 EXPECT_LT(after_deleting
, after_filling
);
700 // The above tests verify basic assertions - that filling writes data and
701 // deleting reduces the amount stored.
703 // The below tests make assumptions about implementation specifics, such as
704 // data compression, compaction efficiency, and the maximum amount of
705 // metadata and log data remains after a deletion. It is possible that
706 // changes to the implementation may require these constants to be tweaked.
708 const int kTestFillBytes
= 1024 * 1024 * 5; // 5MB
709 EXPECT_GT(after_filling
, kTestFillBytes
);
711 const int kTestCompactBytes
= 1024 * 10; // 10kB
712 EXPECT_LT(after_deleting
, kTestCompactBytes
);
715 // Complex multi-step (converted from pyauto) tests begin here.
717 // Verify null key path persists after restarting browser.
718 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
, PRE_NullKeyPathPersistence
) {
719 NavigateAndWaitForTitle(shell(), "bug_90635.html", "#part1",
723 // Verify null key path persists after restarting browser.
724 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
, NullKeyPathPersistence
) {
725 NavigateAndWaitForTitle(shell(), "bug_90635.html", "#part2",
726 "pass - second run");
729 // Verify that a VERSION_CHANGE transaction is rolled back after a
730 // renderer/browser crash
731 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
,
732 PRE_PRE_VersionChangeCrashResilience
) {
733 NavigateAndWaitForTitle(shell(), "version_change_crash.html", "#part1",
734 "pass - part1 - complete");
737 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
, PRE_VersionChangeCrashResilience
) {
738 NavigateAndWaitForTitle(shell(), "version_change_crash.html", "#part2",
739 "pass - part2 - crash me");
740 // If we actually crash here then googletest will not run the next step
741 // (VersionChangeCrashResilience) as an optimization. googletest's
742 // ASSERT_DEATH/EXIT fails to work properly (on Windows) due to how we
743 // implement the PRE_* test mechanism.
747 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
, VersionChangeCrashResilience
) {
748 NavigateAndWaitForTitle(shell(), "version_change_crash.html", "#part3",
749 "pass - part3 - rolled back");
753 // Disable this test for ASAN on Android because it takes too long to run.
754 #if defined(ANDROID) && defined(ADDRESS_SANITIZER)
755 #define MAYBE_ConnectionsClosedOnTabClose DISABLED_ConnectionsClosedOnTabClose
757 #define MAYBE_ConnectionsClosedOnTabClose ConnectionsClosedOnTabClose
759 // Verify that open DB connections are closed when a tab is destroyed.
760 IN_PROC_BROWSER_TEST_F(
761 IndexedDBBrowserTest
, MAYBE_ConnectionsClosedOnTabClose
) {
762 NavigateAndWaitForTitle(shell(), "version_change_blocked.html", "#tab1",
763 "setVersion(2) complete");
765 // Start on a different URL to force a new renderer process.
766 Shell
* new_shell
= CreateBrowser();
767 NavigateToURL(new_shell
, GURL(url::kAboutBlankURL
));
768 NavigateAndWaitForTitle(new_shell
, "version_change_blocked.html", "#tab2",
769 "setVersion(3) blocked");
771 base::string16
expected_title16(ASCIIToUTF16("setVersion(3) complete"));
772 TitleWatcher
title_watcher(new_shell
->web_contents(), expected_title16
);
774 shell()->web_contents()->GetRenderProcessHost()->Shutdown(0, true);
777 EXPECT_EQ(expected_title16
, title_watcher
.WaitAndGetTitle());
780 // Verify that a "close" event is fired at database connections when
781 // the backing store is deleted.
782 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest
, ForceCloseEventTest
) {
783 NavigateAndWaitForTitle(shell(), "force_close_event.html", NULL
,
786 GetContext()->TaskRunner()->PostTask(
788 base::Bind(&IndexedDBContextImpl::DeleteForOrigin
,
792 base::string16
expected_title16(ASCIIToUTF16("connection closed"));
793 TitleWatcher
title_watcher(shell()->web_contents(), expected_title16
);
794 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("connection closed with error"));
795 EXPECT_EQ(expected_title16
, title_watcher
.WaitAndGetTitle());
798 class IndexedDBBrowserTestSingleProcess
: public IndexedDBBrowserTest
{
800 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
801 command_line
->AppendSwitch(switches::kSingleProcess
);
805 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestSingleProcess
,
806 RenderThreadShutdownTest
) {
807 SimpleTest(GetTestUrl("indexeddb", "shutdown_with_requests.html"));
810 } // namespace content