xref: /freebsd-src/crypto/openssl/test/cmp_asn_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2007-2023 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 static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
15*e0c4386eSCy Schubert 
16*e0c4386eSCy Schubert typedef struct test_fixture {
17*e0c4386eSCy Schubert     const char *test_case_name;
18*e0c4386eSCy Schubert     int expected;
19*e0c4386eSCy Schubert     ASN1_OCTET_STRING *src_string;
20*e0c4386eSCy Schubert     ASN1_OCTET_STRING *tgt_string;
21*e0c4386eSCy Schubert 
22*e0c4386eSCy Schubert } CMP_ASN_TEST_FIXTURE;
23*e0c4386eSCy Schubert 
set_up(const char * const test_case_name)24*e0c4386eSCy Schubert static CMP_ASN_TEST_FIXTURE *set_up(const char *const test_case_name)
25*e0c4386eSCy Schubert {
26*e0c4386eSCy Schubert     CMP_ASN_TEST_FIXTURE *fixture;
27*e0c4386eSCy Schubert 
28*e0c4386eSCy Schubert     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
29*e0c4386eSCy Schubert         return NULL;
30*e0c4386eSCy Schubert     fixture->test_case_name = test_case_name;
31*e0c4386eSCy Schubert     return fixture;
32*e0c4386eSCy Schubert }
33*e0c4386eSCy Schubert 
tear_down(CMP_ASN_TEST_FIXTURE * fixture)34*e0c4386eSCy Schubert static void tear_down(CMP_ASN_TEST_FIXTURE *fixture)
35*e0c4386eSCy Schubert {
36*e0c4386eSCy Schubert     ASN1_OCTET_STRING_free(fixture->src_string);
37*e0c4386eSCy Schubert     if (fixture->tgt_string != fixture->src_string)
38*e0c4386eSCy Schubert         ASN1_OCTET_STRING_free(fixture->tgt_string);
39*e0c4386eSCy Schubert 
40*e0c4386eSCy Schubert     OPENSSL_free(fixture);
41*e0c4386eSCy Schubert }
42*e0c4386eSCy Schubert 
execute_cmp_asn1_get_int_test(CMP_ASN_TEST_FIXTURE * fixture)43*e0c4386eSCy Schubert static int execute_cmp_asn1_get_int_test(CMP_ASN_TEST_FIXTURE *fixture)
44*e0c4386eSCy Schubert {
45*e0c4386eSCy Schubert     int res = 0;
46*e0c4386eSCy Schubert     ASN1_INTEGER *asn1integer = ASN1_INTEGER_new();
47*e0c4386eSCy Schubert     const int good_int = 77;
48*e0c4386eSCy Schubert     const int64_t max_int = INT_MAX;
49*e0c4386eSCy Schubert 
50*e0c4386eSCy Schubert     if (!TEST_ptr(asn1integer))
51*e0c4386eSCy Schubert         return res;
52*e0c4386eSCy Schubert 
53*e0c4386eSCy Schubert     if (!TEST_true(ASN1_INTEGER_set(asn1integer, good_int))) {
54*e0c4386eSCy Schubert         ASN1_INTEGER_free(asn1integer);
55*e0c4386eSCy Schubert         return 0;
56*e0c4386eSCy Schubert     }
57*e0c4386eSCy Schubert     res = TEST_int_eq(good_int, ossl_cmp_asn1_get_int(asn1integer));
58*e0c4386eSCy Schubert     if (res == 0)
59*e0c4386eSCy Schubert         goto err;
60*e0c4386eSCy Schubert 
61*e0c4386eSCy Schubert     res = 0;
62*e0c4386eSCy Schubert     if (!TEST_true(ASN1_INTEGER_set_int64(asn1integer, max_int + 1)))
63*e0c4386eSCy Schubert         goto err;
64*e0c4386eSCy Schubert     res = TEST_int_eq(-2, ossl_cmp_asn1_get_int(asn1integer));
65*e0c4386eSCy Schubert 
66*e0c4386eSCy Schubert  err:
67*e0c4386eSCy Schubert     ASN1_INTEGER_free(asn1integer);
68*e0c4386eSCy Schubert     return res;
69*e0c4386eSCy Schubert }
70*e0c4386eSCy Schubert 
test_cmp_asn1_get_int(void)71*e0c4386eSCy Schubert static int test_cmp_asn1_get_int(void)
72*e0c4386eSCy Schubert {
73*e0c4386eSCy Schubert     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
74*e0c4386eSCy Schubert     fixture->expected = 1;
75*e0c4386eSCy Schubert     EXECUTE_TEST(execute_cmp_asn1_get_int_test, tear_down);
76*e0c4386eSCy Schubert     return result;
77*e0c4386eSCy Schubert }
78*e0c4386eSCy Schubert 
execute_CMP_ASN1_OCTET_STRING_set1_test(CMP_ASN_TEST_FIXTURE * fixture)79*e0c4386eSCy Schubert static int execute_CMP_ASN1_OCTET_STRING_set1_test(CMP_ASN_TEST_FIXTURE *
80*e0c4386eSCy Schubert                                                    fixture)
81*e0c4386eSCy Schubert {
82*e0c4386eSCy Schubert     if (!TEST_int_eq(fixture->expected,
83*e0c4386eSCy Schubert                      ossl_cmp_asn1_octet_string_set1(&fixture->tgt_string,
84*e0c4386eSCy Schubert                                                      fixture->src_string)))
85*e0c4386eSCy Schubert         return 0;
86*e0c4386eSCy Schubert     if (fixture->expected != 0)
87*e0c4386eSCy Schubert         return TEST_int_eq(0, ASN1_OCTET_STRING_cmp(fixture->tgt_string,
88*e0c4386eSCy Schubert                                                     fixture->src_string));
89*e0c4386eSCy Schubert     return 1;
90*e0c4386eSCy Schubert }
91*e0c4386eSCy Schubert 
test_ASN1_OCTET_STRING_set(void)92*e0c4386eSCy Schubert static int test_ASN1_OCTET_STRING_set(void)
93*e0c4386eSCy Schubert {
94*e0c4386eSCy Schubert     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
95*e0c4386eSCy Schubert     fixture->expected = 1;
96*e0c4386eSCy Schubert     if (!TEST_ptr(fixture->tgt_string = ASN1_OCTET_STRING_new())
97*e0c4386eSCy Schubert             || !TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
98*e0c4386eSCy Schubert             || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
99*e0c4386eSCy Schubert                                                 sizeof(rand_data)))) {
100*e0c4386eSCy Schubert         tear_down(fixture);
101*e0c4386eSCy Schubert         fixture = NULL;
102*e0c4386eSCy Schubert     }
103*e0c4386eSCy Schubert     EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
104*e0c4386eSCy Schubert     return result;
105*e0c4386eSCy Schubert }
106*e0c4386eSCy Schubert 
test_ASN1_OCTET_STRING_set_tgt_is_src(void)107*e0c4386eSCy Schubert static int test_ASN1_OCTET_STRING_set_tgt_is_src(void)
108*e0c4386eSCy Schubert {
109*e0c4386eSCy Schubert     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
110*e0c4386eSCy Schubert     fixture->expected = 1;
111*e0c4386eSCy Schubert     if (!TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
112*e0c4386eSCy Schubert             || !(fixture->tgt_string = fixture->src_string)
113*e0c4386eSCy Schubert             || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
114*e0c4386eSCy Schubert                                                 sizeof(rand_data)))) {
115*e0c4386eSCy Schubert         tear_down(fixture);
116*e0c4386eSCy Schubert         fixture = NULL;
117*e0c4386eSCy Schubert     }
118*e0c4386eSCy Schubert     EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
119*e0c4386eSCy Schubert     return result;
120*e0c4386eSCy Schubert }
121*e0c4386eSCy Schubert 
122*e0c4386eSCy Schubert 
cleanup_tests(void)123*e0c4386eSCy Schubert void cleanup_tests(void)
124*e0c4386eSCy Schubert {
125*e0c4386eSCy Schubert     return;
126*e0c4386eSCy Schubert }
127*e0c4386eSCy Schubert 
setup_tests(void)128*e0c4386eSCy Schubert int setup_tests(void)
129*e0c4386eSCy Schubert {
130*e0c4386eSCy Schubert     RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
131*e0c4386eSCy Schubert     /* ASN.1 related tests */
132*e0c4386eSCy Schubert     ADD_TEST(test_cmp_asn1_get_int);
133*e0c4386eSCy Schubert     ADD_TEST(test_ASN1_OCTET_STRING_set);
134*e0c4386eSCy Schubert     ADD_TEST(test_ASN1_OCTET_STRING_set_tgt_is_src);
135*e0c4386eSCy Schubert     return 1;
136*e0c4386eSCy Schubert }
137