#
# Makefile to run tests with system binaries
# USE: make -f Makefile.localtest tests CRYPTSETUP_PATH=/sbin
#      (append TESTSUITE_NOSKIP=y to avoid treating skipped tests as success)
#
CPPFLAGS=-I../lib/ -I../lib/luks1 -DHAVE_DECL_DM_TASK_RETRY_REMOVE -DKERNEL_KEYRING \
	-DHAVE_SYS_SYSMACROS_H -DNO_CRYPTSETUP_PATH
CFLAGS=-O2 -g -Wall -D_GNU_SOURCE
LDLIBS=-lcryptsetup -ldevmapper
TESTS=$(wildcard *-test *-test2) api-test api-test-2 all-symbols-test unit-utils-crypt-test
TESTS_UTILS=differ unit-utils-io unit-wipe

ifneq ($(RUN_SSH_PLUGIN_TEST),)
TESTS += ssh-test-plugin
endif

ifneq ($(RUN_SYSTEMD_PLUGIN_TEST),)
TESTS += systemd-test-plugin
TESTS_UTILS += fake_systemd_tpm_path.so
endif

check-programs: $(TESTS_UTILS) $(TESTS)

differ: differ.o
	$(CC) -o $@ $^

api-test: api-test.o test_utils.o
	$(CC) -o $@ $^ $(LDLIBS)

api-test-2: api-test-2.o test_utils.o
	$(CC) -o $@ $^ $(LDLIBS)

unit-wipe: unit-wipe.o
	$(CC) -o $@ $^ $(LDLIBS)

unit-utils-io: unit-utils-io.o ../lib/utils_io.o
	$(CC) -o $@ $^

unit-utils-crypt-test: unit-utils-crypt.o ../lib/utils_crypt.o
	$(CC) -o $@ $^ $(LDLIBS)

test-symbols-list.h: generate-symbols-list
	./generate-symbols-list ../lib/libcryptsetup.sym > test-symbols-list.h

all-symbols-test.o: test-symbols-list.h
	$(CC) -c $*.c

all-symbols-test: all-symbols-test.o
	$(CC) -o $@ $^ -ldl

fake_systemd_tpm_path.so: fake_systemd_tpm_path.c
	$(CC) -fPIC -shared -D_GNU_SOURCE -o fake_systemd_tpm_path.so fake_systemd_tpm_path.c

tests: $(TESTS_UTILS) $(TESTS)
	@for test in $(sort $(TESTS)); do \
		echo [$$test]; \
		./$$test; \
		[ $(if $(TESTSUITE_NOSKIP),,$$? -ne 77 -a) $$? -ne 0 ] && exit 1; \
		true; \
	done;

clean:
	rm -f *.o $(TESTS_UTILS) api-test api-test-2 unit-utils-crypt-test \
		all-symbols-test test-symbols-list.h

.PHONY: clean
