INSTALL.md: Restore $ as command prompt indicator
[oweals/openssl.git] / test / cmp_status_test.c
1 /*
2  * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright Nokia 2007-2019
4  * Copyright Siemens AG 2015-2019
5  *
6  * Licensed under the Apache License 2.0 (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11
12 #include "cmp_testlib.h"
13
14 DEFINE_STACK_OF(ASN1_UTF8STRING)
15
16 typedef struct test_fixture {
17     const char *test_case_name;
18     int pkistatus;
19     const char *str;  /* Not freed by tear_down */
20     const char *text; /* Not freed by tear_down */
21     int pkifailure;
22 } CMP_STATUS_TEST_FIXTURE;
23
24 static CMP_STATUS_TEST_FIXTURE *set_up(const char *const test_case_name)
25 {
26     CMP_STATUS_TEST_FIXTURE *fixture;
27
28     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
29         return NULL;
30     fixture->test_case_name = test_case_name;
31     return fixture;
32 }
33
34 static void tear_down(CMP_STATUS_TEST_FIXTURE *fixture)
35 {
36     OPENSSL_free(fixture);
37 }
38
39
40 /*
41  * Tests PKIStatusInfo creation and get-functions
42  */
43 static int execute_PKISI_test(CMP_STATUS_TEST_FIXTURE *fixture)
44 {
45     OSSL_CMP_PKISI *si = NULL;
46     int status;
47     ASN1_UTF8STRING *statusString = NULL;
48     int res = 0, i;
49
50     if (!TEST_ptr(si = OSSL_CMP_STATUSINFO_new(fixture->pkistatus,
51                                                fixture->pkifailure,
52                                                fixture->text)))
53         goto end;
54
55     status = ossl_cmp_pkisi_get_status(si);
56     if (!TEST_int_eq(fixture->pkistatus, status)
57             || !TEST_str_eq(fixture->str, ossl_cmp_PKIStatus_to_string(status)))
58         goto end;
59
60     if (!TEST_ptr(statusString =
61                   sk_ASN1_UTF8STRING_value(ossl_cmp_pkisi_get0_statusString(si),
62                                            0))
63             || !TEST_str_eq(fixture->text, (char *)statusString->data))
64         goto end;
65
66     if (!TEST_int_eq(fixture->pkifailure,
67                      ossl_cmp_pkisi_get_pkifailureinfo(si)))
68         goto end;
69     for (i = 0; i <= OSSL_CMP_PKIFAILUREINFO_MAX; i++)
70         if (!TEST_int_eq((fixture->pkifailure >> i) & 1,
71                          ossl_cmp_pkisi_check_pkifailureinfo(si, i)))
72             goto end;
73
74     res = 1;
75
76  end:
77     OSSL_CMP_PKISI_free(si);
78     return res;
79 }
80
81 static int test_PKISI(void)
82 {
83     SETUP_TEST_FIXTURE(CMP_STATUS_TEST_FIXTURE, set_up);
84     fixture->pkistatus = OSSL_CMP_PKISTATUS_revocationNotification;
85     fixture->str = "PKIStatus: revocation notification - a revocation of the cert has occurred";
86     fixture->text = "this is an additional text describing the failure";
87     fixture->pkifailure = OSSL_CMP_CTX_FAILINFO_unsupportedVersion |
88         OSSL_CMP_CTX_FAILINFO_badDataFormat;
89     EXECUTE_TEST(execute_PKISI_test, tear_down);
90     return result;
91 }
92
93
94
95 void cleanup_tests(void)
96 {
97     return;
98 }
99
100 int setup_tests(void)
101 {
102     /*-
103      * this tests all of:
104      * OSSL_CMP_STATUSINFO_new()
105      * ossl_cmp_pkisi_get_status()
106      * ossl_cmp_PKIStatus_to_string()
107      * ossl_cmp_pkisi_get0_statusString()
108      * ossl_cmp_pkisi_get_pkifailureinfo()
109      * ossl_cmp_pkisi_check_pkifailureinfo()
110      */
111     ADD_TEST(test_PKISI);
112     return 1;
113 }