• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include <sched.h>
3 
4 using namespace testing::ext;
5 
6 class LinuxUnshareTest : public testing::Test {
SetUp()7     void SetUp() override {}
TearDown()8     void TearDown() override {}
9 };
10 constexpr int NUM = -10;
11 /**
12  * @tc.name: unshare_001
13  * @tc.desc: Verify that the unshare function can successfully create a new PID namespace by using the
14  *           CLONE_NEWPID flag.
15  * @tc.type: FUNC
16  **/
17 HWTEST_F(LinuxUnshareTest, unshare_001, TestSize.Level1)
18 {
19     pid_t pid = fork();
20     ASSERT_NE(-1, pid);
21 
22     if (pid == 0) {
23         int result = unshare(CLONE_NEWPID);
24         EXPECT_EQ(0, result);
25         exit(0);
26     } else {
27         int status;
28         if (waitpid(pid, &status, 0) == -1) {
29             perror("waitpid");
30         }
31     }
32 }
33 
34 /**
35  * @tc.name: unshare_002
36  * @tc.desc: Verify the behavior of the unshare() function when an invalid flag is provided as an argument,
37  *           expecting the function call to fail and return -1.
38  * @tc.type: FUNC
39  **/
40 HWTEST_F(LinuxUnshareTest, unshare_002, TestSize.Level1)
41 {
42     int result = unshare(NUM);
43     EXPECT_EQ(-1, result);
44 }