testutil: always print errors on failure
[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 }
70
71 /**********************************************************************
72  *
73  * Test of ameth_lib's standard_methods
74  *
75  ***/
76
77 static SIMPLE_FIXTURE setup_standard_methods(const char *const test_case_name)
78 {
79     SIMPLE_FIXTURE fixture;
80     fixture.test_case_name = test_case_name;
81     return fixture;
82 }
83
84 #include "internal/asn1_int.h"
85 #include "../crypto/asn1/standard_methods.h"
86
87 static int execute_standard_methods(SIMPLE_FIXTURE fixture)
88 {
89     const EVP_PKEY_ASN1_METHOD **tmp;
90     int last_pkey_id = -1;
91     size_t i;
92
93     for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
94          i++, tmp++) {
95         if ((*tmp)->pkey_id < last_pkey_id) {
96             last_pkey_id = 0;
97             break;
98         }
99         last_pkey_id = (*tmp)->pkey_id;
100     }
101
102     if (last_pkey_id != 0) {
103         fprintf(stderr, "%s: Table order OK\n", fixture.test_section);
104         return 1;
105     }
106
107     for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
108          i++, tmp++)
109         fprintf(stderr, "%s: Index %" OSSLzu ", pkey ID %d, Name=%s\n",
110                fixture.test_section, i, (*tmp)->pkey_id,
111                OBJ_nid2sn((*tmp)->pkey_id));
112
113     return 0;
114 }
115
116 static void teardown_standard_methods(SIMPLE_FIXTURE fixture)
117 {
118 }
119
120 /**********************************************************************
121  *
122  * Test driver
123  *
124  ***/
125
126 static struct {
127     const char *section;
128     SIMPLE_FIXTURE (*setup)(const char *const test_case_name);
129     int (*execute)(SIMPLE_FIXTURE);
130     void (*teardown)(SIMPLE_FIXTURE);
131 } tests[] = {
132     {"asn1 tlb_standard", setup_tbl_standard, execute_tbl_standard,
133      teardown_tbl_standard},
134     {"asn1 standard_methods", setup_standard_methods, execute_standard_methods,
135      teardown_standard_methods}
136 };
137
138 static int drive_tests(int idx)
139 {
140     SETUP_TEST_FIXTURE(SIMPLE_FIXTURE, tests[idx].setup);
141     fixture.test_section = tests[idx].section;
142     EXECUTE_TEST(tests[idx].execute, tests[idx].teardown);
143 }
144
145 int main(int argc, char **argv)
146 {
147     ADD_ALL_TESTS(drive_tests, OSSL_NELEM(tests));
148
149     return run_tests(argv[0]);
150 }