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 <arpa/inet.h>
24 #include <gtest/gtest.h>
25 #include <netinet/in.h>
26 #include <sys/stat.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include "securec.h"
30
31 using namespace testing::ext;
32
33 static const char *TEST_VALID_PATH = "/data/local/tmp/test_dir";
34 static const char *TEST_VALID_FILE = "/data/local/tmp/test_dir/test.txt";
35 static const char *TEST_LINK_PATH = "/data/local/tmp/symlinkat_test_dir";
36 static const char *TEST_LINK_FILE = "/data/local/tmp/test_dir/symlinkat_test.txt";
37 static const char *TEST_NOACCESS_PATH = "/data/local/abcd/abcd";
38
39 class HatsSymlinkatTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp();
44 void TearDown();
45 private:
46 };
SetUp()47 void HatsSymlinkatTest::SetUp()
48 {
49 int fd = -1;
50 if (access(TEST_VALID_PATH, F_OK) == 0) {
51 (void)remove(TEST_VALID_PATH);
52 }
53 (void)mkdir(TEST_VALID_PATH, S_IWUSR | S_IRUSR | S_IXUSR);
54 fd = open(TEST_VALID_FILE, O_WRONLY | O_CREAT, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
55 close(fd);
56 }
TearDown()57 void HatsSymlinkatTest::TearDown()
58 {
59 (void)remove(TEST_VALID_FILE);
60 (void)remove(TEST_LINK_FILE);
61 (void)remove(TEST_VALID_PATH);
62 (void)remove(TEST_LINK_PATH);
63 }
SetUpTestCase()64 void HatsSymlinkatTest::SetUpTestCase()
65 {
66 }
TearDownTestCase()67 void HatsSymlinkatTest::TearDownTestCase()
68 {
69 }
70
71 /*
72 * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0100
73 * @tc.name : SymlinkatFileAndPathSuccess_0001
74 * @tc.desc : symlinkat create file and path symlink success.
75 * @tc.size : MediumTest
76 * @tc.type : Function
77 * @tc.level : Level 1
78 */
HWTEST_F(HatsSymlinkatTest, SymlinkatFileAndPathSuccess_0001, Function | MediumTest | Level1)79 HWTEST_F(HatsSymlinkatTest, SymlinkatFileAndPathSuccess_0001, Function | MediumTest | Level1)
80 {
81 // symlinkat create file symlink success
82 int ret = symlinkat(TEST_VALID_FILE, AT_FDCWD, TEST_LINK_FILE);
83 EXPECT_EQ(ret, 0);
84 EXPECT_EQ(access(TEST_LINK_FILE, F_OK), 0);
85
86 // symlinkat create path symlink success
87 ret = symlinkat(TEST_VALID_PATH, AT_FDCWD, TEST_LINK_PATH);
88 EXPECT_EQ(ret, 0);
89 EXPECT_EQ(access(TEST_LINK_PATH, F_OK), 0);
90 }
91
92 /*
93 * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0200
94 * @tc.name : SymlinkatNoExistLinkPathFailed_0002
95 * @tc.desc : symlinkat create path symlink which is no exist path failed, errno ENOENT.
96 * @tc.size : MediumTest
97 * @tc.type : Function
98 * @tc.level : Level 1
99 */
HWTEST_F(HatsSymlinkatTest, SymlinkatNoPermissionPathFailed_0002, Function | MediumTest | Level1)100 HWTEST_F(HatsSymlinkatTest, SymlinkatNoPermissionPathFailed_0002, Function | MediumTest | Level1)
101 {
102 errno = 0;
103 int ret = symlinkat(TEST_VALID_PATH, AT_FDCWD, TEST_NOACCESS_PATH);
104 EXPECT_EQ(ret, -1);
105 EXPECT_EQ(errno, ENOENT);
106 }
107
108 /*
109 * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0300
110 * @tc.name : SymlinkatExistPathFailed_0003
111 * @tc.desc : symlinkat create path symlink which is already exist failed, errno EEXIST.
112 * @tc.size : MediumTest
113 * @tc.type : Function
114 * @tc.level : Level 1
115 */
HWTEST_F(HatsSymlinkatTest, SymlinkatExistPathFailed_0003, Function | MediumTest | Level1)116 HWTEST_F(HatsSymlinkatTest, SymlinkatExistPathFailed_0003, Function | MediumTest | Level1)
117 {
118 int ret;
119 if (access(TEST_LINK_PATH, F_OK) != 0) {
120 ret = mkdir(TEST_LINK_PATH, S_IWUSR | S_IRUSR | S_IXUSR);
121 EXPECT_EQ(ret, 0);
122 }
123
124 errno = 0;
125 ret = symlinkat(TEST_VALID_PATH, AT_FDCWD, TEST_LINK_PATH);
126 EXPECT_EQ(ret, -1);
127 EXPECT_EQ(errno, EEXIST);
128 }
129
130 /*
131 * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0400
132 * @tc.name : SymlinkatLinkPathLongerFailed_0004
133 * @tc.desc : symlinkat create path symlink which the name is too longer failed, errno ENAMETOOLONG.
134 * @tc.size : MediumTest
135 * @tc.type : Function
136 * @tc.level : Level 1
137 */
HWTEST_F(HatsSymlinkatTest, SymlinkatLinkPathLongerFailed_0004, Function | MediumTest | Level1)138 HWTEST_F(HatsSymlinkatTest, SymlinkatLinkPathLongerFailed_0004, Function | MediumTest | Level1)
139 {
140 int ret;
141 char name[256];
142 (void)memset_s(name, sizeof(name), 's', sizeof(name));
143
144 errno = 0;
145 ret = symlinkat(TEST_VALID_PATH, AT_FDCWD, name);
146 EXPECT_EQ(ret, -1);
147 EXPECT_EQ(errno, ENAMETOOLONG);
148 }
149
150 /*
151 * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0500
152 * @tc.name : SymlinkatLinkPathNullptrFailed_0005
153 * @tc.desc : symlinkat create path symlink is nullptr failed, errno EFAULT.
154 * @tc.size : MediumTest
155 * @tc.type : Function
156 * @tc.level : Level 1
157 */
HWTEST_F(HatsSymlinkatTest, SymlinkatLinkPathNullptrFailed_0005, Function | MediumTest | Level1)158 HWTEST_F(HatsSymlinkatTest, SymlinkatLinkPathNullptrFailed_0005, Function | MediumTest | Level1)
159 {
160 errno = 0;
161 int ret = symlinkat(TEST_VALID_PATH, AT_FDCWD, nullptr);
162 EXPECT_EQ(ret, -1);
163 EXPECT_EQ(errno, EFAULT);
164 }
165
166 /*
167 * @tc.number : SUB_KERNEL_SYSCALL_SYMLINKAT_0600
168 * @tc.name : SymlinkatInvalidFdFailed_0006
169 * @tc.desc : symlinkat used invalid fd failed, errno EBADF.
170 * @tc.size : MediumTest
171 * @tc.type : Function
172 * @tc.level : Level 1
173 */
HWTEST_F(HatsSymlinkatTest, SymlinkatInvalidFdFailed_0006, Function | MediumTest | Level1)174 HWTEST_F(HatsSymlinkatTest, SymlinkatInvalidFdFailed_0006, Function | MediumTest | Level1)
175 {
176 errno = 0;
177 int ret = symlinkat("test_path", -1, "symlinkat_test_dir");
178 EXPECT_EQ(ret, -1);
179 EXPECT_EQ(errno, EBADF);
180 }
181