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

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

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

Powered by Google App Engine
This is Rietveld 408576698