blob: 77fa7fdedf3abedd51983b51eca0d4b42818a892 [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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011void mbedtls_base64_encode( char *src_string, char *dst_string, int dst_buf_size,
Paul Bakker33b43f12013-08-20 11:48:36 +020012 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031void 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 */
52void base64_encode_hex( char *src_hex, char *dst, int dst_buf_size,
53 int result )
54{
Paul Bakkerbd51b262014-07-10 15:26:12 +020055 unsigned char *src = NULL, *res = NULL;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010056 size_t len, src_len;
Paul Bakkerd5983182014-07-04 13:50:31 +020057
58 src = unhexify_alloc( src_hex, &src_len );
59 res = zero_alloc( dst_buf_size );
60
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010061 TEST_ASSERT( mbedtls_base64_encode( res, dst_buf_size, &len, src, src_len ) == result );
Paul Bakkerd5983182014-07-04 13:50:31 +020062 if( result == 0 )
63 {
64 TEST_ASSERT( len == strlen( dst ) );
65 TEST_ASSERT( memcmp( dst, res, len ) == 0 );
66 }
Paul Bakker6697b6c2014-07-04 18:35:50 +020067
Paul Bakkerbd51b262014-07-10 15:26:12 +020068exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_free( src );
70 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +020071}
72/* END_CASE */
73
74/* BEGIN_CASE */
75void base64_decode_hex( char *src, char *dst_hex, int dst_buf_size,
76 int result )
77{
Paul Bakkerbd51b262014-07-10 15:26:12 +020078 unsigned char *dst = NULL, *res = NULL;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010079 size_t len, dst_len;
Paul Bakkerd5983182014-07-04 13:50:31 +020080
81 dst = unhexify_alloc( dst_hex, &dst_len );
82 res = zero_alloc( dst_buf_size );
83
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010084 TEST_ASSERT( mbedtls_base64_decode( res, dst_buf_size, &len, (unsigned char *) src,
Paul Bakkerd5983182014-07-04 13:50:31 +020085 strlen( src ) ) == result );
86 if( result == 0 )
87 {
88 TEST_ASSERT( len == dst_len );
89 TEST_ASSERT( memcmp( dst, res, len ) == 0 );
90 }
Paul Bakker6697b6c2014-07-04 18:35:50 +020091
Paul Bakkerbd51b262014-07-10 15:26:12 +020092exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_free( dst );
94 mbedtls_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +020095}
96/* END_CASE */
97
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +020098/* BEGIN_CASE */
99void base64_decode_hex_src( char *src_hex, char *dst_ref, int result )
100{
101 unsigned char dst[1000] = { 0 };
102 unsigned char *src;
103 size_t src_len, len;
104
105 src = unhexify_alloc( src_hex, &src_len );
106
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100107 TEST_ASSERT( mbedtls_base64_decode( dst, sizeof( dst ), &len, src, src_len ) == result );
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200108 if( result == 0 )
109 {
110 TEST_ASSERT( len == strlen( dst_ref ) );
111 TEST_ASSERT( memcmp( dst, dst_ref, len ) == 0 );
112 }
113
114exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_free( src );
Manuel Pégourié-Gonnard64938c62014-10-15 21:45:39 +0200116}
117/* END_CASE */
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200120void base64_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000121{
Andres AG93012e82016-09-09 09:10:28 +0100122 TEST_ASSERT( mbedtls_base64_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000123}
Paul Bakker33b43f12013-08-20 11:48:36 +0200124/* END_CASE */