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_LOCALTIME_003
293 * @tc.name test settimeofday api
294 * @tc.desc [C- SOFTWARE -0200]
295 */
296 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeLocaltime003, Function | MediumTest | Level1)
297 {
298 char cTime[32]; /* 32, no special meaning */
299 time_t tStart;
300 time_t tEnd;
301 struct timezone tz;
302 struct timeval timeSet = {
303 .tv_sec = 86399, /* 86399, no special meaning */
304 .tv_usec = 0
305 };
306
307 int ret = gettimeofday(NULL, &tz);
308 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
309
310 ret = settimeofday(&timeSet, &tz);
311 time(&tStart);
312 sleep(2); /* 2, no special meaning */
313 time(&tEnd);
314 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
315
316 struct tm *tmStart = localtime(&tStart);
317 strftime(cTime, sizeof(cTime), "%H:%M:%S", tmStart);
318 ICUNIT_ASSERT_STRING_EQUAL(cTime, "07:59:59", 0);
319 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
320 struct tm *tmEnd = localtime(&tEnd);
321 strftime(cTime, sizeof(cTime), "%H:%M:%S", tmEnd);
322 ICUNIT_ASSERT_STRING_EQUAL(cTime, "08:00:01", 0);
323 LOG("\n time_t=%lld, first time:%s", tEnd, cTime);
324 return 0;
325 }
326
327 /* *
328 * @tc.number SUB_KERNEL_TIME_LOCALTIMER_001
329 * @tc.name localtime_r api base test
330 * @tc.desc [C- SOFTWARE -0200]
331 */
332 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeLocaltimer001, Function | MediumTest | Level1)
333 {
334 char cTime[32];
335 time_t tStart;
336 time_t tEnd;
337 struct tm tmrStart = { 0 };
338 struct tm tmrEnd = { 0 };
339 struct timeval tSet = {
340 .tv_sec = 86399,
341 .tv_usec = 0
342 };
343
344 int ret = settimeofday(&tSet, NULL);
345 time(&tStart);
346 sleep(2);
347 time(&tEnd);
348 TEST_ASSERT_EQUAL_INT(0, ret);
349
350 struct tm *tmrStartPtr = localtime_r(&tStart, &tmrStart);
351 struct tm *tmrEndPtr = localtime_r(&tEnd, &tmrEnd);
352
353 strftime(cTime, sizeof(cTime), "%H:%M:%S", &tmrStart);
354 TEST_ASSERT_EQUAL_STRING("07:59:59", cTime);
355 strftime(cTime, sizeof(cTime), "%H:%M:%S", tmrStartPtr);
356 TEST_ASSERT_EQUAL_STRING("07:59:59", cTime);
357 strftime(cTime, sizeof(cTime), "%H:%M:%S", &tmrEnd);
358 TEST_ASSERT_EQUAL_STRING("08:00:01", cTime);
359 strftime(cTime, sizeof(cTime), "%H:%M:%S", tmrEndPtr);
360 TEST_ASSERT_EQUAL_STRING("08:00:01", cTime);
361 return 0;
362 }
363
364 /* *
365 * @tc.number SUB_KERNEL_TIME_LOCALTIMER_002
366 * @tc.name test localtime_r api for range
367 * @tc.desc [C- SOFTWARE -0200]
368 */
369 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeLocaltimer002, Function | MediumTest | Level1)
370 {
371 char cTime[32];
372 struct tm tmrResult = { 0 };
373
374 time_t tStart = LONG_MAX;
375 struct tm *tmStart = localtime_r(&tStart, &tmrResult);
376 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
377 TEST_ASSERT_EQUAL_STRING("38-01-19 11:14:07", cTime);
378 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
379
380 tStart = LONG_MIN;
381 tmStart = localtime_r(&tStart, &tmrResult);
382 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
383 TEST_ASSERT_EQUAL_STRING("01-12-14 04:45:52", cTime);
384 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
385
386 tStart = 0;
387 tmStart = localtime_r(&tStart, &tmrResult);
388 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
389 TEST_ASSERT_EQUAL_STRING("70-01-01 08:00:00", cTime);
390 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
391
392 tStart = -1;
393 tmStart = localtime_r(&tStart, &tmrResult);
394 strftime(cTime, sizeof(cTime), "%y-%m-%d %H:%M:%S", tmStart);
395 TEST_ASSERT_EQUAL_STRING("70-01-01 07:59:59", cTime);
396 LOG("\n time_t=%lld, first time:%s", tStart, cTime);
397 return 0;
398 }
399 #endif
400
401 /* *
402 * @tc.number SUB_KERNEL_TIME_MKTIME_001
403 * @tc.name mktime api base test
404 * @tc.desc [C- SOFTWARE -0200]
405 */
406 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeMktime001, Function | MediumTest | Level1)
407 {
408 // default time zone east 8
409 struct tm timeptr = { 0 };
410 time_t testTime = 18880;
411 char timeStr[TIME_STR_LEN] = {0};
412
413 INIT_TM(timeptr, 2020, 7, 9, 18, 10, 0, 7);
414 time_t timeRet = mktime(&timeptr);
415 LOG("\n 2020-7-9 18:10:00, mktime Ret = %lld", timeRet);
416 TEST_ASSERT_EQUAL_INT(1596996600, timeRet);
417
418 INIT_TM(timeptr, 1970, 0, 1, 8, 0, 0, 0);
419 timeRet = mktime(&timeptr);
420 LOG("\n 1970-1-1 08:00:00, mktime Ret = %lld", timeRet);
421 TEST_ASSERT_EQUAL_INT(28800, timeRet);
422
423 struct tm *stm = localtime(&testTime);
424 LOG("\n testTime 18880, tm : %s", TmToStr(stm, timeStr, TIME_STR_LEN));
425 timeRet = mktime(stm);
426 TEST_ASSERT_EQUAL_INT(18880, timeRet);
427 LOG("\n input 18880, mktime Ret = %lld", timeRet);
428
429 testTime = LONG_MAX;
430 stm = localtime(&testTime);
431 LOG("\n testTime LONG_MAX, tm : %s", TmToStr(stm, timeStr, TIME_STR_LEN));
432 timeRet = mktime(stm);
433 TEST_ASSERT_EQUAL_INT(LONG_MAX, timeRet);
434 LOG("\n input LONG_MAX, mktime Ret = %lld", timeRet);
435
436 testTime = 0;
437 stm = localtime(&testTime);
438 LOG("\n testTime 0, tm : %s", TmToStr(stm, timeStr, TIME_STR_LEN));
439 timeRet = mktime(stm);
440 TEST_ASSERT_EQUAL_INT(0, timeRet);
441 LOG("\n input 0, mktime Ret = %lld", timeRet);
442 return 0;
443 }
444
445 /* *
446 * @tc.number SUB_KERNEL_TIME_MKTIME_002
447 * @tc.name mktime api test for invalid input
448 * @tc.desc [C- SOFTWARE -0200]
449 */
450 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeMktime002, Function | MediumTest | Level1)
451 {
452 struct tm timeptr = { 0 };
453 LOG("\n sizeof(time_t) = %d", sizeof(time_t));
454 INIT_TM(timeptr, 1969, 7, 9, 10, 10, 0, 7);
455 time_t timeRet = mktime(&timeptr);
456 LOG("\n 1800-8-9 10:10:00, mktime Ret lld = %lld", timeRet);
457 #if (LOSCFG_LIBC_MUSL == 1)
458 TEST_ASSERT_EQUAL_INT(-1, timeRet);
459 #endif
460 #if (LOSCFG_LIBC_NEWLIB == 1)
461 TEST_ASSERT_LESS_THAN_INT(0, timeRet);
462 #endif
463 return 0;
464 }
465
466 #if (LOSCFG_LIBC_MUSL == 1)
467 /* *
468 * @tc.number SUB_KERNEL_TIME_STRFTIME_001
469 * @tc.name test strftime api
470 * @tc.desc [C- SOFTWARE -0200]
471 */
472 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeStrftime001, Function | MediumTest | Level3)
473 {
474 char buffer[80];
475 time_t mtime = 0;
476 size_t ftime = 0;
477
478 mtime = LONG_MAX;
479 ftime = strftime(buffer, 80, "%y-%m-%d %H:%M:%S", localtime(&mtime));
480 TEST_ASSERT_GREATER_THAN_INT(0, ftime);
481 TEST_ASSERT_EQUAL_STRING("38-01-19 11:14:07", buffer);
482 LOG("\nresult: %s, expected : %s", buffer, "38-01-19 11:14:07");
483
484 mtime = LONG_MIN;
485 ftime = strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtime(&mtime));
486 TEST_ASSERT_GREATER_THAN_INT(0, ftime);
487 TEST_ASSERT_EQUAL_STRING("1901-12-14 04:45:52", buffer);
488 LOG("\nresult: %s, expected : %s", buffer, "1901-12-14 04:45:52");
489
490 mtime = 18880;
491 ftime = strftime(buffer, 80, "%F %T", localtime(&mtime));
492 TEST_ASSERT_GREATER_THAN_INT(0, ftime);
493 TEST_ASSERT_EQUAL_STRING("1970-01-01 13:14:40", buffer);
494 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
495
496 mtime = 18880;
497 ftime = strftime(buffer, 80, "%D %w %H:%M:%S", localtime(&mtime));
498 TEST_ASSERT_GREATER_THAN_INT(0, ftime);
499 TEST_ASSERT_EQUAL_STRING("01/01/70 4 13:14:40", buffer);
500 LOG("\nresult: %s, expected : %s", buffer, "01/01/70 4 13:14:40");
501 return 0;
502 };
503
504 /* *
505 * @tc.number SUB_KERNEL_TIME_STRFTIME_002
506 * @tc.name test strftime api base case
507 * @tc.desc [C- SOFTWARE -0200]
508 */
509 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeStrftime002, Function | MediumTest | Level3)
510 {
511 char buffer[80];
512 time_t mtime = 18880;
513 size_t ftime = 0;
514 struct tm *tmTime = localtime(&mtime);
515
516 ftime = strftime(buffer, 80, "%Ex %EX %A", tmTime);
517 TEST_ASSERT_GREATER_THAN_INT(0, ftime);
518 TEST_ASSERT_EQUAL_STRING("01/01/70 13:14:40 Thursday", buffer);
519 LOG("\nresult: %s, expected : %s", buffer, "01/01/70 13:14:40 Thursday");
520
521 ftime = strftime(buffer, 80, "%x %X", tmTime);
522 TEST_ASSERT_GREATER_THAN_INT(0, ftime);
523 TEST_ASSERT_EQUAL_STRING("01/01/70 13:14:40", buffer);
524 LOG("\nresult: %s, expected : %s", buffer, "01/01/70 13:14:40");
525
526 ftime = strftime(buffer, 80, "%D %A %H:%M:%S", tmTime);
527 TEST_ASSERT_GREATER_THAN_INT(0, ftime);
528 TEST_ASSERT_EQUAL_STRING("01/01/70 Thursday 13:14:40", buffer);
529 LOG("\nresult: %s, expected : %s", buffer, "01/01/70 Thursday 13:14:40");
530 return 0;
531 };
532
533 /* *
534 * @tc.number SUB_KERNEL_TIME_STRFTIME_003
535 * @tc.name test strftime api for abnormal input
536 * @tc.desc [C- SOFTWARE -0200]
537 */
538 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimeStrftime003, Function | MediumTest | Level3)
539 {
540 char buffer[80];
541 time_t mtime = 18880;
542 size_t ftime = 0;
543 struct tm *tmTime = localtime(&mtime);
544
545 ftime = strftime(buffer, 12, "%Y-%m-%d %H:%M:%S", tmTime);
546 TEST_ASSERT_EQUAL_INT(0, ftime);
547 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
548
549 ftime = strftime(buffer, 80, "", tmTime);
550 TEST_ASSERT_EQUAL_INT(0, ftime);
551 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
552
553 ftime = strftime(buffer, 19, "%Y-%m-%d %H:%M:%S", tmTime);
554 TEST_ASSERT_EQUAL_INT(0, ftime);
555 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
556
557 ftime = strftime(buffer, 20, "%Y-%m-%d %H:%M:%S", tmTime);
558 TEST_ASSERT_EQUAL_INT(19, ftime);
559 TEST_ASSERT_EQUAL_STRING("1970-01-01 13:14:40", buffer);
560 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
561
562 tmTime->__tm_zone = "UTC+8";
563 ftime = strftime(buffer, 80, "%F %T %Z", tmTime);
564 TEST_ASSERT_EQUAL_INT(20, ftime);
565 LOG("\nresult: %s, expected : %s", buffer, "1970-01-01 13:14:40");
566 return 0;
567 };
568 #endif
569
570 /* *
571 * @tc.number SUB_KERNEL_TIME_API_TIMES_0100
572 * @tc.name test times basic
573 * @tc.desc [C- SOFTWARE -0200]
574 */
575 LITE_TEST_CASE(PosixTimeFuncTestSuite, testTimes, Function | MediumTest | Level1)
576 {
577 const int testClockt = LOSCFG_BASE_CORE_TICK_PER_SECOND;
578 const int msPerClock = OS_SYS_MS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND;
579 struct tms start = { 0 };
580 struct tms end = { 0 };
581 clock_t stTime = times(&start);
582 LOG("start_clock: stTime: %ld", stTime);
583 LOG("start_clock: tms_utime: %ld, tms_stime: %ld, tms_cutime:%ld, tms_cstime:%ld", start.tms_utime,
584 start.tms_stime, start.tms_cutime, start.tms_cstime);
585
586 KeepRun(testClockt * msPerClock);
587
588 clock_t endTime = times(&end);
589 LOG("end_clock: endTime: %ld", endTime);
590 LOG("end_clock: tms_utime: %ld, tms_stime: %ld, tms_cutime:%ld, tms_cstime:%ld", end.tms_utime, end.tms_stime,
591 end.tms_cutime, end.tms_cstime);
592
593 LOG("Real Time: %ld, User Time %ld, System Time %ld\n", (long)(endTime - stTime),
594 (long)(end.tms_utime - start.tms_utime), (long)(end.tms_stime - start.tms_stime));
595
596 if (!CheckValueClose((end.tms_utime - start.tms_utime), testClockt, 0.02)) {
597 TEST_FAIL();
598 }
599 if (!CheckValueClose((endTime - stTime), testClockt, 0.02)) {
600 TEST_FAIL();
601 }
602 return 0;
603 }
604
605 RUN_TEST_SUITE(PosixTimeFuncTestSuite);
606
PosixTimeFuncTest(void)607 void PosixTimeFuncTest(void)
608 {
609 LOG("begin PosixTimeFuncTest....\n");
610
611 RUN_ONE_TESTCASE(testTimeUSleep001);
612 RUN_ONE_TESTCASE(testTimeUSleep002);
613
614 RUN_ONE_TESTCASE(testTimeGmtime001);
615 #if (LOSCFG_LIBC_MUSL == 1)
616 RUN_ONE_TESTCASE(testTimeLocaltime001);
617 RUN_ONE_TESTCASE(testTimeLocaltime002);
618 RUN_ONE_TESTCASE(testTimeLocaltime003);
619 RUN_ONE_TESTCASE(testTimeLocaltimer001);
620 RUN_ONE_TESTCASE(testTimeLocaltimer002);
621 #endif
622 RUN_ONE_TESTCASE(testTimeMktime001);
623 RUN_ONE_TESTCASE(testTimeMktime002);
624 #if (LOSCFG_LIBC_MUSL == 1)
625 RUN_ONE_TESTCASE(testTimeStrftime001);
626 RUN_ONE_TESTCASE(testTimeStrftime002);
627 RUN_ONE_TESTCASE(testTimeStrftime003);
628 #endif
629 RUN_ONE_TESTCASE(testTimes);
630 return;
631 }
632