Add ICU message format support
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_browsertest.cc
blobb0097ed249eee391d392502e48364d93aad38bc6
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/location.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/test/thread_test_helper.h"
17 #include "content/browser/browser_main_loop.h"
18 #include "content/browser/indexed_db/indexed_db_class_factory.h"
19 #include "content/browser/indexed_db/indexed_db_context_impl.h"
20 #include "content/browser/indexed_db/mock_browsertest_indexed_db_class_factory.h"
21 #include "content/browser/web_contents/web_contents_impl.h"
22 #include "content/public/browser/browser_context.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/render_process_host.h"
25 #include "content/public/browser/storage_partition.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/common/content_switches.h"
28 #include "content/public/common/url_constants.h"
29 #include "content/public/test/browser_test_utils.h"
30 #include "content/public/test/content_browser_test.h"
31 #include "content/public/test/content_browser_test_utils.h"
32 #include "content/shell/browser/shell.h"
33 #include "net/base/escape.h"
34 #include "net/base/net_errors.h"
35 #include "net/test/embedded_test_server/embedded_test_server.h"
36 #include "net/test/embedded_test_server/http_request.h"
37 #include "net/test/embedded_test_server/http_response.h"
38 #include "storage/browser/blob/blob_storage_context.h"
39 #include "storage/browser/database/database_util.h"
40 #include "storage/browser/quota/quota_manager.h"
42 using base::ASCIIToUTF16;
43 using storage::QuotaManager;
44 using storage::DatabaseUtil;
46 namespace content {
48 // This browser test is aimed towards exercising the IndexedDB bindings and
49 // the actual implementation that lives in the browser side.
50 class IndexedDBBrowserTest : public ContentBrowserTest {
51 public:
52 IndexedDBBrowserTest() : disk_usage_(-1) {}
54 void SetUp() override {
55 GetTestClassFactory()->Reset();
56 IndexedDBClassFactory::SetIndexedDBClassFactoryGetter(GetIDBClassFactory);
57 ContentBrowserTest::SetUp();
60 void TearDown() override {
61 IndexedDBClassFactory::SetIndexedDBClassFactoryGetter(NULL);
62 ContentBrowserTest::TearDown();
65 void FailOperation(FailClass failure_class,
66 FailMethod failure_method,
67 int fail_on_instance_num,
68 int fail_on_call_num) {
69 GetTestClassFactory()->FailOperation(
70 failure_class, failure_method, fail_on_instance_num, fail_on_call_num);
73 void SimpleTest(const GURL& test_url, bool incognito = false) {
74 // The test page will perform tests on IndexedDB, then navigate to either
75 // a #pass or #fail ref.
76 Shell* the_browser = incognito ? CreateOffTheRecordBrowser() : shell();
78 VLOG(0) << "Navigating to URL and blocking.";
79 NavigateToURLBlockUntilNavigationsComplete(the_browser, test_url, 2);
80 VLOG(0) << "Navigation done.";
81 std::string result =
82 the_browser->web_contents()->GetLastCommittedURL().ref();
83 if (result != "pass") {
84 std::string js_result;
85 ASSERT_TRUE(ExecuteScriptAndExtractString(
86 the_browser->web_contents(),
87 "window.domAutomationController.send(getLog())",
88 &js_result));
89 FAIL() << "Failed: " << js_result;
93 void NavigateAndWaitForTitle(Shell* shell,
94 const char* filename,
95 const char* hash,
96 const char* expected_string) {
97 GURL url = GetTestUrl("indexeddb", filename);
98 if (hash)
99 url = GURL(url.spec() + hash);
101 base::string16 expected_title16(ASCIIToUTF16(expected_string));
102 TitleWatcher title_watcher(shell->web_contents(), expected_title16);
103 NavigateToURL(shell, url);
104 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
107 IndexedDBContextImpl* GetContext() {
108 StoragePartition* partition =
109 BrowserContext::GetDefaultStoragePartition(
110 shell()->web_contents()->GetBrowserContext());
111 return static_cast<IndexedDBContextImpl*>(partition->GetIndexedDBContext());
114 void SetQuota(int quota_kilobytes) {
115 const int kTemporaryStorageQuotaSize =
116 quota_kilobytes * 1024 * QuotaManager::kPerHostTemporaryPortion;
117 SetTempQuota(kTemporaryStorageQuotaSize,
118 BrowserContext::GetDefaultStoragePartition(
119 shell()->web_contents()->GetBrowserContext())->GetQuotaManager());
122 static void SetTempQuota(int64 bytes, scoped_refptr<QuotaManager> qm) {
123 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
124 BrowserThread::PostTask(
125 BrowserThread::IO, FROM_HERE,
126 base::Bind(&IndexedDBBrowserTest::SetTempQuota, bytes, qm));
127 return;
129 DCHECK_CURRENTLY_ON(BrowserThread::IO);
130 qm->SetTemporaryGlobalOverrideQuota(bytes, storage::QuotaCallback());
131 // Don't return until the quota has been set.
132 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper(
133 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
134 ASSERT_TRUE(helper->Run());
137 virtual int64 RequestDiskUsage() {
138 PostTaskAndReplyWithResult(
139 GetContext()->TaskRunner(),
140 FROM_HERE,
141 base::Bind(&IndexedDBContext::GetOriginDiskUsage,
142 GetContext(),
143 GURL("file:///")),
144 base::Bind(&IndexedDBBrowserTest::DidGetDiskUsage, this));
145 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper(
146 BrowserMainLoop::GetInstance()->indexed_db_thread()->task_runner()));
147 EXPECT_TRUE(helper->Run());
148 // Wait for DidGetDiskUsage to be called.
149 base::MessageLoop::current()->RunUntilIdle();
150 return disk_usage_;
153 virtual int RequestBlobFileCount() {
154 PostTaskAndReplyWithResult(
155 GetContext()->TaskRunner(), FROM_HERE,
156 base::Bind(&IndexedDBContextImpl::GetOriginBlobFileCount, GetContext(),
157 GURL("file:///")),
158 base::Bind(&IndexedDBBrowserTest::DidGetBlobFileCount, this));
159 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper(
160 BrowserMainLoop::GetInstance()->indexed_db_thread()->task_runner()));
161 EXPECT_TRUE(helper->Run());
162 // Wait for DidGetBlobFileCount to be called.
163 base::MessageLoop::current()->RunUntilIdle();
164 return blob_file_count_;
167 private:
168 static MockBrowserTestIndexedDBClassFactory* GetTestClassFactory() {
169 static ::base::LazyInstance<MockBrowserTestIndexedDBClassFactory>::Leaky
170 s_factory = LAZY_INSTANCE_INITIALIZER;
171 return s_factory.Pointer();
174 static IndexedDBClassFactory* GetIDBClassFactory() {
175 return GetTestClassFactory();
178 virtual void DidGetDiskUsage(int64 bytes) {
179 disk_usage_ = bytes;
182 virtual void DidGetBlobFileCount(int count) { blob_file_count_ = count; }
184 int64 disk_usage_;
185 int blob_file_count_ = 0;
187 DISALLOW_COPY_AND_ASSIGN(IndexedDBBrowserTest);
190 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CursorTest) {
191 SimpleTest(GetTestUrl("indexeddb", "cursor_test.html"));
194 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CursorTestIncognito) {
195 SimpleTest(GetTestUrl("indexeddb", "cursor_test.html"),
196 true /* incognito */);
199 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CursorPrefetch) {
200 SimpleTest(GetTestUrl("indexeddb", "cursor_prefetch.html"));
203 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, IndexTest) {
204 SimpleTest(GetTestUrl("indexeddb", "index_test.html"));
207 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, KeyPathTest) {
208 SimpleTest(GetTestUrl("indexeddb", "key_path_test.html"));
211 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, TransactionGetTest) {
212 SimpleTest(GetTestUrl("indexeddb", "transaction_get_test.html"));
215 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, KeyTypesTest) {
216 SimpleTest(GetTestUrl("indexeddb", "key_types_test.html"));
219 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, ObjectStoreTest) {
220 SimpleTest(GetTestUrl("indexeddb", "object_store_test.html"));
223 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, DatabaseTest) {
224 SimpleTest(GetTestUrl("indexeddb", "database_test.html"));
227 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, TransactionTest) {
228 SimpleTest(GetTestUrl("indexeddb", "transaction_test.html"));
231 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CallbackAccounting) {
232 SimpleTest(GetTestUrl("indexeddb", "callback_accounting.html"));
235 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, DoesntHangTest) {
236 SimpleTest(GetTestUrl("indexeddb", "transaction_run_forever.html"));
237 CrashTab(shell()->web_contents());
238 SimpleTest(GetTestUrl("indexeddb", "transaction_not_blocked.html"));
241 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, Bug84933Test) {
242 const GURL url = GetTestUrl("indexeddb", "bug_84933.html");
244 // Just navigate to the URL. Test will crash if it fails.
245 NavigateToURLBlockUntilNavigationsComplete(shell(), url, 1);
248 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, Bug106883Test) {
249 const GURL url = GetTestUrl("indexeddb", "bug_106883.html");
251 // Just navigate to the URL. Test will crash if it fails.
252 NavigateToURLBlockUntilNavigationsComplete(shell(), url, 1);
255 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, Bug109187Test) {
256 const GURL url = GetTestUrl("indexeddb", "bug_109187.html");
258 // Just navigate to the URL. Test will crash if it fails.
259 NavigateToURLBlockUntilNavigationsComplete(shell(), url, 1);
262 class IndexedDBBrowserTestWithLowQuota : public IndexedDBBrowserTest {
263 public:
264 IndexedDBBrowserTestWithLowQuota() {}
266 void SetUpOnMainThread() override {
267 const int kInitialQuotaKilobytes = 5000;
268 SetQuota(kInitialQuotaKilobytes);
271 private:
272 DISALLOW_COPY_AND_ASSIGN(IndexedDBBrowserTestWithLowQuota);
275 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithLowQuota, QuotaTest) {
276 SimpleTest(GetTestUrl("indexeddb", "quota_test.html"));
279 class IndexedDBBrowserTestWithGCExposed : public IndexedDBBrowserTest {
280 public:
281 IndexedDBBrowserTestWithGCExposed() {}
283 void SetUpCommandLine(base::CommandLine* command_line) override {
284 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc");
287 private:
288 DISALLOW_COPY_AND_ASSIGN(IndexedDBBrowserTestWithGCExposed);
291 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithGCExposed,
292 DatabaseCallbacksTest) {
293 SimpleTest(GetTestUrl("indexeddb", "database_callbacks_first.html"));
296 static void CopyLevelDBToProfile(Shell* shell,
297 scoped_refptr<IndexedDBContextImpl> context,
298 const std::string& test_directory) {
299 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread());
300 base::FilePath leveldb_dir(FILE_PATH_LITERAL("file__0.indexeddb.leveldb"));
301 base::FilePath test_data_dir =
302 GetTestFilePath("indexeddb", test_directory.c_str()).Append(leveldb_dir);
303 base::FilePath dest = context->data_path().Append(leveldb_dir);
304 // If we don't create the destination directory first, the contents of the
305 // leveldb directory are copied directly into profile/IndexedDB instead of
306 // profile/IndexedDB/file__0.xxx/
307 ASSERT_TRUE(base::CreateDirectory(dest));
308 const bool kRecursive = true;
309 ASSERT_TRUE(base::CopyDirectory(test_data_dir,
310 context->data_path(),
311 kRecursive));
314 class IndexedDBBrowserTestWithPreexistingLevelDB : public IndexedDBBrowserTest {
315 public:
316 IndexedDBBrowserTestWithPreexistingLevelDB() {}
317 void SetUpOnMainThread() override {
318 scoped_refptr<IndexedDBContextImpl> context = GetContext();
319 context->TaskRunner()->PostTask(
320 FROM_HERE,
321 base::Bind(
322 &CopyLevelDBToProfile, shell(), context, EnclosingLevelDBDir()));
323 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper(
324 BrowserMainLoop::GetInstance()->indexed_db_thread()->task_runner()));
325 ASSERT_TRUE(helper->Run());
328 virtual std::string EnclosingLevelDBDir() = 0;
330 private:
331 DISALLOW_COPY_AND_ASSIGN(IndexedDBBrowserTestWithPreexistingLevelDB);
334 class IndexedDBBrowserTestWithVersion0Schema : public
335 IndexedDBBrowserTestWithPreexistingLevelDB {
336 std::string EnclosingLevelDBDir() override { return "migration_from_0"; }
339 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithVersion0Schema, MigrationTest) {
340 SimpleTest(GetTestUrl("indexeddb", "migration_test.html"));
343 class IndexedDBBrowserTestWithVersion123456Schema : public
344 IndexedDBBrowserTestWithPreexistingLevelDB {
345 std::string EnclosingLevelDBDir() override { return "schema_version_123456"; }
348 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithVersion123456Schema,
349 DestroyTest) {
350 int64 original_size = RequestDiskUsage();
351 EXPECT_GT(original_size, 0);
352 SimpleTest(GetTestUrl("indexeddb", "open_bad_db.html"));
353 int64 new_size = RequestDiskUsage();
354 EXPECT_GT(new_size, 0);
355 EXPECT_NE(original_size, new_size);
358 class IndexedDBBrowserTestWithVersion987654SSVData : public
359 IndexedDBBrowserTestWithPreexistingLevelDB {
360 std::string EnclosingLevelDBDir() override { return "ssv_version_987654"; }
363 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithVersion987654SSVData,
364 DestroyTest) {
365 int64 original_size = RequestDiskUsage();
366 EXPECT_GT(original_size, 0);
367 SimpleTest(GetTestUrl("indexeddb", "open_bad_db.html"));
368 int64 new_size = RequestDiskUsage();
369 EXPECT_GT(new_size, 0);
370 EXPECT_NE(original_size, new_size);
373 class IndexedDBBrowserTestWithCorruptLevelDB : public
374 IndexedDBBrowserTestWithPreexistingLevelDB {
375 std::string EnclosingLevelDBDir() override { return "corrupt_leveldb"; }
378 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithCorruptLevelDB,
379 DestroyTest) {
380 int64 original_size = RequestDiskUsage();
381 EXPECT_GT(original_size, 0);
382 SimpleTest(GetTestUrl("indexeddb", "open_bad_db.html"));
383 int64 new_size = RequestDiskUsage();
384 EXPECT_GT(new_size, 0);
385 EXPECT_NE(original_size, new_size);
388 class IndexedDBBrowserTestWithMissingSSTFile : public
389 IndexedDBBrowserTestWithPreexistingLevelDB {
390 std::string EnclosingLevelDBDir() override { return "missing_sst"; }
393 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithMissingSSTFile,
394 DestroyTest) {
395 int64 original_size = RequestDiskUsage();
396 EXPECT_GT(original_size, 0);
397 SimpleTest(GetTestUrl("indexeddb", "open_missing_table.html"));
398 int64 new_size = RequestDiskUsage();
399 EXPECT_GT(new_size, 0);
400 EXPECT_NE(original_size, new_size);
403 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, LevelDBLogFileTest) {
404 // Any page that opens an IndexedDB will work here.
405 SimpleTest(GetTestUrl("indexeddb", "database_test.html"));
406 base::FilePath leveldb_dir(FILE_PATH_LITERAL("file__0.indexeddb.leveldb"));
407 base::FilePath log_file(FILE_PATH_LITERAL("LOG"));
408 base::FilePath log_file_path =
409 GetContext()->data_path().Append(leveldb_dir).Append(log_file);
410 int64 size;
411 EXPECT_TRUE(base::GetFileSize(log_file_path, &size));
412 EXPECT_GT(size, 0);
415 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, CanDeleteWhenOverQuotaTest) {
416 SimpleTest(GetTestUrl("indexeddb", "fill_up_5k.html"));
417 int64 size = RequestDiskUsage();
418 const int kQuotaKilobytes = 2;
419 EXPECT_GT(size, kQuotaKilobytes * 1024);
420 SetQuota(kQuotaKilobytes);
421 SimpleTest(GetTestUrl("indexeddb", "delete_over_quota.html"));
424 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, EmptyBlob) {
425 // First delete all IDB's for the test origin
426 GetContext()->TaskRunner()->PostTask(
427 FROM_HERE, base::Bind(&IndexedDBContextImpl::DeleteForOrigin,
428 GetContext(), GURL("file:///")));
429 EXPECT_EQ(0, RequestBlobFileCount()); // Start with no blob files.
430 const GURL test_url = GetTestUrl("indexeddb", "empty_blob.html");
431 // For some reason Android's futimes fails (EPERM) in this test. Do not assert
432 // file times on Android, but do so on other platforms. crbug.com/467247
433 // TODO(cmumford): Figure out why this is the case and fix if possible.
434 #if defined(OS_ANDROID)
435 SimpleTest(GURL(test_url.spec() + "#ignoreTimes"));
436 #else
437 SimpleTest(GURL(test_url.spec()));
438 #endif
439 // Test stores one blob and one file to disk, so expect two files.
440 EXPECT_EQ(2, RequestBlobFileCount());
443 // Very flaky on many bots. See crbug.com/459835
444 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithGCExposed, DISABLED_BlobDidAck) {
445 SimpleTest(GetTestUrl("indexeddb", "blob_did_ack.html"));
446 // Wait for idle so that the blob ack has time to be received/processed by
447 // the browser process.
448 base::MessageLoop::current()->RunUntilIdle();
449 content::ChromeBlobStorageContext* blob_context =
450 ChromeBlobStorageContext::GetFor(
451 shell()->web_contents()->GetBrowserContext());
452 EXPECT_EQ(0UL, blob_context->context()->blob_count());
455 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestWithGCExposed, BlobDidAckPrefetch) {
456 SimpleTest(GetTestUrl("indexeddb", "blob_did_ack_prefetch.html"));
457 // Wait for idle so that the blob ack has time to be received/processed by
458 // the browser process.
459 base::MessageLoop::current()->RunUntilIdle();
460 content::ChromeBlobStorageContext* blob_context =
461 ChromeBlobStorageContext::GetFor(
462 shell()->web_contents()->GetBrowserContext());
463 EXPECT_EQ(0UL, blob_context->context()->blob_count());
466 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, BlobsCountAgainstQuota) {
467 SimpleTest(GetTestUrl("indexeddb", "blobs_use_quota.html"));
470 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, DeleteForOriginDeletesBlobs) {
471 SimpleTest(GetTestUrl("indexeddb", "write_20mb_blob.html"));
472 int64 size = RequestDiskUsage();
473 // This assertion assumes that we do not compress blobs.
474 EXPECT_GT(size, 20 << 20 /* 20 MB */);
475 GetContext()->TaskRunner()->PostTask(
476 FROM_HERE, base::Bind(&IndexedDBContextImpl::DeleteForOrigin,
477 GetContext(), GURL("file:///")));
478 scoped_refptr<base::ThreadTestHelper> helper(new base::ThreadTestHelper(
479 BrowserMainLoop::GetInstance()->indexed_db_thread()->task_runner()));
480 ASSERT_TRUE(helper->Run());
481 EXPECT_EQ(0, RequestDiskUsage());
484 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, DiskFullOnCommit) {
485 // Ignore several preceding transactions:
486 // * The test calls deleteDatabase() which opens the backing store:
487 // #1: IndexedDBBackingStore::OpenBackingStore
488 // => IndexedDBBackingStore::SetUpMetadata
489 // #2: IndexedDBBackingStore::OpenBackingStore
490 // => IndexedDBBackingStore::CleanUpBlobJournal (no-op)
491 // * The test calls open(), to create a new database:
492 // #3: IndexedDBFactoryImpl::Open
493 // => IndexedDBDatabase::Create
494 // => IndexedDBBackingStore::CreateIDBDatabaseMetaData
495 // #4: IndexedDBTransaction::Commit - initial "versionchange" transaction
496 // * Once the connection is opened, the test runs:
497 // #5: IndexedDBTransaction::Commit - the test's "readwrite" transaction)
498 const int instance_num = 5;
499 const int call_num = 1;
500 FailOperation(FAIL_CLASS_LEVELDB_TRANSACTION, FAIL_METHOD_COMMIT_DISK_FULL,
501 instance_num, call_num);
502 SimpleTest(GetTestUrl("indexeddb", "disk_full_on_commit.html"));
505 namespace {
507 static void CompactIndexedDBBackingStore(
508 scoped_refptr<IndexedDBContextImpl> context,
509 const GURL& origin_url) {
510 IndexedDBFactory* factory = context->GetIDBFactory();
512 std::pair<IndexedDBFactory::OriginDBMapIterator,
513 IndexedDBFactory::OriginDBMapIterator> range =
514 factory->GetOpenDatabasesForOrigin(origin_url);
516 if (range.first == range.second) // If no open db's for this origin
517 return;
519 // Compact the first db's backing store since all the db's are in the same
520 // backing store.
521 IndexedDBDatabase* db = range.first->second;
522 IndexedDBBackingStore* backing_store = db->backing_store();
523 backing_store->Compact();
526 static void CorruptIndexedDBDatabase(
527 IndexedDBContextImpl* context,
528 const GURL& origin_url,
529 base::WaitableEvent* signal_when_finished) {
531 CompactIndexedDBBackingStore(context, origin_url);
533 int num_files = 0;
534 int num_errors = 0;
535 const bool recursive = false;
536 for (const base::FilePath& idb_data_path :
537 context->GetStoragePaths(origin_url)) {
538 base::FileEnumerator enumerator(
539 idb_data_path, recursive, base::FileEnumerator::FILES);
540 for (base::FilePath idb_file = enumerator.Next(); !idb_file.empty();
541 idb_file = enumerator.Next()) {
542 int64 size(0);
543 GetFileSize(idb_file, &size);
545 if (idb_file.Extension() == FILE_PATH_LITERAL(".ldb")) {
546 num_files++;
547 base::File file(
548 idb_file, base::File::FLAG_WRITE | base::File::FLAG_OPEN_TRUNCATED);
549 if (file.IsValid()) {
550 // Was opened truncated, expand back to the original
551 // file size and fill with zeros (corrupting the file).
552 file.SetLength(size);
553 } else {
554 num_errors++;
558 VLOG(0) << "There were " << num_files << " in " << idb_data_path.value()
559 << " with " << num_errors << " errors";
562 signal_when_finished->Signal();
565 const std::string s_corrupt_db_test_prefix = "/corrupt/test/";
567 static scoped_ptr<net::test_server::HttpResponse> CorruptDBRequestHandler(
568 IndexedDBContextImpl* context,
569 const GURL& origin_url,
570 const std::string& path,
571 IndexedDBBrowserTest* test,
572 const net::test_server::HttpRequest& request) {
573 std::string request_path;
574 if (path.find(s_corrupt_db_test_prefix) != std::string::npos)
575 request_path = request.relative_url.substr(s_corrupt_db_test_prefix.size());
576 else
577 return scoped_ptr<net::test_server::HttpResponse>();
579 // Remove the query string if present.
580 std::string request_query;
581 size_t query_pos = request_path.find('?');
582 if (query_pos != std::string::npos) {
583 request_query = request_path.substr(query_pos + 1);
584 request_path = request_path.substr(0, query_pos);
587 if (request_path == "corruptdb" && !request_query.empty()) {
588 VLOG(0) << "Requested to corrupt IndexedDB: " << request_query;
589 base::WaitableEvent signal_when_finished(false, false);
590 context->TaskRunner()->PostTask(FROM_HERE,
591 base::Bind(&CorruptIndexedDBDatabase,
592 base::ConstRef(context),
593 origin_url,
594 &signal_when_finished));
595 signal_when_finished.Wait();
597 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
598 new net::test_server::BasicHttpResponse);
599 http_response->set_code(net::HTTP_OK);
600 return http_response.Pass();
601 } else if (request_path == "fail" && !request_query.empty()) {
602 FailClass failure_class = FAIL_CLASS_NOTHING;
603 FailMethod failure_method = FAIL_METHOD_NOTHING;
604 int instance_num = 1;
605 int call_num = 1;
606 std::string fail_class;
607 std::string fail_method;
609 url::Component query(0, request_query.length()), key_pos, value_pos;
610 while (url::ExtractQueryKeyValue(
611 request_query.c_str(), &query, &key_pos, &value_pos)) {
612 std::string escaped_key(request_query.substr(key_pos.begin, key_pos.len));
613 std::string escaped_value(
614 request_query.substr(value_pos.begin, value_pos.len));
616 std::string key = net::UnescapeURLComponent(
617 escaped_key,
618 net::UnescapeRule::NORMAL | net::UnescapeRule::SPACES |
619 net::UnescapeRule::URL_SPECIAL_CHARS);
621 std::string value = net::UnescapeURLComponent(
622 escaped_value,
623 net::UnescapeRule::NORMAL | net::UnescapeRule::SPACES |
624 net::UnescapeRule::URL_SPECIAL_CHARS);
626 if (key == "method")
627 fail_method = value;
628 else if (key == "class")
629 fail_class = value;
630 else if (key == "instNum")
631 instance_num = atoi(value.c_str());
632 else if (key == "callNum")
633 call_num = atoi(value.c_str());
634 else
635 NOTREACHED() << "Unknown param: \"" << key << "\"";
638 if (fail_class == "LevelDBTransaction") {
639 failure_class = FAIL_CLASS_LEVELDB_TRANSACTION;
640 if (fail_method == "Get")
641 failure_method = FAIL_METHOD_GET;
642 else if (fail_method == "Commit")
643 failure_method = FAIL_METHOD_COMMIT;
644 else
645 NOTREACHED() << "Unknown method: \"" << fail_method << "\"";
646 } else if (fail_class == "LevelDBIterator") {
647 failure_class = FAIL_CLASS_LEVELDB_ITERATOR;
648 if (fail_method == "Seek")
649 failure_method = FAIL_METHOD_SEEK;
650 else
651 NOTREACHED() << "Unknown method: \"" << fail_method << "\"";
652 } else {
653 NOTREACHED() << "Unknown class: \"" << fail_class << "\"";
656 DCHECK_GE(instance_num, 1);
657 DCHECK_GE(call_num, 1);
659 test->FailOperation(failure_class, failure_method, instance_num, call_num);
661 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
662 new net::test_server::BasicHttpResponse);
663 http_response->set_code(net::HTTP_OK);
664 return http_response.Pass();
667 // A request for a test resource
668 base::FilePath resource_path =
669 content::GetTestFilePath("indexeddb", request_path.c_str());
670 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
671 new net::test_server::BasicHttpResponse);
672 http_response->set_code(net::HTTP_OK);
673 std::string file_contents;
674 if (!base::ReadFileToString(resource_path, &file_contents))
675 return scoped_ptr<net::test_server::HttpResponse>();
676 http_response->set_content(file_contents);
677 return http_response.Pass();
680 } // namespace
682 class IndexedDBBrowserCorruptionTest
683 : public IndexedDBBrowserTest,
684 public ::testing::WithParamInterface<const char*> {
685 public:
686 void SetUpCommandLine(base::CommandLine* command_line) override {
687 // Experimental for IDBObjectStore.getAll()
688 command_line->AppendSwitch(
689 switches::kEnableExperimentalWebPlatformFeatures);
693 IN_PROC_BROWSER_TEST_P(IndexedDBBrowserCorruptionTest,
694 OperationOnCorruptedOpenDatabase) {
695 ASSERT_TRUE(embedded_test_server()->Started() ||
696 embedded_test_server()->InitializeAndWaitUntilReady());
697 const GURL& origin_url = embedded_test_server()->base_url();
698 embedded_test_server()->RegisterRequestHandler(
699 base::Bind(&CorruptDBRequestHandler,
700 base::Unretained(GetContext()),
701 origin_url,
702 s_corrupt_db_test_prefix,
703 this));
705 std::string test_file = s_corrupt_db_test_prefix +
706 "corrupted_open_db_detection.html#" + GetParam();
707 SimpleTest(embedded_test_server()->GetURL(test_file));
709 test_file = s_corrupt_db_test_prefix + "corrupted_open_db_recovery.html";
710 SimpleTest(embedded_test_server()->GetURL(test_file));
713 INSTANTIATE_TEST_CASE_P(IndexedDBBrowserCorruptionTestInstantiation,
714 IndexedDBBrowserCorruptionTest,
715 ::testing::Values("failGetBlobJournal",
716 "get",
717 "getAll",
718 "failWebkitGetDatabaseNames",
719 "iterate",
720 "failTransactionCommit",
721 "clearObjectStore"));
723 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest,
724 DeleteCompactsBackingStore) {
725 const GURL test_url = GetTestUrl("indexeddb", "delete_compact.html");
726 SimpleTest(GURL(test_url.spec() + "#fill"));
727 int64 after_filling = RequestDiskUsage();
728 EXPECT_GT(after_filling, 0);
730 SimpleTest(GURL(test_url.spec() + "#purge"));
731 int64 after_deleting = RequestDiskUsage();
732 EXPECT_LT(after_deleting, after_filling);
734 // The above tests verify basic assertions - that filling writes data and
735 // deleting reduces the amount stored.
737 // The below tests make assumptions about implementation specifics, such as
738 // data compression, compaction efficiency, and the maximum amount of
739 // metadata and log data remains after a deletion. It is possible that
740 // changes to the implementation may require these constants to be tweaked.
742 const int kTestFillBytes = 1024 * 1024 * 5; // 5MB
743 EXPECT_GT(after_filling, kTestFillBytes);
745 const int kTestCompactBytes = 1024 * 10; // 10kB
746 EXPECT_LT(after_deleting, kTestCompactBytes);
749 // Complex multi-step (converted from pyauto) tests begin here.
751 // Verify null key path persists after restarting browser.
752 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, PRE_NullKeyPathPersistence) {
753 NavigateAndWaitForTitle(shell(), "bug_90635.html", "#part1",
754 "pass - first run");
757 // Verify null key path persists after restarting browser.
758 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, NullKeyPathPersistence) {
759 NavigateAndWaitForTitle(shell(), "bug_90635.html", "#part2",
760 "pass - second run");
763 // Verify that a VERSION_CHANGE transaction is rolled back after a
764 // renderer/browser crash
765 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest,
766 PRE_PRE_VersionChangeCrashResilience) {
767 NavigateAndWaitForTitle(shell(), "version_change_crash.html", "#part1",
768 "pass - part1 - complete");
771 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, PRE_VersionChangeCrashResilience) {
772 NavigateAndWaitForTitle(shell(), "version_change_crash.html", "#part2",
773 "pass - part2 - crash me");
774 // If we actually crash here then googletest will not run the next step
775 // (VersionChangeCrashResilience) as an optimization. googletest's
776 // ASSERT_DEATH/EXIT fails to work properly (on Windows) due to how we
777 // implement the PRE_* test mechanism.
778 exit(0);
781 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, VersionChangeCrashResilience) {
782 NavigateAndWaitForTitle(shell(), "version_change_crash.html", "#part3",
783 "pass - part3 - rolled back");
786 // crbug.com/427529
787 // Disable this test for ASAN on Android because it takes too long to run.
788 #if defined(ANDROID) && defined(ADDRESS_SANITIZER)
789 #define MAYBE_ConnectionsClosedOnTabClose DISABLED_ConnectionsClosedOnTabClose
790 #else
791 #define MAYBE_ConnectionsClosedOnTabClose ConnectionsClosedOnTabClose
792 #endif
793 // Verify that open DB connections are closed when a tab is destroyed.
794 IN_PROC_BROWSER_TEST_F(
795 IndexedDBBrowserTest, MAYBE_ConnectionsClosedOnTabClose) {
796 NavigateAndWaitForTitle(shell(), "version_change_blocked.html", "#tab1",
797 "setVersion(2) complete");
799 // Start on a different URL to force a new renderer process.
800 Shell* new_shell = CreateBrowser();
801 NavigateToURL(new_shell, GURL(url::kAboutBlankURL));
802 NavigateAndWaitForTitle(new_shell, "version_change_blocked.html", "#tab2",
803 "setVersion(3) blocked");
805 base::string16 expected_title16(ASCIIToUTF16("setVersion(3) complete"));
806 TitleWatcher title_watcher(new_shell->web_contents(), expected_title16);
808 shell()->web_contents()->GetRenderProcessHost()->Shutdown(0, true);
809 shell()->Close();
811 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
814 // Verify that a "close" event is fired at database connections when
815 // the backing store is deleted.
816 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTest, ForceCloseEventTest) {
817 NavigateAndWaitForTitle(shell(), "force_close_event.html", NULL,
818 "connection ready");
820 GetContext()->TaskRunner()->PostTask(
821 FROM_HERE,
822 base::Bind(&IndexedDBContextImpl::DeleteForOrigin,
823 GetContext(),
824 GURL("file:///")));
826 base::string16 expected_title16(ASCIIToUTF16("connection closed"));
827 TitleWatcher title_watcher(shell()->web_contents(), expected_title16);
828 title_watcher.AlsoWaitForTitle(ASCIIToUTF16("connection closed with error"));
829 EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
832 class IndexedDBBrowserTestSingleProcess : public IndexedDBBrowserTest {
833 public:
834 void SetUpCommandLine(base::CommandLine* command_line) override {
835 command_line->AppendSwitch(switches::kSingleProcess);
839 IN_PROC_BROWSER_TEST_F(IndexedDBBrowserTestSingleProcess,
840 RenderThreadShutdownTest) {
841 SimpleTest(GetTestUrl("indexeddb", "shutdown_with_requests.html"));
844 } // namespace content