summaryrefslogtreecommitdiffstats
path: root/extensions/browser/api/storage/settings_quota_unittest.cc
blob: 391043e0fbbe9d828a389daf73df87d025d5beac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/json/json_writer.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "extensions/browser/api/storage/settings_storage_quota_enforcer.h"
#include "extensions/browser/value_store/testing_value_store.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::DictionaryValue;
using base::ListValue;
using base::Value;

namespace extensions {

// To save typing ValueStore::DEFAULTS/IGNORE_QUOTA everywhere.
const ValueStore::WriteOptions DEFAULTS = ValueStore::DEFAULTS;
const ValueStore::WriteOptions IGNORE_QUOTA =
    ValueStore::IGNORE_QUOTA;

class ExtensionSettingsQuotaTest : public testing::Test {
 public:
  ExtensionSettingsQuotaTest()
      : byte_value_1_(new base::FundamentalValue(1)),
        byte_value_16_(new base::StringValue("sixteen bytes.")),
        byte_value_256_(new base::ListValue()),
        delegate_(new TestingValueStore()) {
    for (int i = 1; i < 89; ++i) {
      byte_value_256_->Append(new base::FundamentalValue(i));
    }
    ValidateByteValues();
  }

  void ValidateByteValues() {
    std::string validate_sizes;
    base::JSONWriter::Write(byte_value_1_.get(), &validate_sizes);
    ASSERT_EQ(1u, validate_sizes.size());
    base::JSONWriter::Write(byte_value_16_.get(), &validate_sizes);
    ASSERT_EQ(16u, validate_sizes.size());
    base::JSONWriter::Write(byte_value_256_.get(), &validate_sizes);
    ASSERT_EQ(256u, validate_sizes.size());
  }

  void TearDown() override { ASSERT_TRUE(storage_.get() != NULL); }

 protected:
  // Creates |storage_|.  Must only be called once.
  void CreateStorage(
      size_t quota_bytes, size_t quota_bytes_per_item, size_t max_items) {
    ASSERT_TRUE(storage_.get() == NULL);
    SettingsStorageQuotaEnforcer::Limits limits =
        { quota_bytes, quota_bytes_per_item, max_items };
    storage_.reset(new SettingsStorageQuotaEnforcer(limits, delegate_));
  }

  // Returns whether the settings in |storage_| and |delegate_| are the same as
  // |settings|.
  bool SettingsEqual(const base::DictionaryValue& settings) {
    return settings.Equals(&storage_->Get()->settings()) &&
           settings.Equals(&delegate_->Get()->settings());
  }

  // Values with different serialized sizes.
  scoped_ptr<base::Value> byte_value_1_;
  scoped_ptr<base::Value> byte_value_16_;
  scoped_ptr<base::ListValue> byte_value_256_;

  // Quota enforcing storage area being tested.
  scoped_ptr<SettingsStorageQuotaEnforcer> storage_;

