1/* BEGIN_HEADER */
2#include "mbedtls/base64.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_BASE64_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void mbedtls_base64_encode( char *src_string, char *dst_string, int dst_buf_size,
12                    int result )
13{
14    unsigned char src_str[1000];
15    unsigned char dst_str[1000];
16    size_t len;
17
18    memset(src_str, 0x00, 1000);
19    memset(dst_str, 0x00, 1000);
20
21    strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
22    TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, strlen( (char *) src_str ) ) == result );
23    if( result == 0 )
24    {
25        TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
26    }
27}
28/* END_CASE */
29
30/* BEGIN_CASE */
31void mbedtls_base64_decode( char *src_string, char *dst_string, int result )
32{
33    unsigned char src_str[1000];
34    unsigned char dst_str[1000];
35    size_t len;
36    int res;
37
38    memset(src_str, 0x00, 1000);
39    memset(dst_str, 0x00, 1000);
40
41    strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
42    res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
43    TEST_ASSERT( res == result );
44    if( result == 0 )
45    {
46        TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
47    }
48}
49/* END_CASE */
50
51/* BEGIN_CASE */
52void base64_encode_hex( char *src_hex, char *dst, int dst_buf_size,
53                        int result )
54{
55    unsigned char *src = NULL, *res = NULL;
56    size_t len, src_len;
57
58    src = unhexify_alloc( src_hex, &src_len );
59    res = zero_alloc( dst_buf_size );
60
61    TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src, src_len ) == result );
62    if( result == 0 )
63    {
64        TEST_ASSERT( len == strlen( dst ) );
65        TEST_ASSERT( memcmp( dst, res, len ) == 0 );
66    }
67
68exit:
69    mbedtls_free( src );
70    mbedtls_free( res );
71}
72/* END_CASE */
73
74/* BEGIN_CASE */
75void base64_decode_hex( char *src, char *dst_hex, int dst_buf_size,
76                        int result )
77{
78    unsigned char *dst = NULL, *res = NULL;
79    size_t len, dst_len;
80
81    dst = unhexify_alloc( dst_hex, &dst_len );
82    res = zero_alloc( dst_buf_size );
83
84    TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
85                                strlen( src ) ) == result );
86    if( result == 0 )
87    {
88        TEST_ASSERT( len == dst_len );
89        TEST_ASSERT( memcmp( dst, res, len ) == 0 );
90    }
91
92exit:
93    mbedtls_free( dst );
94    mbedtls_free( res );
95}
96/* END_CASE */
97
98/* BEGIN_CASE */
99void base64_decode_hex_src( char *src_hex, char *dst_ref, int result )
100{
101    unsigned char dst[1000] = { 0 };
102    unsigned char *src;
103    size_t src_len, len;
104
105    src = unhexify_alloc( src_hex, &src_len );
106
107    TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src, src_len ) == result );
108    if( result == 0 )
109    {
110        TEST_ASSERT( len == strlen( dst_ref ) );
111        TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
112    }
113
114exit:
115    mbedtls_free( src );
116}
117/* END_CASE */
118
119/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
120void base64_selftest()
121{
122    TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
123}
124/* END_CASE */
125