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()
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
));
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 void TearDown() override
{ ASSERT_TRUE(storage_
.get() != NULL
); }
50 // Creates |storage_|. Must only be called once.
52 size_t quota_bytes
, size_t quota_bytes_per_item
, size_t max_items
) {
53 ASSERT_TRUE(storage_
.get() == NULL
);
54 SettingsStorageQuotaEnforcer::Limits limits
=
55 { quota_bytes
, quota_bytes_per_item
, max_items
};
56 storage_
.reset(new SettingsStorageQuotaEnforcer(limits
, delegate_
));
59 // Returns whether the settings in |storage_| and |delegate_| are the same as
61 bool SettingsEqual(const base::DictionaryValue
& settings
) {
62 return settings
.Equals(&storage_
->Get()->settings()) &&
63 settings
.Equals(&delegate_
->Get()->settings());
66 // Values with different serialized sizes.
67 scoped_ptr
<base::Value
> byte_value_1_
;
68 scoped_ptr
<base::Value
> byte_value_16_
;
69 scoped_ptr
<base::ListValue
> byte_value_256_
;
71 // Quota enforcing storage area being tested.
72 scoped_ptr
<SettingsStorageQuotaEnforcer
> storage_
;
74 // In-memory storage area being delegated to. Always owned by |storage_|.
75 TestingValueStore
* delegate_
;
78 TEST_F(ExtensionSettingsQuotaTest
, ZeroQuotaBytes
) {
79 base::DictionaryValue empty
;
80 CreateStorage(0, UINT_MAX
, UINT_MAX
);
82 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
83 EXPECT_FALSE(storage_
->Remove("a")->HasError());
84 EXPECT_FALSE(storage_
->Remove("b")->HasError());
85 EXPECT_TRUE(SettingsEqual(empty
));
88 TEST_F(ExtensionSettingsQuotaTest
, KeySizeTakenIntoAccount
) {
89 base::DictionaryValue empty
;
90 CreateStorage(8u, UINT_MAX
, UINT_MAX
);
92 storage_
->Set(DEFAULTS
, "Really long key", *byte_value_1_
)->HasError());
93 EXPECT_TRUE(SettingsEqual(empty
));
96 TEST_F(ExtensionSettingsQuotaTest
, SmallByteQuota
) {
97 base::DictionaryValue settings
;
98 CreateStorage(8u, UINT_MAX
, UINT_MAX
);
100 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
101 settings
.Set("a", byte_value_1_
->DeepCopy());
102 EXPECT_TRUE(SettingsEqual(settings
));
104 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "b", *byte_value_16_
)->HasError());
105 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", *byte_value_256_
)->HasError());
106 EXPECT_TRUE(SettingsEqual(settings
));
109 TEST_F(ExtensionSettingsQuotaTest
, MediumByteQuota
) {
110 base::DictionaryValue settings
;
111 CreateStorage(40, UINT_MAX
, UINT_MAX
);
113 base::DictionaryValue to_set
;
114 to_set
.Set("a", byte_value_1_
->DeepCopy());
115 to_set
.Set("b", byte_value_16_
->DeepCopy());
116 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
117 settings
.Set("a", byte_value_1_
->DeepCopy());
118 settings
.Set("b", byte_value_16_
->DeepCopy());
119 EXPECT_TRUE(SettingsEqual(settings
));
121 // Should be able to set value to other under-quota value.
122 to_set
.Set("a", byte_value_16_
->DeepCopy());
123 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
124 settings
.Set("a", byte_value_16_
->DeepCopy());
125 EXPECT_TRUE(SettingsEqual(settings
));
127 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", *byte_value_256_
)->HasError());
128 EXPECT_TRUE(SettingsEqual(settings
));
131 TEST_F(ExtensionSettingsQuotaTest
, ZeroMaxKeys
) {
132 base::DictionaryValue empty
;
133 CreateStorage(UINT_MAX
, UINT_MAX
, 0);
135 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
136 EXPECT_FALSE(storage_
->Remove("a")->HasError());
137 EXPECT_FALSE(storage_
->Remove("b")->HasError());
138 EXPECT_TRUE(SettingsEqual(empty
));
141 TEST_F(ExtensionSettingsQuotaTest
, SmallMaxKeys
) {
142 base::DictionaryValue settings
;
143 CreateStorage(UINT_MAX
, UINT_MAX
, 1);
145 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
146 settings
.Set("a", byte_value_1_
->DeepCopy());
147 EXPECT_TRUE(SettingsEqual(settings
));
149 // Should be able to set existing key to other value without going over quota.
150 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", *byte_value_16_
)->HasError());
151 settings
.Set("a", byte_value_16_
->DeepCopy());
152 EXPECT_TRUE(SettingsEqual(settings
));
154 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "b", *byte_value_16_
)->HasError());
155 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", *byte_value_256_
)->HasError());
156 EXPECT_TRUE(SettingsEqual(settings
));
159 TEST_F(ExtensionSettingsQuotaTest
, MediumMaxKeys
) {
160 base::DictionaryValue settings
;
161 CreateStorage(UINT_MAX
, UINT_MAX
, 2);
163 base::DictionaryValue to_set
;
164 to_set
.Set("a", byte_value_1_
->DeepCopy());
165 to_set
.Set("b", byte_value_16_
->DeepCopy());
166 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
167 settings
.Set("a", byte_value_1_
->DeepCopy());
168 settings
.Set("b", byte_value_16_
->DeepCopy());
169 EXPECT_TRUE(SettingsEqual(settings
));
171 // Should be able to set existing keys to other values without going over
173 to_set
.Set("a", byte_value_16_
->DeepCopy());
174 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
175 settings
.Set("a", byte_value_16_
->DeepCopy());
176 EXPECT_TRUE(SettingsEqual(settings
));
178 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", *byte_value_256_
)->HasError());
179 EXPECT_TRUE(SettingsEqual(settings
));
182 TEST_F(ExtensionSettingsQuotaTest
, RemovingExistingSettings
) {
183 base::DictionaryValue settings
;
184 CreateStorage(266, UINT_MAX
, 2);
186 storage_
->Set(DEFAULTS
, "b", *byte_value_16_
);
187 settings
.Set("b", byte_value_16_
->DeepCopy());
189 storage_
->Set(DEFAULTS
, "c", *byte_value_256_
);
190 EXPECT_TRUE(SettingsEqual(settings
));
192 // Try again with "b" removed, enough quota.
193 EXPECT_FALSE(storage_
->Remove("b")->HasError());
194 settings
.Remove("b", NULL
);
195 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "c", *byte_value_256_
)->HasError());
196 settings
.Set("c", byte_value_256_
->DeepCopy());
197 EXPECT_TRUE(SettingsEqual(settings
));
199 // Enough byte quota but max keys not high enough.
200 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
201 settings
.Set("a", byte_value_1_
->DeepCopy());
202 EXPECT_TRUE(SettingsEqual(settings
));
204 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "b", *byte_value_1_
)->HasError());
205 EXPECT_TRUE(SettingsEqual(settings
));
207 // Back under max keys.
208 EXPECT_FALSE(storage_
->Remove("a")->HasError());
209 settings
.Remove("a", NULL
);
210 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", *byte_value_1_
)->HasError());
211 settings
.Set("b", byte_value_1_
->DeepCopy());
212 EXPECT_TRUE(SettingsEqual(settings
));
215 TEST_F(ExtensionSettingsQuotaTest
, RemovingNonexistentSettings
) {
216 base::DictionaryValue settings
;
217 CreateStorage(36, UINT_MAX
, 3);
220 base::DictionaryValue to_set
;
221 to_set
.Set("b1", byte_value_16_
->DeepCopy());
222 to_set
.Set("b2", byte_value_16_
->DeepCopy());
223 storage_
->Set(DEFAULTS
, to_set
);
224 settings
.Set("b1", byte_value_16_
->DeepCopy());
225 settings
.Set("b2", byte_value_16_
->DeepCopy());
226 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
227 EXPECT_TRUE(SettingsEqual(settings
));
229 // Remove some settings that don't exist.
230 std::vector
<std::string
> to_remove
;
231 to_remove
.push_back("a1");
232 to_remove
.push_back("a2");
233 EXPECT_FALSE(storage_
->Remove(to_remove
)->HasError());
234 EXPECT_FALSE(storage_
->Remove("b")->HasError());
235 EXPECT_TRUE(SettingsEqual(settings
));
238 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
239 EXPECT_TRUE(SettingsEqual(settings
));
241 // Max out key count.
243 to_set
.Set("b1", byte_value_1_
->DeepCopy());
244 to_set
.Set("b2", byte_value_1_
->DeepCopy());
245 storage_
->Set(DEFAULTS
, to_set
);
246 settings
.Set("b1", byte_value_1_
->DeepCopy());
247 settings
.Set("b2", byte_value_1_
->DeepCopy());
248 storage_
->Set(DEFAULTS
, "b3", *byte_value_1_
);
249 settings
.Set("b3", byte_value_1_
->DeepCopy());
250 EXPECT_TRUE(SettingsEqual(settings
));
252 // Remove some settings that don't exist.
254 to_remove
.push_back("a1");
255 to_remove
.push_back("a2");
256 EXPECT_FALSE(storage_
->Remove(to_remove
)->HasError());
257 EXPECT_FALSE(storage_
->Remove("b")->HasError());
258 EXPECT_TRUE(SettingsEqual(settings
));
261 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
262 EXPECT_TRUE(SettingsEqual(settings
));
265 TEST_F(ExtensionSettingsQuotaTest
, Clear
) {
266 base::DictionaryValue settings
;
267 CreateStorage(40, UINT_MAX
, 5);
269 // Test running out of byte quota.
271 base::DictionaryValue to_set
;
272 to_set
.Set("a", byte_value_16_
->DeepCopy());
273 to_set
.Set("b", byte_value_16_
->DeepCopy());
274 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
275 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", *byte_value_16_
)->HasError());
277 EXPECT_FALSE(storage_
->Clear()->HasError());
280 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
281 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", *byte_value_16_
)->HasError());
284 // Test reaching max keys.
287 base::DictionaryValue to_set
;
288 to_set
.Set("a", byte_value_1_
->DeepCopy());
289 to_set
.Set("b", byte_value_1_
->DeepCopy());
290 to_set
.Set("c", byte_value_1_
->DeepCopy());
291 to_set
.Set("d", byte_value_1_
->DeepCopy());
292 to_set
.Set("e", byte_value_1_
->DeepCopy());
293 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
294 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "f", *byte_value_1_
)->HasError());
299 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
300 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "f", *byte_value_1_
)->HasError());
304 TEST_F(ExtensionSettingsQuotaTest
, ChangingUsedBytesWithSet
) {
305 base::DictionaryValue settings
;
306 CreateStorage(20, UINT_MAX
, UINT_MAX
);
308 // Change a setting to make it go over quota.
309 storage_
->Set(DEFAULTS
, "a", *byte_value_16_
);
310 settings
.Set("a", byte_value_16_
->DeepCopy());
311 EXPECT_TRUE(SettingsEqual(settings
));
313 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", *byte_value_256_
)->HasError());
314 EXPECT_TRUE(SettingsEqual(settings
));
316 // Change a setting to reduce usage and room for another setting.
317 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "foobar", *byte_value_1_
)->HasError());
318 storage_
->Set(DEFAULTS
, "a", *byte_value_1_
);
319 settings
.Set("a", byte_value_1_
->DeepCopy());
321 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "foobar", *byte_value_1_
)->HasError());
322 settings
.Set("foobar", byte_value_1_
->DeepCopy());
323 EXPECT_TRUE(SettingsEqual(settings
));
326 TEST_F(ExtensionSettingsQuotaTest
, SetsOnlyEntirelyCompletedWithByteQuota
) {
327 base::DictionaryValue settings
;
328 CreateStorage(40, UINT_MAX
, UINT_MAX
);
330 storage_
->Set(DEFAULTS
, "a", *byte_value_16_
);
331 settings
.Set("a", byte_value_16_
->DeepCopy());
333 // The entire change is over quota.
334 base::DictionaryValue to_set
;
335 to_set
.Set("b", byte_value_16_
->DeepCopy());
336 to_set
.Set("c", byte_value_16_
->DeepCopy());
337 EXPECT_TRUE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
338 EXPECT_TRUE(SettingsEqual(settings
));
340 // The entire change is over quota, but quota reduced in existing key.
341 to_set
.Set("a", byte_value_1_
->DeepCopy());
342 EXPECT_FALSE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
343 settings
.Set("a", byte_value_1_
->DeepCopy());
344 settings
.Set("b", byte_value_16_
->DeepCopy());
345 settings
.Set("c", byte_value_16_
->DeepCopy());
346 EXPECT_TRUE(SettingsEqual(settings
));
349 TEST_F(ExtensionSettingsQuotaTest
, SetsOnlyEntireCompletedWithMaxKeys
) {
350 base::DictionaryValue settings
;
351 CreateStorage(UINT_MAX
, UINT_MAX
, 2);
353 storage_
->Set(DEFAULTS
, "a", *byte_value_1_
);
354 settings
.Set("a", byte_value_1_
->DeepCopy());
356 base::DictionaryValue to_set
;
357 to_set
.Set("b", byte_value_16_
->DeepCopy());
358 to_set
.Set("c", byte_value_16_
->DeepCopy());
359 EXPECT_TRUE(storage_
->Set(DEFAULTS
, to_set
)->HasError());
360 EXPECT_TRUE(SettingsEqual(settings
));
363 TEST_F(ExtensionSettingsQuotaTest
, WithInitialDataAndByteQuota
) {
364 base::DictionaryValue settings
;
365 delegate_
->Set(DEFAULTS
, "a", *byte_value_256_
);
366 settings
.Set("a", byte_value_256_
->DeepCopy());
368 CreateStorage(280, UINT_MAX
, UINT_MAX
);
369 EXPECT_TRUE(SettingsEqual(settings
));
372 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", *byte_value_16_
)->HasError());
373 settings
.Set("b", byte_value_16_
->DeepCopy());
374 EXPECT_TRUE(SettingsEqual(settings
));
377 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", *byte_value_16_
)->HasError());
378 EXPECT_TRUE(SettingsEqual(settings
));
380 // Reduce usage of original setting so that "c" can fit.
381 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", *byte_value_16_
)->HasError());
382 settings
.Set("a", byte_value_16_
->DeepCopy());
383 EXPECT_TRUE(SettingsEqual(settings
));
385 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "c", *byte_value_16_
)->HasError());
386 settings
.Set("c", byte_value_16_
->DeepCopy());
387 EXPECT_TRUE(SettingsEqual(settings
));
389 // Remove to free up some more data.
390 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "d", *byte_value_256_
)->HasError());
392 std::vector
<std::string
> to_remove
;
393 to_remove
.push_back("a");
394 to_remove
.push_back("b");
395 storage_
->Remove(to_remove
);
396 settings
.Remove("a", NULL
);
397 settings
.Remove("b", NULL
);
398 EXPECT_TRUE(SettingsEqual(settings
));
400 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "d", *byte_value_256_
)->HasError());
401 settings
.Set("d", byte_value_256_
->DeepCopy());
402 EXPECT_TRUE(SettingsEqual(settings
));
405 TEST_F(ExtensionSettingsQuotaTest
, WithInitialDataAndMaxKeys
) {
406 base::DictionaryValue settings
;
407 delegate_
->Set(DEFAULTS
, "a", *byte_value_1_
);
408 settings
.Set("a", byte_value_1_
->DeepCopy());
409 CreateStorage(UINT_MAX
, UINT_MAX
, 2);
411 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", *byte_value_1_
)->HasError());
412 settings
.Set("b", byte_value_1_
->DeepCopy());
414 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", *byte_value_1_
)->HasError());
416 EXPECT_TRUE(SettingsEqual(settings
));
419 TEST_F(ExtensionSettingsQuotaTest
, InitiallyOverByteQuota
) {
420 base::DictionaryValue settings
;
421 settings
.Set("a", byte_value_16_
->DeepCopy());
422 settings
.Set("b", byte_value_16_
->DeepCopy());
423 settings
.Set("c", byte_value_16_
->DeepCopy());
424 delegate_
->Set(DEFAULTS
, settings
);
426 CreateStorage(40, UINT_MAX
, UINT_MAX
);
427 EXPECT_TRUE(SettingsEqual(settings
));
429 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "d", *byte_value_16_
)->HasError());
431 // Take under quota by reducing size of an existing setting
432 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
433 settings
.Set("a", byte_value_1_
->DeepCopy());
434 EXPECT_TRUE(SettingsEqual(settings
));
436 // Should be able set another small setting.
437 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "d", *byte_value_1_
)->HasError());
438 settings
.Set("d", byte_value_1_
->DeepCopy());
439 EXPECT_TRUE(SettingsEqual(settings
));
442 TEST_F(ExtensionSettingsQuotaTest
, InitiallyOverMaxKeys
) {
443 base::DictionaryValue settings
;
444 settings
.Set("a", byte_value_16_
->DeepCopy());
445 settings
.Set("b", byte_value_16_
->DeepCopy());
446 settings
.Set("c", byte_value_16_
->DeepCopy());
447 delegate_
->Set(DEFAULTS
, settings
);
449 CreateStorage(UINT_MAX
, UINT_MAX
, 2);
450 EXPECT_TRUE(SettingsEqual(settings
));
452 // Can't set either an existing or new setting.
453 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "d", *byte_value_16_
)->HasError());
454 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
455 EXPECT_TRUE(SettingsEqual(settings
));
457 // Should be able after removing 2.
458 storage_
->Remove("a");
459 settings
.Remove("a", NULL
);
460 storage_
->Remove("b");
461 settings
.Remove("b", NULL
);
462 EXPECT_TRUE(SettingsEqual(settings
));
464 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "e", *byte_value_1_
)->HasError());
465 settings
.Set("e", byte_value_1_
->DeepCopy());
466 EXPECT_TRUE(SettingsEqual(settings
));
468 // Still can't set any.
469 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "d", *byte_value_16_
)->HasError());
470 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
471 EXPECT_TRUE(SettingsEqual(settings
));
474 TEST_F(ExtensionSettingsQuotaTest
, ZeroQuotaBytesPerSetting
) {
475 base::DictionaryValue empty
;
476 CreateStorage(UINT_MAX
, 0, UINT_MAX
);
478 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
479 EXPECT_FALSE(storage_
->Remove("a")->HasError());
480 EXPECT_FALSE(storage_
->Remove("b")->HasError());
481 EXPECT_TRUE(SettingsEqual(empty
));
484 TEST_F(ExtensionSettingsQuotaTest
, QuotaBytesPerSetting
) {
485 base::DictionaryValue settings
;
487 CreateStorage(UINT_MAX
, 20, UINT_MAX
);
489 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
490 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", *byte_value_16_
)->HasError());
491 settings
.Set("a", byte_value_16_
->DeepCopy());
492 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", *byte_value_256_
)->HasError());
494 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", *byte_value_1_
)->HasError());
495 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", *byte_value_16_
)->HasError());
496 settings
.Set("b", byte_value_16_
->DeepCopy());
497 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "b", *byte_value_256_
)->HasError());
499 EXPECT_TRUE(SettingsEqual(settings
));
502 TEST_F(ExtensionSettingsQuotaTest
, QuotaBytesPerSettingWithInitialSettings
) {
503 base::DictionaryValue settings
;
505 delegate_
->Set(DEFAULTS
, "a", *byte_value_1_
);
506 delegate_
->Set(DEFAULTS
, "b", *byte_value_16_
);
507 delegate_
->Set(DEFAULTS
, "c", *byte_value_256_
);
508 CreateStorage(UINT_MAX
, 20, UINT_MAX
);
510 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", *byte_value_1_
)->HasError());
511 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "a", *byte_value_16_
)->HasError());
512 settings
.Set("a", byte_value_16_
->DeepCopy());
513 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "a", *byte_value_256_
)->HasError());
515 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", *byte_value_1_
)->HasError());
516 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "b", *byte_value_16_
)->HasError());
517 settings
.Set("b", byte_value_16_
->DeepCopy());
518 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "b", *byte_value_256_
)->HasError());
520 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "c", *byte_value_1_
)->HasError());
521 EXPECT_FALSE(storage_
->Set(DEFAULTS
, "c", *byte_value_16_
)->HasError());
522 settings
.Set("c", byte_value_16_
->DeepCopy());
523 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", *byte_value_256_
)->HasError());
525 EXPECT_TRUE(SettingsEqual(settings
));
528 TEST_F(ExtensionSettingsQuotaTest
,
529 QuotaBytesPerSettingWithInitialSettingsForced
) {
530 // This is a lazy test to make sure IGNORE_QUOTA lets through changes: the
531 // test above copied, but using IGNORE_QUOTA and asserting nothing is ever
533 base::DictionaryValue settings
;
535 delegate_
->Set(DEFAULTS
, "a", *byte_value_1_
);
536 delegate_
->Set(DEFAULTS
, "b", *byte_value_16_
);
537 delegate_
->Set(DEFAULTS
, "c", *byte_value_256_
);
538 CreateStorage(UINT_MAX
, 20, UINT_MAX
);
540 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "a", *byte_value_1_
)->HasError());
541 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "a", *byte_value_16_
)->HasError());
542 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "a", *byte_value_256_
)->HasError());
543 settings
.Set("a", byte_value_256_
->DeepCopy());
545 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "b", *byte_value_1_
)->HasError());
546 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "b", *byte_value_16_
)->HasError());
547 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "b", *byte_value_256_
)->HasError());
548 settings
.Set("b", byte_value_256_
->DeepCopy());
550 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "c", *byte_value_1_
)->HasError());
551 EXPECT_FALSE(storage_
->Set(IGNORE_QUOTA
, "c", *byte_value_16_
)->HasError());
552 settings
.Set("c", byte_value_16_
->DeepCopy());
554 // ... except the last. Make sure it can still fail.
555 EXPECT_TRUE(storage_
->Set(DEFAULTS
, "c", *byte_value_256_
)->HasError());
557 EXPECT_TRUE(SettingsEqual(settings
));
560 TEST_F(ExtensionSettingsQuotaTest
, GetBytesInUse
) {
561 // Just testing GetBytesInUse, no need for a quota.
562 CreateStorage(UINT_MAX
, UINT_MAX
, UINT_MAX
);
564 std::vector
<std::string
> ab
;
568 EXPECT_EQ(0u, storage_
->GetBytesInUse());
569 EXPECT_EQ(0u, storage_
->GetBytesInUse("a"));
570 EXPECT_EQ(0u, storage_
->GetBytesInUse("b"));
571 EXPECT_EQ(0u, storage_
->GetBytesInUse(ab
));
573 storage_
->Set(DEFAULTS
, "a", *byte_value_1_
);
575 EXPECT_EQ(2u, storage_
->GetBytesInUse());
576 EXPECT_EQ(2u, storage_
->GetBytesInUse("a"));
577 EXPECT_EQ(0u, storage_
->GetBytesInUse("b"));
578 EXPECT_EQ(2u, storage_
->GetBytesInUse(ab
));
580 storage_
->Set(DEFAULTS
, "b", *byte_value_1_
);
582 EXPECT_EQ(4u, storage_
->GetBytesInUse());
583 EXPECT_EQ(2u, storage_
->GetBytesInUse("a"));
584 EXPECT_EQ(2u, storage_
->GetBytesInUse("b"));
585 EXPECT_EQ(4u, storage_
->GetBytesInUse(ab
));
587 storage_
->Set(DEFAULTS
, "c", *byte_value_1_
);
589 EXPECT_EQ(6u, storage_
->GetBytesInUse());
590 EXPECT_EQ(2u, storage_
->GetBytesInUse("a"));
591 EXPECT_EQ(2u, storage_
->GetBytesInUse("b"));
592 EXPECT_EQ(4u, storage_
->GetBytesInUse(ab
));
595 } // namespace extensions