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 <cstdio>
17 #include <cstdlib>
18 #include <cerrno>
19 #include <fcntl.h>
20 #include <string>
21 #include <unistd.h>
22 #include <vector>
23 #include <gtest/gtest.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include "securec.h"
27 
28 using namespace testing::ext;
29 using namespace std;
30 
31 class FchdirApiTest : 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 FchdirApiTest::SetUp()
40 {
41 }
TearDown()42 void FchdirApiTest::TearDown()
43 {
44 }
SetUpTestCase()45 void FchdirApiTest::SetUpTestCase()
46 {
47 }
TearDownTestCase()48 void FchdirApiTest::TearDownTestCase()
49 {
50 }
51 
52 static const int PATH_MAX_LEN = 128;
53 static const char *TEST_FILE = "/data/local/tmp/fchdir.txt";
54 static const char *TEST_DIR = "/data/local/tmp/fchdirTest";
55 static const mode_t MODE_0644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
56 
57 /*
58  * @tc.number : SUB_KERNEL_SYSCALL_FCHDIR_0100
59  * @tc.name   : FchdirChangeDirectorySuccess_0001
60  * @tc.desc   : fchdir change the current working directory and enter valid directory success.
61  * @tc.size   : MediumTest
62  * @tc.type   : Function
63  * @tc.level  : Level 1
64  */
HWTEST_F(FchdirApiTest, FchdirChangeDirectorySuccess_0001, Function | MediumTest | Level1)65 HWTEST_F(FchdirApiTest, FchdirChangeDirectorySuccess_0001, Function | MediumTest | Level1)
66 {
67     int ret;
68     char path[PATH_MAX_LEN] = { 0 };
69     if (access(TEST_DIR, F_OK) != 0) {
70         ret = mkdir(TEST_DIR, 0755);
71         EXPECT_EQ(ret, 0);
72     }
73 
74     int dirfd = open(TEST_DIR, O_RDONLY | O_DIRECTORY);
75     EXPECT_TRUE(dirfd > 0);
76     ret = fchdir(dirfd);
77     EXPECT_EQ(ret, 0);
78     char *dir = getcwd(path, sizeof(path));
79     EXPECT_NE(dir, nullptr);
80     EXPECT_STREQ(dir, TEST_DIR);
81 
82     close(dirfd);
83     remove(TEST_DIR);
84 }
85 
86 /*
87  * @tc.number : SUB_KERNEL_SYSCALL_FCHDIR_0200
88  * @tc.name   : FchdirChangeDirectoryFail_0002
89  * @tc.desc   : fchdir change the current working directory and enter invalid directory fail, errno EBADF.
90  * @tc.size   : MediumTest
91  * @tc.type   : Function
92  * @tc.level  : Level 2
93  */
HWTEST_F(FchdirApiTest, FchdirChangeDirectoryFail_0002, Function | MediumTest | Level2)94 HWTEST_F(FchdirApiTest, FchdirChangeDirectoryFail_0002, Function | MediumTest | Level2)
95 {
96     int ret;
97     int fd = -1;
98     errno = 0;
99     ret = fchdir(fd);
100     EXPECT_EQ(ret, -1);
101     EXPECT_EQ(errno, EBADF);
102 }
103 
104 /*
105  * @tc.number : SUB_KERNEL_SYSCALL_FCHDIR_0300
106  * @tc.name   : FchdirChangeFileDescriptorFail_0003
107  * @tc.desc   : fchdir change into a file fail, errno ENOTDIR.
108  * @tc.size   : MediumTest
109  * @tc.type   : Function
110  * @tc.level  : Level 2
111  */
HWTEST_F(FchdirApiTest, FchdirChangeFileDescriptorFail_0003, Function | MediumTest | Level2)112 HWTEST_F(FchdirApiTest, FchdirChangeFileDescriptorFail_0003, Function | MediumTest | Level2)
113 {
114     int ret;
115     int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644);
116     EXPECT_TRUE(fd > 0);
117 
118     errno = 0;
119     ret = fchdir(fd);
120     EXPECT_EQ(ret, -1);
121     EXPECT_EQ(errno, ENOTDIR);
122 
123     close(fd);
124     remove(TEST_FILE);
125 }
126