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 <unistd.h>
18 #include "functionalext.h"
19
20 const char *path = "/data/test.txt";
21
22 /**
23 * @tc.name : fsync_0100
24 * @tc.desc : Tests of fsync on file descriptors in different modes
25 * @tc.level : Level 0
26 */
fsync_0100(void)27 void fsync_0100(void)
28 {
29 int fd = open(path, O_RDWR | O_CREAT);
30 EXPECT_TRUE("fsync_0100", fd >= 0);
31
32 int result = fsync(fd);
33 EXPECT_EQ("fsync_0100", result, 0);
34 close(fd);
35
36 fd = open(path, O_RDONLY);
37 result = fsync(fd);
38 EXPECT_EQ("fsync_0100", result, 0);
39 close(fd);
40
41 remove(path);
42 }
43
44 /**
45 * @tc.name : fsync_0200
46 * @tc.desc : fd is a directory
47 * @tc.level : Level 1
48 */
fsync_0200(void)49 void fsync_0200(void)
50 {
51 int fd = open("/data", O_RDONLY);
52 EXPECT_NE("fsync_0200", fd, -1);
53
54 int result = fsync(fd);
55 EXPECT_EQ("fsync_0200", result, 0);
56 close(fd);
57 }
58
59 /**
60 * @tc.name : fsync_0300
61 * @tc.desc : Can't sync an invalid fd
62 * @tc.level : Level 2
63 */
fsync_0300(void)64 void fsync_0300(void)
65 {
66 errno = 0;
67 int result = fsync(-1);
68 EXPECT_EQ("fsync_0300", result, -1);
69 EXPECT_EQ("fsync_0300", errno, EBADF);
70 }
71
main(int argc,char * argv[])72 int main(int argc, char *argv[])
73 {
74 fsync_0100();
75 fsync_0200();
76 fsync_0300();
77
78 return t_status;
79 }