1MBEDTLS_TEST_PATH = .
2include ../scripts/common.make
3
4# 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
7# Also include library headers, for the sake of invasive tests.
8LOCAL_CFLAGS += -I../library
9
10# 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
15ifdef RECORD_PSA_STATUS_COVERAGE_LOG
16LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
17endif
18
19GENERATED_BIGNUM_DATA_FILES := $(patsubst tests/%,%,$(shell \
20	$(PYTHON) ../framework/scripts/generate_bignum_tests.py --list || \
21	echo FAILED \
22))
23ifeq ($(GENERATED_BIGNUM_DATA_FILES),FAILED)
24$(error "$(PYTHON) ../framework/scripts/generate_bignum_tests.py --list" failed)
25endif
26GENERATED_DATA_FILES += $(GENERATED_BIGNUM_DATA_FILES)
27
28GENERATED_CONFIG_DATA_FILES := $(patsubst tests/%,%,$(shell \
29	$(PYTHON) ../framework/scripts/generate_config_tests.py --list || \
30	echo FAILED \
31))
32ifeq ($(GENERATED_CONFIG_DATA_FILES),FAILED)
33$(error "$(PYTHON) ../framework/scripts/generate_config_tests.py --list" failed)
34endif
35GENERATED_DATA_FILES += $(GENERATED_CONFIG_DATA_FILES)
36
37GENERATED_ECP_DATA_FILES := $(patsubst tests/%,%,$(shell \
38	$(PYTHON) ../framework/scripts/generate_ecp_tests.py --list || \
39	echo FAILED \
40))
41ifeq ($(GENERATED_ECP_DATA_FILES),FAILED)
42$(error "$(PYTHON) ../framework/scripts/generate_ecp_tests.py --list" failed)
43endif
44GENERATED_DATA_FILES += $(GENERATED_ECP_DATA_FILES)
45
46GENERATED_PSA_DATA_FILES := $(patsubst tests/%,%,$(shell \
47	$(PYTHON) ../framework/scripts/generate_psa_tests.py --list || \
48	echo FAILED \
49))
50ifeq ($(GENERATED_PSA_DATA_FILES),FAILED)
51$(error "$(PYTHON) ../framework/scripts/generate_psa_tests.py --list" failed)
52endif
53GENERATED_DATA_FILES += $(GENERATED_PSA_DATA_FILES)
54
55GENERATED_FILES = $(GENERATED_DATA_FILES)
56GENERATED_FILES += src/test_keys.h src/test_certs.h
57
58.PHONY: generated_files
59generated_files: $(GENERATED_FILES)
60
61# generate_bignum_tests.py and generate_psa_tests.py spend more time analyzing
62# inputs than generating outputs. Its inputs are the same no matter which files
63# are being generated.
64# It's rare not to want all the outputs. So always generate all of its outputs.
65# Use an intermediate phony dependency so that parallel builds don't run
66# a separate instance of the recipe for each output file.
67$(GENERATED_BIGNUM_DATA_FILES): $(gen_file_dep) generated_bignum_test_data
68generated_bignum_test_data: ../framework/scripts/generate_bignum_tests.py
69generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_common.py
70generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_core.py
71generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_mod_raw.py
72generated_bignum_test_data: ../framework/scripts/mbedtls_framework/bignum_mod.py
73generated_bignum_test_data: ../framework/scripts/mbedtls_framework/test_case.py
74generated_bignum_test_data: ../framework/scripts/mbedtls_framework/test_data_generation.py
75generated_bignum_test_data:
76	echo "  Gen   $(GENERATED_BIGNUM_DATA_FILES)"
77	$(PYTHON) ../framework/scripts/generate_bignum_tests.py
78.SECONDARY: generated_bignum_test_data
79
80# We deliberately omit the configuration files (mbedtls_config.h,
81# crypto_config.h) from the depenency list because during development
82# and on the CI, we often edit those in a way that doesn't change the
83# output, to comment out certain options, or even to remove certain
84# lines which do affect the output negatively (it will miss the
85# corresponding test cases).
86$(GENERATED_CONFIG_DATA_FILES): $(gen_file_dep) generated_config_test_data
87generated_config_test_data: ../framework/scripts/generate_config_tests.py
88generated_config_test_data: ../scripts/config.py
89generated_config_test_data: ../framework/scripts/mbedtls_framework/test_case.py
90generated_config_test_data: ../framework/scripts/mbedtls_framework/test_data_generation.py
91generated_config_test_data:
92	echo "  Gen   $(GENERATED_CONFIG_DATA_FILES)"
93	$(PYTHON) ../framework/scripts/generate_config_tests.py
94.SECONDARY: generated_config_test_data
95
96$(GENERATED_ECP_DATA_FILES): $(gen_file_dep) generated_ecp_test_data
97generated_ecp_test_data: ../framework/scripts/generate_ecp_tests.py
98generated_ecp_test_data: ../framework/scripts/mbedtls_framework/bignum_common.py
99generated_ecp_test_data: ../framework/scripts/mbedtls_framework/ecp.py
100generated_ecp_test_data: ../framework/scripts/mbedtls_framework/test_case.py
101generated_ecp_test_data: ../framework/scripts/mbedtls_framework/test_data_generation.py
102generated_ecp_test_data:
103	echo "  Gen   $(GENERATED_ECP_DATA_FILES)"
104	$(PYTHON) ../framework/scripts/generate_ecp_tests.py
105.SECONDARY: generated_ecp_test_data
106
107$(GENERATED_PSA_DATA_FILES): $(gen_file_dep) generated_psa_test_data
108generated_psa_test_data: ../framework/scripts/generate_psa_tests.py
109generated_psa_test_data: ../framework/scripts/mbedtls_framework/crypto_data_tests.py
110generated_psa_test_data: ../framework/scripts/mbedtls_framework/crypto_knowledge.py
111generated_psa_test_data: ../framework/scripts/mbedtls_framework/macro_collector.py
112generated_psa_test_data: ../framework/scripts/mbedtls_framework/psa_information.py
113generated_psa_test_data: ../framework/scripts/mbedtls_framework/psa_storage.py
114generated_psa_test_data: ../framework/scripts/mbedtls_framework/test_case.py
115generated_psa_test_data: ../framework/scripts/mbedtls_framework/test_data_generation.py
116## The generated file only depends on the options that are present in
117## crypto_config.h, not on which options are set. To avoid regenerating this
118## file all the time when switching between configurations, don't declare
119## crypto_config.h as a dependency. Remove this file from your working tree
120## if you've just added or removed an option in crypto_config.h.
121#generated_psa_test_data: ../include/psa/crypto_config.h
122generated_psa_test_data: ../include/psa/crypto_values.h
123generated_psa_test_data: ../include/psa/crypto_extra.h
124generated_psa_test_data: suites/test_suite_psa_crypto_metadata.data
125generated_psa_test_data:
126	echo "  Gen   $(GENERATED_PSA_DATA_FILES) ..."
127	$(PYTHON) ../framework/scripts/generate_psa_tests.py
128.SECONDARY: generated_psa_test_data
129
130# A test application is built for each suites/test_suite_*.data file.
131# Application name is same as .data file's base name and can be
132# constructed by stripping path 'suites/' and extension .data.
133DATA_FILES := $(wildcard suites/test_suite_*.data)
134# Make sure that generated data files are included even if they don't
135# exist yet when the makefile is parsed.
136DATA_FILES += $(filter-out $(DATA_FILES),$(GENERATED_DATA_FILES))
137APPS = $(basename $(subst suites/,,$(DATA_FILES)))
138
139# Construct executable name by adding OS specific suffix $(EXEXT).
140BINARIES := $(addsuffix $(EXEXT),$(APPS))
141
142.SILENT:
143
144.PHONY: all check test clean
145
146all: $(BINARIES)
147
148mbedtls_test: $(MBEDTLS_TEST_OBJS)
149
150src/test_certs.h: ../framework/scripts/generate_test_cert_macros.py \
151				  $($(PYTHON) ../framework/scripts/generate_test_cert_macros.py --list-dependencies)
152	$(PYTHON) ../framework/scripts/generate_test_cert_macros.py --output $@
153
154src/test_keys.h: ../framework/scripts/generate_test_keys.py
155	$(PYTHON) ../framework/scripts/generate_test_keys.py --output $@
156
157TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h)
158ifdef RECORD_PSA_STATUS_COVERAGE_LOG
159# Explicitly depend on this header because on a clean copy of the source tree,
160# it doesn't exist yet and must be generated as part of the build, and
161# therefore the wildcard enumeration above doesn't include it.
162TEST_OBJS_DEPS += include/test/instrument_record_status.h
163endif
164TEST_OBJS_DEPS += src/test_certs.h src/test_keys.h
165
166# Rule to compile common test C files in src folder
167src/%.o : src/%.c $(TEST_OBJS_DEPS)
168	echo "  CC    $<"
169	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
170
171src/drivers/%.o : src/drivers/%.c
172	echo "  CC    $<"
173	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
174
175src/test_helpers/%.o : src/test_helpers/%.c
176	echo "  CC    $<"
177	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
178
179C_FILES := $(addsuffix .c,$(APPS))
180c: $(C_FILES)
181
182# Wildcard target for test code generation:
183# A .c file is generated for each .data file in the suites/ directory. Each .c
184# file depends on a .data and .function file from suites/ directory. Following
185# nameing convention is followed:
186#
187#     C file        |        Depends on
188#-----------------------------------------------------------------------------
189#  foo.c            | suites/foo.function suites/foo.data
190#  foo.bar.c        | suites/foo.function suites/foo.bar.data
191#
192# Note above that .c and .data files have same base name.
193# However, corresponding .function file's base name is the word before first
194# dot in .c file's base name.
195#
196.SECONDEXPANSION:
197%.c: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data ../framework/scripts/generate_test_code.py suites/helpers.function suites/main_test.function suites/host_test.function
198	echo "  Gen   $@"
199	$(PYTHON) ../framework/scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
200		-d suites/$*.data \
201		-t suites/main_test.function \
202		-p suites/host_test.function \
203		-s suites  \
204		--helpers-file suites/helpers.function \
205		-o .
206
207
208$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS)
209	echo "  CC    $<"
210	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
211
212clean:
213ifndef WINDOWS
214	rm -rf $(BINARIES) *.c *.datax
215	rm -f src/*.o src/drivers/*.o src/test_helpers/*.o src/libmbed*
216	rm -f include/test/instrument_record_status.h
217	rm -f include/alt-extra/*/*_alt.h
218	rm -rf libtestdriver1
219	rm -f ../library/libtestdriver1.a
220else
221	if exist *.c del /Q /F *.c
222	if exist *.exe del /Q /F *.exe
223	if exist *.datax del /Q /F *.datax
224	if exist src/*.o del /Q /F src/*.o
225	if exist src/drivers/*.o del /Q /F src/drivers/*.o
226	if exist src/test_helpers/*.o del /Q /F src/test_helpers/*.o
227	if exist src/libmbed* del /Q /F src/libmbed*
228	if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h
229endif
230
231# Test suites caught by SKIP_TEST_SUITES are built but not executed.
232check: $(BINARIES)
233	perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES)
234
235test: check
236
237# Generate variants of some headers for testing
238include/alt-extra/%_alt.h: ../include/%.h
239	perl -p -e 's/^(# *(define|ifndef) +\w+_)H\b/$${1}ALT_H/' $< >$@
240
241# Generate test library
242
243# Perl code that is executed to transform each original line from a library
244# source file into the corresponding line in the test driver copy of the
245# library. Add a LIBTESTDRIVER1_/libtestdriver1_ to mbedtls_xxx and psa_xxx
246# symbols.
247define libtestdriver1_rewrite :=
248	s!^(\s*#\s*include\s*[\"<])(mbedtls|psa)/!$${1}libtestdriver1/include/$${2}/!; \
249	next if /^\s*#\s*include/; \
250	s/\b(?=MBEDTLS_|PSA_)/LIBTESTDRIVER1_/g; \
251	s/\b(?=mbedtls_|psa_)/libtestdriver1_/g;
252endef
253
254libtestdriver1.a: export MBEDTLS_PATH := $(patsubst ../..//%,/%,../../$(MBEDTLS_PATH))
255libtestdriver1.a:
256	# Copy the library and fake a 3rdparty Makefile include.
257	rm -Rf ./libtestdriver1
258	mkdir ./libtestdriver1
259	cp -Rf ../library ./libtestdriver1
260	cp -Rf ../include ./libtestdriver1
261	cp -Rf ../scripts ./libtestdriver1
262	mkdir ./libtestdriver1/3rdparty
263	touch ./libtestdriver1/3rdparty/Makefile.inc
264
265	# Set the test driver base (minimal) configuration.
266	cp ./include/test/drivers/config_test_driver.h ./libtestdriver1/include/mbedtls/mbedtls_config.h
267
268	# Set the PSA cryptography configuration for the test library.
269	# It is set from the copied include/psa/crypto_config.h of the Mbed TLS
270        # library the test library is intended to be linked with extended by
271        # ./include/test/drivers/crypto_config_test_driver_extension.h to
272        # mirror the PSA_ACCEL_* macros.
273	mv ./libtestdriver1/include/psa/crypto_config.h ./libtestdriver1/include/psa/crypto_config.h.bak
274	head -n -1 ./libtestdriver1/include/psa/crypto_config.h.bak > ./libtestdriver1/include/psa/crypto_config.h
275	cat ./include/test/drivers/crypto_config_test_driver_extension.h >> ./libtestdriver1/include/psa/crypto_config.h
276	echo "#endif /* PSA_CRYPTO_CONFIG_H */" >> ./libtestdriver1/include/psa/crypto_config.h
277
278	# Prefix MBEDTLS_* PSA_* symbols with LIBTESTDRIVER1_ as well as
279	# mbedtls_* psa_* symbols with libtestdriver1_ to avoid symbol clash
280	# when this test driver library is linked with the Mbed TLS library.
281	perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/library/*.[ch]
282	perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/include/*/*.h
283
284	$(MAKE) -C ./libtestdriver1/library CFLAGS="-I../../ $(CFLAGS)" LDFLAGS="$(LDFLAGS)" libmbedcrypto.a
285	cp ./libtestdriver1/library/libmbedcrypto.a ../library/libtestdriver1.a
286
287ifdef RECORD_PSA_STATUS_COVERAGE_LOG
288include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile
289	echo "  Gen  $@"
290	sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
291endif
292