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 <unistd.h>
21
22 #include "test.h"
23
24 const char *fromfile = "/data/tests/libc-test/src/fromfile.txt";
25 const char *tofile = "/data/tests/libc-test/src/tofile.txt";
26 const char *path = "/proc/version";
27
28 /**
29 * @tc.name : splice_0100
30 * @tc.desc : splice 0 data to/from a pipe
31 * @tc.level : Level 0
32 */
splice_0100(void)33 void splice_0100(void)
34 {
35 int fromfd = open(fromfile, O_RDWR | O_CREAT);
36 if (fromfd < 0) {
37 t_error("%s failed: fromfd = %d\n", __func__, fromfd);
38 }
39
40 int tofd = open(tofile, O_RDWR | O_CREAT);
41 if (tofd < 0) {
42 t_error("%s failed: tofd = %d\n", __func__, tofd);
43 }
44
45 off_t off = 0;
46 size_t len = 0;
47 int flags = 0;
48
49 errno = 0;
50 ssize_t result = splice(fromfd, &off, tofd, &off, len, flags);
51 if (result != 0) {
52 t_error("%s failed: result = %ld\n", __func__, result);
53 }
54
55 if (errno != 0) {
56 t_error("%s failed: errno = %d\n", __func__, errno);
57 }
58
59 remove(fromfile);
60 remove(tofile);
61 }
62
63 /**
64 * @tc.name : splice_0200
65 * @tc.desc : splice data to/from a pipe
66 * @tc.level : Level 1
67 */
splice_0200(void)68 void splice_0200(void)
69 {
70 char buf[BUFSIZ] = {0};
71 FILE *f = fopen(path, "r");
72 if (f == NULL) {
73 t_error("%s failed: fopen\n", __func__);
74 return;
75 }
76
77 fgets(buf, sizeof(buf), f);
78 fclose(f);
79
80 int fromfd = open(path, O_RDONLY);
81 if (fromfd < 0) {
82 t_error("%s failed: fromfd = %d\n", __func__, fromfd);
83 }
84
85 int pipe1[1 + 1];
86 int result = pipe(pipe1);
87 if (result != 0) {
88 t_error("%s failed: result = %d\n", __func__, result);
89 return;
90 }
91
92 size_t len = BUFSIZ;
93 int flags = SPLICE_F_MORE | SPLICE_F_MOVE;
94
95 ssize_t bytes = splice(fromfd, NULL, pipe1[1], NULL, len, flags);
96 if (bytes <= 0) {
97 t_error("%s failed: bytes = %ld\n", __func__, bytes);
98 return;
99 }
100
101 close(pipe1[1]);
102
103 char buf2[BUFSIZ] = {0};
104 f = fdopen(pipe1[0], "r");
105 if (f == NULL) {
106 t_error("%s failed: fdopen\n", __func__);
107 return;
108 }
109
110 fgets(buf2, sizeof(buf2), f);
111 fclose(f);
112
113 if (strcmp(buf2, buf)) {
114 t_error("%s failed: buf2 = %s\n", __func__, buf2);
115 return;
116 }
117 }
118
119 /**
120 * @tc.name : splice_0300
121 * @tc.desc : splice data to/from a pipe with invalid parameters
122 * @tc.level : Level 2
123 */
splice_0300(void)124 void splice_0300(void)
125 {
126 errno = 0;
127 ssize_t bytes = splice(-1, NULL, -1, NULL, -1, -1);
128 if (bytes >= 0) {
129 t_error("%s failed: bytes = %ld\n", __func__, bytes);
130 }
131
132 if (errno == 0) {
133 t_error("%s failed: errno = %ld\n", __func__, errno);
134 }
135 }
136
main(int argc,char * argv[])137 int main(int argc, char *argv[])
138 {
139 splice_0100();
140 splice_0200();
141 splice_0300();
142
143 return t_status;
144 }
145