OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stddef.h> |
| 6 |
| 7 #include <algorithm> |
| 8 #include <cstring> |
| 9 #include <string> |
| 10 |
| 11 #include "base/files/file.h" |
| 12 #include "base/files/file_path.h" |
| 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/strings/string_piece.h" |
| 15 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" |
| 16 #include "content/browser/indexed_db/leveldb/leveldb_database.h" |
| 17 #include "content/browser/indexed_db/leveldb/leveldb_env.h" |
| 18 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" |
| 19 #include "content/browser/indexed_db/leveldb/leveldb_transaction.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "third_party/leveldatabase/env_chromium.h" |
| 22 |
| 23 namespace content { |
| 24 |
| 25 namespace { |
| 26 |
| 27 class SimpleComparator : public LevelDBComparator { |
| 28 public: |
| 29 int Compare(const base::StringPiece& a, |
| 30 const base::StringPiece& b) const override { |
| 31 size_t len = std::min(a.size(), b.size()); |
| 32 return memcmp(a.begin(), b.begin(), len); |
| 33 } |
| 34 const char* Name() const override { return "temp_comparator"; } |
| 35 }; |
| 36 |
| 37 class LevelDBTransactionTest : public testing::Test { |
| 38 public: |
| 39 LevelDBTransactionTest() {} |
| 40 void SetUp() override { |
| 41 ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); |
| 42 LevelDBDatabase::Open(temp_directory_.GetPath(), &comparator_, &leveldb_); |
| 43 ASSERT_TRUE(leveldb_); |
| 44 } |
| 45 void TearDown() override {} |
| 46 |
| 47 protected: |
| 48 // Convenience methods to access the database outside any |
| 49 // transaction to cut down on boilerplate around calls. |
| 50 void Put(const base::StringPiece& key, const std::string& value) { |
| 51 std::string put_value = value; |
| 52 leveldb::Status s = leveldb_->Put(key, &put_value); |
| 53 ASSERT_TRUE(s.ok()); |
| 54 } |
| 55 |
| 56 void Remove(const base::StringPiece& key) { |
| 57 leveldb::Status s = leveldb_->Remove(key); |
| 58 ASSERT_TRUE(s.ok()); |
| 59 } |
| 60 |
| 61 void Get(const base::StringPiece& key, std::string* value, bool* found) { |
| 62 leveldb::Status s = leveldb_->Get(key, value, found); |
| 63 ASSERT_TRUE(s.ok()); |
| 64 } |
| 65 |
| 66 bool Has(const base::StringPiece& key) { |
| 67 bool found; |
| 68 std::string value; |
| 69 leveldb::Status s = leveldb_->Get(key, &value, &found); |
| 70 EXPECT_TRUE(s.ok()); |
| 71 return found; |
| 72 } |
| 73 |
| 74 // Convenience wrappers for LevelDBTransaction operations to |
| 75 // avoid boilerplate in tests. |
| 76 bool TransactionHas(LevelDBTransaction* transaction, |
| 77 const base::StringPiece& key) { |
| 78 std::string value; |
| 79 bool found; |
| 80 leveldb::Status s = transaction->Get(key, &value, &found); |
| 81 EXPECT_TRUE(s.ok()); |
| 82 return found; |
| 83 } |
| 84 |
| 85 void TransactionPut(LevelDBTransaction* transaction, |
| 86 const base::StringPiece& key, |
| 87 const std::string& value) { |
| 88 std::string put_value = value; |
| 89 transaction->Put(key, &put_value); |
| 90 } |
| 91 |
| 92 int Compare(const base::StringPiece& a, const base::StringPiece& b) const { |
| 93 return comparator_.Compare(a, b); |
| 94 } |
| 95 |
| 96 LevelDBDatabase* db() { return leveldb_.get(); } |
| 97 |
| 98 const std::string key_before_range_ = "a"; |
| 99 const std::string range_start_ = "b"; |
| 100 const std::string key_in_range1_ = "c"; |
| 101 const std::string key_in_range2_ = "d"; |
| 102 const std::string range_end_ = "e"; |
| 103 const std::string key_after_range_ = "f"; |
| 104 const std::string value_ = "value"; |
| 105 |
| 106 private: |
| 107 base::ScopedTempDir temp_directory_; |
| 108 SimpleComparator comparator_; |
| 109 std::unique_ptr<LevelDBDatabase> leveldb_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(LevelDBTransactionTest); |
| 112 }; |
| 113 |
| 114 } // namespace |
| 115 |
| 116 TEST_F(LevelDBTransactionTest, GetAndPut) { |
| 117 leveldb::Status status; |
| 118 |
| 119 const std::string key("key"); |
| 120 std::string got_value; |
| 121 |
| 122 const std::string old_value("value"); |
| 123 Put(key, old_value); |
| 124 |
| 125 scoped_refptr<LevelDBTransaction> transaction = new LevelDBTransaction(db()); |
| 126 |
| 127 const std::string new_value("new value"); |
| 128 Put(key, new_value); |
| 129 |
| 130 bool found = false; |
| 131 status = transaction->Get(key, &got_value, &found); |
| 132 EXPECT_TRUE(status.ok()); |
| 133 EXPECT_TRUE(found); |
| 134 EXPECT_EQ(Compare(got_value, old_value), 0); |
| 135 |
| 136 Get(key, &got_value, &found); |
| 137 EXPECT_TRUE(found); |
| 138 EXPECT_EQ(Compare(got_value, new_value), 0); |
| 139 |
| 140 const std::string added_key("added key"); |
| 141 const std::string added_value("added value"); |
| 142 Put(added_key, added_value); |
| 143 |
| 144 Get(added_key, &got_value, &found); |
| 145 EXPECT_TRUE(found); |
| 146 EXPECT_EQ(Compare(got_value, added_value), 0); |
| 147 |
| 148 EXPECT_FALSE(TransactionHas(transaction.get(), added_key)); |
| 149 |
| 150 const std::string another_key("another key"); |
| 151 const std::string another_value("another value"); |
| 152 TransactionPut(transaction.get(), another_key, another_value); |
| 153 |
| 154 status = transaction->Get(another_key, &got_value, &found); |
| 155 EXPECT_TRUE(status.ok()); |
| 156 EXPECT_TRUE(found); |
| 157 EXPECT_EQ(Compare(got_value, another_value), 0); |
| 158 } |
| 159 |
| 160 TEST_F(LevelDBTransactionTest, Iterator) { |
| 161 const std::string key1("key1"); |
| 162 const std::string value1("value1"); |
| 163 const std::string key2("key2"); |
| 164 const std::string value2("value2"); |
| 165 |
| 166 Put(key1, value1); |
| 167 Put(key2, value2); |
| 168 |
| 169 scoped_refptr<LevelDBTransaction> transaction = new LevelDBTransaction(db()); |
| 170 |
| 171 Remove(key2); |
| 172 |
| 173 std::unique_ptr<LevelDBIterator> it = transaction->CreateIterator(); |
| 174 |
| 175 it->Seek(std::string()); |
| 176 |
| 177 EXPECT_TRUE(it->IsValid()); |
| 178 EXPECT_EQ(Compare(it->Key(), key1), 0); |
| 179 EXPECT_EQ(Compare(it->Value(), value1), 0); |
| 180 |
| 181 it->Next(); |
| 182 |
| 183 EXPECT_TRUE(it->IsValid()); |
| 184 EXPECT_EQ(Compare(it->Key(), key2), 0); |
| 185 EXPECT_EQ(Compare(it->Value(), value2), 0); |
| 186 |
| 187 it->Next(); |
| 188 |
| 189 EXPECT_FALSE(it->IsValid()); |
| 190 } |
| 191 |
| 192 TEST_F(LevelDBTransactionTest, Commit) { |
| 193 const std::string key1("key1"); |
| 194 const std::string key2("key2"); |
| 195 const std::string value1("value1"); |
| 196 const std::string value2("value2"); |
| 197 const std::string value3("value3"); |
| 198 |
| 199 std::string got_value; |
| 200 bool found; |
| 201 |
| 202 scoped_refptr<LevelDBTransaction> transaction = new LevelDBTransaction(db()); |
| 203 |
| 204 TransactionPut(transaction.get(), key1, value1); |
| 205 TransactionPut(transaction.get(), key2, value2); |
| 206 TransactionPut(transaction.get(), key2, value3); |
| 207 |
| 208 leveldb::Status status = transaction->Commit(); |
| 209 EXPECT_TRUE(status.ok()); |
| 210 |
| 211 Get(key1, &got_value, &found); |
| 212 EXPECT_TRUE(found); |
| 213 EXPECT_EQ(value1, got_value); |
| 214 |
| 215 Get(key2, &got_value, &found); |
| 216 EXPECT_TRUE(found); |
| 217 EXPECT_EQ(value3, got_value); |
| 218 } |
| 219 |
| 220 // Test removals where the data is in the database, and the range has a closed |
| 221 // upper bound (inclusive). |
| 222 TEST_F(LevelDBTransactionTest, RemoveRangeBackingData) { |
| 223 std::string got_value; |
| 224 leveldb::Status status; |
| 225 |
| 226 Put(key_before_range_, value_); |
| 227 Put(range_start_, value_); |
| 228 Put(key_in_range1_, value_); |
| 229 Put(key_in_range2_, value_); |
| 230 Put(range_end_, value_); |
| 231 Put(key_after_range_, value_); |
| 232 |
| 233 scoped_refptr<LevelDBTransaction> transaction = new LevelDBTransaction(db()); |
| 234 |
| 235 const bool upper_open = false; |
| 236 status = transaction->RemoveRange(range_start_, range_end_, upper_open); |
| 237 EXPECT_TRUE(status.ok()); |
| 238 |
| 239 EXPECT_TRUE(TransactionHas(transaction.get(), key_before_range_)); |
| 240 EXPECT_FALSE(TransactionHas(transaction.get(), range_start_)); |
| 241 EXPECT_FALSE(TransactionHas(transaction.get(), key_in_range1_)); |
| 242 EXPECT_FALSE(TransactionHas(transaction.get(), key_in_range2_)); |
| 243 EXPECT_FALSE(TransactionHas(transaction.get(), range_end_)); |
| 244 EXPECT_TRUE(TransactionHas(transaction.get(), key_after_range_)); |
| 245 |
| 246 status = transaction->Commit(); |
| 247 EXPECT_TRUE(status.ok()); |
| 248 |
| 249 EXPECT_TRUE(Has(key_before_range_)); |
| 250 EXPECT_FALSE(Has(range_start_)); |
| 251 EXPECT_FALSE(Has(key_in_range1_)); |
| 252 EXPECT_FALSE(Has(key_in_range2_)); |
| 253 EXPECT_FALSE(Has(range_end_)); |
| 254 EXPECT_TRUE(Has(key_after_range_)); |
| 255 } |
| 256 |
| 257 // Test removals where the data is in the database, and the range has an open |
| 258 // upper bound (exclusive). |
| 259 TEST_F(LevelDBTransactionTest, RemoveRangeBackingDataUpperOpen) { |
| 260 std::string got_value; |
| 261 leveldb::Status status; |
| 262 |
| 263 Put(key_before_range_, value_); |
| 264 Put(range_start_, value_); |
| 265 Put(key_in_range1_, value_); |
| 266 Put(key_in_range2_, value_); |
| 267 Put(range_end_, value_); |
| 268 Put(key_after_range_, value_); |
| 269 |
| 270 scoped_refptr<LevelDBTransaction> transaction = new LevelDBTransaction(db()); |
| 271 |
| 272 const bool upper_open = true; |
| 273 status = transaction->RemoveRange(range_start_, range_end_, upper_open); |
| 274 EXPECT_TRUE(status.ok()); |
| 275 |
| 276 EXPECT_TRUE(TransactionHas(transaction.get(), key_before_range_)); |
| 277 EXPECT_FALSE(TransactionHas(transaction.get(), range_start_)); |
| 278 EXPECT_FALSE(TransactionHas(transaction.get(), key_in_range1_)); |
| 279 EXPECT_FALSE(TransactionHas(transaction.get(), key_in_range2_)); |
| 280 EXPECT_TRUE(TransactionHas(transaction.get(), range_end_)); |
| 281 EXPECT_TRUE(TransactionHas(transaction.get(), key_after_range_)); |
| 282 |
| 283 status = transaction->Commit(); |
| 284 EXPECT_TRUE(status.ok()); |
| 285 |
| 286 EXPECT_TRUE(Has(key_before_range_)); |
| 287 EXPECT_FALSE(Has(range_start_)); |
| 288 EXPECT_FALSE(Has(key_in_range1_)); |
| 289 EXPECT_FALSE(Has(key_in_range2_)); |
| 290 EXPECT_TRUE(Has(range_end_)); |
| 291 EXPECT_TRUE(Has(key_after_range_)); |
| 292 } |
| 293 |
| 294 // Test removals where the data is in transaction's memory map, and the range |
| 295 // has a closed upper bound (inclusive). |
| 296 TEST_F(LevelDBTransactionTest, RemoveRangeMemoryData) { |
| 297 std::string got_value; |
| 298 leveldb::Status status; |
| 299 |
| 300 scoped_refptr<LevelDBTransaction> transaction = new LevelDBTransaction(db()); |
| 301 |
| 302 TransactionPut(transaction.get(), key_before_range_, value_); |
| 303 TransactionPut(transaction.get(), range_start_, value_); |
| 304 TransactionPut(transaction.get(), key_in_range1_, value_); |
| 305 TransactionPut(transaction.get(), key_in_range2_, value_); |
| 306 TransactionPut(transaction.get(), range_end_, value_); |
| 307 TransactionPut(transaction.get(), key_after_range_, value_); |
| 308 |
| 309 const bool upper_open = false; |
| 310 status = transaction->RemoveRange(range_start_, range_end_, upper_open); |
| 311 EXPECT_TRUE(status.ok()); |
| 312 |
| 313 EXPECT_TRUE(TransactionHas(transaction.get(), key_before_range_)); |
| 314 EXPECT_FALSE(TransactionHas(transaction.get(), range_start_)); |
| 315 EXPECT_FALSE(TransactionHas(transaction.get(), key_in_range1_)); |
| 316 EXPECT_FALSE(TransactionHas(transaction.get(), key_in_range2_)); |
| 317 EXPECT_FALSE(TransactionHas(transaction.get(), range_end_)); |
| 318 EXPECT_TRUE(TransactionHas(transaction.get(), key_after_range_)); |
| 319 |
| 320 status = transaction->Commit(); |
| 321 EXPECT_TRUE(status.ok()); |
| 322 |
| 323 EXPECT_TRUE(Has(key_before_range_)); |
| 324 EXPECT_FALSE(Has(range_start_)); |
| 325 EXPECT_FALSE(Has(key_in_range1_)); |
| 326 EXPECT_FALSE(Has(key_in_range2_)); |
| 327 EXPECT_FALSE(Has(range_end_)); |
| 328 EXPECT_TRUE(Has(key_after_range_)); |
| 329 } |
| 330 |
| 331 // Test removals where the data is in transaction's memory map, and the range |
| 332 // has an open upper bound (exclusive). |
| 333 TEST_F(LevelDBTransactionTest, RemoveRangeMemoryDataUpperOpen) { |
| 334 std::string got_value; |
| 335 leveldb::Status status; |
| 336 |
| 337 scoped_refptr<LevelDBTransaction> transaction = new LevelDBTransaction(db()); |
| 338 |
| 339 TransactionPut(transaction.get(), key_before_range_, value_); |
| 340 TransactionPut(transaction.get(), range_start_, value_); |
| 341 TransactionPut(transaction.get(), key_in_range1_, value_); |
| 342 TransactionPut(transaction.get(), key_in_range2_, value_); |
| 343 TransactionPut(transaction.get(), range_end_, value_); |
| 344 TransactionPut(transaction.get(), key_after_range_, value_); |
| 345 |
| 346 const bool upper_open = true; |
| 347 status = transaction->RemoveRange(range_start_, range_end_, upper_open); |
| 348 EXPECT_TRUE(status.ok()); |
| 349 |
| 350 EXPECT_TRUE(TransactionHas(transaction.get(), key_before_range_)); |
| 351 EXPECT_FALSE(TransactionHas(transaction.get(), range_start_)); |
| 352 EXPECT_FALSE(TransactionHas(transaction.get(), key_in_range1_)); |
| 353 EXPECT_FALSE(TransactionHas(transaction.get(), key_in_range2_)); |
| 354 EXPECT_TRUE(TransactionHas(transaction.get(), range_end_)); |
| 355 EXPECT_TRUE(TransactionHas(transaction.get(), key_after_range_)); |
| 356 |
| 357 status = transaction->Commit(); |
| 358 EXPECT_TRUE(status.ok()); |
| 359 |
| 360 EXPECT_TRUE(Has(key_before_range_)); |
| 361 EXPECT_FALSE(Has(range_start_)); |
| 362 EXPECT_FALSE(Has(key_in_range1_)); |
| 363 EXPECT_FALSE(Has(key_in_range2_)); |
| 364 EXPECT_TRUE(Has(range_end_)); |
| 365 EXPECT_TRUE(Has(key_after_range_)); |
| 366 } |
| 367 |
| 368 } // namespace content |
OLD | NEW |