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
|
// Copyright 2015 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 <stdint.h>
#include <string>
#include <utility>
#include "base/callback_forward.h"
#include "base/run_loop.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/extensions/app_data_migrator.h"
#include "chrome/browser/extensions/extension_special_storage_policy.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/indexed_db_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/manifest.h"
#include "storage/browser/fileapi/file_system_context.h"
#include "storage/browser/fileapi/file_system_operation_runner.h"
#include "storage/browser/fileapi/file_system_url.h"
#include "storage/browser/test/mock_blob_url_request_context.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
std::unique_ptr<TestingProfile> GetTestingProfile() {
TestingProfile::Builder profile_builder;
return profile_builder.Build();
}
}
namespace extensions {
class AppDataMigratorTest : public testing::Test {
public:
AppDataMigratorTest()
: thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
void SetUp() override {
profile_ = GetTestingProfile();
registry_ = ExtensionRegistry::Get(profile_.get());
migrator_ = std::unique_ptr<AppDataMigrator>(
new AppDataMigrator(profile_.get(), registry_));
default_partition_ =
content::BrowserContext::GetDefaultStoragePartition(profile_.get());
idb_context_ = default_partition_->GetIndexedDBContext();
default_fs_context_ = default_partition_->GetFileSystemContext();
url_request_context_ = std::unique_ptr<content::MockBlobURLRequestContext>(
new content::MockBlobURLRequestContext());
}
void TearDown() override {}
protected:
content::TestBrowserThreadBundle thread_bundle_;
std::unique_ptr<TestingProfile> profile_;
std::unique_ptr<AppDataMigrator> migrator_;
content::StoragePartition* default_partition_;
ExtensionRegistry* registry_;
storage::FileSystemContext* default_fs_context_;
content::IndexedDBContext* idb_context_;
std::unique_ptr<content::MockBlobURLRequestContext> url_request_context_;
};
scoped_refptr<const Extension> GetTestExtension(bool platform_app) {
scoped_refptr<const Extension> app;
if (platform_app) {
app = ExtensionBuilder()
.SetManifest(
DictionaryBuilder()
.Set("name", "test app")
.Set("version", "1")
.Set("app", DictionaryBuilder()
.Set("background",
DictionaryBuilder()
.Set("scripts",
ListBuilder()
.Append("background.js")
.Build())
.Build())
.Build())
.Set("permissions",
ListBuilder().Append("unlimitedStorage").Build())
.Build())
.Build();
} else {
app = ExtensionBuilder()
.SetManifest(
DictionaryBuilder()
.Set("name", "test app")
.Set("version", "1")
.Set("app", DictionaryBuilder()
.Set("launch",
DictionaryBuilder()
.Set("local_path", "index.html")
.Build())
.Build())
.Set("permissions",
ListBuilder().Append("unlimitedStorage").Build())
.Build())
.Build();
}
return app;
}
void MigrationCallback() {
}
void DidWrite(base::File::Error status, int64_t bytes, bool complete) {
base::RunLoop::QuitCurrentWhenIdleDeprecated();
}
void DidCreate(base::File::Error status) {
}
void DidOpenFileSystem(const GURL& root,
const std::string& name,
base::File::Error result) {
}
void OpenFileSystems(storage::FileSystemContext* fs_context,
GURL extension_url) {
fs_context->OpenFileSystem(extension_url, storage::kFileSystemTypeTemporary,
storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
base::BindOnce(&DidOpenFileSystem));
fs_context->OpenFileSystem(extension_url, storage::kFileSystemTypePersistent,
storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
base::BindOnce(&DidOpenFileSystem));
content::RunAllTasksUntilIdle();
}
void GenerateTestFiles(content::MockBlobURLRequestContext* url_request_context,
const Extension* ext,
storage::FileSystemContext* fs_context,
Profile* profile) {
profile->GetExtensionSpecialStoragePolicy()->GrantRightsForExtension(ext,
profile);
base::FilePath path(FILE_PATH_LITERAL("test.txt"));
GURL extension_url =
extensions::Extension::GetBaseURLFromExtensionId(ext->id());
OpenFileSystems(fs_context, extension_url);
storage::FileSystemURL fs_temp_url = fs_context->CreateCrackedFileSystemURL(
extension_url, storage::kFileSystemTypeTemporary, path);
storage::FileSystemURL fs_persistent_url =
fs_context->CreateCrackedFileSystemURL(
extension_url, storage::kFileSystemTypePersistent, path);
content::ScopedTextBlob blob1(*url_request_context, "blob-id:success1",
"Hello, world!\n");
fs_context->operation_runner()->CreateFile(fs_temp_url, false,
base::Bind(&DidCreate));
fs_context->operation_runner()->CreateFile(fs_persistent_url, false,
base::Bind(&DidCreate));
content::RunAllTasksUntilIdle();
fs_context->operation_runner()->Write(fs_temp_url, blob1.GetBlobDataHandle(),
0, base::BindRepeating(&DidWrite));
content::RunAllTasksUntilIdle();
fs_context->operation_runner()->Write(fs_persistent_url,
blob1.GetBlobDataHandle(), 0,
base::BindRepeating(&DidWrite));
content::RunAllTasksUntilIdle();
}
void VerifyFileContents(base::File file, base::OnceClosure on_close_callback) {
ASSERT_EQ(14, file.GetLength());
std::unique_ptr<char[]> buffer(new char[15]);
file.Read(0, buffer.get(), 14);
buffer.get()[14] = 0;
std::string expected = "Hello, world!\n";
std::string actual = buffer.get();
EXPECT_EQ(expected, actual);
file.Close();
if (!on_close_callback.is_null())
std::move(on_close_callback).Run();
base::RunLoop::QuitCurrentWhenIdleDeprecated();
}
void VerifyTestFilesMigrated(content::StoragePartition* new_partition,
const Extension* new_ext) {
GURL extension_url =
extensions::Extension::GetBaseURLFromExtensionId(new_ext->id());
storage::FileSystemContext* new_fs_context =
new_partition->GetFileSystemContext();
OpenFileSystems(new_fs_context, extension_url);
base::FilePath path(FILE_PATH_LITERAL("test.txt"));
storage::FileSystemURL fs_temp_url =
new_fs_context->CreateCrackedFileSystemURL(
extension_url, storage::kFileSystemTypeTemporary, path);
storage::FileSystemURL fs_persistent_url =
new_fs_context->CreateCrackedFileSystemURL(
extension_url, storage::kFileSystemTypePersistent, path);
new_fs_context->operation_runner()->OpenFile(
fs_temp_url, base::File::FLAG_READ | base::File::FLAG_OPEN,
base::Bind(&VerifyFileContents));
content::RunAllTasksUntilIdle();
new_fs_context->operation_runner()->OpenFile(
fs_persistent_url, base::File::FLAG_READ | base::File::FLAG_OPEN,
base::Bind(&VerifyFileContents));
content::RunAllTasksUntilIdle();
}
TEST_F(AppDataMigratorTest, ShouldMigrate) {
scoped_refptr<const Extension> old_ext = GetTestExtension(false);
scoped_refptr<const Extension> new_ext = GetTestExtension(true);
EXPECT_TRUE(AppDataMigrator::NeedsMigration(old_ext.get(), new_ext.get()));
}
TEST_F(AppDataMigratorTest, ShouldNotMigratePlatformApp) {
scoped_refptr<const Extension> old_ext = GetTestExtension(true);
scoped_refptr<const Extension> new_ext = GetTestExtension(true);
EXPECT_FALSE(AppDataMigrator::NeedsMigration(old_ext.get(), new_ext.get()));
}
TEST_F(AppDataMigratorTest, ShouldNotMigrateLegacyApp) {
scoped_refptr<const Extension> old_ext = GetTestExtension(false);
scoped_refptr<const Extension> new_ext = GetTestExtension(false);
EXPECT_FALSE(AppDataMigrator::NeedsMigration(old_ext.get(), new_ext.get()));
}
TEST_F(AppDataMigratorTest, NoOpMigration) {
scoped_refptr<const Extension> old_ext = GetTestExtension(false);
scoped_refptr<const Extension> new_ext = GetTestExtension(true);
// Nothing to migrate. Basically this should just not cause an error
migrator_->DoMigrationAndReply(old_ext.get(), new_ext.get(),
base::Bind(&MigrationCallback));
}
// crbug.com/747589
TEST_F(AppDataMigratorTest, DISABLED_FileSystemMigration) {
scoped_refptr<const Extension> old_ext = GetTestExtension(false);
scoped_refptr<const Extension> new_ext = GetTestExtension(true);
GenerateTestFiles(url_request_context_.get(), old_ext.get(),
default_fs_context_, profile_.get());
migrator_->DoMigrationAndReply(old_ext.get(), new_ext.get(),
base::Bind(&MigrationCallback));
content::RunAllTasksUntilIdle();
registry_->AddEnabled(new_ext);
GURL extension_url =
extensions::Extension::GetBaseURLFromExtensionId(new_ext->id());
content::StoragePartition* new_partition =
content::BrowserContext::GetStoragePartitionForSite(profile_.get(),
extension_url);
ASSERT_NE(new_partition->GetPath(), default_partition_->GetPath());
VerifyTestFilesMigrated(new_partition, new_ext.get());
// Clean up.
content::RunAllTasksUntilIdle();
}
} // namespace extensions
|