ca_internals_test.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2021-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. #include <string.h>
  10. #include "apps.h"
  11. #include "testutil.h"
  12. #include "crypto/asn1.h"
  13. #define binname "ca_internals_test"
  14. char *default_config_file = NULL;
  15. static int test_do_updatedb(void)
  16. {
  17. CA_DB *db = NULL;
  18. time_t testdateutc;
  19. int rv;
  20. size_t argc = test_get_argument_count();
  21. BIO *bio_tmp;
  22. char *testdate;
  23. char *indexfile;
  24. int need64bit;
  25. int have64bit;
  26. if (argc != 4) {
  27. TEST_error("Usage: %s: do_updatedb dbfile testdate need64bit\n", binname);
  28. TEST_error(" testdate format: ASN1-String\n");
  29. return 0;
  30. }
  31. /*
  32. * if the test will only work with 64bit time_t and
  33. * the build only supports 32, assume the test as success
  34. */
  35. need64bit = (int)strtol(test_get_argument(3), NULL, 0);
  36. have64bit = sizeof(time_t) > sizeof(uint32_t);
  37. if (need64bit && !have64bit) {
  38. BIO_printf(bio_out, "skipping test (need64bit: %i, have64bit: %i)",
  39. need64bit, have64bit);
  40. return 1;
  41. }
  42. testdate = test_get_argument(2);
  43. testdateutc = ossl_asn1_string_to_time_t(testdate);
  44. if (TEST_time_t_lt(testdateutc, 0)) {
  45. return 0;
  46. }
  47. indexfile = test_get_argument(1);
  48. db = load_index(indexfile, NULL);
  49. if (TEST_ptr_null(db)) {
  50. return 0;
  51. }
  52. bio_tmp = bio_err;
  53. bio_err = bio_out;
  54. rv = do_updatedb(db, &testdateutc);
  55. bio_err = bio_tmp;
  56. if (rv > 0) {
  57. if (!TEST_true(save_index(indexfile, "new", db)))
  58. goto end;
  59. if (!TEST_true(rotate_index(indexfile, "new", "old")))
  60. goto end;
  61. }
  62. end:
  63. free_index(db);
  64. return 1;
  65. }
  66. int setup_tests(void)
  67. {
  68. char *command = test_get_argument(0);
  69. if (test_get_argument_count() < 1) {
  70. TEST_error("%s: no command specified for testing\n", binname);
  71. return 0;
  72. }
  73. if (strcmp(command, "do_updatedb") == 0)
  74. return test_do_updatedb();
  75. TEST_error("%s: command '%s' is not supported for testing\n", binname, command);
  76. return 0;
  77. }