1 /*
2 * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
3 *
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.
9 */
10
11 /*
12 * This program tests the use of OSSL_PARAM, currently in raw form.
13 */
14
15 #include "internal/nelem.h"
16 #include "internal/cryptlib.h"
17 #include "testutil.h"
18
19 struct testdata {
20 const char *in;
21 const unsigned char *expected;
22 size_t expected_len;
23 const char sep;
24 };
25
26 static const unsigned char test_1[] = { 0xAB, 0xCD, 0xEF, 0xF1 };
27 static const unsigned char test_2[] = { 0xAB, 0xCD, 0xEF, 0x76, 0x00 };
28
29 static struct testdata tbl_testdata[] = {
30 {
31 "AB:CD:EF:F1",
32 test_1, sizeof(test_1),
33 ':',
34 },
35 {
36 "AB:CD:EF:76:00",
37 test_2, sizeof(test_2),
38 ':',
39 },
40 {
41 "AB_CD_EF_F1",
42 test_1, sizeof(test_1),
43 '_',
44 },
45 {
46 "AB_CD_EF_76_00",
47 test_2, sizeof(test_2),
48 '_',
49 },
50 {
51 "ABCDEFF1",
52 test_1, sizeof(test_1),
53 '\0',
54 },
55 {
56 "ABCDEF7600",
57 test_2, sizeof(test_2),
58 '\0',
59 },
60 };
61
test_hexstr_sep_to_from(int test_index)62 static int test_hexstr_sep_to_from(int test_index)
63 {
64 int ret = 0;
65 long len = 0;
66 unsigned char *buf = NULL;
67 char *out = NULL;
68 struct testdata *test = &tbl_testdata[test_index];
69
70 if (!TEST_ptr(buf = ossl_hexstr2buf_sep(test->in, &len, test->sep))
71 || !TEST_mem_eq(buf, len, test->expected, test->expected_len)
72 || !TEST_ptr(out = ossl_buf2hexstr_sep(buf, len, test->sep))
73 || !TEST_str_eq(out, test->in))
74 goto err;
75
76 ret = 1;
77 err:
78 OPENSSL_free(buf);
79 OPENSSL_free(out);
80 return ret;
81 }
82
test_hexstr_to_from(int test_index)83 static int test_hexstr_to_from(int test_index)
84 {
85 int ret = 0;
86 long len = 0;
87 unsigned char *buf = NULL;
88 char *out = NULL;
89 struct testdata *test = &tbl_testdata[test_index];
90
91 if (test->sep != '_') {
92 if (!TEST_ptr(buf = OPENSSL_hexstr2buf(test->in, &len))
93 || !TEST_mem_eq(buf, len, test->expected, test->expected_len)
94 || !TEST_ptr(out = OPENSSL_buf2hexstr(buf, len)))
95 goto err;
96 if (test->sep == ':') {
97 if (!TEST_str_eq(out, test->in))
98 goto err;
99 } else if (!TEST_str_ne(out, test->in)) {
100 goto err;
101 }
102 } else {
103 if (!TEST_ptr_null(buf = OPENSSL_hexstr2buf(test->in, &len)))
104 goto err;
105 }
106 ret = 1;
107 err:
108 OPENSSL_free(buf);
109 OPENSSL_free(out);
110 return ret;
111 }
112
test_hexstr_ex_to_from(int test_index)113 static int test_hexstr_ex_to_from(int test_index)
114 {
115 size_t len = 0;
116 char out[64];
117 unsigned char buf[64];
118 struct testdata *test = &tbl_testdata[test_index];
119
120 return TEST_true(OPENSSL_hexstr2buf_ex(buf, sizeof(buf), &len, test->in, ':'))
121 && TEST_mem_eq(buf, len, test->expected, test->expected_len)
122 && TEST_false(OPENSSL_buf2hexstr_ex(out, 3 * len - 1, NULL, buf, len,
123 ':'))
124 && TEST_true(OPENSSL_buf2hexstr_ex(out, sizeof(out), NULL, buf, len,
125 ':'))
126 && TEST_str_eq(out, test->in)
127 && TEST_true(OPENSSL_buf2hexstr_ex(out, sizeof(out), NULL, buf, 0,
128 ':'))
129 && TEST_size_t_eq(strlen(out), 0);
130 }
131
setup_tests(void)132 int setup_tests(void)
133 {
134 ADD_ALL_TESTS(test_hexstr_sep_to_from, OSSL_NELEM(tbl_testdata));
135 ADD_ALL_TESTS(test_hexstr_to_from, OSSL_NELEM(tbl_testdata));
136 ADD_ALL_TESTS(test_hexstr_ex_to_from, 2);
137 return 1;
138 }
139