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