xref: /src/crypto/openssl/test/asn1_stable_parse_test.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2023-2024 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 
10 #include <openssl/evp.h>
11 #include "testutil.h"
12 
13 static char *config_file = NULL;
14 
15 typedef enum OPTION_choice {
16     OPT_ERR = -1,
17     OPT_EOF = 0,
18     OPT_CONFIG_FILE,
19     OPT_TEST_ENUM
20 } OPTION_CHOICE;
21 
test_get_options(void)22 const OPTIONS *test_get_options(void)
23 {
24     static const OPTIONS options[] = {
25         OPT_TEST_OPTIONS_DEFAULT_USAGE,
26         { "config", OPT_CONFIG_FILE, '<',
27             "The configuration file to use for the libctx" },
28         { NULL }
29     };
30     return options;
31 }
32 
33 /*
34  * Test that parsing a config file with incorrect stable settings aren't parsed
35  * and appropriate errors are raised
36  */
test_asn1_stable_parse(void)37 static int test_asn1_stable_parse(void)
38 {
39     int testret = 0;
40     unsigned long errcode;
41     OSSL_LIB_CTX *newctx = OSSL_LIB_CTX_new();
42 
43     if (!TEST_ptr(newctx))
44         goto out;
45 
46     if (!TEST_int_eq(OSSL_LIB_CTX_load_config(newctx, config_file), 0))
47         goto err;
48 
49     errcode = ERR_peek_error();
50     if (ERR_GET_LIB(errcode) != ERR_LIB_ASN1)
51         goto err;
52     if (ERR_GET_REASON(errcode) != ASN1_R_INVALID_STRING_TABLE_VALUE)
53         goto err;
54 
55     ERR_clear_error();
56 
57     testret = 1;
58 err:
59     OSSL_LIB_CTX_free(newctx);
60 out:
61     return testret;
62 }
63 
setup_tests(void)64 int setup_tests(void)
65 {
66     OPTION_CHOICE o;
67 
68     while ((o = opt_next()) != OPT_EOF) {
69         switch (o) {
70         case OPT_CONFIG_FILE:
71             config_file = opt_arg();
72             break;
73         default:
74             return 0;
75         }
76     }
77 
78     ADD_TEST(test_asn1_stable_parse);
79     return 1;
80 }
81