1 // Copyright 2020 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 "brillo/namespaces/mock_platform.h"
6 #include "brillo/namespaces/mount_namespace.h"
7 #include "brillo/namespaces/platform.h"
8 
9 #include <unistd.h>
10 
11 #include <memory>
12 
13 #include <base/files/file_path.h>
14 #include <gmock/gmock.h>
15 #include <gtest/gtest.h>
16 
17 using ::testing::_;
18 using ::testing::DoAll;
19 using ::testing::NiceMock;
20 using ::testing::Return;
21 using ::testing::SetArgPointee;
22 
23 namespace brillo {
24 
25 class MountNamespaceTest : public ::testing::Test {
26  public:
MountNamespaceTest()27   MountNamespaceTest() {}
~MountNamespaceTest()28   ~MountNamespaceTest() {}
SetUp()29   void SetUp() {}
30 
TearDown()31   void TearDown() {}
32 
33  protected:
34   NiceMock<MockPlatform> platform_;
35 
36  private:
37   DISALLOW_COPY_AND_ASSIGN(MountNamespaceTest);
38 };
39 
TEST_F(MountNamespaceTest,CreateNamespace)40 TEST_F(MountNamespaceTest, CreateNamespace) {
41   std::unique_ptr<MountNamespace> ns =
42       std::make_unique<MountNamespace>(base::FilePath(), &platform_);
43   EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
44   EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
45   EXPECT_CALL(platform_, Waitpid(_, _))
46       .WillOnce(DoAll(SetArgPointee<1>(0x00000000), Return(0)));
47   EXPECT_TRUE(ns->Create());
48   EXPECT_CALL(platform_, Unmount(ns->path(), _, _)).WillOnce(Return(true));
49 }
50 
TEST_F(MountNamespaceTest,CreateNamespaceFailedOnWaitpid)51 TEST_F(MountNamespaceTest, CreateNamespaceFailedOnWaitpid) {
52   std::unique_ptr<MountNamespace> ns =
53       std::make_unique<MountNamespace>(base::FilePath(), &platform_);
54   EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
55   EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
56   EXPECT_CALL(platform_, Waitpid(_, _)).WillOnce(Return(-1));
57   EXPECT_FALSE(ns->Create());
58 }
59 
TEST_F(MountNamespaceTest,CreateNamespaceFailedOnMount)60 TEST_F(MountNamespaceTest, CreateNamespaceFailedOnMount) {
61   std::unique_ptr<MountNamespace> ns =
62       std::make_unique<MountNamespace>(base::FilePath(), &platform_);
63   EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
64   EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(-1));
65   EXPECT_FALSE(ns->Create());
66 }
67 
TEST_F(MountNamespaceTest,CreateNamespaceFailedOnStatus)68 TEST_F(MountNamespaceTest, CreateNamespaceFailedOnStatus) {
69   std::unique_ptr<MountNamespace> ns =
70       std::make_unique<MountNamespace>(base::FilePath(), &platform_);
71   EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
72   EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
73   EXPECT_CALL(platform_, Waitpid(_, _))
74       .WillOnce(DoAll(SetArgPointee<1>(0xFFFFFFFF), Return(0)));
75   EXPECT_FALSE(ns->Create());
76 }
77 
TEST_F(MountNamespaceTest,DestroyAfterUnmountFailsAndUnmountSucceeds)78 TEST_F(MountNamespaceTest, DestroyAfterUnmountFailsAndUnmountSucceeds) {
79   std::unique_ptr<MountNamespace> ns =
80       std::make_unique<MountNamespace>(base::FilePath(), &platform_);
81   EXPECT_CALL(platform_, Fork()).WillOnce(Return(1));
82   EXPECT_CALL(platform_, Mount(_, _, _, _, _)).WillOnce(Return(0));
83   EXPECT_CALL(platform_, Waitpid(_, _))
84       .WillOnce(DoAll(SetArgPointee<1>(0x00000000), Return(0)));
85   EXPECT_TRUE(ns->Create());
86   EXPECT_CALL(platform_, Unmount(ns->path(), _, _)).WillOnce(Return(false));
87   EXPECT_FALSE(ns->Destroy());
88   EXPECT_CALL(platform_, Unmount(ns->path(), _, _)).WillOnce(Return(true));
89   EXPECT_TRUE(ns->Destroy());
90 }
91 
92 }  // namespace brillo
93