Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2018 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/stdinc.h"
7 : :
8 : : #include "spdk_internal/cunit.h"
9 : :
10 : : #include "util/base64.c"
11 : :
12 : : char text_A[] = "FZB3";
13 : : uint8_t raw_A[] = {0x15, 0x90, 0x77};
14 : : char text_B[] = "AbC/1+c=";
15 : : char text_urlsafe_B[] = "AbC_1-c=";
16 : : uint8_t raw_B[] = {0x01, 0xB0, 0xBF, 0xD7, 0xE7};
17 : : char text_C[] = "AbC/1+cC";
18 : : char text_urlsafe_C[] = "AbC_1-cC";
19 : : uint8_t raw_C[] = {0x01, 0xB0, 0xBF, 0xD7, 0xE7, 0x02};
20 : : char text_D[] = "AbC/1w==";
21 : : char text_urlsafe_D[] = "AbC_1w==";
22 : : uint8_t raw_D[] = {0x01, 0xB0, 0xBF, 0xD7};
23 : : char text_E[] = "AbC12===";
24 : : char text_F[] = "AbCd112";
25 : : char text_G[] = "AbCd12";
26 : : char text_H[] = "AbC12";
27 : : char text_I[] = "AQu/1+cCCBUnOBFWv+HzoL3BOVUBItP2mRDdqhnxAtIT4hD1wbQ30Ylm8R+7khPS";//64 bytes
28 : : char text_urlsafe_I[] =
29 : : "AQu_1-cCCBUnOBFWv-HzoL3BOVUBItP2mRDdqhnxAtIT4hD1wbQ30Ylm8R-7khPS";//64 bytes
30 : : uint8_t raw_I[] = {0x01, 0x0B, 0xBF, 0xD7, 0xE7, 0x02, 0x08, 0x15, 0x27, 0x38, 0x11, 0x56, 0xBF, 0xE1, 0xF3, 0xA0,
31 : : 0xBD, 0xC1, 0x39, 0x55, 0x01, 0x22, 0xD3, 0xF6, 0x99, 0x10, 0xDD, 0xAA, 0x19, 0xF1, 0x02, 0xD2,
32 : : 0x13, 0xE2, 0x10, 0xF5, 0xC1, 0xB4, 0x37, 0xD1, 0x89, 0x66, 0xF1, 0x1F, 0xBB, 0x92, 0x13, 0xD2
33 : : };
34 : : char text_J[] =
35 : : "AQu/1+cCCBUnOBFWv+HzoL3BOVUBItP2mRDdqhnxAtIT4hD1wbQ30Ylm8R+7khPSvcE5VecCCBUZ8QLiEPVm8b3BOVUBItP2GfEC4hD1ZvE5VQEi0/aJZvEfu5LiEPUTvcE5VQEi0/YZEQ==";
36 : : char text_urlsafe_J[] =
37 : : "AQu_1-cCCBUnOBFWv-HzoL3BOVUBItP2mRDdqhnxAtIT4hD1wbQ30Ylm8R-7khPSvcE5VecCCBUZ8QLiEPVm8b3BOVUBItP2GfEC4hD1ZvE5VQEi0_aJZvEfu5LiEPUTvcE5VQEi0_YZEQ==";
38 : : uint8_t raw_J[] = {0x01, 0x0B, 0xBF, 0xD7, 0xE7, 0x02, 0x08, 0x15, 0x27, 0x38, 0x11, 0x56, 0xBF, 0xE1, 0xF3, 0xA0,
39 : : 0xBD, 0xC1, 0x39, 0x55, 0x01, 0x22, 0xD3, 0xF6, 0x99, 0x10, 0xDD, 0xAA, 0x19, 0xF1, 0x02, 0xD2,
40 : : 0x13, 0xE2, 0x10, 0xF5, 0xC1, 0xB4, 0x37, 0xD1, 0x89, 0x66, 0xF1, 0x1F, 0xBB, 0x92, 0x13, 0xD2,
41 : : 0xBD, 0xC1, 0x39, 0x55, 0xE7, 0x02, 0x08, 0x15, 0x19, 0xF1, 0x02, 0xE2, 0x10, 0xF5, 0x66, 0xF1,
42 : : 0xBD, 0xC1, 0x39, 0x55, 0x01, 0x22, 0xD3, 0xF6, 0x19, 0xF1, 0x02, 0xE2, 0x10, 0xF5, 0x66, 0xF1,
43 : : 0x39, 0x55, 0x01, 0x22, 0xD3, 0xF6, 0x89, 0x66, 0xF1, 0x1F, 0xBB, 0x92, 0xE2, 0x10, 0xF5, 0x13,
44 : : 0xBD, 0xC1, 0x39, 0x55, 0x01, 0x22, 0xD3, 0xF6, 0x19, 0x11
45 : : };
46 : :
47 : : static void
48 : 2 : test_base64_get_encoded_strlen(void)
49 : : {
50 : 2 : uint32_t raw_lens[4] = {8, 9, 10, 11};
51 : 2 : uint32_t text_strlens[4] = {12, 12, 16, 16};
52 : : uint32_t text_strlen;
53 : : int i;
54 : :
55 [ + + ]: 10 : for (i = 0; i < 4; i++) {
56 : 8 : text_strlen = spdk_base64_get_encoded_strlen(raw_lens[i]);
57 : 8 : CU_ASSERT_EQUAL(text_strlen, text_strlens[i]);
58 : : }
59 : 2 : }
60 : :
61 : : static void
62 : 2 : test_base64_get_decoded_len(void)
63 : : {
64 : 2 : uint32_t text_strlens[4] = {8, 10, 11, 12};
65 : 2 : uint32_t raw_lens[4] = {6, 7, 8, 9};
66 : : uint32_t bin_len;
67 : : int i;
68 : :
69 [ + + ]: 10 : for (i = 0; i < 4; i++) {
70 : 8 : bin_len = spdk_base64_get_decoded_len(text_strlens[i]);
71 : 8 : CU_ASSERT_EQUAL(bin_len, raw_lens[i]);
72 : : }
73 : 2 : }
74 : :
75 : : static void
76 : 2 : test_base64_encode(void)
77 : : {
78 : 2 : char text[200];
79 : : int ret;
80 : :
81 : 2 : ret = spdk_base64_encode(text, raw_A, sizeof(raw_A));
82 : 2 : CU_ASSERT_EQUAL(ret, 0);
83 : 2 : CU_ASSERT(strcmp(text, text_A) == 0);
84 : 2 : CU_ASSERT_EQUAL(strlen(text), strlen(text_A));
85 : :
86 : 2 : ret = spdk_base64_encode(text, raw_B, sizeof(raw_B));
87 : 2 : CU_ASSERT_EQUAL(ret, 0);
88 : 2 : CU_ASSERT(strcmp(text, text_B) == 0);
89 : 2 : CU_ASSERT_EQUAL(strlen(text), strlen(text_B));
90 : :
91 : 2 : ret = spdk_base64_encode(text, raw_C, sizeof(raw_C));
92 : 2 : CU_ASSERT_EQUAL(ret, 0);
93 : 2 : CU_ASSERT(strcmp(text, text_C) == 0);
94 : :
95 : 2 : ret = spdk_base64_encode(text, raw_D, sizeof(raw_D));
96 : 2 : CU_ASSERT_EQUAL(ret, 0);
97 : 2 : CU_ASSERT(strcmp(text, text_D) == 0);
98 : :
99 : 2 : ret = spdk_base64_encode(text, raw_I, sizeof(raw_I));
100 : 2 : CU_ASSERT_EQUAL(ret, 0);
101 : 2 : CU_ASSERT(strcmp(text, text_I) == 0);
102 : :
103 : 2 : ret = spdk_base64_encode(text, raw_J, sizeof(raw_J));
104 : 2 : CU_ASSERT_EQUAL(ret, 0);
105 : 2 : CU_ASSERT(strcmp(text, text_J) == 0);
106 : :
107 : 2 : ret = spdk_base64_encode(NULL, raw_A, sizeof(raw_A));
108 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
109 : 2 : ret = spdk_base64_encode(text, NULL, sizeof(raw_A));
110 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
111 : 2 : ret = spdk_base64_encode(text, raw_A, 0);
112 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
113 : 2 : }
114 : :
115 : : static void
116 : 2 : test_base64_decode(void)
117 : : {
118 : 2 : char raw_buf[200];
119 : 2 : void *raw = (void *)raw_buf;
120 : 2 : size_t raw_len;
121 : : int ret;
122 : :
123 : : /* len */
124 : 2 : ret = spdk_base64_decode(NULL, &raw_len, text_A);
125 : 2 : CU_ASSERT_EQUAL(ret, 0);
126 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_A));
127 : :
128 : : /* decode */
129 : 2 : ret = spdk_base64_decode(raw, &raw_len, text_A);
130 : 2 : CU_ASSERT_EQUAL(ret, 0);
131 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_A));
132 : 2 : CU_ASSERT(memcmp(raw, raw_A, sizeof(raw_A)) == 0);
133 : :
134 : : /* len */
135 : 2 : ret = spdk_base64_decode(NULL, &raw_len, text_B);
136 : 2 : CU_ASSERT_EQUAL(ret, 0);
137 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_B));
138 : :
139 : : /* decode */
140 : 2 : ret = spdk_base64_decode(raw, &raw_len, text_B);
141 : 2 : CU_ASSERT_EQUAL(ret, 0);
142 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_B));
143 : 2 : CU_ASSERT(memcmp(raw, raw_B, sizeof(raw_B)) == 0);
144 : :
145 : : /* len */
146 : 2 : ret = spdk_base64_decode(NULL, &raw_len, text_C);
147 : 2 : CU_ASSERT_EQUAL(ret, 0);
148 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_C));
149 : :
150 : : /* decode */
151 : 2 : ret = spdk_base64_decode(raw, &raw_len, text_C);
152 : 2 : CU_ASSERT_EQUAL(ret, 0);
153 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_C));
154 : 2 : CU_ASSERT(memcmp(raw, raw_C, sizeof(raw_C)) == 0);
155 : :
156 : : /* len */
157 : 2 : ret = spdk_base64_decode(NULL, &raw_len, text_D);
158 : 2 : CU_ASSERT_EQUAL(ret, 0);
159 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_D));
160 : :
161 : : /* decode */
162 : 2 : ret = spdk_base64_decode(raw, &raw_len, text_D);
163 : 2 : CU_ASSERT_EQUAL(ret, 0);
164 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_D));
165 : 2 : CU_ASSERT(memcmp(raw, raw_D, sizeof(raw_D)) == 0);
166 : :
167 : : /* len */
168 : 2 : ret = spdk_base64_decode(NULL, &raw_len, text_I);
169 : 2 : CU_ASSERT_EQUAL(ret, 0);
170 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_I));
171 : :
172 : : /* decode */
173 : 2 : ret = spdk_base64_decode(raw, &raw_len, text_I);
174 : 2 : CU_ASSERT_EQUAL(ret, 0);
175 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_I));
176 : 2 : CU_ASSERT(memcmp(raw, raw_I, sizeof(raw_I)) == 0);
177 : :
178 : : /* len */
179 : 2 : ret = spdk_base64_decode(NULL, &raw_len, text_J);
180 : 2 : CU_ASSERT_EQUAL(ret, 0);
181 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_J));
182 : :
183 : : /* decode */
184 : 2 : ret = spdk_base64_decode(raw, &raw_len, text_J);
185 : 2 : CU_ASSERT_EQUAL(ret, 0);
186 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_J));
187 : 2 : CU_ASSERT(memcmp(raw, raw_J, sizeof(raw_J)) == 0);
188 : :
189 : 2 : ret = spdk_base64_decode(raw, &raw_len, text_E);
190 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
191 : 2 : ret = spdk_base64_decode(raw, &raw_len, text_F);
192 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
193 : 2 : ret = spdk_base64_decode(raw, &raw_len, text_G);
194 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
195 : 2 : ret = spdk_base64_decode(raw, &raw_len, text_H);
196 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
197 : 2 : ret = spdk_base64_decode(raw, &raw_len, NULL);
198 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
199 : 2 : }
200 : :
201 : : static void
202 : 2 : test_base64_urlsafe_encode(void)
203 : : {
204 : 2 : char text[200];
205 : : int ret;
206 : :
207 : 2 : ret = spdk_base64_urlsafe_encode(text, raw_A, sizeof(raw_A));
208 : 2 : CU_ASSERT_EQUAL(ret, 0);
209 : 2 : CU_ASSERT(strcmp(text, text_A) == 0);
210 : 2 : CU_ASSERT_EQUAL(strlen(text), strlen(text_A));
211 : :
212 : 2 : ret = spdk_base64_urlsafe_encode(text, raw_B, sizeof(raw_B));
213 : 2 : CU_ASSERT_EQUAL(ret, 0);
214 : 2 : CU_ASSERT(strcmp(text, text_urlsafe_B) == 0);
215 : 2 : CU_ASSERT_EQUAL(strlen(text), strlen(text_urlsafe_B));
216 : :
217 : 2 : ret = spdk_base64_urlsafe_encode(text, raw_C, sizeof(raw_C));
218 : 2 : CU_ASSERT_EQUAL(ret, 0);
219 : 2 : CU_ASSERT(strcmp(text, text_urlsafe_C) == 0);
220 : :
221 : 2 : ret = spdk_base64_urlsafe_encode(text, raw_D, sizeof(raw_D));
222 : 2 : CU_ASSERT_EQUAL(ret, 0);
223 : 2 : CU_ASSERT(strcmp(text, text_urlsafe_D) == 0);
224 : :
225 : 2 : ret = spdk_base64_urlsafe_encode(text, raw_I, sizeof(raw_I));
226 : 2 : CU_ASSERT_EQUAL(ret, 0);
227 : 2 : CU_ASSERT(strcmp(text, text_urlsafe_I) == 0);
228 : :
229 : 2 : ret = spdk_base64_urlsafe_encode(text, raw_J, sizeof(raw_J));
230 : 2 : CU_ASSERT_EQUAL(ret, 0);
231 : 2 : CU_ASSERT(strcmp(text, text_urlsafe_J) == 0);
232 : :
233 : 2 : ret = spdk_base64_urlsafe_encode(NULL, raw_A, sizeof(raw_A));
234 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
235 : 2 : ret = spdk_base64_urlsafe_encode(text, NULL, sizeof(raw_A));
236 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
237 : 2 : ret = spdk_base64_urlsafe_encode(text, raw_A, 0);
238 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
239 : 2 : }
240 : :
241 : : static void
242 : 2 : test_base64_urlsafe_decode(void)
243 : : {
244 : 2 : char raw_buf[200];
245 : 2 : void *raw = (void *)raw_buf;
246 : 2 : size_t raw_len = 0;
247 : : int ret;
248 : :
249 : : /* len */
250 : 2 : ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_A);
251 : 2 : CU_ASSERT_EQUAL(ret, 0);
252 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_A));
253 : :
254 : : /* decode */
255 : 2 : ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_A);
256 : 2 : CU_ASSERT_EQUAL(ret, 0);
257 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_A));
258 : 2 : CU_ASSERT(memcmp(raw, raw_A, sizeof(raw_A)) == 0);
259 : :
260 : : /* len */
261 : 2 : ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_urlsafe_B);
262 : 2 : CU_ASSERT_EQUAL(ret, 0);
263 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_B));
264 : :
265 : : /* decode */
266 : 2 : ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_B);
267 : 2 : CU_ASSERT_EQUAL(ret, 0);
268 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_B));
269 : 2 : CU_ASSERT(memcmp(raw, raw_B, sizeof(raw_B)) == 0);
270 : :
271 : : /* len */
272 : 2 : ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_urlsafe_C);
273 : 2 : CU_ASSERT_EQUAL(ret, 0);
274 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_C));
275 : :
276 : : /* decode */
277 : 2 : ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_C);
278 : 2 : CU_ASSERT_EQUAL(ret, 0);
279 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_C));
280 : 2 : CU_ASSERT(memcmp(raw, raw_C, sizeof(raw_C)) == 0);
281 : :
282 : : /* len */
283 : 2 : ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_urlsafe_D);
284 : 2 : CU_ASSERT_EQUAL(ret, 0);
285 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_D));
286 : :
287 : : /* decode */
288 : 2 : ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_D);
289 : 2 : CU_ASSERT_EQUAL(ret, 0);
290 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_D));
291 : 2 : CU_ASSERT(memcmp(raw, raw_D, sizeof(raw_D)) == 0);
292 : :
293 : : /* len */
294 : 2 : ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_urlsafe_I);
295 : 2 : CU_ASSERT_EQUAL(ret, 0);
296 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_I));
297 : :
298 : : /* decode */
299 : 2 : ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_I);
300 : 2 : CU_ASSERT_EQUAL(ret, 0);
301 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_I));
302 : 2 : CU_ASSERT(memcmp(raw, raw_I, sizeof(raw_I)) == 0);
303 : :
304 : : /* len */
305 : 2 : ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_urlsafe_J);
306 : 2 : CU_ASSERT_EQUAL(ret, 0);
307 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_J));
308 : :
309 : : /* decode */
310 : 2 : ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_J);
311 : 2 : CU_ASSERT_EQUAL(ret, 0);
312 : 2 : CU_ASSERT_EQUAL(raw_len, sizeof(raw_J));
313 : 2 : CU_ASSERT(memcmp(raw, raw_J, sizeof(raw_J)) == 0);
314 : :
315 : 2 : ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_E);
316 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
317 : 2 : ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_F);
318 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
319 : 2 : ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_G);
320 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
321 : 2 : ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_H);
322 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
323 : 2 : ret = spdk_base64_urlsafe_decode(raw, &raw_len, NULL);
324 : 2 : CU_ASSERT_EQUAL(ret, -EINVAL);
325 : 2 : }
326 : :
327 : : int
328 : 2 : main(int argc, char **argv)
329 : : {
330 : 2 : CU_pSuite suite = NULL;
331 : : unsigned int num_failures;
332 : :
333 : 2 : CU_initialize_registry();
334 : :
335 : 2 : suite = CU_add_suite("base64", NULL, NULL);
336 : :
337 : 2 : CU_ADD_TEST(suite, test_base64_get_encoded_strlen);
338 : 2 : CU_ADD_TEST(suite, test_base64_get_decoded_len);
339 : 2 : CU_ADD_TEST(suite, test_base64_encode);
340 : 2 : CU_ADD_TEST(suite, test_base64_decode);
341 : 2 : CU_ADD_TEST(suite, test_base64_urlsafe_encode);
342 : 2 : CU_ADD_TEST(suite, test_base64_urlsafe_decode);
343 : :
344 : :
345 : 2 : num_failures = spdk_ut_run_tests(argc, argv, NULL);
346 : :
347 : 2 : CU_cleanup_registry();
348 : :
349 : 2 : return num_failures;
350 : : }
|