  // In-memory storage area being delegated to.  Always owned by |storage_|.
  TestingValueStore* delegate_;
};

TEST_F(ExtensionSettingsQuotaTest, ZeroQuotaBytes) {
  base::DictionaryValue empty;
  CreateStorage(0, UINT_MAX, UINT_MAX);

  EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  EXPECT_FALSE(storage_->Remove("a")->HasError());
  EXPECT_FALSE(storage_->Remove("b")->HasError());
  EXPECT_TRUE(SettingsEqual(empty));
}

TEST_F(ExtensionSettingsQuotaTest, KeySizeTakenIntoAccount) {
  base::DictionaryValue empty;
  CreateStorage(8u, UINT_MAX, UINT_MAX);
  EXPECT_TRUE(
      storage_->Set(DEFAULTS, "Really long key", *byte_value_1_)->HasError());
  EXPECT_TRUE(SettingsEqual(empty));
}

TEST_F(ExtensionSettingsQuotaTest, SmallByteQuota) {
  base::DictionaryValue settings;
  CreateStorage(8u, UINT_MAX, UINT_MAX);

  EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  settings.Set("a", byte_value_1_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError());
  EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, MediumByteQuota) {
  base::DictionaryValue settings;
  CreateStorage(40, UINT_MAX, UINT_MAX);

  base::DictionaryValue to_set;
  to_set.Set("a", byte_value_1_->DeepCopy());
  to_set.Set("b", byte_value_16_->DeepCopy());
  EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
  settings.Set("a", byte_value_1_->DeepCopy());
  settings.Set("b", byte_value_16_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  // Should be able to set value to other under-quota value.
  to_set.Set("a", byte_value_16_->DeepCopy());
  EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
  settings.Set("a", byte_value_16_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, ZeroMaxKeys) {
  base::DictionaryValue empty;
  CreateStorage(UINT_MAX, UINT_MAX, 0);

  EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  EXPECT_FALSE(storage_->Remove("a")->HasError());
  EXPECT_FALSE(storage_->Remove("b")->HasError());
  EXPECT_TRUE(SettingsEqual(empty));
}

TEST_F(ExtensionSettingsQuotaTest, SmallMaxKeys) {
  base::DictionaryValue settings;
  CreateStorage(UINT_MAX, UINT_MAX, 1);

  EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  settings.Set("a", byte_value_1_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  // Should be able to set existing key to other value without going over quota.
  EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_16_)->HasError());
  settings.Set("a", byte_value_16_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError());
  EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, MediumMaxKeys) {
  base::DictionaryValue settings;
  CreateStorage(UINT_MAX, UINT_MAX, 2);

  base::DictionaryValue to_set;
  to_set.Set("a", byte_value_1_->DeepCopy());
  to_set.Set("b", byte_value_16_->DeepCopy());
  EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
  settings.Set("a", byte_value_1_->DeepCopy());
  settings.Set("b", byte_value_16_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  // Should be able to set existing keys to other values without going over
  // quota.
  to_set.Set("a", byte_value_16_->DeepCopy());
  EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
  settings.Set("a", byte_value_16_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, RemovingExistingSettings) {
  base::DictionaryValue settings;
  CreateStorage(266, UINT_MAX, 2);

  storage_->Set(DEFAULTS, "b", *byte_value_16_);
  settings.Set("b", byte_value_16_->DeepCopy());
  // Not enough quota.
  storage_->Set(DEFAULTS, "c", *byte_value_256_);
  EXPECT_TRUE(SettingsEqual(settings));

  // Try again with "b" removed, enough quota.
  EXPECT_FALSE(storage_->Remove("b")->HasError());
  settings.Remove("b", NULL);
  EXPECT_FALSE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());
  settings.Set("c", byte_value_256_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  // Enough byte quota but max keys not high enough.
  EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  settings.Set("a", byte_value_1_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));

  // Back under max keys.
  EXPECT_FALSE(storage_->Remove("a")->HasError());
  settings.Remove("a", NULL);
  EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError());
  settings.Set("b", byte_value_1_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, RemovingNonexistentSettings) {
  base::DictionaryValue settings;
  CreateStorage(36, UINT_MAX, 3);

  // Max out bytes.
  base::DictionaryValue to_set;
  to_set.Set("b1", byte_value_16_->DeepCopy());
  to_set.Set("b2", byte_value_16_->DeepCopy());
  storage_->Set(DEFAULTS, to_set);
  settings.Set("b1", byte_value_16_->DeepCopy());
  settings.Set("b2", byte_value_16_->DeepCopy());
  EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));

  // Remove some settings that don't exist.
  std::vector<std::string> to_remove;
  to_remove.push_back("a1");
  to_remove.push_back("a2");
  EXPECT_FALSE(storage_->Remove(to_remove)->HasError());
  EXPECT_FALSE(storage_->Remove("b")->HasError());
  EXPECT_TRUE(SettingsEqual(settings));

  // Still no quota.
  EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));

  // Max out key count.
  to_set.Clear();
  to_set.Set("b1", byte_value_1_->DeepCopy());
  to_set.Set("b2", byte_value_1_->DeepCopy());
  storage_->Set(DEFAULTS, to_set);
  settings.Set("b1", byte_value_1_->DeepCopy());
  settings.Set("b2", byte_value_1_->DeepCopy());
  storage_->Set(DEFAULTS, "b3", *byte_value_1_);
  settings.Set("b3", byte_value_1_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  // Remove some settings that don't exist.
  to_remove.clear();
  to_remove.push_back("a1");
  to_remove.push_back("a2");
  EXPECT_FALSE(storage_->Remove(to_remove)->HasError());
  EXPECT_FALSE(storage_->Remove("b")->HasError());
  EXPECT_TRUE(SettingsEqual(settings));

  // Still no quota.
  EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, Clear) {
  base::DictionaryValue settings;
  CreateStorage(40, UINT_MAX, 5);

  // Test running out of byte quota.
  {
    base::DictionaryValue to_set;
    to_set.Set("a", byte_value_16_->DeepCopy());
    to_set.Set("b", byte_value_16_->DeepCopy());
    EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
    EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError());

    EXPECT_FALSE(storage_->Clear()->HasError());

    // (repeat)
    EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
    EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError());
  }

  // Test reaching max keys.
  storage_->Clear();
  {
    base::DictionaryValue to_set;
    to_set.Set("a", byte_value_1_->DeepCopy());
    to_set.Set("b", byte_value_1_->DeepCopy());
    to_set.Set("c", byte_value_1_->DeepCopy());
    to_set.Set("d", byte_value_1_->DeepCopy());
    to_set.Set("e", byte_value_1_->DeepCopy());
    EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
    EXPECT_TRUE(storage_->Set(DEFAULTS, "f", *byte_value_1_)->HasError());

    storage_->Clear();

    // (repeat)
    EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
    EXPECT_TRUE(storage_->Set(DEFAULTS, "f", *byte_value_1_)->HasError());
  }
}

