2023-03-03 05:55:16 +01:00
|
|
|
/* 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"
|
2023-03-13 07:28:06 +01:00
|
|
|
|
2023-04-18 10:29:02 +02:00
|
|
|
#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
|
|
|
|
|
2023-03-03 05:55:16 +01:00
|
|
|
/* END_HEADER */
|
|
|
|
|
2023-03-13 07:28:06 +01:00
|
|
|
/* BEGIN_DEPENDENCIES */
|
|
|
|
|
|
|
|
/* END_DEPENDENCIES */
|
|
|
|
|
2023-03-03 05:55:16 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
|
|
|
|
void time_get_milliseconds()
|
|
|
|
{
|
2023-03-14 10:33:42 +01:00
|
|
|
mbedtls_ms_time_t current = mbedtls_ms_time();
|
2023-03-03 05:55:16 +01:00
|
|
|
(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()
|
|
|
|
{
|
2023-03-14 10:33:42 +01:00
|
|
|
mbedtls_time_t current = mbedtls_time(NULL);
|
2023-03-03 05:55:16 +01:00
|
|
|
(void) current;
|
|
|
|
/* This goto is added to avoid warnings from the generated code. */
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2023-04-18 10:29:02 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
|
|
|
|
void time_delay_milliseconds(int delay_ms)
|
|
|
|
{
|
|
|
|
mbedtls_ms_time_t current = mbedtls_ms_time();
|
|
|
|
mbedtls_ms_time_t elapsed_ms;
|
|
|
|
|
|
|
|
sleep_ms(delay_ms);
|
|
|
|
|
|
|
|
elapsed_ms = mbedtls_ms_time() - current;
|
|
|
|
TEST_ASSERT(elapsed_ms >= delay_ms && elapsed_ms < 4000 + delay_ms);
|
|
|
|
/* This goto is added to avoid warnings from the generated code. */
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
|
2023-04-18 11:09:03 +02:00
|
|
|
|
|
|
|
/*
|
2023-04-20 04:01:42 +02:00
|
|
|
* WARNING: DO NOT ENABLE THIS TEST. We keep the code here to document the
|
|
|
|
* reason.
|
2023-04-18 11:09:03 +02:00
|
|
|
*
|
|
|
|
* The test often failed on the CI. See #1517. CI failures cannot be
|
|
|
|
* completely avoided due to out-of-sync clock sources.
|
|
|
|
*/
|
2023-04-18 10:29:02 +02:00
|
|
|
void time_delay_seconds(int delay_secs)
|
|
|
|
{
|
|
|
|
mbedtls_time_t current = mbedtls_time(NULL);
|
|
|
|
mbedtls_time_t elapsed_secs;
|
|
|
|
|
|
|
|
sleep_ms(delay_secs * 1000);
|
|
|
|
|
|
|
|
elapsed_secs = mbedtls_time(NULL) - current;
|
|
|
|
|
|
|
|
/*
|
2023-04-19 08:07:59 +02:00
|
|
|
* `mbedtls_time()` was defined as c99 function `time()`, returns the number
|
|
|
|
* of seconds since the Epoch. And it is affected by discontinuous changes
|
|
|
|
* from automatic drift adjustment or time setting system call. The POSIX.1
|
|
|
|
* specification for clock_settime says that discontinuous changes in
|
|
|
|
* CLOCK_REALTIME should not affect `nanosleep()`.
|
2023-04-18 10:29:02 +02:00
|
|
|
*
|
2023-04-19 08:07:59 +02:00
|
|
|
* If discontinuous changes occur during `nanosleep()`, we will get
|
2023-04-20 04:01:42 +02:00
|
|
|
* `elapsed_secs < delay_secs` for backward or `elapsed_secs > delay_secs`
|
2023-04-19 08:07:59 +02:00
|
|
|
* for forward.
|
2023-04-18 10:29:02 +02:00
|
|
|
*
|
2023-04-19 08:07:59 +02:00
|
|
|
* The following tolerance windows cannot be guaranteed.
|
|
|
|
* PLEASE DO NOT ENABLE IT IN CI TEST.
|
2023-04-18 10:29:02 +02:00
|
|
|
*/
|
2023-04-19 08:07:59 +02:00
|
|
|
TEST_ASSERT(elapsed_secs - delay_secs >= -1 &&
|
|
|
|
elapsed_secs - delay_secs < 4);
|
2023-04-18 10:29:02 +02:00
|
|
|
/* This goto is added to avoid warnings from the generated code. */
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|