2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_HEADER */
|
2009-06-28 21:50:27 +00:00
|
|
|
#include <polarssl/base64.h>
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_HEADER */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:POLARSSL_BASE64_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 13:16:06 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void base64_encode( char *src_string, char *dst_string, int dst_buf_size,
|
|
|
|
int result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char dst_str[1000];
|
2013-08-20 09:48:36 +00:00
|
|
|
size_t len = dst_buf_size;
|
2009-06-28 21:50:27 +00:00
|
|
|
|
|
|
|
memset(src_str, 0x00, 1000);
|
|
|
|
memset(dst_str, 0x00, 1000);
|
|
|
|
|
2014-04-17 14:06:37 +00:00
|
|
|
strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( base64_encode( dst_str, &len, src_str, strlen( (char *) src_str ) ) == result );
|
|
|
|
if( result == 0 )
|
2009-07-11 15:29:30 +00:00
|
|
|
{
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
|
2009-07-11 15:29:30 +00:00
|
|
|
}
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void base64_decode( char *src_string, char *dst_string, int result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
unsigned char src_str[1000];
|
|
|
|
unsigned char dst_str[1000];
|
2011-04-24 15:53:29 +00:00
|
|
|
size_t len = 1000;
|
2009-06-28 21:50:27 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
memset(src_str, 0x00, 1000);
|
|
|
|
memset(dst_str, 0x00, 1000);
|
2014-04-17 14:06:37 +00:00
|
|
|
|
|
|
|
strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
|
2014-04-17 14:07:20 +00:00
|
|
|
res = base64_decode( dst_str, &len, src_str, strlen( (char *) src_str ) );
|
|
|
|
TEST_ASSERT( res == result );
|
2013-08-20 09:48:36 +00:00
|
|
|
if( result == 0 )
|
2009-07-11 15:29:30 +00:00
|
|
|
{
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
|
2009-07-11 15:29:30 +00:00
|
|
|
}
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-05 11:29:38 +00:00
|
|
|
|
2014-07-04 11:50:31 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void base64_encode_hex( char *src_hex, char *dst, int dst_buf_size,
|
|
|
|
int result )
|
|
|
|
{
|
2014-07-10 13:26:12 +00:00
|
|
|
unsigned char *src = NULL, *res = NULL;
|
2014-07-04 11:50:31 +00:00
|
|
|
size_t len = dst_buf_size, src_len;
|
|
|
|
|
|
|
|
src = unhexify_alloc( src_hex, &src_len );
|
|
|
|
res = zero_alloc( dst_buf_size );
|
|
|
|
|
|
|
|
TEST_ASSERT( base64_encode( res, &len, src, src_len ) == result );
|
|
|
|
if( result == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( len == strlen( dst ) );
|
|
|
|
TEST_ASSERT( memcmp( dst, res, len ) == 0 );
|
|
|
|
}
|
2014-07-04 16:35:50 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2014-07-04 16:35:50 +00:00
|
|
|
polarssl_free( src );
|
|
|
|
polarssl_free( res );
|
2014-07-04 11:50:31 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void base64_decode_hex( char *src, char *dst_hex, int dst_buf_size,
|
|
|
|
int result )
|
|
|
|
{
|
2014-07-10 13:26:12 +00:00
|
|
|
unsigned char *dst = NULL, *res = NULL;
|
2014-07-04 11:50:31 +00:00
|
|
|
size_t len = dst_buf_size, dst_len;
|
|
|
|
|
|
|
|
dst = unhexify_alloc( dst_hex, &dst_len );
|
|
|
|
res = zero_alloc( dst_buf_size );
|
|
|
|
|
|
|
|
TEST_ASSERT( base64_decode( res, &len, (unsigned char *) src,
|
|
|
|
strlen( src ) ) == result );
|
|
|
|
if( result == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( len == dst_len );
|
|
|
|
TEST_ASSERT( memcmp( dst, res, len ) == 0 );
|
|
|
|
}
|
2014-07-04 16:35:50 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2014-07-04 16:35:50 +00:00
|
|
|
polarssl_free( dst );
|
|
|
|
polarssl_free( res );
|
2014-07-04 11:50:31 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-10-10 10:48:03 +00:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
|
2013-08-20 09:48:36 +00:00
|
|
|
void base64_selftest()
|
2009-07-05 11:29:38 +00:00
|
|
|
{
|
|
|
|
TEST_ASSERT( base64_self_test( 0 ) == 0 );
|
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|