Make EVP_PKEY_CTX_[get|set]_group_name work for DH too
[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
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
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
43 static int execute_cmp_asn1_get_int_test(CMP_ASN_TEST_FIXTURE *fixture)
44 {
45     ASN1_INTEGER *asn1integer = ASN1_INTEGER_new();
46     ASN1_INTEGER_set(asn1integer, 77);
47     if (!TEST_int_eq(77, ossl_cmp_asn1_get_int(asn1integer)))
48         return 0;
49     ASN1_INTEGER_free(asn1integer);
50     return 1;
51 }
52
53 static int test_cmp_asn1_get_int(void)
54 {
55     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
56     fixture->expected = 1;
57     EXECUTE_TEST(execute_cmp_asn1_get_int_test, tear_down);
58     return result;
59 }
60
61 static int execute_CMP_ASN1_OCTET_STRING_set1_test(CMP_ASN_TEST_FIXTURE *
62                                                    fixture)
63 {
64     if (!TEST_int_eq(fixture->expected,
65                      ossl_cmp_asn1_octet_string_set1(&fixture->tgt_string,
66                                                      fixture->src_string)))
67         return 0;
68     if (fixture->expected != 0)
69         return TEST_int_eq(0, ASN1_OCTET_STRING_cmp(fixture->tgt_string,
70                                                     fixture->src_string));
71     return 1;
72 }
73
74 static int test_ASN1_OCTET_STRING_set(void)
75 {
76     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
77     fixture->expected = 1;
78     if (!TEST_ptr(fixture->tgt_string = ASN1_OCTET_STRING_new())
79             || !TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
80             || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
81                                                 sizeof(rand_data)))) {
82         tear_down(fixture);
83         fixture = NULL;
84     }
85     EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
86     return result;
87 }
88
89 static int test_ASN1_OCTET_STRING_set_tgt_is_src(void)
90 {
91     SETUP_TEST_FIXTURE(CMP_ASN_TEST_FIXTURE, set_up);
92     fixture->expected = 1;
93     if (!TEST_ptr(fixture->src_string = ASN1_OCTET_STRING_new())
94             || !(fixture->tgt_string = fixture->src_string)
95             || !TEST_true(ASN1_OCTET_STRING_set(fixture->src_string, rand_data,
96                                                 sizeof(rand_data)))) {
97         tear_down(fixture);
98         fixture = NULL;
99     }
100     EXECUTE_TEST(execute_CMP_ASN1_OCTET_STRING_set1_test, tear_down);
101     return result;
102 }
103
104
105 void cleanup_tests(void)
106 {
107     return;
108 }
109
110 int setup_tests(void)
111 {
112     RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
113     /* ASN.1 related tests */
114     ADD_TEST(test_cmp_asn1_get_int);
115     ADD_TEST(test_ASN1_OCTET_STRING_set);
116     ADD_TEST(test_ASN1_OCTET_STRING_set_tgt_is_src);
117     /*
118      * TODO make sure that total number of tests (here currently 24) is shown,
119      * also for other cmp_*text.c. Currently the test drivers always show 1.
120      */
121
122     return 1;
123 }