File: file_utils_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 (167 lines) | stat: -rw-r--r-- 5,917 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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/apps/app_service/file_utils.h"

#include <memory>
#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#include "base/files/file_path.h"
#include "base/strings/strcat.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/common/extension.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/common/file_system/file_system_util.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_constants.h"

namespace apps {

namespace {

using ::testing::ElementsAre;
using ::testing::IsEmpty;

class FileUtilsTest : public ::testing::Test {
 public:
  void SetUp() override {
    testing::Test::SetUp();
    profile_manager_ = std::make_unique<TestingProfileManager>(
        TestingBrowserProcess::GetGlobal());
    ASSERT_TRUE(profile_manager_->SetUp());
    profile_ = profile_manager_->CreateTestingProfile("testing_profile");
    ASSERT_TRUE(
        storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
            mount_name_, storage::FileSystemType::kFileSystemTypeExternal,
            storage::FileSystemMountOption(), base::FilePath(fs_root_)));
    ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
  }

  void TearDown() override {
    ASSERT_TRUE(
        storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
            mount_name_));
    profile_ = nullptr;
    profile_manager_->DeleteAllTestingProfiles();
    profile_manager_.reset();
  }

  TestingProfile* GetProfile() { return profile_; }

  base::FilePath GetTempDir() { return scoped_temp_dir_.GetPath(); }

  // FileUtils explicitly relies on ChromeOS Files.app for files manipulation.
  const url::Origin GetFileManagerOrigin() {
    return url::Origin::Create(file_manager::util::GetFileManagerURL());
  }

  // Converts the given virtual |path| to a file system URL. Uses test file
  // system type.
  storage::FileSystemURL ToTestFileSystemURL(const std::string& path) {
    return storage::FileSystemURL::CreateForTest(
        blink::StorageKey::CreateFirstParty(GetFileManagerOrigin()),
        storage::FileSystemType::kFileSystemTypeTest, base::FilePath(path));
  }

  // For a given |root| converts the given virtual |path| to a GURL.
  GURL ToGURL(const base::FilePath& root, const std::string& path) {
    const std::string abs_path = root.Append(path).value();
    return GURL(base::StrCat({url::kFileSystemScheme, ":",
                              GetFileManagerOrigin().Serialize(), abs_path}));
  }

 protected:
  const std::string mount_name_ = "TestMountName";
  const std::string fs_root_ = "/path/to/test/filesystemroot";

 private:
  content::BrowserTaskEnvironment task_environment_;
  std::unique_ptr<TestingProfileManager> profile_manager_;
  base::ScopedTempDir scoped_temp_dir_;
  raw_ptr<TestingProfile> profile_ = nullptr;
};

TEST_F(FileUtilsTest, GetFileSystemURL) {
  std::vector<GURL> url_list;
  std::vector<storage::FileSystemURL> fsurl_list;

  // Case 1: url_list is empty.
  fsurl_list = GetFileSystemURL(GetProfile(), url_list);
  EXPECT_THAT(fsurl_list, IsEmpty());

  // Case 2: url_list contains a GURL.
  const std::string path = "Documents/foo.txt";
  url_list.push_back(ToGURL(base::FilePath(storage::kTestDir), path));
  fsurl_list = GetFileSystemURL(GetProfile(), url_list);
  EXPECT_THAT(fsurl_list, ElementsAre(ToTestFileSystemURL(path)));
}

TEST_F(FileUtilsTest, GetFileSystemUrls) {
  std::vector<GURL> url_list;
  std::vector<base::FilePath> fp_list;

  // Case 1: fp_list is empty.
  url_list = GetFileSystemUrls(GetProfile(), fp_list);
  EXPECT_THAT(url_list, IsEmpty());

  // Case 2: fp_list contain some paths.
  const std::string path = "Images/foo.jpg";
  fp_list.push_back(base::FilePath(fs_root_).Append(path));
  url_list = GetFileSystemUrls(GetProfile(), fp_list);
  // Given a list of absolute file paths, return a list of filesystem:// URLs
  // that use the kFileSystemTypeExternal type with Files Manager's origin.
  // TODO(crbug.com/40763788): The use of Files Manager origin in these URLs is
  // probably incorrect and should be revisited.
  EXPECT_THAT(
      url_list,
      ElementsAre(ToGURL(
          base::FilePath(storage::kExternalDir).Append(mount_name_), path)));

  // Case 3: paths not originating in a known root are ignored.
  fp_list.push_back(base::FilePath("/not/a/known/root").Append(path));
  url_list = GetFileSystemUrls(GetProfile(), fp_list);
  // Still just one path corresponding to foo.jpg under a known root.
  EXPECT_THAT(
      url_list,
      ElementsAre(ToGURL(
          base::FilePath(storage::kExternalDir).Append(mount_name_), path)));
}

TEST_F(FileUtilsTest, GetSingleFileSystemURL) {
  GURL url;
  storage::FileSystemURL fsurl;

  const std::string path = "Documents/foo.txt";
  url = ToGURL(base::FilePath(storage::kTestDir), path);
  fsurl = GetFileSystemURL(GetProfile(), url);
  EXPECT_EQ(fsurl, ToTestFileSystemURL(path));
}

TEST_F(FileUtilsTest, GetSingleFileSystemUrl) {
  GURL url;
  base::FilePath file_path;

  const std::string path = "Images/foo.jpg";
  file_path = base::FilePath(fs_root_).Append(path);
  url = GetFileSystemUrl(GetProfile(), file_path);
  EXPECT_EQ(
      url,
      ToGURL(base::FilePath(storage::kExternalDir).Append(mount_name_), path));
}

}  // namespace

}  // namespace apps