• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <dirent.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <arpa/inet.h>
25 #include <gtest/gtest.h>
26 #include <netinet/in.h>
27 #include <sys/stat.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
30 
31 using namespace testing::ext;
32 
33 
34 static const char *UNLINKAT_TEST_FILE = "/data/local/tmp/tryUnlinkat.txt";
35 static const char *UNLINKAT_TEST_DIR = "/data/local/tmp";
36 static const char *UNLINKAT_TEST_FILENAME = "tryUnlinkat.txt";
37 
38 class HatsUnlinkatTest : 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 HatsUnlinkatTest::SetUp()
47 {
48 }
TearDown()49 void HatsUnlinkatTest::TearDown()
50 {
51 }
SetUpTestCase()52 void HatsUnlinkatTest::SetUpTestCase()
53 {
54 }
TearDownTestCase()55 void HatsUnlinkatTest::TearDownTestCase()
56 {
57     unlink(UNLINKAT_TEST_FILE);
58 }
59 
60 
61 /*
62  * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0100
63  * @tc.name   : UnlinkatUnlinkFileSuccess_0001
64  * @tc.desc   : Unlinkat unlink file success.
65  * @tc.size   : MediumTest
66  * @tc.type   : Function
67  * @tc.level  : Level 1
68  */
69 HWTEST_F(HatsUnlinkatTest, UnlinkatUnlinkFileSuccess_0001, Function | MediumTest | Level1)
70 {
71     int fileFd;
72     int dirFd;
73     int ret;
74 
75     fileFd = open(UNLINKAT_TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
76     EXPECT_TRUE(fileFd >= 3);
77     close(fileFd);
78 
79     dirFd = open(UNLINKAT_TEST_DIR, O_RDONLY);
80     EXPECT_TRUE(dirFd >= 3);
81 
82     ret = unlinkat(dirFd, UNLINKAT_TEST_FILENAME, 0);
83     EXPECT_TRUE(ret == 0);
84     close(dirFd);
85 
86     fileFd = open(UNLINKAT_TEST_FILE, O_RDWR, 0644);
87     EXPECT_TRUE(fileFd == -1);
88     close(fileFd);
89 }
90 
91 /*
92  * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0200
93  * @tc.name   : UnlinkatUnlinkEmptyDirectorySuccess_0002
94  * @tc.desc   : Unlinkat unlink an empty directory success.
95  * @tc.size   : MediumTest
96  * @tc.type   : Function
97  * @tc.level  : Level 1
98  */
99 HWTEST_F(HatsUnlinkatTest, UnlinkatUnlinkEmptyDirectorySuccess_0002, Function | MediumTest | Level1)
100 {
101     int ret;
102     DIR *testDir;
103     struct dirent *dirEntry;
104     const char *unlinkatNewDir1 = "/data/local/tmp/unlinkatDir1";
105 
106 
107     ret = mkdir(unlinkatNewDir1, 0777);
108     if (ret == -1) {
109         testDir = opendir(unlinkatNewDir1);
110         EXPECT_TRUE(testDir != nullptr);
111 
112         dirEntry = readdir(testDir);
113         EXPECT_TRUE(dirEntry != nullptr);
114 
115         ret = strcmp(dirEntry->d_name, ".");
116         EXPECT_TRUE(ret == 0);
117     }
118 
119     ret = unlinkat(AT_FDCWD, unlinkatNewDir1, AT_REMOVEDIR);
120     EXPECT_TRUE(ret == 0);
121 }
122 
123 /*
124  * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0300
125  * @tc.name   : UnlinkatUnlinkNonemptyDirectoryFail_0003
126  * @tc.desc   : Unlinkat unlink a non-empty directory fail.
127  * @tc.size   : MediumTest
128  * @tc.type   : Function
129  * @tc.level  : Level 2
130  */
131 HWTEST_F(HatsUnlinkatTest, UnlinkatUnlinkNonemptyDirectoryFail_0003, Function | MediumTest | Level2)
132 {
133     int fileFd;
134     int ret;
135     DIR *testDir;
136     struct dirent *dirEntry;
137     const char *unlinkatNewDir2 = "/data/local/tmp/unlinkatDir2";
138     const char *unlinkatNewFile2 = "/data/local/tmp/unlinkatDir2/testFile.txt";
139 
140     ret = mkdir(unlinkatNewDir2, 0777);
141     if (ret == -1) {
142         testDir = opendir(unlinkatNewDir2);
143         EXPECT_TRUE(testDir != nullptr);
144 
145         dirEntry = readdir(testDir);
146         EXPECT_TRUE(dirEntry != nullptr);
147 
148         ret = strcmp(dirEntry->d_name, ".");
149         if (ret == 0) {
150             fileFd = open(unlinkatNewFile2, O_RDWR | O_CREAT, 0777);
151             EXPECT_TRUE(fileFd >= 3);
152             close(fileFd);
153         }
154     } else {
155         fileFd = open(unlinkatNewFile2, O_RDWR | O_CREAT, 0777);
156         EXPECT_TRUE(fileFd >= 3);
157         close(fileFd);
158     }
159     errno = 0;
160     ret = unlinkat(AT_FDCWD, unlinkatNewDir2, AT_REMOVEDIR);
161     EXPECT_TRUE(ret == -1);
162     EXPECT_EQ(errno, ENOTEMPTY);
163 
164     ret = unlinkat(AT_FDCWD, unlinkatNewFile2, 0);
165     EXPECT_TRUE(ret == 0);
166 
167     ret = unlinkat(AT_FDCWD, unlinkatNewDir2, AT_REMOVEDIR);
168     EXPECT_TRUE(ret == 0);
169 }
170 
171 /*
172  * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0400
173  * @tc.name   : UnlinkatNonexistFileFail_0004
174  * @tc.desc   : Unlinkat a non-exist file fail.
175  * @tc.size   : MediumTest
176  * @tc.type   : Function
177  * @tc.level  : Level 2
178  */
179 HWTEST_F(HatsUnlinkatTest, UnlinkatNonexistFileFail_0004, Function | MediumTest | Level2)
180 {
181     int fileFd;
182     int dirFd;
183     int ret;
184 
185     remove(UNLINKAT_TEST_FILE);
186     fileFd = open(UNLINKAT_TEST_FILE, O_RDWR | O_TRUNC, 0644);
187     EXPECT_TRUE(fileFd == -1);
188     close(fileFd);
189 
190     dirFd = open(UNLINKAT_TEST_DIR, O_RDONLY);
191     EXPECT_TRUE(dirFd >= 3);
192 
193     errno = 0;
194     ret = unlinkat(dirFd, UNLINKAT_TEST_FILENAME, 0);
195     EXPECT_TRUE(ret == -1);
196     EXPECT_EQ(errno, ENOENT);
197     close(dirFd);
198 }
199 
200 /*
201  * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0500
202  * @tc.name   : UnlinkatDirectoryWhenFlagIsNotRemovedirFail_0005
203  * @tc.desc   : Unlinkat unlink a directory when flag is not set to AT_REMOVEDIR fail.
204  * @tc.size   : MediumTest
205  * @tc.type   : Function
206  * @tc.level  : Level 2
207  */
208 HWTEST_F(HatsUnlinkatTest, UnlinkatDirectoryWhenFlagIsNotRemovedirFail_0005, Function | MediumTest | Level2)
209 {
210     int ret;
211     DIR *testDir;
212     struct dirent *dirEntry;
213     const char *unlinkatNewDir3 = "/data/local/tmp/unlinkatDir3";
214 
215     ret = mkdir(unlinkatNewDir3, 0777);
216     if (ret == -1) {
217         testDir = opendir(unlinkatNewDir3);
218         EXPECT_TRUE(testDir != nullptr);
219 
220         dirEntry = readdir(testDir);
221         EXPECT_TRUE(dirEntry != nullptr);
222 
223         ret = strcmp(dirEntry->d_name, ".");
224         EXPECT_TRUE(ret == 0);
225     }
226     errno = 0;
227     ret = unlinkat(AT_FDCWD, unlinkatNewDir3, 0);
228     EXPECT_TRUE(ret == -1);
229     EXPECT_EQ(errno, EISDIR);
230 
231     ret = unlinkat(AT_FDCWD, unlinkatNewDir3, AT_REMOVEDIR);
232     EXPECT_TRUE(ret == 0);
233 }
234 
235 /*
236  * @tc.number : SUB_KERNEL_SYSCALL_UNLINKAT_0600
237  * @tc.name   : UnlinkatFileWhenFlagIsNot0Fail_0006
238  * @tc.desc   : Unlinkat a file when flag is not set 0 fail.
239  * @tc.size   : MediumTest
240  * @tc.type   : Function
241  * @tc.level  : Level 2
242  */
243 HWTEST_F(HatsUnlinkatTest, UnlinkatFileWhenFlagIsNot0Fail_0006, Function | MediumTest | Level2)
244 {
245     int fileFd;
246     int dirFd;
247     int ret;
248 
249     fileFd = open(UNLINKAT_TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
250     EXPECT_TRUE(fileFd >= 3);
251     close(fileFd);
252 
253     dirFd = open(UNLINKAT_TEST_DIR, O_RDONLY);
254     EXPECT_TRUE(dirFd >= 3);
255 
256     errno = 0;
257     ret = unlinkat(dirFd, UNLINKAT_TEST_FILENAME, AT_REMOVEDIR);
258     EXPECT_TRUE(ret == -1);
259     EXPECT_EQ(errno, ENOTDIR);
260 
261     ret = unlinkat(dirFd, UNLINKAT_TEST_FILENAME, 0);
262     EXPECT_TRUE(ret == 0);
263     close(dirFd);
264 }