test_suite_timing.function 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* BEGIN_HEADER */
  2. /* This test module exercises the timing module. Since, depending on the
  3. * underlying operating system, the timing routines are not always reliable,
  4. * this suite only performs very basic sanity checks of the timing API.
  5. */
  6. #include <limits.h>
  7. #include "mbedtls/timing.h"
  8. /* END_HEADER */
  9. /* BEGIN_DEPENDENCIES
  10. * depends_on:MBEDTLS_TIMING_C
  11. * END_DEPENDENCIES
  12. */
  13. /* BEGIN_CASE */
  14. void timing_hardclock( )
  15. {
  16. (void) mbedtls_timing_hardclock();
  17. /* This goto is added to avoid warnings from the generated code. */
  18. goto exit;
  19. }
  20. /* END_CASE */
  21. /* BEGIN_CASE */
  22. void timing_get_timer( )
  23. {
  24. struct mbedtls_timing_hr_time time;
  25. (void) mbedtls_timing_get_timer( &time, 1 );
  26. (void) mbedtls_timing_get_timer( &time, 0 );
  27. /* This goto is added to avoid warnings from the generated code. */
  28. goto exit;
  29. }
  30. /* END_CASE */
  31. /* BEGIN_CASE */
  32. void timing_set_alarm( int seconds )
  33. {
  34. if( seconds == 0 )
  35. {
  36. mbedtls_set_alarm( seconds );
  37. TEST_ASSERT( mbedtls_timing_alarmed == 1 );
  38. }
  39. else
  40. {
  41. mbedtls_set_alarm( seconds );
  42. TEST_ASSERT( mbedtls_timing_alarmed == 0 ||
  43. mbedtls_timing_alarmed == 1 );
  44. }
  45. }
  46. /* END_CASE */
  47. /* BEGIN_CASE */
  48. void timing_delay( int fin_ms )
  49. {
  50. mbedtls_timing_delay_context ctx;
  51. int result;
  52. if( fin_ms == 0 )
  53. {
  54. mbedtls_timing_set_delay( &ctx, 0, 0 );
  55. result = mbedtls_timing_get_delay( &ctx );
  56. TEST_ASSERT( result == -1 );
  57. }
  58. else
  59. {
  60. mbedtls_timing_set_delay( &ctx, fin_ms / 2, fin_ms );
  61. result = mbedtls_timing_get_delay( &ctx );
  62. TEST_ASSERT( result >= 0 && result <= 2 );
  63. }
  64. }
  65. /* END_CASE */