• 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(0, &status, 0);
44     printf("3333333333333 pid : %u\n", 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 EXIT:
50     return NULL;
51 }
52 
ThreadFunc2(void * arg)53 static void *ThreadFunc2(void *arg)
54 {
55     int status = 0;
56 
57     int ret = waitpid(0, &status, 0);
58     printf("222222222222222222 pid : %u\n", ret);
59     ICUNIT_ASSERT_NOT_EQUAL_NULL(ret, -1, ret);
60     ICUNIT_ASSERT_NOT_EQUAL_NULL(g_thread001, 0, g_thread001);
61     g_thread002++;
62 
63 EXIT:
64     return NULL;
65 }
66 
ProcessTest(void)67 static int ProcessTest(void)
68 {
69     int ret;
70     int status = 100;
71     pthread_t newPthread, newPthread1;
72     pid_t pid, pid1, pid2;
73     int count = 0;
74 
75     ret = setpgrp();
76     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
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 * 30); // 1000, 10, 30, 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 * 20); // 1000, 10, 20, 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 * 15); // 1000, 10, 15, 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 * 5); // 1000, 10, 5, Used to calculate the delay time.
109     ret = pthread_create(&newPthread1, NULL, ThreadFunc2, NULL);
110     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
111 
112     usleep(1000 * 10 * 5); // 1000, 10, 5, Used to calculate the delay time.
113 
114     ret = waitpid(0, &status, 0);
115     printf("111111111111111 pid : %u\n", ret);
116     ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
117     g_thread001++;
118     ICUNIT_ASSERT_NOT_EQUAL(g_thread002, 1, g_thread001);
119 
120     ret = pthread_join(newPthread, NULL);
121     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
122 
123     ret = pthread_join(newPthread1, NULL);
124     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
125 
126     ICUNIT_ASSERT_EQUAL(g_thread001, 2, g_thread001); // 2, assert that function Result is equal to this.
127 
128     exit(255); // 255, exit args
129 EXIT:
130     return 0;
131 }
132 
TestCase(void)133 static int TestCase(void)
134 {
135     int ret;
136     int status = 0;
137     pid_t pid = fork();
138     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, assert pid equal to this.
139     if (pid == 0) {
140         ProcessTest();
141         exit(g_iCunitErrLineNo);
142     }
143 
144     ret = waitpid(pid, &status, 0);
145     ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT);
146     status = WEXITSTATUS(status);
147     ICUNIT_GOTO_EQUAL(status, 255, status, EXIT); // 255, assert status equal to this.
148     return 0;
149 EXIT:
150     return 1;
151 }
152 
ItTestProcess022(void)153 void ItTestProcess022(void)
154 {
155     TEST_ADD_CASE("IT_POSIX_PROCESS_022", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
156 }
157