1/*
2 * Copyright (c) 2022 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 <signal.h>
17#include <stdlib.h>
18#include <sys/timerfd.h>
19#include <time.h>
20#include "test.h"
21
22#define NANOSECOND (1000000000)
23#define MICROSECONDS (1000000)
24#define DATA_TWO (2)
25
26extern int __timerfd_settime64(int, int, const struct itimerspec *, struct itimerspec *);
27
28void exception_handler(int sig)
29{
30    exit(t_status);
31}
32
33/**
34 * @tc.name      : timerfd_settime_0100
35 * @tc.desc      : Start the timer specified by fd
36 * @tc.level     : Level 0
37 */
38void timerfd_settime_0100(void)
39{
40    struct itimerspec its = {{0, 0}, {DATA_TWO, 0}};
41    struct itimerspec val;
42    int fd, result;
43
44    fd = timerfd_create(CLOCK_REALTIME, 0);
45    if (fd < 0) {
46        t_error("%s timerfd_create failed\n", __func__);
47        return;
48    }
49
50    result = timerfd_settime(fd, 0, &its, NULL);
51    if (result != 0) {
52        t_error("%s timerfd_settime failed\n", __func__);
53        return;
54    }
55
56    result = usleep(MICROSECONDS);
57    if (result != 0) {
58        t_error("%s usleep failed\n", __func__);
59        return;
60    }
61
62    result = timerfd_gettime(fd, &val);
63    if (result != 0) {
64        t_error("%s timerfd_gettime failed\n", __func__);
65        return;
66    }
67    if (val.it_value.tv_nsec > NANOSECOND) {
68        t_error("%s timerfd error\n");
69    }
70}
71
72/**
73 * @tc.name      : timerfd_settime_0200
74 * @tc.desc      : arms the timer with invalid parameters
75 * @tc.level     : Level 2
76 */
77void timerfd_settime_0200(void)
78{
79    signal(SIGSEGV, exception_handler);
80
81    timerfd_settime(-1, 0, NULL, NULL);
82}
83
84/**
85 * @tc.name      : timerfd_settime64_0100
86 * @tc.desc      : Start the timer specified by fd
87 * @tc.level     : Level 0
88 */
89void timerfd_settime64_0100(void)
90{
91    struct itimerspec its = {{0, 0}, {DATA_TWO, 0}};
92    struct itimerspec val;
93    int fd, result;
94
95    fd = timerfd_create(CLOCK_REALTIME, 0);
96    if (fd < 0) {
97        t_error("%s timerfd_create failed\n", __func__);
98        return;
99    }
100
101    result = __timerfd_settime64(fd, 0, &its, NULL);
102    if (result != 0) {
103        t_error("%s __timerfd_settime64 failed\n", __func__);
104        return;
105    }
106
107    result = usleep(MICROSECONDS);
108    if (result != 0) {
109        t_error("%s usleep failed\n", __func__);
110        return;
111    }
112
113    result = timerfd_gettime(fd, &val);
114    if (result != 0) {
115        t_error("%s timerfd_gettime failed\n", __func__);
116        return;
117    }
118    if (val.it_value.tv_nsec > NANOSECOND) {
119        t_error("%s timerfd error\n");
120    }
121}
122
123int main(int argc, char *argv[])
124{
125    timerfd_settime_0100();
126    timerfd_settime_0200();
127    timerfd_settime64_0100();
128    return t_status;
129}