TEST_F(ExtensionSettingsQuotaTest, ChangingUsedBytesWithSet) {
  base::DictionaryValue settings;
  CreateStorage(20, UINT_MAX, UINT_MAX);

  // Change a setting to make it go over quota.
  storage_->Set(DEFAULTS, "a", *byte_value_16_);
  settings.Set("a", byte_value_16_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_256_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));

  // Change a setting to reduce usage and room for another setting.
  EXPECT_TRUE(storage_->Set(DEFAULTS, "foobar", *byte_value_1_)->HasError());
  storage_->Set(DEFAULTS, "a", *byte_value_1_);
  settings.Set("a", byte_value_1_->DeepCopy());

  EXPECT_FALSE(storage_->Set(DEFAULTS, "foobar", *byte_value_1_)->HasError());
  settings.Set("foobar", byte_value_1_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, SetsOnlyEntirelyCompletedWithByteQuota) {
  base::DictionaryValue settings;
  CreateStorage(40, UINT_MAX, UINT_MAX);

  storage_->Set(DEFAULTS, "a", *byte_value_16_);
  settings.Set("a", byte_value_16_->DeepCopy());

  // The entire change is over quota.
  base::DictionaryValue to_set;
  to_set.Set("b", byte_value_16_->DeepCopy());
  to_set.Set("c", byte_value_16_->DeepCopy());
  EXPECT_TRUE(storage_->Set(DEFAULTS, to_set)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));

  // The entire change is over quota, but quota reduced in existing key.
  to_set.Set("a", byte_value_1_->DeepCopy());
  EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError());
  settings.Set("a", byte_value_1_->DeepCopy());
  settings.Set("b", byte_value_16_->DeepCopy());
  settings.Set("c", byte_value_16_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, SetsOnlyEntireCompletedWithMaxKeys) {
  base::DictionaryValue settings;
  CreateStorage(UINT_MAX, UINT_MAX, 2);

  storage_->Set(DEFAULTS, "a", *byte_value_1_);
  settings.Set("a", byte_value_1_->DeepCopy());

  base::DictionaryValue to_set;
  to_set.Set("b", byte_value_16_->DeepCopy());
  to_set.Set("c", byte_value_16_->DeepCopy());
  EXPECT_TRUE(storage_->Set(DEFAULTS, to_set)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, WithInitialDataAndByteQuota) {
  base::DictionaryValue settings;
  delegate_->Set(DEFAULTS, "a", *byte_value_256_);
  settings.Set("a", byte_value_256_->DeepCopy());

  CreateStorage(280, UINT_MAX, UINT_MAX);
  EXPECT_TRUE(SettingsEqual(settings));

  // Add some data.
  EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError());
  settings.Set("b", byte_value_16_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  // Not enough quota.
  EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));

  // Reduce usage of original setting so that "c" can fit.
  EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_16_)->HasError());
  settings.Set("a", byte_value_16_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  EXPECT_FALSE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError());
  settings.Set("c", byte_value_16_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  // Remove to free up some more data.
  EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_256_)->HasError());

  std::vector<std::string> to_remove;
  to_remove.push_back("a");
  to_remove.push_back("b");
  storage_->Remove(to_remove);
  settings.Remove("a", NULL);
  settings.Remove("b", NULL);
  EXPECT_TRUE(SettingsEqual(settings));

  EXPECT_FALSE(storage_->Set(DEFAULTS, "d", *byte_value_256_)->HasError());
  settings.Set("d", byte_value_256_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, WithInitialDataAndMaxKeys) {
  base::DictionaryValue settings;
  delegate_->Set(DEFAULTS, "a", *byte_value_1_);
  settings.Set("a", byte_value_1_->DeepCopy());
  CreateStorage(UINT_MAX, UINT_MAX, 2);

  EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError());
  settings.Set("b", byte_value_1_->DeepCopy());

  EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_1_)->HasError());

  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, InitiallyOverByteQuota) {
  base::DictionaryValue settings;
  settings.Set("a", byte_value_16_->DeepCopy());
  settings.Set("b", byte_value_16_->DeepCopy());
  settings.Set("c", byte_value_16_->DeepCopy());
  delegate_->Set(DEFAULTS, settings);

  CreateStorage(40, UINT_MAX, UINT_MAX);
  EXPECT_TRUE(SettingsEqual(settings));

  EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_16_)->HasError());

  // Take under quota by reducing size of an existing setting
  EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  settings.Set("a", byte_value_1_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  // Should be able set another small setting.
  EXPECT_FALSE(storage_->Set(DEFAULTS, "d", *byte_value_1_)->HasError());
  settings.Set("d", byte_value_1_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, InitiallyOverMaxKeys) {
  base::DictionaryValue settings;
  settings.Set("a", byte_value_16_->DeepCopy());
  settings.Set("b", byte_value_16_->DeepCopy());
  settings.Set("c", byte_value_16_->DeepCopy());
  delegate_->Set(DEFAULTS, settings);

  CreateStorage(UINT_MAX, UINT_MAX, 2);
  EXPECT_TRUE(SettingsEqual(settings));

  // Can't set either an existing or new setting.
  EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_16_)->HasError());
  EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));

  // Should be able after removing 2.
  storage_->Remove("a");
  settings.Remove("a", NULL);
  storage_->Remove("b");
  settings.Remove("b", NULL);
  EXPECT_TRUE(SettingsEqual(settings));

  EXPECT_FALSE(storage_->Set(DEFAULTS, "e", *byte_value_1_)->HasError());
  settings.Set("e", byte_value_1_->DeepCopy());
  EXPECT_TRUE(SettingsEqual(settings));

  // Still can't set any.
  EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_16_)->HasError());
  EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, ZeroQuotaBytesPerSetting) {
  base::DictionaryValue empty;
  CreateStorage(UINT_MAX, 0, UINT_MAX);

  EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  EXPECT_FALSE(storage_->Remove("a")->HasError());
  EXPECT_FALSE(storage_->Remove("b")->HasError());
  EXPECT_TRUE(SettingsEqual(empty));
}

