Commit 898edb77 authored by Paul Bakker's avatar Paul Bakker
Browse files

Merged the revamped test framework into development

parents 3a074a79 68a4fce8
......@@ -20,9 +20,9 @@ function(add_test_suite suite_name)
add_custom_command(
OUTPUT test_suite_${data_name}.c
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_code.pl ${CMAKE_CURRENT_SOURCE_DIR}/suites test_suite_${suite_name} test_suite_${data_name}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_code.pl polarssl fct.h suites/helpers.function suites/test_suite_${suite_name}.function suites/test_suite_${data_name}.data
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_code.pl polarssl suites/helpers.function suites/main_test.function suites/test_suite_${suite_name}.function suites/test_suite_${data_name}.data
)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_executable(test_suite_${data_name} test_suite_${data_name}.c)
target_link_libraries(test_suite_${data_name} ${libs})
......
......@@ -54,71 +54,71 @@ APPS = test_suite_aes.ecb test_suite_aes.cbc \
all: $(APPS)
test_suite_aes.ecb.c : suites/test_suite_aes.function suites/test_suite_aes.ecb.data scripts/generate_code.pl suites/helpers.function
test_suite_aes.ecb.c : suites/test_suite_aes.function suites/test_suite_aes.ecb.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_aes test_suite_aes.ecb
test_suite_aes.cbc.c : suites/test_suite_aes.function suites/test_suite_aes.cbc.data scripts/generate_code.pl suites/helpers.function
test_suite_aes.cbc.c : suites/test_suite_aes.function suites/test_suite_aes.cbc.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_aes test_suite_aes.cbc
test_suite_aes.cfb.c : suites/test_suite_aes.function suites/test_suite_aes.cfb.data scripts/generate_code.pl suites/helpers.function
test_suite_aes.cfb.c : suites/test_suite_aes.function suites/test_suite_aes.cfb.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_aes test_suite_aes.cfb
test_suite_aes.rest.c : suites/test_suite_aes.function suites/test_suite_aes.rest.data scripts/generate_code.pl suites/helpers.function
test_suite_aes.rest.c : suites/test_suite_aes.function suites/test_suite_aes.rest.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_aes test_suite_aes.rest
test_suite_cipher.aes.c : suites/test_suite_cipher.function suites/test_suite_cipher.aes.data scripts/generate_code.pl suites/helpers.function
test_suite_cipher.aes.c : suites/test_suite_cipher.function suites/test_suite_cipher.aes.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_cipher test_suite_cipher.aes
test_suite_cipher.blowfish.c : suites/test_suite_cipher.function suites/test_suite_cipher.blowfish.data scripts/generate_code.pl suites/helpers.function
test_suite_cipher.blowfish.c : suites/test_suite_cipher.function suites/test_suite_cipher.blowfish.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_cipher test_suite_cipher.blowfish
test_suite_cipher.camellia.c : suites/test_suite_cipher.function suites/test_suite_cipher.camellia.data scripts/generate_code.pl suites/helpers.function
test_suite_cipher.camellia.c : suites/test_suite_cipher.function suites/test_suite_cipher.camellia.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_cipher test_suite_cipher.camellia
test_suite_cipher.des.c : suites/test_suite_cipher.function suites/test_suite_cipher.des.data scripts/generate_code.pl suites/helpers.function
test_suite_cipher.des.c : suites/test_suite_cipher.function suites/test_suite_cipher.des.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_cipher test_suite_cipher.des
test_suite_cipher.null.c : suites/test_suite_cipher.function suites/test_suite_cipher.null.data scripts/generate_code.pl suites/helpers.function
test_suite_cipher.null.c : suites/test_suite_cipher.function suites/test_suite_cipher.null.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_cipher test_suite_cipher.null
test_suite_cipher.padding.c : suites/test_suite_cipher.function suites/test_suite_cipher.padding.data scripts/generate_code.pl suites/helpers.function
test_suite_cipher.padding.c : suites/test_suite_cipher.function suites/test_suite_cipher.padding.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_cipher test_suite_cipher.padding
test_suite_gcm.decrypt_128.c : suites/test_suite_gcm.function suites/test_suite_gcm.decrypt_128.data scripts/generate_code.pl suites/helpers.function
test_suite_gcm.decrypt_128.c : suites/test_suite_gcm.function suites/test_suite_gcm.decrypt_128.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_gcm test_suite_gcm.decrypt_128
test_suite_gcm.decrypt_192.c : suites/test_suite_gcm.function suites/test_suite_gcm.decrypt_192.data scripts/generate_code.pl suites/helpers.function
test_suite_gcm.decrypt_192.c : suites/test_suite_gcm.function suites/test_suite_gcm.decrypt_192.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_gcm test_suite_gcm.decrypt_192
test_suite_gcm.decrypt_256.c : suites/test_suite_gcm.function suites/test_suite_gcm.decrypt_256.data scripts/generate_code.pl suites/helpers.function
test_suite_gcm.decrypt_256.c : suites/test_suite_gcm.function suites/test_suite_gcm.decrypt_256.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_gcm test_suite_gcm.decrypt_256
test_suite_gcm.encrypt_128.c : suites/test_suite_gcm.function suites/test_suite_gcm.encrypt_128.data scripts/generate_code.pl suites/helpers.function
test_suite_gcm.encrypt_128.c : suites/test_suite_gcm.function suites/test_suite_gcm.encrypt_128.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_gcm test_suite_gcm.encrypt_128
test_suite_gcm.encrypt_192.c : suites/test_suite_gcm.function suites/test_suite_gcm.encrypt_192.data scripts/generate_code.pl suites/helpers.function
test_suite_gcm.encrypt_192.c : suites/test_suite_gcm.function suites/test_suite_gcm.encrypt_192.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_gcm test_suite_gcm.encrypt_192
test_suite_gcm.encrypt_256.c : suites/test_suite_gcm.function suites/test_suite_gcm.encrypt_256.data scripts/generate_code.pl suites/helpers.function
test_suite_gcm.encrypt_256.c : suites/test_suite_gcm.function suites/test_suite_gcm.encrypt_256.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_gcm test_suite_gcm.encrypt_256
%.c : suites/%.function suites/%.data scripts/generate_code.pl suites/helpers.function
%.c : suites/%.function suites/%.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites $* $*
......
This diff is collapsed.
......@@ -9,9 +9,8 @@ my $data_name = shift or die "Missing data name";
my $test_file = $data_name.".c";
my $test_helper_file = $suite_dir."/helpers.function";
my $test_case_file = $suite_dir."/".$suite_name.".function";
my $test_data_file = $suite_dir."/".$data_name.".data";
open(TEST_DATA, "$test_data_file") or die "Opening test cases '$test_data_file': $!";
my $test_case_data = $suite_dir."/".$data_name.".data";
my $test_main_file = $suite_dir."/main_test.function";
my $line_separator = $/;
undef $/;
......@@ -20,21 +19,33 @@ open(TEST_HELPERS, "$test_helper_file") or die "Opening test helpers '$test_help
my $test_helpers = <TEST_HELPERS>;
close(TEST_HELPERS);
open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!";
my $test_main = <TEST_MAIN>;
close(TEST_MAIN);
open(TEST_CASES, "$test_case_file") or die "Opening test cases '$test_case_file': $!";
my $test_cases = <TEST_CASES>;
close(TEST_CASES);
my ( $suite_header ) = $test_cases =~ /BEGIN_HEADER\n(.*?)\nEND_HEADER/s;
my ( $suite_defines ) = $test_cases =~ /BEGIN_DEPENDENCIES\n(.*?)\nEND_DEPENDENCIES/s;
open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!";
my $test_data = <TEST_DATA>;
close(TEST_DATA);
my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s;
my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s;
my $requirements;
if ($suite_defines =~ /^depends_on:/)
{
( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/;
}
my @var_req_arr = split(/:/, $requirements);
my $suite_pre_code;
my $suite_post_code;
my $dispatch_code;
my $mapping_code;
my %mapping_values;
while (@var_req_arr)
{
......@@ -48,114 +59,168 @@ $/ = $line_separator;
open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
print TEST_FILE << "END";
#include "fct.h"
#include <polarssl/config.h>
$suite_header
$test_helpers
FCT_BGN()
{
$suite_pre_code
#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
unsigned char buf[1000000];
memory_buffer_alloc_init( buf, sizeof(buf) );
#endif
FCT_SUITE_BGN($suite_name)
{
END
while (my $line = <TEST_DATA>)
{
my $description = $line;
$line = <TEST_DATA>;
$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
my $test_name = $description;
$test_name =~ tr/A-Z \-/a-z__/;
$test_name =~ tr/a-z0-9_//cd;
while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
{
my $function_deps = $1;
my $function_decl = $2;
# Carve the defines required for this test case
my $requirements;
if ($line =~ /^depends_on:/)
# Sanity checks of function
if ($function_decl !~ /^void /)
{
my $depends_on_line = $line;
$line = <TEST_DATA>;
( $requirements ) = $depends_on_line =~ /^depends_on:(.*)$/;
die "Test function does not have 'void' as return type\n";
}
my @var_req_arr = split(/:/, $requirements);
my $pre_code;
my $post_code;
while (@var_req_arr)
if ($function_decl !~ /^void (\w+)\(\s*(.*?)\s*\)\s*{(.*?)}/ms)
{
my $req = shift @var_req_arr;
$pre_code .= "#ifdef $req\n";
$post_code .= "#endif /* $req */\n";
die "Function declaration not in expected format\n";
}
my $function_name = $1;
my $function_params = $2;
my $function_pre_code;
my $function_post_code;
my $param_defs;
my $param_checks;
my @dispatch_params;
my @var_def_arr = split(/,\s*/, $function_params);
my $i = 1;
my $mapping_regex = "".$function_name;
my $mapping_count = 0;
$function_decl =~ s/^void /void test_suite_/;
if ($function_deps =~ /^depends_on:/)
{
( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
}
my $command_line = $line;
$line = <TEST_DATA>;
# Carve the case name and variable values
#
my ( $case, $var_value ) = $command_line =~ /^([\w_]+):(.*)$/;
# Escape the escaped colons (Not really escaped now)
#
$var_value =~ s/\\:/{colon_sign}/g;
foreach my $req (split(/:/, $function_deps))
{
$function_pre_code .= "#ifdef $req\n";
$function_post_code .= "#endif /* $req */\n";
}
# Carve the case and variable definition
#
my ( $var_def, $case_code ) = $test_cases =~ /BEGIN_CASE\n$case:([^\n]*)\n(.*?)\nEND_CASE/s;
foreach my $def (@var_def_arr)
{
# Handle the different parameter types
if( substr($def, 0, 4) eq "int " )
{
$param_defs .= " int param$i;\n";
$param_checks .= " if( verify_int( params[$i], &param$i ) != 0 ) return( 2 );\n";
push @dispatch_params, "param$i";
$mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
$mapping_count++;
}
elsif( substr($def, 0, 6) eq "char *" )
{
$param_defs .= " char *param$i = params[$i];\n";
$param_checks .= " if( verify_string( &param$i ) != 0 ) return( 2 );\n";
push @dispatch_params, "param$i";
$mapping_regex .= ":[^:]+";
}
else
{
die "Parameter declaration not of supported type (int, char *)\n";
}
$i++;
my @var_def_arr = split(/:/, $var_def);
my @var_value_arr = split(/:/, $var_value);
}
while (@var_def_arr)
# Find non-integer values we should map for this function
if( $mapping_count)
{
my $def = shift @var_def_arr;
my $val = shift @var_value_arr;
my @res = $test_data =~ /^$mapping_regex/msg;
foreach my $value (@res)
{
$mapping_values{$value} = 1 if ($value !~ /^\d+$/);
}
}
$case_code =~ s/\{$def\}/$val/g;
my $call_params = join ", ", @dispatch_params;
my $param_count = @var_def_arr + 1;
$dispatch_code .= << "END";
if( strcmp( params[0], "$function_name" ) == 0 )
{
$function_pre_code
$param_defs
if( cnt != $param_count )
{
fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count );
return( 2 );
}
$case_code = "int ${test_name}_code_present = 0;\nTEST_ASSERT( ${test_name}_code_present == 1 );" if ($case_code =~ /^\s*$/);
$case_code =~ s/{colon_sign}/:/g;
$case_code =~ s/TEST_ASSERT/fct_chk/g;
$case_code =~ s/TEST_EQUALS/fct_chk/g;
$param_checks
test_suite_$function_name( $call_params );
return ( 0 );
$function_post_code
return ( 3 );
}
else
END
$case_code =~ s/^/ /gm;
my $function_code = $function_pre_code . $function_decl . "\n" . $function_post_code;
$test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
}
# Find specific case dependencies that we should be able to check
# and make check code
my $dep_check_code;
print TEST_FILE << "END";
$pre_code
FCT_TEST_BGN($test_name)
$case_code
FCT_TEST_END();
$post_code
my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
my %case_deps;
foreach my $deps (@res)
{
foreach my $dep (split(/:/, $deps))
{
$case_deps{$dep} = 1;
}
}
while( my ($key, $value) = each(%case_deps) )
{
$dep_check_code .= << "END";
if( strcmp( str, "$key" ) == 0 )
{
#if defined($key)
return( 0 );
#else
return( 1 );
#endif
}
END
}
print TEST_FILE << "END";
# Make mapping code
while( my ($key, $value) = each(%mapping_values) )
{
$mapping_code .= << "END";
if( strcmp( str, "$key" ) == 0 )
{
*value = ( $key );
return( 0 );
}
FCT_SUITE_END();
END
}
#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
memory_buffer_alloc_status();
#endif
$dispatch_code =~ s/^(.+)/ $1/mg;
$suite_post_code
}
FCT_END();
$test_main =~ s/TEST_FILENAME/$test_case_data/;
$test_main =~ s/FUNCTION_CODE//;
$test_main =~ s/DEP_CHECK_CODE/$dep_check_code/;
$test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/;
$test_main =~ s/MAPPING_CODE/$mapping_code/;
print TEST_FILE << "END";
$test_main
END
close(TEST_DATA);
close(TEST_FILE);
......@@ -9,6 +9,10 @@ typedef UINT32 uint32_t;
#include <inttypes.h>
#endif
#include <assert.h>
#include <stdlib.h>
#include <string.h>
/*
* 32-bit integer manipulation macros (big endian)
*/
......
#include <stdio.h>
#include <string.h>
static int test_errors = 0;
static int test_assert( int correct, char *test )
{
if( correct )
return( 0 );
test_errors++;
if( test_errors == 1 )
printf( "FAILED\n" );
printf( " %s\n", test );
return( 1 );
}
#define TEST_ASSERT( TEST ) \
do { test_assert( (TEST) ? 1 : 0, #TEST ); \
if( test_errors) return; \
} while (0)
int verify_string( char **str )
{
if( (*str)[0] != '"' ||
(*str)[strlen( *str ) - 1] != '"' )
{
printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
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;
}
if( str[i] < '0' || str[i] > '9' )
{
digits = 0;
break;
}
}
if( digits )
{
if( hex )
*value = strtol( str, NULL, 16 );
else
*value = strtol( str, NULL, 10 );
return( 0 );
}
MAPPING_CODE
printf( "Expected integer for parameter and got: %s\n", str );
return( -1 );
}
int dep_check( char *str )
{
if( str == NULL )
return( 1 );
DEP_CHECK_CODE
return( 1 );
}
SUITE_PRE_DEP
#define TEST_SUITE_ACTIVE
FUNCTION_CODE
SUITE_POST_DEP
int dispatch_test(int cnt, char *params[50])
{
int ret;
((void) cnt);
((void) params);
#if defined(TEST_SUITE_ACTIVE)
DISPATCH_FUNCTION
{
fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
fflush( stdout );
return( 1 );
}
#else
return( 3 );
#endif
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 == '\\' )
{
*p++;
*p++;
continue;
}
if( *p == ':' )
{
if( p + 1 < buf + len )
{
cur = p + 1;
params[cnt++] = cur;
}
*p = '\0';
}
*p++;
}
// 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 );
}
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];
#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)