| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/drive/file_system_util.h" | 5 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 namespace util { | 30 namespace util { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // Sets up ProfileManager for testing and marks the current thread as UI by | 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 | 35 // TestBrowserThreadBundle. We need the thread since Profile objects must be |
| 36 // touched from UI and hence has CHECK/DCHECKs for it. | 36 // touched from UI and hence has CHECK/DCHECKs for it. |
| 37 class ProfileRelatedFileSystemUtilTest : public testing::Test { | 37 class ProfileRelatedFileSystemUtilTest : public testing::Test { |
| 38 protected: | 38 protected: |
| 39 ProfileRelatedFileSystemUtilTest() | 39 ProfileRelatedFileSystemUtilTest() |
| 40 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) { | 40 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {} |
| 41 } | |
| 42 | 41 |
| 43 void SetUp() override { ASSERT_TRUE(testing_profile_manager_.SetUp()); } | 42 void SetUp() override { ASSERT_TRUE(testing_profile_manager_.SetUp()); } |
| 44 | 43 |
| 45 TestingProfileManager& testing_profile_manager() { | 44 TestingProfileManager& testing_profile_manager() { |
| 46 return testing_profile_manager_; | 45 return testing_profile_manager_; |
| 47 } | 46 } |
| 48 | 47 |
| 49 private: | 48 private: |
| 50 content::TestBrowserThreadBundle thread_bundle_; | 49 content::TestBrowserThreadBundle thread_bundle_; |
| 51 TestingProfileManager testing_profile_manager_; | 50 TestingProfileManager testing_profile_manager_; |
| 52 }; | 51 }; |
| 53 | 52 |
| 54 } // namespace | 53 } // namespace |
| 55 | 54 |
| 56 TEST_F(ProfileRelatedFileSystemUtilTest, GetDriveMountPointPath) { | 55 TEST_F(ProfileRelatedFileSystemUtilTest, GetDriveMountPointPath) { |
| 57 Profile* profile = testing_profile_manager().CreateTestingProfile("user1"); | 56 Profile* profile = testing_profile_manager().CreateTestingProfile("user1"); |
| 58 const std::string user_id_hash = | 57 const std::string user_id_hash = |
| 59 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user1"); | 58 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user1"); |
| 60 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("/special/drive-" + user_id_hash), | 59 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("/special/drive-" + user_id_hash), |
| 61 GetDriveMountPointPath(profile)); | 60 GetDriveMountPointPath(profile)); |
| 62 } | 61 } |
| 63 | 62 |
| 64 TEST_F(ProfileRelatedFileSystemUtilTest, ExtractProfileFromPath) { | 63 TEST_F(ProfileRelatedFileSystemUtilTest, ExtractProfileFromPath) { |
| 65 Profile* profile1 = testing_profile_manager().CreateTestingProfile("user1"); | 64 Profile* profile1 = testing_profile_manager().CreateTestingProfile("user1"); |
| 66 Profile* profile2 = testing_profile_manager().CreateTestingProfile("user2"); | 65 Profile* profile2 = testing_profile_manager().CreateTestingProfile("user2"); |
| 67 const std::string user1_id_hash = | 66 const std::string user1_id_hash = |
| 68 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user1"); | 67 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user1"); |
| 69 const std::string user2_id_hash = | 68 const std::string user2_id_hash = |
| 70 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user2"); | 69 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user2"); |
| 71 EXPECT_EQ(profile1, | 70 EXPECT_EQ(profile1, ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe( |
| 72 ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe( | 71 "/special/drive-" + user1_id_hash))); |
| 73 "/special/drive-" + user1_id_hash))); | 72 EXPECT_EQ(profile2, ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe( |
| 74 EXPECT_EQ(profile2, | 73 "/special/drive-" + user2_id_hash + "/root/xxx"))); |
| 75 ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe( | 74 EXPECT_EQ(NULL, ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe( |
| 76 "/special/drive-" + user2_id_hash + "/root/xxx"))); | 75 "/special/non-drive-path"))); |
| 77 EXPECT_EQ(NULL, ExtractProfileFromPath( | |
| 78 base::FilePath::FromUTF8Unsafe("/special/non-drive-path"))); | |
| 79 } | 76 } |
| 80 | 77 |
| 81 class FileSystemUtilTest : public testing::Test { | 78 TEST_F(ProfileRelatedFileSystemUtilTest, ExtractDrivePathFromFileSystemUrl) { |
| 82 content::TestBrowserThreadBundle thread_bundle_; | |
| 83 }; | |
| 84 | |
| 85 TEST_F(FileSystemUtilTest, IsUnderDriveMountPoint) { | |
| 86 EXPECT_FALSE(IsUnderDriveMountPoint( | |
| 87 base::FilePath::FromUTF8Unsafe("/wherever/foo.txt"))); | |
| 88 EXPECT_FALSE(IsUnderDriveMountPoint( | |
| 89 base::FilePath::FromUTF8Unsafe("/special/foo.txt"))); | |
| 90 EXPECT_FALSE(IsUnderDriveMountPoint( | |
| 91 base::FilePath::FromUTF8Unsafe("special/drive/foo.txt"))); | |
| 92 | |
| 93 EXPECT_TRUE(IsUnderDriveMountPoint( | |
| 94 base::FilePath::FromUTF8Unsafe("/special/drive"))); | |
| 95 EXPECT_TRUE(IsUnderDriveMountPoint( | |
| 96 base::FilePath::FromUTF8Unsafe("/special/drive/foo.txt"))); | |
| 97 EXPECT_TRUE(IsUnderDriveMountPoint( | |
| 98 base::FilePath::FromUTF8Unsafe("/special/drive/subdir/foo.txt"))); | |
| 99 EXPECT_TRUE(IsUnderDriveMountPoint( | |
| 100 base::FilePath::FromUTF8Unsafe("/special/drive-xxx/foo.txt"))); | |
| 101 } | |
| 102 | |
| 103 TEST_F(FileSystemUtilTest, ExtractDrivePath) { | |
| 104 EXPECT_EQ(base::FilePath(), | |
| 105 ExtractDrivePath( | |
| 106 base::FilePath::FromUTF8Unsafe("/wherever/foo.txt"))); | |
| 107 EXPECT_EQ(base::FilePath(), | |
| 108 ExtractDrivePath( | |
| 109 base::FilePath::FromUTF8Unsafe("/special/foo.txt"))); | |
| 110 | |
| 111 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive"), | |
| 112 ExtractDrivePath( | |
| 113 base::FilePath::FromUTF8Unsafe("/special/drive"))); | |
| 114 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo.txt"), | |
| 115 ExtractDrivePath( | |
| 116 base::FilePath::FromUTF8Unsafe("/special/drive/foo.txt"))); | |
| 117 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/subdir/foo.txt"), | |
| 118 ExtractDrivePath(base::FilePath::FromUTF8Unsafe( | |
| 119 "/special/drive/subdir/foo.txt"))); | |
| 120 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo.txt"), | |
| 121 ExtractDrivePath( | |
| 122 base::FilePath::FromUTF8Unsafe("/special/drive-xxx/foo.txt"))); | |
| 123 } | |
| 124 | |
| 125 TEST_F(FileSystemUtilTest, ExtractDrivePathFromFileSystemUrl) { | |
| 126 TestingProfile profile; | 79 TestingProfile profile; |
| 127 | 80 |
| 128 // Set up file system context for testing. | 81 // Set up file system context for testing. |
| 129 base::ScopedTempDir temp_dir_; | 82 base::ScopedTempDir temp_dir_; |
| 130 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 83 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 131 | 84 |
| 132 scoped_refptr<storage::ExternalMountPoints> mount_points = | 85 scoped_refptr<storage::ExternalMountPoints> mount_points = |
| 133 storage::ExternalMountPoints::CreateRefCounted(); | 86 storage::ExternalMountPoints::CreateRefCounted(); |
| 134 scoped_refptr<storage::FileSystemContext> context( | 87 scoped_refptr<storage::FileSystemContext> context( |
| 135 new storage::FileSystemContext( | 88 new storage::FileSystemContext( |
| 136 base::ThreadTaskRunnerHandle::Get().get(), | 89 base::ThreadTaskRunnerHandle::Get().get(), |
| 137 base::ThreadTaskRunnerHandle::Get().get(), | 90 base::ThreadTaskRunnerHandle::Get().get(), mount_points.get(), |
| 138 mount_points.get(), | |
| 139 NULL, // special_storage_policy | 91 NULL, // special_storage_policy |
| 140 NULL, // quota_manager_proxy, | 92 NULL, // quota_manager_proxy, |
| 141 ScopedVector<storage::FileSystemBackend>(), | 93 ScopedVector<storage::FileSystemBackend>(), |
| 142 std::vector<storage::URLRequestAutoMountHandler>(), | 94 std::vector<storage::URLRequestAutoMountHandler>(), |
| 143 temp_dir_.path(), // partition_path | 95 temp_dir_.path(), // partition_path |
| 144 content::CreateAllowFileAccessOptions())); | 96 content::CreateAllowFileAccessOptions())); |
| 145 | 97 |
| 146 // Type:"external" + virtual_path:"drive/foo/bar" resolves to "drive/foo/bar". | 98 // Type:"external" + virtual_path:"drive/foo/bar" resolves to "drive/foo/bar". |
| 147 const std::string& drive_mount_name = | 99 const std::string& drive_mount_name = |
| 148 GetDriveMountPointPath(&profile).BaseName().AsUTF8Unsafe(); | 100 GetDriveMountPointPath(&profile).BaseName().AsUTF8Unsafe(); |
| 149 mount_points->RegisterFileSystem(drive_mount_name, | 101 mount_points->RegisterFileSystem( |
| 150 storage::kFileSystemTypeDrive, | 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")))); |
| 108 |
| 109 // Virtual mount name should not affect the extracted path. |
| 110 mount_points->RevokeFileSystem(drive_mount_name); |
| 111 mount_points->RegisterFileSystem("drive2", storage::kFileSystemTypeDrive, |
| 151 storage::FileSystemMountOption(), | 112 storage::FileSystemMountOption(), |
| 152 GetDriveMountPointPath(&profile)); | 113 GetDriveMountPointPath(&profile)); |
| 153 EXPECT_EQ( | 114 EXPECT_EQ( |
| 154 base::FilePath::FromUTF8Unsafe("drive/foo/bar"), | |
| 155 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL( | |
| 156 "filesystem:chrome-extension://dummy-id/external/" + | |
| 157 drive_mount_name + "/foo/bar")))); | |
| 158 | |
| 159 // Virtual mount name should not affect the extracted path. | |
| 160 mount_points->RevokeFileSystem(drive_mount_name); | |
| 161 mount_points->RegisterFileSystem("drive2", | |
| 162 storage::kFileSystemTypeDrive, | |
| 163 storage::FileSystemMountOption(), | |
| 164 GetDriveMountPointPath(&profile)); | |
| 165 EXPECT_EQ( | |
| 166 base::FilePath::FromUTF8Unsafe("drive/foo/bar"), | 115 base::FilePath::FromUTF8Unsafe("drive/foo/bar"), |
| 167 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL( | 116 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL( |
| 168 "filesystem:chrome-extension://dummy-id/external/drive2/foo/bar")))); | 117 "filesystem:chrome-extension://dummy-id/external/drive2/foo/bar")))); |
| 169 | 118 |
| 170 // Type:"external" + virtual_path:"Downloads/foo" is not a Drive path. | 119 // Type:"external" + virtual_path:"Downloads/foo" is not a Drive path. |
| 171 mount_points->RegisterFileSystem("Downloads", | 120 mount_points->RegisterFileSystem( |
| 172 storage::kFileSystemTypeNativeLocal, | 121 "Downloads", storage::kFileSystemTypeNativeLocal, |
| 173 storage::FileSystemMountOption(), | 122 storage::FileSystemMountOption(), temp_dir_.path()); |
| 174 temp_dir_.path()); | |
| 175 EXPECT_EQ( | 123 EXPECT_EQ( |
| 176 base::FilePath(), | 124 base::FilePath(), |
| 177 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL( | 125 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL( |
| 178 "filesystem:chrome-extension://dummy-id/external/Downloads/foo")))); | 126 "filesystem:chrome-extension://dummy-id/external/Downloads/foo")))); |
| 179 | 127 |
| 180 // Type:"isolated" + virtual_path:"isolated_id/name" mapped on a Drive path. | 128 // Type:"isolated" + virtual_path:"isolated_id/name" mapped on a Drive path. |
| 181 std::string isolated_name; | 129 std::string isolated_name; |
| 182 std::string isolated_id = | 130 std::string isolated_id = |
| 183 storage::IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 131 storage::IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
| 184 storage::kFileSystemTypeNativeForPlatformApp, | 132 storage::kFileSystemTypeNativeForPlatformApp, std::string(), |
| 185 std::string(), | |
| 186 GetDriveMountPointPath(&profile).AppendASCII("bar/buz"), | 133 GetDriveMountPointPath(&profile).AppendASCII("bar/buz"), |
| 187 &isolated_name); | 134 &isolated_name); |
| 188 EXPECT_EQ( | 135 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/bar/buz"), |
| 189 base::FilePath::FromUTF8Unsafe("drive/bar/buz"), | 136 ExtractDrivePathFromFileSystemUrl(context->CrackURL( |
| 190 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL( | 137 GURL("filesystem:chrome-extension://dummy-id/isolated/" + |
| 191 "filesystem:chrome-extension://dummy-id/isolated/" + | 138 isolated_id + "/" + isolated_name)))); |
| 192 isolated_id + "/" + isolated_name)))); | |
| 193 } | 139 } |
| 194 | 140 |
| 195 TEST_F(FileSystemUtilTest, EscapeUnescapeCacheFileName) { | 141 TEST_F(ProfileRelatedFileSystemUtilTest, GetCacheRootPath) { |
| 196 const std::string kUnescapedFileName( | |
| 197 "tmp:`~!@#$%^&*()-_=+[{|]}\\\\;\',<.>/?"); | |
| 198 const std::string kEscapedFileName( | |
| 199 "tmp:`~!@#$%25^&*()-_=+[{|]}\\\\;\',<%2E>%2F?"); | |
| 200 EXPECT_EQ(kEscapedFileName, EscapeCacheFileName(kUnescapedFileName)); | |
| 201 EXPECT_EQ(kUnescapedFileName, UnescapeCacheFileName(kEscapedFileName)); | |
| 202 } | |
| 203 | |
| 204 TEST_F(FileSystemUtilTest, NormalizeFileName) { | |
| 205 EXPECT_EQ("", NormalizeFileName("")); | |
| 206 EXPECT_EQ("foo", NormalizeFileName("foo")); | |
| 207 // Slash | |
| 208 EXPECT_EQ("foo_zzz", NormalizeFileName("foo/zzz")); | |
| 209 EXPECT_EQ("___", NormalizeFileName("///")); | |
| 210 // Japanese hiragana "hi" + semi-voiced-mark is normalized to "pi". | |
| 211 EXPECT_EQ("\xE3\x81\xB4", NormalizeFileName("\xE3\x81\xB2\xE3\x82\x9A")); | |
| 212 // Dot | |
| 213 EXPECT_EQ("_", NormalizeFileName(".")); | |
| 214 EXPECT_EQ("_", NormalizeFileName("..")); | |
| 215 EXPECT_EQ("_", NormalizeFileName("...")); | |
| 216 EXPECT_EQ(".bashrc", NormalizeFileName(".bashrc")); | |
| 217 EXPECT_EQ("._", NormalizeFileName("./")); | |
| 218 } | |
| 219 | |
| 220 TEST_F(FileSystemUtilTest, GetCacheRootPath) { | |
| 221 TestingProfile profile; | 142 TestingProfile profile; |
| 222 base::FilePath profile_path = profile.GetPath(); | 143 base::FilePath profile_path = profile.GetPath(); |
| 223 EXPECT_EQ(profile_path.AppendASCII("GCache/v1"), | 144 EXPECT_EQ(profile_path.AppendASCII("GCache/v1"), |
| 224 util::GetCacheRootPath(&profile)); | 145 util::GetCacheRootPath(&profile)); |
| 225 } | 146 } |
| 226 | 147 |
| 227 TEST_F(FileSystemUtilTest, GDocFile) { | |
| 228 base::ScopedTempDir temp_dir; | |
| 229 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 230 | |
| 231 GURL url("https://docs.google.com/document/d/" | |
| 232 "1YsCnrMxxgp7LDdtlFDt-WdtEIth89vA9inrILtvK-Ug/edit"); | |
| 233 std::string resource_id("1YsCnrMxxgp7LDdtlFDt-WdtEIth89vA9inrILtvK-Ug"); | |
| 234 | |
| 235 // Read and write gdoc. | |
| 236 base::FilePath file = temp_dir.path().AppendASCII("test.gdoc"); | |
| 237 EXPECT_TRUE(CreateGDocFile(file, url, resource_id)); | |
| 238 EXPECT_EQ(url, ReadUrlFromGDocFile(file)); | |
| 239 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file)); | |
| 240 | |
| 241 // Read and write gsheet. | |
| 242 file = temp_dir.path().AppendASCII("test.gsheet"); | |
| 243 EXPECT_TRUE(CreateGDocFile(file, url, resource_id)); | |
| 244 EXPECT_EQ(url, ReadUrlFromGDocFile(file)); | |
| 245 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file)); | |
| 246 | |
| 247 // Read and write gslides. | |
| 248 file = temp_dir.path().AppendASCII("test.gslides"); | |
| 249 EXPECT_TRUE(CreateGDocFile(file, url, resource_id)); | |
| 250 EXPECT_EQ(url, ReadUrlFromGDocFile(file)); | |
| 251 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file)); | |
| 252 | |
| 253 // Read and write gdraw. | |
| 254 file = temp_dir.path().AppendASCII("test.gdraw"); | |
| 255 EXPECT_TRUE(CreateGDocFile(file, url, resource_id)); | |
| 256 EXPECT_EQ(url, ReadUrlFromGDocFile(file)); | |
| 257 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file)); | |
| 258 | |
| 259 // Read and write gtable. | |
| 260 file = temp_dir.path().AppendASCII("test.gtable"); | |
| 261 EXPECT_TRUE(CreateGDocFile(file, url, resource_id)); | |
| 262 EXPECT_EQ(url, ReadUrlFromGDocFile(file)); | |
| 263 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file)); | |
| 264 | |
| 265 // Non GDoc file. | |
| 266 file = temp_dir.path().AppendASCII("test.txt"); | |
| 267 std::string data = "Hello world!"; | |
| 268 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(file, data)); | |
| 269 EXPECT_TRUE(ReadUrlFromGDocFile(file).is_empty()); | |
| 270 EXPECT_TRUE(ReadResourceIdFromGDocFile(file).empty()); | |
| 271 } | |
| 272 | |
| 273 } // namespace util | 148 } // namespace util |
| 274 } // namespace drive | 149 } // namespace drive |
| OLD | NEW |