blob: 3a8bf430f388a35e94db1580cd036f156fc2eb17 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/base64.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_BASE64_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010011void mbedtls_base64_encode( char * src_string, char * dst_string,
12 int dst_buf_size, int result )
Paul Bakker367dae42009-06-28 21:50:27 +000013{
14 unsigned char src_str[1000];
15 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010016 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +000017
18 memset(src_str, 0x00, 1000);
19 memset(dst_str, 0x00, 1000);
20
Paul Bakkerdd0aae92014-04-17 16:06:37 +020021 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010022 TEST_ASSERT( mbedtls_base64_encode( dst_str, dst_buf_size, &len, src_str, strlen( (char *) src_str ) ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020023 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +000024 {
Paul Bakker33b43f12013-08-20 11:48:36 +020025 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +000026 }
Paul Bakker367dae42009-06-28 21:50:27 +000027}
Paul Bakker33b43f12013-08-20 11:48:36 +020028/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000029
Paul Bakker33b43f12013-08-20 11:48:36 +020030/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010031void mbedtls_base64_decode( char * src_string, char * dst_string, int result )
Paul Bakker367dae42009-06-28 21:50:27 +000032{
33 unsigned char src_str[1000];
34 unsigned char dst_str[1000];
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010035 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +020036 int res;
Paul Bakker367dae42009-06-28 21:50:27 +000037
38 memset(src_str, 0x00, 1000);
39 memset(dst_str, 0x00, 1000);
Paul Bakkerdd0aae92014-04-17 16:06:37 +020040
41 strncpy( (char *) src_str, src_string, sizeof(src_str) - 1 );
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010042 res = mbedtls_base64_decode( dst_str, sizeof( dst_str ), &len, src_str, strlen( (char *) src_str ) );
Paul Bakker94b916c2014-04-17 16:07:20 +020043 TEST_ASSERT( res == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020044 if( result == 0 )
Paul Bakker5946fd92009-07-11 15:29:30 +000045 {
Paul Bakker33b43f12013-08-20 11:48:36 +020046 TEST_ASSERT( strcmp( (char *) dst_str, dst_string ) == 0 );
Paul Bakker5946fd92009-07-11 15:29:30 +000047 }
Paul Bakker367dae42009-06-28 21:50:27 +000048}
Paul Bakker33b43f12013-08-20 11:48:36 +020049/* END_CASE */
Paul Bakker3d360822009-07-05 11:29:38 +000050
Paul Bakkerd5983182014-07-04 13:50:31 +020051/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010052void base64_encode_hex( data_t * src, char * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +020053 int result )
54{
Azim Khand30ca132017-06-09 04:32:58 +010055 unsigned char *res = NULL;
56 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +020057
Paul Bakkerd5983182014-07-04 13:50:31 +020058 res = zero_alloc( dst_buf_size );
59
Azim Khand30ca132017-06-09 04:32:58 +010060 TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src->x, src->len ) == result );
Paul Bakkerd5983182014-07-04 13:50:31 +020061 if( result == 0 )
62 {
63 TEST_ASSERT( len == strlen( dst ) );
64 TEST_ASSERT( memcmp( dst, res, len ) == 0 );
65 }
Paul Bakker6697b6c2014-07-04 18:35:50 +020066
Paul Bakkerbd51b262014-07-10 15:26:12 +020067exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +020069}
70/* END_CASE */
71
72/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010073void base64_decode_hex( char * src, data_t * dst, int dst_buf_size,
Paul Bakkerd5983182014-07-04 13:50:31 +020074 int result )
75{
Azim Khand30ca132017-06-09 04:32:58 +010076 unsigned char *res = NULL;
77 size_t len;
Paul Bakkerd5983182014-07-04 13:50:31 +020078
Paul Bakkerd5983182014-07-04 13:50:31 +020079 res = zero_alloc( dst_buf_size );
80
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010081 TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
Paul Bakkerd5983182014-07-04 13:50:31 +020082 strlen( src ) ) == result );
83 if( result == 0 )
84 {
Azim Khand30ca132017-06-09 04:32:58 +010085 TEST_ASSERT( len == dst->len );
86 TEST_ASSERT( memcmp( dst->x, res, len ) == 0 );
Paul Bakkerd5983182014-07-04 13:50:31 +020087 }
Paul Bakker6697b6c2014-07-04 18:35:50 +020088
Paul Bakkerbd51b262014-07-10 15:26:12 +020089exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +020091}
92/* END_CASE */
93
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +020094/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010095void base64_decode_hex_src( data_t * src, char * dst_ref, int result )
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +020096{
97 unsigned char dst[1000] = { 0 };
Azim Khand30ca132017-06-09 04:32:58 +010098 size_t len;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +020099
Azim Khand30ca132017-06-09 04:32:58 +0100100 TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src->x, src->len ) == result );
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200101 if( result == 0 )
102 {
103 TEST_ASSERT( len == strlen( dst_ref ) );
104 TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
105 }
106
107exit:
Azim Khand30ca132017-06-09 04:32:58 +0100108 ;;
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200109}
110/* END_CASE */
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100113void base64_selftest( )
Paul Bakker3d360822009-07-05 11:29:38 +0000114{
Andres AG93012e82016-09-09 09:10:28 +0100115 TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000116}
Paul Bakker33b43f12013-08-20 11:48:36 +0200117/* END_CASE */