blob: b8a318f48d7b8753607893544aa2a0070c7fd64e [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00002#include <polarssl/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
6 * depends_on:POLARSSL_BASE64_C
7 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
11void base64_encode( char *src_string, char *dst_string, int dst_buf_size,
12 int result )
Paul Bakker367dae42009-06-28 21:50:27 +000013{
14 unsigned char src_str[1000];
15 unsigned char dst_str[1000];
Paul Bakker33b43f12013-08-20 11:48:36 +020016 size_t len = dst_buf_size;
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 );
Paul Bakker33b43f12013-08-20 11:48:36 +020022 TEST_ASSERT( base64_encode( dst_str, &len, src_str, strlen( (char *) src_str ) ) == result );
23 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 */
31void 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];
Paul Bakkerf4a3f302011-04-24 15:53:29 +000035 size_t len = 1000;
Paul Bakker367dae42009-06-28 21:50:27 +000036 int res;
37
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 );
Paul Bakker94b916c2014-04-17 16:07:20 +020042 res = base64_decode( dst_str, &len, src_str, strlen( (char *) src_str ) );
43 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{
55 unsigned char *src, *res;
56 size_t len = dst_buf_size, src_len;
57
58 src = unhexify_alloc( src_hex, &src_len );
59 res = zero_alloc( dst_buf_size );
60
61 TEST_ASSERT( base64_encode( res, &len, src, src_len ) == result );
62 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
68 polarssl_free( src );
69 polarssl_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +020070}
71/* END_CASE */
72
73/* BEGIN_CASE */
74void base64_decode_hex( char *src, char *dst_hex, int dst_buf_size,
75 int result )
76{
77 unsigned char *dst, *res;
78 size_t len = dst_buf_size, dst_len;
79
80 dst = unhexify_alloc( dst_hex, &dst_len );
81 res = zero_alloc( dst_buf_size );
82
83 TEST_ASSERT( base64_decode( res, &len, (unsigned char *) src,
84 strlen( src ) ) == result );
85 if( result == 0 )
86 {
87 TEST_ASSERT( len == dst_len );
88 TEST_ASSERT( memcmp( dst, res, len ) == 0 );
89 }
Paul Bakker6697b6c2014-07-04 18:35:50 +020090
91 polarssl_free( dst );
92 polarssl_free( res );
Paul Bakkerd5983182014-07-04 13:50:31 +020093}
94/* END_CASE */
95
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +020096/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +020097void base64_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +000098{
99 TEST_ASSERT( base64_self_test( 0 ) == 0 );
100}
Paul Bakker33b43f12013-08-20 11:48:36 +0200101/* END_CASE */