blob: 42d3da6ac2a622195f4292951fa20605c740cf87 [file] [log] [blame]
Paul Bakker50157ff2016-07-19 14:57:00 +01001/* BEGIN_HEADER */
Gilles Peskine8064bf32017-10-10 19:56:06 +02002
k-stachowiak72233422019-01-29 10:19:49 +01003/* 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 Peskine8064bf32017-10-10 19:56:06 +02007
8#include <limits.h>
9
Paul Bakker50157ff2016-07-19 14:57:00 +010010#include "mbedtls/timing.h"
Gilles Peskine8064bf32017-10-10 19:56:06 +020011
Paul Bakker50157ff2016-07-19 14:57:00 +010012/* END_HEADER */
13
14/* BEGIN_DEPENDENCIES
15 * depends_on:MBEDTLS_TIMING_C
16 * END_DEPENDENCIES
17 */
18
Gilles Peskine8064bf32017-10-10 19:56:06 +020019/* BEGIN_CASE */
Gilles Peskine078f1a12017-10-11 16:13:13 +020020void timing_hardclock( )
21{
k-stachowiak72233422019-01-29 10:19:49 +010022 (void) mbedtls_timing_hardclock();
23 /* This goto is added to avoid warnings from the generated code. */
24 goto exit;
25}
26/* END_CASE */
Gilles Peskine078f1a12017-10-11 16:13:13 +020027
k-stachowiak72233422019-01-29 10:19:49 +010028/* BEGIN_CASE */
29void 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 Peskine2a26d622017-10-18 20:00:32 +020038
k-stachowiak72233422019-01-29 10:19:49 +010039/* BEGIN_CASE */
40void timing_set_alarm( int seconds )
41{
42 if( seconds == 0 ) {
43 mbedtls_set_alarm( seconds );
44 TEST_ASSERT( mbedtls_timing_alarmed == 1 );
45 } else {
46 mbedtls_set_alarm( seconds );
47 TEST_ASSERT( mbedtls_timing_alarmed == 0 ||
48 mbedtls_timing_alarmed == 1 );
49 }
50}
51/* END_CASE */
Gilles Peskine078f1a12017-10-11 16:13:13 +020052
k-stachowiak72233422019-01-29 10:19:49 +010053/* BEGIN_CASE */
54void timing_delay( int fin_ms )
55{
56 mbedtls_timing_delay_context ctx;
57 int result;
58 if( fin_ms == 0 ) {
59 mbedtls_timing_set_delay( &ctx, 0, 0 );
60 result = mbedtls_timing_get_delay( &ctx );
61 TEST_ASSERT( result == -1 );
62 } else {
63 mbedtls_timing_set_delay( &ctx, fin_ms / 2, fin_ms );
64 result = mbedtls_timing_get_delay( &ctx );
65 TEST_ASSERT( result >= 0 && result <= 2 );
66 }
Gilles Peskine078f1a12017-10-11 16:13:13 +020067}
68/* END_CASE */