| 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 | 8beb250 | 2023-05-06 11:55:22 +0800 | [diff] [blame] | 13 | #if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \ | 
|  | 14 | defined(__MINGW32__) || defined(_WIN64) | 
| Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 15 | #include <windows.h> | 
|  | 16 | #elif _POSIX_C_SOURCE >= 199309L | 
|  | 17 | #include <time.h> | 
|  | 18 | #else | 
|  | 19 | #include <unistd.h> | 
|  | 20 | #endif | 
|  | 21 | void sleep_ms(int milliseconds) | 
|  | 22 | { | 
| Jerry Yu | 8beb250 | 2023-05-06 11:55:22 +0800 | [diff] [blame] | 23 | #if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \ | 
|  | 24 | defined(__MINGW32__) || defined(_WIN64) | 
| Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 25 | Sleep(milliseconds); | 
|  | 26 | #elif _POSIX_C_SOURCE >= 199309L | 
|  | 27 | struct timespec ts; | 
|  | 28 | ts.tv_sec = milliseconds / 1000; | 
|  | 29 | ts.tv_nsec = (milliseconds % 1000) * 1000000; | 
|  | 30 | nanosleep(&ts, NULL); | 
|  | 31 | #else | 
|  | 32 | usleep(milliseconds * 1000); | 
|  | 33 | #endif | 
|  | 34 | } | 
|  | 35 | #endif | 
|  | 36 |  | 
| Jerry Yu | 9a12df0 | 2023-03-03 12:55:16 +0800 | [diff] [blame] | 37 | /* END_HEADER */ | 
|  | 38 |  | 
| Jerry Yu | c5b48a6 | 2023-03-13 14:28:06 +0800 | [diff] [blame] | 39 | /* BEGIN_DEPENDENCIES */ | 
|  | 40 |  | 
|  | 41 | /* END_DEPENDENCIES */ | 
|  | 42 |  | 
| Jerry Yu | 9a12df0 | 2023-03-03 12:55:16 +0800 | [diff] [blame] | 43 | /* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */ | 
|  | 44 | void time_get_milliseconds() | 
|  | 45 | { | 
| Jerry Yu | e7ea823 | 2023-03-14 17:33:42 +0800 | [diff] [blame] | 46 | mbedtls_ms_time_t current = mbedtls_ms_time(); | 
| Jerry Yu | 9a12df0 | 2023-03-03 12:55:16 +0800 | [diff] [blame] | 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 | { | 
| Jerry Yu | e7ea823 | 2023-03-14 17:33:42 +0800 | [diff] [blame] | 56 | mbedtls_time_t current = mbedtls_time(NULL); | 
| Jerry Yu | 9a12df0 | 2023-03-03 12:55:16 +0800 | [diff] [blame] | 57 | (void) current; | 
|  | 58 | /* This goto is added to avoid warnings from the generated code. */ | 
|  | 59 | goto exit; | 
|  | 60 | } | 
|  | 61 | /* END_CASE */ | 
| Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +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 | mbedtls_ms_time_t elapsed_ms; | 
|  | 68 |  | 
| Jerry Yu | 484a9e1 | 2023-05-05 17:03:29 +0800 | [diff] [blame] | 69 | /* | 
|  | 70 | * WARNING: DO NOT ENABLE THIS TEST. We keep the code here to document the | 
|  | 71 | *          reason. | 
|  | 72 | * | 
|  | 73 | *          Windows CI reports random test fail on platform-suite. It might | 
|  | 74 | *          be caused by this case. | 
|  | 75 | */ | 
| Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 76 | sleep_ms(delay_ms); | 
|  | 77 |  | 
|  | 78 | elapsed_ms = mbedtls_ms_time() - current; | 
|  | 79 | TEST_ASSERT(elapsed_ms >= delay_ms && elapsed_ms < 4000 + delay_ms); | 
|  | 80 | /* This goto is added to avoid warnings from the generated code. */ | 
|  | 81 | goto exit; | 
|  | 82 | } | 
|  | 83 | /* END_CASE */ | 
|  | 84 |  | 
|  | 85 | /* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */ | 
| Jerry Yu | ed9b9a7 | 2023-04-18 17:09:03 +0800 | [diff] [blame] | 86 |  | 
|  | 87 | /* | 
| Jerry Yu | ad2091d | 2023-04-20 10:01:42 +0800 | [diff] [blame] | 88 | * WARNING: DO NOT ENABLE THIS TEST. We keep the code here to document the | 
|  | 89 | *          reason. | 
| Jerry Yu | ed9b9a7 | 2023-04-18 17:09:03 +0800 | [diff] [blame] | 90 | * | 
|  | 91 | *          The test often failed on the CI. See #1517. CI failures cannot be | 
|  | 92 | *          completely avoided due to out-of-sync clock sources. | 
|  | 93 | */ | 
| Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 94 | void time_delay_seconds(int delay_secs) | 
|  | 95 | { | 
|  | 96 | mbedtls_time_t current = mbedtls_time(NULL); | 
|  | 97 | mbedtls_time_t elapsed_secs; | 
|  | 98 |  | 
|  | 99 | sleep_ms(delay_secs * 1000); | 
|  | 100 |  | 
|  | 101 | elapsed_secs = mbedtls_time(NULL) - current; | 
|  | 102 |  | 
|  | 103 | /* | 
| Jerry Yu | d3c7d53 | 2023-04-19 14:07:59 +0800 | [diff] [blame] | 104 | * `mbedtls_time()` was defined as c99 function `time()`, returns the number | 
|  | 105 | * of seconds since the Epoch. And it is affected by discontinuous changes | 
|  | 106 | * from automatic drift adjustment or time setting system call. The POSIX.1 | 
|  | 107 | * specification for clock_settime says that discontinuous changes in | 
|  | 108 | * CLOCK_REALTIME should not affect `nanosleep()`. | 
| Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 109 | * | 
| Jerry Yu | d3c7d53 | 2023-04-19 14:07:59 +0800 | [diff] [blame] | 110 | * If discontinuous changes occur during `nanosleep()`, we will get | 
| Jerry Yu | ad2091d | 2023-04-20 10:01:42 +0800 | [diff] [blame] | 111 | * `elapsed_secs < delay_secs` for backward or `elapsed_secs > delay_secs` | 
| Jerry Yu | d3c7d53 | 2023-04-19 14:07:59 +0800 | [diff] [blame] | 112 | * for forward. | 
| Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 113 | * | 
| Jerry Yu | d3c7d53 | 2023-04-19 14:07:59 +0800 | [diff] [blame] | 114 | * The following tolerance windows cannot be guaranteed. | 
|  | 115 | * PLEASE DO NOT ENABLE IT IN CI TEST. | 
| Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 116 | */ | 
| Jerry Yu | d3c7d53 | 2023-04-19 14:07:59 +0800 | [diff] [blame] | 117 | TEST_ASSERT(elapsed_secs - delay_secs >= -1 && | 
|  | 118 | elapsed_secs - delay_secs <   4); | 
| Jerry Yu | d1190a5 | 2023-04-18 16:29:02 +0800 | [diff] [blame] | 119 | /* This goto is added to avoid warnings from the generated code. */ | 
|  | 120 | goto exit; | 
|  | 121 | } | 
|  | 122 | /* END_CASE */ | 
| Andrzej Kurek | 60de0b1 | 2023-05-09 16:38:04 -0400 | [diff] [blame] | 123 |  | 
|  | 124 | /* BEGIN_CASE */ | 
|  | 125 | void check_mbedtls_calloc_overallocation(intmax_t num, intmax_t size) | 
|  | 126 | { | 
|  | 127 | unsigned char *buf; | 
|  | 128 | buf = mbedtls_calloc((size_t) num, (size_t) size); | 
| Andrzej Kurek | cf669b0 | 2023-07-03 09:49:07 -0400 | [diff] [blame] | 129 | /* Dummy usage of the pointer to prevent optimizing it */ | 
|  | 130 | mbedtls_printf("calloc pointer : %p\n", buf); | 
| Andrzej Kurek | 60de0b1 | 2023-05-09 16:38:04 -0400 | [diff] [blame] | 131 | TEST_ASSERT(buf == NULL); | 
|  | 132 |  | 
|  | 133 | exit: | 
|  | 134 | mbedtls_free(buf); | 
|  | 135 | } | 
|  | 136 | /* END_CASE */ |