• 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 <string.h>
20 #include "test.h"
21 
22 const char *path_in = "/data/tests/libc-test/src/file_in.txt";
23 const char *path_out = "/data/tests/libc-test/src/file_out.txt";
24 const char *str = "Hello";
25 
26 /**
27  * @tc.name      : copy_file_range_0100
28  * @tc.desc      : Copy a range of data from one file to another
29  * @tc.level     : Level 0
30  */
copy_file_range_0100(void)31 void copy_file_range_0100(void)
32 {
33     int fd_in, fd_out, wlen, result;
34     char buffer[BUFSIZ];
35 
36     fd_in = open(path_in, O_RDWR | O_CREAT);
37     if (fd_in == -1) {
38         t_error("%s open path_in failed\n", __func__);
39         return;
40     }
41 
42     wlen = write(fd_in, str, strlen(str));
43     if (wlen != strlen(str)) {
44         t_error("%s write failed\n", __func__);
45         return;
46     }
47 
48     close(fd_in);
49     fd_in = open(path_in, O_RDONLY);
50 
51     fd_out = open(path_out, O_RDWR | O_CREAT);
52     if (fd_out == -1) {
53         t_error("%s open path_out failed\n", __func__);
54         return;
55     }
56 
57     do {
58         result = copy_file_range(fd_in, NULL, fd_out, NULL, wlen, 0);
59         if (result == -1) {
60             t_error("%s copy_file_range failed\n", __func__);
61             return;
62         }
63         wlen -= result;
64     } while (wlen > 0 && result > 0);
65 
66     close(fd_out);
67     fd_out = open(path_out, O_RDONLY);
68     memset(buffer, 0, sizeof(buffer));
69     result = read(fd_out, buffer, sizeof(buffer));
70     if (result == -1) {
71         t_error("%s read failed\n", __func__);
72     }
73     if (strcmp(str, buffer)) {
74         t_error("%s buffer is %s, not %s", __func__, buffer, str);
75     }
76 
77     close(fd_in);
78     close(fd_out);
79     remove(path_in);
80     remove(path_out);
81 }
82 
83 /**
84  * @tc.name      : copy_file_range_0200
85  * @tc.desc      : One or more file descriptors are not valid
86  * @tc.level     : Level 2
87  */
copy_file_range_0200(void)88 void copy_file_range_0200(void)
89 {
90     errno = 0;
91     int result = copy_file_range(-1, NULL, -1, NULL, 0, 0);
92     if (result != -1) {
93         t_error("%s copy_file_range should failed, result is %d\n", __func__, result);
94     }
95     if (errno != EBADF) {
96         t_error("%s errno is %d, not EBADF", __func__, errno);
97     }
98 }
99 
100 /**
101  * @tc.name      : copy_file_range_0300
102  * @tc.desc      : File permissions are restricted
103  * @tc.level     : Level 2
104  */
copy_file_range_0300(void)105 void copy_file_range_0300(void)
106 {
107     int fd_in, fd_out, wlen, result;
108     char buffer[BUFSIZ];
109 
110     fd_in = open(path_in, O_WRONLY | O_CREAT);
111     if (fd_in == -1) {
112         t_error("%s open path_in failed\n", __func__);
113         return;
114     }
115 
116     wlen = write(fd_in, str, strlen(str));
117     if (wlen != strlen(str)) {
118         t_error("%s write failed\n", __func__);
119         return;
120     }
121 
122     close(fd_in);
123     fd_in = open(path_in, O_WRONLY);
124 
125     fd_out = open(path_out, O_RDONLY | O_CREAT);
126     if (fd_out == -1) {
127         t_error("%s open path_out failed\n", __func__);
128         return;
129     }
130 
131     errno = 0;
132     result = copy_file_range(fd_in, NULL, fd_out, NULL, wlen, 0);
133     if (result != -1) {
134         t_error("%s copy_file_range should failded\n", __func__);
135     }
136     if (errno != EBADF) {
137         t_error("%s errno is %d, not EBADF\n", __func__, errno);
138     }
139 
140     close(fd_in);
141     close(fd_out);
142     remove(path_in);
143     remove(path_out);
144 }
145 
main(int argc,char * argv[])146 int main(int argc, char *argv[])
147 {
148     copy_file_range_0100();
149     copy_file_range_0200();
150     copy_file_range_0300();
151     return t_status;
152 }