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
;
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
{
26 ExtensionSettingsQuotaTest()
28 byte_value_16_("sixteen bytes."),
29 delegate_(new TestingValueStore()) {
30 for (int i
= 1; i
< 89; ++i
) {
31 byte_value_256_
.AppendInteger(i
);
36 void ValidateByteValues() {
37 std::string validate_sizes
;
38 base::JSONWriter::Write(byte_value_1_
, &validate_sizes
);
39 ASSERT_EQ(1u, validate_sizes
.size());
40 base::JSONWriter::Write(byte_value_16_
, &validate_sizes
);
41 ASSERT_EQ(16u, validate_sizes
.size());
42 base::JSONWriter::Write(byte_value_256_
, &validate_sizes
);
43 ASSERT_EQ(256u, validate_sizes
.size());
46 void TearDown() override
{ ASSERT_TRUE(storage_
.get() != NULL
); }
49 // Creates |storage_|. Must only be called once.
51 size_t quota_bytes
, size_t quota_bytes_per_item
, size_t max_items
) {
52 ASSERT_TRUE(storage_
.get() == NULL
);
53 SettingsStorageQuotaEnforcer::Limits limits
=
54 { quota_bytes
, quota_bytes_per_item
, max_items
};
55 storage_
.reset(new SettingsStorageQuotaEnforcer(limits
, delegate_
));
58 // Returns whether the settings in |storage_| and |delegate_| are the same as
60 bool SettingsEqual(const base::DictionaryValue
& settings
) {
61 return settings
.Equals(&storage_
->Get()->settings()) &&
62 settings
.Equals(&delegate_
->Get()->settings());
65 // Values with different serialized sizes.
66 base::FundamentalValue byte_value_1_
;
67 base::StringValue byte_value_16_
;
68 base::ListValue byte_value_256_
;
70 // Quota enforcing storage area being tested.
71 scoped_ptr
<SettingsStorageQuotaEnforcer
> storage_
;
73 // In-memory storage area being delegated to. Always owned by |storage_|.
74 TestingValueStore
* delegate_
;
77 TEST_F(ExtensionSettingsQuotaTest
, ZeroQuotaBytes
) {
78 base::DictionaryValue empty
;
79 CreateStorage(0, UINT_MAX
, UINT_MAX
);
81 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
82 EXPECT_FALSE(storage_
->Remove("a")->HasError());
83 EXPECT_FALSE(storage_
->Remove("b")->HasError());
84 EXPECT_TRUE(SettingsEqual(empty
));
87 TEST_F(ExtensionSettingsQuotaTest
, KeySizeTakenIntoAccount
) {
88 base::DictionaryValue empty
;
89 CreateStorage(8u, UINT_MAX
, UINT_MAX
);
91 storage_
->Set(DEFAULTS
, "Really long key", byte_value_1_
)->HasError());
92 EXPECT_TRUE(SettingsEqual(empty
));
95 TEST_F(ExtensionSettingsQuotaTest
, SmallByteQuota
) {
96 base::DictionaryValue settings
;
97 CreateStorage(8u, UINT_MAX
, UINT_MAX
);
99 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
100 settings
.Set("a", byte_value_1_
.CreateDeepCopy());
101 EXPECT_TRUE(SettingsEqual(settings
));
103 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "b", byte_value_16_
)->HasError());
104 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", byte_value_256_
)->HasError());
105 EXPECT_TRUE(SettingsEqual(settings
));
108 TEST_F(ExtensionSettingsQuotaTest
, MediumByteQuota
) {
109 base::DictionaryValue settings
;
110 CreateStorage(40, UINT_MAX
, UINT_MAX
);
112 base::DictionaryValue to_set
;
113 to_set
.Set("a", byte_value_1_
.CreateDeepCopy());
114 to_set
.Set("b", byte_value_16_
.CreateDeepCopy());
115 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
116 settings
.Set("a", byte_value_1_
.CreateDeepCopy());
117 settings
.Set("b", byte_value_16_
.CreateDeepCopy());
118 EXPECT_TRUE(SettingsEqual(settings
));
120 // Should be able to set value to other under-quota value.
121 to_set
.Set("a", byte_value_16_
.CreateDeepCopy());
122 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
123 settings
.Set("a", byte_value_16_
.CreateDeepCopy());
124 EXPECT_TRUE(SettingsEqual(settings
));
126 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", byte_value_256_
)->HasError());
127 EXPECT_TRUE(SettingsEqual(settings
));
130 TEST_F(ExtensionSettingsQuotaTest
, ZeroMaxKeys
) {
131 base::DictionaryValue empty
;
132 CreateStorage(UINT_MAX
, UINT_MAX
, 0);
134 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
135 EXPECT_FALSE(storage_
->Remove("a")->HasError());
136 EXPECT_FALSE(storage_
->Remove("b")->HasError());
137 EXPECT_TRUE(SettingsEqual(empty
));
140 TEST_F(ExtensionSettingsQuotaTest
, SmallMaxKeys
) {
141 base::DictionaryValue settings
;
142 CreateStorage(UINT_MAX
, UINT_MAX
, 1);
144 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
145 settings
.Set("a", byte_value_1_
.CreateDeepCopy());
146 EXPECT_TRUE(SettingsEqual(settings
));
148 // Should be able to set existing key to other value without going over quota.
149 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", byte_value_16_
)->HasError());
150 settings
.Set("a", byte_value_16_
.CreateDeepCopy());
151 EXPECT_TRUE(SettingsEqual(settings
));
153 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "b", byte_value_16_
)->HasError());
154 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", byte_value_256_
)->HasError());
155 EXPECT_TRUE(SettingsEqual(settings
));
158 TEST_F(ExtensionSettingsQuotaTest
, MediumMaxKeys
) {
159 base::DictionaryValue settings
;
160 CreateStorage(UINT_MAX
, UINT_MAX
, 2);
162 base::DictionaryValue to_set
;
163 to_set
.Set("a", byte_value_1_
.CreateDeepCopy());
164 to_set
.Set("b", byte_value_16_
.CreateDeepCopy());
165 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
166 settings
.Set("a", byte_value_1_
.CreateDeepCopy());
167 settings
.Set("b", byte_value_16_
.CreateDeepCopy());
168 EXPECT_TRUE(SettingsEqual(settings
));
170 // Should be able to set existing keys to other values without going over
172 to_set
.Set("a", byte_value_16_
.CreateDeepCopy());
173 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
174 settings
.Set("a", byte_value_16_
.CreateDeepCopy());
175 EXPECT_TRUE(SettingsEqual(settings
));
177 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", byte_value_256_
)->HasError());
178 EXPECT_TRUE(SettingsEqual(settings
));
181 TEST_F(ExtensionSettingsQuotaTest
, RemovingExistingSettings
) {
182 base::DictionaryValue settings
;
183 CreateStorage(266, UINT_MAX
, 2);
185 storage_
->Set(DEFAULTS
, "b", byte_value_16_
);
186 settings
.Set("b", byte_value_16_
.CreateDeepCopy());
188 storage_
->Set(DEFAULTS
, "c", byte_value_256_
);
189 EXPECT_TRUE(SettingsEqual(settings
));
191 // Try again with "b" removed, enough quota.
192 EXPECT_FALSE(storage_
->Remove("b")->HasError());
193 settings
.Remove("b", NULL
);
194 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "c", byte_value_256_
)->HasError());
195 settings
.Set("c", byte_value_256_
.CreateDeepCopy());
196 EXPECT_TRUE(SettingsEqual(settings
));
198 // Enough byte quota but max keys not high enough.
199 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
200 settings
.Set("a", byte_value_1_
.CreateDeepCopy());
201 EXPECT_TRUE(SettingsEqual(settings
));
203 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "b", byte_value_1_
)->HasError());
204 EXPECT_TRUE(SettingsEqual(settings
));
206 // Back under max keys.
207 EXPECT_FALSE(storage_
->Remove("a")->HasError());
208 settings
.Remove("a", NULL
);
209 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", byte_value_1_
)->HasError());
210 settings
.Set("b", byte_value_1_
.CreateDeepCopy());
211 EXPECT_TRUE(SettingsEqual(settings
));
214 TEST_F(ExtensionSettingsQuotaTest
, RemovingNonexistentSettings
) {
215 base::DictionaryValue settings
;
216 CreateStorage(36, UINT_MAX
, 3);
219 base::DictionaryValue to_set
;
220 to_set
.Set("b1", byte_value_16_
.CreateDeepCopy());
221 to_set
.Set("b2", byte_value_16_
.CreateDeepCopy());
222 storage_
->Set(DEFAULTS
, to_set
);
223 settings
.Set("b1", byte_value_16_
.CreateDeepCopy());
224 settings
.Set("b2", byte_value_16_
.CreateDeepCopy());
225 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
226 EXPECT_TRUE(SettingsEqual(settings
));
228 // Remove some settings that don't exist.
229 std::vector
<std::string
> to_remove
;
230 to_remove
.push_back("a1");
231 to_remove
.push_back("a2");
232 EXPECT_FALSE(storage_
->Remove(to_remove
)->HasError());
233 EXPECT_FALSE(storage_
->Remove("b")->HasError());
234 EXPECT_TRUE(SettingsEqual(settings
));
237 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
238 EXPECT_TRUE(SettingsEqual(settings
));
240 // Max out key count.
242 to_set
.Set("b1", byte_value_1_
.CreateDeepCopy());
243 to_set
.Set("b2", byte_value_1_
.CreateDeepCopy());
244 storage_
->Set(DEFAULTS
, to_set
);
245 settings
.Set("b1", byte_value_1_
.CreateDeepCopy());
246 settings
.Set("b2", byte_value_1_
.CreateDeepCopy());
247 storage_
->Set(DEFAULTS
, "b3", byte_value_1_
);
248 settings
.Set("b3", byte_value_1_
.CreateDeepCopy());
249 EXPECT_TRUE(SettingsEqual(settings
));
251 // Remove some settings that don't exist.
253 to_remove
.push_back("a1");
254 to_remove
.push_back("a2");
255 EXPECT_FALSE(storage_
->Remove(to_remove
)->HasError());
256 EXPECT_FALSE(storage_
->Remove("b")->HasError());
257 EXPECT_TRUE(SettingsEqual(settings
));
260 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
261 EXPECT_TRUE(SettingsEqual(settings
));
264 TEST_F(ExtensionSettingsQuotaTest
, Clear
) {
265 base::DictionaryValue settings
;
266 CreateStorage(40, UINT_MAX
, 5);
268 // Test running out of byte quota.
270 base::DictionaryValue to_set
;
271 to_set
.Set("a", byte_value_16_
.CreateDeepCopy());
272 to_set
.Set("b", byte_value_16_
.CreateDeepCopy());
273 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
274 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", byte_value_16_
)->HasError());
276 EXPECT_FALSE(storage_
->Clear()->HasError());
279 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
280 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", byte_value_16_
)->HasError());
283 // Test reaching max keys.
286 base::DictionaryValue to_set
;
287 to_set
.Set("a", byte_value_1_
.CreateDeepCopy());
288 to_set
.Set("b", byte_value_1_
.CreateDeepCopy());
289 to_set
.Set("c", byte_value_1_
.CreateDeepCopy());
290 to_set
.Set("d", byte_value_1_
.CreateDeepCopy());
291 to_set
.Set("e", byte_value_1_
.CreateDeepCopy());
292 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
293 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "f", byte_value_1_
)->HasError());
298 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
299 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "f", byte_value_1_
)->HasError());
303 TEST_F(ExtensionSettingsQuotaTest
, ChangingUsedBytesWithSet
) {
304 base::DictionaryValue settings
;
305 CreateStorage(20, UINT_MAX
, UINT_MAX
);
307 // Change a setting to make it go over quota.
308 storage_
->Set(DEFAULTS
, "a", byte_value_16_
);
309 settings
.Set("a", byte_value_16_
.CreateDeepCopy());
310 EXPECT_TRUE(SettingsEqual(settings
));
312 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", byte_value_256_
)->HasError());
313 EXPECT_TRUE(SettingsEqual(settings
));
315 // Change a setting to reduce usage and room for another setting.
316 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "foobar", byte_value_1_
)->HasError());
317 storage_
->Set(DEFAULTS
, "a", byte_value_1_
);
318 settings
.Set("a", byte_value_1_
.CreateDeepCopy());
320 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "foobar", byte_value_1_
)->HasError());
321 settings
.Set("foobar", byte_value_1_
.CreateDeepCopy());
322 EXPECT_TRUE(SettingsEqual(settings
));
325 TEST_F(ExtensionSettingsQuotaTest
, SetsOnlyEntirelyCompletedWithByteQuota
) {
326 base::DictionaryValue settings
;
327 CreateStorage(40, UINT_MAX
, UINT_MAX
);
329 storage_
->Set(DEFAULTS
, "a", byte_value_16_
);
330 settings
.Set("a", byte_value_16_
.CreateDeepCopy());
332 // The entire change is over quota.
333 base::DictionaryValue to_set
;
334 to_set
.Set("b", byte_value_16_
.CreateDeepCopy());
335 to_set
.Set("c", byte_value_16_
.CreateDeepCopy());
336 EXPECT_TRUE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
337 EXPECT_TRUE(SettingsEqual(settings
));
339 // The entire change is over quota, but quota reduced in existing key.
340 to_set
.Set("a", byte_value_1_
.CreateDeepCopy());
341 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
342 settings
.Set("a", byte_value_1_
.CreateDeepCopy());
343 settings
.Set("b", byte_value_16_
.CreateDeepCopy());
344 settings
.Set("c", byte_value_16_
.CreateDeepCopy());
345 EXPECT_TRUE(SettingsEqual(settings
));
348 TEST_F(ExtensionSettingsQuotaTest
, SetsOnlyEntireCompletedWithMaxKeys
) {
349 base::DictionaryValue settings
;
350 CreateStorage(UINT_MAX
, UINT_MAX
, 2);
352 storage_
->Set(DEFAULTS
, "a", byte_value_1_
);
353 settings
.Set("a", byte_value_1_
.CreateDeepCopy());
355 base::DictionaryValue to_set
;
356 to_set
.Set("b", byte_value_16_
.CreateDeepCopy());
357 to_set
.Set("c", byte_value_16_
.CreateDeepCopy());
358 EXPECT_TRUE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
359 EXPECT_TRUE(SettingsEqual(settings
));
362 TEST_F(ExtensionSettingsQuotaTest
, WithInitialDataAndByteQuota
) {
363 base::DictionaryValue settings
;
364 delegate_
->Set(DEFAULTS
, "a", byte_value_256_
);
365 settings
.Set("a", byte_value_256_
.CreateDeepCopy());
367 CreateStorage(280, UINT_MAX
, UINT_MAX
);
368 EXPECT_TRUE(SettingsEqual(settings
));
371 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", byte_value_16_
)->HasError());
372 settings
.Set("b", byte_value_16_
.CreateDeepCopy());
373 EXPECT_TRUE(SettingsEqual(settings
));
376 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", byte_value_16_
)->HasError());
377 EXPECT_TRUE(SettingsEqual(settings
));
379 // Reduce usage of original setting so that "c" can fit.
380 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", byte_value_16_
)->HasError());
381 settings
.Set("a", byte_value_16_
.CreateDeepCopy());
382 EXPECT_TRUE(SettingsEqual(settings
));
384 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "c", byte_value_16_
)->HasError());
385 settings
.Set("c", byte_value_16_
.CreateDeepCopy());
386 EXPECT_TRUE(SettingsEqual(settings
));
388 // Remove to free up some more data.
389 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "d", byte_value_256_
)->HasError());
391 std::vector
<std::string
> to_remove
;
392 to_remove
.push_back("a");
393 to_remove
.push_back("b");
394 storage_
->Remove(to_remove
);
395 settings
.Remove("a", NULL
);
396 settings
.Remove("b", NULL
);
397 EXPECT_TRUE(SettingsEqual(settings
));
399 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "d", byte_value_256_
)->HasError());
400 settings
.Set("d", byte_value_256_
.CreateDeepCopy());
401 EXPECT_TRUE(SettingsEqual(settings
));
404 TEST_F(ExtensionSettingsQuotaTest
, WithInitialDataAndMaxKeys
) {
405 base::DictionaryValue settings
;
406 delegate_
->Set(DEFAULTS
, "a", byte_value_1_
);
407 settings
.Set("a", byte_value_1_
.CreateDeepCopy());
408 CreateStorage(UINT_MAX
, UINT_MAX
, 2);
410 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", byte_value_1_
)->HasError());
411 settings
.Set("b", byte_value_1_
.CreateDeepCopy());
413 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", byte_value_1_
)->HasError());
415 EXPECT_TRUE(SettingsEqual(settings
));
418 TEST_F(ExtensionSettingsQuotaTest
, InitiallyOverByteQuota
) {
419 base::DictionaryValue settings
;
420 settings
.Set("a", byte_value_16_
.CreateDeepCopy());
421 settings
.Set("b", byte_value_16_
.CreateDeepCopy());
422 settings
.Set("c", byte_value_16_
.CreateDeepCopy());
423 delegate_
->Set(DEFAULTS
, settings
);
425 CreateStorage(40, UINT_MAX
, UINT_MAX
);
426 EXPECT_TRUE(SettingsEqual(settings
));
428 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "d", byte_value_16_
)->HasError());
430 // Take under quota by reducing size of an existing setting
431 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
432 settings
.Set("a", byte_value_1_
.CreateDeepCopy());
433 EXPECT_TRUE(SettingsEqual(settings
));
435 // Should be able set another small setting.
436 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "d", byte_value_1_
)->HasError());
437 settings
.Set("d", byte_value_1_
.CreateDeepCopy());
438 EXPECT_TRUE(SettingsEqual(settings
));
441 TEST_F(ExtensionSettingsQuotaTest
, InitiallyOverMaxKeys
) {
442 base::DictionaryValue settings
;
443 settings
.Set("a", byte_value_16_
.CreateDeepCopy());
444 settings
.Set("b", byte_value_16_
.CreateDeepCopy());
445 settings
.Set("c", byte_value_16_
.CreateDeepCopy());
446 delegate_
->Set(DEFAULTS
, settings
);
448 CreateStorage(UINT_MAX
, UINT_MAX
, 2);
449 EXPECT_TRUE(SettingsEqual(settings
));
451 // Can't set either an existing or new setting.
452 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "d", byte_value_16_
)->HasError());
453 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
454 EXPECT_TRUE(SettingsEqual(settings
));
456 // Should be able after removing 2.
457 storage_
->Remove("a");
458 settings
.Remove("a", NULL
);
459 storage_
->Remove("b");
460 settings
.Remove("b", NULL
);
461 EXPECT_TRUE(SettingsEqual(settings
));
463 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "e", byte_value_1_
)->HasError());
464 settings
.Set("e", byte_value_1_
.CreateDeepCopy());
465 EXPECT_TRUE(SettingsEqual(settings
));
467 // Still can't set any.
468 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "d", byte_value_16_
)->HasError());
469 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
470 EXPECT_TRUE(SettingsEqual(settings
));
473 TEST_F(ExtensionSettingsQuotaTest
, ZeroQuotaBytesPerSetting
) {
474 base::DictionaryValue empty
;
475 CreateStorage(UINT_MAX
, 0, UINT_MAX
);
477 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
478 EXPECT_FALSE(storage_
->Remove("a")->HasError());
479 EXPECT_FALSE(storage_
->Remove("b")->HasError());
480 EXPECT_TRUE(SettingsEqual(empty
));
483 TEST_F(ExtensionSettingsQuotaTest
, QuotaBytesPerSetting
) {
484 base::DictionaryValue settings
;
486 CreateStorage(UINT_MAX
, 20, UINT_MAX
);
488 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
489 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", byte_value_16_
)->HasError());
490 settings
.Set("a", byte_value_16_
.CreateDeepCopy());
491 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", byte_value_256_
)->HasError());
493 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", byte_value_1_
)->HasError());
494 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", byte_value_16_
)->HasError());
495 settings
.Set("b", byte_value_16_
.CreateDeepCopy());
496 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "b", byte_value_256_
)->HasError());
498 EXPECT_TRUE(SettingsEqual(settings
));
501 TEST_F(ExtensionSettingsQuotaTest
, QuotaBytesPerSettingWithInitialSettings
) {
502 base::DictionaryValue settings
;
504 delegate_
->Set(DEFAULTS
, "a", byte_value_1_
);
505 delegate_
->Set(DEFAULTS
, "b", byte_value_16_
);
506 delegate_
->Set(DEFAULTS
, "c", byte_value_256_
);
507 CreateStorage(UINT_MAX
, 20, UINT_MAX
);
509 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", byte_value_1_
)->HasError());
510 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", byte_value_16_
)->HasError());
511 settings
.Set("a", byte_value_16_
.CreateDeepCopy());
512 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", byte_value_256_
)->HasError());
514 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", byte_value_1_
)->HasError());
515 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", byte_value_16_
)->HasError());
516 settings
.Set("b", byte_value_16_
.CreateDeepCopy());
517 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "b", byte_value_256_
)->HasError());
519 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "c", byte_value_1_
)->HasError());
520 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "c", byte_value_16_
)->HasError());
521 settings
.Set("c", byte_value_16_
.CreateDeepCopy());
522 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", byte_value_256_
)->HasError());
524 EXPECT_TRUE(SettingsEqual(settings
));
527 TEST_F(ExtensionSettingsQuotaTest
,
528 QuotaBytesPerSettingWithInitialSettingsForced
) {
529 // This is a lazy test to make sure IGNORE_QUOTA lets through changes: the
530 // test above copied, but using IGNORE_QUOTA and asserting nothing is ever
532 base::DictionaryValue settings
;
534 delegate_
->Set(DEFAULTS
, "a", byte_value_1_
);
535 delegate_
->Set(DEFAULTS
, "b", byte_value_16_
);
536 delegate_
->Set(DEFAULTS
, "c", byte_value_256_
);
537 CreateStorage(UINT_MAX
, 20, UINT_MAX
);
539 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "a", byte_value_1_
)->HasError());
540 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "a", byte_value_16_
)->HasError());
541 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "a", byte_value_256_
)->HasError());
542 settings
.Set("a", byte_value_256_
.CreateDeepCopy());
544 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "b", byte_value_1_
)->HasError());
545 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "b", byte_value_16_
)->HasError());
546 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "b", byte_value_256_
)->HasError());
547 settings
.Set("b", byte_value_256_
.CreateDeepCopy());
549 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "c", byte_value_1_
)->HasError());
550 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "c", byte_value_16_
)->HasError());
551 settings
.Set("c", byte_value_16_
.CreateDeepCopy());
553 // ... except the last. Make sure it can still fail.
554 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", byte_value_256_
)->HasError());
556 EXPECT_TRUE(SettingsEqual(settings
));
559 TEST_F(ExtensionSettingsQuotaTest
, GetBytesInUse
) {
560 // Just testing GetBytesInUse, no need for a quota.
561 CreateStorage(UINT_MAX
, UINT_MAX
, UINT_MAX
);
563 std::vector
<std::string
> ab
;
567 EXPECT_EQ(0u, storage_
->GetBytesInUse());
568 EXPECT_EQ(0u, storage_
->GetBytesInUse("a"));
569 EXPECT_EQ(0u, storage_
->GetBytesInUse("b"));
570 EXPECT_EQ(0u, storage_
->GetBytesInUse(ab
));
572 storage_
->Set(DEFAULTS
, "a", byte_value_1_
);
574 EXPECT_EQ(2u, storage_
->GetBytesInUse());
575 EXPECT_EQ(2u, storage_
->GetBytesInUse("a"));
576 EXPECT_EQ(0u, storage_
->GetBytesInUse("b"));
577 EXPECT_EQ(2u, storage_
->GetBytesInUse(ab
));
579 storage_
->Set(DEFAULTS
, "b", byte_value_1_
);
581 EXPECT_EQ(4u, storage_
->GetBytesInUse());
582 EXPECT_EQ(2u, storage_
->GetBytesInUse("a"));
583 EXPECT_EQ(2u, storage_
->GetBytesInUse("b"));
584 EXPECT_EQ(4u, storage_
->GetBytesInUse(ab
));
586 storage_
->Set(DEFAULTS
, "c", byte_value_1_
);
588 EXPECT_EQ(6u, storage_
->GetBytesInUse());
589 EXPECT_EQ(2u, storage_
->GetBytesInUse("a"));
590 EXPECT_EQ(2u, storage_
->GetBytesInUse("b"));
591 EXPECT_EQ(4u, storage_
->GetBytesInUse(ab
));
594 } // namespace extensions