x509_load_cert_file_test.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Licensed under the Apache License 2.0 (the "License"). You may not use
  3. * this file except in compliance with the License. You can obtain a copy
  4. * in the file LICENSE in the source distribution or at
  5. * https://www.openssl.org/source/license.html
  6. */
  7. #include <stdio.h>
  8. #include <openssl/err.h>
  9. #include <openssl/x509_vfy.h>
  10. #include "testutil.h"
  11. static const char *chain;
  12. static const char *crl;
  13. static int test_load_cert_file(void)
  14. {
  15. int ret = 0, i;
  16. X509_STORE *store = NULL;
  17. X509_LOOKUP *lookup = NULL;
  18. STACK_OF(X509) *certs = NULL;
  19. STACK_OF(X509_OBJECT) *objs = NULL;
  20. if (!TEST_ptr(store = X509_STORE_new())
  21. || !TEST_ptr(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()))
  22. || !TEST_true(X509_load_cert_file(lookup, chain, X509_FILETYPE_PEM))
  23. || !TEST_ptr(certs = X509_STORE_get1_all_certs(store))
  24. || !TEST_int_eq(sk_X509_num(certs), 4)
  25. || !TEST_ptr(objs = X509_STORE_get1_objects(store))
  26. || !TEST_int_eq(sk_X509_OBJECT_num(objs), 4))
  27. goto err;
  28. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  29. const X509_OBJECT *obj = sk_X509_OBJECT_value(objs, i);
  30. if (!TEST_int_eq(X509_OBJECT_get_type(obj), X509_LU_X509))
  31. goto err;
  32. }
  33. if (crl != NULL && !TEST_true(X509_load_crl_file(lookup, crl, X509_FILETYPE_PEM)))
  34. goto err;
  35. ret = 1;
  36. err:
  37. OSSL_STACK_OF_X509_free(certs);
  38. sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
  39. X509_STORE_free(store);
  40. return ret;
  41. }
  42. OPT_TEST_DECLARE_USAGE("cert.pem [crl.pem]\n")
  43. int setup_tests(void)
  44. {
  45. if (!test_skip_common_options()) {
  46. TEST_error("Error parsing test options\n");
  47. return 0;
  48. }
  49. chain = test_get_argument(0);
  50. if (chain == NULL)
  51. return 0;
  52. crl = test_get_argument(1);
  53. ADD_TEST(test_load_cert_file);
  54. return 1;
  55. }