blob: 3d0f52bd177761d707050add2438c70480eb1711 [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
67 sleep_ms(delay_ms);
68
69 elapsed_ms = mbedtls_ms_time() - current;
70 TEST_ASSERT(elapsed_ms >= delay_ms && elapsed_ms < 4000 + delay_ms);
71 /* This goto is added to avoid warnings from the generated code. */
72 goto exit;
73}
74/* END_CASE */
75
76/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
Jerry Yued9b9a72023-04-18 17:09:03 +080077
78/*
79 * WARNING: DONOT ENABLE THIS TEST. RESERVE IT HERE TO KEEP THE REASON.
80 *
81 * The test often failed on the CI. See #1517. CI failures cannot be
82 * completely avoided due to out-of-sync clock sources.
83 */
Jerry Yud1190a52023-04-18 16:29:02 +080084void time_delay_seconds(int delay_secs)
85{
86 mbedtls_time_t current = mbedtls_time(NULL);
87 mbedtls_time_t elapsed_secs;
88
89 sleep_ms(delay_secs * 1000);
90
91 elapsed_secs = mbedtls_time(NULL) - current;
92
93 /*
Jerry Yud3c7d532023-04-19 14:07:59 +080094 * `mbedtls_time()` was defined as c99 function `time()`, returns the number
95 * of seconds since the Epoch. And it is affected by discontinuous changes
96 * from automatic drift adjustment or time setting system call. The POSIX.1
97 * specification for clock_settime says that discontinuous changes in
98 * CLOCK_REALTIME should not affect `nanosleep()`.
Jerry Yud1190a52023-04-18 16:29:02 +080099 *
Jerry Yud3c7d532023-04-19 14:07:59 +0800100 * If discontinuous changes occur during `nanosleep()`, we will get
101 * `elapsed_secs < delay_secs` for backward and `elapsed_secs > delay_secs`
102 * for forward.
Jerry Yud1190a52023-04-18 16:29:02 +0800103 *
Jerry Yud3c7d532023-04-19 14:07:59 +0800104 * The following tolerance windows cannot be guaranteed.
105 * PLEASE DO NOT ENABLE IT IN CI TEST.
Jerry Yud1190a52023-04-18 16:29:02 +0800106 */
Jerry Yud3c7d532023-04-19 14:07:59 +0800107 TEST_ASSERT(elapsed_secs - delay_secs >= -1 &&
108 elapsed_secs - delay_secs < 4);
Jerry Yud1190a52023-04-18 16:29:02 +0800109 /* This goto is added to avoid warnings from the generated code. */
110 goto exit;
111}
112/* END_CASE */