1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/test/test_simple_task_runner.h"
#include "base/threading/thread.h"
#include "content/browser/browser_thread_impl.h"
#include "content/browser/indexed_db/indexed_db_connection.h"
#include "content/browser/indexed_db/indexed_db_context_impl.h"
#include "content/browser/indexed_db/indexed_db_factory_impl.h"
#include "content/browser/indexed_db/mock_indexed_db_callbacks.h"
#include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/mock_special_storage_policy.h"
#include "content/public/test/test_browser_context.h"
#include "storage/browser/quota/quota_manager.h"
#include "storage/browser/quota/special_storage_policy.h"
#include "storage/common/database/database_identifier.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
class IndexedDBTest : public testing::Test {
public:
const GURL kNormalOrigin;
const GURL kSessionOnlyOrigin;
IndexedDBTest()
: kNormalOrigin("http://normal/"),
kSessionOnlyOrigin("http://session-only/"),
task_runner_(new base::TestSimpleTaskRunner),
special_storage_policy_(new MockSpecialStoragePolicy),
file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_),
io_thread_(BrowserThread::IO, &message_loop_) {
special_storage_policy_->AddSessionOnly(kSessionOnlyOrigin);
}
protected:
void FlushIndexedDBTaskRunner() { task_runner_->RunUntilIdle(); }
base::MessageLoopForIO message_loop_;
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_;
private:
BrowserThreadImpl file_thread_;
BrowserThreadImpl io_thread_;
DISALLOW_COPY_AND_ASSIGN(IndexedDBTest);
};
TEST_F(IndexedDBTest, ClearSessionOnlyDatabases) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath normal_path;
base::FilePath session_only_path;
// Create the scope which will ensure we run the destructor of the context
// which should trigger the clean up.
{
scoped_refptr<IndexedDBContextImpl> idb_context =
new IndexedDBContextImpl(temp_dir.path(),
special_storage_policy_.get(),
NULL,
task_runner_.get());
normal_path = idb_context->GetFilePathForTesting(
storage::GetIdentifierFromOrigin(kNormalOrigin));
session_only_path = idb_context->GetFilePathForTesting(
storage::GetIdentifierFromOrigin(kSessionOnlyOrigin));
ASSERT_TRUE(base::CreateDirectory(normal_path));
ASSERT_TRUE(base::CreateDirectory(session_only_path));
FlushIndexedDBTaskRunner();
message_loop_.RunUntilIdle();
}
FlushIndexedDBTaskRunner();
message_loop_.RunUntilIdle();
EXPECT_TRUE(base::DirectoryExists(normal_path));
EXPECT_FALSE(base::DirectoryExists(session_only_path));
}
TEST_F(IndexedDBTest, SetForceKeepSessionState) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath normal_path;
base::FilePath session_only_path;
// Create the scope which will ensure we run the destructor of the context.
{
// Create some indexedDB paths.
// With the levelDB backend, these are directories.
scoped_refptr<IndexedDBContextImpl> idb_context =
new IndexedDBContextImpl(temp_dir.path(),
special_storage_policy_.get(),
NULL,
task_runner_.get());
// Save session state. This should bypass the destruction-time deletion.
idb_context->SetForceKeepSessionState();
normal_path = idb_context->GetFilePathForTesting(
storage::GetIdentifierFromOrigin(kNormalOrigin));
session_only_path = idb_context->GetFilePathForTesting(
storage::GetIdentifierFromOrigin(kSessionOnlyOrigin));
ASSERT_TRUE(base::CreateDirectory(normal_path));
ASSERT_TRUE(base::CreateDirectory(session_only_path));
message_loop_.RunUntilIdle();
}
// Make sure we wait until the destructor has run.
message_loop_.RunUntilIdle();
// No data was cleared because of SetForceKeepSessionState.
EXPECT_TRUE(base::DirectoryExists(normal_path));
EXPECT_TRUE(base::DirectoryExists(session_only_path));
}
class ForceCloseDBCallbacks : public IndexedDBCallbacks {
public:
ForceCloseDBCallbacks(scoped_refptr<IndexedDBContextImpl> idb_context,
const GURL& origin_url)
: IndexedDBCallbacks(NULL, 0, 0),
idb_context_(idb_context),
origin_url_(origin_url) {}
void OnSuccess() override {}
void OnSuccess(const std::vector<base::string16>&) override {}
void OnSuccess(scoped_ptr<IndexedDBConnection> connection,
const IndexedDBDatabaseMetadata& metadata) override {
connection_ = connection.Pass();
idb_context_->ConnectionOpened(origin_url_, connection_.get());
}
IndexedDBConnection* connection() { return connection_.get(); }
protected:
~ForceCloseDBCallbacks() override {}
private:
scoped_refptr<IndexedDBContextImpl> idb_context_;
GURL origin_url_;
scoped_ptr<IndexedDBConnection> connection_;
DISALLOW_COPY_AND_ASSIGN(ForceCloseDBCallbacks);
};
TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnDelete) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
scoped_refptr<MockIndexedDBDatabaseCallbacks> open_db_callbacks(
new MockIndexedDBDatabaseCallbacks());
scoped_refptr<MockIndexedDBDatabaseCallbacks> closed_db_callbacks(
new MockIndexedDBDatabaseCallbacks());
base::FilePath test_path;
// Create the scope which will ensure we run the destructor of the context.
{
TestBrowserContext browser_context;
const GURL kTestOrigin("http://test/");
scoped_refptr<IndexedDBContextImpl> idb_context =
new IndexedDBContextImpl(temp_dir.path(),
special_storage_policy_.get(),
NULL,
task_runner_.get());
scoped_refptr<ForceCloseDBCallbacks> open_callbacks =
new ForceCloseDBCallbacks(idb_context, kTestOrigin);
scoped_refptr<ForceCloseDBCallbacks> closed_callbacks =
new ForceCloseDBCallbacks(idb_context, kTestOrigin);
IndexedDBFactory* factory = idb_context->GetIDBFactory();
test_path = idb_context->GetFilePathForTesting(
storage::GetIdentifierFromOrigin(kTestOrigin));
IndexedDBPendingConnection open_connection(open_callbacks,
open_db_callbacks,
0 /* child_process_id */,
0 /* host_transaction_id */,
0 /* version */);
factory->Open(base::ASCIIToUTF16("opendb"),
open_connection,
NULL /* request_context */,
kTestOrigin,
idb_context->data_path());
IndexedDBPendingConnection closed_connection(closed_callbacks,
closed_db_callbacks,
0 /* child_process_id */,
0 /* host_transaction_id */,
0 /* version */);
factory->Open(base::ASCIIToUTF16("closeddb"),
closed_connection,
NULL /* request_context */,
kTestOrigin,
idb_context->data_path());
closed_callbacks->connection()->Close();
idb_context->TaskRunner()->PostTask(
FROM_HERE,
base::Bind(
&IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin));
FlushIndexedDBTaskRunner();
message_loop_.RunUntilIdle();
}
// Make sure we wait until the destructor has run.
message_loop_.RunUntilIdle();
EXPECT_TRUE(open_db_callbacks->forced_close_called());
EXPECT_FALSE(closed_db_callbacks->forced_close_called());
EXPECT_FALSE(base::DirectoryExists(test_path));
}
TEST_F(IndexedDBTest, DeleteFailsIfDirectoryLocked) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const GURL kTestOrigin("http://test/");
scoped_refptr<IndexedDBContextImpl> idb_context = new IndexedDBContextImpl(
temp_dir.path(), special_storage_policy_.get(), NULL, task_runner_.get());
base::FilePath test_path = idb_context->GetFilePathForTesting(
storage::GetIdentifierFromOrigin(kTestOrigin));
ASSERT_TRUE(base::CreateDirectory(test_path));
scoped_ptr<LevelDBLock> lock =
LevelDBDatabase::LockForTesting(test_path);
ASSERT_TRUE(lock);
idb_context->TaskRunner()->PostTask(
FROM_HERE,
base::Bind(
&IndexedDBContextImpl::DeleteForOrigin, idb_context, kTestOrigin));
FlushIndexedDBTaskRunner();
EXPECT_TRUE(base::DirectoryExists(test_path));
}
TEST_F(IndexedDBTest, ForceCloseOpenDatabasesOnCommitFailure) {
const GURL kTestOrigin("http://test/");
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
scoped_refptr<IndexedDBContextImpl> context = new IndexedDBContextImpl(
temp_dir.path(), special_storage_policy_.get(), NULL, task_runner_.get());
scoped_refptr<IndexedDBFactoryImpl> factory =
static_cast<IndexedDBFactoryImpl*>(context->GetIDBFactory());
scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks());
scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks(
new MockIndexedDBDatabaseCallbacks());
const int64 transaction_id = 1;
IndexedDBPendingConnection connection(
callbacks,
db_callbacks,
0 /* child_process_id */,
transaction_id,
IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION);
factory->Open(base::ASCIIToUTF16("db"),
connection,
NULL /* request_context */,
kTestOrigin,
temp_dir.path());
EXPECT_TRUE(callbacks->connection());
// ConnectionOpened() is usually called by the dispatcher.
context->ConnectionOpened(kTestOrigin, callbacks->connection());
EXPECT_TRUE(factory->IsBackingStoreOpen(kTestOrigin));
// Simulate the write failure.
leveldb::Status status = leveldb::Status::IOError("Simulated failure");
callbacks->connection()->database()->TransactionCommitFailed(status);
EXPECT_TRUE(db_callbacks->forced_close_called());
EXPECT_FALSE(factory->IsBackingStoreOpen(kTestOrigin));
}
} // namespace content
|