blob: 0b5efa9f3ad1e03f7fb20ad904a102247c790e2a [file] [log] [blame]
Ben Chan1e5a0cb2012-03-22 00:41:52 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Ben Chan8dcede82011-07-25 20:56:13 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chand30e7432011-11-28 13:43:17 -08005// Unit tests for cros_disks::MountManager. See mount-manager.h for details
6// on MountManager.
7
Ben Chan5ccd9fe2013-11-13 18:28:27 -08008#include "cros-disks/mount_manager.h"
Ben Chan8dcede82011-07-25 20:56:13 -07009
10#include <sys/mount.h>
11#include <sys/unistd.h>
12
Ben Chan632c9f82011-10-11 12:22:16 -070013#include <algorithm>
Ben Chan8dcede82011-07-25 20:56:13 -070014#include <string>
15#include <vector>
16
Sergei Datsenkoa910bba2019-06-18 13:31:59 +100017#include <brillo/process_reaper.h>
Ben Chan8dcede82011-07-25 20:56:13 -070018#include <gmock/gmock.h>
19#include <gtest/gtest.h>
20
Ben Chanbe2b4a72011-11-08 13:42:23 -080021#include "cros-disks/metrics.h"
Ben Chan8fb742b2014-04-28 23:46:57 -070022#include "cros-disks/mount_entry.h"
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +090023#include "cros-disks/mount_options.h"
Anand K Mistrye9a60fb2019-11-07 16:41:03 +110024#include "cros-disks/mount_point.h"
25#include "cros-disks/mounter.h"
Ben Chan8dcede82011-07-25 20:56:13 -070026#include "cros-disks/platform.h"
27
Ben Chan213c6d92019-04-10 16:21:52 -070028using testing::_;
Ben Chan09c90d02012-04-25 22:09:09 -070029using testing::ElementsAre;
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +090030using testing::Invoke;
Ben Chan8dcede82011-07-25 20:56:13 -070031using testing::Return;
Ben Chan8dcede82011-07-25 20:56:13 -070032
Anand K Mistry40cff452019-07-30 10:24:48 +100033namespace cros_disks {
Ben Chan460439f2011-09-13 09:16:28 -070034namespace {
Ben Chan8dcede82011-07-25 20:56:13 -070035
Ben Chanadc5d002014-03-12 15:02:26 -070036const char kMountRootDirectory[] = "/media/removable";
37const char kTestSourcePath[] = "source";
38const char kTestMountPath[] = "/media/removable/test";
39const char kInvalidMountPath[] = "/media/removable/../test/doc";
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +090040const char kMountOptionRemount[] = "remount";
41const char kMountOptionReadOnly[] = "ro";
Anand K Mistrye9a60fb2019-11-07 16:41:03 +110042const char kMountOptionReadWrite[] = "rw";
Ben Chan460439f2011-09-13 09:16:28 -070043
44} // namespace
45
Ben Chan8dcede82011-07-25 20:56:13 -070046// A mock platform class for testing the mount manager base class.
47class MockPlatform : public Platform {
48 public:
Ben Chan40ecb9b2017-03-08 11:26:07 -080049 MockPlatform() = default;
Ben Chan8dcede82011-07-25 20:56:13 -070050
Ben Chan21150af2019-09-11 17:04:07 -070051 MOCK_METHOD(bool, CreateDirectory, (const std::string&), (const, override));
52 MOCK_METHOD(bool,
53 CreateOrReuseEmptyDirectory,
54 (const std::string&),
55 (const, override));
56 MOCK_METHOD(bool,
57 CreateOrReuseEmptyDirectoryWithFallback,
58 (std::string*, unsigned, const std::set<std::string>&),
59 (const, override));
60 MOCK_METHOD(bool,
61 RemoveEmptyDirectory,
62 (const std::string&),
63 (const, override));
64 MOCK_METHOD(bool,
65 SetOwnership,
66 (const std::string&, uid_t, gid_t),
67 (const, override));
68 MOCK_METHOD(bool,
69 SetPermissions,
70 (const std::string&, mode_t),
71 (const, override));
Ben Chan8dcede82011-07-25 20:56:13 -070072};
73
74// A mock mount manager class for testing the mount manager base class.
75class MountManagerUnderTest : public MountManager {
76 public:
Sergei Datsenkoa910bba2019-06-18 13:31:59 +100077 MountManagerUnderTest(Platform* platform,
78 Metrics* metrics,
79 brillo::ProcessReaper* process_reaper)
80 : MountManager(kMountRootDirectory, platform, metrics, process_reaper) {}
Ben Chan8dcede82011-07-25 20:56:13 -070081
Anand K Mistrye9a60fb2019-11-07 16:41:03 +110082 ~MountManagerUnderTest() override { UnmountAll(); }
83
Ben Chan21150af2019-09-11 17:04:07 -070084 MOCK_METHOD(bool, CanMount, (const std::string&), (const, override));
85 MOCK_METHOD(MountSourceType, GetMountSourceType, (), (const, override));
86 MOCK_METHOD(MountErrorType,
87 DoMount,
88 (const std::string&,
89 const std::string&,
90 const std::vector<std::string>&,
91 const std::string&,
92 MountOptions*),
93 (override));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +110094 MOCK_METHOD(MountErrorType, DoUnmount, (const std::string&), (override));
Ben Chan21150af2019-09-11 17:04:07 -070095 MOCK_METHOD(bool,
96 ShouldReserveMountPathOnError,
97 (MountErrorType),
98 (const, override));
99 MOCK_METHOD(std::string,
100 SuggestMountPath,
101 (const std::string&),
102 (const, override));
Ben Chan8dcede82011-07-25 20:56:13 -0700103};
104
105class MountManagerTest : public ::testing::Test {
106 public:
Anand K Mistrye9a60fb2019-11-07 16:41:03 +1100107 MountManagerTest() : manager_(&platform_, &metrics_, &process_reaper_) {
108 ON_CALL(manager_, GetMountSourceType())
109 .WillByDefault(Return(MOUNT_SOURCE_REMOVABLE_DEVICE));
110 }
111
112 std::unique_ptr<MountPoint> MakeMountPoint(const std::string& mount_path) {
Anand K Mistryf1bd0862019-12-20 11:54:55 +1100113 return MountPoint::CreateLeaking(base::FilePath(mount_path));
Anand K Mistrye9a60fb2019-11-07 16:41:03 +1100114 }
Ben Chan8dcede82011-07-25 20:56:13 -0700115
116 protected:
Ben Chanbe2b4a72011-11-08 13:42:23 -0800117 Metrics metrics_;
Ben Chan8dcede82011-07-25 20:56:13 -0700118 MockPlatform platform_;
Sergei Datsenkoa910bba2019-06-18 13:31:59 +1000119 brillo::ProcessReaper process_reaper_;
Ben Chan8dcede82011-07-25 20:56:13 -0700120 MountManagerUnderTest manager_;
Ben Chan213c6d92019-04-10 16:21:52 -0700121 std::string filesystem_type_;
122 std::string mount_path_;
123 std::string source_path_;
124 std::vector<std::string> options_;
Ben Chan8dcede82011-07-25 20:56:13 -0700125};
126
Tatsuhisa Yamaguchi578b3bf2016-09-30 16:57:18 +0900127// Mock action to emulate DoMount with fallback to read-only mode.
Ben Chande0e3f62017-09-26 06:28:39 -0700128MountErrorType DoMountSuccessReadOnly(const std::string& source_path,
129 const std::string& filesystem_type,
130 const std::vector<std::string>& options,
131 const std::string& mount_path,
132 MountOptions* applied_options) {
Tatsuhisa Yamaguchi578b3bf2016-09-30 16:57:18 +0900133 applied_options->Initialize(options, false, "", "");
134 applied_options->SetReadOnlyOption();
135 return MOUNT_ERROR_NONE;
136}
137
138// Mock action to emulate DoMount successfully finished.
Ben Chande0e3f62017-09-26 06:28:39 -0700139MountErrorType DoMountSuccess(const std::string& source_path,
140 const std::string& filesystem_type,
141 const std::vector<std::string>& options,
142 const std::string& mount_path,
143 MountOptions* applied_options) {
Tatsuhisa Yamaguchi578b3bf2016-09-30 16:57:18 +0900144 applied_options->Initialize(options, false, "", "");
145 return MOUNT_ERROR_NONE;
146}
147
Ben Chan9ed09e32011-11-22 16:24:06 -0800148// Verifies that MountManager::Initialize() returns false when it fails to
149// create the mount root directory.
Ben Chan8dcede82011-07-25 20:56:13 -0700150TEST_F(MountManagerTest, InitializeFailedInCreateDirectory) {
151 EXPECT_CALL(platform_, CreateDirectory(kMountRootDirectory))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700152 .WillOnce(Return(false));
Ben Chande0e3f62017-09-26 06:28:39 -0700153 EXPECT_CALL(platform_, SetOwnership(kMountRootDirectory, getuid(), getgid()))
Ben Chan8dcede82011-07-25 20:56:13 -0700154 .Times(0);
Ben Chande0e3f62017-09-26 06:28:39 -0700155 EXPECT_CALL(platform_, SetPermissions(kMountRootDirectory, _)).Times(0);
Ben Chan8dcede82011-07-25 20:56:13 -0700156
157 EXPECT_FALSE(manager_.Initialize());
158}
159
Ben Chan9ed09e32011-11-22 16:24:06 -0800160// Verifies that MountManager::Initialize() returns false when it fails to
161// set the ownership of the created mount root directory.
Ben Chan8dcede82011-07-25 20:56:13 -0700162TEST_F(MountManagerTest, InitializeFailedInSetOwnership) {
163 EXPECT_CALL(platform_, CreateDirectory(kMountRootDirectory))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700164 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700165 EXPECT_CALL(platform_, SetOwnership(kMountRootDirectory, getuid(), getgid()))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700166 .WillOnce(Return(false));
Ben Chande0e3f62017-09-26 06:28:39 -0700167 EXPECT_CALL(platform_, SetPermissions(kMountRootDirectory, _)).Times(0);
Ben Chan8dcede82011-07-25 20:56:13 -0700168
169 EXPECT_FALSE(manager_.Initialize());
170}
171
Ben Chan9ed09e32011-11-22 16:24:06 -0800172// Verifies that MountManager::Initialize() returns false when it fails to
173// set the permissions of the created mount root directory.
Ben Chan8dcede82011-07-25 20:56:13 -0700174TEST_F(MountManagerTest, InitializeFailedInSetPermissions) {
175 EXPECT_CALL(platform_, CreateDirectory(kMountRootDirectory))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700176 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700177 EXPECT_CALL(platform_, SetOwnership(kMountRootDirectory, getuid(), getgid()))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700178 .WillOnce(Return(true));
Ben Chan8dcede82011-07-25 20:56:13 -0700179 EXPECT_CALL(platform_, SetPermissions(kMountRootDirectory, _))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700180 .WillOnce(Return(false));
Ben Chan8dcede82011-07-25 20:56:13 -0700181
182 EXPECT_FALSE(manager_.Initialize());
183}
184
Ben Chan9ed09e32011-11-22 16:24:06 -0800185// Verifies that MountManager::Initialize() returns true when it creates
186// the mount root directory with the specified ownership and permissions.
Ben Chan8dcede82011-07-25 20:56:13 -0700187TEST_F(MountManagerTest, InitializeSucceeded) {
188 EXPECT_CALL(platform_, CreateDirectory(kMountRootDirectory))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700189 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700190 EXPECT_CALL(platform_, SetOwnership(kMountRootDirectory, getuid(), getgid()))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700191 .WillOnce(Return(true));
Ben Chan8dcede82011-07-25 20:56:13 -0700192 EXPECT_CALL(platform_, SetPermissions(kMountRootDirectory, _))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700193 .WillOnce(Return(true));
Ben Chan8dcede82011-07-25 20:56:13 -0700194
195 EXPECT_TRUE(manager_.Initialize());
196}
197
Ben Chan9ed09e32011-11-22 16:24:06 -0800198// Verifies that MountManager::Mount() returns an error when it is invoked
199// to mount an empty source path.
Ben Chan8dcede82011-07-25 20:56:13 -0700200TEST_F(MountManagerTest, MountFailedWithEmptySourcePath) {
201 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700202 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan8dcede82011-07-25 20:56:13 -0700203 .Times(0);
204 EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900205 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100206 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chan8dcede82011-07-25 20:56:13 -0700207 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
208
Ben Chande0e3f62017-09-26 06:28:39 -0700209 EXPECT_EQ(
210 MOUNT_ERROR_INVALID_ARGUMENT,
211 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700212}
213
Ben Chan9ed09e32011-11-22 16:24:06 -0800214// Verifies that MountManager::Mount() returns an error when it is invoked
Ben Chan44e7ea62014-08-29 18:13:37 -0700215// with a nullptr mount path.
Ben Chan8dcede82011-07-25 20:56:13 -0700216TEST_F(MountManagerTest, MountFailedWithNullMountPath) {
Ben Chanadc5d002014-03-12 15:02:26 -0700217 source_path_ = kTestSourcePath;
Ben Chan8dcede82011-07-25 20:56:13 -0700218
219 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700220 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan8dcede82011-07-25 20:56:13 -0700221 .Times(0);
222 EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900223 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100224 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chan8dcede82011-07-25 20:56:13 -0700225 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
226
Ben Chanfcb2fc02011-11-21 09:44:07 -0800227 EXPECT_EQ(MOUNT_ERROR_INVALID_ARGUMENT,
Ben Chan44e7ea62014-08-29 18:13:37 -0700228 manager_.Mount(source_path_, filesystem_type_, options_, nullptr));
Ben Chan8dcede82011-07-25 20:56:13 -0700229}
230
Ben Chanadc5d002014-03-12 15:02:26 -0700231// Verifies that MountManager::Mount() returns an error when it is invoked
232// with an invalid mount path.
233TEST_F(MountManagerTest, MountFailedWithInvalidMountPath) {
234 source_path_ = kTestSourcePath;
235 mount_path_ = kInvalidMountPath;
236
237 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
238 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
239 .Times(0);
240 EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900241 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100242 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chanadc5d002014-03-12 15:02:26 -0700243 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
244
Ben Chande0e3f62017-09-26 06:28:39 -0700245 EXPECT_EQ(
246 MOUNT_ERROR_INVALID_PATH,
247 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700248}
249
250// Verifies that MountManager::Mount() returns an error when it is invoked
251// without a given mount path and the suggested mount path is invalid.
252TEST_F(MountManagerTest, MountFailedWithInvalidSuggestedMountPath) {
253 source_path_ = kTestSourcePath;
Ben Chan213c6d92019-04-10 16:21:52 -0700254 std::string suggested_mount_path = kInvalidMountPath;
Ben Chanadc5d002014-03-12 15:02:26 -0700255
256 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
257 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
258 .Times(0);
259 EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900260 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100261 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chanadc5d002014-03-12 15:02:26 -0700262 EXPECT_CALL(manager_, SuggestMountPath(_))
263 .WillRepeatedly(Return(suggested_mount_path));
264
Ben Chande0e3f62017-09-26 06:28:39 -0700265 EXPECT_EQ(
266 MOUNT_ERROR_INVALID_PATH,
267 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700268
269 options_.push_back("mountlabel=custom_label");
Ben Chande0e3f62017-09-26 06:28:39 -0700270 EXPECT_EQ(
271 MOUNT_ERROR_INVALID_PATH,
272 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700273}
274
275// Verifies that MountManager::Mount() returns an error when it is invoked
276// with an mount label that yields an invalid mount path.
277TEST_F(MountManagerTest, MountFailedWithInvalidMountLabel) {
278 source_path_ = kTestSourcePath;
Ben Chan213c6d92019-04-10 16:21:52 -0700279 std::string suggested_mount_path = kTestSourcePath;
Ben Chanadc5d002014-03-12 15:02:26 -0700280 options_.push_back("mountlabel=../custom_label");
281
282 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
283 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
284 .Times(0);
285 EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900286 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100287 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chanadc5d002014-03-12 15:02:26 -0700288 EXPECT_CALL(manager_, SuggestMountPath(_))
289 .WillOnce(Return(suggested_mount_path));
290
Ben Chande0e3f62017-09-26 06:28:39 -0700291 EXPECT_EQ(
292 MOUNT_ERROR_INVALID_PATH,
293 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700294}
295
Ben Chan9ed09e32011-11-22 16:24:06 -0800296// Verifies that MountManager::Mount() returns an error when it fails to
297// create the specified mount directory.
Ben Chan8dcede82011-07-25 20:56:13 -0700298TEST_F(MountManagerTest, MountFailedInCreateOrReuseEmptyDirectory) {
Ben Chanadc5d002014-03-12 15:02:26 -0700299 source_path_ = kTestSourcePath;
300 mount_path_ = kTestMountPath;
Ben Chan8dcede82011-07-25 20:56:13 -0700301
302 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700303 .WillOnce(Return(false));
Ben Chanf8692882011-08-21 10:15:30 -0700304 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan8dcede82011-07-25 20:56:13 -0700305 .Times(0);
306 EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900307 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100308 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chan8dcede82011-07-25 20:56:13 -0700309 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
310
Ben Chande0e3f62017-09-26 06:28:39 -0700311 EXPECT_EQ(
312 MOUNT_ERROR_DIRECTORY_CREATION_FAILED,
313 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700314 EXPECT_EQ(kTestMountPath, mount_path_);
Ben Chan8dcede82011-07-25 20:56:13 -0700315 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
316}
317
Ben Chan9ed09e32011-11-22 16:24:06 -0800318// Verifies that MountManager::Mount() returns an error when it fails to
319// create a specified but already reserved mount directory.
Ben Chanf8692882011-08-21 10:15:30 -0700320TEST_F(MountManagerTest, MountFailedInCreateDirectoryDueToReservedMountPath) {
Ben Chanadc5d002014-03-12 15:02:26 -0700321 source_path_ = kTestSourcePath;
322 mount_path_ = kTestMountPath;
Ben Chanf8692882011-08-21 10:15:30 -0700323
324 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_)).Times(0);
325 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
326 .Times(0);
327 EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900328 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100329 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700330 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
331
Ben Chanfcb2fc02011-11-21 09:44:07 -0800332 manager_.ReserveMountPath(mount_path_, MOUNT_ERROR_UNKNOWN_FILESYSTEM);
Ben Chanf8692882011-08-21 10:15:30 -0700333 EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_));
Ben Chanfcb2fc02011-11-21 09:44:07 -0800334 EXPECT_EQ(MOUNT_ERROR_UNKNOWN_FILESYSTEM,
Ben Chan632c9f82011-10-11 12:22:16 -0700335 manager_.GetMountErrorOfReservedMountPath(mount_path_));
Ben Chande0e3f62017-09-26 06:28:39 -0700336 EXPECT_EQ(
337 MOUNT_ERROR_DIRECTORY_CREATION_FAILED,
338 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700339 EXPECT_EQ(kTestMountPath, mount_path_);
Ben Chanf8692882011-08-21 10:15:30 -0700340 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
341 EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_));
Ben Chanfcb2fc02011-11-21 09:44:07 -0800342 EXPECT_EQ(MOUNT_ERROR_UNKNOWN_FILESYSTEM,
Ben Chan632c9f82011-10-11 12:22:16 -0700343 manager_.GetMountErrorOfReservedMountPath(mount_path_));
Ben Chanf8692882011-08-21 10:15:30 -0700344}
345
Ben Chan9ed09e32011-11-22 16:24:06 -0800346// Verifies that MountManager::Mount() returns an error when it fails to
347// create a mount directory after a number of trials.
Ben Chan8dcede82011-07-25 20:56:13 -0700348TEST_F(MountManagerTest, MountFailedInCreateOrReuseEmptyDirectoryWithFallback) {
Ben Chanadc5d002014-03-12 15:02:26 -0700349 source_path_ = kTestSourcePath;
Ben Chan213c6d92019-04-10 16:21:52 -0700350 std::string suggested_mount_path = kTestMountPath;
Ben Chan8dcede82011-07-25 20:56:13 -0700351
352 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700353 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700354 .WillOnce(Return(false));
Ben Chan8dcede82011-07-25 20:56:13 -0700355 EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900356 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100357 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chan8dcede82011-07-25 20:56:13 -0700358 EXPECT_CALL(manager_, SuggestMountPath(source_path_))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700359 .WillOnce(Return(suggested_mount_path));
Ben Chan8dcede82011-07-25 20:56:13 -0700360
Ben Chande0e3f62017-09-26 06:28:39 -0700361 EXPECT_EQ(
362 MOUNT_ERROR_DIRECTORY_CREATION_FAILED,
363 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700364 EXPECT_EQ("", mount_path_);
365 EXPECT_FALSE(manager_.IsMountPathInCache(suggested_mount_path));
366}
367
Ben Chan9ed09e32011-11-22 16:24:06 -0800368// Verifies that MountManager::Mount() returns an error when it fails to
369// set the ownership of the created mount directory.
Ben Chan1d5a8e72011-08-01 15:21:39 -0700370TEST_F(MountManagerTest, MountFailedInSetOwnership) {
Ben Chanadc5d002014-03-12 15:02:26 -0700371 source_path_ = kTestSourcePath;
372 mount_path_ = kTestMountPath;
Ben Chan1d5a8e72011-08-01 15:21:39 -0700373
374 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
375 .WillOnce(Return(true));
Ben Chanf8692882011-08-21 10:15:30 -0700376 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700377 .Times(0);
Ben Chan1d5a8e72011-08-01 15:21:39 -0700378 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
379 .WillOnce(Return(false));
380 EXPECT_CALL(platform_, SetPermissions(_, _)).Times(0);
381 EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_))
382 .WillOnce(Return(true));
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900383 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100384 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chan1d5a8e72011-08-01 15:21:39 -0700385 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
386
Ben Chande0e3f62017-09-26 06:28:39 -0700387 EXPECT_EQ(
388 MOUNT_ERROR_DIRECTORY_CREATION_FAILED,
389 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700390 EXPECT_EQ(kTestMountPath, mount_path_);
Ben Chan1d5a8e72011-08-01 15:21:39 -0700391 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
392}
393
Ben Chan9ed09e32011-11-22 16:24:06 -0800394// Verifies that MountManager::Mount() returns an error when it fails to
395// set the permissions of the created mount directory.
Ben Chan1d5a8e72011-08-01 15:21:39 -0700396TEST_F(MountManagerTest, MountFailedInSetPermissions) {
Ben Chanadc5d002014-03-12 15:02:26 -0700397 source_path_ = kTestSourcePath;
398 mount_path_ = kTestMountPath;
Ben Chan1d5a8e72011-08-01 15:21:39 -0700399
400 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
401 .WillOnce(Return(true));
Ben Chanf8692882011-08-21 10:15:30 -0700402 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700403 .Times(0);
Ben Chan1d5a8e72011-08-01 15:21:39 -0700404 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
405 .WillOnce(Return(true));
406 EXPECT_CALL(platform_, SetPermissions(mount_path_, _))
407 .WillOnce(Return(false));
408 EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_))
409 .WillOnce(Return(true));
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900410 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100411 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chan1d5a8e72011-08-01 15:21:39 -0700412 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
413
Ben Chande0e3f62017-09-26 06:28:39 -0700414 EXPECT_EQ(
415 MOUNT_ERROR_DIRECTORY_CREATION_FAILED,
416 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700417 EXPECT_EQ(kTestMountPath, mount_path_);
Ben Chan1d5a8e72011-08-01 15:21:39 -0700418 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
419}
420
Ben Chan9ed09e32011-11-22 16:24:06 -0800421// Verifies that MountManager::Mount() returns no error when it successfully
Tatsuhisa Yamaguchi578b3bf2016-09-30 16:57:18 +0900422// mounts a source path to a specified mount path in read-write mode.
Ben Chan8dcede82011-07-25 20:56:13 -0700423TEST_F(MountManagerTest, MountSucceededWithGivenMountPath) {
Ben Chanadc5d002014-03-12 15:02:26 -0700424 source_path_ = kTestSourcePath;
425 mount_path_ = kTestMountPath;
Ben Chan8dcede82011-07-25 20:56:13 -0700426
Tatsuhisa Yamaguchi578b3bf2016-09-30 16:57:18 +0900427 options_.push_back(MountOptions::kOptionReadWrite);
Ben Chan8dcede82011-07-25 20:56:13 -0700428 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700429 .WillOnce(Return(true));
Ben Chanf8692882011-08-21 10:15:30 -0700430 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan8dcede82011-07-25 20:56:13 -0700431 .Times(0);
Ben Chan1d5a8e72011-08-01 15:21:39 -0700432 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
433 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700434 EXPECT_CALL(platform_, SetPermissions(mount_path_, _)).WillOnce(Return(true));
Ben Chan8dcede82011-07-25 20:56:13 -0700435 EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700436 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700437 EXPECT_CALL(manager_,
438 DoMount(source_path_, filesystem_type_, options_, mount_path_, _))
Tatsuhisa Yamaguchi578b3bf2016-09-30 16:57:18 +0900439 .WillOnce(Invoke(DoMountSuccess));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100440 EXPECT_CALL(manager_, DoUnmount(mount_path_))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800441 .WillOnce(Return(MOUNT_ERROR_NONE));
Ben Chan8dcede82011-07-25 20:56:13 -0700442 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
443
Ben Chande0e3f62017-09-26 06:28:39 -0700444 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_,
445 options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700446 EXPECT_EQ(kTestMountPath, mount_path_);
Ben Chan8dcede82011-07-25 20:56:13 -0700447 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900448
Anand K Mistrye9a60fb2019-11-07 16:41:03 +1100449 base::Optional<MountEntry> mount_entry;
450 mount_entry = manager_.GetMountEntryForTest(source_path_);
451 EXPECT_TRUE(mount_entry);
452 EXPECT_FALSE(mount_entry->is_read_only);
George Burgess IV3d44fa82016-09-07 23:47:21 -0700453
454 EXPECT_TRUE(manager_.UnmountAll());
455 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900456}
457
Tatsuhisa Yamaguchi578b3bf2016-09-30 16:57:18 +0900458// Verifies that MountManager::Mount() stores correct mount status in cache when
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900459// read-only option is specified.
460TEST_F(MountManagerTest, MountCachesStatusWithReadOnlyOption) {
461 source_path_ = kTestSourcePath;
462 mount_path_ = kTestMountPath;
463
464 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
465 .WillOnce(Return(true));
466 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
467 .Times(0);
468 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
469 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700470 EXPECT_CALL(platform_, SetPermissions(mount_path_, _)).WillOnce(Return(true));
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900471 // Add read-only mount option.
Tatsuhisa Yamaguchi578b3bf2016-09-30 16:57:18 +0900472 options_.push_back(MountOptions::kOptionReadOnly);
Ben Chande0e3f62017-09-26 06:28:39 -0700473 EXPECT_CALL(manager_,
474 DoMount(source_path_, filesystem_type_, options_, mount_path_, _))
Tatsuhisa Yamaguchi578b3bf2016-09-30 16:57:18 +0900475 .WillOnce(Invoke(DoMountSuccess));
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900476 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
477
Ben Chande0e3f62017-09-26 06:28:39 -0700478 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_,
479 options_, &mount_path_));
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900480 EXPECT_EQ(kTestMountPath, mount_path_);
481 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
482
Anand K Mistrye9a60fb2019-11-07 16:41:03 +1100483 base::Optional<MountEntry> mount_entry;
484 mount_entry = manager_.GetMountEntryForTest(source_path_);
485 EXPECT_TRUE(mount_entry);
486 EXPECT_TRUE(mount_entry->is_read_only);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900487}
488
Tatsuhisa Yamaguchi578b3bf2016-09-30 16:57:18 +0900489// Verifies that MountManager::Mount() stores correct mount status in cache when
490// the mounter requested to mount in read-write mode but fell back to read-only
491// mode.
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900492TEST_F(MountManagerTest, MountSuccededWithReadOnlyFallback) {
493 source_path_ = kTestSourcePath;
494 mount_path_ = kTestMountPath;
495
496 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
497 .WillOnce(Return(true));
498 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
499 .Times(0);
500 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
501 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700502 EXPECT_CALL(platform_, SetPermissions(mount_path_, _)).WillOnce(Return(true));
Tatsuhisa Yamaguchi578b3bf2016-09-30 16:57:18 +0900503 options_.push_back(MountOptions::kOptionReadWrite);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900504 // Emulate Mounter added read-only option as a fallback.
Ben Chande0e3f62017-09-26 06:28:39 -0700505 EXPECT_CALL(manager_,
506 DoMount(source_path_, filesystem_type_, options_, mount_path_, _))
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900507 .WillOnce(Invoke(DoMountSuccessReadOnly));
508 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
509
Ben Chande0e3f62017-09-26 06:28:39 -0700510 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_,
511 options_, &mount_path_));
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900512 EXPECT_EQ(kTestMountPath, mount_path_);
513 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
514
Anand K Mistrye9a60fb2019-11-07 16:41:03 +1100515 base::Optional<MountEntry> mount_entry;
516 mount_entry = manager_.GetMountEntryForTest(source_path_);
517 EXPECT_TRUE(mount_entry);
518 EXPECT_TRUE(mount_entry->is_read_only);
Ben Chan8dcede82011-07-25 20:56:13 -0700519}
520
Ben Chan9ed09e32011-11-22 16:24:06 -0800521// Verifies that MountManager::Mount() returns no error when it successfully
522// mounts a source path with no mount path specified.
Ben Chan8dcede82011-07-25 20:56:13 -0700523TEST_F(MountManagerTest, MountSucceededWithEmptyMountPath) {
Ben Chanadc5d002014-03-12 15:02:26 -0700524 source_path_ = kTestSourcePath;
Ben Chan213c6d92019-04-10 16:21:52 -0700525 std::string suggested_mount_path = kTestMountPath;
Ben Chan8dcede82011-07-25 20:56:13 -0700526
527 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700528 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700529 .WillOnce(Return(true));
Ben Chan1d5a8e72011-08-01 15:21:39 -0700530 EXPECT_CALL(platform_, SetOwnership(suggested_mount_path, _, _))
531 .WillOnce(Return(true));
532 EXPECT_CALL(platform_, SetPermissions(suggested_mount_path, _))
533 .WillOnce(Return(true));
Ben Chan8dcede82011-07-25 20:56:13 -0700534 EXPECT_CALL(platform_, RemoveEmptyDirectory(suggested_mount_path))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700535 .WillOnce(Return(true));
Ben Chan8dcede82011-07-25 20:56:13 -0700536 EXPECT_CALL(manager_, DoMount(source_path_, filesystem_type_, options_,
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900537 suggested_mount_path, _))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800538 .WillOnce(Return(MOUNT_ERROR_NONE));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100539 EXPECT_CALL(manager_, DoUnmount(suggested_mount_path))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800540 .WillOnce(Return(MOUNT_ERROR_NONE));
Ben Chan8dcede82011-07-25 20:56:13 -0700541 EXPECT_CALL(manager_, SuggestMountPath(source_path_))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700542 .WillOnce(Return(suggested_mount_path));
Ben Chan8dcede82011-07-25 20:56:13 -0700543
Ben Chande0e3f62017-09-26 06:28:39 -0700544 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_,
545 options_, &mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700546 EXPECT_EQ(suggested_mount_path, mount_path_);
547 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
548 EXPECT_TRUE(manager_.UnmountAll());
Ben Chanf8692882011-08-21 10:15:30 -0700549 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700550}
551
Ben Chan09c90d02012-04-25 22:09:09 -0700552// Verifies that MountManager::Mount() returns no error when it successfully
553// mounts a source path with a given mount label in options.
554TEST_F(MountManagerTest, MountSucceededWithGivenMountLabel) {
Ben Chanadc5d002014-03-12 15:02:26 -0700555 source_path_ = kTestSourcePath;
Ben Chan213c6d92019-04-10 16:21:52 -0700556 std::string suggested_mount_path = kTestMountPath;
557 std::string final_mount_path =
558 std::string(kMountRootDirectory) + "/custom_label";
Ben Chan09c90d02012-04-25 22:09:09 -0700559 options_.push_back("mountlabel=custom_label");
Ben Chan213c6d92019-04-10 16:21:52 -0700560 std::vector<std::string> updated_options;
Ben Chan09c90d02012-04-25 22:09:09 -0700561
562 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
563 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
564 .WillOnce(Return(true));
565 EXPECT_CALL(platform_, SetOwnership(final_mount_path, _, _))
566 .WillOnce(Return(true));
567 EXPECT_CALL(platform_, SetPermissions(final_mount_path, _))
568 .WillOnce(Return(true));
569 EXPECT_CALL(platform_, RemoveEmptyDirectory(final_mount_path))
570 .WillOnce(Return(true));
571 EXPECT_CALL(manager_, DoMount(source_path_, filesystem_type_, updated_options,
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900572 final_mount_path, _))
Ben Chan09c90d02012-04-25 22:09:09 -0700573 .WillOnce(Return(MOUNT_ERROR_NONE));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100574 EXPECT_CALL(manager_, DoUnmount(final_mount_path))
Ben Chan09c90d02012-04-25 22:09:09 -0700575 .WillOnce(Return(MOUNT_ERROR_NONE));
576 EXPECT_CALL(manager_, SuggestMountPath(source_path_))
577 .WillOnce(Return(suggested_mount_path));
578
Ben Chande0e3f62017-09-26 06:28:39 -0700579 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_,
580 options_, &mount_path_));
Ben Chan09c90d02012-04-25 22:09:09 -0700581 EXPECT_EQ(final_mount_path, mount_path_);
582 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
583 EXPECT_TRUE(manager_.UnmountAll());
584 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
585}
586
Ben Chan9ed09e32011-11-22 16:24:06 -0800587// Verifies that MountManager::Mount() handles the mounting of an already
588// mounted source path properly.
Ben Chan8dcede82011-07-25 20:56:13 -0700589TEST_F(MountManagerTest, MountWithAlreadyMountedSourcePath) {
Ben Chanadc5d002014-03-12 15:02:26 -0700590 source_path_ = kTestSourcePath;
Ben Chan213c6d92019-04-10 16:21:52 -0700591 std::string suggested_mount_path = kTestMountPath;
Ben Chan8dcede82011-07-25 20:56:13 -0700592
593 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700594 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700595 .WillOnce(Return(true));
Ben Chan1d5a8e72011-08-01 15:21:39 -0700596 EXPECT_CALL(platform_, SetOwnership(suggested_mount_path, _, _))
597 .WillOnce(Return(true));
598 EXPECT_CALL(platform_, SetPermissions(suggested_mount_path, _))
599 .WillOnce(Return(true));
Ben Chan8dcede82011-07-25 20:56:13 -0700600 EXPECT_CALL(platform_, RemoveEmptyDirectory(suggested_mount_path))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700601 .WillOnce(Return(true));
Ben Chan8dcede82011-07-25 20:56:13 -0700602 EXPECT_CALL(manager_, DoMount(source_path_, filesystem_type_, options_,
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900603 suggested_mount_path, _))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800604 .WillOnce(Return(MOUNT_ERROR_NONE));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100605 EXPECT_CALL(manager_, DoUnmount(suggested_mount_path))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800606 .WillOnce(Return(MOUNT_ERROR_NONE));
Ben Chan8dcede82011-07-25 20:56:13 -0700607 EXPECT_CALL(manager_, SuggestMountPath(source_path_))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700608 .WillOnce(Return(suggested_mount_path));
Ben Chan8dcede82011-07-25 20:56:13 -0700609
Ben Chande0e3f62017-09-26 06:28:39 -0700610 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_,
611 options_, &mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700612 EXPECT_EQ(suggested_mount_path, mount_path_);
613 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
614
615 // Mount an already-mounted source path without specifying a mount path
616 mount_path_.clear();
Ben Chande0e3f62017-09-26 06:28:39 -0700617 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_,
618 options_, &mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700619 EXPECT_EQ(suggested_mount_path, mount_path_);
620 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
621
622 // Mount an already-mounted source path to the same mount path
623 mount_path_ = suggested_mount_path;
Ben Chande0e3f62017-09-26 06:28:39 -0700624 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_,
625 options_, &mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700626 EXPECT_EQ(suggested_mount_path, mount_path_);
627 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
628
629 // Mount an already-mounted source path to a different mount path
630 mount_path_ = "another-path";
Ben Chande0e3f62017-09-26 06:28:39 -0700631 EXPECT_EQ(
632 MOUNT_ERROR_PATH_ALREADY_MOUNTED,
633 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700634 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
635 EXPECT_TRUE(manager_.IsMountPathInCache(suggested_mount_path));
636
637 EXPECT_TRUE(manager_.UnmountAll());
Ben Chanf8692882011-08-21 10:15:30 -0700638 EXPECT_FALSE(manager_.IsMountPathReserved(suggested_mount_path));
639}
640
Ben Chan9ed09e32011-11-22 16:24:06 -0800641// Verifies that MountManager::Mount() successfully reserves a path for a given
642// type of error. A specific mount path is given in this case.
Ben Chanf8692882011-08-21 10:15:30 -0700643TEST_F(MountManagerTest, MountSucceededWithGivenMountPathInReservedCase) {
Ben Chanadc5d002014-03-12 15:02:26 -0700644 source_path_ = kTestSourcePath;
645 mount_path_ = kTestMountPath;
Ben Chanf8692882011-08-21 10:15:30 -0700646
647 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
648 .WillOnce(Return(true));
649 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
650 .Times(0);
651 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
652 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700653 EXPECT_CALL(platform_, SetPermissions(mount_path_, _)).WillOnce(Return(true));
Ben Chanf8692882011-08-21 10:15:30 -0700654 EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_))
655 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700656 EXPECT_CALL(manager_,
657 DoMount(source_path_, filesystem_type_, options_, mount_path_, _))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800658 .WillOnce(Return(MOUNT_ERROR_UNKNOWN_FILESYSTEM));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100659 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700660 EXPECT_CALL(manager_,
Ben Chanfcb2fc02011-11-21 09:44:07 -0800661 ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM))
Ben Chanf8692882011-08-21 10:15:30 -0700662 .WillOnce(Return(true));
663 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
664
Ben Chande0e3f62017-09-26 06:28:39 -0700665 EXPECT_EQ(
666 MOUNT_ERROR_UNKNOWN_FILESYSTEM,
667 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700668 EXPECT_EQ(kTestMountPath, mount_path_);
Ben Chanf8692882011-08-21 10:15:30 -0700669 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
670 EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_));
671 EXPECT_TRUE(manager_.UnmountAll());
672 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
673 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
674}
675
Ben Chan9ed09e32011-11-22 16:24:06 -0800676// Verifies that MountManager::Mount() successfully reserves a path for a given
677// type of error. No specific mount path is given in this case.
Ben Chanf8692882011-08-21 10:15:30 -0700678TEST_F(MountManagerTest, MountSucceededWithEmptyMountPathInReservedCase) {
Ben Chanadc5d002014-03-12 15:02:26 -0700679 source_path_ = kTestSourcePath;
Ben Chan213c6d92019-04-10 16:21:52 -0700680 std::string suggested_mount_path = kTestMountPath;
Ben Chanf8692882011-08-21 10:15:30 -0700681
682 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
683 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
684 .WillOnce(Return(true));
685 EXPECT_CALL(platform_, SetOwnership(suggested_mount_path, _, _))
686 .WillOnce(Return(true));
687 EXPECT_CALL(platform_, SetPermissions(suggested_mount_path, _))
688 .WillOnce(Return(true));
689 EXPECT_CALL(platform_, RemoveEmptyDirectory(suggested_mount_path))
690 .WillOnce(Return(true));
691 EXPECT_CALL(manager_, DoMount(source_path_, filesystem_type_, options_,
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900692 suggested_mount_path, _))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800693 .WillOnce(Return(MOUNT_ERROR_UNKNOWN_FILESYSTEM));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100694 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700695 EXPECT_CALL(manager_,
Ben Chanfcb2fc02011-11-21 09:44:07 -0800696 ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM))
Ben Chanf8692882011-08-21 10:15:30 -0700697 .WillOnce(Return(true));
698 EXPECT_CALL(manager_, SuggestMountPath(source_path_))
699 .WillOnce(Return(suggested_mount_path));
700
Ben Chande0e3f62017-09-26 06:28:39 -0700701 EXPECT_EQ(
702 MOUNT_ERROR_UNKNOWN_FILESYSTEM,
703 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanf8692882011-08-21 10:15:30 -0700704 EXPECT_EQ(suggested_mount_path, mount_path_);
705 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
706 EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_));
707 EXPECT_TRUE(manager_.UnmountAll());
708 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
709 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
710}
711
Ben Chan9ed09e32011-11-22 16:24:06 -0800712// Verifies that MountManager::Mount() successfully reserves a path for a given
713// type of error and returns the same error when it tries to mount the same path
714// again.
Ben Chan632c9f82011-10-11 12:22:16 -0700715TEST_F(MountManagerTest, MountSucceededWithAlreadyReservedMountPath) {
Ben Chanadc5d002014-03-12 15:02:26 -0700716 source_path_ = kTestSourcePath;
Ben Chan213c6d92019-04-10 16:21:52 -0700717 std::string suggested_mount_path = kTestMountPath;
Ben Chan632c9f82011-10-11 12:22:16 -0700718
719 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
720 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
721 .WillOnce(Return(true));
722 EXPECT_CALL(platform_, SetOwnership(suggested_mount_path, _, _))
723 .WillOnce(Return(true));
724 EXPECT_CALL(platform_, SetPermissions(suggested_mount_path, _))
725 .WillOnce(Return(true));
726 EXPECT_CALL(platform_, RemoveEmptyDirectory(suggested_mount_path))
727 .WillOnce(Return(true));
728 EXPECT_CALL(manager_, DoMount(source_path_, filesystem_type_, options_,
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900729 suggested_mount_path, _))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800730 .WillOnce(Return(MOUNT_ERROR_UNKNOWN_FILESYSTEM));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100731 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chan632c9f82011-10-11 12:22:16 -0700732 EXPECT_CALL(manager_,
Ben Chanfcb2fc02011-11-21 09:44:07 -0800733 ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM))
Ben Chan632c9f82011-10-11 12:22:16 -0700734 .WillOnce(Return(true));
735 EXPECT_CALL(manager_, SuggestMountPath(source_path_))
736 .WillOnce(Return(suggested_mount_path));
737
Ben Chande0e3f62017-09-26 06:28:39 -0700738 EXPECT_EQ(
739 MOUNT_ERROR_UNKNOWN_FILESYSTEM,
740 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chan632c9f82011-10-11 12:22:16 -0700741 EXPECT_EQ(suggested_mount_path, mount_path_);
742 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
743 EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_));
744
745 mount_path_ = "";
Ben Chande0e3f62017-09-26 06:28:39 -0700746 EXPECT_EQ(
747 MOUNT_ERROR_UNKNOWN_FILESYSTEM,
748 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chan632c9f82011-10-11 12:22:16 -0700749 EXPECT_EQ(suggested_mount_path, mount_path_);
750 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
751 EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_));
752
753 EXPECT_TRUE(manager_.UnmountAll());
754 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
755 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
756}
757
Ben Chan9ed09e32011-11-22 16:24:06 -0800758// Verifies that MountManager::Mount() successfully reserves a path for a given
759// type of error and returns the same error when it tries to mount the same path
760// again.
Ben Chanf8692882011-08-21 10:15:30 -0700761TEST_F(MountManagerTest, MountFailedWithGivenMountPathInReservedCase) {
Ben Chanadc5d002014-03-12 15:02:26 -0700762 source_path_ = kTestSourcePath;
763 mount_path_ = kTestMountPath;
Ben Chanf8692882011-08-21 10:15:30 -0700764
765 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
766 .WillOnce(Return(true));
767 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
768 .Times(0);
769 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
770 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700771 EXPECT_CALL(platform_, SetPermissions(mount_path_, _)).WillOnce(Return(true));
Ben Chanf8692882011-08-21 10:15:30 -0700772 EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_))
773 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700774 EXPECT_CALL(manager_,
775 DoMount(source_path_, filesystem_type_, options_, mount_path_, _))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800776 .WillOnce(Return(MOUNT_ERROR_UNKNOWN_FILESYSTEM));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100777 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700778 EXPECT_CALL(manager_,
Ben Chanfcb2fc02011-11-21 09:44:07 -0800779 ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM))
Ben Chanf8692882011-08-21 10:15:30 -0700780 .WillOnce(Return(false));
781 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
782
Ben Chande0e3f62017-09-26 06:28:39 -0700783 EXPECT_EQ(
784 MOUNT_ERROR_UNKNOWN_FILESYSTEM,
785 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700786 EXPECT_EQ(kTestMountPath, mount_path_);
Ben Chanf8692882011-08-21 10:15:30 -0700787 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
788 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
789}
790
Ben Chan9ed09e32011-11-22 16:24:06 -0800791// Verifies that MountManager::Mount() fails to mount or reserve a path for
792// a type of error that is not enabled for reservation.
Ben Chanf8692882011-08-21 10:15:30 -0700793TEST_F(MountManagerTest, MountFailedWithEmptyMountPathInReservedCase) {
Ben Chanadc5d002014-03-12 15:02:26 -0700794 source_path_ = kTestSourcePath;
Ben Chan213c6d92019-04-10 16:21:52 -0700795 std::string suggested_mount_path = kTestMountPath;
Ben Chanf8692882011-08-21 10:15:30 -0700796
797 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
798 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
799 .WillOnce(Return(true));
800 EXPECT_CALL(platform_, SetOwnership(suggested_mount_path, _, _))
801 .WillOnce(Return(true));
802 EXPECT_CALL(platform_, SetPermissions(suggested_mount_path, _))
803 .WillOnce(Return(true));
804 EXPECT_CALL(platform_, RemoveEmptyDirectory(suggested_mount_path))
805 .WillOnce(Return(true));
806 EXPECT_CALL(manager_, DoMount(source_path_, filesystem_type_, options_,
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900807 suggested_mount_path, _))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800808 .WillOnce(Return(MOUNT_ERROR_UNKNOWN_FILESYSTEM));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100809 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700810 EXPECT_CALL(manager_,
Ben Chanfcb2fc02011-11-21 09:44:07 -0800811 ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM))
Ben Chanf8692882011-08-21 10:15:30 -0700812 .WillOnce(Return(false));
813 EXPECT_CALL(manager_, SuggestMountPath(source_path_))
814 .WillOnce(Return(suggested_mount_path));
815
Ben Chande0e3f62017-09-26 06:28:39 -0700816 EXPECT_EQ(
817 MOUNT_ERROR_UNKNOWN_FILESYSTEM,
818 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanf8692882011-08-21 10:15:30 -0700819 EXPECT_EQ("", mount_path_);
820 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
821 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700822}
823
Ben Chan9ed09e32011-11-22 16:24:06 -0800824// Verifies that MountManager::Unmount() returns an error when it is invoked
825// to unmount an empty path.
Ben Chan8dcede82011-07-25 20:56:13 -0700826TEST_F(MountManagerTest, UnmountFailedWithEmptyPath) {
827 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700828 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan8dcede82011-07-25 20:56:13 -0700829 .Times(0);
830 EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900831 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100832 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chan8dcede82011-07-25 20:56:13 -0700833 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
834
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100835 EXPECT_EQ(MOUNT_ERROR_INVALID_ARGUMENT, manager_.Unmount(mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700836}
837
Ben Chan9ed09e32011-11-22 16:24:06 -0800838// Verifies that MountManager::Unmount() returns an error when it fails to
839// unmount a path that is not mounted.
Ben Chan8dcede82011-07-25 20:56:13 -0700840TEST_F(MountManagerTest, UnmountFailedWithPathNotMounted) {
841 mount_path_ = "nonexistent-path";
842
843 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700844 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan8dcede82011-07-25 20:56:13 -0700845 .Times(0);
846 EXPECT_CALL(platform_, RemoveEmptyDirectory(_)).Times(0);
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +0900847 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100848 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Ben Chan8dcede82011-07-25 20:56:13 -0700849 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
850
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100851 EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED, manager_.Unmount(mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700852}
853
Ben Chan9ed09e32011-11-22 16:24:06 -0800854// Verifies that MountManager::Unmount() returns no error when it successfully
855// unmounts a source path.
Ben Chan8dcede82011-07-25 20:56:13 -0700856TEST_F(MountManagerTest, UnmountSucceededWithGivenSourcePath) {
Ben Chanadc5d002014-03-12 15:02:26 -0700857 source_path_ = kTestSourcePath;
858 mount_path_ = kTestMountPath;
Ben Chan8dcede82011-07-25 20:56:13 -0700859
860 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700861 .WillOnce(Return(true));
Ben Chanf8692882011-08-21 10:15:30 -0700862 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan8dcede82011-07-25 20:56:13 -0700863 .Times(0);
Ben Chan1d5a8e72011-08-01 15:21:39 -0700864 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
865 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700866 EXPECT_CALL(platform_, SetPermissions(mount_path_, _)).WillOnce(Return(true));
Ben Chanf8692882011-08-21 10:15:30 -0700867 EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700868 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700869 EXPECT_CALL(manager_,
870 DoMount(source_path_, filesystem_type_, options_, mount_path_, _))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800871 .WillOnce(Return(MOUNT_ERROR_NONE));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100872 EXPECT_CALL(manager_, DoUnmount(mount_path_))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800873 .WillOnce(Return(MOUNT_ERROR_NONE));
Ben Chan8dcede82011-07-25 20:56:13 -0700874 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
875
Ben Chande0e3f62017-09-26 06:28:39 -0700876 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_,
877 options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700878 EXPECT_EQ(kTestMountPath, mount_path_);
Ben Chan8dcede82011-07-25 20:56:13 -0700879 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
880
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100881 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Unmount(source_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700882 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
883}
884
Ben Chan9ed09e32011-11-22 16:24:06 -0800885// Verifies that MountManager::Unmount() returns no error when it successfully
886// unmounts a mount path.
Ben Chan8dcede82011-07-25 20:56:13 -0700887TEST_F(MountManagerTest, UnmountSucceededWithGivenMountPath) {
Ben Chanadc5d002014-03-12 15:02:26 -0700888 source_path_ = kTestSourcePath;
889 mount_path_ = kTestMountPath;
Ben Chan8dcede82011-07-25 20:56:13 -0700890
891 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700892 .WillOnce(Return(true));
Ben Chanf8692882011-08-21 10:15:30 -0700893 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
Ben Chan8dcede82011-07-25 20:56:13 -0700894 .Times(0);
Ben Chan1d5a8e72011-08-01 15:21:39 -0700895 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
896 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700897 EXPECT_CALL(platform_, SetPermissions(mount_path_, _)).WillOnce(Return(true));
Ben Chanf8692882011-08-21 10:15:30 -0700898 EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_))
Ben Chan1d5a8e72011-08-01 15:21:39 -0700899 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700900 EXPECT_CALL(manager_,
901 DoMount(source_path_, filesystem_type_, options_, mount_path_, _))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800902 .WillOnce(Return(MOUNT_ERROR_NONE));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100903 EXPECT_CALL(manager_, DoUnmount(mount_path_))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800904 .WillOnce(Return(MOUNT_ERROR_NONE));
Ben Chan8dcede82011-07-25 20:56:13 -0700905 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
906
Ben Chande0e3f62017-09-26 06:28:39 -0700907 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_,
908 options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700909 EXPECT_EQ(kTestMountPath, mount_path_);
Ben Chan8dcede82011-07-25 20:56:13 -0700910 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
911
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100912 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Unmount(mount_path_));
Ben Chan8dcede82011-07-25 20:56:13 -0700913 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
914}
915
Sergei Datsenko2879b5c2019-09-26 13:47:40 +1000916// Verifies that MountManager::Unmount() removes mount path from cache if
917// it appears to be not mounted.
918TEST_F(MountManagerTest, UnmountRemovesFromCacheIfNotMounted) {
919 source_path_ = kTestSourcePath;
920 mount_path_ = kTestMountPath;
921
922 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
923 .WillOnce(Return(true));
924 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
925 .Times(0);
926 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
927 .WillOnce(Return(true));
928 EXPECT_CALL(platform_, SetPermissions(mount_path_, _)).WillOnce(Return(true));
929 EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_))
930 .WillOnce(Return(true));
931 EXPECT_CALL(manager_,
932 DoMount(source_path_, filesystem_type_, options_, mount_path_, _))
933 .WillOnce(Return(MOUNT_ERROR_NONE));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100934 EXPECT_CALL(manager_, DoUnmount(mount_path_))
Anand K Mistrye9a60fb2019-11-07 16:41:03 +1100935 .Times(2)
936 .WillRepeatedly(Return(MOUNT_ERROR_PATH_NOT_MOUNTED));
Sergei Datsenko2879b5c2019-09-26 13:47:40 +1000937 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
938
939 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(source_path_, filesystem_type_,
940 options_, &mount_path_));
941 EXPECT_EQ(kTestMountPath, mount_path_);
942 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
943
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100944 EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED, manager_.Unmount(mount_path_));
Sergei Datsenko2879b5c2019-09-26 13:47:40 +1000945 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
946}
947
Ben Chan9ed09e32011-11-22 16:24:06 -0800948// Verifies that MountManager::Unmount() returns no error when it is invoked
949// to unmount the source path of a reserved mount path.
Ben Chanf8692882011-08-21 10:15:30 -0700950TEST_F(MountManagerTest, UnmountSucceededWithGivenSourcePathInReservedCase) {
Ben Chanadc5d002014-03-12 15:02:26 -0700951 source_path_ = kTestSourcePath;
952 mount_path_ = kTestMountPath;
Ben Chanf8692882011-08-21 10:15:30 -0700953
954 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
955 .WillOnce(Return(true));
956 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
957 .Times(0);
958 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
959 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700960 EXPECT_CALL(platform_, SetPermissions(mount_path_, _)).WillOnce(Return(true));
Ben Chanf8692882011-08-21 10:15:30 -0700961 EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_))
962 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700963 EXPECT_CALL(manager_,
964 DoMount(source_path_, filesystem_type_, options_, mount_path_, _))
Ben Chanfcb2fc02011-11-21 09:44:07 -0800965 .WillOnce(Return(MOUNT_ERROR_UNKNOWN_FILESYSTEM));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100966 EXPECT_CALL(manager_, DoUnmount(mount_path_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -0700967 EXPECT_CALL(manager_,
Ben Chanfcb2fc02011-11-21 09:44:07 -0800968 ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM))
Ben Chanf8692882011-08-21 10:15:30 -0700969 .WillOnce(Return(true));
970 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
971
Ben Chande0e3f62017-09-26 06:28:39 -0700972 EXPECT_EQ(
973 MOUNT_ERROR_UNKNOWN_FILESYSTEM,
974 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -0700975 EXPECT_EQ(kTestMountPath, mount_path_);
Ben Chanf8692882011-08-21 10:15:30 -0700976 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
977 EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_));
978
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +1100979 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Unmount(source_path_));
Ben Chanf8692882011-08-21 10:15:30 -0700980 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
981 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
982}
983
Ben Chan9ed09e32011-11-22 16:24:06 -0800984// Verifies that MountManager::Unmount() returns no error when it is invoked
985// to unmount a reserved mount path.
Ben Chanf8692882011-08-21 10:15:30 -0700986TEST_F(MountManagerTest, UnmountSucceededWithGivenMountPathInReservedCase) {
Ben Chanadc5d002014-03-12 15:02:26 -0700987 source_path_ = kTestSourcePath;
988 mount_path_ = kTestMountPath;
Ben Chanf8692882011-08-21 10:15:30 -0700989
990 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectory(mount_path_))
991 .WillOnce(Return(true));
992 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
993 .Times(0);
994 EXPECT_CALL(platform_, SetOwnership(mount_path_, _, _))
995 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700996 EXPECT_CALL(platform_, SetPermissions(mount_path_, _)).WillOnce(Return(true));
Ben Chanf8692882011-08-21 10:15:30 -0700997 EXPECT_CALL(platform_, RemoveEmptyDirectory(mount_path_))
998 .WillOnce(Return(true));
Ben Chande0e3f62017-09-26 06:28:39 -0700999 EXPECT_CALL(manager_,
1000 DoMount(source_path_, filesystem_type_, options_, mount_path_, _))
Ben Chanfcb2fc02011-11-21 09:44:07 -08001001 .WillOnce(Return(MOUNT_ERROR_UNKNOWN_FILESYSTEM));
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +11001002 EXPECT_CALL(manager_, DoUnmount(mount_path_)).Times(0);
Ben Chanf8692882011-08-21 10:15:30 -07001003 EXPECT_CALL(manager_,
Ben Chanfcb2fc02011-11-21 09:44:07 -08001004 ShouldReserveMountPathOnError(MOUNT_ERROR_UNKNOWN_FILESYSTEM))
Ben Chanf8692882011-08-21 10:15:30 -07001005 .WillOnce(Return(true));
1006 EXPECT_CALL(manager_, SuggestMountPath(_)).Times(0);
1007
Ben Chande0e3f62017-09-26 06:28:39 -07001008 EXPECT_EQ(
1009 MOUNT_ERROR_UNKNOWN_FILESYSTEM,
1010 manager_.Mount(source_path_, filesystem_type_, options_, &mount_path_));
Ben Chanadc5d002014-03-12 15:02:26 -07001011 EXPECT_EQ(kTestMountPath, mount_path_);
Ben Chanf8692882011-08-21 10:15:30 -07001012 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
1013 EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_));
1014
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +11001015 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Unmount(mount_path_));
Ben Chanf8692882011-08-21 10:15:30 -07001016 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
1017 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
1018}
1019
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +09001020// Verifies that MountManager::AddOrUpdateMountStateCache() works as expected.
1021TEST_F(MountManagerTest, AddOrUpdateMountStateCache) {
Ben Chan213c6d92019-04-10 16:21:52 -07001022 std::string result;
Ben Chanadc5d002014-03-12 15:02:26 -07001023 source_path_ = kTestSourcePath;
1024 mount_path_ = kTestMountPath;
Ben Chan213c6d92019-04-10 16:21:52 -07001025 std::string mount_path_2 = "target2";
Tatsuhisa Yamaguchi5a6a3032016-08-19 20:03:54 +09001026 bool is_read_only = true;
Ben Chan8dcede82011-07-25 20:56:13 -07001027
Anand K Mistrye9a60fb2019-11-07 16:41:03 +11001028 manager_.AddOrUpdateMountStateCache(source_path_, MakeMountPoint(mount_path_),
1029 is_read_only);
Ben Chan8dcede82011-07-25 20:56:13 -07001030 EXPECT_TRUE(manager_.GetMountPathFromCache(source_path_, &result));
1031 EXPECT_EQ(mount_path_, result);
Anand K Mistrye9a60fb2019-11-07 16:41:03 +11001032 base::Optional<MountEntry> mount_entry;
1033 mount_entry = manager_.GetMountEntryForTest(source_path_);
1034 EXPECT_TRUE(mount_entry);
1035 EXPECT_EQ(kTestMountPath, mount_entry->mount_path);
1036 EXPECT_EQ(is_read_only, mount_entry->is_read_only);
Ben Chan8dcede82011-07-25 20:56:13 -07001037
Anand K Mistrye9a60fb2019-11-07 16:41:03 +11001038 manager_.AddOrUpdateMountStateCache(source_path_,
1039 MakeMountPoint(mount_path_2), false);
1040 mount_entry = manager_.GetMountEntryForTest(source_path_);
1041 EXPECT_TRUE(mount_entry);
1042 EXPECT_EQ(mount_path_2, mount_entry->mount_path);
1043 EXPECT_FALSE(mount_entry->is_read_only);
Ben Chan8dcede82011-07-25 20:56:13 -07001044
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +09001045 EXPECT_FALSE(manager_.RemoveMountPathFromCache(mount_path_));
1046 EXPECT_TRUE(manager_.RemoveMountPathFromCache(mount_path_2));
Ben Chan8dcede82011-07-25 20:56:13 -07001047}
1048
Ben Chan9ed09e32011-11-22 16:24:06 -08001049// Verifies that MountManager::IsMountPathInCache() works as expected.
Ben Chan8dcede82011-07-25 20:56:13 -07001050TEST_F(MountManagerTest, IsMountPathInCache) {
Ben Chanadc5d002014-03-12 15:02:26 -07001051 source_path_ = kTestSourcePath;
1052 mount_path_ = kTestMountPath;
Ben Chan8dcede82011-07-25 20:56:13 -07001053
1054 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
Anand K Mistrye9a60fb2019-11-07 16:41:03 +11001055 manager_.AddOrUpdateMountStateCache(source_path_, MakeMountPoint(mount_path_),
1056 false);
Ben Chan8dcede82011-07-25 20:56:13 -07001057 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
1058 EXPECT_TRUE(manager_.RemoveMountPathFromCache(mount_path_));
1059 EXPECT_FALSE(manager_.IsMountPathInCache(mount_path_));
1060}
1061
Ben Chan9ed09e32011-11-22 16:24:06 -08001062// Verifies that MountManager::RemoveMountPathFromCache() works as expected.
Ben Chan8dcede82011-07-25 20:56:13 -07001063TEST_F(MountManagerTest, RemoveMountPathFromCache) {
Ben Chanadc5d002014-03-12 15:02:26 -07001064 source_path_ = kTestSourcePath;
1065 mount_path_ = kTestMountPath;
Ben Chan8dcede82011-07-25 20:56:13 -07001066
1067 EXPECT_FALSE(manager_.RemoveMountPathFromCache(mount_path_));
Anand K Mistrye9a60fb2019-11-07 16:41:03 +11001068 manager_.AddOrUpdateMountStateCache(source_path_, MakeMountPoint(mount_path_),
1069 false);
Ben Chan8dcede82011-07-25 20:56:13 -07001070 EXPECT_TRUE(manager_.RemoveMountPathFromCache(mount_path_));
1071 EXPECT_FALSE(manager_.RemoveMountPathFromCache(mount_path_));
1072}
1073
Ben Chan9ed09e32011-11-22 16:24:06 -08001074// Verifies that MountManager::GetReservedMountPaths() works as expected.
Ben Chan632c9f82011-10-11 12:22:16 -07001075TEST_F(MountManagerTest, GetReservedMountPaths) {
Ben Chan213c6d92019-04-10 16:21:52 -07001076 std::set<std::string> reserved_paths;
1077 std::set<std::string> expected_paths;
1078 std::string path1 = "path1";
1079 std::string path2 = "path2";
Ben Chan632c9f82011-10-11 12:22:16 -07001080
1081 reserved_paths = manager_.GetReservedMountPaths();
Ben Chand30e7432011-11-28 13:43:17 -08001082 EXPECT_TRUE(expected_paths == reserved_paths);
Ben Chan632c9f82011-10-11 12:22:16 -07001083
Ben Chanfcb2fc02011-11-21 09:44:07 -08001084 manager_.ReserveMountPath(path1, MOUNT_ERROR_UNKNOWN_FILESYSTEM);
Ben Chan632c9f82011-10-11 12:22:16 -07001085 reserved_paths = manager_.GetReservedMountPaths();
1086 expected_paths.insert(path1);
Ben Chand30e7432011-11-28 13:43:17 -08001087 EXPECT_TRUE(expected_paths == reserved_paths);
Ben Chan632c9f82011-10-11 12:22:16 -07001088
Ben Chanfcb2fc02011-11-21 09:44:07 -08001089 manager_.ReserveMountPath(path2, MOUNT_ERROR_UNKNOWN_FILESYSTEM);
Ben Chan632c9f82011-10-11 12:22:16 -07001090 reserved_paths = manager_.GetReservedMountPaths();
1091 expected_paths.insert(path2);
Ben Chand30e7432011-11-28 13:43:17 -08001092 EXPECT_TRUE(expected_paths == reserved_paths);
Ben Chan632c9f82011-10-11 12:22:16 -07001093
1094 manager_.UnreserveMountPath(path1);
1095 reserved_paths = manager_.GetReservedMountPaths();
1096 expected_paths.erase(path1);
Ben Chand30e7432011-11-28 13:43:17 -08001097 EXPECT_TRUE(expected_paths == reserved_paths);
Ben Chan632c9f82011-10-11 12:22:16 -07001098
1099 manager_.UnreserveMountPath(path2);
1100 reserved_paths = manager_.GetReservedMountPaths();
1101 expected_paths.erase(path2);
Ben Chand30e7432011-11-28 13:43:17 -08001102 EXPECT_TRUE(expected_paths == reserved_paths);
Ben Chan632c9f82011-10-11 12:22:16 -07001103}
1104
Ben Chan9ed09e32011-11-22 16:24:06 -08001105// Verifies that MountManager::ReserveMountPath() and
1106// MountManager::UnreserveMountPath() work as expected.
Ben Chanf8692882011-08-21 10:15:30 -07001107TEST_F(MountManagerTest, ReserveAndUnreserveMountPath) {
Ben Chanadc5d002014-03-12 15:02:26 -07001108 mount_path_ = kTestMountPath;
Ben Chanf8692882011-08-21 10:15:30 -07001109
1110 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
Ben Chanfcb2fc02011-11-21 09:44:07 -08001111 EXPECT_EQ(MOUNT_ERROR_NONE,
Ben Chan632c9f82011-10-11 12:22:16 -07001112 manager_.GetMountErrorOfReservedMountPath(mount_path_));
Ben Chanfcb2fc02011-11-21 09:44:07 -08001113 manager_.ReserveMountPath(mount_path_, MOUNT_ERROR_UNKNOWN_FILESYSTEM);
Ben Chanf8692882011-08-21 10:15:30 -07001114 EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_));
Ben Chanfcb2fc02011-11-21 09:44:07 -08001115 EXPECT_EQ(MOUNT_ERROR_UNKNOWN_FILESYSTEM,
Ben Chan632c9f82011-10-11 12:22:16 -07001116 manager_.GetMountErrorOfReservedMountPath(mount_path_));
Ben Chanf8692882011-08-21 10:15:30 -07001117 manager_.UnreserveMountPath(mount_path_);
1118 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
Ben Chanfcb2fc02011-11-21 09:44:07 -08001119 EXPECT_EQ(MOUNT_ERROR_NONE,
Ben Chan632c9f82011-10-11 12:22:16 -07001120 manager_.GetMountErrorOfReservedMountPath(mount_path_));
Ben Chanf8692882011-08-21 10:15:30 -07001121
1122 // Removing a nonexistent mount path should be ok
1123 manager_.UnreserveMountPath(mount_path_);
1124 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
1125
1126 // Adding an existent mount path should be ok
Ben Chanfcb2fc02011-11-21 09:44:07 -08001127 manager_.ReserveMountPath(mount_path_, MOUNT_ERROR_UNSUPPORTED_FILESYSTEM);
Ben Chanf8692882011-08-21 10:15:30 -07001128 EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_));
Ben Chanfcb2fc02011-11-21 09:44:07 -08001129 EXPECT_EQ(MOUNT_ERROR_UNSUPPORTED_FILESYSTEM,
Ben Chan632c9f82011-10-11 12:22:16 -07001130 manager_.GetMountErrorOfReservedMountPath(mount_path_));
Ben Chanfcb2fc02011-11-21 09:44:07 -08001131 manager_.ReserveMountPath(mount_path_, MOUNT_ERROR_UNKNOWN_FILESYSTEM);
Ben Chanf8692882011-08-21 10:15:30 -07001132 EXPECT_TRUE(manager_.IsMountPathReserved(mount_path_));
Ben Chanfcb2fc02011-11-21 09:44:07 -08001133 EXPECT_EQ(MOUNT_ERROR_UNSUPPORTED_FILESYSTEM,
Ben Chan632c9f82011-10-11 12:22:16 -07001134 manager_.GetMountErrorOfReservedMountPath(mount_path_));
Ben Chanf8692882011-08-21 10:15:30 -07001135 manager_.UnreserveMountPath(mount_path_);
1136 EXPECT_FALSE(manager_.IsMountPathReserved(mount_path_));
Ben Chanfcb2fc02011-11-21 09:44:07 -08001137 EXPECT_EQ(MOUNT_ERROR_NONE,
Ben Chan632c9f82011-10-11 12:22:16 -07001138 manager_.GetMountErrorOfReservedMountPath(mount_path_));
Ben Chanf8692882011-08-21 10:15:30 -07001139}
1140
Ben Chan8fb742b2014-04-28 23:46:57 -07001141// Verifies that MountManager::GetMountEntries() returns the expected list of
1142// mount entries under different scenarios.
1143TEST_F(MountManagerTest, GetMountEntries) {
Ben Chan7dfb8102017-10-17 15:47:37 -07001144 // No mount entry is returned.
Ben Chan213c6d92019-04-10 16:21:52 -07001145 std::vector<MountEntry> mount_entries = manager_.GetMountEntries();
Ben Chan8fb742b2014-04-28 23:46:57 -07001146 EXPECT_TRUE(mount_entries.empty());
1147
1148 // A normal mount entry is returned.
Anand K Mistrye9a60fb2019-11-07 16:41:03 +11001149 manager_.AddOrUpdateMountStateCache(kTestSourcePath,
1150 MakeMountPoint(kTestMountPath), false);
Ben Chan7dfb8102017-10-17 15:47:37 -07001151 mount_entries = manager_.GetMountEntries();
Ben Chan8fb742b2014-04-28 23:46:57 -07001152 ASSERT_EQ(1, mount_entries.size());
Ben Chan0214e302017-10-17 15:39:16 -07001153 EXPECT_EQ(MOUNT_ERROR_NONE, mount_entries[0].error_type);
1154 EXPECT_EQ(kTestSourcePath, mount_entries[0].source_path);
1155 EXPECT_EQ(MOUNT_SOURCE_REMOVABLE_DEVICE, mount_entries[0].source_type);
1156 EXPECT_EQ(kTestMountPath, mount_entries[0].mount_path);
Ben Chan8fb742b2014-04-28 23:46:57 -07001157
1158 // A reserved mount entry is returned.
1159 manager_.ReserveMountPath(kTestMountPath, MOUNT_ERROR_UNKNOWN_FILESYSTEM);
Ben Chan7dfb8102017-10-17 15:47:37 -07001160 mount_entries = manager_.GetMountEntries();
Ben Chan8fb742b2014-04-28 23:46:57 -07001161 ASSERT_EQ(1, mount_entries.size());
Ben Chan0214e302017-10-17 15:39:16 -07001162 EXPECT_EQ(MOUNT_ERROR_UNKNOWN_FILESYSTEM, mount_entries[0].error_type);
1163 EXPECT_EQ(kTestSourcePath, mount_entries[0].source_path);
1164 EXPECT_EQ(MOUNT_SOURCE_REMOVABLE_DEVICE, mount_entries[0].source_type);
1165 EXPECT_EQ(kTestMountPath, mount_entries[0].mount_path);
Ben Chan8fb742b2014-04-28 23:46:57 -07001166}
1167
Ben Chan09c90d02012-04-25 22:09:09 -07001168// Verifies that MountManager::ExtractMountLabelFromOptions() extracts a mount
1169// label from the given options and returns true.
1170TEST_F(MountManagerTest, ExtractMountLabelFromOptions) {
Ben Chan213c6d92019-04-10 16:21:52 -07001171 std::vector<std::string> options = {"ro", "mountlabel=My USB Drive",
1172 "noexec"};
1173 std::string mount_label;
Ben Chan09c90d02012-04-25 22:09:09 -07001174
1175 EXPECT_TRUE(manager_.ExtractMountLabelFromOptions(&options, &mount_label));
1176 EXPECT_THAT(options, ElementsAre("ro", "noexec"));
1177 EXPECT_EQ("My USB Drive", mount_label);
1178}
1179
1180// Verifies that MountManager::ExtractMountLabelFromOptions() returns false
1181// when no mount label is found in the given options.
1182TEST_F(MountManagerTest, ExtractMountLabelFromOptionsWithNoMountLabel) {
Ben Chan213c6d92019-04-10 16:21:52 -07001183 std::vector<std::string> options;
1184 std::string mount_label;
Ben Chan09c90d02012-04-25 22:09:09 -07001185
1186 EXPECT_FALSE(manager_.ExtractMountLabelFromOptions(&options, &mount_label));
1187 EXPECT_THAT(options, ElementsAre());
1188 EXPECT_EQ("", mount_label);
1189
1190 options.push_back("ro");
1191 EXPECT_FALSE(manager_.ExtractMountLabelFromOptions(&options, &mount_label));
1192 EXPECT_THAT(options, ElementsAre("ro"));
1193 EXPECT_EQ("", mount_label);
1194
1195 options.push_back("mountlabel");
1196 EXPECT_FALSE(manager_.ExtractMountLabelFromOptions(&options, &mount_label));
1197 EXPECT_THAT(options, ElementsAre("ro", "mountlabel"));
1198 EXPECT_EQ("", mount_label);
1199}
1200
1201// Verifies that MountManager::ExtractMountLabelFromOptions() extracts the last
1202// mount label from the given options with two mount labels.
1203TEST_F(MountManagerTest, ExtractMountLabelFromOptionsWithTwoMountLabels) {
Ben Chan213c6d92019-04-10 16:21:52 -07001204 std::vector<std::string> options = {"ro", "mountlabel=My USB Drive", "noexec",
1205 "mountlabel=Another Label"};
1206 std::string mount_label;
Ben Chan09c90d02012-04-25 22:09:09 -07001207
1208 EXPECT_TRUE(manager_.ExtractMountLabelFromOptions(&options, &mount_label));
1209 EXPECT_THAT(options, ElementsAre("ro", "noexec"));
1210 EXPECT_EQ("Another Label", mount_label);
1211}
1212
Ben Chan9ed09e32011-11-22 16:24:06 -08001213// Verifies that MountManager::IsPathImmediateChildOfParent() correctly
1214// determines if a path is an immediate child of another path.
Ben Chan8dcede82011-07-25 20:56:13 -07001215TEST_F(MountManagerTest, IsPathImmediateChildOfParent) {
Anand K Mistry09f2db12019-11-07 17:06:56 +11001216 EXPECT_TRUE(manager_.IsPathImmediateChildOfParent(
1217 base::FilePath("/media/archive/test.zip"),
1218 base::FilePath("/media/archive")));
1219 EXPECT_TRUE(manager_.IsPathImmediateChildOfParent(
1220 base::FilePath("/media/archive/test.zip/"),
1221 base::FilePath("/media/archive")));
1222 EXPECT_TRUE(manager_.IsPathImmediateChildOfParent(
1223 base::FilePath("/media/archive/test.zip"),
1224 base::FilePath("/media/archive/")));
1225 EXPECT_TRUE(manager_.IsPathImmediateChildOfParent(
1226 base::FilePath("/media/archive/test.zip/"),
1227 base::FilePath("/media/archive/")));
Ben Chan8dcede82011-07-25 20:56:13 -07001228 EXPECT_FALSE(manager_.IsPathImmediateChildOfParent(
Anand K Mistry09f2db12019-11-07 17:06:56 +11001229 base::FilePath("/media/archive/test.zip/doc.zip"),
1230 base::FilePath("/media/archive/")));
1231 EXPECT_FALSE(manager_.IsPathImmediateChildOfParent(
1232 base::FilePath("/media/archive/test.zip"),
1233 base::FilePath("/media/removable")));
1234 EXPECT_FALSE(manager_.IsPathImmediateChildOfParent(
1235 base::FilePath("/tmp/archive/test.zip"),
1236 base::FilePath("/media/removable")));
1237 EXPECT_FALSE(manager_.IsPathImmediateChildOfParent(
1238 base::FilePath("/media"), base::FilePath("/media/removable")));
1239 EXPECT_FALSE(manager_.IsPathImmediateChildOfParent(
1240 base::FilePath("/media/removable"), base::FilePath("/media/removable")));
1241 EXPECT_FALSE(manager_.IsPathImmediateChildOfParent(
1242 base::FilePath("/media/removable/"), base::FilePath("/media/removable")));
1243 EXPECT_FALSE(manager_.IsPathImmediateChildOfParent(
1244 base::FilePath("/media/removable/."),
1245 base::FilePath("/media/removable")));
1246 EXPECT_FALSE(manager_.IsPathImmediateChildOfParent(
1247 base::FilePath("/media/removable/.."),
1248 base::FilePath("/media/removable")));
Ben Chan8dcede82011-07-25 20:56:13 -07001249}
1250
Ben Chanadc5d002014-03-12 15:02:26 -07001251// Verifies that MountManager::IsValidMountPath() correctly determines if a
1252// mount path is an immediate child of the mount root.
1253TEST_F(MountManagerTest, IsValidMountPath) {
Anand K Mistry09f2db12019-11-07 17:06:56 +11001254 EXPECT_TRUE(
1255 manager_.IsValidMountPath(base::FilePath("/media/removable/test")));
1256 EXPECT_TRUE(
1257 manager_.IsValidMountPath(base::FilePath("/media/removable/test/")));
1258 EXPECT_TRUE(
1259 manager_.IsValidMountPath(base::FilePath("/media/removable/test/")));
1260 EXPECT_TRUE(
1261 manager_.IsValidMountPath(base::FilePath("/media/removable//test")));
1262 EXPECT_FALSE(
1263 manager_.IsValidMountPath(base::FilePath("/media/archive/test")));
1264 EXPECT_FALSE(manager_.IsValidMountPath(base::FilePath("/media/removable")));
1265 EXPECT_FALSE(manager_.IsValidMountPath(base::FilePath("/media/removable/")));
1266 EXPECT_FALSE(manager_.IsValidMountPath(base::FilePath("/media/removable/.")));
1267 EXPECT_FALSE(
1268 manager_.IsValidMountPath(base::FilePath("/media/removable/..")));
1269 EXPECT_FALSE(
1270 manager_.IsValidMountPath(base::FilePath("/media/removable/test/doc")));
1271 EXPECT_FALSE(
1272 manager_.IsValidMountPath(base::FilePath("/media/removable/../test")));
1273 EXPECT_FALSE(
1274 manager_.IsValidMountPath(base::FilePath("/media/removable/../test/")));
1275 EXPECT_FALSE(
1276 manager_.IsValidMountPath(base::FilePath("/media/removable/test/..")));
1277 EXPECT_FALSE(
1278 manager_.IsValidMountPath(base::FilePath("/media/removable/test/../")));
Ben Chanadc5d002014-03-12 15:02:26 -07001279}
1280
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +09001281// Verifies that MountManager::Mount() returns an error when the source is
1282// not mounted yet but attempted to remount it.
1283TEST_F(MountManagerTest, RemountFailedNotMounted) {
1284 options_.push_back(kMountOptionRemount);
1285
1286 EXPECT_CALL(manager_, DoMount(_, _, _, _, _)).Times(0);
Anand K Mistry5a6d5fa2019-11-05 17:21:48 +11001287 EXPECT_CALL(manager_, DoUnmount(_)).Times(0);
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +09001288
1289 // source_path = kTestSourcePath has not been mounted yet.
1290 EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED,
1291 manager_.Mount(kTestSourcePath, filesystem_type_, options_,
1292 &mount_path_));
1293}
1294
1295// Verifies that MountManager::Mount() returns no error when it successfully
1296// remounts a source path on a specified mount path.
1297TEST_F(MountManagerTest, RemountSucceededWithGivenSourcePath) {
Anand K Mistrye9a60fb2019-11-07 16:41:03 +11001298 // Since the mount point will be remounted, only expect unmount to be called
1299 // once.
1300 EXPECT_CALL(manager_, DoUnmount(kTestMountPath))
1301 .WillOnce(Return(MOUNT_ERROR_NONE));
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +09001302
Anand K Mistrye9a60fb2019-11-07 16:41:03 +11001303 // Mount a device in read-write mode.
1304 EXPECT_CALL(manager_, SuggestMountPath(_)).WillOnce(Return(kTestMountPath));
1305 EXPECT_CALL(platform_, CreateOrReuseEmptyDirectoryWithFallback(_, _, _))
1306 .WillOnce(Return(true));
1307 EXPECT_CALL(platform_, SetOwnership(kTestMountPath, _, _))
1308 .WillOnce(Return(true));
1309 EXPECT_CALL(platform_, SetPermissions(kTestMountPath, _))
1310 .WillOnce(Return(true));
1311 EXPECT_CALL(manager_,
1312 DoMount(kTestSourcePath, filesystem_type_,
1313 std::vector<std::string>({kMountOptionReadWrite}),
1314 kTestMountPath, _))
1315 .WillOnce(Invoke(DoMountSuccess));
1316 mount_path_ = "";
1317 EXPECT_EQ(MOUNT_ERROR_NONE,
1318 manager_.Mount(kTestSourcePath, filesystem_type_,
1319 {kMountOptionReadWrite}, &mount_path_));
1320 EXPECT_EQ(kTestMountPath, mount_path_);
1321 base::Optional<MountEntry> mount_entry;
1322 mount_entry = manager_.GetMountEntryForTest(kTestSourcePath);
1323 ASSERT_TRUE(mount_entry);
1324 EXPECT_FALSE(mount_entry->is_read_only);
1325 EXPECT_EQ(kTestMountPath, mount_entry->mount_path);
1326
1327 // Remount with read-only mount option.
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +09001328 options_.push_back(kMountOptionRemount);
1329 options_.push_back(kMountOptionReadOnly);
Ben Chan213c6d92019-04-10 16:21:52 -07001330 std::vector<std::string> expected_options = options_;
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +09001331 EXPECT_CALL(manager_, DoMount(kTestSourcePath, filesystem_type_,
1332 expected_options, kTestMountPath, _))
1333 .WillOnce(Invoke(DoMountSuccess));
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +09001334 mount_path_ = "";
1335 EXPECT_EQ(MOUNT_ERROR_NONE, manager_.Mount(kTestSourcePath, filesystem_type_,
1336 options_, &mount_path_));
1337 EXPECT_EQ(kTestMountPath, mount_path_);
1338 EXPECT_TRUE(manager_.IsMountPathInCache(mount_path_));
1339
Anand K Mistrye9a60fb2019-11-07 16:41:03 +11001340 mount_entry = manager_.GetMountEntryForTest(kTestSourcePath);
1341 EXPECT_TRUE(mount_entry);
1342 EXPECT_TRUE(mount_entry->is_read_only);
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +09001343
1344 // Should be unmounted correctly even after remount.
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +09001345 EXPECT_TRUE(manager_.UnmountAll());
1346 EXPECT_FALSE(manager_.IsMountPathInCache(kTestMountPath));
1347 EXPECT_FALSE(manager_.IsMountPathReserved(kTestMountPath));
1348}
1349
Ben Chan8dcede82011-07-25 20:56:13 -07001350} // namespace cros_disks