OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/file_util.h" |
| 6 #include "base/scoped_temp_dir.h" |
| 7 #include "chrome/test/base/ui_test_utils.h" |
| 8 #include "content/browser/browser_thread_impl.h" |
| 9 #include "content/browser/in_process_webkit/indexed_db_context_impl.h" |
| 10 #include "content/public/common/url_constants.h" |
| 11 #include "content/test/test_browser_context.h" |
| 12 #include "webkit/database/database_util.h" |
| 13 #include "webkit/quota/mock_special_storage_policy.h" |
| 14 #include "webkit/quota/special_storage_policy.h" |
| 15 |
| 16 using content::BrowserContext; |
| 17 using content::BrowserThread; |
| 18 using content::BrowserThreadImpl; |
| 19 using webkit_database::DatabaseUtil; |
| 20 |
| 21 class IndexedDBTest : public testing::Test { |
| 22 public: |
| 23 IndexedDBTest() |
| 24 : message_loop_(MessageLoop::TYPE_IO), |
| 25 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_), |
| 26 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_), |
| 27 io_thread_(BrowserThread::IO, &message_loop_) { |
| 28 } |
| 29 |
| 30 protected: |
| 31 MessageLoop message_loop_; |
| 32 |
| 33 private: |
| 34 BrowserThreadImpl webkit_thread_; |
| 35 BrowserThreadImpl file_thread_; |
| 36 BrowserThreadImpl io_thread_; |
| 37 }; |
| 38 |
| 39 TEST_F(IndexedDBTest, ClearLocalState) { |
| 40 ScopedTempDir temp_dir; |
| 41 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 42 |
| 43 FilePath protected_path; |
| 44 FilePath unprotected_path; |
| 45 |
| 46 // Create the scope which will ensure we run the destructor of the webkit |
| 47 // context which should trigger the clean up. |
| 48 { |
| 49 TestBrowserContext browser_context; |
| 50 |
| 51 // Test our assumptions about what is protected and what is not. |
| 52 const GURL kProtectedOrigin("https://foo/"); |
| 53 const GURL kUnprotectedOrigin("http://foo/"); |
| 54 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 55 new quota::MockSpecialStoragePolicy; |
| 56 special_storage_policy->AddProtected(kProtectedOrigin); |
| 57 browser_context.SetSpecialStoragePolicy(special_storage_policy); |
| 58 quota::SpecialStoragePolicy* policy = |
| 59 browser_context.GetSpecialStoragePolicy(); |
| 60 ASSERT_TRUE(policy->IsStorageProtected(kProtectedOrigin)); |
| 61 ASSERT_FALSE(policy->IsStorageProtected(kUnprotectedOrigin)); |
| 62 |
| 63 // Create some indexedDB paths. |
| 64 // With the levelDB backend, these are directories. |
| 65 IndexedDBContextImpl* idb_context = |
| 66 static_cast<IndexedDBContextImpl*>( |
| 67 BrowserContext::GetIndexedDBContext(&browser_context)); |
| 68 idb_context->set_data_path_for_testing(temp_dir.path()); |
| 69 protected_path = idb_context->GetFilePathForTesting( |
| 70 DatabaseUtil::GetOriginIdentifier(kProtectedOrigin)); |
| 71 unprotected_path = idb_context->GetFilePathForTesting( |
| 72 DatabaseUtil::GetOriginIdentifier(kUnprotectedOrigin)); |
| 73 ASSERT_TRUE(file_util::CreateDirectory(protected_path)); |
| 74 ASSERT_TRUE(file_util::CreateDirectory(unprotected_path)); |
| 75 |
| 76 // Setup to clear all unprotected origins on exit. |
| 77 idb_context->set_clear_local_state_on_exit(true); |
| 78 message_loop_.RunAllPending(); |
| 79 } |
| 80 |
| 81 // Make sure we wait until the destructor has run. |
| 82 message_loop_.RunAllPending(); |
| 83 |
| 84 ASSERT_TRUE(file_util::DirectoryExists(protected_path)); |
| 85 ASSERT_FALSE(file_util::DirectoryExists(unprotected_path)); |
| 86 } |
| 87 |
| 88 TEST_F(IndexedDBTest, ClearSessionOnlyDatabases) { |
| 89 ScopedTempDir temp_dir; |
| 90 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 91 |
| 92 FilePath normal_path; |
| 93 FilePath session_only_path; |
| 94 |
| 95 // Create the scope which will ensure we run the destructor of the webkit |
| 96 // context which should trigger the clean up. |
| 97 { |
| 98 TestBrowserContext browser_context; |
| 99 |
| 100 const GURL kNormalOrigin("http://normal/"); |
| 101 const GURL kSessionOnlyOrigin("http://session-only/"); |
| 102 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 103 new quota::MockSpecialStoragePolicy; |
| 104 special_storage_policy->AddSessionOnly(kSessionOnlyOrigin); |
| 105 |
| 106 // Create some indexedDB paths. |
| 107 // With the levelDB backend, these are directories. |
| 108 IndexedDBContextImpl* idb_context = |
| 109 static_cast<IndexedDBContextImpl*>( |
| 110 BrowserContext::GetIndexedDBContext(&browser_context)); |
| 111 |
| 112 // Override the storage policy with our own. |
| 113 idb_context->special_storage_policy_ = special_storage_policy; |
| 114 idb_context->set_data_path_for_testing(temp_dir.path()); |
| 115 |
| 116 normal_path = idb_context->GetFilePathForTesting( |
| 117 DatabaseUtil::GetOriginIdentifier(kNormalOrigin)); |
| 118 session_only_path = idb_context->GetFilePathForTesting( |
| 119 DatabaseUtil::GetOriginIdentifier(kSessionOnlyOrigin)); |
| 120 ASSERT_TRUE(file_util::CreateDirectory(normal_path)); |
| 121 ASSERT_TRUE(file_util::CreateDirectory(session_only_path)); |
| 122 message_loop_.RunAllPending(); |
| 123 } |
| 124 |
| 125 message_loop_.RunAllPending(); |
| 126 |
| 127 EXPECT_TRUE(file_util::DirectoryExists(normal_path)); |
| 128 EXPECT_FALSE(file_util::DirectoryExists(session_only_path)); |
| 129 } |
| 130 |
| 131 TEST_F(IndexedDBTest, SaveSessionState) { |
| 132 ScopedTempDir temp_dir; |
| 133 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 134 |
| 135 FilePath normal_path; |
| 136 FilePath session_only_path; |
| 137 |
| 138 // Create the scope which will ensure we run the destructor of the webkit |
| 139 // context. |
| 140 { |
| 141 TestBrowserContext browser_context; |
| 142 |
| 143 const GURL kNormalOrigin("http://normal/"); |
| 144 const GURL kSessionOnlyOrigin("http://session-only/"); |
| 145 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 146 new quota::MockSpecialStoragePolicy; |
| 147 special_storage_policy->AddSessionOnly(kSessionOnlyOrigin); |
| 148 |
| 149 // Create some indexedDB paths. |
| 150 // With the levelDB backend, these are directories. |
| 151 IndexedDBContextImpl* idb_context = |
| 152 static_cast<IndexedDBContextImpl*>( |
| 153 BrowserContext::GetIndexedDBContext(&browser_context)); |
| 154 |
| 155 // Override the storage policy with our own. |
| 156 idb_context->special_storage_policy_ = special_storage_policy; |
| 157 idb_context->set_clear_local_state_on_exit(true); |
| 158 idb_context->set_data_path_for_testing(temp_dir.path()); |
| 159 |
| 160 // Save session state. This should bypass the destruction-time deletion. |
| 161 idb_context->SaveSessionState(); |
| 162 |
| 163 normal_path = idb_context->GetFilePathForTesting( |
| 164 DatabaseUtil::GetOriginIdentifier(kNormalOrigin)); |
| 165 session_only_path = idb_context->GetFilePathForTesting( |
| 166 DatabaseUtil::GetOriginIdentifier(kSessionOnlyOrigin)); |
| 167 ASSERT_TRUE(file_util::CreateDirectory(normal_path)); |
| 168 ASSERT_TRUE(file_util::CreateDirectory(session_only_path)); |
| 169 message_loop_.RunAllPending(); |
| 170 } |
| 171 |
| 172 // Make sure we wait until the destructor has run. |
| 173 message_loop_.RunAllPending(); |
| 174 |
| 175 // No data was cleared because of SaveSessionState. |
| 176 EXPECT_TRUE(file_util::DirectoryExists(normal_path)); |
| 177 EXPECT_TRUE(file_util::DirectoryExists(session_only_path)); |
| 178 } |
OLD | NEW |