]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/hexstr_test.c
2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
12 * This program tests the use of OSSL_PARAM, currently in raw form.
15 #include "internal/nelem.h"
16 #include "internal/cryptlib.h"
22 const unsigned char *expected
;
27 static const unsigned char test_1
[] = { 0xAB, 0xCD, 0xEF, 0xF1 };
28 static const unsigned char test_2
[] = { 0xAB, 0xCD, 0xEF, 0x76, 0x00 };
30 static struct testdata tbl_testdata
[] = {
33 test_1
, sizeof(test_1
),
38 test_2
, sizeof(test_2
),
43 test_1
, sizeof(test_1
),
48 test_2
, sizeof(test_2
),
53 test_1
, sizeof(test_1
),
58 test_2
, sizeof(test_2
),
63 static int test_hexstr_sep_to_from(int test_index
)
67 unsigned char *buf
= NULL
;
69 struct testdata
*test
= &tbl_testdata
[test_index
];
71 if (!TEST_ptr(buf
= ossl_hexstr2buf_sep(test
->in
, &len
, test
->sep
))
72 || !TEST_mem_eq(buf
, len
, test
->expected
, test
->expected_len
)
73 || !TEST_ptr(out
= ossl_buf2hexstr_sep(buf
, len
, test
->sep
))
74 || !TEST_str_eq(out
, test
->in
))
84 static int test_hexstr_to_from(int test_index
)
88 unsigned char *buf
= NULL
;
90 struct testdata
*test
= &tbl_testdata
[test_index
];
92 if (test
->sep
!= '_') {
93 if (!TEST_ptr(buf
= OPENSSL_hexstr2buf(test
->in
, &len
))
94 || !TEST_mem_eq(buf
, len
, test
->expected
, test
->expected_len
)
95 || !TEST_ptr(out
= OPENSSL_buf2hexstr(buf
, len
)))
97 if (test
->sep
== ':') {
98 if (!TEST_str_eq(out
, test
->in
))
100 } else if (!TEST_str_ne(out
, test
->in
)) {
104 if (!TEST_ptr_null(buf
= OPENSSL_hexstr2buf(test
->in
, &len
)))
114 static int test_hexstr_ex_to_from(int test_index
)
118 unsigned char buf
[64];
119 struct testdata
*test
= &tbl_testdata
[test_index
];
121 return TEST_true(OPENSSL_hexstr2buf_ex(buf
, sizeof(buf
), &len
, test
->in
, ':'))
122 && TEST_mem_eq(buf
, len
, test
->expected
, test
->expected_len
)
123 && TEST_true(OPENSSL_buf2hexstr_ex(out
, sizeof(out
), NULL
, buf
, len
,
125 && TEST_str_eq(out
, test
->in
);
128 int setup_tests(void)
130 ADD_ALL_TESTS(test_hexstr_sep_to_from
, OSSL_NELEM(tbl_testdata
));
131 ADD_ALL_TESTS(test_hexstr_to_from
, OSSL_NELEM(tbl_testdata
));
132 ADD_ALL_TESTS(test_hexstr_ex_to_from
, 2);