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