Update path of checkdeps to buildtools checkout
[chromium-blink-merge.git] / extensions / browser / api / storage / settings_quota_unittest.cc
blob2865d66b0fddfe2c0d5156fcd7206eec926719de
1 // Copyright 2014 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.
5 #include "base/json/json_writer.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "extensions/browser/api/storage/settings_storage_quota_enforcer.h"
10 #include "extensions/browser/value_store/testing_value_store.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using base::DictionaryValue;
14 using base::ListValue;
15 using base::Value;
17 namespace extensions {
19 // To save typing ValueStore::DEFAULTS/IGNORE_QUOTA everywhere.
20 const ValueStore::WriteOptions DEFAULTS = ValueStore::DEFAULTS;
21 const ValueStore::WriteOptions IGNORE_QUOTA =
22 ValueStore::IGNORE_QUOTA;
24 class ExtensionSettingsQuotaTest : public testing::Test {
25 public:
26 ExtensionSettingsQuotaTest()
27 : byte_value_1_(new base::FundamentalValue(1)),
28 byte_value_16_(new base::StringValue("sixteen bytes.")),
29 byte_value_256_(new base::ListValue()),
30 delegate_(new TestingValueStore()) {
31 for (int i = 1; i < 89; ++i) {
32 byte_value_256_->Append(new base::FundamentalValue(i));
34 ValidateByteValues();
37 void ValidateByteValues() {
38 std::string validate_sizes;
39 base::JSONWriter::Write(byte_value_1_.get(), &validate_sizes);
40 ASSERT_EQ(1u, validate_sizes.size());
41 base::JSONWriter::Write(byte_value_16_.get(), &validate_sizes);
42 ASSERT_EQ(16u, validate_sizes.size());
43 base::JSONWriter::Write(byte_value_256_.get(), &validate_sizes);
44 ASSERT_EQ(256u, validate_sizes.size());
47 virtual void TearDown() OVERRIDE {
48 ASSERT_TRUE(storage_.get() != NULL);
51 protected:
52 // Creates |storage_|. Must only be called once.
53 void CreateStorage(
54 size_t quota_bytes, size_t quota_bytes_per_item, size_t max_items) {
55 ASSERT_TRUE(storage_.get() == NULL);
56 SettingsStorageQuotaEnforcer::Limits limits =
57 { quota_bytes, quota_bytes_per_item, max_items };
58 storage_.reset(new SettingsStorageQuotaEnforcer(limits, delegate_));
61 // Returns whether the settings in |storage_| and |delegate_| are the same as
62 // |settings|.
63 bool SettingsEqual(const base::DictionaryValue& settings) {
64 return settings.Equals(&storage_->Get()->settings()) &&
65 settings.Equals(&delegate_->Get()->settings());
68 // Values with different serialized sizes.
69 scoped_ptr<base::Value> byte_value_1_;
70 scoped_ptr<base::Value> byte_value_16_;
71 scoped_ptr<base::ListValue> byte_value_256_;
73 // Quota enforcing storage area being tested.
74 scoped_ptr<SettingsStorageQuotaEnforcer> storage_;
76 // In-memory storage area being delegated to. Always owned by |storage_|.
77 TestingValueStore* delegate_;
80 TEST_F(ExtensionSettingsQuotaTest, ZeroQuotaBytes) {
81 base::DictionaryValue empty;
82 CreateStorage(0, UINT_MAX, UINT_MAX);
84 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
85 EXPECT_FALSE(storage_->Remove("a")->HasError());
86 EXPECT_FALSE(storage_->Remove("b")->HasError());
87 EXPECT_TRUE(SettingsEqual(empty));
90 TEST_F(ExtensionSettingsQuotaTest, KeySizeTakenIntoAccount) {
91 base::DictionaryValue empty;
92 CreateStorage(8u, UINT_MAX, UINT_MAX);
93 EXPECT_TRUE(
94 storage_->Set(DEFAULTS, "Really long key", *byte_value_1_)->HasError());
95 EXPECT_TRUE(SettingsEqual(empty));
98 TEST_F(ExtensionSettingsQuotaTest, SmallByteQuota) {
99 base::DictionaryValue settings;
100 CreateStorage(8u, UINT_MAX, UINT_MAX);
102 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
103 settings.Set("a", byte_value_1_->DeepCopy());
104 EXPECT_TRUE(SettingsEqual(settings));
106 EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError());
107 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
108 EXPECT_TRUE(SettingsEqual(settings));
111 TEST_F(ExtensionSettingsQuotaTest, MediumByteQuota) {
112 base::DictionaryValue settings;
113 CreateStorage(40, UINT_MAX, UINT_MAX);
115 base::DictionaryValue to_set;
116 to_set.Set("a", byte_value_1_->DeepCopy());
117 to_set.Set("b", byte_value_16_->DeepCopy());
118 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
119 settings.Set("a", byte_value_1_->DeepCopy());
120 settings.Set("b", byte_value_16_->DeepCopy());
121 EXPECT_TRUE(SettingsEqual(settings));
123 // Should be able to set value to other under-quota value.
124 to_set.Set("a", byte_value_16_->DeepCopy());
125 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
126 settings.Set("a", byte_value_16_->DeepCopy());
127 EXPECT_TRUE(SettingsEqual(settings));
129 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
130 EXPECT_TRUE(SettingsEqual(settings));
133 TEST_F(ExtensionSettingsQuotaTest, ZeroMaxKeys) {
134 base::DictionaryValue empty;
135 CreateStorage(UINT_MAX, UINT_MAX, 0);
137 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
138 EXPECT_FALSE(storage_->Remove("a")->HasError());
139 EXPECT_FALSE(storage_->Remove("b")->HasError());
140 EXPECT_TRUE(SettingsEqual(empty));
143 TEST_F(ExtensionSettingsQuotaTest, SmallMaxKeys) {
144 base::DictionaryValue settings;
145 CreateStorage(UINT_MAX, UINT_MAX, 1);
147 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
148 settings.Set("a", byte_value_1_->DeepCopy());
149 EXPECT_TRUE(SettingsEqual(settings));
151 // Should be able to set existing key to other value without going over quota.
152 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_16_)->HasError());
153 settings.Set("a", byte_value_16_->DeepCopy());
154 EXPECT_TRUE(SettingsEqual(settings));
156 EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError());
157 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
158 EXPECT_TRUE(SettingsEqual(settings));
161 TEST_F(ExtensionSettingsQuotaTest, MediumMaxKeys) {
162 base::DictionaryValue settings;
163 CreateStorage(UINT_MAX, UINT_MAX, 2);
165 base::DictionaryValue to_set;
166 to_set.Set("a", byte_value_1_->DeepCopy());
167 to_set.Set("b", byte_value_16_->DeepCopy());
168 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
169 settings.Set("a", byte_value_1_->DeepCopy());
170 settings.Set("b", byte_value_16_->DeepCopy());
171 EXPECT_TRUE(SettingsEqual(settings));
173 // Should be able to set existing keys to other values without going over
174 // quota.
175 to_set.Set("a", byte_value_16_->DeepCopy());
176 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
177 settings.Set("a", byte_value_16_->DeepCopy());
178 EXPECT_TRUE(SettingsEqual(settings));
180 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
181 EXPECT_TRUE(SettingsEqual(settings));
184 TEST_F(ExtensionSettingsQuotaTest, RemovingExistingSettings) {
185 base::DictionaryValue settings;
186 CreateStorage(266, UINT_MAX, 2);
188 storage_->Set(DEFAULTS, "b", *byte_value_16_);
189 settings.Set("b", byte_value_16_->DeepCopy());
190 // Not enough quota.
191 storage_->Set(DEFAULTS, "c", *byte_value_256_);
192 EXPECT_TRUE(SettingsEqual(settings));
194 // Try again with "b" removed, enough quota.
195 EXPECT_FALSE(storage_->Remove("b")->HasError());
196 settings.Remove("b", NULL);
197 EXPECT_FALSE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
198 settings.Set("c", byte_value_256_->DeepCopy());
199 EXPECT_TRUE(SettingsEqual(settings));
201 // Enough byte quota but max keys not high enough.
202 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
203 settings.Set("a", byte_value_1_->DeepCopy());
204 EXPECT_TRUE(SettingsEqual(settings));
206 EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError());
207 EXPECT_TRUE(SettingsEqual(settings));
209 // Back under max keys.
210 EXPECT_FALSE(storage_->Remove("a")->HasError());
211 settings.Remove("a", NULL);
212 EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError());
213 settings.Set("b", byte_value_1_->DeepCopy());
214 EXPECT_TRUE(SettingsEqual(settings));
217 TEST_F(ExtensionSettingsQuotaTest, RemovingNonexistentSettings) {
218 base::DictionaryValue settings;
219 CreateStorage(36, UINT_MAX, 3);
221 // Max out bytes.
222 base::DictionaryValue to_set;
223 to_set.Set("b1", byte_value_16_->DeepCopy());
224 to_set.Set("b2", byte_value_16_->DeepCopy());
225 storage_->Set(DEFAULTS, to_set);
226 settings.Set("b1", byte_value_16_->DeepCopy());
227 settings.Set("b2", byte_value_16_->DeepCopy());
228 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
229 EXPECT_TRUE(SettingsEqual(settings));
231 // Remove some settings that don't exist.
232 std::vector<std::string> to_remove;
233 to_remove.push_back("a1");
234 to_remove.push_back("a2");
235 EXPECT_FALSE(storage_->Remove(to_remove)->HasError());
236 EXPECT_FALSE(storage_->Remove("b")->HasError());
237 EXPECT_TRUE(SettingsEqual(settings));
239 // Still no quota.
240 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
241 EXPECT_TRUE(SettingsEqual(settings));
243 // Max out key count.
244 to_set.Clear();
245 to_set.Set("b1", byte_value_1_->DeepCopy());
246 to_set.Set("b2", byte_value_1_->DeepCopy());
247 storage_->Set(DEFAULTS, to_set);
248 settings.Set("b1", byte_value_1_->DeepCopy());
249 settings.Set("b2", byte_value_1_->DeepCopy());
250 storage_->Set(DEFAULTS, "b3", *byte_value_1_);
251 settings.Set("b3", byte_value_1_->DeepCopy());
252 EXPECT_TRUE(SettingsEqual(settings));
254 // Remove some settings that don't exist.
255 to_remove.clear();
256 to_remove.push_back("a1");
257 to_remove.push_back("a2");
258 EXPECT_FALSE(storage_->Remove(to_remove)->HasError());
259 EXPECT_FALSE(storage_->Remove("b")->HasError());
260 EXPECT_TRUE(SettingsEqual(settings));
262 // Still no quota.
263 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
264 EXPECT_TRUE(SettingsEqual(settings));
267 TEST_F(ExtensionSettingsQuotaTest, Clear) {
268 base::DictionaryValue settings;
269 CreateStorage(40, UINT_MAX, 5);
271 // Test running out of byte quota.
273 base::DictionaryValue to_set;
274 to_set.Set("a", byte_value_16_->DeepCopy());
275 to_set.Set("b", byte_value_16_->DeepCopy());
276 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
277 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError());
279 EXPECT_FALSE(storage_->Clear()->HasError());
281 // (repeat)
282 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
283 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError());
286 // Test reaching max keys.
287 storage_->Clear();
289 base::DictionaryValue to_set;
290 to_set.Set("a", byte_value_1_->DeepCopy());
291 to_set.Set("b", byte_value_1_->DeepCopy());
292 to_set.Set("c", byte_value_1_->DeepCopy());
293 to_set.Set("d", byte_value_1_->DeepCopy());
294 to_set.Set("e", byte_value_1_->DeepCopy());
295 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
296 EXPECT_TRUE(storage_->Set(DEFAULTS, "f", *byte_value_1_)->HasError());
298 storage_->Clear();
300 // (repeat)
301 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
302 EXPECT_TRUE(storage_->Set(DEFAULTS, "f", *byte_value_1_)->HasError());
306 TEST_F(ExtensionSettingsQuotaTest, ChangingUsedBytesWithSet) {
307 base::DictionaryValue settings;
308 CreateStorage(20, UINT_MAX, UINT_MAX);
310 // Change a setting to make it go over quota.
311 storage_->Set(DEFAULTS, "a", *byte_value_16_);
312 settings.Set("a", byte_value_16_->DeepCopy());
313 EXPECT_TRUE(SettingsEqual(settings));
315 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_256_)->HasError());
316 EXPECT_TRUE(SettingsEqual(settings));
318 // Change a setting to reduce usage and room for another setting.
319 EXPECT_TRUE(storage_->Set(DEFAULTS, "foobar", *byte_value_1_)->HasError());
320 storage_->Set(DEFAULTS, "a", *byte_value_1_);
321 settings.Set("a", byte_value_1_->DeepCopy());
323 EXPECT_FALSE(storage_->Set(DEFAULTS, "foobar", *byte_value_1_)->HasError());
324 settings.Set("foobar", byte_value_1_->DeepCopy());
325 EXPECT_TRUE(SettingsEqual(settings));
328 TEST_F(ExtensionSettingsQuotaTest, SetsOnlyEntirelyCompletedWithByteQuota) {
329 base::DictionaryValue settings;
330 CreateStorage(40, UINT_MAX, UINT_MAX);
332 storage_->Set(DEFAULTS, "a", *byte_value_16_);
333 settings.Set("a", byte_value_16_->DeepCopy());
335 // The entire change is over quota.
336 base::DictionaryValue to_set;
337 to_set.Set("b", byte_value_16_->DeepCopy());
338 to_set.Set("c", byte_value_16_->DeepCopy());
339 EXPECT_TRUE(storage_->Set(DEFAULTS, to_set)->HasError());
340 EXPECT_TRUE(SettingsEqual(settings));
342 // The entire change is over quota, but quota reduced in existing key.
343 to_set.Set("a", byte_value_1_->DeepCopy());
344 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
345 settings.Set("a", byte_value_1_->DeepCopy());
346 settings.Set("b", byte_value_16_->DeepCopy());
347 settings.Set("c", byte_value_16_->DeepCopy());
348 EXPECT_TRUE(SettingsEqual(settings));
351 TEST_F(ExtensionSettingsQuotaTest, SetsOnlyEntireCompletedWithMaxKeys) {
352 base::DictionaryValue settings;
353 CreateStorage(UINT_MAX, UINT_MAX, 2);
355 storage_->Set(DEFAULTS, "a", *byte_value_1_);
356 settings.Set("a", byte_value_1_->DeepCopy());
358 base::DictionaryValue to_set;
359 to_set.Set("b", byte_value_16_->DeepCopy());
360 to_set.Set("c", byte_value_16_->DeepCopy());
361 EXPECT_TRUE(storage_->Set(DEFAULTS, to_set)->HasError());
362 EXPECT_TRUE(SettingsEqual(settings));
365 TEST_F(ExtensionSettingsQuotaTest, WithInitialDataAndByteQuota) {
366 base::DictionaryValue settings;
367 delegate_->Set(DEFAULTS, "a", *byte_value_256_);
368 settings.Set("a", byte_value_256_->DeepCopy());
370 CreateStorage(280, UINT_MAX, UINT_MAX);
371 EXPECT_TRUE(SettingsEqual(settings));
373 // Add some data.
374 EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError());
375 settings.Set("b", byte_value_16_->DeepCopy());
376 EXPECT_TRUE(SettingsEqual(settings));
378 // Not enough quota.
379 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError());
380 EXPECT_TRUE(SettingsEqual(settings));
382 // Reduce usage of original setting so that "c" can fit.
383 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_16_)->HasError());
384 settings.Set("a", byte_value_16_->DeepCopy());
385 EXPECT_TRUE(SettingsEqual(settings));
387 EXPECT_FALSE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError());
388 settings.Set("c", byte_value_16_->DeepCopy());
389 EXPECT_TRUE(SettingsEqual(settings));
391 // Remove to free up some more data.
392 EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_256_)->HasError());
394 std::vector<std::string> to_remove;
395 to_remove.push_back("a");
396 to_remove.push_back("b");
397 storage_->Remove(to_remove);
398 settings.Remove("a", NULL);
399 settings.Remove("b", NULL);
400 EXPECT_TRUE(SettingsEqual(settings));
402 EXPECT_FALSE(storage_->Set(DEFAULTS, "d", *byte_value_256_)->HasError());
403 settings.Set("d", byte_value_256_->DeepCopy());
404 EXPECT_TRUE(SettingsEqual(settings));
407 TEST_F(ExtensionSettingsQuotaTest, WithInitialDataAndMaxKeys) {
408 base::DictionaryValue settings;
409 delegate_->Set(DEFAULTS, "a", *byte_value_1_);
410 settings.Set("a", byte_value_1_->DeepCopy());
411 CreateStorage(UINT_MAX, UINT_MAX, 2);
413 EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError());
414 settings.Set("b", byte_value_1_->DeepCopy());
416 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_1_)->HasError());
418 EXPECT_TRUE(SettingsEqual(settings));
421 TEST_F(ExtensionSettingsQuotaTest, InitiallyOverByteQuota) {
422 base::DictionaryValue settings;
423 settings.Set("a", byte_value_16_->DeepCopy());
424 settings.Set("b", byte_value_16_->DeepCopy());
425 settings.Set("c", byte_value_16_->DeepCopy());
426 delegate_->Set(DEFAULTS, settings);
428 CreateStorage(40, UINT_MAX, UINT_MAX);
429 EXPECT_TRUE(SettingsEqual(settings));
431 EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_16_)->HasError());
433 // Take under quota by reducing size of an existing setting
434 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
435 settings.Set("a", byte_value_1_->DeepCopy());
436 EXPECT_TRUE(SettingsEqual(settings));
438 // Should be able set another small setting.
439 EXPECT_FALSE(storage_->Set(DEFAULTS, "d", *byte_value_1_)->HasError());
440 settings.Set("d", byte_value_1_->DeepCopy());
441 EXPECT_TRUE(SettingsEqual(settings));
444 TEST_F(ExtensionSettingsQuotaTest, InitiallyOverMaxKeys) {
445 base::DictionaryValue settings;
446 settings.Set("a", byte_value_16_->DeepCopy());
447 settings.Set("b", byte_value_16_->DeepCopy());
448 settings.Set("c", byte_value_16_->DeepCopy());
449 delegate_->Set(DEFAULTS, settings);
451 CreateStorage(UINT_MAX, UINT_MAX, 2);
452 EXPECT_TRUE(SettingsEqual(settings));
454 // Can't set either an existing or new setting.
455 EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_16_)->HasError());
456 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
457 EXPECT_TRUE(SettingsEqual(settings));
459 // Should be able after removing 2.
460 storage_->Remove("a");
461 settings.Remove("a", NULL);
462 storage_->Remove("b");
463 settings.Remove("b", NULL);
464 EXPECT_TRUE(SettingsEqual(settings));
466 EXPECT_FALSE(storage_->Set(DEFAULTS, "e", *byte_value_1_)->HasError());
467 settings.Set("e", byte_value_1_->DeepCopy());
468 EXPECT_TRUE(SettingsEqual(settings));
470 // Still can't set any.
471 EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_16_)->HasError());
472 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
473 EXPECT_TRUE(SettingsEqual(settings));
476 TEST_F(ExtensionSettingsQuotaTest, ZeroQuotaBytesPerSetting) {
477 base::DictionaryValue empty;
478 CreateStorage(UINT_MAX, 0, UINT_MAX);
480 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
481 EXPECT_FALSE(storage_->Remove("a")->HasError());
482 EXPECT_FALSE(storage_->Remove("b")->HasError());
483 EXPECT_TRUE(SettingsEqual(empty));
486 TEST_F(ExtensionSettingsQuotaTest, QuotaBytesPerSetting) {
487 base::DictionaryValue settings;
489 CreateStorage(UINT_MAX, 20, UINT_MAX);
491 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
492 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_16_)->HasError());
493 settings.Set("a", byte_value_16_->DeepCopy());
494 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_256_)->HasError());
496 EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError());
497 EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError());
498 settings.Set("b", byte_value_16_->DeepCopy());
499 EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_256_)->HasError());
501 EXPECT_TRUE(SettingsEqual(settings));
504 TEST_F(ExtensionSettingsQuotaTest, QuotaBytesPerSettingWithInitialSettings) {
505 base::DictionaryValue settings;
507 delegate_->Set(DEFAULTS, "a", *byte_value_1_);
508 delegate_->Set(DEFAULTS, "b", *byte_value_16_);
509 delegate_->Set(DEFAULTS, "c", *byte_value_256_);
510 CreateStorage(UINT_MAX, 20, UINT_MAX);
512 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
513 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_16_)->HasError());
514 settings.Set("a", byte_value_16_->DeepCopy());
515 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_256_)->HasError());
517 EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError());
518 EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError());
519 settings.Set("b", byte_value_16_->DeepCopy());
520 EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_256_)->HasError());
522 EXPECT_FALSE(storage_->Set(DEFAULTS, "c", *byte_value_1_)->HasError());
523 EXPECT_FALSE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError());
524 settings.Set("c", byte_value_16_->DeepCopy());
525 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
527 EXPECT_TRUE(SettingsEqual(settings));
530 TEST_F(ExtensionSettingsQuotaTest,
531 QuotaBytesPerSettingWithInitialSettingsForced) {
532 // This is a lazy test to make sure IGNORE_QUOTA lets through changes: the
533 // test above copied, but using IGNORE_QUOTA and asserting nothing is ever
534 // rejected...
535 base::DictionaryValue settings;
537 delegate_->Set(DEFAULTS, "a", *byte_value_1_);
538 delegate_->Set(DEFAULTS, "b", *byte_value_16_);
539 delegate_->Set(DEFAULTS, "c", *byte_value_256_);
540 CreateStorage(UINT_MAX, 20, UINT_MAX);
542 EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "a", *byte_value_1_)->HasError());
543 EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "a", *byte_value_16_)->HasError());
544 EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "a", *byte_value_256_)->HasError());
545 settings.Set("a", byte_value_256_->DeepCopy());
547 EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "b", *byte_value_1_)->HasError());
548 EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "b", *byte_value_16_)->HasError());
549 EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "b", *byte_value_256_)->HasError());
550 settings.Set("b", byte_value_256_->DeepCopy());
552 EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "c", *byte_value_1_)->HasError());
553 EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "c", *byte_value_16_)->HasError());
554 settings.Set("c", byte_value_16_->DeepCopy());
556 // ... except the last. Make sure it can still fail.
557 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
559 EXPECT_TRUE(SettingsEqual(settings));
562 TEST_F(ExtensionSettingsQuotaTest, GetBytesInUse) {
563 // Just testing GetBytesInUse, no need for a quota.
564 CreateStorage(UINT_MAX, UINT_MAX, UINT_MAX);
566 std::vector<std::string> ab;
567 ab.push_back("a");
568 ab.push_back("b");
570 EXPECT_EQ(0u, storage_->GetBytesInUse());
571 EXPECT_EQ(0u, storage_->GetBytesInUse("a"));
572 EXPECT_EQ(0u, storage_->GetBytesInUse("b"));
573 EXPECT_EQ(0u, storage_->GetBytesInUse(ab));
575 storage_->Set(DEFAULTS, "a", *byte_value_1_);
577 EXPECT_EQ(2u, storage_->GetBytesInUse());
578 EXPECT_EQ(2u, storage_->GetBytesInUse("a"));
579 EXPECT_EQ(0u, storage_->GetBytesInUse("b"));
580 EXPECT_EQ(2u, storage_->GetBytesInUse(ab));
582 storage_->Set(DEFAULTS, "b", *byte_value_1_);
584 EXPECT_EQ(4u, storage_->GetBytesInUse());
585 EXPECT_EQ(2u, storage_->GetBytesInUse("a"));
586 EXPECT_EQ(2u, storage_->GetBytesInUse("b"));
587 EXPECT_EQ(4u, storage_->GetBytesInUse(ab));
589 storage_->Set(DEFAULTS, "c", *byte_value_1_);
591 EXPECT_EQ(6u, storage_->GetBytesInUse());
592 EXPECT_EQ(2u, storage_->GetBytesInUse("a"));
593 EXPECT_EQ(2u, storage_->GetBytesInUse("b"));
594 EXPECT_EQ(4u, storage_->GetBytesInUse(ab));
597 } // namespace extensions