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 <time.h>
19 #include <unistd.h>
20
21 #include "test.h"
22 #define SIGNUM 40
23 static int count = 0;
24
handler(int sig)25 void handler(int sig)
26 {
27 count++;
28
29 return;
30 }
31
32 /**
33 * @tc.name : timer_create_0100
34 * @tc.desc : create a per-process timer
35 * @tc.level : Level 0
36 */
timer_create_0100(void)37 void timer_create_0100(void)
38 {
39 struct sigevent sev;
40 timer_t timerid;
41
42 sev.sigev_notify = SIGEV_SIGNAL;
43 sev.sigev_signo = SIGNUM;
44 sev.sigev_value.sival_ptr = &timerid;
45
46 signal(SIGNUM, handler);
47
48 int result = timer_create(CLOCK_REALTIME, &sev, &timerid);
49 if (result != 0) {
50 t_error("%s failed: result = %d\n", __func__, result);
51 }
52
53 struct itimerspec its;
54 its.it_value.tv_sec = 1;
55 its.it_value.tv_nsec = 0;
56 its.it_interval.tv_sec = its.it_value.tv_sec;
57 its.it_interval.tv_nsec = its.it_value.tv_nsec;
58
59 result = timer_settime(timerid, 0, &its, NULL);
60 if (result != 0) {
61 t_error("%s failed: result = %d\n", __func__, result);
62 }
63
64 while (count <= 0) {
65 sleep(1);
66 }
67
68 result = timer_delete(timerid);
69 if (result != 0) {
70 t_error("%s failed: result = %d\n", __func__, result);
71 }
72 }
73
74 /**
75 * @tc.name : timer_create_0200
76 * @tc.desc : create a per-process timer with invalid parameters
77 * @tc.level : Level 2
78 */
timer_create_0200(void)79 void timer_create_0200(void)
80 {
81 errno = 0;
82 int result = timer_create(-1, NULL, NULL);
83 if (result == 0) {
84 t_error("%s failed: result = %d\n", __func__, result);
85 }
86
87 if (errno != EINVAL) {
88 t_error("%s failed: errno = %d\n", __func__, errno);
89 }
90 }
91
main(int argc,char * argv[])92 int main(int argc, char *argv[])
93 {
94 timer_create_0100();
95 timer_create_0200();
96
97 return t_status;
98 }
99