1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include <unistd.h> 17#include <string.h> 18#include <sys/types.h> 19#include <fcntl.h> 20#include <gtest/gtest.h> 21#include "log.h" 22#include "utils.h" 23#include "ClockID.h" 24 25using namespace testing::ext; 26 27class UsleepParamTest : public testing::TestWithParam<int> { 28}; 29class SleepParamTest : public testing::TestWithParam<int> { 30}; 31class SleepTest : public testing::Test { 32}; 33 34/** 35 * @tc.number SUB_KERNEL_TIME_API_CLOCK_NANOSLEEP_0200 36 * @tc.name clock_nanosleep fail test - non-support clock_id 37 * @tc.desc [C- SOFTWARE -0200] 38 */ 39HWTEST_P(AllClockIDTest, testClockNanosleepInvalidID, Reliability | SmallTest | Level2) 40{ 41 clockid_t cid = GetParam(); 42 const char* cname = ALL_CLOCKS_NAME[cid]; 43 LOG("test %s", cname); 44 struct timespec req = {0, 100}; 45 struct timespec rem = {0}; 46 int rt = clock_nanosleep(cid, 0, &req, &rem); 47 if (cid == CLOCK_SGI_CYCLE) { 48 ASSERT_EQ(rt, EINVAL) << cname << " should not support.\n"; 49 } 50} 51INSTANTIATE_TEST_CASE_P(SleepTest, AllClockIDTest, ALL_CLOCK_IDS); 52 53/** 54 * @tc.number SUB_KERNEL_TIME_API_CLOCK_NANOSLEEP_0300 55 * @tc.name clock_nanosleep fail test - invalid parameter 56 * @tc.desc [C- SOFTWARE -0200] 57 */ 58HWTEST_F(SleepTest, testClockNanosleepInvalidPara, Reliability | SmallTest | Level2) 59{ 60 struct timespec req = {0, 100}; 61 struct timespec rem = {0}; 62 int rt; 63 64 // invlid clock_id 65 int id = GetRandom(1000) + 12; 66 LOG("check invlid clockid: %d...", id); 67 rt = clock_nanosleep(id, 0, &req, &rem); 68 EXPECT_EQ(rt, EINVAL); 69 70 id = -GetRandom(1000) - 12; 71 LOG("check invlid clockid: %d...", id); 72 rt = clock_nanosleep(id, 0, &req, &rem); 73 EXPECT_EQ(rt, EINVAL); 74 75 // invlid flag 76 int flag = TIMER_ABSTIME; 77 LOG("check invlid flag: %d...", flag); 78 rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem); 79 EXPECT_EQ(rt, ENOTSUP); 80 flag = GetRandom(100) + 1; 81 LOG("check invlid flag: %d...", flag); 82 rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem); 83 EXPECT_EQ(rt, EINVAL); 84 flag = -GetRandom(100) - 1; 85 LOG("check invlid flag: %d...", flag); 86 rt = clock_nanosleep(CLOCK_REALTIME, flag, &req, &rem); 87 EXPECT_EQ(rt, EINVAL); 88 89 // invlid timespec 90 req.tv_sec = -1; 91 req.tv_nsec = 1; 92 LOG("check invlid timespec: tv_sec=-1 ..."); 93 rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem); 94 EXPECT_EQ(rt, EINVAL); 95 req.tv_sec = 1; 96 req.tv_nsec = -1; 97 LOG("check invlid timespec: tv_nsec=-1 ..."); 98 rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem); 99 EXPECT_EQ(rt, EINVAL); 100 req.tv_sec = 1; 101 req.tv_nsec = 1000 * 1000 * 1000 + 1; 102 LOG("check invlid timespec: tv_nsec overflow ..."); 103 rt = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem); 104 EXPECT_EQ(rt, EINVAL); 105 106 // invlid remain 107 // para not used, so not tested. 108} 109