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

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

Issue 2861473002: Clear up session only storage on localstorage shutdown (Closed)
Patch Set: test and nicer Created 3 years, 7 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 "base/strings/utf_string_conversions.h"
(...skipping 11 matching lines...) Expand all
22 #include "mojo/public/cpp/bindings/associated_binding.h" 22 #include "mojo/public/cpp/bindings/associated_binding.h"
23 #include "mojo/public/cpp/bindings/binding.h" 23 #include "mojo/public/cpp/bindings/binding.h"
24 #include "mojo/public/cpp/bindings/binding_set.h" 24 #include "mojo/public/cpp/bindings/binding_set.h"
25 #include "services/file/file_service.h" 25 #include "services/file/file_service.h"
26 #include "services/file/public/interfaces/constants.mojom.h" 26 #include "services/file/public/interfaces/constants.mojom.h"
27 #include "services/file/user_id_map.h" 27 #include "services/file/user_id_map.h"
28 #include "services/service_manager/public/cpp/interface_factory.h" 28 #include "services/service_manager/public/cpp/interface_factory.h"
29 #include "services/service_manager/public/cpp/service_context.h" 29 #include "services/service_manager/public/cpp/service_context.h"
30 #include "services/service_manager/public/cpp/service_test.h" 30 #include "services/service_manager/public/cpp/service_test.h"
31 #include "services/service_manager/public/interfaces/service_factory.mojom.h" 31 #include "services/service_manager/public/interfaces/service_factory.mojom.h"
32 #include "storage/browser/test/mock_special_storage_policy.h"
32 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
33 #include "third_party/leveldatabase/env_chromium.h" 34 #include "third_party/leveldatabase/env_chromium.h"
34 #include "third_party/leveldatabase/src/include/leveldb/db.h" 35 #include "third_party/leveldatabase/src/include/leveldb/db.h"
35 36
36 using leveldb::StdStringToUint8Vector; 37 using leveldb::StdStringToUint8Vector;
37 using leveldb::Uint8VectorToStdString; 38 using leveldb::Uint8VectorToStdString;
38 39
39 namespace content { 40 namespace content {
40 41
41 namespace { 42 namespace {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 }; 112 };
112 113
113 } // namespace 114 } // namespace
114 115
115 class LocalStorageContextMojoTest : public testing::Test { 116 class LocalStorageContextMojoTest : public testing::Test {
116 public: 117 public:
117 LocalStorageContextMojoTest() 118 LocalStorageContextMojoTest()
118 : db_(&mock_data_), 119 : db_(&mock_data_),
119 db_binding_(&db_), 120 db_binding_(&db_),
120 task_runner_(new MockDOMStorageTaskRunner( 121 task_runner_(new MockDOMStorageTaskRunner(
121 base::ThreadTaskRunnerHandle::Get().get())) { 122 base::ThreadTaskRunnerHandle::Get().get())),
123 mock_special_storage_policy_(new MockSpecialStoragePolicy()) {
122 EXPECT_TRUE(temp_path_.CreateUniqueTempDir()); 124 EXPECT_TRUE(temp_path_.CreateUniqueTempDir());
123 dom_storage_context_ = new DOMStorageContextImpl( 125 dom_storage_context_ = new DOMStorageContextImpl(
124 temp_path_.GetPath(), base::FilePath(), nullptr, task_runner_); 126 temp_path_.GetPath(), base::FilePath(), nullptr, task_runner_);
125 } 127 }
126 128
127 ~LocalStorageContextMojoTest() override { 129 ~LocalStorageContextMojoTest() override {
128 if (dom_storage_context_) 130 if (dom_storage_context_)
129 dom_storage_context_->Shutdown(); 131 dom_storage_context_->Shutdown();
132 if (context_)
133 context_->Shutdown();
130 } 134 }
131 135
132 LocalStorageContextMojo* context() { 136 LocalStorageContextMojo* context() {
133 if (!context_) { 137 if (!context_) {
134 context_ = base::MakeUnique<LocalStorageContextMojo>( 138 context_ = new LocalStorageContextMojo(
135 nullptr, task_runner_, temp_path_.GetPath(), 139 nullptr, task_runner_, temp_path_.GetPath(),
136 base::FilePath(FILE_PATH_LITERAL("leveldb"))); 140 base::FilePath(FILE_PATH_LITERAL("leveldb")),
141 special_storage_policy());
137 db_binding_.Bind(context_->DatabaseRequestForTesting()); 142 db_binding_.Bind(context_->DatabaseRequestForTesting());
138 } 143 }
139 return context_.get(); 144 return context_.get();
140 } 145 }
141 146
147 void ShutdownContext() {
148 context_->Shutdown();
149 context_ = nullptr;
150 base::RunLoop().RunUntilIdle();
151 }
152
142 DOMStorageNamespace* local_storage_namespace() { 153 DOMStorageNamespace* local_storage_namespace() {
143 return dom_storage_context_->GetStorageNamespace(kLocalStorageNamespaceId); 154 return dom_storage_context_->GetStorageNamespace(kLocalStorageNamespaceId);
144 } 155 }
145 156
157 MockSpecialStoragePolicy* special_storage_policy() {
158 return mock_special_storage_policy_.get();
159 }
160
146 void FlushAndPurgeDOMStorageMemory() { 161 void FlushAndPurgeDOMStorageMemory() {
147 dom_storage_context_->Flush(); 162 dom_storage_context_->Flush();
148 base::RunLoop().RunUntilIdle(); 163 base::RunLoop().RunUntilIdle();
149 dom_storage_context_->PurgeMemory(DOMStorageContextImpl::PURGE_AGGRESSIVE); 164 dom_storage_context_->PurgeMemory(DOMStorageContextImpl::PURGE_AGGRESSIVE);
150 } 165 }
151 166
152 const std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data() { 167 const std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data() {
153 return mock_data_; 168 return mock_data_;
154 } 169 }
155 170
(...skipping 13 matching lines...) Expand all
169 private: 184 private:
170 TestBrowserThreadBundle thread_bundle_; 185 TestBrowserThreadBundle thread_bundle_;
171 base::ScopedTempDir temp_path_; 186 base::ScopedTempDir temp_path_;
172 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_; 187 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_;
173 MockLevelDBDatabase db_; 188 MockLevelDBDatabase db_;
174 mojo::AssociatedBinding<leveldb::mojom::LevelDBDatabase> db_binding_; 189 mojo::AssociatedBinding<leveldb::mojom::LevelDBDatabase> db_binding_;
175 190
176 scoped_refptr<MockDOMStorageTaskRunner> task_runner_; 191 scoped_refptr<MockDOMStorageTaskRunner> task_runner_;
177 scoped_refptr<DOMStorageContextImpl> dom_storage_context_; 192 scoped_refptr<DOMStorageContextImpl> dom_storage_context_;
178 193
179 std::unique_ptr<LocalStorageContextMojo> context_; 194 scoped_refptr<LocalStorageContextMojo> context_;
195
196 scoped_refptr<MockSpecialStoragePolicy> mock_special_storage_policy_;
180 197
181 DISALLOW_COPY_AND_ASSIGN(LocalStorageContextMojoTest); 198 DISALLOW_COPY_AND_ASSIGN(LocalStorageContextMojoTest);
182 }; 199 };
183 200
184 TEST_F(LocalStorageContextMojoTest, Basic) { 201 TEST_F(LocalStorageContextMojoTest, Basic) {
185 auto key = StdStringToUint8Vector("key"); 202 auto key = StdStringToUint8Vector("key");
186 auto value = StdStringToUint8Vector("value"); 203 auto value = StdStringToUint8Vector("value");
187 204
188 mojom::LevelDBWrapperPtr wrapper; 205 mojom::LevelDBWrapperPtr wrapper;
189 context()->OpenLocalStorage(url::Origin(GURL("http://foobar.com")), 206 context()->OpenLocalStorage(url::Origin(GURL("http://foobar.com")),
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 run_loop.Run(); 618 run_loop.Run();
602 EXPECT_TRUE(success); 619 EXPECT_TRUE(success);
603 EXPECT_EQ(LocalStorageContextMojo::MigrateString(value), result); 620 EXPECT_EQ(LocalStorageContextMojo::MigrateString(value), result);
604 621
605 // Origin1 should no longer exist in old storage. 622 // Origin1 should no longer exist in old storage.
606 area = local->OpenStorageArea(origin1.GetURL()); 623 area = local->OpenStorageArea(origin1.GetURL());
607 ASSERT_EQ(0u, area->Length()); 624 ASSERT_EQ(0u, area->Length());
608 local->CloseStorageArea(area); 625 local->CloseStorageArea(area);
609 } 626 }
610 627
628 TEST_F(LocalStorageContextMojoTest, ShutdownClearsData) {
629 url::Origin origin1(GURL("http://foobar.com"));
630 url::Origin origin2(GURL("http://example.com"));
631 auto key1 = StdStringToUint8Vector("key1");
632 auto key2 = StdStringToUint8Vector("key");
633 auto value = StdStringToUint8Vector("value");
634
635 mojom::LevelDBWrapperPtr wrapper;
636 context()->OpenLocalStorage(origin1, MakeRequest(&wrapper));
637 wrapper->Put(key1, value, "source", base::Bind(&NoOpSuccess));
638 wrapper->Put(key2, value, "source", base::Bind(&NoOpSuccess));
639 wrapper.reset();
640
641 context()->OpenLocalStorage(origin2, MakeRequest(&wrapper));
642 wrapper->Put(key2, value, "source", base::Bind(&NoOpSuccess));
643 wrapper.reset();
644
645 // Make sure all data gets committed to the DB.
646 base::RunLoop().RunUntilIdle();
647
648 special_storage_policy()->AddSessionOnly(origin1.GetURL());
649 ShutdownContext();
650
651 // Data from origin2 should exist, including meta-data, but nothing should
652 // exist for origin1.
653 EXPECT_EQ(3u, mock_data().size());
654 for (const auto& it : mock_data()) {
655 if (Uint8VectorToStdString(it.first) == "VERSION")
656 continue;
657 EXPECT_EQ(std::string::npos,
658 Uint8VectorToStdString(it.first).find(origin1.Serialize()));
659 EXPECT_NE(std::string::npos,
660 Uint8VectorToStdString(it.first).find(origin2.Serialize()));
661 }
662 }
663
611 namespace { 664 namespace {
612 665
613 class ServiceTestClient : public service_manager::test::ServiceTestClient, 666 class ServiceTestClient : public service_manager::test::ServiceTestClient,
614 public service_manager::mojom::ServiceFactory, 667 public service_manager::mojom::ServiceFactory,
615 public service_manager::InterfaceFactory< 668 public service_manager::InterfaceFactory<
616 service_manager::mojom::ServiceFactory> { 669 service_manager::mojom::ServiceFactory> {
617 public: 670 public:
618 explicit ServiceTestClient(service_manager::test::ServiceTest* test) 671 explicit ServiceTestClient(service_manager::test::ServiceTest* test)
619 : service_manager::test::ServiceTestClient(test) { 672 : service_manager::test::ServiceTestClient(test) {
620 registry_.AddInterface<service_manager::mojom::ServiceFactory>(this); 673 registry_.AddInterface<service_manager::mojom::ServiceFactory>(this);
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 } 770 }
718 771
719 private: 772 private:
720 TestBrowserThreadBundle thread_bundle_; 773 TestBrowserThreadBundle thread_bundle_;
721 base::ScopedTempDir temp_path_; 774 base::ScopedTempDir temp_path_;
722 775
723 DISALLOW_COPY_AND_ASSIGN(LocalStorageContextMojoTestWithService); 776 DISALLOW_COPY_AND_ASSIGN(LocalStorageContextMojoTestWithService);
724 }; 777 };
725 778
726 TEST_F(LocalStorageContextMojoTestWithService, InMemory) { 779 TEST_F(LocalStorageContextMojoTestWithService, InMemory) {
727 auto context = base::MakeUnique<LocalStorageContextMojo>( 780 auto context = base::MakeShared<LocalStorageContextMojo>(
728 connector(), nullptr, base::FilePath(), base::FilePath()); 781 connector(), nullptr, base::FilePath(), base::FilePath(), nullptr);
729 auto key = StdStringToUint8Vector("key"); 782 auto key = StdStringToUint8Vector("key");
730 auto value = StdStringToUint8Vector("value"); 783 auto value = StdStringToUint8Vector("value");
731 784
732 mojom::LevelDBWrapperPtr wrapper; 785 mojom::LevelDBWrapperPtr wrapper;
733 context->OpenLocalStorage(url::Origin(GURL("http://foobar.com")), 786 context->OpenLocalStorage(url::Origin(GURL("http://foobar.com")),
734 MakeRequest(&wrapper)); 787 MakeRequest(&wrapper));
735 788
736 DoTestPut(context.get(), key, value); 789 DoTestPut(context.get(), key, value);
737 std::vector<uint8_t> result; 790 std::vector<uint8_t> result;
738 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 791 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
739 EXPECT_EQ(value, result); 792 EXPECT_EQ(value, result);
740 793
741 context.reset(); 794 context->Shutdown();
795 context = nullptr;
742 base::RunLoop().RunUntilIdle(); 796 base::RunLoop().RunUntilIdle();
743 797
744 // Should not have created any files. 798 // Should not have created any files.
745 EXPECT_TRUE(FirstEntryInDir().empty()); 799 EXPECT_TRUE(FirstEntryInDir().empty());
746 800
747 // Re-opening should get fresh data. 801 // Re-opening should get fresh data.
748 context = base::MakeUnique<LocalStorageContextMojo>( 802 context = base::MakeShared<LocalStorageContextMojo>(
749 connector(), nullptr, base::FilePath(), base::FilePath()); 803 connector(), nullptr, base::FilePath(), base::FilePath(), nullptr);
750 EXPECT_FALSE(DoTestGet(context.get(), key, &result)); 804 EXPECT_FALSE(DoTestGet(context.get(), key, &result));
805 context->Shutdown();
751 } 806 }
752 807
753 TEST_F(LocalStorageContextMojoTestWithService, InMemoryInvalidPath) { 808 TEST_F(LocalStorageContextMojoTestWithService, InMemoryInvalidPath) {
754 auto context = base::MakeUnique<LocalStorageContextMojo>( 809 auto context = base::MakeShared<LocalStorageContextMojo>(
755 connector(), nullptr, base::FilePath(), 810 connector(), nullptr, base::FilePath(),
756 base::FilePath(FILE_PATH_LITERAL("../../"))); 811 base::FilePath(FILE_PATH_LITERAL("../../")), nullptr);
757 auto key = StdStringToUint8Vector("key"); 812 auto key = StdStringToUint8Vector("key");
758 auto value = StdStringToUint8Vector("value"); 813 auto value = StdStringToUint8Vector("value");
759 814
760 mojom::LevelDBWrapperPtr wrapper; 815 mojom::LevelDBWrapperPtr wrapper;
761 context->OpenLocalStorage(url::Origin(GURL("http://foobar.com")), 816 context->OpenLocalStorage(url::Origin(GURL("http://foobar.com")),
762 MakeRequest(&wrapper)); 817 MakeRequest(&wrapper));
763 818
764 DoTestPut(context.get(), key, value); 819 DoTestPut(context.get(), key, value);
765 std::vector<uint8_t> result; 820 std::vector<uint8_t> result;
766 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 821 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
767 EXPECT_EQ(value, result); 822 EXPECT_EQ(value, result);
768 823
769 context.reset(); 824 context->Shutdown();
825 context = nullptr;
770 base::RunLoop().RunUntilIdle(); 826 base::RunLoop().RunUntilIdle();
771 827
772 // Should not have created any files. 828 // Should not have created any files.
773 EXPECT_TRUE(FirstEntryInDir().empty()); 829 EXPECT_TRUE(FirstEntryInDir().empty());
774 } 830 }
775 831
776 TEST_F(LocalStorageContextMojoTestWithService, OnDisk) { 832 TEST_F(LocalStorageContextMojoTestWithService, OnDisk) {
777 base::FilePath test_path(FILE_PATH_LITERAL("test_path")); 833 base::FilePath test_path(FILE_PATH_LITERAL("test_path"));
778 auto context = base::MakeUnique<LocalStorageContextMojo>( 834 auto context = base::MakeShared<LocalStorageContextMojo>(
779 connector(), nullptr, base::FilePath(), test_path); 835 connector(), nullptr, base::FilePath(), test_path, nullptr);
780 auto key = StdStringToUint8Vector("key"); 836 auto key = StdStringToUint8Vector("key");
781 auto value = StdStringToUint8Vector("value"); 837 auto value = StdStringToUint8Vector("value");
782 838
783 DoTestPut(context.get(), key, value); 839 DoTestPut(context.get(), key, value);
784 std::vector<uint8_t> result; 840 std::vector<uint8_t> result;
785 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 841 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
786 EXPECT_EQ(value, result); 842 EXPECT_EQ(value, result);
787 843
788 context.reset(); 844 context->Shutdown();
845 context = nullptr;
789 base::RunLoop().RunUntilIdle(); 846 base::RunLoop().RunUntilIdle();
790 847
791 // Should have created files. 848 // Should have created files.
792 EXPECT_EQ(test_path, FirstEntryInDir().BaseName()); 849 EXPECT_EQ(test_path, FirstEntryInDir().BaseName());
793 850
794 // Should be able to re-open. 851 // Should be able to re-open.
795 context = base::MakeUnique<LocalStorageContextMojo>( 852 context = base::MakeShared<LocalStorageContextMojo>(
796 connector(), nullptr, base::FilePath(), test_path); 853 connector(), nullptr, base::FilePath(), test_path, nullptr);
797 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 854 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
798 EXPECT_EQ(value, result); 855 EXPECT_EQ(value, result);
856 context->Shutdown();
799 } 857 }
800 858
801 TEST_F(LocalStorageContextMojoTestWithService, InvalidVersionOnDisk) { 859 TEST_F(LocalStorageContextMojoTestWithService, InvalidVersionOnDisk) {
802 base::FilePath test_path(FILE_PATH_LITERAL("test_path")); 860 base::FilePath test_path(FILE_PATH_LITERAL("test_path"));
803 861
804 // Create context and add some data to it. 862 // Create context and add some data to it.
805 auto context = base::MakeUnique<LocalStorageContextMojo>( 863 auto context = base::MakeShared<LocalStorageContextMojo>(
806 connector(), nullptr, base::FilePath(), test_path); 864 connector(), nullptr, base::FilePath(), test_path, nullptr);
807 auto key = StdStringToUint8Vector("key"); 865 auto key = StdStringToUint8Vector("key");
808 auto value = StdStringToUint8Vector("value"); 866 auto value = StdStringToUint8Vector("value");
809 867
810 DoTestPut(context.get(), key, value); 868 DoTestPut(context.get(), key, value);
811 std::vector<uint8_t> result; 869 std::vector<uint8_t> result;
812 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 870 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
813 EXPECT_EQ(value, result); 871 EXPECT_EQ(value, result);
814 872
815 context.reset(); 873 context->Shutdown();
874 context = nullptr;
816 base::RunLoop().RunUntilIdle(); 875 base::RunLoop().RunUntilIdle();
817 876
818 { 877 {
819 // Mess up version number in database. 878 // Mess up version number in database.
820 leveldb_env::ChromiumEnv env; 879 leveldb_env::ChromiumEnv env;
821 leveldb::DB* db = nullptr; 880 leveldb::DB* db = nullptr;
822 leveldb::Options options; 881 leveldb::Options options;
823 options.env = &env; 882 options.env = &env;
824 base::FilePath db_path = 883 base::FilePath db_path =
825 temp_path().Append(test_path).Append(FILE_PATH_LITERAL("leveldb")); 884 temp_path().Append(test_path).Append(FILE_PATH_LITERAL("leveldb"));
826 ASSERT_TRUE(leveldb::DB::Open(options, db_path.AsUTF8Unsafe(), &db).ok()); 885 ASSERT_TRUE(leveldb::DB::Open(options, db_path.AsUTF8Unsafe(), &db).ok());
827 std::unique_ptr<leveldb::DB> db_owner(db); 886 std::unique_ptr<leveldb::DB> db_owner(db);
828 ASSERT_TRUE(db->Put(leveldb::WriteOptions(), "VERSION", "argh").ok()); 887 ASSERT_TRUE(db->Put(leveldb::WriteOptions(), "VERSION", "argh").ok());
829 } 888 }
830 889
831 // Make sure data is gone. 890 // Make sure data is gone.
832 context = base::MakeUnique<LocalStorageContextMojo>( 891 context = base::MakeShared<LocalStorageContextMojo>(
833 connector(), nullptr, base::FilePath(), test_path); 892 connector(), nullptr, base::FilePath(), test_path, nullptr);
834 EXPECT_FALSE(DoTestGet(context.get(), key, &result)); 893 EXPECT_FALSE(DoTestGet(context.get(), key, &result));
835 894
836 // Write data again. 895 // Write data again.
837 DoTestPut(context.get(), key, value); 896 DoTestPut(context.get(), key, value);
838 897
839 context.reset(); 898 context->Shutdown();
899 context = nullptr;
840 base::RunLoop().RunUntilIdle(); 900 base::RunLoop().RunUntilIdle();
841 901
842 // Data should have been preserved now. 902 // Data should have been preserved now.
843 context = base::MakeUnique<LocalStorageContextMojo>( 903 context = base::MakeShared<LocalStorageContextMojo>(
844 connector(), nullptr, base::FilePath(), test_path); 904 connector(), nullptr, base::FilePath(), test_path, nullptr);
845 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 905 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
846 EXPECT_EQ(value, result); 906 EXPECT_EQ(value, result);
907 context->Shutdown();
847 } 908 }
848 909
849 } // namespace content 910 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698