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.
5 #include "chrome/browser/chromeos/drive/file_system_util.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "chrome/browser/chromeos/profiles/profile_helper.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/testing_profile_manager.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "content/public/test/test_file_system_options.h"
21 #include "google_apis/drive/test_util.h"
22 #include "storage/browser/fileapi/external_mount_points.h"
23 #include "storage/browser/fileapi/file_system_backend.h"
24 #include "storage/browser/fileapi/file_system_context.h"
25 #include "storage/browser/fileapi/file_system_url.h"
26 #include "storage/browser/fileapi/isolated_context.h"
27 #include "testing/gtest/include/gtest/gtest.h"
34 // Sets up ProfileManager for testing and marks the current thread as UI by
35 // TestBrowserThreadBundle. We need the thread since Profile objects must be
36 // touched from UI and hence has CHECK/DCHECKs for it.
37 class ProfileRelatedFileSystemUtilTest
: public testing::Test
{
39 ProfileRelatedFileSystemUtilTest()
40 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {}
42 void SetUp() override
{ ASSERT_TRUE(testing_profile_manager_
.SetUp()); }
44 TestingProfileManager
& testing_profile_manager() {
45 return testing_profile_manager_
;
49 content::TestBrowserThreadBundle thread_bundle_
;
50 TestingProfileManager testing_profile_manager_
;
55 TEST_F(ProfileRelatedFileSystemUtilTest
, GetDriveMountPointPath
) {
56 Profile
* profile
= testing_profile_manager().CreateTestingProfile("user1");
57 const std::string user_id_hash
=
58 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user1");
59 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("/special/drive-" + user_id_hash
),
60 GetDriveMountPointPath(profile
));
63 TEST_F(ProfileRelatedFileSystemUtilTest
, ExtractProfileFromPath
) {
64 Profile
* profile1
= testing_profile_manager().CreateTestingProfile("user1");
65 Profile
* profile2
= testing_profile_manager().CreateTestingProfile("user2");
66 const std::string user1_id_hash
=
67 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user1");
68 const std::string user2_id_hash
=
69 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user2");
70 EXPECT_EQ(profile1
, ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe(
71 "/special/drive-" + user1_id_hash
)));
72 EXPECT_EQ(profile2
, ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe(
73 "/special/drive-" + user2_id_hash
+ "/root/xxx")));
74 EXPECT_EQ(NULL
, ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe(
75 "/special/non-drive-path")));
78 TEST_F(ProfileRelatedFileSystemUtilTest
, ExtractDrivePathFromFileSystemUrl
) {
79 TestingProfile profile
;
81 // Set up file system context for testing.
82 base::ScopedTempDir temp_dir_
;
83 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
85 scoped_refptr
<storage::ExternalMountPoints
> mount_points
=
86 storage::ExternalMountPoints::CreateRefCounted();
87 scoped_refptr
<storage::FileSystemContext
> context(
88 new storage::FileSystemContext(
89 base::ThreadTaskRunnerHandle::Get().get(),
90 base::ThreadTaskRunnerHandle::Get().get(), mount_points
.get(),
91 NULL
, // special_storage_policy
92 NULL
, // quota_manager_proxy,
93 ScopedVector
<storage::FileSystemBackend
>(),
94 std::vector
<storage::URLRequestAutoMountHandler
>(),
95 temp_dir_
.path(), // partition_path
96 content::CreateAllowFileAccessOptions()));
98 // Type:"external" + virtual_path:"drive/foo/bar" resolves to "drive/foo/bar".
99 const std::string
& drive_mount_name
=
100 GetDriveMountPointPath(&profile
).BaseName().AsUTF8Unsafe();
101 mount_points
->RegisterFileSystem(
102 drive_mount_name
, storage::kFileSystemTypeDrive
,
103 storage::FileSystemMountOption(), GetDriveMountPointPath(&profile
));
104 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo/bar"),
105 ExtractDrivePathFromFileSystemUrl(context
->CrackURL(
106 GURL("filesystem:chrome-extension://dummy-id/external/" +
107 drive_mount_name
+ "/foo/bar"))));
109 // Virtual mount name should not affect the extracted path.
110 mount_points
->RevokeFileSystem(drive_mount_name
);
111 mount_points
->RegisterFileSystem("drive2", storage::kFileSystemTypeDrive
,
112 storage::FileSystemMountOption(),
113 GetDriveMountPointPath(&profile
));
115 base::FilePath::FromUTF8Unsafe("drive/foo/bar"),
116 ExtractDrivePathFromFileSystemUrl(context
->CrackURL(GURL(
117 "filesystem:chrome-extension://dummy-id/external/drive2/foo/bar"))));
119 // Type:"external" + virtual_path:"Downloads/foo" is not a Drive path.
120 mount_points
->RegisterFileSystem(
121 "Downloads", storage::kFileSystemTypeNativeLocal
,
122 storage::FileSystemMountOption(), temp_dir_
.path());
125 ExtractDrivePathFromFileSystemUrl(context
->CrackURL(GURL(
126 "filesystem:chrome-extension://dummy-id/external/Downloads/foo"))));
128 // Type:"isolated" + virtual_path:"isolated_id/name" mapped on a Drive path.
129 std::string isolated_name
;
130 std::string isolated_id
=
131 storage::IsolatedContext::GetInstance()->RegisterFileSystemForPath(
132 storage::kFileSystemTypeNativeForPlatformApp
, std::string(),
133 GetDriveMountPointPath(&profile
).AppendASCII("bar/buz"),
135 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/bar/buz"),
136 ExtractDrivePathFromFileSystemUrl(context
->CrackURL(
137 GURL("filesystem:chrome-extension://dummy-id/isolated/" +
138 isolated_id
+ "/" + isolated_name
))));
141 TEST_F(ProfileRelatedFileSystemUtilTest
, GetCacheRootPath
) {
142 TestingProfile profile
;
143 base::FilePath profile_path
= profile
.GetPath();
144 EXPECT_EQ(profile_path
.AppendASCII("GCache/v1"),
145 util::GetCacheRootPath(&profile
));