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 = %lu", 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: %ld (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 int semValue = 0;
109
110 TEST_ASSERT_EQUAL_INT(0, sem_init((sem_t *)&sem, 0, 0));
111
112 ts = GetDelayedTime(100);
113 LOG("predicted time:%lld, %d", ts.tv_sec, ts.tv_nsec);
114 if (sem_timedwait((sem_t *)&sem, &ts) == -1) {
115 TEST_ASSERT_EQUAL_INT(ETIMEDOUT, errno);
116 } else {
117 LOG("\n> sem_timedwait return unexpected");
118 }
119
120 clock_gettime(CLOCK_REALTIME, &tsNow);
121 LOG("tsNow %lld, %d", tsNow.tv_sec, tsNow.tv_nsec);
122 int timeDiff = GetTimeDiff(tsNow, ts); // calculate time different
123 LOG("timeDiff %d", timeDiff);
124 TEST_ASSERT_LESS_THAN_INT(20, abs(timeDiff));
125
126 TEST_ASSERT_EQUAL_INT(0, sem_destroy((sem_t *)&sem));
127 return 0;
128 }
129
130 /* *
131 * @tc.number SUB_KERNEL_IPC_SEM_TIMEDWAIT_001
132 * @tc.name sem_timedwait get semaphore success
133 * @tc.desc [C- SOFTWARE -0200]
134 */
135 LITE_TEST_CASE(PosixSemaphoreFuncTestSuite, testIpcSem_Timedwait002, Function | MediumTest | Level1)
136 {
137 struct timespec ts = { 0 };
138 struct timespec tsNow = { 0 };
139 struct timespec tsBegin = { 0 };
140 sem_t sem;
141 int semValue = 0;
142
143 TEST_ASSERT_EQUAL_INT(0, sem_init((sem_t *)&sem, 0, 1));
144
145 ts = GetDelayedTime(100);
146 LOG("\n ts %lld, %d", ts.tv_sec, ts.tv_nsec);
147 clock_gettime(CLOCK_REALTIME, &tsBegin);
148 int ret = sem_timedwait((sem_t *)&sem, &ts);
149 clock_gettime(CLOCK_REALTIME, &tsNow);
150
151 TEST_ASSERT_EQUAL_INT(0, ret);
152
153 LOG("\n tsBegin %ld, %ld, tsNow %ld, %ld", tsBegin.tv_sec, tsBegin.tv_nsec, tsNow.tv_sec, tsNow.tv_nsec);
154 int timeDiff = GetTimeDiff(tsNow, tsBegin); // calculate time different
155 LOG("\n timeDiff %d", timeDiff);
156 TEST_ASSERT_LESS_THAN_INT(20, timeDiff);
157
158 // try get semaphore again
159 ts = GetDelayedTime(100);
160 LOG("\n ts %d, %d", ts.tv_sec, ts.tv_nsec);
161 ret = sem_timedwait((sem_t *)&sem, &ts);
162 clock_gettime(CLOCK_REALTIME, &tsNow);
163 TEST_ASSERT_EQUAL_INT(-1, ret);
164 TEST_ASSERT_EQUAL_INT(ETIMEDOUT, errno);
165 LOG("\n tsNow %ld, %ld", tsNow.tv_sec, tsNow.tv_nsec);
166 timeDiff = GetTimeDiff(tsNow, tsBegin); // calculate time different
167 LOG("\n wait timeDiff %d", timeDiff);
168
169 TEST_ASSERT_EQUAL_INT(0, sem_destroy((sem_t *)&sem));
170 return 0;
171 }
172
173 /* *
174 * @tc.number SUB_KERNEL_IPC_SEM_TIMEDWAIT_003
175 * @tc.name {sem_timedwait} get semaphore, wait time abnormal, tv_nsec less than 0
176 * @tc.desc [C- SOFTWARE -0200]
177 */
178 LITE_TEST_CASE(PosixSemaphoreFuncTestSuite, testIpcSem_Timedwait003, Function | MediumTest | Level3)
179 {
180 int ret;
181 struct timespec ts;
182 sem_t sem;
183
184 ts.tv_sec = 0;
185 ts.tv_nsec = 200000;
186 ret = sem_timedwait((sem_t *)&sem, &ts);
187 TEST_ASSERT_EQUAL_INT(-1, ret);
188 TEST_ASSERT_EQUAL_INT(errno, EINVAL);
189
190 ret = sem_init((sem_t *)&sem, 0, 0);
191 TEST_ASSERT_EQUAL_INT(0, ret);
192
193 ts.tv_sec = time(NULL);
194 ts.tv_nsec = -2;
195 ret = sem_timedwait((sem_t *)&sem, &ts);
196 TEST_ASSERT_EQUAL_INT(-1, ret);
197 TEST_ASSERT_EQUAL_INT(errno, EINVAL);
198
199 ts.tv_sec = time(NULL);
200 ts.tv_nsec = NANO_S;
201 ret = sem_timedwait((sem_t *)&sem, &ts);
202 TEST_ASSERT_EQUAL_INT(-1, ret);
203 TEST_ASSERT_EQUAL_INT(errno, EINVAL);
204
205 ret = sem_timedwait((sem_t *)&sem, NULL);
206 TEST_ASSERT_EQUAL_INT(-1, ret);
207 TEST_ASSERT_EQUAL_INT(errno, EINVAL);
208
209 ret = sem_destroy((sem_t *)&sem);
210 TEST_ASSERT_EQUAL_INT(0, ret);
211
212 ts.tv_sec = time(NULL);
213 ts.tv_nsec = 2000000;
214 ret = sem_timedwait(NULL, &ts);
215 TEST_ASSERT_EQUAL_INT(-1, ret);
216 TEST_ASSERT_EQUAL_INT(errno, EINVAL);
217 return 0;
218 }
219
220 /* *
221 * @tc.number SUB_KERNEL_IPC_SEM_TIMEDWAIT_004
222 * @tc.name sem_trywait get semaphore success
223 * @tc.desc [C- SOFTWARE -0200]
224 */
225 LITE_TEST_CASE(PosixSemaphoreFuncTestSuite, testIpcSem_Trywait004, Function | MediumTest | Level3)
226 {
227 sem_t sem;
228 int val;
229 int ret;
230
231 ret = sem_init(&sem, 0, 1);
232 TEST_ASSERT_EQUAL_INT(0, ret);
233
234 ret = sem_trywait(&sem);
235 TEST_ASSERT_EQUAL_INT(0, ret);
236
237 ret = sem_getvalue(&sem, &val);
238 TEST_ASSERT_EQUAL_INT(0, ret);
239
240 if (val <= 0) {
241 sem_destroy(&sem);
242 return LOS_OK;
243 } else {
244 TEST_ASSERT_EQUAL_INT(0, ret);
245 }
246 return 0;
247 }
248
249 RUN_TEST_SUITE(PosixSemaphoreFuncTestSuite);
250
PosixSemaphoreFuncTest()251 void PosixSemaphoreFuncTest()
252 {
253 PRINT_EMG("begin PosixSemaphoreFuncTest....");
254 RUN_ONE_TESTCASE(testIpcSem_Timedwait001);
255 RUN_ONE_TESTCASE(testIpcSem_Timedwait002);
256 RUN_ONE_TESTCASE(testIpcSem_Timedwait003);
257 RUN_ONE_TESTCASE(testIpcSem_Trywait004);
258
259 return;
260 }