blob: 6367b3eb82073f44ceff697f7022bae99496da17 [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
Gilles Peskine687d1ab2021-04-22 01:01:56 +020019.PHONY: generated_files
Werner Lewis8b2df742022-07-08 13:54:57 +010020GENERATED_BIGNUM_DATA_FILES := $(patsubst tests/%,%,$(shell \
David Horstmannf6f3bca2024-05-29 17:57:08 +010021 $(PYTHON) ../framework/scripts/generate_bignum_tests.py --list || \
Werner Lewis8b2df742022-07-08 13:54:57 +010022 echo FAILED \
23))
24ifeq ($(GENERATED_BIGNUM_DATA_FILES),FAILED)
David Horstmannf6f3bca2024-05-29 17:57:08 +010025$(error "$(PYTHON) ../framework/scripts/generate_bignum_tests.py --list" failed)
Werner Lewis8b2df742022-07-08 13:54:57 +010026endif
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010027GENERATED_ECP_DATA_FILES := $(patsubst tests/%,%,$(shell \
David Horstmannf6f3bca2024-05-29 17:57:08 +010028 $(PYTHON) ../framework/scripts/generate_ecp_tests.py --list || \
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010029 echo FAILED \
30))
31ifeq ($(GENERATED_ECP_DATA_FILES),FAILED)
David Horstmannf6f3bca2024-05-29 17:57:08 +010032$(error "$(PYTHON) ../framework/scripts/generate_ecp_tests.py --list" failed)
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010033endif
Werner Lewis8b2df742022-07-08 13:54:57 +010034GENERATED_PSA_DATA_FILES := $(patsubst tests/%,%,$(shell \
David Horstmannf6f3bca2024-05-29 17:57:08 +010035 $(PYTHON) ../framework/scripts/generate_psa_tests.py --list || \
Gilles Peskinee9ad95a2021-07-13 18:36:05 +020036 echo FAILED \
37))
Werner Lewis8b2df742022-07-08 13:54:57 +010038ifeq ($(GENERATED_PSA_DATA_FILES),FAILED)
David Horstmannf6f3bca2024-05-29 17:57:08 +010039$(error "$(PYTHON) ../framework/scripts/generate_psa_tests.py --list" failed)
Gilles Peskinee9ad95a2021-07-13 18:36:05 +020040endif
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010041GENERATED_FILES := $(GENERATED_PSA_DATA_FILES) $(GENERATED_ECP_DATA_FILES) $(GENERATED_BIGNUM_DATA_FILES)
Valerio Setti96daf672024-04-11 11:50:46 +020042generated_files: $(GENERATED_FILES) src/test_keys.h src/test_certs.h
Gilles Peskine687d1ab2021-04-22 01:01:56 +020043
Werner Lewis8b2df742022-07-08 13:54:57 +010044# generate_bignum_tests.py and generate_psa_tests.py spend more time analyzing
45# inputs than generating outputs. Its inputs are the same no matter which files
46# are being generated.
Gilles Peskine5df77c62021-07-13 17:22:58 +020047# It's rare not to want all the outputs. So always generate all of its outputs.
48# Use an intermediate phony dependency so that parallel builds don't run
49# a separate instance of the recipe for each output file.
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010050.SECONDARY: generated_bignum_test_data generated_ecp_test_data generated_psa_test_data
Gilles Peskine0b62b7a2023-09-08 16:19:13 +020051$(GENERATED_BIGNUM_DATA_FILES): $(gen_file_dep) generated_bignum_test_data
David Horstmannf6f3bca2024-05-29 17:57:08 +010052generated_bignum_test_data: ../framework/scripts/generate_bignum_tests.py
David Horstmanncd84bb22024-05-03 14:36:12 +010053generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_common.py
54generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_core.py
55generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_mod_raw.py
56generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_mod.py
57generated_bignum_test_data: ../framework/scripts/mbedtls_framework/test_case.py
58generated_bignum_test_data: ../framework/scripts/mbedtls_framework/test_data_generation.py
Werner Lewis8b2df742022-07-08 13:54:57 +010059generated_bignum_test_data:
60 echo " Gen $(GENERATED_BIGNUM_DATA_FILES)"
David Horstmannf6f3bca2024-05-29 17:57:08 +010061 $(PYTHON) ../framework/scripts/generate_bignum_tests.py
Werner Lewis8b2df742022-07-08 13:54:57 +010062
Gilles Peskine0b62b7a2023-09-08 16:19:13 +020063$(GENERATED_ECP_DATA_FILES): $(gen_file_dep) generated_ecp_test_data
David Horstmannf6f3bca2024-05-29 17:57:08 +010064generated_ecp_test_data: ../framework/scripts/generate_ecp_tests.py
David Horstmanncd84bb22024-05-03 14:36:12 +010065generated_ecp_test_data: ../framework/scripts/mbedtls_framework/bignum_common.py
66generated_ecp_test_data: ../framework/scripts/mbedtls_framework/ecp.py
67generated_ecp_test_data: ../framework/scripts/mbedtls_framework/test_case.py
68generated_ecp_test_data: ../framework/scripts/mbedtls_framework/test_data_generation.py
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010069generated_ecp_test_data:
70 echo " Gen $(GENERATED_ECP_DATA_FILES)"
David Horstmannf6f3bca2024-05-29 17:57:08 +010071 $(PYTHON) ../framework/scripts/generate_ecp_tests.py
Gabor Mezei95ecaaf2023-01-16 16:53:29 +010072
Gilles Peskine0b62b7a2023-09-08 16:19:13 +020073$(GENERATED_PSA_DATA_FILES): $(gen_file_dep) generated_psa_test_data
David Horstmannf6f3bca2024-05-29 17:57:08 +010074generated_psa_test_data: ../framework/scripts/generate_psa_tests.py
David Horstmanncd84bb22024-05-03 14:36:12 +010075generated_psa_test_data: ../framework/scripts/mbedtls_framework/crypto_data_tests.py
76generated_psa_test_data: ../framework/scripts/mbedtls_framework/crypto_knowledge.py
77generated_psa_test_data: ../framework/scripts/mbedtls_framework/macro_collector.py
78generated_psa_test_data: ../framework/scripts/mbedtls_framework/psa_information.py
79generated_psa_test_data: ../framework/scripts/mbedtls_framework/psa_storage.py
80generated_psa_test_data: ../framework/scripts/mbedtls_framework/test_case.py
81generated_psa_test_data: ../framework/scripts/mbedtls_framework/test_data_generation.py
Gilles Peskine1411c7c2021-04-22 14:50:16 +020082## The generated file only depends on the options that are present in
83## crypto_config.h, not on which options are set. To avoid regenerating this
84## file all the time when switching between configurations, don't declare
85## crypto_config.h as a dependency. Remove this file from your working tree
86## if you've just added or removed an option in crypto_config.h.
Ronald Cronc7e9e362024-06-10 09:41:49 +020087#generated_psa_test_data: ../tf-psa-crypto/include/psa/crypto_config.h
88generated_psa_test_data: ../tf-psa-crypto/include/psa/crypto_values.h
89generated_psa_test_data: ../tf-psa-crypto/include/psa/crypto_extra.h
Gilles Peskine5df77c62021-07-13 17:22:58 +020090generated_psa_test_data: suites/test_suite_psa_crypto_metadata.data
91generated_psa_test_data:
Werner Lewis8b2df742022-07-08 13:54:57 +010092 echo " Gen $(GENERATED_PSA_DATA_FILES) ..."
David Horstmannf6f3bca2024-05-29 17:57:08 +010093 $(PYTHON) ../framework/scripts/generate_psa_tests.py
Gilles Peskine687d1ab2021-04-22 01:01:56 +020094
Azim Khan27a35e72018-06-29 12:39:19 +010095# A test application is built for each suites/test_suite_*.data file.
96# Application name is same as .data file's base name and can be
97# constructed by stripping path 'suites/' and extension .data.
Gilles Peskine687d1ab2021-04-22 01:01:56 +020098DATA_FILES := $(wildcard suites/test_suite_*.data)
99# Make sure that generated data files are included even if they don't
100# exist yet when the makefile is parsed.
Werner Lewis8b2df742022-07-08 13:54:57 +0100101DATA_FILES += $(filter-out $(DATA_FILES),$(GENERATED_FILES))
Gilles Peskine687d1ab2021-04-22 01:01:56 +0200102APPS = $(basename $(subst suites/,,$(DATA_FILES)))
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000103
Azim Khan27a35e72018-06-29 12:39:19 +0100104# Construct executable name by adding OS specific suffix $(EXEXT).
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000105BINARIES := $(addsuffix $(EXEXT),$(APPS))
106
Paul Bakker0049c2f2009-07-11 19:15:43 +0000107.SILENT:
108
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200109.PHONY: all check test clean
110
Azim Khan1de892b2017-06-09 15:02:36 +0100111all: $(BINARIES)
112
Ronald Cronddaf99c2020-06-19 11:27:26 +0200113mbedtls_test: $(MBEDTLS_TEST_OBJS)
114
David Horstmannf6f3bca2024-05-29 17:57:08 +0100115src/test_certs.h: ../framework/scripts/generate_test_cert_macros.py \
116 $($(PYTHON) ../framework/scripts/generate_test_cert_macros.py --list-dependencies)
117 $(PYTHON) ../framework/scripts/generate_test_cert_macros.py --output $@
Valerio Setti8284f3d2024-04-08 17:52:12 +0200118
David Horstmannf6f3bca2024-05-29 17:57:08 +0100119src/test_keys.h: ../framework/scripts/generate_test_keys.py
120 $(PYTHON) ../framework/scripts/generate_test_keys.py --output $@
Valerio Setti8284f3d2024-04-08 17:52:12 +0200121
Gilles Peskinee1d51bd2021-01-20 19:47:23 +0100122TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h)
Gilles Peskined71539f2020-11-25 18:17:17 +0100123ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Gilles Peskine75829a42021-01-25 13:46:14 +0100124# Explicitly depend on this header because on a clean copy of the source tree,
125# it doesn't exist yet and must be generated as part of the build, and
126# therefore the wildcard enumeration above doesn't include it.
Gilles Peskined71539f2020-11-25 18:17:17 +0100127TEST_OBJS_DEPS += include/test/instrument_record_status.h
128endif
Valerio Setti8284f3d2024-04-08 17:52:12 +0200129TEST_OBJS_DEPS += src/test_certs.h src/test_keys.h
Gilles Peskined71539f2020-11-25 18:17:17 +0100130
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200131# Rule to compile common test C files in src folder
Gilles Peskined71539f2020-11-25 18:17:17 +0100132src/%.o : src/%.c $(TEST_OBJS_DEPS)
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200133 echo " CC $<"
134 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
135
Steven Cooremana70d5882020-07-16 20:26:18 +0200136src/drivers/%.o : src/drivers/%.c
137 echo " CC $<"
138 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
139
Yanray Wangb458b8c2022-10-28 11:49:33 +0800140src/test_helpers/%.o : src/test_helpers/%.c
141 echo " CC $<"
142 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
143
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000144C_FILES := $(addsuffix .c,$(APPS))
Gilles Peskine1d7cc082022-11-10 19:50:34 +0100145c: $(C_FILES)
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000146
Azim Khan27a35e72018-06-29 12:39:19 +0100147# Wildcard target for test code generation:
148# A .c file is generated for each .data file in the suites/ directory. Each .c
149# file depends on a .data and .function file from suites/ directory. Following
150# nameing convention is followed:
151#
152# C file | Depends on
153#-----------------------------------------------------------------------------
154# foo.c | suites/foo.function suites/foo.data
155# foo.bar.c | suites/foo.function suites/foo.bar.data
156#
157# Note above that .c and .data files have same base name.
158# However, corresponding .function file's base name is the word before first
159# dot in .c file's base name.
160#
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000161.SECONDEXPANSION:
David Horstmannf6f3bca2024-05-29 17:57:08 +0100162%.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 +0100163 echo " Gen $@"
David Horstmannf6f3bca2024-05-29 17:57:08 +0100164 $(PYTHON) ../framework/scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100165 -d suites/$*.data \
Azim Khan1de892b2017-06-09 15:02:36 +0100166 -t suites/main_test.function \
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100167 -p suites/host_test.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100168 -s suites \
Azim Khane3b26af2018-06-29 02:36:57 +0100169 --helpers-file suites/helpers.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100170 -o .
Paul Bakker286bf3c2013-04-08 18:09:51 +0200171
Paul Bakker286bf3c2013-04-08 18:09:51 +0200172
Gilles Peskined71539f2020-11-25 18:17:17 +0100173$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS)
Manuel Pégourié-Gonnard78ec2b02015-07-08 22:12:06 +0100174 echo " CC $<"
Gilles Peskine4ad57332023-12-22 11:30:30 +0100175 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
Paul Bakker286bf3c2013-04-08 18:09:51 +0200176
Paul Bakker0049c2f2009-07-11 19:15:43 +0000177clean:
Paul Bakker62f88dc2012-05-10 21:26:28 +0000178ifndef WINDOWS
Gilles Peskined0254222021-09-14 11:28:22 +0200179 rm -rf $(BINARIES) *.c *.datax
Valerio Setti96daf672024-04-11 11:50:46 +0200180 rm -f src/*.o src/drivers/*.o src/test_helpers/*.o src/libmbed* src/test_keys.h src/test_certs.h
181 rm -f src/test_keys.h src/test_certs.h
Gilles Peskined71539f2020-11-25 18:17:17 +0100182 rm -f include/test/instrument_record_status.h
Gilles Peskinedf6e84a2023-02-22 22:09:51 +0100183 rm -f include/alt-extra/*/*_alt.h
Ronald Cron72b25da2021-04-28 18:29:24 +0200184 rm -rf libtestdriver1
Valerio Settid1b6ef12024-05-07 16:00:21 +0200185 rm -rf libpsaclient libpsaserver
Manuel Pégourié-Gonnard85101052022-12-29 16:04:35 +0100186 rm -f ../library/libtestdriver1.a
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200187else
Darryl Green6c0f94c2018-10-17 16:12:33 +0100188 if exist *.c del /Q /F *.c
189 if exist *.exe del /Q /F *.exe
190 if exist *.datax del /Q /F *.datax
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200191 if exist src/*.o del /Q /F src/*.o
Steven Cooremana70d5882020-07-16 20:26:18 +0200192 if exist src/drivers/*.o del /Q /F src/drivers/*.o
Valerio Setti96daf672024-04-11 11:50:46 +0200193 if exist src/test_keys.h del /Q /F src/test_keys.h
194 if exist src/test_certs.h del /Q /F src/test_cers.h
Yanray Wangb458b8c2022-10-28 11:49:33 +0800195 if exist src/test_helpers/*.o del /Q /F src/test_helpers/*.o
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200196 if exist src/libmbed* del /Q /F src/libmed*
Gilles Peskined71539f2020-11-25 18:17:17 +0100197 if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h
Azim Khan27a35e72018-06-29 12:39:19 +0100198endif
Paul Bakker0049c2f2009-07-11 19:15:43 +0000199
Gilles Peskineac372cc2018-11-29 10:15:06 +0000200# Test suites caught by SKIP_TEST_SUITES are built but not executed.
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000201check: $(BINARIES)
Gilles Peskine396853a2021-09-20 18:57:55 +0200202 perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES)
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200203
204test: check
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000205
Gilles Peskinedf6e84a2023-02-22 22:09:51 +0100206# Generate variants of some headers for testing
207include/alt-extra/%_alt.h: ../include/%.h
208 perl -p -e 's/^(# *(define|ifndef) +\w+_)H\b/$${1}ALT_H/' $< >$@
Ronald Cronc7e9e362024-06-10 09:41:49 +0200209include/alt-extra/%_alt.h: ../tf-psa-crypto/include/%.h
210 perl -p -e 's/^(# *(define|ifndef) +\w+_)H\b/$${1}ALT_H/' $< >$@
Gilles Peskinedf6e84a2023-02-22 22:09:51 +0100211
Ronald Cron72b25da2021-04-28 18:29:24 +0200212# Generate test library
213
214# Perl code that is executed to transform each original line from a library
215# source file into the corresponding line in the test driver copy of the
216# library. Add a LIBTESTDRIVER1_/libtestdriver1_ to mbedtls_xxx and psa_xxx
217# symbols.
218define libtestdriver1_rewrite :=
219 s!^(\s*#\s*include\s*[\"<])(mbedtls|psa)/!$${1}libtestdriver1/include/$${2}/!; \
220 next if /^\s*#\s*include/; \
221 s/\b(?=MBEDTLS_|PSA_)/LIBTESTDRIVER1_/g; \
222 s/\b(?=mbedtls_|psa_)/libtestdriver1_/g;
223endef
224
Gilles Peskine1c13aa72024-03-04 11:06:56 +0100225libtestdriver1.a: export MBEDTLS_PATH := $(patsubst ../..//%,/%,../../$(MBEDTLS_PATH))
Ronald Cron72b25da2021-04-28 18:29:24 +0200226libtestdriver1.a:
227 # Copy the library and fake a 3rdparty Makefile include.
228 rm -Rf ./libtestdriver1
229 mkdir ./libtestdriver1
230 cp -Rf ../library ./libtestdriver1
231 cp -Rf ../include ./libtestdriver1
232 cp -Rf ../scripts ./libtestdriver1
233 mkdir ./libtestdriver1/3rdparty
234 touch ./libtestdriver1/3rdparty/Makefile.inc
235
236 # Set the test driver base (minimal) configuration.
237 cp ./include/test/drivers/config_test_driver.h ./libtestdriver1/include/mbedtls/mbedtls_config.h
238
239 # Set the PSA cryptography configuration for the test library.
240 # It is set from the copied include/psa/crypto_config.h of the Mbed TLS
241 # library the test library is intended to be linked with extended by
242 # ./include/test/drivers/crypto_config_test_driver_extension.h to
243 # mirror the PSA_ACCEL_* macros.
244 mv ./libtestdriver1/include/psa/crypto_config.h ./libtestdriver1/include/psa/crypto_config.h.bak
245 head -n -1 ./libtestdriver1/include/psa/crypto_config.h.bak > ./libtestdriver1/include/psa/crypto_config.h
246 cat ./include/test/drivers/crypto_config_test_driver_extension.h >> ./libtestdriver1/include/psa/crypto_config.h
247 echo "#endif /* PSA_CRYPTO_CONFIG_H */" >> ./libtestdriver1/include/psa/crypto_config.h
248
249 # Prefix MBEDTLS_* PSA_* symbols with LIBTESTDRIVER1_ as well as
250 # mbedtls_* psa_* symbols with libtestdriver1_ to avoid symbol clash
251 # when this test driver library is linked with the Mbed TLS library.
252 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/library/*.[ch]
253 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/include/*/*.h
254
255 $(MAKE) -C ./libtestdriver1/library CFLAGS="-I../../ $(CFLAGS)" LDFLAGS="$(LDFLAGS)" libmbedcrypto.a
256 cp ./libtestdriver1/library/libmbedcrypto.a ../library/libtestdriver1.a
257
Gilles Peskine51681552019-05-20 19:35:37 +0200258ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Ronald Cronc7e9e362024-06-10 09:41:49 +0200259include/test/instrument_record_status.h: ../tf-psa-crypto/include/psa/crypto.h Makefile
Gilles Peskined71539f2020-11-25 18:17:17 +0100260 echo " Gen $@"
Ronald Cronc7e9e362024-06-10 09:41:49 +0200261 sed <../tf-psa-crypto/include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
Gilles Peskine51681552019-05-20 19:35:37 +0200262endif