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