File: skyvault_test_base.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 (248 lines) | stat: -rw-r--r-- 8,813 bytes parent folder | download | duplicates (3)
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
// Copyright 2024 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/ash/policy/skyvault/test/skyvault_test_base.h"

#include <string>

#include "base/base_paths.h"
#include "base/check.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/test/test_future.h"
#include "chrome/browser/ash/drive/drive_integration_service_factory.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/file_manager/volume_manager.h"
#include "chrome/browser/ash/policy/skyvault/local_files_migration_constants.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "storage/browser/file_system/external_mount_points.h"

namespace policy::local_user_files {

base::FilePath GetTestFilePath(const std::string& file_name) {
  // Get the path to file manager's test data directory.
  base::FilePath source_dir;
  CHECK(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &source_dir));
  base::FilePath test_data_dir = source_dir.AppendASCII("chrome")
                                     .AppendASCII("test")
                                     .AppendASCII("data")
                                     .AppendASCII("chromeos")
                                     .AppendASCII("file_manager");
  return test_data_dir.Append(base::FilePath::FromUTF8Unsafe(file_name));
}

void SkyvaultTestBase::TearDown() {
  InProcessBrowserTest::TearDown();
  storage::ExternalMountPoints::GetSystemInstance()->RevokeAllFileSystems();
}

void SkyvaultTestBase::SetUpMyFiles() {
  my_files_dir_ = GetMyFilesPath(browser()->profile());
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    ASSERT_TRUE(base::CreateDirectory(my_files_dir_));
  }
  std::string mount_point_name =
      file_manager::util::GetDownloadsMountPointName(profile());
  storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
      mount_point_name);
  CHECK(storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
      mount_point_name, storage::kFileSystemTypeLocal,
      storage::FileSystemMountOption(), my_files_dir_));
  file_manager::VolumeManager::Get(profile())
      ->RegisterDownloadsDirectoryForTesting(my_files_dir_);
}

base::FilePath SkyvaultTestBase::CreateTestDir(
    const std::string& test_dir_name,
    const base::FilePath& parent_dir) {
  const base::FilePath dir_path = parent_dir.AppendASCII(test_dir_name);
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    CHECK(base::CreateDirectory(dir_path));
    CHECK(base::PathExists(dir_path));
  }

  return dir_path;
}

base::FilePath SkyvaultTestBase::CopyTestFile(
    const std::string& test_file_name,
    const base::FilePath& parent_dir) {
  const base::FilePath copied_file_path =
      parent_dir.AppendASCII(test_file_name);

  const base::FilePath test_file_path = GetTestFilePath(test_file_name);
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    CHECK(base::CopyFile(test_file_path, copied_file_path));
    CHECK(base::PathExists(copied_file_path));
  }

  return copied_file_path;
}

void SkyvaultOneDriveTest::SetUpODFS() {
  provided_file_system_ =
      file_manager::test::MountFakeProvidedFileSystemOneDrive(profile());
}

void SkyvaultOneDriveTest::CheckPathExistsOnODFS(const base::FilePath& path) {
  ASSERT_TRUE(provided_file_system_);
  base::test::TestFuture<
      std::unique_ptr<ash::file_system_provider::EntryMetadata>,
      base::File::Error>
      future;
  provided_file_system_->GetMetadata(path, {}, future.GetCallback());
  EXPECT_EQ(base::File::Error::FILE_OK, future.Get<base::File::Error>());
}

void SkyvaultOneDriveTest::CheckPathNotFoundOnODFS(const base::FilePath& path) {
  ASSERT_TRUE(provided_file_system_);
  base::test::TestFuture<
      std::unique_ptr<ash::file_system_provider::EntryMetadata>,
      base::File::Error>
      future;
  provided_file_system_->GetMetadata(path, {}, future.GetCallback());
  EXPECT_EQ(base::File::Error::FILE_ERROR_NOT_FOUND,
            future.Get<base::File::Error>());
}

SkyvaultGoogleDriveTest::SkyvaultGoogleDriveTest() {
  EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
  drive_mount_point_ = temp_dir_.GetPath().Append("drivefs");
  drive_root_dir_ = drive_mount_point_.AppendASCII("root");
}

SkyvaultGoogleDriveTest::~SkyvaultGoogleDriveTest() = default;

void SkyvaultGoogleDriveTest::SetUpInProcessBrowserTestFixture() {
  // Setup drive integration service.
  create_drive_integration_service_ = base::BindRepeating(
      &SkyvaultGoogleDriveTest::CreateDriveIntegrationService,
      base::Unretained(this));
  service_factory_for_test_ = std::make_unique<
      drive::DriveIntegrationServiceFactory::ScopedFactoryForTest>(
      &create_drive_integration_service_);
}

