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
32 #include <time.h>
33 #include <semaphore.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include "ohos_types.h"
37 #include "posix_test.h"
38 #include "los_config.h"
39 #include "kernel_test.h"
40 #include "log.h"
41
42 #define NANO_S 1000000000
43
44 /* *
45 * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency
46 * @param : subsystem name is utils
47 * @param : module name is utilsFile
48 * @param : test suit name is PosixSemaphoreFuncTestSuite
49 */
50 LITE_TEST_SUIT(Posix, PosixSemaphore, PosixSemaphoreFuncTestSuite);
51
52 /* *
53 * @tc.setup : setup for all testcases
54 * @return : setup result, TRUE is success, FALSE is fail
55 */
PosixSemaphoreFuncTestSuiteSetUp(void)56 static BOOL PosixSemaphoreFuncTestSuiteSetUp(void)
57 {
58 return TRUE;
59 }
60
61 /* *
62 * @tc.teardown : teardown for all testcases
63 * @return : teardown result, TRUE is success, FALSE is fail
64 */
PosixSemaphoreFuncTestSuiteTearDown(void)65 static BOOL PosixSemaphoreFuncTestSuiteTearDown(void)
66 {
67 LOG("+-------------------------------------------+\n");
68 return TRUE;
69 }
70
71 // get cur-time plus ms
GetDelayedTime(unsigned int ms)72 struct timespec GetDelayedTime(unsigned int ms)
73 {
74 LOG("GetDelayedTime ms = %u", ms);
75 struct timespec ts = { 0 };
76 const unsigned int nsecPerSec = 1000000000;
77 const unsigned int nsecPerMs = 1000000;
78 unsigned long long setTimeNs = ms * nsecPerMs;
79 struct timespec tsNow = { 0 };
80 clock_gettime(CLOCK_REALTIME, &tsNow);
81 LOG("Cur time tv_sec: %lld, tv_nsec: %ld", tsNow.tv_sec, tsNow.tv_nsec);
82 ts.tv_sec = tsNow.tv_sec + (tsNow.tv_nsec + setTimeNs) / nsecPerSec;
83 ts.tv_nsec = (tsNow.tv_nsec + setTimeNs) % nsecPerSec;
84 return ts;
85 }
86
87 // calculate time difference, in ms
GetTimeDiff(struct timespec ts1,struct timespec ts2)88 int GetTimeDiff(struct timespec ts1, struct timespec ts2)
89 {
90 const int nsecPerSec = 1000000000;
91 const int nsecPerMs = 1000000;
92 int ms = (ts1.tv_sec - ts2.tv_sec) * nsecPerSec + (ts1.tv_nsec - ts2.tv_nsec);
93 ms = ms / nsecPerMs;
94 LOG("different result: %d (ms)", ms);
95 return ms;
96 }
97
98 /* *
99 * @tc.number SUB_KERNEL_IPC_SEM_TIMEDWAIT_001
100 * @tc.name sem_timedwait get semaphore timeout
101 * @tc.desc [C- SOFTWARE -0200]
102 */
103 LITE_TEST_CASE(PosixSemaphoreFuncTestSuite, testIpcSem_Timedwait001, Function | MediumTest | Level1)
104 {
105 struct timespec ts = { 0 };
106 struct timespec tsNow = { 0 };
107 sem_t sem;
108
109 ICUNIT_ASSERT_EQUAL(sem_init((sem_t *)&sem, 0, 0), 0, 0);
110
111 ts = GetDelayedTime(100);
112 LOG("predicted time:%lld, %ld", ts.tv_sec, ts.tv_nsec);
113 if (sem_timedwait((sem_t *)&sem, &ts) == -1) {
114 ICUNIT_ASSERT_EQUAL(errno, ETIMEDOUT, errno);
115 } else {
116 LOG("\n> sem_timedwait return unexpected");
117 }
118
119 clock_gettime(CLOCK_REALTIME, &tsNow);
120 LOG("tsNow %lld, %ld", tsNow.tv_sec, tsNow.tv_nsec);
121 int timeDiff = GetTimeDiff(tsNow, ts); // calculate time different
122 LOG("timeDiff %d", timeDiff);
123 ICUNIT_ASSERT_EQUAL(abs(timeDiff) < 20, TRUE, 0);
124
125 ICUNIT_ASSERT_EQUAL(sem_destroy((sem_t *)&sem), 0, 0);
126 return 0;
127 }
128
129 /* *
130 * @tc.number SUB_KERNEL_IPC_SEM_TIMEDWAIT_001
131 * @tc.name sem_timedwait get semaphore success
132 * @tc.desc [C- SOFTWARE -0200]
133 */
134 LITE_TEST_CASE(PosixSemaphoreFuncTestSuite, testIpcSem_Timedwait002, Function | MediumTest | Level1)
135 {
136 struct timespec ts = { 0 };
137 struct timespec tsNow = { 0 };
138 struct timespec tsBegin = { 0 };
139 sem_t sem;
140
141 ICUNIT_ASSERT_EQUAL(sem_init((sem_t *)&sem, 0, 1), 0, 0);
142
143 ts = GetDelayedTime(100);
144 LOG("\n ts %lld, %ld", ts.tv_sec, ts.tv_nsec);
145 clock_gettime(CLOCK_REALTIME, &tsBegin);
146 int ret = sem_timedwait((sem_t *)&sem, &ts);
147 clock_gettime(CLOCK_REALTIME, &tsNow);
148
149 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
150
151 LOG("\n tsBegin %lld, %ld, tsNow %lld, %ld", tsBegin.tv_sec, tsBegin.tv_nsec, tsNow.tv_sec, tsNow.tv_nsec);
152 int timeDiff = GetTimeDiff(tsNow, tsBegin); // calculate time different
153 LOG("\n timeDiff %d", timeDiff);
154 ICUNIT_ASSERT_WITHIN_EQUAL(timeDiff, timeDiff, 19, 0);
155
156 // try get semaphore again
157 ts = GetDelayedTime(100);
158 LOG("\n ts %lld, %ld", ts.tv_sec, ts.tv_nsec);
159 ret = sem_timedwait((sem_t *)&sem, &ts);
160 clock_gettime(CLOCK_REALTIME, &tsNow);
161 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
162 ICUNIT_ASSERT_EQUAL(errno, ETIMEDOUT, errno);
163 LOG("\n tsNow %lld, %ld", tsNow.tv_sec, tsNow.tv_nsec);
164 timeDiff = GetTimeDiff(tsNow, tsBegin); // calculate time different
165 LOG("\n wait timeDiff %d", timeDiff);
166
167 ICUNIT_ASSERT_EQUAL(sem_destroy((sem_t *)&sem), 0, 0);
168 return 0;
169 }
170
171 /* *
172 * @tc.number SUB_KERNEL_IPC_SEM_TIMEDWAIT_003
173 * @tc.name {sem_timedwait} get semaphore, wait time abnormal, tv_nsec less than 0
174 * @tc.desc [C- SOFTWARE -0200]
175 */
176 LITE_TEST_CASE(PosixSemaphoreFuncTestSuite, testIpcSem_Timedwait003, Function | MediumTest | Level3)
177 {
178 int ret;
179 struct timespec ts;
180 sem_t sem;
181
182 ts.tv_sec = 0;
183 ts.tv_nsec = 200000;
184 ret = sem_timedwait((sem_t *)&sem, &ts);
185 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
186 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
187
188 ret = sem_init((sem_t *)&sem, 0, 0);
189 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
190
191 ts.tv_sec = time(NULL);
192 ts.tv_nsec = -2;
193 ret = sem_timedwait((sem_t *)&sem, &ts);
194 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
195 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
196
197 ts.tv_sec = time(NULL);
198 ts.tv_nsec = NANO_S;
199 ret = sem_timedwait((sem_t *)&sem, &ts);
200 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
201 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
202
203 ret = sem_timedwait((sem_t *)&sem, NULL);
204 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
205 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
206
207 ret = sem_destroy((sem_t *)&sem);
208 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
209
210 ts.tv_sec = time(NULL);
211 ts.tv_nsec = 2000000;
212 ret = sem_timedwait(NULL, &ts);
213 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
214 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
215 return 0;
216 }
217
218 /* *
219 * @tc.number SUB_KERNEL_IPC_SEM_TIMEDWAIT_004
220 * @tc.name sem_trywait get semaphore success
221 * @tc.desc [C- SOFTWARE -0200]
222 */
223 LITE_TEST_CASE(PosixSemaphoreFuncTestSuite, testIpcSem_Trywait004, Function | MediumTest | Level3)
224 {
225 sem_t sem;
226 int val;
227 int ret;
228
229 ret = sem_init(&sem, 0, 1);
230 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
231
232 ret = sem_trywait(&sem);
233 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
234
235 ret = sem_getvalue(&sem, &val);
236 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
237
238 if (val <= 0) {
239 sem_destroy(&sem);
240 return LOS_OK;
241 } else {
242 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
243 }
244 return 0;
245 }
246
247 RUN_TEST_SUITE(PosixSemaphoreFuncTestSuite);
248
PosixSemaphoreFuncTest()249 void PosixSemaphoreFuncTest()
250 {
251 PRINT_EMG("begin PosixSemaphoreFuncTest....");
252 RUN_ONE_TESTCASE(testIpcSem_Timedwait001);
253 RUN_ONE_TESTCASE(testIpcSem_Timedwait002);
254 RUN_ONE_TESTCASE(testIpcSem_Timedwait003);
255 RUN_ONE_TESTCASE(testIpcSem_Trywait004);
256
257 return;
258 }