xref: /aosp_15_r20/external/libbrillo/brillo/namespaces/mount_namespace_test.cc (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2020 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #include "brillo/namespaces/mock_platform.h"
6*1a96fba6SXin Li #include "brillo/namespaces/mount_namespace.h"
7*1a96fba6SXin Li #include "brillo/namespaces/platform.h"
8*1a96fba6SXin Li 
9*1a96fba6SXin Li #include <unistd.h>
10*1a96fba6SXin Li 
11*1a96fba6SXin Li #include <memory>
12*1a96fba6SXin Li 
13*1a96fba6SXin Li #include <base/files/file_path.h>
14*1a96fba6SXin Li #include <gmock/gmock.h>
15*1a96fba6SXin Li #include <gtest/gtest.h>
16*1a96fba6SXin Li 
17*1a96fba6SXin Li using ::testing::_;
18*1a96fba6SXin Li using ::testing::DoAll;
19*1a96fba6SXin Li using ::testing::NiceMock;
20*1a96fba6SXin Li using ::testing::Return;
21*1a96fba6SXin Li using ::testing::SetArgPointee;
22*1a96fba6SXin Li 
23*1a96fba6SXin Li namespace brillo {
24*1a96fba6SXin Li 
25*1a96fba6SXin Li class MountNamespaceTest : public ::testing::Test {
26*1a96fba6SXin Li  public:
MountNamespaceTest()27*1a96fba6SXin Li   MountNamespaceTest() {}
~MountNamespaceTest()28*1a96fba6SXin Li   ~MountNamespaceTest() {}
SetUp()29*1a96fba6SXin Li   void SetUp() {}
30*1a96fba6SXin Li 
TearDown()31*1a96fba6SXin Li   void TearDown() {}
32*1a96fba6SXin Li 
33*1a96fba6SXin Li  protected:
34*1a96fba6SXin Li   NiceMock<MockPlatform> platform_;
35*1a96fba6SXin Li 
36*1a96fba6SXin Li  private:
37*1a96fba6SXin Li   DISALLOW_COPY_AND_ASSIGN(MountNamespaceTest);
38*1a96fba6SXin Li };
39*1a96fba6SXin Li 
TEST_F(MountNamespaceTest,CreateNamespace)40*1a96fba6SXin Li TEST_F(MountNamespaceTest, CreateNamespace) {
41*1a96fba6SXin Li   std::unique_ptr<MountNamespace> ns =
42*1a96fba6SXin Li       std::make_unique<MountNamespace>(base::FilePath(), &platform_);
43*1a96fba6SXin Li   EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
44*1a96fba6SXin Li   EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
45*1a96fba6SXin Li   EXPECT_CALL(platform_, Waitpid(_, _))
46*1a96fba6SXin Li       .WillOnce(DoAll(SetArgPointee<1>(0x00000000), Return(0)));
47*1a96fba6SXin Li   EXPECT_TRUE(ns->Create());
48*1a96fba6SXin Li   EXPECT_CALL(platform_, Unmount(ns->path(), _, _)).WillOnce(Return(true));
49*1a96fba6SXin Li }
50*1a96fba6SXin Li 
TEST_F(MountNamespaceTest,CreateNamespaceFailedOnWaitpid)51*1a96fba6SXin Li TEST_F(MountNamespaceTest, CreateNamespaceFailedOnWaitpid) {
52*1a96fba6SXin Li   std::unique_ptr<MountNamespace> ns =
53*1a96fba6SXin Li       std::make_unique<MountNamespace>(base::FilePath(), &platform_);
54*1a96fba6SXin Li   EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
55*1a96fba6SXin Li   EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
56*1a96fba6SXin Li   EXPECT_CALL(platform_, Waitpid(_, _)).WillOnce(Return(-1));
57*1a96fba6SXin Li   EXPECT_FALSE(ns->Create());
58*1a96fba6SXin Li }
59*1a96fba6SXin Li 
TEST_F(MountNamespaceTest,CreateNamespaceFailedOnMount)60*1a96fba6SXin Li TEST_F(MountNamespaceTest, CreateNamespaceFailedOnMount) {
61*1a96fba6SXin Li   std::unique_ptr<MountNamespace> ns =
62*1a96fba6SXin Li       std::make_unique<MountNamespace>(base::FilePath(), &platform_);
63*1a96fba6SXin Li   EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
64*1a96fba6SXin Li   EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(-1));
65*1a96fba6SXin Li   EXPECT_FALSE(ns->Create());
66*1a96fba6SXin Li }
67*1a96fba6SXin Li 
TEST_F(MountNamespaceTest,CreateNamespaceFailedOnStatus)68*1a96fba6SXin Li TEST_F(MountNamespaceTest, CreateNamespaceFailedOnStatus) {
69*1a96fba6SXin Li   std::unique_ptr<MountNamespace> ns =
70*1a96fba6SXin Li       std::make_unique<MountNamespace>(base::FilePath(), &platform_);
71*1a96fba6SXin Li   EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
72*1a96fba6SXin Li   EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
73*1a96fba6SXin Li   EXPECT_CALL(platform_, Waitpid(_, _))
74*1a96fba6SXin Li       .WillOnce(DoAll(SetArgPointee<1>(0xFFFFFFFF), Return(0)));
75*1a96fba6SXin Li   EXPECT_FALSE(ns->Create());
76*1a96fba6SXin Li }
77*1a96fba6SXin Li 
TEST_F(MountNamespaceTest,DestroyAfterUnmountFailsAndUnmountSucceeds)78*1a96fba6SXin Li TEST_F(MountNamespaceTest, DestroyAfterUnmountFailsAndUnmountSucceeds) {
79*1a96fba6SXin Li   std::unique_ptr<MountNamespace> ns =
80*1a96fba6SXin Li       std::make_unique<MountNamespace>(base::FilePath(), &platform_);
81*1a96fba6SXin Li   EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
82*1a96fba6SXin Li   EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
83*1a96fba6SXin Li   EXPECT_CALL(platform_, Waitpid(_, _))
84*1a96fba6SXin Li       .WillOnce(DoAll(SetArgPointee<1>(0x00000000), Return(0)));
85*1a96fba6SXin Li   EXPECT_TRUE(ns->Create());
86*1a96fba6SXin Li   EXPECT_CALL(platform_, Unmount(ns->path(), _, _)).WillOnce(Return(false));
87*1a96fba6SXin Li   EXPECT_FALSE(ns->Destroy());
88*1a96fba6SXin Li   EXPECT_CALL(platform_, Unmount(ns->path(), _, _)).WillOnce(Return(true));
89*1a96fba6SXin Li   EXPECT_TRUE(ns->Destroy());
90*1a96fba6SXin Li }
91*1a96fba6SXin Li 
92*1a96fba6SXin Li }  // namespace brillo
93