Paul Bakker | 50157ff | 2016-07-19 14:57:00 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Gilles Peskine | 8064bf3 | 2017-10-10 19:56:06 +0200 | [diff] [blame] | 2 | |
k-stachowiak | 7223342 | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 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 | */ |
Gilles Peskine | 8064bf3 | 2017-10-10 19:56:06 +0200 | [diff] [blame] | 7 | |
| 8 | #include <limits.h> |
| 9 | |
Paul Bakker | 50157ff | 2016-07-19 14:57:00 +0100 | [diff] [blame] | 10 | #include "mbedtls/timing.h" |
Gilles Peskine | 8064bf3 | 2017-10-10 19:56:06 +0200 | [diff] [blame] | 11 | |
Paul Bakker | 50157ff | 2016-07-19 14:57:00 +0100 | [diff] [blame] | 12 | /* END_HEADER */ |
| 13 | |
| 14 | /* BEGIN_DEPENDENCIES |
| 15 | * depends_on:MBEDTLS_TIMING_C |
| 16 | * END_DEPENDENCIES |
| 17 | */ |
| 18 | |
Gilles Peskine | 8064bf3 | 2017-10-10 19:56:06 +0200 | [diff] [blame] | 19 | /* BEGIN_CASE */ |
Gilles Peskine | 078f1a1 | 2017-10-11 16:13:13 +0200 | [diff] [blame] | 20 | void timing_hardclock( ) |
| 21 | { |
k-stachowiak | 7223342 | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 22 | (void) mbedtls_timing_hardclock(); |
| 23 | /* This goto is added to avoid warnings from the generated code. */ |
| 24 | goto exit; |
| 25 | } |
| 26 | /* END_CASE */ |
Gilles Peskine | 078f1a1 | 2017-10-11 16:13:13 +0200 | [diff] [blame] | 27 | |
k-stachowiak | 7223342 | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 28 | /* BEGIN_CASE */ |
| 29 | void timing_get_timer( ) |
| 30 | { |
| 31 | struct mbedtls_timing_hr_time time; |
| 32 | (void) mbedtls_timing_get_timer( &time, 1 ); |
| 33 | (void) mbedtls_timing_get_timer( &time, 0 ); |
| 34 | /* This goto is added to avoid warnings from the generated code. */ |
| 35 | goto exit; |
| 36 | } |
| 37 | /* END_CASE */ |
Gilles Peskine | 2a26d62 | 2017-10-18 20:00:32 +0200 | [diff] [blame] | 38 | |
k-stachowiak | 7223342 | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 39 | /* BEGIN_CASE */ |
| 40 | void timing_set_alarm( int seconds ) |
| 41 | { |
k-stachowiak | fa44458 | 2019-02-05 09:22:20 +0100 | [diff] [blame] | 42 | if( seconds == 0 ) |
| 43 | { |
k-stachowiak | 7223342 | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 44 | mbedtls_set_alarm( seconds ); |
| 45 | TEST_ASSERT( mbedtls_timing_alarmed == 1 ); |
k-stachowiak | fa44458 | 2019-02-05 09:22:20 +0100 | [diff] [blame] | 46 | } |
| 47 | else |
| 48 | { |
k-stachowiak | 7223342 | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 49 | mbedtls_set_alarm( seconds ); |
| 50 | TEST_ASSERT( mbedtls_timing_alarmed == 0 || |
| 51 | mbedtls_timing_alarmed == 1 ); |
| 52 | } |
| 53 | } |
| 54 | /* END_CASE */ |
Gilles Peskine | 078f1a1 | 2017-10-11 16:13:13 +0200 | [diff] [blame] | 55 | |
k-stachowiak | 7223342 | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 56 | /* BEGIN_CASE */ |
| 57 | void timing_delay( int fin_ms ) |
| 58 | { |
| 59 | mbedtls_timing_delay_context ctx; |
| 60 | int result; |
k-stachowiak | fa44458 | 2019-02-05 09:22:20 +0100 | [diff] [blame] | 61 | if( fin_ms == 0 ) |
| 62 | { |
k-stachowiak | 7223342 | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 63 | mbedtls_timing_set_delay( &ctx, 0, 0 ); |
| 64 | result = mbedtls_timing_get_delay( &ctx ); |
| 65 | TEST_ASSERT( result == -1 ); |
k-stachowiak | fa44458 | 2019-02-05 09:22:20 +0100 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | { |
k-stachowiak | 7223342 | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 69 | mbedtls_timing_set_delay( &ctx, fin_ms / 2, fin_ms ); |
| 70 | result = mbedtls_timing_get_delay( &ctx ); |
| 71 | TEST_ASSERT( result >= 0 && result <= 2 ); |
| 72 | } |
Gilles Peskine | 078f1a1 | 2017-10-11 16:13:13 +0200 | [diff] [blame] | 73 | } |
| 74 | /* END_CASE */ |