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