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 <climits>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
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 <sys/uio.h>
27 #include "securec.h"
28
29 using namespace testing::ext;
30 using namespace std;
31
32 class ReadlinkatApiTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp();
37 void TearDown();
38
39 private:
40 };
41
42 static const char *TEST_DIR = "/data/local/tmp/tmp";
43 static const char *TEST_FILE = "/data/local/tmp/tmp/test";
44 static const char *TEST_LINK = "/data/local/tmp/tmp/link";
45
SetUpTestCase()46 void ReadlinkatApiTest::SetUpTestCase()
47 {
48 }
49
TearDownTestCase()50 void ReadlinkatApiTest::TearDownTestCase()
51 {
52 }
53
SetUp()54 void ReadlinkatApiTest::SetUp()
55 {
56 mkdir(TEST_DIR, S_IRWXU | S_IRWXG | S_IRWXO);
57 }
58
TearDown()59 void ReadlinkatApiTest::TearDown()
60 {
61 rmdir(TEST_DIR);
62 }
63
64 /*
65 * @tc.number : SUB_KERNEL_SYSCALL_READLINKAT_0100
66 * @tc.name : ReadlinkatNormalReadSuccess_0001
67 * @tc.desc : readlinkat should read the target of a symlink in the specified directory.
68 * @tc.size : MediumTest
69 * @tc.type : Function
70 * @tc.level : Level 1
71 */
HWTEST_F(ReadlinkatApiTest, ReadlinkatNormalReadSuccess_0001, Function | MediumTest | Level1)72 HWTEST_F(ReadlinkatApiTest, ReadlinkatNormalReadSuccess_0001, Function | MediumTest | Level1)
73 {
74 int fd = -1;
75 char buf[1024] = {0};
76 const char *data = "Hello, World!";
77 int dirFd = open(TEST_DIR, O_RDONLY | O_DIRECTORY);
78 EXPECT_TRUE(dirFd >= 0);
79
80 fd = openat(dirFd, TEST_FILE, O_RDWR | O_CREAT, 0644);
81 write(fd, data, strlen(data));
82 close(fd);
83 symlink(TEST_FILE, TEST_LINK);
84 ssize_t bytesRead = readlinkat(dirFd, TEST_LINK, buf, sizeof(buf) - 1);
85 EXPECT_GE(bytesRead, 0);
86 EXPECT_STREQ(buf, TEST_FILE);
87
88 close(dirFd);
89 unlink(TEST_LINK);
90 unlink(TEST_FILE);
91 }
92
93 /*
94 * @tc.number : SUB_KERNEL_SYSCALL_READLINKAT_0200
95 * @tc.name : ReadlinkatInvalidFdFailed_0002
96 * @tc.desc : readlinkat use invalid fd, errno EBADF.
97 * @tc.size : MediumTest
98 * @tc.type : Function
99 * @tc.level : Level 2
100 */
HWTEST_F(ReadlinkatApiTest, ReadlinkatInvalidFdFailed_0002, Function | MediumTest | Level2)101 HWTEST_F(ReadlinkatApiTest, ReadlinkatInvalidFdFailed_0002, Function | MediumTest | Level2)
102 {
103 int fd = -1;
104 char buf[1024] = {0};
105 int dirFd = open(TEST_DIR, O_RDONLY | O_DIRECTORY);
106 fd = openat(dirFd, TEST_FILE, O_RDWR | O_CREAT, 0644);
107 errno = 0;
108 ssize_t bytesWritten = readlinkat(-1, TEST_FILE, buf, sizeof(buf) - 1);
109 EXPECT_EQ(bytesWritten, -1);
110 EXPECT_EQ(errno, EINVAL);
111
112 close(fd);
113 close(dirFd);
114 unlink(TEST_FILE);
115 }
116
117 /*
118 * @tc.number : SUB_KERNEL_SYSCALL_READLINKAT_0300
119 * @tc.name : ReadlinkatNonexistentLinkFailed_0003
120 * @tc.desc : readlinkat symlink which is not exist, errno ENOENT.
121 * @tc.size : MediumTest
122 * @tc.type : Function
123 * @tc.level : Level 2
124 */
HWTEST_F(ReadlinkatApiTest, ReadlinkatNonexistentLinkFailed_0003, Function | MediumTest | Level2)125 HWTEST_F(ReadlinkatApiTest, ReadlinkatNonexistentLinkFailed_0003, Function | MediumTest | Level2)
126 {
127 char buf[1024] = {0};
128 int dirFd = open(TEST_DIR, O_RDONLY);
129 unlinkat(dirFd, TEST_LINK, 0);
130
131 errno = 0;
132 ssize_t bytesRead = readlinkat(dirFd, TEST_LINK, buf, sizeof(buf) - 1);
133 EXPECT_EQ(bytesRead, -1);
134 EXPECT_EQ(errno, ENOENT);
135
136 close(dirFd);
137 }
138