Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cros-disks/fuse_mount_manager.h" |
| 6 | |
Anand K Mistry | 68418e6 | 2018-08-22 11:37:04 +1000 | [diff] [blame] | 7 | #include <sys/mount.h> |
| 8 | |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <base/strings/string_util.h> |
Simon Glass | 2b1da09 | 2020-05-21 12:24:16 -0600 | [diff] [blame] | 14 | #include <brillo/process/process_reaper.h> |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 15 | #include <gmock/gmock.h> |
| 16 | #include <gtest/gtest.h> |
| 17 | |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 18 | #include "cros-disks/fuse_mounter.h" |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 19 | #include "cros-disks/metrics.h" |
| 20 | #include "cros-disks/mount_options.h" |
Anand K Mistry | 25a5f85 | 2020-01-14 17:08:39 +1100 | [diff] [blame] | 21 | #include "cros-disks/mount_point.h" |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 22 | #include "cros-disks/platform.h" |
Sergei Datsenko | 88035aa | 2020-11-15 00:24:01 +1100 | [diff] [blame] | 23 | #include "cros-disks/sandboxed_process.h" |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 24 | #include "cros-disks/uri.h" |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 25 | |
Anand K Mistry | 9f4611e | 2019-12-19 16:06:39 +1100 | [diff] [blame] | 26 | using testing::_; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 27 | using testing::ByMove; |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 28 | using testing::DoAll; |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 29 | using testing::Return; |
| 30 | using testing::SetArgPointee; |
Anand K Mistry | 9f4611e | 2019-12-19 16:06:39 +1100 | [diff] [blame] | 31 | using testing::WithArg; |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 32 | |
| 33 | namespace cros_disks { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | const char kMountRoot[] = "/mntroot"; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 38 | const char kWorkingDirRoot[] = "/wkdir"; |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 39 | const char kNoType[] = ""; |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 40 | const char kSomeMountpoint[] = "/mnt"; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 41 | const Uri kSomeSource("fuse", "something"); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 42 | |
| 43 | // Mock Platform implementation for testing. |
| 44 | class MockPlatform : public Platform { |
| 45 | public: |
| 46 | MockPlatform() = default; |
| 47 | |
Ben Chan | ef8e603 | 2019-09-27 08:24:56 -0700 | [diff] [blame] | 48 | MOCK_METHOD(MountErrorType, |
Sergei Datsenko | 199f4f4 | 2020-10-08 10:47:12 +1100 | [diff] [blame] | 49 | Mount, |
| 50 | (const std::string& source, |
| 51 | const std::string& target, |
| 52 | const std::string& filesystem_type, |
| 53 | uint64_t flags, |
| 54 | const std::string& options), |
| 55 | (const, override)); |
| 56 | MOCK_METHOD(MountErrorType, |
Ben Chan | ef8e603 | 2019-09-27 08:24:56 -0700 | [diff] [blame] | 57 | Unmount, |
| 58 | (const std::string&, int), |
| 59 | (const, override)); |
| 60 | MOCK_METHOD(bool, DirectoryExists, (const std::string&), (const, override)); |
| 61 | MOCK_METHOD(bool, CreateDirectory, (const std::string&), (const, override)); |
| 62 | MOCK_METHOD(bool, |
| 63 | SetPermissions, |
| 64 | (const std::string&, mode_t), |
| 65 | (const, override)); |
| 66 | MOCK_METHOD(bool, |
| 67 | CreateTemporaryDirInDir, |
| 68 | (const std::string&, const std::string&, std::string*), |
| 69 | (const, override)); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 70 | }; |
| 71 | |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 72 | // Mock implementation of a Mounter. |
| 73 | class MockMounter : public Mounter { |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 74 | public: |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 75 | MOCK_METHOD(std::unique_ptr<MountPoint>, |
| 76 | Mount, |
| 77 | (const std::string& source, |
| 78 | const base::FilePath& target_path, |
| 79 | std::vector<std::string> params, |
| 80 | MountErrorType* error), |
| 81 | (const, override)); |
| 82 | MOCK_METHOD(bool, |
| 83 | CanMount, |
| 84 | (const std::string& source, |
| 85 | const std::vector<std::string>& params, |
| 86 | base::FilePath* suggested_dir_name), |
Ben Chan | ef8e603 | 2019-09-27 08:24:56 -0700 | [diff] [blame] | 87 | (const, override)); |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 88 | }; |
| 89 | |
Sergei Datsenko | 88035aa | 2020-11-15 00:24:01 +1100 | [diff] [blame] | 90 | class MockSandboxedProcess : public SandboxedProcess { |
| 91 | public: |
| 92 | MockSandboxedProcess() = default; |
| 93 | pid_t StartImpl(base::ScopedFD, base::ScopedFD, base::ScopedFD) override { |
| 94 | return 123; |
| 95 | } |
| 96 | MOCK_METHOD(int, WaitImpl, (), (override)); |
| 97 | MOCK_METHOD(int, WaitNonBlockingImpl, (), (override)); |
| 98 | }; |
| 99 | |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 100 | } // namespace |
| 101 | |
| 102 | class FUSEMountManagerTest : public ::testing::Test { |
| 103 | public: |
| 104 | FUSEMountManagerTest() |
Sergei Datsenko | a910bba | 2019-06-18 13:31:59 +1000 | [diff] [blame] | 105 | : manager_(kMountRoot, |
| 106 | kWorkingDirRoot, |
| 107 | &platform_, |
| 108 | &metrics_, |
| 109 | &process_reaper_), |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 110 | foo_(new MockMounter()), |
| 111 | bar_(new MockMounter()), |
| 112 | baz_(new MockMounter()) { |
Sergei Datsenko | 85a1833 | 2019-04-08 14:25:03 +1000 | [diff] [blame] | 113 | ON_CALL(platform_, Unmount(_, _)) |
Sergei Datsenko | b362e4a | 2019-04-03 17:23:24 +1100 | [diff] [blame] | 114 | .WillByDefault(Return(MOUNT_ERROR_INVALID_ARGUMENT)); |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 115 | ON_CALL(platform_, DirectoryExists(_)).WillByDefault(Return(true)); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | protected: |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 119 | void RegisterHelper(std::unique_ptr<Mounter> helper) { |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 120 | manager_.RegisterHelper(std::move(helper)); |
| 121 | } |
| 122 | |
Anand K Mistry | 25a5f85 | 2020-01-14 17:08:39 +1100 | [diff] [blame] | 123 | std::unique_ptr<MountPoint> DoMount(const std::string& type, |
| 124 | const std::string& src, |
| 125 | MountErrorType* error) { |
Sergei Datsenko | 3928f78 | 2020-12-31 09:14:04 +1100 | [diff] [blame] | 126 | std::unique_ptr<MountPoint> mount_point = |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 127 | manager_.DoMount(src, type, {}, base::FilePath(kSomeMountpoint), error); |
Anand K Mistry | 25a5f85 | 2020-01-14 17:08:39 +1100 | [diff] [blame] | 128 | if (*error == MOUNT_ERROR_NONE) { |
| 129 | EXPECT_TRUE(mount_point); |
| 130 | } else { |
| 131 | EXPECT_FALSE(mount_point); |
| 132 | } |
| 133 | return mount_point; |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | Metrics metrics_; |
| 137 | MockPlatform platform_; |
Sergei Datsenko | a910bba | 2019-06-18 13:31:59 +1000 | [diff] [blame] | 138 | brillo::ProcessReaper process_reaper_; |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 139 | FUSEMountManager manager_; |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 140 | std::unique_ptr<MockMounter> foo_; |
| 141 | std::unique_ptr<MockMounter> bar_; |
| 142 | std::unique_ptr<MockMounter> baz_; |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | // Verifies that CanMount returns false when there are no handlers registered. |
| 146 | TEST_F(FUSEMountManagerTest, CanMount_NoHandlers) { |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 147 | EXPECT_FALSE(manager_.CanMount(kSomeSource.value())); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Verifies that CanMount returns false when known helpers can't handle that. |
| 151 | TEST_F(FUSEMountManagerTest, CanMount_NotHandled) { |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 152 | EXPECT_CALL(*foo_, CanMount).WillOnce(Return(false)); |
| 153 | EXPECT_CALL(*bar_, CanMount).WillOnce(Return(false)); |
| 154 | EXPECT_CALL(*baz_, CanMount).WillOnce(Return(false)); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 155 | RegisterHelper(std::move(foo_)); |
| 156 | RegisterHelper(std::move(bar_)); |
| 157 | RegisterHelper(std::move(baz_)); |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 158 | EXPECT_FALSE(manager_.CanMount(kSomeSource.value())); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Verify that CanMount returns true when there is a helper that can handle |
| 162 | // this source. |
| 163 | TEST_F(FUSEMountManagerTest, CanMount) { |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 164 | EXPECT_CALL(*foo_, CanMount).WillOnce(Return(false)); |
| 165 | EXPECT_CALL(*bar_, CanMount).WillOnce(Return(true)); |
| 166 | EXPECT_CALL(*baz_, CanMount).Times(0); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 167 | RegisterHelper(std::move(foo_)); |
| 168 | RegisterHelper(std::move(bar_)); |
| 169 | RegisterHelper(std::move(baz_)); |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 170 | EXPECT_TRUE(manager_.CanMount(kSomeSource.value())); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Verify that SuggestMountPath dispatches query for name to the correct helper. |
| 174 | TEST_F(FUSEMountManagerTest, SuggestMountPath) { |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 175 | EXPECT_CALL(*foo_, CanMount).WillOnce(Return(false)); |
| 176 | EXPECT_CALL(*bar_, CanMount) |
| 177 | .WillOnce( |
| 178 | DoAll(SetArgPointee<2>(base::FilePath("suffix")), Return(true))); |
| 179 | EXPECT_CALL(*baz_, CanMount).Times(0); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 180 | RegisterHelper(std::move(foo_)); |
| 181 | RegisterHelper(std::move(bar_)); |
| 182 | RegisterHelper(std::move(baz_)); |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 183 | EXPECT_EQ("/mntroot/suffix", manager_.SuggestMountPath(kSomeSource.value())); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 184 | } |
| 185 | |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 186 | // Verify that DoMount fails when there are no helpers. |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 187 | TEST_F(FUSEMountManagerTest, DoMount_NoHandlers) { |
Anand K Mistry | 25a5f85 | 2020-01-14 17:08:39 +1100 | [diff] [blame] | 188 | MountErrorType mount_error; |
| 189 | std::unique_ptr<MountPoint> mount_point = |
| 190 | DoMount(kNoType, kSomeSource.value(), &mount_error); |
| 191 | EXPECT_EQ(MOUNT_ERROR_UNKNOWN_FILESYSTEM, mount_error); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // Verify that DoMount fails when helpers don't handle this source. |
| 195 | TEST_F(FUSEMountManagerTest, DoMount_NotHandled) { |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 196 | EXPECT_CALL(*foo_, CanMount).WillOnce(Return(false)); |
| 197 | EXPECT_CALL(*bar_, CanMount).WillOnce(Return(false)); |
| 198 | EXPECT_CALL(*baz_, CanMount).WillOnce(Return(false)); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 199 | RegisterHelper(std::move(foo_)); |
| 200 | RegisterHelper(std::move(bar_)); |
| 201 | RegisterHelper(std::move(baz_)); |
Anand K Mistry | 25a5f85 | 2020-01-14 17:08:39 +1100 | [diff] [blame] | 202 | MountErrorType mount_error; |
| 203 | std::unique_ptr<MountPoint> mount_point = |
| 204 | DoMount(kNoType, kSomeSource.value(), &mount_error); |
| 205 | EXPECT_EQ(MOUNT_ERROR_UNKNOWN_FILESYSTEM, mount_error); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Verify that DoMount delegates mounting to the correct helpers when |
| 209 | // dispatching by source description. |
| 210 | TEST_F(FUSEMountManagerTest, DoMount_BySource) { |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 211 | EXPECT_CALL(*foo_, CanMount).WillOnce(Return(false)); |
| 212 | EXPECT_CALL(*bar_, CanMount) |
Sergei Datsenko | 88035aa | 2020-11-15 00:24:01 +1100 | [diff] [blame] | 213 | .WillOnce( |
Sergei Datsenko | f3ebcc3 | 2020-12-07 23:47:56 +1100 | [diff] [blame] | 214 | DoAll(SetArgPointee<2>(base::FilePath("suffix")), Return(true))); |
| 215 | EXPECT_CALL(*baz_, CanMount).Times(0); |
| 216 | |
| 217 | EXPECT_CALL(*foo_, Mount).Times(0); |
| 218 | EXPECT_CALL(*baz_, Mount).Times(0); |
| 219 | |
| 220 | EXPECT_CALL(*bar_, Mount(kSomeSource.value(), _, _, _)) |
| 221 | .WillOnce(DoAll(SetArgPointee<3>(MOUNT_ERROR_NONE), |
| 222 | Return(ByMove(MountPoint::CreateLeaking( |
| 223 | base::FilePath(kSomeMountpoint)))))); |
| 224 | |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 225 | RegisterHelper(std::move(foo_)); |
| 226 | RegisterHelper(std::move(bar_)); |
| 227 | RegisterHelper(std::move(baz_)); |
Anand K Mistry | 25a5f85 | 2020-01-14 17:08:39 +1100 | [diff] [blame] | 228 | MountErrorType mount_error; |
| 229 | std::unique_ptr<MountPoint> mount_point = |
| 230 | DoMount(kNoType, kSomeSource.value(), &mount_error); |
| 231 | EXPECT_EQ(MOUNT_ERROR_NONE, mount_error); |
| 232 | EXPECT_EQ(base::FilePath(kSomeMountpoint), mount_point->path()); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | } // namespace cros_disks |