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 "testing/gtest/include/gtest/gtest.h" | |
6 | |
7 #include "base/json/json_writer.h" | |
8 #include "base/values.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "chrome/browser/extensions/settings/settings_backend.h" | |
12 #include "chrome/browser/extensions/settings/settings_storage_quota_enforcer.h" | |
13 #include "chrome/browser/value_store/testing_value_store.h" | |
14 | |
15 namespace extensions { | |
16 | |
17 // To save typing ValueStore::DEFAULTS/IGNORE_QUOTA everywhere. | |
18 const ValueStore::WriteOptions DEFAULTS = ValueStore::DEFAULTS; | |
19 const ValueStore::WriteOptions IGNORE_QUOTA = | |
20 ValueStore::IGNORE_QUOTA; | |
21 | |
22 class ExtensionSettingsQuotaTest : public testing::Test { | |
23 public: | |
24 ExtensionSettingsQuotaTest() | |
25 : byte_value_1_(Value::CreateIntegerValue(1)), | |
26 byte_value_16_(Value::CreateStringValue("sixteen bytes.")), | |
27 byte_value_256_(new ListValue()), | |
28 delegate_(new TestingValueStore()) { | |
29 for (int i = 1; i < 89; ++i) { | |
30 byte_value_256_->Append(Value::CreateIntegerValue(i)); | |
31 } | |
32 ValidateByteValues(); | |
33 } | |
34 | |
35 void ValidateByteValues() { | |
36 std::string validate_sizes; | |
37 base::JSONWriter::Write(byte_value_1_.get(), &validate_sizes); | |
38 ASSERT_EQ(1u, validate_sizes.size()); | |
39 base::JSONWriter::Write(byte_value_16_.get(), &validate_sizes); | |
40 ASSERT_EQ(16u, validate_sizes.size()); | |
41 base::JSONWriter::Write(byte_value_256_.get(), &validate_sizes); | |
42 ASSERT_EQ(256u, validate_sizes.size()); | |
43 } | |
44 | |
45 virtual void TearDown() OVERRIDE { | |
46 ASSERT_TRUE(storage_.get() != NULL); | |
47 } | |
48 | |
49 protected: | |
50 // Creates |storage_|. Must only be called once. | |
51 void CreateStorage( | |
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_)); | |
57 } | |
58 | |
59 // Returns whether the settings in |storage_| and |delegate_| are the same as | |
60 // |settings|. | |
61 bool SettingsEqual(const DictionaryValue& settings) { | |
62 return settings.Equals(storage_->Get()->settings().get()) && | |
63 settings.Equals(delegate_->Get()->settings().get()); | |
64 } | |
65 | |
66 // Values with different serialized sizes. | |
67 scoped_ptr<Value> byte_value_1_; | |
68 scoped_ptr<Value> byte_value_16_; | |
69 scoped_ptr<ListValue> byte_value_256_; | |
70 | |
71 // Quota enforcing storage area being tested. | |
72 scoped_ptr<SettingsStorageQuotaEnforcer> storage_; | |
73 | |
74 // In-memory storage area being delegated to. Always owned by |storage_|. | |
75 TestingValueStore* delegate_; | |
76 }; | |
77 | |
78 TEST_F(ExtensionSettingsQuotaTest, ZeroQuotaBytes) { | |
79 DictionaryValue empty; | |
80 CreateStorage(0, UINT_MAX, UINT_MAX); | |
81 | |
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)); | |
86 } | |
87 | |
88 TEST_F(ExtensionSettingsQuotaTest, KeySizeTakenIntoAccount) { | |
89 DictionaryValue empty; | |
90 CreateStorage(8u, UINT_MAX, UINT_MAX); | |
91 EXPECT_TRUE( | |
92 storage_->Set(DEFAULTS, "Really long key", *byte_value_1_)->HasError()); | |
93 EXPECT_TRUE(SettingsEqual(empty)); | |
94 } | |
95 | |
96 TEST_F(ExtensionSettingsQuotaTest, SmallByteQuota) { | |
97 DictionaryValue settings; | |
98 CreateStorage(8u, UINT_MAX, UINT_MAX); | |
99 | |
100 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError()); | |
101 settings.Set("a", byte_value_1_->DeepCopy()); | |
102 EXPECT_TRUE(SettingsEqual(settings)); | |
103 | |
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)); | |
107 } | |
108 | |
109 TEST_F(ExtensionSettingsQuotaTest, MediumByteQuota) { | |
110 DictionaryValue settings; | |
111 CreateStorage(40, UINT_MAX, UINT_MAX); | |
112 | |
113 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)); | |
120 | |
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)); | |
126 | |
127 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError()); | |
128 EXPECT_TRUE(SettingsEqual(settings)); | |
129 } | |
130 | |
131 TEST_F(ExtensionSettingsQuotaTest, ZeroMaxKeys) { | |
132 DictionaryValue empty; | |
133 CreateStorage(UINT_MAX, UINT_MAX, 0); | |
134 | |
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)); | |
139 } | |
140 | |
141 TEST_F(ExtensionSettingsQuotaTest, SmallMaxKeys) { | |
142 DictionaryValue settings; | |
143 CreateStorage(UINT_MAX, UINT_MAX, 1); | |
144 | |
145 EXPECT_FALSE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError()); | |
146 settings.Set("a", byte_value_1_->DeepCopy()); | |
147 EXPECT_TRUE(SettingsEqual(settings)); | |
148 | |
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)); | |
153 | |
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)); | |
157 } | |
158 | |
159 TEST_F(ExtensionSettingsQuotaTest, MediumMaxKeys) { | |
160 DictionaryValue settings; | |
161 CreateStorage(UINT_MAX, UINT_MAX, 2); | |
162 | |
163 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)); | |
170 | |
171 // Should be able to set existing keys to other values without going over | |
172 // quota. | |
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)); | |
177 | |
178 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError()); | |
179 EXPECT_TRUE(SettingsEqual(settings)); | |
180 } | |
181 | |
182 TEST_F(ExtensionSettingsQuotaTest, RemovingExistingSettings) { | |
183 DictionaryValue settings; | |
184 CreateStorage(266, UINT_MAX, 2); | |
185 | |
186 storage_->Set(DEFAULTS, "b", *byte_value_16_); | |
187 settings.Set("b", byte_value_16_->DeepCopy()); | |
188 // Not enough quota. | |
189 storage_->Set(DEFAULTS, "c", *byte_value_256_); | |
190 EXPECT_TRUE(SettingsEqual(settings)); | |
191 | |
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)); | |
198 | |
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)); | |
203 | |
204 EXPECT_TRUE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError()); | |
205 EXPECT_TRUE(SettingsEqual(settings)); | |
206 | |
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)); | |
213 } | |
214 | |
215 TEST_F(ExtensionSettingsQuotaTest, RemovingNonexistentSettings) { | |
216 DictionaryValue settings; | |
217 CreateStorage(36, UINT_MAX, 3); | |
218 | |
219 // Max out bytes. | |
220 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)); | |
228 | |
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)); | |
236 | |
237 // Still no quota. | |
238 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError()); | |
239 EXPECT_TRUE(SettingsEqual(settings)); | |
240 | |
241 // Max out key count. | |
242 to_set.Clear(); | |
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)); | |
251 | |
252 // Remove some settings that don't exist. | |
253 to_remove.clear(); | |
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)); | |
259 | |
260 // Still no quota. | |
261 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_1_)->HasError()); | |
262 EXPECT_TRUE(SettingsEqual(settings)); | |
263 } | |
264 | |
265 TEST_F(ExtensionSettingsQuotaTest, Clear) { | |
266 DictionaryValue settings; | |
267 CreateStorage(40, UINT_MAX, 5); | |
268 | |
269 // Test running out of byte quota. | |
270 { | |
271 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()); | |
276 | |
277 EXPECT_FALSE(storage_->Clear()->HasError()); | |
278 | |
279 // (repeat) | |
280 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
281 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError()); | |
282 } | |
283 | |
284 // Test reaching max keys. | |
285 storage_->Clear(); | |
286 { | |
287 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()); | |
295 | |
296 storage_->Clear(); | |
297 | |
298 // (repeat) | |
299 EXPECT_FALSE(storage_->Set(DEFAULTS, to_set)->HasError()); | |
300 EXPECT_TRUE(storage_->Set(DEFAULTS, "f", *byte_value_1_)->HasError()); | |
301 } | |
302 } | |
303 | |
304 TEST_F(ExtensionSettingsQuotaTest, ChangingUsedBytesWithSet) { | |
305 DictionaryValue settings; | |
306 CreateStorage(20, UINT_MAX, UINT_MAX); | |
307 | |
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)); | |
312 | |
313 EXPECT_TRUE(storage_->Set(DEFAULTS, "a", *byte_value_256_)->HasError()); | |
314 EXPECT_TRUE(SettingsEqual(settings)); | |
315 | |
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()); | |
320 | |
321 EXPECT_FALSE(storage_->Set(DEFAULTS, "foobar", *byte_value_1_)->HasError()); | |
322 settings.Set("foobar", byte_value_1_->DeepCopy()); | |
323 EXPECT_TRUE(SettingsEqual(settings)); | |
324 } | |
325 | |
326 TEST_F(ExtensionSettingsQuotaTest, SetsOnlyEntirelyCompletedWithByteQuota) { | |
327 DictionaryValue settings; | |
328 CreateStorage(40, UINT_MAX, UINT_MAX); | |
329 | |
330 storage_->Set(DEFAULTS, "a", *byte_value_16_); | |
331 settings.Set("a", byte_value_16_->DeepCopy()); | |
332 | |
333 // The entire change is over quota. | |
334 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)); | |
339 | |
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)); | |
347 } | |
348 | |
349 TEST_F(ExtensionSettingsQuotaTest, SetsOnlyEntireCompletedWithMaxKeys) { | |
350 DictionaryValue settings; | |
351 CreateStorage(UINT_MAX, UINT_MAX, 2); | |
352 | |
353 storage_->Set(DEFAULTS, "a", *byte_value_1_); | |
354 settings.Set("a", byte_value_1_->DeepCopy()); | |
355 | |
356 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)); | |
361 } | |
362 | |
363 TEST_F(ExtensionSettingsQuotaTest, WithInitialDataAndByteQuota) { | |
364 DictionaryValue settings; | |
365 delegate_->Set(DEFAULTS, "a", *byte_value_256_); | |
366 settings.Set("a", byte_value_256_->DeepCopy()); | |
367 | |
368 CreateStorage(280, UINT_MAX, UINT_MAX); | |
369 EXPECT_TRUE(SettingsEqual(settings)); | |
370 | |
371 // Add some data. | |
372 EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_16_)->HasError()); | |
373 settings.Set("b", byte_value_16_->DeepCopy()); | |
374 EXPECT_TRUE(SettingsEqual(settings)); | |
375 | |
376 // Not enough quota. | |
377 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError()); | |
378 EXPECT_TRUE(SettingsEqual(settings)); | |
379 | |
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)); | |
384 | |
385 EXPECT_FALSE(storage_->Set(DEFAULTS, "c", *byte_value_16_)->HasError()); | |
386 settings.Set("c", byte_value_16_->DeepCopy()); | |
387 EXPECT_TRUE(SettingsEqual(settings)); | |
388 | |
389 // Remove to free up some more data. | |
390 EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_256_)->HasError()); | |
391 | |
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)); | |
399 | |
400 EXPECT_FALSE(storage_->Set(DEFAULTS, "d", *byte_value_256_)->HasError()); | |
401 settings.Set("d", byte_value_256_->DeepCopy()); | |
402 EXPECT_TRUE(SettingsEqual(settings)); | |
403 } | |
404 | |
405 TEST_F(ExtensionSettingsQuotaTest, WithInitialDataAndMaxKeys) { | |
406 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); | |
410 | |
411 EXPECT_FALSE(storage_->Set(DEFAULTS, "b", *byte_value_1_)->HasError()); | |
412 settings.Set("b", byte_value_1_->DeepCopy()); | |
413 | |
414 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_1_)->HasError()); | |
415 | |
416 EXPECT_TRUE(SettingsEqual(settings)); | |
417 } | |
418 | |
419 TEST_F(ExtensionSettingsQuotaTest, InitiallyOverByteQuota) { | |
420 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); | |
425 | |
426 CreateStorage(40, UINT_MAX, UINT_MAX); | |
427 EXPECT_TRUE(SettingsEqual(settings)); | |
428 | |
429 EXPECT_TRUE(storage_->Set(DEFAULTS, "d", *byte_value_16_)->HasError()); | |
430 | |
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)); | |
435 | |
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)); | |
440 } | |
441 | |
442 TEST_F(ExtensionSettingsQuotaTest, InitiallyOverMaxKeys) { | |
443 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); | |
448 | |
449 CreateStorage(UINT_MAX, UINT_MAX, 2); | |
450 EXPECT_TRUE(SettingsEqual(settings)); | |
451 | |
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)); | |
456 | |
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)); | |
463 | |
464 EXPECT_FALSE(storage_->Set(DEFAULTS, "e", *byte_value_1_)->HasError()); | |
465 settings.Set("e", byte_value_1_->DeepCopy()); | |
466 EXPECT_TRUE(SettingsEqual(settings)); | |
467 | |
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)); | |
472 } | |
473 | |
474 TEST_F(ExtensionSettingsQuotaTest, ZeroQuotaBytesPerSetting) { | |
475 DictionaryValue empty; | |
476 CreateStorage(UINT_MAX, 0, UINT_MAX); | |
477 | |
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)); | |
482 } | |
483 | |
484 TEST_F(ExtensionSettingsQuotaTest, QuotaBytesPerSetting) { | |
485 DictionaryValue settings; | |
486 | |
487 CreateStorage(UINT_MAX, 20, UINT_MAX); | |
488 | |
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()); | |
493 | |
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()); | |
498 | |
499 EXPECT_TRUE(SettingsEqual(settings)); | |
500 } | |
501 | |
502 TEST_F(ExtensionSettingsQuotaTest, QuotaBytesPerSettingWithInitialSettings) { | |
503 DictionaryValue settings; | |
504 | |
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); | |
509 | |
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()); | |
514 | |
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()); | |
519 | |
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()); | |
524 | |
525 EXPECT_TRUE(SettingsEqual(settings)); | |
526 } | |
527 | |
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 | |
532 // rejected... | |
533 DictionaryValue settings; | |
534 | |
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); | |
539 | |
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()); | |
544 | |
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()); | |
549 | |
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()); | |
553 | |
554 // ... except the last. Make sure it can still fail. | |
555 EXPECT_TRUE(storage_->Set(DEFAULTS, "c", *byte_value_256_)->HasError()); | |
556 | |
557 EXPECT_TRUE(SettingsEqual(settings)); | |
558 } | |
559 | |
560 TEST_F(ExtensionSettingsQuotaTest, GetBytesInUse) { | |
561 // Just testing GetBytesInUse, no need for a quota. | |
562 CreateStorage(UINT_MAX, UINT_MAX, UINT_MAX); | |
563 | |
564 std::vector<std::string> ab; | |
565 ab.push_back("a"); | |
566 ab.push_back("b"); | |
567 | |
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)); | |
572 | |
573 storage_->Set(DEFAULTS, "a", *byte_value_1_); | |
574 | |
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)); | |
579 | |
580 storage_->Set(DEFAULTS, "b", *byte_value_1_); | |
581 | |
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)); | |
586 | |
587 storage_->Set(DEFAULTS, "c", *byte_value_1_); | |
588 | |
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)); | |
593 } | |
594 | |
595 } // namespace extensions | |
OLD | NEW |