2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/base64.h"
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_BASE64_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 15:16:06 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_base64_encode( char *src_string, char *dst_string, int dst_buf_size,
|
2013-08-20 11:48:36 +02:00
|
|
|
int result )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char dst_str[1000];
|
2015-06-02 17:30:35 +02:00
|
|
|
size_t len;
|
2009-06-28 23:50:27 +02:00
|
|
|
|
|
|
|
memset(src_str, 0x00, 1000);
|
|
|
|
memset(dst_str, 0x00, 1000);
|
|
|
|
|
2014-04-17 16:06:37 +02:00
|
|
|
strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
|
2015-06-02 17:30:35 +02:00
|
|
|
TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, strlen( (char *) src_str ) ) == result );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( result == 0 )
|
2009-07-11 17:29:30 +02:00
|
|
|
{
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
|
2009-07-11 17:29:30 +02:00
|
|
|
}
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_base64_decode( char *src_string, char *dst_string, int result )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char dst_str[1000];
|
2015-06-02 17:30:35 +02:00
|
|
|
size_t len;
|
2014-10-15 21:45:39 +02:00
|
|
|
int res;
|
2009-06-28 23:50:27 +02:00
|
|
|
|
|
|
|
memset(src_str, 0x00, 1000);
|
|
|
|
memset(dst_str, 0x00, 1000);
|
2014-04-17 16:06:37 +02:00
|
|
|
|
|
|
|
strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
|
2015-06-02 17:30:35 +02:00
|
|
|
res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( res == result );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( result == 0 )
|
2009-07-11 17:29:30 +02:00
|
|
|
{
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
|
2009-07-11 17:29:30 +02:00
|
|
|
}
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-05 13:29:38 +02:00
|
|
|
|
2014-07-04 13:50:31 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void base64_encode_hex( char *src_hex, char *dst, int dst_buf_size,
|
|
|
|
int result )
|
|
|
|
{
|
2014-07-10 15:26:12 +02:00
|
|
|
unsigned char *src = NULL, *res = NULL;
|
2015-06-02 17:30:35 +02:00
|
|
|
size_t len, src_len;
|
2014-07-04 13:50:31 +02:00
|
|
|
|
|
|
|
src = unhexify_alloc( src_hex, &src_len );
|
|
|
|
res = zero_alloc( dst_buf_size );
|
|
|
|
|
2015-06-02 17:30:35 +02:00
|
|
|
TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src, src_len ) == result );
|
2014-07-04 13:50:31 +02:00
|
|
|
if( result == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( len == strlen( dst ) );
|
|
|
|
TEST_ASSERT( memcmp( dst, res, len ) == 0 );
|
|
|
|
}
|
2014-07-04 18:35:50 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( src );
|
|
|
|
mbedtls_free( res );
|
2014-07-04 13:50:31 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void base64_decode_hex( char *src, char *dst_hex, int dst_buf_size,
|
|
|
|
int result )
|
|
|
|
{
|
2014-07-10 15:26:12 +02:00
|
|
|
unsigned char *dst = NULL, *res = NULL;
|
2015-06-02 17:30:35 +02:00
|
|
|
size_t len, dst_len;
|
2014-07-04 13:50:31 +02:00
|
|
|
|
|
|
|
dst = unhexify_alloc( dst_hex, &dst_len );
|
|
|
|
res = zero_alloc( dst_buf_size );
|
|
|
|
|
2015-06-02 17:30:35 +02:00
|
|
|
TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
|
2014-07-04 13:50:31 +02:00
|
|
|
strlen( src ) ) == result );
|
|
|
|
if( result == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( len == dst_len );
|
|
|
|
TEST_ASSERT( memcmp( dst, res, len ) == 0 );
|
|
|
|
}
|
2014-07-04 18:35:50 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( dst );
|
|
|
|
mbedtls_free( res );
|
2014-07-04 13:50:31 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-10-15 21:45:39 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void base64_decode_hex_src( char *src_hex, char *dst_ref, int result )
|
|
|
|
{
|
|
|
|
unsigned char dst[1000] = { 0 };
|
|
|
|
unsigned char *src;
|
|
|
|
size_t src_len, len;
|
|
|
|
|
|
|
|
src = unhexify_alloc( src_hex, &src_len );
|
|
|
|
|
2015-06-02 17:30:35 +02:00
|
|
|
TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src, src_len ) == result );
|
2014-10-15 21:45:39 +02:00
|
|
|
if( result == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( len == strlen( dst_ref ) );
|
|
|
|
TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( src );
|
2014-10-15 21:45:39 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2013-08-20 11:48:36 +02:00
|
|
|
void base64_selftest()
|
2009-07-05 13:29:38 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_base64_self_test( 0 ) == 0 );
|
2009-07-05 13:29:38 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|