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 | |
| 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 | } |
Jerry Yu | 9a12df0 | 2023-03-03 12:55:16 +0800 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | /* END_HEADER */ |
| 36 | |
Jerry Yu | c5b48a6 | 2023-03-13 14:28:06 +0800 | [diff] [blame^] | 37 | /* BEGIN_DEPENDENCIES */ |
| 38 | |
| 39 | /* END_DEPENDENCIES */ |
| 40 | |
| 41 | |
Jerry Yu | 9a12df0 | 2023-03-03 12:55:16 +0800 | [diff] [blame] | 42 | |
| 43 | /* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */ |
| 44 | void time_get_milliseconds() |
| 45 | { |
| 46 | mbedtls_ms_time_t current = mbedtls_ms_time(); |
| 47 | (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 */ |
| 54 | void time_get_seconds() |
| 55 | { |
| 56 | mbedtls_time_t current = mbedtls_time(NULL); |
| 57 | (void) current; |
| 58 | /* This goto is added to avoid warnings from the generated code. */ |
| 59 | goto exit; |
| 60 | } |
| 61 | /* END_CASE */ |
Jerry Yu | c5b48a6 | 2023-03-13 14:28:06 +0800 | [diff] [blame^] | 62 | |
| 63 | /* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */ |
| 64 | void time_delay_milliseconds(int delay_ms) |
| 65 | { |
| 66 | mbedtls_ms_time_t current = mbedtls_ms_time(); |
| 67 | |
| 68 | sleep_ms(delay_ms); |
| 69 | |
| 70 | current = mbedtls_ms_time() - current; |
| 71 | TEST_ASSERT(current == delay_ms || current == delay_ms + 1); |
| 72 | /* This goto is added to avoid warnings from the generated code. */ |
| 73 | goto exit; |
| 74 | } |
| 75 | /* END_CASE */ |
| 76 | |
| 77 | /* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */ |
| 78 | void time_delay_seconds(int delay) |
| 79 | { |
| 80 | mbedtls_time_t current = mbedtls_time(NULL); |
| 81 | sleep_ms(delay*1000); |
| 82 | current = mbedtls_time(NULL) - current; |
| 83 | TEST_ASSERT(current == delay); |
| 84 | /* This goto is added to avoid warnings from the generated code. */ |
| 85 | goto exit; |
| 86 | } |
| 87 | /* END_CASE */ |