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