2
0

gmdifftest.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2015-2021 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 <openssl/crypto.h>
  10. #include "testutil.h"
  11. #define SECS_PER_DAY (24 * 60 * 60)
  12. /*
  13. * Time checking test code. Check times are identical for a wide range of
  14. * offsets. This should be run on a machine with 64 bit time_t or it will
  15. * trigger the very errors the routines fix.
  16. */
  17. static int check_time(long offset)
  18. {
  19. struct tm tm1, tm2, o1;
  20. int off_day, off_sec;
  21. long toffset;
  22. time_t t1, t2;
  23. time(&t1);
  24. t2 = t1 + offset;
  25. OPENSSL_gmtime(&t2, &tm2);
  26. OPENSSL_gmtime(&t1, &tm1);
  27. o1 = tm1;
  28. if (!TEST_true(OPENSSL_gmtime_adj(&tm1, 0, offset))
  29. || !TEST_int_eq(tm1.tm_year, tm2.tm_year)
  30. || !TEST_int_eq(tm1.tm_mon, tm2.tm_mon)
  31. || !TEST_int_eq(tm1.tm_mday, tm2.tm_mday)
  32. || !TEST_int_eq(tm1.tm_hour, tm2.tm_hour)
  33. || !TEST_int_eq(tm1.tm_min, tm2.tm_min)
  34. || !TEST_int_eq(tm1.tm_sec, tm2.tm_sec)
  35. || !TEST_true(OPENSSL_gmtime_diff(&off_day, &off_sec, &o1, &tm1)))
  36. return 0;
  37. toffset = (long)off_day * SECS_PER_DAY + off_sec;
  38. if (!TEST_long_eq(offset, toffset))
  39. return 0;
  40. return 1;
  41. }
  42. static int test_gmtime(int offset)
  43. {
  44. return check_time(offset)
  45. && check_time(-offset)
  46. && check_time(offset * 1000L)
  47. && check_time(-offset * 1000L)
  48. && check_time(offset * 1000000L)
  49. && check_time(-offset * 1000000L);
  50. }
  51. int setup_tests(void)
  52. {
  53. if (sizeof(time_t) < 8)
  54. TEST_info("Skipping; time_t is less than 64-bits");
  55. else
  56. ADD_ALL_TESTS_NOSUBTEST(test_gmtime, 1000);
  57. return 1;
  58. }