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 */ |
| 77 | void time_delay_seconds(int delay_secs) |
| 78 | { |
| 79 | mbedtls_time_t current = mbedtls_time(NULL); |
| 80 | mbedtls_time_t elapsed_secs; |
| 81 | |
| 82 | sleep_ms(delay_secs * 1000); |
| 83 | |
| 84 | elapsed_secs = mbedtls_time(NULL) - current; |
| 85 | |
| 86 | /* |
| 87 | * `mbedtls_time` was defined as c99 function `time`, it uses |
| 88 | * CLOCK_REALTIME and returns the number of seconds since the Epoch. And |
| 89 | * `nanosleep` uses CLOCK_MONOTONIC. The time sources are out of sync. |
| 90 | * |
| 91 | * If CLOCK_MONOTONIC is faster than CLOCK_REALTIME and `nanosleep` exits at |
| 92 | * the end of a second, `elapsed_secs` will be less than `delay_secs`. |
| 93 | * |
| 94 | * Workaround it with 1 second tolerance. |
| 95 | */ |
| 96 | TEST_ASSERT(elapsed_secs >= delay_secs - 1); |
| 97 | /* If CLOCK_MONOTONIC is slower than CLOCK_REALTIME or nanosleep does not |
| 98 | * exit on time. |
| 99 | */ |
| 100 | TEST_ASSERT(elapsed_secs < 4 + delay_secs); |
| 101 | /* This goto is added to avoid warnings from the generated code. */ |
| 102 | goto exit; |
| 103 | } |
| 104 | /* END_CASE */ |