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

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

Powered by Google App Engine
This is Rietveld 408576698