• 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 <cstdlib>
17 #include <cstdio>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <gtest/gtest.h>
23 
24 using namespace testing::ext;
25 using namespace std;
26 
27 static const char *INVALID_FILE_PATH = "/data/invalid";
28 static const char *TEST_FILE = "/data/local/tmp/chroot_file";
29 mode_t MODE_0755 = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
30 
31 class ChrootApiTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp();
36     void TearDown();
37 private:
38 };
SetUp()39 void ChrootApiTest::SetUp()
40 {
41 }
TearDown()42 void ChrootApiTest::TearDown()
43 {
44 }
SetUpTestCase()45 void ChrootApiTest::SetUpTestCase()
46 {
47 }
TearDownTestCase()48 void ChrootApiTest::TearDownTestCase()
49 {
50 }
51 
52 /*
53  * @tc.number : SUB_KERNEL_SYSCALL_CHDIR_0200
54  * @tc.name   : ChrootNullptrPathFailed_0002
55  * @tc.desc   : Chroot nullptr failed.
56  * @tc.size   : MediumTest
57  * @tc.type   : Function
58  * @tc.level  : Level 2
59  */
60 HWTEST_F(ChrootApiTest, ChrootNullptrPathFailed_0002, Function | MediumTest | Level2)
61 {
62     int ret;
63     errno = 0;
64     ret = chroot(nullptr);
65     EXPECT_EQ(ret, -1);
66     EXPECT_EQ(errno, EFAULT);
67 }
68 
69 /*
70  * @tc.number : SUB_KERNEL_SYSCALL_CHDIR_0300
71  * @tc.name   : ChrootInvalidPathFailed_0003
72  * @tc.desc   : Chroot invalid dir failed.
73  * @tc.size   : MediumTest
74  * @tc.type   : Function
75  * @tc.level  : Level 2
76  */
77 HWTEST_F(ChrootApiTest, ChrootInvalidPathFailed_0003, Function | MediumTest | Level2)
78 {
79     int ret;
80     errno = 0;
81     ret = chroot(INVALID_FILE_PATH);
82     EXPECT_EQ(ret, -1);
83     EXPECT_EQ(errno, ENOENT);
84 }
85 
86 /*
87  * @tc.number : SUB_KERNEL_SYSCALL_CHDIR_0400
88  * @tc.name   : ChrootFileFailed_0004
89  * @tc.desc   : Chroot file failed.
90  * @tc.size   : MediumTest
91  * @tc.type   : Function
92  * @tc.level  : Level 2
93  */
94 HWTEST_F(ChrootApiTest, ChrootFileFailed_0004, Function | MediumTest | Level2)
95 {
96     int ret;
97     int fd;
98     fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0755);
99     close(fd);
100 
101     errno = 0;
102     ret = chroot(TEST_FILE);
103     EXPECT_EQ(ret, -1);
104     EXPECT_EQ(errno, ENOTDIR);
105     unlink(TEST_FILE);
106 }