/*
- * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
*
- * Licensed under the OpenSSL license (the "License"). You may not use
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
#include <openssl/evp.h>
#include <openssl/objects.h>
#include "testutil.h"
-#include "e_os.h"
+#include "internal/nelem.h"
struct testdata {
char *data; /* TIME string value */
int expected_type; /* expected type after set/set_string_gmt */
int check_result; /* check result */
time_t t; /* expected time_t*/
- int cmp_result; /* compariston to baseline result */
- int convert_result; /* convertion result */
+ int cmp_result; /* comparison to baseline result */
+ int convert_result; /* conversion result */
};
static struct testdata tbl_testdata_pos[] = {
/* ASSUMES SIGNED TIME_T */
static struct testdata tbl_testdata_neg[] = {
- { "19011213204552Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, -2147483648, -1, 0, },
+ { "19011213204552Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, INT_MIN, -1, 0, },
{ "691006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
{ "19691006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
};
+/* explicit casts to time_t short warnings on systems with 32-bit time_t */
static struct testdata tbl_testdata_pos_64bit[] = {
- { "20380119031408Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0x80000000, 1, 1, },
- { "20380119031409Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0x80000001, 1, 1, },
- { "380119031408Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0x80000000, 1, 1, },
- { "20500101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, 2524651200, 1, 0, },
+ { "20380119031408Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
+ { "20380119031409Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000001, 1, 1, },
+ { "380119031408Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
+ { "20500101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)0x967b1ec0, 1, 0, },
};
/* ASSUMES SIGNED TIME_T */
static struct testdata tbl_testdata_neg_64bit[] = {
- { "19011213204551Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, -2147483649, -1, 0, },
- { "19000101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, -2208945600, -1, 0, },
+ { "19011213204551Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2147483649LL, -1, 0, },
+ { "19000101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2208945600LL, -1, 0, },
};
/* A baseline time to compare to */
return test_table(tbl_testdata_neg_64bit, idx);
}
+struct compare_testdata {
+ ASN1_TIME t1;
+ ASN1_TIME t2;
+ int result;
+};
+
+static unsigned char TODAY_GEN_STR[] = "20170825000000Z";
+static unsigned char TOMORROW_GEN_STR[] = "20170826000000Z";
+static unsigned char TODAY_UTC_STR[] = "170825000000Z";
+static unsigned char TOMORROW_UTC_STR[] = "170826000000Z";
+
+#define TODAY_GEN { sizeof(TODAY_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TODAY_GEN_STR, 0 }
+#define TOMORROW_GEN { sizeof(TOMORROW_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TOMORROW_GEN_STR, 0 }
+#define TODAY_UTC { sizeof(TODAY_UTC_STR)-1, V_ASN1_UTCTIME, TODAY_UTC_STR, 0 }
+#define TOMORROW_UTC { sizeof(TOMORROW_UTC_STR)-1, V_ASN1_UTCTIME, TOMORROW_UTC_STR, 0 }
+
+static struct compare_testdata tbl_compare_testdata[] = {
+ { TODAY_GEN, TODAY_GEN, 0 },
+ { TODAY_GEN, TODAY_UTC, 0 },
+ { TODAY_GEN, TOMORROW_GEN, -1 },
+ { TODAY_GEN, TOMORROW_UTC, -1 },
+
+ { TODAY_UTC, TODAY_GEN, 0 },
+ { TODAY_UTC, TODAY_UTC, 0 },
+ { TODAY_UTC, TOMORROW_GEN, -1 },
+ { TODAY_UTC, TOMORROW_UTC, -1 },
+
+ { TOMORROW_GEN, TODAY_GEN, 1 },
+ { TOMORROW_GEN, TODAY_UTC, 1 },
+ { TOMORROW_GEN, TOMORROW_GEN, 0 },
+ { TOMORROW_GEN, TOMORROW_UTC, 0 },
+
+ { TOMORROW_UTC, TODAY_GEN, 1 },
+ { TOMORROW_UTC, TODAY_UTC, 1 },
+ { TOMORROW_UTC, TOMORROW_GEN, 0 },
+ { TOMORROW_UTC, TOMORROW_UTC, 0 }
+};
+
+static int test_table_compare(int idx)
+{
+ struct compare_testdata *td = &tbl_compare_testdata[idx];
+
+ return TEST_int_eq(ASN1_TIME_compare(&td->t1, &td->t2), td->result);
+}
+
+static int test_time_dup(void)
+{
+ int ret = 0;
+ ASN1_TIME *asn1_time = NULL;
+ ASN1_TIME *asn1_time_dup = NULL;
+ ASN1_TIME *asn1_gentime = NULL;
+
+ asn1_time = ASN1_TIME_adj(NULL, time(NULL), 0, 0);
+ if (asn1_time == NULL) {
+ TEST_info("Internal error.");
+ goto err;
+ }
+
+ asn1_gentime = ASN1_TIME_to_generalizedtime(asn1_time, NULL);
+ if (asn1_gentime == NULL) {
+ TEST_info("Internal error.");
+ goto err;
+ }
+
+ asn1_time_dup = ASN1_TIME_dup(asn1_time);
+ if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
+ TEST_info("ASN1_TIME_dup() failed.");
+ goto err;
+ }
+ if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
+ TEST_info("ASN1_TIME_dup() duplicated non-identical value.");
+ goto err;
+ }
+ ASN1_STRING_free(asn1_time_dup);
+
+ asn1_time_dup = ASN1_UTCTIME_dup(asn1_time);
+ if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
+ TEST_info("ASN1_UTCTIME_dup() failed.");
+ goto err;
+ }
+ if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
+ TEST_info("ASN1_UTCTIME_dup() duplicated non-identical UTCTIME value.");
+ goto err;
+ }
+ ASN1_STRING_free(asn1_time_dup);
+
+ asn1_time_dup = ASN1_GENERALIZEDTIME_dup(asn1_gentime);
+ if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
+ TEST_info("ASN1_GENERALIZEDTIME_dup() failed.");
+ goto err;
+ }
+ if (!TEST_int_eq(ASN1_TIME_compare(asn1_gentime, asn1_time_dup), 0)) {
+ TEST_info("ASN1_GENERALIZEDTIME_dup() dup'ed non-identical value.");
+ goto err;
+ }
+
+ ret = 1;
+ err:
+ ASN1_STRING_free(asn1_time);
+ ASN1_STRING_free(asn1_gentime);
+ ASN1_STRING_free(asn1_time_dup);
+ return ret;
+}
+
int setup_tests(void)
{
+ /*
+ * On platforms where |time_t| is an unsigned integer, t will be a
+ * positive number.
+ *
+ * We check if we're on a platform with a signed |time_t| with '!(t > 0)'
+ * because some compilers are picky if you do 't < 0', or even 't <= 0'
+ * if |t| is unsigned.
+ */
time_t t = -1;
+ /*
+ * On some platforms, |time_t| is signed, but a negative value is an
+ * error, and using it with gmtime() or localtime() generates a NULL.
+ * If that is the case, we can't perform tests on negative values.
+ */
struct tm *ptm = localtime(&t);
ADD_ALL_TESTS(test_table_pos, OSSL_NELEM(tbl_testdata_pos));
- if (ptm != NULL) {
+ if (!(t > 0) && ptm != NULL) {
TEST_info("Adding negative-sign time_t tests");
ADD_ALL_TESTS(test_table_neg, OSSL_NELEM(tbl_testdata_neg));
}
if (sizeof(time_t) > sizeof(uint32_t)) {
TEST_info("Adding 64-bit time_t tests");
ADD_ALL_TESTS(test_table_pos_64bit, OSSL_NELEM(tbl_testdata_pos_64bit));
- if (ptm != NULL) {
+#ifndef __hpux
+ if (!(t > 0) && ptm != NULL) {
TEST_info("Adding negative-sign 64-bit time_t tests");
ADD_ALL_TESTS(test_table_neg_64bit, OSSL_NELEM(tbl_testdata_neg_64bit));
}
+#endif
}
+ ADD_ALL_TESTS(test_table_compare, OSSL_NELEM(tbl_compare_testdata));
+ ADD_TEST(test_time_dup);
return 1;
}