mydir=tests$(S)mkeystash_compat
BUILDTOP=$(REL)..$(S)..

RUN_SETUP = @KRB5_RUN_ENV@ KRB5_KDC_PROFILE=kdc.conf KRB5_CONFIG=krb5.conf

OBJS = bigendian.o
SRCS = $(srcdir)/bigendian.c

TEST_DB = ./testdb
TEST_REALM = FOO.TEST.REALM
TEST_MKEY = footes

KADMIN_OPTS= -d $(TEST_DB) -r $(TEST_REALM)
KDB_OPTS= $(KADMIN_OPTS) -P $(TEST_MKEY)

check-unix:: mkeystash_check

bigendian: $(OUTPRE)bigendian.$(OBJEXT) $(SUPPORT_DEPLIB)
	$(CC_LINK) $(ALL_CFLAGS) -o bigendian $(OUTPRE)bigendian.$(OBJEXT)

kdc.conf: Makefile
	rm -rf kdc.conf
	@echo "[realms]" > kdc.conf
	@echo "$(TEST_REALM) = {" >> kdc.conf
	@echo "  master_key_type = des3-cbc-sha1" >> kdc.conf
	@echo "  key_stash_file = `pwd`/stash_file" >> kdc.conf
	@echo "}" >> kdc.conf

krb5.conf: Makefile
	cat $(top_srcdir)/config-files/krb5.conf > krb5.new
	echo "[dbmodules]" >> krb5.new
	echo " db_module_dir = $(BUILDTOP)/plugins/kdb" >> krb5.new
	mv krb5.new krb5.conf

# Verify that the mkey stash code is backward compat with old/non-keytab stashfile format
mkeystash_check: kdc.conf krb5.conf bigendian
	$(RM) $(TEST_DB)* stash_file
	$(RUN_SETUP) $(VALGRIND) ../../kadmin/dbutil/kdb5_util $(KDB_OPTS) create -s -W
	# overwrite keytab stash file with old format stash, depends on endianness of current test system
	./bigendian && cp $(srcdir)/old_stash_bendian stash_file || cp $(srcdir)/old_stash_lendian stash_file
	# getprinc will fail if old stash file can not be read
	$(RUN_SETUP) $(VALGRIND) ../../kadmin/cli/kadmin.local $(KADMIN_OPTS) -q 'getprinc K/M'
	$(RUN_SETUP) $(VALGRIND) ../../kadmin/dbutil/kdb5_util $(KDB_OPTS) destroy -f
	$(RM) $(TEST_DB)* stash_file

clean::
	$(RM) kdc.conf krb5.conf bigendian.$(OBJEXT) bigendian

