blob: 6ae1026780f1e9ddea1412e7936bcd63fef3fb2b [file] [log] [blame]
Jerry Yu9a12df02023-03-03 12:55:16 +08001/* BEGIN_HEADER */
2
3/* This test module exercises the platform_* module. Since, depending on the
4 * underlying operating system, the time routines are not always reliable,
5 * this suite only performs very basic sanity checks of the timing API.
6 */
7
8#include <limits.h>
9
10#if defined(MBEDTLS_HAVE_TIME)
11#include "mbedtls/platform_time.h"
Jerry Yuc5b48a62023-03-13 14:28:06 +080012
Jerry Yud1190a52023-04-18 16:29:02 +080013#ifdef WIN32
14#include <windows.h>
15#elif _POSIX_C_SOURCE >= 199309L
16#include <time.h>
17#else
18#include <unistd.h>
19#endif
20void sleep_ms(int milliseconds)
21{
22#ifdef WIN32
23 Sleep(milliseconds);
24#elif _POSIX_C_SOURCE >= 199309L
25 struct timespec ts;
26 ts.tv_sec = milliseconds / 1000;
27 ts.tv_nsec = (milliseconds % 1000) * 1000000;
28 nanosleep(&ts, NULL);
29#else
30 usleep(milliseconds * 1000);
31#endif
32}
33#endif
34
Jerry Yu9a12df02023-03-03 12:55:16 +080035/* END_HEADER */
36
Jerry Yuc5b48a62023-03-13 14:28:06 +080037/* BEGIN_DEPENDENCIES */
38
39/* END_DEPENDENCIES */
40
Jerry Yu9a12df02023-03-03 12:55:16 +080041/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
42void time_get_milliseconds()
43{
Jerry Yue7ea8232023-03-14 17:33:42 +080044 mbedtls_ms_time_t current = mbedtls_ms_time();
Jerry Yu9a12df02023-03-03 12:55:16 +080045 (void) current;
46 /* This goto is added to avoid warnings from the generated code. */
47 goto exit;
48}
49/* END_CASE */
50
51/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
52void time_get_seconds()
53{
Jerry Yue7ea8232023-03-14 17:33:42 +080054 mbedtls_time_t current = mbedtls_time(NULL);
Jerry Yu9a12df02023-03-03 12:55:16 +080055 (void) current;
56 /* This goto is added to avoid warnings from the generated code. */
57 goto exit;
58}
59/* END_CASE */
Jerry Yud1190a52023-04-18 16:29:02 +080060
61/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
62void time_delay_milliseconds(int delay_ms)
63{
64 mbedtls_ms_time_t current = mbedtls_ms_time();
65 mbedtls_ms_time_t elapsed_ms;
66
Jerry Yu484a9e12023-05-05 17:03:29 +080067 /*
68 * WARNING: DO NOT ENABLE THIS TEST. We keep the code here to document the
69 * reason.
70 *
71 * Windows CI reports random test fail on platform-suite. It might
72 * be caused by this case.
73 */
Jerry Yud1190a52023-04-18 16:29:02 +080074 sleep_ms(delay_ms);
75
76 elapsed_ms = mbedtls_ms_time() - current;
77 TEST_ASSERT(elapsed_ms >= delay_ms && elapsed_ms < 4000 + delay_ms);
78 /* This goto is added to avoid warnings from the generated code. */
79 goto exit;
80}
81/* END_CASE */
82
83/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
Jerry Yued9b9a72023-04-18 17:09:03 +080084
85/*
Jerry Yuad2091d2023-04-20 10:01:42 +080086 * WARNING: DO NOT ENABLE THIS TEST. We keep the code here to document the
87 * reason.
Jerry Yued9b9a72023-04-18 17:09:03 +080088 *
89 * The test often failed on the CI. See #1517. CI failures cannot be
90 * completely avoided due to out-of-sync clock sources.
91 */
Jerry Yud1190a52023-04-18 16:29:02 +080092void time_delay_seconds(int delay_secs)
93{
94 mbedtls_time_t current = mbedtls_time(NULL);
95 mbedtls_time_t elapsed_secs;
96
97 sleep_ms(delay_secs * 1000);
98
99 elapsed_secs = mbedtls_time(NULL) - current;
100
101 /*
Jerry Yud3c7d532023-04-19 14:07:59 +0800102 * `mbedtls_time()` was defined as c99 function `time()`, returns the number
103 * of seconds since the Epoch. And it is affected by discontinuous changes
104 * from automatic drift adjustment or time setting system call. The POSIX.1
105 * specification for clock_settime says that discontinuous changes in
106 * CLOCK_REALTIME should not affect `nanosleep()`.
Jerry Yud1190a52023-04-18 16:29:02 +0800107 *
Jerry Yud3c7d532023-04-19 14:07:59 +0800108 * If discontinuous changes occur during `nanosleep()`, we will get
Jerry Yuad2091d2023-04-20 10:01:42 +0800109 * `elapsed_secs < delay_secs` for backward or `elapsed_secs > delay_secs`
Jerry Yud3c7d532023-04-19 14:07:59 +0800110 * for forward.
Jerry Yud1190a52023-04-18 16:29:02 +0800111 *
Jerry Yud3c7d532023-04-19 14:07:59 +0800112 * The following tolerance windows cannot be guaranteed.
113 * PLEASE DO NOT ENABLE IT IN CI TEST.
Jerry Yud1190a52023-04-18 16:29:02 +0800114 */
Jerry Yud3c7d532023-04-19 14:07:59 +0800115 TEST_ASSERT(elapsed_secs - delay_secs >= -1 &&
116 elapsed_secs - delay_secs < 4);
Jerry Yud1190a52023-04-18 16:29:02 +0800117 /* This goto is added to avoid warnings from the generated code. */
118 goto exit;
119}
120/* END_CASE */