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