blob: f0373381ef80ad075c1d00e39f6c3ac19e5094c6 [file] [log] [blame]
Paul Bakker0049c2f2009-07-11 19:15:43 +00001
2# To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS
Paul Bakker0049c2f2009-07-11 19:15:43 +00003
Mohammad Azim Khan95402612017-07-19 10:15:54 +01004CFLAGS ?= -O2
Paul Elliott9e3256a2020-12-17 18:17:32 +00005WARNING_CFLAGS ?= -Wall -Wextra -Wformat=2 -Wno-format-nonliteral
Alon Bar-Levada41052015-02-18 17:47:52 +02006LDFLAGS ?=
Paul Bakker0049c2f2009-07-11 19:15:43 +00007
Gilles Peskine396853a2021-09-20 18:57:55 +02008# Set this to -v to see the details of failing test cases
9TEST_FLAGS ?= $(if $(filter-out 0 OFF Off off NO No no FALSE False false N n,$(CTEST_OUTPUT_ON_FAILURE)),-v,)
10
Gilles Peskine687d1ab2021-04-22 01:01:56 +020011default: all
12
Gilles Peskine76dd3aa2020-07-02 15:58:37 +020013# Include public header files from ../include, test-specific header files
14# from ./include, and private header files (used by some invasive tests)
15# from ../library.
Ronald Cron02c78b72020-05-27 09:22:32 +020016LOCAL_CFLAGS = $(WARNING_CFLAGS) -I./include -I../include -I../library -D_FILE_OFFSET_BITS=64
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000017LOCAL_LDFLAGS = -L../library \
Gilles Peskine4fa9f9f2020-02-26 19:05:31 +010018 -lmbedtls$(SHARED_SUFFIX) \
19 -lmbedx509$(SHARED_SUFFIX) \
Manuel Pégourié-Gonnard21e1ac22015-06-25 08:45:12 +020020 -lmbedcrypto$(SHARED_SUFFIX)
Paul Bakker0049c2f2009-07-11 19:15:43 +000021
Christoph M. Wintersteiger6ea2dea12019-01-21 17:26:19 +000022include ../3rdparty/Makefile.inc
23LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
Christoph M. Wintersteigerd5fd7662018-10-25 12:47:03 +010024
Nicholas Wilson61fa4362018-06-25 12:10:00 +010025# Enable definition of various functions used throughout the testsuite
26# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
27# on non-POSIX platforms.
28LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L
29
Paul Bakker674e0b02014-03-26 13:26:52 +010030ifndef SHARED
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020031MBEDLIBS=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
Paul Bakker674e0b02014-03-26 13:26:52 +010032else
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020033MBEDLIBS=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
Paul Bakker674e0b02014-03-26 13:26:52 +010034endif
35
Paul Bakkerc7ffd362012-04-05 12:08:29 +000036ifdef DEBUG
Alon Bar-Levf7a9f302015-02-18 17:55:05 +020037LOCAL_CFLAGS += -g3
Paul Bakkerc7ffd362012-04-05 12:08:29 +000038endif
39
Gilles Peskine51681552019-05-20 19:35:37 +020040ifdef RECORD_PSA_STATUS_COVERAGE_LOG
41LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
42endif
43
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +020044# if we're running on Windows, build for Windows
Paul Bakkercd5b5292012-05-10 20:49:10 +000045ifdef WINDOWS
Alon Bar-Lev18ba0cc2015-02-14 01:04:58 +020046WINDOWS_BUILD=1
47endif
48
49ifdef WINDOWS_BUILD
50DLEXT=dll
51EXEXT=.exe
Alon Bar-Levada41052015-02-18 17:47:52 +020052LOCAL_LDFLAGS += -lws2_32
Alon Bar-Lev18ba0cc2015-02-14 01:04:58 +020053ifdef SHARED
54SHARED_SUFFIX=.$(DLEXT)
55endif
Manuel Pégourié-Gonnard8d4a6132015-06-24 12:16:20 +020056else
Andres Amaya Garcia420f0cc2018-03-27 19:17:21 +010057DLEXT ?= so
Manuel Pégourié-Gonnard8d4a6132015-06-24 12:16:20 +020058EXEXT=
59SHARED_SUFFIX=
Gilles Peskine8b427c82021-07-13 18:14:25 +020060endif
61
62ifdef WINDOWS
63PYTHON ?= python
64else
Gilles Peskineb61a6142021-04-22 00:21:58 +020065PYTHON ?= $(shell if type python3 >/dev/null 2>/dev/null; then echo python3; else echo python; fi)
Paul Bakkercd5b5292012-05-10 20:49:10 +000066endif
67
Gilles Peskine687d1ab2021-04-22 01:01:56 +020068.PHONY: generated_files
Werner Lewis8b2df742022-07-08 13:54:57 +010069GENERATED_BIGNUM_DATA_FILES := $(patsubst tests/%,%,$(shell \
70 $(PYTHON) scripts/generate_bignum_tests.py --list || \
71 echo FAILED \
72))
73ifeq ($(GENERATED_BIGNUM_DATA_FILES),FAILED)
74$(error "$(PYTHON) scripts/generate_bignum_tests.py --list" failed)
75endif
76GENERATED_PSA_DATA_FILES := $(patsubst tests/%,%,$(shell \
Gilles Peskinee9ad95a2021-07-13 18:36:05 +020077 $(PYTHON) scripts/generate_psa_tests.py --list || \
78 echo FAILED \
79))
Werner Lewis8b2df742022-07-08 13:54:57 +010080ifeq ($(GENERATED_PSA_DATA_FILES),FAILED)
Gilles Peskinee9ad95a2021-07-13 18:36:05 +020081$(error "$(PYTHON) scripts/generate_psa_tests.py --list" failed)
82endif
Werner Lewis8b2df742022-07-08 13:54:57 +010083GENERATED_FILES := $(GENERATED_PSA_DATA_FILES) $(GENERATED_BIGNUM_DATA_FILES)
Gilles Peskine687d1ab2021-04-22 01:01:56 +020084generated_files: $(GENERATED_FILES)
85
Werner Lewis8b2df742022-07-08 13:54:57 +010086# generate_bignum_tests.py and generate_psa_tests.py spend more time analyzing
87# inputs than generating outputs. Its inputs are the same no matter which files
88# are being generated.
Gilles Peskine5df77c62021-07-13 17:22:58 +020089# It's rare not to want all the outputs. So always generate all of its outputs.
90# Use an intermediate phony dependency so that parallel builds don't run
91# a separate instance of the recipe for each output file.
Werner Lewis8b2df742022-07-08 13:54:57 +010092.SECONDARY: generated_bignum_test_data generated_psa_test_data
93$(GENERATED_BIGNUM_DATA_FILES): generated_bignum_test_data
94generated_bignum_test_data: scripts/generate_bignum_tests.py
Werner Lewis99e81782022-09-30 16:28:43 +010095generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_common.py
96generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_core.py
Gabor Mezeic426d9b2022-11-15 18:51:20 +010097generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_mod_raw.py
Werner Lewisc84b7312022-11-30 14:43:31 +000098generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_mod.py
Werner Lewis8b2df742022-07-08 13:54:57 +010099generated_bignum_test_data: ../scripts/mbedtls_dev/test_case.py
Gilles Peskine64f2efd2022-09-16 21:41:47 +0200100generated_bignum_test_data: ../scripts/mbedtls_dev/test_data_generation.py
Werner Lewis8b2df742022-07-08 13:54:57 +0100101generated_bignum_test_data:
102 echo " Gen $(GENERATED_BIGNUM_DATA_FILES)"
103 $(PYTHON) scripts/generate_bignum_tests.py
104
105$(GENERATED_PSA_DATA_FILES): generated_psa_test_data
Gilles Peskine5df77c62021-07-13 17:22:58 +0200106generated_psa_test_data: scripts/generate_psa_tests.py
Gilles Peskine26f90542022-03-15 16:39:51 +0100107generated_psa_test_data: ../scripts/mbedtls_dev/crypto_knowledge.py
108generated_psa_test_data: ../scripts/mbedtls_dev/macro_collector.py
109generated_psa_test_data: ../scripts/mbedtls_dev/psa_storage.py
110generated_psa_test_data: ../scripts/mbedtls_dev/test_case.py
Gilles Peskine64f2efd2022-09-16 21:41:47 +0200111generated_psa_test_data: ../scripts/mbedtls_dev/test_data_generation.py
Gilles Peskine1411c7c2021-04-22 14:50:16 +0200112## The generated file only depends on the options that are present in
113## crypto_config.h, not on which options are set. To avoid regenerating this
114## file all the time when switching between configurations, don't declare
115## crypto_config.h as a dependency. Remove this file from your working tree
116## if you've just added or removed an option in crypto_config.h.
Gilles Peskine5df77c62021-07-13 17:22:58 +0200117#generated_psa_test_data: ../include/psa/crypto_config.h
118generated_psa_test_data: ../include/psa/crypto_values.h
119generated_psa_test_data: ../include/psa/crypto_extra.h
120generated_psa_test_data: suites/test_suite_psa_crypto_metadata.data
121generated_psa_test_data:
Werner Lewis8b2df742022-07-08 13:54:57 +0100122 echo " Gen $(GENERATED_PSA_DATA_FILES) ..."
Gilles Peskine687d1ab2021-04-22 01:01:56 +0200123 $(PYTHON) scripts/generate_psa_tests.py
124
Azim Khan27a35e72018-06-29 12:39:19 +0100125# A test application is built for each suites/test_suite_*.data file.
126# Application name is same as .data file's base name and can be
127# constructed by stripping path 'suites/' and extension .data.
Gilles Peskine687d1ab2021-04-22 01:01:56 +0200128DATA_FILES := $(wildcard suites/test_suite_*.data)
129# Make sure that generated data files are included even if they don't
130# exist yet when the makefile is parsed.
Werner Lewis8b2df742022-07-08 13:54:57 +0100131DATA_FILES += $(filter-out $(DATA_FILES),$(GENERATED_FILES))
Gilles Peskine687d1ab2021-04-22 01:01:56 +0200132APPS = $(basename $(subst suites/,,$(DATA_FILES)))
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000133
Azim Khan27a35e72018-06-29 12:39:19 +0100134# Construct executable name by adding OS specific suffix $(EXEXT).
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000135BINARIES := $(addsuffix $(EXEXT),$(APPS))
136
Paul Bakker0049c2f2009-07-11 19:15:43 +0000137.SILENT:
138
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200139.PHONY: all check test clean
140
Azim Khan1de892b2017-06-09 15:02:36 +0100141all: $(BINARIES)
142
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200143$(MBEDLIBS):
Manuel Pégourié-Gonnardfc367082015-06-26 16:50:24 +0200144 $(MAKE) -C ../library
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200145
Steven Cooremana70d5882020-07-16 20:26:18 +0200146MBEDTLS_TEST_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c src/drivers/*.c))
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200147
Ronald Cronddaf99c2020-06-19 11:27:26 +0200148mbedtls_test: $(MBEDTLS_TEST_OBJS)
149
Gilles Peskinee1d51bd2021-01-20 19:47:23 +0100150TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h)
Gilles Peskined71539f2020-11-25 18:17:17 +0100151ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Gilles Peskine75829a42021-01-25 13:46:14 +0100152# Explicitly depend on this header because on a clean copy of the source tree,
153# it doesn't exist yet and must be generated as part of the build, and
154# therefore the wildcard enumeration above doesn't include it.
Gilles Peskined71539f2020-11-25 18:17:17 +0100155TEST_OBJS_DEPS += include/test/instrument_record_status.h
156endif
157
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200158# Rule to compile common test C files in src folder
Gilles Peskined71539f2020-11-25 18:17:17 +0100159src/%.o : src/%.c $(TEST_OBJS_DEPS)
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200160 echo " CC $<"
161 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
162
Steven Cooremana70d5882020-07-16 20:26:18 +0200163src/drivers/%.o : src/drivers/%.c
164 echo " CC $<"
165 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
166
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000167C_FILES := $(addsuffix .c,$(APPS))
Gilles Peskine1d7cc082022-11-10 19:50:34 +0100168c: $(C_FILES)
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000169
Azim Khan27a35e72018-06-29 12:39:19 +0100170# Wildcard target for test code generation:
171# A .c file is generated for each .data file in the suites/ directory. Each .c
172# file depends on a .data and .function file from suites/ directory. Following
173# nameing convention is followed:
174#
175# C file | Depends on
176#-----------------------------------------------------------------------------
177# foo.c | suites/foo.function suites/foo.data
178# foo.bar.c | suites/foo.function suites/foo.bar.data
179#
180# Note above that .c and .data files have same base name.
181# However, corresponding .function file's base name is the word before first
182# dot in .c file's base name.
183#
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000184.SECONDEXPANSION:
Azim Khan27a35e72018-06-29 12:39:19 +0100185%.c: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data 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 +0100186 echo " Gen $@"
Azim Khan27a35e72018-06-29 12:39:19 +0100187 $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100188 -d suites/$*.data \
Azim Khan1de892b2017-06-09 15:02:36 +0100189 -t suites/main_test.function \
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100190 -p suites/host_test.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100191 -s suites \
Azim Khane3b26af2018-06-29 02:36:57 +0100192 --helpers-file suites/helpers.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100193 -o .
Paul Bakker286bf3c2013-04-08 18:09:51 +0200194
Paul Bakker286bf3c2013-04-08 18:09:51 +0200195
Gilles Peskined71539f2020-11-25 18:17:17 +0100196$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS)
Manuel Pégourié-Gonnard78ec2b02015-07-08 22:12:06 +0100197 echo " CC $<"
Ronald Cronf5ea29a2020-06-19 10:42:29 +0200198 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
Paul Bakker286bf3c2013-04-08 18:09:51 +0200199
Paul Bakker0049c2f2009-07-11 19:15:43 +0000200clean:
Paul Bakker62f88dc2012-05-10 21:26:28 +0000201ifndef WINDOWS
Gilles Peskined0254222021-09-14 11:28:22 +0200202 rm -rf $(BINARIES) *.c *.datax
Steven Cooremana70d5882020-07-16 20:26:18 +0200203 rm -f src/*.o src/drivers/*.o src/libmbed*
Gilles Peskined71539f2020-11-25 18:17:17 +0100204 rm -f include/test/instrument_record_status.h
Ronald Cron72b25da2021-04-28 18:29:24 +0200205 rm -rf libtestdriver1
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200206else
Darryl Green6c0f94c2018-10-17 16:12:33 +0100207 if exist *.c del /Q /F *.c
208 if exist *.exe del /Q /F *.exe
209 if exist *.datax del /Q /F *.datax
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200210 if exist src/*.o del /Q /F src/*.o
Steven Cooremana70d5882020-07-16 20:26:18 +0200211 if exist src/drivers/*.o del /Q /F src/drivers/*.o
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200212 if exist src/libmbed* del /Q /F src/libmed*
Gilles Peskined71539f2020-11-25 18:17:17 +0100213 if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h
Azim Khan27a35e72018-06-29 12:39:19 +0100214endif
Paul Bakker0049c2f2009-07-11 19:15:43 +0000215
Gilles Peskine687d1ab2021-04-22 01:01:56 +0200216neat: clean
217ifndef WINDOWS
218 rm -f $(GENERATED_FILES)
219else
220 for %f in ($(subst /,\,$(GENERATED_FILES))) if exist %f del /Q /F %f
221endif
222
Gilles Peskineac372cc2018-11-29 10:15:06 +0000223# Test suites caught by SKIP_TEST_SUITES are built but not executed.
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000224check: $(BINARIES)
Gilles Peskine396853a2021-09-20 18:57:55 +0200225 perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES)
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200226
227test: check
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000228
Ronald Cron72b25da2021-04-28 18:29:24 +0200229# Generate test library
230
231# Perl code that is executed to transform each original line from a library
232# source file into the corresponding line in the test driver copy of the
233# library. Add a LIBTESTDRIVER1_/libtestdriver1_ to mbedtls_xxx and psa_xxx
234# symbols.
235define libtestdriver1_rewrite :=
236 s!^(\s*#\s*include\s*[\"<])(mbedtls|psa)/!$${1}libtestdriver1/include/$${2}/!; \
237 next if /^\s*#\s*include/; \
238 s/\b(?=MBEDTLS_|PSA_)/LIBTESTDRIVER1_/g; \
239 s/\b(?=mbedtls_|psa_)/libtestdriver1_/g;
240endef
241
242libtestdriver1.a:
243 # Copy the library and fake a 3rdparty Makefile include.
244 rm -Rf ./libtestdriver1
245 mkdir ./libtestdriver1
246 cp -Rf ../library ./libtestdriver1
247 cp -Rf ../include ./libtestdriver1
248 cp -Rf ../scripts ./libtestdriver1
249 mkdir ./libtestdriver1/3rdparty
250 touch ./libtestdriver1/3rdparty/Makefile.inc
251
252 # Set the test driver base (minimal) configuration.
253 cp ./include/test/drivers/config_test_driver.h ./libtestdriver1/include/mbedtls/mbedtls_config.h
254
255 # Set the PSA cryptography configuration for the test library.
256 # It is set from the copied include/psa/crypto_config.h of the Mbed TLS
257 # library the test library is intended to be linked with extended by
258 # ./include/test/drivers/crypto_config_test_driver_extension.h to
259 # mirror the PSA_ACCEL_* macros.
260 mv ./libtestdriver1/include/psa/crypto_config.h ./libtestdriver1/include/psa/crypto_config.h.bak
261 head -n -1 ./libtestdriver1/include/psa/crypto_config.h.bak > ./libtestdriver1/include/psa/crypto_config.h
262 cat ./include/test/drivers/crypto_config_test_driver_extension.h >> ./libtestdriver1/include/psa/crypto_config.h
263 echo "#endif /* PSA_CRYPTO_CONFIG_H */" >> ./libtestdriver1/include/psa/crypto_config.h
264
265 # Prefix MBEDTLS_* PSA_* symbols with LIBTESTDRIVER1_ as well as
266 # mbedtls_* psa_* symbols with libtestdriver1_ to avoid symbol clash
267 # when this test driver library is linked with the Mbed TLS library.
268 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/library/*.[ch]
269 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/include/*/*.h
270
271 $(MAKE) -C ./libtestdriver1/library CFLAGS="-I../../ $(CFLAGS)" LDFLAGS="$(LDFLAGS)" libmbedcrypto.a
272 cp ./libtestdriver1/library/libmbedcrypto.a ../library/libtestdriver1.a
273
Gilles Peskine51681552019-05-20 19:35:37 +0200274ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Ronald Cron02c78b72020-05-27 09:22:32 +0200275include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile
Gilles Peskined71539f2020-11-25 18:17:17 +0100276 echo " Gen $@"
Gilles Peskine51681552019-05-20 19:35:37 +0200277 sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
278endif