File: devtools_file_helper_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 (450 lines) | stat: -rw-r--r-- 17,445 bytes parent folder | download | duplicates (5)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// 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/devtools/devtools_file_helper.h"

#include <vector>

#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/files/scoped_temp_file.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/task/current_thread.h"
#include "base/test/bind.h"
#include "base/test/mock_callback.h"
#include "base/uuid.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::IsEmpty;
using ::testing::IsNull;
using ::testing::NotNull;
using ::testing::Optional;
using ::testing::Pointee;
using ::testing::Return;
using ::testing::SizeIs;
using ::testing::StrictMock;
using ::testing::Test;

namespace {

#if BUILDFLAG(IS_WIN)
static const char kDownloadPath[] = "c:\\\\path\\to\\download";
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
static const char kDownloadPath[] = "/path/to/download";
#endif  // BUILDFLAG(IS_WIN)

class MockDelegate : public DevToolsFileHelper::Delegate {
 public:
  MOCK_METHOD(void,
              FileSystemAdded,
              (const std::string&, const DevToolsFileHelper::FileSystem*),
              (override));
  MOCK_METHOD(void, FileSystemRemoved, (const std::string&), (override));
  MOCK_METHOD(void,
              FilePathsChanged,
              (const std::vector<std::string>&,
               const std::vector<std::string>&,
               const std::vector<std::string>&),
              (override));
};

class MockStorage : public DevToolsFileHelper::Storage {
 public:
  MOCK_METHOD(DevToolsFileHelper::FileSystem,
              RegisterFileSystem,
              (const base::FilePath&, const std::string&),
              (override));
  MOCK_METHOD(void, UnregisterFileSystem, (const base::FilePath&), (override));

  MOCK_METHOD(std::vector<base::FilePath>,
              GetDraggedFileSystemPaths,
              (const GURL&),
              (override));
};

// testing::InvokeArgument<N> does not work with base::OnceCallback. Use this
// gmock action template to invoke base::OnceCallback. `k` is the k-th argument
// and `T` is the callback's type.
ACTION_TEMPLATE(InvokeCallbackArgument,
                HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
                AND_0_VALUE_PARAMS()) {
  std::move(const_cast<T&>(std::get<k>(args))).Run();
}
ACTION_TEMPLATE(InvokeCallbackArgument,
                HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
                AND_1_VALUE_PARAMS(p0)) {
  std::move(const_cast<T&>(std::get<k>(args))).Run(p0);
}

}  // namespace

class DevToolsFileHelperTest : public Test {
 protected:
  StrictMock<MockDelegate>& delegate() const { return *delegate_; }
  DevToolsFileHelper* file_helper() const { return file_helper_.get(); }
  TestingProfile* profile() const { return profile_.get(); }
  StrictMock<MockStorage>& storage() const { return *storage_; }

  DevToolsFileHelper::SelectFileCallback FakeSelectFileCallback(
      base::FilePath path) {
    return base::BindLambdaForTesting(
        [path](DevToolsFileHelper::SelectedCallback selected_callback,
               DevToolsFileHelper::CanceledCallback, const base::FilePath&) {
          std::move(selected_callback).Run(path);
        });
  }

  void SetUp() override {
    TestingProfile::Builder builder;
    profile_ = builder.Build();
    storage_ = std::make_unique<StrictMock<MockStorage>>();
    delegate_ = std::make_unique<StrictMock<MockDelegate>>();
    file_helper_ = std::make_unique<DevToolsFileHelper>(
        profile(), delegate_.get(), storage_.get());

    DownloadCoreServiceFactory::GetForBrowserContext(profile())
        ->SetDownloadManagerDelegateForTesting(
            std::make_unique<ChromeDownloadManagerDelegate>(profile()));
    DownloadPrefs::FromBrowserContext(profile())->SetDownloadPath(
        base::FilePath::FromASCII(kDownloadPath));
  }

  void TearDown() override {
    DownloadCoreServiceFactory::GetForBrowserContext(profile())
        ->SetDownloadManagerDelegateForTesting(nullptr);

    file_helper_.reset();
    delegate_.reset();
    storage_.reset();
    profile_.reset();
  }

