tests: Add helpers.c and helpers.h files

The purpose of helpers.c file is to contain the helper
functions that have been in helpers.function so far and
that are not related to the mechanism of unit test
execution and not related to random number generation
(will be moved in a dedicated file).

The purpose of helpers.h is to contain the interface
exposed by helpers.c thus helper function prototypes.

Make the changes in the build systems (make and cmake)
to build helpers.c and link it to test executables
along with mbedtls library.

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
This commit is contained in:
Ronald Cron 2020-06-03 10:11:18 +02:00
parent 849930a50e
commit b6d6d4c61a
6 changed files with 79 additions and 7 deletions

3
tests/.gitignore vendored
View file

@ -9,3 +9,6 @@ data_files/ctr_drbg_seed
data_files/entropy_seed data_files/entropy_seed
include/test/instrument_record_status.h include/test/instrument_record_status.h
src/*.o
src/libmbed*

View file

@ -46,9 +46,9 @@ function(add_test_suite suite_name)
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py mbedtls ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py mbedtls ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data
) )
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) add_executable(test_suite_${data_name} test_suite_${data_name}.c $<TARGET_OBJECTS:mbedtests>)
add_executable(test_suite_${data_name} test_suite_${data_name}.c)
target_link_libraries(test_suite_${data_name} ${libs}) target_link_libraries(test_suite_${data_name} ${libs})
target_include_directories(test_suite_${data_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX}) if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
message(STATUS "The test suite ${data_name} will not be executed.") message(STATUS "The test suite ${data_name} will not be executed.")
else() else()
@ -66,6 +66,10 @@ if(MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-")
endif(MSVC) endif(MSVC)
file(GLOB MBEDTESTS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c)
add_library(mbedtests OBJECT ${MBEDTESTS_FILES})
target_include_directories(mbedtests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
add_test_suite(aes aes.cbc) add_test_suite(aes aes.cbc)
add_test_suite(aes aes.cfb) add_test_suite(aes aes.cfb)
add_test_suite(aes aes.ecb) add_test_suite(aes aes.ecb)

View file

@ -21,9 +21,9 @@ LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L
ifndef SHARED ifndef SHARED
DEP=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a MBEDLIBS=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
else else
DEP=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT) MBEDLIBS=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
endif endif
ifdef DEBUG ifdef DEBUG
@ -74,9 +74,16 @@ BINARIES := $(addsuffix $(EXEXT),$(APPS))
all: $(BINARIES) all: $(BINARIES)
$(DEP): $(MBEDLIBS):
$(MAKE) -C ../library $(MAKE) -C ../library
MBEDTESTS_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c))
# Rule to compile common test C files in src folder
src/%.o : src/%.c
echo " CC $<"
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
C_FILES := $(addsuffix .c,$(APPS)) C_FILES := $(addsuffix .c,$(APPS))
# Wildcard target for test code generation: # Wildcard target for test code generation:
@ -105,9 +112,9 @@ C_FILES := $(addsuffix .c,$(APPS))
-o . -o .
$(BINARIES): %$(EXEXT): %.c $(DEP) $(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(MBEDTESTS_OBJS)
echo " CC $<" echo " CC $<"
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTESTS_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
# Some test suites require additional header files. # Some test suites require additional header files.
$(filter test_suite_psa_crypto%, $(BINARIES)): include/test/psa_crypto_helpers.h $(filter test_suite_psa_crypto%, $(BINARIES)): include/test/psa_crypto_helpers.h
@ -118,10 +125,13 @@ $(addprefix embedded_,$(filter test_suite_psa_%, $(APPS))): embedded_%: TESTS/mb
clean: clean:
ifndef WINDOWS ifndef WINDOWS
rm -rf $(BINARIES) *.c *.datax TESTS rm -rf $(BINARIES) *.c *.datax TESTS
rm -f src/*.o src/libmbed*
else else
if exist *.c del /Q /F *.c if exist *.c del /Q /F *.c
if exist *.exe del /Q /F *.exe if exist *.exe del /Q /F *.exe
if exist *.datax del /Q /F *.datax if exist *.datax del /Q /F *.datax
if exist src/*.o del /Q /F src/*.o
if exist src/libmbed* del /Q /F src/libmed*
ifneq ($(wildcard TESTS/.*),) ifneq ($(wildcard TESTS/.*),)
rmdir /Q /S TESTS rmdir /Q /S TESTS
endif endif

View file

@ -0,0 +1,35 @@
/**
* \file helpers.h
*
* \brief This file contains the prototypes of helper functions for the
* purpose of testing.
*/
/* Copyright (C) 2020, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#ifndef TEST_HELPERS_H
#define TEST_HELPERS_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#endif /* TEST_HELPERS_H */

19
tests/src/helpers.c Normal file
View file

@ -0,0 +1,19 @@
/* Copyright (C) 2020, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#include <test/helpers.h>

View file

@ -3,6 +3,7 @@
/* Headers */ /* Headers */
#include <test/macros.h> #include <test/macros.h>
#include <test/helpers.h>
#include <stdlib.h> #include <stdlib.h>