| 1 | //===-- Unittests for setitimer -------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM |
| 4 | // Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "hdr/types/struct_itimerval.h" |
| 11 | #include "hdr/types/struct_sigaction.h" |
| 12 | #include "src/signal/sigaction.h" |
| 13 | #include "src/signal/sigemptyset.h" |
| 14 | #include "src/sys/time/setitimer.h" |
| 15 | #include "test/UnitTest/ErrnoCheckingTest.h" |
| 16 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
| 17 | #include "test/UnitTest/Test.h" |
| 18 | |
| 19 | using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; |
| 20 | using LlvmLibcSysTimeSetitimerTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
| 21 | |
| 22 | static bool timer_fired(false); |
| 23 | |
| 24 | extern "C" void handle_sigalrm(int) { timer_fired = true; } |
| 25 | |
| 26 | TEST_F(LlvmLibcSysTimeSetitimerTest, SmokeTest) { |
| 27 | libc_errno = 0; |
| 28 | struct sigaction sa; |
| 29 | sa.sa_handler = handle_sigalrm; |
| 30 | LIBC_NAMESPACE::sigemptyset(&sa.sa_mask); |
| 31 | sa.sa_flags = 0; |
| 32 | LIBC_NAMESPACE::sigaction(SIGALRM, &sa, nullptr); |
| 33 | |
| 34 | struct itimerval timer; |
| 35 | timer.it_value.tv_sec = 0; |
| 36 | timer.it_value.tv_usec = 200000; |
| 37 | timer.it_interval.tv_sec = 0; |
| 38 | timer.it_interval.tv_usec = 0; // One-shot timer |
| 39 | |
| 40 | ASSERT_THAT(LIBC_NAMESPACE::setitimer(0, &timer, nullptr), |
| 41 | returns(EQ(0)).with_errno(EQ(0))); |
| 42 | |
| 43 | while (true) { |
| 44 | if (timer_fired) |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | ASSERT_TRUE(timer_fired); |
| 49 | } |
| 50 | |
| 51 | TEST_F(LlvmLibcSysTimeSetitimerTest, InvalidRetTest) { |
| 52 | struct itimerval timer; |
| 53 | |
| 54 | // out of range timer type (which) |
| 55 | ASSERT_THAT(LIBC_NAMESPACE::setitimer(99, &timer, nullptr), |
| 56 | returns(NE(0)).with_errno(NE(0))); |
| 57 | } |
| 58 | |