gmdifftest.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. OPENSSL_gmtime_adj(&tm1, 0, offset);
  29. if (!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. }
  49. int setup_tests(void)
  50. {
  51. if (sizeof(time_t) < 8)
  52. TEST_info("Skipping; time_t is less than 64-bits");
  53. else
  54. ADD_ALL_TESTS_NOSUBTEST(test_gmtime, 1000000);
  55. return 1;
  56. }