Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / mount_path_util_unittest.cc
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 "chrome/browser/chromeos/file_system_provider/mount_path_util.h"
6
7 #include <string>
8
9 #include "base/files/file.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system.h"
12 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
13 #include "chrome/browser/chromeos/file_system_provider/service.h"
14 #include "chrome/browser/chromeos/file_system_provider/service_factory.h"
15 #include "chrome/browser/chromeos/login/users/fake_user_manager.h"
16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "chrome/test/base/testing_profile_manager.h"
21 #include "components/keyed_service/core/keyed_service.h"
22 #include "content/public/browser/browser_context.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "extensions/browser/extension_registry.h"
25 #include "storage/browser/fileapi/external_mount_points.h"
26 #include "storage/browser/fileapi/isolated_context.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28
29 namespace chromeos {
30 namespace file_system_provider {
31 namespace util {
32
33 namespace {
34
35 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
36 const char kFileSystemId[] = "File/System/Id";
37 const char kDisplayName[] = "Camera Pictures";
38
39 // Creates a FileSystemURL for tests.
40 storage::FileSystemURL CreateFileSystemURL(
41     Profile* profile,
42     const ProvidedFileSystemInfo& file_system_info,
43     const base::FilePath& file_path) {
44   const std::string origin =
45       std::string("chrome-extension://") + file_system_info.extension_id();
46   const base::FilePath mount_path = file_system_info.mount_path();
47   const storage::ExternalMountPoints* const mount_points =
48       storage::ExternalMountPoints::GetSystemInstance();
49   DCHECK(mount_points);
50   DCHECK(file_path.IsAbsolute());
51   base::FilePath relative_path(file_path.value().substr(1));
52   return mount_points->CreateCrackedFileSystemURL(
53       GURL(origin),
54       storage::kFileSystemTypeExternal,
55       base::FilePath(mount_path.BaseName().Append(relative_path)));
56 }
57
58 // Creates a Service instance. Used to be able to destroy the service in
59 // TearDown().
60 KeyedService* CreateService(content::BrowserContext* context) {
61   return new Service(Profile::FromBrowserContext(context),
62                      extensions::ExtensionRegistry::Get(context));
63 }
64
65 }  // namespace
66
67 class FileSystemProviderMountPathUtilTest : public testing::Test {
68  protected:
69   FileSystemProviderMountPathUtilTest() {}
70   virtual ~FileSystemProviderMountPathUtilTest() {}
71
72   virtual void SetUp() override {
73     profile_manager_.reset(
74         new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
75     ASSERT_TRUE(profile_manager_->SetUp());
76     profile_ = profile_manager_->CreateTestingProfile("testing-profile");
77     user_manager_ = new FakeUserManager();
78     user_manager_enabler_.reset(new ScopedUserManagerEnabler(user_manager_));
79     user_manager_->AddUser(profile_->GetProfileName());
80     ServiceFactory::GetInstance()->SetTestingFactory(profile_, &CreateService);
81     file_system_provider_service_ = Service::Get(profile_);
82     file_system_provider_service_->SetFileSystemFactoryForTesting(
83         base::Bind(&FakeProvidedFileSystem::Create));
84   }
85
86   virtual void TearDown() override {
87     // Setting the testing factory to NULL will destroy the created service
88     // associated with the testing profile.
89     ServiceFactory::GetInstance()->SetTestingFactory(profile_, NULL);
90   }
91
92   content::TestBrowserThreadBundle thread_bundle_;
93   scoped_ptr<TestingProfileManager> profile_manager_;
94   TestingProfile* profile_;  // Owned by TestingProfileManager.
95   scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_;
96   FakeUserManager* user_manager_;
97   Service* file_system_provider_service_;  // Owned by its factory.
98 };
99
100 TEST_F(FileSystemProviderMountPathUtilTest, GetMountPath) {
101   const base::FilePath result =
102       GetMountPath(profile_, kExtensionId, kFileSystemId);
103   const std::string expected =
104       "/provided/mbflcebpggnecokmikipoihdbecnjfoj:"
105       "File%2FSystem%2FId:testing-profile-hash";
106   EXPECT_EQ(expected, result.AsUTF8Unsafe());
107 }
108
109 TEST_F(FileSystemProviderMountPathUtilTest, IsFileSystemProviderLocalPath) {
110   const base::FilePath mount_path =
111       GetMountPath(profile_, kExtensionId, kFileSystemId);
112   const base::FilePath file_path =
113       base::FilePath::FromUTF8Unsafe("/hello/world.txt");
114   const base::FilePath local_file_path =
115       mount_path.Append(base::FilePath(file_path.value().substr(1)));
116
117   EXPECT_TRUE(IsFileSystemProviderLocalPath(mount_path));
118   EXPECT_TRUE(IsFileSystemProviderLocalPath(local_file_path));
119
120   EXPECT_FALSE(IsFileSystemProviderLocalPath(
121       base::FilePath::FromUTF8Unsafe("provided/hello-world/test.txt")));
122   EXPECT_FALSE(IsFileSystemProviderLocalPath(
123       base::FilePath::FromUTF8Unsafe("/provided")));
124   EXPECT_FALSE(
125       IsFileSystemProviderLocalPath(base::FilePath::FromUTF8Unsafe("/")));
126   EXPECT_FALSE(IsFileSystemProviderLocalPath(base::FilePath()));
127 }
128
129 TEST_F(FileSystemProviderMountPathUtilTest, Parser) {
130   const bool result = file_system_provider_service_->MountFileSystem(
131       kExtensionId, MountOptions(kFileSystemId, kDisplayName));
132   ASSERT_TRUE(result);
133   const ProvidedFileSystemInfo file_system_info =
134       file_system_provider_service_->GetProvidedFileSystem(kExtensionId,
135                                                            kFileSystemId)
136           ->GetFileSystemInfo();
137
138   const base::FilePath kFilePath =
139       base::FilePath::FromUTF8Unsafe("/hello/world.txt");
140   const storage::FileSystemURL url =
141       CreateFileSystemURL(profile_, file_system_info, kFilePath);
142   EXPECT_TRUE(url.is_valid());
143
144   FileSystemURLParser parser(url);
145   EXPECT_TRUE(parser.Parse());
146
147   ProvidedFileSystemInterface* file_system = parser.file_system();
148   ASSERT_TRUE(file_system);
149   EXPECT_EQ(kFileSystemId, file_system->GetFileSystemInfo().file_system_id());
150   EXPECT_EQ(kFilePath.AsUTF8Unsafe(), parser.file_path().AsUTF8Unsafe());
151 }
152
153 TEST_F(FileSystemProviderMountPathUtilTest, Parser_RootPath) {
154   const bool result = file_system_provider_service_->MountFileSystem(
155       kExtensionId, MountOptions(kFileSystemId, kDisplayName));
156   ASSERT_TRUE(result);
157   const ProvidedFileSystemInfo file_system_info =
158       file_system_provider_service_->GetProvidedFileSystem(kExtensionId,
159                                                            kFileSystemId)
160           ->GetFileSystemInfo();
161
162   const base::FilePath kFilePath = base::FilePath::FromUTF8Unsafe("/");
163   const storage::FileSystemURL url =
164       CreateFileSystemURL(profile_, file_system_info, kFilePath);
165   EXPECT_TRUE(url.is_valid());
166
167   FileSystemURLParser parser(url);
168   EXPECT_TRUE(parser.Parse());
169
170   ProvidedFileSystemInterface* file_system = parser.file_system();
171   ASSERT_TRUE(file_system);
172   EXPECT_EQ(kFileSystemId, file_system->GetFileSystemInfo().file_system_id());
173   EXPECT_EQ(kFilePath.AsUTF8Unsafe(), parser.file_path().AsUTF8Unsafe());
174 }
175
176 TEST_F(FileSystemProviderMountPathUtilTest, Parser_WrongUrl) {
177   const ProvidedFileSystemInfo file_system_info(
178       kExtensionId,
179       MountOptions(kFileSystemId, kDisplayName),
180       GetMountPath(profile_, kExtensionId, kFileSystemId));
181
182   const base::FilePath kFilePath = base::FilePath::FromUTF8Unsafe("/hello");
183   const storage::FileSystemURL url =
184       CreateFileSystemURL(profile_, file_system_info, kFilePath);
185   // It is impossible to create a cracked URL for a mount point which doesn't
186   // exist, therefore is will always be invalid, and empty.
187   EXPECT_FALSE(url.is_valid());
188
189   FileSystemURLParser parser(url);
190   EXPECT_FALSE(parser.Parse());
191 }
192
193 TEST_F(FileSystemProviderMountPathUtilTest, Parser_IsolatedURL) {
194   const bool result = file_system_provider_service_->MountFileSystem(
195       kExtensionId, MountOptions(kFileSystemId, kDisplayName));
196   ASSERT_TRUE(result);
197   const ProvidedFileSystemInfo file_system_info =
198       file_system_provider_service_->GetProvidedFileSystem(kExtensionId,
199                                                            kFileSystemId)
200           ->GetFileSystemInfo();
201
202   const base::FilePath kFilePath =
203       base::FilePath::FromUTF8Unsafe("/hello/world.txt");
204   const storage::FileSystemURL url =
205       CreateFileSystemURL(profile_, file_system_info, kFilePath);
206   EXPECT_TRUE(url.is_valid());
207
208   // Create an isolated URL for the original one.
209   storage::IsolatedContext* const isolated_context =
210       storage::IsolatedContext::GetInstance();
211   const std::string isolated_file_system_id =
212       isolated_context->RegisterFileSystemForPath(
213           storage::kFileSystemTypeProvided,
214           url.filesystem_id(),
215           url.path(),
216           NULL);
217
218   const base::FilePath isolated_virtual_path =
219       isolated_context->CreateVirtualRootPath(isolated_file_system_id)
220           .Append(kFilePath.BaseName().value());
221
222   const storage::FileSystemURL isolated_url =
223       isolated_context->CreateCrackedFileSystemURL(
224           url.origin(),
225           storage::kFileSystemTypeIsolated,
226           isolated_virtual_path);
227
228   EXPECT_TRUE(isolated_url.is_valid());
229
230   FileSystemURLParser parser(isolated_url);
231   EXPECT_TRUE(parser.Parse());
232
233   ProvidedFileSystemInterface* file_system = parser.file_system();
234   ASSERT_TRUE(file_system);
235   EXPECT_EQ(kFileSystemId, file_system->GetFileSystemInfo().file_system_id());
236   EXPECT_EQ(kFilePath.AsUTF8Unsafe(), parser.file_path().AsUTF8Unsafe());
237 }
238
239 TEST_F(FileSystemProviderMountPathUtilTest, LocalPathParser) {
240   const bool result = file_system_provider_service_->MountFileSystem(
241       kExtensionId, MountOptions(kFileSystemId, kDisplayName));
242   ASSERT_TRUE(result);
243   const ProvidedFileSystemInfo file_system_info =
244       file_system_provider_service_->GetProvidedFileSystem(kExtensionId,
245                                                            kFileSystemId)
246           ->GetFileSystemInfo();
247
248   const base::FilePath kFilePath =
249       base::FilePath::FromUTF8Unsafe("/hello/world.txt");
250   const base::FilePath kLocalFilePath = file_system_info.mount_path().Append(
251       base::FilePath(kFilePath.value().substr(1)));
252
253   LOG(ERROR) << kLocalFilePath.value();
254   LocalPathParser parser(profile_, kLocalFilePath);
255   EXPECT_TRUE(parser.Parse());
256
257   ProvidedFileSystemInterface* file_system = parser.file_system();
258   ASSERT_TRUE(file_system);
259   EXPECT_EQ(kFileSystemId, file_system->GetFileSystemInfo().file_system_id());
260   EXPECT_EQ(kFilePath.AsUTF8Unsafe(), parser.file_path().AsUTF8Unsafe());
261 }
262
263 TEST_F(FileSystemProviderMountPathUtilTest, LocalPathParser_RootPath) {
264   const bool result = file_system_provider_service_->MountFileSystem(
265       kExtensionId, MountOptions(kFileSystemId, kDisplayName));
266   ASSERT_TRUE(result);
267   const ProvidedFileSystemInfo file_system_info =
268       file_system_provider_service_->GetProvidedFileSystem(kExtensionId,
269                                                            kFileSystemId)
270           ->GetFileSystemInfo();
271
272   const base::FilePath kFilePath = base::FilePath::FromUTF8Unsafe("/");
273   const base::FilePath kLocalFilePath = file_system_info.mount_path();
274
275   LocalPathParser parser(profile_, kLocalFilePath);
276   EXPECT_TRUE(parser.Parse());
277
278   ProvidedFileSystemInterface* file_system = parser.file_system();
279   ASSERT_TRUE(file_system);
280   EXPECT_EQ(kFileSystemId, file_system->GetFileSystemInfo().file_system_id());
281   EXPECT_EQ(kFilePath.AsUTF8Unsafe(), parser.file_path().AsUTF8Unsafe());
282 }
283
284 TEST_F(FileSystemProviderMountPathUtilTest, LocalPathParser_WrongPath) {
285   {
286     const base::FilePath kFilePath = base::FilePath::FromUTF8Unsafe("/hello");
287     LocalPathParser parser(profile_, kFilePath);
288     EXPECT_FALSE(parser.Parse());
289   }
290
291   {
292     const base::FilePath kFilePath =
293         base::FilePath::FromUTF8Unsafe("/provided");
294     LocalPathParser parser(profile_, kFilePath);
295     EXPECT_FALSE(parser.Parse());
296   }
297
298   {
299     const base::FilePath kFilePath =
300         base::FilePath::FromUTF8Unsafe("provided/hello/world");
301     LocalPathParser parser(profile_, kFilePath);
302     EXPECT_FALSE(parser.Parse());
303   }
304 }
305
306 }  // namespace util
307 }  // namespace file_system_provider
308 }  // namespace chromeos