• 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 
Testcase(void)33 static int Testcase(void)
34 {
35     int ret;
36     int status;
37     int pid;
38 
39     ret = fork();
40     if (ret == 0) {
41         exit(5); // 5, exit args
42     } else if (ret > 0) {
43         pid = ret;
44         ret = waitpid(0, &status, 0);
45         ICUNIT_ASSERT_EQUAL(ret, pid, ret);
46         ICUNIT_ASSERT_EQUAL(WEXITSTATUS(status), 5, status); // 5, assert that function Result is equal to this.
47         ICUNIT_ASSERT_EQUAL(WIFEXITED(status), 1, status);
48     }
49     ICUNIT_ASSERT_WITHIN_EQUAL(ret, 0, 100000, ret); // 100000, assert that function Result is equal to this.
50 
51     ret = fork();
52     if (ret == 0) {
53         WAIT_PARENT_FIRST_TO_RUN(1);
54         exit(6); // 6, exit args
55     } else if (ret > 0) {
56         pid = ret;
57         ret = waitpid(0, &status, 0);
58         status = WEXITSTATUS(status);
59         ICUNIT_ASSERT_EQUAL(ret, pid, ret);
60         ICUNIT_ASSERT_EQUAL(status, 6, status); // 6, assert that function Result is equal to this.
61     }
62 
63     ICUNIT_ASSERT_WITHIN_EQUAL(ret, 0, 100000, ret); // 100000, assert that function Result is equal to this.
64 
65     ret = fork();
66     if (ret == 0) {
67         exit(1);
68     } else if (ret > 0) {
69         pid = ret;
70         ret = wait(&status);
71         status = WEXITSTATUS(status);
72         ICUNIT_ASSERT_EQUAL(ret, pid, ret);
73         ICUNIT_ASSERT_EQUAL(status, 1, status);
74     }
75 
76     ICUNIT_ASSERT_WITHIN_EQUAL(ret, 0, 100000, ret); // 100000, assert that function Result is equal to this.
77 
78     ret = fork();
79     if (ret == 0) {
80         WAIT_PARENT_FIRST_TO_RUN(1);
81         exit(2); // 2, exit args
82     } else if (ret > 0) {
83         pid = ret;
84         ret = wait(&status);
85         status = WEXITSTATUS(status);
86         ICUNIT_ASSERT_EQUAL(ret, pid, ret);
87         ICUNIT_ASSERT_EQUAL(status, 2, status); // 2, assert that function Result is equal to this.
88     }
89     ICUNIT_ASSERT_WITHIN_EQUAL(ret, 0, 100000, ret); // 100000, assert that function Result is equal to this.
90 
91     ret = fork();
92     if (ret == 0) {
93         exit(3); // 3, exit args
94     } else if (ret > 0) {
95         pid = ret;
96         ret = waitpid(pid, &status, 0);
97         status = WEXITSTATUS(status);
98         ICUNIT_ASSERT_EQUAL(ret, pid, ret);
99         ICUNIT_ASSERT_EQUAL(status, 3, status); // 3, assert that function Result is equal to this.
100     }
101     ICUNIT_ASSERT_WITHIN_EQUAL(ret, 0, 100000, ret); // 100000, assert that function Result is equal to this.
102 
103     ret = fork();
104     if (ret == 0) {
105         WAIT_PARENT_FIRST_TO_RUN(1);
106         exit(4); // 4, exit args
107     } else if (ret > 0) {
108         pid = ret;
109         ret = waitpid(ret, &status, 0);
110         status = WEXITSTATUS(status);
111         ICUNIT_ASSERT_EQUAL(ret, pid, ret);
112         ICUNIT_ASSERT_EQUAL(status, 4, status); // 4, assert that function Result is equal to this.
113     }
114     ICUNIT_ASSERT_WITHIN_EQUAL(ret, 0, 100000, ret); // 100000, assert that function Result is equal to this.
115 
116     return 0;
117 }
118 
ItTestProcess004(void)119 void ItTestProcess004(void)
120 {
121     TEST_ADD_CASE("IT_POSIX_PROCESS_004", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
122 }
123