Jerry Yu | 9a12df0 | 2023-03-03 12:55:16 +0800 | [diff] [blame] | 1 | /* 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 Yu | c5b48a6 | 2023-03-13 14:28:06 +0800 | [diff] [blame] | 12 | |
Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 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 |
| 20 | void 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 Yu | 9a12df0 | 2023-03-03 12:55:16 +0800 | [diff] [blame] | 35 | /* END_HEADER */ |
| 36 | |
Jerry Yu | c5b48a6 | 2023-03-13 14:28:06 +0800 | [diff] [blame] | 37 | /* BEGIN_DEPENDENCIES */ |
| 38 | |
| 39 | /* END_DEPENDENCIES */ |
| 40 | |
Jerry Yu | 9a12df0 | 2023-03-03 12:55:16 +0800 | [diff] [blame] | 41 | /* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */ |
| 42 | void time_get_milliseconds() |
| 43 | { |
Jerry Yu | e7ea823 | 2023-03-14 17:33:42 +0800 | [diff] [blame] | 44 | mbedtls_ms_time_t current = mbedtls_ms_time(); |
Jerry Yu | 9a12df0 | 2023-03-03 12:55:16 +0800 | [diff] [blame] | 45 | (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 */ |
| 52 | void time_get_seconds() |
| 53 | { |
Jerry Yu | e7ea823 | 2023-03-14 17:33:42 +0800 | [diff] [blame] | 54 | mbedtls_time_t current = mbedtls_time(NULL); |
Jerry Yu | 9a12df0 | 2023-03-03 12:55:16 +0800 | [diff] [blame] | 55 | (void) current; |
| 56 | /* This goto is added to avoid warnings from the generated code. */ |
| 57 | goto exit; |
| 58 | } |
| 59 | /* END_CASE */ |
Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 60 | |
| 61 | /* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */ |
| 62 | void 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 */ |
Jerry Yu | ed9b9a7 | 2023-04-18 17:09:03 +0800 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * WARNING: DONOT ENABLE THIS TEST. RESERVE IT HERE TO KEEP THE REASON. |
| 80 | * |
| 81 | * The test often failed on the CI. See #1517. CI failures cannot be |
| 82 | * completely avoided due to out-of-sync clock sources. |
| 83 | */ |
Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 84 | void time_delay_seconds(int delay_secs) |
| 85 | { |
| 86 | mbedtls_time_t current = mbedtls_time(NULL); |
| 87 | mbedtls_time_t elapsed_secs; |
| 88 | |
| 89 | sleep_ms(delay_secs * 1000); |
| 90 | |
| 91 | elapsed_secs = mbedtls_time(NULL) - current; |
| 92 | |
| 93 | /* |
Jerry Yu | d3c7d53 | 2023-04-19 14:07:59 +0800 | [diff] [blame^] | 94 | * `mbedtls_time()` was defined as c99 function `time()`, returns the number |
| 95 | * of seconds since the Epoch. And it is affected by discontinuous changes |
| 96 | * from automatic drift adjustment or time setting system call. The POSIX.1 |
| 97 | * specification for clock_settime says that discontinuous changes in |
| 98 | * CLOCK_REALTIME should not affect `nanosleep()`. |
Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 99 | * |
Jerry Yu | d3c7d53 | 2023-04-19 14:07:59 +0800 | [diff] [blame^] | 100 | * If discontinuous changes occur during `nanosleep()`, we will get |
| 101 | * `elapsed_secs < delay_secs` for backward and `elapsed_secs > delay_secs` |
| 102 | * for forward. |
Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 103 | * |
Jerry Yu | d3c7d53 | 2023-04-19 14:07:59 +0800 | [diff] [blame^] | 104 | * The following tolerance windows cannot be guaranteed. |
| 105 | * PLEASE DO NOT ENABLE IT IN CI TEST. |
Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 106 | */ |
Jerry Yu | d3c7d53 | 2023-04-19 14:07:59 +0800 | [diff] [blame^] | 107 | TEST_ASSERT(elapsed_secs - delay_secs >= -1 && |
| 108 | elapsed_secs - delay_secs < 4); |
Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 109 | /* This goto is added to avoid warnings from the generated code. */ |
| 110 | goto exit; |
| 111 | } |
| 112 | /* END_CASE */ |