blob: 54d394470aae6c0560722ab412457d483a211414 [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 */
77void time_delay_seconds(int delay_secs)
78{
79 mbedtls_time_t current = mbedtls_time(NULL);
80 mbedtls_time_t elapsed_secs;
81
82 sleep_ms(delay_secs * 1000);
83
84 elapsed_secs = mbedtls_time(NULL) - current;
85
86 /*
87 * `mbedtls_time` was defined as c99 function `time`, it uses
88 * CLOCK_REALTIME and returns the number of seconds since the Epoch. And
89 * `nanosleep` uses CLOCK_MONOTONIC. The time sources are out of sync.
90 *
91 * If CLOCK_MONOTONIC is faster than CLOCK_REALTIME and `nanosleep` exits at
92 * the end of a second, `elapsed_secs` will be less than `delay_secs`.
93 *
94 * Workaround it with 1 second tolerance.
95 */
96 TEST_ASSERT(elapsed_secs >= delay_secs - 1);
97 /* If CLOCK_MONOTONIC is slower than CLOCK_REALTIME or nanosleep does not
98 * exit on time.
99 */
100 TEST_ASSERT(elapsed_secs < 4 + delay_secs);
101 /* This goto is added to avoid warnings from the generated code. */
102 goto exit;
103}
104/* END_CASE */