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 typedef void (*TEST_FUN)();
21
22 const char *path = "/data/readtest.txt";
23
24 /**
25 * @tc.name : fdatasync_0100
26 * @tc.desc : Verify flush kernel's cache (parameter valid)
27 * @tc.level : Level 0
28 */
fdatasync_0100()29 void fdatasync_0100()
30 {
31 char str[] = "hello";
32 char buffer[1024] = {"\0"};
33 int fd = open(path, O_RDWR | O_CREAT);
34 EXPECT_NE("fdatasync_0100", fd, -1);
35
36 write(fd, str, sizeof(str));
37 lseek(fd, 0L, SEEK_SET);
38 read(fd, buffer, 1024);
39
40 int result = fdatasync(fd);
41 EXPECT_EQ("fdatasync_0100", result, 0);
42 EXPECT_STREQ("fdatasync_0100", buffer, "hello");
43
44 close(fd);
45 remove(path);
46 }
47
48 /**
49 * @tc.name : fdatasync_0200
50 * @tc.desc : Verify failed to flush kernel's cache (file close)
51 * @tc.level : Level 2
52 */
fdatasync_0200()53 void fdatasync_0200()
54 {
55 char str[] = "hello\n";
56 char buffer[1024] = {"\0"};
57 int fd = open(path, O_RDWR | O_CREAT);
58 EXPECT_NE("fdatasync_0100", fd, -1);
59 close(fd);
60
61 int result = fdatasync(fd);
62 EXPECT_EQ("fdatasync_0200", result, -1);
63
64 remove(path);
65 }
66
67 /**
68 * @tc.name : fdatasync_0300
69 * @tc.desc : Validation failed to flush kernel's cache (invalid parameter)
70 * @tc.level : Level 2
71 */
fdatasync_0300()72 void fdatasync_0300()
73 {
74 int result = fdatasync(-1);
75 EXPECT_EQ("fdatasync_0300", result, -1);
76 }
77
78 TEST_FUN G_Fun_Array[] = {
79 fdatasync_0100,
80 fdatasync_0200,
81 fdatasync_0300,
82 };
83
main(int argc,char * argv[])84 int main(int argc, char *argv[])
85 {
86 int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
87 for (int pos = 0; pos < num; ++pos) {
88 G_Fun_Array[pos]();
89 }
90
91 return t_status;
92 }