fc0ac202b4154a644c41e3161d12860b6a8c7df7
[openssl.git] / test / asn1_internal_test.c
1 /*
2  * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 the asn1 module */
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #include <openssl/asn1.h>
16 #include <openssl/evp.h>
17 #include <openssl/objects.h>
18 #include "testutil.h"
19 #include "e_os.h"
20
21 typedef struct {
22     const char *test_case_name;
23     const char *test_section;
24 } SIMPLE_FIXTURE;
25
26 /**********************************************************************
27  *
28  * Test of a_strnid's tbl_standard
29  *
30  ***/
31
32 static SIMPLE_FIXTURE setup_tbl_standard(const char *const test_case_name)
33 {
34     SIMPLE_FIXTURE fixture;
35     fixture.test_case_name = test_case_name;
36     return fixture;
37 }
38
39 #include "../crypto/asn1/tbl_standard.h"
40
41 static int execute_tbl_standard(SIMPLE_FIXTURE fixture)
42 {
43     const ASN1_STRING_TABLE *tmp;
44     int last_nid = -1;
45     size_t i;
46
47     for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
48         if (tmp->nid < last_nid) {
49             last_nid = 0;
50             break;
51         }
52         last_nid = tmp->nid;
53     }
54
55     if (last_nid != 0) {
56         fprintf(stderr, "%s: Table order OK\n", fixture.test_section);
57         return 1;
58     }
59
60     for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
61         fprintf(stderr, "%s: Index %" OSSLzu ", NID %d, Name=%s\n",
62                fixture.test_section, i, tmp->nid, OBJ_nid2ln(tmp->nid));
63
64     return 0;
65 }
66
67 static void teardown_tbl_standard(SIMPLE_FIXTURE fixture)
68 {
69     ERR_print_errors_fp(stderr);
70 }
71
72 /**********************************************************************
73  *
74  * Test of ameth_lib's standard_methods
75  *
76  ***/
77
78 static SIMPLE_FIXTURE setup_standard_methods(const char *const test_case_name)
79 {
80     SIMPLE_FIXTURE fixture;
81     fixture.test_case_name = test_case_name;
82     return fixture;
83 }
84
85 #include "internal/asn1_int.h"
86 #include "../crypto/asn1/standard_methods.h"
87
88 static int execute_standard_methods(SIMPLE_FIXTURE fixture)
89 {
90     const EVP_PKEY_ASN1_METHOD **tmp;
91     int last_pkey_id = -1;
92     size_t i;
93
94     for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
95          i++, tmp++) {
96         if ((*tmp)->pkey_id < last_pkey_id) {
97             last_pkey_id = 0;
98             break;
99         }
100         last_pkey_id = (*tmp)->pkey_id;
101     }
102
103     if (last_pkey_id != 0) {
104         fprintf(stderr, "%s: Table order OK\n", fixture.test_section);
105         return 1;
106     }
107
108     for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
109          i++, tmp++)
110         fprintf(stderr, "%s: Index %" OSSLzu ", pkey ID %d, Name=%s\n",
111                fixture.test_section, i, (*tmp)->pkey_id,
112                OBJ_nid2sn((*tmp)->pkey_id));
113
114     return 0;
115 }
116
117 static void teardown_standard_methods(SIMPLE_FIXTURE fixture)
118 {
119     ERR_print_errors_fp(stderr);
120 }
121
122 /**********************************************************************
123  *
124  * Test driver
125  *
126  ***/
127
128 static struct {
129     const char *section;
130     SIMPLE_FIXTURE (*setup)(const char *const test_case_name);
131     int (*execute)(SIMPLE_FIXTURE);
132     void (*teardown)(SIMPLE_FIXTURE);
133 } tests[] = {
134     {"asn1 tlb_standard", setup_tbl_standard, execute_tbl_standard,
135      teardown_tbl_standard},
136     {"asn1 standard_methods", setup_standard_methods, execute_standard_methods,
137      teardown_standard_methods}
138 };
139
140 static int drive_tests(int idx)
141 {
142     SETUP_TEST_FIXTURE(SIMPLE_FIXTURE, tests[idx].setup);
143     fixture.test_section = tests[idx].section;
144     EXECUTE_TEST(tests[idx].execute, tests[idx].teardown);
145 }
146
147 int main(int argc, char **argv)
148 {
149     ADD_ALL_TESTS(drive_tests, OSSL_NELEM(tests));
150
151     return run_tests(argv[0]);
152 }