xref: /src/crypto/openssl/test/pkey_meth_test.c (revision 8a62a2a5659d1839d8799b4274c04469d7f17c78)
1 /*
2  * Copyright 2016-2020 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 /* Internal tests for EVP_PKEY method ordering */
11 
12 /* We need to use some deprecated APIs */
13 #define OPENSSL_SUPPRESS_DEPRECATED
14 
15 #include <stdio.h>
16 #include <string.h>
17 
18 #include <openssl/evp.h>
19 #include "testutil.h"
20 
21 /* Test of EVP_PKEY_ASN1_METHOD ordering */
22 static int test_asn1_meths(void)
23 {
24     int i;
25     int prev = -1;
26     int good = 1;
27     int pkey_id;
28     const EVP_PKEY_ASN1_METHOD *ameth;
29 
30     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
31         ameth = EVP_PKEY_asn1_get0(i);
32         EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
33         if (pkey_id < prev)
34             good = 0;
35         prev = pkey_id;
36     }
37     if (!good) {
38         TEST_error("EVP_PKEY_ASN1_METHOD table out of order");
39         for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
40             const char *info;
41 
42             ameth = EVP_PKEY_asn1_get0(i);
43             EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, &info, NULL, ameth);
44             if (info == NULL)
45                 info = "<NO NAME>";
46             TEST_note("%d : %s : %s", pkey_id, OBJ_nid2ln(pkey_id), info);
47         }
48     }
49     return good;
50 }
51 
52 #ifndef OPENSSL_NO_DEPRECATED_3_0
53 /* Test of EVP_PKEY_METHOD ordering */
54 static int test_pkey_meths(void)
55 {
56     size_t i;
57     int prev = -1;
58     int good = 1;
59     int pkey_id;
60     const EVP_PKEY_METHOD *pmeth;
61 
62     for (i = 0; i < EVP_PKEY_meth_get_count(); i++) {
63         pmeth = EVP_PKEY_meth_get0(i);
64         EVP_PKEY_meth_get0_info(&pkey_id, NULL, pmeth);
65         if (pkey_id < prev)
66             good = 0;
67         prev = pkey_id;
68     }
69     if (!good) {
70         TEST_error("EVP_PKEY_METHOD table out of order");
71         for (i = 0; i < EVP_PKEY_meth_get_count(); i++) {
72             pmeth = EVP_PKEY_meth_get0(i);
73             EVP_PKEY_meth_get0_info(&pkey_id, NULL, pmeth);
74             TEST_note("%d : %s", pkey_id, OBJ_nid2ln(pkey_id));
75         }
76     }
77     return good;
78 }
79 #endif
80 
81 int setup_tests(void)
82 {
83     ADD_TEST(test_asn1_meths);
84 #ifndef OPENSSL_NO_DEPRECATED_3_0
85     ADD_TEST(test_pkey_meths);
86 #endif
87     return 1;
88 }
89