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
32 using namespace testing::ext;
33
34 static const char *UTIMENSAT_TEST_FILE = "/data/local/tmp/tryUtimensat.txt";
35 static const int ACCESS_TIME_SEC = 10;
36 static const int ACCESS_TIME_NSEC = 0;
37 static const int MODIFICATION_TIME_SEC = 11;
38 static const int MODIFICATION_TIME_NSEC = 0;
39
40 class HatsUtimensatTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 private:
47 };
SetUp()48 void HatsUtimensatTest::SetUp()
49 {
50 }
TearDown()51 void HatsUtimensatTest::TearDown()
52 {
53 }
SetUpTestCase()54 void HatsUtimensatTest::SetUpTestCase()
55 {
56 }
TearDownTestCase()57 void HatsUtimensatTest::TearDownTestCase()
58 {
59 unlink(UTIMENSAT_TEST_FILE);
60 }
61
62
63 /*
64 * @tc.number : SUB_KERNEL_SYSCALL_UTIMENSAT_0100
65 * @tc.name : UtimensatChangeFileTimeSuccess_0001
66 * @tc.desc : utimensat change file access time and modification time success.
67 * @tc.size : MediumTest
68 * @tc.type : Function
69 * @tc.level : Level 1
70 */
71 HWTEST_F(HatsUtimensatTest, UtimensatChangeFileTimeSuccess_0001, Function | MediumTest | Level1)
72 {
73 int ret;
74 int fd;
75 struct timespec newTime[2];
76 struct stat fileStat1;
77 struct stat fileStat2;
78
79 fd = open(UTIMENSAT_TEST_FILE, O_RDONLY);
80 if (fd != -1) {
81 ret = remove(UTIMENSAT_TEST_FILE);
82 EXPECT_TRUE(ret == 0);
83 }
84 close(fd);
85
86 fd = open(UTIMENSAT_TEST_FILE, O_RDONLY | O_CREAT | O_TRUNC, 0644);
87 EXPECT_TRUE(fd >= 3);
88 close(fd);
89
90 ret = stat(UTIMENSAT_TEST_FILE, &fileStat1);
91 EXPECT_TRUE(ret == 0);
92
93 //access time
94 newTime[0].tv_sec = ACCESS_TIME_SEC;
95 newTime[0].tv_nsec = ACCESS_TIME_NSEC;
96 //modification time
97 newTime[1].tv_sec = MODIFICATION_TIME_SEC;
98 newTime[1].tv_nsec = MODIFICATION_TIME_NSEC;
99
100 ret = utimensat(AT_FDCWD, UTIMENSAT_TEST_FILE, newTime, 0);
101 EXPECT_TRUE(ret == 0);
102
103 ret = stat(UTIMENSAT_TEST_FILE, &fileStat2);
104 EXPECT_TRUE(ret == 0);
105
106 ret = (fileStat1.st_atime != fileStat2.st_atime);
107 EXPECT_TRUE(ret == 1);
108 ret = (fileStat1.st_mtime != fileStat2.st_mtime);
109 EXPECT_TRUE(ret == 1);
110 }
111
112 /*
113 * @tc.number : SUB_KERNEL_SYSCALL_UTIMENSAT_0200
114 * @tc.name : UtimensatInvalidTimeFail_0002
115 * @tc.desc : utimensat set time using invalid time value fail.
116 * @tc.size : MediumTest
117 * @tc.type : Function
118 * @tc.level : Level 2
119 */
120 HWTEST_F(HatsUtimensatTest, UtimensatInvalidTimeFail_0002, Function | MediumTest | Level2)
121 {
122 int ret;
123 int fd;
124 struct timespec newTime[2];
125 struct stat fileStat1;
126
127 fd = open(UTIMENSAT_TEST_FILE, O_RDONLY);
128 if (fd != -1) {
129 ret = remove(UTIMENSAT_TEST_FILE);
130 EXPECT_TRUE(ret == 0);
131 }
132 close(fd);
133
134 fd = open(UTIMENSAT_TEST_FILE, O_RDONLY | O_CREAT | O_TRUNC, 0644);
135 EXPECT_TRUE(fd >= 3);
136 close(fd);
137
138 ret = stat(UTIMENSAT_TEST_FILE, &fileStat1);
139 EXPECT_TRUE(ret == 0);
140
141 //access time
142 newTime[0].tv_sec = -1;
143 newTime[0].tv_nsec = -1;
144 //modification time
145 newTime[1].tv_sec = -1;
146 newTime[1].tv_nsec = -1;
147
148 errno = 0;
149 ret = utimensat(AT_FDCWD, UTIMENSAT_TEST_FILE, newTime, 0);
150 EXPECT_TRUE(ret == -1);
151 EXPECT_EQ(errno, EINVAL);
152 }
153
154 /*
155 * @tc.number : SUB_KERNEL_SYSCALL_UTIMENSAT_0300
156 * @tc.name : UtimensatSetTimeOfNonexistFileFail_0003
157 * @tc.desc : utimensat set time of a non-exist file fail.
158 * @tc.size : MediumTest
159 * @tc.type : Function
160 * @tc.level : Level 2
161 */
162 HWTEST_F(HatsUtimensatTest, UtimensatSetTimeOfNonexistFileFail_0003, Function | MediumTest | Level2)
163 {
164 int ret;
165 int fd;
166 struct timespec newTime[2];
167
168 fd = open(UTIMENSAT_TEST_FILE, O_RDONLY);
169 if (fd != -1) {
170 ret = remove(UTIMENSAT_TEST_FILE);
171 EXPECT_TRUE(ret == 0);
172 }
173 close(fd);
174
175 //access time
176 newTime[0].tv_sec = ACCESS_TIME_SEC;
177 newTime[0].tv_nsec = ACCESS_TIME_NSEC;
178 //modification time
179 newTime[1].tv_sec = MODIFICATION_TIME_SEC;
180 newTime[1].tv_nsec = MODIFICATION_TIME_NSEC;
181
182 errno = 0;
183 ret = utimensat(AT_FDCWD, UTIMENSAT_TEST_FILE, newTime, 0);
184 EXPECT_TRUE(ret == -1);
185 EXPECT_EQ(errno, ENOENT);
186 }
187
188 /*
189 * @tc.number : SUB_KERNEL_SYSCALL_UTIMENSAT_0400
190 * @tc.name : UtimensatInvalidFlagFail_0004
191 * @tc.desc : utimensat change time with invalid flag fail.
192 * @tc.size : MediumTest
193 * @tc.type : Function
194 * @tc.level : Level 2
195 */
196 HWTEST_F(HatsUtimensatTest, UtimensatInvalidFlagFail_0004, Function | MediumTest | Level2)
197 {
198 int ret;
199 int fd;
200 struct timespec newTime[2];
201
202 fd = open(UTIMENSAT_TEST_FILE, O_RDONLY);
203 if (fd != -1) {
204 ret = remove(UTIMENSAT_TEST_FILE);
205 EXPECT_TRUE(ret == 0);
206 }
207 close(fd);
208
209 fd = open(UTIMENSAT_TEST_FILE, O_RDONLY | O_CREAT | O_TRUNC, 0644);
210 EXPECT_TRUE(fd >= 3);
211 close(fd);
212
213 //access time
214 newTime[0].tv_sec = ACCESS_TIME_SEC;
215 newTime[0].tv_nsec = ACCESS_TIME_NSEC;
216 //modification time
217 newTime[1].tv_sec = MODIFICATION_TIME_SEC;
218 newTime[1].tv_nsec = MODIFICATION_TIME_NSEC;
219
220 errno = 0;
221 ret = utimensat(AT_FDCWD, UTIMENSAT_TEST_FILE, newTime, -1);
222 EXPECT_TRUE(ret == -1);
223 EXPECT_EQ(errno, EINVAL);
224 }
225