| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 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 "webkit/browser/chromeos/fileapi/cros_mount_point_provider.h" | |
| 6 | |
| 7 #include <set> | |
| 8 | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "chromeos/dbus/cros_disks_client.h" | |
| 12 #include "googleurl/src/url_util.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "webkit/browser/fileapi/external_mount_points.h" | |
| 15 #include "webkit/browser/fileapi/file_permission_policy.h" | |
| 16 #include "webkit/browser/fileapi/file_system_url.h" | |
| 17 #include "webkit/browser/fileapi/isolated_context.h" | |
| 18 #include "webkit/browser/quota/mock_special_storage_policy.h" | |
| 19 | |
| 20 #define FPL(x) FILE_PATH_LITERAL(x) | |
| 21 | |
| 22 using fileapi::ExternalMountPoints; | |
| 23 using fileapi::FileSystemURL; | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 FileSystemURL CreateFileSystemURL(const std::string& extension, | |
| 28 const char* path, | |
| 29 ExternalMountPoints* mount_points) { | |
| 30 return mount_points->CreateCrackedFileSystemURL( | |
| 31 GURL("chrome-extension://" + extension + "/"), | |
| 32 fileapi::kFileSystemTypeExternal, | |
| 33 base::FilePath::FromUTF8Unsafe(path)); | |
| 34 } | |
| 35 | |
| 36 TEST(CrosMountPointProviderTest, DefaultMountPoints) { | |
| 37 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = | |
| 38 new quota::MockSpecialStoragePolicy(); | |
| 39 scoped_refptr<fileapi::ExternalMountPoints> mount_points( | |
| 40 fileapi::ExternalMountPoints::CreateRefCounted()); | |
| 41 chromeos::CrosMountPointProvider provider( | |
| 42 storage_policy, | |
| 43 mount_points.get(), | |
| 44 fileapi::ExternalMountPoints::GetSystemInstance()); | |
| 45 provider.AddSystemMountPoints(); | |
| 46 std::vector<base::FilePath> root_dirs = provider.GetRootDirectories(); | |
| 47 std::set<base::FilePath> root_dirs_set(root_dirs.begin(), root_dirs.end()); | |
| 48 | |
| 49 // By default there should be 4 mount points (in system mount points): | |
| 50 EXPECT_EQ(4u, root_dirs.size()); | |
| 51 base::FilePath home_path; | |
| 52 ASSERT_TRUE(PathService::Get(base::DIR_HOME, &home_path)); | |
| 53 | |
| 54 EXPECT_TRUE(root_dirs_set.count(home_path.AppendASCII("Downloads"))); | |
| 55 EXPECT_TRUE(root_dirs_set.count( | |
| 56 chromeos::CrosDisksClient::GetRemovableDiskMountPoint())); | |
| 57 EXPECT_TRUE(root_dirs_set.count( | |
| 58 chromeos::CrosDisksClient::GetArchiveMountPoint())); | |
| 59 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/usr/share/oem")))); | |
| 60 } | |
| 61 | |
| 62 TEST(CrosMountPointProviderTest, GetRootDirectories) { | |
| 63 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = | |
| 64 new quota::MockSpecialStoragePolicy(); | |
| 65 scoped_refptr<fileapi::ExternalMountPoints> mount_points( | |
| 66 fileapi::ExternalMountPoints::CreateRefCounted()); | |
| 67 | |
| 68 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points( | |
| 69 fileapi::ExternalMountPoints::CreateRefCounted()); | |
| 70 | |
| 71 chromeos::CrosMountPointProvider provider( | |
| 72 storage_policy, | |
| 73 mount_points.get(), | |
| 74 system_mount_points.get()); | |
| 75 | |
| 76 const size_t initial_root_dirs_size = provider.GetRootDirectories().size(); | |
| 77 | |
| 78 // Register 'local' test mount points. | |
| 79 mount_points->RegisterFileSystem("c", | |
| 80 fileapi::kFileSystemTypeNativeLocal, | |
| 81 base::FilePath(FPL("/a/b/c"))); | |
| 82 mount_points->RegisterFileSystem("d", | |
| 83 fileapi::kFileSystemTypeNativeLocal, | |
| 84 base::FilePath(FPL("/b/c/d"))); | |
| 85 | |
| 86 // Register system test mount points. | |
| 87 system_mount_points->RegisterFileSystem("d", | |
| 88 fileapi::kFileSystemTypeNativeLocal, | |
| 89 base::FilePath(FPL("/g/c/d"))); | |
| 90 system_mount_points->RegisterFileSystem("e", | |
| 91 fileapi::kFileSystemTypeNativeLocal, | |
| 92 base::FilePath(FPL("/g/d/e"))); | |
| 93 | |
| 94 std::vector<base::FilePath> root_dirs = provider.GetRootDirectories(); | |
| 95 std::set<base::FilePath> root_dirs_set(root_dirs.begin(), root_dirs.end()); | |
| 96 EXPECT_EQ(initial_root_dirs_size + 4, root_dirs.size()); | |
| 97 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/a/b/c")))); | |
| 98 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/b/c/d")))); | |
| 99 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/g/c/d")))); | |
| 100 EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/g/d/e")))); | |
| 101 } | |
| 102 | |
| 103 TEST(CrosMountPointProviderTest, AccessPermissions) { | |
| 104 const int kPermission = fileapi::kReadFilePermissions; | |
| 105 | |
| 106 url_util::AddStandardScheme("chrome-extension"); | |
| 107 | |
| 108 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy = | |
| 109 new quota::MockSpecialStoragePolicy(); | |
| 110 scoped_refptr<fileapi::ExternalMountPoints> mount_points( | |
| 111 fileapi::ExternalMountPoints::CreateRefCounted()); | |
| 112 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points( | |
| 113 fileapi::ExternalMountPoints::CreateRefCounted()); | |
| 114 chromeos::CrosMountPointProvider provider( | |
| 115 storage_policy, | |
| 116 mount_points.get(), | |
| 117 system_mount_points.get()); | |
| 118 | |
| 119 std::string extension("ddammdhioacbehjngdmkjcjbnfginlla"); | |
| 120 | |
| 121 storage_policy->AddFileHandler(extension); | |
| 122 | |
| 123 // Initialize mount points. | |
| 124 ASSERT_TRUE(system_mount_points->RegisterFileSystem( | |
| 125 "system", | |
| 126 fileapi::kFileSystemTypeNativeLocal, | |
| 127 base::FilePath(FPL("/g/system")))); | |
| 128 ASSERT_TRUE(mount_points->RegisterFileSystem( | |
| 129 "removable", | |
| 130 fileapi::kFileSystemTypeNativeLocal, | |
| 131 base::FilePath(FPL("/media/removable")))); | |
| 132 ASSERT_TRUE(mount_points->RegisterFileSystem( | |
| 133 "oem", | |
| 134 fileapi::kFileSystemTypeRestrictedNativeLocal, | |
| 135 base::FilePath(FPL("/usr/share/oem")))); | |
| 136 | |
| 137 // Provider specific mount point access. | |
| 138 EXPECT_EQ( | |
| 139 fileapi::FILE_PERMISSION_ALWAYS_DENY, | |
| 140 provider.GetPermissionPolicy( | |
| 141 CreateFileSystemURL(extension, "removable/foo", mount_points.get()), | |
| 142 kPermission)); | |
| 143 | |
| 144 provider.GrantFileAccessToExtension(extension, | |
| 145 base::FilePath(FPL("removable/foo"))); | |
| 146 EXPECT_EQ( | |
| 147 fileapi::FILE_PERMISSION_USE_FILE_PERMISSION, | |
| 148 provider.GetPermissionPolicy( | |
| 149 CreateFileSystemURL(extension, "removable/foo", mount_points.get()), | |
| 150 kPermission)); | |
| 151 EXPECT_EQ( | |
| 152 fileapi::FILE_PERMISSION_ALWAYS_DENY, | |
| 153 provider.GetPermissionPolicy( | |
| 154 CreateFileSystemURL(extension, "removable/foo1", mount_points.get()), | |
| 155 kPermission)); | |
| 156 | |
| 157 // System mount point access. | |
| 158 EXPECT_EQ( | |
| 159 fileapi::FILE_PERMISSION_ALWAYS_DENY, | |
| 160 provider.GetPermissionPolicy( | |
| 161 CreateFileSystemURL(extension, "system/foo", | |
| 162 system_mount_points.get()), | |
| 163 kPermission)); | |
| 164 | |
| 165 provider.GrantFileAccessToExtension(extension, | |
| 166 base::FilePath(FPL("system/foo"))); | |
| 167 EXPECT_EQ( | |
| 168 fileapi::FILE_PERMISSION_USE_FILE_PERMISSION, | |
| 169 provider.GetPermissionPolicy( | |
| 170 CreateFileSystemURL(extension, "system/foo", | |
| 171 system_mount_points.get()), | |
| 172 kPermission)); | |
| 173 EXPECT_EQ( | |
| 174 fileapi::FILE_PERMISSION_ALWAYS_DENY, | |
| 175 provider.GetPermissionPolicy( | |
| 176 CreateFileSystemURL(extension, "system/foo1", | |
| 177 system_mount_points.get()), | |
| 178 kPermission)); | |
| 179 | |
| 180 // oem is restricted file system. | |
| 181 provider.GrantFileAccessToExtension( | |
| 182 extension, base::FilePath(FPL("oem/foo"))); | |
| 183 // The extension should not be able to access the file even if | |
| 184 // GrantFileAccessToExtension was called. | |
| 185 EXPECT_EQ( | |
| 186 fileapi::FILE_PERMISSION_ALWAYS_DENY, | |
| 187 provider.GetPermissionPolicy( | |
| 188 CreateFileSystemURL(extension, "oem/foo", mount_points.get()), | |
| 189 kPermission)); | |
| 190 | |
| 191 provider.GrantFullAccessToExtension(extension); | |
| 192 // The extension should be able to access restricted file system after it was | |
| 193 // granted full access. | |
| 194 EXPECT_EQ( | |
| 195 fileapi::FILE_PERMISSION_USE_FILE_PERMISSION, | |
| 196 provider.GetPermissionPolicy( | |
| 197 CreateFileSystemURL(extension, "oem/foo", mount_points.get()), | |
| 198 kPermission)); | |
| 199 // The extension which was granted full access should be able to access any | |
| 200 // path on current file systems. | |
| 201 EXPECT_EQ( | |
| 202 fileapi::FILE_PERMISSION_USE_FILE_PERMISSION, | |
| 203 provider.GetPermissionPolicy( | |
| 204 CreateFileSystemURL(extension, "removable/foo1", mount_points.get()), | |
| 205 kPermission)); | |
| 206 EXPECT_EQ( | |
| 207 fileapi::FILE_PERMISSION_USE_FILE_PERMISSION, | |
| 208 provider.GetPermissionPolicy( | |
| 209 CreateFileSystemURL(extension, "system/foo1", | |
| 210 system_mount_points.get()), | |
| 211 kPermission)); | |
| 212 | |
| 213 // The extension cannot access new mount points. | |
| 214 // TODO(tbarzic): This should probably be changed. | |
| 215 ASSERT_TRUE(mount_points->RegisterFileSystem( | |
| 216 "test", | |
| 217 fileapi::kFileSystemTypeNativeLocal, | |
| 218 base::FilePath(FPL("/foo/test")))); | |
| 219 EXPECT_EQ( | |
| 220 fileapi::FILE_PERMISSION_ALWAYS_DENY, | |
| 221 provider.GetPermissionPolicy( | |
| 222 CreateFileSystemURL(extension, "test_/foo", mount_points.get()), | |
| 223 kPermission)); | |
| 224 | |
| 225 provider.RevokeAccessForExtension(extension); | |
| 226 EXPECT_EQ( | |
| 227 fileapi::FILE_PERMISSION_ALWAYS_DENY, | |
| 228 provider.GetPermissionPolicy( | |
| 229 CreateFileSystemURL(extension, "removable/foo", mount_points.get()), | |
| 230 kPermission)); | |
| 231 | |
| 232 fileapi::FileSystemURL internal_url = FileSystemURL::CreateForTest( | |
| 233 GURL("chrome://foo"), | |
| 234 fileapi::kFileSystemTypeExternal, | |
| 235 base::FilePath(FPL("removable/"))); | |
| 236 // Internal WebUI should have full access. | |
| 237 EXPECT_EQ( | |
| 238 fileapi::FILE_PERMISSION_ALWAYS_ALLOW, | |
| 239 provider.GetPermissionPolicy(internal_url, kPermission)); | |
| 240 } | |
| 241 | |
| 242 TEST(CrosMountPointProvider, GetVirtualPathConflictWithSystemPoints) { | |
| 243 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy = | |
| 244 new quota::MockSpecialStoragePolicy(); | |
| 245 scoped_refptr<fileapi::ExternalMountPoints> mount_points( | |
| 246 fileapi::ExternalMountPoints::CreateRefCounted()); | |
| 247 scoped_refptr<fileapi::ExternalMountPoints> system_mount_points( | |
| 248 fileapi::ExternalMountPoints::CreateRefCounted()); | |
| 249 chromeos::CrosMountPointProvider provider(storage_policy, | |
| 250 mount_points.get(), | |
| 251 system_mount_points.get()); | |
| 252 | |
| 253 const fileapi::FileSystemType type = fileapi::kFileSystemTypeNativeLocal; | |
| 254 | |
| 255 // Provider specific mount points. | |
| 256 ASSERT_TRUE( | |
| 257 mount_points->RegisterFileSystem("b", type, base::FilePath(FPL("/a/b")))); | |
| 258 ASSERT_TRUE( | |
| 259 mount_points->RegisterFileSystem("y", type, base::FilePath(FPL("/z/y")))); | |
| 260 ASSERT_TRUE( | |
| 261 mount_points->RegisterFileSystem("n", type, base::FilePath(FPL("/m/n")))); | |
| 262 | |
| 263 // System mount points | |
| 264 ASSERT_TRUE(system_mount_points->RegisterFileSystem( | |
| 265 "gb", type, base::FilePath(FPL("/a/b")))); | |
| 266 ASSERT_TRUE( | |
| 267 system_mount_points->RegisterFileSystem( | |
| 268 "gz", type, base::FilePath(FPL("/z")))); | |
| 269 ASSERT_TRUE(system_mount_points->RegisterFileSystem( | |
| 270 "gp", type, base::FilePath(FPL("/m/n/o/p")))); | |
| 271 | |
| 272 struct TestCase { | |
| 273 const base::FilePath::CharType* const local_path; | |
| 274 bool success; | |
| 275 const base::FilePath::CharType* const virtual_path; | |
| 276 }; | |
| 277 | |
| 278 const TestCase kTestCases[] = { | |
| 279 // Same paths in both mount points. | |
| 280 { FPL("/a/b/c/d"), true, FPL("b/c/d") }, | |
| 281 // System mount points path more specific. | |
| 282 { FPL("/m/n/o/p/r/s"), true, FPL("n/o/p/r/s") }, | |
| 283 // System mount points path less specific. | |
| 284 { FPL("/z/y/x"), true, FPL("y/x") }, | |
| 285 // Only system mount points path matches. | |
| 286 { FPL("/z/q/r/s"), true, FPL("gz/q/r/s") }, | |
| 287 // No match. | |
| 288 { FPL("/foo/xxx"), false, FPL("") }, | |
| 289 }; | |
| 290 | |
| 291 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | |
| 292 // Initialize virtual path with a value. | |
| 293 base::FilePath virtual_path(FPL("/mount")); | |
| 294 base::FilePath local_path(kTestCases[i].local_path); | |
| 295 EXPECT_EQ(kTestCases[i].success, | |
| 296 provider.GetVirtualPath(local_path, &virtual_path)) | |
| 297 << "Resolving " << kTestCases[i].local_path; | |
| 298 | |
| 299 // There are no guarantees for |virtual_path| value if |GetVirtualPath| | |
| 300 // fails. | |
| 301 if (!kTestCases[i].success) | |
| 302 continue; | |
| 303 | |
| 304 base::FilePath expected_virtual_path(kTestCases[i].virtual_path); | |
| 305 EXPECT_EQ(expected_virtual_path, virtual_path) | |
| 306 << "Resolving " << kTestCases[i].local_path; | |
| 307 } | |
| 308 } | |
| 309 | |
| 310 } // namespace | |
| OLD | NEW |