# Host test for what a friend may send over the hub (main/hub_policy.c).
#
#   nix develop --command sh -c 'cd tests/hub_policy && make && ./test_hub_policy'
#
# main/hub_policy.c is compiled straight from the firmware source. It reads
# only datatypes.h and hub_creds.h, both of which compile on the host as they
# are, so there is no shim.
#
# comm_ids.h is every name in datatypes.h's COMM_PACKET_ID, extracted here
# and compiled against datatypes.h itself, so the compiler gives each its
# value. That list is what lets the test fail when upstream adds an ID and
# nobody has decided whether a friend may send it.

CFLAGS += -O1 -g -std=c11 -D_POSIX_C_SOURCE=200809L -Wall -Wextra
SAN ?= -fsanitize=address,undefined -fno-omit-frame-pointer

MAIN = ../../main

SRCS = test_hub_policy.c $(MAIN)/hub_policy.c

all: test_hub_policy

comm_ids.h: $(MAIN)/datatypes.h
	awk '/^typedef enum/ { buf = "" } { buf = buf "\n" $$0 } \
		/} COMM_PACKET_ID;/ { print buf; exit }' $< \
		| grep -oE '^[[:space:]]*COMM_[A-Z0-9_]+' | sed 's/^[[:space:]]*//' \
		| awk '{ printf "\t{ \"%s\", %s },\n", $$0, $$0 }' > $@

test_hub_policy: $(SRCS) comm_ids.h $(MAIN)/hub_policy.h $(MAIN)/datatypes.h
	$(CC) $(CFLAGS) $(SAN) -I$(MAIN) -I. -o $@ $(SRCS)

clean:
	rm -f test_hub_policy comm_ids.h

.PHONY: all clean
