• 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 
37 static int g_thread001;
38 static int g_thread002;
ThreadFunc(void * arg)39 static void *ThreadFunc(void *arg)
40 {
41     int status = 0;
42 
43     int ret = waitpid(-1, &status, 0);
44     printf("%s 33333 pid : %d\n", __FUNCTION__, ret);
45     ICUNIT_ASSERT_NOT_EQUAL_NULL(ret, -1, ret);
46     ICUNIT_ASSERT_NOT_EQUAL_NULL(g_thread002, 0, g_thread002);
47     g_thread001++;
48 
49     return reinterpret_cast<void *>(9); // 9, set thread return value.
50 EXIT:
51     return NULL;
52 }
53 
ThreadFunc2(void * arg)54 static void *ThreadFunc2(void *arg)
55 {
56     int status = 0;
57 
58     int ret = waitpid(-1, &status, 0);
59     g_thread002++;
60     printf("%s 222222 pid : %d\n", __FUNCTION__, ret);
61     ICUNIT_ASSERT_NOT_EQUAL_NULL(ret, -1, ret);
62     ICUNIT_ASSERT_NOT_EQUAL_NULL(g_thread001, 0, g_thread001);
63 
64     return reinterpret_cast<void *>(9); // 9, set thread return value.
65 EXIT:
66     return NULL;
67 }
68 
ProcessTest(void)69 static int ProcessTest(void)
70 {
71     int ret;
72     int *error = nullptr;
73     int status = 100;
74     pthread_t newPthread, newPthread1;
75     pid_t pid, pid1, pid2;
76     int count = 0;
77 
78     g_thread001 = 0;
79     g_thread002 = 0;
80 
81     pid = fork();
82     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, assert pid equal to this.
83     g_backPid = pid;
84 
85     if (pid == 0) {
86         usleep(1000 * 10 * 20); // 1000, 10, 20, Used to calculate the delay time.
87         exit(3); // 3, exit args
88     }
89 
90     pid1 = fork();
91     ICUNIT_GOTO_WITHIN_EQUAL(pid1, 0, 100000, pid, EXIT); // 100000, assert pid1 equal to this.
92     g_backPid1 = pid1;
93     if (pid1 == 0) {
94         usleep(1000 * 10 * 15); // 1000, 10, 15, Used to calculate the delay time.
95         exit(4); // 4, exit args
96     }
97 
98     pid2 = fork();
99     ICUNIT_GOTO_WITHIN_EQUAL(pid2, 0, 100000, pid, EXIT); // 100000, assert pid2 equal to this.
100     if (pid2 == 0) {
101         usleep(1000 * 10 * 10); // 1000, 10, 10, Used to calculate the delay time.
102         exit(4); // 4, exit args
103     }
104 
105     ret = pthread_create(&newPthread, NULL, ThreadFunc, NULL);
106     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
107 
108     usleep(1000 * 10 * 2); // 1000, 10, 2, Used to calculate the delay time.
109     ret = pthread_create(&newPthread1, NULL, ThreadFunc2, NULL);
110     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
111     usleep(1000 * 10 * 2); // 1000, 10, 2, Used to calculate the delay time.
112 
113     ret = waitpid(-1, &status, 0);
114     g_thread001++;
115     printf("%s 11111 pid : %d\n", __FUNCTION__, ret);
116     ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
117     ICUNIT_ASSERT_NOT_EQUAL(g_thread002, 1, g_thread001);
118 
119     ret = pthread_join(newPthread1, NULL);
120     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
121 
122     ret = pthread_join(newPthread, NULL);
123     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
124 
125     ICUNIT_ASSERT_EQUAL(g_thread001, 2, g_thread001); // 2, assert that function Result is equal to this.
126 
127     exit(255); // 255, exit args
128 EXIT:
129     return 0;
130 }
131 
TestCase(void)132 static int TestCase(void)
133 {
134     int ret;
135     int status = 0;
136     pid_t pid = fork();
137     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, assert pid equal to this.
138     if (pid == 0) {
139         ProcessTest();
140         exit(g_iCunitErrLineNo);
141     }
142 
143     ret = waitpid(pid, &status, 0);
144     ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT);
145     status = WEXITSTATUS(status);
146     ICUNIT_GOTO_EQUAL(status, 255, status, EXIT); // 255, assert status equal to this.
147 
148     return 0;
149 EXIT:
150     return 1;
151 }
152 
ItTestProcess023(void)153 void ItTestProcess023(void)
154 {
155     TEST_ADD_CASE("IT_POSIX_PROCESS_023", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
156 }
157