178172Sru/*
278172Sru * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
378172Sru * Copyright Nokia 2007-2019
478172Sru * Copyright Siemens AG 2015-2019
578172Sru *
678172Sru * Licensed under the Apache License 2.0 (the "License").  You may not use
778172Sru * this file except in compliance with the License.  You can obtain a copy
878172Sru * in the file LICENSE in the source distribution or at
978172Sru * https://www.openssl.org/source/license.html
1078172Sru */
1178172Sru
1278172Sru#include "helpers/cmp_testlib.h"
1378172Sru
1478172Srutypedef struct test_fixture {
1578172Sru    const char *test_case_name;
1678172Sru    int pkistatus;
1778172Sru    const char *str;  /* Not freed by tear_down */
1878172Sru    const char *text; /* Not freed by tear_down */
1978172Sru    int pkifailure;
2078172Sru} CMP_STATUS_TEST_FIXTURE;
2178172Sru
2278172Srustatic CMP_STATUS_TEST_FIXTURE *set_up(const char *const test_case_name)
2378172Sru{
2478172Sru    CMP_STATUS_TEST_FIXTURE *fixture;
2578172Sru
2678172Sru    if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
2778172Sru        return NULL;
2878172Sru    fixture->test_case_name = test_case_name;
2978172Sru    return fixture;
3078172Sru}
3178172Sru
3278172Srustatic void tear_down(CMP_STATUS_TEST_FIXTURE *fixture)
3378172Sru{
3478172Sru    OPENSSL_free(fixture);
3578172Sru}
3678172Sru
3778172Sru
3878172Sru/*
3978172Sru * Tests PKIStatusInfo creation and get-functions
4078172Sru */
4178172Srustatic int execute_PKISI_test(CMP_STATUS_TEST_FIXTURE *fixture)
4278172Sru{
4378172Sru    OSSL_CMP_PKISI *si = NULL;
4478172Sru    int status;
4578172Sru    ASN1_UTF8STRING *statusString = NULL;
4678172Sru    int res = 0, i;
4778172Sru
4878172Sru    if (!TEST_ptr(si = OSSL_CMP_STATUSINFO_new(fixture->pkistatus,
4978172Sru                                               fixture->pkifailure,
5078172Sru                                               fixture->text)))
5178172Sru        goto end;
5278172Sru
5378172Sru    status = ossl_cmp_pkisi_get_status(si);
5478172Sru    if (!TEST_int_eq(fixture->pkistatus, status)
5578172Sru            || !TEST_str_eq(fixture->str, ossl_cmp_PKIStatus_to_string(status)))
5678172Sru        goto end;
5778172Sru
5878172Sru    if (!TEST_ptr(statusString =
5978172Sru                  sk_ASN1_UTF8STRING_value(ossl_cmp_pkisi_get0_statusString(si),
6078172Sru                                           0))
61            || !TEST_mem_eq(fixture->text, strlen(fixture->text),
62                            (char *)statusString->data, statusString->length))
63        goto end;
64
65    if (!TEST_int_eq(fixture->pkifailure,
66                     ossl_cmp_pkisi_get_pkifailureinfo(si)))
67        goto end;
68    for (i = 0; i <= OSSL_CMP_PKIFAILUREINFO_MAX; i++)
69        if (!TEST_int_eq((fixture->pkifailure >> i) & 1,
70                         ossl_cmp_pkisi_check_pkifailureinfo(si, i)))
71            goto end;
72
73    res = 1;
74
75 end:
76    OSSL_CMP_PKISI_free(si);
77    return res;
78}
79
80static int test_PKISI(void)
81{
82    SETUP_TEST_FIXTURE(CMP_STATUS_TEST_FIXTURE, set_up);
83    fixture->pkistatus = OSSL_CMP_PKISTATUS_revocationNotification;
84    fixture->str = "PKIStatus: revocation notification - a revocation of the cert has occurred";
85    fixture->text = "this is an additional text describing the failure";
86    fixture->pkifailure = OSSL_CMP_CTX_FAILINFO_unsupportedVersion |
87        OSSL_CMP_CTX_FAILINFO_badDataFormat;
88    EXECUTE_TEST(execute_PKISI_test, tear_down);
89    return result;
90}
91
92
93
94void cleanup_tests(void)
95{
96    return;
97}
98
99int setup_tests(void)
100{
101    /*-
102     * this tests all of:
103     * OSSL_CMP_STATUSINFO_new()
104     * ossl_cmp_pkisi_get_status()
105     * ossl_cmp_PKIStatus_to_string()
106     * ossl_cmp_pkisi_get0_statusString()
107     * ossl_cmp_pkisi_get_pkifailureinfo()
108     * ossl_cmp_pkisi_check_pkifailureinfo()
109     */
110    ADD_TEST(test_PKISI);
111    return 1;
112}
113