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_process.h"
32
ProcessTest002(void)33 static int ProcessTest002(void)
34 {
35 int pid;
36
37 pid = fork();
38 if (pid == 0) {
39 WAIT_PARENT_FIRST_TO_RUN(1);
40 exit(12); // 12, exit args
41 }
42
43 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
44
45 pid = fork();
46 if (pid == 0) {
47 WAIT_PARENT_FIRST_TO_RUN(1);
48 exit(12); // 12, exit args
49 }
50
51 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
52
53 pid = fork();
54 if (pid == 0) {
55 WAIT_PARENT_FIRST_TO_RUN(1);
56 exit(12); // 12, exit args
57 }
58
59 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
60
61 pid = fork();
62 if (pid == 0) {
63 WAIT_PARENT_FIRST_TO_RUN(1);
64 exit(12); // 12, exit args
65 }
66
67 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
68
69 pid = fork();
70 if (pid == 0) {
71 WAIT_PARENT_FIRST_TO_RUN(1);
72 exit(12); // 12, exit args
73 }
74
75 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
76
77 pid = fork();
78 if (pid == 0) {
79 WAIT_PARENT_FIRST_TO_RUN(1);
80 exit(12); // 12, exit args
81 }
82
83 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
84
85 pid = fork();
86 if (pid == 0) {
87 WAIT_PARENT_FIRST_TO_RUN(1);
88 exit(12); // 12, exit args
89 }
90
91 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
92
93 pid = fork();
94 if (pid == 0) {
95 WAIT_PARENT_FIRST_TO_RUN(1);
96 exit(12); // 12, exit args
97 }
98
99 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
100
101 pid = fork();
102 if (pid == 0) {
103 WAIT_PARENT_FIRST_TO_RUN(1);
104 exit(12); // 12, exit args
105 }
106
107 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
108
109 pid = fork();
110 if (pid == 0) {
111 WAIT_PARENT_FIRST_TO_RUN(1);
112 exit(8); // 8, exit args
113 }
114
115 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
116
117 return 0;
118 }
119
ProcessTest001(int * id)120 static int ProcessTest001(int *id)
121 {
122 int ret;
123 int status;
124 int pid;
125
126 pid = fork();
127 if (pid == 0) {
128 ret = ProcessTest002();
129 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
130 WAIT_PARENT_FIRST_TO_RUN(200); // 200, wait time.
131 exit(7); // 7, exit args
132 }
133
134 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
135 *id = pid;
136 return 0;
137 }
138
Testcase(void)139 static int Testcase(void)
140 {
141 int ret;
142 int status;
143 int pid;
144
145 ret = fork();
146 if (ret == 0) {
147 ret = ProcessTest001(&pid);
148 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
149 ret = waitpid(pid, &status, 0);
150 status = WEXITSTATUS(status);
151 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
152 ICUNIT_ASSERT_EQUAL(status, 7, status); // 7, assert that function Result is equal to this.
153 exit(8); // 8, exit args
154 } else if (ret > 0) {
155 pid = ret;
156 ret = wait(&status);
157 status = WEXITSTATUS(status);
158 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
159 ICUNIT_ASSERT_EQUAL(status, 8, status); // 8, assert that function Result is equal to this.
160 }
161
162 return 0;
163 }
164
ItTestProcess005(void)165 void ItTestProcess005(void)
166 {
167 TEST_ADD_CASE("IT_POSIX_PROCESS_005", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
168 }
169