1/* 2 * Copyright (C) 2024 HiHope Open Source Organization. 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 <csignal> 17#include <ctime> 18#include <iostream> 19#include <unistd.h> 20#include <gtest/gtest.h> 21#include "securec.h" 22 23using namespace testing::ext; 24 25static bool g_timerExpired = false; 26static const int DELAY_TIME = 100; 27 28void TimerHandler(int sig) 29{ 30 g_timerExpired = true; 31} 32 33class SetiTimerApiTest : public testing::Test { 34public: 35static void SetUpTestCase(); 36static void TearDownTestCase(); 37void SetUp(); 38void TearDown(); 39private: 40}; 41void SetiTimerApiTest::SetUp() 42{ 43 g_timerExpired = false; 44 sighandler_t oldHandler = signal(SIGALRM, TimerHandler); 45 EXPECT_NE(oldHandler, SIG_ERR); 46} 47void SetiTimerApiTest::TearDown() 48{ 49 sighandler_t oldHandler = signal(SIGALRM, SIG_DFL); 50 EXPECT_NE(oldHandler, SIG_ERR); 51} 52void SetiTimerApiTest::SetUpTestCase() 53{ 54} 55void SetiTimerApiTest::TearDownTestCase() 56{ 57} 58 59/* 60 * @tc.number : SUB_KERNEL_SYSCALL_SETITIMER_0100 61 * @tc.name : SetiTimerAndGetitimerGetRealTimeSuccess_0001 62 * @tc.desc : Test setitimer and getitimer the ITIMER_REAL timer. 63 * @tc.size : MediumTest 64 * @tc.type : Function 65 * @tc.level : Level 1 66 */ 67HWTEST_F(SetiTimerApiTest, SetiTimerAndGetitimerGetRealTimeSuccess_0001, Function | MediumTest | Level1) 68{ 69 struct itimerval timer_val = { 70 .it_interval = { 71 .tv_sec = 0, 72 .tv_usec = 0, 73 }, 74 .it_value = { 75 .tv_sec = 0, 76 .tv_usec = 50, 77 }, 78 }; 79 struct itimerval remaining; 80 struct itimerval fetched; 81 82 EXPECT_EQ(setitimer(ITIMER_REAL, &timer_val, &remaining), 0); 83 84 EXPECT_EQ(getitimer(ITIMER_REAL, &fetched), 0); 85 86 EXPECT_NEAR(fetched.it_value.tv_sec, timer_val.it_value.tv_sec, 1); 87 EXPECT_NEAR(fetched.it_value.tv_usec, timer_val.it_value.tv_usec, 1000); 88 EXPECT_EQ(fetched.it_interval.tv_sec, timer_val.it_interval.tv_sec); 89 EXPECT_EQ(fetched.it_interval.tv_usec, timer_val.it_interval.tv_usec); 90 91 usleep(DELAY_TIME); 92 EXPECT_TRUE(g_timerExpired); 93 EXPECT_EQ(signal(SIGALRM, SIG_DFL), TimerHandler); 94} 95