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 const int TEST_COUNT = 10;
34
ThreadFunc2(void * arg)35 static void *ThreadFunc2(void *arg) {}
36
ProcessTest001(void)37 static int ProcessTest001(void)
38 {
39 int ret;
40 int status;
41 int pid;
42 int data;
43 pthread_t newPthread, newPthread1;
44 int currProcessPri = getpriority(PRIO_PROCESS, getpid());
45 ICUNIT_ASSERT_WITHIN_EQUAL(currProcessPri, 0, 31, currProcessPri); // 31, assert currProcessPri equal to this.
46
47 ret = setpriority(PRIO_PROCESS, getpid(), currProcessPri - 2); // 2, Used to calculate priorities.
48 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
49
50 data = ret;
51 ret = pthread_create(&newPthread, NULL, ThreadFunc2, &data);
52 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
53
54 exit(255); // 255, exit args
55 return 0;
56 }
57
Child(void)58 static int Child(void)
59 {
60 int count = TEST_COUNT;
61 int status = 0;
62
63 while (count > 0) {
64 int ret = fork();
65 if (ret == 0) {
66 ret = ProcessTest001();
67 exit(ret);
68 } else if (ret > 0) {
69 int pid = ret;
70 ret = wait(&status);
71 status = WEXITSTATUS(status);
72 if (ret != pid) {
73 printf("wait child %d failed, is %d!\n", pid, ret);
74 exit(__LINE__);
75 }
76 if (status != 255) { // 255, assert that function Result is equal to this.
77 printf("child is error line :%d \n", status);
78 exit(__LINE__);
79 }
80 } else {
81 printf("fork failed!\n");
82 exit(__LINE__);
83 }
84
85 count--;
86 }
87
88 return 0;
89 }
90
Testcase(void)91 static int Testcase(void)
92 {
93 int ret;
94 int status;
95 int pid;
96
97 int temp = GetCpuCount();
98 if (temp <= 1) {
99 return 0;
100 }
101
102 ret = fork();
103 ICUNIT_ASSERT_WITHIN_EQUAL(ret, 0, 100000, ret); // 100000, assert that function Result is equal to this.
104 if (ret == 0) {
105 exit(Child());
106 } else if (ret > 0) {
107 pid = ret;
108 ret = waitpid(pid, &status, 0);
109 status = WEXITSTATUS(status);
110 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
111 ICUNIT_ASSERT_EQUAL(status, 0, status);
112 }
113
114 return 0;
115 }
116
ItTestProcess010(void)117 void ItTestProcess010(void)
118 {
119 TEST_ADD_CASE("IT_POSIX_PROCESS_010", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
120 }
121