 private:
  content::BrowserTaskEnvironment task_environment_;
  std::unique_ptr<StrictMock<MockDelegate>> delegate_;
  std::unique_ptr<StrictMock<MockStorage>> storage_;
  std::unique_ptr<TestingProfile> profile_;
  std::unique_ptr<DevToolsFileHelper> file_helper_;
};

TEST_F(DevToolsFileHelperTest, SaveToFileBase64) {
  base::ScopedTempFile tf;
  ASSERT_TRUE(tf.Create());
  const std::vector<uint8_t> data{0, 'a', 's', 'm', 1, 0, 0, 0};

  base::RunLoop run_loop;
  file_helper()->Save(
      "https://example.com/test.wasm", "AGFzbQEAAAA=", /* save_as */ true,
      /* is_base64 */ true, FakeSelectFileCallback(tf.path()),
      base::BindLambdaForTesting([&](const std::string&) { run_loop.Quit(); }),
      base::DoNothing());
  run_loop.Run();

  EXPECT_EQ(base::ReadFileToBytes(tf.path()), data);
}

TEST_F(DevToolsFileHelperTest, SaveToFileInvalidBase64) {
  base::ScopedTempFile tf;
  ASSERT_TRUE(tf.Create());

  base::RunLoop run_loop;
  file_helper()->Save(
      "https://example.com/test.wasm", "~~~~",
      /* save_as */ true,
      /* is_base64 */ true, FakeSelectFileCallback(tf.path()),
      base::BindLambdaForTesting([&](const std::string&) { run_loop.Quit(); }),
      base::DoNothing());
  run_loop.Run();

  EXPECT_THAT(base::ReadFileToBytes(tf.path()), Optional(IsEmpty()));
}

TEST_F(DevToolsFileHelperTest, SaveToFileText) {
  base::ScopedTempFile tf;
  ASSERT_TRUE(tf.Create());
  const std::vector<uint8_t> data{'s', 'o', 'm', 'e', ' ', 't', 'e', 'x', 't'};

  base::RunLoop run_loop;
  file_helper()->Save(
      "https://example.com/test.txt", "some text",
      /* save_as */ true,
      /* is_base64 */ false, FakeSelectFileCallback(tf.path()),
      base::BindLambdaForTesting([&](const std::string&) { run_loop.Quit(); }),
      base::DoNothing());
  run_loop.Run();

  EXPECT_EQ(base::ReadFileToBytes(tf.path()), data);
}

TEST_F(DevToolsFileHelperTest, AddFileSystemWithIllegalTypeAutomatic) {
  EXPECT_CALL(delegate(), FileSystemAdded("<illegal type>", IsNull())).Times(1);

  file_helper()->AddFileSystem("automatic", base::DoNothing(),
                               base::DoNothing());

  EXPECT_THAT(profile()->GetPrefs()->GetDict(prefs::kDevToolsFileSystemPaths),
              IsEmpty());
}

TEST_F(DevToolsFileHelperTest, AddFileSystemWithIllegalTypeUUID) {
  EXPECT_CALL(delegate(), FileSystemAdded("<illegal type>", IsNull())).Times(1);

  file_helper()->AddFileSystem(
      base::Uuid::GenerateRandomV4().AsLowercaseString(), base::DoNothing(),
      base::DoNothing());

  EXPECT_THAT(profile()->GetPrefs()->GetDict(prefs::kDevToolsFileSystemPaths),
              IsEmpty());
}

TEST_F(DevToolsFileHelperTest, AddFileSystemWithSelectionCanceled) {
  base::MockCallback<DevToolsFileHelper::SelectFileCallback> select_file_cb;
  EXPECT_CALL(select_file_cb, Run)
      .WillOnce(InvokeCallbackArgument<1, base::OnceCallback<void(void)>>());
  EXPECT_CALL(delegate(), FileSystemAdded("<selection cancelled>", IsNull()))
      .Times(1);

  file_helper()->AddFileSystem("", select_file_cb.Get(), base::DoNothing());

  EXPECT_THAT(profile()->GetPrefs()->GetDict(prefs::kDevToolsFileSystemPaths),
              IsEmpty());
}

TEST_F(DevToolsFileHelperTest, ConnectAutomaticFileSystemWithRelativePath) {
  base::MockCallback<DevToolsFileHelper::ConnectCallback> connect_cb;
  EXPECT_CALL(connect_cb, Run(false)).Times(1);
  EXPECT_CALL(delegate(), FileSystemAdded("<illegal path>", IsNull())).Times(1);

  file_helper()->ConnectAutomaticFileSystem(
      "path/to/folder", base::Uuid::GenerateRandomV4(),
      /* add_if_missing */ false, base::DoNothing(), connect_cb.Get());

  EXPECT_THAT(profile()->GetPrefs()->GetDict(prefs::kDevToolsFileSystemPaths),
              IsEmpty());
}

TEST_F(DevToolsFileHelperTest, ConnectAutomaticFileSystemWithNonExistentPath) {
  base::ScopedTempDir td;
  ASSERT_TRUE(td.CreateUniqueTempDir());
  base::FilePath path = td.GetPath().AppendASCII("NonExistent");
  base::MockCallback<DevToolsFileHelper::ConnectCallback> connect_cb;
  EXPECT_CALL(connect_cb, Run(false)).Times(1);
  EXPECT_CALL(delegate(), FileSystemAdded("<illegal path>", IsNull())).Times(1);

  base::RunLoop run_loop;
  ON_CALL(delegate(), FileSystemAdded).WillByDefault([&] { run_loop.Quit(); });
  file_helper()->ConnectAutomaticFileSystem(
      path.AsUTF8Unsafe(), base::Uuid::GenerateRandomV4(),
      /* add_if_missing */ true, base::DoNothing(), connect_cb.Get());
  run_loop.Run();

  EXPECT_THAT(profile()->GetPrefs()->GetDict(prefs::kDevToolsFileSystemPaths),
              IsEmpty());
}

TEST_F(DevToolsFileHelperTest, ConnectAutomaticFileSystemButNotAddingMissing) {
  base::ScopedTempDir td;
  ASSERT_TRUE(td.CreateUniqueTempDir());
  base::FilePath path = td.GetPath();
  base::MockCallback<DevToolsFileHelper::ConnectCallback> connect_cb;
  EXPECT_CALL(connect_cb, Run(false)).Times(1);

  file_helper()->ConnectAutomaticFileSystem(
      path.AsUTF8Unsafe(), base::Uuid::GenerateRandomV4(),
      /* add_if_missing */ false, base::DoNothing(), connect_cb.Get());

  EXPECT_THAT(profile()->GetPrefs()->GetDict(prefs::kDevToolsFileSystemPaths),
              IsEmpty());
}

TEST_F(DevToolsFileHelperTest, ConnectAutomaticFileSystemInfoBarDenied) {
  base::ScopedTempDir td;
  ASSERT_TRUE(td.CreateUniqueTempDir());
  base::FilePath path = td.GetPath();
  base::MockCallback<DevToolsFileHelper::HandlePermissionsCallback>
      handle_permissions_callback;
  EXPECT_CALL(handle_permissions_callback, Run)
      .WillOnce(
          InvokeCallbackArgument<2, base::OnceCallback<void(bool)>>(false));
  base::MockCallback<DevToolsFileHelper::ConnectCallback> connect_cb;
  EXPECT_CALL(connect_cb, Run(false)).Times(1);
  EXPECT_CALL(delegate(), FileSystemAdded("<permission denied>", IsNull()))
      .Times(1);

  base::RunLoop run_loop;
  ON_CALL(delegate(), FileSystemAdded).WillByDefault([&] { run_loop.Quit(); });
  file_helper()->ConnectAutomaticFileSystem(
      path.AsUTF8Unsafe(), base::Uuid::GenerateRandomV4(),
      /* add_if_missing */ true, handle_permissions_callback.Get(),
      connect_cb.Get());
  run_loop.Run();

  EXPECT_THAT(profile()->GetPrefs()->GetDict(prefs::kDevToolsFileSystemPaths),
              IsEmpty());
}

