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

Side by Side Diff: content/browser/leveldb_wrapper_impl_unittest.cc

Issue 2588273005: Test most of the rest of the LevelDBWrapper public API. (Closed)
Patch Set: rebase, and try to fix windows compile Created 4 years 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 | « no previous file | 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/leveldb_wrapper_impl.h" 5 #include "content/browser/leveldb_wrapper_impl.h"
6 6
7 #include "base/debug/stack_trace.h" 7 #include "base/run_loop.h"
8
9 #include "components/leveldb/public/cpp/util.h" 8 #include "components/leveldb/public/cpp/util.h"
10 #include "content/public/test/test_browser_thread_bundle.h" 9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "mojo/public/cpp/bindings/associated_binding.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 using leveldb::StdStringToUint8Vector; 13 using leveldb::StdStringToUint8Vector;
14 using leveldb::Uint8VectorToStdString; 14 using leveldb::Uint8VectorToStdString;
15 15
16 namespace content { 16 namespace content {
17 17
18 namespace { 18 namespace {
19 19
20 const char* kTestPrefix = "abc"; 20 const char* kTestPrefix = "abc";
(...skipping 12 matching lines...) Expand all
33 return base::StringPiece(reinterpret_cast<const char*>(data.data()), 33 return base::StringPiece(reinterpret_cast<const char*>(data.data()),
34 data.size()); 34 data.size());
35 } 35 }
36 36
37 bool StartsWith(const std::vector<uint8_t>& key, 37 bool StartsWith(const std::vector<uint8_t>& key,
38 const std::vector<uint8_t>& prefix) { 38 const std::vector<uint8_t>& prefix) {
39 return base::StartsWith(AsStringPiece(key), AsStringPiece(prefix), 39 return base::StartsWith(AsStringPiece(key), AsStringPiece(prefix),
40 base::CompareCase::SENSITIVE); 40 base::CompareCase::SENSITIVE);
41 } 41 }
42 42
43 std::vector<uint8_t> successor(std::vector<uint8_t> data) {
44 for (unsigned i = data.size(); i > 0; --i) {
45 if (data[i - 1] < 255) {
46 data[i - 1]++;
47 return data;
48 }
49 }
50 NOTREACHED();
51 return data;
52 }
53
43 class MockLevelDBDatabase : public leveldb::mojom::LevelDBDatabase { 54 class MockLevelDBDatabase : public leveldb::mojom::LevelDBDatabase {
44 public: 55 public:
45 explicit MockLevelDBDatabase( 56 explicit MockLevelDBDatabase(
46 std::map<std::vector<uint8_t>, std::vector<uint8_t>>* mock_data) 57 std::map<std::vector<uint8_t>, std::vector<uint8_t>>* mock_data)
47 : mock_data_(*mock_data) {} 58 : mock_data_(*mock_data) {}
48 59
49 void Put(const std::vector<uint8_t>& key, 60 void Put(const std::vector<uint8_t>& key,
50 const std::vector<uint8_t>& value, 61 const std::vector<uint8_t>& value,
51 const PutCallback& callback) override { 62 const PutCallback& callback) override {
52 FAIL(); 63 FAIL();
(...skipping 13 matching lines...) Expand all
66 const WriteCallback& callback) override { 77 const WriteCallback& callback) override {
67 for (const auto& op : operations) { 78 for (const auto& op : operations) {
68 switch (op->type) { 79 switch (op->type) {
69 case leveldb::mojom::BatchOperationType::PUT_KEY: 80 case leveldb::mojom::BatchOperationType::PUT_KEY:
70 mock_data_[op->key] = *op->value; 81 mock_data_[op->key] = *op->value;
71 break; 82 break;
72 case leveldb::mojom::BatchOperationType::DELETE_KEY: 83 case leveldb::mojom::BatchOperationType::DELETE_KEY:
73 mock_data_.erase(op->key); 84 mock_data_.erase(op->key);
74 break; 85 break;
75 case leveldb::mojom::BatchOperationType::DELETE_PREFIXED_KEY: 86 case leveldb::mojom::BatchOperationType::DELETE_PREFIXED_KEY:
76 FAIL(); 87 mock_data_.erase(mock_data_.lower_bound(op->key),
88 mock_data_.lower_bound(successor(op->key)));
77 break; 89 break;
78 } 90 }
79 } 91 }
80 callback.Run(leveldb::mojom::DatabaseError::OK); 92 callback.Run(leveldb::mojom::DatabaseError::OK);
81 } 93 }
82 94
83 void Get(const std::vector<uint8_t>& key, 95 void Get(const std::vector<uint8_t>& key,
84 const GetCallback& callback) override { 96 const GetCallback& callback) override {
85 FAIL(); 97 FAIL();
86 } 98 }
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 const IteratorPrevCallback& callback) override { 158 const IteratorPrevCallback& callback) override {
147 FAIL(); 159 FAIL();
148 } 160 }
149 161
150 private: 162 private:
151 std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data_; 163 std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data_;
152 }; 164 };
153 165
154 void NoOp() {} 166 void NoOp() {}
155 167
156 void GetCallback(bool* called, 168 void GetCallback(const base::Closure& callback,
157 bool* success_out, 169 bool* success_out,
158 std::vector<uint8_t>* value_out, 170 std::vector<uint8_t>* value_out,
159 bool success, 171 bool success,
160 const std::vector<uint8_t>& value) { 172 const std::vector<uint8_t>& value) {
161 *called = true;
162 *success_out = success; 173 *success_out = success;
163 *value_out = value; 174 *value_out = value;
175 callback.Run();
164 } 176 }
165 177
166 void GetAllCallback(bool* called, 178 void SuccessCallback(const base::Closure& callback,
167 leveldb::mojom::DatabaseError* status_out, 179 bool* success_out,
168 std::vector<mojom::KeyValuePtr>* data_out, 180 bool success) {
169 leveldb::mojom::DatabaseError status, 181 *success_out = success;
170 std::vector<mojom::KeyValuePtr> data) { 182 callback.Run();
171 *called = true;
172 *status_out = status;
173 *data_out = std::move(data);
174 } 183 }
175 184
176 void SuccessCallback(bool* called, bool* success_out, bool success) {
177 *called = true;
178 *success_out = success;
179 }
180
181 void NoOpSuccess(bool success) {}
182
183 } // namespace 185 } // namespace
184 186
185 class LevelDBWrapperImplTest : public testing::Test { 187 class LevelDBWrapperImplTest : public testing::Test,
188 public mojom::LevelDBObserver {
186 public: 189 public:
190 struct Observation {
191 enum { kAdd, kChange, kDelete, kDeleteAll } type;
192 std::string key;
193 std::string old_value;
194 std::string new_value;
195 std::string source;
196 };
197
187 LevelDBWrapperImplTest() 198 LevelDBWrapperImplTest()
188 : db_(&mock_data_), 199 : db_(&mock_data_),
189 level_db_wrapper_(&db_, 200 level_db_wrapper_(&db_,
190 kTestPrefix, 201 kTestPrefix,
191 kTestSizeLimit, 202 kTestSizeLimit,
192 base::TimeDelta::FromSeconds(5), 203 base::TimeDelta::FromSeconds(5),
193 10 * 1024 * 1024 /* max_bytes_per_hour */, 204 10 * 1024 * 1024 /* max_bytes_per_hour */,
194 60 /* max_commits_per_hour */, 205 60 /* max_commits_per_hour */,
195 base::Bind(&NoOp)) { 206 base::Bind(&NoOp)),
207 observer_binding_(this) {
196 set_mock_data(std::string(kTestPrefix) + "def", "defdata"); 208 set_mock_data(std::string(kTestPrefix) + "def", "defdata");
197 set_mock_data(std::string(kTestPrefix) + "123", "123data"); 209 set_mock_data(std::string(kTestPrefix) + "123", "123data");
198 set_mock_data("123", "baddata"); 210 set_mock_data("123", "baddata");
211
212 level_db_wrapper_.Bind(mojo::MakeRequest(&level_db_wrapper_ptr_));
213 mojom::LevelDBObserverAssociatedPtrInfo ptr_info;
214 observer_binding_.Bind(&ptr_info, level_db_wrapper_ptr_.associated_group());
215 level_db_wrapper_ptr_->AddObserver(std::move(ptr_info));
199 } 216 }
200 217
201 void set_mock_data(const std::string& key, const std::string& value) { 218 void set_mock_data(const std::string& key, const std::string& value) {
202 mock_data_[StdStringToUint8Vector(key)] = StdStringToUint8Vector(value); 219 mock_data_[StdStringToUint8Vector(key)] = StdStringToUint8Vector(value);
203 } 220 }
204 221
222 void set_mock_data(const std::vector<uint8_t>& key,
223 const std::vector<uint8_t>& value) {
224 mock_data_[key] = value;
225 }
226
205 bool has_mock_data(const std::string& key) { 227 bool has_mock_data(const std::string& key) {
206 return mock_data_.find(StdStringToUint8Vector(key)) != mock_data_.end(); 228 return mock_data_.find(StdStringToUint8Vector(key)) != mock_data_.end();
207 } 229 }
208 230
209 std::string get_mock_data(const std::string& key) { 231 std::string get_mock_data(const std::string& key) {
210 return has_mock_data(key) 232 return has_mock_data(key)
211 ? Uint8VectorToStdString(mock_data_[StdStringToUint8Vector(key)]) 233 ? Uint8VectorToStdString(mock_data_[StdStringToUint8Vector(key)])
212 : ""; 234 : "";
213 } 235 }
214 236
215 mojom::LevelDBWrapper* wrapper() { return &level_db_wrapper_; } 237 mojom::LevelDBWrapper* wrapper() { return level_db_wrapper_ptr_.get(); }
238
239 bool GetSync(const std::vector<uint8_t>& key, std::vector<uint8_t>* result) {
240 base::RunLoop run_loop;
241 bool success = false;
242 wrapper()->Get(key, base::Bind(&GetCallback, run_loop.QuitClosure(),
243 &success, result));
244 run_loop.Run();
245 return success;
246 }
247
248 bool PutSync(const std::vector<uint8_t>& key,
249 const std::vector<uint8_t>& value,
250 std::string source = kTestSource) {
251 base::RunLoop run_loop;
252 bool success = false;
253 wrapper()->Put(
254 key, value, source,
255 base::Bind(&SuccessCallback, run_loop.QuitClosure(), &success));
256 run_loop.Run();
257 return success;
258 }
259
260 bool DeleteSync(const std::vector<uint8_t>& key) {
261 base::RunLoop run_loop;
262 bool success = false;
263 wrapper()->Delete(
264 key, kTestSource,
265 base::Bind(&SuccessCallback, run_loop.QuitClosure(), &success));
266 run_loop.Run();
267 return success;
268 }
269
270 bool DeleteAllSync() {
271 base::RunLoop run_loop;
272 bool success = false;
273 wrapper()->DeleteAll(
274 kTestSource,
275 base::Bind(&SuccessCallback, run_loop.QuitClosure(), &success));
276 run_loop.Run();
277 return success;
278 }
216 279
217 void CommitChanges() { 280 void CommitChanges() {
218 ASSERT_TRUE(level_db_wrapper_.commit_batch_); 281 ASSERT_TRUE(level_db_wrapper_.commit_batch_);
219 level_db_wrapper_.CommitChanges(); 282 level_db_wrapper_.CommitChanges();
220 } 283 }
221 284
285 const std::vector<Observation>& observations() { return observations_; }
286
222 private: 287 private:
288 // LevelDBObserver:
289 void KeyAdded(const std::vector<uint8_t>& key,
290 const std::vector<uint8_t>& value,
291 const std::string& source) override {
292 observations_.push_back({Observation::kAdd, Uint8VectorToStdString(key), "",
293 Uint8VectorToStdString(value), source});
294 }
295 void KeyChanged(const std::vector<uint8_t>& key,
296 const std::vector<uint8_t>& new_value,
297 const std::vector<uint8_t>& old_value,
298 const std::string& source) override {
299 observations_.push_back({Observation::kChange, Uint8VectorToStdString(key),
300 Uint8VectorToStdString(old_value),
301 Uint8VectorToStdString(new_value), source});
302 }
303 void KeyDeleted(const std::vector<uint8_t>& key,
304 const std::vector<uint8_t>& old_value,
305 const std::string& source) override {
306 observations_.push_back({Observation::kDelete, Uint8VectorToStdString(key),
307 Uint8VectorToStdString(old_value), "", source});
308 }
309 void AllDeleted(const std::string& source) override {
310 observations_.push_back({Observation::kDeleteAll, "", "", "", source});
311 }
312 void GetAllComplete(const std::string& source) override {}
313
223 TestBrowserThreadBundle thread_bundle_; 314 TestBrowserThreadBundle thread_bundle_;
224 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_; 315 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_;
225 MockLevelDBDatabase db_; 316 MockLevelDBDatabase db_;
226 LevelDBWrapperImpl level_db_wrapper_; 317 LevelDBWrapperImpl level_db_wrapper_;
318 mojom::LevelDBWrapperPtr level_db_wrapper_ptr_;
319 mojo::AssociatedBinding<mojom::LevelDBObserver> observer_binding_;
320 std::vector<Observation> observations_;
227 }; 321 };
228 322
229 TEST_F(LevelDBWrapperImplTest, GetLoadedFromMap) { 323 TEST_F(LevelDBWrapperImplTest, GetLoadedFromMap) {
230 bool called = false;
231 bool success;
232 std::vector<uint8_t> result; 324 std::vector<uint8_t> result;
233 wrapper()->Get(StdStringToUint8Vector("123"), 325 EXPECT_TRUE(GetSync(StdStringToUint8Vector("123"), &result));
234 base::Bind(&GetCallback, &called, &success, &result));
235 ASSERT_TRUE(called);
236 EXPECT_TRUE(success);
237 EXPECT_EQ(StdStringToUint8Vector("123data"), result); 326 EXPECT_EQ(StdStringToUint8Vector("123data"), result);
238 327
239 called = false; 328 EXPECT_FALSE(GetSync(StdStringToUint8Vector("x"), &result));
240 wrapper()->Get(StdStringToUint8Vector("x"),
241 base::Bind(&GetCallback, &called, &success, &result));
242 ASSERT_TRUE(called);
243 EXPECT_FALSE(success);
244 } 329 }
245 330
246 TEST_F(LevelDBWrapperImplTest, GetFromPutOverwrite) { 331 TEST_F(LevelDBWrapperImplTest, GetFromPutOverwrite) {
247 std::vector<uint8_t> key = StdStringToUint8Vector("123"); 332 std::vector<uint8_t> key = StdStringToUint8Vector("123");
248 std::vector<uint8_t> value = StdStringToUint8Vector("foo"); 333 std::vector<uint8_t> value = StdStringToUint8Vector("foo");
249 334
250 bool called = false; 335 EXPECT_TRUE(PutSync(key, value));
251 bool success;
252 wrapper()->Put(key, value, kTestSource,
253 base::Bind(&SuccessCallback, &called, &success));
254 ASSERT_TRUE(called);
255 EXPECT_TRUE(success);
256 336
257 called = false;
258 std::vector<uint8_t> result; 337 std::vector<uint8_t> result;
259 wrapper()->Get(key, base::Bind(&GetCallback, &called, &success, &result)); 338 EXPECT_TRUE(GetSync(key, &result));
260 ASSERT_TRUE(called);
261 EXPECT_TRUE(success);
262 EXPECT_EQ(value, result); 339 EXPECT_EQ(value, result);
263 } 340 }
264 341
265 TEST_F(LevelDBWrapperImplTest, GetFromPutNewKey) { 342 TEST_F(LevelDBWrapperImplTest, GetFromPutNewKey) {
266 std::vector<uint8_t> key = StdStringToUint8Vector("newkey"); 343 std::vector<uint8_t> key = StdStringToUint8Vector("newkey");
267 std::vector<uint8_t> value = StdStringToUint8Vector("foo"); 344 std::vector<uint8_t> value = StdStringToUint8Vector("foo");
268 345
269 bool called = false; 346 EXPECT_TRUE(PutSync(key, value));
270 bool success;
271 wrapper()->Put(key, value, kTestSource,
272 base::Bind(&SuccessCallback, &called, &success));
273 ASSERT_TRUE(called);
274 EXPECT_TRUE(success);
275 347
276 called = false;
277 std::vector<uint8_t> result; 348 std::vector<uint8_t> result;
278 wrapper()->Get(key, base::Bind(&GetCallback, &called, &success, &result)); 349 EXPECT_TRUE(GetSync(key, &result));
279 ASSERT_TRUE(called);
280 EXPECT_TRUE(success);
281 EXPECT_EQ(value, result); 350 EXPECT_EQ(value, result);
282 } 351 }
283 352
284 TEST_F(LevelDBWrapperImplTest, GetAll) { 353 TEST_F(LevelDBWrapperImplTest, GetAll) {
285 bool called = false;
286 leveldb::mojom::DatabaseError status; 354 leveldb::mojom::DatabaseError status;
287 std::vector<mojom::KeyValuePtr> data; 355 std::vector<mojom::KeyValuePtr> data;
288 wrapper()->GetAll(kTestSource, 356 EXPECT_TRUE(wrapper()->GetAll(kTestSource, &status, &data));
289 base::Bind(&GetAllCallback, &called, &status, &data));
290 ASSERT_TRUE(called);
291 EXPECT_EQ(leveldb::mojom::DatabaseError::OK, status); 357 EXPECT_EQ(leveldb::mojom::DatabaseError::OK, status);
292 EXPECT_EQ(2u, data.size()); 358 EXPECT_EQ(2u, data.size());
293 } 359 }
294 360
295 TEST_F(LevelDBWrapperImplTest, CommitPutToDB) { 361 TEST_F(LevelDBWrapperImplTest, CommitPutToDB) {
296 std::string key1 = "123"; 362 std::string key1 = "123";
297 std::string value1 = "foo"; 363 std::string value1 = "foo";
298 std::string key2 = "abc"; 364 std::string key2 = "abc";
299 std::string value2 = "data abc"; 365 std::string value2 = "data abc";
300 366
301 wrapper()->Put(StdStringToUint8Vector(key1), StdStringToUint8Vector(value1), 367 EXPECT_TRUE(
302 kTestSource, base::Bind(&NoOpSuccess)); 368 PutSync(StdStringToUint8Vector(key1), StdStringToUint8Vector(value1)));
303 wrapper()->Put(StdStringToUint8Vector(key2), 369 EXPECT_TRUE(PutSync(StdStringToUint8Vector(key2),
304 StdStringToUint8Vector("old value"), kTestSource, 370 StdStringToUint8Vector("old value")));
305 base::Bind(&NoOpSuccess)); 371 EXPECT_TRUE(
306 wrapper()->Put(StdStringToUint8Vector(key2), StdStringToUint8Vector(value2), 372 PutSync(StdStringToUint8Vector(key2), StdStringToUint8Vector(value2)));
307 kTestSource, base::Bind(&NoOpSuccess)); 373
374 EXPECT_FALSE(has_mock_data(kTestPrefix + key2));
308 375
309 CommitChanges(); 376 CommitChanges();
310
311 EXPECT_TRUE(has_mock_data(kTestPrefix + key1)); 377 EXPECT_TRUE(has_mock_data(kTestPrefix + key1));
312 EXPECT_EQ(value1, get_mock_data(kTestPrefix + key1)); 378 EXPECT_EQ(value1, get_mock_data(kTestPrefix + key1));
313 EXPECT_TRUE(has_mock_data(kTestPrefix + key2)); 379 EXPECT_TRUE(has_mock_data(kTestPrefix + key2));
314 EXPECT_EQ(value2, get_mock_data(kTestPrefix + key2)); 380 EXPECT_EQ(value2, get_mock_data(kTestPrefix + key2));
315 } 381 }
316 382
383 TEST_F(LevelDBWrapperImplTest, PutObservations) {
384 std::string key = "new_key";
385 std::string value1 = "foo";
386 std::string value2 = "data abc";
387 std::string source1 = "source1";
388 std::string source2 = "source2";
389
390 EXPECT_TRUE(PutSync(StdStringToUint8Vector(key),
391 StdStringToUint8Vector(value1), source1));
392 ASSERT_EQ(1u, observations().size());
393 EXPECT_EQ(Observation::kAdd, observations()[0].type);
394 EXPECT_EQ(key, observations()[0].key);
395 EXPECT_EQ(value1, observations()[0].new_value);
396 EXPECT_EQ(source1, observations()[0].source);
397
398 EXPECT_TRUE(PutSync(StdStringToUint8Vector(key),
399 StdStringToUint8Vector(value2), source2));
400 ASSERT_EQ(2u, observations().size());
401 EXPECT_EQ(Observation::kChange, observations()[1].type);
402 EXPECT_EQ(key, observations()[1].key);
403 EXPECT_EQ(value1, observations()[1].old_value);
404 EXPECT_EQ(value2, observations()[1].new_value);
405 EXPECT_EQ(source2, observations()[1].source);
406 }
407
408 TEST_F(LevelDBWrapperImplTest, DeleteNonExistingKey) {
409 EXPECT_TRUE(DeleteSync(StdStringToUint8Vector("doesn't exist")));
410 EXPECT_EQ(0u, observations().size());
411 }
412
413 TEST_F(LevelDBWrapperImplTest, DeleteExistingKey) {
414 std::string key = "newkey";
415 std::string value = "foo";
416 set_mock_data(kTestPrefix + key, value);
417
418 EXPECT_TRUE(DeleteSync(StdStringToUint8Vector(key)));
419 ASSERT_EQ(1u, observations().size());
420 EXPECT_EQ(Observation::kDelete, observations()[0].type);
421 EXPECT_EQ(key, observations()[0].key);
422 EXPECT_EQ(value, observations()[0].old_value);
423 EXPECT_EQ(kTestSource, observations()[0].source);
424
425 EXPECT_TRUE(has_mock_data(kTestPrefix + key));
426
427 CommitChanges();
428 EXPECT_FALSE(has_mock_data(kTestPrefix + key));
429 }
430
431 TEST_F(LevelDBWrapperImplTest, DeleteAll) {
432 std::string key = "newkey";
433 std::string value = "foo";
434 std::string dummy_key = "foobar";
435 set_mock_data(dummy_key, value);
436 set_mock_data(kTestPrefix + key, value);
437
438 EXPECT_TRUE(DeleteAllSync());
439 ASSERT_EQ(1u, observations().size());
440 EXPECT_EQ(Observation::kDeleteAll, observations()[0].type);
441 EXPECT_EQ(kTestSource, observations()[0].source);
442
443 EXPECT_TRUE(has_mock_data(kTestPrefix + key));
444 EXPECT_TRUE(has_mock_data(dummy_key));
445
446 CommitChanges();
447 EXPECT_FALSE(has_mock_data(kTestPrefix + key));
448 EXPECT_TRUE(has_mock_data(dummy_key));
449
450 // Deleting all again should still work, an cause an observation.
451 EXPECT_TRUE(DeleteAllSync());
452 ASSERT_EQ(2u, observations().size());
453 EXPECT_EQ(Observation::kDeleteAll, observations()[1].type);
454 EXPECT_EQ(kTestSource, observations()[1].source);
455
456 // And now we've deleted all, writing something the quota size should work.
457 EXPECT_TRUE(PutSync(std::vector<uint8_t>(kTestSizeLimit, 'b'),
458 std::vector<uint8_t>()));
459 }
460
317 TEST_F(LevelDBWrapperImplTest, PutOverQuotaLargeValue) { 461 TEST_F(LevelDBWrapperImplTest, PutOverQuotaLargeValue) {
318 std::vector<uint8_t> key = StdStringToUint8Vector("newkey"); 462 std::vector<uint8_t> key = StdStringToUint8Vector("newkey");
319 std::vector<uint8_t> value(kTestSizeLimit, 4); 463 std::vector<uint8_t> value(kTestSizeLimit, 4);
320 464
321 bool called = false; 465 EXPECT_FALSE(PutSync(key, value));
322 bool success;
323 wrapper()->Put(key, value, kTestSource,
324 base::Bind(&SuccessCallback, &called, &success));
325 ASSERT_TRUE(called);
326 EXPECT_FALSE(success);
327 466
328 value.resize(kTestSizeLimit / 2); 467 value.resize(kTestSizeLimit / 2);
329 called = false; 468 EXPECT_TRUE(PutSync(key, value));
330 wrapper()->Put(key, value, kTestSource,
331 base::Bind(&SuccessCallback, &called, &success));
332 ASSERT_TRUE(called);
333 EXPECT_TRUE(success);
334 } 469 }
335 470
336 TEST_F(LevelDBWrapperImplTest, PutOverQuotaLargeKey) { 471 TEST_F(LevelDBWrapperImplTest, PutOverQuotaLargeKey) {
337 std::vector<uint8_t> key(kTestSizeLimit, 'a'); 472 std::vector<uint8_t> key(kTestSizeLimit, 'a');
338 std::vector<uint8_t> value = StdStringToUint8Vector("newvalue"); 473 std::vector<uint8_t> value = StdStringToUint8Vector("newvalue");
339 474
340 bool called = false; 475 EXPECT_FALSE(PutSync(key, value));
341 bool success;
342 wrapper()->Put(key, value, kTestSource,
343 base::Bind(&SuccessCallback, &called, &success));
344 ASSERT_TRUE(called);
345 EXPECT_FALSE(success);
346 476
347 key.resize(kTestSizeLimit / 2); 477 key.resize(kTestSizeLimit / 2);
348 called = false; 478 EXPECT_TRUE(PutSync(key, value));
349 wrapper()->Put(key, value, kTestSource,
350 base::Bind(&SuccessCallback, &called, &success));
351 ASSERT_TRUE(called);
352 EXPECT_TRUE(success);
353 } 479 }
354 480
355 TEST_F(LevelDBWrapperImplTest, PutWhenAlreadyOverQuota) { 481 TEST_F(LevelDBWrapperImplTest, PutWhenAlreadyOverQuota) {
356 std::string key = "largedata"; 482 std::string key = "largedata";
357 std::vector<uint8_t> value(kTestSizeLimit, 4); 483 std::vector<uint8_t> value(kTestSizeLimit, 4);
358 484
359 set_mock_data(kTestPrefix + key, Uint8VectorToStdString(value)); 485 set_mock_data(kTestPrefix + key, Uint8VectorToStdString(value));
360 486
361 // Put with same data should succeed. 487 // Put with same data should succeed.
362 bool called = false; 488 EXPECT_TRUE(PutSync(StdStringToUint8Vector(key), value));
363 bool success;
364 wrapper()->Put(StdStringToUint8Vector(key), value, kTestSource,
365 base::Bind(&SuccessCallback, &called, &success));
366 ASSERT_TRUE(called);
367 EXPECT_TRUE(success);
368 489
369 // Put with same data size should succeed. 490 // Put with same data size should succeed.
370 called = false;
371 value[1] = 13; 491 value[1] = 13;
372 wrapper()->Put(StdStringToUint8Vector(key), value, kTestSource, 492 EXPECT_TRUE(PutSync(StdStringToUint8Vector(key), value));
373 base::Bind(&SuccessCallback, &called, &success));
374 ASSERT_TRUE(called);
375 EXPECT_TRUE(success);
376 493
377 // Adding a new key when already over quota should not succeed. 494 // Adding a new key when already over quota should not succeed.
378 called = false; 495 EXPECT_FALSE(PutSync(StdStringToUint8Vector("newkey"), {1, 2, 3}));
379 wrapper()->Put(StdStringToUint8Vector("newkey"), {1, 2, 3}, kTestSource,
380 base::Bind(&SuccessCallback, &called, &success));
381 ASSERT_TRUE(called);
382 EXPECT_FALSE(success);
383 496
384 // Reducing size should also succeed. 497 // Reducing size should also succeed.
385 value.resize(kTestSizeLimit / 2); 498 value.resize(kTestSizeLimit / 2);
386 called = false; 499 EXPECT_TRUE(PutSync(StdStringToUint8Vector(key), value));
387 wrapper()->Put(StdStringToUint8Vector(key), value, kTestSource,
388 base::Bind(&SuccessCallback, &called, &success));
389 ASSERT_TRUE(called);
390 EXPECT_TRUE(success);
391 500
392 // Increasing size again should succeed, as still under the limit. 501 // Increasing size again should succeed, as still under the limit.
393 value.resize(value.size() + 1); 502 value.resize(value.size() + 1);
394 called = false; 503 EXPECT_TRUE(PutSync(StdStringToUint8Vector(key), value));
395 wrapper()->Put(StdStringToUint8Vector(key), value, kTestSource,
396 base::Bind(&SuccessCallback, &called, &success));
397 ASSERT_TRUE(called);
398 EXPECT_TRUE(success);
399 504
400 // But increasing back to original size should fail. 505 // But increasing back to original size should fail.
401 value.resize(kTestSizeLimit); 506 value.resize(kTestSizeLimit);
402 called = false; 507 EXPECT_FALSE(PutSync(StdStringToUint8Vector(key), value));
403 wrapper()->Put(StdStringToUint8Vector(key), value, kTestSource,
404 base::Bind(&SuccessCallback, &called, &success));
405 ASSERT_TRUE(called);
406 EXPECT_FALSE(success);
407 } 508 }
408 509
409 TEST_F(LevelDBWrapperImplTest, PutWhenAlreadyOverQuotaBecauseOfLargeKey) { 510 TEST_F(LevelDBWrapperImplTest, PutWhenAlreadyOverQuotaBecauseOfLargeKey) {
410 std::vector<uint8_t> key(kTestSizeLimit, 'x'); 511 std::vector<uint8_t> key(kTestSizeLimit, 'x');
411 std::vector<uint8_t> value = StdStringToUint8Vector("value"); 512 std::vector<uint8_t> value = StdStringToUint8Vector("value");
412 513
413 set_mock_data(kTestPrefix + Uint8VectorToStdString(key), 514 set_mock_data(kTestPrefix + Uint8VectorToStdString(key),
414 Uint8VectorToStdString(value)); 515 Uint8VectorToStdString(value));
415 516
416 // Put with same data size should succeed. 517 // Put with same data size should succeed.
417 bool called = false;
418 bool success;
419 value[0] = 'X'; 518 value[0] = 'X';
420 wrapper()->Put(key, value, kTestSource, 519 EXPECT_TRUE(PutSync(key, value));
421 base::Bind(&SuccessCallback, &called, &success));
422 ASSERT_TRUE(called);
423 EXPECT_TRUE(success);
424 520
425 // Reducing size should also succeed. 521 // Reducing size should also succeed.
426 value.clear(); 522 value.clear();
427 called = false; 523 EXPECT_TRUE(PutSync(key, value));
428 wrapper()->Put(key, value, kTestSource,
429 base::Bind(&SuccessCallback, &called, &success));
430 ASSERT_TRUE(called);
431 EXPECT_TRUE(success);
432 524
433 // Increasing size should fail. 525 // Increasing size should fail.
434 value.resize(1, 'a'); 526 value.resize(1, 'a');
435 called = false; 527 EXPECT_FALSE(PutSync(key, value));
436 wrapper()->Put(key, value, kTestSource,
437 base::Bind(&SuccessCallback, &called, &success));
438 ASSERT_TRUE(called);
439 EXPECT_FALSE(success);
440 } 528 }
441 529
442 } // namespace content 530 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698