1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci#include <stdio.h>
16f6603c60Sopenharmony_ci#include <stdlib.h>
17f6603c60Sopenharmony_ci#include <string.h>
18f6603c60Sopenharmony_ci#include <stdlib.h>
19f6603c60Sopenharmony_ci#include <limits.h>
20f6603c60Sopenharmony_ci#include <sys/shm.h>
21f6603c60Sopenharmony_ci#include <assert.h>
22f6603c60Sopenharmony_ci#include <unistd.h>
23f6603c60Sopenharmony_ci#include <gtest/gtest.h>
24f6603c60Sopenharmony_ci#include "utils.h"
25f6603c60Sopenharmony_ci#include "mt_utils.h"
26f6603c60Sopenharmony_ci#include "log.h"
27f6603c60Sopenharmony_ci#include "KernelConstants.h"
28f6603c60Sopenharmony_ci
29f6603c60Sopenharmony_ciusing namespace testing::ext;
30f6603c60Sopenharmony_ci
31f6603c60Sopenharmony_ciclass ProcessTest : public::testing::Test {
32f6603c60Sopenharmony_ci};
33f6603c60Sopenharmony_ci
34f6603c60Sopenharmony_ci/**
35f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_PROCESS_LINE_BIGEXIT_0100
36f6603c60Sopenharmony_ci * @tc.name     Basic test about _Exit
37f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
38f6603c60Sopenharmony_ci */
39f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testLineBigExit, Function | MediumTest | Level2)
40f6603c60Sopenharmony_ci{
41f6603c60Sopenharmony_ci    int exitCode;
42f6603c60Sopenharmony_ci    pid_t pid;
43f6603c60Sopenharmony_ci    int reInt[4] = {0, 1, 100, 255};
44f6603c60Sopenharmony_ci
45f6603c60Sopenharmony_ci    for (int i = 0; i < sizeof(reInt) / sizeof(int); i++) {
46f6603c60Sopenharmony_ci        pid = fork();
47f6603c60Sopenharmony_ci        ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
48f6603c60Sopenharmony_ci        if (pid == 0) {
49f6603c60Sopenharmony_ci            _Exit(reInt[i]);
50f6603c60Sopenharmony_ci        }
51f6603c60Sopenharmony_ci        Msleep(50);
52f6603c60Sopenharmony_ci        exitCode = 0;
53f6603c60Sopenharmony_ci        ASSERT_EQ(CheckProcStatus(pid, &exitCode, 0), 1);
54f6603c60Sopenharmony_ci        ASSERT_EQ(exitCode, reInt[i]);
55f6603c60Sopenharmony_ci    }
56f6603c60Sopenharmony_ci}
57f6603c60Sopenharmony_ci
58f6603c60Sopenharmony_ci/**
59f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_PROCESS_LINE_BIGEXIT_0200
60f6603c60Sopenharmony_ci * @tc.name     Test _Exit about IO flush
61f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
62f6603c60Sopenharmony_ci */
63f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testLineBigExitFlush, Function | MediumTest | Level3)
64f6603c60Sopenharmony_ci{
65f6603c60Sopenharmony_ci    const char* testFile = "TEST_FILE.txt";
66f6603c60Sopenharmony_ci    char writeBuf[] = "this is a file";
67f6603c60Sopenharmony_ci    char readBuf[20] = {0};
68f6603c60Sopenharmony_ci    pid_t pid;
69f6603c60Sopenharmony_ci
70f6603c60Sopenharmony_ci    pid = fork();
71f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
72f6603c60Sopenharmony_ci    if (pid == 0) {
73f6603c60Sopenharmony_ci
74f6603c60Sopenharmony_ci        // write
75f6603c60Sopenharmony_ci        FILE *fp = fopen(testFile, "w+");
76f6603c60Sopenharmony_ci        if (fp == nullptr) {
77f6603c60Sopenharmony_ci            LOG("> child fopen errno = %d", errno);
78f6603c60Sopenharmony_ci            _Exit(1);
79f6603c60Sopenharmony_ci        }
80f6603c60Sopenharmony_ci        fwrite(writeBuf, sizeof(writeBuf), 1, fp);
81f6603c60Sopenharmony_ci        _Exit(0);
82f6603c60Sopenharmony_ci    }
83f6603c60Sopenharmony_ci
84f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
85f6603c60Sopenharmony_ci
86f6603c60Sopenharmony_ci    // read
87f6603c60Sopenharmony_ci    FILE *fp = fopen(testFile, "r+");
88f6603c60Sopenharmony_ci    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
89f6603c60Sopenharmony_ci    EXPECT_EQ(fread(readBuf, sizeof(writeBuf), 1, fp), 0);
90f6603c60Sopenharmony_ci    EXPECT_STRNE(writeBuf, readBuf) << "> writeBuf = " << writeBuf\
91f6603c60Sopenharmony_ci                                    << "\n> readBuf = " << readBuf;
92f6603c60Sopenharmony_ci    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" <<errno;
93f6603c60Sopenharmony_ci    remove(testFile);
94f6603c60Sopenharmony_ci}
95f6603c60Sopenharmony_ci
96f6603c60Sopenharmony_ciint g_shmid0;
97f6603c60Sopenharmony_ci
98f6603c60Sopenharmony_civoid AtexitCallback0(void)
99f6603c60Sopenharmony_ci{
100f6603c60Sopenharmony_ci    LOG("> AtexitCallback0");
101f6603c60Sopenharmony_ci    pid_t *shared = (int*)shmat(g_shmid0, nullptr, 0);
102f6603c60Sopenharmony_ci    if (shared == (void *)-1) {
103f6603c60Sopenharmony_ci        LOG("> AtexitCallback0 shmat errno = %d", errno);
104f6603c60Sopenharmony_ci        _Exit(1);
105f6603c60Sopenharmony_ci    }
106f6603c60Sopenharmony_ci    *shared = getppid();
107f6603c60Sopenharmony_ci    if ((shmdt(shared)) == -1) {
108f6603c60Sopenharmony_ci        LOG("> AtexitCallback0 shmdt errno = %d", errno);
109f6603c60Sopenharmony_ci        _Exit(1);
110f6603c60Sopenharmony_ci    }
111f6603c60Sopenharmony_ci    LOG("> 333");
112f6603c60Sopenharmony_ci}
113f6603c60Sopenharmony_ci
114f6603c60Sopenharmony_ci/**
115f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_PROCESS_LINE_BIGEXIT_0300
116f6603c60Sopenharmony_ci * @tc.name     Test _Exit about call functions registered with atexit
117f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
118f6603c60Sopenharmony_ci */
119f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testLineBigExitAtexit, Function | MediumTest | Level3)
120f6603c60Sopenharmony_ci{
121f6603c60Sopenharmony_ci    const int memSize = 1024;
122f6603c60Sopenharmony_ci    g_shmid0 = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
123f6603c60Sopenharmony_ci    ASSERT_NE(g_shmid0, -1) << "> parent: shmid errno = " << errno;
124f6603c60Sopenharmony_ci    pid_t pid = fork();
125f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
126f6603c60Sopenharmony_ci    if (pid == 0) {
127f6603c60Sopenharmony_ci        Msleep(20);
128f6603c60Sopenharmony_ci        if (atexit(AtexitCallback0) != 0) {
129f6603c60Sopenharmony_ci            _Exit(1);
130f6603c60Sopenharmony_ci        }
131f6603c60Sopenharmony_ci        LOG("> 222");
132f6603c60Sopenharmony_ci        _Exit(0);
133f6603c60Sopenharmony_ci    }
134f6603c60Sopenharmony_ci    pid_t *shared = (int*)shmat(g_shmid0, nullptr, 0);
135f6603c60Sopenharmony_ci    LOG("> 111");
136f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
137f6603c60Sopenharmony_ci    LOG("> 444");
138f6603c60Sopenharmony_ci    ASSERT_NE(shared, (void*)-1) << "> shmat errno = " << errno;
139f6603c60Sopenharmony_ci    EXPECT_NE(*shared, getpid());
140f6603c60Sopenharmony_ci    ASSERT_NE(shmdt(shared), -1) << "> parent: shmdt errno = " << errno;
141f6603c60Sopenharmony_ci    ASSERT_NE(shmctl(g_shmid0, IPC_RMID, nullptr), -1) << "> shmctl : IPC_RMID : errno = " << errno;
142f6603c60Sopenharmony_ci}
143f6603c60Sopenharmony_ci
144f6603c60Sopenharmony_ci/**
145f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_PROCESS_LINE_EXIT_0100
146f6603c60Sopenharmony_ci * @tc.name     Basic test about _exit
147f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
148f6603c60Sopenharmony_ci */
149f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testLineExit, Function | MediumTest | Level2)
150f6603c60Sopenharmony_ci{
151f6603c60Sopenharmony_ci    int exitCode;
152f6603c60Sopenharmony_ci    pid_t pid;
153f6603c60Sopenharmony_ci    int reInt[4] = {0, 1, 100, 255};
154f6603c60Sopenharmony_ci
155f6603c60Sopenharmony_ci    for (int i = 0; i < sizeof(reInt) / sizeof(int); i++) {
156f6603c60Sopenharmony_ci        pid = fork();
157f6603c60Sopenharmony_ci        ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
158f6603c60Sopenharmony_ci        if (pid == 0) {
159f6603c60Sopenharmony_ci            _exit(reInt[i]);
160f6603c60Sopenharmony_ci        }
161f6603c60Sopenharmony_ci        Msleep(50);
162f6603c60Sopenharmony_ci        exitCode = 0;
163f6603c60Sopenharmony_ci        ASSERT_EQ(CheckProcStatus(pid, &exitCode, 0), 1);
164f6603c60Sopenharmony_ci        ASSERT_EQ(exitCode, reInt[i]);
165f6603c60Sopenharmony_ci    }
166f6603c60Sopenharmony_ci}
167f6603c60Sopenharmony_ci
168f6603c60Sopenharmony_ci/**
169f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_PROCESS_LINE_EXIT_0200
170f6603c60Sopenharmony_ci * @tc.name     Test _exit about IO flush
171f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
172f6603c60Sopenharmony_ci */
173f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testLineExitFlush, Function | MediumTest | Level3)
174f6603c60Sopenharmony_ci{
175f6603c60Sopenharmony_ci    const char* testFile = "TEST_FILE.txt";
176f6603c60Sopenharmony_ci    char writeBuf[] = "this is a file";
177f6603c60Sopenharmony_ci    char readBuf[20] = {0};
178f6603c60Sopenharmony_ci    pid_t pid;
179f6603c60Sopenharmony_ci
180f6603c60Sopenharmony_ci    pid = fork();
181f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
182f6603c60Sopenharmony_ci    if (pid == 0) {
183f6603c60Sopenharmony_ci
184f6603c60Sopenharmony_ci        // write
185f6603c60Sopenharmony_ci        FILE *fp = fopen(testFile, "w+");
186f6603c60Sopenharmony_ci        if (fp == nullptr) {
187f6603c60Sopenharmony_ci            LOG("> child fopen errno = %d", errno);
188f6603c60Sopenharmony_ci            _exit(1);
189f6603c60Sopenharmony_ci        }
190f6603c60Sopenharmony_ci        fwrite(writeBuf, sizeof(writeBuf), 1, fp);
191f6603c60Sopenharmony_ci        _exit(0);
192f6603c60Sopenharmony_ci    }
193f6603c60Sopenharmony_ci
194f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
195f6603c60Sopenharmony_ci
196f6603c60Sopenharmony_ci    // read
197f6603c60Sopenharmony_ci    FILE *fp = fopen(testFile, "r+");
198f6603c60Sopenharmony_ci    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
199f6603c60Sopenharmony_ci    EXPECT_EQ(fread(readBuf, sizeof(writeBuf), 1, fp), 0);
200f6603c60Sopenharmony_ci    EXPECT_STRNE(writeBuf, readBuf) << "> writeBuf = " << writeBuf\
201f6603c60Sopenharmony_ci                                    << "\n> readBuf = " << readBuf;
202f6603c60Sopenharmony_ci    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" <<errno;
203f6603c60Sopenharmony_ci    remove(testFile);
204f6603c60Sopenharmony_ci}
205f6603c60Sopenharmony_ci
206f6603c60Sopenharmony_ciint g_shmid1;
207f6603c60Sopenharmony_ci
208f6603c60Sopenharmony_civoid AtexitCallback1(void)
209f6603c60Sopenharmony_ci{
210f6603c60Sopenharmony_ci    LOG("> AtexitCallback0");
211f6603c60Sopenharmony_ci    pid_t *shared = (int*)shmat(g_shmid1, nullptr, 0);
212f6603c60Sopenharmony_ci    if (shared == (void *)-1) {
213f6603c60Sopenharmony_ci        LOG("> AtexitCallback0 shmat errno = %d", errno);
214f6603c60Sopenharmony_ci        _exit(1);
215f6603c60Sopenharmony_ci    }
216f6603c60Sopenharmony_ci    *shared = getppid();
217f6603c60Sopenharmony_ci    if ((shmdt(shared)) == -1) {
218f6603c60Sopenharmony_ci        LOG("> AtexitCallback0 shmdt errno = %d", errno);
219f6603c60Sopenharmony_ci        _exit(1);
220f6603c60Sopenharmony_ci    }
221f6603c60Sopenharmony_ci    LOG("> 333");
222f6603c60Sopenharmony_ci}
223f6603c60Sopenharmony_ci
224f6603c60Sopenharmony_ci/**
225f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_PROCESS_LINE_EXIT_0300
226f6603c60Sopenharmony_ci * @tc.name     Test _exit about call functions registered with atexit
227f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
228f6603c60Sopenharmony_ci */
229f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testLineExitAtexit, Function | MediumTest | Level3)
230f6603c60Sopenharmony_ci{
231f6603c60Sopenharmony_ci    const int memSize = 1024;
232f6603c60Sopenharmony_ci    g_shmid1 = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
233f6603c60Sopenharmony_ci    ASSERT_NE(g_shmid1, -1) << "> parent: shmid errno = " << errno;
234f6603c60Sopenharmony_ci    pid_t pid = fork();
235f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
236f6603c60Sopenharmony_ci    if (pid == 0) {
237f6603c60Sopenharmony_ci        Msleep(20);
238f6603c60Sopenharmony_ci        if (atexit(AtexitCallback1) != 0) {
239f6603c60Sopenharmony_ci            _exit(1);
240f6603c60Sopenharmony_ci        }
241f6603c60Sopenharmony_ci        LOG("> 222");
242f6603c60Sopenharmony_ci        _exit(0);
243f6603c60Sopenharmony_ci    }
244f6603c60Sopenharmony_ci    pid_t *shared = (int*)shmat(g_shmid1, nullptr, 0);
245f6603c60Sopenharmony_ci    LOG("> 111");
246f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
247f6603c60Sopenharmony_ci    LOG("> 444");
248f6603c60Sopenharmony_ci    ASSERT_NE(shared, (void*)-1) << "> shmat errno = " << errno;
249f6603c60Sopenharmony_ci    EXPECT_NE(*shared, getpid());
250f6603c60Sopenharmony_ci    ASSERT_NE(shmdt(shared), -1) << "> parent: shmdt errno = " << errno;
251f6603c60Sopenharmony_ci    ASSERT_NE(shmctl(g_shmid1, IPC_RMID, nullptr), -1) << "> shmctl : IPC_RMID : errno = " << errno;
252f6603c60Sopenharmony_ci}
253f6603c60Sopenharmony_ci
254f6603c60Sopenharmony_ci/**
255f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_PROCESS_EXIT_0100
256f6603c60Sopenharmony_ci * @tc.name     Basic test about exit
257f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
258f6603c60Sopenharmony_ci */
259f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testExit, Function | MediumTest | Level2)
260f6603c60Sopenharmony_ci{
261f6603c60Sopenharmony_ci    int exitCode;
262f6603c60Sopenharmony_ci    pid_t pid;
263f6603c60Sopenharmony_ci    int reInt[4] = {0, 1, 100, 255};
264f6603c60Sopenharmony_ci
265f6603c60Sopenharmony_ci    for (int i = 0; i < sizeof(reInt) / sizeof(int); i++) {
266f6603c60Sopenharmony_ci        pid = fork();
267f6603c60Sopenharmony_ci        ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
268f6603c60Sopenharmony_ci        if (pid == 0) {
269f6603c60Sopenharmony_ci            exit(reInt[i]);
270f6603c60Sopenharmony_ci        }
271f6603c60Sopenharmony_ci        Msleep(50);
272f6603c60Sopenharmony_ci        exitCode = 0;
273f6603c60Sopenharmony_ci        ASSERT_EQ(CheckProcStatus(pid, &exitCode, 0), 1);
274f6603c60Sopenharmony_ci        ASSERT_EQ(exitCode, reInt[i]);
275f6603c60Sopenharmony_ci    }
276f6603c60Sopenharmony_ci}
277f6603c60Sopenharmony_ci/**
278f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_PROCESS_ASSERT_0100
279f6603c60Sopenharmony_ci * @tc.name     Basic test about assert true
280f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
281f6603c60Sopenharmony_ci */
282f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testAssertTrue, Function | MediumTest | Level3)
283f6603c60Sopenharmony_ci{
284f6603c60Sopenharmony_ci    pid_t pid = fork();
285f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
286f6603c60Sopenharmony_ci    if (pid == 0) {
287f6603c60Sopenharmony_ci        assert(true);
288f6603c60Sopenharmony_ci        exit(0);
289f6603c60Sopenharmony_ci    }
290f6603c60Sopenharmony_ci    Msleep(50);
291f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
292f6603c60Sopenharmony_ci}
293f6603c60Sopenharmony_ci
294f6603c60Sopenharmony_ciint FunctionAssertFalse(void)
295f6603c60Sopenharmony_ci{
296f6603c60Sopenharmony_ci    pid_t pid = fork();
297f6603c60Sopenharmony_ci    if (pid < 0) {
298f6603c60Sopenharmony_ci        LOG("> fork errno = %d", errno);
299f6603c60Sopenharmony_ci        return 0;
300f6603c60Sopenharmony_ci    } else if (pid == 0) {
301f6603c60Sopenharmony_ci        assert(false);
302f6603c60Sopenharmony_ci        LOG("> child not stop");
303f6603c60Sopenharmony_ci        exit(0);
304f6603c60Sopenharmony_ci    }
305f6603c60Sopenharmony_ci    Msleep(50);
306f6603c60Sopenharmony_ci    int exitCode = 0;
307f6603c60Sopenharmony_ci    int reInt = CheckProcStatus(pid, &exitCode, 0);
308f6603c60Sopenharmony_ci    if ((reInt == 2) && (exitCode == SIGABRT)) {
309f6603c60Sopenharmony_ci        LOG("> Success");
310f6603c60Sopenharmony_ci        return 1;
311f6603c60Sopenharmony_ci    }
312f6603c60Sopenharmony_ci    LOG("> Fail");
313f6603c60Sopenharmony_ci    return 0;
314f6603c60Sopenharmony_ci}
315f6603c60Sopenharmony_ci
316f6603c60Sopenharmony_ci/**
317f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_PROCESS_ASSERT_0200
318f6603c60Sopenharmony_ci * @tc.name     Basic test about assert false
319f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
320f6603c60Sopenharmony_ci */
321f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testAssertFalse, Function | MediumTest | Level3)
322f6603c60Sopenharmony_ci{
323f6603c60Sopenharmony_ci    pid_t pid = fork();
324f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
325f6603c60Sopenharmony_ci    if (pid == 0) {
326f6603c60Sopenharmony_ci        assert(false);
327f6603c60Sopenharmony_ci        LOG("> child not stop");
328f6603c60Sopenharmony_ci        exit(0);
329f6603c60Sopenharmony_ci    }
330f6603c60Sopenharmony_ci    Msleep(50);
331f6603c60Sopenharmony_ci    int exitCode = 0;
332f6603c60Sopenharmony_ci    ASSERT_EQ(CheckProcStatus(pid, &exitCode, 0), 2);
333f6603c60Sopenharmony_ci    ASSERT_EQ(exitCode, SIGABRT);
334f6603c60Sopenharmony_ci}
335f6603c60Sopenharmony_ci
336f6603c60Sopenharmony_ciint g_shmid;
337f6603c60Sopenharmony_ci
338f6603c60Sopenharmony_civoid AtexitCallback(void)
339f6603c60Sopenharmony_ci{
340f6603c60Sopenharmony_ci    LOG("> AtexitCallback");
341f6603c60Sopenharmony_ci    pid_t *shared = (int*)shmat(g_shmid, nullptr, 0);
342f6603c60Sopenharmony_ci    if (shared == (void *)-1) {
343f6603c60Sopenharmony_ci        LOG("> AtexitCallback shmat errno = %d", errno);
344f6603c60Sopenharmony_ci        exit(1);
345f6603c60Sopenharmony_ci    }
346f6603c60Sopenharmony_ci    *shared = getppid();
347f6603c60Sopenharmony_ci    if ((shmdt(shared)) == -1) {
348f6603c60Sopenharmony_ci        LOG("> AtexitCallback shmdt errno = %d", errno);
349f6603c60Sopenharmony_ci        exit(1);
350f6603c60Sopenharmony_ci    }
351f6603c60Sopenharmony_ci}
352f6603c60Sopenharmony_ci
353f6603c60Sopenharmony_ci/**
354f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_PROCESS_ATEXIT_0100
355f6603c60Sopenharmony_ci * @tc.name     Basic test about atexit
356f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
357f6603c60Sopenharmony_ci */
358f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testAtexit, Function | MediumTest | Level3)
359f6603c60Sopenharmony_ci{
360f6603c60Sopenharmony_ci    const int memSize = 1024;
361f6603c60Sopenharmony_ci    g_shmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
362f6603c60Sopenharmony_ci    ASSERT_NE(g_shmid, -1) << "> parent: shmid errno = " << errno;
363f6603c60Sopenharmony_ci    pid_t pid = fork();
364f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
365f6603c60Sopenharmony_ci    if (pid == 0) {
366f6603c60Sopenharmony_ci        Msleep(20);
367f6603c60Sopenharmony_ci        if (atexit(AtexitCallback) != 0) {
368f6603c60Sopenharmony_ci            LOG("> atexit errno = %d", errno);
369f6603c60Sopenharmony_ci            exit(1);
370f6603c60Sopenharmony_ci        }
371f6603c60Sopenharmony_ci        exit(0);
372f6603c60Sopenharmony_ci    }
373f6603c60Sopenharmony_ci    pid_t *shared = (int*)shmat(g_shmid, nullptr, 0);
374f6603c60Sopenharmony_ci
375f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
376f6603c60Sopenharmony_ci    ASSERT_NE(shared, (void*)-1) << "> shmat errno = " << errno;
377f6603c60Sopenharmony_ci    EXPECT_EQ(*shared, getpid());
378f6603c60Sopenharmony_ci    ASSERT_NE(shmdt(shared), -1) << "> parent: shmdt errno = " << errno;
379f6603c60Sopenharmony_ci    ASSERT_NE(shmctl(g_shmid, IPC_RMID, nullptr), -1) << "> shmctl : IPC_RMID : errno = " << errno;
380f6603c60Sopenharmony_ci}
381f6603c60Sopenharmony_ci
382f6603c60Sopenharmony_ci/**
383f6603c60Sopenharmony_ci * @tc.number     SUB_KERNEL_PROCESS_WAIT_0100
384f6603c60Sopenharmony_ci * @tc.name       test wait return
385f6603c60Sopenharmony_ci * @tc.desc       [C- SOFTWARE -0200]
386f6603c60Sopenharmony_ci */
387f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testWaitReturn, Function | MediumTest | Level3)
388f6603c60Sopenharmony_ci{
389f6603c60Sopenharmony_ci    InitGlobalVariable();
390f6603c60Sopenharmony_ci    pid_t pid = fork();
391f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> fork errno = " << errno;
392f6603c60Sopenharmony_ci    if (pid == 0) {
393f6603c60Sopenharmony_ci        int myPid = (int)getpid();
394f6603c60Sopenharmony_ci        LOG("> child pid = %d", myPid);
395f6603c60Sopenharmony_ci        SetGlobalVariable(myPid);
396f6603c60Sopenharmony_ci        exit(0);
397f6603c60Sopenharmony_ci    }
398f6603c60Sopenharmony_ci    Msleep(20);
399f6603c60Sopenharmony_ci    int pidChild = (int)wait(nullptr);
400f6603c60Sopenharmony_ci    LOG("> pidChild = %d", pidChild);
401f6603c60Sopenharmony_ci    int expectPid = GetGlobalVariable();
402f6603c60Sopenharmony_ci    EXPECT_EQ(pidChild, expectPid);
403f6603c60Sopenharmony_ci    DeleteGlobalVariable();
404f6603c60Sopenharmony_ci}
405f6603c60Sopenharmony_ci
406f6603c60Sopenharmony_ci
407f6603c60Sopenharmony_ci/**
408f6603c60Sopenharmony_ci * @tc.number     SUB_KERNEL_PROCESS_WAIT_0200
409f6603c60Sopenharmony_ci * @tc.name       test wait parameter
410f6603c60Sopenharmony_ci * @tc.desc       [C- SOFTWARE -0200]
411f6603c60Sopenharmony_ci */
412f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testWaitTest, Function | MediumTest | Level3)
413f6603c60Sopenharmony_ci{
414f6603c60Sopenharmony_ci    int childReturn = GetRandom(256) - 1;
415f6603c60Sopenharmony_ci    LOG("> childReturn = %d", childReturn);
416f6603c60Sopenharmony_ci
417f6603c60Sopenharmony_ci    pid_t pid = fork();
418f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> fork errno = " << errno;
419f6603c60Sopenharmony_ci    if (pid == 0) {
420f6603c60Sopenharmony_ci        exit(childReturn);
421f6603c60Sopenharmony_ci    }
422f6603c60Sopenharmony_ci    Msleep(20);
423f6603c60Sopenharmony_ci    int status = 0;
424f6603c60Sopenharmony_ci    int pidChild = (int)wait(&status);
425f6603c60Sopenharmony_ci    EXPECT_NE(pidChild, -1);
426f6603c60Sopenharmony_ci    EXPECT_TRUE(WIFEXITED(status));
427f6603c60Sopenharmony_ci    EXPECT_EQ(WEXITSTATUS(status), childReturn);
428f6603c60Sopenharmony_ci}
429f6603c60Sopenharmony_ci
430f6603c60Sopenharmony_ci/**
431f6603c60Sopenharmony_ci * @tc.number     SUB_KERNEL_PROCESS_WAIT_0300
432f6603c60Sopenharmony_ci * @tc.name       test wait killed by SIGABRT
433f6603c60Sopenharmony_ci * @tc.desc       [C- SOFTWARE -0200]
434f6603c60Sopenharmony_ci */
435f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testWaitKilled, Function | MediumTest | Level3)
436f6603c60Sopenharmony_ci{
437f6603c60Sopenharmony_ci    pid_t pid = fork();
438f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> fork errno = " << errno;
439f6603c60Sopenharmony_ci    if (pid == 0) {
440f6603c60Sopenharmony_ci        Msleep(40);
441f6603c60Sopenharmony_ci        exit(0);
442f6603c60Sopenharmony_ci    }
443f6603c60Sopenharmony_ci    Msleep(20);
444f6603c60Sopenharmony_ci    kill(pid, SIGABRT);
445f6603c60Sopenharmony_ci    int status = 0;
446f6603c60Sopenharmony_ci    int pidChild = (int)wait(&status);
447f6603c60Sopenharmony_ci    EXPECT_NE(pidChild, -1);
448f6603c60Sopenharmony_ci    EXPECT_TRUE(WIFSIGNALED(status));
449f6603c60Sopenharmony_ci    EXPECT_EQ(WTERMSIG(status), SIGABRT);
450f6603c60Sopenharmony_ci}
451f6603c60Sopenharmony_ci
452f6603c60Sopenharmony_ci/**
453f6603c60Sopenharmony_ci * @tc.number     SUB_KERNEL_PROCESS_WAITPID_0100
454f6603c60Sopenharmony_ci * @tc.name       waitpid basic test
455f6603c60Sopenharmony_ci * @tc.desc       [C- SOFTWARE -0200]
456f6603c60Sopenharmony_ci */
457f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testWaitPidBasic, Function | MediumTest | Level3)
458f6603c60Sopenharmony_ci{
459f6603c60Sopenharmony_ci    int childReturn = GetRandom(128) - 1;
460f6603c60Sopenharmony_ci    LOG("> childReturn = %d", childReturn);
461f6603c60Sopenharmony_ci
462f6603c60Sopenharmony_ci    pid_t pid = fork();
463f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> fork errno = " << errno;
464f6603c60Sopenharmony_ci    if (pid == 0) {
465f6603c60Sopenharmony_ci        exit(childReturn);
466f6603c60Sopenharmony_ci    }
467f6603c60Sopenharmony_ci    Msleep(20);
468f6603c60Sopenharmony_ci    int status = 0;
469f6603c60Sopenharmony_ci    int pidChild = (int)waitpid(pid, &status, 0);
470f6603c60Sopenharmony_ci    EXPECT_NE(pidChild, -1);
471f6603c60Sopenharmony_ci    EXPECT_TRUE(WIFEXITED(status));
472f6603c60Sopenharmony_ci    EXPECT_EQ(WEXITSTATUS(status), childReturn);
473f6603c60Sopenharmony_ci}
474f6603c60Sopenharmony_ci
475f6603c60Sopenharmony_ci/**
476f6603c60Sopenharmony_ci * @tc.number     SUB_KERNEL_PROCESS_WAITPID_0200
477f6603c60Sopenharmony_ci * @tc.name       test waitpid with WNOHANG
478f6603c60Sopenharmony_ci * @tc.desc       [C- SOFTWARE -0200]
479f6603c60Sopenharmony_ci */
480f6603c60Sopenharmony_ciHWTEST_F(ProcessTest, testWaitPidTest, Function | MediumTest | Level3)
481f6603c60Sopenharmony_ci{
482f6603c60Sopenharmony_ci    InitGlobalVariable();
483f6603c60Sopenharmony_ci    SetGlobalVariable(1);
484f6603c60Sopenharmony_ci    pid_t pid = fork();
485f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "> fork errno = " << errno;
486f6603c60Sopenharmony_ci    if (pid == 0) {
487f6603c60Sopenharmony_ci        Msleep(50);
488f6603c60Sopenharmony_ci        SetGlobalVariable(2);
489f6603c60Sopenharmony_ci        exit(0);
490f6603c60Sopenharmony_ci    }
491f6603c60Sopenharmony_ci    Msleep(20);
492f6603c60Sopenharmony_ci    int status = 0;
493f6603c60Sopenharmony_ci    int pidChild = (int)waitpid(pid, &status, WNOHANG);
494f6603c60Sopenharmony_ci    int expectPid = GetGlobalVariable();
495f6603c60Sopenharmony_ci    EXPECT_EQ(expectPid, 1);
496f6603c60Sopenharmony_ci    EXPECT_EQ(pidChild, 0);
497f6603c60Sopenharmony_ci    Msleep(50);
498f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
499f6603c60Sopenharmony_ci    DeleteGlobalVariable();
500f6603c60Sopenharmony_ci}
501