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_get_timer()
21{
22    struct mbedtls_timing_hr_time time;
23
24    memset(&time, 0, sizeof(time));
25
26    (void) mbedtls_timing_get_timer(&time, 1);
27
28    /* Check that a non-zero time was written back */
29    int all_zero = 1;
30    for (size_t i = 0; i < sizeof(time); i++) {
31        all_zero &= ((unsigned char *) &time)[i] == 0;
32    }
33    TEST_ASSERT(!all_zero);
34
35    (void) mbedtls_timing_get_timer(&time, 0);
36
37    /* This goto is added to avoid warnings from the generated code. */
38    goto exit;
39}
40/* END_CASE */
41
42/* BEGIN_CASE */
43void timing_delay(int fin_ms)
44{
45    mbedtls_timing_delay_context ctx;
46    int result;
47    if (fin_ms == 0) {
48        mbedtls_timing_set_delay(&ctx, 0, 0);
49        result = mbedtls_timing_get_delay(&ctx);
50        TEST_ASSERT(result == -1);
51    } else {
52        mbedtls_timing_set_delay(&ctx, fin_ms / 2, fin_ms);
53        result = mbedtls_timing_get_delay(&ctx);
54        TEST_ASSERT(result >= 0 && result <= 2);
55    }
56}
57/* END_CASE */
58