The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
base_16_32_64_tests.c
Go to the documentation of this file.
1#include <freeradius-devel/util/acutest.h>
2#include <freeradius-devel/util/acutest_helpers.h>
3
4#include "base16.h"
5#include "base32.h"
6#include "base64.h"
7
8typedef struct {
9 struct {
10 char const *str;
11 size_t len;
12 } cleartext;
13
14 struct {
15 char const *str;
16 size_t len;
17 } encoded;
19
20
21static const test_vector base16_vectors[] = {
22 { L(""), L("") },
23 { L("f"), L("66") },
24 { L("fo"), L("666f") },
25 { L("foo"), L("666f6f") },
26 { L("foob"), L("666f6f62") },
27 { L("fooba"), L("666f6f6261") },
28 { L("foobar"), L("666f6f626172") }
29};
30
31static const test_vector base32_vectors[] = {
32 { L(""), L("") },
33 { L("f"), L("MY======") },
34 { L("fo"), L("MZXQ====") },
35 { L("foo"), L("MZXW6===") },
36 { L("foob"), L("MZXW6YQ=") },
37 { L("fooba"), L("MZXW6YTB") },
38 { L("foobar"), L("MZXW6YTBOI======") }
39};
40
42 { L(""), L("") },
43 { L("f"), L("CO======") },
44 { L("fo"), L("CPNG====") },
45 { L("foo"), L("CPNMU===") },
46 { L("foob"), L("CPNMUOG=") },
47 { L("fooba"), L("CPNMUOJ1") },
48 { L("foobar"), L("CPNMUOJ1E8======") }
49};
50
51static const test_vector base64_vectors[] = {
52 { L(""), L("") },
53 { L("f"), L("Zg==") },
54 { L("fo"), L("Zm8=") },
55 { L("foo"), L("Zm9v") },
56 { L("foob"), L("Zm9vYg==") },
57 { L("fooba"), L("Zm9vYmE=") },
58 { L("foobar"), L("Zm9vYmFy") }
59};
60
61static void test_base16_encode(void)
62{
63 char buffer[17] = { 0xff }; /* Initialise to catch bad terminators */
65 size_t i;
66
68
69 for (i = 0; i < NUM_ELEMENTS(base16_vectors); i++) {
70 fr_sbuff_set_to_start(&out);
72 &FR_DBUFF_TMP((uint8_t const *)base16_vectors[i].cleartext.str,
75 TEST_MSG("%s", fr_strerror());
76
77 fr_sbuff_set_to_start(&out);
79 }
80}
81
82static void test_base16_decode(void)
83{
84 char buffer[7] = { 0xff }; /* Initialise to catch bad terminators */
86 size_t i;
87
88 fr_dbuff_init(&out, (uint8_t *)buffer, sizeof(buffer));
89
90 for (i = 0; i < NUM_ELEMENTS(base16_vectors); i++) {
93 &FR_SBUFF_IN(base16_vectors[i].encoded.str,
94 base16_vectors[i].encoded.len), true),
96 TEST_MSG("%s", fr_strerror());
97
98 fr_dbuff_in_bytes(&out, 0x00); /* Terminate */
99
101 TEST_CHECK_STRCMP((char *)fr_dbuff_current(&out), base16_vectors[i].cleartext.str);
102 }
103}
104
105static void test_base32_encode(void)
106{
107 char buffer[17] = { 0xff }; /* Initialise to catch bad terminators */
109 size_t i;
110
112
113 for (i = 0; i < NUM_ELEMENTS(base32_vectors); i++) {
114 fr_sbuff_set_to_start(&out);
116 &FR_DBUFF_TMP((uint8_t const *)base32_vectors[i].cleartext.str,
118 true),
120 TEST_MSG("%s", fr_strerror());
121
122 fr_sbuff_set_to_start(&out);
124 }
125}
126
127static void test_base32_decode(void)
128{
129 char buffer[7] = { 0xff }; /* Initialise to catch bad terminators */
131 size_t i;
132
133 fr_dbuff_init(&out, (uint8_t *)buffer, sizeof(buffer));
134
135 for (i = 0; i < NUM_ELEMENTS(base32_vectors); i++) {
138 &FR_SBUFF_IN(base32_vectors[i].encoded.str,
139 base32_vectors[i].encoded.len),
140 true, true),
142 TEST_MSG("%s", fr_strerror());
143
144 fr_dbuff_in_bytes(&out, 0x00); /* Terminate */
145
147 TEST_CHECK_STRCMP((char *)fr_dbuff_current(&out), base32_vectors[i].cleartext.str);
148
149 }
150}
151
152static void test_base32_hex_encode(void)
153{
154 char buffer[17] = { 0xff }; /* Initialise to catch bad terminators */
156 size_t i;
157
159
160 for (i = 0; i < NUM_ELEMENTS(base32_hex_vectors); i++) {
161 fr_sbuff_set_to_start(&out);
163 &FR_DBUFF_TMP((uint8_t const *)base32_hex_vectors[i].cleartext.str,
167 TEST_MSG("%s", fr_strerror());
168
169 fr_sbuff_set_to_start(&out);
171 }
172}
173
174static void test_base32_hex_decode(void)
175{
176 char buffer[7] = { 0xff }; /* Initialise to catch bad terminators */
178 size_t i;
179
180 fr_dbuff_init(&out, (uint8_t *)buffer, sizeof(buffer));
181
182 for (i = 0; i < NUM_ELEMENTS(base32_vectors); i++) {
185 &FR_SBUFF_IN(base32_hex_vectors[i].encoded.str,
189 TEST_MSG("%s", fr_strerror());
190
191 fr_dbuff_in_bytes(&out, 0x00); /* Terminate */
192
194 TEST_CHECK_STRCMP((char *)fr_dbuff_current(&out), base32_hex_vectors[i].cleartext.str);
195
196 }
197}
198
199static void test_base64_encode(void)
200{
201 char buffer[17] = { 0xff }; /* Initialise to catch bad terminators */
203 size_t i;
204
206
207 for (i = 0; i < NUM_ELEMENTS(base64_vectors); i++) {
208 fr_sbuff_set_to_start(&out);
210 &FR_DBUFF_TMP((uint8_t const *)base64_vectors[i].cleartext.str,
212 true),
214 TEST_MSG("%s", fr_strerror());
215
216 fr_sbuff_set_to_start(&out);
218 }
219}
220
221static void test_base64_decode(void)
222{
223 char buffer[7] = { 0xff }; /* Initialise to catch bad terminators */
225 size_t i;
226
227 fr_dbuff_init(&out, (uint8_t *)buffer, sizeof(buffer));
228
229 for (i = 0; i < NUM_ELEMENTS(base64_vectors); i++) {
232 &FR_SBUFF_IN(base64_vectors[i].encoded.str,
233 base64_vectors[i].encoded.len),
234 true, true),
236 TEST_MSG("%s", fr_strerror());
237
238 fr_dbuff_in_bytes(&out, 0x00); /* Terminate */
239
241 TEST_CHECK_STRCMP((char *)fr_dbuff_current(&out), base64_vectors[i].cleartext.str);
242
243 }
244}
245
247 { "base16_encode", test_base16_encode },
248 { "base16_decode", test_base16_decode },
249
250 { "base32_encode", test_base32_encode },
251 { "base32_decode", test_base32_decode },
252 { "base32_hex_encode", test_base32_hex_encode },
253 { "base32_hex_decode", test_base32_hex_decode },
254
255 { "base64.encode", test_base64_encode },
256 { "base64.decode", test_base64_decode },
257 { NULL }
258};
static int const char char buffer[256]
Definition acutest.h:576
#define TEST_MSG(...)
Definition acutest.h:215
#define TEST_CHECK_SLEN(_got, _exp)
#define TEST_CHECK_STRCMP(_got, _exp)
#define fr_base16_encode(_out, _in)
Definition base16.h:57
#define fr_base16_decode(_err, _out, _in, _no_trailing)
Definition base16.h:95
ssize_t fr_base32_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, bool add_padding, char const alphabet[static UINT8_MAX])
Base 64 encode binary data.
Definition base32.c:190
uint8_t const fr_base32_hex_alphabet_decode[UINT8_MAX]
Definition base32.c:140
fr_slen_t fr_base32_decode_nstd(fr_sbuff_parse_error_t *err, fr_dbuff_t *out, fr_sbuff_t *in, bool expect_padding, bool no_trailing, uint8_t const alphabet[static UINT8_MAX])
Definition base32.c:315
char const fr_base32_hex_alphabet_encode[UINT8_MAX]
Definition base32.c:105
#define fr_base32_encode(_out, _in, _add_padding)
Definition base32.h:62
#define fr_base32_decode(_out, _in, _expect_padding, _no_trailing)
Definition base32.h:69
#define fr_base64_encode(_out, _in, _add_padding)
Definition base64.h:74
#define fr_base64_decode(_out, _in, _expect_padding, _no_trailing)
Definition base64.h:81
static void test_base64_decode(void)
struct test_vector::@106 encoded
static void test_base64_encode(void)
static const test_vector base32_hex_vectors[]
static void test_base16_encode(void)
static void test_base32_hex_encode(void)
struct test_vector::@105 cleartext
static const test_vector base32_vectors[]
static void test_base32_encode(void)
static const test_vector base16_vectors[]
static void test_base32_hex_decode(void)
static void test_base32_decode(void)
static void test_base16_decode(void)
static const test_vector base64_vectors[]
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define NUM_ELEMENTS(_t)
Definition build.h:337
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition dbuff.h:911
#define fr_dbuff_init(_out, _start, _len_or_end)
Initialise an dbuff for encoding or decoding.
Definition dbuff.h:354
#define fr_dbuff_set_to_start(_dbuff_or_marker)
Reset the 'current' position of the dbuff or marker to the 'start' of the buffer.
Definition dbuff.h:1155
#define fr_dbuff_in_bytes(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker.
Definition dbuff.h:1465
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:514
long int ssize_t
unsigned char uint8_t
#define FR_SBUFF_IN(_start, _len_or_end)
#define fr_sbuff_current(_sbuff_or_marker)
#define fr_sbuff_init_out(_out, _start, _len_or_end)
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:554
static size_t char ** out
Definition value.h:997