File: smb_service_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 (201 lines) | stat: -rw-r--r-- 7,618 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
// Copyright 2023 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/smb_client/smb_service_test_base.h"

#include "base/test/simple_test_tick_clock.h"
#include "chrome/browser/ash/file_manager/volume_manager.h"
#include "chrome/browser/ash/file_manager/volume_manager_factory.h"
#include "chrome/test/base/testing_browser_process.h"

namespace ash::smb_client {

namespace {

void SaveMountResult(SmbMountResult* out, SmbMountResult result) {
  *out = result;
}

// Creates a new VolumeManager for tests.
// By default, VolumeManager KeyedService is null for testing.
std::unique_ptr<KeyedService> BuildVolumeManager(
    content::BrowserContext* context) {
  return std::make_unique<file_manager::VolumeManager>(
      Profile::FromBrowserContext(context),
      nullptr /* drive_integration_service */,
      nullptr /* power_manager_client */,
      disks::DiskMountManager::GetInstance(),
      nullptr /* file_system_provider_service */,
      file_manager::VolumeManager::GetMtpStorageInfoCallback());
}

}  // namespace

// MockSmbFsMounter
MockSmbFsMounter::MockSmbFsMounter() = default;
MockSmbFsMounter::~MockSmbFsMounter() = default;

// MockSmbFsImpl
MockSmbFsImpl::MockSmbFsImpl(mojo::PendingReceiver<smbfs::mojom::SmbFs> pending)
    : receiver_(this, std::move(pending)) {
  receiver_.set_disconnect_handler(
      base::BindOnce(&MockSmbFsImpl::OnDisconnect, base::Unretained(this)));
}

MockSmbFsImpl::~MockSmbFsImpl() = default;

// SmbServiceBaseTest
SmbServiceBaseTest::TestSmbFsInstance::TestSmbFsInstance(
    mojo::PendingReceiver<smbfs::mojom::SmbFs> pending)
    : mock_smbfs(std::move(pending)) {}

SmbServiceBaseTest::TestSmbFsInstance::~TestSmbFsInstance() = default;

SmbServiceBaseTest::SmbServiceBaseTest() {
  profile_manager_ = std::make_unique<TestingProfileManager>(
      TestingBrowserProcess::GetGlobal());
  CHECK(profile_manager_->SetUp());

  {
    auto user_manager_temp = std::make_unique<FakeChromeUserManager>();

    profile_ = profile_manager_->CreateTestingProfile("test-user@example.com");
    user_manager_temp->AddUser(
        AccountId::FromUserEmail(profile_->GetProfileUserName()));

    // Run pending async tasks resulting from profile construction to ensure
    // these are complete before the test begins.
    base::RunLoop().RunUntilIdle();

    user_manager_enabler_ = std::make_unique<user_manager::ScopedUserManager>(
        std::move(user_manager_temp));
  }

  SmbProviderClient::InitializeFake();
  ConciergeClient::InitializeFake(/*fake_cicerone_client=*/nullptr);

  disk_mount_manager_ = new disks::FakeDiskMountManager();
  // Takes ownership of |disk_mount_manager_|, but Shutdown() must be called.
  disks::DiskMountManager::InitializeForTesting(disk_mount_manager_);
}

SmbServiceBaseTest::~SmbServiceBaseTest() {
  // The following reset() and Shutdown() are required to override and delete
  // the global dependencies.
  smb_service.reset();
  user_manager_enabler_.reset();
  profile_manager_.reset();
  disks::DiskMountManager::Shutdown();
  SmbProviderClient::Shutdown();
}

void SmbServiceBaseTest::CreateService(TestingProfile* profile) {
  SmbService::DisableShareDiscoveryForTesting();
  file_manager::VolumeManagerFactory::GetInstance()->SetTestingFactory(
      profile, base::BindRepeating(&BuildVolumeManager));

  smb_service = std::make_unique<SmbService>(
      profile, std::make_unique<base::SimpleTestTickClock>());
}

void SmbServiceBaseTest::ExpectInvalidUrl(const std::string& url) {
  SmbMountResult result = SmbMountResult::kSuccess;
  smb_service->Mount(
      "" /* display_name */, base::FilePath(url), "" /* username */,
      "" /* password */, false /* use_kerberos */,
      false /* should_open_file_manager_after_mount */,
      false /* save_credentials */, base::BindOnce(&SaveMountResult, &result));
  EXPECT_EQ(result, SmbMountResult::kInvalidUrl);
}

void SmbServiceBaseTest::ExpectInvalidSsoUrl(const std::string& url) {
  SmbMountResult result = SmbMountResult::kSuccess;
  smb_service->Mount(
      "" /* display_name */, base::FilePath(url), "" /* username */,
      "" /* password */, true /* use_kerberos */,
      false /* should_open_file_manager_after_mount */,
      false /* save_credentials */, base::BindOnce(&SaveMountResult, &result));
  EXPECT_EQ(result, SmbMountResult::kInvalidSsoUrl);
}

void SmbServiceBaseTest::WaitForSetupComplete() {
  {
    base::RunLoop run_loop;
    smb_service->OnSetupCompleteForTesting(run_loop.QuitClosure());
    run_loop.Run();
  }
  {
    // Share gathering needs to complete at least once before a share can be
    // mounted.
    base::RunLoop run_loop;
    smb_service->GatherSharesInNetwork(
        base::DoNothing(),
        base::BindLambdaForTesting(
            [&run_loop](const std::vector<SmbUrl>& shares_gathered, bool done) {
              if (done) {
                run_loop.Quit();
              }
            }));
    run_loop.Run();
  }
}

std::unique_ptr<disks::MountPoint> SmbServiceBaseTest::MakeMountPoint(
    const base::FilePath& path) {
  return std::make_unique<disks::MountPoint>(path, disk_mount_manager_);
}

std::unique_ptr<SmbServiceBaseTest::TestSmbFsInstance>
SmbServiceBaseTest::MountBasicShare(const std::string& share_path,
                                    const std::string& mount_path,
                                    SmbService::MountResponse callback) {
  mojo::Remote<smbfs::mojom::SmbFs> smbfs_remote;
  auto instance = std::make_unique<TestSmbFsInstance>(
      smbfs_remote.BindNewPipeAndPassReceiver());

  smbfs::SmbFsHost::Delegate* smbfs_host_delegate = nullptr;
  // Use a NiceMock<> so that the ON_CALL below doesn't complain.
  auto mock_mounter = std::make_unique<NiceMock<MockSmbFsMounter>>();

  smb_service->SetSmbFsMounterCreationCallbackForTesting(
      base::BindLambdaForTesting([&mock_mounter, &smbfs_host_delegate](
                                     const std::string& share_path,
                                     const std::string& mount_dir_name,
                                     const SmbFsShare::MountOptions& options,
                                     smbfs::SmbFsHost::Delegate* delegate)
                                     -> std::unique_ptr<smbfs::SmbFsMounter> {
        smbfs_host_delegate = delegate;
        return std::move(mock_mounter);
      }));

  // Use ON_CALL instead of EXPECT_CALL because there might be a failure
  // earlier in the mount process and this won't be called.
  ON_CALL(*mock_mounter, Mount(_))
      .WillByDefault(
          [this, &smbfs_host_delegate, &smbfs_remote, &instance,
           &mount_path](smbfs::SmbFsMounter::DoneCallback mount_callback) {
            std::move(mount_callback)
                .Run(smbfs::mojom::MountError::kOk,
                     std::make_unique<smbfs::SmbFsHost>(
                         MakeMountPoint(base::FilePath(mount_path)),
                         smbfs_host_delegate, std::move(smbfs_remote),
                         instance->delegate.BindNewPipeAndPassReceiver()));
          });

  base::RunLoop run_loop;
  smb_service->Mount(
      kDisplayName, base::FilePath(share_path), "" /* username */,
      "" /* password */, false /* use_kerberos */,
      false /* should_open_file_manager_after_mount */,
      false /* save_credentials */,
      base::BindLambdaForTesting([&run_loop, &callback](SmbMountResult result) {
        std::move(callback).Run(result);
        run_loop.Quit();
      }));
  run_loop.Run();

  return instance;
}

}  // namespace ash::smb_client