| 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/leveldb_wrapper_impl.h" | 5 #include "content/browser/leveldb_wrapper_impl.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "components/leveldb/public/cpp/util.h" | 9 #include "components/leveldb/public/cpp/util.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "content/test/mock_leveldb_database.h" | 11 #include "content/test/mock_leveldb_database.h" |
| 12 #include "mojo/public/cpp/bindings/associated_binding.h" | 12 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 13 #include "mojo/public/cpp/bindings/strong_associated_binding.h" | 13 #include "mojo/public/cpp/bindings/strong_associated_binding.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using leveldb::StdStringToUint8Vector; | 16 using leveldb::StdStringToUint8Vector; |
| 17 using leveldb::Uint8VectorToStdString; | 17 using leveldb::Uint8VectorToStdString; |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const char* kTestPrefix = "abc"; | 23 const char* kTestPrefix = "abc"; |
| 24 const char* kTestSource = "source"; | 24 const char* kTestSource = "source"; |
| 25 const size_t kTestSizeLimit = 512; | 25 const size_t kTestSizeLimit = 512; |
| 26 | 26 |
| 27 class GetAllCallback : public mojom::LevelDBWrapperGetAllCallback { | 27 class GetAllCallback : public mojom::LevelDBWrapperGetAllCallback { |
| 28 public: | 28 public: |
| 29 static mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo CreateAndBind( | 29 static mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo CreateAndBind( |
| 30 mojo::AssociatedGroup* associated_group, | |
| 31 bool* result, | 30 bool* result, |
| 32 const base::Closure& callback) { | 31 const base::Closure& callback) { |
| 33 mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo ptr_info; | 32 mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo ptr_info; |
| 34 mojom::LevelDBWrapperGetAllCallbackAssociatedRequest request; | 33 auto request = mojo::MakeRequest(&ptr_info); |
| 35 associated_group->CreateAssociatedInterface( | |
| 36 mojo::AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); | |
| 37 mojo::MakeStrongAssociatedBinding( | 34 mojo::MakeStrongAssociatedBinding( |
| 38 base::WrapUnique(new GetAllCallback(result, callback)), | 35 base::WrapUnique(new GetAllCallback(result, callback)), |
| 39 std::move(request)); | 36 std::move(request)); |
| 40 return ptr_info; | 37 return ptr_info; |
| 41 } | 38 } |
| 42 | 39 |
| 43 private: | 40 private: |
| 44 GetAllCallback(bool* result, const base::Closure& callback) | 41 GetAllCallback(bool* result, const base::Closure& callback) |
| 45 : m_result(result), m_callback(callback) {} | 42 : m_result(result), m_callback(callback) {} |
| 46 void Complete(bool success) override { | 43 void Complete(bool success) override { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 10 * 1024 * 1024 /* max_bytes_per_hour */, | 99 10 * 1024 * 1024 /* max_bytes_per_hour */, |
| 103 60 /* max_commits_per_hour */, | 100 60 /* max_commits_per_hour */, |
| 104 &delegate_), | 101 &delegate_), |
| 105 observer_binding_(this) { | 102 observer_binding_(this) { |
| 106 set_mock_data(std::string(kTestPrefix) + "def", "defdata"); | 103 set_mock_data(std::string(kTestPrefix) + "def", "defdata"); |
| 107 set_mock_data(std::string(kTestPrefix) + "123", "123data"); | 104 set_mock_data(std::string(kTestPrefix) + "123", "123data"); |
| 108 set_mock_data("123", "baddata"); | 105 set_mock_data("123", "baddata"); |
| 109 | 106 |
| 110 level_db_wrapper_.Bind(mojo::MakeRequest(&level_db_wrapper_ptr_)); | 107 level_db_wrapper_.Bind(mojo::MakeRequest(&level_db_wrapper_ptr_)); |
| 111 mojom::LevelDBObserverAssociatedPtrInfo ptr_info; | 108 mojom::LevelDBObserverAssociatedPtrInfo ptr_info; |
| 112 observer_binding_.Bind(&ptr_info, associated_group()); | 109 observer_binding_.Bind(&ptr_info); |
| 113 level_db_wrapper_ptr_->AddObserver(std::move(ptr_info)); | 110 level_db_wrapper_ptr_->AddObserver(std::move(ptr_info)); |
| 114 } | 111 } |
| 115 | 112 |
| 116 void set_mock_data(const std::string& key, const std::string& value) { | 113 void set_mock_data(const std::string& key, const std::string& value) { |
| 117 mock_data_[StdStringToUint8Vector(key)] = StdStringToUint8Vector(value); | 114 mock_data_[StdStringToUint8Vector(key)] = StdStringToUint8Vector(value); |
| 118 } | 115 } |
| 119 | 116 |
| 120 void set_mock_data(const std::vector<uint8_t>& key, | 117 void set_mock_data(const std::vector<uint8_t>& key, |
| 121 const std::vector<uint8_t>& value) { | 118 const std::vector<uint8_t>& value) { |
| 122 mock_data_[key] = value; | 119 mock_data_[key] = value; |
| 123 } | 120 } |
| 124 | 121 |
| 125 bool has_mock_data(const std::string& key) { | 122 bool has_mock_data(const std::string& key) { |
| 126 return mock_data_.find(StdStringToUint8Vector(key)) != mock_data_.end(); | 123 return mock_data_.find(StdStringToUint8Vector(key)) != mock_data_.end(); |
| 127 } | 124 } |
| 128 | 125 |
| 129 std::string get_mock_data(const std::string& key) { | 126 std::string get_mock_data(const std::string& key) { |
| 130 return has_mock_data(key) | 127 return has_mock_data(key) |
| 131 ? Uint8VectorToStdString(mock_data_[StdStringToUint8Vector(key)]) | 128 ? Uint8VectorToStdString(mock_data_[StdStringToUint8Vector(key)]) |
| 132 : ""; | 129 : ""; |
| 133 } | 130 } |
| 134 | 131 |
| 135 void clear_mock_data() { mock_data_.clear(); } | 132 void clear_mock_data() { mock_data_.clear(); } |
| 136 | 133 |
| 137 mojom::LevelDBWrapper* wrapper() { return level_db_wrapper_ptr_.get(); } | 134 mojom::LevelDBWrapper* wrapper() { return level_db_wrapper_ptr_.get(); } |
| 138 mojo::AssociatedGroup* associated_group() { | |
| 139 return level_db_wrapper_ptr_.associated_group(); | |
| 140 } | |
| 141 | 135 |
| 142 bool GetSync(const std::vector<uint8_t>& key, std::vector<uint8_t>* result) { | 136 bool GetSync(const std::vector<uint8_t>& key, std::vector<uint8_t>* result) { |
| 143 base::RunLoop run_loop; | 137 base::RunLoop run_loop; |
| 144 bool success = false; | 138 bool success = false; |
| 145 wrapper()->Get(key, base::Bind(&GetCallback, run_loop.QuitClosure(), | 139 wrapper()->Get(key, base::Bind(&GetCallback, run_loop.QuitClosure(), |
| 146 &success, result)); | 140 &success, result)); |
| 147 run_loop.Run(); | 141 run_loop.Run(); |
| 148 return success; | 142 return success; |
| 149 } | 143 } |
| 150 | 144 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 EXPECT_TRUE(GetSync(key, &result)); | 243 EXPECT_TRUE(GetSync(key, &result)); |
| 250 EXPECT_EQ(value, result); | 244 EXPECT_EQ(value, result); |
| 251 } | 245 } |
| 252 | 246 |
| 253 TEST_F(LevelDBWrapperImplTest, GetAll) { | 247 TEST_F(LevelDBWrapperImplTest, GetAll) { |
| 254 leveldb::mojom::DatabaseError status; | 248 leveldb::mojom::DatabaseError status; |
| 255 std::vector<mojom::KeyValuePtr> data; | 249 std::vector<mojom::KeyValuePtr> data; |
| 256 base::RunLoop run_loop; | 250 base::RunLoop run_loop; |
| 257 bool result = false; | 251 bool result = false; |
| 258 EXPECT_TRUE(wrapper()->GetAll( | 252 EXPECT_TRUE(wrapper()->GetAll( |
| 259 GetAllCallback::CreateAndBind(associated_group(), &result, | 253 GetAllCallback::CreateAndBind(&result, run_loop.QuitClosure()), &status, |
| 260 run_loop.QuitClosure()), | 254 &data)); |
| 261 &status, &data)); | |
| 262 EXPECT_EQ(leveldb::mojom::DatabaseError::OK, status); | 255 EXPECT_EQ(leveldb::mojom::DatabaseError::OK, status); |
| 263 EXPECT_EQ(2u, data.size()); | 256 EXPECT_EQ(2u, data.size()); |
| 264 EXPECT_FALSE(result); | 257 EXPECT_FALSE(result); |
| 265 run_loop.Run(); | 258 run_loop.Run(); |
| 266 EXPECT_TRUE(result); | 259 EXPECT_TRUE(result); |
| 267 } | 260 } |
| 268 | 261 |
| 269 TEST_F(LevelDBWrapperImplTest, CommitPutToDB) { | 262 TEST_F(LevelDBWrapperImplTest, CommitPutToDB) { |
| 270 std::string key1 = "123"; | 263 std::string key1 = "123"; |
| 271 std::string value1 = "foo"; | 264 std::string value1 = "foo"; |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 // Reducing size should also succeed. | 474 // Reducing size should also succeed. |
| 482 value.clear(); | 475 value.clear(); |
| 483 EXPECT_TRUE(PutSync(key, value)); | 476 EXPECT_TRUE(PutSync(key, value)); |
| 484 | 477 |
| 485 // Increasing size should fail. | 478 // Increasing size should fail. |
| 486 value.resize(1, 'a'); | 479 value.resize(1, 'a'); |
| 487 EXPECT_FALSE(PutSync(key, value)); | 480 EXPECT_FALSE(PutSync(key, value)); |
| 488 } | 481 } |
| 489 | 482 |
| 490 } // namespace content | 483 } // namespace content |
| OLD | NEW |