Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: webkit/dom_storage/session_storage_database_unittest.cc

Issue 10176005: Add dom_storage::SessionStorageDatabase. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Code review. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "webkit/dom_storage/session_storage_database.h"
6
7 #include <map>
8 #include <string>
9
10 #include "base/file_util.h"
11 #include "base/logging.h"
12 #include "base/scoped_temp_dir.h"
13 #include "base/string_number_conversions.h"
14 #include "base/utf_string_conversions.h"
15 #include "googleurl/src/gurl.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/leveldatabase/src/include/leveldb/db.h"
18 #include "third_party/leveldatabase/src/include/leveldb/iterator.h"
19 #include "third_party/leveldatabase/src/include/leveldb/options.h"
20 #include "webkit/dom_storage/dom_storage_types.h"
21
22 namespace dom_storage {
23
24 class SessionStorageDatabaseTest : public testing::Test {
25 public:
26 SessionStorageDatabaseTest();
27 virtual ~SessionStorageDatabaseTest();
28 virtual void SetUp() OVERRIDE;
29
30 protected:
31 typedef std::map<std::string, std::string> DataMap;
32
33 // Helpers.
34 static bool IsNamespaceKey(const std::string& key,
35 int64* namespace_id);
36 static bool IsNamespaceOriginKey(const std::string& key,
37 int64* namespace_id,
38 std::string* origin);
39 static bool IsMapRefCountKey(const std::string& key,
40 int64* map_id);
41 static bool IsMapValueKey(const std::string& key,
42 int64* map_id);
43 void ResetDatabase();
44 void ReadData(DataMap* data) const;
45 void CheckConsistency() const;
46 void CheckEmpty() const;
47 void DumpData() const;
48 void CheckData(int64 namespace_id,
49 const GURL& origin,
50 const ValuesMap& reference) const;
51 void CompareValuesMaps(const ValuesMap& map1, const ValuesMap& map2) const;
52 std::string GetMapForArea(int64 namespace_id,
53 const GURL& origin) const;
54 int64 GetMapRefCount(const std::string& map_id) const;
55 int64 NextNamespaceId() const;
56
57 ScopedTempDir temp_dir_;
58 scoped_refptr<SessionStorageDatabase> db_;
59
60 // Test data.
61 GURL origin1;
62 GURL origin2;
63 GURL origin3;
64 string16 key1;
65 string16 key2;
66 string16 key3;
67 string16 key4;
68 NullableString16 value1;
69 NullableString16 value2;
70 NullableString16 value3;
71 NullableString16 value4;
72 NullableString16 value5;
73 NullableString16 value_null;
74
75 DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabaseTest);
76 };
77
78 SessionStorageDatabaseTest::SessionStorageDatabaseTest()
79 : origin1("http://www.origin1.com"),
80 origin2("http://www.origin2.com"),
81 origin3("http://www.origin3.com"),
82 key1(ASCIIToUTF16("key1")),
83 key2(ASCIIToUTF16("key2")),
84 key3(ASCIIToUTF16("key3")),
85 key4(ASCIIToUTF16("key4")),
86 value1(NullableString16(ASCIIToUTF16("value1"), false)),
87 value2(NullableString16(ASCIIToUTF16("value2"), false)),
88 value3(NullableString16(ASCIIToUTF16("value3"), false)),
89 value4(NullableString16(ASCIIToUTF16("value4"), false)),
90 value5(NullableString16(ASCIIToUTF16("value5"), false)),
91 value_null(NullableString16(true)) { }
92
93 SessionStorageDatabaseTest::~SessionStorageDatabaseTest() { }
94
95 void SessionStorageDatabaseTest::SetUp() {
96 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
97 ResetDatabase();
98 }
99
100 void SessionStorageDatabaseTest::ResetDatabase() {
101 db_ = new SessionStorageDatabase(temp_dir_.path());
102 ASSERT_TRUE(db_->LazyOpen(true));
103 }
104
105 // static
106 bool SessionStorageDatabaseTest::IsNamespaceKey(const std::string& key,
107 int64* namespace_id) {
108 std::string namespace_prefix = SessionStorageDatabase::NamespacePrefix();
109 if (key.find(namespace_prefix) != 0)
110 return false;
111 if (key == namespace_prefix)
112 return false;
113
114 size_t second_dash = key.find('-', namespace_prefix.length());
115 if (second_dash != std::string::npos)
116 return false;
117
118 // Key is of the form "namespace-<namespaceid>".
119 std::string namespace_id_str = key.substr(namespace_prefix.length());
120 bool conversion_ok = base::StringToInt64(namespace_id_str, namespace_id);
121 EXPECT_TRUE(conversion_ok);
122 return true;
123 }
124
125 // static
126 bool SessionStorageDatabaseTest::IsNamespaceOriginKey(const std::string& key,
127 int64* namespace_id,
128 std::string* origin) {
129 std::string namespace_prefix = SessionStorageDatabase::NamespacePrefix();
130 if (key.find(namespace_prefix) != 0)
131 return false;
132 size_t second_dash = key.find('-', namespace_prefix.length());
133 if (second_dash == std::string::npos)
134 return false;
135 // Key is of the form "namespace-<namespaceid>-<origin>", and the value
136 // is the map id.
137 std::string namespace_id_str =
138 key.substr(namespace_prefix.length(),
139 second_dash - namespace_prefix.length());
140 bool conversion_ok = base::StringToInt64(namespace_id_str, namespace_id);
141 EXPECT_TRUE(conversion_ok);
142 return true;
143 }
144
145 // static
146 bool SessionStorageDatabaseTest::IsMapRefCountKey(const std::string& key,
147 int64* map_id) {
148 std::string map_prefix = SessionStorageDatabase::MapPrefix();
149 if (key.find(map_prefix) != 0)
150 return false;
151 size_t second_dash = key.find('-', map_prefix.length());
152 if (second_dash != std::string::npos)
153 return false;
154 // Key is of the form "map-<mapid>" and the value is the ref count.
155 std::string map_id_str = key.substr(map_prefix.length(), second_dash);
156 bool conversion_ok = base::StringToInt64(map_id_str, map_id);
157 EXPECT_TRUE(conversion_ok);
158 return true;
159 }
160
161 // static
162 bool SessionStorageDatabaseTest::IsMapValueKey(const std::string& key,
163 int64* map_id) {
164 std::string map_prefix = SessionStorageDatabase::MapPrefix();
165 if (key.find(map_prefix) != 0)
166 return false;
167 size_t second_dash = key.find('-', map_prefix.length());
168 if (second_dash == std::string::npos)
169 return false;
170 // Key is of the form "map-<mapid>-key".
171 std::string map_id_str =
172 key.substr(map_prefix.length(), second_dash - map_prefix.length());
173 bool conversion_ok = base::StringToInt64(map_id_str, map_id);
174 EXPECT_TRUE(conversion_ok);
175 return true;
176 }
177
178 void SessionStorageDatabaseTest::ReadData(DataMap* data) const {
179 leveldb::DB* leveldb = db_->db_.get();
180 scoped_ptr<leveldb::Iterator> it(
181 leveldb->NewIterator(leveldb::ReadOptions()));
182 for (it->SeekToFirst(); it->Valid(); it->Next()) {
183 (*data)[it->key().ToString()] = it->value().ToString();
184 }
185 }
186
187 void SessionStorageDatabaseTest::CheckConsistency() const {
188 DataMap data;
189 ReadData(&data);
190 // Empty db is ok.
191 if (data.empty())
192 return;
193
194 // For detecting rubbish keys.
195 size_t valid_keys = 0;
196
197 std::string next_namespace_id_key =
198 SessionStorageDatabase::NextNamespaceIdKey();
199 std::string next_map_id_key = SessionStorageDatabase::NextMapIdKey();
200 // Check the namespace start key.
201 if (data.find(SessionStorageDatabase::NamespacePrefix()) == data.end()) {
202 // If there is no namespace start key, the database may contain only counter
203 // keys.
204 for (DataMap::const_iterator it = data.begin(); it != data.end(); ++it) {
205 ASSERT_TRUE(it->first == next_namespace_id_key ||
206 it->first == next_map_id_key);
207 }
208 return;
209 }
210 ++valid_keys;
211
212 // Iterate the "namespace-" keys.
213 std::set<int64> found_namespace_ids;
214 int64 max_namespace_id = -1;
215 std::map<int64, int64> map_refcounts;
216 int64 max_map_id = -1;
217
218 for (DataMap::const_iterator it = data.begin(); it != data.end(); ++it) {
219 int64 namespace_id;
220 std::string origin;
221 if (IsNamespaceKey(it->first, &namespace_id)) {
222 ASSERT_GT(namespace_id, 0);
223 found_namespace_ids.insert(namespace_id);
224 if (namespace_id > max_namespace_id)
225 max_namespace_id = namespace_id;
226 ++valid_keys;
227 } else if (IsNamespaceOriginKey(
228 it->first, &namespace_id, &origin)) {
229 // Check that the corresponding "namespace-<namespaceid>" key exists. It
230 // has been read by now, since the keys are stored in order.
231 ASSERT_TRUE(found_namespace_ids.find(namespace_id) !=
232 found_namespace_ids.end());
233 int64 map_id;
234 bool conversion_ok = base::StringToInt64(it->second, &map_id);
235 ASSERT_TRUE(conversion_ok);
236 ASSERT_GE(map_id, 0);
237 ++map_refcounts[map_id];
238 if (map_id > max_map_id)
239 max_map_id = map_id;
240 ++valid_keys;
241 }
242 }
243 if (max_namespace_id != -1) {
244 // The database contains namespaces.
245 ASSERT_TRUE(data.find(next_namespace_id_key) != data.end());
246 int64 next_namespace_id;
247 bool conversion_ok =
248 base::StringToInt64(data[next_namespace_id_key], &next_namespace_id);
249 ASSERT_TRUE(conversion_ok);
250 ASSERT_GT(next_namespace_id, max_namespace_id);
251 }
252 if (max_map_id != -1) {
253 // The database contains maps.
254 ASSERT_TRUE(data.find(next_map_id_key) != data.end());
255 int64 next_map_id;
256 bool conversion_ok =
257 base::StringToInt64(data[next_map_id_key], &next_map_id);
258 ASSERT_TRUE(conversion_ok);
259 ASSERT_GT(next_map_id, max_map_id);
260 }
261
262 // Iterate the "map-" keys.
263 std::set<int64> found_map_ids;
264 for (DataMap::const_iterator it = data.begin(); it != data.end(); ++it) {
265 int64 map_id;
266 if (IsMapRefCountKey(it->first, &map_id)) {
267 int64 ref_count;
268 bool conversion_ok = base::StringToInt64(it->second, &ref_count);
269 ASSERT_TRUE(conversion_ok);
270 // Check that the map is not stale.
271 ASSERT_GT(ref_count, 0);
272 ASSERT_TRUE(map_refcounts.find(map_id) != map_refcounts.end());
273 ASSERT_EQ(map_refcounts[map_id], ref_count);
274 // Mark the map as existing.
275 map_refcounts.erase(map_id);
276 found_map_ids.insert(map_id);
277 ++valid_keys;
278 } else if (IsMapValueKey(it->first, &map_id)) {
279 ASSERT_TRUE(found_map_ids.find(map_id) != found_map_ids.end());
280 ++valid_keys;
281 }
282 }
283 // Check that all maps referred to exist.
284 ASSERT_EQ(0U, map_refcounts.size());
285
286 // Count valid keys.
287 if (data.find(next_namespace_id_key) != data.end())
288 ++valid_keys;
289
290 if (data.find(next_map_id_key) != data.end())
291 ++valid_keys;
292
293 ASSERT_EQ(data.size(), valid_keys);
294 }
295
296 void SessionStorageDatabaseTest::CheckEmpty() const {
297 DataMap data;
298 ReadData(&data);
299 size_t valid_keys = 0;
300 if (data.find(SessionStorageDatabase::NamespacePrefix()) != data.end())
301 ++valid_keys;
302 if (data.find(SessionStorageDatabase::NextNamespaceIdKey()) != data.end())
303 ++valid_keys;
304 if (data.find(SessionStorageDatabase::NextMapIdKey()) != data.end())
305 ++valid_keys;
306 EXPECT_EQ(valid_keys, data.size());
307 }
308
309 void SessionStorageDatabaseTest::DumpData() const {
310 LOG(WARNING) << "---- Session storage contents";
311 scoped_ptr<leveldb::Iterator> it(
312 db_->db_->NewIterator(leveldb::ReadOptions()));
313 for (it->SeekToFirst(); it->Valid(); it->Next()) {
314 int64 dummy_map_id;
315 if (IsMapValueKey(it->key().ToString(), &dummy_map_id)) {
316 // Convert the value back to string16.
317 string16 value;
318 size_t len = it->value().size() / sizeof(char16);
319 value.resize(len);
320 value.assign(reinterpret_cast<const char16*>(it->value().data()), len);
321 LOG(WARNING) << it->key().ToString() << ": " << value;
322 } else {
323 LOG(WARNING) << it->key().ToString() << ": " << it->value().ToString();
324 }
325 }
326 LOG(WARNING) << "----";
327 }
328
329 void SessionStorageDatabaseTest::CheckData(int64 namespace_id,
330 const GURL& origin,
331 const ValuesMap& reference) const {
332 ValuesMap values;
333 db_->ReadAreaValues(namespace_id, origin, &values);
334 CompareValuesMaps(values, reference);
335 }
336
337 void SessionStorageDatabaseTest::CompareValuesMaps(
338 const ValuesMap& map1,
339 const ValuesMap& map2) const {
340 ASSERT_EQ(map2.size(), map1.size());
341 for (ValuesMap::const_iterator it = map1.begin(); it != map1.end(); ++it) {
342 string16 key = it->first;
343 ASSERT_TRUE(map2.find(key) != map2.end());
344 NullableString16 val1 = it->second;
345 NullableString16 val2 = map2.find(key)->second;
346 EXPECT_EQ(val2.is_null(), val1.is_null());
347 EXPECT_EQ(val2.string(), val1.string());
348 }
349 }
350
351 std::string SessionStorageDatabaseTest::GetMapForArea(
352 int64 namespace_id, const GURL& origin) const {
353 bool exists;
354 std::string map_id;
355 EXPECT_TRUE(db_->GetMapForArea(namespace_id, origin,
356 &exists, &map_id));
357 EXPECT_TRUE(exists);
358 return map_id;
359 }
360
361 int64 SessionStorageDatabaseTest::GetMapRefCount(
362 const std::string& map_id) const {
363 int64 ref_count;
364 EXPECT_TRUE(db_->GetMapRefCount(map_id, &ref_count));
365 return ref_count;
366 }
367
368 int64 SessionStorageDatabaseTest::NextNamespaceId() const {
369 int64 next_namespace_id;
370 EXPECT_TRUE(db_->GetNextNamespaceId(&next_namespace_id));
371 return next_namespace_id;
372 }
373
374 TEST_F(SessionStorageDatabaseTest, EmptyDatabaseSanityCheck) {
375 // An empty database should be valid.
376 CheckConsistency();
377 }
378
379 TEST_F(SessionStorageDatabaseTest, WriteDataForOneOrigin) {
380 // Keep track on what the values should look like.
381 ValuesMap reference;
382 // Write data.
383 {
384 ValuesMap changes;
385 changes[key1] = value1;
386 changes[key2] = value2;
387 changes[key3] = value3;
388 reference[key1] = value1;
389 reference[key2] = value2;
390 reference[key3] = value3;
391 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, false, changes));
392 }
393 CheckConsistency();
394 CheckData(1, origin1, reference);
395
396 // Overwrite and delete values.
397 {
398 ValuesMap changes;
399 changes[key1] = value4;
400 changes[key3] = value_null;
401 reference[key1] = value4;
402 reference.erase(key3);
403 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, false, changes));
404 }
405 CheckConsistency();
406 CheckData(1, origin1, reference);
407
408 // Clear data before writing.
409 {
410 ValuesMap changes;
411 changes[key2] = value2;
412 reference.erase(key1);
413 reference[key2] = value2;
414 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, true, changes));
415 }
416 CheckConsistency();
417 CheckData(1, origin1, reference);
418 }
419
420 TEST_F(SessionStorageDatabaseTest, WriteDataForTwoOrigins) {
421 // Write data.
422 ValuesMap data1;
423 data1[key1] = value1;
424 data1[key2] = value2;
425 data1[key3] = value3;
426 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
427
428 ValuesMap data2;
429 data2[key1] = value4;
430 data2[key2] = value1;
431 data2[key3] = value2;
432 EXPECT_TRUE(db_->CommitAreaChanges(1, origin2, false, data2));
433
434 CheckConsistency();
435 CheckData(1, origin1, data1);
436 CheckData(1, origin2, data2);
437 }
438
439 TEST_F(SessionStorageDatabaseTest, WriteDataForTwoNamespaces) {
440 // Write data.
441 ValuesMap data11;
442 data11[key1] = value1;
443 data11[key2] = value2;
444 data11[key3] = value3;
445 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, false, data11));
446 ValuesMap data12;
447 data12[key2] = value4;
448 data12[key3] = value3;
449 EXPECT_TRUE(db_->CommitAreaChanges(1, origin2, false, data12));
450 ValuesMap data21;
451 data21[key1] = value2;
452 data21[key2] = value4;
453 EXPECT_TRUE(db_->CommitAreaChanges(2, origin1, false, data21));
454 ValuesMap data22;
455 data22[key2] = value1;
456 data22[key3] = value2;
457 EXPECT_TRUE(db_->CommitAreaChanges(2, origin2, false, data22));
458 CheckConsistency();
459 CheckData(1, origin1, data11);
460 CheckData(1, origin2, data12);
461 CheckData(2, origin1, data21);
462 CheckData(2, origin2, data22);
463 }
464
465 TEST_F(SessionStorageDatabaseTest, ShallowCopy) {
466 // Write data for a namespace, for 2 origins.
467 ValuesMap data1;
468 data1[key1] = value1;
469 data1[key2] = value2;
470 data1[key3] = value3;
471 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
472 ValuesMap data2;
473 data2[key1] = value2;
474 data2[key3] = value1;
475 ASSERT_TRUE(db_->CommitAreaChanges(1, origin2, false, data2));
476 // Make a shallow copy.
477 EXPECT_TRUE(db_->CloneNamespace(1, 5));
478 // Now both namespaces should have the same data.
479 CheckConsistency();
480 CheckData(1, origin1, data1);
481 CheckData(1, origin2, data2);
482 CheckData(5, origin1, data1);
483 CheckData(5, origin2, data2);
484 // Both the namespaces refer to the same maps.
485 EXPECT_EQ(GetMapForArea(1, origin1), GetMapForArea(5, origin1));
486 EXPECT_EQ(GetMapForArea(1, origin2), GetMapForArea(5, origin2));
487 EXPECT_EQ(2, GetMapRefCount(GetMapForArea(1, origin1)));
488 EXPECT_EQ(2, GetMapRefCount(GetMapForArea(1, origin2)));
489 }
490
491 TEST_F(SessionStorageDatabaseTest, DeepCopy) {
492 ValuesMap data1;
493 data1[key1] = value1;
494 data1[key2] = value2;
495 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
496 EXPECT_TRUE(db_->CloneNamespace(1, 5));
497
498 // Make the shallow copy deep.
499 EXPECT_TRUE(db_->DeepCopyArea(5, origin1));
500
501 // Write data into the deep copy.
502 ValuesMap changes;
503 ValuesMap reference;
504 changes[key1] = value_null;
505 changes[key2] = value4;
506 changes[key3] = value4;
507 reference[key2] = value4;
508 reference[key3] = value4;
509 EXPECT_TRUE(db_->CommitAreaChanges(5, origin1, false, changes));
510
511 // Values in the original namespace were not changed.
512 CheckData(1, origin1, data1);
513 // But values in the deep copy were.
514 CheckData(5, origin1, reference);
515
516 // The namespaces no longer refer to the same map.
517 EXPECT_NE(GetMapForArea(1, origin1), GetMapForArea(5, origin1));
518 EXPECT_EQ(1, GetMapRefCount(GetMapForArea(1, origin1)));
519 EXPECT_EQ(1, GetMapRefCount(GetMapForArea(5, origin1)));
520 }
521
522 TEST_F(SessionStorageDatabaseTest, ManyShallowCopies) {
523 // Write data for a namespace, for 2 origins.
524 ValuesMap data1;
525 data1[key1] = value1;
526 data1[key2] = value2;
527 data1[key3] = value3;
528 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
529 ValuesMap data2;
530 data2[key1] = value2;
531 data2[key3] = value1;
532 ASSERT_TRUE(db_->CommitAreaChanges(1, origin2, false, data2));
533
534 // Make a two shallow copies.
535 EXPECT_TRUE(db_->CloneNamespace(1, 5));
536 EXPECT_TRUE(db_->CloneNamespace(1, 6));
537
538 // Make a shallow copy of a shallow copy.
539 EXPECT_TRUE(db_->CloneNamespace(6, 7));
540
541 // Now all namespaces should have the same data.
542 CheckConsistency();
543 CheckData(1, origin1, data1);
544 CheckData(5, origin1, data1);
545 CheckData(6, origin1, data1);
546 CheckData(7, origin1, data1);
547 CheckData(1, origin2, data2);
548 CheckData(5, origin2, data2);
549 CheckData(6, origin2, data2);
550 CheckData(7, origin2, data2);
551
552 // All namespaces refer to the same maps.
553 EXPECT_EQ(GetMapForArea(1, origin1), GetMapForArea(5, origin1));
554 EXPECT_EQ(GetMapForArea(1, origin2), GetMapForArea(5, origin2));
555 EXPECT_EQ(GetMapForArea(1, origin1), GetMapForArea(6, origin1));
556 EXPECT_EQ(GetMapForArea(1, origin2), GetMapForArea(6, origin2));
557 EXPECT_EQ(GetMapForArea(1, origin1), GetMapForArea(7, origin1));
558 EXPECT_EQ(GetMapForArea(1, origin2), GetMapForArea(7, origin2));
559
560 // Check the ref counts.
561 EXPECT_EQ(4, GetMapRefCount(GetMapForArea(1, origin1)));
562 EXPECT_EQ(4, GetMapRefCount(GetMapForArea(1, origin2)));
563 }
564
565 TEST_F(SessionStorageDatabaseTest, DisassociateShallowCopy) {
566 ValuesMap data1;
567 data1[key1] = value1;
568 data1[key2] = value2;
569 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
570 EXPECT_TRUE(db_->CloneNamespace(1, 5));
571
572 // Disassoaciate the shallow copy.
573 EXPECT_TRUE(db_->DeleteArea(5, origin1));
574 CheckConsistency();
575
576 // Now new data can be written to that map.
577 ValuesMap reference;
578 ValuesMap changes;
579 changes[key1] = value_null;
580 changes[key2] = value4;
581 changes[key3] = value4;
582 reference[key2] = value4;
583 reference[key3] = value4;
584 EXPECT_TRUE(db_->CommitAreaChanges(5, origin1, false, changes));
585
586 // Values in the original map were not changed.
587 CheckData(1, origin1, data1);
588
589 // But values in the disassociated map were.
590 CheckData(5, origin1, reference);
591 }
592
593 TEST_F(SessionStorageDatabaseTest, DeleteNamespace) {
594 ValuesMap data1;
595 data1[key1] = value1;
596 data1[key2] = value2;
597 data1[key3] = value3;
598 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
599 ValuesMap data2;
600 data2[key2] = value4;
601 data2[key3] = value3;
602 ASSERT_TRUE(db_->CommitAreaChanges(1, origin2, false, data2));
603 EXPECT_TRUE(db_->DeleteNamespace(1));
604 CheckConsistency();
605 CheckEmpty();
606 }
607
608 TEST_F(SessionStorageDatabaseTest, DeleteNamespaceWithShallowCopy) {
609 // Write data for a namespace, for 2 origins.
610 ValuesMap data1;
611 data1[key1] = value1;
612 data1[key2] = value2;
613 data1[key3] = value3;
614 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
615 ValuesMap data2;
616 data2[key1] = value2;
617 data2[key3] = value1;
618 ASSERT_TRUE(db_->CommitAreaChanges(1, origin2, false, data2));
619
620 // Make a shallow copy and delete the original namespace.
621 EXPECT_TRUE(db_->CloneNamespace(1, 5));;
622 EXPECT_TRUE(db_->DeleteNamespace(1));
623
624 // The original namespace has no data.
625 CheckConsistency();
626 CheckData(1, origin1, ValuesMap());
627 CheckData(1, origin2, ValuesMap());
628 // But the copy persists.
629 CheckData(5, origin1, data1);
630 CheckData(5, origin2, data2);
631 }
632
633 TEST_F(SessionStorageDatabaseTest, DeleteArea) {
634 // Write data for a namespace, for 2 origins.
635 ValuesMap data1;
636 data1[key1] = value1;
637 data1[key2] = value2;
638 data1[key3] = value3;
639 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
640 ValuesMap data2;
641 data2[key1] = value2;
642 data2[key3] = value1;
643 ASSERT_TRUE(db_->CommitAreaChanges(1, origin2, false, data2));
644
645 EXPECT_TRUE(db_->DeleteArea(1, origin2));
646 CheckConsistency();
647 // The data for the non-deleted origin persists.
648 CheckData(1, origin1, data1);
649 // The data for the deleted origin is gone.
650 CheckData(1, origin2, ValuesMap());
651 }
652
653 TEST_F(SessionStorageDatabaseTest, DeleteAreaWithShallowCopy) {
654 // Write data for a namespace, for 2 origins.
655 ValuesMap data1;
656 data1[key1] = value1;
657 data1[key2] = value2;
658 data1[key3] = value3;
659 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
660 ValuesMap data2;
661 data2[key1] = value2;
662 data2[key3] = value1;
663 ASSERT_TRUE(db_->CommitAreaChanges(1, origin2, false, data2));
664
665 // Make a shallow copy and delete an origin from the original namespace.
666 EXPECT_TRUE(db_->CloneNamespace(1, 5));
667 EXPECT_TRUE(db_->DeleteArea(1, origin1));
668 CheckConsistency();
669
670 // The original namespace has data for only the non-deleted origin.
671 CheckData(1, origin1, ValuesMap());
672 CheckData(1, origin2, data2);
673 // But the copy persists.
674 CheckData(5, origin1, data1);
675 CheckData(5, origin2, data2);
676 }
677
678 TEST_F(SessionStorageDatabaseTest, WriteRawBytes) {
679 // Write data which is not valid utf8 and contains null bytes.
680 unsigned char raw_data[10] = {255, 0, 0, 0, 1, 2, 3, 4, 5, 0};
681 ValuesMap changes;
682 string16 string_with_raw_data;
683 string_with_raw_data.assign(reinterpret_cast<char16*>(raw_data), 5);
684 changes[key1] = NullableString16(string_with_raw_data, false);
685 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, false, changes));
686 CheckConsistency();
687 ValuesMap values;
688 db_->ReadAreaValues(1, origin1, &values);
689 const unsigned char* data =
690 reinterpret_cast<const unsigned char*>(values[key1].string().data());
691 for (int i = 0; i < 10; ++i)
692 EXPECT_EQ(raw_data[i], data[i]);
693 }
694
695 TEST_F(SessionStorageDatabaseTest, NextNamespaceId) {
696 // Create namespaces, check the next namespace id.
697 ValuesMap data1;
698 data1[key1] = value1;
699 data1[key2] = value2;
700 ASSERT_TRUE(db_->CommitAreaChanges(10, origin1, false, data1));
701 EXPECT_EQ(10 + 1, NextNamespaceId());
702 ASSERT_TRUE(db_->CommitAreaChanges(343, origin1, false, data1));
703 EXPECT_EQ(343 + 1, NextNamespaceId());
704 ASSERT_TRUE(db_->CommitAreaChanges(99, origin1, false, data1));
705 EXPECT_EQ(343 + 1, NextNamespaceId());
706
707 // Close the database and recreate it.
708 ResetDatabase();
709
710 // The next namespace id is persisted.
711 EXPECT_EQ(344, NextNamespaceId());
712
713 // Create more namespaces.
714 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
715 EXPECT_EQ(344 + 1 + 1, NextNamespaceId());
716
717 EXPECT_TRUE(db_->CommitAreaChanges(959, origin1, false, data1));
718 EXPECT_EQ(344 + 959 + 1, NextNamespaceId());
719 }
720
721 TEST_F(SessionStorageDatabaseTest, NamespaceOffset) {
722 // Create a namespace with id 1.
723 ValuesMap data1;
724 data1[key1] = value1;
725 data1[key2] = value2;
726 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
727
728 // Close the database and recreate it.
729 ResetDatabase();
730
731 // Create another namespace with id 1.
732 ValuesMap data2;
733 data2[key1] = value3;
734 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, false, data2));
735
736 // Now the values for namespace 1 are the new ones.
737 CheckData(1, origin1, data2);
738
739 // The values for the old namespace 1 are still accessible via id -1.
740 CheckData(-1, origin1, data1);
741 }
742
743 TEST_F(SessionStorageDatabaseTest, NamespaceOffsetCloneNamespace) {
744 // Create a namespace with id 1.
745 ValuesMap data1;
746 data1[key1] = value1;
747 data1[key2] = value2;
748 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
749
750 // Close the database and recreate it.
751 ResetDatabase();
752
753 // Create another namespace with id 1.
754 ValuesMap data2;
755 data2[key1] = value3;
756 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, false, data2));
757
758 // Make a shallow copy of the newly created namespace.
759 EXPECT_TRUE(db_->CloneNamespace(1, 20));
760
761 // The clone contains values from the newly created namespace.
762 CheckData(20, origin1, data2);
763 CheckData(1, origin1, data2);
764
765 // The values for the old namespace 1 are still accessible via id -1.
766 CheckData(-1, origin1, data1);
767
768 // Close the database and recreate it.
769 ResetDatabase();
770
771 // The namespace and the clone are still accessible.
772 CheckData(-1, origin1, data2);
773 CheckData(-20, origin1, data2);
774 CheckData(-22, origin1, data1);
775 }
776
777 TEST_F(SessionStorageDatabaseTest, NamespaceOffsetDeepCopyArea) {
778 // Create a namespace with id 1.
779 ValuesMap data1;
780 data1[key1] = value1;
781 data1[key2] = value2;
782 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
783
784 // Close the database and recreate it.
785 ResetDatabase();
786
787 // Create another namespace with id 1.
788 ValuesMap data2;
789 data2[key1] = value3;
790 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, false, data2));
791
792 // Make a shallow copy of the newly created namespace.
793 EXPECT_TRUE(db_->CloneNamespace(1, 20));
794 // And make it deep.
795 EXPECT_TRUE(db_->DeepCopyArea(20, origin1));
796
797 // Now the values can be altered in the deep copy.
798 ValuesMap data3;
799 data3[key1] = value2;
800 EXPECT_TRUE(db_->CommitAreaChanges(20, origin1, false, data3));
801
802 CheckData(20, origin1, data3);
803 CheckData(1, origin1, data2);
804
805 // The values for the old namespace 1 are still accessible via id -1.
806 CheckData(-1, origin1, data1);
807
808 // Close the database and recreate it.
809 ResetDatabase();
810
811 // The namespace and the deep copy are still accessible.
812 CheckData(-1, origin1, data3);
813 CheckData(-20, origin1, data2);
814 CheckData(-22, origin1, data1);
815 }
816
817 TEST_F(SessionStorageDatabaseTest, NamespaceOffsetDeleteArea) {
818 // Create a namespace with id 1.
819 ValuesMap data1;
820 data1[key1] = value1;
821 data1[key2] = value2;
822 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
823
824 // Close the database and recreate it.
825 ResetDatabase();
826
827 // Create another namespace with id 1.
828 ValuesMap data2;
829 data2[key1] = value3;
830 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, false, data2));
831
832 // Delete origin1 from the newly created namespace.
833 EXPECT_TRUE(db_->DeleteArea(1, origin1));
834
835 // Namespace 1 is empty.
836 CheckData(1, origin1, ValuesMap());
837
838 // The values for the old namespace 1 are still accessible via id -1.
839 CheckData(-1, origin1, data1);
840 }
841
842 TEST_F(SessionStorageDatabaseTest, NamespaceOffsetDeleteNamespace) {
843 // Create a namespace with id 1.
844 ValuesMap data1;
845 data1[key1] = value1;
846 data1[key2] = value2;
847 ASSERT_TRUE(db_->CommitAreaChanges(1, origin1, false, data1));
848
849 // Close the database and recreate it.
850 ResetDatabase();
851
852 // Create another namespace with id 1.
853 ValuesMap data2;
854 data2[key1] = value3;
855 EXPECT_TRUE(db_->CommitAreaChanges(1, origin1, false, data2));
856
857 // Delete the newly created namespace.
858 EXPECT_TRUE(db_->DeleteNamespace(1));
859
860 // Namespace 1 is empty.
861 CheckData(1, origin1, ValuesMap());
862
863 // The values for the old namespace 1 are still accessible via id -1.
864 CheckData(-1, origin1, data1);
865 }
866
867 } // namespace dom_storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698