1/* BEGIN_HEADER */ 2 3/* This test module exercises the timing module. Since, depending on the 4 * underlying operating system, the timing 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#include "mbedtls/timing.h" 11 12/* END_HEADER */ 13 14/* BEGIN_DEPENDENCIES 15 * depends_on:MBEDTLS_TIMING_C 16 * END_DEPENDENCIES 17 */ 18 19/* BEGIN_CASE */ 20void timing_hardclock() 21{ 22 (void) mbedtls_timing_hardclock(); 23 /* This goto is added to avoid warnings from the generated code. */ 24 goto exit; 25} 26/* END_CASE */ 27 28/* BEGIN_CASE */ 29void timing_get_timer() 30{ 31 struct mbedtls_timing_hr_time time; 32 33 memset(&time, 0, sizeof(time)); 34 35 (void) mbedtls_timing_get_timer(&time, 1); 36 37 /* Check that a non-zero time was written back */ 38 int all_zero = 1; 39 for (size_t i = 0; i < sizeof(time); i++) { 40 all_zero &= ((unsigned char *) &time)[i] == 0; 41 } 42 TEST_ASSERT(!all_zero); 43 44 (void) mbedtls_timing_get_timer(&time, 0); 45 46 /* This goto is added to avoid warnings from the generated code. */ 47 goto exit; 48} 49/* END_CASE */ 50 51/* BEGIN_CASE */ 52void timing_set_alarm(int seconds) 53{ 54 if (seconds == 0) { 55 mbedtls_set_alarm(seconds); 56 TEST_ASSERT(mbedtls_timing_alarmed == 1); 57 } else { 58 mbedtls_set_alarm(seconds); 59 TEST_ASSERT(mbedtls_timing_alarmed == 0 || 60 mbedtls_timing_alarmed == 1); 61 } 62} 63/* END_CASE */ 64 65/* BEGIN_CASE */ 66void timing_delay(int fin_ms) 67{ 68 mbedtls_timing_delay_context ctx; 69 int result; 70 if (fin_ms == 0) { 71 mbedtls_timing_set_delay(&ctx, 0, 0); 72 result = mbedtls_timing_get_delay(&ctx); 73 TEST_ASSERT(result == -1); 74 } else { 75 mbedtls_timing_set_delay(&ctx, fin_ms / 2, fin_ms); 76 result = mbedtls_timing_get_delay(&ctx); 77 TEST_ASSERT(result >= 0 && result <= 2); 78 } 79} 80/* END_CASE */ 81