Skip to content

Instantly share code, notes, and snippets.

@lzq420241
Created October 10, 2020 12:01
Show Gist options
  • Save lzq420241/63ad7d7465c8452ba0c480d168c99748 to your computer and use it in GitHub Desktop.
Save lzq420241/63ad7d7465c8452ba0c480d168c99748 to your computer and use it in GitHub Desktop.
mbedtls_tests transplant to YoC
diff --git a/tests/Makefile b/tests/Makefile
index 511db9db5..0b99fd4fd 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -55,7 +55,7 @@ DLEXT ?= so
EXEXT=
SHARED_SUFFIX=
# python2 for POSIX since FreeBSD has only python2 as default.
-PYTHON ?= python2
+PYTHON ?= python
endif
# Zlib shared library extensions:
@@ -121,9 +121,9 @@ C_FILES := $(addsuffix .c,$(APPS))
-o .
-$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(MBEDTLS_TEST_OBJS)
+$(BINA.RIES): %$(EXEXT): %.c $(MBEDLIBS) $(MBEDTLS_TEST_OBJS)
echo " CC $<"
- $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
+# $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
# Some test suites require additional header files.
$(filter test_suite_psa_crypto%, $(BINARIES)): include/test/psa_crypto_helpers.h
diff --git a/tests/scripts/generate_test_code.py b/tests/scripts/generate_test_code.py
index 7382fb6ec..b2ff93b47 100755
--- a/tests/scripts/generate_test_code.py
+++ b/tests/scripts/generate_test_code.py
@@ -317,7 +317,7 @@ def gen_function_wrapper(name, local_vars, args_dispatch):
"""
# Then create the wrapper
wrapper = '''
-void {name}_wrapper( void ** params )
+static void {name}_wrapper( void ** params )
{{
{unused_params}{locals}
{name}( {args} );
@@ -941,6 +941,8 @@ def add_input_info(funcs_file, data_file, template_file,
:return:
"""
snippets['test_file'] = c_file
+ snippets['main_name'] = c_file.strip("./")[:-2].replace(".","_")
+ snippets['datax_file'] = '"/lfs%sdatax"' % c_file.strip(".").strip("c")
snippets['test_main_file'] = template_file
snippets['test_case_file'] = funcs_file
snippets['test_case_data_file'] = data_file
diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function
index ec43d1349..a44012f10 100644
--- a/tests/suites/helpers.function
+++ b/tests/suites/helpers.function
@@ -7,6 +7,7 @@
#include <test/random.h>
#include <stdlib.h>
+#include <vfs.h>
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
@@ -400,12 +401,12 @@ jmp_buf jmp_tmp;
*
* \param step The step number to report.
*/
-void test_set_step( unsigned long step )
+static void test_set_step( unsigned long step )
{
test_info.step = step;
}
-void test_fail( const char *test, int line_no, const char* filename )
+static void test_fail( const char *test, int line_no, const char* filename )
{
if( test_info.result == TEST_RESULT_FAILED )
{
@@ -419,7 +420,7 @@ void test_fail( const char *test, int line_no, const char* filename )
test_info.filename = filename;
}
-void test_skip( const char *test, int line_no, const char* filename )
+static void test_skip( const char *test, int line_no, const char* filename )
{
test_info.result = TEST_RESULT_SKIPPED;
test_info.test = test;
diff --git a/tests/suites/host_test.function b/tests/suites/host_test.function
index cce2899f4..9fb172817 100644
--- a/tests/suites/host_test.function
+++ b/tests/suites/host_test.function
@@ -8,7 +8,7 @@
*
* \return 0 if success else 1
*/
-int verify_string( char **str )
+static int verify_string( char **str )
{
if( ( *str )[0] != '"' ||
( *str )[strlen( *str ) - 1] != '"' )
@@ -33,7 +33,7 @@ int verify_string( char **str )
*
* \return 0 if success else 1
*/
-int verify_int( char *str, int *value )
+static int verify_int( char *str, int *value )
{
size_t i;
int minus = 0;
@@ -97,6 +97,23 @@ int verify_int( char *str, int *value )
argv[0], \
"TESTCASE_FILENAME"
+static char *my_fgets(int fd, char *s, int n)
+{
+ int c;
+ char *cs;
+ cs = s;
+
+ while(--n > 0 && (c = aos_read(fd, s, 1)) > 0)
+ {
+ if((*cs++ = *s++) == '\n')
+ {
+ break;
+ }
+ }
+
+ *cs = '\0';
+ return (c == 0 && cs == s) ? NULL : s ;
+}
/**
* \brief Read a line from the passed file pointer.
@@ -107,7 +124,7 @@ int verify_int( char *str, int *value )
*
* \return 0 if success else -1
*/
-int get_line( FILE *f, char *buf, size_t len )
+static int get_line( int fd, char *buf, size_t len )
{
char *ret;
int i = 0, str_len = 0, has_string = 0;
@@ -115,7 +132,7 @@ int get_line( FILE *f, char *buf, size_t len )
/* Read until we get a valid line */
do
{
- ret = fgets( buf, len, f );
+ ret = my_fgets( fd, buf, len );
if( ret == NULL )
return( -1 );
@@ -502,10 +519,10 @@ static void write_outcome_result( FILE *outcome_file,
*
* \return Program exit status.
*/
-int execute_tests( int argc , const char ** argv )
+static int execute_tests( int argc , const char ** argv )
{
/* Local Configurations and options */
- const char *default_filename = "DATA_FILE";
+ const char *default_filename = "/lfs/DATA_FILE";
const char *test_filename = NULL;
const char **test_files = NULL;
size_t testfile_count = 0;
@@ -518,7 +535,7 @@ int execute_tests( int argc , const char ** argv )
size_t testfile_index, i, cnt;
int ret;
unsigned total_errors = 0, total_tests = 0, total_skipped = 0;
- FILE *file;
+ int file_fd;
char buf[5000];
char *params[50];
/* Store for proccessed integer params. */
@@ -614,8 +631,8 @@ int execute_tests( int argc , const char ** argv )
test_filename = test_files[ testfile_index ];
- file = fopen( test_filename, "r" );
- if( file == NULL )
+ file_fd = aos_open( test_filename, O_RDONLY );
+ if( file_fd < 0 )
{
mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
test_filename );
@@ -624,7 +641,7 @@ int execute_tests( int argc , const char ** argv )
return( 1 );
}
- while( !feof( file ) )
+ do
{
if( unmet_dep_count > 0 )
{
@@ -635,7 +652,7 @@ int execute_tests( int argc , const char ** argv )
unmet_dep_count = 0;
missing_unmet_dependencies = 0;
- if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
+ if( ( ret = get_line( file_fd, buf, sizeof(buf) ) ) != 0 )
break;
mbedtls_fprintf( stdout, "%s%.66s",
test_info.result == TEST_RESULT_FAILED ? "\n" : "", buf );
@@ -648,7 +665,7 @@ int execute_tests( int argc , const char ** argv )
total_tests++;
- if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
+ if( ( ret = get_line( file_fd, buf, sizeof( buf ) ) ) != 0 )
break;
cnt = parse_arguments( buf, strlen( buf ), params,
sizeof( params ) / sizeof( params[0] ) );
@@ -673,7 +690,7 @@ int execute_tests( int argc , const char ** argv )
}
}
- if( ( ret = get_line( file, buf, sizeof( buf ) ) ) != 0 )
+ if( ( ret = get_line( file_fd, buf, sizeof( buf ) ) ) != 0 )
break;
cnt = parse_arguments( buf, strlen( buf ), params,
sizeof( params ) / sizeof( params[0] ) );
@@ -782,19 +799,19 @@ int execute_tests( int argc , const char ** argv )
else if( ret == DISPATCH_INVALID_TEST_DATA )
{
mbedtls_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
- fclose( file );
+ aos_close( file_fd );
mbedtls_exit( 2 );
}
else if( ret == DISPATCH_TEST_FN_NOT_FOUND )
{
mbedtls_fprintf( stderr, "FAILED: FATAL TEST FUNCTION NOT FOUND\n" );
- fclose( file );
+ aos_close( file_fd );
mbedtls_exit( 2 );
}
else
total_errors++;
- }
- fclose( file );
+ } while (1);
+ aos_close( file_fd );
}
if( outcome_file != NULL )
diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function
index 6901256f8..f07f69092 100644
--- a/tests/suites/main_test.function
+++ b/tests/suites/main_test.function
@@ -77,7 +77,7 @@ $functions_code
*
* \return 0 if exp_id is found. 1 otherwise.
*/
-int get_expression( int32_t exp_id, int32_t * out_value )
+static int get_expression( int32_t exp_id, int32_t * out_value )
{
int ret = KEY_VALUE_MAPPING_FOUND;
@@ -144,7 +144,7 @@ typedef void (*TestWrapper_t)( void ** );
* $generator_script
*
*/
-TestWrapper_t test_funcs[] =
+static TestWrapper_t test_funcs[] =
{
$dispatch_code
#line $line_no "suites/main_test.function"
@@ -162,7 +162,7 @@ $dispatch_code
* \param params Parameters to pass
*
*/
-void execute_function_ptr(TestWrapper_t fp, void **params)
+static void execute_function_ptr(TestWrapper_t fp, void **params)
{
#if defined(MBEDTLS_CHECK_PARAMS)
if ( setjmp( param_fail_jmp ) == 0 )
@@ -190,7 +190,7 @@ void execute_function_ptr(TestWrapper_t fp, void **params)
* DISPATCH_TEST_FN_NOT_FOUND if not found
* DISPATCH_UNSUPPORTED_SUITE if not compile time enabled.
*/
-int dispatch_test( size_t func_idx, void ** params )
+static int dispatch_test( size_t func_idx, void ** params )
{
int ret = DISPATCH_TEST_SUCCESS;
TestWrapper_t fp = NULL;
@@ -221,7 +221,7 @@ int dispatch_test( size_t func_idx, void ** params )
* DISPATCH_TEST_FN_NOT_FOUND if not found
* DISPATCH_UNSUPPORTED_SUITE if not compile time enabled.
*/
-int check_test( size_t func_idx )
+static int check_test( size_t func_idx )
{
int ret = DISPATCH_TEST_SUCCESS;
TestWrapper_t fp = NULL;
@@ -257,7 +257,7 @@ $platform_code
*
* \return Exit code.
*/
-int main( int argc, const char *argv[] )
+int $main_name( int argc, const char *argv[] )
{
int ret = mbedtls_test_platform_setup();
if( ret != 0 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment