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,
33 sizeof(test_1),
34 ':',
35 },
36 {
37 "AB:CD:EF:76:00",
38 test_2,
39 sizeof(test_2),
40 ':',
41 },
42 {
43 "AB_CD_EF_F1",
44 test_1,
45 sizeof(test_1),
46 '_',
47 },
48 {
49 "AB_CD_EF_76_00",
50 test_2,
51 sizeof(test_2),
52 '_',
53 },
54 {
55 "ABCDEFF1",
56 test_1,
57 sizeof(test_1),
58 '\0',
59 },
60 {
61 "ABCDEF7600",
62 test_2,
63 sizeof(test_2),
64 '\0',
65 },
66 };
67
test_hexstr_sep_to_from(int test_index)68 static int test_hexstr_sep_to_from(int test_index)
69 {
70 int ret = 0;
71 long len = 0;
72 unsigned char *buf = NULL;
73 char *out = NULL;
74 struct testdata *test = &tbl_testdata[test_index];
75
76 if (!TEST_ptr(buf = ossl_hexstr2buf_sep(test->in, &len, test->sep))
77 || !TEST_mem_eq(buf, len, test->expected, test->expected_len)
78 || !TEST_ptr(out = ossl_buf2hexstr_sep(buf, len, test->sep))
79 || !TEST_str_eq(out, test->in))
80 goto err;
81
82 ret = 1;
83 err:
84 OPENSSL_free(buf);
85 OPENSSL_free(out);
86 return ret;
87 }
88
test_hexstr_to_from(int test_index)89 static int test_hexstr_to_from(int test_index)
90 {
91 int ret = 0;
92 long len = 0;
93 unsigned char *buf = NULL;
94 char *out = NULL;
95 struct testdata *test = &tbl_testdata[test_index];
96
97 if (test->sep != '_') {
98 if (!TEST_ptr(buf = OPENSSL_hexstr2buf(test->in, &len))
99 || !TEST_mem_eq(buf, len, test->expected, test->expected_len)
100 || !TEST_ptr(out = OPENSSL_buf2hexstr(buf, len)))
101 goto err;
102 if (test->sep == ':') {
103 if (!TEST_str_eq(out, test->in))
104 goto err;
105 } else if (!TEST_str_ne(out, test->in)) {
106 goto err;
107 }
108 } else {
109 if (!TEST_ptr_null(buf = OPENSSL_hexstr2buf(test->in, &len)))
110 goto err;
111 }
112 ret = 1;
113 err:
114 OPENSSL_free(buf);
115 OPENSSL_free(out);
116 return ret;
117 }
118
test_hexstr_ex_to_from(int test_index)119 static int test_hexstr_ex_to_from(int test_index)
120 {
121 size_t len = 0;
122 char out[64];
123 unsigned char buf[64];
124 struct testdata *test = &tbl_testdata[test_index];
125
126 return TEST_true(OPENSSL_hexstr2buf_ex(buf, sizeof(buf), &len, test->in, ':'))
127 && TEST_mem_eq(buf, len, test->expected, test->expected_len)
128 && TEST_false(OPENSSL_buf2hexstr_ex(out, 3 * len - 1, NULL, buf, len,
129 ':'))
130 && TEST_true(OPENSSL_buf2hexstr_ex(out, sizeof(out), NULL, buf, len,
131 ':'))
132 && TEST_str_eq(out, test->in)
133 && TEST_true(OPENSSL_buf2hexstr_ex(out, sizeof(out), NULL, buf, 0,
134 ':'))
135 && TEST_size_t_eq(strlen(out), 0);
136 }
137
setup_tests(void)138 int setup_tests(void)
139 {
140 ADD_ALL_TESTS(test_hexstr_sep_to_from, OSSL_NELEM(tbl_testdata));
141 ADD_ALL_TESTS(test_hexstr_to_from, OSSL_NELEM(tbl_testdata));
142 ADD_ALL_TESTS(test_hexstr_ex_to_from, 2);
143 return 1;
144 }
145