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

Side by Side Diff: content/browser/dom_storage/local_storage_context_mojo_unittest.cc

Issue 2604273002: Migrate data from old localstorage format to new format. (Closed)
Patch Set: nit Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "content/browser/dom_storage/local_storage_context_mojo.h" 5 #include "content/browser/dom_storage/local_storage_context_mojo.h"
6 6
7 #include "base/files/file_enumerator.h" 7 #include "base/files/file_enumerator.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/utf_string_conversions.h"
10 #include "components/filesystem/public/interfaces/file_system.mojom.h" 11 #include "components/filesystem/public/interfaces/file_system.mojom.h"
11 #include "components/leveldb/public/cpp/util.h" 12 #include "components/leveldb/public/cpp/util.h"
13 #include "content/browser/dom_storage/dom_storage_area.h"
14 #include "content/browser/dom_storage/dom_storage_context_impl.h"
15 #include "content/browser/dom_storage/dom_storage_namespace.h"
16 #include "content/browser/dom_storage/dom_storage_task_runner.h"
17 #include "content/common/dom_storage/dom_storage_types.h"
12 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/local_storage_usage_info.h" 19 #include "content/public/browser/local_storage_usage_info.h"
14 #include "content/public/test/test_browser_thread_bundle.h" 20 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "content/test/mock_leveldb_database.h" 21 #include "content/test/mock_leveldb_database.h"
16 #include "mojo/public/cpp/bindings/associated_binding.h" 22 #include "mojo/public/cpp/bindings/associated_binding.h"
17 #include "mojo/public/cpp/bindings/binding.h" 23 #include "mojo/public/cpp/bindings/binding.h"
18 #include "mojo/public/cpp/bindings/binding_set.h" 24 #include "mojo/public/cpp/bindings/binding_set.h"
19 #include "services/file/file_service.h" 25 #include "services/file/file_service.h"
20 #include "services/file/public/interfaces/constants.mojom.h" 26 #include "services/file/public/interfaces/constants.mojom.h"
21 #include "services/file/user_id_map.h" 27 #include "services/file/user_id_map.h"
(...skipping 25 matching lines...) Expand all
47 void GetCallback(const base::Closure& callback, 53 void GetCallback(const base::Closure& callback,
48 bool* success_out, 54 bool* success_out,
49 std::vector<uint8_t>* value_out, 55 std::vector<uint8_t>* value_out,
50 bool success, 56 bool success,
51 const std::vector<uint8_t>& value) { 57 const std::vector<uint8_t>& value) {
52 *success_out = success; 58 *success_out = success;
53 *value_out = value; 59 *value_out = value;
54 callback.Run(); 60 callback.Run();
55 } 61 }
56 62
63 void NoOpGet(bool success, const std::vector<uint8_t>& value) {}
64
65 std::vector<uint8_t> String16ToUint8Vector(const base::string16& input) {
66 const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data());
67 return std::vector<uint8_t>(data, data + input.size() * sizeof(base::char16));
68 }
69
57 class TestLevelDBObserver : public mojom::LevelDBObserver { 70 class TestLevelDBObserver : public mojom::LevelDBObserver {
58 public: 71 public:
59 struct Observation { 72 struct Observation {
60 enum { kAdd, kChange, kDelete, kDeleteAll } type; 73 enum { kAdd, kChange, kDelete, kDeleteAll } type;
61 std::string key; 74 std::string key;
62 std::string old_value; 75 std::string old_value;
63 std::string new_value; 76 std::string new_value;
64 std::string source; 77 std::string source;
65 }; 78 };
66 79
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 } 114 }
102 115
103 std::vector<Observation> observations_; 116 std::vector<Observation> observations_;
104 mojo::AssociatedBinding<mojom::LevelDBObserver> binding_; 117 mojo::AssociatedBinding<mojom::LevelDBObserver> binding_;
105 }; 118 };
106 119
107 } // namespace 120 } // namespace
108 121
109 class LocalStorageContextMojoTest : public testing::Test { 122 class LocalStorageContextMojoTest : public testing::Test {
110 public: 123 public:
111 LocalStorageContextMojoTest() : db_(&mock_data_), db_binding_(&db_) {} 124 LocalStorageContextMojoTest()
125 : db_(&mock_data_),
126 db_binding_(&db_),
127 task_runner_(new MockDOMStorageTaskRunner(
128 base::ThreadTaskRunnerHandle::Get().get())) {
129 EXPECT_TRUE(temp_path_.CreateUniqueTempDir());
130 dom_storage_context_ = new DOMStorageContextImpl(
131 temp_path_.GetPath(), base::FilePath(), nullptr, task_runner_);
132 }
133
134 ~LocalStorageContextMojoTest() override {
135 if (dom_storage_context_)
136 dom_storage_context_->Shutdown();
137 }
112 138
113 LocalStorageContextMojo* context() { 139 LocalStorageContextMojo* context() {
114 if (!context_) { 140 if (!context_) {
115 context_ = 141 context_ = base::MakeUnique<LocalStorageContextMojo>(
116 base::MakeUnique<LocalStorageContextMojo>(nullptr, base::FilePath()); 142 nullptr, task_runner_, temp_path_.GetPath(),
143 base::FilePath(FILE_PATH_LITERAL("leveldb")));
117 db_binding_.Bind(context_->DatabaseRequestForTesting()); 144 db_binding_.Bind(context_->DatabaseRequestForTesting());
118 } 145 }
119 return context_.get(); 146 return context_.get();
120 } 147 }
148
149 DOMStorageNamespace* local_storage_namespace() {
150 return dom_storage_context_->GetStorageNamespace(kLocalStorageNamespaceId);
151 }
152
153 void FlushAndPurgeDOMStorageMemory() {
154 dom_storage_context_->Flush();
155 base::RunLoop().RunUntilIdle();
156 dom_storage_context_->PurgeMemory(DOMStorageContextImpl::PURGE_AGGRESSIVE);
157 }
158
121 const std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data() { 159 const std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data() {
122 return mock_data_; 160 return mock_data_;
123 } 161 }
124 162
125 void set_mock_data(const std::string& key, const std::string& value) { 163 void set_mock_data(const std::string& key, const std::string& value) {
126 mock_data_[StdStringToUint8Vector(key)] = StdStringToUint8Vector(value); 164 mock_data_[StdStringToUint8Vector(key)] = StdStringToUint8Vector(value);
127 } 165 }
128 166
129 std::vector<LocalStorageUsageInfo> GetStorageUsageSync() { 167 std::vector<LocalStorageUsageInfo> GetStorageUsageSync() {
130 base::RunLoop run_loop; 168 base::RunLoop run_loop;
131 std::vector<LocalStorageUsageInfo> result; 169 std::vector<LocalStorageUsageInfo> result;
132 context()->GetStorageUsage(base::BindOnce(&GetStorageUsageCallback, 170 context()->GetStorageUsage(base::BindOnce(&GetStorageUsageCallback,
133 run_loop.QuitClosure(), &result)); 171 run_loop.QuitClosure(), &result));
134 run_loop.Run(); 172 run_loop.Run();
135 return result; 173 return result;
136 } 174 }
137 175
138 private: 176 private:
139 TestBrowserThreadBundle thread_bundle_; 177 TestBrowserThreadBundle thread_bundle_;
178 base::ScopedTempDir temp_path_;
140 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_; 179 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_;
141 MockLevelDBDatabase db_; 180 MockLevelDBDatabase db_;
142 mojo::AssociatedGroup associated_group_; 181 mojo::AssociatedGroup associated_group_;
143 mojo::AssociatedBinding<leveldb::mojom::LevelDBDatabase> db_binding_; 182 mojo::AssociatedBinding<leveldb::mojom::LevelDBDatabase> db_binding_;
144 183
184 scoped_refptr<MockDOMStorageTaskRunner> task_runner_;
185 scoped_refptr<DOMStorageContextImpl> dom_storage_context_;
186
145 std::unique_ptr<LocalStorageContextMojo> context_; 187 std::unique_ptr<LocalStorageContextMojo> context_;
146 188
147 DISALLOW_COPY_AND_ASSIGN(LocalStorageContextMojoTest); 189 DISALLOW_COPY_AND_ASSIGN(LocalStorageContextMojoTest);
148 }; 190 };
149 191
150 TEST_F(LocalStorageContextMojoTest, Basic) { 192 TEST_F(LocalStorageContextMojoTest, Basic) {
151 auto key = StdStringToUint8Vector("key"); 193 auto key = StdStringToUint8Vector("key");
152 auto value = StdStringToUint8Vector("value"); 194 auto value = StdStringToUint8Vector("value");
153 195
154 mojom::LevelDBWrapperPtr wrapper; 196 mojom::LevelDBWrapperPtr wrapper;
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 continue; 566 continue;
525 EXPECT_EQ(std::string::npos, 567 EXPECT_EQ(std::string::npos,
526 Uint8VectorToStdString(it.first).find(origin1a.Serialize())); 568 Uint8VectorToStdString(it.first).find(origin1a.Serialize()));
527 EXPECT_EQ(std::string::npos, 569 EXPECT_EQ(std::string::npos,
528 Uint8VectorToStdString(it.first).find(origin1b.Serialize())); 570 Uint8VectorToStdString(it.first).find(origin1b.Serialize()));
529 EXPECT_NE(std::string::npos, 571 EXPECT_NE(std::string::npos,
530 Uint8VectorToStdString(it.first).find(origin2.Serialize())); 572 Uint8VectorToStdString(it.first).find(origin2.Serialize()));
531 } 573 }
532 } 574 }
533 575
576 TEST_F(LocalStorageContextMojoTest, Migration) {
577 url::Origin origin1(GURL("http://foobar.com"));
578 url::Origin origin2(GURL("http://example.com"));
579 base::string16 key = base::ASCIIToUTF16("key");
580 base::string16 value = base::ASCIIToUTF16("value");
581
582 DOMStorageNamespace* local = local_storage_namespace();
583 DOMStorageArea* area = local->OpenStorageArea(origin1.GetURL());
584 base::NullableString16 dummy;
585 area->SetItem(key, value, &dummy);
586 local->CloseStorageArea(area);
587 FlushAndPurgeDOMStorageMemory();
588
589 // Opening origin2 and accessing its data should not migrate anything.
590 mojom::LevelDBWrapperPtr wrapper;
591 context()->OpenLocalStorage(origin2, MakeRequest(&wrapper));
592 wrapper->Get(std::vector<uint8_t>(), base::Bind(&NoOpGet));
593 wrapper.reset();
594 base::RunLoop().RunUntilIdle();
595 EXPECT_TRUE(mock_data().empty());
596
597 // Opening origin1 and accessing its data should migrate its storage.
598 context()->OpenLocalStorage(origin1, MakeRequest(&wrapper));
599 wrapper->Get(std::vector<uint8_t>(), base::Bind(&NoOpGet));
600 base::RunLoop().RunUntilIdle();
601 EXPECT_FALSE(mock_data().empty());
602
603 base::RunLoop run_loop;
604 bool success = false;
605 std::vector<uint8_t> result;
606 wrapper->Get(
607 String16ToUint8Vector(key),
608 base::Bind(&GetCallback, run_loop.QuitClosure(), &success, &result));
609 run_loop.Run();
610 EXPECT_TRUE(success);
611 EXPECT_EQ(String16ToUint8Vector(value), result);
612
613 // Origin1 should no longer exist in old storage.
614 area = local->OpenStorageArea(origin1.GetURL());
615 ASSERT_EQ(0u, area->Length());
616 local->CloseStorageArea(area);
617 }
618
534 namespace { 619 namespace {
535 620
536 class ServiceTestClient : public service_manager::test::ServiceTestClient, 621 class ServiceTestClient : public service_manager::test::ServiceTestClient,
537 public service_manager::mojom::ServiceFactory, 622 public service_manager::mojom::ServiceFactory,
538 public service_manager::InterfaceFactory< 623 public service_manager::InterfaceFactory<
539 service_manager::mojom::ServiceFactory> { 624 service_manager::mojom::ServiceFactory> {
540 public: 625 public:
541 explicit ServiceTestClient(service_manager::test::ServiceTest* test) 626 explicit ServiceTestClient(service_manager::test::ServiceTest* test)
542 : service_manager::test::ServiceTestClient(test) {} 627 : service_manager::test::ServiceTestClient(test) {}
543 ~ServiceTestClient() override {} 628 ~ServiceTestClient() override {}
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 667
583 protected: 668 protected:
584 void SetUp() override { 669 void SetUp() override {
585 ServiceTest::SetUp(); 670 ServiceTest::SetUp();
586 ASSERT_TRUE(temp_path_.CreateUniqueTempDir()); 671 ASSERT_TRUE(temp_path_.CreateUniqueTempDir());
587 file::AssociateServiceUserIdWithUserDir(test_userid(), 672 file::AssociateServiceUserIdWithUserDir(test_userid(),
588 temp_path_.GetPath()); 673 temp_path_.GetPath());
589 } 674 }
590 675
591 void TearDown() override { 676 void TearDown() override {
592 temp_path_.Take();
593 ServiceTest::TearDown(); 677 ServiceTest::TearDown();
594 } 678 }
595 679
596 std::unique_ptr<service_manager::Service> CreateService() override { 680 std::unique_ptr<service_manager::Service> CreateService() override {
597 return base::MakeUnique<ServiceTestClient>(this); 681 return base::MakeUnique<ServiceTestClient>(this);
598 } 682 }
599 683
600 std::unique_ptr<base::MessageLoop> CreateMessageLoop() override { 684 std::unique_ptr<base::MessageLoop> CreateMessageLoop() override {
601 return nullptr; 685 return nullptr;
602 } 686 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 }; 727 };
644 728
645 // Enable when http://crbug.com/677194 is fixed and ServiceTest works 729 // Enable when http://crbug.com/677194 is fixed and ServiceTest works
646 // correctly on Android. 730 // correctly on Android.
647 #if defined(OS_ANDROID) 731 #if defined(OS_ANDROID)
648 #define MAYBE_InMemory DISABLED_InMemory 732 #define MAYBE_InMemory DISABLED_InMemory
649 #else 733 #else
650 #define MAYBE_InMemory InMemory 734 #define MAYBE_InMemory InMemory
651 #endif 735 #endif
652 TEST_F(LocalStorageContextMojoTestWithService, MAYBE_InMemory) { 736 TEST_F(LocalStorageContextMojoTestWithService, MAYBE_InMemory) {
653 auto context = 737 auto context = base::MakeUnique<LocalStorageContextMojo>(
654 base::MakeUnique<LocalStorageContextMojo>(connector(), base::FilePath()); 738 connector(), nullptr, base::FilePath(), base::FilePath());
655 auto key = StdStringToUint8Vector("key"); 739 auto key = StdStringToUint8Vector("key");
656 auto value = StdStringToUint8Vector("value"); 740 auto value = StdStringToUint8Vector("value");
657 741
658 mojom::LevelDBWrapperPtr wrapper; 742 mojom::LevelDBWrapperPtr wrapper;
659 context->OpenLocalStorage(url::Origin(GURL("http://foobar.com")), 743 context->OpenLocalStorage(url::Origin(GURL("http://foobar.com")),
660 MakeRequest(&wrapper)); 744 MakeRequest(&wrapper));
661 745
662 DoTestPut(context.get(), key, value); 746 DoTestPut(context.get(), key, value);
663 std::vector<uint8_t> result; 747 std::vector<uint8_t> result;
664 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 748 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
665 EXPECT_EQ(value, result); 749 EXPECT_EQ(value, result);
666 750
667 context.reset(); 751 context.reset();
668 base::RunLoop().RunUntilIdle(); 752 base::RunLoop().RunUntilIdle();
669 753
670 // Should not have created any files. 754 // Should not have created any files.
671 EXPECT_TRUE(FirstEntryInDir().empty()); 755 EXPECT_TRUE(FirstEntryInDir().empty());
672 756
673 // Re-opening should get fresh data. 757 // Re-opening should get fresh data.
674 context.reset(new LocalStorageContextMojo(connector(), base::FilePath())); 758 context = base::MakeUnique<LocalStorageContextMojo>(
759 connector(), nullptr, base::FilePath(), base::FilePath());
675 EXPECT_FALSE(DoTestGet(context.get(), key, &result)); 760 EXPECT_FALSE(DoTestGet(context.get(), key, &result));
676 } 761 }
677 762
678 // Enable when http://crbug.com/677194 is fixed and ServiceTest works 763 // Enable when http://crbug.com/677194 is fixed and ServiceTest works
679 // correctly on Android. 764 // correctly on Android.
680 #if defined(OS_ANDROID) 765 #if defined(OS_ANDROID)
681 #define MAYBE_InMemoryInvalidPath DISABLED_InMemoryInvalidPath 766 #define MAYBE_InMemoryInvalidPath DISABLED_InMemoryInvalidPath
682 #else 767 #else
683 #define MAYBE_InMemoryInvalidPath InMemoryInvalidPath 768 #define MAYBE_InMemoryInvalidPath InMemoryInvalidPath
684 #endif 769 #endif
685 TEST_F(LocalStorageContextMojoTestWithService, MAYBE_InMemoryInvalidPath) { 770 TEST_F(LocalStorageContextMojoTestWithService, MAYBE_InMemoryInvalidPath) {
686 auto context = base::MakeUnique<LocalStorageContextMojo>( 771 auto context = base::MakeUnique<LocalStorageContextMojo>(
687 connector(), base::FilePath(FILE_PATH_LITERAL("../../"))); 772 connector(), nullptr, base::FilePath(),
773 base::FilePath(FILE_PATH_LITERAL("../../")));
688 auto key = StdStringToUint8Vector("key"); 774 auto key = StdStringToUint8Vector("key");
689 auto value = StdStringToUint8Vector("value"); 775 auto value = StdStringToUint8Vector("value");
690 776
691 mojom::LevelDBWrapperPtr wrapper; 777 mojom::LevelDBWrapperPtr wrapper;
692 context->OpenLocalStorage(url::Origin(GURL("http://foobar.com")), 778 context->OpenLocalStorage(url::Origin(GURL("http://foobar.com")),
693 MakeRequest(&wrapper)); 779 MakeRequest(&wrapper));
694 780
695 DoTestPut(context.get(), key, value); 781 DoTestPut(context.get(), key, value);
696 std::vector<uint8_t> result; 782 std::vector<uint8_t> result;
697 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 783 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
698 EXPECT_EQ(value, result); 784 EXPECT_EQ(value, result);
699 785
700 context.reset(); 786 context.reset();
701 base::RunLoop().RunUntilIdle(); 787 base::RunLoop().RunUntilIdle();
702 788
703 // Should not have created any files. 789 // Should not have created any files.
704 EXPECT_TRUE(FirstEntryInDir().empty()); 790 EXPECT_TRUE(FirstEntryInDir().empty());
705 } 791 }
706 792
707 // Enable when http://crbug.com/677194 is fixed and ServiceTest works 793 // Enable when http://crbug.com/677194 is fixed and ServiceTest works
708 // correctly on Android. 794 // correctly on Android.
709 #if defined(OS_ANDROID) 795 #if defined(OS_ANDROID)
710 #define MAYBE_OnDisk DISABLED_OnDisk 796 #define MAYBE_OnDisk DISABLED_OnDisk
711 #else 797 #else
712 #define MAYBE_OnDisk OnDisk 798 #define MAYBE_OnDisk OnDisk
713 #endif 799 #endif
714 TEST_F(LocalStorageContextMojoTestWithService, MAYBE_OnDisk) { 800 TEST_F(LocalStorageContextMojoTestWithService, MAYBE_OnDisk) {
715 base::FilePath test_path(FILE_PATH_LITERAL("test_path")); 801 base::FilePath test_path(FILE_PATH_LITERAL("test_path"));
716 auto context = 802 auto context = base::MakeUnique<LocalStorageContextMojo>(
717 base::MakeUnique<LocalStorageContextMojo>(connector(), test_path); 803 connector(), nullptr, base::FilePath(), test_path);
718 auto key = StdStringToUint8Vector("key"); 804 auto key = StdStringToUint8Vector("key");
719 auto value = StdStringToUint8Vector("value"); 805 auto value = StdStringToUint8Vector("value");
720 806
721 DoTestPut(context.get(), key, value); 807 DoTestPut(context.get(), key, value);
722 std::vector<uint8_t> result; 808 std::vector<uint8_t> result;
723 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 809 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
724 EXPECT_EQ(value, result); 810 EXPECT_EQ(value, result);
725 811
726 context.reset(); 812 context.reset();
727 base::RunLoop().RunUntilIdle(); 813 base::RunLoop().RunUntilIdle();
728 814
729 // Should have created files. 815 // Should have created files.
730 EXPECT_EQ(test_path, FirstEntryInDir().BaseName()); 816 EXPECT_EQ(test_path, FirstEntryInDir().BaseName());
731 817
732 // Should be able to re-open. 818 // Should be able to re-open.
733 context.reset(new LocalStorageContextMojo(connector(), test_path)); 819 context = base::MakeUnique<LocalStorageContextMojo>(
820 connector(), nullptr, base::FilePath(), test_path);
734 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 821 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
735 EXPECT_EQ(value, result); 822 EXPECT_EQ(value, result);
736 } 823 }
737 824
738 // Enable when http://crbug.com/677194 is fixed and ServiceTest works 825 // Enable when http://crbug.com/677194 is fixed and ServiceTest works
739 // correctly on Android. 826 // correctly on Android.
740 #if defined(OS_ANDROID) 827 #if defined(OS_ANDROID)
741 #define MAYBE_InvalidVersionOnDisk DISABLED_InvalidVersionOnDisk 828 #define MAYBE_InvalidVersionOnDisk DISABLED_InvalidVersionOnDisk
742 #else 829 #else
743 #define MAYBE_InvalidVersionOnDisk InvalidVersionOnDisk 830 #define MAYBE_InvalidVersionOnDisk InvalidVersionOnDisk
744 #endif 831 #endif
745 TEST_F(LocalStorageContextMojoTestWithService, MAYBE_InvalidVersionOnDisk) { 832 TEST_F(LocalStorageContextMojoTestWithService, MAYBE_InvalidVersionOnDisk) {
746 base::FilePath test_path(FILE_PATH_LITERAL("test_path")); 833 base::FilePath test_path(FILE_PATH_LITERAL("test_path"));
747 834
748 // Create context and add some data to it. 835 // Create context and add some data to it.
749 auto context = 836 auto context = base::MakeUnique<LocalStorageContextMojo>(
750 base::MakeUnique<LocalStorageContextMojo>(connector(), test_path); 837 connector(), nullptr, base::FilePath(), test_path);
751 auto key = StdStringToUint8Vector("key"); 838 auto key = StdStringToUint8Vector("key");
752 auto value = StdStringToUint8Vector("value"); 839 auto value = StdStringToUint8Vector("value");
753 840
754 DoTestPut(context.get(), key, value); 841 DoTestPut(context.get(), key, value);
755 std::vector<uint8_t> result; 842 std::vector<uint8_t> result;
756 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 843 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
757 EXPECT_EQ(value, result); 844 EXPECT_EQ(value, result);
758 845
759 context.reset(); 846 context.reset();
760 base::RunLoop().RunUntilIdle(); 847 base::RunLoop().RunUntilIdle();
761 848
762 { 849 {
763 // Mess up version number in database. 850 // Mess up version number in database.
764 leveldb_env::ChromiumEnv env; 851 leveldb_env::ChromiumEnv env;
765 leveldb::DB* db = nullptr; 852 leveldb::DB* db = nullptr;
766 leveldb::Options options; 853 leveldb::Options options;
767 options.env = &env; 854 options.env = &env;
768 base::FilePath db_path = 855 base::FilePath db_path =
769 temp_path().Append(test_path).Append(FILE_PATH_LITERAL("leveldb")); 856 temp_path().Append(test_path).Append(FILE_PATH_LITERAL("leveldb"));
770 ASSERT_TRUE(leveldb::DB::Open(options, db_path.AsUTF8Unsafe(), &db).ok()); 857 ASSERT_TRUE(leveldb::DB::Open(options, db_path.AsUTF8Unsafe(), &db).ok());
771 std::unique_ptr<leveldb::DB> db_owner(db); 858 std::unique_ptr<leveldb::DB> db_owner(db);
772 ASSERT_TRUE(db->Put(leveldb::WriteOptions(), "VERSION", "argh").ok()); 859 ASSERT_TRUE(db->Put(leveldb::WriteOptions(), "VERSION", "argh").ok());
773 } 860 }
774 861
775 // Make sure data is gone. 862 // Make sure data is gone.
776 context = base::MakeUnique<LocalStorageContextMojo>(connector(), test_path); 863 context = base::MakeUnique<LocalStorageContextMojo>(
864 connector(), nullptr, base::FilePath(), test_path);
777 EXPECT_FALSE(DoTestGet(context.get(), key, &result)); 865 EXPECT_FALSE(DoTestGet(context.get(), key, &result));
778 866
779 // Write data again. 867 // Write data again.
780 DoTestPut(context.get(), key, value); 868 DoTestPut(context.get(), key, value);
781 869
782 context.reset(); 870 context.reset();
783 base::RunLoop().RunUntilIdle(); 871 base::RunLoop().RunUntilIdle();
784 872
785 // Data should have been preserved now. 873 // Data should have been preserved now.
786 context = base::MakeUnique<LocalStorageContextMojo>(connector(), test_path); 874 context = base::MakeUnique<LocalStorageContextMojo>(
875 connector(), nullptr, base::FilePath(), test_path);
787 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 876 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
788 EXPECT_EQ(value, result); 877 EXPECT_EQ(value, result);
789 } 878 }
790 879
791 } // namespace content 880 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/dom_storage/local_storage_context_mojo.cc ('k') | content/browser/leveldb_wrapper_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698