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