1a8e1175bSopenharmony_ci/* BEGIN_HEADER */
2a8e1175bSopenharmony_ci
3a8e1175bSopenharmony_ci/* This test module exercises the platform_* module. Since, depending on the
4a8e1175bSopenharmony_ci * underlying operating system, the time 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#if defined(MBEDTLS_HAVE_TIME)
11a8e1175bSopenharmony_ci#include "mbedtls/platform_time.h"
12a8e1175bSopenharmony_ci
13a8e1175bSopenharmony_ci#if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
14a8e1175bSopenharmony_ci    defined(__MINGW32__) || defined(_WIN64)
15a8e1175bSopenharmony_ci#include <windows.h>
16a8e1175bSopenharmony_ci#elif _POSIX_C_SOURCE >= 199309L
17a8e1175bSopenharmony_ci#include <time.h>
18a8e1175bSopenharmony_ci#else
19a8e1175bSopenharmony_ci#include <unistd.h>
20a8e1175bSopenharmony_ci#endif
21a8e1175bSopenharmony_civoid sleep_ms(int milliseconds)
22a8e1175bSopenharmony_ci{
23a8e1175bSopenharmony_ci#if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
24a8e1175bSopenharmony_ci    defined(__MINGW32__) || defined(_WIN64)
25a8e1175bSopenharmony_ci    Sleep(milliseconds);
26a8e1175bSopenharmony_ci#elif _POSIX_C_SOURCE >= 199309L
27a8e1175bSopenharmony_ci    struct timespec ts;
28a8e1175bSopenharmony_ci    ts.tv_sec = milliseconds / 1000;
29a8e1175bSopenharmony_ci    ts.tv_nsec = (milliseconds % 1000) * 1000000;
30a8e1175bSopenharmony_ci    nanosleep(&ts, NULL);
31a8e1175bSopenharmony_ci#else
32a8e1175bSopenharmony_ci    usleep(milliseconds * 1000);
33a8e1175bSopenharmony_ci#endif
34a8e1175bSopenharmony_ci}
35a8e1175bSopenharmony_ci#endif
36a8e1175bSopenharmony_ci
37a8e1175bSopenharmony_ci/* END_HEADER */
38a8e1175bSopenharmony_ci
39a8e1175bSopenharmony_ci/* BEGIN_DEPENDENCIES */
40a8e1175bSopenharmony_ci
41a8e1175bSopenharmony_ci/* END_DEPENDENCIES */
42a8e1175bSopenharmony_ci
43a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
44a8e1175bSopenharmony_civoid time_get_milliseconds()
45a8e1175bSopenharmony_ci{
46a8e1175bSopenharmony_ci    mbedtls_ms_time_t current = mbedtls_ms_time();
47a8e1175bSopenharmony_ci    (void) current;
48a8e1175bSopenharmony_ci    /* This goto is added to avoid warnings from the generated code. */
49a8e1175bSopenharmony_ci    goto exit;
50a8e1175bSopenharmony_ci}
51a8e1175bSopenharmony_ci/* END_CASE */
52a8e1175bSopenharmony_ci
53a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
54a8e1175bSopenharmony_civoid time_get_seconds()
55a8e1175bSopenharmony_ci{
56a8e1175bSopenharmony_ci    mbedtls_time_t current = mbedtls_time(NULL);
57a8e1175bSopenharmony_ci    (void) current;
58a8e1175bSopenharmony_ci    /* This goto is added to avoid warnings from the generated code. */
59a8e1175bSopenharmony_ci    goto exit;
60a8e1175bSopenharmony_ci}
61a8e1175bSopenharmony_ci/* END_CASE */
62a8e1175bSopenharmony_ci
63a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
64a8e1175bSopenharmony_civoid time_delay_milliseconds(int delay_ms)
65a8e1175bSopenharmony_ci{
66a8e1175bSopenharmony_ci    mbedtls_ms_time_t current = mbedtls_ms_time();
67a8e1175bSopenharmony_ci    mbedtls_ms_time_t elapsed_ms;
68a8e1175bSopenharmony_ci
69a8e1175bSopenharmony_ci    /*
70a8e1175bSopenharmony_ci     * WARNING: DO NOT ENABLE THIS TEST. We keep the code here to document the
71a8e1175bSopenharmony_ci     *          reason.
72a8e1175bSopenharmony_ci     *
73a8e1175bSopenharmony_ci     *          Windows CI reports random test fail on platform-suite. It might
74a8e1175bSopenharmony_ci     *          be caused by this case.
75a8e1175bSopenharmony_ci     */
76a8e1175bSopenharmony_ci    sleep_ms(delay_ms);
77a8e1175bSopenharmony_ci
78a8e1175bSopenharmony_ci    elapsed_ms = mbedtls_ms_time() - current;
79a8e1175bSopenharmony_ci    TEST_ASSERT(elapsed_ms >= delay_ms && elapsed_ms < 4000 + delay_ms);
80a8e1175bSopenharmony_ci    /* This goto is added to avoid warnings from the generated code. */
81a8e1175bSopenharmony_ci    goto exit;
82a8e1175bSopenharmony_ci}
83a8e1175bSopenharmony_ci/* END_CASE */
84a8e1175bSopenharmony_ci
85a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_HAVE_TIME */
86a8e1175bSopenharmony_ci
87a8e1175bSopenharmony_ci/*
88a8e1175bSopenharmony_ci * WARNING: DO NOT ENABLE THIS TEST. We keep the code here to document the
89a8e1175bSopenharmony_ci *          reason.
90a8e1175bSopenharmony_ci *
91a8e1175bSopenharmony_ci *          The test often failed on the CI. See #1517. CI failures cannot be
92a8e1175bSopenharmony_ci *          completely avoided due to out-of-sync clock sources.
93a8e1175bSopenharmony_ci */
94a8e1175bSopenharmony_civoid time_delay_seconds(int delay_secs)
95a8e1175bSopenharmony_ci{
96a8e1175bSopenharmony_ci    mbedtls_time_t current = mbedtls_time(NULL);
97a8e1175bSopenharmony_ci    mbedtls_time_t elapsed_secs;
98a8e1175bSopenharmony_ci
99a8e1175bSopenharmony_ci    sleep_ms(delay_secs * 1000);
100a8e1175bSopenharmony_ci
101a8e1175bSopenharmony_ci    elapsed_secs = mbedtls_time(NULL) - current;
102a8e1175bSopenharmony_ci
103a8e1175bSopenharmony_ci    /*
104a8e1175bSopenharmony_ci     * `mbedtls_time()` was defined as c99 function `time()`, returns the number
105a8e1175bSopenharmony_ci     * of seconds since the Epoch. And it is affected by discontinuous changes
106a8e1175bSopenharmony_ci     * from automatic drift adjustment or time setting system call. The POSIX.1
107a8e1175bSopenharmony_ci     * specification for clock_settime says that discontinuous changes in
108a8e1175bSopenharmony_ci     * CLOCK_REALTIME should not affect `nanosleep()`.
109a8e1175bSopenharmony_ci     *
110a8e1175bSopenharmony_ci     * If discontinuous changes occur during `nanosleep()`, we will get
111a8e1175bSopenharmony_ci     * `elapsed_secs < delay_secs` for backward or `elapsed_secs > delay_secs`
112a8e1175bSopenharmony_ci     * for forward.
113a8e1175bSopenharmony_ci     *
114a8e1175bSopenharmony_ci     * The following tolerance windows cannot be guaranteed.
115a8e1175bSopenharmony_ci     * PLEASE DO NOT ENABLE IT IN CI TEST.
116a8e1175bSopenharmony_ci     */
117a8e1175bSopenharmony_ci    TEST_ASSERT(elapsed_secs - delay_secs >= -1 &&
118a8e1175bSopenharmony_ci                elapsed_secs - delay_secs <   4);
119a8e1175bSopenharmony_ci    /* This goto is added to avoid warnings from the generated code. */
120a8e1175bSopenharmony_ci    goto exit;
121a8e1175bSopenharmony_ci}
122a8e1175bSopenharmony_ci/* END_CASE */
123a8e1175bSopenharmony_ci
124a8e1175bSopenharmony_ci/* BEGIN_CASE */
125a8e1175bSopenharmony_civoid check_mbedtls_calloc_overallocation(intmax_t num, intmax_t size)
126a8e1175bSopenharmony_ci{
127a8e1175bSopenharmony_ci    unsigned char *buf;
128a8e1175bSopenharmony_ci    buf = mbedtls_calloc((size_t) num, (size_t) size);
129a8e1175bSopenharmony_ci    /* Dummy usage of the pointer to prevent optimizing it */
130a8e1175bSopenharmony_ci    mbedtls_printf("calloc pointer : %p\n", buf);
131a8e1175bSopenharmony_ci    TEST_ASSERT(buf == NULL);
132a8e1175bSopenharmony_ci
133a8e1175bSopenharmony_ciexit:
134a8e1175bSopenharmony_ci    mbedtls_free(buf);
135a8e1175bSopenharmony_ci}
136a8e1175bSopenharmony_ci/* END_CASE */
137