1/*
2 * Copyright (c) 2021 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 "AlarmTest.h"
17
18#include <signal.h>
19#include <time.h>
20#include <unistd.h>
21#include <sys/time.h>
22#include <sys/types.h>
23#include <gtest/gtest.h>
24#include "log.h"
25#include "utils.h"
26
27using namespace testing::ext;
28
29// static membor must init before use.
30int AlarmTest::mReceivedSignal = 0;
31
32// general signal handler
33void AlarmTest::SignalHandler(int signum)
34{
35    LOG("handler recv a signal: %d", signum);
36    mReceivedSignal = signum;
37}
38
39/**
40 * @tc.number  SUB_KERNEL_TIME_API_ALARM_0100
41 * @tc.name    alarm function test, cancel alarm. basic alarm function is tested in {signal}
42 * @tc.desc    [C- SOFTWARE -0200]
43 */
44HWTEST_F(AlarmTest, testAlarmCancel, Function | MediumTest | Level2)
45{
46    LOG("init alarm");
47    int rt = alarm(1);
48    EXPECT_EQ(rt, 0);
49    Msleep(500);
50
51    rt = alarm(0); // cancel alarm
52    EXPECT_EQ(rt, 1);
53    KeepRun(600);
54    EXPECT_EQ(mReceivedSignal, 0);
55}
56
57/**
58 * @tc.number  SUB_KERNEL_TIME_API_ALARM_0200
59 * @tc.name    alarm function test, multi alarm call test
60 * @tc.desc    [C- SOFTWARE -0200]
61 */
62HWTEST_F(AlarmTest, testAlarmMultiCall, Function | MediumTest | Level2)
63{
64    LOG("init alarm");
65    int rt = alarm(3);
66    EXPECT_EQ(rt, 0);
67
68    sleep(1);
69    LOG("set a new alarm");
70    rt = alarm(4);
71    EXPECT_EQ(rt, 2);
72    EXPECT_EQ(mReceivedSignal, 0);
73
74    LOG("sleep 2.5s...");
75    Msleep(2500);
76    EXPECT_EQ(mReceivedSignal, 0);
77
78    LOG("sleep 2s...");
79    Msleep(2000);
80    EXPECT_EQ(mReceivedSignal, SIGALRM);
81}
82
83/**
84 * @tc.number  SUB_KERNEL_TIME_API_ALARM_0300
85 * @tc.name    test thar alarm should not reserved to sub process via fork
86 * @tc.desc    [C- SOFTWARE -0200]
87 */
88HWTEST_F(AlarmTest, testAlarmFork, Function | MediumTest | Level2)
89{
90    int rt = alarm(1);
91    EXPECT_EQ(rt, 0);
92
93    pid_t pid = fork();
94    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
95    if (pid == 0) { // child
96        Msleep(MSLEEP_TIME);
97        if (mReceivedSignal != 0) {
98            if (mReceivedSignal == SIGALRM) {
99                LOG("child received SIGALRM!");
100            } else {
101                LOG("child received an unexpected signal: %d", mReceivedSignal);
102            }
103            exit(1);
104        } else {
105            exit(0);
106        }
107    } else { // parent
108        Msleep(MSLEEP_TIME);
109        if (mReceivedSignal != SIGALRM) {
110            Msleep(500);
111        }
112        EXPECT_EQ(mReceivedSignal, SIGALRM) << " expect no equal" << errno;
113        WaitProcExitedOK(pid);
114    }
115}
116
117/**
118 * @tc.number  SUB_KERNEL_TIME_API_UALARM_0100
119 * @tc.name    ualarm function create oneshot mode timer
120 * @tc.desc    [C- SOFTWARE -0200]
121 */
122HWTEST_F(AlarmTest, testUalarmOneshot, Function | MediumTest | Level3)
123{
124    useconds_t rt = ualarm(50000, 0);
125    EXPECT_EQ(rt, 0U) << "ERROR: ualarm return error!";
126    Msleep(61);
127    LOG("mReceivedSignal = %d, SIGALRM = %d", mReceivedSignal, SIGALRM);
128    EXPECT_EQ(mReceivedSignal, SIGALRM) << "ERROR: mReceivedSignal != SIGALRM";
129}
130
131/**
132 * @tc.number  SUB_KERNEL_TIME_API_UALARM_0200
133 * @tc.name    ualarm function create repeate mode timer
134 * @tc.desc    [C- SOFTWARE -0200]
135 */
136HWTEST_F(AlarmTest, testUalarmRepeate, Function | MediumTest | Level3)
137{
138    int count = 0;
139    useconds_t rt = ualarm(50000, 50000);
140    EXPECT_EQ(rt, 0U) << "ERROR: ualarm return error!";
141    while (true) {
142        if (mReceivedSignal == SIGALRM) {
143            count++;
144            if (count > 3) {
145                break;
146            }
147            mReceivedSignal = 0;
148        }
149        Msleep(10);
150    }
151
152    LOG("mReceivedSignal = %d, SIGALRM = %d", mReceivedSignal, SIGALRM);
153    EXPECT_EQ(mReceivedSignal, SIGALRM) << "ERROR: mReceivedSignal != SIGALRM";
154}
155
156/**
157 * @tc.number  SUB_KERNEL_TIME_API_UALARM_0300
158 * @tc.name    ualarm function stop alarm test
159 * @tc.desc    [C- SOFTWARE -0200]
160 */
161HWTEST_F(AlarmTest, testUalarmStop, Function | MediumTest | Level3)
162{
163    int ret;
164
165    mReceivedSignal = 0;
166    ualarm(50000, 0);
167    ret = ualarm(0, 0);
168    LOG("ret = %d", ret);
169    EXPECT_GE(ret, 50000) << "ERROR: ret < 50000";
170    ret = ualarm(0, 0);
171    LOG("ret = %d", ret);
172    EXPECT_EQ(ret, 0) << "ERROR: ret != 0";
173    Msleep(100);
174    EXPECT_EQ(mReceivedSignal, 0) << "ERROR: mReceivedSignal != 0";
175
176    ualarm(50000, 0);
177    Msleep(20);
178    ret = ualarm(0, 0);
179    LOG("ret = %d", ret);
180    EXPECT_LE(ret, 30000) << "ERROR: ret < 30000";
181    Msleep(40);
182    EXPECT_EQ(mReceivedSignal, 0) << "ERROR: mReceivedSignal != 0";
183
184    ret = ualarm(0, 0);
185    LOG("ret = %d", ret);
186    EXPECT_EQ(ret, 0) << "ERROR: ret != 0";
187}
188
189/**
190 * @tc.number  SUB_KERNEL_TIME_API_UALARM_0400
191 * @tc.name    ualarm function errno for ENOMEM test
192 * @tc.desc    [C- SOFTWARE -0200]
193 */
194HWTEST_F(AlarmTest, testUalarmEINVAL, Function | MediumTest | Level3)
195{
196    unsigned long OverMaxNum = 1000000;
197
198    errno = 0;
199    ualarm(OverMaxNum, 0);
200    EXPECT_EQ(errno, EINVAL) << "ERROR: errno != EINVAL";
201
202    errno = 0;
203    ualarm(OverMaxNum, OverMaxNum);
204    EXPECT_EQ(errno, EINVAL) << "ERROR: errno != EINVAL";
205
206    errno = 0;
207    ualarm(0, OverMaxNum);
208    EXPECT_EQ(errno, EINVAL) << "ERROR: errno != EINVAL";
209}
210
211/**
212 * @tc.number  SUB_KERNEL_TIME_API_TIMER_CREATE_0100
213 * @tc.name    timer_create function create a timer for give signal test
214 * @tc.desc    [C- SOFTWARE -0200]
215 */
216HWTEST_F(AlarmTest, testTimerCreateEventSignal, Function | MediumTest | Level3)
217{
218    timer_t tid = nullptr;
219    struct sigevent ev = {0};
220    struct itimerspec its = {0};
221
222    ASSERT_NE(signal(SIGUSR1, SignalHandler), SIG_ERR) << "ERROR: signal()";
223    ev.sigev_signo = SIGUSR1;
224    ev.sigev_notify = SIGEV_SIGNAL;
225    EXPECT_EQ(timer_create(CLOCK_REALTIME, &ev, &tid), 0) << "ERROR: timer_create() != 0";
226    LOG("tid = %p", tid);
227
228    its.it_value.tv_sec     = 0;
229    its.it_value.tv_nsec    = 50000000;  // 50 millisecond
230    its.it_interval.tv_sec  = 0;
231    its.it_interval.tv_nsec = 0;
232    EXPECT_EQ(timer_settime(tid, 0, &its, NULL), 0) << "ERROR: timer_settime() != 0";
233
234    uint32_t setMillisec = its.it_value.tv_sec * 1000 + its.it_value.tv_nsec / 1000000;
235    Msleep(setMillisec + ACCURACY_ERROR);
236    EXPECT_EQ(mReceivedSignal, ev.sigev_signo) << "mReceivedSignal != ev.sigev_signo";
237    EXPECT_EQ(timer_delete(tid), 0) << "ERROR: timer_delete() != 0";
238
239    // restore
240    signal(SIGUSR1, SIG_DFL);
241}
242
243/**
244 * @tc.number  SUB_KERNEL_TIME_API_TIMER_CREATE_0200
245 * @tc.name    timer_create function test
246 * @tc.desc    [C- SOFTWARE -0200]
247 */
248HWTEST_F(AlarmTest, testTimerCreateEventDefault, Function | MediumTest | Level3)
249{
250    timer_t tid = nullptr;
251    uint32_t setMillisec = 0;
252    struct itimerspec its = {0};
253
254    EXPECT_EQ(timer_create(CLOCK_REALTIME, NULL, &tid), 0) << "ERROR: timer_create() != 0";
255    LOG("tid = %p", tid);
256
257    its.it_value.tv_sec     = 0;
258    its.it_value.tv_nsec    = 50000000;  // 50 millisecond
259    its.it_interval.tv_sec  = 0;
260    its.it_interval.tv_nsec = 0;
261    EXPECT_EQ(timer_settime(tid, 0, &its, NULL), 0) << "ERROR: timer_settime() != 0";
262
263    setMillisec = its.it_value.tv_sec * 1000 + its.it_value.tv_nsec / 1000000;
264    LOG("setMillisec = %u", setMillisec);
265    Msleep(setMillisec + ACCURACY_ERROR);
266    EXPECT_EQ(mReceivedSignal, SIGALRM) << "mReceivedSignal != SIGALRM";
267
268    mReceivedSignal = 0;
269    /* 1 second */
270    its.it_value.tv_sec = 1;
271    its.it_value.tv_nsec = 0;
272    its.it_interval.tv_sec = 0;
273    its.it_interval.tv_nsec = 0;
274    EXPECT_EQ(timer_settime(tid, 0, &its, NULL), 0) << "ERROR: timer_settime() != 0";
275
276    setMillisec = its.it_value.tv_sec * 1000 + its.it_value.tv_nsec / 1000000;
277    LOG("setMillisec = %u", setMillisec);
278    Msleep(setMillisec + ACCURACY_ERROR);
279
280    EXPECT_EQ(mReceivedSignal, SIGALRM) << "mReceivedSignal != SIGALRM";
281    EXPECT_EQ(timer_delete(tid), 0) << "ERROR: timer_delete() != 0";
282}
283
284/**
285 * @tc.number  SUB_KERNEL_TIME_API_TIMER_CREATE_0300
286 * @tc.name    timer_create function create a timer for unique identify test
287 * @tc.desc    [C- SOFTWARE -0200]
288 */
289HWTEST_F(AlarmTest, testTimerCreateUniqueId, Function | MediumTest | Level2)
290{
291    int i, k, ret;
292    const int max = 32;
293    timer_t tid = NULL;
294    timer_t tidArr[max];
295
296    for (i = 0; i < max; i++) {
297        tidArr[i] = (timer_t)-1;
298    }
299
300    for (k = 0; k < max; k++) {
301        ret = timer_create(CLOCK_REALTIME, nullptr, &tid);
302        EXPECT_EQ(ret, 0) << "ERROR: timer_create() != 0";
303
304        for (i = 0; i < max; i++) {
305            if (tid == tidArr[i]) {
306                break;
307            }
308        }
309        EXPECT_EQ(i, max) << "ERROR: i < max that timer id already exist";
310
311        for (i = 0; i < max; i++) {
312            if (tidArr[i] == ((timer_t)-1)) {
313                break;
314            }
315        }
316        EXPECT_LT(i, max) << "ERROR: i == max that timer id is full";
317
318        if (i < max) {
319            tidArr[i] = tid;
320        }
321    }
322
323    for (k = 0; k < max; k++) {
324        if (tidArr[k] != (timer_t)-1) {
325            ret = timer_delete(tidArr[k]);
326            EXPECT_EQ(ret, 0) << "ERROR: timer_delete() != 0";
327        }
328    }
329}
330
331/**
332 * @tc.number  SUB_KERNEL_TIME_API_TIMER_CREATE_0400
333 * @tc.name    timer_create function that timer shall not be inherited by a child process across a fork
334 * @tc.desc    [C- SOFTWARE -0200]
335 */
336HWTEST_F(AlarmTest, testTimerCreateFork, Function | MediumTest | Level2)
337{
338    timer_t tid = nullptr;
339    struct itimerspec its = {0};
340
341    ASSERT_EQ(timer_create(CLOCK_REALTIME, NULL, &tid), 0) << "> timer_create fail, errno = " << errno;
342    its.it_value.tv_sec = 0;
343    its.it_value.tv_nsec = 50000000;
344    its.it_interval.tv_sec = 0;
345    its.it_interval.tv_nsec = 0;
346    EXPECT_EQ(timer_settime(tid, 0, &its, NULL), 0) << "ERROR: timer_settime() != 0";
347
348    uint32_t setMillisec = its.it_value.tv_sec * 1000 + its.it_value.tv_nsec / 1000000;
349    LOG("setMillisec = %u", setMillisec);
350
351    pid_t pid = fork();
352    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
353    if (pid == 0) { // child
354        Msleep(1100);
355        if (mReceivedSignal == 0) {
356            LOG("child process did not inherit timer!");
357            exit(0);
358        } else {
359            LOG("child received an unexpected signal: %d", mReceivedSignal);
360            exit(1);
361        }
362    } else { // parent
363        Msleep(setMillisec + ACCURACY_ERROR);
364        EXPECT_EQ(mReceivedSignal, SIGALRM);
365        WaitProcExitedOK(pid);
366        EXPECT_EQ(timer_delete(tid), 0) << "ERROR: timer_delete() != 0";
367    }
368}
369
370/**
371 * @tc.number  SUB_KERNEL_TIME_API_TIMER_CREATE_0500
372 * @tc.name    timer_create function errno for EINVAL test
373 * @tc.desc    [C- SOFTWARE -0200]
374 */
375HWTEST_F(AlarmTest, testTimerCreateEINVAL, Function | MediumTest | Level4)
376{
377    timer_t tid = nullptr;
378    clockid_t clockid = GetRandom(2048);
379
380    EXPECT_EQ(timer_create(clockid, NULL, &tid), -1) << "ERROR: timer_create() != -1";
381    EXPECT_EQ(errno, EINVAL) << "ERROR: errno != EINVAL, errno = " << errno << " EINVAL = " << EINVAL;
382}
383
384/**
385 * @tc.number  SUB_KERNEL_TIME_API_TIMER_GETTIME_0100
386 * @tc.name    timer_gettime function create a timer and get time test
387 * @tc.desc    [C- SOFTWARE -0200]
388 */
389HWTEST_F(AlarmTest, testTimerGetTime, Function | MediumTest | Level3)
390{
391    timer_t tid = nullptr;
392    struct sigevent ev = {0};
393    struct itimerspec its = {0};
394    struct itimerspec getIts = {0};
395    int index = 0;
396    uint32_t getMsValue[20];
397    const uint32_t delay = 200;
398
399    ASSERT_NE(signal(SIGINT, SignalHandler), SIG_ERR) << "ERROR: signal()";
400    ev.sigev_signo = SIGINT;
401    ev.sigev_notify = SIGEV_SIGNAL;
402    EXPECT_EQ(timer_create(CLOCK_REALTIME, &ev, &tid), 0) << "ERROR: timer_create() != 0";
403    LOG("tid = %p", tid);
404
405    its.it_value.tv_sec = 2;
406    its.it_value.tv_nsec = 0;
407    its.it_interval.tv_sec = 0;
408    its.it_interval.tv_nsec = 0;
409    EXPECT_EQ(timer_settime(tid, 0, &its, NULL), 0) << "ERROR: timer_settime() != 0";
410    EXPECT_EQ(timer_gettime(tid, &getIts), 0) << "ERROR: timer_gettime() != 0";
411
412    uint32_t setMillisec = its.it_value.tv_sec * 1000 + its.it_value.tv_nsec / 1000000;
413    uint32_t getMillisec = getIts.it_value.tv_sec * 1000 + getIts.it_value.tv_nsec / 1000000;
414    LOG("setMillisec = %u, getMillisecv = %u", setMillisec, getMillisec);
415    EXPECT_LE(getMillisec, setMillisec);
416    LOG("%u, %u, %u", setMillisec, getMillisec, getMillisec - setMillisec);
417
418    while (true) {
419        Msleep(delay);
420        EXPECT_EQ(timer_gettime(tid, &getIts), 0) << "ERROR: timer_gettime() != 0";
421        getMillisec = getIts.it_value.tv_sec * 1000 + getIts.it_value.tv_nsec / 1000000;
422        if (getMillisec == 0) {
423            break;
424        }
425        getMsValue[index++] = getMillisec;
426    }
427    Msleep(150);
428
429    for (int i = 0; i < index; i++) {
430        /* delay should add 10 millisecond to ajust */
431        if (setMillisec < (delay + 10)) {
432            break;
433        }
434        setMillisec -= delay + 10;
435        EXPECT_GE(getMsValue[i], setMillisec);
436        LOG("%u, %u, %u", setMillisec, getMsValue[i], getMsValue[i] - setMillisec);
437    }
438    EXPECT_EQ(mReceivedSignal, ev.sigev_signo) << "mReceivedSignal != ev.sigev_signo";
439    EXPECT_EQ(timer_delete(tid), 0) << "ERROR: timer_delete() != 0";
440
441    // restore
442    signal(SIGINT, SIG_DFL);
443}
444
445/**
446 * @tc.number  SUB_KERNEL_TIME_API_TIMER_GETOVERRUN_0100
447 * @tc.name    timer_getoverrun function create a timer for SIGALRM signal and get overrun time test
448 * @tc.desc    [C- SOFTWARE -0200]
449 */
450HWTEST_F(AlarmTest, testTimerGetOverrun, Function | MediumTest | Level3)
451{
452    timer_t tid = nullptr;
453    sigset_t mask;
454    struct sigevent ev = {0};
455    struct itimerspec its = {0};
456
457    /* Block timer signal temporarily */
458    sigemptyset(&mask);
459    sigaddset(&mask, SIGALRM);
460    EXPECT_NE(sigprocmask(SIG_SETMASK, &mask, NULL), -1) << "ERROR: sigprocmask() == -1";
461
462    ev.sigev_signo = SIGALRM;
463    ev.sigev_notify = SIGEV_SIGNAL;
464    EXPECT_EQ(timer_create(CLOCK_REALTIME, &ev, &tid), 0) << "ERROR: timer_create() != 0";
465
466    its.it_value.tv_sec     = 0;
467    its.it_value.tv_nsec    = 50000000;  // 50 millisecond
468    its.it_interval.tv_sec  = its.it_value.tv_sec;
469    its.it_interval.tv_nsec = its.it_value.tv_nsec;
470    EXPECT_EQ(timer_settime(tid, 0, &its, NULL), 0) << "ERROR: timer_settime() != 0";
471
472    int setMillisec = its.it_value.tv_sec * 1000 + its.it_value.tv_nsec / 1000000;
473    LOG("setMillisec = %u", setMillisec);
474    Msleep(150);
475    EXPECT_NE(sigprocmask(SIG_UNBLOCK, &mask, NULL), -1) << "ERROR: sigprocmask() == -1";
476
477    int overrun = timer_getoverrun(tid);
478    LOG("timer_getoverrun(tid) = %d", overrun);
479
480    EXPECT_GE(overrun, 2) << "ERROR: timer_getoverrun(tid) < 2";
481    EXPECT_EQ(timer_delete(tid), 0) << "ERROR: timer_delete() != 0";
482}
483
484/**
485 * @tc.number  SUB_KERNEL_TIME_API_SETITIMER_0100
486 * @tc.name    setitimer function create oneshot mode timer
487 * @tc.desc    [C- SOFTWARE -0200]
488 */
489HWTEST_F(AlarmTest, testSetItTimerOneshot, Function | MediumTest | Level3)
490{
491    struct itimerval setItv = {0};
492    uint32_t setMillisec;
493
494    /* 50 millisecond */
495    setItv.it_value.tv_sec = 0;
496    setItv.it_value.tv_usec = 50000;
497    setItv.it_interval.tv_sec = 0;
498    setItv.it_interval.tv_usec = 0;
499    EXPECT_EQ(setitimer(ITIMER_REAL, &setItv, NULL), 0) << "ERROR: setitimer() != 0";
500
501    setMillisec = setItv.it_value.tv_sec * 1000 + setItv.it_value.tv_usec / 1000;
502    Msleep(setMillisec + ACCURACY_ERROR);
503    LOG("mReceivedSignal = %d, SIGALRM = %d", mReceivedSignal, SIGALRM);
504    EXPECT_EQ(mReceivedSignal, SIGALRM) << "ERROR: mReceivedSignal != SIGALRM";
505
506    mReceivedSignal = 0;
507    /* 1 second */
508    setItv.it_value.tv_sec = 1;
509    setItv.it_value.tv_usec = 0;
510    setItv.it_interval.tv_sec = 0;
511    setItv.it_interval.tv_usec = 0;
512    EXPECT_EQ(setitimer(ITIMER_REAL, &setItv, NULL), 0) << "ERROR: setitimer() != 0";
513    setMillisec = setItv.it_value.tv_sec * 1000 + setItv.it_value.tv_usec / 1000;
514    Msleep(setMillisec + ACCURACY_ERROR);
515    LOG("mReceivedSignal = %d, SIGALRM = %d", mReceivedSignal, SIGALRM);
516    EXPECT_EQ(mReceivedSignal, SIGALRM) << "ERROR: mReceivedSignal != SIGALRM";
517}
518
519/**
520 * @tc.number  SUB_KERNEL_TIME_API_SETITIMER_0200
521 * @tc.name    setitimer function create repeate mode timer
522 * @tc.desc    [C- SOFTWARE -0200]
523 */
524HWTEST_F(AlarmTest, testSetItTimerRepeate, Function | MediumTest | Level3)
525{
526    int count = 0;
527    struct itimerval setItv = {0};
528
529    /* 50 millisecond */
530    setItv.it_value.tv_sec = 0;
531    setItv.it_value.tv_usec = 50000;
532    setItv.it_interval.tv_sec = setItv.it_value.tv_sec;
533    setItv.it_interval.tv_usec = setItv.it_value.tv_usec;
534    EXPECT_EQ(setitimer(ITIMER_REAL, &setItv, NULL), 0) << "ERROR: setitimer() != 0";
535
536    uint32_t setMillisec = setItv.it_value.tv_sec * 1000 + setItv.it_value.tv_usec / 1000;
537    while (true) {
538        count++;
539        Msleep(setMillisec + ACCURACY_ERROR);
540        if (mReceivedSignal == SIGALRM) {
541            count++;
542            if (count > 3) {
543                break;
544            }
545            mReceivedSignal = 0;
546        }
547    }
548
549    LOG("mReceivedSignal = %d, SIGALRM = %d", mReceivedSignal, SIGALRM);
550    EXPECT_EQ(mReceivedSignal, SIGALRM) << "ERROR: mReceivedSignal != SIGALRM";
551}
552
553/**
554 * @tc.number  SUB_KERNEL_TIME_API_SETITIMER_0300
555 * @tc.name    setitimer function that cancel timer
556 * @tc.desc    [C- SOFTWARE -0200]
557 */
558HWTEST_F(AlarmTest, testCancelTimer, Function | MediumTest | Level3)
559{
560    struct itimerval setItv = {0};
561
562    LOG("start a timer");
563    setItv.it_value.tv_sec = 0;
564    setItv.it_value.tv_usec = 1000;
565    setItv.it_interval.tv_sec = 0;
566    setItv.it_interval.tv_usec = 1000;
567    EXPECT_EQ(setitimer(ITIMER_REAL, &setItv, NULL), 0) << "setitimer fail, errno = " << errno;
568    KeepRun(50);
569    EXPECT_EQ(mReceivedSignal, SIGALRM);
570
571    LOG("cancel timer");
572    setItv.it_value.tv_usec = 0;
573    EXPECT_EQ(setitimer(ITIMER_REAL, &setItv, NULL), 0) << "setitimer fail, errno = " << errno;
574    mReceivedSignal = 0;
575    KeepRun(100);
576    EXPECT_EQ(mReceivedSignal, 0);
577}
578
579/**
580 * @tc.number  SUB_KERNEL_TIME_API_SETITIMER_0400
581 * @tc.name    setitimer function test which ovalue is not null
582 * @tc.desc    [C- SOFTWARE -0200]
583 */
584HWTEST_F(AlarmTest, testSetItTimerOldvalue, Function | MediumTest | Level3)
585{
586    struct itimerval setItv = {0};
587    struct itimerval oldItv = {0};
588    uint32_t setMillisec;
589
590    setItv.it_value.tv_sec = 1;
591    setItv.it_value.tv_usec = 0;
592    setItv.it_interval.tv_sec = 0;
593    setItv.it_interval.tv_usec = 100000;
594    EXPECT_EQ(setitimer(ITIMER_REAL, &setItv, NULL), 0) << "ERROR: setitimer() != 0";
595    setMillisec = setItv.it_value.tv_sec * 1000 + setItv.it_value.tv_usec / 1000;
596    usleep((setMillisec + ACCURACY_ERROR) * 1000);
597    LOG("mReceivedSignal = %d, SIGALRM = %d", mReceivedSignal, SIGALRM);
598    EXPECT_EQ(mReceivedSignal, SIGALRM) << "ERROR: mReceivedSignal != SIGALRM";
599
600    EXPECT_EQ(setitimer(ITIMER_REAL, &setItv, &oldItv), 0) << "setitimer fail, errno = " << errno;
601    uint32_t oldMillisec = oldItv.it_value.tv_sec * 1000 + oldItv.it_value.tv_usec / 1000;
602    EXPECT_GE(setMillisec, oldMillisec);
603    EXPECT_EQ(oldItv.it_interval.tv_sec, 0);
604    EXPECT_EQ(oldItv.it_interval.tv_usec, 100000);
605}
606
607/**
608 * @tc.number  SUB_KERNEL_TIME_API_GETITIMER_0100
609 * @tc.name    getitimer function create a timer and get time test
610 * @tc.desc    [C- SOFTWARE -0200]
611 */
612HWTEST_F(AlarmTest, testGetItTimer, Function | MediumTest | Level3)
613{
614    struct itimerval setItv = {0};
615    struct itimerval getItv = {0};
616    int index = 0;
617    uint32_t getMsValue[20];
618    const uint32_t delay = 200;
619
620    /* 50 millisecond */
621    setItv.it_value.tv_sec = 2;
622    setItv.it_value.tv_usec = 0;
623    setItv.it_interval.tv_sec = 0;
624    setItv.it_interval.tv_usec = 0;
625    EXPECT_EQ(setitimer(ITIMER_REAL, &setItv, NULL), 0) << "ERROR: setitimer() != 0";
626    EXPECT_EQ(getitimer(ITIMER_REAL, &getItv), 0) << "ERROR: getitimer() != 0";
627
628    uint32_t setMillisec = setItv.it_value.tv_sec * 1000 + setItv.it_value.tv_usec / 1000;
629    uint32_t getMillisec = getItv.it_value.tv_sec * 1000 + getItv.it_value.tv_usec / 1000;
630    EXPECT_LE(getMillisec, setMillisec);
631    LOG("%u, %u, %u", setMillisec, getMillisec, getMillisec - setMillisec);
632
633    while (true) {
634        Msleep(delay);
635        EXPECT_EQ(getitimer(ITIMER_REAL, &getItv), 0) << "ERROR: getitimer() != 0";
636        getMillisec = getItv.it_value.tv_sec * 1000 + getItv.it_value.tv_usec / 1000;
637        if (getMillisec == 0) {
638            break;
639        }
640        getMsValue[index++] = getMillisec;
641    }
642    Msleep(150);
643
644    for (int i = 0; i < index; i++) {
645        /* delay should add 10 millisecond to ajust */
646        if (setMillisec < (delay + 10)) {
647            break;
648        }
649        setMillisec -= delay + 10;
650        EXPECT_GE(getMsValue[i], setMillisec);
651        LOG("%u, %u, %u", setMillisec, getMsValue[i], getMsValue[i] - setMillisec);
652    }
653    LOG("mReceivedSignal = %d, SIGALRM = %d", mReceivedSignal, SIGALRM);
654    EXPECT_EQ(mReceivedSignal, SIGALRM) << "ERROR: mReceivedSignal != SIGALRM";
655}
656