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 {
37 pid_t pid = *((pid_t *)arg);
38 int ret;
39 int status = 0;
40 ret = waitpid(pid, &status, 0);
41 status = WEXITSTATUS(status);
42 ICUNIT_ASSERT_EQUAL_NULL(ret, pid, ret);
43 ICUNIT_ASSERT_EQUAL_NULL(status, 12, status); // 12, assert that function Result is equal to this.
44 }
45
ProcessTest001(void)46 static int ProcessTest001(void)
47 {
48 int ret;
49 int status = 0;
50 int pid, pid1;
51 int data;
52 int pri;
53 pthread_t newPthread, newPthread1;
54 int count = 4;
55 pthread_attr_t a = { 0 };
56 struct sched_param param = { 0 };
57 int currProcessPri = getpriority(PRIO_PROCESS, getpid());
58 ICUNIT_ASSERT_WITHIN_EQUAL(currProcessPri, 0, 31, currProcessPri); // 31, assert that function Result is equal to this.
59
60 ret = setpriority(PRIO_PROCESS, getpid(), currProcessPri - 2); // 2, Used to calculate priorities.
61 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
62
63 pid = fork();
64 if (pid == 0) {
65 WAIT_PARENT_FIRST_TO_RUN(50); // 50, wait time.
66 exit(12); // 12, exit args
67 }
68
69 ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
70 pid1 = fork();
71 if (pid1 == 0) {
72 WAIT_PARENT_FIRST_TO_RUN(40); // 40, wait time.
73 exit(10); // 10, exit args
74 }
75
76 ICUNIT_ASSERT_WITHIN_EQUAL(pid1, 0, 100000, pid1); // 100000, assert that function Result is equal to this.
77 data = pid;
78 ret = pthread_create(&newPthread, NULL, ThreadFunc2, &data);
79 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
80
81 ret = waitpid(pid1, &status, 0);
82 status = WEXITSTATUS(status);
83 ICUNIT_ASSERT_EQUAL(ret, pid1, ret);
84 ICUNIT_ASSERT_EQUAL(status, 10, status); // 10, assert that function Result is equal to this.
85
86 ret = pthread_detach(newPthread);
87 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
88
89 return 0;
90 }
91
TestCase(void)92 static int TestCase(void)
93 {
94 int ret;
95 int status;
96 int pid;
97 int count = TEST_COUNT;
98
99 int temp = GetCpuCount();
100 if (temp <= 1) {
101 return 0;
102 }
103
104 while (count > 0) {
105 ret = fork();
106 if (ret == 0) {
107 ret = ProcessTest001();
108 exit(11); // 11, exit args
109 } else if (ret > 0) {
110 pid = ret;
111 ret = wait(&status);
112 status = WEXITSTATUS(status);
113 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
114 ICUNIT_ASSERT_EQUAL(status, 11, status); // 11, assert that function Result is equal to this.
115 }
116 count--;
117 }
118 return 0;
119 }
120
ItTestProcess006(void)121 void ItTestProcess006(void)
122 {
123 TEST_ADD_CASE("IT_POSIX_PROCESS_006", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
124 }
125