1 /*
2 * Copyright 2007-2021 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 "helpers/cmp_testlib.h"
13
14 typedef struct test_fixture {
15 const char *test_case_name;
16 int pkistatus;
17 const char *str; /* Not freed by tear_down */
18 const char *text; /* Not freed by tear_down */
19 int pkifailure;
20 } CMP_STATUS_TEST_FIXTURE;
21
set_up(const char * const test_case_name)22 static CMP_STATUS_TEST_FIXTURE *set_up(const char *const test_case_name)
23 {
24 CMP_STATUS_TEST_FIXTURE *fixture;
25
26 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
27 return NULL;
28 fixture->test_case_name = test_case_name;
29 return fixture;
30 }
31
tear_down(CMP_STATUS_TEST_FIXTURE * fixture)32 static void tear_down(CMP_STATUS_TEST_FIXTURE *fixture)
33 {
34 OPENSSL_free(fixture);
35 }
36
37 /*
38 * Tests PKIStatusInfo creation and get-functions
39 */
execute_PKISI_test(CMP_STATUS_TEST_FIXTURE * fixture)40 static int execute_PKISI_test(CMP_STATUS_TEST_FIXTURE *fixture)
41 {
42 OSSL_CMP_PKISI *si = NULL;
43 int status;
44 ASN1_UTF8STRING *statusString = NULL;
45 int res = 0, i;
46
47 if (!TEST_ptr(si = OSSL_CMP_STATUSINFO_new(fixture->pkistatus,
48 fixture->pkifailure,
49 fixture->text)))
50 goto end;
51
52 status = ossl_cmp_pkisi_get_status(si);
53 if (!TEST_int_eq(fixture->pkistatus, status)
54 || !TEST_str_eq(fixture->str, ossl_cmp_PKIStatus_to_string(status)))
55 goto end;
56
57 if (!TEST_ptr(statusString = sk_ASN1_UTF8STRING_value(ossl_cmp_pkisi_get0_statusString(si),
58 0))
59 || !TEST_mem_eq(fixture->text, strlen(fixture->text),
60 (char *)statusString->data, statusString->length))
61 goto end;
62
63 if (!TEST_int_eq(fixture->pkifailure,
64 ossl_cmp_pkisi_get_pkifailureinfo(si)))
65 goto end;
66 for (i = 0; i <= OSSL_CMP_PKIFAILUREINFO_MAX; i++)
67 if (!TEST_int_eq((fixture->pkifailure >> i) & 1,
68 ossl_cmp_pkisi_check_pkifailureinfo(si, i)))
69 goto end;
70
71 res = 1;
72
73 end:
74 OSSL_CMP_PKISI_free(si);
75 return res;
76 }
77
test_PKISI(void)78 static int test_PKISI(void)
79 {
80 SETUP_TEST_FIXTURE(CMP_STATUS_TEST_FIXTURE, set_up);
81 fixture->pkistatus = OSSL_CMP_PKISTATUS_revocationNotification;
82 fixture->str = "PKIStatus: revocation notification - a revocation of the cert has occurred";
83 fixture->text = "this is an additional text describing the failure";
84 fixture->pkifailure = OSSL_CMP_CTX_FAILINFO_unsupportedVersion | OSSL_CMP_CTX_FAILINFO_badDataFormat;
85 EXECUTE_TEST(execute_PKISI_test, tear_down);
86 return result;
87 }
88
cleanup_tests(void)89 void cleanup_tests(void)
90 {
91 return;
92 }
93
setup_tests(void)94 int setup_tests(void)
95 {
96 /*-
97 * this tests all of:
98 * OSSL_CMP_STATUSINFO_new()
99 * ossl_cmp_pkisi_get_status()
100 * ossl_cmp_PKIStatus_to_string()
101 * ossl_cmp_pkisi_get0_statusString()
102 * ossl_cmp_pkisi_get_pkifailureinfo()
103 * ossl_cmp_pkisi_check_pkifailureinfo()
104 */
105 ADD_TEST(test_PKISI);
106 return 1;
107 }
108