ProcessTest.cpp 14.7 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/shm.h>
#include <assert.h>
#include <unistd.h>
#include <gtest/gtest.h>
#include "utils.h"
#include "mt_utils.h"
#include "log.h"
#include "KernelConstants.h"

using namespace testing::ext;

class ProcessTest : public::testing::Test {
};

/**
 * @tc.number   SUB_KERNEL_PROCESS_LINE_BIGEXIT_0100
 * @tc.name     Basic test about _Exit
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testLineBigExit, Function | MediumTest | Level2)
{
    int exitCode;
    pid_t pid;
    int reInt[4] = {0, 1, 100, 255};

    for (int i = 0; i < sizeof(reInt) / sizeof(int); i++) {
        pid = fork();
        ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
        if (pid == 0) {
            _Exit(reInt[i]);
        }
        Msleep(50);
        exitCode = 0;
        ASSERT_EQ(CheckProcStatus(pid, &exitCode), 1);
        ASSERT_EQ(exitCode, reInt[i]);
    }
}

/**
 * @tc.number   SUB_KERNEL_PROCESS_LINE_BIGEXIT_0200
 * @tc.name     Test _Exit about IO flush
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testLineBigExitFlush, Function | MediumTest | Level3)
{
    const char* testFile = "TEST_FILE.txt";
    char writeBuf[] = "this is a file";
    char readBuf[20] = {0};
    pid_t pid;

    pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
    if (pid == 0) {

        // write
        FILE *fp = fopen(testFile, "w+");
        if (fp == nullptr) {
            LOG("> child fopen errno = %d", errno);
            _Exit(1);
        }
        fwrite(writeBuf, sizeof(writeBuf), 1, fp);
        _Exit(0);
    }

    WaitProcExitedOK(pid);

    // read
    FILE *fp = fopen(testFile, "r+");
88
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    EXPECT_EQ(fread(readBuf, sizeof(writeBuf), 1, fp), 0);
    EXPECT_STRNE(writeBuf, readBuf) << "> writeBuf = " << writeBuf\
                                    << "\n> readBuf = " << readBuf;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" <<errno;
    remove(testFile);
}

int g_shmid0;

void AtexitCallback0(void)
{
    LOG("> AtexitCallback0");
    pid_t *shared = (int*)shmat(g_shmid0, nullptr, 0);
    if (shared == (void *)-1) {
        LOG("> AtexitCallback0 shmat errno = %d", errno);
        _Exit(1);
    }
    *shared = getppid();
    if ((shmdt(shared)) == -1) {
        LOG("> AtexitCallback0 shmdt errno = %d", errno);
        _Exit(1);
    }
    LOG("> 333");
}

/**
 * @tc.number   SUB_KERNEL_PROCESS_LINE_BIGEXIT_0300
 * @tc.name     Test _Exit about call functions registered with atexit
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testLineBigExitAtexit, Function | MediumTest | Level3)
{
    const int memSize = 1024;
    g_shmid0 = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
    ASSERT_NE(g_shmid0, -1) << "> parent: shmid errno = " << errno;
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
    if (pid == 0) {
        Msleep(20);
        if (atexit(AtexitCallback0) != 0) {
            _Exit(1);
        }
        LOG("> 222");
        _Exit(0);
    }
    pid_t *shared = (int*)shmat(g_shmid0, nullptr, 0);
    LOG("> 111");
    WaitProcExitedOK(pid);
    LOG("> 444");
    ASSERT_NE(shared, (void*)-1) << "> shmat errno = " << errno;
    EXPECT_NE(*shared, getpid());
    ASSERT_NE(shmdt(shared), -1) << "> parent: shmdt errno = " << errno;
    ASSERT_NE(shmctl(g_shmid0, IPC_RMID, nullptr), -1) << "> shmctl : IPC_RMID : errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_PROCESS_LINE_EXIT_0100
 * @tc.name     Basic test about _exit
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testLineExit, Function | MediumTest | Level2)
{
    int exitCode;
    pid_t pid;
    int reInt[4] = {0, 1, 100, 255};

    for (int i = 0; i < sizeof(reInt) / sizeof(int); i++) {
        pid = fork();
        ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
        if (pid == 0) {
            _exit(reInt[i]);
        }
        Msleep(50);
        exitCode = 0;
        ASSERT_EQ(CheckProcStatus(pid, &exitCode), 1);
        ASSERT_EQ(exitCode, reInt[i]);
    }
}

/**
 * @tc.number   SUB_KERNEL_PROCESS_LINE_EXIT_0200
 * @tc.name     Test _exit about IO flush
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testLineExitFlush, Function | MediumTest | Level3)
{
    const char* testFile = "TEST_FILE.txt";
    char writeBuf[] = "this is a file";
    char readBuf[20] = {0};
    pid_t pid;

    pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
    if (pid == 0) {

        // write
        FILE *fp = fopen(testFile, "w+");
        if (fp == nullptr) {
            LOG("> child fopen errno = %d", errno);
            _exit(1);
        }
        fwrite(writeBuf, sizeof(writeBuf), 1, fp);
        _exit(0);
    }

    WaitProcExitedOK(pid);

    // read
    FILE *fp = fopen(testFile, "r+");
198
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    EXPECT_EQ(fread(readBuf, sizeof(writeBuf), 1, fp), 0);
    EXPECT_STRNE(writeBuf, readBuf) << "> writeBuf = " << writeBuf\
                                    << "\n> readBuf = " << readBuf;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" <<errno;
    remove(testFile);
}

int g_shmid1;

void AtexitCallback1(void)
{
    LOG("> AtexitCallback0");
    pid_t *shared = (int*)shmat(g_shmid1, nullptr, 0);
    if (shared == (void *)-1) {
        LOG("> AtexitCallback0 shmat errno = %d", errno);
        _exit(1);
    }
    *shared = getppid();
    if ((shmdt(shared)) == -1) {
        LOG("> AtexitCallback0 shmdt errno = %d", errno);
        _exit(1);
    }
    LOG("> 333");
}

/**
 * @tc.number   SUB_KERNEL_PROCESS_LINE_EXIT_0300
 * @tc.name     Test _exit about call functions registered with atexit
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testLineExitAtexit, Function | MediumTest | Level3)
{
    const int memSize = 1024;
    g_shmid1 = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
    ASSERT_NE(g_shmid1, -1) << "> parent: shmid errno = " << errno;
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
    if (pid == 0) {
        Msleep(20);
        if (atexit(AtexitCallback1) != 0) {
            _exit(1);
        }
        LOG("> 222");
        _exit(0);
    }
    pid_t *shared = (int*)shmat(g_shmid1, nullptr, 0);
    LOG("> 111");
    WaitProcExitedOK(pid);
    LOG("> 444");
    ASSERT_NE(shared, (void*)-1) << "> shmat errno = " << errno;
    EXPECT_NE(*shared, getpid());
    ASSERT_NE(shmdt(shared), -1) << "> parent: shmdt errno = " << errno;
    ASSERT_NE(shmctl(g_shmid1, IPC_RMID, nullptr), -1) << "> shmctl : IPC_RMID : errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_PROCESS_EXIT_0100
 * @tc.name     Basic test about exit
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testExit, Function | MediumTest | Level2)
{
    int exitCode;
    pid_t pid;
    int reInt[4] = {0, 1, 100, 255};

    for (int i = 0; i < sizeof(reInt) / sizeof(int); i++) {
        pid = fork();
        ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
        if (pid == 0) {
            exit(reInt[i]);
        }
        Msleep(50);
        exitCode = 0;
        ASSERT_EQ(CheckProcStatus(pid, &exitCode), 1);
        ASSERT_EQ(exitCode, reInt[i]);
    }
}
J
jiyong 已提交
277
#if 0
M
mamingshuai 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
/**
 * @tc.number   SUB_KERNEL_PROCESS_EXIT_0200
 * @tc.name     Test exit about IO flush
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testExitFlush, Function | MediumTest | Level3)
{
    const char* testFile = "TEST_FILE.txt";
    char writeBuf[] = "this is a file";
    char readBuf[20] = {0};
    pid_t pid;

    pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
    if (pid == 0) {

        // write
        FILE *fp = fopen(testFile, "w+");
        if (fp == nullptr) {
            LOG("> child fopen errno = %d", errno);
            exit(1);
        }
        fwrite(writeBuf, sizeof(writeBuf), 1, fp);
        exit(0);
    }

    WaitProcExitedOK(pid);

    // read
    FILE *fp = fopen(testFile, "r+");
308
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
309 310 311 312 313 314
    EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0);
    EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf\
                                    << "\n> readBuf = " << readBuf;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" <<errno;
    remove(testFile);
}
J
jiyong 已提交
315
#endif
M
mamingshuai 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
/**
 * @tc.number   SUB_KERNEL_PROCESS_ASSERT_0100
 * @tc.name     Basic test about assert true
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testAssertTrue, Function | MediumTest | Level3)
{
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
    if (pid == 0) {
        assert(true);
        exit(0);
    }
    Msleep(50);
    AssertProcExitedOK(pid);
}

int FunctionAssertFalse(void)
{
    pid_t pid = fork();
    if (pid < 0) {
        LOG("> fork errno = %d", errno);
        return 0;
    } else if (pid == 0) {
        assert(false);
        LOG("> child not stop");
        exit(0);
    }
    Msleep(50);
    int exitCode = 0;
    int reInt = CheckProcStatus(pid, &exitCode);
    if ((reInt == 2) && (exitCode == SIGABRT)) {
        LOG("> Success");
        return 1;
    }
    LOG("> Fail");
    return 0;
}

/**
 * @tc.number   SUB_KERNEL_PROCESS_ASSERT_0200
 * @tc.name     Basic test about assert false
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testAssertFalse, Function | MediumTest | Level3)
{
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
    if (pid == 0) {
        assert(false);
        LOG("> child not stop");
        exit(0);
    }
    Msleep(50);
    int exitCode = 0;
    ASSERT_EQ(CheckProcStatus(pid, &exitCode), 2);
    ASSERT_EQ(exitCode, SIGABRT);
}

int g_shmid;

void AtexitCallback(void)
{
    LOG("> AtexitCallback");
    pid_t *shared = (int*)shmat(g_shmid, nullptr, 0);
    if (shared == (void *)-1) {
        LOG("> AtexitCallback shmat errno = %d", errno);
        exit(1);
    }
    *shared = getppid();
    if ((shmdt(shared)) == -1) {
        LOG("> AtexitCallback shmdt errno = %d", errno);
        exit(1);
    }
}

/**
 * @tc.number   SUB_KERNEL_PROCESS_ATEXIT_0100
 * @tc.name     Basic test about atexit
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testAtexit, Function | MediumTest | Level3)
{
    const int memSize = 1024;
    g_shmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
    ASSERT_NE(g_shmid, -1) << "> parent: shmid errno = " << errno;
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
    if (pid == 0) {
        Msleep(20);
        if (atexit(AtexitCallback) != 0) {
            LOG("> atexit errno = %d", errno);
            exit(1);
        }
        exit(0);
    }
    pid_t *shared = (int*)shmat(g_shmid, nullptr, 0);

    WaitProcExitedOK(pid);
    ASSERT_NE(shared, (void*)-1) << "> shmat errno = " << errno;
    EXPECT_EQ(*shared, getpid());
    ASSERT_NE(shmdt(shared), -1) << "> parent: shmdt errno = " << errno;
    ASSERT_NE(shmctl(g_shmid, IPC_RMID, nullptr), -1) << "> shmctl : IPC_RMID : errno = " << errno;
}

/**
 * @tc.number     SUB_KERNEL_PROCESS_WAIT_0100
 * @tc.name       test wait return
 * @tc.desc       [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testWaitReturn, Function | MediumTest | Level3)
{
    InitGlobalVariable();
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> fork errno = " << errno;
    if (pid == 0) {
        int myPid = (int)getpid();
        LOG("> child pid = %d", myPid);
        SetGlobalVariable(myPid);
        exit(0);
    }
    Msleep(20);
    int pidChild = (int)wait(nullptr);
    LOG("> pidChild = %d", pidChild);
    int expectPid = GetGlobalVariable();
    EXPECT_EQ(pidChild, expectPid);
    DeleteGlobalVariable();
}


/**
 * @tc.number     SUB_KERNEL_PROCESS_WAIT_0200
 * @tc.name       test wait parameter
 * @tc.desc       [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testWaitTest, Function | MediumTest | Level3)
{
    int childReturn = GetRandom(256) - 1;
    LOG("> childReturn = %d", childReturn);

    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> fork errno = " << errno;
    if (pid == 0) {
        exit(childReturn);
    }
    Msleep(20);
    int status = 0;
    int pidChild = (int)wait(&status);
    EXPECT_NE(pidChild, -1);
    EXPECT_TRUE(WIFEXITED(status));
    EXPECT_EQ(WEXITSTATUS(status), childReturn);
}

/**
 * @tc.number     SUB_KERNEL_PROCESS_WAIT_0300
 * @tc.name       test wait killed by SIGABRT
 * @tc.desc       [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testWaitKilled, Function | MediumTest | Level3)
{
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> fork errno = " << errno;
    if (pid == 0) {
        Msleep(40);
        exit(0);
    }
    Msleep(20);
    kill(pid, SIGABRT);
    int status = 0;
    int pidChild = (int)wait(&status);
    EXPECT_NE(pidChild, -1);
    EXPECT_TRUE(WIFSIGNALED(status));
    EXPECT_EQ(WTERMSIG(status), SIGABRT);
}

/**
 * @tc.number     SUB_KERNEL_PROCESS_WAITPID_0100
 * @tc.name       waitpid basic test
 * @tc.desc       [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testWaitPidBasic, Function | MediumTest | Level3)
{
    int childReturn = GetRandom(128) - 1;
    LOG("> childReturn = %d", childReturn);

    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> fork errno = " << errno;
    if (pid == 0) {
        exit(childReturn);
    }
    Msleep(20);
    int status = 0;
    int pidChild = (int)waitpid(pid, &status, 0);
    EXPECT_NE(pidChild, -1);
    EXPECT_TRUE(WIFEXITED(status));
    EXPECT_EQ(WEXITSTATUS(status), childReturn);
}

/**
 * @tc.number     SUB_KERNEL_PROCESS_WAITPID_0200
 * @tc.name       test waitpid with WNOHANG
 * @tc.desc       [C- SOFTWARE -0200]
 */
HWTEST_F(ProcessTest, testWaitPidTest, Function | MediumTest | Level3)
{
    InitGlobalVariable();
    SetGlobalVariable(1);
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> fork errno = " << errno;
    if (pid == 0) {
        Msleep(50);
        SetGlobalVariable(2);
        exit(0);
    }
    Msleep(20);
    int status = 0;
    int pidChild = (int)waitpid(pid, &status, WNOHANG);
    int expectPid = GetGlobalVariable();
    EXPECT_EQ(expectPid, 1);
    EXPECT_EQ(pidChild, 0);
    Msleep(50);
    DeleteGlobalVariable();
}