2 * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
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
12 #include "helpers/cmp_testlib.h"
14 static unsigned char rand_data
[OSSL_CMP_TRANSACTIONID_LENGTH
];
16 typedef struct test_fixture
{
17 const char *test_case_name
;
19 ASN1_OCTET_STRING
*src_string
;
20 ASN1_OCTET_STRING
*tgt_string
;
22 } CMP_ASN_TEST_FIXTURE
;
24 static CMP_ASN_TEST_FIXTURE
*set_up(const char *const test_case_name
)
26 CMP_ASN_TEST_FIXTURE
*fixture
;
28 if (!TEST_ptr(fixture
= OPENSSL_zalloc(sizeof(*fixture
))))
30 fixture
->test_case_name
= test_case_name
;
34 static void tear_down(CMP_ASN_TEST_FIXTURE
*fixture
)
36 ASN1_OCTET_STRING_free(fixture
->src_string
);
37 if (fixture
->tgt_string
!= fixture
->src_string
)
38 ASN1_OCTET_STRING_free(fixture
->tgt_string
);
40 OPENSSL_free(fixture
);
43 static int execute_cmp_asn1_get_int_test(CMP_ASN_TEST_FIXTURE
*fixture
)
46 ASN1_INTEGER
*asn1integer
= ASN1_INTEGER_new();
48 if (!TEST_ptr(asn1integer
))
50 ASN1_INTEGER_set(asn1integer
, 77);
51 res
= TEST_int_eq(77, ossl_cmp_asn1_get_int(asn1integer
));
52 ASN1_INTEGER_free(asn1integer
);
56 static int test_cmp_asn1_get_int(void)
58 SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE
, set_up
);
59 fixture
->expected
= 1;
60 EXECUTE_TEST(execute_cmp_asn1_get_int_test
, tear_down
);
64 static int execute_CMP_ASN1_OCTET_STRING_set1_test(CMP_ASN_TEST_FIXTURE
*
67 if (!TEST_int_eq(fixture
->expected
,
68 ossl_cmp_asn1_octet_string_set1(&fixture
->tgt_string
,
69 fixture
->src_string
)))
71 if (fixture
->expected
!= 0)
72 return TEST_int_eq(0, ASN1_OCTET_STRING_cmp(fixture
->tgt_string
,
73 fixture
->src_string
));
77 static int test_ASN1_OCTET_STRING_set(void)
79 SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE
, set_up
);
80 fixture
->expected
= 1;
81 if (!TEST_ptr(fixture
->tgt_string
= ASN1_OCTET_STRING_new())
82 || !TEST_ptr(fixture
->src_string
= ASN1_OCTET_STRING_new())
83 || !TEST_true(ASN1_OCTET_STRING_set(fixture
->src_string
, rand_data
,
84 sizeof(rand_data
)))) {
88 EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test
, tear_down
);
92 static int test_ASN1_OCTET_STRING_set_tgt_is_src(void)
94 SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE
, set_up
);
95 fixture
->expected
= 1;
96 if (!TEST_ptr(fixture
->src_string
= ASN1_OCTET_STRING_new())
97 || !(fixture
->tgt_string
= fixture
->src_string
)
98 || !TEST_true(ASN1_OCTET_STRING_set(fixture
->src_string
, rand_data
,
99 sizeof(rand_data
)))) {
103 EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test
, tear_down
);
108 void cleanup_tests(void)
113 int setup_tests(void)
115 RAND_bytes(rand_data
, OSSL_CMP_TRANSACTIONID_LENGTH
);
116 /* ASN.1 related tests */
117 ADD_TEST(test_cmp_asn1_get_int
);
118 ADD_TEST(test_ASN1_OCTET_STRING_set
);
119 ADD_TEST(test_ASN1_OCTET_STRING_set_tgt_is_src
);
121 * TODO make sure that total number of tests (here currently 24) is shown,
122 * also for other cmp_*text.c. Currently the test drivers always show 1.