File: drive_upload_observer_browsertest.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 (281 lines) | stat: -rw-r--r-- 11,389 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
// 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/drive_upload_observer.h"

#include "base/path_service.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "base/test/test_future.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/file_manager/io_task.h"
#include "chrome/browser/ash/policy/skyvault/policy_utils.h"
#include "chrome/browser/ash/policy/skyvault/test/skyvault_test_base.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/ash/components/drivefs/mojom/drivefs.mojom.h"
#include "components/drive/file_errors.h"
#include "content/public/test/browser_test.h"
#include "storage/browser/file_system/file_system_url.h"

using storage::FileSystemURL;

namespace ash::cloud_upload {

using ::base::test::RunOnceCallback;
using testing::_;

namespace {

const int64_t kFileSize = 123456;

const policy::local_user_files::UploadTrigger kTrigger =
    policy::local_user_files::UploadTrigger::kDownload;

}  // namespace

// Tests the Drive upload workflow using the static
// `DriveUploadObserver::Observe` method. Ensures that the upload completes with
// the expected results.
class DriveUploadObserverTest
    : public policy::local_user_files::SkyvaultGoogleDriveTest {
 public:
  DriveUploadObserverTest() = default;

  DriveUploadObserverTest(const DriveUploadObserverTest&) = delete;
  DriveUploadObserverTest& operator=(const DriveUploadObserverTest&) = delete;

  base::FilePath observed_relative_drive_path(const FileInfo& info) override {
    base::FilePath observed_relative_drive_path;
    drive_integration_service()->GetRelativeDrivePath(
        drive_root_dir().AppendASCII(info.test_file_name_),
        &observed_relative_drive_path);
    return observed_relative_drive_path;
  }

  // IOTaskController::Observer:
  void OnIOTaskStatus(
      const file_manager::io_task::ProgressStatus& status) override {
    // Get the source_file_path_ used in the map; status contains the value
    // after the sync.
    auto it = source_files_.find(
        drive_mount_point().Append(status.sources[0].url.path().BaseName()));
    if (status.type == file_manager::io_task::OperationType::kDelete &&
        status.sources.size() == 1 && it != source_files_.end() &&
        status.state == file_manager::io_task::State::kSuccess) {
      if (on_delete_callback_) {
        std::move(on_delete_callback_).Run();
      }
    }
  }

  void SimulateDriveUploadQueued(const FileInfo& info) {
    // Set file metadata for `drivefs::mojom::DriveFs::GetMetadata`.
    drivefs::FakeMetadata metadata;
    metadata.path = observed_relative_drive_path(info);
    metadata.original_name = info.test_file_name_;
    fake_drivefs().SetMetadata(std::move(metadata));

    // Simulate server sync events.
    drivefs::mojom::SyncingStatusPtr status =
        drivefs::mojom::SyncingStatus::New();
    status->item_events.emplace_back(
        std::in_place, 12, 34, observed_relative_drive_path(info).value(),
        drivefs::mojom::ItemEvent::State::kQueued, 0u, kFileSize,
        drivefs::mojom::ItemEventReason::kTransfer);
    drivefs_delegate()->OnSyncingStatusUpdate(status.Clone());
    drivefs_delegate().FlushForTesting();
  }

  void SimulateDriveUploadInProgress(int64_t bytes_transferred,
                                     const FileInfo& info) {
    // Set file metadata for `drivefs::mojom::DriveFs::GetMetadata`.
    drivefs::FakeMetadata metadata;
    metadata.path = observed_relative_drive_path(info);
    metadata.original_name = info.test_file_name_;
    fake_drivefs().SetMetadata(std::move(metadata));

    // Simulate server sync events.
    drivefs::mojom::SyncingStatusPtr status =
        drivefs::mojom::SyncingStatus::New();
    status->item_events.emplace_back(
        std::in_place, 12, 34, observed_relative_drive_path(info).value(),
        drivefs::mojom::ItemEvent::State::kInProgress, bytes_transferred,
        kFileSize, drivefs::mojom::ItemEventReason::kTransfer);
    drivefs_delegate()->OnSyncingStatusUpdate(status.Clone());
    drivefs_delegate().FlushForTesting();
  }

  void SimulateDriveUploadCompleted(const FileInfo& info) {
    // Set file metadata for `drivefs::mojom::DriveFs::GetMetadata`.
    drivefs::FakeMetadata metadata;
    metadata.path = observed_relative_drive_path(info);
    metadata.original_name = info.test_file_name_;
    fake_drivefs().SetMetadata(std::move(metadata));

    // Simulate server sync events.
    drivefs::mojom::SyncingStatusPtr status =
        drivefs::mojom::SyncingStatus::New();
    status->item_events.emplace_back(
        std::in_place, 12, 34, observed_relative_drive_path(info).value(),
        drivefs::mojom::ItemEvent::State::kCompleted, kFileSize, kFileSize,
        drivefs::mojom::ItemEventReason::kTransfer);
    drivefs_delegate()->OnSyncingStatusUpdate(status.Clone());
    drivefs_delegate().FlushForTesting();
  }

  void SimulateDriveUploadFailure(const FileInfo& info) {
    drivefs::mojom::SyncingStatusPtr fail_status =
        drivefs::mojom::SyncingStatus::New();
    fail_status->item_events.emplace_back(
        std::in_place, 12, 34, observed_relative_drive_path(info).value(),
        drivefs::mojom::ItemEvent::State::kFailed, 123, kFileSize,
        drivefs::mojom::ItemEventReason::kTransfer);
    drivefs_delegate()->OnSyncingStatusUpdate(fail_status->Clone());
    drivefs_delegate().FlushForTesting();
  }

 protected:
  base::OnceClosure on_delete_callback_;
};

// Send file queuing event, the file should be immediately uploaded.
IN_PROC_BROWSER_TEST_F(DriveUploadObserverTest, ImmediatelyUpload) {
  const std::string test_file_name = "archive.tar.gz";
  base::FilePath source_file_path =
      SetUpSourceFile(test_file_name, drive_mount_point());

  EXPECT_CALL(fake_drivefs(), ImmediatelyUpload)
      .WillOnce(RunOnceCallback<1>(drive::FileError::FILE_ERROR_OK));

  base::MockCallback<base::RepeatingCallback<void(int64_t)>> progress_callback;
  base::MockCallback<base::OnceCallback<void(bool)>> upload_callback;
  EXPECT_CALL(upload_callback, Run(/*success=*/true));
  DriveUploadObserver::Observe(
      profile(), drive_root_dir().AppendASCII(test_file_name), kTrigger,
      kFileSize, progress_callback.Get(), upload_callback.Get());

  auto it = source_files_.find(source_file_path);
  SimulateDriveUploadQueued(it->second);

  // Check that the source file has been moved to Drive.
  CheckPathExistsOnDrive(observed_relative_drive_path(it->second));
}

// Send progress updates then completed sync events.
IN_PROC_BROWSER_TEST_F(DriveUploadObserverTest, SuccessfulSync) {
  const std::string test_file_name = "image.webp";
  base::FilePath source_file_path =
      SetUpSourceFile(test_file_name, drive_mount_point());

  base::MockCallback<base::RepeatingCallback<void(int64_t)>> progress_callback;
  base::MockCallback<base::OnceCallback<void(bool)>> upload_callback;
  EXPECT_CALL(progress_callback, Run(/*bytes_so_far=*/kFileSize / 4));
  EXPECT_CALL(progress_callback, Run(/*bytes_so_far=*/kFileSize));
  EXPECT_CALL(upload_callback, Run(/*success=*/true));
  DriveUploadObserver::Observe(
      profile(), drive_root_dir().AppendASCII(test_file_name), kTrigger,
      kFileSize, progress_callback.Get(), upload_callback.Get());

  auto it = source_files_.find(source_file_path);
  SimulateDriveUploadInProgress(kFileSize / 4, it->second);
  SimulateDriveUploadCompleted(it->second);

  // Check that the source file has been moved to Drive.
  CheckPathExistsOnDrive(observed_relative_drive_path(it->second));
}

// Send syncing error event, the cached file should be deleted.
IN_PROC_BROWSER_TEST_F(DriveUploadObserverTest, ErrorSync) {
  const std::string test_file_name = "id3Audio.mp3";
  base::FilePath source_file_path =
      SetUpSourceFile(test_file_name, drive_mount_point());

  base::MockCallback<base::RepeatingCallback<void(int64_t)>> progress_callback;
  base::MockCallback<base::OnceCallback<void(bool)>> upload_callback;
  EXPECT_CALL(upload_callback, Run(/*success=*/false));
  DriveUploadObserver::Observe(
      profile(), drive_root_dir().AppendASCII(test_file_name), kTrigger,
      kFileSize, progress_callback.Get(), upload_callback.Get());

  SetUpObservers();

  auto future = base::test::TestFuture<void>();
  on_delete_callback_ = future.GetCallback();

  auto it = source_files_.find(source_file_path);
  SimulateDriveUploadFailure(it->second);
  EXPECT_TRUE(future.Wait());
}

// When the sync timer times out and no download url in the file metadata, the
// upload will fail and the file will be deleted from the cache.
IN_PROC_BROWSER_TEST_F(DriveUploadObserverTest, NoSyncUpdates) {
  const std::string test_file_name = "popup.pdf";
  base::FilePath source_file_path =
      SetUpSourceFile(test_file_name, drive_mount_point());

  base::MockCallback<base::RepeatingCallback<void(int64_t)>> progress_callback;
  base::MockCallback<base::OnceCallback<void(bool)>> upload_callback;
  scoped_refptr<DriveUploadObserver> drive_upload_observer =
      new DriveUploadObserver(profile(),
                              drive_root_dir().AppendASCII(test_file_name),
                              kTrigger, kFileSize, progress_callback.Get());
  drive_upload_observer->Run(upload_callback.Get());

  EXPECT_TRUE(drive_upload_observer->no_sync_update_timeout_.IsRunning());

  SetUpObservers();

  auto future = base::test::TestFuture<void>();
  on_delete_callback_ = future.GetCallback();

  EXPECT_CALL(upload_callback, Run(/*success=*/false));

  drivefs::FakeMetadata metadata;
  auto it = source_files_.find(source_file_path);
  metadata.path = observed_relative_drive_path(it->second);
  metadata.original_name = test_file_name;
  fake_drivefs().SetMetadata(std::move(metadata));

  drive_upload_observer->no_sync_update_timeout_.FireNow();

  base::RunLoop loop;
  loop.RunUntilIdle();
  EXPECT_TRUE(future.Wait());
}

// When the sync timer times out and no file metadata is returned, the upload
// will fail and the file will be deleted from the cache.
IN_PROC_BROWSER_TEST_F(DriveUploadObserverTest, NoFileMetadata) {
  const std::string test_file_name = "popup.pdf";
  base::FilePath source_file_path =
      SetUpSourceFile(test_file_name, drive_mount_point());

  base::MockCallback<base::RepeatingCallback<void(int64_t)>> progress_callback;
  base::MockCallback<base::OnceCallback<void(bool)>> upload_callback;
  scoped_refptr<DriveUploadObserver> drive_upload_observer =
      new DriveUploadObserver(profile(),
                              drive_root_dir().AppendASCII(test_file_name),
                              kTrigger, kFileSize, progress_callback.Get());
  drive_upload_observer->Run(upload_callback.Get());

  EXPECT_TRUE(drive_upload_observer->no_sync_update_timeout_.IsRunning());

  SetUpObservers();

  auto future = base::test::TestFuture<void>();
  on_delete_callback_ = future.GetCallback();

  EXPECT_CALL(upload_callback, Run(/*success=*/false));
  drive_upload_observer->no_sync_update_timeout_.FireNow();

  base::RunLoop loop;
  loop.RunUntilIdle();
  EXPECT_TRUE(future.Wait());
}

}  // namespace ash::cloud_upload