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 <string>
20 #include <vector>
21 #include <fcntl.h>
22 #include <unistd.h>
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 static const int PATH_MAX_LEN = 128;
32 static const char *TEST_FILE_PATH = "/data/local/tmp";
33 static const char *TEST_FILE = "/data/local/tmp/test.txt";
34 static const char *TEST_FILE_NAME = "test.txt";
35 static const char *TEST_NOT_EXIST_FILE = "/data/local/tmp/NotExist.txt";
36 static const char *TEST_NOT_EXIST_FILE_NAME = "NotExist.txt";
37
38 class FaccessatApiTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp();
43 void TearDown();
44 private:
45 };
SetUp()46 void FaccessatApiTest::SetUp()
47 {
48 }
TearDown()49 void FaccessatApiTest::TearDown()
50 {
51 }
SetUpTestCase()52 void FaccessatApiTest::SetUpTestCase()
53 {
54 }
TearDownTestCase()55 void FaccessatApiTest::TearDownTestCase()
56 {
57 }
58
59 /*
60 * @tc.number : SUB_KERNEL_SYSCALL_FACCESSAT_0100
61 * @tc.name : FaccessatCheckSuccess_0001
62 * @tc.desc : faccessat checks the calling process can access the file in current directory successful.
63 * @tc.size : MediumTest
64 * @tc.type : Function
65 * @tc.level : Level 1
66 */
HWTEST_F(FaccessatApiTest, FaccessatCheckSuccess_0001, Function | MediumTest | Level1)67 HWTEST_F(FaccessatApiTest, FaccessatCheckSuccess_0001, Function | MediumTest | Level1)
68 {
69 int ret;
70 char path[PATH_MAX_LEN];
71 char *dir = getcwd(path, sizeof(path));
72 EXPECT_NE(dir, nullptr);
73 if (access(TEST_FILE_PATH, F_OK) != 0) {
74 ret = mkdir(TEST_FILE_PATH, 0755);
75 EXPECT_EQ(ret, 0);
76 }
77 ret = chdir(TEST_FILE_PATH);
78 EXPECT_EQ(ret, 0);
79 if (access(TEST_FILE_NAME, F_OK) == -1) {
80 FILE *fp = fopen(TEST_FILE_NAME, "w");
81 EXPECT_NE(fp, nullptr);
82 fclose(fp);
83 }
84 ret = faccessat(AT_FDCWD, TEST_FILE_NAME, F_OK, AT_EACCESS);
85 EXPECT_EQ(ret, 0);
86
87 remove(TEST_FILE);
88 chdir(path);
89 }
90
91 /*
92 * @tc.number : SUB_KERNEL_SYSCALL_FACCESSAT_0200
93 * @tc.name : FaccessatPermissionCheckTest_0002
94 * @tc.desc : faccessat check user's permissions of valid fd success.
95 * @tc.size : MediumTest
96 * @tc.type : Function
97 * @tc.level : Level 1
98 */
HWTEST_F(FaccessatApiTest, FaccessatPermissionCheckTest_0002, Function | MediumTest | Level1)99 HWTEST_F(FaccessatApiTest, FaccessatPermissionCheckTest_0002, Function | MediumTest | Level1)
100 {
101 int ret;
102 int fd = open(TEST_FILE, O_RDWR | O_CREAT, 0755);
103 EXPECT_TRUE(fd > 0);
104
105 ret = faccessat(fd, TEST_FILE, F_OK, AT_EACCESS);
106 EXPECT_TRUE(ret == 0);
107
108 ret = faccessat(fd, TEST_FILE, R_OK, AT_EACCESS);
109 EXPECT_TRUE(ret == 0);
110
111 ret = faccessat(fd, TEST_FILE, W_OK, AT_EACCESS);
112 EXPECT_TRUE(ret == 0);
113
114 ret = faccessat(fd, TEST_FILE, X_OK, AT_EACCESS);
115 EXPECT_TRUE(ret == 0);
116
117 close(fd);
118 remove(TEST_FILE);
119 }
120
121 /*
122 * @tc.number : SUB_KERNEL_SYSCALL_FACCESSAT_0300
123 * @tc.name : FaccessatCheckInvalidFdFail_0003
124 * @tc.desc : faccessat check invalid fd permission fail, errno ENOENT.
125 * @tc.size : MediumTest
126 * @tc.type : Function
127 * @tc.level : Level 2
128 */
HWTEST_F(FaccessatApiTest, FaccessatCheckInvalidFdFail_0003, Function | MediumTest | Level2)129 HWTEST_F(FaccessatApiTest, FaccessatCheckInvalidFdFail_0003, Function | MediumTest | Level2)
130 {
131 int ret;
132 int invalidFd = -1;
133 errno = 0;
134 ret = faccessat(invalidFd, TEST_NOT_EXIST_FILE, F_OK, AT_EACCESS);
135 EXPECT_EQ(ret, -1);
136 EXPECT_EQ(errno, ENOENT);
137 }
138
139 /*
140 * @tc.number : SUB_KERNEL_SYSCALL_FACCESSAT_0400
141 * @tc.name : FaccessatCheckFail_0004
142 * @tc.desc : faccessat check non-exist file permission fail, errno ENOENT.
143 * @tc.size : MediumTest
144 * @tc.type : Function
145 * @tc.level : Level 2
146 */
HWTEST_F(FaccessatApiTest, FaccessatCheckFail_0004, Function | MediumTest | Level2)147 HWTEST_F(FaccessatApiTest, FaccessatCheckFail_0004, Function | MediumTest | Level2)
148 {
149 errno = 0;
150 int ret = faccessat(AT_FDCWD, TEST_NOT_EXIST_FILE_NAME, W_OK, AT_EACCESS);
151 EXPECT_EQ(ret, -1);
152 EXPECT_EQ(errno, ENOENT);
153 }
154