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