xref: /third_party/openssl/test/cmp_asn_test.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci * Copyright Nokia 2007-2019
4e1051a39Sopenharmony_ci * Copyright Siemens AG 2015-2019
5e1051a39Sopenharmony_ci *
6e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
7e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
8e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
9e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
10e1051a39Sopenharmony_ci */
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ci#include "helpers/cmp_testlib.h"
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_cistatic unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_citypedef struct test_fixture {
17e1051a39Sopenharmony_ci    const char *test_case_name;
18e1051a39Sopenharmony_ci    int expected;
19e1051a39Sopenharmony_ci    ASN1_OCTET_STRING *src_string;
20e1051a39Sopenharmony_ci    ASN1_OCTET_STRING *tgt_string;
21e1051a39Sopenharmony_ci
22e1051a39Sopenharmony_ci} CMP_ASN_TEST_FIXTURE;
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_cistatic CMP_ASN_TEST_FIXTURE *set_up(const char *const test_case_name)
25e1051a39Sopenharmony_ci{
26e1051a39Sopenharmony_ci    CMP_ASN_TEST_FIXTURE *fixture;
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ci    if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
29e1051a39Sopenharmony_ci        return NULL;
30e1051a39Sopenharmony_ci    fixture->test_case_name = test_case_name;
31e1051a39Sopenharmony_ci    return fixture;
32e1051a39Sopenharmony_ci}
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_cistatic void tear_down(CMP_ASN_TEST_FIXTURE *fixture)
35e1051a39Sopenharmony_ci{
36e1051a39Sopenharmony_ci    ASN1_OCTET_STRING_free(fixture->src_string);
37e1051a39Sopenharmony_ci    if (fixture->tgt_string != fixture->src_string)
38e1051a39Sopenharmony_ci        ASN1_OCTET_STRING_free(fixture->tgt_string);
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_ci    OPENSSL_free(fixture);
41e1051a39Sopenharmony_ci}
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_cistatic int execute_cmp_asn1_get_int_test(CMP_ASN_TEST_FIXTURE *fixture)
44e1051a39Sopenharmony_ci{
45e1051a39Sopenharmony_ci    int res;
46e1051a39Sopenharmony_ci    ASN1_INTEGER *asn1integer = ASN1_INTEGER_new();
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ci    if (!TEST_ptr(asn1integer))
49e1051a39Sopenharmony_ci        return 0;
50e1051a39Sopenharmony_ci    if (!TEST_true(ASN1_INTEGER_set(asn1integer, 77))) {
51e1051a39Sopenharmony_ci        ASN1_INTEGER_free(asn1integer);
52e1051a39Sopenharmony_ci        return 0;
53e1051a39Sopenharmony_ci    }
54e1051a39Sopenharmony_ci    res = TEST_int_eq(77, ossl_cmp_asn1_get_int(asn1integer));
55e1051a39Sopenharmony_ci    ASN1_INTEGER_free(asn1integer);
56e1051a39Sopenharmony_ci    return res;
57e1051a39Sopenharmony_ci}
58e1051a39Sopenharmony_ci
59e1051a39Sopenharmony_cistatic int test_cmp_asn1_get_int(void)
60e1051a39Sopenharmony_ci{
61e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
62e1051a39Sopenharmony_ci    fixture->expected = 1;
63e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_cmp_asn1_get_int_test, tear_down);
64e1051a39Sopenharmony_ci    return result;
65e1051a39Sopenharmony_ci}
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_cistatic int execute_CMP_ASN1_OCTET_STRING_set1_test(CMP_ASN_TEST_FIXTURE *
68e1051a39Sopenharmony_ci                                                   fixture)
69e1051a39Sopenharmony_ci{
70e1051a39Sopenharmony_ci    if (!TEST_int_eq(fixture->expected,
71e1051a39Sopenharmony_ci                     ossl_cmp_asn1_octet_string_set1(&fixture->tgt_string,
72e1051a39Sopenharmony_ci                                                     fixture->src_string)))
73e1051a39Sopenharmony_ci        return 0;
74e1051a39Sopenharmony_ci    if (fixture->expected != 0)
75e1051a39Sopenharmony_ci        return TEST_int_eq(0, ASN1_OCTET_STRING_cmp(fixture->tgt_string,
76e1051a39Sopenharmony_ci                                                    fixture->src_string));
77e1051a39Sopenharmony_ci    return 1;
78e1051a39Sopenharmony_ci}
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_cistatic int test_ASN1_OCTET_STRING_set(void)
81e1051a39Sopenharmony_ci{
82e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
83e1051a39Sopenharmony_ci    fixture->expected = 1;
84e1051a39Sopenharmony_ci    if (!TEST_ptr(fixture->tgt_string = ASN1_OCTET_STRING_new())
85e1051a39Sopenharmony_ci            || !TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
86e1051a39Sopenharmony_ci            || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
87e1051a39Sopenharmony_ci                                                sizeof(rand_data)))) {
88e1051a39Sopenharmony_ci        tear_down(fixture);
89e1051a39Sopenharmony_ci        fixture = NULL;
90e1051a39Sopenharmony_ci    }
91e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
92e1051a39Sopenharmony_ci    return result;
93e1051a39Sopenharmony_ci}
94e1051a39Sopenharmony_ci
95e1051a39Sopenharmony_cistatic int test_ASN1_OCTET_STRING_set_tgt_is_src(void)
96e1051a39Sopenharmony_ci{
97e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
98e1051a39Sopenharmony_ci    fixture->expected = 1;
99e1051a39Sopenharmony_ci    if (!TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
100e1051a39Sopenharmony_ci            || !(fixture->tgt_string = fixture->src_string)
101e1051a39Sopenharmony_ci            || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
102e1051a39Sopenharmony_ci                                                sizeof(rand_data)))) {
103e1051a39Sopenharmony_ci        tear_down(fixture);
104e1051a39Sopenharmony_ci        fixture = NULL;
105e1051a39Sopenharmony_ci    }
106e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
107e1051a39Sopenharmony_ci    return result;
108e1051a39Sopenharmony_ci}
109e1051a39Sopenharmony_ci
110e1051a39Sopenharmony_ci
111e1051a39Sopenharmony_civoid cleanup_tests(void)
112e1051a39Sopenharmony_ci{
113e1051a39Sopenharmony_ci    return;
114e1051a39Sopenharmony_ci}
115e1051a39Sopenharmony_ci
116e1051a39Sopenharmony_ciint setup_tests(void)
117e1051a39Sopenharmony_ci{
118e1051a39Sopenharmony_ci    RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
119e1051a39Sopenharmony_ci    /* ASN.1 related tests */
120e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_asn1_get_int);
121e1051a39Sopenharmony_ci    ADD_TEST(test_ASN1_OCTET_STRING_set);
122e1051a39Sopenharmony_ci    ADD_TEST(test_ASN1_OCTET_STRING_set_tgt_is_src);
123e1051a39Sopenharmony_ci    return 1;
124e1051a39Sopenharmony_ci}
125