Add explicit test coverage for mbedtls_asn1_write_len()

This commit is contained in:
Paul Bakker 2016-07-14 10:27:36 +01:00 committed by Simon Butcher
parent 7eb1243fb4
commit e325db9055
2 changed files with 57 additions and 0 deletions

View file

@ -48,3 +48,24 @@ mbedtls_asn1_write_ia5_string:"ABC":"":3:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
ASN.1 Write IA5 String #5 (Buffer too small for string)
mbedtls_asn1_write_ia5_string:"ABC":"":2:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
ASN.1 Write / Read Length #0 (Len = 0, short form)
mbedtls_asn1_write_len:0:"00":1
ASN.1 Write / Read Length #1 (Len = 127, short form)
mbedtls_asn1_write_len:127:"7F":1
ASN.1 Write / Read Length #2 (Len = 128, long form)
mbedtls_asn1_write_len:128:"8180":2
ASN.1 Write / Read Length #3 (Len = 255, long form)
mbedtls_asn1_write_len:255:"81FF":2
ASN.1 Write / Read Length #4 (Len = 256, long form)
mbedtls_asn1_write_len:256:"820100":3
ASN.1 Write / Read Length #5 (Len = 65535, max supported length)
mbedtls_asn1_write_len:65535:"82FFFF":3
ASN.1 Write / Read Length #6 (Len = 65536, not supported)
mbedtls_asn1_write_len:65536:"":MBEDTLS_ERR_ASN1_INVALID_LENGTH

View file

@ -82,3 +82,39 @@ void mbedtls_asn1_write_ia5_string( char *str, char *hex_asn1,
}
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_asn1_write_len( int len, char *check_str, int result )
{
int ret;
unsigned char buf[150];
unsigned char asn1[150];
unsigned char *p;
size_t asn1_len, i;
memset( buf, GUARD_VAL, sizeof( buf ) );
memset( asn1, 0, sizeof( asn1 ) );
asn1_len = unhexify( asn1, check_str );
p = buf + GUARD_LEN + asn1_len;
ret = mbedtls_asn1_write_len( &p, buf + GUARD_LEN, (size_t) len );
TEST_ASSERT( ret == result );
/* Check for buffer overwrite on both sides */
for( i = 0; i < GUARD_LEN; i++ )
{
TEST_ASSERT( buf[i] == GUARD_VAL );
TEST_ASSERT( buf[GUARD_LEN + asn1_len + i] == GUARD_VAL );
}
if( result >= 0 )
{
TEST_ASSERT( (size_t) ret == asn1_len );
TEST_ASSERT( p + asn1_len == buf + GUARD_LEN + asn1_len );
TEST_ASSERT( memcmp( p, asn1, asn1_len ) == 0 );
}
}
/* END_CASE */