asn1_time_test.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright 1999-2022 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 tests for the asn1 module */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <crypto/asn1.h>
  13. #include <openssl/asn1.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/objects.h>
  16. #include "testutil.h"
  17. #include "internal/nelem.h"
  18. struct testdata {
  19. char *data; /* TIME string value */
  20. int type; /* GENERALIZED OR UTC */
  21. int expected_type; /* expected type after set/set_string_gmt */
  22. int check_result; /* check result */
  23. time_t t; /* expected time_t*/
  24. int cmp_result; /* comparison to baseline result */
  25. int convert_result; /* conversion result */
  26. };
  27. struct TESTDATA_asn1_to_utc {
  28. char *input;
  29. time_t expected;
  30. };
  31. static const struct TESTDATA_asn1_to_utc asn1_to_utc[] = {
  32. {
  33. /*
  34. * last second of standard time in central Europe in 2021
  35. * specified in GMT
  36. */
  37. "210328005959Z",
  38. 1616893199,
  39. },
  40. {
  41. /*
  42. * first second of daylight saving time in central Europe in 2021
  43. * specified in GMT
  44. */
  45. "210328010000Z",
  46. 1616893200,
  47. },
  48. {
  49. /*
  50. * last second of standard time in central Europe in 2021
  51. * specified in offset to GMT
  52. */
  53. "20210328015959+0100",
  54. 1616893199,
  55. },
  56. {
  57. /*
  58. * first second of daylight saving time in central Europe in 2021
  59. * specified in offset to GMT
  60. */
  61. "20210328030000+0200",
  62. 1616893200,
  63. },
  64. {
  65. /*
  66. * Invalid strings should get -1 as a result
  67. */
  68. "INVALID",
  69. -1,
  70. },
  71. };
  72. static struct testdata tbl_testdata_pos[] = {
  73. { "0", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, /* Bad time */
  74. { "ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  75. { "0ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  76. { "1-700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  77. { "`9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  78. { "19700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
  79. { "A00101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
  80. { "A9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  81. { "1A700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  82. { "19A00101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  83. { "197A0101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  84. { "1970A101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  85. { "19700A01000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  86. { "197001A1000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  87. { "1970010A000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  88. { "19700101A00000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  89. { "197001010A0000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  90. { "1970010100A000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  91. { "19700101000A00Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  92. { "197001010000A0Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  93. { "1970010100000AZ", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  94. { "700101000000X", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
  95. { "19700101000000X", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  96. { "19700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, /* Epoch begins */
  97. { "700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, /* ditto */
  98. { "20380119031407Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, }, /* Max 32bit time_t */
  99. { "380119031407Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, },
  100. { "20371231235959Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, }, /* Just before 2038 */
  101. { "20371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 1, }, /* Bad UTC time */
  102. { "371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, },
  103. { "19701006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, },
  104. { "701006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, },
  105. { "19991231000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, /* Match baseline */
  106. { "199912310000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, /* In various flavors */
  107. { "991231000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  108. { "9912310000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  109. { "9912310000+0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  110. { "199912310000+0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  111. { "9912310000-0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  112. { "199912310000-0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  113. { "199912310100+0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  114. { "199912302300-0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  115. { "199912302300-A000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, },
  116. { "199912302300-0A00", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, },
  117. { "9912310100+0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  118. { "9912302300-0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  119. };
  120. /* ASSUMES SIGNED TIME_T */
  121. static struct testdata tbl_testdata_neg[] = {
  122. { "19011213204552Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, INT_MIN, -1, 0, },
  123. { "691006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
  124. { "19691006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
  125. };
  126. /* explicit casts to time_t short warnings on systems with 32-bit time_t */
  127. static struct testdata tbl_testdata_pos_64bit[] = {
  128. { "20380119031408Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
  129. { "20380119031409Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000001, 1, 1, },
  130. { "380119031408Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
  131. { "20500101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)0x967b1ec0, 1, 0, },
  132. };
  133. /* ASSUMES SIGNED TIME_T */
  134. static struct testdata tbl_testdata_neg_64bit[] = {
  135. { "19011213204551Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2147483649LL, -1, 0, },
  136. { "19000101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2208945600LL, -1, 0, },
  137. };
  138. /* A baseline time to compare to */
  139. static ASN1_TIME gtime = {
  140. 15,
  141. V_ASN1_GENERALIZEDTIME,
  142. (unsigned char*)"19991231000000Z",
  143. 0
  144. };
  145. static time_t gtime_t = 946598400;
  146. static int test_table(struct testdata *tbl, int idx)
  147. {
  148. int error = 0;
  149. ASN1_TIME atime;
  150. ASN1_TIME *ptime;
  151. struct testdata *td = &tbl[idx];
  152. int day, sec;
  153. atime.data = (unsigned char*)td->data;
  154. atime.length = strlen((char*)atime.data);
  155. atime.type = td->type;
  156. atime.flags = 0;
  157. if (!TEST_int_eq(ASN1_TIME_check(&atime), td->check_result)) {
  158. TEST_info("ASN1_TIME_check(%s) unexpected result", atime.data);
  159. error = 1;
  160. }
  161. if (td->check_result == 0)
  162. return 1;
  163. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, td->t), 0)) {
  164. TEST_info("ASN1_TIME_cmp_time_t(%s vs %ld) compare failed", atime.data, (long)td->t);
  165. error = 1;
  166. }
  167. if (!TEST_true(ASN1_TIME_diff(&day, &sec, &atime, &atime))) {
  168. TEST_info("ASN1_TIME_diff(%s) to self failed", atime.data);
  169. error = 1;
  170. }
  171. if (!TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
  172. TEST_info("ASN1_TIME_diff(%s) to self not equal", atime.data);
  173. error = 1;
  174. }
  175. if (!TEST_true(ASN1_TIME_diff(&day, &sec, &gtime, &atime))) {
  176. TEST_info("ASN1_TIME_diff(%s) to baseline failed", atime.data);
  177. error = 1;
  178. } else if (!((td->cmp_result == 0 && TEST_true((day == 0 && sec == 0))) ||
  179. (td->cmp_result == -1 && TEST_true((day < 0 || sec < 0))) ||
  180. (td->cmp_result == 1 && TEST_true((day > 0 || sec > 0))))) {
  181. TEST_info("ASN1_TIME_diff(%s) to baseline bad comparison", atime.data);
  182. error = 1;
  183. }
  184. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, gtime_t), td->cmp_result)) {
  185. TEST_info("ASN1_TIME_cmp_time_t(%s) to baseline bad comparison", atime.data);
  186. error = 1;
  187. }
  188. ptime = ASN1_TIME_set(NULL, td->t);
  189. if (!TEST_ptr(ptime)) {
  190. TEST_info("ASN1_TIME_set(%ld) failed", (long)td->t);
  191. error = 1;
  192. } else {
  193. int local_error = 0;
  194. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
  195. TEST_info("ASN1_TIME_set(%ld) compare failed (%s->%s)",
  196. (long)td->t, td->data, ptime->data);
  197. local_error = error = 1;
  198. }
  199. if (!TEST_int_eq(ptime->type, td->expected_type)) {
  200. TEST_info("ASN1_TIME_set(%ld) unexpected type", (long)td->t);
  201. local_error = error = 1;
  202. }
  203. if (local_error)
  204. TEST_info("ASN1_TIME_set() = %*s", ptime->length, ptime->data);
  205. ASN1_TIME_free(ptime);
  206. }
  207. ptime = ASN1_TIME_new();
  208. if (!TEST_ptr(ptime)) {
  209. TEST_info("ASN1_TIME_new() failed");
  210. error = 1;
  211. } else {
  212. int local_error = 0;
  213. if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
  214. TEST_info("ASN1_TIME_set_string_gmt(%s) failed", td->data);
  215. local_error = error = 1;
  216. }
  217. if (!TEST_int_eq(ASN1_TIME_normalize(ptime), td->check_result)) {
  218. TEST_info("ASN1_TIME_normalize(%s) failed", td->data);
  219. local_error = error = 1;
  220. }
  221. if (!TEST_int_eq(ptime->type, td->expected_type)) {
  222. TEST_info("ASN1_TIME_set_string_gmt(%s) unexpected type", td->data);
  223. local_error = error = 1;
  224. }
  225. day = sec = 0;
  226. if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
  227. TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string_gmt() failed", day, sec, td->data);
  228. local_error = error = 1;
  229. }
  230. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
  231. TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string_gnt() to baseline bad comparison", td->data);
  232. local_error = error = 1;
  233. }
  234. if (local_error)
  235. TEST_info("ASN1_TIME_set_string_gmt() = %*s", ptime->length, ptime->data);
  236. ASN1_TIME_free(ptime);
  237. }
  238. ptime = ASN1_TIME_new();
  239. if (!TEST_ptr(ptime)) {
  240. TEST_info("ASN1_TIME_new() failed");
  241. error = 1;
  242. } else {
  243. int local_error = 0;
  244. if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
  245. TEST_info("ASN1_TIME_set_string(%s) failed", td->data);
  246. local_error = error = 1;
  247. }
  248. day = sec = 0;
  249. if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
  250. TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string() failed", day, sec, td->data);
  251. local_error = error = 1;
  252. }
  253. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
  254. TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string() to baseline bad comparison", td->data);
  255. local_error = error = 1;
  256. }
  257. if (local_error)
  258. TEST_info("ASN1_TIME_set_string() = %*s", ptime->length, ptime->data);
  259. ASN1_TIME_free(ptime);
  260. }
  261. if (td->type == V_ASN1_UTCTIME) {
  262. ptime = ASN1_TIME_to_generalizedtime(&atime, NULL);
  263. if (td->convert_result == 1 && !TEST_ptr(ptime)) {
  264. TEST_info("ASN1_TIME_to_generalizedtime(%s) failed", atime.data);
  265. error = 1;
  266. } else if (td->convert_result == 0 && !TEST_ptr_null(ptime)) {
  267. TEST_info("ASN1_TIME_to_generalizedtime(%s) should have failed", atime.data);
  268. error = 1;
  269. }
  270. if (ptime != NULL && !TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
  271. TEST_info("ASN1_TIME_to_generalizedtime(%s->%s) bad result", atime.data, ptime->data);
  272. error = 1;
  273. }
  274. ASN1_TIME_free(ptime);
  275. }
  276. /* else cannot simply convert GENERALIZEDTIME to UTCTIME */
  277. if (error)
  278. TEST_error("atime=%s", atime.data);
  279. return !error;
  280. }
  281. static int test_table_pos(int idx)
  282. {
  283. return test_table(tbl_testdata_pos, idx);
  284. }
  285. static int test_table_neg(int idx)
  286. {
  287. return test_table(tbl_testdata_neg, idx);
  288. }
  289. static int test_table_pos_64bit(int idx)
  290. {
  291. return test_table(tbl_testdata_pos_64bit, idx);
  292. }
  293. static int test_table_neg_64bit(int idx)
  294. {
  295. return test_table(tbl_testdata_neg_64bit, idx);
  296. }
  297. struct compare_testdata {
  298. ASN1_TIME t1;
  299. ASN1_TIME t2;
  300. int result;
  301. };
  302. static unsigned char TODAY_GEN_STR[] = "20170825000000Z";
  303. static unsigned char TOMORROW_GEN_STR[] = "20170826000000Z";
  304. static unsigned char TODAY_UTC_STR[] = "170825000000Z";
  305. static unsigned char TOMORROW_UTC_STR[] = "170826000000Z";
  306. #define TODAY_GEN { sizeof(TODAY_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TODAY_GEN_STR, 0 }
  307. #define TOMORROW_GEN { sizeof(TOMORROW_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TOMORROW_GEN_STR, 0 }
  308. #define TODAY_UTC { sizeof(TODAY_UTC_STR)-1, V_ASN1_UTCTIME, TODAY_UTC_STR, 0 }
  309. #define TOMORROW_UTC { sizeof(TOMORROW_UTC_STR)-1, V_ASN1_UTCTIME, TOMORROW_UTC_STR, 0 }
  310. static struct compare_testdata tbl_compare_testdata[] = {
  311. { TODAY_GEN, TODAY_GEN, 0 },
  312. { TODAY_GEN, TODAY_UTC, 0 },
  313. { TODAY_GEN, TOMORROW_GEN, -1 },
  314. { TODAY_GEN, TOMORROW_UTC, -1 },
  315. { TODAY_UTC, TODAY_GEN, 0 },
  316. { TODAY_UTC, TODAY_UTC, 0 },
  317. { TODAY_UTC, TOMORROW_GEN, -1 },
  318. { TODAY_UTC, TOMORROW_UTC, -1 },
  319. { TOMORROW_GEN, TODAY_GEN, 1 },
  320. { TOMORROW_GEN, TODAY_UTC, 1 },
  321. { TOMORROW_GEN, TOMORROW_GEN, 0 },
  322. { TOMORROW_GEN, TOMORROW_UTC, 0 },
  323. { TOMORROW_UTC, TODAY_GEN, 1 },
  324. { TOMORROW_UTC, TODAY_UTC, 1 },
  325. { TOMORROW_UTC, TOMORROW_GEN, 0 },
  326. { TOMORROW_UTC, TOMORROW_UTC, 0 }
  327. };
  328. static int test_table_compare(int idx)
  329. {
  330. struct compare_testdata *td = &tbl_compare_testdata[idx];
  331. return TEST_int_eq(ASN1_TIME_compare(&td->t1, &td->t2), td->result);
  332. }
  333. static int test_time_dup(void)
  334. {
  335. int ret = 0;
  336. ASN1_TIME *asn1_time = NULL;
  337. ASN1_TIME *asn1_time_dup = NULL;
  338. ASN1_TIME *asn1_gentime = NULL;
  339. asn1_time = ASN1_TIME_adj(NULL, time(NULL), 0, 0);
  340. if (asn1_time == NULL) {
  341. TEST_info("Internal error.");
  342. goto err;
  343. }
  344. asn1_gentime = ASN1_TIME_to_generalizedtime(asn1_time, NULL);
  345. if (asn1_gentime == NULL) {
  346. TEST_info("Internal error.");
  347. goto err;
  348. }
  349. asn1_time_dup = ASN1_TIME_dup(asn1_time);
  350. if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
  351. TEST_info("ASN1_TIME_dup() failed.");
  352. goto err;
  353. }
  354. if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
  355. TEST_info("ASN1_TIME_dup() duplicated non-identical value.");
  356. goto err;
  357. }
  358. ASN1_STRING_free(asn1_time_dup);
  359. asn1_time_dup = ASN1_UTCTIME_dup(asn1_time);
  360. if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
  361. TEST_info("ASN1_UTCTIME_dup() failed.");
  362. goto err;
  363. }
  364. if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
  365. TEST_info("ASN1_UTCTIME_dup() duplicated non-identical UTCTIME value.");
  366. goto err;
  367. }
  368. ASN1_STRING_free(asn1_time_dup);
  369. asn1_time_dup = ASN1_GENERALIZEDTIME_dup(asn1_gentime);
  370. if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
  371. TEST_info("ASN1_GENERALIZEDTIME_dup() failed.");
  372. goto err;
  373. }
  374. if (!TEST_int_eq(ASN1_TIME_compare(asn1_gentime, asn1_time_dup), 0)) {
  375. TEST_info("ASN1_GENERALIZEDTIME_dup() dup'ed non-identical value.");
  376. goto err;
  377. }
  378. ret = 1;
  379. err:
  380. ASN1_STRING_free(asn1_time);
  381. ASN1_STRING_free(asn1_gentime);
  382. ASN1_STRING_free(asn1_time_dup);
  383. return ret;
  384. }
  385. static int convert_asn1_to_time_t(int idx)
  386. {
  387. time_t testdateutc;
  388. testdateutc = ossl_asn1_string_to_time_t(asn1_to_utc[idx].input);
  389. if (!TEST_time_t_eq(testdateutc, asn1_to_utc[idx].expected)) {
  390. TEST_info("ossl_asn1_string_to_time_t (%s) failed: expected %lli, got %lli\n",
  391. asn1_to_utc[idx].input,
  392. (long long int)asn1_to_utc[idx].expected,
  393. (long long int)testdateutc);
  394. return 0;
  395. }
  396. return 1;
  397. }
  398. int setup_tests(void)
  399. {
  400. /*
  401. * On platforms where |time_t| is an unsigned integer, t will be a
  402. * positive number.
  403. *
  404. * We check if we're on a platform with a signed |time_t| with '!(t > 0)'
  405. * because some compilers are picky if you do 't < 0', or even 't <= 0'
  406. * if |t| is unsigned.
  407. */
  408. time_t t = -1;
  409. /*
  410. * On some platforms, |time_t| is signed, but a negative value is an
  411. * error, and using it with gmtime() or localtime() generates a NULL.
  412. * If that is the case, we can't perform tests on negative values.
  413. */
  414. struct tm *ptm = localtime(&t);
  415. ADD_ALL_TESTS(test_table_pos, OSSL_NELEM(tbl_testdata_pos));
  416. if (!(t > 0) && ptm != NULL) {
  417. TEST_info("Adding negative-sign time_t tests");
  418. ADD_ALL_TESTS(test_table_neg, OSSL_NELEM(tbl_testdata_neg));
  419. }
  420. if (sizeof(time_t) > sizeof(uint32_t)) {
  421. TEST_info("Adding 64-bit time_t tests");
  422. ADD_ALL_TESTS(test_table_pos_64bit, OSSL_NELEM(tbl_testdata_pos_64bit));
  423. #ifndef __hpux
  424. if (!(t > 0) && ptm != NULL) {
  425. TEST_info("Adding negative-sign 64-bit time_t tests");
  426. ADD_ALL_TESTS(test_table_neg_64bit, OSSL_NELEM(tbl_testdata_neg_64bit));
  427. }
  428. #endif
  429. }
  430. ADD_ALL_TESTS(test_table_compare, OSSL_NELEM(tbl_compare_testdata));
  431. ADD_TEST(test_time_dup);
  432. ADD_ALL_TESTS(convert_asn1_to_time_t, OSSL_NELEM(asn1_to_utc));
  433. return 1;
  434. }