113d40330Schristos /*
2*b0d17251Schristos * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
313d40330Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
513d40330Schristos * this file except in compliance with the License. You can obtain a copy
613d40330Schristos * in the file LICENSE in the source distribution or at
713d40330Schristos * https://www.openssl.org/source/license.html
813d40330Schristos */
913d40330Schristos
1013d40330Schristos /* Time tests for the asn1 module */
1113d40330Schristos
1213d40330Schristos #include <stdio.h>
1313d40330Schristos #include <string.h>
1413d40330Schristos
1513d40330Schristos #include <openssl/asn1.h>
1613d40330Schristos #include <openssl/evp.h>
1713d40330Schristos #include <openssl/objects.h>
1813d40330Schristos #include "testutil.h"
1913d40330Schristos #include "internal/nelem.h"
2013d40330Schristos
2113d40330Schristos struct testdata {
2213d40330Schristos char *data; /* TIME string value */
2313d40330Schristos int type; /* GENERALIZED OR UTC */
2413d40330Schristos int expected_type; /* expected type after set/set_string_gmt */
2513d40330Schristos int check_result; /* check result */
2613d40330Schristos time_t t; /* expected time_t*/
274ce06407Schristos int cmp_result; /* comparison to baseline result */
284ce06407Schristos int convert_result; /* conversion result */
2913d40330Schristos };
3013d40330Schristos
3113d40330Schristos static struct testdata tbl_testdata_pos[] = {
3213d40330Schristos { "0", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, /* Bad time */
3313d40330Schristos { "ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
3413d40330Schristos { "0ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
3513d40330Schristos { "1-700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
3613d40330Schristos { "`9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
3713d40330Schristos { "19700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
3813d40330Schristos { "A00101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
3913d40330Schristos { "A9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
4013d40330Schristos { "1A700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
4113d40330Schristos { "19A00101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
4213d40330Schristos { "197A0101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
4313d40330Schristos { "1970A101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
4413d40330Schristos { "19700A01000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
4513d40330Schristos { "197001A1000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
4613d40330Schristos { "1970010A000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
4713d40330Schristos { "19700101A00000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
4813d40330Schristos { "197001010A0000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
4913d40330Schristos { "1970010100A000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
5013d40330Schristos { "19700101000A00Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
5113d40330Schristos { "197001010000A0Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
5213d40330Schristos { "1970010100000AZ", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
5313d40330Schristos { "700101000000X", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
5413d40330Schristos { "19700101000000X", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
5513d40330Schristos { "19700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, /* Epoch begins */
5613d40330Schristos { "700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, /* ditto */
5713d40330Schristos { "20380119031407Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, }, /* Max 32bit time_t */
5813d40330Schristos { "380119031407Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, },
5913d40330Schristos { "20371231235959Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, }, /* Just before 2038 */
6013d40330Schristos { "20371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 1, }, /* Bad UTC time */
6113d40330Schristos { "371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, },
6213d40330Schristos { "19701006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, },
6313d40330Schristos { "701006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, },
6413d40330Schristos { "19991231000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, /* Match baseline */
6513d40330Schristos { "199912310000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, /* In various flavors */
6613d40330Schristos { "991231000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
6713d40330Schristos { "9912310000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
6813d40330Schristos { "9912310000+0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
6913d40330Schristos { "199912310000+0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
7013d40330Schristos { "9912310000-0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
7113d40330Schristos { "199912310000-0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
7213d40330Schristos { "199912310100+0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
7313d40330Schristos { "199912302300-0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
7413d40330Schristos { "199912302300-A000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, },
7513d40330Schristos { "199912302300-0A00", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, },
7613d40330Schristos { "9912310100+0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
7713d40330Schristos { "9912302300-0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
7813d40330Schristos };
7913d40330Schristos
8013d40330Schristos /* ASSUMES SIGNED TIME_T */
8113d40330Schristos static struct testdata tbl_testdata_neg[] = {
8213d40330Schristos { "19011213204552Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, INT_MIN, -1, 0, },
8313d40330Schristos { "691006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
8413d40330Schristos { "19691006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
8513d40330Schristos };
8613d40330Schristos
8713d40330Schristos /* explicit casts to time_t short warnings on systems with 32-bit time_t */
8813d40330Schristos static struct testdata tbl_testdata_pos_64bit[] = {
8913d40330Schristos { "20380119031408Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
9013d40330Schristos { "20380119031409Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000001, 1, 1, },
9113d40330Schristos { "380119031408Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
9213d40330Schristos { "20500101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)0x967b1ec0, 1, 0, },
9313d40330Schristos };
9413d40330Schristos
9513d40330Schristos /* ASSUMES SIGNED TIME_T */
9613d40330Schristos static struct testdata tbl_testdata_neg_64bit[] = {
9713d40330Schristos { "19011213204551Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2147483649LL, -1, 0, },
9813d40330Schristos { "19000101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2208945600LL, -1, 0, },
9913d40330Schristos };
10013d40330Schristos
10113d40330Schristos /* A baseline time to compare to */
10213d40330Schristos static ASN1_TIME gtime = {
10313d40330Schristos 15,
10413d40330Schristos V_ASN1_GENERALIZEDTIME,
10513d40330Schristos (unsigned char*)"19991231000000Z",
10613d40330Schristos 0
10713d40330Schristos };
10813d40330Schristos static time_t gtime_t = 946598400;
10913d40330Schristos
test_table(struct testdata * tbl,int idx)11013d40330Schristos static int test_table(struct testdata *tbl, int idx)
11113d40330Schristos {
11213d40330Schristos int error = 0;
11313d40330Schristos ASN1_TIME atime;
11413d40330Schristos ASN1_TIME *ptime;
11513d40330Schristos struct testdata *td = &tbl[idx];
11613d40330Schristos int day, sec;
11713d40330Schristos
11813d40330Schristos atime.data = (unsigned char*)td->data;
11913d40330Schristos atime.length = strlen((char*)atime.data);
12013d40330Schristos atime.type = td->type;
12113d40330Schristos atime.flags = 0;
12213d40330Schristos
12313d40330Schristos if (!TEST_int_eq(ASN1_TIME_check(&atime), td->check_result)) {
12413d40330Schristos TEST_info("ASN1_TIME_check(%s) unexpected result", atime.data);
12513d40330Schristos error = 1;
12613d40330Schristos }
12713d40330Schristos if (td->check_result == 0)
12813d40330Schristos return 1;
12913d40330Schristos
13013d40330Schristos if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, td->t), 0)) {
13113d40330Schristos TEST_info("ASN1_TIME_cmp_time_t(%s vs %ld) compare failed", atime.data, (long)td->t);
13213d40330Schristos error = 1;
13313d40330Schristos }
13413d40330Schristos
13513d40330Schristos if (!TEST_true(ASN1_TIME_diff(&day, &sec, &atime, &atime))) {
13613d40330Schristos TEST_info("ASN1_TIME_diff(%s) to self failed", atime.data);
13713d40330Schristos error = 1;
13813d40330Schristos }
13913d40330Schristos if (!TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
14013d40330Schristos TEST_info("ASN1_TIME_diff(%s) to self not equal", atime.data);
14113d40330Schristos error = 1;
14213d40330Schristos }
14313d40330Schristos
14413d40330Schristos if (!TEST_true(ASN1_TIME_diff(&day, &sec, >ime, &atime))) {
14513d40330Schristos TEST_info("ASN1_TIME_diff(%s) to baseline failed", atime.data);
14613d40330Schristos error = 1;
14713d40330Schristos } else if (!((td->cmp_result == 0 && TEST_true((day == 0 && sec == 0))) ||
14813d40330Schristos (td->cmp_result == -1 && TEST_true((day < 0 || sec < 0))) ||
14913d40330Schristos (td->cmp_result == 1 && TEST_true((day > 0 || sec > 0))))) {
15013d40330Schristos TEST_info("ASN1_TIME_diff(%s) to baseline bad comparison", atime.data);
15113d40330Schristos error = 1;
15213d40330Schristos }
15313d40330Schristos
15413d40330Schristos if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, gtime_t), td->cmp_result)) {
15513d40330Schristos TEST_info("ASN1_TIME_cmp_time_t(%s) to baseline bad comparison", atime.data);
15613d40330Schristos error = 1;
15713d40330Schristos }
15813d40330Schristos
15913d40330Schristos ptime = ASN1_TIME_set(NULL, td->t);
16013d40330Schristos if (!TEST_ptr(ptime)) {
16113d40330Schristos TEST_info("ASN1_TIME_set(%ld) failed", (long)td->t);
16213d40330Schristos error = 1;
16313d40330Schristos } else {
16413d40330Schristos int local_error = 0;
16513d40330Schristos if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
16613d40330Schristos TEST_info("ASN1_TIME_set(%ld) compare failed (%s->%s)",
16713d40330Schristos (long)td->t, td->data, ptime->data);
16813d40330Schristos local_error = error = 1;
16913d40330Schristos }
17013d40330Schristos if (!TEST_int_eq(ptime->type, td->expected_type)) {
17113d40330Schristos TEST_info("ASN1_TIME_set(%ld) unexpected type", (long)td->t);
17213d40330Schristos local_error = error = 1;
17313d40330Schristos }
17413d40330Schristos if (local_error)
17513d40330Schristos TEST_info("ASN1_TIME_set() = %*s", ptime->length, ptime->data);
17613d40330Schristos ASN1_TIME_free(ptime);
17713d40330Schristos }
17813d40330Schristos
17913d40330Schristos ptime = ASN1_TIME_new();
18013d40330Schristos if (!TEST_ptr(ptime)) {
18113d40330Schristos TEST_info("ASN1_TIME_new() failed");
18213d40330Schristos error = 1;
18313d40330Schristos } else {
18413d40330Schristos int local_error = 0;
18513d40330Schristos if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
18613d40330Schristos TEST_info("ASN1_TIME_set_string_gmt(%s) failed", td->data);
18713d40330Schristos local_error = error = 1;
18813d40330Schristos }
18913d40330Schristos if (!TEST_int_eq(ASN1_TIME_normalize(ptime), td->check_result)) {
19013d40330Schristos TEST_info("ASN1_TIME_normalize(%s) failed", td->data);
19113d40330Schristos local_error = error = 1;
19213d40330Schristos }
19313d40330Schristos if (!TEST_int_eq(ptime->type, td->expected_type)) {
19413d40330Schristos TEST_info("ASN1_TIME_set_string_gmt(%s) unexpected type", td->data);
19513d40330Schristos local_error = error = 1;
19613d40330Schristos }
19713d40330Schristos day = sec = 0;
19813d40330Schristos if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
19913d40330Schristos TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string_gmt() failed", day, sec, td->data);
20013d40330Schristos local_error = error = 1;
20113d40330Schristos }
20213d40330Schristos if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
20313d40330Schristos TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string_gnt() to baseline bad comparison", td->data);
20413d40330Schristos local_error = error = 1;
20513d40330Schristos }
20613d40330Schristos if (local_error)
20713d40330Schristos TEST_info("ASN1_TIME_set_string_gmt() = %*s", ptime->length, ptime->data);
20813d40330Schristos ASN1_TIME_free(ptime);
20913d40330Schristos }
21013d40330Schristos
21113d40330Schristos ptime = ASN1_TIME_new();
21213d40330Schristos if (!TEST_ptr(ptime)) {
21313d40330Schristos TEST_info("ASN1_TIME_new() failed");
21413d40330Schristos error = 1;
21513d40330Schristos } else {
21613d40330Schristos int local_error = 0;
21713d40330Schristos if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
21813d40330Schristos TEST_info("ASN1_TIME_set_string(%s) failed", td->data);
21913d40330Schristos local_error = error = 1;
22013d40330Schristos }
22113d40330Schristos day = sec = 0;
22213d40330Schristos if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
22313d40330Schristos TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string() failed", day, sec, td->data);
22413d40330Schristos local_error = error = 1;
22513d40330Schristos }
22613d40330Schristos if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
22713d40330Schristos TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string() to baseline bad comparison", td->data);
22813d40330Schristos local_error = error = 1;
22913d40330Schristos }
23013d40330Schristos if (local_error)
23113d40330Schristos TEST_info("ASN1_TIME_set_string() = %*s", ptime->length, ptime->data);
23213d40330Schristos ASN1_TIME_free(ptime);
23313d40330Schristos }
23413d40330Schristos
23513d40330Schristos if (td->type == V_ASN1_UTCTIME) {
23613d40330Schristos ptime = ASN1_TIME_to_generalizedtime(&atime, NULL);
23713d40330Schristos if (td->convert_result == 1 && !TEST_ptr(ptime)) {
23813d40330Schristos TEST_info("ASN1_TIME_to_generalizedtime(%s) failed", atime.data);
23913d40330Schristos error = 1;
24013d40330Schristos } else if (td->convert_result == 0 && !TEST_ptr_null(ptime)) {
24113d40330Schristos TEST_info("ASN1_TIME_to_generalizedtime(%s) should have failed", atime.data);
24213d40330Schristos error = 1;
24313d40330Schristos }
24413d40330Schristos if (ptime != NULL && !TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
24513d40330Schristos TEST_info("ASN1_TIME_to_generalizedtime(%s->%s) bad result", atime.data, ptime->data);
24613d40330Schristos error = 1;
24713d40330Schristos }
24813d40330Schristos ASN1_TIME_free(ptime);
24913d40330Schristos }
25013d40330Schristos /* else cannot simply convert GENERALIZEDTIME to UTCTIME */
25113d40330Schristos
25213d40330Schristos if (error)
25313d40330Schristos TEST_error("atime=%s", atime.data);
25413d40330Schristos
25513d40330Schristos return !error;
25613d40330Schristos }
25713d40330Schristos
test_table_pos(int idx)25813d40330Schristos static int test_table_pos(int idx)
25913d40330Schristos {
26013d40330Schristos return test_table(tbl_testdata_pos, idx);
26113d40330Schristos }
26213d40330Schristos
test_table_neg(int idx)26313d40330Schristos static int test_table_neg(int idx)
26413d40330Schristos {
26513d40330Schristos return test_table(tbl_testdata_neg, idx);
26613d40330Schristos }
26713d40330Schristos
test_table_pos_64bit(int idx)26813d40330Schristos static int test_table_pos_64bit(int idx)
26913d40330Schristos {
27013d40330Schristos return test_table(tbl_testdata_pos_64bit, idx);
27113d40330Schristos }
27213d40330Schristos
test_table_neg_64bit(int idx)27313d40330Schristos static int test_table_neg_64bit(int idx)
27413d40330Schristos {
27513d40330Schristos return test_table(tbl_testdata_neg_64bit, idx);
27613d40330Schristos }
27713d40330Schristos
27813d40330Schristos struct compare_testdata {
27913d40330Schristos ASN1_TIME t1;
28013d40330Schristos ASN1_TIME t2;
28113d40330Schristos int result;
28213d40330Schristos };
28313d40330Schristos
28413d40330Schristos static unsigned char TODAY_GEN_STR[] = "20170825000000Z";
28513d40330Schristos static unsigned char TOMORROW_GEN_STR[] = "20170826000000Z";
28613d40330Schristos static unsigned char TODAY_UTC_STR[] = "170825000000Z";
28713d40330Schristos static unsigned char TOMORROW_UTC_STR[] = "170826000000Z";
28813d40330Schristos
28913d40330Schristos #define TODAY_GEN { sizeof(TODAY_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TODAY_GEN_STR, 0 }
29013d40330Schristos #define TOMORROW_GEN { sizeof(TOMORROW_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TOMORROW_GEN_STR, 0 }
29113d40330Schristos #define TODAY_UTC { sizeof(TODAY_UTC_STR)-1, V_ASN1_UTCTIME, TODAY_UTC_STR, 0 }
29213d40330Schristos #define TOMORROW_UTC { sizeof(TOMORROW_UTC_STR)-1, V_ASN1_UTCTIME, TOMORROW_UTC_STR, 0 }
29313d40330Schristos
29413d40330Schristos static struct compare_testdata tbl_compare_testdata[] = {
29513d40330Schristos { TODAY_GEN, TODAY_GEN, 0 },
29613d40330Schristos { TODAY_GEN, TODAY_UTC, 0 },
29713d40330Schristos { TODAY_GEN, TOMORROW_GEN, -1 },
29813d40330Schristos { TODAY_GEN, TOMORROW_UTC, -1 },
29913d40330Schristos
30013d40330Schristos { TODAY_UTC, TODAY_GEN, 0 },
30113d40330Schristos { TODAY_UTC, TODAY_UTC, 0 },
30213d40330Schristos { TODAY_UTC, TOMORROW_GEN, -1 },
30313d40330Schristos { TODAY_UTC, TOMORROW_UTC, -1 },
30413d40330Schristos
30513d40330Schristos { TOMORROW_GEN, TODAY_GEN, 1 },
30613d40330Schristos { TOMORROW_GEN, TODAY_UTC, 1 },
30713d40330Schristos { TOMORROW_GEN, TOMORROW_GEN, 0 },
30813d40330Schristos { TOMORROW_GEN, TOMORROW_UTC, 0 },
30913d40330Schristos
31013d40330Schristos { TOMORROW_UTC, TODAY_GEN, 1 },
31113d40330Schristos { TOMORROW_UTC, TODAY_UTC, 1 },
31213d40330Schristos { TOMORROW_UTC, TOMORROW_GEN, 0 },
31313d40330Schristos { TOMORROW_UTC, TOMORROW_UTC, 0 }
31413d40330Schristos };
31513d40330Schristos
test_table_compare(int idx)31613d40330Schristos static int test_table_compare(int idx)
31713d40330Schristos {
31813d40330Schristos struct compare_testdata *td = &tbl_compare_testdata[idx];
31913d40330Schristos
32013d40330Schristos return TEST_int_eq(ASN1_TIME_compare(&td->t1, &td->t2), td->result);
32113d40330Schristos }
32213d40330Schristos
test_time_dup(void)323*b0d17251Schristos static int test_time_dup(void)
324*b0d17251Schristos {
325*b0d17251Schristos int ret = 0;
326*b0d17251Schristos ASN1_TIME *asn1_time = NULL;
327*b0d17251Schristos ASN1_TIME *asn1_time_dup = NULL;
328*b0d17251Schristos ASN1_TIME *asn1_gentime = NULL;
329*b0d17251Schristos
330*b0d17251Schristos asn1_time = ASN1_TIME_adj(NULL, time(NULL), 0, 0);
331*b0d17251Schristos if (asn1_time == NULL) {
332*b0d17251Schristos TEST_info("Internal error.");
333*b0d17251Schristos goto err;
334*b0d17251Schristos }
335*b0d17251Schristos
336*b0d17251Schristos asn1_gentime = ASN1_TIME_to_generalizedtime(asn1_time, NULL);
337*b0d17251Schristos if (asn1_gentime == NULL) {
338*b0d17251Schristos TEST_info("Internal error.");
339*b0d17251Schristos goto err;
340*b0d17251Schristos }
341*b0d17251Schristos
342*b0d17251Schristos asn1_time_dup = ASN1_TIME_dup(asn1_time);
343*b0d17251Schristos if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
344*b0d17251Schristos TEST_info("ASN1_TIME_dup() failed.");
345*b0d17251Schristos goto err;
346*b0d17251Schristos }
347*b0d17251Schristos if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
348*b0d17251Schristos TEST_info("ASN1_TIME_dup() duplicated non-identical value.");
349*b0d17251Schristos goto err;
350*b0d17251Schristos }
351*b0d17251Schristos ASN1_STRING_free(asn1_time_dup);
352*b0d17251Schristos
353*b0d17251Schristos asn1_time_dup = ASN1_UTCTIME_dup(asn1_time);
354*b0d17251Schristos if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
355*b0d17251Schristos TEST_info("ASN1_UTCTIME_dup() failed.");
356*b0d17251Schristos goto err;
357*b0d17251Schristos }
358*b0d17251Schristos if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
359*b0d17251Schristos TEST_info("ASN1_UTCTIME_dup() duplicated non-identical UTCTIME value.");
360*b0d17251Schristos goto err;
361*b0d17251Schristos }
362*b0d17251Schristos ASN1_STRING_free(asn1_time_dup);
363*b0d17251Schristos
364*b0d17251Schristos asn1_time_dup = ASN1_GENERALIZEDTIME_dup(asn1_gentime);
365*b0d17251Schristos if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
366*b0d17251Schristos TEST_info("ASN1_GENERALIZEDTIME_dup() failed.");
367*b0d17251Schristos goto err;
368*b0d17251Schristos }
369*b0d17251Schristos if (!TEST_int_eq(ASN1_TIME_compare(asn1_gentime, asn1_time_dup), 0)) {
370*b0d17251Schristos TEST_info("ASN1_GENERALIZEDTIME_dup() dup'ed non-identical value.");
371*b0d17251Schristos goto err;
372*b0d17251Schristos }
373*b0d17251Schristos
374*b0d17251Schristos ret = 1;
375*b0d17251Schristos err:
376*b0d17251Schristos ASN1_STRING_free(asn1_time);
377*b0d17251Schristos ASN1_STRING_free(asn1_gentime);
378*b0d17251Schristos ASN1_STRING_free(asn1_time_dup);
379*b0d17251Schristos return ret;
380*b0d17251Schristos }
381*b0d17251Schristos
setup_tests(void)38213d40330Schristos int setup_tests(void)
38313d40330Schristos {
38413d40330Schristos /*
38513d40330Schristos * On platforms where |time_t| is an unsigned integer, t will be a
38613d40330Schristos * positive number.
38713d40330Schristos *
38813d40330Schristos * We check if we're on a platform with a signed |time_t| with '!(t > 0)'
38913d40330Schristos * because some compilers are picky if you do 't < 0', or even 't <= 0'
39013d40330Schristos * if |t| is unsigned.
39113d40330Schristos */
39213d40330Schristos time_t t = -1;
39313d40330Schristos /*
39413d40330Schristos * On some platforms, |time_t| is signed, but a negative value is an
39513d40330Schristos * error, and using it with gmtime() or localtime() generates a NULL.
39613d40330Schristos * If that is the case, we can't perform tests on negative values.
39713d40330Schristos */
39813d40330Schristos struct tm *ptm = localtime(&t);
39913d40330Schristos
40013d40330Schristos ADD_ALL_TESTS(test_table_pos, OSSL_NELEM(tbl_testdata_pos));
40113d40330Schristos if (!(t > 0) && ptm != NULL) {
40213d40330Schristos TEST_info("Adding negative-sign time_t tests");
40313d40330Schristos ADD_ALL_TESTS(test_table_neg, OSSL_NELEM(tbl_testdata_neg));
40413d40330Schristos }
40513d40330Schristos if (sizeof(time_t) > sizeof(uint32_t)) {
40613d40330Schristos TEST_info("Adding 64-bit time_t tests");
40713d40330Schristos ADD_ALL_TESTS(test_table_pos_64bit, OSSL_NELEM(tbl_testdata_pos_64bit));
40813d40330Schristos #ifndef __hpux
40913d40330Schristos if (!(t > 0) && ptm != NULL) {
41013d40330Schristos TEST_info("Adding negative-sign 64-bit time_t tests");
41113d40330Schristos ADD_ALL_TESTS(test_table_neg_64bit, OSSL_NELEM(tbl_testdata_neg_64bit));
41213d40330Schristos }
41313d40330Schristos #endif
41413d40330Schristos }
41513d40330Schristos ADD_ALL_TESTS(test_table_compare, OSSL_NELEM(tbl_compare_testdata));
416*b0d17251Schristos ADD_TEST(test_time_dup);
41713d40330Schristos return 1;
41813d40330Schristos }
419