OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "content/browser/dom_storage/dom_storage_area.h" | 13 #include "content/browser/dom_storage/dom_storage_area.h" |
14 #include "content/browser/dom_storage/dom_storage_context_impl.h" | 14 #include "content/browser/dom_storage/dom_storage_context_impl.h" |
15 #include "content/browser/dom_storage/dom_storage_namespace.h" | 15 #include "content/browser/dom_storage/dom_storage_namespace.h" |
16 #include "content/browser/dom_storage/dom_storage_task_runner.h" | 16 #include "content/browser/dom_storage/dom_storage_task_runner.h" |
17 #include "content/public/browser/local_storage_usage_info.h" | 17 #include "content/public/browser/local_storage_usage_info.h" |
18 #include "content/public/browser/session_storage_namespace.h" | |
19 #include "content/public/browser/session_storage_usage_info.h" | 18 #include "content/public/browser/session_storage_usage_info.h" |
20 #include "content/public/test/mock_special_storage_policy.h" | 19 #include "content/public/test/mock_special_storage_policy.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
22 | 21 |
23 using base::ASCIIToUTF16; | 22 using base::ASCIIToUTF16; |
24 | 23 |
25 namespace content { | 24 namespace content { |
26 | 25 |
27 class DOMStorageContextImplTest : public testing::Test { | 26 class DOMStorageContextImplTest : public testing::Test { |
28 public: | 27 public: |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); | 252 dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); |
254 area = dom_namespace->OpenStorageArea(kOrigin); | 253 area = dom_namespace->OpenStorageArea(kOrigin); |
255 read_value = area->GetItem(kKey); | 254 read_value = area->GetItem(kKey); |
256 EXPECT_TRUE(read_value.is_null()); | 255 EXPECT_TRUE(read_value.is_null()); |
257 dom_namespace->CloseStorageArea(area); | 256 dom_namespace->CloseStorageArea(area); |
258 context_->Shutdown(); | 257 context_->Shutdown(); |
259 context_ = NULL; | 258 context_ = NULL; |
260 base::MessageLoop::current()->RunUntilIdle(); | 259 base::MessageLoop::current()->RunUntilIdle(); |
261 } | 260 } |
262 | 261 |
263 TEST_F(DOMStorageContextImplTest, SessionStorageAlias) { | |
264 const int kFirstSessionStorageNamespaceId = 1; | |
265 const std::string kPersistentId = "persistent"; | |
266 context_->CreateSessionNamespace(kFirstSessionStorageNamespaceId, | |
267 kPersistentId); | |
268 DOMStorageNamespace* dom_namespace1 = | |
269 context_->GetStorageNamespace(kFirstSessionStorageNamespaceId); | |
270 ASSERT_TRUE(dom_namespace1); | |
271 DOMStorageArea* area1 = dom_namespace1->OpenStorageArea(kOrigin); | |
272 base::NullableString16 old_value; | |
273 area1->SetItem(kKey, kValue, &old_value); | |
274 EXPECT_TRUE(old_value.is_null()); | |
275 base::NullableString16 read_value = area1->GetItem(kKey); | |
276 EXPECT_TRUE(!read_value.is_null() && read_value.string() == kValue); | |
277 | |
278 // Create an alias. | |
279 const int kAliasSessionStorageNamespaceId = 2; | |
280 context_->CreateAliasSessionNamespace(kFirstSessionStorageNamespaceId, | |
281 kAliasSessionStorageNamespaceId, | |
282 kPersistentId); | |
283 DOMStorageNamespace* dom_namespace2 = | |
284 context_->GetStorageNamespace(kAliasSessionStorageNamespaceId); | |
285 ASSERT_TRUE(dom_namespace2); | |
286 ASSERT_TRUE(dom_namespace2->alias_master_namespace() == dom_namespace1); | |
287 | |
288 // Verify that read values are identical. | |
289 DOMStorageArea* area2 = dom_namespace2->OpenStorageArea(kOrigin); | |
290 read_value = area2->GetItem(kKey); | |
291 EXPECT_TRUE(!read_value.is_null() && read_value.string() == kValue); | |
292 | |
293 // Verify that writes are reflected in both namespaces. | |
294 const base::string16 kValue2(ASCIIToUTF16("value2")); | |
295 area2->SetItem(kKey, kValue2, &old_value); | |
296 read_value = area1->GetItem(kKey); | |
297 EXPECT_TRUE(!read_value.is_null() && read_value.string() == kValue2); | |
298 dom_namespace1->CloseStorageArea(area1); | |
299 dom_namespace2->CloseStorageArea(area2); | |
300 | |
301 // When creating an alias of an alias, ensure that the master relationship | |
302 // is collapsed. | |
303 const int kAlias2SessionStorageNamespaceId = 3; | |
304 context_->CreateAliasSessionNamespace(kAliasSessionStorageNamespaceId, | |
305 kAlias2SessionStorageNamespaceId, | |
306 kPersistentId); | |
307 DOMStorageNamespace* dom_namespace3 = | |
308 context_->GetStorageNamespace(kAlias2SessionStorageNamespaceId); | |
309 ASSERT_TRUE(dom_namespace3); | |
310 ASSERT_TRUE(dom_namespace3->alias_master_namespace() == dom_namespace1); | |
311 } | |
312 | |
313 TEST_F(DOMStorageContextImplTest, SessionStorageMerge) { | |
314 // Create a target namespace that we will merge into. | |
315 const int kTargetSessionStorageNamespaceId = 1; | |
316 const std::string kTargetPersistentId = "persistent"; | |
317 context_->CreateSessionNamespace(kTargetSessionStorageNamespaceId, | |
318 kTargetPersistentId); | |
319 DOMStorageNamespace* target_ns = | |
320 context_->GetStorageNamespace(kTargetSessionStorageNamespaceId); | |
321 ASSERT_TRUE(target_ns); | |
322 DOMStorageArea* target_ns_area = target_ns->OpenStorageArea(kOrigin); | |
323 base::NullableString16 old_value; | |
324 const base::string16 kKey2(ASCIIToUTF16("key2")); | |
325 const base::string16 kKey2Value(ASCIIToUTF16("key2value")); | |
326 target_ns_area->SetItem(kKey, kValue, &old_value); | |
327 target_ns_area->SetItem(kKey2, kKey2Value, &old_value); | |
328 | |
329 // Create a source namespace & its alias. | |
330 const int kSourceSessionStorageNamespaceId = 2; | |
331 const int kAliasSessionStorageNamespaceId = 3; | |
332 const std::string kSourcePersistentId = "persistent_source"; | |
333 context_->CreateSessionNamespace(kSourceSessionStorageNamespaceId, | |
334 kSourcePersistentId); | |
335 context_->CreateAliasSessionNamespace(kSourceSessionStorageNamespaceId, | |
336 kAliasSessionStorageNamespaceId, | |
337 kSourcePersistentId); | |
338 DOMStorageNamespace* alias_ns = | |
339 context_->GetStorageNamespace(kAliasSessionStorageNamespaceId); | |
340 ASSERT_TRUE(alias_ns); | |
341 | |
342 // Create a transaction log that can't be merged. | |
343 const int kPid1 = 10; | |
344 ASSERT_FALSE(alias_ns->IsLoggingRenderer(kPid1)); | |
345 alias_ns->AddTransactionLogProcessId(kPid1); | |
346 ASSERT_TRUE(alias_ns->IsLoggingRenderer(kPid1)); | |
347 const base::string16 kValue2(ASCIIToUTF16("value2")); | |
348 DOMStorageNamespace::TransactionRecord txn; | |
349 txn.origin = kOrigin; | |
350 txn.key = kKey; | |
351 txn.value = base::NullableString16(kValue2, false); | |
352 txn.transaction_type = DOMStorageNamespace::TRANSACTION_READ; | |
353 alias_ns->AddTransaction(kPid1, txn); | |
354 ASSERT_TRUE(alias_ns->Merge(false, kPid1, target_ns, NULL) == | |
355 SessionStorageNamespace::MERGE_RESULT_NOT_MERGEABLE); | |
356 | |
357 // Create a transaction log that can be merged. | |
358 const int kPid2 = 20; | |
359 alias_ns->AddTransactionLogProcessId(kPid2); | |
360 txn.transaction_type = DOMStorageNamespace::TRANSACTION_WRITE; | |
361 alias_ns->AddTransaction(kPid2, txn); | |
362 ASSERT_TRUE(alias_ns->Merge(true, kPid2, target_ns, NULL) == | |
363 SessionStorageNamespace::MERGE_RESULT_MERGEABLE); | |
364 | |
365 // Verify that the merge was successful. | |
366 ASSERT_TRUE(alias_ns->alias_master_namespace() == target_ns); | |
367 base::NullableString16 read_value = target_ns_area->GetItem(kKey); | |
368 EXPECT_TRUE(!read_value.is_null() && read_value.string() == kValue2); | |
369 DOMStorageArea* alias_ns_area = alias_ns->OpenStorageArea(kOrigin); | |
370 read_value = alias_ns_area->GetItem(kKey2); | |
371 EXPECT_TRUE(!read_value.is_null() && read_value.string() == kKey2Value); | |
372 } | |
373 | |
374 } // namespace content | 262 } // namespace content |
OLD | NEW |