2009-06-28 21:50:27 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
my $suite_dir = shift or die "Missing suite directory";
|
|
|
|
my $suite_name = shift or die "Missing suite name";
|
2011-07-13 14:54:54 +00:00
|
|
|
my $data_name = shift or die "Missing data name";
|
|
|
|
my $test_file = $data_name.".c";
|
2009-06-28 21:50:27 +00:00
|
|
|
my $test_helper_file = $suite_dir."/helpers.function";
|
|
|
|
my $test_case_file = $suite_dir."/".$suite_name.".function";
|
2013-08-16 11:31:10 +00:00
|
|
|
my $test_case_data = $suite_dir."/".$data_name.".data";
|
|
|
|
my $test_main_file = $suite_dir."/main_test.function";
|
2009-06-28 21:50:27 +00:00
|
|
|
|
|
|
|
my $line_separator = $/;
|
|
|
|
undef $/;
|
|
|
|
|
|
|
|
open(TEST_HELPERS, "$test_helper_file") or die "Opening test helpers '$test_helper_file': $!";
|
|
|
|
my $test_helpers = <TEST_HELPERS>;
|
|
|
|
close(TEST_HELPERS);
|
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!";
|
|
|
|
my $test_main = <TEST_MAIN>;
|
|
|
|
close(TEST_MAIN);
|
|
|
|
|
2009-06-28 21:50:27 +00:00
|
|
|
open(TEST_CASES, "$test_case_file") or die "Opening test cases '$test_case_file': $!";
|
|
|
|
my $test_cases = <TEST_CASES>;
|
|
|
|
close(TEST_CASES);
|
2013-08-16 11:31:10 +00:00
|
|
|
|
|
|
|
open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!";
|
|
|
|
my $test_data = <TEST_DATA>;
|
|
|
|
close(TEST_DATA);
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s;
|
|
|
|
my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s;
|
2011-05-26 13:16:06 +00:00
|
|
|
|
|
|
|
my $requirements;
|
|
|
|
if ($suite_defines =~ /^depends_on:/)
|
|
|
|
{
|
|
|
|
( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/;
|
|
|
|
}
|
2013-08-16 11:31:10 +00:00
|
|
|
|
2011-05-26 13:16:06 +00:00
|
|
|
my @var_req_arr = split(/:/, $requirements);
|
|
|
|
my $suite_pre_code;
|
|
|
|
my $suite_post_code;
|
2013-08-16 11:31:10 +00:00
|
|
|
my $dispatch_code;
|
|
|
|
my $mapping_code;
|
|
|
|
my %mapping_values;
|
2011-05-26 13:16:06 +00:00
|
|
|
|
|
|
|
while (@var_req_arr)
|
|
|
|
{
|
|
|
|
my $req = shift @var_req_arr;
|
|
|
|
|
|
|
|
$suite_pre_code .= "#ifdef $req\n";
|
|
|
|
$suite_post_code .= "#endif /* $req */\n";
|
|
|
|
}
|
2009-06-28 21:50:27 +00:00
|
|
|
|
|
|
|
$/ = $line_separator;
|
|
|
|
|
|
|
|
open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
|
|
|
|
print TEST_FILE << "END";
|
2013-06-24 17:17:50 +00:00
|
|
|
#include <polarssl/config.h>
|
2011-05-26 13:16:06 +00:00
|
|
|
|
2013-09-15 15:05:21 +00:00
|
|
|
$suite_pre_code
|
2009-06-28 21:50:27 +00:00
|
|
|
$suite_header
|
2013-09-15 15:05:21 +00:00
|
|
|
$suite_post_code
|
2009-06-28 21:50:27 +00:00
|
|
|
|
|
|
|
$test_helpers
|
|
|
|
|
|
|
|
END
|
|
|
|
|
2013-08-20 10:06:33 +00:00
|
|
|
$test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/;
|
|
|
|
$test_main =~ s/SUITE_POST_DEP/$suite_post_code/;
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg)
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
2013-08-16 11:31:10 +00:00
|
|
|
my $function_deps = $1;
|
2013-08-20 09:48:36 +00:00
|
|
|
my $function_decl = $2;
|
|
|
|
|
|
|
|
# Sanity checks of function
|
|
|
|
if ($function_decl !~ /^void /)
|
|
|
|
{
|
|
|
|
die "Test function does not have 'void' as return type\n";
|
|
|
|
}
|
|
|
|
if ($function_decl !~ /^void (\w+)\(\s*(.*?)\s*\)\s*{(.*?)}/ms)
|
|
|
|
{
|
|
|
|
die "Function declaration not in expected format\n";
|
|
|
|
}
|
|
|
|
my $function_name = $1;
|
|
|
|
my $function_params = $2;
|
2013-08-16 11:31:10 +00:00
|
|
|
my $function_pre_code;
|
|
|
|
my $function_post_code;
|
|
|
|
my $param_defs;
|
|
|
|
my $param_checks;
|
|
|
|
my @dispatch_params;
|
2013-08-20 09:48:36 +00:00
|
|
|
my @var_def_arr = split(/,\s*/, $function_params);
|
2013-08-16 11:31:10 +00:00
|
|
|
my $i = 1;
|
|
|
|
my $mapping_regex = "".$function_name;
|
|
|
|
my $mapping_count = 0;
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
$function_decl =~ s/^void /void test_suite_/;
|
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
if ($function_deps =~ /^depends_on:/)
|
2009-10-03 19:57:10 +00:00
|
|
|
{
|
2013-08-16 11:31:10 +00:00
|
|
|
( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/;
|
|
|
|
}
|
2009-10-03 19:57:10 +00:00
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
foreach my $req (split(/:/, $function_deps))
|
|
|
|
{
|
|
|
|
$function_pre_code .= "#ifdef $req\n";
|
|
|
|
$function_post_code .= "#endif /* $req */\n";
|
2009-10-03 19:57:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
foreach my $def (@var_def_arr)
|
2009-10-03 19:57:10 +00:00
|
|
|
{
|
2013-08-16 11:31:10 +00:00
|
|
|
# Handle the different parameter types
|
2013-08-20 09:48:36 +00:00
|
|
|
if( substr($def, 0, 4) eq "int " )
|
2013-08-16 11:31:10 +00:00
|
|
|
{
|
|
|
|
$param_defs .= " int param$i;\n";
|
|
|
|
$param_checks .= " if( verify_int( params[$i], ¶m$i ) != 0 ) return( 2 );\n";
|
|
|
|
push @dispatch_params, "param$i";
|
|
|
|
|
|
|
|
$mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)";
|
|
|
|
$mapping_count++;
|
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
elsif( substr($def, 0, 6) eq "char *" )
|
2013-08-16 11:31:10 +00:00
|
|
|
{
|
|
|
|
$param_defs .= " char *param$i = params[$i];\n";
|
|
|
|
$param_checks .= " if( verify_string( ¶m$i ) != 0 ) return( 2 );\n";
|
|
|
|
push @dispatch_params, "param$i";
|
|
|
|
$mapping_regex .= ":[^:]+";
|
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
die "Parameter declaration not of supported type (int, char *)\n";
|
|
|
|
}
|
2013-08-16 11:31:10 +00:00
|
|
|
$i++;
|
|
|
|
|
|
|
|
}
|
2009-10-03 19:57:10 +00:00
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
# Find non-integer values we should map for this function
|
|
|
|
if( $mapping_count)
|
|
|
|
{
|
|
|
|
my @res = $test_data =~ /^$mapping_regex/msg;
|
|
|
|
foreach my $value (@res)
|
|
|
|
{
|
|
|
|
$mapping_values{$value} = 1 if ($value !~ /^\d+$/);
|
|
|
|
}
|
2009-10-03 19:57:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
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 );
|
|
|
|
}
|
2009-10-03 19:57:10 +00:00
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
$param_checks
|
2013-08-20 09:48:36 +00:00
|
|
|
test_suite_$function_name( $call_params );
|
|
|
|
return ( 0 );
|
2013-08-16 11:31:10 +00:00
|
|
|
$function_post_code
|
|
|
|
return ( 3 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
END
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
my $function_code = $function_pre_code . $function_decl . "\n" . $function_post_code;
|
|
|
|
$test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/;
|
2013-08-16 11:31:10 +00:00
|
|
|
}
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
# Find specific case dependencies that we should be able to check
|
|
|
|
# and make check code
|
|
|
|
my $dep_check_code;
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
my @res = $test_data =~ /^depends_on:([\w:]+)/msg;
|
|
|
|
my %case_deps;
|
|
|
|
foreach my $deps (@res)
|
|
|
|
{
|
|
|
|
foreach my $dep (split(/:/, $deps))
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
2013-08-16 11:31:10 +00:00
|
|
|
$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
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
|
|
|
END
|
|
|
|
}
|
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
# Make mapping code
|
|
|
|
while( my ($key, $value) = each(%mapping_values) )
|
|
|
|
{
|
|
|
|
$mapping_code .= << "END";
|
|
|
|
if( strcmp( str, "$key" ) == 0 )
|
|
|
|
{
|
|
|
|
*value = ( $key );
|
|
|
|
return( 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-16 11:31:10 +00:00
|
|
|
END
|
|
|
|
}
|
2011-05-26 13:16:06 +00:00
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
$dispatch_code =~ s/^(.+)/ $1/mg;
|
|
|
|
|
|
|
|
$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/;
|
2013-07-03 12:00:49 +00:00
|
|
|
|
2013-08-16 11:31:10 +00:00
|
|
|
print TEST_FILE << "END";
|
|
|
|
$test_main
|
2009-06-28 21:50:27 +00:00
|
|
|
END
|
|
|
|
|
|
|
|
close(TEST_FILE);
|