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,
12                            int dst_buf_size, 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( data_t * src, char * dst, int dst_buf_size,
53                        int result )
54{
55    unsigned char *res = NULL;
56    size_t len;
57
58    res = mbedtls_test_zero_alloc( dst_buf_size );
59
60    TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
61    if( result == 0 )
62    {
63        TEST_ASSERT( len == strlen( dst ) );
64        TEST_ASSERT( memcmp( dst, res, len ) == 0 );
65    }
66
67exit:
68    mbedtls_free( res );
69}
70/* END_CASE */
71
72/* BEGIN_CASE */
73void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
74                        int result )
75{
76    unsigned char *res = NULL;
77    size_t len;
78
79    res = mbedtls_test_zero_alloc( dst_buf_size );
80
81    TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
82                                strlen( src ) ) == result );
83    if( result == 0 )
84    {
85        TEST_ASSERT( len == dst->len );
86        TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
87    }
88
89exit:
90    mbedtls_free( res );
91}
92/* END_CASE */
93
94/* BEGIN_CASE */
95void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
96{
97    unsigned char dst[1000] = { 0 };
98    size_t len;
99
100    TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
101    if( result == 0 )
102    {
103        TEST_ASSERT( len == strlen( dst_ref ) );
104        TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
105    }
106
107exit:
108    ;;
109}
110/* END_CASE */
111
112/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
113void base64_selftest(  )
114{
115    TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
116}
117/* END_CASE */
118