SleepTest.cpp 3.4 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
/*
 * 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 <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <gtest/gtest.h>
#include "log.h"
#include "utils.h"
#include "ClockID.h"

using namespace testing::ext;

27 28 29 30 31 32
class UsleepParamTest : public testing::TestWithParam<int> {
};
class SleepParamTest : public testing::TestWithParam<int> {
};
class SleepTest : public testing::Test {
};
M
mamingshuai 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46

/**
 * @tc.number SUB_KERNEL_TIME_API_CLOCK_NANOSLEEP_0200
 * @tc.name   clock_nanosleep fail test - non-support clock_id
 * @tc.desc   [C- SOFTWARE -0200]
 */
HWTEST_P(AllClockIDTest, testClockNanosleepInvalidID, Reliability | SmallTest | Level2)
{
    clockid_t cid = GetParam();
    const char* cname = ALL_CLOCKS_NAME[cid];
    LOG("test %s", cname);
    struct timespec req = {0, 100};
    struct timespec rem = {0};
    int rt = clock_nanosleep(cid, 0, &req, &rem);
47
    if (cid == CLOCK_SGI_CYCLE) {
M
mamingshuai 已提交
48
        ASSERT_EQ(rt, EINVAL) << cname << " should not support.\n";
49
    } 
M
mamingshuai 已提交
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 88 89
}
INSTANTIATE_TEST_CASE_P(SleepTest, AllClockIDTest, ALL_CLOCK_IDS);

/**
 * @tc.number SUB_KERNEL_TIME_API_CLOCK_NANOSLEEP_0300
 * @tc.name   clock_nanosleep fail test - invalid parameter
 * @tc.desc   [C- SOFTWARE -0200]
 */
HWTEST_F(SleepTest, testClockNanosleepInvalidPara, Reliability | SmallTest | Level2)
{
    struct timespec req = {0, 100};
    struct timespec rem = {0};
    int rt;

    // invlid clock_id
    int id = GetRandom(1000) + 12;
    LOG("check invlid clockid: %d...", id);
    rt = clock_nanosleep(id, 0, &req, &rem);
    EXPECT_EQ(rt, EINVAL);

    id = -GetRandom(1000) - 12;
    LOG("check invlid clockid: %d...", id);
    rt = clock_nanosleep(id, 0, &req, &rem);
    EXPECT_EQ(rt, EINVAL);

    // invlid flag
    int flag = TIMER_ABSTIME;
    LOG("check invlid flag: %d...", flag);
    rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem);
    EXPECT_EQ(rt, ENOTSUP);
    flag = GetRandom(100) + 1;
    LOG("check invlid flag: %d...", flag);
    rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem);
    EXPECT_EQ(rt, EINVAL);
    flag = -GetRandom(100) - 1;
    LOG("check invlid flag: %d...", flag);
    rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem);
    EXPECT_EQ(rt, EINVAL);

    // invlid timespec
90
    req.tv_sec = -1;
M
mamingshuai 已提交
91 92 93 94
    req.tv_nsec = 1;
    LOG("check invlid timespec: tv_sec=-1 ...");
    rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
    EXPECT_EQ(rt, EINVAL);
95
    req.tv_sec = 1;
M
mamingshuai 已提交
96 97 98 99
    req.tv_nsec = -1;
    LOG("check invlid timespec: tv_nsec=-1 ...");
    rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
    EXPECT_EQ(rt, EINVAL);
100 101
    req.tv_sec = 1;
    req.tv_nsec = 1000 * 1000 * 1000 + 1;
M
mamingshuai 已提交
102 103 104 105 106 107 108
    LOG("check invlid timespec: tv_nsec overflow ...");
    rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
    EXPECT_EQ(rt, EINVAL);

    // invlid remain
    // para not used, so not tested.
}