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