From e325db9055fa92c375b286448e5a864a669ae62c Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Thu, 14 Jul 2016 10:27:36 +0100 Subject: [PATCH] Add explicit test coverage for mbedtls_asn1_write_len() --- tests/suites/test_suite_asn1write.data | 21 +++++++++++++ tests/suites/test_suite_asn1write.function | 36 ++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/tests/suites/test_suite_asn1write.data b/tests/suites/test_suite_asn1write.data index 61934f556..0c4074ed3 100644 --- a/tests/suites/test_suite_asn1write.data +++ b/tests/suites/test_suite_asn1write.data @@ -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 diff --git a/tests/suites/test_suite_asn1write.function b/tests/suites/test_suite_asn1write.function index a15c8216d..160190f65 100644 --- a/tests/suites/test_suite_asn1write.function +++ b/tests/suites/test_suite_asn1write.function @@ -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 */