blob: 8dd4098f3aceb0ee1e6bd7051ddf8b7d48eb30f3 [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 /*
94 * `mbedtls_time` was defined as c99 function `time`, it uses
95 * CLOCK_REALTIME and returns the number of seconds since the Epoch. And
96 * `nanosleep` uses CLOCK_MONOTONIC. The time sources are out of sync.
97 *
98 * If CLOCK_MONOTONIC is faster than CLOCK_REALTIME and `nanosleep` exits at
99 * the end of a second, `elapsed_secs` will be less than `delay_secs`.
100 *
101 * Workaround it with 1 second tolerance.
102 */
103 TEST_ASSERT(elapsed_secs >= delay_secs - 1);
104 /* If CLOCK_MONOTONIC is slower than CLOCK_REALTIME or nanosleep does not
105 * exit on time.
106 */
107 TEST_ASSERT(elapsed_secs < 4 + delay_secs);
108 /* This goto is added to avoid warnings from the generated code. */
109 goto exit;
110}
111/* END_CASE */