time_test.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "testutil.h"
  10. #include "internal/time.h"
  11. static int test_time_to_timeval(void)
  12. {
  13. OSSL_TIME a;
  14. struct timeval tv;
  15. a = ossl_time_zero();
  16. tv = ossl_time_to_timeval(a);
  17. if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 0))
  18. return 0;
  19. /* Test that zero round trips */
  20. if (!TEST_true(ossl_time_is_zero(ossl_time_from_timeval(tv))))
  21. return 0;
  22. /* We should round up nano secs to the next usec */
  23. a = ossl_ticks2time(1);
  24. tv = ossl_time_to_timeval(a);
  25. if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 1))
  26. return 0;
  27. a = ossl_ticks2time(999);
  28. tv = ossl_time_to_timeval(a);
  29. if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 1))
  30. return 0;
  31. a = ossl_ticks2time(1000);
  32. tv = ossl_time_to_timeval(a);
  33. if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 1))
  34. return 0;
  35. a = ossl_ticks2time(1001);
  36. tv = ossl_time_to_timeval(a);
  37. if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 2))
  38. return 0;
  39. a = ossl_ticks2time(999000);
  40. tv = ossl_time_to_timeval(a);
  41. if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 999))
  42. return 0;
  43. a = ossl_ticks2time(999999001);
  44. tv = ossl_time_to_timeval(a);
  45. if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 0))
  46. return 0;
  47. a = ossl_ticks2time(999999999);
  48. tv = ossl_time_to_timeval(a);
  49. if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 0))
  50. return 0;
  51. a = ossl_ticks2time(1000000000);
  52. tv = ossl_time_to_timeval(a);
  53. if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 0))
  54. return 0;
  55. a = ossl_ticks2time(1000000001);
  56. tv = ossl_time_to_timeval(a);
  57. if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 1))
  58. return 0;
  59. /*
  60. * Note that we don't currently support infinity round tripping. Instead
  61. * callers need to explicitly test for infinity.
  62. */
  63. return 1;
  64. }
  65. int setup_tests(void)
  66. {
  67. ADD_TEST(test_time_to_timeval);
  68. return 1;
  69. }