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 <fcntl.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
19 #include "functionalext.h"
20
21 #define TEST_FD_SIZE 2
22 #define TEST_BUFFER_SIZE 2
23
pipe2_test(const char * msg,int flag)24 static void pipe2_test(const char *msg, int flag)
25 {
26 int pipefd[TEST_FD_SIZE];
27 char buf[TEST_BUFFER_SIZE];
28 int ret = pipe2(pipefd, flag);
29 EXPECT_EQ(msg, ret, CMPFLAG);
30 if (ret != 0) {
31 return;
32 }
33
34 memset(buf, 0x0, sizeof(buf));
35 pid_t pid = fork();
36 if (pid == 0) {
37 close(pipefd[1]);
38 while (read(pipefd[0], buf, 1) > 0) {
39 EXPECT_STREQ(msg, buf, "a");
40 }
41 close(pipefd[0]);
42 _exit(0);
43 } else if (pid > 0) {
44 buf[0] = 'a';
45 close(pipefd[0]);
46 write(pipefd[1], buf, 1);
47 close(pipefd[1]);
48 wait(NULL);
49 } else {
50 close(pipefd[0]);
51 close(pipefd[1]);
52 EXPECT_FALSE(msg, 1);
53 }
54 }
55
56 /**
57 * @tc.name : pipe2_0100
58 * @tc.desc : Create pipelines in two ways: O_CLOEXEC and O_NONBLOCK, and implement data transfer
59 * @tc.level : Level 0
60 */
pipe2_0100(void)61 void pipe2_0100(void)
62 {
63 pipe2_test("pipe2_0100", 0);
64 pipe2_test("pipe2_0100", O_CLOEXEC);
65 pipe2_test("pipe2_0100", O_NONBLOCK);
66 }
67
main(void)68 int main(void)
69 {
70 pipe2_0100();
71 return t_status;
72 }