blob: 8654b6e029fd9f0fc29e40359e36f45bf69e6b5c [file] [log] [blame]
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +11001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "cros-disks/mount_point.h"
6
Ben Chan4cc7a012019-04-10 16:32:56 -07007#include <utility>
8
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +11009#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
Sergei Datsenko0ba12032021-01-07 08:51:14 +110012#include "cros-disks/platform.h"
13
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110014namespace cros_disks {
15
16namespace {
17
18using testing::_;
19using testing::Return;
20
Anand K Mistryf1bd0862019-12-20 11:54:55 +110021constexpr char kMountPath[] = "/mount/path";
Sergei Datsenko0ba12032021-01-07 08:51:14 +110022constexpr char kSource[] = "source";
23constexpr char kFSType[] = "fstype";
24constexpr char kOptions[] = "foo=bar";
Anand K Mistryf1bd0862019-12-20 11:54:55 +110025
Sergei Datsenko0ba12032021-01-07 08:51:14 +110026class MockPlatform : public Platform {
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110027 public:
Sergei Datsenko0ba12032021-01-07 08:51:14 +110028 MOCK_METHOD(MountErrorType,
29 Mount,
30 (const std::string& source,
31 const std::string& target,
32 const std::string& filesystem_type,
33 uint64_t flags,
34 const std::string& options),
35 (const, override));
36 MOCK_METHOD(MountErrorType,
37 Unmount,
38 (const std::string& path, int flags),
39 (const, override));
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110040};
41
42} // namespace
43
44TEST(MountPointTest, Unmount) {
Sergei Datsenko0ba12032021-01-07 08:51:14 +110045 MockPlatform platform;
46 MountPointData data = {
47 .mount_path = base::FilePath(kMountPath),
48 .source = kSource,
49 .filesystem_type = kFSType,
50 .flags = MS_DIRSYNC | MS_NODEV,
51 .data = kOptions,
52 };
53 auto mount_point = std::make_unique<MountPoint>(std::move(data), &platform);
Anand K Mistryf1bd0862019-12-20 11:54:55 +110054
Sergei Datsenko0ba12032021-01-07 08:51:14 +110055 EXPECT_CALL(platform, Unmount(kMountPath, _))
François Degros3a796902019-07-12 14:49:58 +100056 .WillOnce(Return(MOUNT_ERROR_INVALID_ARCHIVE))
57 .WillOnce(Return(MOUNT_ERROR_NONE));
Anand K Mistryf1bd0862019-12-20 11:54:55 +110058 EXPECT_EQ(MOUNT_ERROR_INVALID_ARCHIVE, mount_point->Unmount());
59 EXPECT_EQ(MOUNT_ERROR_NONE, mount_point->Unmount());
60 EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED, mount_point->Unmount());
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110061}
62
Sergei Datsenko0ba12032021-01-07 08:51:14 +110063TEST(MountPointTest, UnmountBusy) {
64 MockPlatform platform;
65 MountPointData data = {
66 .mount_path = base::FilePath(kMountPath),
67 .source = kSource,
68 .filesystem_type = kFSType,
69 .flags = MS_DIRSYNC | MS_NODEV,
70 .data = kOptions,
71 };
72 auto mount_point = std::make_unique<MountPoint>(std::move(data), &platform);
Anand K Mistryf1bd0862019-12-20 11:54:55 +110073
Sergei Datsenko0ba12032021-01-07 08:51:14 +110074 // Unmount will retry unmounting with force and detach if mount point busy.
75 EXPECT_CALL(platform, Unmount(kMountPath, 0))
76 .WillOnce(Return(MOUNT_ERROR_PATH_ALREADY_MOUNTED));
77 EXPECT_CALL(platform, Unmount(kMountPath, MNT_DETACH | MNT_FORCE))
78 .WillOnce(Return(MOUNT_ERROR_NONE));
79 EXPECT_EQ(MOUNT_ERROR_NONE, mount_point->Unmount());
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110080}
81
Sergei Datsenko0ba12032021-01-07 08:51:14 +110082TEST(MountPointTest, UnmountOnDestroy) {
83 MockPlatform platform;
84 MountPointData data = {
85 .mount_path = base::FilePath(kMountPath),
86 .source = kSource,
87 .filesystem_type = kFSType,
88 .flags = MS_DIRSYNC | MS_NODEV,
89 .data = kOptions,
90 };
91 auto mount_point = std::make_unique<MountPoint>(std::move(data), &platform);
Anand K Mistryf1bd0862019-12-20 11:54:55 +110092
Sergei Datsenko0ba12032021-01-07 08:51:14 +110093 EXPECT_CALL(platform, Unmount).WillOnce(Return(MOUNT_ERROR_INVALID_ARCHIVE));
94 mount_point.reset();
95}
96
97TEST(MountPointTest, Remount) {
98 MockPlatform platform;
99 MountPointData data = {
100 .mount_path = base::FilePath(kMountPath),
101 .source = kSource,
102 .filesystem_type = kFSType,
103 .flags = MS_DIRSYNC | MS_NODEV,
104 .data = kOptions,
105 };
106 auto mount_point = std::make_unique<MountPoint>(std::move(data), &platform);
107 EXPECT_FALSE(mount_point->is_read_only());
108
109 EXPECT_CALL(platform,
110 Mount(kSource, kMountPath, kFSType,
111 MS_DIRSYNC | MS_NODEV | MS_RDONLY | MS_REMOUNT, kOptions))
112 .WillOnce(Return(MOUNT_ERROR_NONE));
113 EXPECT_EQ(MOUNT_ERROR_NONE, mount_point->Remount(true));
114 EXPECT_TRUE(mount_point->is_read_only());
115
116 EXPECT_CALL(platform, Mount(kSource, kMountPath, kFSType,
117 MS_DIRSYNC | MS_NODEV | MS_REMOUNT, kOptions))
118 .WillOnce(Return(MOUNT_ERROR_INTERNAL));
119 EXPECT_EQ(MOUNT_ERROR_INTERNAL, mount_point->Remount(false));
120 EXPECT_TRUE(mount_point->is_read_only());
121}
122
123TEST(MountPointTest, Mount) {
124 MockPlatform platform;
125 MountPointData data = {
126 .mount_path = base::FilePath(kMountPath),
127 .source = kSource,
128 .filesystem_type = kFSType,
129 .flags = MS_DIRSYNC | MS_NODEV,
130 .data = kOptions,
131 };
132 MountErrorType error = MOUNT_ERROR_UNKNOWN;
133 EXPECT_CALL(platform, Mount(kSource, kMountPath, kFSType,
134 MS_DIRSYNC | MS_NODEV, kOptions))
135 .WillOnce(Return(MOUNT_ERROR_INVALID_ARGUMENT))
136 .WillOnce(Return(MOUNT_ERROR_NONE));
137 auto mount_point = MountPoint::Mount(data, &platform, &error);
138 EXPECT_FALSE(mount_point);
139 EXPECT_EQ(MOUNT_ERROR_INVALID_ARGUMENT, error);
140 mount_point = MountPoint::Mount(data, &platform, &error);
141 EXPECT_TRUE(mount_point);
142 EXPECT_EQ(MOUNT_ERROR_NONE, error);
143 EXPECT_EQ(kMountPath, mount_point->path().value());
Anand K Mistryf1bd0862019-12-20 11:54:55 +1100144
145 mount_point->Release();
Sergei Datsenko0ba12032021-01-07 08:51:14 +1100146}
147
148TEST(MountPointTest, Release) {
149 MockPlatform platform;
150 MountPointData data = {
151 .mount_path = base::FilePath(kMountPath),
152 .source = kSource,
153 .filesystem_type = kFSType,
154 .flags = MS_DIRSYNC | MS_NODEV,
155 .data = kOptions,
156 };
157 auto mount_point = std::make_unique<MountPoint>(std::move(data), &platform);
158
159 EXPECT_CALL(platform, Mount).Times(0);
160 EXPECT_CALL(platform, Unmount).Times(0);
161 mount_point->Release();
Anand K Mistryf1bd0862019-12-20 11:54:55 +1100162 EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED, mount_point->Unmount());
Sergei Datsenko0ba12032021-01-07 08:51:14 +1100163 EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED, mount_point->Remount(true));
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +1100164}
165
166} // namespace cros_disks