1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include "it_test_vid.h"
32 #include "stdio.h"
33 #include "unistd.h"
34 #include "sys/stat.h"
35 #include "time.h"
36 #include "stdlib.h"
37
TimerFunc(int sig)38 static VOID TimerFunc(int sig)
39 {
40 return;
41 }
42
ChildFunc()43 static void ChildFunc()
44 {
45 timer_t tid;
46 struct sigevent ent;
47 INT32 *ret1 = NULL;
48 int ret;
49 struct itimerspec its;
50 struct itimerspec its2;
51 timer_t *tid2 = nullptr;
52 int i = 0;
53 signal(SIGUSR1, TimerFunc);
54 ent.sigev_notify = SIGEV_SIGNAL;
55 ent.sigev_signo = SIGUSR1;
56
57 tid2 = (timer_t *)malloc(sizeof(UINTPTR) * 1024);
58 ret1 = (int *)malloc(sizeof(int) * 1024);
59 (void)memset_s(tid2, sizeof(char *) * 1024, 0, sizeof(char *) * 1024);
60 (void)memset_s(ret1, sizeof(int) * 1024, 0xff, sizeof(int) * 1024);
61 while (i < 1024) {
62 *(ret1 + i) = timer_create(CLOCK_REALTIME, &ent, tid2 + i);
63 if (*(ret1 + i) == 0) {
64 ICUNIT_ASSERT_EQUAL_VOID(*(ret1 + i), 0, *(ret1 + i));
65 ICUNIT_ASSERT_EQUAL_VOID(i, (int)(*(tid2 + i)), i);
66 } else {
67 ICUNIT_ASSERT_EQUAL_VOID(*(ret1 + i), -1, *(ret1 + i));
68 }
69 i++;
70 }
71
72 i = 0;
73 while (*(ret1 + i) == 0) {
74 ret = timer_delete(*(tid2 + i));
75 ICUNIT_ASSERT_EQUAL_VOID(ret, 0, ret);
76 i++;
77 }
78
79 i = 0;
80 while (i < 1024) {
81 *(ret1 + i) = timer_create(CLOCK_REALTIME, &ent, tid2 + i);
82 if (*(ret1 + i) == 0) {
83 ICUNIT_ASSERT_EQUAL_VOID(*(ret1 + i), 0, *(ret1 + i));
84 ICUNIT_ASSERT_EQUAL_VOID(i, (int)(*(tid2 + i)), i);
85 } else {
86 ICUNIT_ASSERT_EQUAL_VOID(*(ret1 + i), -1, *(ret1 + i));
87 }
88 i++;
89 }
90
91 i = 0;
92 while (*(ret1 + i) == 0) {
93 ret = timer_delete(*(tid2 + i));
94 ICUNIT_ASSERT_EQUAL_VOID(ret, 0, ret);
95 i++;
96 }
97
98 free(tid2);
99 ret1 = (INT32 *)timer_create(CLOCK_REALTIME, &ent, &tid);
100 ICUNIT_ASSERT_EQUAL_VOID(ret1, 0, ret1);
101
102 its.it_interval.tv_sec = 1;
103 its.it_interval.tv_nsec = 0;
104 its.it_value.tv_sec = 1;
105 its.it_value.tv_nsec = 0;
106 ret1 = (INT32 *)timer_settime(tid, 0, &its, NULL);
107 ICUNIT_ASSERT_EQUAL_VOID(ret1, 0, ret1);
108 sleep(1);
109 ret1 = (INT32 *)timer_gettime(tid, &its2);
110 ICUNIT_ASSERT_EQUAL_VOID(ret1, 0, ret1);
111 ret1 = (INT32 *)timer_getoverrun(tid);
112 ret1 = (INT32 *)(((int)(intptr_t)ret1 >= 0) ? 0 : -1);
113 ICUNIT_ASSERT_EQUAL_VOID(ret1, 0, ret1);
114
115 ret1 = (INT32 *)timer_delete(tid);
116 ICUNIT_ASSERT_EQUAL_VOID(ret1, 0, ret1);
117
118 exit((int)(intptr_t)tid);
119 }
120
TestCase(VOID)121 static UINT32 TestCase(VOID)
122 {
123 int pid;
124 int status = 0;
125 pid = fork();
126 if (pid == 0) {
127 ChildFunc();
128 }
129
130 waitpid(pid, &status, 0);
131 return 0;
132 }
133
ItSecVid001(VOID)134 VOID ItSecVid001(VOID)
135 {
136 TEST_ADD_CASE("IT_SEC_VID_001", TestCase, TEST_POSIX, TEST_SEC, TEST_LEVEL0, TEST_FUNCTION);
137 }
138