toolchaintest.cpp 4.1 KB
Newer Older
F
FondMemoryVVV 已提交
1
/* Copyright (C) 2022 Huawei Device Co., Ltd.
F
FondMemoryVVV 已提交
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
 * 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>

#include "gtest/gtest.h"
#include "runtest.h"

using namespace std;
using namespace testing::ext;
using namespace testing;
namespace OHOS {
F
FondMemoryVVV 已提交
36
class Toolchaintest : public ::testing::TestWithParam<string> {};
F
FondMemoryVVV 已提交
37

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

F
FondMemoryVVV 已提交
41
volatile int g_tStatus = 0;
F
FondMemoryVVV 已提交
42

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

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

F
FondMemoryVVV 已提交
63
static int RunTests(const char *argvs)
F
FondMemoryVVV 已提交
64
{
zzuli_lyw's avatar
zzuli_lyw 已提交
65
    int timeoutsec = 10, timeout = 0;
F
FondMemoryVVV 已提交
66 67 68
    int status, pid;
    sigset_t set;
    void (*retfunc)(int);
F
FondMemoryVVV 已提交
69
    // signal set
F
FondMemoryVVV 已提交
70 71 72
    sigemptyset(&set);
    sigaddset(&set, SIGCHLD);
    sigprocmask(SIG_BLOCK, &set, nullptr);
F
FondMemoryVVV 已提交
73
    retfunc = signal(SIGCHLD, Handler);
F
FondMemoryVVV 已提交
74 75 76
    if (retfunc == SIG_ERR) {
        printf("signal triggering failed:%s\n", strerror(errno));
    }
F
FondMemoryVVV 已提交
77 78
    pid = Start(argvs);
    // The function system call failed
F
FondMemoryVVV 已提交
79 80 81 82 83 84
    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 已提交
85
    // Maximum blocking time
F
FondMemoryVVV 已提交
86 87 88
    tp.tv_sec = timeoutsec;
    tp.tv_nsec = 0;
    if (sigtimedwait(&set, nullptr, &tp) == -1) {
F
FondMemoryVVV 已提交
89
        // Call it again
F
FondMemoryVVV 已提交
90 91 92 93 94 95 96 97 98
        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 已提交
99
    // Waiting for the process to stop
F
FondMemoryVVV 已提交
100 101 102 103 104
    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 已提交
105 106 107 108
    // Process state
    if (WIFEXITED(status)) { // The right exit
        if (WEXITSTATUS(status) == 0) { // operate successfully
            return g_tStatus;
F
FondMemoryVVV 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122
        }
        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 已提交
123
 * @tc.name      : Toolchaintest.LibcTest
F
FondMemoryVVV 已提交
124 125 126
 * @tc.desc      : start test
 * @tc.level     : Level 3
 */
F
FondMemoryVVV 已提交
127
HWTEST_P(Toolchaintest, LibcTest, Function | MediumTest | Level3)
F
FondMemoryVVV 已提交
128 129 130
{
    int ret;
    string testName = GetParam();
F
FondMemoryVVV 已提交
131
    ret = RunTests(testName.c_str());
F
FondMemoryVVV 已提交
132 133 134 135 136 137 138
    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 已提交
139
INSTANTIATE_TEST_SUITE_P(libcTest, Toolchaintest, testing::ValuesIn(temp.begin(), temp.end()));
zzuli_lyw's avatar
zzuli_lyw 已提交
140
} // namespace OHOS