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 <stdio.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include "test.h"
21
22 #define BUFF_SIZE (20)
23
24 /**
25 * @tc.name : tee_0100
26 * @tc.desc : Copy data between two pipe file descriptors
27 * @tc.level : Level 0
28 */
tee_0100(void)29 void tee_0100(void)
30 {
31 char buf[BUFF_SIZE];
32 char *text = "Hello";
33 int result;
34 memset(buf, 0, sizeof(buf));
35
36 int pipefd1[2];
37 result = pipe(pipefd1);
38 if (result == -1) {
39 t_error("%s pipe stdout failed\n", __func__);
40 }
41 if (write(pipefd1[1], text, strlen(text)) == -1) {
42 t_error("%s write failed\n", __func__);
43 }
44
45 int pipefd2[2];
46 result = pipe(pipefd2);
47 if (result == -1) {
48 t_error("%s pipe file failed\n", __func__);
49 }
50
51 result = tee(pipefd1[0], pipefd2[1], 32768, SPLICE_F_NONBLOCK);
52 if (result == -1) {
53 t_error("%s tee failed\n", __func__);
54 }
55 if (read(pipefd2[0], buf, sizeof(buf)) == -1) {
56 t_error("%s read failed\n", __func__);
57 }
58
59 if (strcmp(buf, text)) {
60 t_error("%s tee failed, buf = %s\n", __func__, buf);
61 }
62 close(pipefd1[0]);
63 close(pipefd1[1]);
64 close(pipefd2[0]);
65 close(pipefd2[1]);
66 }
67
68 /**
69 * @tc.name : tee_0200
70 * @tc.desc : Input argument is not a pipe descriptor
71 * @tc.level : Level 2
72 */
tee_0200(void)73 void tee_0200(void)
74 {
75 char buf[BUFF_SIZE];
76 char *text = "Hello";
77 char *path = "/data/tests/libc-test/src/file.txt";
78 int result;
79
80 int fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0666);
81 if (fd < 0) {
82 t_error("%s open failed\n", __func__);
83 }
84
85 int pipefd2[2];
86 result = pipe(pipefd2);
87 if (result == -1) {
88 t_error("%s pipe file failed\n", __func__);
89 }
90
91 result = tee(fd, pipefd2[1], 32768, SPLICE_F_NONBLOCK);
92 if (result != -1) {
93 t_error("%s test failed\n", __func__);
94 }
95 close(fd);
96 close(pipefd2[0]);
97 close(pipefd2[1]);
98 }
99
main(int argc,char * argv[])100 int main(int argc, char *argv[])
101 {
102 tee_0100();
103 tee_0200();
104 return t_status;
105 }