VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.7/test/hexstr_test.c

最後變更 在這個檔案是 105949,由 vboxsync 提交於 2 月 前

openssl-3.1.7: Applied and adjusted our OpenSSL changes to 3.1.7. bugref:10757

檔案大小: 3.3 KB
 
1/*
2 * Copyright 2020-2021 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
19struct testdata
20{
21 const char *in;
22 const unsigned char *expected;
23 size_t expected_len;
24 const char sep;
25};
26
27static const unsigned char test_1[] = { 0xAB, 0xCD, 0xEF, 0xF1 };
28static const unsigned char test_2[] = { 0xAB, 0xCD, 0xEF, 0x76, 0x00 };
29
30static 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
63static 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;
78err:
79 OPENSSL_free(buf);
80 OPENSSL_free(out);
81 return ret;
82}
83
84static 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;
108err:
109 OPENSSL_free(buf);
110 OPENSSL_free(out);
111 return ret;
112}
113
114static 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_true(OPENSSL_buf2hexstr_ex(out, sizeof(out), NULL, buf, len,
124 ':'))
125 && TEST_str_eq(out, test->in);
126}
127
128int setup_tests(void)
129{
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);
133 return 1;
134}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette