1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include "it_test_signal.h"
32 #include "signal.h"
33 #include "fcntl.h"
34
TestPipeSingleProcess()35 int TestPipeSingleProcess()
36 {
37 int pipefd[2]; // 2, array subscript
38 int retValue = -1;
39 pid_t pid;
40 retValue = pipe(pipefd);
41 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
42
43 int *readFd = &pipefd[0];
44 int *writeFd = &pipefd[1];
45
46 char readbuffer[100]; // 100, array subscript
47 int status, ret;
48
49 int flag = fcntl(*readFd, F_GETFL);
50 fcntl(*readFd, F_SETFL, flag | O_NONBLOCK);
51
52 pid = fork();
53 if (pid == -1) {
54 printf("Fork Error!\n");
55 return -1;
56 } else if (pid == 0) {
57 for (int i = 0; i < 3; i++) { // 3, Number of cycles
58 errno = 0;
59 char sentence1[15] = "Hello World";
60 char a[4] = {0};
61 (void)sprintf_s(a, sizeof(a), "%d", i);
62 (void)strcat_s(sentence1, 15, a); // 15, sizeof sentence1
63 int ret = write(*writeFd, sentence1, strlen(sentence1) + 1);
64 usleep(100000); // 100000, Used to calculate the delay time.
65
66 printf("read\n");
67 (void)memset_s(readbuffer, sizeof(readbuffer), 0, sizeof(readbuffer));
68 retValue = read(*readFd, readbuffer, sizeof(readbuffer));
69 printf("Receive %d bytes data : %s,%d\n", retValue, readbuffer, errno);
70 if (strncmp((readbuffer), (readbuffer), (strlen(sentence1))) != 0) {
71 exit(errno);
72 }
73 usleep(100000); // 100000, Used to calculate the delay time.
74 }
75 exit(0);
76 }
77 ret = waitpid(pid, &status, 0);
78 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
79 ICUNIT_ASSERT_EQUAL(WEXITSTATUS(status), 0, WEXITSTATUS(status));
80 ret = unlink("/dev/pipe0");
81 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
82 ret = close(pipefd[0]);
83 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
84 ret = unlink("/dev/pipe0");
85 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
86 ret = close(pipefd[1]);
87 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
88 ret = unlink("/dev/pipe0");
89 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
90 return 0;
91 }
92
93
ItPosixPipe001(void)94 void ItPosixPipe001(void)
95 {
96 TEST_ADD_CASE(__FUNCTION__, TestPipeSingleProcess, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
97 }
98