| OLD | NEW |
| 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 "components/filesystem/public/interfaces/file_system.mojom.h" | 10 #include "components/filesystem/public/interfaces/file_system.mojom.h" |
| 11 #include "components/leveldb/public/cpp/util.h" | 11 #include "components/leveldb/public/cpp/util.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/local_storage_usage_info.h" | 13 #include "content/public/browser/local_storage_usage_info.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "content/test/mock_leveldb_database.h" | 15 #include "content/test/mock_leveldb_database.h" |
| 16 #include "mojo/public/cpp/bindings/associated_binding.h" | 16 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 17 #include "mojo/public/cpp/bindings/binding.h" | 17 #include "mojo/public/cpp/bindings/binding.h" |
| 18 #include "mojo/public/cpp/bindings/binding_set.h" | 18 #include "mojo/public/cpp/bindings/binding_set.h" |
| 19 #include "services/file/file_service.h" | 19 #include "services/file/file_service.h" |
| 20 #include "services/file/public/interfaces/constants.mojom.h" | 20 #include "services/file/public/interfaces/constants.mojom.h" |
| 21 #include "services/file/user_id_map.h" | 21 #include "services/file/user_id_map.h" |
| 22 #include "services/service_manager/public/cpp/interface_factory.h" | 22 #include "services/service_manager/public/cpp/interface_factory.h" |
| 23 #include "services/service_manager/public/cpp/interface_registry.h" | 23 #include "services/service_manager/public/cpp/interface_registry.h" |
| 24 #include "services/service_manager/public/cpp/service_context.h" | 24 #include "services/service_manager/public/cpp/service_context.h" |
| 25 #include "services/service_manager/public/cpp/service_test.h" | 25 #include "services/service_manager/public/cpp/service_test.h" |
| 26 #include "services/service_manager/public/interfaces/service_factory.mojom.h" | 26 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 #include "third_party/leveldatabase/env_chromium.h" |
| 29 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 28 | 30 |
| 29 using leveldb::StdStringToUint8Vector; | 31 using leveldb::StdStringToUint8Vector; |
| 30 using leveldb::Uint8VectorToStdString; | 32 using leveldb::Uint8VectorToStdString; |
| 31 | 33 |
| 32 namespace content { | 34 namespace content { |
| 33 | 35 |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| 36 void NoOpSuccess(bool success) {} | 38 void NoOpSuccess(bool success) {} |
| 37 | 39 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 } // namespace | 107 } // namespace |
| 106 | 108 |
| 107 class LocalStorageContextMojoTest : public testing::Test { | 109 class LocalStorageContextMojoTest : public testing::Test { |
| 108 public: | 110 public: |
| 109 LocalStorageContextMojoTest() : db_(&mock_data_), db_binding_(&db_) {} | 111 LocalStorageContextMojoTest() : db_(&mock_data_), db_binding_(&db_) {} |
| 110 | 112 |
| 111 LocalStorageContextMojo* context() { | 113 LocalStorageContextMojo* context() { |
| 112 if (!context_) { | 114 if (!context_) { |
| 113 context_ = | 115 context_ = |
| 114 base::MakeUnique<LocalStorageContextMojo>(nullptr, base::FilePath()); | 116 base::MakeUnique<LocalStorageContextMojo>(nullptr, base::FilePath()); |
| 115 context_->SetDatabaseForTesting(db_binding_.CreateInterfacePtrAndBind()); | 117 db_binding_.Bind(context_->DatabaseRequestForTesting()); |
| 116 } | 118 } |
| 117 return context_.get(); | 119 return context_.get(); |
| 118 } | 120 } |
| 119 const std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data() { | 121 const std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data() { |
| 120 return mock_data_; | 122 return mock_data_; |
| 121 } | 123 } |
| 122 | 124 |
| 123 void set_mock_data(const std::string& key, const std::string& value) { | 125 void set_mock_data(const std::string& key, const std::string& value) { |
| 124 mock_data_[StdStringToUint8Vector(key)] = StdStringToUint8Vector(value); | 126 mock_data_[StdStringToUint8Vector(key)] = StdStringToUint8Vector(value); |
| 125 } | 127 } |
| 126 | 128 |
| 127 std::vector<LocalStorageUsageInfo> GetStorageUsageSync() { | 129 std::vector<LocalStorageUsageInfo> GetStorageUsageSync() { |
| 128 base::RunLoop run_loop; | 130 base::RunLoop run_loop; |
| 129 std::vector<LocalStorageUsageInfo> result; | 131 std::vector<LocalStorageUsageInfo> result; |
| 130 context()->GetStorageUsage(base::BindOnce(&GetStorageUsageCallback, | 132 context()->GetStorageUsage(base::BindOnce(&GetStorageUsageCallback, |
| 131 run_loop.QuitClosure(), &result)); | 133 run_loop.QuitClosure(), &result)); |
| 132 run_loop.Run(); | 134 run_loop.Run(); |
| 133 return result; | 135 return result; |
| 134 } | 136 } |
| 135 | 137 |
| 136 private: | 138 private: |
| 137 TestBrowserThreadBundle thread_bundle_; | 139 TestBrowserThreadBundle thread_bundle_; |
| 138 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_; | 140 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_; |
| 139 MockLevelDBDatabase db_; | 141 MockLevelDBDatabase db_; |
| 140 mojo::Binding<leveldb::mojom::LevelDBDatabase> db_binding_; | 142 mojo::AssociatedGroup associated_group_; |
| 143 mojo::AssociatedBinding<leveldb::mojom::LevelDBDatabase> db_binding_; |
| 141 | 144 |
| 142 std::unique_ptr<LocalStorageContextMojo> context_; | 145 std::unique_ptr<LocalStorageContextMojo> context_; |
| 143 | 146 |
| 144 DISALLOW_COPY_AND_ASSIGN(LocalStorageContextMojoTest); | 147 DISALLOW_COPY_AND_ASSIGN(LocalStorageContextMojoTest); |
| 145 }; | 148 }; |
| 146 | 149 |
| 147 TEST_F(LocalStorageContextMojoTest, Basic) { | 150 TEST_F(LocalStorageContextMojoTest, Basic) { |
| 148 auto key = StdStringToUint8Vector("key"); | 151 auto key = StdStringToUint8Vector("key"); |
| 149 auto value = StdStringToUint8Vector("value"); | 152 auto value = StdStringToUint8Vector("value"); |
| 150 | 153 |
| (...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 725 | 728 |
| 726 // Should have created files. | 729 // Should have created files. |
| 727 EXPECT_EQ(test_path, FirstEntryInDir().BaseName()); | 730 EXPECT_EQ(test_path, FirstEntryInDir().BaseName()); |
| 728 | 731 |
| 729 // Should be able to re-open. | 732 // Should be able to re-open. |
| 730 context.reset(new LocalStorageContextMojo(connector(), test_path)); | 733 context.reset(new LocalStorageContextMojo(connector(), test_path)); |
| 731 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); | 734 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); |
| 732 EXPECT_EQ(value, result); | 735 EXPECT_EQ(value, result); |
| 733 } | 736 } |
| 734 | 737 |
| 738 // Enable when http://crbug.com/677194 is fixed and ServiceTest works |
| 739 // correctly on Android. |
| 740 #if defined(OS_ANDROID) |
| 741 #define MAYBE_InvalidVersionOnDisk DISABLED_InvalidVersionOnDisk |
| 742 #else |
| 743 #define MAYBE_InvalidVersionOnDisk InvalidVersionOnDisk |
| 744 #endif |
| 745 TEST_F(LocalStorageContextMojoTestWithService, MAYBE_InvalidVersionOnDisk) { |
| 746 base::FilePath test_path(FILE_PATH_LITERAL("test_path")); |
| 747 |
| 748 // Create context and add some data to it. |
| 749 auto context = |
| 750 base::MakeUnique<LocalStorageContextMojo>(connector(), test_path); |
| 751 auto key = StdStringToUint8Vector("key"); |
| 752 auto value = StdStringToUint8Vector("value"); |
| 753 |
| 754 DoTestPut(context.get(), key, value); |
| 755 std::vector<uint8_t> result; |
| 756 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); |
| 757 EXPECT_EQ(value, result); |
| 758 |
| 759 context.reset(); |
| 760 base::RunLoop().RunUntilIdle(); |
| 761 |
| 762 { |
| 763 // Mess up version number in database. |
| 764 leveldb_env::ChromiumEnv env; |
| 765 leveldb::DB* db = nullptr; |
| 766 leveldb::Options options; |
| 767 options.env = &env; |
| 768 base::FilePath db_path = |
| 769 temp_path().Append(test_path).Append(FILE_PATH_LITERAL("leveldb")); |
| 770 ASSERT_TRUE(leveldb::DB::Open(options, db_path.AsUTF8Unsafe(), &db).ok()); |
| 771 std::unique_ptr<leveldb::DB> db_owner(db); |
| 772 ASSERT_TRUE(db->Put(leveldb::WriteOptions(), "VERSION", "argh").ok()); |
| 773 } |
| 774 |
| 775 // Make sure data is gone. |
| 776 context = base::MakeUnique<LocalStorageContextMojo>(connector(), test_path); |
| 777 EXPECT_FALSE(DoTestGet(context.get(), key, &result)); |
| 778 |
| 779 // Write data again. |
| 780 DoTestPut(context.get(), key, value); |
| 781 |
| 782 context.reset(); |
| 783 base::RunLoop().RunUntilIdle(); |
| 784 |
| 785 // Data should have been preserved now. |
| 786 context = base::MakeUnique<LocalStorageContextMojo>(connector(), test_path); |
| 787 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); |
| 788 EXPECT_EQ(value, result); |
| 789 } |
| 790 |
| 735 } // namespace content | 791 } // namespace content |
| OLD | NEW |