76cc2edcfb07e8ab9b714c368b915bc50e02771f
[openssl.git] / test / x509_internal_test.c
1 /*
2  * Copyright 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 x509 and x509v3 modules */
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #include <openssl/x509.h>
16 #include <openssl/x509v3.h>
17 #include "testutil.h"
18 #include "e_os.h"
19
20 typedef struct {
21     const char *test_case_name;
22     const char *test_section;
23 } SIMPLE_FIXTURE;
24
25 /**********************************************************************
26  *
27  * Test of x509v3
28  *
29  ***/
30
31 static SIMPLE_FIXTURE setup_standard_exts(const char *const test_case_name)
32 {
33     SIMPLE_FIXTURE fixture;
34     fixture.test_case_name = test_case_name;
35     return fixture;
36 }
37
38 #include "../crypto/x509v3/ext_dat.h"
39 #include "../crypto/x509v3/standard_exts.h"
40
41 static int execute_standard_exts(SIMPLE_FIXTURE fixture)
42 {
43     size_t i;
44     int prev = -1, good = 1;
45     const X509V3_EXT_METHOD **tmp;
46
47     tmp = standard_exts;
48     for (i = 0; i < OSSL_NELEM(standard_exts); i++, tmp++) {
49         if ((*tmp)->ext_nid < prev)
50             good = 0;
51         prev = (*tmp)->ext_nid;
52
53     }
54     if (!good) {
55         tmp = standard_exts;
56         fprintf(stderr, "Extensions out of order!\n");
57         for (i = 0; i < STANDARD_EXTENSION_COUNT; i++, tmp++)
58             fprintf(stderr, "%d : %s\n", (*tmp)->ext_nid,
59                     OBJ_nid2sn((*tmp)->ext_nid));
60     } else {
61         fprintf(stderr, "Order OK\n");
62     }
63
64     return good;
65 }
66
67 static void teardown_standard_exts(SIMPLE_FIXTURE fixture)
68 {
69     ERR_print_errors_fp(stderr);
70 }
71
72 /**********************************************************************
73  *
74  * Test driver
75  *
76  ***/
77
78 static struct {
79     const char *section;
80     SIMPLE_FIXTURE (*setup)(const char *const test_case_name);
81     int (*execute)(SIMPLE_FIXTURE);
82     void (*teardown)(SIMPLE_FIXTURE);
83 } tests[] = {
84     {"standard_exts", setup_standard_exts, execute_standard_exts,
85      teardown_standard_exts},
86 };
87
88 static int drive_tests(int idx)
89 {
90     SETUP_TEST_FIXTURE(SIMPLE_FIXTURE, tests[idx].setup);
91     fixture.test_section = tests[idx].section;
92     EXECUTE_TEST(tests[idx].execute, tests[idx].teardown);
93 }
94
95 int main(int argc, char **argv)
96 {
97     ADD_ALL_TESTS(drive_tests, OSSL_NELEM(tests));
98
99     return run_tests(argv[0]);
100 }