Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Chan | d30e743 | 2011-11-28 13:43:17 -0800 | [diff] [blame] | 5 | // Unit tests for cros_disks::MountManager. See mount-manager.h for details |
| 6 | // on MountManager. |
| 7 | |
Ben Chan | 5ccd9fe | 2013-11-13 18:28:27 -0800 | [diff] [blame] | 8 | #include "cros-disks/mount_manager.h" |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 9 | |
| 10 | #include <sys/mount.h> |
| 11 | #include <sys/unistd.h> |
| 12 | |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 13 | #include <algorithm> |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 14 | #include <string> |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 15 | #include <unordered_set> |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 16 | #include <utility> |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 19 | #include <base/check.h> |
Simon Glass | 2b1da09 | 2020-05-21 12:24:16 -0600 | [diff] [blame] | 20 | #include <brillo/process/process_reaper.h> |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 21 | #include <gmock/gmock.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | |
Ben Chan | be2b4a7 | 2011-11-08 13:42:23 -0800 | [diff] [blame] | 24 | #include "cros-disks/metrics.h" |
Ben Chan | 8fb742b | 2014-04-28 23:46:57 -0700 | [diff] [blame] | 25 | #include "cros-disks/mount_entry.h" |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 26 | #include "cros-disks/mount_options.h" |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 27 | #include "cros-disks/mount_point.h" |
| 28 | #include "cros-disks/mounter.h" |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 29 | #include "cros-disks/platform.h" |
| 30 | |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 31 | using testing::_; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 32 | using testing::ByMove; |
| 33 | using testing::DoAll; |
Ben Chan | 09c90d0 | 2012-04-25 22:09:09 -0700 | [diff] [blame] | 34 | using testing::ElementsAre; |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 35 | using testing::Invoke; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 36 | using testing::Return; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 37 | using testing::SetArgPointee; |
Anand K Mistry | d0a0523 | 2020-01-24 14:04:18 +1100 | [diff] [blame] | 38 | using testing::WithArgs; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 39 | |
Anand K Mistry | 40cff45 | 2019-07-30 10:24:48 +1000 | [diff] [blame] | 40 | namespace cros_disks { |
Ben Chan | 460439f | 2011-09-13 09:16:28 -0700 | [diff] [blame] | 41 | namespace { |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 42 | |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 43 | const char kMountRootDirectory[] = "/media/removable"; |
| 44 | const char kTestSourcePath[] = "source"; |
| 45 | const char kTestMountPath[] = "/media/removable/test"; |
| 46 | const char kInvalidMountPath[] = "/media/removable/../test/doc"; |
Tatsuhisa Yamaguchi | b670bd1 | 2016-09-28 23:06:44 +0900 | [diff] [blame] | 47 | const char kMountOptionRemount[] = "remount"; |
| 48 | const char kMountOptionReadOnly[] = "ro"; |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 49 | const char kMountOptionReadWrite[] = "rw"; |
Ben Chan | 460439f | 2011-09-13 09:16:28 -0700 | [diff] [blame] | 50 | |
| 51 | } // namespace |
| 52 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 53 | // A mock platform class for testing the mount manager base class. |
| 54 | class MockPlatform : public Platform { |
| 55 | public: |
Ben Chan | 40ecb9b | 2017-03-08 11:26:07 -0800 | [diff] [blame] | 56 | MockPlatform() = default; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 57 | |
Ben Chan | 21150af | 2019-09-11 17:04:07 -0700 | [diff] [blame] | 58 | MOCK_METHOD(bool, CreateDirectory, (const std::string&), (const, override)); |
| 59 | MOCK_METHOD(bool, |
| 60 | CreateOrReuseEmptyDirectory, |
| 61 | (const std::string&), |
| 62 | (const, override)); |
| 63 | MOCK_METHOD(bool, |
| 64 | CreateOrReuseEmptyDirectoryWithFallback, |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 65 | (std::string*, unsigned, const std::unordered_set<std::string>&), |
Ben Chan | 21150af | 2019-09-11 17:04:07 -0700 | [diff] [blame] | 66 | (const, override)); |
| 67 | MOCK_METHOD(bool, |
| 68 | RemoveEmptyDirectory, |
| 69 | (const std::string&), |
| 70 | (const, override)); |
| 71 | MOCK_METHOD(bool, |
| 72 | SetOwnership, |
| 73 | (const std::string&, uid_t, gid_t), |
| 74 | (const, override)); |
| 75 | MOCK_METHOD(bool, |
| 76 | SetPermissions, |
| 77 | (const std::string&, mode_t), |
| 78 | (const, override)); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 79 | MOCK_METHOD(MountErrorType, |
| 80 | Unmount, |
| 81 | (const std::string&, int), |
| 82 | (const, override)); |
| 83 | MOCK_METHOD(MountErrorType, |
| 84 | Mount, |
| 85 | (const std::string& source, |
| 86 | const std::string& target, |
| 87 | const std::string& filesystem_type, |
| 88 | uint64_t flags, |
| 89 | const std::string& options), |
| 90 | (const, override)); |
Anand K Mistry | d0a0523 | 2020-01-24 14:04:18 +1100 | [diff] [blame] | 91 | }; |
| 92 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 93 | // A mock mount manager class for testing the mount manager base class. |
| 94 | class MountManagerUnderTest : public MountManager { |
| 95 | public: |
Sergei Datsenko | a910bba | 2019-06-18 13:31:59 +1000 | [diff] [blame] | 96 | MountManagerUnderTest(Platform* platform, |
| 97 | Metrics* metrics, |
| 98 | brillo::ProcessReaper* process_reaper) |
| 99 | : MountManager(kMountRootDirectory, platform, metrics, process_reaper) {} |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 100 | |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 101 | ~MountManagerUnderTest() override { UnmountAll(); } |
| 102 | |
Ben Chan | 21150af | 2019-09-11 17:04:07 -0700 | [diff] [blame] | 103 | MOCK_METHOD(bool, CanMount, (const std::string&), (const, override)); |
| 104 | MOCK_METHOD(MountSourceType, GetMountSourceType, (), (const, override)); |
Anand K Mistry | d0a0523 | 2020-01-24 14:04:18 +1100 | [diff] [blame] | 105 | MOCK_METHOD(std::unique_ptr<MountPoint>, |
Ben Chan | 21150af | 2019-09-11 17:04:07 -0700 | [diff] [blame] | 106 | DoMount, |
| 107 | (const std::string&, |
| 108 | const std::string&, |
| 109 | const std::vector<std::string>&, |
Anand K Mistry | d0a0523 | 2020-01-24 14:04:18 +1100 | [diff] [blame] | 110 | const base::FilePath&, |
Anand K Mistry | d0a0523 | 2020-01-24 14:04:18 +1100 | [diff] [blame] | 111 | MountErrorType*), |
Ben Chan | 21150af | 2019-09-11 17:04:07 -0700 | [diff] [blame] | 112 | (override)); |
Ben Chan | 21150af | 2019-09-11 17:04:07 -0700 | [diff] [blame] | 113 | MOCK_METHOD(bool, |
| 114 | ShouldReserveMountPathOnError, |
| 115 | (MountErrorType), |
| 116 | (const, override)); |
| 117 | MOCK_METHOD(std::string, |
| 118 | SuggestMountPath, |
| 119 | (const std::string&), |
| 120 | (const, override)); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 121 | |
| 122 | // Adds or updates a mapping |source_path| to its mount state in the cache. |
| 123 | void AddMount(const std::string& source_path, |
| 124 | std::unique_ptr<MountPoint> mount_point) { |
| 125 | DCHECK(mount_point); |
| 126 | DCHECK(!FindMountBySource(source_path)); |
| 127 | mount_states_.insert({source_path, std::move(mount_point)}); |
| 128 | } |
| 129 | |
| 130 | using MountManager::GetMountErrorOfReservedMountPath; |
| 131 | using MountManager::ReserveMountPath; |
| 132 | using MountManager::UnreserveMountPath; |
| 133 | |
| 134 | bool IsMountPathInCache(const std::string& path) { |
| 135 | return FindMountByMountPath(base::FilePath(path)); |
| 136 | } |
| 137 | |
| 138 | bool IsMountPathReserved(const std::string& path) { |
| 139 | return MountManager::IsMountPathReserved(base::FilePath(path)); |
| 140 | } |
| 141 | |
| 142 | void AddMountStateCache(const std::string& source, |
| 143 | std::unique_ptr<MountPoint> mount_point) { |
| 144 | mount_states_.insert({source, std::move(mount_point)}); |
| 145 | } |
| 146 | |
| 147 | bool RemoveMountPathFromCache(const std::string& path) { |
| 148 | MountPoint* mp = FindMountByMountPath(base::FilePath(path)); |
| 149 | if (!mp) |
| 150 | return false; |
| 151 | return RemoveMount(mp); |
| 152 | } |
| 153 | |
| 154 | std::unordered_set<std::string> GetReservedMountPaths() { |
| 155 | std::unordered_set<std::string> result; |
| 156 | for (const auto& element : reserved_mount_paths_) { |
| 157 | result.insert(element.first.value()); |
| 158 | } |
| 159 | return result; |
| 160 | } |
| 161 | |
| 162 | base::Optional<MountEntry> GetMountEntryForTest(const std::string& source) { |
| 163 | MountPoint* mp = FindMountBySource(source); |
| 164 | if (!mp) |
| 165 | return {}; |
| 166 | return MountEntry(MOUNT_ERROR_NONE, source, GetMountSourceType(), |
| 167 | mp->path().value(), mp->is_read_only()); |
| 168 | } |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | class MountManagerTest : public ::testing::Test { |
| 172 | public: |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 173 | MountManagerTest() : manager_(&platform_, &metrics_, &process_reaper_) { |
| 174 | ON_CALL(manager_, GetMountSourceType()) |
| 175 | .WillByDefault(Return(MOUNT_SOURCE_REMOVABLE_DEVICE)); |
| 176 | } |
| 177 | |
| 178 | std::unique_ptr<MountPoint> MakeMountPoint(const std::string& mount_path) { |
Anand K Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 179 | return MountPoint::CreateLeaking(base::FilePath(mount_path)); |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 180 | } |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 181 | |
| 182 | protected: |
Ben Chan | be2b4a7 | 2011-11-08 13:42:23 -0800 | [diff] [blame] | 183 | Metrics metrics_; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 184 | MockPlatform platform_; |
Sergei Datsenko | a910bba | 2019-06-18 13:31:59 +1000 | [diff] [blame] | 185 | brillo::ProcessReaper process_reaper_; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 186 | MountManagerUnderTest manager_; |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 187 | std::string filesystem_type_; |
| 188 | std::string mount_path_; |
| 189 | std::string source_path_; |
| 190 | std::vector<std::string> options_; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 193 | // Verifies that MountManager::Initialize() returns false when it fails to |
| 194 | // create the mount root directory. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 195 | TEST_F(MountManagerTest, InitializeFailedInCreateDirectory) { |
| 196 | EXPECT_CALL(platform_, CreateDirectory(kMountRootDirectory)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 197 | .WillOnce(Return(false)); |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 198 | EXPECT_CALL(platform_, SetOwnership(kMountRootDirectory, getuid(), getgid())) |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 199 | .Times(0); |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 200 | EXPECT_CALL(platform_, SetPermissions(kMountRootDirectory, _)).Times(0); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 201 | |
| 202 | EXPECT_FALSE(manager_.Initialize()); |
| 203 | } |
| 204 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 205 | // Verifies that MountManager::Initialize() returns false when it fails to |
| 206 | // set the ownership of the created mount root directory. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 207 | TEST_F(MountManagerTest, InitializeFailedInSetOwnership) { |
| 208 | EXPECT_CALL(platform_, CreateDirectory(kMountRootDirectory)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 209 | .WillOnce(Return(true)); |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 210 | EXPECT_CALL(platform_, SetOwnership(kMountRootDirectory, getuid(), getgid())) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 211 | .WillOnce(Return(false)); |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 212 | EXPECT_CALL(platform_, SetPermissions(kMountRootDirectory, _)).Times(0); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 213 | |
| 214 | EXPECT_FALSE(manager_.Initialize()); |
| 215 | } |
| 216 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 217 | // Verifies that MountManager::Initialize() returns false when it fails to |
| 218 | // set the permissions of the created mount root directory. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 219 | TEST_F(MountManagerTest, InitializeFailedInSetPermissions) { |
| 220 | EXPECT_CALL(platform_, CreateDirectory(kMountRootDirectory)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 221 | .WillOnce(Return(true)); |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 222 | EXPECT_CALL(platform_, SetOwnership(kMountRootDirectory, getuid(), getgid())) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 223 | .WillOnce(Return(true)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 224 | EXPECT_CALL(platform_, SetPermissions(kMountRootDirectory, _)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 225 | .WillOnce(Return(false)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 226 | |
| 227 | EXPECT_FALSE(manager_.Initialize()); |
| 228 | } |
| 229 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 230 | // Verifies that MountManager::Initialize() returns true when it creates |
| 231 | // the mount root directory with the specified ownership and permissions. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 232 | TEST_F(MountManagerTest, InitializeSucceeded) { |
| 233 | EXPECT_CALL(platform_, CreateDirectory(kMountRootDirectory)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 234 | .WillOnce(Return(true)); |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 235 | EXPECT_CALL(platform_, SetOwnership(kMountRootDirectory, getuid(), getgid())) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 236 | .WillOnce(Return(true)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 237 | EXPECT_CALL(platform_, SetPermissions(kMountRootDirectory, _)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 238 | .WillOnce(Return(true)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 239 | |
| 240 | EXPECT_TRUE(manager_.Initialize()); |
| 241 | } |
| 242 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 243 | // Verifies that MountManager::Mount() returns an error when it is invoked |
| 244 | // to mount an empty source path. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 245 | TEST_F(MountManagerTest, MountFailedWithEmptySourcePath) { |
| 246 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 247 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 248 | .Times(0); |
| 249 | EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 250 | EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 251 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 252 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 253 | EXPECT_EQ( |
| 254 | MOUNT_ERROR_INVALID_ARGUMENT, |
| 255 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 258 | // Verifies that MountManager::Mount() returns an error when it is invoked |
Ben Chan | 44e7ea6 | 2014-08-29 18:13:37 -0700 | [diff] [blame] | 259 | // with a nullptr mount path. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 260 | TEST_F(MountManagerTest, MountFailedWithNullMountPath) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 261 | source_path_ = kTestSourcePath; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 262 | |
| 263 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 264 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 265 | .Times(0); |
| 266 | EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 267 | EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 268 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 269 | |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 270 | EXPECT_EQ(MOUNT_ERROR_INVALID_ARGUMENT, |
Ben Chan | 44e7ea6 | 2014-08-29 18:13:37 -0700 | [diff] [blame] | 271 | manager_.Mount(source_path_, filesystem_type_, options_, nullptr)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 274 | // Verifies that MountManager::Mount() returns an error when it is invoked |
| 275 | // with an invalid mount path. |
| 276 | TEST_F(MountManagerTest, MountFailedWithInvalidMountPath) { |
| 277 | source_path_ = kTestSourcePath; |
| 278 | mount_path_ = kInvalidMountPath; |
| 279 | |
| 280 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
| 281 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 282 | .Times(0); |
| 283 | EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 284 | EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 285 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 286 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 287 | EXPECT_EQ( |
| 288 | MOUNT_ERROR_INVALID_PATH, |
| 289 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // Verifies that MountManager::Mount() returns an error when it is invoked |
| 293 | // without a given mount path and the suggested mount path is invalid. |
| 294 | TEST_F(MountManagerTest, MountFailedWithInvalidSuggestedMountPath) { |
| 295 | source_path_ = kTestSourcePath; |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 296 | std::string suggested_mount_path = kInvalidMountPath; |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 297 | |
| 298 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
| 299 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 300 | .Times(0); |
| 301 | EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 302 | EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 303 | EXPECT_CALL(manager_, SuggestMountPath(_)) |
| 304 | .WillRepeatedly(Return(suggested_mount_path)); |
| 305 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 306 | EXPECT_EQ( |
| 307 | MOUNT_ERROR_INVALID_PATH, |
| 308 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 309 | |
| 310 | options_.push_back("mountlabel=custom_label"); |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 311 | EXPECT_EQ( |
| 312 | MOUNT_ERROR_INVALID_PATH, |
| 313 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Verifies that MountManager::Mount() returns an error when it is invoked |
| 317 | // with an mount label that yields an invalid mount path. |
| 318 | TEST_F(MountManagerTest, MountFailedWithInvalidMountLabel) { |
| 319 | source_path_ = kTestSourcePath; |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 320 | std::string suggested_mount_path = kTestSourcePath; |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 321 | options_.push_back("mountlabel=../custom_label"); |
| 322 | |
| 323 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
| 324 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 325 | .Times(0); |
| 326 | EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 327 | EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 328 | EXPECT_CALL(manager_, SuggestMountPath(_)) |
| 329 | .WillOnce(Return(suggested_mount_path)); |
| 330 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 331 | EXPECT_EQ( |
| 332 | MOUNT_ERROR_INVALID_PATH, |
| 333 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 336 | // Verifies that MountManager::Mount() returns an error when it fails to |
| 337 | // create the specified mount directory. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 338 | TEST_F(MountManagerTest, MountFailedInCreateOrReuseEmptyDirectory) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 339 | source_path_ = kTestSourcePath; |
| 340 | mount_path_ = kTestMountPath; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 341 | |
| 342 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 343 | .WillOnce(Return(false)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 344 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 345 | .Times(0); |
| 346 | EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 347 | EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 348 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 349 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 350 | EXPECT_EQ( |
| 351 | MOUNT_ERROR_DIRECTORY_CREATION_FAILED, |
| 352 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 353 | EXPECT_EQ(kTestMountPath, mount_path_); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 354 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 355 | } |
| 356 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 357 | // Verifies that MountManager::Mount() returns an error when it fails to |
| 358 | // create a specified but already reserved mount directory. |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 359 | TEST_F(MountManagerTest, MountFailedInCreateDirectoryDueToReservedMountPath) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 360 | source_path_ = kTestSourcePath; |
| 361 | mount_path_ = kTestMountPath; |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 362 | base::FilePath mount_path(mount_path_); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 363 | |
| 364 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)).Times(0); |
| 365 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 366 | .Times(0); |
| 367 | EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 368 | EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 369 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 370 | |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 371 | manager_.ReserveMountPath(mount_path, MOUNT_ERROR_UNKNOWN_FILESYSTEM); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 372 | EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_)); |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 373 | EXPECT_EQ(MOUNT_ERROR_UNKNOWN_FILESYSTEM, |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 374 | manager_.GetMountErrorOfReservedMountPath(mount_path)); |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 375 | EXPECT_EQ( |
| 376 | MOUNT_ERROR_DIRECTORY_CREATION_FAILED, |
| 377 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 378 | EXPECT_EQ(kTestMountPath, mount_path_); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 379 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 380 | EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_)); |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 381 | EXPECT_EQ(MOUNT_ERROR_UNKNOWN_FILESYSTEM, |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 382 | manager_.GetMountErrorOfReservedMountPath(mount_path)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 385 | // Verifies that MountManager::Mount() returns an error when it fails to |
| 386 | // create a mount directory after a number of trials. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 387 | TEST_F(MountManagerTest, MountFailedInCreateOrReuseEmptyDirectoryWithFallback) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 388 | source_path_ = kTestSourcePath; |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 389 | std::string suggested_mount_path = kTestMountPath; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 390 | |
| 391 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 392 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 393 | .WillOnce(Return(false)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 394 | EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 395 | EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 396 | EXPECT_CALL(manager_, SuggestMountPath(source_path_)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 397 | .WillOnce(Return(suggested_mount_path)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 398 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 399 | EXPECT_EQ( |
| 400 | MOUNT_ERROR_DIRECTORY_CREATION_FAILED, |
| 401 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 402 | EXPECT_EQ("", mount_path_); |
| 403 | EXPECT_FALSE(manager_.IsMountPathInCache(suggested_mount_path)); |
| 404 | } |
| 405 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 406 | // Verifies that MountManager::Mount() returns no error when it successfully |
Tatsuhisa Yamaguchi | 578b3bf | 2016-09-30 16:57:18 +0900 | [diff] [blame] | 407 | // mounts a source path to a specified mount path in read-write mode. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 408 | TEST_F(MountManagerTest, MountSucceededWithGivenMountPath) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 409 | source_path_ = kTestSourcePath; |
| 410 | mount_path_ = kTestMountPath; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 411 | base::FilePath mount_path(mount_path_); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 412 | |
Sergei Datsenko | 3928f78 | 2020-12-31 09:14:04 +1100 | [diff] [blame] | 413 | options_.push_back("rw"); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 414 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 415 | .WillOnce(Return(true)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 416 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 417 | .Times(0); |
| 418 | EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 419 | .WillOnce(Return(true)); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 420 | auto ptr = std::make_unique<MountPoint>( |
| 421 | MountPointData{ |
| 422 | .mount_path = mount_path, |
| 423 | .flags = IsReadOnlyMount(options_) ? MS_RDONLY : 0, |
| 424 | }, |
| 425 | &platform_); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 426 | EXPECT_CALL(manager_, |
| 427 | DoMount(source_path_, filesystem_type_, options_, mount_path, _)) |
| 428 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_NONE), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 429 | Return(ByMove(std::move(ptr))))); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 430 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 431 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 432 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_, |
| 433 | options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 434 | EXPECT_EQ(kTestMountPath, mount_path_); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 435 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 436 | |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 437 | base::Optional<MountEntry> mount_entry; |
| 438 | mount_entry = manager_.GetMountEntryForTest(source_path_); |
| 439 | EXPECT_TRUE(mount_entry); |
| 440 | EXPECT_FALSE(mount_entry->is_read_only); |
George Burgess IV | 3d44fa8 | 2016-09-07 23:47:21 -0700 | [diff] [blame] | 441 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 442 | EXPECT_CALL(platform_, Unmount(mount_path_, _)) |
| 443 | .WillOnce(Return(MOUNT_ERROR_NONE)); |
George Burgess IV | 3d44fa8 | 2016-09-07 23:47:21 -0700 | [diff] [blame] | 444 | EXPECT_TRUE(manager_.UnmountAll()); |
| 445 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 446 | } |
| 447 | |
Tatsuhisa Yamaguchi | 578b3bf | 2016-09-30 16:57:18 +0900 | [diff] [blame] | 448 | // Verifies that MountManager::Mount() stores correct mount status in cache when |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 449 | // read-only option is specified. |
| 450 | TEST_F(MountManagerTest, MountCachesStatusWithReadOnlyOption) { |
| 451 | source_path_ = kTestSourcePath; |
| 452 | mount_path_ = kTestMountPath; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 453 | base::FilePath mount_path(mount_path_); |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 454 | |
| 455 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)) |
| 456 | .WillOnce(Return(true)); |
| 457 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 458 | .Times(0); |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 459 | // Add read-only mount option. |
Sergei Datsenko | 3928f78 | 2020-12-31 09:14:04 +1100 | [diff] [blame] | 460 | options_.push_back("ro"); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 461 | auto ptr = std::make_unique<MountPoint>( |
| 462 | MountPointData{ |
| 463 | .mount_path = mount_path, |
| 464 | .flags = IsReadOnlyMount(options_) ? MS_RDONLY : 0, |
| 465 | }, |
| 466 | &platform_); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 467 | EXPECT_CALL(manager_, |
| 468 | DoMount(source_path_, filesystem_type_, options_, mount_path, _)) |
| 469 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_NONE), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 470 | Return(ByMove(std::move(ptr))))); |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 471 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 472 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 473 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_, |
| 474 | options_, &mount_path_)); |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 475 | EXPECT_EQ(kTestMountPath, mount_path_); |
| 476 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 477 | |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 478 | base::Optional<MountEntry> mount_entry; |
| 479 | mount_entry = manager_.GetMountEntryForTest(source_path_); |
| 480 | EXPECT_TRUE(mount_entry); |
| 481 | EXPECT_TRUE(mount_entry->is_read_only); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 482 | EXPECT_CALL(platform_, Unmount(mount_path_, _)) |
| 483 | .WillOnce(Return(MOUNT_ERROR_NONE)); |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 484 | } |
| 485 | |
Tatsuhisa Yamaguchi | 578b3bf | 2016-09-30 16:57:18 +0900 | [diff] [blame] | 486 | // Verifies that MountManager::Mount() stores correct mount status in cache when |
| 487 | // the mounter requested to mount in read-write mode but fell back to read-only |
| 488 | // mode. |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 489 | TEST_F(MountManagerTest, MountSuccededWithReadOnlyFallback) { |
| 490 | source_path_ = kTestSourcePath; |
| 491 | mount_path_ = kTestMountPath; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 492 | base::FilePath mount_path(mount_path_); |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 493 | |
| 494 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)) |
| 495 | .WillOnce(Return(true)); |
| 496 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 497 | .Times(0); |
Sergei Datsenko | 3928f78 | 2020-12-31 09:14:04 +1100 | [diff] [blame] | 498 | options_.push_back("rw"); |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 499 | // Emulate Mounter added read-only option as a fallback. |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 500 | auto ptr = std::make_unique<MountPoint>( |
| 501 | MountPointData{ |
| 502 | .mount_path = mount_path, |
| 503 | .flags = MS_RDONLY, |
| 504 | }, |
| 505 | &platform_); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 506 | EXPECT_CALL(manager_, |
| 507 | DoMount(source_path_, filesystem_type_, options_, mount_path, _)) |
| 508 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_NONE), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 509 | Return(ByMove(std::move(ptr))))); |
| 510 | |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 511 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 512 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 513 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_, |
| 514 | options_, &mount_path_)); |
Tatsuhisa Yamaguchi | 5a6a303 | 2016-08-19 20:03:54 +0900 | [diff] [blame] | 515 | EXPECT_EQ(kTestMountPath, mount_path_); |
| 516 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 517 | |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 518 | base::Optional<MountEntry> mount_entry; |
| 519 | mount_entry = manager_.GetMountEntryForTest(source_path_); |
| 520 | EXPECT_TRUE(mount_entry); |
| 521 | EXPECT_TRUE(mount_entry->is_read_only); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 522 | EXPECT_CALL(platform_, Unmount(mount_path_, _)) |
| 523 | .WillOnce(Return(MOUNT_ERROR_NONE)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 526 | // Verifies that MountManager::Mount() returns no error when it successfully |
| 527 | // mounts a source path with no mount path specified. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 528 | TEST_F(MountManagerTest, MountSucceededWithEmptyMountPath) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 529 | source_path_ = kTestSourcePath; |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 530 | std::string suggested_mount_path = kTestMountPath; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 531 | base::FilePath mount_path(suggested_mount_path); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 532 | |
| 533 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 534 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 535 | .WillOnce(Return(true)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 536 | EXPECT_CALL(platform_, RemoveEmptyDirectory(suggested_mount_path)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 537 | .WillOnce(Return(true)); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 538 | |
| 539 | auto ptr = std::make_unique<MountPoint>( |
| 540 | MountPointData{ |
| 541 | .mount_path = mount_path, |
| 542 | .flags = IsReadOnlyMount(options_) ? MS_RDONLY : 0, |
| 543 | }, |
| 544 | &platform_); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 545 | EXPECT_CALL(manager_, |
| 546 | DoMount(source_path_, filesystem_type_, options_, mount_path, _)) |
| 547 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_NONE), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 548 | Return(ByMove(std::move(ptr))))); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 549 | EXPECT_CALL(manager_, SuggestMountPath(source_path_)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 550 | .WillOnce(Return(suggested_mount_path)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 551 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 552 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_, |
| 553 | options_, &mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 554 | EXPECT_EQ(suggested_mount_path, mount_path_); |
| 555 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 556 | EXPECT_CALL(platform_, Unmount(suggested_mount_path, _)) |
| 557 | .WillOnce(Return(MOUNT_ERROR_NONE)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 558 | EXPECT_TRUE(manager_.UnmountAll()); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 559 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Ben Chan | 09c90d0 | 2012-04-25 22:09:09 -0700 | [diff] [blame] | 562 | // Verifies that MountManager::Mount() returns no error when it successfully |
| 563 | // mounts a source path with a given mount label in options. |
| 564 | TEST_F(MountManagerTest, MountSucceededWithGivenMountLabel) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 565 | source_path_ = kTestSourcePath; |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 566 | std::string suggested_mount_path = kTestMountPath; |
| 567 | std::string final_mount_path = |
| 568 | std::string(kMountRootDirectory) + "/custom_label"; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 569 | base::FilePath mount_path(final_mount_path); |
Ben Chan | 09c90d0 | 2012-04-25 22:09:09 -0700 | [diff] [blame] | 570 | options_.push_back("mountlabel=custom_label"); |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 571 | std::vector<std::string> updated_options; |
Ben Chan | 09c90d0 | 2012-04-25 22:09:09 -0700 | [diff] [blame] | 572 | |
| 573 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
| 574 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 575 | .WillOnce(Return(true)); |
Ben Chan | 09c90d0 | 2012-04-25 22:09:09 -0700 | [diff] [blame] | 576 | EXPECT_CALL(platform_, RemoveEmptyDirectory(final_mount_path)) |
| 577 | .WillOnce(Return(true)); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 578 | auto ptr = std::make_unique<MountPoint>( |
| 579 | MountPointData{ |
| 580 | .mount_path = mount_path, |
| 581 | .flags = IsReadOnlyMount(options_) ? MS_RDONLY : 0, |
| 582 | }, |
| 583 | &platform_); |
| 584 | EXPECT_CALL(manager_, |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 585 | DoMount(source_path_, filesystem_type_, _, mount_path, _)) |
| 586 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_NONE), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 587 | Return(ByMove(std::move(ptr))))); |
Ben Chan | 09c90d0 | 2012-04-25 22:09:09 -0700 | [diff] [blame] | 588 | EXPECT_CALL(manager_, SuggestMountPath(source_path_)) |
| 589 | .WillOnce(Return(suggested_mount_path)); |
| 590 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 591 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_, |
| 592 | options_, &mount_path_)); |
Ben Chan | 09c90d0 | 2012-04-25 22:09:09 -0700 | [diff] [blame] | 593 | EXPECT_EQ(final_mount_path, mount_path_); |
| 594 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 595 | EXPECT_CALL(platform_, Unmount(final_mount_path, _)) |
| 596 | .WillOnce(Return(MOUNT_ERROR_NONE)); |
Ben Chan | 09c90d0 | 2012-04-25 22:09:09 -0700 | [diff] [blame] | 597 | EXPECT_TRUE(manager_.UnmountAll()); |
| 598 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
| 599 | } |
| 600 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 601 | // Verifies that MountManager::Mount() handles the mounting of an already |
| 602 | // mounted source path properly. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 603 | TEST_F(MountManagerTest, MountWithAlreadyMountedSourcePath) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 604 | source_path_ = kTestSourcePath; |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 605 | std::string suggested_mount_path = kTestMountPath; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 606 | base::FilePath mount_path(suggested_mount_path); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 607 | |
| 608 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 609 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 610 | .WillOnce(Return(true)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 611 | EXPECT_CALL(platform_, RemoveEmptyDirectory(suggested_mount_path)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 612 | .WillOnce(Return(true)); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 613 | auto ptr = std::make_unique<MountPoint>( |
| 614 | MountPointData{ |
| 615 | .mount_path = mount_path, |
| 616 | .flags = IsReadOnlyMount(options_) ? MS_RDONLY : 0, |
| 617 | }, |
| 618 | &platform_); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 619 | EXPECT_CALL(manager_, |
| 620 | DoMount(source_path_, filesystem_type_, options_, mount_path, _)) |
| 621 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_NONE), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 622 | Return(ByMove(std::move(ptr))))); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 623 | EXPECT_CALL(manager_, SuggestMountPath(source_path_)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 624 | .WillOnce(Return(suggested_mount_path)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 625 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 626 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_, |
| 627 | options_, &mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 628 | EXPECT_EQ(suggested_mount_path, mount_path_); |
| 629 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 630 | |
| 631 | // Mount an already-mounted source path without specifying a mount path |
| 632 | mount_path_.clear(); |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 633 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_, |
| 634 | options_, &mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 635 | EXPECT_EQ(suggested_mount_path, mount_path_); |
| 636 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 637 | |
| 638 | // Mount an already-mounted source path to the same mount path |
| 639 | mount_path_ = suggested_mount_path; |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 640 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_, |
| 641 | options_, &mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 642 | EXPECT_EQ(suggested_mount_path, mount_path_); |
| 643 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 644 | |
| 645 | // Mount an already-mounted source path to a different mount path |
| 646 | mount_path_ = "another-path"; |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 647 | EXPECT_EQ( |
| 648 | MOUNT_ERROR_PATH_ALREADY_MOUNTED, |
| 649 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 650 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 651 | EXPECT_TRUE(manager_.IsMountPathInCache(suggested_mount_path)); |
| 652 | |
| 653 | EXPECT_TRUE(manager_.UnmountAll()); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 654 | EXPECT_FALSE(manager_.IsMountPathReserved(suggested_mount_path)); |
| 655 | } |
| 656 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 657 | // Verifies that MountManager::Mount() successfully reserves a path for a given |
| 658 | // type of error. A specific mount path is given in this case. |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 659 | TEST_F(MountManagerTest, MountSucceededWithGivenMountPathInReservedCase) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 660 | source_path_ = kTestSourcePath; |
| 661 | mount_path_ = kTestMountPath; |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 662 | |
| 663 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)) |
| 664 | .WillOnce(Return(true)); |
| 665 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 666 | .Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 667 | EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_)) |
| 668 | .WillOnce(Return(true)); |
Anand K Mistry | d0a0523 | 2020-01-24 14:04:18 +1100 | [diff] [blame] | 669 | EXPECT_CALL(manager_, DoMount(source_path_, filesystem_type_, options_, |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 670 | base::FilePath(mount_path_), _)) |
| 671 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_UNKNOWN_FILESYSTEM), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 672 | Return(ByMove(nullptr)))); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 673 | EXPECT_CALL(manager_, |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 674 | ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM)) |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 675 | .WillOnce(Return(true)); |
| 676 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 677 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 678 | EXPECT_EQ( |
| 679 | MOUNT_ERROR_UNKNOWN_FILESYSTEM, |
| 680 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 681 | EXPECT_EQ(kTestMountPath, mount_path_); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 682 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 683 | EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_)); |
| 684 | EXPECT_TRUE(manager_.UnmountAll()); |
| 685 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
| 686 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
| 687 | } |
| 688 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 689 | // Verifies that MountManager::Mount() successfully reserves a path for a given |
| 690 | // type of error. No specific mount path is given in this case. |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 691 | TEST_F(MountManagerTest, MountSucceededWithEmptyMountPathInReservedCase) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 692 | source_path_ = kTestSourcePath; |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 693 | std::string suggested_mount_path = kTestMountPath; |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 694 | |
| 695 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
| 696 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 697 | .WillOnce(Return(true)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 698 | EXPECT_CALL(platform_, RemoveEmptyDirectory(suggested_mount_path)) |
| 699 | .WillOnce(Return(true)); |
| 700 | EXPECT_CALL(manager_, DoMount(source_path_, filesystem_type_, options_, |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 701 | base::FilePath(suggested_mount_path), _)) |
| 702 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_UNKNOWN_FILESYSTEM), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 703 | Return(ByMove(nullptr)))); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 704 | EXPECT_CALL(manager_, |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 705 | ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM)) |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 706 | .WillOnce(Return(true)); |
| 707 | EXPECT_CALL(manager_, SuggestMountPath(source_path_)) |
| 708 | .WillOnce(Return(suggested_mount_path)); |
| 709 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 710 | EXPECT_EQ( |
| 711 | MOUNT_ERROR_UNKNOWN_FILESYSTEM, |
| 712 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 713 | EXPECT_EQ(suggested_mount_path, mount_path_); |
| 714 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 715 | EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_)); |
| 716 | EXPECT_TRUE(manager_.UnmountAll()); |
| 717 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 718 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
| 719 | } |
| 720 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 721 | // Verifies that MountManager::Mount() successfully reserves a path for a given |
| 722 | // type of error and returns the same error when it tries to mount the same path |
| 723 | // again. |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 724 | TEST_F(MountManagerTest, MountSucceededWithAlreadyReservedMountPath) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 725 | source_path_ = kTestSourcePath; |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 726 | std::string suggested_mount_path = kTestMountPath; |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 727 | |
| 728 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
| 729 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 730 | .WillOnce(Return(true)); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 731 | EXPECT_CALL(platform_, RemoveEmptyDirectory(suggested_mount_path)) |
| 732 | .WillOnce(Return(true)); |
| 733 | EXPECT_CALL(manager_, DoMount(source_path_, filesystem_type_, options_, |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 734 | base::FilePath(suggested_mount_path), _)) |
| 735 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_UNKNOWN_FILESYSTEM), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 736 | Return(ByMove(nullptr)))); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 737 | EXPECT_CALL(manager_, |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 738 | ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM)) |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 739 | .WillOnce(Return(true)); |
| 740 | EXPECT_CALL(manager_, SuggestMountPath(source_path_)) |
| 741 | .WillOnce(Return(suggested_mount_path)); |
| 742 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 743 | EXPECT_EQ( |
| 744 | MOUNT_ERROR_UNKNOWN_FILESYSTEM, |
| 745 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 746 | EXPECT_EQ(suggested_mount_path, mount_path_); |
| 747 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 748 | EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_)); |
| 749 | |
| 750 | mount_path_ = ""; |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 751 | EXPECT_EQ( |
| 752 | MOUNT_ERROR_UNKNOWN_FILESYSTEM, |
| 753 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 754 | EXPECT_EQ(suggested_mount_path, mount_path_); |
| 755 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 756 | EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_)); |
| 757 | |
| 758 | EXPECT_TRUE(manager_.UnmountAll()); |
| 759 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 760 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
| 761 | } |
| 762 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 763 | // Verifies that MountManager::Mount() successfully reserves a path for a given |
| 764 | // type of error and returns the same error when it tries to mount the same path |
| 765 | // again. |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 766 | TEST_F(MountManagerTest, MountFailedWithGivenMountPathInReservedCase) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 767 | source_path_ = kTestSourcePath; |
| 768 | mount_path_ = kTestMountPath; |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 769 | |
| 770 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)) |
| 771 | .WillOnce(Return(true)); |
| 772 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 773 | .Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 774 | EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_)) |
| 775 | .WillOnce(Return(true)); |
Anand K Mistry | d0a0523 | 2020-01-24 14:04:18 +1100 | [diff] [blame] | 776 | EXPECT_CALL(manager_, DoMount(source_path_, filesystem_type_, options_, |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 777 | base::FilePath(mount_path_), _)) |
| 778 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_UNKNOWN_FILESYSTEM), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 779 | Return(ByMove(nullptr)))); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 780 | EXPECT_CALL(manager_, |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 781 | ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM)) |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 782 | .WillOnce(Return(false)); |
| 783 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 784 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 785 | EXPECT_EQ( |
| 786 | MOUNT_ERROR_UNKNOWN_FILESYSTEM, |
| 787 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 788 | EXPECT_EQ(kTestMountPath, mount_path_); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 789 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 790 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
| 791 | } |
| 792 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 793 | // Verifies that MountManager::Mount() fails to mount or reserve a path for |
| 794 | // a type of error that is not enabled for reservation. |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 795 | TEST_F(MountManagerTest, MountFailedWithEmptyMountPathInReservedCase) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 796 | source_path_ = kTestSourcePath; |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 797 | std::string suggested_mount_path = kTestMountPath; |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 798 | |
| 799 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
| 800 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 801 | .WillOnce(Return(true)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 802 | EXPECT_CALL(platform_, RemoveEmptyDirectory(suggested_mount_path)) |
| 803 | .WillOnce(Return(true)); |
| 804 | EXPECT_CALL(manager_, DoMount(source_path_, filesystem_type_, options_, |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 805 | base::FilePath(suggested_mount_path), _)) |
| 806 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_UNKNOWN_FILESYSTEM), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 807 | Return(ByMove(nullptr)))); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 808 | EXPECT_CALL(manager_, |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 809 | ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM)) |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 810 | .WillOnce(Return(false)); |
| 811 | EXPECT_CALL(manager_, SuggestMountPath(source_path_)) |
| 812 | .WillOnce(Return(suggested_mount_path)); |
| 813 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 814 | EXPECT_EQ( |
| 815 | MOUNT_ERROR_UNKNOWN_FILESYSTEM, |
| 816 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 817 | EXPECT_EQ("", mount_path_); |
| 818 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 819 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 822 | // Verifies that MountManager::Unmount() returns an error when it is invoked |
| 823 | // to unmount an empty path. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 824 | TEST_F(MountManagerTest, UnmountFailedWithEmptyPath) { |
| 825 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 826 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 827 | .Times(0); |
| 828 | EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 829 | EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 830 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 831 | |
François Degros | 3fe77c5 | 2020-02-20 14:44:54 +1100 | [diff] [blame] | 832 | EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED, manager_.Unmount(mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 835 | // Verifies that MountManager::Unmount() returns an error when it fails to |
| 836 | // unmount a path that is not mounted. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 837 | TEST_F(MountManagerTest, UnmountFailedWithPathNotMounted) { |
| 838 | mount_path_ = "nonexistent-path"; |
| 839 | |
| 840 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 841 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 842 | .Times(0); |
| 843 | EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 844 | EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 845 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 846 | |
Anand K Mistry | 5a6d5fa | 2019-11-05 17:21:48 +1100 | [diff] [blame] | 847 | EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED, manager_.Unmount(mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 850 | // Verifies that MountManager::Unmount() returns no error when it successfully |
| 851 | // unmounts a source path. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 852 | TEST_F(MountManagerTest, UnmountSucceededWithGivenSourcePath) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 853 | source_path_ = kTestSourcePath; |
| 854 | mount_path_ = kTestMountPath; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 855 | base::FilePath mount_path(mount_path_); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 856 | |
| 857 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 858 | .WillOnce(Return(true)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 859 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 860 | .Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 861 | EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 862 | .WillOnce(Return(true)); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 863 | auto ptr = std::make_unique<MountPoint>( |
| 864 | MountPointData{ |
| 865 | .mount_path = mount_path, |
| 866 | .flags = IsReadOnlyMount(options_) ? MS_RDONLY : 0, |
| 867 | }, |
| 868 | &platform_); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 869 | EXPECT_CALL(manager_, |
| 870 | DoMount(source_path_, filesystem_type_, options_, mount_path, _)) |
| 871 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_NONE), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 872 | Return(ByMove(std::move(ptr))))); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 873 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 874 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 875 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_, |
| 876 | options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 877 | EXPECT_EQ(kTestMountPath, mount_path_); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 878 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 879 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 880 | EXPECT_CALL(platform_, Unmount(mount_path_, _)) |
| 881 | .WillOnce(Return(MOUNT_ERROR_NONE)); |
Anand K Mistry | 5a6d5fa | 2019-11-05 17:21:48 +1100 | [diff] [blame] | 882 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Unmount(source_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 883 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 884 | } |
| 885 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 886 | // Verifies that MountManager::Unmount() returns no error when it successfully |
| 887 | // unmounts a mount path. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 888 | TEST_F(MountManagerTest, UnmountSucceededWithGivenMountPath) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 889 | source_path_ = kTestSourcePath; |
| 890 | mount_path_ = kTestMountPath; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 891 | base::FilePath mount_path(mount_path_); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 892 | |
| 893 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 894 | .WillOnce(Return(true)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 895 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 896 | .Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 897 | EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_)) |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 898 | .WillOnce(Return(true)); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 899 | auto ptr = std::make_unique<MountPoint>( |
| 900 | MountPointData{ |
| 901 | .mount_path = mount_path, |
| 902 | .flags = IsReadOnlyMount(options_) ? MS_RDONLY : 0, |
| 903 | }, |
| 904 | &platform_); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 905 | EXPECT_CALL(manager_, |
| 906 | DoMount(source_path_, filesystem_type_, options_, mount_path, _)) |
| 907 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_NONE), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 908 | Return(ByMove(std::move(ptr))))); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 909 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 910 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 911 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_, |
| 912 | options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 913 | EXPECT_EQ(kTestMountPath, mount_path_); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 914 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 915 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 916 | EXPECT_CALL(platform_, Unmount(mount_path_, _)) |
| 917 | .WillOnce(Return(MOUNT_ERROR_NONE)); |
Anand K Mistry | 5a6d5fa | 2019-11-05 17:21:48 +1100 | [diff] [blame] | 918 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Unmount(mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 919 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 920 | } |
| 921 | |
Sergei Datsenko | 2879b5c | 2019-09-26 13:47:40 +1000 | [diff] [blame] | 922 | // Verifies that MountManager::Unmount() removes mount path from cache if |
| 923 | // it appears to be not mounted. |
| 924 | TEST_F(MountManagerTest, UnmountRemovesFromCacheIfNotMounted) { |
| 925 | source_path_ = kTestSourcePath; |
| 926 | mount_path_ = kTestMountPath; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 927 | base::FilePath mount_path(mount_path_); |
Sergei Datsenko | 2879b5c | 2019-09-26 13:47:40 +1000 | [diff] [blame] | 928 | |
| 929 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)) |
| 930 | .WillOnce(Return(true)); |
| 931 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 932 | .Times(0); |
Sergei Datsenko | 2879b5c | 2019-09-26 13:47:40 +1000 | [diff] [blame] | 933 | EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_)) |
| 934 | .WillOnce(Return(true)); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 935 | auto ptr = std::make_unique<MountPoint>( |
| 936 | MountPointData{ |
| 937 | .mount_path = mount_path, |
| 938 | .flags = IsReadOnlyMount(options_) ? MS_RDONLY : 0, |
| 939 | }, |
| 940 | &platform_); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 941 | EXPECT_CALL(manager_, |
| 942 | DoMount(source_path_, filesystem_type_, options_, mount_path, _)) |
| 943 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_NONE), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 944 | Return(ByMove(std::move(ptr))))); |
Sergei Datsenko | 2879b5c | 2019-09-26 13:47:40 +1000 | [diff] [blame] | 945 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 946 | |
| 947 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_, |
| 948 | options_, &mount_path_)); |
| 949 | EXPECT_EQ(kTestMountPath, mount_path_); |
| 950 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 951 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 952 | EXPECT_CALL(platform_, Unmount(mount_path_, _)) |
| 953 | .WillOnce(Return(MOUNT_ERROR_PATH_NOT_MOUNTED)); |
| 954 | |
Anand K Mistry | 5a6d5fa | 2019-11-05 17:21:48 +1100 | [diff] [blame] | 955 | EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED, manager_.Unmount(mount_path_)); |
Sergei Datsenko | 2879b5c | 2019-09-26 13:47:40 +1000 | [diff] [blame] | 956 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 957 | } |
| 958 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 959 | // Verifies that MountManager::Unmount() returns no error when it is invoked |
| 960 | // to unmount the source path of a reserved mount path. |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 961 | TEST_F(MountManagerTest, UnmountSucceededWithGivenSourcePathInReservedCase) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 962 | source_path_ = kTestSourcePath; |
| 963 | mount_path_ = kTestMountPath; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 964 | base::FilePath mount_path(mount_path_); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 965 | |
| 966 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)) |
| 967 | .WillOnce(Return(true)); |
| 968 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 969 | .Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 970 | EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_)) |
| 971 | .WillOnce(Return(true)); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 972 | EXPECT_CALL(manager_, |
| 973 | DoMount(source_path_, filesystem_type_, options_, mount_path, _)) |
| 974 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_UNKNOWN_FILESYSTEM), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 975 | Return(ByMove(nullptr)))); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 976 | EXPECT_CALL(manager_, |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 977 | ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM)) |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 978 | .WillOnce(Return(true)); |
| 979 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 980 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 981 | EXPECT_EQ( |
| 982 | MOUNT_ERROR_UNKNOWN_FILESYSTEM, |
| 983 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 984 | EXPECT_EQ(kTestMountPath, mount_path_); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 985 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 986 | EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_)); |
| 987 | |
Anand K Mistry | 5a6d5fa | 2019-11-05 17:21:48 +1100 | [diff] [blame] | 988 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Unmount(source_path_)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 989 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 990 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
| 991 | } |
| 992 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 993 | // Verifies that MountManager::Unmount() returns no error when it is invoked |
| 994 | // to unmount a reserved mount path. |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 995 | TEST_F(MountManagerTest, UnmountSucceededWithGivenMountPathInReservedCase) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 996 | source_path_ = kTestSourcePath; |
| 997 | mount_path_ = kTestMountPath; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 998 | base::FilePath mount_path(mount_path_); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 999 | |
| 1000 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)) |
| 1001 | .WillOnce(Return(true)); |
| 1002 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 1003 | .Times(0); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1004 | EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_)) |
| 1005 | .WillOnce(Return(true)); |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 1006 | EXPECT_CALL(manager_, |
| 1007 | DoMount(source_path_, filesystem_type_, options_, mount_path, _)) |
| 1008 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_UNKNOWN_FILESYSTEM), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 1009 | Return(ByMove(nullptr)))); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1010 | EXPECT_CALL(manager_, |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 1011 | ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM)) |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1012 | .WillOnce(Return(true)); |
| 1013 | EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0); |
| 1014 | |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 1015 | EXPECT_EQ( |
| 1016 | MOUNT_ERROR_UNKNOWN_FILESYSTEM, |
| 1017 | manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_)); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 1018 | EXPECT_EQ(kTestMountPath, mount_path_); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1019 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 1020 | EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_)); |
| 1021 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 1022 | EXPECT_CALL(platform_, Unmount).Times(0); |
Anand K Mistry | 5a6d5fa | 2019-11-05 17:21:48 +1100 | [diff] [blame] | 1023 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Unmount(mount_path_)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1024 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 1025 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
| 1026 | } |
| 1027 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 1028 | // Verifies that MountManager::IsMountPathInCache() works as expected. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 1029 | TEST_F(MountManagerTest, IsMountPathInCache) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 1030 | source_path_ = kTestSourcePath; |
| 1031 | mount_path_ = kTestMountPath; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 1032 | |
| 1033 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1034 | manager_.AddMountStateCache(source_path_, MakeMountPoint(mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 1035 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 1036 | EXPECT_TRUE(manager_.RemoveMountPathFromCache(mount_path_)); |
| 1037 | EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_)); |
| 1038 | } |
| 1039 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 1040 | // Verifies that MountManager::RemoveMountPathFromCache() works as expected. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 1041 | TEST_F(MountManagerTest, RemoveMountPathFromCache) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 1042 | source_path_ = kTestSourcePath; |
| 1043 | mount_path_ = kTestMountPath; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 1044 | |
| 1045 | EXPECT_FALSE(manager_.RemoveMountPathFromCache(mount_path_)); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1046 | manager_.AddMountStateCache(source_path_, MakeMountPoint(mount_path_)); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 1047 | EXPECT_TRUE(manager_.RemoveMountPathFromCache(mount_path_)); |
| 1048 | EXPECT_FALSE(manager_.RemoveMountPathFromCache(mount_path_)); |
| 1049 | } |
| 1050 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 1051 | // Verifies that MountManager::GetReservedMountPaths() works as expected. |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 1052 | TEST_F(MountManagerTest, GetReservedMountPaths) { |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1053 | std::unordered_set<std::string> reserved_paths; |
| 1054 | std::unordered_set<std::string> expected_paths; |
| 1055 | base::FilePath path1("path1"); |
| 1056 | base::FilePath path2("path2"); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 1057 | |
| 1058 | reserved_paths = manager_.GetReservedMountPaths(); |
Ben Chan | d30e743 | 2011-11-28 13:43:17 -0800 | [diff] [blame] | 1059 | EXPECT_TRUE(expected_paths == reserved_paths); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 1060 | |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 1061 | manager_.ReserveMountPath(path1, MOUNT_ERROR_UNKNOWN_FILESYSTEM); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 1062 | reserved_paths = manager_.GetReservedMountPaths(); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1063 | expected_paths.insert(path1.value()); |
Ben Chan | d30e743 | 2011-11-28 13:43:17 -0800 | [diff] [blame] | 1064 | EXPECT_TRUE(expected_paths == reserved_paths); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 1065 | |
Ben Chan | fcb2fc0 | 2011-11-21 09:44:07 -0800 | [diff] [blame] | 1066 | manager_.ReserveMountPath(path2, MOUNT_ERROR_UNKNOWN_FILESYSTEM); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 1067 | reserved_paths = manager_.GetReservedMountPaths(); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1068 | expected_paths.insert(path2.value()); |
Ben Chan | d30e743 | 2011-11-28 13:43:17 -0800 | [diff] [blame] | 1069 | EXPECT_TRUE(expected_paths == reserved_paths); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 1070 | |
| 1071 | manager_.UnreserveMountPath(path1); |
| 1072 | reserved_paths = manager_.GetReservedMountPaths(); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1073 | expected_paths.erase(path1.value()); |
Ben Chan | d30e743 | 2011-11-28 13:43:17 -0800 | [diff] [blame] | 1074 | EXPECT_TRUE(expected_paths == reserved_paths); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 1075 | |
| 1076 | manager_.UnreserveMountPath(path2); |
| 1077 | reserved_paths = manager_.GetReservedMountPaths(); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1078 | expected_paths.erase(path2.value()); |
Ben Chan | d30e743 | 2011-11-28 13:43:17 -0800 | [diff] [blame] | 1079 | EXPECT_TRUE(expected_paths == reserved_paths); |
Ben Chan | 632c9f8 | 2011-10-11 12:22:16 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 1082 | // Verifies that MountManager::ReserveMountPath() and |
| 1083 | // MountManager::UnreserveMountPath() work as expected. |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1084 | TEST_F(MountManagerTest, ReserveAndUnreserveMountPath) { |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 1085 | mount_path_ = kTestMountPath; |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1086 | |
| 1087 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1088 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.GetMountErrorOfReservedMountPath( |
| 1089 | base::FilePath(mount_path_))); |
| 1090 | manager_.ReserveMountPath(base::FilePath(mount_path_), |
| 1091 | MOUNT_ERROR_UNKNOWN_FILESYSTEM); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1092 | EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_)); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1093 | EXPECT_EQ( |
| 1094 | MOUNT_ERROR_UNKNOWN_FILESYSTEM, |
| 1095 | manager_.GetMountErrorOfReservedMountPath(base::FilePath(mount_path_))); |
| 1096 | manager_.UnreserveMountPath(base::FilePath(mount_path_)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1097 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1098 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.GetMountErrorOfReservedMountPath( |
| 1099 | base::FilePath(mount_path_))); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1100 | |
| 1101 | // Removing a nonexistent mount path should be ok |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1102 | manager_.UnreserveMountPath(base::FilePath(mount_path_)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1103 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
| 1104 | |
| 1105 | // Adding an existent mount path should be ok |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1106 | manager_.ReserveMountPath(base::FilePath(mount_path_), |
| 1107 | MOUNT_ERROR_UNSUPPORTED_FILESYSTEM); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1108 | EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_)); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1109 | EXPECT_EQ( |
| 1110 | MOUNT_ERROR_UNSUPPORTED_FILESYSTEM, |
| 1111 | manager_.GetMountErrorOfReservedMountPath(base::FilePath(mount_path_))); |
| 1112 | manager_.ReserveMountPath(base::FilePath(mount_path_), |
| 1113 | MOUNT_ERROR_UNKNOWN_FILESYSTEM); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1114 | EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_)); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1115 | EXPECT_EQ( |
| 1116 | MOUNT_ERROR_UNSUPPORTED_FILESYSTEM, |
| 1117 | manager_.GetMountErrorOfReservedMountPath(base::FilePath(mount_path_))); |
| 1118 | manager_.UnreserveMountPath(base::FilePath(mount_path_)); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1119 | EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_)); |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1120 | EXPECT_EQ(MOUNT_ERROR_NONE, manager_.GetMountErrorOfReservedMountPath( |
| 1121 | base::FilePath(mount_path_))); |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Ben Chan | 8fb742b | 2014-04-28 23:46:57 -0700 | [diff] [blame] | 1124 | // Verifies that MountManager::GetMountEntries() returns the expected list of |
| 1125 | // mount entries under different scenarios. |
| 1126 | TEST_F(MountManagerTest, GetMountEntries) { |
Ben Chan | 7dfb810 | 2017-10-17 15:47:37 -0700 | [diff] [blame] | 1127 | // No mount entry is returned. |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 1128 | std::vector<MountEntry> mount_entries = manager_.GetMountEntries(); |
Ben Chan | 8fb742b | 2014-04-28 23:46:57 -0700 | [diff] [blame] | 1129 | EXPECT_TRUE(mount_entries.empty()); |
| 1130 | |
| 1131 | // A normal mount entry is returned. |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1132 | manager_.AddMountStateCache(kTestSourcePath, MakeMountPoint(kTestMountPath)); |
Ben Chan | 7dfb810 | 2017-10-17 15:47:37 -0700 | [diff] [blame] | 1133 | mount_entries = manager_.GetMountEntries(); |
Ben Chan | 8fb742b | 2014-04-28 23:46:57 -0700 | [diff] [blame] | 1134 | ASSERT_EQ(1, mount_entries.size()); |
Ben Chan | 0214e30 | 2017-10-17 15:39:16 -0700 | [diff] [blame] | 1135 | EXPECT_EQ(MOUNT_ERROR_NONE, mount_entries[0].error_type); |
| 1136 | EXPECT_EQ(kTestSourcePath, mount_entries[0].source_path); |
| 1137 | EXPECT_EQ(MOUNT_SOURCE_REMOVABLE_DEVICE, mount_entries[0].source_type); |
| 1138 | EXPECT_EQ(kTestMountPath, mount_entries[0].mount_path); |
Ben Chan | 8fb742b | 2014-04-28 23:46:57 -0700 | [diff] [blame] | 1139 | |
| 1140 | // A reserved mount entry is returned. |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 1141 | manager_.ReserveMountPath(base::FilePath(kTestMountPath), |
| 1142 | MOUNT_ERROR_UNKNOWN_FILESYSTEM); |
Ben Chan | 7dfb810 | 2017-10-17 15:47:37 -0700 | [diff] [blame] | 1143 | mount_entries = manager_.GetMountEntries(); |
Ben Chan | 8fb742b | 2014-04-28 23:46:57 -0700 | [diff] [blame] | 1144 | ASSERT_EQ(1, mount_entries.size()); |
Ben Chan | 0214e30 | 2017-10-17 15:39:16 -0700 | [diff] [blame] | 1145 | EXPECT_EQ(MOUNT_ERROR_UNKNOWN_FILESYSTEM, mount_entries[0].error_type); |
| 1146 | EXPECT_EQ(kTestSourcePath, mount_entries[0].source_path); |
| 1147 | EXPECT_EQ(MOUNT_SOURCE_REMOVABLE_DEVICE, mount_entries[0].source_type); |
| 1148 | EXPECT_EQ(kTestMountPath, mount_entries[0].mount_path); |
Ben Chan | 8fb742b | 2014-04-28 23:46:57 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 1151 | // Verifies that MountManager::IsPathImmediateChildOfParent() correctly |
| 1152 | // determines if a path is an immediate child of another path. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 1153 | TEST_F(MountManagerTest, IsPathImmediateChildOfParent) { |
Anand K Mistry | 09f2db1 | 2019-11-07 17:06:56 +1100 | [diff] [blame] | 1154 | EXPECT_TRUE(manager_.IsPathImmediateChildOfParent( |
| 1155 | base::FilePath("/media/archive/test.zip"), |
| 1156 | base::FilePath("/media/archive"))); |
| 1157 | EXPECT_TRUE(manager_.IsPathImmediateChildOfParent( |
| 1158 | base::FilePath("/media/archive/test.zip/"), |
| 1159 | base::FilePath("/media/archive"))); |
| 1160 | EXPECT_TRUE(manager_.IsPathImmediateChildOfParent( |
| 1161 | base::FilePath("/media/archive/test.zip"), |
| 1162 | base::FilePath("/media/archive/"))); |
| 1163 | EXPECT_TRUE(manager_.IsPathImmediateChildOfParent( |
| 1164 | base::FilePath("/media/archive/test.zip/"), |
| 1165 | base::FilePath("/media/archive/"))); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 1166 | EXPECT_FALSE(manager_.IsPathImmediateChildOfParent( |
Anand K Mistry | 09f2db1 | 2019-11-07 17:06:56 +1100 | [diff] [blame] | 1167 | base::FilePath("/media/archive/test.zip/doc.zip"), |
| 1168 | base::FilePath("/media/archive/"))); |
| 1169 | EXPECT_FALSE(manager_.IsPathImmediateChildOfParent( |
| 1170 | base::FilePath("/media/archive/test.zip"), |
| 1171 | base::FilePath("/media/removable"))); |
| 1172 | EXPECT_FALSE(manager_.IsPathImmediateChildOfParent( |
| 1173 | base::FilePath("/tmp/archive/test.zip"), |
| 1174 | base::FilePath("/media/removable"))); |
| 1175 | EXPECT_FALSE(manager_.IsPathImmediateChildOfParent( |
| 1176 | base::FilePath("/media"), base::FilePath("/media/removable"))); |
| 1177 | EXPECT_FALSE(manager_.IsPathImmediateChildOfParent( |
| 1178 | base::FilePath("/media/removable"), base::FilePath("/media/removable"))); |
| 1179 | EXPECT_FALSE(manager_.IsPathImmediateChildOfParent( |
| 1180 | base::FilePath("/media/removable/"), base::FilePath("/media/removable"))); |
| 1181 | EXPECT_FALSE(manager_.IsPathImmediateChildOfParent( |
| 1182 | base::FilePath("/media/removable/."), |
| 1183 | base::FilePath("/media/removable"))); |
| 1184 | EXPECT_FALSE(manager_.IsPathImmediateChildOfParent( |
| 1185 | base::FilePath("/media/removable/.."), |
| 1186 | base::FilePath("/media/removable"))); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 1189 | // Verifies that MountManager::IsValidMountPath() correctly determines if a |
| 1190 | // mount path is an immediate child of the mount root. |
| 1191 | TEST_F(MountManagerTest, IsValidMountPath) { |
Anand K Mistry | 09f2db1 | 2019-11-07 17:06:56 +1100 | [diff] [blame] | 1192 | EXPECT_TRUE( |
| 1193 | manager_.IsValidMountPath(base::FilePath("/media/removable/test"))); |
| 1194 | EXPECT_TRUE( |
| 1195 | manager_.IsValidMountPath(base::FilePath("/media/removable/test/"))); |
| 1196 | EXPECT_TRUE( |
| 1197 | manager_.IsValidMountPath(base::FilePath("/media/removable/test/"))); |
| 1198 | EXPECT_TRUE( |
| 1199 | manager_.IsValidMountPath(base::FilePath("/media/removable//test"))); |
| 1200 | EXPECT_FALSE( |
| 1201 | manager_.IsValidMountPath(base::FilePath("/media/archive/test"))); |
| 1202 | EXPECT_FALSE(manager_.IsValidMountPath(base::FilePath("/media/removable"))); |
| 1203 | EXPECT_FALSE(manager_.IsValidMountPath(base::FilePath("/media/removable/"))); |
| 1204 | EXPECT_FALSE(manager_.IsValidMountPath(base::FilePath("/media/removable/."))); |
| 1205 | EXPECT_FALSE( |
| 1206 | manager_.IsValidMountPath(base::FilePath("/media/removable/.."))); |
| 1207 | EXPECT_FALSE( |
| 1208 | manager_.IsValidMountPath(base::FilePath("/media/removable/test/doc"))); |
| 1209 | EXPECT_FALSE( |
| 1210 | manager_.IsValidMountPath(base::FilePath("/media/removable/../test"))); |
| 1211 | EXPECT_FALSE( |
| 1212 | manager_.IsValidMountPath(base::FilePath("/media/removable/../test/"))); |
| 1213 | EXPECT_FALSE( |
| 1214 | manager_.IsValidMountPath(base::FilePath("/media/removable/test/.."))); |
| 1215 | EXPECT_FALSE( |
| 1216 | manager_.IsValidMountPath(base::FilePath("/media/removable/test/../"))); |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Tatsuhisa Yamaguchi | b670bd1 | 2016-09-28 23:06:44 +0900 | [diff] [blame] | 1219 | // Verifies that MountManager::Mount() returns an error when the source is |
| 1220 | // not mounted yet but attempted to remount it. |
| 1221 | TEST_F(MountManagerTest, RemountFailedNotMounted) { |
| 1222 | options_.push_back(kMountOptionRemount); |
| 1223 | |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 1224 | EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0); |
Tatsuhisa Yamaguchi | b670bd1 | 2016-09-28 23:06:44 +0900 | [diff] [blame] | 1225 | |
| 1226 | // source_path = kTestSourcePath has not been mounted yet. |
| 1227 | EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED, |
| 1228 | manager_.Mount(kTestSourcePath, filesystem_type_, options_, |
| 1229 | &mount_path_)); |
| 1230 | } |
| 1231 | |
| 1232 | // Verifies that MountManager::Mount() returns no error when it successfully |
| 1233 | // remounts a source path on a specified mount path. |
| 1234 | TEST_F(MountManagerTest, RemountSucceededWithGivenSourcePath) { |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 1235 | // Mount a device in read-write mode. |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 1236 | base::FilePath mount_path(kTestMountPath); |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 1237 | EXPECT_CALL(manager_, SuggestMountPath(_)).WillOnce(Return(kTestMountPath)); |
| 1238 | EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _)) |
| 1239 | .WillOnce(Return(true)); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 1240 | |
| 1241 | auto ptr = std::make_unique<MountPoint>( |
| 1242 | MountPointData{ |
| 1243 | .mount_path = mount_path, |
| 1244 | .source = kTestSourcePath, |
| 1245 | .flags = 0, |
| 1246 | }, |
| 1247 | &platform_); |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 1248 | EXPECT_CALL(manager_, |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 1249 | DoMount(kTestSourcePath, filesystem_type_, _, mount_path, _)) |
| 1250 | .WillOnce(DoAll(SetArgPointee<4>(MOUNT_ERROR_NONE), |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 1251 | Return(ByMove(std::move(ptr))))); |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 1252 | mount_path_ = ""; |
| 1253 | EXPECT_EQ(MOUNT_ERROR_NONE, |
| 1254 | manager_.Mount(kTestSourcePath, filesystem_type_, |
| 1255 | {kMountOptionReadWrite}, &mount_path_)); |
| 1256 | EXPECT_EQ(kTestMountPath, mount_path_); |
| 1257 | base::Optional<MountEntry> mount_entry; |
| 1258 | mount_entry = manager_.GetMountEntryForTest(kTestSourcePath); |
| 1259 | ASSERT_TRUE(mount_entry); |
| 1260 | EXPECT_FALSE(mount_entry->is_read_only); |
| 1261 | EXPECT_EQ(kTestMountPath, mount_entry->mount_path); |
| 1262 | |
| 1263 | // Remount with read-only mount option. |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 1264 | EXPECT_CALL(platform_, Mount(kTestSourcePath, kTestMountPath, |
| 1265 | filesystem_type_, MS_RDONLY | MS_REMOUNT, _)) |
| 1266 | .WillOnce(Return(MOUNT_ERROR_NONE)); |
Tatsuhisa Yamaguchi | b670bd1 | 2016-09-28 23:06:44 +0900 | [diff] [blame] | 1267 | mount_path_ = ""; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 1268 | EXPECT_EQ(MOUNT_ERROR_NONE, |
| 1269 | manager_.Mount(kTestSourcePath, filesystem_type_, |
| 1270 | {kMountOptionRemount, kMountOptionReadOnly}, |
| 1271 | &mount_path_)); |
Tatsuhisa Yamaguchi | b670bd1 | 2016-09-28 23:06:44 +0900 | [diff] [blame] | 1272 | EXPECT_EQ(kTestMountPath, mount_path_); |
| 1273 | EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_)); |
| 1274 | |
Anand K Mistry | e9a60fb | 2019-11-07 16:41:03 +1100 | [diff] [blame] | 1275 | mount_entry = manager_.GetMountEntryForTest(kTestSourcePath); |
| 1276 | EXPECT_TRUE(mount_entry); |
| 1277 | EXPECT_TRUE(mount_entry->is_read_only); |
Tatsuhisa Yamaguchi | b670bd1 | 2016-09-28 23:06:44 +0900 | [diff] [blame] | 1278 | |
| 1279 | // Should be unmounted correctly even after remount. |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 1280 | EXPECT_CALL(platform_, Unmount(kTestMountPath, _)) |
| 1281 | .WillOnce(Return(MOUNT_ERROR_NONE)); |
Tatsuhisa Yamaguchi | b670bd1 | 2016-09-28 23:06:44 +0900 | [diff] [blame] | 1282 | EXPECT_TRUE(manager_.UnmountAll()); |
| 1283 | EXPECT_FALSE(manager_.IsMountPathInCache(kTestMountPath)); |
| 1284 | EXPECT_FALSE(manager_.IsMountPathReserved(kTestMountPath)); |
| 1285 | } |
| 1286 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 1287 | } // namespace cros_disks |