mbedtls/tests/suites/test_suite_platform.function
Jerry Yu c5b48a6f04 Add time test with delay
Signed-off-by: Jerry Yu <jerry.h.yu@arm.com>
2023-03-13 15:25:44 +08:00

87 lines
2 KiB
C

/* BEGIN_HEADER */
/* This test module exercises the platform_* module. Since, depending on the
* underlying operating system, the time routines are not always reliable,
* this suite only performs very basic sanity checks of the timing API.
*/
#include <limits.h>
#if defined(MBEDTLS_HAVE_TIME)
#include "mbedtls/platform_time.h"
#ifdef WIN32
#include <windows.h>
#elif _POSIX_C_SOURCE >= 199309L
#include <time.h>
#else
#include <unistd.h>
#endif
void sleep_ms(int milliseconds)
{
#ifdef WIN32
Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
usleep(milliseconds * 1000);
#endif
}
#endif
/* END_HEADER */
/* BEGIN_DEPENDENCIES */
/* END_DEPENDENCIES */
/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
void time_get_milliseconds()
{
mbedtls_ms_time_t current = mbedtls_ms_time();
(void) current;
/* This goto is added to avoid warnings from the generated code. */
goto exit;
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
void time_get_seconds()
{
mbedtls_time_t current = mbedtls_time(NULL);
(void) current;
/* This goto is added to avoid warnings from the generated code. */
goto exit;
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
void time_delay_milliseconds(int delay_ms)
{
mbedtls_ms_time_t current = mbedtls_ms_time();
sleep_ms(delay_ms);
current = mbedtls_ms_time() - current;
TEST_ASSERT(current == delay_ms || current == delay_ms + 1);
/* This goto is added to avoid warnings from the generated code. */
goto exit;
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
void time_delay_seconds(int delay)
{
mbedtls_time_t current = mbedtls_time(NULL);
sleep_ms(delay*1000);
current = mbedtls_time(NULL) - current;
TEST_ASSERT(current == delay);
/* This goto is added to avoid warnings from the generated code. */
goto exit;
}
/* END_CASE */