TEST_F(DevToolsFileHelperTest, ConnectAutomaticFileSystemAlreadyKnown) {
  base::ScopedTempDir td;
  ASSERT_TRUE(td.CreateUniqueTempDir());
  base::FilePath path = td.GetPath();
  base::Uuid uuid = base::Uuid::GenerateRandomV4();
  {
    ScopedDictPrefUpdate update(profile()->GetPrefs(),
                                prefs::kDevToolsFileSystemPaths);
    update.Get().Set(path.AsUTF8Unsafe(), uuid.AsLowercaseString());
  }
  EXPECT_THAT(file_helper()->GetFileSystems(), IsEmpty());
  DevToolsFileHelper::FileSystem file_system{
      "automatic", "test", "filesystem:test", path.AsUTF8Unsafe()};
  base::MockCallback<DevToolsFileHelper::HandlePermissionsCallback>
      handle_permissions_callback;
  EXPECT_CALL(handle_permissions_callback, Run).Times(0);
  base::MockCallback<DevToolsFileHelper::ConnectCallback> connect_cb;
  EXPECT_CALL(connect_cb, Run(true)).Times(1);
  EXPECT_CALL(storage(), RegisterFileSystem(path, "automatic"))
      .WillOnce(Return(file_system));
  EXPECT_CALL(delegate(), FileSystemAdded(IsEmpty(), Pointee(file_system)))
      .Times(1);

  base::RunLoop run_loop;
  ON_CALL(delegate(), FileSystemAdded).WillByDefault([&] { run_loop.Quit(); });
  file_helper()->ConnectAutomaticFileSystem(path.AsUTF8Unsafe(), uuid,
                                            /* add_if_missing */ false,
                                            handle_permissions_callback.Get(),
                                            connect_cb.Get());
  run_loop.Run();

  const base::Value::Dict& file_system_paths_value =
      profile()->GetPrefs()->GetDict(prefs::kDevToolsFileSystemPaths);
  EXPECT_THAT(file_system_paths_value, SizeIs(1));
  EXPECT_THAT(file_system_paths_value.FindString(path.AsUTF8Unsafe()),
              Pointee(uuid.AsLowercaseString()));
}

TEST_F(DevToolsFileHelperTest, ConnectAutomaticFileSystemNewlyAdded) {
  EXPECT_THAT(file_helper()->GetFileSystems(), IsEmpty());
  base::ScopedTempDir td;
  ASSERT_TRUE(td.CreateUniqueTempDir());
  base::FilePath path = td.GetPath();
  base::Uuid uuid = base::Uuid::GenerateRandomV4();
  DevToolsFileHelper::FileSystem file_system{
      "automatic", "test", "filesystem:test", path.AsUTF8Unsafe()};
  base::MockCallback<DevToolsFileHelper::HandlePermissionsCallback>
      handle_permissions_callback;
  EXPECT_CALL(handle_permissions_callback, Run)
      .WillOnce(
          InvokeCallbackArgument<2, base::OnceCallback<void(bool)>>(true));
  base::MockCallback<DevToolsFileHelper::ConnectCallback> connect_cb;
  EXPECT_CALL(connect_cb, Run(true)).Times(1);
  EXPECT_CALL(storage(), RegisterFileSystem(path, "automatic"))
      .WillOnce(Return(file_system));
  EXPECT_CALL(delegate(), FileSystemAdded(IsEmpty(), Pointee(file_system)))
      .Times(1);

  base::RunLoop run_loop;
  ON_CALL(delegate(), FileSystemAdded).WillByDefault([&] { run_loop.Quit(); });
  file_helper()->ConnectAutomaticFileSystem(path.AsUTF8Unsafe(), uuid,
                                            /* add_if_missing */ true,
                                            handle_permissions_callback.Get(),
                                            connect_cb.Get());
  run_loop.Run();

  const base::Value::Dict& file_system_paths_value =
      profile()->GetPrefs()->GetDict(prefs::kDevToolsFileSystemPaths);
  EXPECT_THAT(file_system_paths_value, SizeIs(1));
  EXPECT_THAT(file_system_paths_value.FindString(path.AsUTF8Unsafe()),
              Pointee(uuid.AsLowercaseString()));
}