void SkyvaultGoogleDriveTest::SetUpOnMainThread() {
  SetDriveConnectionStatusForTesting(ConnectionStatus::kConnected);
  SkyvaultTestBase::SetUpOnMainThread();
}

void SkyvaultGoogleDriveTest::SetUp() {
  InProcessBrowserTest::SetUp();
  // Create the Google Drive root directory
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    EXPECT_TRUE(base::CreateDirectory(drive_root_dir_));
  }
}

void SkyvaultGoogleDriveTest::TearDown() {
  SkyvaultTestBase::TearDown();
  storage::ExternalMountPoints::GetSystemInstance()->RevokeAllFileSystems();
}

void SkyvaultGoogleDriveTest::TearDownOnMainThread() {
  RemoveObservers();
  SkyvaultTestBase::TearDownOnMainThread();
}

void SkyvaultGoogleDriveTest::SetUpObservers() {
  // Subscribe to IOTasks updates to track the copy/move to Drive progress.
  file_manager::VolumeManager::Get(profile())
      ->io_task_controller()
      ->AddObserver(this);
}

void SkyvaultGoogleDriveTest::RemoveObservers() {
  file_manager::VolumeManager::Get(profile())
      ->io_task_controller()
      ->RemoveObserver(this);
}

base::FilePath SkyvaultGoogleDriveTest::SetUpSourceFile(
    const std::string& test_file_name,
    base::FilePath source_path) {
  base::FilePath source_file_path = CopyTestFile(test_file_name, source_path);

  base::FilePath local_relative_path;
  GetMyFilesPath(browser()->profile())
      .AppendRelativePath(source_file_path, &local_relative_path);
  FileInfo info(test_file_name, local_relative_path);
  // Check that the source file exists at the intended source location and is
  // not in Drive.
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    EXPECT_TRUE(base::PathExists(source_file_path));
    CheckPathNotFoundOnDrive(observed_relative_drive_path(info));
  }

  source_files_.emplace(source_file_path, std::move(info));
  return source_file_path;
}

void SkyvaultGoogleDriveTest::CheckPathExistsOnDrive(
    const base::FilePath& path) {
  drive_integration_service()->GetDriveFsInterface()->GetMetadata(
      path, base::BindOnce(&SkyvaultGoogleDriveTest::OnGetMetadataExpectSuccess,
                           base::Unretained(this)));
  base::ScopedAllowBlockingForTesting allow_blocking;
  Wait();
}
void SkyvaultGoogleDriveTest::CheckPathNotFoundOnDrive(
    const base::FilePath& path) {
  drive_integration_service()->GetDriveFsInterface()->GetMetadata(
      path,
      base::BindOnce(&SkyvaultGoogleDriveTest::OnGetMetadataExpectNotFound,
                     base::Unretained(this)));
  base::ScopedAllowBlockingForTesting allow_blocking;
  Wait();
}

void SkyvaultGoogleDriveTest::Wait() {
  base::ScopedAllowBlockingForTesting allow_blocking;
  ASSERT_FALSE(run_loop_);
  run_loop_ = std::make_unique<base::RunLoop>();
  run_loop_->Run();
  run_loop_ = nullptr;
}

void SkyvaultGoogleDriveTest::EndWait() {
  ASSERT_TRUE(run_loop_);
  run_loop_->Quit();
}

DriveIntegrationService* SkyvaultGoogleDriveTest::CreateDriveIntegrationService(
    Profile* profile) {
  base::ScopedAllowBlockingForTesting allow_blocking;
  fake_drivefs_helpers_[profile] =
      std::make_unique<file_manager::test::FakeSimpleDriveFsHelper>(
          profile, drive_mount_point_);
  return new DriveIntegrationService(
      g_browser_process->local_state(), profile, "", drive_mount_point_,
      fake_drivefs_helpers_[profile]->CreateFakeDriveFsListenerFactory());
}

void SkyvaultGoogleDriveTest::OnGetMetadataExpectSuccess(
    drive::FileError error,
    drivefs::mojom::FileMetadataPtr metadata) {
  EXPECT_EQ(drive::FILE_ERROR_OK, error);
  EndWait();
}

void SkyvaultGoogleDriveTest::OnGetMetadataExpectNotFound(
    drive::FileError error,
    drivefs::mojom::FileMetadataPtr metadata) {
  EXPECT_EQ(drive::FILE_ERROR_NOT_FOUND, error);
  EndWait();
}

}  // namespace policy::local_user_files