70439bf0afe9eedd3880969b9c50244dd9ea9547
[openssl.git] / test / cmp_asn_test.c
1 /*
2  * Copyright 2007-2019 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 "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
24 static CMP_ASN_TEST_FIXTURE *set_up(const char *const test_case_name)
25 {
26     CMP_ASN_TEST_FIXTURE *fixture;
27     int setup_ok = 0;
28
29     /* Allocate memory owned by the fixture, exit on error */
30     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
31         goto err;
32     fixture->test_case_name = test_case_name;
33     setup_ok = 1;
34
35  err:
36     if (!setup_ok) {
37 #ifndef OPENSSL_NO_STDIO
38         ERR_print_errors_fp(stderr);
39 #endif
40         exit(EXIT_FAILURE);
41     }
42     return fixture;
43 }
44
45 static void tear_down(CMP_ASN_TEST_FIXTURE *fixture)
46 {
47     ASN1_OCTET_STRING_free(fixture->src_string);
48     if (fixture->tgt_string != fixture->src_string)
49         ASN1_OCTET_STRING_free(fixture->tgt_string);
50
51     OPENSSL_free(fixture);
52 }
53
54 static int execute_cmp_asn1_get_int_test(CMP_ASN_TEST_FIXTURE *
55                                                    fixture)
56 {
57     ASN1_INTEGER *asn1integer = ASN1_INTEGER_new();
58     ASN1_INTEGER_set(asn1integer, 77);
59     if (!TEST_int_eq(77, ossl_cmp_asn1_get_int(asn1integer)))
60         return 0;
61     ASN1_INTEGER_free(asn1integer);
62     return 1;
63 }
64
65 static int test_cmp_asn1_get_int(void)
66 {
67     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
68     fixture->expected = 1;
69     EXECUTE_TEST(execute_cmp_asn1_get_int_test, tear_down);
70     return result;
71 }
72
73 static int execute_CMP_ASN1_OCTET_STRING_set1_test(CMP_ASN_TEST_FIXTURE *
74                                                    fixture)
75 {
76     if (!TEST_int_eq(fixture->expected,
77                      ossl_cmp_asn1_octet_string_set1(&fixture->tgt_string,
78                                                      fixture->src_string)))
79         return 0;
80     if (fixture->expected != 0)
81         return TEST_int_eq(0, ASN1_OCTET_STRING_cmp(fixture->tgt_string,
82                                                     fixture->src_string));
83     return 1;
84 }
85
86 static int test_ASN1_OCTET_STRING_set(void)
87 {
88     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
89     fixture->expected = 1;
90     if (!TEST_ptr(fixture->tgt_string = ASN1_OCTET_STRING_new())
91             || !TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
92             || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
93                                                 sizeof(rand_data)))) {
94         tear_down(fixture);
95         fixture = NULL;
96     }
97     EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
98     return result;
99 }
100
101 static int test_ASN1_OCTET_STRING_set_tgt_is_src(void)
102 {
103     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
104     fixture->expected = 1;
105     if (!TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
106             || !(fixture->tgt_string = fixture->src_string)
107             || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
108                                                 sizeof(rand_data)))) {
109         tear_down(fixture);
110         fixture = NULL;
111     }
112     EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
113     return result;
114 }
115
116
117 void cleanup_tests(void)
118 {
119     return;
120 }
121
122 int setup_tests(void)
123 {
124     /* ASN.1 related tests */
125     ADD_TEST(test_cmp_asn1_get_int);
126     ADD_TEST(test_ASN1_OCTET_STRING_set);
127     ADD_TEST(test_ASN1_OCTET_STRING_set_tgt_is_src);
128     /* TODO make sure that total number of tests (here currently 24) is shown,
129      also for other cmp_*text.c. Currently the test drivers always show 1. */
130
131     return 1;
132 }