TEST_F(DevToolsFileHelperTest, ConnectAndDisconnectKnownAutomaticFileSystem) {
  base::ScopedTempDir td;
  ASSERT_TRUE(td.CreateUniqueTempDir());
  base::FilePath path = td.GetPath();
  base::Uuid uuid = base::Uuid::GenerateRandomV4();
  {
    ScopedDictPrefUpdate update(profile()->GetPrefs(),
                                prefs::kDevToolsFileSystemPaths);
    update.Get().Set(path.AsUTF8Unsafe(), uuid.AsLowercaseString());
  }
  EXPECT_THAT(file_helper()->GetFileSystems(), IsEmpty());
  DevToolsFileHelper::FileSystem file_system{
      "automatic", "test", "filesystem:test", path.AsUTF8Unsafe()};
  base::MockCallback<DevToolsFileHelper::HandlePermissionsCallback>
      handle_permissions_callback;
  EXPECT_CALL(handle_permissions_callback, Run).Times(0);
  base::MockCallback<DevToolsFileHelper::ConnectCallback> connect_cb;
  EXPECT_CALL(connect_cb, Run(true)).Times(1);
  EXPECT_CALL(storage(), RegisterFileSystem(path, "automatic"))
      .WillOnce(Return(file_system));
  EXPECT_CALL(delegate(), FileSystemAdded(IsEmpty(), Pointee(file_system)))
      .Times(1);

  {
    // Connect the known automatic file system.
    base::RunLoop run_loop;
    ON_CALL(delegate(), FileSystemAdded).WillByDefault([&] {
      run_loop.Quit();
    });
    file_helper()->ConnectAutomaticFileSystem(path.AsUTF8Unsafe(), uuid,
                                              /* add_if_missing */ false,
                                              handle_permissions_callback.Get(),
                                              connect_cb.Get());
    run_loop.Run();

    EXPECT_TRUE(file_helper()->IsFileSystemAdded(path.AsUTF8Unsafe()));
  }

  EXPECT_CALL(storage(), UnregisterFileSystem(path)).Times(1);
  EXPECT_CALL(delegate(), FileSystemRemoved(path.AsUTF8Unsafe())).Times(1);

  {
    // Disconnect the previously connected automatic file system.
    base::RunLoop run_loop;
    ON_CALL(delegate(), FileSystemRemoved).WillByDefault([&] {
      run_loop.Quit();
    });
    file_helper()->DisconnectAutomaticFileSystem(path.AsUTF8Unsafe());
    run_loop.Run();

    EXPECT_FALSE(file_helper()->IsFileSystemAdded(path.AsUTF8Unsafe()));
  }
}

TEST_F(DevToolsFileHelperTest, DisconnectAutomaticFileSystemNotConnected) {
  base::ScopedTempDir td;
  ASSERT_TRUE(td.CreateUniqueTempDir());

  file_helper()->DisconnectAutomaticFileSystem(td.GetPath().AsUTF8Unsafe());
}

TEST_F(DevToolsFileHelperTest, RemoveAutomaticFileSystemNotConnected) {
  base::ScopedTempDir td;
  ASSERT_TRUE(td.CreateUniqueTempDir());
  base::FilePath path = td.GetPath();
  base::Uuid uuid = base::Uuid::GenerateRandomV4();
  {
    ScopedDictPrefUpdate update(profile()->GetPrefs(),
                                prefs::kDevToolsFileSystemPaths);
    update.Get().Set(path.AsUTF8Unsafe(), uuid.AsLowercaseString());
  }

  file_helper()->RemoveFileSystem(path.AsUTF8Unsafe());

  EXPECT_THAT(profile()->GetPrefs()->GetDict(prefs::kDevToolsFileSystemPaths),
              IsEmpty());
}