blob: 933cf6d1c6116133b4cd4afc0fb92c066bb2b750 [file] [log] [blame]
Gilles Peskine570e5482024-01-02 18:11:10 +01001MBEDTLS_TEST_PATH = .
Gilles Peskinef3d1ae12023-12-22 11:40:58 +01002include ../scripts/common.make
Paul Bakker0049c2f2009-07-11 19:15:43 +00003
Gilles Peskine396853a2021-09-20 18:57:55 +02004# Set this to -v to see the details of failing test cases
5TEST_FLAGS ?= $(if $(filter-out 0 OFF Off off NO No no FALSE False false N n,$(CTEST_OUTPUT_ON_FAILURE)),-v,)
6
Gilles Peskinef5c5ce72023-12-22 11:36:53 +01007# Also include library headers, for the sake of invasive tests.
8LOCAL_CFLAGS += -I../library
Paul Bakker0049c2f2009-07-11 19:15:43 +00009
Nicholas Wilson61fa4362018-06-25 12:10:00 +010010# Enable definition of various functions used throughout the testsuite
11# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
12# on non-POSIX platforms.
13LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L
14
Gilles Peskine51681552019-05-20 19:35:37 +020015ifdef RECORD_PSA_STATUS_COVERAGE_LOG
16LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
17endif
18
Werner Lewis8b2df742022-07-08 13:54:57 +010019GENERATED_BIGNUM_DATA_FILES := $(patsubst tests/%,%,$(shell \
David Horstmannea091522024-05-29 17:57:08 +010020 $(PYTHON) ../framework/scripts/generate_bignum_tests.py --list || \
Werner Lewis8b2df742022-07-08 13:54:57 +010021 echo FAILED \
22))
23ifeq ($(GENERATED_BIGNUM_DATA_FILES),FAILED)
David Horstmannea091522024-05-29 17:57:08 +010024$(error "$(PYTHON) ../framework/scripts/generate_bignum_tests.py --list" failed)
Werner Lewis8b2df742022-07-08 13:54:57 +010025endif
Gilles Peskine26d279e2024-05-23 16:38:07 +020026GENERATED_DATA_FILES += $(GENERATED_BIGNUM_DATA_FILES)
27
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010028GENERATED_ECP_DATA_FILES := $(patsubst tests/%,%,$(shell \
David Horstmannea091522024-05-29 17:57:08 +010029 $(PYTHON) ../framework/scripts/generate_ecp_tests.py --list || \
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010030 echo FAILED \
31))
32ifeq ($(GENERATED_ECP_DATA_FILES),FAILED)
David Horstmannea091522024-05-29 17:57:08 +010033$(error "$(PYTHON) ../framework/scripts/generate_ecp_tests.py --list" failed)
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010034endif
Gilles Peskine26d279e2024-05-23 16:38:07 +020035GENERATED_DATA_FILES += $(GENERATED_ECP_DATA_FILES)
36
Werner Lewis8b2df742022-07-08 13:54:57 +010037GENERATED_PSA_DATA_FILES := $(patsubst tests/%,%,$(shell \
David Horstmannea091522024-05-29 17:57:08 +010038 $(PYTHON) ../framework/scripts/generate_psa_tests.py --list || \
Gilles Peskinee9ad95a2021-07-13 18:36:05 +020039 echo FAILED \
40))
Werner Lewis8b2df742022-07-08 13:54:57 +010041ifeq ($(GENERATED_PSA_DATA_FILES),FAILED)
David Horstmannea091522024-05-29 17:57:08 +010042$(error "$(PYTHON) ../framework/scripts/generate_psa_tests.py --list" failed)
Gilles Peskinee9ad95a2021-07-13 18:36:05 +020043endif
Gilles Peskine26d279e2024-05-23 16:38:07 +020044GENERATED_DATA_FILES += $(GENERATED_PSA_DATA_FILES)
45
46GENERATED_FILES = $(GENERATED_DATA_FILES)
47GENERATED_FILES += src/test_keys.h src/test_certs.h
48
49.PHONY: generated_files
50generated_files: $(GENERATED_FILES)
Gilles Peskine687d1ab2021-04-22 01:01:56 +020051
Werner Lewis8b2df742022-07-08 13:54:57 +010052# generate_bignum_tests.py and generate_psa_tests.py spend more time analyzing
53# inputs than generating outputs. Its inputs are the same no matter which files
54# are being generated.
Gilles Peskine5df77c62021-07-13 17:22:58 +020055# It's rare not to want all the outputs. So always generate all of its outputs.
56# Use an intermediate phony dependency so that parallel builds don't run
57# a separate instance of the recipe for each output file.
Gilles Peskine0b62b7a2023-09-08 16:19:13 +020058$(GENERATED_BIGNUM_DATA_FILES): $(gen_file_dep) generated_bignum_test_data
David Horstmannea091522024-05-29 17:57:08 +010059generated_bignum_test_data: ../framework/scripts/generate_bignum_tests.py
David Horstmann9638ca32024-05-03 14:36:12 +010060generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_common.py
61generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_core.py
62generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_mod_raw.py
63generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_mod.py
64generated_bignum_test_data: ../framework/scripts/mbedtls_framework/test_case.py
65generated_bignum_test_data: ../framework/scripts/mbedtls_framework/test_data_generation.py
Werner Lewis8b2df742022-07-08 13:54:57 +010066generated_bignum_test_data:
67 echo " Gen $(GENERATED_BIGNUM_DATA_FILES)"
David Horstmannea091522024-05-29 17:57:08 +010068 $(PYTHON) ../framework/scripts/generate_bignum_tests.py
Gilles Peskine26d279e2024-05-23 16:38:07 +020069.SECONDARY: generated_bignum_test_data
Werner Lewis8b2df742022-07-08 13:54:57 +010070
Gilles Peskine0b62b7a2023-09-08 16:19:13 +020071$(GENERATED_ECP_DATA_FILES): $(gen_file_dep) generated_ecp_test_data
David Horstmannea091522024-05-29 17:57:08 +010072generated_ecp_test_data: ../framework/scripts/generate_ecp_tests.py
David Horstmann9638ca32024-05-03 14:36:12 +010073generated_ecp_test_data: ../framework/scripts/mbedtls_framework/bignum_common.py
74generated_ecp_test_data: ../framework/scripts/mbedtls_framework/ecp.py
75generated_ecp_test_data: ../framework/scripts/mbedtls_framework/test_case.py
76generated_ecp_test_data: ../framework/scripts/mbedtls_framework/test_data_generation.py
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010077generated_ecp_test_data:
78 echo " Gen $(GENERATED_ECP_DATA_FILES)"
David Horstmannea091522024-05-29 17:57:08 +010079 $(PYTHON) ../framework/scripts/generate_ecp_tests.py
Gilles Peskine26d279e2024-05-23 16:38:07 +020080.SECONDARY: generated_ecp_test_data
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010081
Gilles Peskine0b62b7a2023-09-08 16:19:13 +020082$(GENERATED_PSA_DATA_FILES): $(gen_file_dep) generated_psa_test_data
David Horstmannea091522024-05-29 17:57:08 +010083generated_psa_test_data: ../framework/scripts/generate_psa_tests.py
David Horstmann9638ca32024-05-03 14:36:12 +010084generated_psa_test_data: ../framework/scripts/mbedtls_framework/crypto_data_tests.py
85generated_psa_test_data: ../framework/scripts/mbedtls_framework/crypto_knowledge.py
86generated_psa_test_data: ../framework/scripts/mbedtls_framework/macro_collector.py
87generated_psa_test_data: ../framework/scripts/mbedtls_framework/psa_information.py
88generated_psa_test_data: ../framework/scripts/mbedtls_framework/psa_storage.py
89generated_psa_test_data: ../framework/scripts/mbedtls_framework/test_case.py
90generated_psa_test_data: ../framework/scripts/mbedtls_framework/test_data_generation.py
Gilles Peskine1411c7c2021-04-22 14:50:16 +020091## The generated file only depends on the options that are present in
92## crypto_config.h, not on which options are set. To avoid regenerating this
93## file all the time when switching between configurations, don't declare
94## crypto_config.h as a dependency. Remove this file from your working tree
95## if you've just added or removed an option in crypto_config.h.
Gilles Peskine5df77c62021-07-13 17:22:58 +020096#generated_psa_test_data: ../include/psa/crypto_config.h
97generated_psa_test_data: ../include/psa/crypto_values.h
98generated_psa_test_data: ../include/psa/crypto_extra.h
99generated_psa_test_data: suites/test_suite_psa_crypto_metadata.data
100generated_psa_test_data:
Werner Lewis8b2df742022-07-08 13:54:57 +0100101 echo " Gen $(GENERATED_PSA_DATA_FILES) ..."
David Horstmannea091522024-05-29 17:57:08 +0100102 $(PYTHON) ../framework/scripts/generate_psa_tests.py
Gilles Peskine26d279e2024-05-23 16:38:07 +0200103.SECONDARY: generated_psa_test_data
Gilles Peskine687d1ab2021-04-22 01:01:56 +0200104
Azim Khan27a35e72018-06-29 12:39:19 +0100105# A test application is built for each suites/test_suite_*.data file.
106# Application name is same as .data file's base name and can be
107# constructed by stripping path 'suites/' and extension .data.
Gilles Peskine687d1ab2021-04-22 01:01:56 +0200108DATA_FILES := $(wildcard suites/test_suite_*.data)
109# Make sure that generated data files are included even if they don't
110# exist yet when the makefile is parsed.
Gilles Peskine26d279e2024-05-23 16:38:07 +0200111DATA_FILES += $(filter-out $(DATA_FILES),$(GENERATED_DATA_FILES))
Gilles Peskine687d1ab2021-04-22 01:01:56 +0200112APPS = $(basename $(subst suites/,,$(DATA_FILES)))
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000113
Azim Khan27a35e72018-06-29 12:39:19 +0100114# Construct executable name by adding OS specific suffix $(EXEXT).
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000115BINARIES := $(addsuffix $(EXEXT),$(APPS))
116
Paul Bakker0049c2f2009-07-11 19:15:43 +0000117.SILENT:
118
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200119.PHONY: all check test clean
120
Azim Khan1de892b2017-06-09 15:02:36 +0100121all: $(BINARIES)
122
Ronald Cronddaf99c2020-06-19 11:27:26 +0200123mbedtls_test: $(MBEDTLS_TEST_OBJS)
124
David Horstmannea091522024-05-29 17:57:08 +0100125src/test_certs.h: ../framework/scripts/generate_test_cert_macros.py \
126 $($(PYTHON) ../framework/scripts/generate_test_cert_macros.py --list-dependencies)
127 $(PYTHON) ../framework/scripts/generate_test_cert_macros.py --output $@
Valerio Settia607a1b2024-04-08 17:52:12 +0200128
David Horstmannea091522024-05-29 17:57:08 +0100129src/test_keys.h: ../framework/scripts/generate_test_keys.py
130 $(PYTHON) ../framework/scripts/generate_test_keys.py --output $@
Valerio Settia607a1b2024-04-08 17:52:12 +0200131
Gilles Peskinee1d51bd2021-01-20 19:47:23 +0100132TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h)
Gilles Peskined71539f2020-11-25 18:17:17 +0100133ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Gilles Peskine75829a42021-01-25 13:46:14 +0100134# Explicitly depend on this header because on a clean copy of the source tree,
135# it doesn't exist yet and must be generated as part of the build, and
136# therefore the wildcard enumeration above doesn't include it.
Gilles Peskined71539f2020-11-25 18:17:17 +0100137TEST_OBJS_DEPS += include/test/instrument_record_status.h
138endif
Valerio Settia607a1b2024-04-08 17:52:12 +0200139TEST_OBJS_DEPS += src/test_certs.h src/test_keys.h
Gilles Peskined71539f2020-11-25 18:17:17 +0100140
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200141# Rule to compile common test C files in src folder
Gilles Peskined71539f2020-11-25 18:17:17 +0100142src/%.o : src/%.c $(TEST_OBJS_DEPS)
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200143 echo " CC $<"
144 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
145
Steven Cooremana70d5882020-07-16 20:26:18 +0200146src/drivers/%.o : src/drivers/%.c
147 echo " CC $<"
148 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
149
Yanray Wangb458b8c2022-10-28 11:49:33 +0800150src/test_helpers/%.o : src/test_helpers/%.c
151 echo " CC $<"
152 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
153
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000154C_FILES := $(addsuffix .c,$(APPS))
Gilles Peskine1d7cc082022-11-10 19:50:34 +0100155c: $(C_FILES)
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000156
Azim Khan27a35e72018-06-29 12:39:19 +0100157# Wildcard target for test code generation:
158# A .c file is generated for each .data file in the suites/ directory. Each .c
159# file depends on a .data and .function file from suites/ directory. Following
160# nameing convention is followed:
161#
162# C file | Depends on
163#-----------------------------------------------------------------------------
164# foo.c | suites/foo.function suites/foo.data
165# foo.bar.c | suites/foo.function suites/foo.bar.data
166#
167# Note above that .c and .data files have same base name.
168# However, corresponding .function file's base name is the word before first
169# dot in .c file's base name.
170#
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000171.SECONDEXPANSION:
David Horstmannea091522024-05-29 17:57:08 +0100172%.c: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data ../framework/scripts/generate_test_code.py suites/helpers.function suites/main_test.function suites/host_test.function
Manuel Pégourié-Gonnard78ec2b02015-07-08 22:12:06 +0100173 echo " Gen $@"
David Horstmannea091522024-05-29 17:57:08 +0100174 $(PYTHON) ../framework/scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100175 -d suites/$*.data \
Azim Khan1de892b2017-06-09 15:02:36 +0100176 -t suites/main_test.function \
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100177 -p suites/host_test.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100178 -s suites \
Azim Khane3b26af2018-06-29 02:36:57 +0100179 --helpers-file suites/helpers.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100180 -o .
Paul Bakker286bf3c2013-04-08 18:09:51 +0200181
Paul Bakker286bf3c2013-04-08 18:09:51 +0200182
Gilles Peskined71539f2020-11-25 18:17:17 +0100183$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS)
Manuel Pégourié-Gonnard78ec2b02015-07-08 22:12:06 +0100184 echo " CC $<"
Gilles Peskine4ad57332023-12-22 11:30:30 +0100185 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
Paul Bakker286bf3c2013-04-08 18:09:51 +0200186
Paul Bakker0049c2f2009-07-11 19:15:43 +0000187clean:
Paul Bakker62f88dc2012-05-10 21:26:28 +0000188ifndef WINDOWS
Gilles Peskined0254222021-09-14 11:28:22 +0200189 rm -rf $(BINARIES) *.c *.datax
Valerio Setti85a00662024-04-11 11:50:46 +0200190 rm -f src/*.o src/drivers/*.o src/test_helpers/*.o src/libmbed* src/test_keys.h src/test_certs.h
191 rm -f src/test_keys.h src/test_certs.h
Gilles Peskined71539f2020-11-25 18:17:17 +0100192 rm -f include/test/instrument_record_status.h
Gilles Peskinedf6e84a2023-02-22 22:09:51 +0100193 rm -f include/alt-extra/*/*_alt.h
Ronald Cron72b25da2021-04-28 18:29:24 +0200194 rm -rf libtestdriver1
Manuel Pégourié-Gonnard85101052022-12-29 16:04:35 +0100195 rm -f ../library/libtestdriver1.a
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200196else
Darryl Green6c0f94c2018-10-17 16:12:33 +0100197 if exist *.c del /Q /F *.c
198 if exist *.exe del /Q /F *.exe
199 if exist *.datax del /Q /F *.datax
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200200 if exist src/*.o del /Q /F src/*.o
Steven Cooremana70d5882020-07-16 20:26:18 +0200201 if exist src/drivers/*.o del /Q /F src/drivers/*.o
Valerio Setti85a00662024-04-11 11:50:46 +0200202 if exist src/test_keys.h del /Q /F src/test_keys.h
203 if exist src/test_certs.h del /Q /F src/test_cers.h
Yanray Wangb458b8c2022-10-28 11:49:33 +0800204 if exist src/test_helpers/*.o del /Q /F src/test_helpers/*.o
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200205 if exist src/libmbed* del /Q /F src/libmed*
Gilles Peskined71539f2020-11-25 18:17:17 +0100206 if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h
Azim Khan27a35e72018-06-29 12:39:19 +0100207endif
Paul Bakker0049c2f2009-07-11 19:15:43 +0000208
Gilles Peskineac372cc2018-11-29 10:15:06 +0000209# Test suites caught by SKIP_TEST_SUITES are built but not executed.
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000210check: $(BINARIES)
Gilles Peskine396853a2021-09-20 18:57:55 +0200211 perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES)
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200212
213test: check
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000214
Gilles Peskinedf6e84a2023-02-22 22:09:51 +0100215# Generate variants of some headers for testing
216include/alt-extra/%_alt.h: ../include/%.h
217 perl -p -e 's/^(# *(define|ifndef) +\w+_)H\b/$${1}ALT_H/' $< >$@
218
Ronald Cron72b25da2021-04-28 18:29:24 +0200219# Generate test library
220
221# Perl code that is executed to transform each original line from a library
222# source file into the corresponding line in the test driver copy of the
223# library. Add a LIBTESTDRIVER1_/libtestdriver1_ to mbedtls_xxx and psa_xxx
224# symbols.
225define libtestdriver1_rewrite :=
226 s!^(\s*#\s*include\s*[\"<])(mbedtls|psa)/!$${1}libtestdriver1/include/$${2}/!; \
227 next if /^\s*#\s*include/; \
228 s/\b(?=MBEDTLS_|PSA_)/LIBTESTDRIVER1_/g; \
229 s/\b(?=mbedtls_|psa_)/libtestdriver1_/g;
230endef
231
Gilles Peskine1c13aa72024-03-04 11:06:56 +0100232libtestdriver1.a: export MBEDTLS_PATH := $(patsubst ../..//%,/%,../../$(MBEDTLS_PATH))
Ronald Cron72b25da2021-04-28 18:29:24 +0200233libtestdriver1.a:
234 # Copy the library and fake a 3rdparty Makefile include.
235 rm -Rf ./libtestdriver1
236 mkdir ./libtestdriver1
237 cp -Rf ../library ./libtestdriver1
238 cp -Rf ../include ./libtestdriver1
239 cp -Rf ../scripts ./libtestdriver1
240 mkdir ./libtestdriver1/3rdparty
241 touch ./libtestdriver1/3rdparty/Makefile.inc
242
243 # Set the test driver base (minimal) configuration.
244 cp ./include/test/drivers/config_test_driver.h ./libtestdriver1/include/mbedtls/mbedtls_config.h
245
246 # Set the PSA cryptography configuration for the test library.
247 # It is set from the copied include/psa/crypto_config.h of the Mbed TLS
248 # library the test library is intended to be linked with extended by
249 # ./include/test/drivers/crypto_config_test_driver_extension.h to
250 # mirror the PSA_ACCEL_* macros.
251 mv ./libtestdriver1/include/psa/crypto_config.h ./libtestdriver1/include/psa/crypto_config.h.bak
252 head -n -1 ./libtestdriver1/include/psa/crypto_config.h.bak > ./libtestdriver1/include/psa/crypto_config.h
253 cat ./include/test/drivers/crypto_config_test_driver_extension.h >> ./libtestdriver1/include/psa/crypto_config.h
254 echo "#endif /* PSA_CRYPTO_CONFIG_H */" >> ./libtestdriver1/include/psa/crypto_config.h
255
256 # Prefix MBEDTLS_* PSA_* symbols with LIBTESTDRIVER1_ as well as
257 # mbedtls_* psa_* symbols with libtestdriver1_ to avoid symbol clash
258 # when this test driver library is linked with the Mbed TLS library.
259 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/library/*.[ch]
260 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/include/*/*.h
261
262 $(MAKE) -C ./libtestdriver1/library CFLAGS="-I../../ $(CFLAGS)" LDFLAGS="$(LDFLAGS)" libmbedcrypto.a
263 cp ./libtestdriver1/library/libmbedcrypto.a ../library/libtestdriver1.a
264
Gilles Peskine51681552019-05-20 19:35:37 +0200265ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Ronald Cron02c78b72020-05-27 09:22:32 +0200266include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile
Gilles Peskined71539f2020-11-25 18:17:17 +0100267 echo " Gen $@"
Gilles Peskine51681552019-05-20 19:35:37 +0200268 sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
269endif