TEST_F(ExtensionSettingsQuotaTest, QuotaBytesPerSetting) {
  base::DictionaryValue settings;

  CreateStorage(UINT_MAX, 20, UINT_MAX);

  EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_16_)->HasError());
  settings.Set("a", byte_value_16_->DeepCopy());
  EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_256_)->HasError());

  EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError());
  EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError());
  settings.Set("b", byte_value_16_->DeepCopy());
  EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_256_)->HasError());

  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, QuotaBytesPerSettingWithInitialSettings) {
  base::DictionaryValue settings;

  delegate_->Set(DEFAULTS, "a", *byte_value_1_);
  delegate_->Set(DEFAULTS, "b", *byte_value_16_);
  delegate_->Set(DEFAULTS, "c", *byte_value_256_);
  CreateStorage(UINT_MAX, 20, UINT_MAX);

  EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError());
  EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_16_)->HasError());
  settings.Set("a", byte_value_16_->DeepCopy());
  EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_256_)->HasError());

  EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError());
  EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError());
  settings.Set("b", byte_value_16_->DeepCopy());
  EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_256_)->HasError());

  EXPECT_FALSE(storage_->Set(DEFAULTS, "c", *byte_value_1_)->HasError());
  EXPECT_FALSE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError());
  settings.Set("c", byte_value_16_->DeepCopy());
  EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());

  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest,
    QuotaBytesPerSettingWithInitialSettingsForced) {
  // This is a lazy test to make sure IGNORE_QUOTA lets through changes: the
  // test above copied, but using IGNORE_QUOTA and asserting nothing is ever
  // rejected...
  base::DictionaryValue settings;

  delegate_->Set(DEFAULTS, "a", *byte_value_1_);
  delegate_->Set(DEFAULTS, "b", *byte_value_16_);
  delegate_->Set(DEFAULTS, "c", *byte_value_256_);
  CreateStorage(UINT_MAX, 20, UINT_MAX);

  EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "a", *byte_value_1_)->HasError());
  EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "a", *byte_value_16_)->HasError());
  EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "a", *byte_value_256_)->HasError());
  settings.Set("a", byte_value_256_->DeepCopy());

  EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "b", *byte_value_1_)->HasError());
  EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "b", *byte_value_16_)->HasError());
  EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "b", *byte_value_256_)->HasError());
  settings.Set("b", byte_value_256_->DeepCopy());

  EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "c", *byte_value_1_)->HasError());
  EXPECT_FALSE(storage_->Set(IGNORE_QUOTA, "c", *byte_value_16_)->HasError());
  settings.Set("c", byte_value_16_->DeepCopy());

  // ... except the last.  Make sure it can still fail.
  EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError());

  EXPECT_TRUE(SettingsEqual(settings));
}

