1 // Copyright (c) 2012 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"
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/test/test_file_system_options.h"
14 #include "google_apis/drive/test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "webkit/browser/fileapi/external_mount_points.h"
17 #include "webkit/browser/fileapi/file_system_backend.h"
18 #include "webkit/browser/fileapi/file_system_context.h"
19 #include "webkit/browser/fileapi/file_system_url.h"
20 #include "webkit/browser/fileapi/isolated_context.h"
25 TEST(FileSystemUtilTest, FilePathToDriveURL) {
28 // Path with alphabets and numbers.
29 path = GetDriveMyDriveRootPath().AppendASCII("foo/bar012.txt");
30 EXPECT_EQ(path, DriveURLToFilePath(FilePathToDriveURL(path)));
33 path = GetDriveMyDriveRootPath().AppendASCII(
34 " !\"#$%&'()*+,-.:;<=>?@[\\]^_`{|}~");
35 EXPECT_EQ(path, DriveURLToFilePath(FilePathToDriveURL(path)));
38 path = GetDriveMyDriveRootPath().AppendASCII("%19%20%21.txt");
39 EXPECT_EQ(path, DriveURLToFilePath(FilePathToDriveURL(path)));
41 // Path with multi byte characters.
42 base::string16 utf16_string;
43 utf16_string.push_back(0x307b); // HIRAGANA_LETTER_HO
44 utf16_string.push_back(0x3052); // HIRAGANA_LETTER_GE
45 path = GetDriveMyDriveRootPath().Append(
46 base::FilePath::FromUTF8Unsafe(base::UTF16ToUTF8(utf16_string) + ".txt"));
47 EXPECT_EQ(path, DriveURLToFilePath(FilePathToDriveURL(path)));
50 TEST(FileSystemUtilTest, IsUnderDriveMountPoint) {
51 EXPECT_FALSE(IsUnderDriveMountPoint(
52 base::FilePath::FromUTF8Unsafe("/wherever/foo.txt")));
53 EXPECT_FALSE(IsUnderDriveMountPoint(
54 base::FilePath::FromUTF8Unsafe("/special/foo.txt")));
55 EXPECT_FALSE(IsUnderDriveMountPoint(
56 base::FilePath::FromUTF8Unsafe("/special/drivex/foo.txt")));
57 EXPECT_FALSE(IsUnderDriveMountPoint(
58 base::FilePath::FromUTF8Unsafe("special/drivex/foo.txt")));
60 EXPECT_TRUE(IsUnderDriveMountPoint(
61 base::FilePath::FromUTF8Unsafe("/special/drive")));
62 EXPECT_TRUE(IsUnderDriveMountPoint(
63 base::FilePath::FromUTF8Unsafe("/special/drive/foo.txt")));
64 EXPECT_TRUE(IsUnderDriveMountPoint(
65 base::FilePath::FromUTF8Unsafe("/special/drive/subdir/foo.txt")));
68 TEST(FileSystemUtilTest, ExtractDrivePath) {
69 EXPECT_EQ(base::FilePath(),
71 base::FilePath::FromUTF8Unsafe("/wherever/foo.txt")));
72 EXPECT_EQ(base::FilePath(),
74 base::FilePath::FromUTF8Unsafe("/special/foo.txt")));
75 EXPECT_EQ(base::FilePath(),
77 base::FilePath::FromUTF8Unsafe("/special/drivex/foo.txt")));
79 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive"),
81 base::FilePath::FromUTF8Unsafe("/special/drive")));
82 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo.txt"),
84 base::FilePath::FromUTF8Unsafe("/special/drive/foo.txt")));
85 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/subdir/foo.txt"),
86 ExtractDrivePath(base::FilePath::FromUTF8Unsafe(
87 "/special/drive/subdir/foo.txt")));
90 TEST(FileSystemUtilTest, ExtractDrivePathFromFileSystemUrl) {
91 // Set up file system context for testing.
92 base::ScopedTempDir temp_dir_;
93 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
95 base::MessageLoop message_loop;
96 scoped_refptr<fileapi::ExternalMountPoints> mount_points =
97 fileapi::ExternalMountPoints::CreateRefCounted();
98 scoped_refptr<fileapi::FileSystemContext> context(
99 new fileapi::FileSystemContext(
100 base::MessageLoopProxy::current().get(),
101 base::MessageLoopProxy::current().get(),
103 NULL, // special_storage_policy
104 NULL, // quota_manager_proxy,
105 ScopedVector<fileapi::FileSystemBackend>(),
106 temp_dir_.path(), // partition_path
107 content::CreateAllowFileAccessOptions()));
109 // Type:"external" + virtual_path:"drive/foo/bar" resolves to "drive/foo/bar".
110 const std::string& drive_mount_name =
111 GetDriveMountPointPath().BaseName().AsUTF8Unsafe();
112 mount_points->RegisterFileSystem(
114 fileapi::kFileSystemTypeDrive,
115 fileapi::FileSystemMountOption(),
116 GetDriveMountPointPath());
118 base::FilePath::FromUTF8Unsafe(drive_mount_name + "/foo/bar"),
119 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
120 "filesystem:chrome-extension://dummy-id/external/" +
121 drive_mount_name + "/foo/bar"))));
123 // Virtual mount name should not affect the extracted path.
124 mount_points->RevokeFileSystem(drive_mount_name);
125 mount_points->RegisterFileSystem(
127 fileapi::kFileSystemTypeDrive,
128 fileapi::FileSystemMountOption(),
129 GetDriveMountPointPath());
131 base::FilePath::FromUTF8Unsafe(drive_mount_name + "/foo/bar"),
132 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
133 "filesystem:chrome-extension://dummy-id/external/drive2/foo/bar"))));
135 // Type:"external" + virtual_path:"Downloads/foo" is not a Drive path.
136 mount_points->RegisterFileSystem(
138 fileapi::kFileSystemTypeNativeLocal,
139 fileapi::FileSystemMountOption(),
143 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
144 "filesystem:chrome-extension://dummy-id/external/Downloads/foo"))));
146 // Type:"isolated" + virtual_path:"isolated_id/name" mapped on a Drive path.
147 std::string isolated_name;
148 std::string isolated_id =
149 fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForPath(
150 fileapi::kFileSystemTypeNativeForPlatformApp,
151 GetDriveMountPointPath().AppendASCII("bar/buz"),
154 base::FilePath::FromUTF8Unsafe(drive_mount_name + "/bar/buz"),
155 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
156 "filesystem:chrome-extension://dummy-id/isolated/" +
157 isolated_id + "/" + isolated_name))));
160 TEST(FileSystemUtilTest, EscapeUnescapeCacheFileName) {
161 const std::string kUnescapedFileName(
162 "tmp:`~!@#$%^&*()-_=+[{|]}\\\\;\',<.>/?");
163 const std::string kEscapedFileName(
164 "tmp:`~!@#$%25^&*()-_=+[{|]}\\\\;\',<%2E>%2F?");
165 EXPECT_EQ(kEscapedFileName, EscapeCacheFileName(kUnescapedFileName));
166 EXPECT_EQ(kUnescapedFileName, UnescapeCacheFileName(kEscapedFileName));
169 TEST(FileSystemUtilTest, NormalizeFileName) {
170 EXPECT_EQ("", NormalizeFileName(""));
171 EXPECT_EQ("foo", NormalizeFileName("foo"));
173 EXPECT_EQ("foo_zzz", NormalizeFileName("foo/zzz"));
174 EXPECT_EQ("___", NormalizeFileName("///"));
175 // Japanese hiragana "hi" + semi-voiced-mark is normalized to "pi".
176 EXPECT_EQ("\xE3\x81\xB4", NormalizeFileName("\xE3\x81\xB2\xE3\x82\x9A"));
178 EXPECT_EQ("_", NormalizeFileName("."));
179 EXPECT_EQ("_", NormalizeFileName(".."));
180 EXPECT_EQ("_", NormalizeFileName("..."));
181 EXPECT_EQ(".bashrc", NormalizeFileName(".bashrc"));
182 EXPECT_EQ("._", NormalizeFileName("./"));
185 TEST(FileSystemUtilTest, GetCacheRootPath) {
186 TestingProfile profile;
187 base::FilePath profile_path = profile.GetPath();
188 EXPECT_EQ(profile_path.AppendASCII("GCache/v1"),
189 util::GetCacheRootPath(&profile));
192 TEST(FileSystemUtilTest, GDocFile) {
193 base::ScopedTempDir temp_dir;
194 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
196 GURL url("https://docs.google.com/document/d/"
197 "1YsCnrMxxgp7LDdtlFDt-WdtEIth89vA9inrILtvK-Ug/edit");
198 std::string resource_id("1YsCnrMxxgp7LDdtlFDt-WdtEIth89vA9inrILtvK-Ug");
200 // Read and write gdoc.
201 base::FilePath file = temp_dir.path().AppendASCII("test.gdoc");
202 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
203 EXPECT_TRUE(HasGDocFileExtension(file));
204 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
205 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
207 // Read and write gsheet.
208 file = temp_dir.path().AppendASCII("test.gsheet");
209 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
210 EXPECT_TRUE(HasGDocFileExtension(file));
211 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
212 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
214 // Read and write gslides.
215 file = temp_dir.path().AppendASCII("test.gslides");
216 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
217 EXPECT_TRUE(HasGDocFileExtension(file));
218 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
219 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
221 // Read and write gdraw.
222 file = temp_dir.path().AppendASCII("test.gdraw");
223 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
224 EXPECT_TRUE(HasGDocFileExtension(file));
225 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
226 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
228 // Read and write gtable.
229 file = temp_dir.path().AppendASCII("test.gtable");
230 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
231 EXPECT_TRUE(HasGDocFileExtension(file));
232 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
233 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
235 // Read and write glink.
236 file = temp_dir.path().AppendASCII("test.glink");
237 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
238 EXPECT_TRUE(HasGDocFileExtension(file));
239 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
240 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
243 file = temp_dir.path().AppendASCII("test.txt");
244 std::string data = "Hello world!";
245 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(file, data));
246 EXPECT_FALSE(HasGDocFileExtension(file));
247 EXPECT_TRUE(ReadUrlFromGDocFile(file).is_empty());
248 EXPECT_TRUE(ReadResourceIdFromGDocFile(file).empty());