• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 /* BEGIN_HEADER */
17 
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "securec.h"
23 #include "bsl_sal.h"
24 #include "bsl_errno.h"
25 #include "bsl_base64.h"
26 #include "bsl_uio.h"
27 #include "bsl_base64.h"
28 
29 /* END_HEADER */
30 static const uint8_t src_01[] = "123";
31 static const char encodeResult_01[] = "MTIz";
32 
33 static const uint8_t src_02[] = "a";
34 static const char encodeResult_02[] = "YQ==";
35 
36 static const uint8_t src_03[] = " ";
37 static const char encodeResult_03[] = "IA==";
38 
39 static const uint8_t src_04[] = "\r";
40 static const char encodeResult_04[] = "DQ==";
41 
42 static const uint8_t src_05[] = "\n";
43 static const char encodeResult_05[] = "Cg==";
44 
45 static const uint8_t src_06[] = "\r\n";
46 static const char encodeResult_06[] = "DQo=";
47 
48 static const uint8_t src_07[] = "bas64eVBFH2 46 JF   \n  3274jg891    \n  12974";
49 static const char encodeResult_07[] = "YmFzNjRlVkJGSDIgNDYgSkYgICAKICAzMjc0amc4OTEgICAgCiAgMTI5NzQ=";
50 
51 static const uint8_t src_08[] = "EIR234hdi234 0  idd3     12EH9kfhwu0914l   39u14109u4        8214 klhr184yu h    "
52                                 "0923174 hfweh7e0124W  R2342E\nWF9\niofh392   281h236891FHWY1990hf732";
53 static const char encodeResult_08[] =
54     "RUlSMjM0aGRpMjM0IDAgIGlkZDMgICAgIDEyRUg5a2Zod3UwOTE0bCAgIDM5dTE0MTA5dTQgICAgICAgIDgyMTQga2xocjE4NHl1IGggICAgMDkyMz"
55     "E3NCBoZndlaDdlMDEyNFcgIFIyMzQyRQpXRjkKaW9maDM5MiAgIDI4MWgyMzY4OTFGSFdZMTk5MGhmNzMy";
56 static const char encodeResult_08_withNL[] =
57     "RUlSMjM0aGRpMjM0IDAgIGlkZDMgICAgIDEyRUg5a2Zod3UwOTE0bCAgIDM5dTE0\nMTA5dTQgICAgICAgIDgyMTQga2xocjE4NHl1IGggICAgMDky"
58     "MzE3NCBoZndlaDdl\nMDEyNFcgIFIyMzQyRQpXRjkKaW9maDM5MiAgIDI4MWgyMzY4OTFGSFdZMTk5MGhm\nNzMy\n";
59 static const char encodeResult_09[] =
60     "YUVJUjIzNGhkaTIzNCAwICBpZGQzICAgICAxMkVIOWtmaHd1MDkxNGwgICAzOXUx\nNDEwOXU0ICAgICAgICA4MjE0IGtsaHIxODR5dSBoICAgIDA5"
61     "MjMxNzQgaGZ3ZWg3\nZTAxMjRXICBSMjM0MkUKV0Y5CmlvZmgzOTIgICAyODFoMjM2ODkxRkhXWTE5OTBo\nZjczMmJhczY0ZVZCRkgyIDQ2IEpGIC"
62     "AgCiAgMzI3NGpnODkxICAgIAogIDEyOTc0\n";
63 static const uint8_t src_09[] = "Base64编码和解码测试ChineseVersion";
64 static const uint8_t src_10[] = "ZnUzeWU4R0hFNzEzMjY0RU5EUUlZSFI4OWhoODlURjczVUhGRElVSDMyOThZZk5FMzE4aGQyODNlajMwNEg0";
65 static const uint8_t src_11[] =
66     "aEVXVURKRFE5MkVVMTkwMzcxMzBSSkkyM1VSMDkyMzIzNEQyMUUxMjhZM0UxODI5NEVZM05SRDMyUjI0MjM0RkdG";
67 static const uint8_t src_12[] =
68     "ZnUzeWU4R0hFNzEzMjY0RU5EUUlZSFI4OWhoODlURjczVUhGRElVSDMyOThZZk5FMzE4aGQyODNlajMwNEg0emY=";
69 static const uint8_t src_13[] = "fu3ye8GHE713264ENDQIYHR89hh89TF73UHFDIUH3298Yf";
70 static const uint8_t src_14[] = "fu3ye8GHE713264ENDQIYHR89hh89TF73UHFDIUH3298Yf2";
71 static const uint8_t src_15[] = "fu3ye8GHE713264ENDQIYHR89hh89TF73UHFDIUH3298Yf2k";
72 static const uint8_t src_16[] = "fu3ye8GHE713264ENDQIYHR89hh89TF73UHFDIUH3298YfNE318hd283ej304H";
73 static const uint8_t src_17[] = "fu3ye8GHE713264ENDQIYHR89hh89TF73UHFDIUH3298YfNE318hd283ej304Hf";
74 static const uint8_t src_18[] = "fu3ye8GHE713264ENDQIYHR89hh89TF73UHFDIUH3298YfNE318hd283ej304Hfe";
75 static const uint8_t src_19[] =
76     "HD13fdwCr23r2t4UI3QW1t2vs23F432R1FChfu3ye8GHE713264ENDQIYHR89hh89TF73UHFDIUH3298Yf23hoifdh3f9832yf3ihnfdkJM32RE832"
77     "URDOjdjOWIHFD9832RDJkwmdcOJD38E12U38RDHi3ndewifdh3uiry298398r3843nhrdkncihfhHR2398RE2RFQ32";
78 typedef struct {
79     const uint8_t *src;
80     const uint32_t srcLen;
81     const char *encodeResult;
82     const uint32_t encodeResultLen;
83 } BASE64_TEST_DATA;
84 
85 static const BASE64_TEST_DATA testData[] = {
86     {
87         .src = src_01,
88         .srcLen = (const uint32_t)sizeof(src_01) - 1,
89         .encodeResult = encodeResult_01,
90         .encodeResultLen = (const uint32_t)sizeof(encodeResult_01) - 1,
91     },
92     {
93         .src = src_02,
94         .srcLen = (const uint32_t)sizeof(src_02) - 1,
95         .encodeResult = encodeResult_02,
96         .encodeResultLen = (const uint32_t)sizeof(encodeResult_02) - 1,
97     },
98     {
99         .src = src_03,
100         .srcLen = (const uint32_t)sizeof(src_03) - 1,
101         .encodeResult = encodeResult_03,
102         .encodeResultLen = (const uint32_t)sizeof(encodeResult_03) - 1,
103     },
104     {
105         .src = src_04,
106         .srcLen = (const uint32_t)sizeof(src_04) - 1,
107         .encodeResult = encodeResult_04,
108         .encodeResultLen = (const uint32_t)sizeof(encodeResult_04) - 1,
109     },
110     {
111         .src = src_05,
112         .srcLen = (const uint32_t)sizeof(src_05) - 1,
113         .encodeResult = encodeResult_05,
114         .encodeResultLen = (const uint32_t)sizeof(encodeResult_05) - 1,
115     },
116     {
117         .src = src_06,
118         .srcLen = (const uint32_t)sizeof(src_06) - 1,
119         .encodeResult = encodeResult_06,
120         .encodeResultLen = (const uint32_t)sizeof(encodeResult_06) - 1,
121     },
122     {
123         .src = src_07,
124         .srcLen = (const uint32_t)sizeof(src_07) - 1,
125         .encodeResult = encodeResult_07,
126         .encodeResultLen = (const uint32_t)sizeof(encodeResult_07) - 1,
127     },
128     {
129         .src = src_08,
130         .srcLen = (const uint32_t)sizeof(src_08) - 1,
131         .encodeResult = encodeResult_08,
132         .encodeResultLen = (const uint32_t)sizeof(encodeResult_08) - 1,
133     },
134     {
135         .src = src_09,
136         .srcLen = (const uint32_t)sizeof(src_09) - 1,
137         .encodeResult = NULL,
138         .encodeResultLen = 0,
139     },
140     {
141         .src = src_10,
142         .srcLen = (const uint32_t)sizeof(src_10) - 1,
143         .encodeResult = NULL,
144         .encodeResultLen = 0,
145     },
146     {
147         .src = src_11,
148         .srcLen = (const uint32_t)sizeof(src_11) - 1,
149         .encodeResult = NULL,
150         .encodeResultLen = 0,
151     },
152     {
153         .src = src_12,
154         .srcLen = (const uint32_t)sizeof(src_12) - 1,
155         .encodeResult = NULL,
156         .encodeResultLen = 0,
157     },
158     {
159         .src = src_13,
160         .srcLen = (const uint32_t)sizeof(src_13) - 1,
161         .encodeResult = NULL,
162         .encodeResultLen = 0,
163     },
164     {
165         .src = src_14,
166         .srcLen = (const uint32_t)sizeof(src_14) - 1,
167         .encodeResult = NULL,
168         .encodeResultLen = 0,
169     },
170     {
171         .src = src_15,
172         .srcLen = (const uint32_t)sizeof(src_15) - 1,
173         .encodeResult = NULL,
174         .encodeResultLen = 0,
175     },
176     {
177         .src = src_16,
178         .srcLen = (const uint32_t)sizeof(src_16) - 1,
179         .encodeResult = NULL,
180         .encodeResultLen = 0,
181     },
182     {
183         .src = src_17,
184         .srcLen = (const uint32_t)sizeof(src_17) - 1,
185         .encodeResult = NULL,
186         .encodeResultLen = 0,
187     },
188     {
189         .src = src_18,
190         .srcLen = (const uint32_t)sizeof(src_18) - 1,
191         .encodeResult = NULL,
192         .encodeResultLen = 0,
193     },
194     {
195         .src = src_19,
196         .srcLen = (const uint32_t)sizeof(src_19) - 1,
197         .encodeResult = NULL,
198         .encodeResultLen = 0,
199     },
200 };
201 
202 static const int32_t testCnt = sizeof(testData) / sizeof(testData[0]);
203 
204 /**
205  * @test SDV_BSL_BASE64_FUNC_TC001
206  * @spec  -
207  * @title  Block coding/decoding test
208  * @precon  nan
209  * @brief   1. Call BSL_Base64Encode
210             2. Check whether the encoded result is correct.
211             3. Call BSL_Base64Decode
212             4. Check whether the decoded buffer is the same as the original buffer.
213             5. Check whether the decoded buffer length is the same as the original buffer length.
214 
215  * @expect  1. BSL_SUCCESS
216             2. same
217             3. BSL_SUCCESS
218             4. same
219             5. same
220  * @prior  Level 1
221  * @auto  TRUE
222  */
223 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC001(void)224 void SDV_BSL_BASE64_FUNC_TC001(void)
225 {
226     TestMemInit();
227     for (int32_t i = 0; i < 7; i++) {
228         const uint8_t *srcBuf = testData[i].src;
229         const uint32_t srcLen = testData[i].srcLen;
230 
231         uint32_t encodeBufLen = HITLS_BASE64_ENCODE_LENGTH(srcLen);
232         char encodeBuf[encodeBufLen];
233         uint32_t decodeBufLen = HITLS_BASE64_DECODE_LENGTH(encodeBufLen);
234         uint8_t decodeBuf[decodeBufLen];
235 
236         ASSERT_TRUE(BSL_BASE64_Encode(srcBuf, srcLen, encodeBuf, &encodeBufLen) == BSL_SUCCESS);
237         ASSERT_TRUE(memcmp((const char *)encodeBuf, testData[i].encodeResult, testData[i].encodeResultLen) == 0);
238         ASSERT_TRUE(encodeBufLen == testData[i].encodeResultLen);
239 
240         ASSERT_TRUE(BSL_BASE64_Decode((const char *)encodeBuf, testData[i].encodeResultLen, decodeBuf,
241             &decodeBufLen) == BSL_SUCCESS);
242         ASSERT_TRUE(memcmp((const uint8_t *)decodeBuf, srcBuf, srcLen) == 0);
243         ASSERT_TRUE(decodeBufLen == srcLen);
244     }
245 EXIT:
246     return;
247 }
248 /* END_CASE */
249 
250 /**
251  * @test SDV_BSL_BASE64_FUNC_TC002
252  * @spec  -
253  * @title  Encoding and decoding test for short input streams without line breaks
254  * @precon  nan
255  * @brief   1. Call BSL_Base64EncodeInit
256             2. Call BSL_Base64EncodeUpdate
257             3. Call BSL_Base64EncodeFinal
258             4. Check whether the encoded result is correct.
259             5. Check whether the encoded length is correct.
260             6. Call BSL_Base64DecodeInit
261             7. Call BSL_Base64DecodeUpdate
262             8. Call BSL_Base64DecodeFinal
263             9. Check whether the decoded buffer is the same as the original buffer.
264             10. Check whether the decoded buffer length is the same as the original buffer length.
265 
266  * @expect  1. BSL_SUCCESS
267             2. BSL_SUCCESS
268             3. handling the tail
269             4. same
270             5. same
271             6. BSL_SUCCESS
272             7. BSL_SUCCESS
273             8. BSL_SUCCESS
274             9. same
275             10. same
276  * @prior  Level 1
277  * @auto  TRUE
278  */
279 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC002(void)280 void SDV_BSL_BASE64_FUNC_TC002(void)
281 {
282     for (int32_t i = 0; i < 6; i++) {
283         const uint8_t *srcBuf = testData[i].src;
284         const uint32_t srcLen = testData[i].srcLen;
285 
286         uint32_t encodeBufLen = HITLS_BASE64_ENCODE_LENGTH(srcLen);
287         char *encodeBuf = malloc(encodeBufLen);
288         uint32_t decodeBufLen = HITLS_BASE64_DECODE_LENGTH(encodeBufLen);
289         uint8_t *decodeBuf = malloc(HITLS_BASE64_DECODE_LENGTH(encodeBufLen));
290 
291         uint32_t tmpLen = encodeBufLen;
292         uint32_t total = 0;
293 
294         BSL_Base64Ctx *ctx = BSL_BASE64_CtxNew();
295         ASSERT_TRUE(encodeBuf != NULL);
296         ASSERT_TRUE(decodeBuf != NULL);
297         BSL_BASE64_EncodeInit(ctx);
298         BSL_BASE64_SetFlags(ctx, BSL_BASE64_FLAGS_NO_NEWLINE);
299         ASSERT_TRUE(BSL_BASE64_EncodeUpdate(ctx, srcBuf, srcLen, encodeBuf, &tmpLen) == BSL_SUCCESS);
300         encodeBufLen -= tmpLen;
301         total += tmpLen;
302         tmpLen = encodeBufLen;
303         ASSERT_TRUE(BSL_BASE64_EncodeFinal(ctx, encodeBuf + total, &tmpLen) == BSL_SUCCESS);
304         total += tmpLen;
305 
306         ASSERT_TRUE(total == testData[i].encodeResultLen);
307         ASSERT_TRUE(memcmp((const char *)encodeBuf, testData[i].encodeResult, testData[i].encodeResultLen) == 0);
308 
309         BSL_BASE64_CtxClear(ctx);
310 
311         tmpLen = decodeBufLen;
312         BSL_BASE64_DecodeInit(ctx);
313         ASSERT_TRUE(BSL_BASE64_DecodeUpdate(ctx, encodeBuf, (const uint32_t)total, decodeBuf, &tmpLen) == BSL_SUCCESS);
314         total = 0;
315         decodeBufLen -= tmpLen;
316         total += tmpLen;
317         tmpLen = decodeBufLen;
318         ASSERT_TRUE(BSL_BASE64_DecodeFinal(ctx, decodeBuf + total, &tmpLen) == BSL_SUCCESS);
319         total += tmpLen;
320         ASSERT_TRUE(total == srcLen);
321         ASSERT_TRUE(memcmp((const uint8_t *)decodeBuf, srcBuf, srcLen) == 0);
322 
323         free(encodeBuf);
324         free(decodeBuf);
325         BSL_BASE64_CtxFree(ctx);
326     }
327 EXIT:
328     return;
329 }
330 /* END_CASE */
331 
332 /**
333  * @test SDV_BSL_BASE64_FUNC_TC003
334  * @spec  -
335  * @title  Encoding test for long input streams that require line breaks
336  * @precon  nan
337  * @brief   1. Call BSL_Base64EncodeInit
338             2. Call BSL_Base64EncodeUpdate
339             3. Call BSL_Base64EncodeFinal
340             4. Check whether the encoded result is correct.
341             5. Check whether the encoded length is correct.
342  * @expect  1. BSL_SUCCESS
343             2. BSL_SUCCESS
344             3. handling the tail
345             4. same
346             5. same
347  * @prior  Level 1
348  * @auto  TRUE
349  */
350 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC003(void)351 void SDV_BSL_BASE64_FUNC_TC003(void)
352 {
353     const uint8_t *srcBuf = src_08;
354     const uint32_t srcLen = (const uint32_t)sizeof(src_08) - 1;
355 
356     uint32_t encodeBufLen = HITLS_BASE64_ENCODE_LENGTH(srcLen);
357     char *encodeBuf = malloc(encodeBufLen);
358     uint32_t tmpLen = encodeBufLen;
359     uint32_t total = 0;
360 
361     BSL_Base64Ctx *ctx = BSL_BASE64_CtxNew();
362     ASSERT_TRUE(encodeBuf != NULL);
363     BSL_BASE64_EncodeInit(ctx);
364     ASSERT_TRUE(BSL_BASE64_EncodeUpdate(ctx, srcBuf, srcLen, encodeBuf, &tmpLen) == BSL_SUCCESS);
365     encodeBufLen -= tmpLen;
366     total += tmpLen;
367     tmpLen = encodeBufLen;
368     // encode and check tail for consistency, encodeBuf tail has\n
369     ASSERT_TRUE(BSL_BASE64_EncodeFinal(ctx, encodeBuf + total, &tmpLen) == BSL_SUCCESS);
370     total += tmpLen;
371 
372     ASSERT_TRUE(total == sizeof(encodeResult_08_withNL) - 1);
373     ASSERT_TRUE(memcmp((const char *)encodeBuf, encodeResult_08_withNL, total) == 0);
374 EXIT:
375     free(encodeBuf);
376     BSL_BASE64_CtxFree(ctx);
377 }
378 /* END_CASE */
379 
380 /**
381  * @test SDV_BSL_BASE64_FUNC_TC004
382  * @spec  -
383  * @title  Decoding test for long input streams that require line breaks
384  * @precon  nan
385  * @brief   1. Call BSL_Base64DecodeInit
386             2. Call BSL_Base64DecodeUpdate
387             3. Call BSL_Base64DecodeFinal
388             4. Check whether the decoded buffer is the same as the original buffer.
389             5. Check whether the decoded buffer length is the same as the original buffer length.
390  * @expect  1. BSL_SUCCESS
391             2. BSL_SUCCESS
392             3. BSL_SUCCESS
393             4. same
394             5. same
395  * @prior  Level 1
396  * @auto  TRUE
397  */
398 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC004(void)399 void SDV_BSL_BASE64_FUNC_TC004(void)
400 {
401     const uint8_t *srcBuf = src_08;
402     const uint32_t srcLen = (const uint32_t)sizeof(src_08) - 1;
403     uint32_t encodeBufLen = sizeof(encodeResult_08_withNL) - 1;
404 
405     uint32_t decodeBufLen = HITLS_BASE64_DECODE_LENGTH(encodeBufLen);
406     uint8_t *decodeBuf = malloc(decodeBufLen);
407     uint32_t tmpLen = decodeBufLen;
408     uint32_t total = 0;
409 
410     BSL_Base64Ctx *ctx = BSL_BASE64_CtxNew();
411     ASSERT_TRUE(decodeBuf != NULL);
412     BSL_BASE64_DecodeInit(ctx);
413     ASSERT_TRUE(BSL_BASE64_DecodeUpdate(ctx, encodeResult_08_withNL, encodeBufLen, decodeBuf, &tmpLen) == BSL_SUCCESS);
414     encodeBufLen -= tmpLen;
415     total += tmpLen;
416     tmpLen = encodeBufLen;
417     ASSERT_TRUE(BSL_BASE64_DecodeFinal(ctx, decodeBuf + total, &tmpLen) == BSL_SUCCESS);
418     total += tmpLen;
419 
420     ASSERT_TRUE(total == srcLen);
421     ASSERT_TRUE(memcmp((const uint8_t *)decodeBuf, srcBuf, total) == 0);
422 EXIT:
423     free(decodeBuf);
424     BSL_BASE64_CtxFree(ctx);
425 }
426 /* END_CASE */
427 
428 /**
429  * @test SDV_BSL_BASE64_FUNC_TC005
430  * @spec  -
431  * @title  Encoding and decoding test of the block that generates errors
432  * @precon  nan
433  * @brief   1. Call BSL_Base64Encode
434             2. Check whether the encoded result is correct.
435             3. Call BSL_Base64Decode
436             4. Check whether the decoded buffer is the same as the original buffer.
437             5. Check whether the decoded buffer length is the same as the original buffer length.
438 
439  * @expect  1. BSL_SUCCESS
440             2. same
441             3. BSL_SUCCESS
442             4. same
443             5. same
444  * @prior  Level 1
445  * @auto  TRUE
446  */
447 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC005(void)448 void SDV_BSL_BASE64_FUNC_TC005(void)
449 {
450     const char illEncodeResult[] = "MT-1";
451     uint8_t decodeBuf[4] = {0};
452     uint32_t decodeBufLen = HITLS_BASE64_DECODE_LENGTH(4);
453     ASSERT_TRUE(BSL_BASE64_Decode(illEncodeResult, 4, decodeBuf, &decodeBufLen) != BSL_SUCCESS);
454 EXIT:
455     return;
456 }
457 /* END_CASE */
458 
459 /**
460  * @test SDV_BSL_BASE64_FUNC_TC006
461  * @spec  -
462  * @title  Stream Decoding Test with Errors
463  * @precon  nan
464  * @brief   1. Call BSL_Base64DecodeInit
465             2. Call BSL_Base64DecodeUpdate
466             3. Call BSL_Base64DecodeUpdate
467 
468  * @expect  1. BSL_SUCCESS
469             2. data after padding error
470             3. EOF Error in Middle of Block
471  * @prior  Level 1
472  * @auto  TRUE
473  */
474 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC006(void)475 void SDV_BSL_BASE64_FUNC_TC006(void)
476 {
477     const char illEncodeResult_1[] = "MT=1";
478     const char illEncodeResult_2[] = "MT-1";
479     const char illEncodeResult_3[] = "MT#1";
480     uint32_t decodeBufLen = HITLS_BASE64_DECODE_LENGTH(4);
481     uint8_t *decodeBuf = malloc(decodeBufLen);
482     ASSERT_TRUE(decodeBuf != NULL);
483     BSL_Base64Ctx *ctx = BSL_BASE64_CtxNew();
484     BSL_BASE64_SetFlags(ctx, BSL_BASE64_FLAGS_NO_NEWLINE);
485 
486     BSL_BASE64_DecodeInit(ctx);
487     ASSERT_TRUE(BSL_BASE64_DecodeUpdate(ctx, illEncodeResult_1, sizeof(illEncodeResult_1),
488         decodeBuf, &decodeBufLen) == BSL_BASE64_DATA_AFTER_PADDING);
489 
490     BSL_BASE64_CtxClear(ctx);
491     BSL_BASE64_DecodeInit(ctx);
492     /* If the BSL interface is not invoked to parse the PEM file, the returned value is an error code. */
493     ASSERT_TRUE(BSL_BASE64_DecodeUpdate(ctx, illEncodeResult_2, sizeof(illEncodeResult_2),
494         decodeBuf, &decodeBufLen) == BSL_BASE64_HEADER);
495 
496     BSL_BASE64_CtxClear(ctx);
497     BSL_BASE64_DecodeInit(ctx);
498     ASSERT_TRUE(BSL_BASE64_DecodeUpdate(ctx, illEncodeResult_3, sizeof(illEncodeResult_3),
499         decodeBuf, &decodeBufLen) == BSL_INVALID_ARG);
500 EXIT:
501     free(decodeBuf);
502     BSL_BASE64_CtxFree(ctx);
503 }
504 /* END_CASE */
505 
506 /**
507  * @test SDV_BSL_BASE64_FUNC_TC007
508  * @spec  -
509  * @title  Empty input test
510  * @precon  nan
511  * @brief   1. Call BSL_Base64Encode
512             2. Call BSL_Base64Encode
513             3. Call BSL_Base64Decode
514             4. Call BSL_Base64Decode
515             5. Call BSL_Base64EncodeUpdate
516             6. Call BSL_Base64EncodeFinal
517             7. Call BSL_Base64DecodeUpdate
518             8. Call BSL_Base64DecodeFinal
519 
520  * @expect  1. BSL_NULL_INPUT
521             2. dstBufLen is insufficient:BSL_BASE64_BUF_NOT_ENOUGH
522             3. BSL_NULL_INPUT
523             4. dstBufLen is insufficient:BSL_BASE64_BUF_NOT_ENOUGH
524             5. BSL_NULL_INPUT
525             6. BSL_SUCCESS
526             7. BSL_NULL_INPUT
527             8. BSL_SUCCESS
528  * @prior  Level 1
529  * @auto  TRUE
530  */
531 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC007(void)532 void SDV_BSL_BASE64_FUNC_TC007(void)
533 {
534     uint32_t zeroLen = 0;
535     const uint32_t srcLen = (const uint32_t)sizeof(src_01);
536 
537     uint32_t encodeBufLen = HITLS_BASE64_ENCODE_LENGTH(srcLen);
538     char *encodeBuf = malloc(encodeBufLen);
539     uint32_t decodeBufLen = HITLS_BASE64_DECODE_LENGTH(encodeBufLen);
540     uint8_t *decodeBuf = malloc(decodeBufLen);
541     ASSERT_TRUE(encodeBuf != NULL);
542     ASSERT_TRUE(decodeBuf != NULL);
543     /* Block codec empty input test */
544     ASSERT_TRUE(BSL_BASE64_Encode(NULL, zeroLen, encodeBuf, &encodeBufLen) == BSL_NULL_INPUT);
545     ASSERT_TRUE(BSL_BASE64_Encode(src_01, sizeof(src_01) - 1, encodeBuf, &zeroLen) == BSL_BASE64_BUF_NOT_ENOUGH);
546     ASSERT_TRUE(BSL_BASE64_Decode(NULL, 0, decodeBuf, &decodeBufLen) == BSL_NULL_INPUT);
547     ASSERT_TRUE(BSL_BASE64_Decode(encodeResult_01, 4, decodeBuf, &zeroLen) == BSL_BASE64_BUF_NOT_ENOUGH);
548 
549     /* Stream encoding/decoding empty input test */
550     BSL_Base64Ctx *ctx = BSL_BASE64_CtxNew();
551     BSL_BASE64_EncodeInit(ctx);
552     ASSERT_TRUE(BSL_BASE64_EncodeUpdate(ctx, NULL, zeroLen, encodeBuf, &encodeBufLen) == BSL_NULL_INPUT);
553     ASSERT_TRUE(BSL_BASE64_EncodeFinal(ctx, encodeBuf, &encodeBufLen) == BSL_SUCCESS);
554 
555     BSL_BASE64_CtxClear(ctx);
556     BSL_BASE64_DecodeInit(ctx);
557     ASSERT_TRUE(BSL_BASE64_DecodeUpdate(ctx, NULL, (const uint32_t)encodeBufLen,
558         decodeBuf, &decodeBufLen) == BSL_NULL_INPUT);
559     ASSERT_TRUE(BSL_BASE64_DecodeFinal(ctx, decodeBuf, &decodeBufLen) == BSL_SUCCESS);
560 EXIT:
561     free(encodeBuf);
562     free(decodeBuf);
563     BSL_BASE64_CtxFree(ctx);
564 }
565 /* END_CASE */
566 
567 /**
568  * @test SDV_BSL_BASE64_FUNC_TC008
569  * @spec  -
570  * @title  Multiple update tests
571  * @precon  nan
572  * @brief   1. Call BSL_Base64EncodeUpdate
573             2. Call BSL_Base64EncodeUpdate
574             3. Call BSL_Base64EncodeUpdate
575             4. Call BSL_Base64EncodeFinal
576             5. Call BSL_Base64DecodeUpdate
577             6. Call BSL_Base64DecodeUpdate
578             7. Call BSL_Base64DecodeUpdate
579             8. Call BSL_Base64DecodeFinal
580 
581  * @expect  1. BSL_SUCCESS -> The data is not encoded and is cached in the buffer. dstBufLen is set to 0.
582             2. BSL_SUCCESS
583             3. BSL_SUCCESS -> 3 + 44 < 48, The srcBuf is shorter than the length of a buffer. The data is cached in the
584                               buffer, and dstBufLen is set to 0.
585             4. BSL_SUCCESS -> Handles 3 + 44 characters
586             5. BSL_SUCCESS -> Data that is not decoded is stored in the buffer, and dstBufLen is set to 0.
587             6. BSL_SUCCESS
588             7. BSL_SUCCESS
589             8. BSL_SUCCESS
590  * @prior  Level 1
591  * @auto  TRUE
592  */
593 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC008(void)594 void SDV_BSL_BASE64_FUNC_TC008(void)
595 {
596     uint32_t srcLen = testData[0].srcLen + testData[7].srcLen + testData[6].srcLen;
597     uint32_t encodeBufLen = HITLS_BASE64_ENCODE_LENGTH(srcLen);
598     char *encodeBuf = malloc(encodeBufLen);
599     uint32_t decodeBufLen = HITLS_BASE64_DECODE_LENGTH(encodeBufLen);
600     uint8_t *decodeBuf = malloc(HITLS_BASE64_DECODE_LENGTH(encodeBufLen));
601 
602     /*
603      * The output parameter dstBufLen needs to be updated in real time when the update operation is performed for
604      * multiple times.
605      */
606     uint32_t tmpLen = encodeBufLen;
607     uint32_t total = 0;
608     ASSERT_TRUE(encodeBuf != NULL);
609     ASSERT_TRUE(decodeBuf != NULL);
610     /* encode */
611     BSL_Base64Ctx *ctx = BSL_BASE64_CtxNew();
612     BSL_BASE64_EncodeInit(ctx);
613     ASSERT_TRUE(BSL_BASE64_EncodeUpdate(ctx, testData[1].src, testData[1].srcLen, encodeBuf, &tmpLen) ==
614         BSL_SUCCESS); /* 1bytes */
615     encodeBufLen -= tmpLen;
616     total += tmpLen;
617     tmpLen = encodeBufLen;
618     ASSERT_TRUE(BSL_BASE64_EncodeUpdate(ctx, testData[7].src, testData[7].srcLen, encodeBuf + total, &tmpLen) ==
619         BSL_SUCCESS); /* 147bytes */
620     encodeBufLen -= tmpLen;
621     total += tmpLen;
622     tmpLen = encodeBufLen;
623     ASSERT_TRUE(BSL_BASE64_EncodeUpdate(ctx, testData[6].src, testData[6].srcLen, encodeBuf + total, &tmpLen) ==
624         BSL_SUCCESS); /* 44bytes */
625     encodeBufLen -= tmpLen;
626     total += tmpLen;
627     tmpLen = encodeBufLen;
628     ASSERT_TRUE(BSL_BASE64_EncodeFinal(ctx, encodeBuf + total, &tmpLen) == BSL_SUCCESS);
629     total += tmpLen;
630 
631     ASSERT_TRUE((sizeof(encodeResult_09) - 1) == total);
632     ASSERT_TRUE(memcmp((const char *)encodeBuf, encodeResult_09, total) == 0);
633 
634     BSL_BASE64_CtxClear(ctx);
635 
636     /* decode */
637     tmpLen = decodeBufLen;
638     total = 0;
639     BSL_BASE64_DecodeInit(ctx);
640     ASSERT_TRUE(BSL_BASE64_DecodeUpdate(ctx, testData[0].encodeResult,
641         (const uint32_t)testData[0].encodeResultLen, decodeBuf, &tmpLen) == BSL_SUCCESS); /* 4bytes */
642     decodeBufLen -= tmpLen;
643     total += tmpLen;
644     tmpLen = decodeBufLen;
645     ASSERT_TRUE(BSL_BASE64_DecodeUpdate(ctx, testData[7].encodeResult,
646         (const uint32_t)testData[7].encodeResultLen, decodeBuf + total, &tmpLen) == BSL_SUCCESS); /* 196bytes */
647     decodeBufLen -= tmpLen;
648     total += tmpLen;
649     tmpLen = decodeBufLen;
650     ASSERT_TRUE(BSL_BASE64_DecodeUpdate(ctx, testData[6].encodeResult,
651         (const uint32_t)testData[6].encodeResultLen, decodeBuf + total, &tmpLen) == BSL_SUCCESS); /* 60bytes */
652     decodeBufLen -= tmpLen;
653     total += tmpLen;
654     tmpLen = decodeBufLen;
655     ASSERT_TRUE(BSL_BASE64_DecodeFinal(ctx, decodeBuf + total, &tmpLen) == BSL_SUCCESS);
656     total += tmpLen;
657 
658     ASSERT_TRUE(srcLen == total);
659 EXIT:
660     free(encodeBuf);
661     free(decodeBuf);
662     BSL_BASE64_CtxFree(ctx);
663 }
664 /* END_CASE */
665 
Base64BlockEncDec(const uint8_t * buf,const uint32_t len)666 void Base64BlockEncDec(const uint8_t *buf, const uint32_t len)
667 {
668     const uint8_t *src = buf;
669     const uint32_t srcLen = len;
670 
671     uint32_t hitlsEncLen = HITLS_BASE64_ENCODE_LENGTH(len);
672     char *hitlsEncResult = BSL_SAL_Malloc(hitlsEncLen);
673 
674     uint32_t hitlsDecLen = HITLS_BASE64_DECODE_LENGTH(hitlsEncLen);
675     uint8_t *hitlsDecResult = BSL_SAL_Malloc(hitlsDecLen);
676     TRUE_OR_EXIT(BSL_BASE64_Encode(src, srcLen, hitlsEncResult, &hitlsEncLen) == BSL_SUCCESS);
677 
678     TRUE_OR_EXIT(BSL_BASE64_Decode(hitlsEncResult, hitlsEncLen, hitlsDecResult, &hitlsDecLen) == BSL_SUCCESS);
679     TRUE_OR_EXIT(hitlsDecLen == srcLen);
680 EXIT:
681     BSL_SAL_Free(hitlsEncResult);
682     BSL_SAL_Free(hitlsDecResult);
683 }
684 
685 /**
686  * @test SDV_BSL_BASE64_FUNC_TC009
687  * @spec  -
688  * @title  Block coding/decoding test
689  * @precon  nan
690  * @brief   1. Call BSL_Base64Encode/EVP_EncodeBlock
691             2. Check whether the encoded result is correct.
692             3. Call BSL_Base64Decode/EVP_DecodeBlock
693             4. Check whether the decoded buffer is the same as the original buffer.
694             5. Check whether the decoded buffer length is the same as the original buffer length.
695 
696  * @expect  1. Succeeded
697             2. same
698             3. Succeeded
699             4. same
700             5. same
701  * @prior  Level 1
702  * @auto  TRUE
703  */
704 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC009(void)705 void SDV_BSL_BASE64_FUNC_TC009(void)
706 {
707     Base64BlockEncDec(testData[0].src, testData[0].srcLen);
708     for (int i = 8; i < testCnt; i++) {
709         Base64BlockEncDec(testData[i].src, testData[i].srcLen);
710     }
711 }
712 /* END_CASE */
713 
Base64Stream(const uint8_t * buf,const uint32_t len)714 void Base64Stream(const uint8_t *buf, const uint32_t len)
715 {
716     const uint8_t *src = buf;
717     const uint32_t srcLen = len;
718 
719     uint32_t hitlsEncLen = HITLS_BASE64_ENCODE_LENGTH(len);
720     char *hitlsEncResult = BSL_SAL_Malloc(hitlsEncLen);
721 
722     uint32_t hitlsDecLen = HITLS_BASE64_DECODE_LENGTH(hitlsEncLen);
723     uint8_t *hitlsDecResult = BSL_SAL_Malloc(hitlsDecLen);
724 
725     /* encode */
726     // hitls stream encoding
727     uint32_t tmpLen = hitlsEncLen;
728     uint32_t total = 0;
729     BSL_Base64Ctx *ctx = BSL_BASE64_CtxNew();
730     BSL_BASE64_EncodeInit(ctx);
731     TRUE_OR_EXIT(BSL_BASE64_EncodeUpdate(ctx, src, srcLen, hitlsEncResult, &tmpLen) == BSL_SUCCESS);
732     hitlsEncLen -= tmpLen;
733     total += tmpLen;
734     tmpLen = hitlsEncLen;
735     TRUE_OR_EXIT(BSL_BASE64_EncodeFinal(ctx, hitlsEncResult + total, &tmpLen) == BSL_SUCCESS);
736     total += tmpLen;
737 
738     /* decode */
739     // hitls stream encoding
740     BSL_BASE64_CtxClear(ctx);
741     tmpLen = hitlsDecLen;
742     BSL_BASE64_DecodeInit(ctx);
743     TRUE_OR_EXIT(BSL_BASE64_DecodeUpdate(ctx, hitlsEncResult, (const uint32_t)total, hitlsDecResult, &tmpLen) ==
744         BSL_SUCCESS);
745     total = 0;
746     hitlsDecLen -= tmpLen;
747     total += tmpLen;
748     tmpLen = hitlsDecLen;
749     TRUE_OR_EXIT(BSL_BASE64_DecodeFinal(ctx, hitlsDecResult + total, &tmpLen) == BSL_SUCCESS);
750     total += tmpLen;
751 
752 EXIT:
753     BSL_SAL_Free(hitlsEncResult);
754     BSL_SAL_Free(hitlsDecResult);
755     BSL_BASE64_CtxFree(ctx);
756 }
757 
758 /**
759  * @test SDV_BSL_BASE64_FUNC_TC010
760  * @spec  -
761  * @title  Single-flow encoding/decoding test
762  * @precon  nan
763  * @brief   1. Call BSL_Base64EncodeInit/EVP_EncodeInit
764             2. Call BSL_Base64EncodeUpdate/EVP_EncodeUpdate
765             3. Call BSL_Base64EncodeFinal/EVP_EncodeFinal
766             4. Check whether the encoded result is correct.
767             5. Check whether the encoded length is correct.
768             6. Call BSL_Base64DecodeInit/EVP_DecodeInit
769             7. Call BSL_Base64DecodeUpdate/EVP_DecodeUpdate
770             8. Call BSL_Base64DecodeFinal/EVP_DecodeFinal
771             9. Check whether the decoded buffer is the same as the original buffer.
772             10. Check whether the decoded buffer length is the same as the original buffer length.
773 
774  * @expect  1. Succeeded
775             2. BSL_SUCCESS
776             3. handling the tail
777             4. same
778             5. same
779             6. Succeeded
780             7. BSL_SUCCESS
781             8. BSL_SUCCESS
782             9. same
783             10. same
784  * @prior  Level 1
785  * @auto  TRUE
786  */
787 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC010(void)788 void SDV_BSL_BASE64_FUNC_TC010(void)
789 {
790     for (int i = 0; i < testCnt; i++) {
791         Base64Stream(testData[i].src, testData[i].srcLen);
792     }
793 }
794 /* END_CASE */
795 
Base64StreamMultiUpdate(const BASE64_TEST_DATA data[])796 void Base64StreamMultiUpdate(const BASE64_TEST_DATA data[])
797 {
798     uint32_t hitlsEncLen = HITLS_BASE64_ENCODE_LENGTH(512);
799     char *hitlsEncResult = malloc(hitlsEncLen);
800 
801     uint32_t hitlsDecLen = HITLS_BASE64_DECODE_LENGTH(hitlsEncLen);
802     uint8_t *hitlsDecResult = malloc(hitlsDecLen);
803     /* encode */
804     // hitls stream encoding
805     uint32_t tmpLen = hitlsEncLen;
806     uint32_t total = 0;
807     BSL_Base64Ctx *ctx = BSL_BASE64_CtxNew();
808     ASSERT_TRUE(hitlsEncResult != NULL);
809     ASSERT_TRUE(hitlsDecResult != NULL);
810     BSL_BASE64_EncodeInit(ctx);
811     TRUE_OR_EXIT(BSL_BASE64_EncodeUpdate(ctx, data[12].src, data[12].srcLen, hitlsEncResult, &tmpLen) == BSL_SUCCESS);
812     hitlsEncLen -= tmpLen;
813     total += tmpLen;
814     tmpLen = hitlsEncLen;
815     TRUE_OR_EXIT(BSL_BASE64_EncodeUpdate(ctx, data[13].src, data[13].srcLen, hitlsEncResult + total, &tmpLen) ==
816         BSL_SUCCESS);
817     hitlsEncLen -= tmpLen;
818     total += tmpLen;
819     tmpLen = hitlsEncLen;
820     TRUE_OR_EXIT(BSL_BASE64_EncodeUpdate(ctx, data[14].src, data[14].srcLen, hitlsEncResult + total, &tmpLen) ==
821         BSL_SUCCESS);
822     hitlsEncLen -= tmpLen;
823     total += tmpLen;
824     tmpLen = hitlsEncLen;
825     TRUE_OR_EXIT(BSL_BASE64_EncodeFinal(ctx, hitlsEncResult + total, &tmpLen) == BSL_SUCCESS);
826     total += tmpLen;
827 
828     /* decode */
829     // hitls stream encoding
830     BSL_BASE64_CtxClear(ctx);
831     total = 0;
832     tmpLen = hitlsDecLen;
833     BSL_BASE64_DecodeInit(ctx);
834     TRUE_OR_EXIT(BSL_BASE64_DecodeUpdate(ctx, (const char *)data[9].src, data[9].srcLen, hitlsDecResult, &tmpLen) ==
835         BSL_SUCCESS);
836     hitlsDecLen -= tmpLen;
837     total += tmpLen;
838     tmpLen = hitlsDecLen;
839     TRUE_OR_EXIT(BSL_BASE64_DecodeUpdate(ctx, (const char *)data[10].src, data[10].srcLen, hitlsDecResult + total,
840         &tmpLen) == BSL_SUCCESS);
841     hitlsDecLen -= tmpLen;
842     total += tmpLen;
843     tmpLen = hitlsDecLen;
844     TRUE_OR_EXIT(BSL_BASE64_DecodeUpdate(ctx, (const char *)data[11].src, data[11].srcLen, hitlsDecResult + total,
845         &tmpLen) == BSL_SUCCESS);
846     hitlsDecLen -= tmpLen;
847     total += tmpLen;
848     tmpLen = hitlsDecLen;
849     TRUE_OR_EXIT(BSL_BASE64_DecodeFinal(ctx, hitlsDecResult + total, &tmpLen) == BSL_SUCCESS);
850     total += tmpLen;
851 
852 EXIT:
853     free(hitlsEncResult);
854     free(hitlsDecResult);
855     BSL_BASE64_CtxFree(ctx);
856 }
857 
858 /**
859  * @test SDV_BSL_BASE64_FUNC_TC011
860  * @spec  -
861  * @title  Multiple update tests
862  * @precon  nan
863  * @brief   1. Call BSL_Base64EncodeUpdate/EVP_EncodeUpdate
864             2. Call BSL_Base64EncodeUpdate/EVP_EncodeUpdate
865             3. Call BSL_Base64EncodeUpdate/EVP_EncodeUpdate
866             4. Call BSL_Base64EncodeFinal/EVP_EncodeFinal
867             5. memcmp encode result
868             6. Call BSL_Base64DecodeUpdate/EVP_DecodeUpdate
869             7. Call BSL_Base64DecodeUpdate/EVP_DecodeUpdate
870             8. Call BSL_Base64DecodeUpdate/EVP_DecodeUpdate
871             9. Call BSL_Base64DecodeFinal/EVP_DecodeFinal
872             10. memcmp decode result
873  * @expect  1.2.3.4. succeeded
874             5.same
875             6.7.8.9. succeeded
876             10.same
877  * @prior  Level 1
878  * @auto  TRUE
879  */
880 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC011(void)881 void SDV_BSL_BASE64_FUNC_TC011(void)
882 {
883     Base64StreamMultiUpdate(testData);
884 }
885 /* END_CASE */
886 
887 /* BEGIN_CASE */
SDV_BSL_BASE64_FUNC_TC012(char * src,int expectRes)888 void SDV_BSL_BASE64_FUNC_TC012(char *src, int expectRes)
889 {
890     uint32_t srcBufLen = strlen(src);
891     uint32_t dstBufLen = HITLS_BASE64_DECODE_LENGTH(srcBufLen);
892     uint8_t *dst = BSL_SAL_Malloc(dstBufLen);
893     ASSERT_TRUE(dst != NULL);
894     ASSERT_EQ(BSL_BASE64_Decode(src, srcBufLen, dst, &dstBufLen), (int32_t)expectRes);
895 EXIT:
896     BSL_SAL_Free(dst);
897 }
898 /* END_CASE */