# Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
# Copyright 2015, Daniel Axtens, IBM Corporation
# zlib license, see zlib.h

CC=
CFLAGS=
EXE=
SRCDIR=
SRCTOP=
LIBNAME=
TEST_LDFLAGS=-L.. ../$(LIBNAME).a
WITH_FUZZERS=

COMPATTESTS =
QEMU_RUN=
QEMU_VER:=$(shell command -v $(QEMU_RUN) --version 2> /dev/null)

all: oldtests cvetests $(COMPATTESTS) fuzzer ghtests

oldtests: #set by ../configure
check_cross_dep:
ifneq (,$(QEMU_RUN))
ifeq (,$(QEMU_VER))
	$(error "You need QEMU to run tests on non-native platform")
endif
endif

ALL_SRC_FILES := $(wildcard ../*)

# Only check the fuzzer when it is a stand-alone executable.
ifneq (,$(LIB_FUZZING_ENGINE))
fuzzer:
else
 ifeq (0,$(WITH_FUZZERS))
fuzzer:
 else
fuzzer:
	@${QEMU_RUN} ../checksum_fuzzer$(EXE) $(ALL_SRC_FILES) && \
	${QEMU_RUN} ../compress_fuzzer$(EXE) $(ALL_SRC_FILES) && \
	${QEMU_RUN} ../example_small_fuzzer$(EXE) $(ALL_SRC_FILES) && \
	${QEMU_RUN} ../example_large_fuzzer$(EXE) $(ALL_SRC_FILES) && \
	${QEMU_RUN} ../example_flush_fuzzer$(EXE) $(ALL_SRC_FILES) && \
	${QEMU_RUN} ../example_dict_fuzzer$(EXE) $(ALL_SRC_FILES) && \
	${QEMU_RUN} ../minigzip_fuzzer$(EXE) $(ALL_SRC_FILES)
 endif
endif

teststatic: check_cross_dep
	@TMPST=tmpst_$$$$; \
	HELLOST=tmphellost_$$$$; \
	if echo hello world | ${QEMU_RUN} ../minigzip$(EXE) > $$HELLOST && ${QEMU_RUN} ../minigzip$(EXE) -d < $$HELLOST && ${QEMU_RUN} ../example$(EXE) $$TMPST && ${QEMU_RUN} ../adler32_test$(EXE) && ${QEMU_RUN} ../crc32_test$(EXE); then \
	  echo '		*** zlib test OK ***'; \
	else \
	  echo '		*** zlib test FAILED ***'; exit 1; \
	fi; \
	rm -f $$TMPST $$HELLOST

testshared: check_cross_dep
	@LD_LIBRARY_PATH=`pwd`/..:$(LD_LIBRARY_PATH) ; export LD_LIBRARY_PATH; \
	LD_LIBRARYN32_PATH=`pwd`/..:$(LD_LIBRARYN32_PATH) ; export LD_LIBRARYN32_PATH; \
	DYLD_LIBRARY_PATH=`pwd`/..:$(DYLD_LIBRARY_PATH) ; export DYLD_LIBRARY_PATH; \
	SHLIB_PATH=`pwd`/..:$(SHLIB_PATH) ; export SHLIB_PATH; \
	TMPSH=tmpsh_$$$$; \
	HELLOSH=tmphellosh_$$$$; \
	if echo hello world | ${QEMU_RUN} ../minigzipsh$(EXE) > $$HELLOSH && ${QEMU_RUN} ../minigzipsh$(EXE) -d < $$HELLOSH && ${QEMU_RUN} ../examplesh$(EXE) $$TMPSH && ${QEMU_RUN} ../adler32_testsh$(EXE) && ${QEMU_RUN} ../crc32_testsh$(EXE); then \
	  echo '		*** zlib shared test OK ***'; \
	else \
	  echo '		*** zlib shared test FAILED ***'; exit 1; \
	fi; \
	rm -f $$TMPSH $$HELLOSH

cvetests: testCVEinputs

# Tests requiring zlib-ng to be built with --zlib-compat
compattests: testCVE-2003-0107

testCVEinputs: check_cross_dep
	@EXE=$(EXE) QEMU_RUN="${QEMU_RUN}" $(SRCDIR)/testCVEinputs.sh

testCVE-2003-0107: CVE-2003-0107$(EXE) check_cross_dep
	@if ${QEMU_RUN} ./CVE-2003-0107$(EXE); then \
	  echo '                *** zlib not vulnerable to CVE-2003-0107 ***'; \
	else \
	  echo '                *** zlib VULNERABLE to CVE-2003-0107 ***'; exit 1; \
	fi

CVE-2003-0107.o: $(SRCDIR)/CVE-2003-0107.c
	$(CC) $(CFLAGS) -I.. -I$(SRCTOP) -c -o $@ $(SRCDIR)/CVE-2003-0107.c

CVE-2003-0107$(EXE): CVE-2003-0107.o
	$(CC) $(CFLAGS) -o $@ CVE-2003-0107.o $(TEST_LDFLAGS)

.PHONY: ghtests
ghtests: testGH-361 testGH-364 testGH-751 testGH-1235

.PHONY: testGH-361
testGH-361:
	$(QEMU_RUN) ../minigzip$(EXE) -4 <$(SRCDIR)/GH-361/test.txt >/dev/null

switchlevels$(EXE): $(SRCDIR)/switchlevels.c
	$(CC) $(CFLAGS) -I.. -I$(SRCTOP) -o $@ $< $(TEST_LDFLAGS)

.PHONY: testGH-364
testGH-364: switchlevels$(EXE)
	$(QEMU_RUN) ./switchlevels$(EXE) 1 5 9 3 <$(SRCDIR)/GH-364/test.bin >/dev/null

.PHONY: testGH-751
testGH-751:
	$(QEMU_RUN) ../minigzip$(EXE) <$(SRCDIR)/GH-751/test.txt | $(QEMU_RUN) ../minigzip$(EXE) -d >/dev/null

gh1235$(EXE): $(SRCDIR)/gh1235.c
	$(CC) $(CFLAGS) -I.. -I$(SRCTOP) -o $@ $< $(TEST_LDFLAGS)

.PHONY: testGH-1235
testGH-1235: gh1235$(EXE)
	$(QEMU_RUN) ./gh1235$(EXE)

clean:
	rm -f *.o *.gcda *.gcno *.gcov
	rm -f CVE-2003-0107$(EXE) switchlevels$(EXE) gh1235$(EXE)

distclean:
	rm -f Makefile
