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