File: file_system_backend_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (285 lines) | stat: -rw-r--r-- 12,918 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chrome/browser/ash/fileapi/file_system_backend.h"

#include <stddef.h>

#include <set>

#include "base/files/file_path.h"
#include "chrome/browser/ash/fileapi/file_system_backend_delegate.h"
#include "chromeos/ash/components/dbus/cros_disks/cros_disks_client.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_operation.h"
#include "storage/browser/file_system/file_system_url.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/origin.h"

#define FPL(x) FILE_PATH_LITERAL(x)

using storage::ExternalMountPoints;
using storage::FileSystemURL;

namespace {

FileSystemURL CreateFileSystemURL(const std::string& extension,
                                  const char* path,
                                  ExternalMountPoints* mount_points) {
  return mount_points->CreateCrackedFileSystemURL(
      blink::StorageKey::CreateFromStringForTesting("chrome-extension://" +
                                                    extension + "/"),
      storage::kFileSystemTypeExternal, base::FilePath::FromUTF8Unsafe(path));
}

TEST(ChromeOSFileSystemBackendTest, DefaultMountPoints) {
  // Make sure no system-level mount points are registered before testing
  // to avoid flakiness.
  storage::ExternalMountPoints::GetSystemInstance()->RevokeAllFileSystems();

  scoped_refptr<storage::ExternalMountPoints> mount_points(
      storage::ExternalMountPoints::CreateRefCounted());
  ash::FileSystemBackend backend(
      nullptr,  // profile
      nullptr,  // file_system_provider_delegate
      nullptr,  // mtp_delegate
      nullptr,  // arc_content_delegate
      nullptr,  // arc_documents_provider_delegate
      nullptr,  // drivefs_delegate
      nullptr,  // smbfs_delegate
      mount_points.get(), storage::ExternalMountPoints::GetSystemInstance());
  backend.AddSystemMountPoints();
  std::vector<base::FilePath> root_dirs = backend.GetRootDirectories();
  std::set<base::FilePath> root_dirs_set(root_dirs.begin(), root_dirs.end());

  // By default there should be 3 mount points (in system mount points):
  EXPECT_EQ(2u, root_dirs.size());

  EXPECT_TRUE(
      root_dirs_set.count(ash::CrosDisksClient::GetRemovableDiskMountPoint()));
  EXPECT_TRUE(
      root_dirs_set.count(ash::CrosDisksClient::GetArchiveMountPoint()));
}

TEST(ChromeOSFileSystemBackendTest, GetRootDirectories) {
  scoped_refptr<storage::ExternalMountPoints> mount_points(
      storage::ExternalMountPoints::CreateRefCounted());

  scoped_refptr<storage::ExternalMountPoints> system_mount_points(
      storage::ExternalMountPoints::CreateRefCounted());

  ash::FileSystemBackend backend(nullptr,  // profile
                                 nullptr,  // file_system_provider_delegate
                                 nullptr,  // mtp_delegate
                                 nullptr,  // arc_content_delegate
                                 nullptr,  // arc_documents_provider_delegate
                                 nullptr,  // drivefs_delegate
                                 nullptr,  // smbfs_delegate
                                 mount_points.get(), system_mount_points.get());

  const size_t initial_root_dirs_size = backend.GetRootDirectories().size();

  // Register 'local' test mount points.
  mount_points->RegisterFileSystem("c", storage::kFileSystemTypeLocal,
                                   storage::FileSystemMountOption(),
                                   base::FilePath(FPL("/a/b/c")));
  mount_points->RegisterFileSystem("d", storage::kFileSystemTypeLocal,
                                   storage::FileSystemMountOption(),
                                   base::FilePath(FPL("/b/c/d")));

  // Register system test mount points.
  system_mount_points->RegisterFileSystem("d", storage::kFileSystemTypeLocal,
                                          storage::FileSystemMountOption(),
                                          base::FilePath(FPL("/g/c/d")));
  system_mount_points->RegisterFileSystem("e", storage::kFileSystemTypeLocal,
                                          storage::FileSystemMountOption(),
                                          base::FilePath(FPL("/g/d/e")));

  std::vector<base::FilePath> root_dirs = backend.GetRootDirectories();
  std::set<base::FilePath> root_dirs_set(root_dirs.begin(), root_dirs.end());
  EXPECT_EQ(initial_root_dirs_size + 4, root_dirs.size());
  EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/a/b/c"))));
  EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/b/c/d"))));
  EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/g/c/d"))));
  EXPECT_TRUE(root_dirs_set.count(base::FilePath(FPL("/g/d/e"))));
}

TEST(ChromeOSFileSystemBackendTest, AccessPermissions) {
  scoped_refptr<storage::ExternalMountPoints> mount_points(
      storage::ExternalMountPoints::CreateRefCounted());
  scoped_refptr<storage::ExternalMountPoints> system_mount_points(
      storage::ExternalMountPoints::CreateRefCounted());
  ash::FileSystemBackend backend(nullptr,  // profile
                                 nullptr,  // file_system_provider_delegate
                                 nullptr,  // mtp_delegate
                                 nullptr,  // arc_content_delegate
                                 nullptr,  // arc_documents_provider_delegate
                                 nullptr,  // drivefs_delegate
                                 nullptr,  // smbfs_delegate
                                 mount_points.get(), system_mount_points.get());

  std::string extension("ddammdhioacbehjngdmkjcjbnfginlla");
  url::Origin origin = url::Origin::Create(
      extensions::Extension::GetBaseURLFromExtensionId(extension));

  // Initialize mount points.
  ASSERT_TRUE(system_mount_points->RegisterFileSystem(
      "system", storage::kFileSystemTypeLocal, storage::FileSystemMountOption(),
      base::FilePath(FPL("/g/system"))));
  ASSERT_TRUE(mount_points->RegisterFileSystem(
      "removable", storage::kFileSystemTypeLocal,
      storage::FileSystemMountOption(),
      base::FilePath(FPL("/media/removable"))));

  // Backend specific mount point access.
  EXPECT_FALSE(backend.IsAccessAllowed(
      ash::BackendFunction::kCreateFileSystemOperation,
      storage::OperationType::kCopy,
      CreateFileSystemURL(extension, "removable/foo", mount_points.get())));

  backend.GrantFileAccessToOrigin(origin, base::FilePath(FPL("removable/foo")));
  EXPECT_TRUE(backend.IsAccessAllowed(
      ash::BackendFunction::kCreateFileSystemOperation,
      storage::OperationType::kCopy,
      CreateFileSystemURL(extension, "removable/foo", mount_points.get())));
  EXPECT_FALSE(backend.IsAccessAllowed(
      ash::BackendFunction::kCreateFileSystemOperation,
      storage::OperationType::kCopy,
      CreateFileSystemURL(extension, "removable/foo1", mount_points.get())));

  // System mount point access.
  EXPECT_FALSE(backend.IsAccessAllowed(
      ash::BackendFunction::kCreateFileSystemOperation,
      storage::OperationType::kCopy,
      CreateFileSystemURL(extension, "system/foo", system_mount_points.get())));

  backend.GrantFileAccessToOrigin(origin, base::FilePath(FPL("system/foo")));
  EXPECT_TRUE(backend.IsAccessAllowed(
      ash::BackendFunction::kCreateFileSystemOperation,
      storage::OperationType::kCopy,
      CreateFileSystemURL(extension, "system/foo", system_mount_points.get())));
  EXPECT_FALSE(
      backend.IsAccessAllowed(ash::BackendFunction::kCreateFileSystemOperation,
                              storage::OperationType::kCopy,
                              CreateFileSystemURL(extension, "system/foo1",
                                                  system_mount_points.get())));

  // The extension cannot access new mount points.
  // TODO(tbarzic): This should probably be changed.
  ASSERT_TRUE(mount_points->RegisterFileSystem(
      "test", storage::kFileSystemTypeLocal, storage::FileSystemMountOption(),
      base::FilePath(FPL("/foo/test"))));
  EXPECT_FALSE(backend.IsAccessAllowed(
      ash::BackendFunction::kCreateFileSystemOperation,
      storage::OperationType::kCopy,
      CreateFileSystemURL(extension, "test_/foo", mount_points.get())));

  backend.RevokeAccessForOrigin(origin);
  EXPECT_FALSE(backend.IsAccessAllowed(
      ash::BackendFunction::kCreateFileSystemOperation,
      storage::OperationType::kCopy,
      CreateFileSystemURL(extension, "removable/foo", mount_points.get())));

  // ImageLoader has access to all files GetMetadata(), GetFileStreamReader().
  std::string image_loader("pmfjbimdmchhbnneeidfognadeopoehp");
  EXPECT_TRUE(backend.IsAccessAllowed(
      ash::BackendFunction::kCreateFileSystemOperation,
      storage::OperationType::kGetMetadata,
      CreateFileSystemURL(image_loader, "removable/foo", mount_points.get())));
  EXPECT_TRUE(backend.IsAccessAllowed(
      ash::BackendFunction::kCreateFileStreamReader,
      storage::OperationType::kNone,
      CreateFileSystemURL(image_loader, "removable/foo", mount_points.get())));
  EXPECT_FALSE(backend.IsAccessAllowed(
      ash::BackendFunction::kCreateFileSystemOperation,
      storage::OperationType::kCopy,
      CreateFileSystemURL(image_loader, "removable/foo", mount_points.get())));
  EXPECT_FALSE(backend.IsAccessAllowed(
      ash::BackendFunction::kCreateFileStreamWriter,
      storage::OperationType::kNone,
      CreateFileSystemURL(image_loader, "removable/foo", mount_points.get())));
}

TEST(ChromeOSFileSystemBackendTest, GetVirtualPathConflictWithSystemPoints) {
  scoped_refptr<storage::ExternalMountPoints> mount_points(
      storage::ExternalMountPoints::CreateRefCounted());
  scoped_refptr<storage::ExternalMountPoints> system_mount_points(
      storage::ExternalMountPoints::CreateRefCounted());
  ash::FileSystemBackend backend(nullptr,  // profile
                                 nullptr,  // file_system_provider_delegate
                                 nullptr,  // mtp_delegate
                                 nullptr,  // arc_content_delegate
                                 nullptr,  // arc_documents_provider_delegate
                                 nullptr,  // drivefs_delegate
                                 nullptr,  // smbfs_delegate
                                 mount_points.get(), system_mount_points.get());

  const storage::FileSystemType type = storage::kFileSystemTypeLocal;
  const storage::FileSystemMountOption option =
      storage::FileSystemMountOption();

  // Backend specific mount points.
  ASSERT_TRUE(mount_points->RegisterFileSystem(
      "b", type, option, base::FilePath(FPL("/a/b"))));
  ASSERT_TRUE(mount_points->RegisterFileSystem(
      "y", type, option, base::FilePath(FPL("/z/y"))));
  ASSERT_TRUE(mount_points->RegisterFileSystem(
      "n", type, option, base::FilePath(FPL("/m/n"))));

  // System mount points
  ASSERT_TRUE(system_mount_points->RegisterFileSystem(
      "gb", type, option, base::FilePath(FPL("/a/b"))));
  ASSERT_TRUE(
      system_mount_points->RegisterFileSystem(
          "gz", type, option, base::FilePath(FPL("/z"))));
  ASSERT_TRUE(system_mount_points->RegisterFileSystem(
       "gp", type, option, base::FilePath(FPL("/m/n/o/p"))));

  struct TestCase {
    const base::FilePath::CharType* const local_path;
    bool success;
    const base::FilePath::CharType* const virtual_path;
  };

  const TestCase kTestCases[] = {
    // Same paths in both mount points.
    { FPL("/a/b/c/d"), true, FPL("b/c/d") },
    // System mount points path more specific.
    { FPL("/m/n/o/p/r/s"), true, FPL("n/o/p/r/s") },
    // System mount points path less specific.
    { FPL("/z/y/x"), true, FPL("y/x") },
    // Only system mount points path matches.
    { FPL("/z/q/r/s"), true, FPL("gz/q/r/s") },
    // No match.
    { FPL("/foo/xxx"), false, FPL("") },
  };

  for (size_t i = 0; i < std::size(kTestCases); ++i) {
    // Initialize virtual path with a value.
    base::FilePath virtual_path(FPL("/mount"));
    base::FilePath local_path(kTestCases[i].local_path);
    EXPECT_EQ(kTestCases[i].success,
              backend.GetVirtualPath(local_path, &virtual_path))
        << "Resolving " << kTestCases[i].local_path;

    // There are no guarantees for |virtual_path| value if |GetVirtualPath|
    // fails.
    if (!kTestCases[i].success)
      continue;

    base::FilePath expected_virtual_path(kTestCases[i].virtual_path);
    EXPECT_EQ(expected_virtual_path, virtual_path)
        << "Resolving " << kTestCases[i].local_path;
  }
}

}  // namespace