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
|
// Copyright 2020 The Chromium OS 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 "brillo/namespaces/mock_platform.h"
#include "brillo/namespaces/mount_namespace.h"
#include "brillo/namespaces/platform.h"
#include <unistd.h>
#include <memory>
#include <base/files/file_path.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::_;
using ::testing::DoAll;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::SetArgPointee;
namespace brillo {
class MountNamespaceTest : public ::testing::Test {
public:
MountNamespaceTest() {}
~MountNamespaceTest() {}
void SetUp() {}
void TearDown() {}
protected:
NiceMock<MockPlatform> platform_;
private:
DISALLOW_COPY_AND_ASSIGN(MountNamespaceTest);
};
TEST_F(MountNamespaceTest, CreateNamespace) {
std::unique_ptr<MountNamespace> ns =
std::make_unique<MountNamespace>(base::FilePath(), &platform_);
EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
EXPECT_CALL(platform_, Waitpid(_, _))
.WillOnce(DoAll(SetArgPointee<1>(0x00000000), Return(0)));
EXPECT_TRUE(ns->Create());
EXPECT_CALL(platform_, Unmount(ns->path(), _, _)).WillOnce(Return(true));
}
TEST_F(MountNamespaceTest, CreateNamespaceFailedOnWaitpid) {
std::unique_ptr<MountNamespace> ns =
std::make_unique<MountNamespace>(base::FilePath(), &platform_);
EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
EXPECT_CALL(platform_, Waitpid(_, _)).WillOnce(Return(-1));
EXPECT_FALSE(ns->Create());
}
TEST_F(MountNamespaceTest, CreateNamespaceFailedOnMount) {
std::unique_ptr<MountNamespace> ns =
std::make_unique<MountNamespace>(base::FilePath(), &platform_);
EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(-1));
EXPECT_FALSE(ns->Create());
}
TEST_F(MountNamespaceTest, CreateNamespaceFailedOnStatus) {
std::unique_ptr<MountNamespace> ns =
std::make_unique<MountNamespace>(base::FilePath(), &platform_);
EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
EXPECT_CALL(platform_, Waitpid(_, _))
.WillOnce(DoAll(SetArgPointee<1>(0xFFFFFFFF), Return(0)));
EXPECT_FALSE(ns->Create());
}
TEST_F(MountNamespaceTest, DestroyAfterUnmountFailsAndUnmountSucceeds) {
std::unique_ptr<MountNamespace> ns =
std::make_unique<MountNamespace>(base::FilePath(), &platform_);
EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
EXPECT_CALL(platform_, Waitpid(_, _))
.WillOnce(DoAll(SetArgPointee<1>(0x00000000), Return(0)));
EXPECT_TRUE(ns->Create());
EXPECT_CALL(platform_, Unmount(ns->path(), _, _)).WillOnce(Return(false));
EXPECT_FALSE(ns->Destroy());
EXPECT_CALL(platform_, Unmount(ns->path(), _, _)).WillOnce(Return(true));
EXPECT_TRUE(ns->Destroy());
}
} // namespace brillo
|