| 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" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 callback.Run(); | 68 callback.Run(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void SuccessCallback(const base::Closure& callback, | 71 void SuccessCallback(const base::Closure& callback, |
| 72 bool* success_out, | 72 bool* success_out, |
| 73 bool success) { | 73 bool success) { |
| 74 *success_out = success; | 74 *success_out = success; |
| 75 callback.Run(); | 75 callback.Run(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void NoOpSuccessCallback(bool success) {} |
| 79 |
| 78 } // namespace | 80 } // namespace |
| 79 | 81 |
| 80 class LevelDBWrapperImplTest : public testing::Test, | 82 class LevelDBWrapperImplTest : public testing::Test, |
| 81 public mojom::LevelDBObserver { | 83 public mojom::LevelDBObserver { |
| 82 public: | 84 public: |
| 83 struct Observation { | 85 struct Observation { |
| 84 enum { kAdd, kChange, kDelete, kDeleteAll } type; | 86 enum { kAdd, kChange, kDelete, kDeleteAll } type; |
| 85 std::string key; | 87 std::string key; |
| 86 std::string old_value; | 88 std::string old_value; |
| 87 std::string new_value; | 89 std::string new_value; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 bool DeleteAllSync() { | 169 bool DeleteAllSync() { |
| 168 base::RunLoop run_loop; | 170 base::RunLoop run_loop; |
| 169 bool success = false; | 171 bool success = false; |
| 170 wrapper()->DeleteAll( | 172 wrapper()->DeleteAll( |
| 171 kTestSource, | 173 kTestSource, |
| 172 base::Bind(&SuccessCallback, run_loop.QuitClosure(), &success)); | 174 base::Bind(&SuccessCallback, run_loop.QuitClosure(), &success)); |
| 173 run_loop.Run(); | 175 run_loop.Run(); |
| 174 return success; | 176 return success; |
| 175 } | 177 } |
| 176 | 178 |
| 177 void CommitChanges() { | 179 void CommitChanges() { level_db_wrapper_.ScheduleImmediateCommit(); } |
| 178 ASSERT_TRUE(level_db_wrapper_.commit_batch_); | |
| 179 level_db_wrapper_.CommitChanges(); | |
| 180 } | |
| 181 | 180 |
| 182 const std::vector<Observation>& observations() { return observations_; } | 181 const std::vector<Observation>& observations() { return observations_; } |
| 183 | 182 |
| 184 private: | 183 private: |
| 185 // LevelDBObserver: | 184 // LevelDBObserver: |
| 186 void KeyAdded(const std::vector<uint8_t>& key, | 185 void KeyAdded(const std::vector<uint8_t>& key, |
| 187 const std::vector<uint8_t>& value, | 186 const std::vector<uint8_t>& value, |
| 188 const std::string& source) override { | 187 const std::string& source) override { |
| 189 observations_.push_back({Observation::kAdd, Uint8VectorToStdString(key), "", | 188 observations_.push_back({Observation::kAdd, Uint8VectorToStdString(key), "", |
| 190 Uint8VectorToStdString(value), source}); | 189 Uint8VectorToStdString(value), source}); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 EXPECT_EQ(key, observations()[0].key); | 324 EXPECT_EQ(key, observations()[0].key); |
| 326 EXPECT_EQ(value, observations()[0].old_value); | 325 EXPECT_EQ(value, observations()[0].old_value); |
| 327 EXPECT_EQ(kTestSource, observations()[0].source); | 326 EXPECT_EQ(kTestSource, observations()[0].source); |
| 328 | 327 |
| 329 EXPECT_TRUE(has_mock_data(kTestPrefix + key)); | 328 EXPECT_TRUE(has_mock_data(kTestPrefix + key)); |
| 330 | 329 |
| 331 CommitChanges(); | 330 CommitChanges(); |
| 332 EXPECT_FALSE(has_mock_data(kTestPrefix + key)); | 331 EXPECT_FALSE(has_mock_data(kTestPrefix + key)); |
| 333 } | 332 } |
| 334 | 333 |
| 335 TEST_F(LevelDBWrapperImplTest, DeleteAll) { | 334 TEST_F(LevelDBWrapperImplTest, DeleteAllWithoutLoadedMap) { |
| 336 std::string key = "newkey"; | 335 std::string key = "newkey"; |
| 337 std::string value = "foo"; | 336 std::string value = "foo"; |
| 338 std::string dummy_key = "foobar"; | 337 std::string dummy_key = "foobar"; |
| 339 set_mock_data(dummy_key, value); | 338 set_mock_data(dummy_key, value); |
| 340 set_mock_data(kTestPrefix + key, value); | 339 set_mock_data(kTestPrefix + key, value); |
| 341 | 340 |
| 342 EXPECT_TRUE(DeleteAllSync()); | 341 EXPECT_TRUE(DeleteAllSync()); |
| 343 ASSERT_EQ(1u, observations().size()); | 342 ASSERT_EQ(1u, observations().size()); |
| 344 EXPECT_EQ(Observation::kDeleteAll, observations()[0].type); | 343 EXPECT_EQ(Observation::kDeleteAll, observations()[0].type); |
| 345 EXPECT_EQ(kTestSource, observations()[0].source); | 344 EXPECT_EQ(kTestSource, observations()[0].source); |
| 346 | 345 |
| 347 EXPECT_TRUE(has_mock_data(kTestPrefix + key)); | 346 EXPECT_TRUE(has_mock_data(kTestPrefix + key)); |
| 348 EXPECT_TRUE(has_mock_data(dummy_key)); | 347 EXPECT_TRUE(has_mock_data(dummy_key)); |
| 349 | 348 |
| 350 CommitChanges(); | 349 CommitChanges(); |
| 351 EXPECT_FALSE(has_mock_data(kTestPrefix + key)); | 350 EXPECT_FALSE(has_mock_data(kTestPrefix + key)); |
| 352 EXPECT_TRUE(has_mock_data(dummy_key)); | 351 EXPECT_TRUE(has_mock_data(dummy_key)); |
| 353 | 352 |
| 354 // Deleting all again should still work, an cause an observation. | 353 // Deleting all again should still work, an cause an observation. |
| 355 EXPECT_TRUE(DeleteAllSync()); | 354 EXPECT_TRUE(DeleteAllSync()); |
| 356 ASSERT_EQ(2u, observations().size()); | 355 ASSERT_EQ(2u, observations().size()); |
| 357 EXPECT_EQ(Observation::kDeleteAll, observations()[1].type); | 356 EXPECT_EQ(Observation::kDeleteAll, observations()[1].type); |
| 358 EXPECT_EQ(kTestSource, observations()[1].source); | 357 EXPECT_EQ(kTestSource, observations()[1].source); |
| 359 | 358 |
| 360 // And now we've deleted all, writing something the quota size should work. | 359 // And now we've deleted all, writing something the quota size should work. |
| 361 EXPECT_TRUE(PutSync(std::vector<uint8_t>(kTestSizeLimit, 'b'), | 360 EXPECT_TRUE(PutSync(std::vector<uint8_t>(kTestSizeLimit, 'b'), |
| 362 std::vector<uint8_t>())); | 361 std::vector<uint8_t>())); |
| 363 } | 362 } |
| 364 | 363 |
| 364 TEST_F(LevelDBWrapperImplTest, DeleteAllWithLoadedMap) { |
| 365 std::string key = "newkey"; |
| 366 std::string value = "foo"; |
| 367 std::string dummy_key = "foobar"; |
| 368 set_mock_data(dummy_key, value); |
| 369 |
| 370 EXPECT_TRUE( |
| 371 PutSync(StdStringToUint8Vector(key), StdStringToUint8Vector(value))); |
| 372 |
| 373 EXPECT_TRUE(DeleteAllSync()); |
| 374 ASSERT_EQ(2u, observations().size()); |
| 375 EXPECT_EQ(Observation::kDeleteAll, observations()[1].type); |
| 376 EXPECT_EQ(kTestSource, observations()[1].source); |
| 377 |
| 378 EXPECT_TRUE(has_mock_data(dummy_key)); |
| 379 |
| 380 CommitChanges(); |
| 381 EXPECT_FALSE(has_mock_data(kTestPrefix + key)); |
| 382 EXPECT_TRUE(has_mock_data(dummy_key)); |
| 383 } |
| 384 |
| 385 TEST_F(LevelDBWrapperImplTest, DeleteAllWithPendingMapLoad) { |
| 386 std::string key = "newkey"; |
| 387 std::string value = "foo"; |
| 388 std::string dummy_key = "foobar"; |
| 389 set_mock_data(dummy_key, value); |
| 390 |
| 391 wrapper()->Put(StdStringToUint8Vector(key), StdStringToUint8Vector(value), |
| 392 kTestSource, base::Bind(&NoOpSuccessCallback)); |
| 393 |
| 394 EXPECT_TRUE(DeleteAllSync()); |
| 395 ASSERT_EQ(2u, observations().size()); |
| 396 EXPECT_EQ(Observation::kDeleteAll, observations()[1].type); |
| 397 EXPECT_EQ(kTestSource, observations()[1].source); |
| 398 |
| 399 EXPECT_TRUE(has_mock_data(dummy_key)); |
| 400 |
| 401 CommitChanges(); |
| 402 EXPECT_FALSE(has_mock_data(kTestPrefix + key)); |
| 403 EXPECT_TRUE(has_mock_data(dummy_key)); |
| 404 } |
| 405 |
| 365 TEST_F(LevelDBWrapperImplTest, PutOverQuotaLargeValue) { | 406 TEST_F(LevelDBWrapperImplTest, PutOverQuotaLargeValue) { |
| 366 std::vector<uint8_t> key = StdStringToUint8Vector("newkey"); | 407 std::vector<uint8_t> key = StdStringToUint8Vector("newkey"); |
| 367 std::vector<uint8_t> value(kTestSizeLimit, 4); | 408 std::vector<uint8_t> value(kTestSizeLimit, 4); |
| 368 | 409 |
| 369 EXPECT_FALSE(PutSync(key, value)); | 410 EXPECT_FALSE(PutSync(key, value)); |
| 370 | 411 |
| 371 value.resize(kTestSizeLimit / 2); | 412 value.resize(kTestSizeLimit / 2); |
| 372 EXPECT_TRUE(PutSync(key, value)); | 413 EXPECT_TRUE(PutSync(key, value)); |
| 373 } | 414 } |
| 374 | 415 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 // Reducing size should also succeed. | 466 // Reducing size should also succeed. |
| 426 value.clear(); | 467 value.clear(); |
| 427 EXPECT_TRUE(PutSync(key, value)); | 468 EXPECT_TRUE(PutSync(key, value)); |
| 428 | 469 |
| 429 // Increasing size should fail. | 470 // Increasing size should fail. |
| 430 value.resize(1, 'a'); | 471 value.resize(1, 'a'); |
| 431 EXPECT_FALSE(PutSync(key, value)); | 472 EXPECT_FALSE(PutSync(key, value)); |
| 432 } | 473 } |
| 433 | 474 |
| 434 } // namespace content | 475 } // namespace content |
| OLD | NEW |