Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 1 | // 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 Chan | 4cc7a01 | 2019-04-10 16:32:56 -0700 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 12 | #include "cros-disks/platform.h" |
| 13 | |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 14 | namespace cros_disks { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | using testing::_; |
| 19 | using testing::Return; |
| 20 | |
Anand K Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 21 | constexpr char kMountPath[] = "/mount/path"; |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 22 | constexpr char kSource[] = "source"; |
| 23 | constexpr char kFSType[] = "fstype"; |
| 24 | constexpr char kOptions[] = "foo=bar"; |
Anand K Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 25 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 26 | class MockPlatform : public Platform { |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 27 | public: |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 28 | 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 Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | } // namespace |
| 43 | |
| 44 | TEST(MountPointTest, Unmount) { |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 45 | 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 Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 54 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 55 | EXPECT_CALL(platform, Unmount(kMountPath, _)) |
François Degros | 3a79690 | 2019-07-12 14:49:58 +1000 | [diff] [blame] | 56 | .WillOnce(Return(MOUNT_ERROR_INVALID_ARCHIVE)) |
| 57 | .WillOnce(Return(MOUNT_ERROR_NONE)); |
Anand K Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 58 | 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 Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 61 | } |
| 62 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 63 | TEST(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 Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 73 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 74 | // 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 Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 80 | } |
| 81 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 82 | TEST(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 Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 92 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 93 | EXPECT_CALL(platform, Unmount).WillOnce(Return(MOUNT_ERROR_INVALID_ARCHIVE)); |
| 94 | mount_point.reset(); |
| 95 | } |
| 96 | |
| 97 | TEST(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 | |
| 123 | TEST(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 Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 144 | |
| 145 | mount_point->Release(); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TEST(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 Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 162 | EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED, mount_point->Unmount()); |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 163 | EXPECT_EQ(MOUNT_ERROR_PATH_NOT_MOUNTED, mount_point->Remount(true)); |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | } // namespace cros_disks |