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
TestPipeSingleProcessFcntl()35 int TestPipeSingleProcessFcntl()
36 {
37 int pipefd[2]; // 2, array subscript
38 pid_t pid;
39 int retValue = -1;
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];
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 sprintf(a, "%d", i);
62 strcat(sentence1, a);
63 int ret = write(*writeFd, sentence1, strlen(sentence1) + 1);
64 usleep(100000); // 100000, Used to calculate the delay time.
65 }
66 char sentence1[15] = "Hello World";
67 char a[4] = {0};
68 usleep(10000); // 10000, Used to calculate the delay time.
69 printf("read\n");
70 memset(readbuffer, 0, sizeof(readbuffer));
71 retValue = read(*readFd, readbuffer, sizeof(readbuffer));
72 printf("Receive %d bytes data : %s,%d\n", retValue, readbuffer, errno);
73 printf("Receive %d bytes data : %s,%d\n", retValue, readbuffer + 13, errno); // 13, readbuffer offset.
74 printf("Receive %d bytes data : %s,%d\n", retValue, readbuffer + 26, errno); // 26, readbuffer offset.
75 ICUNIT_ASSERT_SIZE_STRING_EQUAL(readbuffer, "Hello World0", strlen(sentence1), errno);
76 ICUNIT_ASSERT_SIZE_STRING_EQUAL(readbuffer + 13, "Hello World1", strlen(sentence1), errno); // 13, readbuffer offset.
77 ICUNIT_ASSERT_SIZE_STRING_EQUAL(readbuffer + 26, "Hello World2", strlen(sentence1), errno); // 26, readbuffer offset.
78 usleep(100000); // 100000, Used to calculate the delay time.
79
80 printf("read\n");
81 memset(readbuffer, 0, sizeof(readbuffer));
82 fcntl(*readFd, F_SETFL, O_NONBLOCK);
83 ret = fcntl(*readFd, F_GETFL, O_NONBLOCK);
84 printf("fctrl ret=%d,O_NONBLOCK=%d\n", ret, O_NONBLOCK);
85 retValue = read(*readFd, readbuffer, sizeof(readbuffer));
86 printf("Receive %d bytes data : %s,%d\n", retValue, readbuffer, errno);
87
88 ICUNIT_ASSERT_EQUAL(retValue, -1, retValue);
89 ICUNIT_ASSERT_EQUAL(errno, EAGAIN, errno);
90 ICUNIT_ASSERT_SIZE_STRING_EQUAL(readbuffer, readbuffer, strlen(sentence1), errno);
91 usleep(100000); // 100000, Used to calculate the delay time.
92 exit(0);
93 }
94 ret = waitpid(pid, &status, 0);
95 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
96 ret = close(pipefd[0]);
97 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
98 ret = close(pipefd[1]);
99 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
100 return 0;
101 }
102
ItPosixPipe003(void)103 void ItPosixPipe003(void)
104 {
105 TEST_ADD_CASE(__FUNCTION__, TestPipeSingleProcessFcntl, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
106 }
107