• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 static int g_waitPid;
34 static int g_backPid;
35 static int g_backPid1;
36 static int g_backPidFlag = 0;
37 static int g_backPid1Flag = 0;
38 static int g_errorFalg = 0;
39 
ThreadFunc(void * arg)40 static void *ThreadFunc(void *arg)
41 {
42     int status = 0;
43 
44     int ret = waitpid(g_waitPid, &status, 0);
45     if ((ret == g_backPid) && (g_backPidFlag == 0)) {
46         g_backPidFlag = 1;
47         status = WEXITSTATUS(status);
48         ICUNIT_GOTO_EQUAL(status, 3, status, EXIT); // 3, assert status equal to this.
49     } else if ((ret == g_backPid1) && (g_backPid1Flag == 0)) {
50         g_backPid1Flag = 1;
51         status = WEXITSTATUS(status);
52         ICUNIT_GOTO_EQUAL(status, 4, status, EXIT); // 4, assert status equal to this.
53     } else if (g_errorFalg == 0) {
54         g_errorFalg = 1;
55         ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT);
56     } else {
57         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
58     }
59 
60 EXIT:
61     return NULL;
62 }
63 
ThreadFunc1(void * arg)64 static void *ThreadFunc1(void *arg)
65 {
66     int status = 0;
67     int ret = waitpid(g_waitPid, &status, 0);
68     if ((ret == g_backPid) && (g_backPidFlag == 0)) {
69         g_backPidFlag = 1;
70         status = WEXITSTATUS(status);
71         ICUNIT_GOTO_EQUAL(status, 3, status, EXIT); // 3, assert status equal to this.
72     } else if ((ret == g_backPid1) && (g_backPid1Flag == 0)) {
73         g_backPid1Flag = 1;
74         status = WEXITSTATUS(status);
75         ICUNIT_GOTO_EQUAL(status, 4, status, EXIT); // 4, assert status equal to this.
76     } else if (g_errorFalg == 0) {
77         g_errorFalg = 1;
78         ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT);
79     } else {
80         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
81     }
82 
83 EXIT:
84     return NULL;
85 }
86 
ProcessTest(void)87 static int ProcessTest(void)
88 {
89     int ret;
90     int status = 100;
91     pthread_t newPthread, newPthread1;
92     pid_t pid, pid1, pid2;
93     int count = 0;
94 
95     g_errorFalg = 0;
96     g_backPid1Flag = 0;
97     g_errorFalg = 0;
98 
99     g_waitPid = 0;
100     pid = fork();
101     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, assert pid equal to this.
102     g_waitPid = -1;
103     g_backPid = pid;
104 
105     if (pid == 0) {
106         while (count < 20) { // 20, Number of cycles.
107             count++;
108             usleep(1000 * 10 * 2); // 1000, 10, 2, Used to calculate the delay time.
109         }
110         exit(3); // 3, exit args
111     }
112 
113     pid1 = fork();
114     ICUNIT_GOTO_WITHIN_EQUAL(pid1, 0, 100000, pid, EXIT); // 100000, assert pid1 equal to this.
115     g_backPid1 = pid1;
116     if (pid1 == 0) {
117         while (count < 15) { // 15, Number of cycles.
118             count++;
119             usleep(1000 * 10 * 2); // 1000, 10, 2, Used to calculate the delay time.
120         }
121         exit(4); // 4, exit args
122     }
123 
124     ret = pthread_create(&newPthread, NULL, ThreadFunc, NULL);
125     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
126     usleep(1000 * 10 * 2); // 1000, 10, 2, Used to calculate the delay time.
127 
128     ret = pthread_create(&newPthread1, NULL, ThreadFunc1, NULL);
129     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
130     usleep(1000 * 10 * 2); // 1000, 10, 2, Used to calculate the delay time.
131 
132     ret = waitpid(-1, &status, 0);
133     if ((ret == pid) && (g_backPidFlag == 0)) {
134         g_backPidFlag = 1;
135         status = WEXITSTATUS(status);
136         ICUNIT_GOTO_EQUAL(status, 3, status, EXIT); // 3, assert status equal to this.
137     } else if ((ret == pid1) && (g_backPid1Flag == 0)) {
138         g_backPid1Flag = 1;
139         status = WEXITSTATUS(status);
140         ICUNIT_GOTO_EQUAL(status, 4, status, EXIT); // 4, assert status equal to this.
141     } else if (g_errorFalg == 0) {
142         g_errorFalg = 1;
143         ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT);
144     } else {
145         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
146     }
147 
148     ret = pthread_join(newPthread, NULL);
149     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
150 
151     ret = pthread_join(newPthread1, NULL);
152     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
153 
154     exit(255); // 255, exit args
155 EXIT:
156     return 0;
157 }
158 
TestCase(void)159 static int TestCase(void)
160 {
161     int ret;
162     int status = 0;
163     pid_t pid = fork();
164     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, assert pid equal to this.
165     if (pid == 0) {
166         ProcessTest();
167         exit(g_iCunitErrLineNo);
168     }
169 
170     ret = waitpid(pid, &status, 0);
171     ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT);
172     status = WEXITSTATUS(status);
173     ICUNIT_GOTO_EQUAL(status, 255, status, EXIT); // 255, assert status equal to this.
174     return 0;
175 EXIT:
176     return 1;
177 }
178 
ItTestProcess021(void)179 void ItTestProcess021(void)
180 {
181     TEST_ADD_CASE("IT_POSIX_PROCESS_021", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
182 }
183