TEST_F(ExtensionSettingsQuotaTest, GetBytesInUse) {
  // Just testing GetBytesInUse, no need for a quota.
  CreateStorage(UINT_MAX, UINT_MAX, UINT_MAX);

  std::vector<std::string> ab;
  ab.push_back("a");
  ab.push_back("b");

  EXPECT_EQ(0u, storage_->GetBytesInUse());
  EXPECT_EQ(0u, storage_->GetBytesInUse("a"));
  EXPECT_EQ(0u, storage_->GetBytesInUse("b"));
  EXPECT_EQ(0u, storage_->GetBytesInUse(ab));

  storage_->Set(DEFAULTS, "a", *byte_value_1_);

  EXPECT_EQ(2u, storage_->GetBytesInUse());
  EXPECT_EQ(2u, storage_->GetBytesInUse("a"));
  EXPECT_EQ(0u, storage_->GetBytesInUse("b"));
  EXPECT_EQ(2u, storage_->GetBytesInUse(ab));

  storage_->Set(DEFAULTS, "b", *byte_value_1_);

  EXPECT_EQ(4u, storage_->GetBytesInUse());
  EXPECT_EQ(2u, storage_->GetBytesInUse("a"));
  EXPECT_EQ(2u, storage_->GetBytesInUse("b"));
  EXPECT_EQ(4u, storage_->GetBytesInUse(ab));

  storage_->Set(DEFAULTS, "c", *byte_value_1_);

  EXPECT_EQ(6u, storage_->GetBytesInUse());
  EXPECT_EQ(2u, storage_->GetBytesInUse("a"));
  EXPECT_EQ(2u, storage_->GetBytesInUse("b"));
  EXPECT_EQ(4u, storage_->GetBytesInUse(ab));
}

}  // namespace extensions