toolchaintest.cpp 4.1 KB
Newer Older
F
FondMemoryVVV 已提交
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
/*
 * Copyright (C) 2022 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 <iostream>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <ctime>
#include <cstdio>
#include <vector>
#include <csignal>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>

F
FondMemoryVVV 已提交
30
#include "gtest/gtest.h"
F
FondMemoryVVV 已提交
31
#include "runtest.h"
F
FondMemoryVVV 已提交
32

F
FondMemoryVVV 已提交
33 34
using namespace std;
using namespace testing::ext;
F
FondMemoryVVV 已提交
35
using namespace testing;
F
FondMemoryVVV 已提交
36
namespace OHOS {
F
FondMemoryVVV 已提交
37
class toolchaintest : public ::testing::TestWithParam<string> {};
F
FondMemoryVVV 已提交
38

F
FondMemoryVVV 已提交
39
static string filepath = "/data/local/tmp/libc-test/src";
F
FondMemoryVVV 已提交
40
static vector<std::string> temp = runtest::GetFileNames(filepath);
F
FondMemoryVVV 已提交
41

F
FondMemoryVVV 已提交
42 43
volatile int t_status = 0;

F
FondMemoryVVV 已提交
44
static void handler(int sig)
F
FondMemoryVVV 已提交
45 46 47
{
}

F
FondMemoryVVV 已提交
48
static int start(const char *argvs)
F
FondMemoryVVV 已提交
49 50
{
    int pid, space_size = 100*1024;
F
FondMemoryVVV 已提交
51 52
    //Create a child process
    //Set the process stack space
F
FondMemoryVVV 已提交
53 54
    pid = fork();
    if (pid == 0) {
F
FondMemoryVVV 已提交
55
        runtest::t_setrlim(RLIMIT_STACK, space_size);
F
FondMemoryVVV 已提交
56
        //Overloading the subprocess space
F
FondMemoryVVV 已提交
57 58 59 60 61 62 63
        int exe = execl(argvs, "strptime", nullptr);
        printf("exe:%d %s exec failed: %s\n", exe, argvs, strerror(errno));
        exit(1);
    }
    return pid;
}

F
FondMemoryVVV 已提交
64
static int runTests(const char *argvs)
F
FondMemoryVVV 已提交
65 66 67 68
{
    int timeoutsec = 5, timeout = 0;
    int status, pid;
    sigset_t set;
F
FondMemoryVVV 已提交
69
    void (*retfunc)(int);
F
FondMemoryVVV 已提交
70
    //signal set
F
FondMemoryVVV 已提交
71 72 73
    sigemptyset(&set);
    sigaddset(&set, SIGCHLD);
    sigprocmask(SIG_BLOCK, &set, nullptr);
F
FondMemoryVVV 已提交
74
    retfunc = signal(SIGCHLD, handler);
F
FondMemoryVVV 已提交
75
    if (retfunc == SIG_ERR) {
F
FondMemoryVVV 已提交
76 77
        printf("signal triggering failed:%s\n", strerror(errno));
    }
F
FondMemoryVVV 已提交
78 79
    pid = start(argvs);
    //The function system call failed
F
FondMemoryVVV 已提交
80 81 82 83 84 85
    if (pid == -1) {
        printf("%s fork failed: %s\n", argvs, strerror(errno));
        printf("FAIL %s [internal]\n", argvs);
        return -1;
    }
    struct timespec tp;
F
FondMemoryVVV 已提交
86
    //Maximum blocking time
F
FondMemoryVVV 已提交
87 88 89
    tp.tv_sec = timeoutsec;
    tp.tv_nsec = 0;
    if (sigtimedwait(&set, nullptr, &tp) == -1) {
F
FondMemoryVVV 已提交
90
        //Call it again
F
FondMemoryVVV 已提交
91 92 93 94 95 96 97 98 99
        if (errno == EAGAIN) {
            timeout = 1;
        } else {
            printf("%s sigtimedwait failed: %s\n", argvs, strerror(errno));
        }
        if (kill(pid, SIGKILL) == -1) {
            printf("%s kill failed: %s\n", argvs, strerror(errno));
        }
    }
F
FondMemoryVVV 已提交
100
    //Waiting for the process to stop
F
FondMemoryVVV 已提交
101 102 103 104 105
    if (waitpid(pid, &status, 0) != pid) {
        printf("%s waitpid failed: %s\n", argvs, strerror(errno));
        printf("FAIL %s [internal]\n", argvs);
        return -1;
    }
F
FondMemoryVVV 已提交
106
    //Process state
F
FondMemoryVVV 已提交
107 108
    if (WIFEXITED(status)) { //The right exit
        if (WEXITSTATUS(status) == 0) { //operate successfully
F
FondMemoryVVV 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121
            return t_status;
        }
        printf("FAIL %s [status %d]\n", argvs, WEXITSTATUS(status));
    } else if (timeout) {
        printf("FAIL %s [timed out]\n", argvs);
    } else if (WIFSIGNALED(status)) {
        printf("FAIL %s [signal %s]\n", argvs, strsignal(WTERMSIG(status)));
    } else {
        printf("FAIL %s [unknown]\n", argvs);
    }
    return 1;
}

F
FondMemoryVVV 已提交
122

F
FondMemoryVVV 已提交
123
/**
F
FondMemoryVVV 已提交
124
 * @tc.name      : toolchaintest.LibcTest
F
FondMemoryVVV 已提交
125
 * @tc.desc      : start test
F
FondMemoryVVV 已提交
126
 * @tc.level     : Level 3
F
FondMemoryVVV 已提交
127
 */
F
FondMemoryVVV 已提交
128
HWTEST_P(toolchaintest, LibcTest, Function | MediumTest | Level3)
F
FondMemoryVVV 已提交
129 130
{
    int ret;
F
FondMemoryVVV 已提交
131 132 133 134 135 136 137
    string testName = GetParam();
    ret = runTests(testName.c_str());
    if (ret == 0) {
        EXPECT_EQ(0, ret) << "test  " << testName  << "  succeed" << endl;
    } else {
        EXPECT_EQ(1, ret) << "test  " << testName  << "  failed" << endl;
        EXPECT_EQ(-1, ret) << "test  " << testName  << "  failed" << endl;
F
FondMemoryVVV 已提交
138 139
    }
}
F
FondMemoryVVV 已提交
140
INSTANTIATE_TEST_CASE_P(libcTest, toolchaintest, testing::ValuesIn(temp.begin(), temp.end()));
F
FondMemoryVVV 已提交
141
} // namespace OHOS