OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
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 "chrome/browser/extensions/api/storage/settings_backend.h" | |
10 #include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer.
h" | |
11 #include "chrome/browser/value_store/testing_value_store.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace extensions { | |
15 | |
16 // To save typing ValueStore::DEFAULTS/IGNORE_QUOTA everywhere. | |
17 const ValueStore::WriteOptions DEFAULTS = ValueStore::DEFAULTS; | |
18 const ValueStore::WriteOptions IGNORE_QUOTA = | |
19 ValueStore::IGNORE_QUOTA; | |
20 | |
21 class ExtensionSettingsQuotaTest : public testing::Test { | |
22 public: | |
23 ExtensionSettingsQuotaTest() | |
24 : byte_value_1_(Value::CreateIntegerValue(1)), | |
25 byte_value_16_(Value::CreateStringValue("sixteen bytes.")), | |
26 byte_value_256_(new ListValue()), | |
27 delegate_(new TestingValueStore()) { | |
28 for (int i = 1; i < 89; ++i) { | |
29 byte_value_256_->Append(Value::CreateIntegerValue(i)); | |
30 } | |
31 ValidateByteValues(); | |
32 } | |
33 | |
34 void ValidateByteValues() { | |
35 std::string validate_sizes; | |
36 base::JSONWriter::Write(byte_value_1_.get(), &validate_sizes); | |
37 ASSERT_EQ(1u, validate_sizes.size()); | |
38 base::JSONWriter::Write(byte_value_16_.get(), &validate_sizes); | |
39 ASSERT_EQ(16u, validate_sizes.size()); | |
40 base::JSONWriter::Write(byte_value_256_.get(), &validate_sizes); | |
41 ASSERT_EQ(256u, validate_sizes.size()); | |
42 } | |
43 | |
44 virtual void TearDown() OVERRIDE { | |
45 ASSERT_TRUE(storage_.get() != NULL); | |
46 } | |
47 | |
48 protected: | |
49 // Creates |storage_|. Must only be called once. | |
50 void CreateStorage( | |
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_)); | |
56 } | |
57 | |
58 // Returns whether the settings in |storage_| and |delegate_| are the same as | |
59 // |settings|. | |
60 bool SettingsEqual(const DictionaryValue& settings) { | |
61 return settings.Equals(storage_->Get()->settings().get()) && | |
62 settings.Equals(delegate_->Get()->settings().get()); | |
63 } | |
64 | |
65 // Values with different serialized sizes. | |
66 scoped_ptr<Value> byte_value_1_; | |
67 scoped_ptr<Value> byte_value_16_; | |
68 scoped_ptr<ListValue> byte_value_256_; | |
69 | |
70 // Quota enforcing storage area being tested. | |
71 scoped_ptr<SettingsStorageQuotaEnforcer> storage_; | |
72 | |
73 // In-memory storage area being delegated to. Always owned by |storage_|. | |
74 TestingValueStore* delegate_; | |
75 }; | |
76 | |
77 TEST_F(ExtensionSettingsQuotaTest, ZeroQuotaBytes) { | |
78 DictionaryValue empty; | |
79 CreateStorage(0, UINT_MAX, UINT_MAX); | |
80 | |
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)); | |
85 } | |
86 | |
87 TEST_F(ExtensionSettingsQuotaTest, KeySizeTakenIntoAccount) { | |
88 DictionaryValue empty; | |
89 CreateStorage(8u, UINT_MAX, UINT_MAX); | |
90 EXPECT_TRUE( | |
91 storage_->Set(DEFAULTS, "Really long key", *byte_value_1_)->HasError()); | |
92 EXPECT_TRUE(SettingsEqual(empty)); | |
93 } | |
94 | |
95 TEST_F(ExtensionSettingsQuotaTest, SmallByteQuota) { | |
96 DictionaryValue settings; | |
97 CreateStorage(8u, UINT_MAX, UINT_MAX); | |
98 | |
99 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError()); | |
100 settings.Set("a", byte_value_1_->DeepCopy()); | |
101 EXPECT_TRUE(SettingsEqual(settings)); | |
102 | |
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)); | |
106 } | |
107 | |
108 TEST_F(ExtensionSettingsQuotaTest, MediumByteQuota) { | |
109 DictionaryValue settings; | |
110 CreateStorage(40, UINT_MAX, UINT_MAX); | |
111 | |
112 DictionaryValue to_set; | |
113 to_set.Set("a", byte_value_1_->DeepCopy()); | |
114 to_set.Set("b", byte_value_16_->DeepCopy()); | |
115 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
116 settings.Set("a", byte_value_1_->DeepCopy()); | |
117 settings.Set("b", byte_value_16_->DeepCopy()); | |
118 EXPECT_TRUE(SettingsEqual(settings)); | |
119 | |
120 // Should be able to set value to other under-quota value. | |
121 to_set.Set("a", byte_value_16_->DeepCopy()); | |
122 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
123 settings.Set("a", byte_value_16_->DeepCopy()); | |
124 EXPECT_TRUE(SettingsEqual(settings)); | |
125 | |
126 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError()); | |
127 EXPECT_TRUE(SettingsEqual(settings)); | |
128 } | |
129 | |
130 TEST_F(ExtensionSettingsQuotaTest, ZeroMaxKeys) { | |
131 DictionaryValue empty; | |
132 CreateStorage(UINT_MAX, UINT_MAX, 0); | |
133 | |
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)); | |
138 } | |
139 | |
140 TEST_F(ExtensionSettingsQuotaTest, SmallMaxKeys) { | |
141 DictionaryValue settings; | |
142 CreateStorage(UINT_MAX, UINT_MAX, 1); | |
143 | |
144 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError()); | |
145 settings.Set("a", byte_value_1_->DeepCopy()); | |
146 EXPECT_TRUE(SettingsEqual(settings)); | |
147 | |
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_->DeepCopy()); | |
151 EXPECT_TRUE(SettingsEqual(settings)); | |
152 | |
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)); | |
156 } | |
157 | |
158 TEST_F(ExtensionSettingsQuotaTest, MediumMaxKeys) { | |
159 DictionaryValue settings; | |
160 CreateStorage(UINT_MAX, UINT_MAX, 2); | |
161 | |
162 DictionaryValue to_set; | |
163 to_set.Set("a", byte_value_1_->DeepCopy()); | |
164 to_set.Set("b", byte_value_16_->DeepCopy()); | |
165 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
166 settings.Set("a", byte_value_1_->DeepCopy()); | |
167 settings.Set("b", byte_value_16_->DeepCopy()); | |
168 EXPECT_TRUE(SettingsEqual(settings)); | |
169 | |
170 // Should be able to set existing keys to other values without going over | |
171 // quota. | |
172 to_set.Set("a", byte_value_16_->DeepCopy()); | |
173 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
174 settings.Set("a", byte_value_16_->DeepCopy()); | |
175 EXPECT_TRUE(SettingsEqual(settings)); | |
176 | |
177 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError()); | |
178 EXPECT_TRUE(SettingsEqual(settings)); | |
179 } | |
180 | |
181 TEST_F(ExtensionSettingsQuotaTest, RemovingExistingSettings) { | |
182 DictionaryValue settings; | |
183 CreateStorage(266, UINT_MAX, 2); | |
184 | |
185 storage_->Set(DEFAULTS, "b", *byte_value_16_); | |
186 settings.Set("b", byte_value_16_->DeepCopy()); | |
187 // Not enough quota. | |
188 storage_->Set(DEFAULTS, "c", *byte_value_256_); | |
189 EXPECT_TRUE(SettingsEqual(settings)); | |
190 | |
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_->DeepCopy()); | |
196 EXPECT_TRUE(SettingsEqual(settings)); | |
197 | |
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_->DeepCopy()); | |
201 EXPECT_TRUE(SettingsEqual(settings)); | |
202 | |
203 EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError()); | |
204 EXPECT_TRUE(SettingsEqual(settings)); | |
205 | |
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_->DeepCopy()); | |
211 EXPECT_TRUE(SettingsEqual(settings)); | |
212 } | |
213 | |
214 TEST_F(ExtensionSettingsQuotaTest, RemovingNonexistentSettings) { | |
215 DictionaryValue settings; | |
216 CreateStorage(36, UINT_MAX, 3); | |
217 | |
218 // Max out bytes. | |
219 DictionaryValue to_set; | |
220 to_set.Set("b1", byte_value_16_->DeepCopy()); | |
221 to_set.Set("b2", byte_value_16_->DeepCopy()); | |
222 storage_->Set(DEFAULTS, to_set); | |
223 settings.Set("b1", byte_value_16_->DeepCopy()); | |
224 settings.Set("b2", byte_value_16_->DeepCopy()); | |
225 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError()); | |
226 EXPECT_TRUE(SettingsEqual(settings)); | |
227 | |
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)); | |
235 | |
236 // Still no quota. | |
237 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError()); | |
238 EXPECT_TRUE(SettingsEqual(settings)); | |
239 | |
240 // Max out key count. | |
241 to_set.Clear(); | |
242 to_set.Set("b1", byte_value_1_->DeepCopy()); | |
243 to_set.Set("b2", byte_value_1_->DeepCopy()); | |
244 storage_->Set(DEFAULTS, to_set); | |
245 settings.Set("b1", byte_value_1_->DeepCopy()); | |
246 settings.Set("b2", byte_value_1_->DeepCopy()); | |
247 storage_->Set(DEFAULTS, "b3", *byte_value_1_); | |
248 settings.Set("b3", byte_value_1_->DeepCopy()); | |
249 EXPECT_TRUE(SettingsEqual(settings)); | |
250 | |
251 // Remove some settings that don't exist. | |
252 to_remove.clear(); | |
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)); | |
258 | |
259 // Still no quota. | |
260 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError()); | |
261 EXPECT_TRUE(SettingsEqual(settings)); | |
262 } | |
263 | |
264 TEST_F(ExtensionSettingsQuotaTest, Clear) { | |
265 DictionaryValue settings; | |
266 CreateStorage(40, UINT_MAX, 5); | |
267 | |
268 // Test running out of byte quota. | |
269 { | |
270 DictionaryValue to_set; | |
271 to_set.Set("a", byte_value_16_->DeepCopy()); | |
272 to_set.Set("b", byte_value_16_->DeepCopy()); | |
273 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
274 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError()); | |
275 | |
276 EXPECT_FALSE(storage_->Clear()->HasError()); | |
277 | |
278 // (repeat) | |
279 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
280 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError()); | |
281 } | |
282 | |
283 // Test reaching max keys. | |
284 storage_->Clear(); | |
285 { | |
286 DictionaryValue to_set; | |
287 to_set.Set("a", byte_value_1_->DeepCopy()); | |
288 to_set.Set("b", byte_value_1_->DeepCopy()); | |
289 to_set.Set("c", byte_value_1_->DeepCopy()); | |
290 to_set.Set("d", byte_value_1_->DeepCopy()); | |
291 to_set.Set("e", byte_value_1_->DeepCopy()); | |
292 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
293 EXPECT_TRUE(storage_->Set(DEFAULTS, "f", *byte_value_1_)->HasError()); | |
294 | |
295 storage_->Clear(); | |
296 | |
297 // (repeat) | |
298 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
299 EXPECT_TRUE(storage_->Set(DEFAULTS, "f", *byte_value_1_)->HasError()); | |
300 } | |
301 } | |
302 | |
303 TEST_F(ExtensionSettingsQuotaTest, ChangingUsedBytesWithSet) { | |
304 DictionaryValue settings; | |
305 CreateStorage(20, UINT_MAX, UINT_MAX); | |
306 | |
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_->DeepCopy()); | |
310 EXPECT_TRUE(SettingsEqual(settings)); | |
311 | |
312 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_256_)->HasError()); | |
313 EXPECT_TRUE(SettingsEqual(settings)); | |
314 | |
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_->DeepCopy()); | |
319 | |
320 EXPECT_FALSE(storage_->Set(DEFAULTS, "foobar", *byte_value_1_)->HasError()); | |
321 settings.Set("foobar", byte_value_1_->DeepCopy()); | |
322 EXPECT_TRUE(SettingsEqual(settings)); | |
323 } | |
324 | |
325 TEST_F(ExtensionSettingsQuotaTest, SetsOnlyEntirelyCompletedWithByteQuota) { | |
326 DictionaryValue settings; | |
327 CreateStorage(40, UINT_MAX, UINT_MAX); | |
328 | |
329 storage_->Set(DEFAULTS, "a", *byte_value_16_); | |
330 settings.Set("a", byte_value_16_->DeepCopy()); | |
331 | |
332 // The entire change is over quota. | |
333 DictionaryValue to_set; | |
334 to_set.Set("b", byte_value_16_->DeepCopy()); | |
335 to_set.Set("c", byte_value_16_->DeepCopy()); | |
336 EXPECT_TRUE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
337 EXPECT_TRUE(SettingsEqual(settings)); | |
338 | |
339 // The entire change is over quota, but quota reduced in existing key. | |
340 to_set.Set("a", byte_value_1_->DeepCopy()); | |
341 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
342 settings.Set("a", byte_value_1_->DeepCopy()); | |
343 settings.Set("b", byte_value_16_->DeepCopy()); | |
344 settings.Set("c", byte_value_16_->DeepCopy()); | |
345 EXPECT_TRUE(SettingsEqual(settings)); | |
346 } | |
347 | |
348 TEST_F(ExtensionSettingsQuotaTest, SetsOnlyEntireCompletedWithMaxKeys) { | |
349 DictionaryValue settings; | |
350 CreateStorage(UINT_MAX, UINT_MAX, 2); | |
351 | |
352 storage_->Set(DEFAULTS, "a", *byte_value_1_); | |
353 settings.Set("a", byte_value_1_->DeepCopy()); | |
354 | |
355 DictionaryValue to_set; | |
356 to_set.Set("b", byte_value_16_->DeepCopy()); | |
357 to_set.Set("c", byte_value_16_->DeepCopy()); | |
358 EXPECT_TRUE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
359 EXPECT_TRUE(SettingsEqual(settings)); | |
360 } | |
361 | |
362 TEST_F(ExtensionSettingsQuotaTest, WithInitialDataAndByteQuota) { | |
363 DictionaryValue settings; | |
364 delegate_->Set(DEFAULTS, "a", *byte_value_256_); | |
365 settings.Set("a", byte_value_256_->DeepCopy()); | |
366 | |
367 CreateStorage(280, UINT_MAX, UINT_MAX); | |
368 EXPECT_TRUE(SettingsEqual(settings)); | |
369 | |
370 // Add some data. | |
371 EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError()); | |
372 settings.Set("b", byte_value_16_->DeepCopy()); | |
373 EXPECT_TRUE(SettingsEqual(settings)); | |
374 | |
375 // Not enough quota. | |
376 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError()); | |
377 EXPECT_TRUE(SettingsEqual(settings)); | |
378 | |
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_->DeepCopy()); | |
382 EXPECT_TRUE(SettingsEqual(settings)); | |
383 | |
384 EXPECT_FALSE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError()); | |
385 settings.Set("c", byte_value_16_->DeepCopy()); | |
386 EXPECT_TRUE(SettingsEqual(settings)); | |
387 | |
388 // Remove to free up some more data. | |
389 EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_256_)->HasError()); | |
390 | |
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)); | |
398 | |
399 EXPECT_FALSE(storage_->Set(DEFAULTS, "d", *byte_value_256_)->HasError()); | |
400 settings.Set("d", byte_value_256_->DeepCopy()); | |
401 EXPECT_TRUE(SettingsEqual(settings)); | |
402 } | |
403 | |
404 TEST_F(ExtensionSettingsQuotaTest, WithInitialDataAndMaxKeys) { | |
405 DictionaryValue settings; | |
406 delegate_->Set(DEFAULTS, "a", *byte_value_1_); | |
407 settings.Set("a", byte_value_1_->DeepCopy()); | |
408 CreateStorage(UINT_MAX, UINT_MAX, 2); | |
409 | |
410 EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError()); | |
411 settings.Set("b", byte_value_1_->DeepCopy()); | |
412 | |
413 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_1_)->HasError()); | |
414 | |
415 EXPECT_TRUE(SettingsEqual(settings)); | |
416 } | |
417 | |
418 TEST_F(ExtensionSettingsQuotaTest, InitiallyOverByteQuota) { | |
419 DictionaryValue settings; | |
420 settings.Set("a", byte_value_16_->DeepCopy()); | |
421 settings.Set("b", byte_value_16_->DeepCopy()); | |
422 settings.Set("c", byte_value_16_->DeepCopy()); | |
423 delegate_->Set(DEFAULTS, settings); | |
424 | |
425 CreateStorage(40, UINT_MAX, UINT_MAX); | |
426 EXPECT_TRUE(SettingsEqual(settings)); | |
427 | |
428 EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_16_)->HasError()); | |
429 | |
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_->DeepCopy()); | |
433 EXPECT_TRUE(SettingsEqual(settings)); | |
434 | |
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_->DeepCopy()); | |
438 EXPECT_TRUE(SettingsEqual(settings)); | |
439 } | |
440 | |
441 TEST_F(ExtensionSettingsQuotaTest, InitiallyOverMaxKeys) { | |
442 DictionaryValue settings; | |
443 settings.Set("a", byte_value_16_->DeepCopy()); | |
444 settings.Set("b", byte_value_16_->DeepCopy()); | |
445 settings.Set("c", byte_value_16_->DeepCopy()); | |
446 delegate_->Set(DEFAULTS, settings); | |
447 | |
448 CreateStorage(UINT_MAX, UINT_MAX, 2); | |
449 EXPECT_TRUE(SettingsEqual(settings)); | |
450 | |
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)); | |
455 | |
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)); | |
462 | |
463 EXPECT_FALSE(storage_->Set(DEFAULTS, "e", *byte_value_1_)->HasError()); | |
464 settings.Set("e", byte_value_1_->DeepCopy()); | |
465 EXPECT_TRUE(SettingsEqual(settings)); | |
466 | |
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)); | |
471 } | |
472 | |
473 TEST_F(ExtensionSettingsQuotaTest, ZeroQuotaBytesPerSetting) { | |
474 DictionaryValue empty; | |
475 CreateStorage(UINT_MAX, 0, UINT_MAX); | |
476 | |
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)); | |
481 } | |
482 | |
483 TEST_F(ExtensionSettingsQuotaTest, QuotaBytesPerSetting) { | |
484 DictionaryValue settings; | |
485 | |
486 CreateStorage(UINT_MAX, 20, UINT_MAX); | |
487 | |
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_->DeepCopy()); | |
491 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_256_)->HasError()); | |
492 | |
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_->DeepCopy()); | |
496 EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_256_)->HasError()); | |
497 | |
498 EXPECT_TRUE(SettingsEqual(settings)); | |
499 } | |
500 | |
501 TEST_F(ExtensionSettingsQuotaTest, QuotaBytesPerSettingWithInitialSettings) { | |
502 DictionaryValue settings; | |
503 | |
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); | |
508 | |
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_->DeepCopy()); | |
512 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_256_)->HasError()); | |
513 | |
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_->DeepCopy()); | |
517 EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_256_)->HasError()); | |
518 | |
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_->DeepCopy()); | |
522 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError()); | |
523 | |
524 EXPECT_TRUE(SettingsEqual(settings)); | |
525 } | |
526 | |
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 | |
531 // rejected... | |
532 DictionaryValue settings; | |
533 | |
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); | |
538 | |
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_->DeepCopy()); | |
543 | |
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_->DeepCopy()); | |
548 | |
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_->DeepCopy()); | |
552 | |
553 // ... except the last. Make sure it can still fail. | |
554 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError()); | |
555 | |
556 EXPECT_TRUE(SettingsEqual(settings)); | |
557 } | |
558 | |
559 TEST_F(ExtensionSettingsQuotaTest, GetBytesInUse) { | |
560 // Just testing GetBytesInUse, no need for a quota. | |
561 CreateStorage(UINT_MAX, UINT_MAX, UINT_MAX); | |
562 | |
563 std::vector<std::string> ab; | |
564 ab.push_back("a"); | |
565 ab.push_back("b"); | |
566 | |
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)); | |
571 | |
572 storage_->Set(DEFAULTS, "a", *byte_value_1_); | |
573 | |
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)); | |
578 | |
579 storage_->Set(DEFAULTS, "b", *byte_value_1_); | |
580 | |
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)); | |
585 | |
586 storage_->Set(DEFAULTS, "c", *byte_value_1_); | |
587 | |
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)); | |
592 } | |
593 | |
594 } // namespace extensions | |
OLD | NEW |