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