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 #define _GNU_SOURCE
33 #undef _XOPEN_SOURCE
34 #define _XOPEN_SOURCE 600
35
36 #include <sys/time.h>
37 #include <sys/times.h>
38 #include <time.h>
39 #include <errno.h>
40 #include <limits.h>
41 #include "ohos_types.h"
42 #include "posix_test.h"
43 #include "los_config.h"
44 #include "securec.h"
45 #include "kernel_test.h"
46 #include "log.h"
47 #include "los_tick.h"
48 #include <unistd.h>
49
50 #define RET_OK 0
51
52 #define SECS_PER_MIN 60
53 #define SLEEP_ACCURACY 21000 // 20 ms, with 1ms deviation
54 #define ACCURACY_TEST_LOOPS 3 // loops for accuracy test, than count average value
55 #define MILLISECONDS_PER_SECOND 1000
56 #define NANOSECONDS_PER_MILLISECOND 1000000
57 #define TM_BASE_YEAR 1900
58 #define TIME_STR_LEN 100
59
60 #define INIT_TM(tmSt, year, mon, day, hour, min, sec, wday) \
61 do { \
62 (tmSt).tm_sec = (sec); \
63 (tmSt).tm_min = (min); \
64 (tmSt).tm_hour = (hour); \
65 (tmSt).tm_mday = (day); \
66 (tmSt).tm_mon = (mon); \
67 (tmSt).tm_year = (year) - 1900; \
68 (tmSt).tm_wday = wday; \
69 (tmSt).__tm_gmtoff = 0; \
70 (tmSt).__tm_zone = ""; \
71 } while (0)
72
73 /* *
74 * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency
75 * @param : subsystem name is utils
76 * @param : module name is utilsFile
77 * @param : test suit name is CmsisTaskFuncTestSuite
78 */
79 LITE_TEST_SUIT(Posix, PosixTime, PosixTimeFuncTestSuite);
80
81 /* *
82 * @tc.setup : setup for all testcases
83 * @return : setup result, TRUE is success, FALSE is fail
84 */
PosixTimeFuncTestSuiteSetUp(void)85 static BOOL PosixTimeFuncTestSuiteSetUp(void)
86 {
87 return TRUE;
88 }
89
90 /* *
91 * @tc.teardown : teardown for all testcases
92 * @return : teardown result, TRUE is success, FALSE is fail
93 */
PosixTimeFuncTestSuiteTearDown(void)94 static BOOL PosixTimeFuncTestSuiteTearDown(void)
95 {
96 return TRUE;
97 }
98
KeepRun(int msec)99 static int KeepRun(int msec)
100 {
101 struct timespec time1 = { 0, 0 };
102 struct timespec time2 = { 0, 0 };
103 clock_gettime(CLOCK_MONOTONIC, &time1);
104 LOG("KeepRun start : tv_sec=%lld, tv_nsec=%ld\n", time1.tv_sec, time1.tv_nsec);
105 int loop = 0;
106 int ran = 0;
107 while (ran < msec) {
108 ++loop;
109 clock_gettime(CLOCK_MONOTONIC, &time2);
110 ran = (time2.tv_sec - time1.tv_sec) * MILLISECONDS_PER_SECOND;
111 ran += (time2.tv_nsec - time1.tv_nsec) / NANOSECONDS_PER_MILLISECOND;
112 }
113
114 LOG("KeepRun end : tv_sec=%lld, tv_nsec=%ld\n", time2.tv_sec, time2.tv_nsec);
115 return loop;
116 }
117
CheckValueClose(double target,double actual,double accuracy)118 static int CheckValueClose(double target, double actual, double accuracy)
119 {
120 double diff = actual - target;
121 double pct;
122 if (diff < 0) {
123 diff = -diff;
124 }
125 if (actual == 0) {
126 return 0;
127 } else {
128 pct = diff / actual;
129 }
130 return (pct <= accuracy);
131 }
132
TmToStr(const struct tm * timePtr,char * timeStr,unsigned len)133 static char *TmToStr(const struct tm *timePtr, char *timeStr, unsigned len)
134 {
135 if (timePtr == NULL || timeStr == NULL) {
136 return "";
137 }
138 (VOID)sprintf_s(timeStr, len, "%ld/%d/%d %02d:%02d:%02d WEEK(%d)", timePtr->tm_year + TM_BASE_YEAR,
139 timePtr->tm_mon + 1, timePtr->tm_mday, timePtr->tm_hour, timePtr->tm_min, timePtr->tm_sec, timePtr->tm_wday);
140 return timeStr;
141 }
142
143 /* *
144 * @tc.number SUB_KERNEL_TIME_USLEEP_001
145 * @tc.name usleep accuracy test
146 * @tc.desc [C- SOFTWARE -0200]
147 */
148 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeUSleep001, Function | MediumTest | Level1)
149 {
150 // wifiiot无法支持10ms以下的sleep
151 int interval[] = {15*1000, 20*1000, 30*1000, 300*1000};
152 for (unsigned int j = 0; j < sizeof(interval) / sizeof(int); j++) {
153 LOG("\ntest interval:%d\n", interval[j]);
154 struct timespec time1 = { 0 }, time2 = { 0 };
155 long duration; // unit: us
156 double d = 0.0;
157 for (int i = 1; i <= ACCURACY_TEST_LOOPS; i++) {
158 clock_gettime(CLOCK_MONOTONIC, &time1);
159 int rt = usleep(interval[j]);
160 clock_gettime(CLOCK_MONOTONIC, &time2);
161 ICUNIT_ASSERT_EQUAL(rt, RET_OK, rt);
162 duration = (time2.tv_sec - time1.tv_sec) * 1000000 + (time2.tv_nsec - time1.tv_nsec) / 1000;
163 LOG("testloop %d, actual usleep duration: %ld us\n", i, duration);
164 d += duration;
165 }
166 d = d / ACCURACY_TEST_LOOPS; // average
167 LOG("interval:%u, average duration: %.2f\n", interval[j], d);
168 ICUNIT_ASSERT_WITHIN_EQUAL(d, interval[j], d, 0);
169 ICUNIT_ASSERT_WITHIN_EQUAL(d, interval[j] - SLEEP_ACCURACY, interval[j] + SLEEP_ACCURACY, d);
170 }
171 return 0;
172 }
173
174 /* *
175 * @tc.number SUB_KERNEL_TIME_USLEEP_002
176 * @tc.name usleep test for special delay
177 * @tc.desc [C- SOFTWARE -0200]
178 */
179 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeUSleep002, Function | MediumTest | Level1)
180 {
181 struct timespec time1 = { 0 };
182 struct timespec time2 = { 0 };
183 clock_gettime(CLOCK_MONOTONIC, &time1);
184 int rt = usleep(0);
185 clock_gettime(CLOCK_MONOTONIC, &time2);
186 ICUNIT_ASSERT_EQUAL(rt, RET_OK, rt);
187 long long duration = (time2.tv_sec - time1.tv_sec) * 1000000 + (time2.tv_nsec - time1.tv_nsec) / 1000;
188 LOG("\n usleep(0), actual usleep duration: %lld us\n", duration);
189 ICUNIT_ASSERT_WITHIN_EQUAL(duration, duration, 1000, 0);
190 return 0;
191 }
192
193 /* *
194 * @tc.number SUB_KERNEL_TIME_GMTIME_001
195 * @tc.name test gmtime api
196 * @tc.desc [C- SOFTWARE -0200]
197 */
198 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeGmtime001, Function | MediumTest | Level1)
199 {
200 time_t time1 = 18880;
201 char timeStr[TIME_STR_LEN] = {0};
202 LOG("\nsizeof(time_t) = %d, sizeof(struct tm) = %d", sizeof(time_t), sizeof(struct tm));
203 struct tm *timePtr = gmtime(&time1);
204 ICUNIT_ASSERT_STRING_EQUAL("1970/1/1 05:14:40 WEEK(4)", TmToStr(timePtr, timeStr, TIME_STR_LEN), 0);
205
206 time1 = LONG_MAX;
207 timePtr = gmtime(&time1);
208 LOG("\n LONG_MAX = %lld, cvt result : %s", time1, TmToStr(timePtr, timeStr, TIME_STR_LEN));
209 ICUNIT_ASSERT_STRING_EQUAL("2038/1/19 03:14:07 WEEK(2)", TmToStr(timePtr, timeStr, TIME_STR_LEN), 0);
210
211 time1 = LONG_MAX - 1;
212 timePtr = gmtime(&time1);
213 LOG("\n LONG_MAX - 1 = %lld, cvt result : %s", time1, TmToStr(timePtr, timeStr, TIME_STR_LEN));
214 ICUNIT_ASSERT_STRING_EQUAL("2038/1/19 03:14:06 WEEK(2)", TmToStr(timePtr, timeStr, TIME_STR_LEN), 0);
215
216 time1 = LONG_MIN;
217 timePtr = gmtime(&time1);
218 LOG("\n LONG_MIN = %lld, cvt result : %s", time1, TmToStr(timePtr, timeStr, TIME_STR_LEN));
219 ICUNIT_ASSERT_STRING_EQUAL("1901/12/13 20:45:52 WEEK(5)", TmToStr(timePtr, timeStr, TIME_STR_LEN), 0);
220
221 time1 = LONG_MIN + 1;
222 timePtr = gmtime(&time1);
223 LOG("\n LONG_MIN + 1 = %lld, cvt result : %s", time1, TmToStr(timePtr, timeStr, TIME_STR_LEN));
224 ICUNIT_ASSERT_STRING_EQUAL("1901/12/13 20:45:53 WEEK(5)", TmToStr(timePtr, timeStr, TIME_STR_LEN), 0);
225 return 0;
226 };
227
228 #if (LOSCFG_LIBC_MUSL == 1)
229 /* *
230 * @tc.number SUB_KERNEL_TIME_LOCALTIME_001
231 * @tc.name test localtime api
232 * @tc.desc [C- SOFTWARE -0200]
233 */
234 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeLocaltime001, Function | MediumTest | Level1)
235 {
236 char cTime[32];
237 time_t tStart;
238 time_t tEnd;
239
240 struct timeval timeSet = {
241 .tv_sec = 86399,
242 .tv_usec = 0
243 };
244
245 int ret = settimeofday(&timeSet, NULL);
246 time(&tStart);
247 sleep(2);
248 time(&tEnd);
249 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
250
251 struct tm *tmStart = localtime(&tStart);
252 strftime(cTime, sizeof(cTime), "%H:%M:%S", tmStart);
253 ICUNIT_ASSERT_STRING_EQUAL(cTime, "07:59:59", 0);
254 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
255 struct tm *tmEnd = localtime(&tEnd);
256 strftime(cTime, sizeof(cTime), "%H:%M:%S", tmEnd);
257 ICUNIT_ASSERT_STRING_EQUAL(cTime, "08:00:01", 0);
258 LOG("\n time_t=%lld, first time:%s", tEnd, cTime);
259 return 0;
260 }
261
262 /* *
263 * @tc.number SUB_KERNEL_TIME_LOCALTIME_002
264 * @tc.name test localtime api
265 * @tc.desc [C- SOFTWARE -0200]
266 */
267 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeLocaltime002, Function | MediumTest | Level1)
268 {
269 char cTime[32];
270 time_t tStart = LONG_MAX;
271 struct tm *tmStart = localtime(&tStart);
272 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
273 ICUNIT_ASSERT_STRING_EQUAL(cTime, "38-01-19 11:14:07", 0);
274 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
275
276 tStart = LONG_MIN;
277 tmStart = localtime(&tStart);
278 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
279 ICUNIT_ASSERT_STRING_EQUAL(cTime, "01-12-14 04:45:52", 0);
280 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
281
282 tStart = 0;
283 tmStart = localtime(&tStart);
284 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
285 ICUNIT_ASSERT_STRING_EQUAL(cTime, "70-01-01 08:00:00", 0);
286 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
287
288 tStart = -1;
289 tmStart = localtime(&tStart);
290 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
291 ICUNIT_ASSERT_STRING_EQUAL(cTime, "70-01-01 07:59:59", 0);
292 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
293 return 0;
294 }
295
296 /* *
297 * @tc.number SUB_KERNEL_TIME_LOCALTIME_003
298 * @tc.name test settimeofday api
299 * @tc.desc [C- SOFTWARE -0200]
300 */
301 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeLocaltime003, Function | MediumTest | Level1)
302 {
303 char cTime[32]; /* 32, no special meaning */
304 time_t tStart;
305 time_t tEnd;
306 struct timezone tz;
307 struct timeval timeSet = {
308 .tv_sec = 86399, /* 86399, no special meaning */
309 .tv_usec = 0
310 };
311
312 int ret = gettimeofday(NULL, &tz);
313 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
314
315 ret = settimeofday(&timeSet, &tz);
316 time(&tStart);
317 sleep(2); /* 2, sleep time */
318 time(&tEnd);
319 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
320
321 struct tm *tmStart = localtime(&tStart);
322 strftime(cTime, sizeof(cTime), "%H:%M:%S", tmStart);
323 ICUNIT_ASSERT_STRING_EQUAL(cTime, "07:59:59", 0);
324 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
325 struct tm *tmEnd = localtime(&tEnd);
326 strftime(cTime, sizeof(cTime), "%H:%M:%S", tmEnd);
327 ICUNIT_ASSERT_STRING_EQUAL(cTime, "08:00:01", 0);
328 LOG("\n time_t=%lld, first time:%s", tEnd, cTime);
329 return 0;
330 }
331
332 /* *
333 * @tc.number SUB_KERNEL_TIME_LOCALTIMER_001
334 * @tc.name localtime_r api base test
335 * @tc.desc [C- SOFTWARE -0200]
336 */
337 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeLocaltimer001, Function | MediumTest | Level1)
338 {
339 char cTime[32];
340 time_t tStart;
341 time_t tEnd;
342 struct tm tmrStart = { 0 };
343 struct tm tmrEnd = { 0 };
344 struct timeval tSet = {
345 .tv_sec = 86399,
346 .tv_usec = 0
347 };
348
349 int ret = settimeofday(&tSet, NULL);
350 time(&tStart);
351 sleep(2);
352 time(&tEnd);
353 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
354
355 struct tm *tmrStartPtr = localtime_r(&tStart, &tmrStart);
356 struct tm *tmrEndPtr = localtime_r(&tEnd, &tmrEnd);
357
358 strftime(cTime, sizeof(cTime), "%H:%M:%S", &tmrStart);
359 ICUNIT_ASSERT_STRING_EQUAL("07:59:59", cTime, 0);
360 strftime(cTime, sizeof(cTime), "%H:%M:%S", tmrStartPtr);
361 ICUNIT_ASSERT_STRING_EQUAL("07:59:59", cTime, 0);
362 strftime(cTime, sizeof(cTime), "%H:%M:%S", &tmrEnd);
363 ICUNIT_ASSERT_STRING_EQUAL("08:00:01", cTime, 0);
364 strftime(cTime, sizeof(cTime), "%H:%M:%S", tmrEndPtr);
365 ICUNIT_ASSERT_STRING_EQUAL("08:00:01", cTime, 0);
366 return 0;
367 }
368
369 /* *
370 * @tc.number SUB_KERNEL_TIME_LOCALTIMER_002
371 * @tc.name test localtime_r api for range
372 * @tc.desc [C- SOFTWARE -0200]
373 */
374 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeLocaltimer002, Function | MediumTest | Level1)
375 {
376 char cTime[32];
377 struct tm tmrResult = { 0 };
378
379 time_t tStart = LONG_MAX;
380 struct tm *tmStart = localtime_r(&tStart, &tmrResult);
381 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
382 ICUNIT_ASSERT_STRING_EQUAL("38-01-19 11:14:07", cTime, 0);
383 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
384
385 tStart = LONG_MIN;
386 tmStart = localtime_r(&tStart, &tmrResult);
387 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
388 ICUNIT_ASSERT_STRING_EQUAL("01-12-14 04:45:52", cTime, 0);
389 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
390
391 tStart = 0;
392 tmStart = localtime_r(&tStart, &tmrResult);
393 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
394 ICUNIT_ASSERT_STRING_EQUAL("70-01-01 08:00:00", cTime, 0);
395 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
396
397 tStart = -1;
398 tmStart = localtime_r(&tStart, &tmrResult);
399 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
400 ICUNIT_ASSERT_STRING_EQUAL("70-01-01 07:59:59", cTime, 0);
401 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
402 return 0;
403 }
404 #endif
405
406 /* *
407 * @tc.number SUB_KERNEL_TIME_MKTIME_001
408 * @tc.name mktime api base test
409 * @tc.desc [C- SOFTWARE -0200]
410 */
411 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeMktime001, Function | MediumTest | Level1)
412 {
413 struct tm testTM = { 0 };
414 time_t testTime = 18880;
415 char timeStr[TIME_STR_LEN] = {0};
416 struct timeval tv;
417 struct timezone tz;
418
419 // get system timezone
420 int ret = gettimeofday(&tv, &tz);
421 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
422 long sysTimezone = (long)(-tz.tz_minuteswest) * SECS_PER_MIN;
423 LOG("\n system timezone = %ld\n", sysTimezone);
424
425 INIT_TM(testTM, 2020, 7, 9, 18, 10, 0, 7);
426 time_t timeRet = mktime(&testTM);
427 LOG("\n 2020-7-9 18:10:00, mktime Ret = %lld", timeRet);
428 ICUNIT_ASSERT_EQUAL(sysTimezone, testTM.__tm_gmtoff, 0);
429 ICUNIT_ASSERT_EQUAL(1596996600 - testTM.__tm_gmtoff, timeRet, 0);
430
431 INIT_TM(testTM, 1970, 0, 1, 8, 0, 0, 0);
432 timeRet = mktime(&testTM);
433 LOG("\n 1970-1-1 08:00:00, mktime Ret = %lld", timeRet);
434 ICUNIT_ASSERT_EQUAL(sysTimezone, testTM.__tm_gmtoff, 0);
435 ICUNIT_ASSERT_EQUAL(28800 - testTM.__tm_gmtoff, timeRet, 0);
436
437 struct tm *timePtr = localtime(&testTime);
438 LOG("\n testTime 18880, tm : %s", TmToStr(timePtr, timeStr, TIME_STR_LEN));
439 timeRet = mktime(timePtr);
440 ICUNIT_ASSERT_EQUAL(timeRet, 18880, timeRet);
441 LOG("\n input 18880, mktime Ret = %lld", timeRet);
442
443 testTime = LONG_MAX;
444 timePtr = localtime(&testTime);
445 LOG("\n testTime LONG_MAX, tm : %s", TmToStr(timePtr, timeStr, TIME_STR_LEN));
446 timeRet = mktime(timePtr);
447 ICUNIT_ASSERT_EQUAL(timeRet, LONG_MAX, timeRet);
448 LOG("\n input LONG_MAX, mktime Ret = %lld", timeRet);
449
450 testTime = 0;
451 timePtr = localtime(&testTime);
452 LOG("\n testTime 0, tm : %s", TmToStr(timePtr, timeStr, TIME_STR_LEN));
453 timeRet = mktime(timePtr);
454 ICUNIT_ASSERT_EQUAL(timeRet, 0, timeRet);
455 LOG("\n input 0, mktime Ret = %lld", timeRet);
456 return 0;
457 }
458
459 /* *
460 * @tc.number SUB_KERNEL_TIME_MKTIME_002
461 * @tc.name mktime api test for invalid input
462 * @tc.desc [C- SOFTWARE -0200]
463 */
464 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeMktime002, Function | MediumTest | Level1)
465 {
466 struct tm testTM = { 0 };
467 LOG("\n sizeof(time_t) = %d", sizeof(time_t));
468 INIT_TM(testTM, 1969, 7, 9, 10, 10, 0, 7);
469 time_t timeRet = mktime(&testTM);
470 LOG("\n 1800-8-9 10:10:00, mktime Ret lld = %lld", timeRet);
471 #if (LOSCFG_LIBC_MUSL == 1)
472 ICUNIT_ASSERT_EQUAL(timeRet, -1, timeRet);
473 #endif
474 #if (LOSCFG_LIBC_NEWLIB == 1)
475 ICUNIT_ASSERT_WITHIN_EQUAL(timeRet, timeRet, -1, 0);
476 #endif
477 return 0;
478 }
479
480 #if (LOSCFG_LIBC_MUSL == 1)
481 /* *
482 * @tc.number SUB_KERNEL_TIME_STRFTIME_001
483 * @tc.name test strftime api
484 * @tc.desc [C- SOFTWARE -0200]
485 */
486 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeStrftime001, Function | MediumTest | Level3)
487 {
488 char buffer[80];
489 time_t mtime = 0;
490 size_t ftime = 0;
491
492 mtime = LONG_MAX;
493 ftime = strftime(buffer, 80, "%y-%m-%d %H:%M:%S", localtime(&mtime));
494 ICUNIT_ASSERT_WITHIN_EQUAL(ftime, 1, ftime, 0);
495 ICUNIT_ASSERT_STRING_EQUAL("38-01-19 11:14:07", buffer, 0);
496 LOG("\nresult: %s, expected : %s", buffer, "38-01-19 11:14:07");
497
498 mtime = LONG_MIN;
499 ftime = strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtime(&mtime));
500 ICUNIT_ASSERT_WITHIN_EQUAL(ftime, 1, ftime, 0);
501 ICUNIT_ASSERT_STRING_EQUAL("1901-12-14 04:45:52", buffer, 0);
502 LOG("\nresult: %s, expected : %s", buffer, "1901-12-14 04:45:52");
503
504 mtime = 18880;
505 ftime = strftime(buffer, 80, "%F %T", localtime(&mtime));
506 ICUNIT_ASSERT_WITHIN_EQUAL(ftime, 1, ftime, 0);
507 ICUNIT_ASSERT_STRING_EQUAL("1970-01-01 13:14:40", buffer, 0);
508 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
509
510 mtime = 18880;
511 ftime = strftime(buffer, 80, "%D %w %H:%M:%S", localtime(&mtime));
512 ICUNIT_ASSERT_WITHIN_EQUAL(ftime, 1, ftime, 0);
513 ICUNIT_ASSERT_STRING_EQUAL("01/01/70 4 13:14:40", buffer, 0);
514 LOG("\nresult: %s, expected : %s", buffer, "01/01/70 4 13:14:40");
515 return 0;
516 };
517
518 /* *
519 * @tc.number SUB_KERNEL_TIME_STRFTIME_002
520 * @tc.name test strftime api base case
521 * @tc.desc [C- SOFTWARE -0200]
522 */
523 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeStrftime002, Function | MediumTest | Level3)
524 {
525 char buffer[80];
526 time_t mtime = 18880;
527 size_t ftime = 0;
528 struct tm *tmTime = localtime(&mtime);
529
530 ftime = strftime(buffer, 80, "%Ex %EX %A", tmTime);
531 ICUNIT_ASSERT_WITHIN_EQUAL(ftime, 1, ftime, 0);
532 ICUNIT_ASSERT_STRING_EQUAL("01/01/70 13:14:40 Thursday", buffer, 0);
533 LOG("\nresult: %s, expected : %s", buffer, "01/01/70 13:14:40 Thursday");
534
535 ftime = strftime(buffer, 80, "%x %X", tmTime);
536 ICUNIT_ASSERT_WITHIN_EQUAL(ftime, 1, ftime, 0);
537 ICUNIT_ASSERT_STRING_EQUAL("01/01/70 13:14:40", buffer, 0);
538 LOG("\nresult: %s, expected : %s", buffer, "01/01/70 13:14:40");
539
540 ftime = strftime(buffer, 80, "%D %A %H:%M:%S", tmTime);
541 ICUNIT_ASSERT_WITHIN_EQUAL(ftime, 1, ftime, 0);
542 ICUNIT_ASSERT_STRING_EQUAL("01/01/70 Thursday 13:14:40", buffer, 0);
543 LOG("\nresult: %s, expected : %s", buffer, "01/01/70 Thursday 13:14:40");
544 return 0;
545 };
546
547 /* *
548 * @tc.number SUB_KERNEL_TIME_STRFTIME_003
549 * @tc.name test strftime api for abnormal input
550 * @tc.desc [C- SOFTWARE -0200]
551 */
552 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeStrftime003, Function | MediumTest | Level3)
553 {
554 char buffer[80];
555 time_t mtime = 18880;
556 size_t ftime = 0;
557 struct tm *tmTime = localtime(&mtime);
558
559 ftime = strftime(buffer, 12, "%Y-%m-%d %H:%M:%S", tmTime);
560 ICUNIT_ASSERT_EQUAL(ftime, 0, ftime);
561 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
562
563 ftime = strftime(buffer, 80, "", tmTime);
564 ICUNIT_ASSERT_EQUAL(ftime, 0, ftime);
565 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
566
567 ftime = strftime(buffer, 19, "%Y-%m-%d %H:%M:%S", tmTime);
568 ICUNIT_ASSERT_EQUAL(ftime, 0, ftime);
569 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
570
571 ftime = strftime(buffer, 20, "%Y-%m-%d %H:%M:%S", tmTime);
572 ICUNIT_ASSERT_EQUAL(ftime, 19, ftime);
573 ICUNIT_ASSERT_STRING_EQUAL("1970-01-01 13:14:40", buffer, 0);
574 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
575
576 tmTime->__tm_zone = "UTC+8";
577 ftime = strftime(buffer, 80, "%F %T %Z", tmTime);
578 ICUNIT_ASSERT_EQUAL(ftime, 20, ftime);
579 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
580 return 0;
581 };
582 #endif
583
584 /* *
585 * @tc.number SUB_KERNEL_TIME_API_TIMES_0100
586 * @tc.name test times basic
587 * @tc.desc [C- SOFTWARE -0200]
588 */
589 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimes, Function | MediumTest | Level1)
590 {
591 const int testClockt = LOSCFG_BASE_CORE_TICK_PER_SECOND;
592 const int msPerClock = OS_SYS_MS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND;
593 struct tms start = { 0 };
594 struct tms end = { 0 };
595 clock_t stTime = times(&start);
596 LOG("start_clock: stTime: %ld", stTime);
597 LOG("start_clock: tms_utime: %ld, tms_stime: %ld, tms_cutime:%ld, tms_cstime:%ld", start.tms_utime,
598 start.tms_stime, start.tms_cutime, start.tms_cstime);
599
600 KeepRun(testClockt * msPerClock);
601
602 clock_t endTime = times(&end);
603 LOG("end_clock: endTime: %ld", endTime);
604 LOG("end_clock: tms_utime: %ld, tms_stime: %ld, tms_cutime:%ld, tms_cstime:%ld", end.tms_utime, end.tms_stime,
605 end.tms_cutime, end.tms_cstime);
606
607 LOG("Real Time: %ld, User Time %ld, System Time %ld\n", (long)(endTime - stTime),
608 (long)(end.tms_utime - start.tms_utime), (long)(end.tms_stime - start.tms_stime));
609
610 if (!CheckValueClose((end.tms_utime - start.tms_utime), testClockt, 0.02)) {
611 ICUNIT_ASSERT_EQUAL(1, 0, 0);
612 }
613 if (!CheckValueClose((endTime - stTime), testClockt, 0.02)) {
614 ICUNIT_ASSERT_EQUAL(1, 0, 0);
615 }
616 return 0;
617 }
618
619 RUN_TEST_SUITE(PosixTimeFuncTestSuite);
620
PosixTimeFuncTest(void)621 void PosixTimeFuncTest(void)
622 {
623 LOG("begin PosixTimeFuncTest....\n");
624
625 RUN_ONE_TESTCASE(testTimeUSleep001);
626 RUN_ONE_TESTCASE(testTimeUSleep002);
627
628 RUN_ONE_TESTCASE(testTimeGmtime001);
629 #if (LOSCFG_LIBC_MUSL == 1)
630 RUN_ONE_TESTCASE(testTimeLocaltime001);
631 RUN_ONE_TESTCASE(testTimeLocaltime002);
632 RUN_ONE_TESTCASE(testTimeLocaltime003);
633 RUN_ONE_TESTCASE(testTimeLocaltimer001);
634 RUN_ONE_TESTCASE(testTimeLocaltimer002);
635 #endif
636 RUN_ONE_TESTCASE(testTimeMktime001);
637 RUN_ONE_TESTCASE(testTimeMktime002);
638 #if (LOSCFG_LIBC_MUSL == 1)
639 RUN_ONE_TESTCASE(testTimeStrftime001);
640 RUN_ONE_TESTCASE(testTimeStrftime002);
641 RUN_ONE_TESTCASE(testTimeStrftime003);
642 #endif
643 RUN_ONE_TESTCASE(testTimes);
644 return;
645 }
646