1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
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 <fcntl.h>
17 #include <sys/stat.h>
18 #include <sys/time.h>
19 #include <unistd.h>
20 #include "functionalext.h"
21
22 const int FAIL = -1;
23 const int SUCCESS = 0;
24
25 extern int __futimes_time64(int, const struct timeval [2]);
26
27 /**
28 * @tc.name : futimes_0100
29 * @tc.desc : All parameters are valid, TV is not 0, and futimes can modify the timestamp of the file.
30 * @tc.level : Level 0
31 */
futimes_0100(void)32 void futimes_0100(void)
33 {
34 int ret = -1;
35 struct stat s;
36 static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}};
37 tv[0].tv_sec = s.st_atime;
38 tv[0].tv_usec = 0;
39 tv[1].tv_sec = s.st_mtime;
40 tv[1].tv_usec = 0;
41 int fd = open("futimes.txt", O_RDWR | O_CREAT, 777);
42 ret = futimes(fd, tv);
43 EXPECT_EQ("futimes_0100", ret, SUCCESS);
44 remove("futimes.txt");
45 }
46
47 /**
48 * @tc.name : futimes_0200
49 * @tc.desc : All parameters are valid, TV is 0, and futimes can modify the timestamp of the file.
50 * @tc.level : Level 1
51 */
futimes_0200(void)52 void futimes_0200(void)
53 {
54 int ret = -1;
55 int fd = open("futimes.txt", O_RDWR | O_CREAT, 777);
56 ret = futimes(fd, 0);
57 EXPECT_EQ("futimes_0200", ret, SUCCESS);
58 remove("futimes.txt");
59 }
60
61 /**
62 * @tc.name : futimes_0300
63 * @tc.desc : The fd argument is invalid and the futimes function cannot modify the timestamp of the file.
64 * @tc.level : Level 2
65 */
futimes_0300(void)66 void futimes_0300(void)
67 {
68 int ret = -1;
69 static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}};
70 int fd = open("futimes.txt", O_RDWR);
71 ret = futimes(fd, tv);
72 EXPECT_EQ("futimes_0300", ret, FAIL);
73 }
74
75 /**
76 * @tc.name : futimes_time64_0100
77 * @tc.desc : All parameters are valid, TV is not 0, and futimes can modify the timestamp of the file.
78 * @tc.level : Level 0
79 */
futimes_time64_0100(void)80 void futimes_time64_0100(void)
81 {
82 int ret = -1;
83 struct stat s;
84 static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}};
85 tv[0].tv_sec = s.st_atime;
86 tv[0].tv_usec = 0;
87 tv[1].tv_sec = s.st_mtime;
88 tv[1].tv_usec = 0;
89 int fd = open("futimes.txt", O_RDWR | O_CREAT, 777);
90 ret = __futimes_time64(fd, tv);
91 EXPECT_EQ("futimes_time64_0100", ret, SUCCESS);
92 remove("futimes.txt");
93 }
94
main(int argc, char *argv[])95 int main(int argc, char *argv[])
96 {
97 futimes_0100();
98 futimes_0200();
99 futimes_0300();
100 futimes_time64_0100();
101 return t_status;
102 }