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 #include "sys/shm.h"
33
34 static const int TEST_THREAD = 40;
35 static const int TEST_LOOP = 3000;
36
Child2(int shmid)37 static void Child2(int shmid)
38 {
39 int count = 2; // 2, Set the calculation number to determine the cycle status.
40 int *shared = (int *)shmat(shmid, nullptr, 0);
41 ICUNIT_ASSERT_NOT_EQUAL_VOID(shared, reinterpret_cast<void *>(-1), shared);
42
43 while ((*shared) < (TEST_LOOP + 2)) { // 2, Set the cycle number.
44 ICUNIT_ASSERT_EQUAL_VOID(*shared, count, *shared);
45 (*shared)++;
46 count += 3; // 3, Set the calculation number to determine the cycle status.
47 sched_yield();
48 }
49
50 exit(255); // 255, exit args
51 return;
52 }
53
Child1(int shmid)54 static void Child1(int shmid)
55 {
56 int count = 1;
57 int *shared = (int *)shmat(shmid, nullptr, 0);
58 ICUNIT_ASSERT_NOT_EQUAL_VOID(shared, reinterpret_cast<void *>(-1), shared);
59
60 while ((*shared) < (TEST_LOOP + 1)) {
61 ICUNIT_ASSERT_EQUAL_VOID(*shared, count, *shared);
62 (*shared)++;
63 count += 3; // 3, Set the calculation number to determine the cycle status.
64 sched_yield();
65 }
66
67 (*shared) = 100000; // 100000, shared num.
68
69 exit(255); // 255, exit args
70 return;
71 }
72
GroupProcess(void)73 static int GroupProcess(void)
74 {
75 int testPid;
76 int ret;
77 int policy = 0;
78 struct sched_param param = { 0 };
79 int status = 0;
80 pid_t pid, pid1;
81 const int memSize = 1024;
82 int shmid;
83 int *shared = NULL;
84
85 ret = sched_getparam(getpid(), ¶m);
86 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
87 int processPrio = param.sched_priority;
88
89 ret = pthread_getschedparam(pthread_self(), &policy, ¶m);
90 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
91
92 ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
93 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
94
95 /* 1234, Sets the shmget key; 0666 config of shmget */
96 shmid = shmget(static_cast<key_t>(1234), memSize, 0666 | IPC_CREAT);
97 ICUNIT_ASSERT_NOT_EQUAL(shmid, -1, shmid);
98
99 pid = fork();
100 ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, assert pid equal to this.
101 if (pid == 0) {
102 Child1(shmid);
103 printf("[Failed] - [Errline : %d RetCode : 0x%x\n", g_iCunitErrLineNo, g_iCunitErrCode);
104 exit(0);
105 }
106
107 pid1 = fork();
108 ICUNIT_GOTO_WITHIN_EQUAL(pid1, 0, 100000, pid1, EXIT); // 100000, assert pid equal to this.
109 if (pid1 == 0) {
110 Child2(shmid);
111 printf("[Failed] - [Errline : %d RetCode : 0x%x\n", g_iCunitErrLineNo, g_iCunitErrCode);
112 exit(0);
113 }
114
115 shared = (int *)shmat(shmid, nullptr, 0);
116 ICUNIT_ASSERT_NOT_EQUAL(shared, reinterpret_cast<void *>(-1), shared);
117
118 (*shared) = 0;
119
120 while ((*shared) < TEST_LOOP) {
121 (*shared)++;
122 sched_yield();
123 }
124
125 (*shared) = TEST_LOOP + 10; // 10, Set the cycle number.
126
127 param.sched_priority = processPrio - 2; // 2, set pthread priority.
128 ret = sched_setscheduler(pid, SCHED_RR, ¶m);
129 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
130
131 ICUNIT_ASSERT_EQUAL(*shared, 100000, *shared); // 100000, assert that function Result is equal to this.
132
133 ret = waitpid(pid, &status, 0);
134 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
135 status = WEXITSTATUS(status);
136 ICUNIT_ASSERT_EQUAL(status, 255, status); // 255, assert that function Result is equal to this.
137
138 ret = waitpid(pid1, &status, 0);
139 ICUNIT_ASSERT_EQUAL(ret, pid1, ret);
140 status = WEXITSTATUS(status);
141 ICUNIT_ASSERT_EQUAL(status, 255, status); // 255, assert that function Result is equal to this.
142
143 exit(255); // 255, exit args
144 EXIT:
145 return 0;
146 }
147
TestCase(void)148 static int TestCase(void)
149 {
150 int ret;
151 int status = 0;
152 pid_t pid, pid1;
153
154 int temp = GetCpuCount();
155 if (temp != 1) {
156 return 0;
157 }
158
159 pid = fork();
160 ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, assert pid equal to this.
161
162 if (pid == 0) {
163 prctl(PR_SET_NAME, "mainFork", 0UL, 0UL, 0UL);
164 GroupProcess();
165 printf("[Failed] - [Errline : %d RetCode : 0x%x\n", g_iCunitErrLineNo, g_iCunitErrCode);
166 exit(g_iCunitErrLineNo);
167 }
168
169 ret = waitpid(pid, &status, 0);
170 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
171 status = WEXITSTATUS(status);
172 ICUNIT_GOTO_EQUAL(status, 255, status, EXIT); // 255, assert status equal to this.
173
174 return 0;
175 EXIT:
176 return 1;
177 }
178
ItTestProcess042(void)179 void ItTestProcess042(void)
180 {
181 TEST_ADD_CASE("IT_POSIX_PROCESS_042", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
182 }
183