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 | {
|
---|
21 | const char *in;
|
---|
22 | const unsigned char *expected;
|
---|
23 | size_t expected_len;
|
---|
24 | const char sep;
|
---|
25 | };
|
---|
26 |
|
---|
27 | static const unsigned char test_1[] = { 0xAB, 0xCD, 0xEF, 0xF1 };
|
---|
28 | static const unsigned char test_2[] = { 0xAB, 0xCD, 0xEF, 0x76, 0x00 };
|
---|
29 |
|
---|
30 | static struct testdata tbl_testdata[] = {
|
---|
31 | {
|
---|
32 | "AB:CD:EF:F1",
|
---|
33 | test_1, sizeof(test_1),
|
---|
34 | ':',
|
---|
35 | },
|
---|
36 | {
|
---|
37 | "AB:CD:EF:76:00",
|
---|
38 | test_2, sizeof(test_2),
|
---|
39 | ':',
|
---|
40 | },
|
---|
41 | {
|
---|
42 | "AB_CD_EF_F1",
|
---|
43 | test_1, sizeof(test_1),
|
---|
44 | '_',
|
---|
45 | },
|
---|
46 | {
|
---|
47 | "AB_CD_EF_76_00",
|
---|
48 | test_2, sizeof(test_2),
|
---|
49 | '_',
|
---|
50 | },
|
---|
51 | {
|
---|
52 | "ABCDEFF1",
|
---|
53 | test_1, sizeof(test_1),
|
---|
54 | '\0',
|
---|
55 | },
|
---|
56 | {
|
---|
57 | "ABCDEF7600",
|
---|
58 | test_2, sizeof(test_2),
|
---|
59 | '\0',
|
---|
60 | },
|
---|
61 | };
|
---|
62 |
|
---|
63 | static int test_hexstr_sep_to_from(int test_index)
|
---|
64 | {
|
---|
65 | int ret = 0;
|
---|
66 | long len = 0;
|
---|
67 | unsigned char *buf = NULL;
|
---|
68 | char *out = NULL;
|
---|
69 | struct testdata *test = &tbl_testdata[test_index];
|
---|
70 |
|
---|
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))
|
---|
75 | goto err;
|
---|
76 |
|
---|
77 | ret = 1;
|
---|
78 | err:
|
---|
79 | OPENSSL_free(buf);
|
---|
80 | OPENSSL_free(out);
|
---|
81 | return ret;
|
---|
82 | }
|
---|
83 |
|
---|
84 | static int test_hexstr_to_from(int test_index)
|
---|
85 | {
|
---|
86 | int ret = 0;
|
---|
87 | long len = 0;
|
---|
88 | unsigned char *buf = NULL;
|
---|
89 | char *out = NULL;
|
---|
90 | struct testdata *test = &tbl_testdata[test_index];
|
---|
91 |
|
---|
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)))
|
---|
96 | goto err;
|
---|
97 | if (test->sep == ':') {
|
---|
98 | if (!TEST_str_eq(out, test->in))
|
---|
99 | goto err;
|
---|
100 | } else if (!TEST_str_ne(out, test->in)) {
|
---|
101 | goto err;
|
---|
102 | }
|
---|
103 | } else {
|
---|
104 | if (!TEST_ptr_null(buf = OPENSSL_hexstr2buf(test->in, &len)))
|
---|
105 | goto err;
|
---|
106 | }
|
---|
107 | ret = 1;
|
---|
108 | err:
|
---|
109 | OPENSSL_free(buf);
|
---|
110 | OPENSSL_free(out);
|
---|
111 | return ret;
|
---|
112 | }
|
---|
113 |
|
---|
114 | static int test_hexstr_ex_to_from(int test_index)
|
---|
115 | {
|
---|
116 | size_t len = 0;
|
---|
117 | char out[64];
|
---|
118 | unsigned char buf[64];
|
---|
119 | struct testdata *test = &tbl_testdata[test_index];
|
---|
120 |
|
---|
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_false(OPENSSL_buf2hexstr_ex(out, 3 * len - 1, NULL, buf, len,
|
---|
124 | ':'))
|
---|
125 | && TEST_true(OPENSSL_buf2hexstr_ex(out, sizeof(out), NULL, buf, len,
|
---|
126 | ':'))
|
---|
127 | && TEST_str_eq(out, test->in)
|
---|
128 | && TEST_true(OPENSSL_buf2hexstr_ex(out, sizeof(out), NULL, buf, 0,
|
---|
129 | ':'))
|
---|
130 | && TEST_size_t_eq(strlen(out), 0);
|
---|
131 | }
|
---|
132 |
|
---|
133 | int setup_tests(void)
|
---|
134 | {
|
---|
135 | ADD_ALL_TESTS(test_hexstr_sep_to_from, OSSL_NELEM(tbl_testdata));
|
---|
136 | ADD_ALL_TESTS(test_hexstr_to_from, OSSL_NELEM(tbl_testdata));
|
---|
137 | ADD_ALL_TESTS(test_hexstr_ex_to_from, 2);
|
---|
138 | return 1;
|
---|
139 | }
|
---|