pineapple-src/externals/mbedtls/tests/suites/test_suite_base64.function

134 lines
3.4 KiB
Plaintext
Raw Normal View History

2020-12-28 10:15:37 -05:00
/* BEGIN_HEADER */
#include "mbedtls/base64.h"
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_BASE64_C
* END_DEPENDENCIES
*/
/* BEGIN_CASE */
2021-03-05 19:41:47 -05:00
void mbedtls_base64_encode( char * src_string, char * dst_string,
int dst_buf_size, int result )
2020-12-28 10:15:37 -05:00
{
unsigned char src_str[1000];
unsigned char dst_str[1000];
2021-05-12 10:01:52 -04:00
size_t len, src_len;
2020-12-28 10:15:37 -05:00
memset(src_str, 0x00, 1000);
memset(dst_str, 0x00, 1000);
strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
2021-05-12 10:01:52 -04:00
src_len = strlen( (char *) src_str );
TEST_CF_SECRET( src_str, sizeof( src_str ) );
TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, src_len) == result );
TEST_CF_PUBLIC( src_str, sizeof( src_str ) );
/* dest_str will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
CF failures by unmarking it. */
TEST_CF_PUBLIC( dst_str, len );
2020-12-28 10:15:37 -05:00
if( result == 0 )
{
TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
}
}
/* END_CASE */
/* BEGIN_CASE */
2021-03-05 19:41:47 -05:00
void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
2020-12-28 10:15:37 -05:00
{
unsigned char src_str[1000];
unsigned char dst_str[1000];
size_t len;
int res;
memset(src_str, 0x00, 1000);
memset(dst_str, 0x00, 1000);
strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
TEST_ASSERT( res == result );
if( result == 0 )
{
TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
}
}
/* END_CASE */
/* BEGIN_CASE */
2021-03-05 19:41:47 -05:00
void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
2020-12-28 10:15:37 -05:00
int result )
{
2021-03-05 19:41:47 -05:00
unsigned char *res = NULL;
size_t len;
2020-12-28 10:15:37 -05:00
res = zero_alloc( dst_buf_size );
2021-05-12 10:01:52 -04:00
TEST_CF_SECRET( src->x, src->len );
2021-03-05 19:41:47 -05:00
TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
2021-05-12 10:01:52 -04:00
TEST_CF_PUBLIC( src->x, src->len );
/* res will have had tainted data copied to it, prevent the TEST_ASSERT below from triggering
CF failures by unmarking it. */
TEST_CF_PUBLIC( res, len );
2020-12-28 10:15:37 -05:00
if( result == 0 )
{
TEST_ASSERT( len == strlen( dst ) );
TEST_ASSERT( memcmp( dst, res, len ) == 0 );
}
exit:
mbedtls_free( res );
}
/* END_CASE */
/* BEGIN_CASE */
2021-03-05 19:41:47 -05:00
void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
2020-12-28 10:15:37 -05:00
int result )
{
2021-03-05 19:41:47 -05:00
unsigned char *res = NULL;
size_t len;
2020-12-28 10:15:37 -05:00
res = zero_alloc( dst_buf_size );
TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
strlen( src ) ) == result );
if( result == 0 )
{
2021-03-05 19:41:47 -05:00
TEST_ASSERT( len == dst->len );
TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
2020-12-28 10:15:37 -05:00
}
exit:
mbedtls_free( res );
}
/* END_CASE */
/* BEGIN_CASE */
2021-03-05 19:41:47 -05:00
void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
2020-12-28 10:15:37 -05:00
{
unsigned char dst[1000] = { 0 };
2021-03-05 19:41:47 -05:00
size_t len;
2020-12-28 10:15:37 -05:00
2021-03-05 19:41:47 -05:00
TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
2020-12-28 10:15:37 -05:00
if( result == 0 )
{
TEST_ASSERT( len == strlen( dst_ref ) );
TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
}
exit:
2021-03-05 19:41:47 -05:00
;;
2020-12-28 10:15:37 -05:00
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
2021-03-05 19:41:47 -05:00
void base64_selftest( )
2020-12-28 10:15:37 -05:00
{
TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
}
/* END_CASE */