1a8e1175bSopenharmony_ci/* BEGIN_HEADER */ 2a8e1175bSopenharmony_ci 3a8e1175bSopenharmony_ci/* This test module exercises the timing module. Since, depending on the 4a8e1175bSopenharmony_ci * underlying operating system, the timing routines are not always reliable, 5a8e1175bSopenharmony_ci * this suite only performs very basic sanity checks of the timing API. 6a8e1175bSopenharmony_ci */ 7a8e1175bSopenharmony_ci 8a8e1175bSopenharmony_ci#include <limits.h> 9a8e1175bSopenharmony_ci 10a8e1175bSopenharmony_ci#include "mbedtls/timing.h" 11a8e1175bSopenharmony_ci 12a8e1175bSopenharmony_ci/* END_HEADER */ 13a8e1175bSopenharmony_ci 14a8e1175bSopenharmony_ci/* BEGIN_DEPENDENCIES 15a8e1175bSopenharmony_ci * depends_on:MBEDTLS_TIMING_C 16a8e1175bSopenharmony_ci * END_DEPENDENCIES 17a8e1175bSopenharmony_ci */ 18a8e1175bSopenharmony_ci 19a8e1175bSopenharmony_ci/* BEGIN_CASE */ 20a8e1175bSopenharmony_civoid timing_get_timer() 21a8e1175bSopenharmony_ci{ 22a8e1175bSopenharmony_ci struct mbedtls_timing_hr_time time; 23a8e1175bSopenharmony_ci 24a8e1175bSopenharmony_ci memset(&time, 0, sizeof(time)); 25a8e1175bSopenharmony_ci 26a8e1175bSopenharmony_ci (void) mbedtls_timing_get_timer(&time, 1); 27a8e1175bSopenharmony_ci 28a8e1175bSopenharmony_ci /* Check that a non-zero time was written back */ 29a8e1175bSopenharmony_ci int all_zero = 1; 30a8e1175bSopenharmony_ci for (size_t i = 0; i < sizeof(time); i++) { 31a8e1175bSopenharmony_ci all_zero &= ((unsigned char *) &time)[i] == 0; 32a8e1175bSopenharmony_ci } 33a8e1175bSopenharmony_ci TEST_ASSERT(!all_zero); 34a8e1175bSopenharmony_ci 35a8e1175bSopenharmony_ci (void) mbedtls_timing_get_timer(&time, 0); 36a8e1175bSopenharmony_ci 37a8e1175bSopenharmony_ci /* This goto is added to avoid warnings from the generated code. */ 38a8e1175bSopenharmony_ci goto exit; 39a8e1175bSopenharmony_ci} 40a8e1175bSopenharmony_ci/* END_CASE */ 41a8e1175bSopenharmony_ci 42a8e1175bSopenharmony_ci/* BEGIN_CASE */ 43a8e1175bSopenharmony_civoid timing_delay(int fin_ms) 44a8e1175bSopenharmony_ci{ 45a8e1175bSopenharmony_ci mbedtls_timing_delay_context ctx; 46a8e1175bSopenharmony_ci int result; 47a8e1175bSopenharmony_ci if (fin_ms == 0) { 48a8e1175bSopenharmony_ci mbedtls_timing_set_delay(&ctx, 0, 0); 49a8e1175bSopenharmony_ci result = mbedtls_timing_get_delay(&ctx); 50a8e1175bSopenharmony_ci TEST_ASSERT(result == -1); 51a8e1175bSopenharmony_ci } else { 52a8e1175bSopenharmony_ci mbedtls_timing_set_delay(&ctx, fin_ms / 2, fin_ms); 53a8e1175bSopenharmony_ci result = mbedtls_timing_get_delay(&ctx); 54a8e1175bSopenharmony_ci TEST_ASSERT(result >= 0 && result <= 2); 55a8e1175bSopenharmony_ci } 56a8e1175bSopenharmony_ci} 57a8e1175bSopenharmony_ci/* END_CASE */ 58