OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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. | |
tzik
2014/12/03 08:35:49
insert an empty line here?
| |
4 #include "base/callback_forward.h" | |
5 #include "base/message_loop/message_loop.h" | |
6 #include "base/threading/sequenced_worker_pool.h" | |
7 #include "chrome/browser/extensions/app_data_migrator.h" | |
8 #include "chrome/browser/extensions/extension_special_storage_policy.h" | |
9 #include "chrome/test/base/testing_profile.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 #include "content/public/browser/indexed_db_context.h" | |
12 #include "content/public/browser/storage_partition.h" | |
13 #include "content/public/test/mock_blob_url_request_context.h" | |
14 #include "content/public/test/test_browser_thread_bundle.h" | |
15 #include "extensions/browser/extension_registry.h" | |
16 #include "extensions/common/extension.h" | |
17 #include "extensions/common/extension_builder.h" | |
18 #include "extensions/common/manifest.h" | |
19 #include "storage/browser/fileapi/file_system_context.h" | |
20 #include "storage/browser/fileapi/file_system_operation_runner.h" | |
21 #include "storage/browser/fileapi/file_system_url.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 | |
24 namespace { | |
25 scoped_ptr<TestingProfile> GetTestingProfile() { | |
26 TestingProfile::Builder profile_builder; | |
27 return profile_builder.Build(); | |
28 } | |
29 } | |
30 namespace extensions { | |
31 | |
32 class AppDataMigratorTest : public testing::Test { | |
33 public: | |
34 AppDataMigratorTest() | |
35 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} | |
36 | |
37 void SetUp() override { | |
38 profile_ = GetTestingProfile(); | |
39 registry_ = ExtensionRegistry::Get(profile_.get()); | |
40 migrator_ = scoped_ptr<AppDataMigrator>( | |
41 new AppDataMigrator(profile_.get(), registry_)); | |
42 | |
43 default_partition_ = | |
44 content::BrowserContext::GetDefaultStoragePartition(profile_.get()); | |
45 | |
46 idb_context_ = default_partition_->GetIndexedDBContext(); | |
47 idb_context_->SetTaskRunnerForTesting( | |
48 base::MessageLoop::current()->message_loop_proxy().get()); | |
49 | |
50 default_fs_context_ = default_partition_->GetFileSystemContext(); | |
51 | |
52 url_request_context_ = scoped_ptr<content::MockBlobURLRequestContext>( | |
53 new content::MockBlobURLRequestContext(default_fs_context_)); | |
54 } | |
55 | |
56 void TearDown() override {} | |
57 | |
58 protected: | |
59 content::TestBrowserThreadBundle thread_bundle_; | |
60 scoped_ptr<TestingProfile> profile_; | |
61 scoped_ptr<AppDataMigrator> migrator_; | |
62 content::StoragePartition* default_partition_; | |
63 ExtensionRegistry* registry_; | |
64 storage::FileSystemContext* default_fs_context_; | |
65 content::IndexedDBContext* idb_context_; | |
66 scoped_ptr<content::MockBlobURLRequestContext> url_request_context_; | |
67 }; | |
68 | |
69 scoped_refptr<const Extension> GetTestExtension(bool platform_app) { | |
70 scoped_refptr<const Extension> app; | |
71 if (platform_app) { | |
72 app = ExtensionBuilder() | |
73 .SetManifest( | |
74 DictionaryBuilder() | |
75 .Set("name", "test app") | |
76 .Set("version", "1") | |
77 .Set("app", | |
78 DictionaryBuilder().Set( | |
79 "background", | |
80 DictionaryBuilder().Set( | |
81 "scripts", | |
82 ListBuilder().Append("background.js")))) | |
83 .Set("permissions", | |
84 ListBuilder().Append("unlimitedStorage"))) | |
85 .Build(); | |
86 } else { | |
87 app = ExtensionBuilder() | |
88 .SetManifest(DictionaryBuilder() | |
89 .Set("name", "test app") | |
90 .Set("version", "1") | |
91 .Set("app", | |
92 DictionaryBuilder().Set( | |
93 "launch", | |
94 DictionaryBuilder().Set("local_path", | |
95 "index.html"))) | |
96 .Set("permissions", | |
97 ListBuilder().Append("unlimitedStorage"))) | |
98 .Build(); | |
99 } | |
100 return app; | |
101 } | |
102 | |
103 void MigrationCallback() { | |
104 } | |
105 | |
106 void DidWrite(base::File::Error status, int64 bytes, bool complete) { | |
107 base::MessageLoop::current()->Quit(); | |
108 } | |
109 | |
110 void DidCreate(base::File::Error status) { | |
111 } | |
112 | |
113 void DidOpenFileSystem(const GURL& root, | |
114 const std::string& name, | |
115 base::File::Error result) { | |
116 } | |
117 | |
118 void OpenFileSystems(storage::FileSystemContext* fs_context, | |
119 GURL extension_url) { | |
120 fs_context->OpenFileSystem(extension_url, | |
121 storage::kFileSystemTypeTemporary, | |
122 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
123 base::Bind(&DidOpenFileSystem)); | |
124 | |
125 fs_context->OpenFileSystem(extension_url, | |
126 storage::kFileSystemTypePersistent, | |
127 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
128 base::Bind(&DidOpenFileSystem)); | |
129 base::MessageLoop::current()->RunUntilIdle(); | |
130 } | |
131 | |
132 void GenerateTestFiles(content::MockBlobURLRequestContext* url_request_context, | |
133 const Extension* ext, | |
134 storage::FileSystemContext* fs_context, | |
135 Profile* profile) { | |
136 profile->GetExtensionSpecialStoragePolicy()->GrantRightsForExtension(ext, | |
137 profile); | |
138 | |
139 base::FilePath path(FILE_PATH_LITERAL("test.txt")); | |
140 GURL extension_url = | |
141 extensions::Extension::GetBaseURLFromExtensionId(ext->id()); | |
142 | |
143 OpenFileSystems(fs_context, extension_url); | |
144 | |
145 storage::FileSystemURL fs_temp_url = fs_context->CreateCrackedFileSystemURL( | |
146 extension_url, storage::kFileSystemTypeTemporary, path); | |
147 | |
148 storage::FileSystemURL fs_persistent_url = | |
149 fs_context->CreateCrackedFileSystemURL( | |
150 extension_url, storage::kFileSystemTypePersistent, path); | |
151 | |
152 content::ScopedTextBlob blob1( | |
153 *url_request_context, "blob-id:success1", "Hello, world!\n"); | |
154 | |
155 fs_context->operation_runner()->CreateFile( | |
156 fs_temp_url, false, base::Bind(&DidCreate)); | |
157 | |
158 fs_context->operation_runner()->CreateFile( | |
159 fs_persistent_url, false, base::Bind(&DidCreate)); | |
160 base::MessageLoop::current()->RunUntilIdle(); | |
161 | |
162 fs_context->operation_runner()->Write(url_request_context, | |
163 fs_temp_url, | |
164 blob1.GetBlobDataHandle(), | |
165 0, | |
166 base::Bind(&DidWrite)); | |
167 base::MessageLoop::current()->Run(); | |
168 fs_context->operation_runner()->Write(url_request_context, | |
169 fs_persistent_url, | |
170 blob1.GetBlobDataHandle(), | |
171 0, | |
172 base::Bind(&DidWrite)); | |
173 base::MessageLoop::current()->Run(); | |
174 } | |
175 | |
176 void VerifyFileContents(base::File file, | |
177 const base::Closure& on_close_callback) { | |
178 ASSERT_EQ(14, file.GetLength()); | |
179 scoped_ptr<char[]> buffer(new char[15]); | |
180 | |
181 file.Read(0, buffer.get(), 14); | |
182 buffer.get()[14] = 0; | |
183 | |
184 std::string expected = "Hello, world!\n"; | |
185 std::string actual = buffer.get(); | |
186 EXPECT_EQ(expected, actual); | |
187 | |
188 file.Close(); | |
189 if (!on_close_callback.is_null()) | |
190 on_close_callback.Run(); | |
191 base::MessageLoop::current()->Quit(); | |
192 } | |
193 | |
194 void VerifyTestFilesMigrated(content::StoragePartition* new_partition, | |
195 const Extension* new_ext) { | |
196 GURL extension_url = | |
197 extensions::Extension::GetBaseURLFromExtensionId(new_ext->id()); | |
198 storage::FileSystemContext* new_fs_context = | |
199 new_partition->GetFileSystemContext(); | |
200 | |
201 OpenFileSystems(new_fs_context, extension_url); | |
202 | |
203 base::FilePath path(FILE_PATH_LITERAL("test.txt")); | |
204 | |
205 storage::FileSystemURL fs_temp_url = | |
206 new_fs_context->CreateCrackedFileSystemURL( | |
207 extension_url, storage::kFileSystemTypeTemporary, path); | |
208 storage::FileSystemURL fs_persistent_url = | |
209 new_fs_context->CreateCrackedFileSystemURL( | |
210 extension_url, storage::kFileSystemTypePersistent, path); | |
211 | |
212 new_fs_context->operation_runner()->OpenFile( | |
213 fs_temp_url, | |
214 base::File::FLAG_READ | base::File::FLAG_OPEN, | |
215 base::Bind(&VerifyFileContents)); | |
216 base::MessageLoop::current()->Run(); | |
217 new_fs_context->operation_runner()->OpenFile( | |
218 fs_persistent_url, | |
219 base::File::FLAG_READ | base::File::FLAG_OPEN, | |
220 base::Bind(&VerifyFileContents)); | |
221 base::MessageLoop::current()->Run(); | |
222 } | |
223 | |
224 TEST_F(AppDataMigratorTest, ShouldMigrate) { | |
225 scoped_refptr<const Extension> old_ext = GetTestExtension(false); | |
226 scoped_refptr<const Extension> new_ext = GetTestExtension(true); | |
227 | |
228 EXPECT_TRUE(migrator_->NeedsMigration(old_ext, new_ext)); | |
229 } | |
230 | |
231 TEST_F(AppDataMigratorTest, ShouldNotMigratePlatformApp) { | |
232 scoped_refptr<const Extension> old_ext = GetTestExtension(true); | |
233 scoped_refptr<const Extension> new_ext = GetTestExtension(true); | |
234 | |
235 EXPECT_FALSE(migrator_->NeedsMigration(old_ext, new_ext)); | |
236 } | |
237 | |
238 TEST_F(AppDataMigratorTest, ShouldNotMigrateLegacyApp) { | |
239 scoped_refptr<const Extension> old_ext = GetTestExtension(false); | |
240 scoped_refptr<const Extension> new_ext = GetTestExtension(false); | |
241 | |
242 EXPECT_FALSE(migrator_->NeedsMigration(old_ext, new_ext)); | |
243 } | |
244 | |
245 TEST_F(AppDataMigratorTest, NoOpMigration) { | |
246 scoped_refptr<const Extension> old_ext = GetTestExtension(false); | |
247 scoped_refptr<const Extension> new_ext = GetTestExtension(true); | |
248 | |
249 // Nothing to migrate. Basically this should just not cause an error | |
250 migrator_->DoMigrationAndReply( | |
251 old_ext, new_ext, base::Bind(&MigrationCallback)); | |
252 } | |
253 | |
254 TEST_F(AppDataMigratorTest, FileSystemMigration) { | |
255 scoped_refptr<const Extension> old_ext = GetTestExtension(false); | |
256 scoped_refptr<const Extension> new_ext = GetTestExtension(true); | |
257 | |
258 GenerateTestFiles(url_request_context_.get(), | |
259 old_ext.get(), | |
260 default_fs_context_, | |
261 profile_.get()); | |
262 | |
263 migrator_->DoMigrationAndReply( | |
264 old_ext, new_ext, base::Bind(&MigrationCallback)); | |
265 | |
266 base::MessageLoop::current()->RunUntilIdle(); | |
267 | |
268 registry_->AddEnabled(new_ext); | |
269 GURL extension_url = | |
270 extensions::Extension::GetBaseURLFromExtensionId(new_ext->id()); | |
271 | |
272 content::StoragePartition* new_partition = | |
273 content::BrowserContext::GetStoragePartitionForSite(profile_.get(), | |
274 extension_url); | |
275 | |
276 ASSERT_NE(new_partition->GetPath(), default_partition_->GetPath()); | |
277 | |
278 VerifyTestFilesMigrated(new_partition, new_ext); | |
279 } | |
280 } // namespace extensions | |
OLD | NEW |