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 __attribute__((optnone)) static void *ThreadFunc2(void *arg)
36 {
37 printf("111111111111111: exit\n");
38 exit(254); // 254, exit args
39 }
40
ThreadFunc3(void * arg)41 __attribute__((optnone)) static void *ThreadFunc3(void *arg)
42 {
43 while (1) {
44 }
45 }
46
ProcessTest001(void)47 __attribute__((optnone)) static int ProcessTest001(void)
48 {
49 int ret;
50 int status;
51 int pid;
52 int policy = 0;
53 int data;
54 int pri;
55 pthread_t newPthread, newPthread1;
56 int count = 4;
57 pthread_attr_t a = { 0 };
58 struct sched_param param = { 0 };
59 int currProcessPri = getpriority(PRIO_PROCESS, getpid());
60 ICUNIT_ASSERT_WITHIN_EQUAL(currProcessPri, 0, 31, currProcessPri); // 31, assert that function Result is equal to this.
61
62 ret = setpriority(PRIO_PROCESS, getpid(), currProcessPri - 2); // 2, Used to calculate priorities.
63 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
64
65 ret = pthread_getschedparam(pthread_self(), &policy, ¶m);
66 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
67
68 ret = pthread_attr_init(&a);
69 param.sched_priority = 26; /* 26: prio */
70 pthread_attr_setschedparam(&a, ¶m);
71 pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
72 ret = pthread_create(&newPthread, &a, ThreadFunc3, &data);
73 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
74
75 data = ret;
76 ret = pthread_create(&newPthread, NULL, ThreadFunc2, &data);
77 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
78
79 printf("222222222222222: exit\n");
80 exit(255); // 255, exit args
81 return 0;
82 }
83
Testcase(void)84 __attribute__((optnone)) static int Testcase(void)
85 {
86 int ret;
87 int status;
88 int pid;
89 int count = TEST_COUNT;
90 int temp = GetCpuCount();
91 if (temp <= 1) {
92 return 0;
93 }
94
95 while (count > 0) {
96 ret = fork();
97 if (ret == 0) {
98 ret = ProcessTest001();
99 exit(10); // 10, exit args
100 } else if (ret > 0) {
101 pid = ret;
102 ret = wait(&status);
103 status = WEXITSTATUS(status);
104 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
105 ICUNIT_ASSERT_TWO_EQUAL(status, 255, 254, status); // 255, 254, assert that function Result is equal to this.
106 }
107
108 ICUNIT_ASSERT_WITHIN_EQUAL(ret, 0, 100000, ret); // 100000, assert that function Result is equal to this.
109 count--;
110 }
111
112 return 0;
113 }
114
ItTestProcess008(void)115 void ItTestProcess008(void)
116 {
117 TEST_ADD_CASE("IT_POSIX_PROCESS_008", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
118 }
119