blob: c65d011f0f2d99c428c86bf17ff97c85d72cbc74 [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 Yu8beb2502023-05-06 11:55:22 +080013#if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
14 defined(__MINGW32__) || defined(_WIN64)
Jerry Yud1190a52023-04-18 16:29:02 +080015#include <windows.h>
16#elif _POSIX_C_SOURCE >= 199309L
17#include <time.h>
18#else
19#include <unistd.h>
20#endif
21void sleep_ms(int milliseconds)
22{
Jerry Yu8beb2502023-05-06 11:55:22 +080023#if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
24 defined(__MINGW32__) || defined(_WIN64)
Jerry Yud1190a52023-04-18 16:29:02 +080025 Sleep(milliseconds);
26#elif _POSIX_C_SOURCE >= 199309L
27 struct timespec ts;
28 ts.tv_sec = milliseconds / 1000;
29 ts.tv_nsec = (milliseconds % 1000) * 1000000;
30 nanosleep(&ts, NULL);
31#else
32 usleep(milliseconds * 1000);
33#endif
34}
35#endif
36
Jerry Yu9a12df02023-03-03 12:55:16 +080037/* END_HEADER */
38
Jerry Yuc5b48a62023-03-13 14:28:06 +080039/* BEGIN_DEPENDENCIES */
40
41/* END_DEPENDENCIES */
42
Jerry Yu9a12df02023-03-03 12:55:16 +080043/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
44void time_get_milliseconds()
45{
Jerry Yue7ea8232023-03-14 17:33:42 +080046 mbedtls_ms_time_t current = mbedtls_ms_time();
Jerry Yu9a12df02023-03-03 12:55:16 +080047 (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{
Jerry Yue7ea8232023-03-14 17:33:42 +080056 mbedtls_time_t current = mbedtls_time(NULL);
Jerry Yu9a12df02023-03-03 12:55:16 +080057 (void) current;
58 /* This goto is added to avoid warnings from the generated code. */
59 goto exit;
60}
61/* END_CASE */
Jerry Yud1190a52023-04-18 16:29:02 +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 mbedtls_ms_time_t elapsed_ms;
68
Jerry Yu484a9e12023-05-05 17:03:29 +080069 /*
70 * WARNING: DO NOT ENABLE THIS TEST. We keep the code here to document the
71 * reason.
72 *
73 * Windows CI reports random test fail on platform-suite. It might
74 * be caused by this case.
75 */
Jerry Yud1190a52023-04-18 16:29:02 +080076 sleep_ms(delay_ms);
77
78 elapsed_ms = mbedtls_ms_time() - current;
79 TEST_ASSERT(elapsed_ms >= delay_ms && elapsed_ms < 4000 + delay_ms);
80 /* This goto is added to avoid warnings from the generated code. */
81 goto exit;
82}
83/* END_CASE */
84
85/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
Jerry Yued9b9a72023-04-18 17:09:03 +080086
87/*
Jerry Yuad2091d2023-04-20 10:01:42 +080088 * WARNING: DO NOT ENABLE THIS TEST. We keep the code here to document the
89 * reason.
Jerry Yued9b9a72023-04-18 17:09:03 +080090 *
91 * The test often failed on the CI. See #1517. CI failures cannot be
92 * completely avoided due to out-of-sync clock sources.
93 */
Jerry Yud1190a52023-04-18 16:29:02 +080094void time_delay_seconds(int delay_secs)
95{
96 mbedtls_time_t current = mbedtls_time(NULL);
97 mbedtls_time_t elapsed_secs;
98
99 sleep_ms(delay_secs * 1000);
100
101 elapsed_secs = mbedtls_time(NULL) - current;
102
103 /*
Jerry Yud3c7d532023-04-19 14:07:59 +0800104 * `mbedtls_time()` was defined as c99 function `time()`, returns the number
105 * of seconds since the Epoch. And it is affected by discontinuous changes
106 * from automatic drift adjustment or time setting system call. The POSIX.1
107 * specification for clock_settime says that discontinuous changes in
108 * CLOCK_REALTIME should not affect `nanosleep()`.
Jerry Yud1190a52023-04-18 16:29:02 +0800109 *
Jerry Yud3c7d532023-04-19 14:07:59 +0800110 * If discontinuous changes occur during `nanosleep()`, we will get
Jerry Yuad2091d2023-04-20 10:01:42 +0800111 * `elapsed_secs < delay_secs` for backward or `elapsed_secs > delay_secs`
Jerry Yud3c7d532023-04-19 14:07:59 +0800112 * for forward.
Jerry Yud1190a52023-04-18 16:29:02 +0800113 *
Jerry Yud3c7d532023-04-19 14:07:59 +0800114 * The following tolerance windows cannot be guaranteed.
115 * PLEASE DO NOT ENABLE IT IN CI TEST.
Jerry Yud1190a52023-04-18 16:29:02 +0800116 */
Jerry Yud3c7d532023-04-19 14:07:59 +0800117 TEST_ASSERT(elapsed_secs - delay_secs >= -1 &&
118 elapsed_secs - delay_secs < 4);
Jerry Yud1190a52023-04-18 16:29:02 +0800119 /* This goto is added to avoid warnings from the generated code. */
120 goto exit;
121}
122/* END_CASE */
Andrzej Kurek60de0b12023-05-09 16:38:04 -0400123
124/* BEGIN_CASE */
125void check_mbedtls_calloc_overallocation(intmax_t num, intmax_t size)
126{
127 unsigned char *buf;
128 buf = mbedtls_calloc((size_t) num, (size_t) size);
Andrzej Kurekcf669b02023-07-03 09:49:07 -0400129 /* Dummy usage of the pointer to prevent optimizing it */
130 mbedtls_printf("calloc pointer : %p\n", buf);
Andrzej Kurek60de0b12023-05-09 16:38:04 -0400131 TEST_ASSERT(buf == NULL);
132
133exit:
134 mbedtls_free(buf);
135}
136/* END_CASE */