Fix a memory leak on successful load of CRL
[openssl.git] / test / x509_load_cert_file_test.c
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
8 #include <stdio.h>
9 #include <openssl/err.h>
10 #include <openssl/x509_vfy.h>
11
12 #include "testutil.h"
13
14 static const char *chain;
15 static const char *crl;
16
17 static int test_load_cert_file(void)
18 {
19     int ret = 0, i;
20     X509_STORE *store = NULL;
21     X509_LOOKUP *lookup = NULL;
22     STACK_OF(X509) *certs = NULL;
23     STACK_OF(X509_OBJECT) *objs = NULL;
24
25     if (!TEST_ptr(store = X509_STORE_new())
26         || !TEST_ptr(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()))
27         || !TEST_true(X509_load_cert_file(lookup, chain, X509_FILETYPE_PEM))
28         || !TEST_ptr(certs = X509_STORE_get1_all_certs(store))
29         || !TEST_int_eq(sk_X509_num(certs), 4)
30         || !TEST_ptr(objs = X509_STORE_get1_objects(store))
31         || !TEST_int_eq(sk_X509_OBJECT_num(objs), 4))
32         goto err;
33
34     for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
35         const X509_OBJECT *obj = sk_X509_OBJECT_value(objs, i);
36         if (!TEST_int_eq(X509_OBJECT_get_type(obj), X509_LU_X509))
37             goto err;
38     }
39
40     if (crl != NULL && !TEST_true(X509_load_crl_file(lookup, crl, X509_FILETYPE_PEM)))
41         goto err;
42
43     ret = 1;
44
45 err:
46     OSSL_STACK_OF_X509_free(certs);
47     sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
48     X509_STORE_free(store);
49     return ret;
50 }
51
52 OPT_TEST_DECLARE_USAGE("cert.pem [crl.pem]\n")
53
54 int setup_tests(void)
55 {
56     if (!test_skip_common_options()) {
57         TEST_error("Error parsing test options\n");
58         return 0;
59     }
60
61     chain = test_get_argument(0);
62     if (chain == NULL)
63         return 0;
64
65     crl = test_get_argument(1);
66
67     ADD_TEST(test_load_cert_file);
68     return 1;
69 }