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_PSA_DATA_FILES := $(patsubst tests/%,%,$(shell \ 77 $(PYTHON) scripts/generate_psa_tests.py --list || \ 78 echo FAILED \ 79)) 80ifeq ($(GENERATED_PSA_DATA_FILES),FAILED) 81$(error "$(PYTHON) scripts/generate_psa_tests.py --list" failed) 82endif 83GENERATED_FILES := $(GENERATED_PSA_DATA_FILES) $(GENERATED_BIGNUM_DATA_FILES) 84generated_files: $(GENERATED_FILES) 85 86# 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. 89# 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. 92.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 95generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_common.py 96generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_core.py 97generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_mod_raw.py 98generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_mod.py 99generated_bignum_test_data: ../scripts/mbedtls_dev/test_case.py 100generated_bignum_test_data: ../scripts/mbedtls_dev/test_data_generation.py 101generated_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 106generated_psa_test_data: scripts/generate_psa_tests.py 107generated_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 111generated_psa_test_data: ../scripts/mbedtls_dev/test_data_generation.py 112## 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. 117#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: 122 echo " Gen $(GENERATED_PSA_DATA_FILES) ..." 123 $(PYTHON) scripts/generate_psa_tests.py 124 125# 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. 128DATA_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. 131DATA_FILES += $(filter-out $(DATA_FILES),$(GENERATED_FILES)) 132APPS = $(basename $(subst suites/,,$(DATA_FILES))) 133 134# Construct executable name by adding OS specific suffix $(EXEXT). 135BINARIES := $(addsuffix $(EXEXT),$(APPS)) 136 137.SILENT: 138 139.PHONY: all check test clean 140 141all: $(BINARIES) 142 143$(MBEDLIBS): 144 $(MAKE) -C ../library 145 146MBEDTLS_TEST_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c src/drivers/*.c)) 147 148mbedtls_test: $(MBEDTLS_TEST_OBJS) 149 150TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h) 151ifdef RECORD_PSA_STATUS_COVERAGE_LOG 152# 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. 155TEST_OBJS_DEPS += include/test/instrument_record_status.h 156endif 157 158# Rule to compile common test C files in src folder 159src/%.o : src/%.c $(TEST_OBJS_DEPS) 160 echo " CC $<" 161 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $< 162 163src/drivers/%.o : src/drivers/%.c 164 echo " CC $<" 165 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $< 166 167C_FILES := $(addsuffix .c,$(APPS)) 168 169# Wildcard target for test code generation: 170# A .c file is generated for each .data file in the suites/ directory. Each .c 171# file depends on a .data and .function file from suites/ directory. Following 172# nameing convention is followed: 173# 174# C file | Depends on 175#----------------------------------------------------------------------------- 176# foo.c | suites/foo.function suites/foo.data 177# foo.bar.c | suites/foo.function suites/foo.bar.data 178# 179# Note above that .c and .data files have same base name. 180# However, corresponding .function file's base name is the word before first 181# dot in .c file's base name. 182# 183.SECONDEXPANSION: 184%.c: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data scripts/generate_test_code.py suites/helpers.function suites/main_test.function suites/host_test.function 185 echo " Gen $@" 186 $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \ 187 -d suites/$*.data \ 188 -t suites/main_test.function \ 189 -p suites/host_test.function \ 190 -s suites \ 191 --helpers-file suites/helpers.function \ 192 -o . 193 194 195$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS) 196 echo " CC $<" 197 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ 198 199clean: 200ifndef WINDOWS 201 rm -rf $(BINARIES) *.c *.datax 202 rm -f src/*.o src/drivers/*.o src/libmbed* 203 rm -f include/test/instrument_record_status.h 204 rm -f include/alt-extra/*/*_alt.h 205 rm -rf libtestdriver1 206else 207 if exist *.c del /Q /F *.c 208 if exist *.exe del /Q /F *.exe 209 if exist *.datax del /Q /F *.datax 210 if exist src/*.o del /Q /F src/*.o 211 if exist src/drivers/*.o del /Q /F src/drivers/*.o 212 if exist src/libmbed* del /Q /F src/libmed* 213 if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h 214endif 215 216neat: 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 223# Test suites caught by SKIP_TEST_SUITES are built but not executed. 224check: $(BINARIES) 225 perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES) 226 227test: check 228 229# Generate variants of some headers for testing 230include/alt-extra/%_alt.h: ../include/%.h 231 perl -p -e 's/^(# *(define|ifndef) +\w+_)H\b/$${1}ALT_H/' $< >$@ 232 233# Generate test library 234 235# Perl code that is executed to transform each original line from a library 236# source file into the corresponding line in the test driver copy of the 237# library. Add a LIBTESTDRIVER1_/libtestdriver1_ to mbedtls_xxx and psa_xxx 238# symbols. 239define libtestdriver1_rewrite := 240 s!^(\s*#\s*include\s*[\"<])(mbedtls|psa)/!$${1}libtestdriver1/include/$${2}/!; \ 241 next if /^\s*#\s*include/; \ 242 s/\b(?=MBEDTLS_|PSA_)/LIBTESTDRIVER1_/g; \ 243 s/\b(?=mbedtls_|psa_)/libtestdriver1_/g; 244endef 245 246libtestdriver1.a: 247 # Copy the library and fake a 3rdparty Makefile include. 248 rm -Rf ./libtestdriver1 249 mkdir ./libtestdriver1 250 cp -Rf ../library ./libtestdriver1 251 cp -Rf ../include ./libtestdriver1 252 cp -Rf ../scripts ./libtestdriver1 253 mkdir ./libtestdriver1/3rdparty 254 touch ./libtestdriver1/3rdparty/Makefile.inc 255 256 # Set the test driver base (minimal) configuration. 257 cp ./include/test/drivers/config_test_driver.h ./libtestdriver1/include/mbedtls/mbedtls_config.h 258 259 # Set the PSA cryptography configuration for the test library. 260 # It is set from the copied include/psa/crypto_config.h of the Mbed TLS 261 # library the test library is intended to be linked with extended by 262 # ./include/test/drivers/crypto_config_test_driver_extension.h to 263 # mirror the PSA_ACCEL_* macros. 264 mv ./libtestdriver1/include/psa/crypto_config.h ./libtestdriver1/include/psa/crypto_config.h.bak 265 head -n -1 ./libtestdriver1/include/psa/crypto_config.h.bak > ./libtestdriver1/include/psa/crypto_config.h 266 cat ./include/test/drivers/crypto_config_test_driver_extension.h >> ./libtestdriver1/include/psa/crypto_config.h 267 echo "#endif /* PSA_CRYPTO_CONFIG_H */" >> ./libtestdriver1/include/psa/crypto_config.h 268 269 # Prefix MBEDTLS_* PSA_* symbols with LIBTESTDRIVER1_ as well as 270 # mbedtls_* psa_* symbols with libtestdriver1_ to avoid symbol clash 271 # when this test driver library is linked with the Mbed TLS library. 272 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/library/*.[ch] 273 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/include/*/*.h 274 275 $(MAKE) -C ./libtestdriver1/library CFLAGS="-I../../ $(CFLAGS)" LDFLAGS="$(LDFLAGS)" libmbedcrypto.a 276 cp ./libtestdriver1/library/libmbedcrypto.a ../library/libtestdriver1.a 277 278ifdef RECORD_PSA_STATUS_COVERAGE_LOG 279include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile 280 echo " Gen $@" 281 sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p' 282endif 283