• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 HiHope Open Source Organization.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <csignal>
20 #include <string>
21 #include <vector>
22 #include <fcntl.h>
23 #include <sched.h>
24 #include <unistd.h>
25 #include <gtest/gtest.h>
26 #include "securec.h"
27 
28 using namespace testing::ext;
29 
30 static const int PATH_MAX_SIZE = 128;
31 
32 class HatsSetnsTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp();
37     void TearDown();
38 private:
39 };
SetUp()40 void HatsSetnsTest::SetUp()
41 {
42 }
43 
TearDown()44 void HatsSetnsTest::TearDown()
45 {
46 }
47 
SetUpTestCase()48 void HatsSetnsTest::SetUpTestCase()
49 {
50 }
51 
TearDownTestCase()52 void HatsSetnsTest::TearDownTestCase()
53 {
54 }
55 
56 /*
57  * @tc.number : SUB_KERNEL_SYSCALL_SETNS_0100
58  * @tc.name   : SetnsTest_0001
59  * @tc.desc   : setns success.
60  * @tc.size   : MediumTest
61  * @tc.type   : Function
62  * @tc.level  : Level 1
63  */
64 HWTEST_F(HatsSetnsTest, SetnsTest_0001, Function | MediumTest | Level1)
65 {
66     char path[PATH_MAX_SIZE] = { 0 };
67     pid_t pid = getpid();
68 
69     // test CLONE_NEWIPC success
70     int ret = sprintf_s(path, PATH_MAX_SIZE - 1, "/proc/%d/ns/ipc", pid);
71     EXPECT_TRUE(ret > 0);
72     int fd = open(path, O_RDONLY);
73     EXPECT_TRUE(fd > 0);
74 
75     ret = setns(fd, CLONE_NEWIPC);
76     EXPECT_EQ(ret, 0);
77     close(fd);
78 
79     // test CLONE_NEWNET success
80     memset_s(path, PATH_MAX_SIZE, 0, sizeof(path));
81     ret = sprintf_s(path, PATH_MAX_SIZE - 1, "/proc/%d/ns/net", pid);
82     EXPECT_TRUE(ret > 0);
83     fd = open(path, O_RDONLY);
84     EXPECT_TRUE(fd > 0);
85 
86     ret = setns(fd, CLONE_NEWNET);
87     EXPECT_EQ(ret, 0);
88     close(fd);
89 
90     // test CLONE_NEWPID success
91     memset_s(path, PATH_MAX_SIZE, 0, sizeof(path));
92     ret = sprintf_s(path, PATH_MAX_SIZE - 1, "/proc/%d/ns/pid", pid);
93     EXPECT_TRUE(ret > 0);
94     fd = open(path, O_RDONLY);
95     EXPECT_TRUE(fd > 0);
96 
97     ret = setns(fd, CLONE_NEWPID);
98     EXPECT_EQ(ret, 0);
99     close(fd);
100 
101     // test CLONE_NEWUTS success
102     memset_s(path, PATH_MAX_SIZE, 0, sizeof(path));
103     ret = sprintf_s(path, PATH_MAX_SIZE - 1, "/proc/%d/ns/uts", pid);
104     EXPECT_TRUE(ret > 0);
105     fd = open(path, O_RDONLY);
106     EXPECT_TRUE(fd > 0);
107 
108     ret = setns(fd, CLONE_NEWUTS);
109     EXPECT_EQ(ret, 0);
110 
111     close(fd);
112 }
113