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 if (tid2 == NULL) {
59 return;
60 }
61 ret1 = (int *)malloc(sizeof(int) * 1024);
62 if (ret1 == NULL) {
63 free(tid2);
64 return;
65 }
66 (void)memset_s(tid2, sizeof(char *) * 1024, 0, sizeof(char *) * 1024);
67 (void)memset_s(ret1, sizeof(int) * 1024, 0xff, sizeof(int) * 1024);
68 while (i < 1024) {
69 *(ret1 + i) = timer_create(CLOCK_REALTIME, &ent, tid2 + i);
70 if (*(ret1 + i) == 0) {
71 ICUNIT_ASSERT_EQUAL_VOID(*(ret1 + i), 0, *(ret1 + i));
72 ICUNIT_ASSERT_EQUAL_VOID(i, (int)(*(tid2 + i)), i);
73 } else {
74 ICUNIT_ASSERT_EQUAL_VOID(*(ret1 + i), -1, *(ret1 + i));
75 }
76 i++;
77 }
78
79 i = 0;
80 while (*(ret1 + i) == 0) {
81 ret = timer_delete(*(tid2 + i));
82 ICUNIT_ASSERT_EQUAL_VOID(ret, 0, ret);
83 i++;
84 }
85
86 i = 0;
87 while (i < 1024) {
88 *(ret1 + i) = timer_create(CLOCK_REALTIME, &ent, tid2 + i);
89 if (*(ret1 + i) == 0) {
90 ICUNIT_ASSERT_EQUAL_VOID(*(ret1 + i), 0, *(ret1 + i));
91 ICUNIT_ASSERT_EQUAL_VOID(i, (int)(*(tid2 + i)), i);
92 } else {
93 ICUNIT_ASSERT_EQUAL_VOID(*(ret1 + i), -1, *(ret1 + i));
94 }
95 i++;
96 }
97
98 i = 0;
99 while (*(ret1 + i) == 0) {
100 ret = timer_delete(*(tid2 + i));
101 ICUNIT_ASSERT_EQUAL_VOID(ret, 0, ret);
102 i++;
103 }
104
105 free(tid2);
106 ret1 = (INT32 *)timer_create(CLOCK_REALTIME, &ent, &tid);
107 ICUNIT_ASSERT_EQUAL_VOID(ret1, 0, ret1);
108
109 its.it_interval.tv_sec = 1;
110 its.it_interval.tv_nsec = 0;
111 its.it_value.tv_sec = 1;
112 its.it_value.tv_nsec = 0;
113 ret1 = (INT32 *)timer_settime(tid, 0, &its, NULL);
114 ICUNIT_ASSERT_EQUAL_VOID(ret1, 0, ret1);
115 sleep(1);
116 ret1 = (INT32 *)timer_gettime(tid, &its2);
117 ICUNIT_ASSERT_EQUAL_VOID(ret1, 0, ret1);
118 ret1 = (INT32 *)timer_getoverrun(tid);
119 ret1 = (INT32 *)(((int)(intptr_t)ret1 >= 0) ? 0 : -1);
120 ICUNIT_ASSERT_EQUAL_VOID(ret1, 0, ret1);
121
122 ret1 = (INT32 *)timer_delete(tid);
123 ICUNIT_ASSERT_EQUAL_VOID(ret1, 0, ret1);
124
125 exit(static_cast<int>(reinterpret_cast<intptr_t>(tid)));
126 }
127
TestCase(VOID)128 static UINT32 TestCase(VOID)
129 {
130 int pid;
131 int status = 0;
132 pid = fork();
133 if (pid == 0) {
134 ChildFunc();
135 }
136
137 waitpid(pid, &status, 0);
138 return 0;
139 }
140
ItSecVid001(VOID)141 VOID ItSecVid001(VOID)
142 {
143 TEST_ADD_CASE("IT_SEC_VID_001", TestCase, TEST_POSIX, TEST_SEC, TEST_LEVEL0, TEST_FUNCTION);
144 }
145