From c22bb4994c3a16e10dc9e9cdf63ef6651427a3ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 29 May 2014 17:16:45 +0200 Subject: [PATCH] Add tests for asn1_write_ia5_string() --- tests/suites/test_suite_asn1write.data | 18 +++++++++++ tests/suites/test_suite_asn1write.function | 36 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/tests/suites/test_suite_asn1write.data b/tests/suites/test_suite_asn1write.data index c9b20d733..17f2067c6 100644 --- a/tests/suites/test_suite_asn1write.data +++ b/tests/suites/test_suite_asn1write.data @@ -30,3 +30,21 @@ asn1_write_octet_string:"000102030405060708090A0B0C0D0E0F000102030405060708090A0 ASN.1 Write Octet String #9 (l = 128, buffer too small for string) asn1_write_octet_string:"000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F":"":127:POLARSSL_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write IA5 String #0 (Empty string) +asn1_write_ia5_string:"":"1600":2:2 + +ASN.1 Write IA5 String #1 (Large buffer) +asn1_write_ia5_string:"ABC":"1603414243":10:5 + +ASN.1 Write IA5 String #2 (Buffer just fits) +asn1_write_ia5_string:"ABC":"1603414243":5:5 + +ASN.1 Write IA5 String #3 (Buffer too small for tag) +asn1_write_ia5_string:"ABC":"":4:POLARSSL_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write IA5 String #4 (Buffer too small for len) +asn1_write_ia5_string:"ABC":"":3:POLARSSL_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write IA5 String #5 (Buffer too small for string) +asn1_write_ia5_string:"ABC":"":2:POLARSSL_ERR_ASN1_BUF_TOO_SMALL diff --git a/tests/suites/test_suite_asn1write.function b/tests/suites/test_suite_asn1write.function index 53cb739a4..72e9b4bfb 100644 --- a/tests/suites/test_suite_asn1write.function +++ b/tests/suites/test_suite_asn1write.function @@ -46,3 +46,39 @@ void asn1_write_octet_string( char *hex_str, char *hex_asn1, } } /* END_CASE */ + +/* BEGIN_CASE */ +void asn1_write_ia5_string( char *str, char *hex_asn1, + int buf_len, int result ) +{ + int ret; + unsigned char buf[150]; + unsigned char asn1[150] = { 0 }; + size_t str_len, asn1_len, i; + unsigned char *p; + + memset( buf, GUARD_VAL, sizeof( buf ) ); + + str_len = strlen( str ); + asn1_len = unhexify( asn1, hex_asn1 ); + + p = buf + GUARD_LEN + buf_len; + + ret = asn1_write_ia5_string( &p, buf + GUARD_LEN, str, str_len ); + + /* 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 + buf_len + i] == GUARD_VAL ); + } + + if( result >= 0 ) + { + TEST_ASSERT( (size_t) ret == asn1_len ); + TEST_ASSERT( p + asn1_len == buf + GUARD_LEN + buf_len ); + + TEST_ASSERT( memcmp( p, asn1, asn1_len ) == 0 ); + } +} +/* END_CASE */