1a8e1175bSopenharmony_ciMBEDTLS_TEST_PATH = .
2a8e1175bSopenharmony_ciinclude ../scripts/common.make
3a8e1175bSopenharmony_ci
4a8e1175bSopenharmony_ci# Set this to -v to see the details of failing test cases
5a8e1175bSopenharmony_ciTEST_FLAGS ?= $(if $(filter-out 0 OFF Off off NO No no FALSE False false N n,$(CTEST_OUTPUT_ON_FAILURE)),-v,)
6a8e1175bSopenharmony_ci
7a8e1175bSopenharmony_ci# Also include library headers, for the sake of invasive tests.
8a8e1175bSopenharmony_ciLOCAL_CFLAGS += -I../library
9a8e1175bSopenharmony_ci
10a8e1175bSopenharmony_ci# Enable definition of various functions used throughout the testsuite
11a8e1175bSopenharmony_ci# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
12a8e1175bSopenharmony_ci# on non-POSIX platforms.
13a8e1175bSopenharmony_ciLOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L
14a8e1175bSopenharmony_ci
15a8e1175bSopenharmony_ciifdef RECORD_PSA_STATUS_COVERAGE_LOG
16a8e1175bSopenharmony_ciLOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
17a8e1175bSopenharmony_ciendif
18a8e1175bSopenharmony_ci
19a8e1175bSopenharmony_ci.PHONY: generated_files
20a8e1175bSopenharmony_ciGENERATED_BIGNUM_DATA_FILES := $(patsubst tests/%,%,$(shell \
21a8e1175bSopenharmony_ci	$(PYTHON) scripts/generate_bignum_tests.py --list || \
22a8e1175bSopenharmony_ci	echo FAILED \
23a8e1175bSopenharmony_ci))
24a8e1175bSopenharmony_ciifeq ($(GENERATED_BIGNUM_DATA_FILES),FAILED)
25a8e1175bSopenharmony_ci$(error "$(PYTHON) scripts/generate_bignum_tests.py --list" failed)
26a8e1175bSopenharmony_ciendif
27a8e1175bSopenharmony_ciGENERATED_ECP_DATA_FILES := $(patsubst tests/%,%,$(shell \
28a8e1175bSopenharmony_ci	$(PYTHON) scripts/generate_ecp_tests.py --list || \
29a8e1175bSopenharmony_ci	echo FAILED \
30a8e1175bSopenharmony_ci))
31a8e1175bSopenharmony_ciifeq ($(GENERATED_ECP_DATA_FILES),FAILED)
32a8e1175bSopenharmony_ci$(error "$(PYTHON) scripts/generate_ecp_tests.py --list" failed)
33a8e1175bSopenharmony_ciendif
34a8e1175bSopenharmony_ciGENERATED_PSA_DATA_FILES := $(patsubst tests/%,%,$(shell \
35a8e1175bSopenharmony_ci	$(PYTHON) scripts/generate_psa_tests.py --list || \
36a8e1175bSopenharmony_ci	echo FAILED \
37a8e1175bSopenharmony_ci))
38a8e1175bSopenharmony_ciifeq ($(GENERATED_PSA_DATA_FILES),FAILED)
39a8e1175bSopenharmony_ci$(error "$(PYTHON) scripts/generate_psa_tests.py --list" failed)
40a8e1175bSopenharmony_ciendif
41a8e1175bSopenharmony_ciGENERATED_FILES := $(GENERATED_PSA_DATA_FILES) $(GENERATED_ECP_DATA_FILES) $(GENERATED_BIGNUM_DATA_FILES)
42a8e1175bSopenharmony_cigenerated_files: $(GENERATED_FILES)
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_ci# generate_bignum_tests.py and generate_psa_tests.py spend more time analyzing
45a8e1175bSopenharmony_ci# inputs than generating outputs. Its inputs are the same no matter which files
46a8e1175bSopenharmony_ci# are being generated.
47a8e1175bSopenharmony_ci# It's rare not to want all the outputs. So always generate all of its outputs.
48a8e1175bSopenharmony_ci# Use an intermediate phony dependency so that parallel builds don't run
49a8e1175bSopenharmony_ci# a separate instance of the recipe for each output file.
50a8e1175bSopenharmony_ci.SECONDARY: generated_bignum_test_data generated_ecp_test_data generated_psa_test_data
51a8e1175bSopenharmony_ci$(GENERATED_BIGNUM_DATA_FILES): $(gen_file_dep) generated_bignum_test_data
52a8e1175bSopenharmony_cigenerated_bignum_test_data: scripts/generate_bignum_tests.py
53a8e1175bSopenharmony_cigenerated_bignum_test_data: ../scripts/mbedtls_dev/bignum_common.py
54a8e1175bSopenharmony_cigenerated_bignum_test_data: ../scripts/mbedtls_dev/bignum_core.py
55a8e1175bSopenharmony_cigenerated_bignum_test_data: ../scripts/mbedtls_dev/bignum_mod_raw.py
56a8e1175bSopenharmony_cigenerated_bignum_test_data: ../scripts/mbedtls_dev/bignum_mod.py
57a8e1175bSopenharmony_cigenerated_bignum_test_data: ../scripts/mbedtls_dev/test_case.py
58a8e1175bSopenharmony_cigenerated_bignum_test_data: ../scripts/mbedtls_dev/test_data_generation.py
59a8e1175bSopenharmony_cigenerated_bignum_test_data:
60a8e1175bSopenharmony_ci	echo "  Gen   $(GENERATED_BIGNUM_DATA_FILES)"
61a8e1175bSopenharmony_ci	$(PYTHON) scripts/generate_bignum_tests.py
62a8e1175bSopenharmony_ci
63a8e1175bSopenharmony_ci$(GENERATED_ECP_DATA_FILES): $(gen_file_dep) generated_ecp_test_data
64a8e1175bSopenharmony_cigenerated_ecp_test_data: scripts/generate_ecp_tests.py
65a8e1175bSopenharmony_cigenerated_ecp_test_data: ../scripts/mbedtls_dev/bignum_common.py
66a8e1175bSopenharmony_cigenerated_ecp_test_data: ../scripts/mbedtls_dev/ecp.py
67a8e1175bSopenharmony_cigenerated_ecp_test_data: ../scripts/mbedtls_dev/test_case.py
68a8e1175bSopenharmony_cigenerated_ecp_test_data: ../scripts/mbedtls_dev/test_data_generation.py
69a8e1175bSopenharmony_cigenerated_ecp_test_data:
70a8e1175bSopenharmony_ci	echo "  Gen   $(GENERATED_ECP_DATA_FILES)"
71a8e1175bSopenharmony_ci	$(PYTHON) scripts/generate_ecp_tests.py
72a8e1175bSopenharmony_ci
73a8e1175bSopenharmony_ci$(GENERATED_PSA_DATA_FILES): $(gen_file_dep) generated_psa_test_data
74a8e1175bSopenharmony_cigenerated_psa_test_data: scripts/generate_psa_tests.py
75a8e1175bSopenharmony_cigenerated_psa_test_data: ../scripts/mbedtls_dev/crypto_data_tests.py
76a8e1175bSopenharmony_cigenerated_psa_test_data: ../scripts/mbedtls_dev/crypto_knowledge.py
77a8e1175bSopenharmony_cigenerated_psa_test_data: ../scripts/mbedtls_dev/macro_collector.py
78a8e1175bSopenharmony_cigenerated_psa_test_data: ../scripts/mbedtls_dev/psa_information.py
79a8e1175bSopenharmony_cigenerated_psa_test_data: ../scripts/mbedtls_dev/psa_storage.py
80a8e1175bSopenharmony_cigenerated_psa_test_data: ../scripts/mbedtls_dev/test_case.py
81a8e1175bSopenharmony_cigenerated_psa_test_data: ../scripts/mbedtls_dev/test_data_generation.py
82a8e1175bSopenharmony_ci## The generated file only depends on the options that are present in
83a8e1175bSopenharmony_ci## crypto_config.h, not on which options are set. To avoid regenerating this
84a8e1175bSopenharmony_ci## file all the time when switching between configurations, don't declare
85a8e1175bSopenharmony_ci## crypto_config.h as a dependency. Remove this file from your working tree
86a8e1175bSopenharmony_ci## if you've just added or removed an option in crypto_config.h.
87a8e1175bSopenharmony_ci#generated_psa_test_data: ../include/psa/crypto_config.h
88a8e1175bSopenharmony_cigenerated_psa_test_data: ../include/psa/crypto_values.h
89a8e1175bSopenharmony_cigenerated_psa_test_data: ../include/psa/crypto_extra.h
90a8e1175bSopenharmony_cigenerated_psa_test_data: suites/test_suite_psa_crypto_metadata.data
91a8e1175bSopenharmony_cigenerated_psa_test_data:
92a8e1175bSopenharmony_ci	echo "  Gen   $(GENERATED_PSA_DATA_FILES) ..."
93a8e1175bSopenharmony_ci	$(PYTHON) scripts/generate_psa_tests.py
94a8e1175bSopenharmony_ci
95a8e1175bSopenharmony_ci# A test application is built for each suites/test_suite_*.data file.
96a8e1175bSopenharmony_ci# Application name is same as .data file's base name and can be
97a8e1175bSopenharmony_ci# constructed by stripping path 'suites/' and extension .data.
98a8e1175bSopenharmony_ciDATA_FILES := $(wildcard suites/test_suite_*.data)
99a8e1175bSopenharmony_ci# Make sure that generated data files are included even if they don't
100a8e1175bSopenharmony_ci# exist yet when the makefile is parsed.
101a8e1175bSopenharmony_ciDATA_FILES += $(filter-out $(DATA_FILES),$(GENERATED_FILES))
102a8e1175bSopenharmony_ciAPPS = $(basename $(subst suites/,,$(DATA_FILES)))
103a8e1175bSopenharmony_ci
104a8e1175bSopenharmony_ci# Construct executable name by adding OS specific suffix $(EXEXT).
105a8e1175bSopenharmony_ciBINARIES := $(addsuffix $(EXEXT),$(APPS))
106a8e1175bSopenharmony_ci
107a8e1175bSopenharmony_ci.SILENT:
108a8e1175bSopenharmony_ci
109a8e1175bSopenharmony_ci.PHONY: all check test clean
110a8e1175bSopenharmony_ci
111a8e1175bSopenharmony_ciall: $(BINARIES)
112a8e1175bSopenharmony_ci
113a8e1175bSopenharmony_cimbedtls_test: $(MBEDTLS_TEST_OBJS)
114a8e1175bSopenharmony_ci
115a8e1175bSopenharmony_ciTEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h)
116a8e1175bSopenharmony_ciifdef RECORD_PSA_STATUS_COVERAGE_LOG
117a8e1175bSopenharmony_ci# Explicitly depend on this header because on a clean copy of the source tree,
118a8e1175bSopenharmony_ci# it doesn't exist yet and must be generated as part of the build, and
119a8e1175bSopenharmony_ci# therefore the wildcard enumeration above doesn't include it.
120a8e1175bSopenharmony_ciTEST_OBJS_DEPS += include/test/instrument_record_status.h
121a8e1175bSopenharmony_ciendif
122a8e1175bSopenharmony_ci
123a8e1175bSopenharmony_ci# Rule to compile common test C files in src folder
124a8e1175bSopenharmony_cisrc/%.o : src/%.c $(TEST_OBJS_DEPS)
125a8e1175bSopenharmony_ci	echo "  CC    $<"
126a8e1175bSopenharmony_ci	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
127a8e1175bSopenharmony_ci
128a8e1175bSopenharmony_cisrc/drivers/%.o : src/drivers/%.c
129a8e1175bSopenharmony_ci	echo "  CC    $<"
130a8e1175bSopenharmony_ci	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
131a8e1175bSopenharmony_ci
132a8e1175bSopenharmony_cisrc/test_helpers/%.o : src/test_helpers/%.c
133a8e1175bSopenharmony_ci	echo "  CC    $<"
134a8e1175bSopenharmony_ci	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
135a8e1175bSopenharmony_ci
136a8e1175bSopenharmony_ciC_FILES := $(addsuffix .c,$(APPS))
137a8e1175bSopenharmony_cic: $(C_FILES)
138a8e1175bSopenharmony_ci
139a8e1175bSopenharmony_ci# Wildcard target for test code generation:
140a8e1175bSopenharmony_ci# A .c file is generated for each .data file in the suites/ directory. Each .c
141a8e1175bSopenharmony_ci# file depends on a .data and .function file from suites/ directory. Following
142a8e1175bSopenharmony_ci# nameing convention is followed:
143a8e1175bSopenharmony_ci#
144a8e1175bSopenharmony_ci#     C file        |        Depends on
145a8e1175bSopenharmony_ci#-----------------------------------------------------------------------------
146a8e1175bSopenharmony_ci#  foo.c            | suites/foo.function suites/foo.data
147a8e1175bSopenharmony_ci#  foo.bar.c        | suites/foo.function suites/foo.bar.data
148a8e1175bSopenharmony_ci#
149a8e1175bSopenharmony_ci# Note above that .c and .data files have same base name.
150a8e1175bSopenharmony_ci# However, corresponding .function file's base name is the word before first
151a8e1175bSopenharmony_ci# dot in .c file's base name.
152a8e1175bSopenharmony_ci#
153a8e1175bSopenharmony_ci.SECONDEXPANSION:
154a8e1175bSopenharmony_ci%.c: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data scripts/generate_test_code.py suites/helpers.function suites/main_test.function suites/host_test.function
155a8e1175bSopenharmony_ci	echo "  Gen   $@"
156a8e1175bSopenharmony_ci	$(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
157a8e1175bSopenharmony_ci		-d suites/$*.data \
158a8e1175bSopenharmony_ci		-t suites/main_test.function \
159a8e1175bSopenharmony_ci		-p suites/host_test.function \
160a8e1175bSopenharmony_ci		-s suites  \
161a8e1175bSopenharmony_ci		--helpers-file suites/helpers.function \
162a8e1175bSopenharmony_ci		-o .
163a8e1175bSopenharmony_ci
164a8e1175bSopenharmony_ci
165a8e1175bSopenharmony_ci$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS)
166a8e1175bSopenharmony_ci	echo "  CC    $<"
167a8e1175bSopenharmony_ci	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
168a8e1175bSopenharmony_ci
169a8e1175bSopenharmony_ciclean:
170a8e1175bSopenharmony_ciifndef WINDOWS
171a8e1175bSopenharmony_ci	rm -rf $(BINARIES) *.c *.datax
172a8e1175bSopenharmony_ci	rm -f src/*.o src/drivers/*.o src/test_helpers/*.o src/libmbed*
173a8e1175bSopenharmony_ci	rm -f include/test/instrument_record_status.h
174a8e1175bSopenharmony_ci	rm -f include/alt-extra/*/*_alt.h
175a8e1175bSopenharmony_ci	rm -rf libtestdriver1
176a8e1175bSopenharmony_ci	rm -f ../library/libtestdriver1.a
177a8e1175bSopenharmony_cielse
178a8e1175bSopenharmony_ci	if exist *.c del /Q /F *.c
179a8e1175bSopenharmony_ci	if exist *.exe del /Q /F *.exe
180a8e1175bSopenharmony_ci	if exist *.datax del /Q /F *.datax
181a8e1175bSopenharmony_ci	if exist src/*.o del /Q /F src/*.o
182a8e1175bSopenharmony_ci	if exist src/drivers/*.o del /Q /F src/drivers/*.o
183a8e1175bSopenharmony_ci	if exist src/test_helpers/*.o del /Q /F src/test_helpers/*.o
184a8e1175bSopenharmony_ci	if exist src/libmbed* del /Q /F src/libmed*
185a8e1175bSopenharmony_ci	if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h
186a8e1175bSopenharmony_ciendif
187a8e1175bSopenharmony_ci
188a8e1175bSopenharmony_ci# Test suites caught by SKIP_TEST_SUITES are built but not executed.
189a8e1175bSopenharmony_cicheck: $(BINARIES)
190a8e1175bSopenharmony_ci	perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES)
191a8e1175bSopenharmony_ci
192a8e1175bSopenharmony_citest: check
193a8e1175bSopenharmony_ci
194a8e1175bSopenharmony_ci# Generate variants of some headers for testing
195a8e1175bSopenharmony_ciinclude/alt-extra/%_alt.h: ../include/%.h
196a8e1175bSopenharmony_ci	perl -p -e 's/^(# *(define|ifndef) +\w+_)H\b/$${1}ALT_H/' $< >$@
197a8e1175bSopenharmony_ci
198a8e1175bSopenharmony_ci# Generate test library
199a8e1175bSopenharmony_ci
200a8e1175bSopenharmony_ci# Perl code that is executed to transform each original line from a library
201a8e1175bSopenharmony_ci# source file into the corresponding line in the test driver copy of the
202a8e1175bSopenharmony_ci# library. Add a LIBTESTDRIVER1_/libtestdriver1_ to mbedtls_xxx and psa_xxx
203a8e1175bSopenharmony_ci# symbols.
204a8e1175bSopenharmony_cidefine libtestdriver1_rewrite :=
205a8e1175bSopenharmony_ci	s!^(\s*#\s*include\s*[\"<])(mbedtls|psa)/!$${1}libtestdriver1/include/$${2}/!; \
206a8e1175bSopenharmony_ci	next if /^\s*#\s*include/; \
207a8e1175bSopenharmony_ci	s/\b(?=MBEDTLS_|PSA_)/LIBTESTDRIVER1_/g; \
208a8e1175bSopenharmony_ci	s/\b(?=mbedtls_|psa_)/libtestdriver1_/g;
209a8e1175bSopenharmony_ciendef
210a8e1175bSopenharmony_ci
211a8e1175bSopenharmony_cilibtestdriver1.a: export MBEDTLS_PATH := $(patsubst ../..//%,/%,../../$(MBEDTLS_PATH))
212a8e1175bSopenharmony_cilibtestdriver1.a:
213a8e1175bSopenharmony_ci	# Copy the library and fake a 3rdparty Makefile include.
214a8e1175bSopenharmony_ci	rm -Rf ./libtestdriver1
215a8e1175bSopenharmony_ci	mkdir ./libtestdriver1
216a8e1175bSopenharmony_ci	cp -Rf ../library ./libtestdriver1
217a8e1175bSopenharmony_ci	cp -Rf ../include ./libtestdriver1
218a8e1175bSopenharmony_ci	cp -Rf ../scripts ./libtestdriver1
219a8e1175bSopenharmony_ci	mkdir ./libtestdriver1/3rdparty
220a8e1175bSopenharmony_ci	touch ./libtestdriver1/3rdparty/Makefile.inc
221a8e1175bSopenharmony_ci
222a8e1175bSopenharmony_ci	# Set the test driver base (minimal) configuration.
223a8e1175bSopenharmony_ci	cp ./include/test/drivers/config_test_driver.h ./libtestdriver1/include/mbedtls/mbedtls_config.h
224a8e1175bSopenharmony_ci
225a8e1175bSopenharmony_ci	# Set the PSA cryptography configuration for the test library.
226a8e1175bSopenharmony_ci	# It is set from the copied include/psa/crypto_config.h of the Mbed TLS
227a8e1175bSopenharmony_ci        # library the test library is intended to be linked with extended by
228a8e1175bSopenharmony_ci        # ./include/test/drivers/crypto_config_test_driver_extension.h to
229a8e1175bSopenharmony_ci        # mirror the PSA_ACCEL_* macros.
230a8e1175bSopenharmony_ci	mv ./libtestdriver1/include/psa/crypto_config.h ./libtestdriver1/include/psa/crypto_config.h.bak
231a8e1175bSopenharmony_ci	head -n -1 ./libtestdriver1/include/psa/crypto_config.h.bak > ./libtestdriver1/include/psa/crypto_config.h
232a8e1175bSopenharmony_ci	cat ./include/test/drivers/crypto_config_test_driver_extension.h >> ./libtestdriver1/include/psa/crypto_config.h
233a8e1175bSopenharmony_ci	echo "#endif /* PSA_CRYPTO_CONFIG_H */" >> ./libtestdriver1/include/psa/crypto_config.h
234a8e1175bSopenharmony_ci
235a8e1175bSopenharmony_ci	# Prefix MBEDTLS_* PSA_* symbols with LIBTESTDRIVER1_ as well as
236a8e1175bSopenharmony_ci	# mbedtls_* psa_* symbols with libtestdriver1_ to avoid symbol clash
237a8e1175bSopenharmony_ci	# when this test driver library is linked with the Mbed TLS library.
238a8e1175bSopenharmony_ci	perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/library/*.[ch]
239a8e1175bSopenharmony_ci	perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/include/*/*.h
240a8e1175bSopenharmony_ci
241a8e1175bSopenharmony_ci	$(MAKE) -C ./libtestdriver1/library CFLAGS="-I../../ $(CFLAGS)" LDFLAGS="$(LDFLAGS)" libmbedcrypto.a
242a8e1175bSopenharmony_ci	cp ./libtestdriver1/library/libmbedcrypto.a ../library/libtestdriver1.a
243a8e1175bSopenharmony_ci
244a8e1175bSopenharmony_ciifdef RECORD_PSA_STATUS_COVERAGE_LOG
245a8e1175bSopenharmony_ciinclude/test/instrument_record_status.h: ../include/psa/crypto.h Makefile
246a8e1175bSopenharmony_ci	echo "  Gen  $@"
247a8e1175bSopenharmony_ci	sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
248a8e1175bSopenharmony_ciendif
249