forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount_point_unittest.cc
149 lines (130 loc) · 5.74 KB
/
mount_point_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/disks/mount_point.h"
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "chromeos/disks/mock_disk_mount_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::WithArg;
using ::testing::WithoutArgs;
namespace chromeos {
namespace disks {
namespace {
constexpr char kSourcePath[] = "/source/path";
constexpr char kMountPath[] = "/mount/path";
constexpr char kOtherPath[] = "/other/path";
class MountPointTest : public testing::Test {
public:
MountPointTest() = default;
protected:
base::test::TaskEnvironment task_environment_;
MockDiskMountManager disk_mount_manager_;
};
TEST_F(MountPointTest, Mount) {
EXPECT_CALL(disk_mount_manager_,
MountPath(kSourcePath, "", "", _, MOUNT_TYPE_DEVICE,
MOUNT_ACCESS_MODE_READ_WRITE))
.WillOnce(WithoutArgs([this]() {
// Ignore other mount events.
disk_mount_manager_.NotifyMountEvent(
DiskMountManager::MOUNTING, MOUNT_ERROR_NONE,
DiskMountManager::MountPointInfo(kOtherPath, kOtherPath,
MOUNT_TYPE_DEVICE,
MOUNT_CONDITION_NONE));
disk_mount_manager_.NotifyMountEvent(
DiskMountManager::UNMOUNTING, MOUNT_ERROR_NONE,
DiskMountManager::MountPointInfo(kSourcePath, kOtherPath,
MOUNT_TYPE_DEVICE,
MOUNT_CONDITION_NONE));
disk_mount_manager_.NotifyMountEvent(
DiskMountManager::MOUNTING, MOUNT_ERROR_NONE,
DiskMountManager::MountPointInfo(kSourcePath, kOtherPath,
MOUNT_TYPE_ARCHIVE,
MOUNT_CONDITION_NONE));
// This is the real mount event.
disk_mount_manager_.NotifyMountEvent(
DiskMountManager::MOUNTING, MOUNT_ERROR_NONE,
DiskMountManager::MountPointInfo(kSourcePath, kMountPath,
MOUNT_TYPE_DEVICE,
MOUNT_CONDITION_NONE));
}));
EXPECT_CALL(disk_mount_manager_, UnmountPath(kMountPath, _)).Times(1);
base::RunLoop run_loop;
MountPoint::Mount(&disk_mount_manager_, kSourcePath, "", "", {},
MOUNT_TYPE_DEVICE, MOUNT_ACCESS_MODE_READ_WRITE,
base::BindLambdaForTesting(
[&run_loop](MountError mount_error,
std::unique_ptr<MountPoint> mount) {
EXPECT_EQ(MOUNT_ERROR_NONE, mount_error);
EXPECT_EQ(kMountPath, mount->mount_path().value());
run_loop.Quit();
}));
run_loop.Run();
}
TEST_F(MountPointTest, MountFailure) {
EXPECT_CALL(disk_mount_manager_,
MountPath(kSourcePath, "", "", _, MOUNT_TYPE_DEVICE,
MOUNT_ACCESS_MODE_READ_WRITE))
.WillOnce(WithoutArgs([this]() {
disk_mount_manager_.NotifyMountEvent(
DiskMountManager::MOUNTING, MOUNT_ERROR_UNKNOWN,
DiskMountManager::MountPointInfo(
kSourcePath, kMountPath, MOUNT_TYPE_DEVICE,
MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM));
}));
EXPECT_CALL(disk_mount_manager_, UnmountPath(_, _)).Times(0);
base::RunLoop run_loop;
MountPoint::Mount(&disk_mount_manager_, kSourcePath, "", "", {},
MOUNT_TYPE_DEVICE, MOUNT_ACCESS_MODE_READ_WRITE,
base::BindLambdaForTesting(
[&run_loop](MountError mount_error,
std::unique_ptr<MountPoint> mount) {
EXPECT_EQ(MOUNT_ERROR_UNKNOWN, mount_error);
EXPECT_FALSE(mount);
run_loop.Quit();
}));
run_loop.Run();
}
TEST_F(MountPointTest, Unmount) {
EXPECT_CALL(disk_mount_manager_, UnmountPath(kMountPath, _))
.WillOnce(base::test::RunOnceCallback<1>(MOUNT_ERROR_INTERNAL));
base::RunLoop run_loop;
MountPoint mount_point(base::FilePath(kMountPath), &disk_mount_manager_);
mount_point.Unmount(base::BindLambdaForTesting([&run_loop](MountError error) {
EXPECT_EQ(MOUNT_ERROR_INTERNAL, error);
run_loop.Quit();
}));
run_loop.Run();
}
TEST_F(MountPointTest, UnmountOnDestruction) {
EXPECT_CALL(disk_mount_manager_, UnmountPath(kMountPath, _)).Times(1);
MountPoint mount_point(base::FilePath(kMountPath), &disk_mount_manager_);
}
TEST_F(MountPointTest, UnmountThenDestory) {
base::RunLoop run_loop;
EXPECT_CALL(disk_mount_manager_, UnmountPath(kMountPath, _))
.WillOnce(WithArg<1>(
[this, &run_loop](DiskMountManager::UnmountPathCallback callback) {
task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), MOUNT_ERROR_INTERNAL));
task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, run_loop.QuitClosure());
}));
std::unique_ptr<MountPoint> mount_point = std::make_unique<MountPoint>(
base::FilePath(kMountPath), &disk_mount_manager_);
mount_point->Unmount(base::BindLambdaForTesting([](MountError error) {
// Expect that this callback is never run.
FAIL();
}));
mount_point.reset();
run_loop.Run();
}
} // namespace
} // namespace disks
} // namespace chromeos