Add unit tests for base64 internal functions

Add unit tests for mask_of_range(), enc_char() and dec_value().

When constant-flow testing is enabled, verify that these functions are
constant-flow.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-08-03 12:38:55 +02:00
parent d7d3279fdf
commit a64417afe6
2 changed files with 88 additions and 0 deletions

View file

@ -1,3 +1,33 @@
mask_of_range empty (1..0)
mask_of_range:1:0
mask_of_range empty (255..0)
mask_of_range:255:0
mask_of_range empty (42..7)
mask_of_range:42:7
mask_of_range 0..0
mask_of_range:0:0
mask_of_range 42..42
mask_of_range:42:42
mask_of_range 255..255
mask_of_range:255:255
mask_of_range 0..255
mask_of_range:0:255
mask_of_range 'A'..'Z'
mask_of_range:65:90
enc_char (all digits)
enc_chars:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
dec_value (all characters)
dec_chars:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Test case mbedtls_base64_encode #1 buffer just right
mbedtls_base64_encode:"":"":0:0

View file

@ -8,6 +8,64 @@
* END_DEPENDENCIES
*/
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
void mask_of_range( int low_arg, int high_arg )
{
unsigned char low = low_arg, high = high_arg;
unsigned c;
for( c = 0; c <= 0xff; c++ )
{
mbedtls_test_set_step( c );
TEST_CF_SECRET( &c, sizeof( c ) );
unsigned char m = mbedtls_base64_mask_of_range( low, high, c );
TEST_CF_PUBLIC( &c, sizeof( c ) );
if( low <= c && c <= high )
TEST_EQUAL( m, 0xff );
else
TEST_EQUAL( m, 0 );
}
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
void enc_chars( char *chars )
{
for( unsigned value = 0; value < 64; value++ )
{
mbedtls_test_set_step( value );
TEST_CF_SECRET( &value, sizeof( value ) );
unsigned char digit = mbedtls_base64_enc_char( value );
TEST_CF_PUBLIC( &value, sizeof( value ) );
TEST_CF_PUBLIC( &digit, sizeof( digit ) );
TEST_EQUAL( digit, chars[value] );
}
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
void dec_chars( char *chars )
{
char *p;
const size_t chars_len = strlen( chars );
signed char expected;
for( unsigned c = 0; c <= 0xff; c++ )
{
mbedtls_test_set_step( c );
p = memchr( chars, c, chars_len );
if( p == NULL )
expected = -1;
else
expected = p - chars;
TEST_CF_SECRET( &c, sizeof( c ) );
signed char actual = mbedtls_base64_dec_value( c );
TEST_CF_PUBLIC( &c, sizeof( c ) );
TEST_CF_PUBLIC( &actual, sizeof( actual ) );
TEST_EQUAL( actual, expected );
}
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_base64_encode( char * src_string, char * dst_string,
int dst_buf_size, int result )