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 <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 #include "test.h"
21
22 const char *path = "/data/tests/libc-test/src/sync_file_range.txt";
23
24 /**
25 * @tc.name : sync_file_range_0100
26 * @tc.desc : Check simple successful case
27 * @tc.level : Level 0
28 */
sync_file_range_0100(void)29 void sync_file_range_0100(void)
30 {
31 errno = 0;
32 int result, fd;
33
34 fd = open(path, O_RDWR | O_CREAT);
35 if (fd == -1) {
36 t_error("%s open failed\n", __func__);
37 return;
38 }
39
40 result = sync_file_range(fd, 0, 1024, 0);
41 if (result == -1) {
42 t_error("%s sync_file_range failed, result is %d\n", __func__, result);
43 }
44 close(fd);
45 remove(path);
46 }
47
48 /**
49 * @tc.name : sync_file_range_0200
50 * @tc.desc : Check for invalid file descriptor
51 * @tc.level : Level 2
52 */
sync_file_range_0200(void)53 void sync_file_range_0200(void)
54 {
55 errno = 0;
56 int result;
57
58 result = sync_file_range(-1, 0, 0, 0);
59 if (result != -1) {
60 t_error("%s sync_file_range should failed, result is %d\n", __func__, result);
61 }
62 if (errno != EBADF) {
63 t_error("%s sync_file_range did not set errno to EBADF\n", __func__);
64 }
65 }
66
67 /**
68 * @tc.name : sync_file_range_0300
69 * @tc.desc : Check for invalid flags
70 * @tc.level : Level 2
71 */
sync_file_range_0300(void)72 void sync_file_range_0300(void)
73 {
74 errno = 0;
75 int result, fd;
76
77 fd = open(path, O_RDWR | O_CREAT);
78 if (fd == -1) {
79 t_error("%s open failed\n", __func__);
80 return;
81 }
82
83 result = sync_file_range(fd, 0, 0, -1);
84 if (result != -1) {
85 t_error("%s sync_file_range did not failed with invalid flags, result is %d\n", __func__, result);
86 }
87 if (errno != EINVAL) {
88 t_error("%s sync_file_range did not set errno to EINVAL\n", __func__);
89 }
90 close(fd);
91 remove(path);
92 }
93
94 /**
95 * @tc.name : sync_file_range_0400
96 * @tc.desc : Check for negative offset
97 * @tc.level : Level 2
98 */
sync_file_range_0400(void)99 void sync_file_range_0400(void)
100 {
101 errno = 0;
102 int result, fd;
103
104 fd = open(path, O_RDWR | O_CREAT);
105 if (fd == -1) {
106 t_error("%s open failed\n", __func__);
107 return;
108 }
109
110 result = sync_file_range(fd, -1, -1, 0);
111 if (result != -1) {
112 t_error("%s sync_file_range did not failed with invalid offset, result is %d\n", __func__, result);
113 }
114 if (errno != EINVAL) {
115 t_error("%s sync_file_range did not set errno to EINVAL\n", __func__);
116 }
117 close(fd);
118 remove(path);
119 }
120
main(int argc,char * argv[])121 int main(int argc, char *argv[])
122 {
123 sync_file_range_0100();
124 sync_file_range_0200();
125 sync_file_range_0300();
126 sync_file_range_0400();
127 return t_status;
128 }
129