quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

test_suite_platform.function (3932B)


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