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