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