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 */
22static 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    }
38    if (!good) {
39        TEST_error("EVP_PKEY_ASN1_METHOD table out of order");
40        for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
41            const char *info;
42
43            ameth = EVP_PKEY_asn1_get0(i);
44            EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, &info, NULL, ameth);
45            if (info == NULL)
46                info = "<NO NAME>";
47            TEST_note("%d : %s : %s", pkey_id, OBJ_nid2ln(pkey_id), info);
48        }
49    }
50    return good;
51}
52
53#ifndef OPENSSL_NO_DEPRECATED_3_0
54/* Test of EVP_PKEY_METHOD ordering */
55static int test_pkey_meths(void)
56{
57    size_t i;
58    int prev = -1;
59    int good = 1;
60    int pkey_id;
61    const EVP_PKEY_METHOD *pmeth;
62
63    for (i = 0; i < EVP_PKEY_meth_get_count(); i++) {
64        pmeth = EVP_PKEY_meth_get0(i);
65        EVP_PKEY_meth_get0_info(&pkey_id, NULL, pmeth);
66        if (pkey_id < prev)
67            good = 0;
68        prev = pkey_id;
69
70    }
71    if (!good) {
72        TEST_error("EVP_PKEY_METHOD table out of order");
73        for (i = 0; i < EVP_PKEY_meth_get_count(); i++) {
74            pmeth = EVP_PKEY_meth_get0(i);
75            EVP_PKEY_meth_get0_info(&pkey_id, NULL, pmeth);
76            TEST_note("%d : %s", pkey_id, OBJ_nid2ln(pkey_id));
77        }
78    }
79    return good;
80}
81#endif
82
83int setup_tests(void)
84{
85    ADD_TEST(test_asn1_meths);
86#ifndef OPENSSL_NO_DEPRECATED_3_0
87    ADD_TEST(test_pkey_meths);
88#endif
89    return 1;
90}
91