2
0

time_offset_test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2017-2018 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. /* time_t/offset (+/-XXXX) tests for ASN1 and X509 */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <openssl/asn1.h>
  14. #include <openssl/x509.h>
  15. #include "testutil.h"
  16. #include "internal/nelem.h"
  17. typedef struct {
  18. const char *data;
  19. int time_result;
  20. int type;
  21. } TESTDATA;
  22. /**********************************************************************
  23. *
  24. * Test driver
  25. *
  26. ***/
  27. static TESTDATA tests[] = {
  28. { "20001201000000Z", 0, V_ASN1_GENERALIZEDTIME },
  29. { "20001201010000+0100", 0, V_ASN1_GENERALIZEDTIME },
  30. { "20001201050000+0500", 0, V_ASN1_GENERALIZEDTIME },
  31. { "20001130230000-0100", 0, V_ASN1_GENERALIZEDTIME },
  32. { "20001130190000-0500", 0, V_ASN1_GENERALIZEDTIME },
  33. { "20001130190001-0500", 1, V_ASN1_GENERALIZEDTIME }, /* +1 second */
  34. { "20001130185959-0500", -1, V_ASN1_GENERALIZEDTIME }, /* -1 second */
  35. { "001201000000Z", 0, V_ASN1_UTCTIME },
  36. { "001201010000+0100", 0, V_ASN1_UTCTIME },
  37. { "001201050000+0500", 0, V_ASN1_UTCTIME },
  38. { "001130230000-0100", 0, V_ASN1_UTCTIME },
  39. { "001130190000-0500", 0, V_ASN1_UTCTIME },
  40. { "001201000000-0000", 0, V_ASN1_UTCTIME },
  41. { "001201000001-0000", 1, V_ASN1_UTCTIME }, /* +1 second */
  42. { "001130235959-0000", -1, V_ASN1_UTCTIME }, /* -1 second */
  43. { "20001201000000+0000", 0, V_ASN1_GENERALIZEDTIME },
  44. { "20001201000000+0100", -1, V_ASN1_GENERALIZEDTIME },
  45. { "001201000000+0100", -1, V_ASN1_UTCTIME },
  46. { "20001201000000-0100", 1, V_ASN1_GENERALIZEDTIME },
  47. { "001201000000-0100", 1, V_ASN1_UTCTIME },
  48. { "20001201123400+1234", 0, V_ASN1_GENERALIZEDTIME },
  49. { "20001130112600-1234", 0, V_ASN1_GENERALIZEDTIME },
  50. };
  51. static time_t the_time = 975628800;
  52. static ASN1_TIME the_asn1_time = {
  53. 15,
  54. V_ASN1_GENERALIZEDTIME,
  55. (unsigned char*)"20001201000000Z",
  56. 0
  57. };
  58. static int test_offset(int idx)
  59. {
  60. ASN1_TIME at;
  61. const TESTDATA *testdata = &tests[idx];
  62. int ret = -2;
  63. int day, sec;
  64. at.data = (unsigned char*)testdata->data;
  65. at.length = strlen(testdata->data);
  66. at.type = testdata->type;
  67. at.flags = 0;
  68. if (!TEST_true(ASN1_TIME_diff(&day, &sec, &the_asn1_time, &at))) {
  69. TEST_info("ASN1_TIME_diff() failed for %s\n", at.data);
  70. return 0;
  71. }
  72. if (day > 0)
  73. ret = 1;
  74. else if (day < 0)
  75. ret = -1;
  76. else if (sec > 0)
  77. ret = 1;
  78. else if (sec < 0)
  79. ret = -1;
  80. else
  81. ret = 0;
  82. if (!TEST_int_eq(testdata->time_result, ret)) {
  83. TEST_info("ASN1_TIME_diff() test failed for %s day=%d sec=%d\n", at.data, day, sec);
  84. return 0;
  85. }
  86. ret = ASN1_TIME_cmp_time_t(&at, the_time);
  87. if (!TEST_int_eq(testdata->time_result, ret)) {
  88. TEST_info("ASN1_UTCTIME_cmp_time_t() test failed for %s\n", at.data);
  89. return 0;
  90. }
  91. return 1;
  92. }
  93. int setup_tests(void)
  94. {
  95. ADD_ALL_TESTS(test_offset, OSSL_NELEM(tests));
  96. return 1;
  97. }