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 int PATH_MAX_LEN = 128;
28 static const char *TEST_FILE_PATH = "/data/local/tmp";
29 static const char *INVALID_FILE_PATH = "/data/invalid";
30 static const char *TEST_FILE = "/data/local/tmp/chdir_file";
31 mode_t MODE_0755 = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
32
33 class ChdirApiTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 private:
40 };
SetUp()41 void ChdirApiTest::SetUp()
42 {
43 }
TearDown()44 void ChdirApiTest::TearDown()
45 {
46 }
SetUpTestCase()47 void ChdirApiTest::SetUpTestCase()
48 {
49 }
TearDownTestCase()50 void ChdirApiTest::TearDownTestCase()
51 {
52 }
53
54 /*
55 * @tc.number : SUB_KERNEL_SYSCALL_CHDIR_0100
56 * @tc.name : ChdirChangeSuccess_0001
57 * @tc.desc : Chdir change work dir successfully.
58 * @tc.size : MediumTest
59 * @tc.type : Function
60 * @tc.level : Level 1
61 */
HWTEST_F(ChdirApiTest, ChdirChangeSuccess_0001, Function | MediumTest | Level1)62 HWTEST_F(ChdirApiTest, ChdirChangeSuccess_0001, Function | MediumTest | Level1)
63 {
64 int ret;
65 char path[PATH_MAX_LEN];
66 char *dir = getcwd(path, sizeof(path));
67 EXPECT_NE(dir, nullptr);
68
69 ret = chdir(TEST_FILE_PATH);
70 EXPECT_EQ(ret, 0);
71 chdir(path);
72 }
73
74 /*
75 * @tc.number : SUB_KERNEL_SYSCALL_CHDIR_0200
76 * @tc.name : ChdirNullptrPathFailed_0002
77 * @tc.desc : Chdir nullptr failed.
78 * @tc.size : MediumTest
79 * @tc.type : Function
80 * @tc.level : Level 2
81 */
HWTEST_F(ChdirApiTest, ChdirNullptrPathFailed_0002, Function | MediumTest | Level2)82 HWTEST_F(ChdirApiTest, ChdirNullptrPathFailed_0002, Function | MediumTest | Level2)
83 {
84 int ret;
85 char path[PATH_MAX_LEN];
86 char *dir = getcwd(path, sizeof(path));
87 EXPECT_NE(dir, nullptr);
88
89 errno = 0;
90 ret = chdir(nullptr);
91 EXPECT_EQ(ret, -1);
92 EXPECT_EQ(errno, EFAULT);
93 chdir(path);
94 }
95
96 /*
97 * @tc.number : SUB_KERNEL_SYSCALL_CHDIR_0300
98 * @tc.name : ChdirInvalidPathFailed_0003
99 * @tc.desc : Chdir invalid dir failed.
100 * @tc.size : MediumTest
101 * @tc.type : Function
102 * @tc.level : Level 2
103 */
HWTEST_F(ChdirApiTest, ChdirInvalidPathFailed_0003, Function | MediumTest | Level2)104 HWTEST_F(ChdirApiTest, ChdirInvalidPathFailed_0003, Function | MediumTest | Level2)
105 {
106 int ret;
107 char path[PATH_MAX_LEN];
108 char *dir = getcwd(path, sizeof(path));
109 EXPECT_NE(dir, nullptr);
110
111 errno = 0;
112 ret = chdir(INVALID_FILE_PATH);
113 EXPECT_EQ(ret, -1);
114 EXPECT_EQ(errno, ENOENT);
115 chdir(path);
116 }
117
118 /*
119 * @tc.number : SUB_KERNEL_SYSCALL_CHDIR_0400
120 * @tc.name : ChdirFileFailed_0004
121 * @tc.desc : Chdir file failed.
122 * @tc.size : MediumTest
123 * @tc.type : Function
124 * @tc.level : Level 2
125 */
HWTEST_F(ChdirApiTest, ChdirFileFailed_0004, Function | MediumTest | Level2)126 HWTEST_F(ChdirApiTest, ChdirFileFailed_0004, Function | MediumTest | Level2)
127 {
128 int ret;
129 int fd;
130 char path[PATH_MAX_LEN];
131 char *dir = getcwd(path, sizeof(path));
132 EXPECT_NE(dir, nullptr);
133
134 fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0755);
135 close(fd);
136
137 errno = 0;
138 ret = chdir(TEST_FILE);
139 EXPECT_EQ(ret, -1);
140 EXPECT_EQ(errno, ENOTDIR);
141 chdir(path);
142 unlink(TEST_FILE);
143 }