• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021, 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  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "it_test_signal.h"
32 
33 #define TAR_STR_LEN 12
34 
TestCase(VOID)35 static UINT32 TestCase(VOID)
36 {
37     int pipefd[2]; // 2, array subscript
38     int retValue = pipe2(pipefd, O_CLOEXEC);
39     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
40 
41     int *readFd = &pipefd[0];
42     int *writeFd = &pipefd[1];
43     char readbuffer[100];
44     int status, ret;
45 
46     pid_t pid = fork();
47     if (pid == -1) {
48         printf("Fork Error!\n");
49         return -1;
50     } else if (pid == 0) {
51         for (int i = 0; i < 3; i++) { // 3, Number of cycles
52             errno = 0;
53             char sentence1[15] = "Hello World";
54             char a[4] = {0};
55             retValue = sprintf_s(a, sizeof(a), "%d", i);
56             ICUNIT_ASSERT_NOT_EQUAL(retValue, 0, retValue);
57             retValue = strcat_s(sentence1, sizeof(sentence1), a);
58             ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
59             ret = write(*writeFd, sentence1, strlen(sentence1) + 1);
60             ICUNIT_ASSERT_EQUAL(ret, 13, ret); // 13, assert that function Result is equal to this.
61             usleep(100000); // 100000, Used to calculate the delay time.
62 
63             retValue = memset_s(readbuffer, sizeof(readbuffer), 0, sizeof(readbuffer));
64             ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
65             retValue = read(*readFd, readbuffer, sizeof(readbuffer));
66             ICUNIT_ASSERT_EQUAL(retValue, 13, retValue); // 13, assert that function Result is equal to this.
67             ret = strncmp((readbuffer), (sentence1), (strlen(sentence1)));
68             ICUNIT_ASSERT_EQUAL(ret, 0, ret);
69             usleep(100000); // 100000, Used to calculate the delay time.
70         }
71         exit(0);
72     }
73     ret = waitpid(pid, &status, 0);
74     ICUNIT_ASSERT_EQUAL(ret, pid, ret);
75     ICUNIT_ASSERT_EQUAL(WEXITSTATUS(status), 0, WEXITSTATUS(status));
76     ret = unlink("/dev/pipe0");
77     ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
78     ret = close(pipefd[0]);
79     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
80     ret = unlink("/dev/pipe0");
81     ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
82     ret = close(pipefd[1]);
83     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
84     ret = unlink("/dev/pipe0");
85     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
86     return 0;
87 }
88 
ItIpcPipe004(void)89 void ItIpcPipe004(void)
90 {
91     TEST_ADD_CASE(__FUNCTION__, TestCase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
92 }
93