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