blob: 478c52da7b6e4f84142ddabace052d13e29da59c [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
13#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}
Jerry Yu9a12df02023-03-03 12:55:16 +080033#endif
34
35/* END_HEADER */
36
Jerry Yuc5b48a62023-03-13 14:28:06 +080037/* BEGIN_DEPENDENCIES */
38
39/* END_DEPENDENCIES */
40
41
Jerry Yu9a12df02023-03-03 12:55:16 +080042
43/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
44void time_get_milliseconds()
45{
46 mbedtls_ms_time_t current = mbedtls_ms_time();
47 (void) current;
48 /* This goto is added to avoid warnings from the generated code. */
49 goto exit;
50}
51/* END_CASE */
52
53/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
54void time_get_seconds()
55{
56 mbedtls_time_t current = mbedtls_time(NULL);
57 (void) current;
58 /* This goto is added to avoid warnings from the generated code. */
59 goto exit;
60}
61/* END_CASE */
Jerry Yuc5b48a62023-03-13 14:28:06 +080062
63/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
64void time_delay_milliseconds(int delay_ms)
65{
66 mbedtls_ms_time_t current = mbedtls_ms_time();
67
68 sleep_ms(delay_ms);
69
70 current = mbedtls_ms_time() - current;
71 TEST_ASSERT(current == delay_ms || current == delay_ms + 1);
72 /* This goto is added to avoid warnings from the generated code. */
73 goto exit;
74}
75/* END_CASE */
76
77/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
78void time_delay_seconds(int delay)
79{
80 mbedtls_time_t current = mbedtls_time(NULL);
81 sleep_ms(delay*1000);
82 current = mbedtls_time(NULL) - current;
83 TEST_ASSERT(current == delay);
84 /* This goto is added to avoid warnings from the generated code. */
85 goto exit;
86}
87/* END_CASE */