• 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 <errno.h>
17 #include <signal.h>
18 #include <stdlib.h>
19 #include <time.h>
20 #include <unistd.h>
21 
22 #include "test.h"
23 #define SIGNUM 40
24 static int count = 0;
25 
26 extern int __timer_settime64(timer_t, int, const struct itimerspec *__restrict, struct itimerspec *__restrict);
27 
handler(int sig)28 void handler(int sig)
29 {
30     count++;
31 
32     return;
33 }
34 
exception_handler(int sig)35 void exception_handler(int sig)
36 {
37     exit(t_status);
38 }
39 
40 /**
41  * @tc.name      : timer_settime_0100
42  * @tc.desc      : arms the timer
43  * @tc.level     : Level 0
44  */
timer_settime_0100(void)45 void timer_settime_0100(void)
46 {
47     struct sigevent sev;
48     timer_t timerid;
49 
50     sev.sigev_notify = SIGEV_SIGNAL;
51     sev.sigev_signo = SIGNUM;
52     sev.sigev_value.sival_ptr = &timerid;
53 
54     signal(SIGNUM, handler);
55 
56     int result = timer_create(CLOCK_REALTIME, &sev, &timerid);
57     if (result != 0) {
58         t_error("%s failed: result = %d\n", __func__, result);
59     }
60 
61     struct itimerspec its;
62     its.it_value.tv_sec = 1;
63     its.it_value.tv_nsec = 0;
64     its.it_interval.tv_sec = 0;
65     its.it_interval.tv_nsec = 0;
66 
67     result = timer_settime(timerid, 0, &its, NULL);
68     if (result != 0) {
69         t_error("%s failed: result = %d\n", __func__, result);
70     }
71 
72     while (count <= 0) {
73         sleep(1);
74     }
75 
76     result = timer_delete(timerid);
77     if (result != 0) {
78         t_error("%s failed: result = %d\n", __func__, result);
79     }
80 }
81 
82 /**
83  * @tc.name      : timer_settime_0200
84  * @tc.desc      : arms the timer with invalid parameters
85  * @tc.level     : Level 2
86  */
timer_settime_0200(void)87 void timer_settime_0200(void)
88 {
89     signal(SIGSEGV, exception_handler);
90 
91     timer_settime(NULL, 0, NULL, NULL);
92 }
93 
94 /**
95  * @tc.name      : timer_settime64_0100
96  * @tc.desc      : arms the timer
97  * @tc.level     : Level 0
98  */
timer_settime64_0100(void)99 void timer_settime64_0100(void)
100 {
101     struct sigevent sev;
102     timer_t timerid;
103 
104     sev.sigev_notify = SIGEV_SIGNAL;
105     sev.sigev_signo = SIGNUM;
106     sev.sigev_value.sival_ptr = &timerid;
107 
108     signal(SIGNUM, handler);
109 
110     int result = timer_create(CLOCK_REALTIME, &sev, &timerid);
111     if (result != 0) {
112         t_error("%s failed: result = %d\n", __func__, result);
113     }
114 
115     struct itimerspec its;
116     its.it_value.tv_sec = 1;
117     its.it_value.tv_nsec = 0;
118     its.it_interval.tv_sec = 0;
119     its.it_interval.tv_nsec = 0;
120 
121     result = __timer_settime64(timerid, 0, &its, NULL);
122     if (result != 0) {
123         t_error("%s failed: result = %d\n", __func__, result);
124     }
125 
126     while (count <= 0) {
127         sleep(1);
128     }
129 
130     result = timer_delete(timerid);
131     if (result != 0) {
132         t_error("%s failed: result = %d\n", __func__, result);
133     }
134 }
135 
main(int argc,char * argv[])136 int main(int argc, char *argv[])
137 {
138     timer_settime_0100();
139     timer_settime_0200();
140     timer_settime64_0100();
141 
142     return t_status;
143 }
144