2015-01-30 13:12:11 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2015-01-30 13:12:11 +01:00
|
|
|
#else
|
2015-01-30 13:12:11 +01:00
|
|
|
#include <stdio.h>
|
2015-06-22 10:48:01 +02:00
|
|
|
#include <stdlib.h>
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_exit exit
|
|
|
|
#define mbedtls_free free
|
2015-06-22 10:48:01 +02:00
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_fprintf fprintf
|
|
|
|
#define mbedtls_printf printf
|
2015-06-22 10:48:01 +02:00
|
|
|
#define mbedtls_snprintf snprintf
|
2015-01-30 13:12:11 +01:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/memory_buffer_alloc.h"
|
2015-01-26 14:58:04 +01:00
|
|
|
#endif
|
|
|
|
|
2013-08-16 13:31:10 +02:00
|
|
|
static int test_errors = 0;
|
|
|
|
|
2013-09-15 17:05:21 +02:00
|
|
|
SUITE_PRE_DEP
|
|
|
|
#define TEST_SUITE_ACTIVE
|
|
|
|
|
2013-11-25 13:29:43 +01:00
|
|
|
static int test_assert( int correct, const char *test )
|
2013-08-16 13:31:10 +02:00
|
|
|
{
|
|
|
|
if( correct )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
test_errors++;
|
2013-08-19 14:02:10 +02:00
|
|
|
if( test_errors == 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "FAILED\n" );
|
|
|
|
mbedtls_printf( " %s\n", test );
|
2013-08-16 13:31:10 +02:00
|
|
|
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
2013-08-20 12:41:33 +02:00
|
|
|
#define TEST_ASSERT( TEST ) \
|
|
|
|
do { test_assert( (TEST) ? 1 : 0, #TEST ); \
|
2014-07-10 14:59:25 +02:00
|
|
|
if( test_errors) goto exit; \
|
2013-08-20 12:41:33 +02:00
|
|
|
} while (0)
|
2013-08-16 13:31:10 +02:00
|
|
|
|
|
|
|
int verify_string( char **str )
|
|
|
|
{
|
|
|
|
if( (*str)[0] != '"' ||
|
|
|
|
(*str)[strlen( *str ) - 1] != '"' )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
|
2013-08-16 13:31:10 +02:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
(*str)++;
|
|
|
|
(*str)[strlen( *str ) - 1] = '\0';
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int verify_int( char *str, int *value )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int minus = 0;
|
|
|
|
int digits = 1;
|
|
|
|
int hex = 0;
|
|
|
|
|
|
|
|
for( i = 0; i < strlen( str ); i++ )
|
|
|
|
{
|
|
|
|
if( i == 0 && str[i] == '-' )
|
|
|
|
{
|
|
|
|
minus = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
|
|
|
|
str[i - 1] == '0' && str[i] == 'x' )
|
|
|
|
{
|
|
|
|
hex = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-02-01 11:54:28 +01:00
|
|
|
if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
|
|
|
|
( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
|
|
|
|
( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
|
2013-08-16 13:31:10 +02:00
|
|
|
{
|
|
|
|
digits = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( digits )
|
|
|
|
{
|
|
|
|
if( hex )
|
|
|
|
*value = strtol( str, NULL, 16 );
|
|
|
|
else
|
|
|
|
*value = strtol( str, NULL, 10 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
MAPPING_CODE
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "Expected integer for parameter and got: %s\n", str );
|
2013-08-16 13:31:10 +02:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2013-09-15 17:05:21 +02:00
|
|
|
FUNCTION_CODE
|
|
|
|
SUITE_POST_DEP
|
|
|
|
|
2013-08-16 13:31:10 +02:00
|
|
|
int dep_check( char *str )
|
|
|
|
{
|
|
|
|
if( str == NULL )
|
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
DEP_CHECK_CODE
|
|
|
|
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int dispatch_test(int cnt, char *params[50])
|
|
|
|
{
|
|
|
|
int ret;
|
2013-08-20 12:06:33 +02:00
|
|
|
((void) cnt);
|
|
|
|
((void) params);
|
2013-08-16 13:31:10 +02:00
|
|
|
|
2013-08-20 12:06:33 +02:00
|
|
|
#if defined(TEST_SUITE_ACTIVE)
|
2013-08-16 13:31:10 +02:00
|
|
|
DISPATCH_FUNCTION
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
|
2013-08-16 13:31:10 +02:00
|
|
|
fflush( stdout );
|
|
|
|
return( 1 );
|
|
|
|
}
|
2013-08-20 12:06:33 +02:00
|
|
|
#else
|
|
|
|
return( 3 );
|
|
|
|
#endif
|
2013-08-16 13:31:10 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_line( FILE *f, char *buf, size_t len )
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
ret = fgets( buf, len, f );
|
|
|
|
if( ret == NULL )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
|
|
|
|
buf[strlen(buf) - 1] = '\0';
|
|
|
|
if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
|
|
|
|
buf[strlen(buf) - 1] = '\0';
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_arguments( char *buf, size_t len, char *params[50] )
|
|
|
|
{
|
|
|
|
int cnt = 0, i;
|
|
|
|
char *cur = buf;
|
|
|
|
char *p = buf, *q;
|
|
|
|
|
|
|
|
params[cnt++] = cur;
|
|
|
|
|
|
|
|
while( *p != '\0' && p < buf + len )
|
|
|
|
{
|
|
|
|
if( *p == '\\' )
|
|
|
|
{
|
2014-01-22 16:01:17 +01:00
|
|
|
p++;
|
|
|
|
p++;
|
2013-08-16 13:31:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if( *p == ':' )
|
|
|
|
{
|
|
|
|
if( p + 1 < buf + len )
|
|
|
|
{
|
|
|
|
cur = p + 1;
|
|
|
|
params[cnt++] = cur;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
|
2014-01-22 16:01:17 +01:00
|
|
|
p++;
|
2013-08-16 13:31:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace newlines, question marks and colons in strings
|
|
|
|
for( i = 0; i < cnt; i++ )
|
|
|
|
{
|
|
|
|
p = params[i];
|
|
|
|
q = params[i];
|
|
|
|
|
|
|
|
while( *p != '\0' )
|
|
|
|
{
|
|
|
|
if( *p == '\\' && *(p + 1) == 'n' )
|
|
|
|
{
|
|
|
|
p += 2;
|
|
|
|
*(q++) = '\n';
|
|
|
|
}
|
|
|
|
else if( *p == '\\' && *(p + 1) == ':' )
|
|
|
|
{
|
|
|
|
p += 2;
|
|
|
|
*(q++) = ':';
|
|
|
|
}
|
|
|
|
else if( *p == '\\' && *(p + 1) == '?' )
|
|
|
|
{
|
|
|
|
p += 2;
|
|
|
|
*(q++) = '?';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*(q++) = *(p++);
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return( cnt );
|
|
|
|
}
|
|
|
|
|
2015-06-22 10:48:01 +02:00
|
|
|
static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char buf[10] = "xxxxxxxxx";
|
|
|
|
|
|
|
|
ret = mbedtls_snprintf( buf, n, "%s", "123" );
|
|
|
|
if( ret < 0 || (size_t) ret >= n )
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
if( memcmp( ref_buf, buf, sizeof buf ) != 0 ||
|
|
|
|
ref_ret != ret )
|
|
|
|
{
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_test_snprintf( void )
|
|
|
|
{
|
|
|
|
return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
|
|
|
|
test_snprintf( 1, "\0xxxxxxxx", -1 ) != 0 ||
|
|
|
|
test_snprintf( 2, "1\0xxxxxxx", -1 ) != 0 ||
|
|
|
|
test_snprintf( 3, "12\0xxxxxx", -1 ) != 0 ||
|
|
|
|
test_snprintf( 4, "123\0xxxxx", 3 ) != 0 ||
|
|
|
|
test_snprintf( 5, "123\0xxxxx", 3 ) != 0 );
|
|
|
|
}
|
|
|
|
|
2013-08-16 13:31:10 +02:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
|
|
|
|
const char *filename = "TEST_FILENAME";
|
|
|
|
FILE *file;
|
|
|
|
char buf[5000];
|
|
|
|
char *params[50];
|
2015-05-29 11:26:37 +02:00
|
|
|
void *pointer;
|
2013-08-16 13:31:10 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
|
2014-11-27 11:55:27 +01:00
|
|
|
!defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
|
2013-09-29 14:45:34 +02:00
|
|
|
unsigned char alloc_buf[1000000];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
|
2013-08-16 13:31:10 +02:00
|
|
|
#endif
|
|
|
|
|
2015-05-29 11:26:37 +02:00
|
|
|
/*
|
|
|
|
* The C standard doesn't guarantee that all-bits-0 is the representation
|
|
|
|
* of a NULL pointer. We do however use that in our code for initializing
|
|
|
|
* structures, which should work on every modern platform. Let's be sure.
|
|
|
|
*/
|
|
|
|
memset( &pointer, 0, sizeof( void * ) );
|
|
|
|
if( pointer != NULL )
|
|
|
|
{
|
|
|
|
mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" );
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
2015-06-22 10:48:01 +02:00
|
|
|
/*
|
|
|
|
* Make sure we have a snprintf that correctly zero-terminates
|
|
|
|
*/
|
|
|
|
if( run_test_snprintf() != 0 )
|
|
|
|
{
|
|
|
|
mbedtls_fprintf( stderr, "the snprintf implementation is broken\n" );
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2013-08-16 13:31:10 +02:00
|
|
|
file = fopen( filename, "r" );
|
|
|
|
if( file == NULL )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stderr, "Failed to open\n" );
|
2013-08-16 13:31:10 +02:00
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
while( !feof( file ) )
|
|
|
|
{
|
|
|
|
int skip = 0;
|
|
|
|
|
|
|
|
if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
|
|
|
|
break;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
|
|
|
|
mbedtls_fprintf( stdout, " " );
|
2013-08-16 13:31:10 +02:00
|
|
|
for( i = strlen( buf ) + 1; i < 67; i++ )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stdout, "." );
|
|
|
|
mbedtls_fprintf( stdout, " " );
|
2013-08-16 13:31:10 +02:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
total_tests++;
|
|
|
|
|
|
|
|
if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
|
|
|
|
break;
|
|
|
|
cnt = parse_arguments( buf, strlen(buf), params );
|
|
|
|
|
|
|
|
if( strcmp( params[0], "depends_on" ) == 0 )
|
|
|
|
{
|
|
|
|
for( i = 1; i < cnt; i++ )
|
|
|
|
if( dep_check( params[i] ) != 0 )
|
|
|
|
skip = 1;
|
|
|
|
|
|
|
|
if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
|
|
|
|
break;
|
|
|
|
cnt = parse_arguments( buf, strlen(buf), params );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( skip == 0 )
|
|
|
|
{
|
|
|
|
test_errors = 0;
|
|
|
|
ret = dispatch_test( cnt, params );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( skip == 1 || ret == 3 )
|
|
|
|
{
|
|
|
|
total_skipped++;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stdout, "----\n" );
|
2013-08-16 13:31:10 +02:00
|
|
|
fflush( stdout );
|
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( ret == 0 && test_errors == 0 )
|
2013-08-16 13:31:10 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stdout, "PASS\n" );
|
2013-08-16 13:31:10 +02:00
|
|
|
fflush( stdout );
|
|
|
|
}
|
|
|
|
else if( ret == 2 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
|
2013-08-16 13:31:10 +02:00
|
|
|
fclose(file);
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_exit( 2 );
|
2013-08-16 13:31:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
total_errors++;
|
|
|
|
|
|
|
|
if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
|
|
|
|
break;
|
|
|
|
if( strlen(buf) != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
|
2013-08-16 13:31:10 +02:00
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
|
2013-08-16 13:31:10 +02:00
|
|
|
if( total_errors == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stdout, "PASSED" );
|
2013-08-16 13:31:10 +02:00
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stdout, "FAILED" );
|
2013-08-16 13:31:10 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf( stdout, " (%d / %d tests (%d skipped))\n",
|
2013-08-16 13:31:10 +02:00
|
|
|
total_tests - total_errors, total_tests, total_skipped );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
|
2014-11-27 11:55:27 +01:00
|
|
|
!defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
|
|
|
mbedtls_memory_buffer_alloc_status();
|
2013-08-16 13:31:10 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_memory_buffer_alloc_free();
|
2013-09-29 14:45:34 +02:00
|
|
|
#endif
|
2013-08-16 13:31:10 +02:00
|
|
|
|
|
|
|
return( total_errors != 0 );
|
|
|
|
}
|
|
|
|
|