#
PROVE=/usr/bin/prove
TESTS=simple.pl
PARSER_DIR=..
PARSER_BIN=apparmor_parser
PARSER=$(PARSER_DIR)/$(PARSER_BIN)
# parser.conf to use in tests. Note that some test scripts have the parser options hardcoded, so passing PARSER_ARGS=... is not enough to override it.
PARSER_ARGS=--config-file=./parser.conf
PROVE_ARG=-f --directives -j2

ifeq ($(VERBOSE),1)
  PROVE_ARG+=-v
  PYTEST_ARG = -v
else
  VERBOSE=
endif

all: tests

.PHONY: tests error_output gen_dbus gen_xtrans parser_sanity caching minimize equality dirtest valgrind
tests: error_output caching minimize equality dirtest parser_sanity

GEN_TRANS_DIRS=simple_tests/generated_x/ simple_tests/generated_perms_leading/ simple_tests/generated_perms_safe/ simple_tests/generated_dbus

gen_xtrans: $(GEN_TRANS_DIRS)
	./gen-xtrans.py

$(GEN_TRANS_DIRS):
	mkdir $@

gen_dbus: $(GEN_TRANS_DIRS)
	./gen-dbus.py

error_output: $(PARSER)
	LANG=C ./errors.py -p "$(PARSER)" $(PYTEST_ARG)

parser_sanity: $(PARSER) gen_xtrans gen_dbus
	$(Q)LANG=C APPARMOR_PARSER="$(PARSER)" ${PROVE} ${PROVE_ARG} ${TESTS}

# use this target for faster manual testing if you don't want/need to test all the profiles generated by gen-*.py
parser_sanity-no-gen: clean $(PARSER)
	@echo WARNING: not creating the profiles using the gen-*.py scripts
	$(Q)LANG=C APPARMOR_PARSER="$(PARSER)" ${PROVE} ${PROVE_ARG} ${TESTS}

caching: $(PARSER)
	LANG=C ./caching.py -p "$(PARSER)" $(PYTEST_ARG)

minimize: $(PARSER)
	LANG=C APPARMOR_PARSER="$(PARSER) $(PARSER_ARGS)" ./minimize.sh

equality: $(PARSER)
	LANG=C APPARMOR_PARSER="$(PARSER) $(PARSER_ARGS)" ./equality.sh

dirtest: $(PARSER)
	LANG=C APPARMOR_PARSER="$(PARSER) $(PARSER_ARGS)" ./dirtest.sh

valgrind: $(PARSER) gen_xtrans gen_dbus
	LANG=C ./valgrind_simple.py -p "$(PARSER) $(PARSER_ARGS)" -v simple_tests

$(PARSER):
	$(MAKE) -C $(PARSER_DIR) $(PARSER_BIN)

clean:
	find $(GEN_TRANS_DIRS) -type f | xargs rm -f
	rm -f gmon.out
