# Host tests for the crash archive, the CAN collector and the seal.
#
#   nix-shell -p gcc gnumake mbedtls --run 'cd tests/crash && make && ./test_crash'
#
# main/crash_ring.c, main/crash_collect.c and main/crash_seal.c are compiled
# straight from the firmware source, not copied, so the tests cannot drift
# from what ships -- the same arrangement as tests/hub_creds and
# main/script/test. What is replaced is the SoC underneath: flash, NVS, the
# core dump driver, a clock, the CAN bus and the scheduler.
#
# mbedTLS is the real library. The seal is the one part of this whose
# correctness is about bytes rather than control flow, and the test opens
# what the firmware sealed using an independent implementation of the format
# -- a stand-in for GCM would prove nothing.
#
# datatypes.h, comm_can.h and buffer.c are the firmware's own and compile
# here unchanged. That is deliberate for the first two: the packet id
# (COMM_GET_CRASH_REPORT), the hardware types and CAN_STATUS_MSGS_TO_STORE
# are exactly the values that have to be right, and shadowing them would
# make the tests agree with themselves instead of with the firmware.

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

MAIN = ../../main

INCLUDES = -Ishim -I$(MAIN)

LIBS = -lmbedtls -lmbedx509 -lmbedcrypto

SRCS = test_crash.c shim/shim.c \
	$(MAIN)/crash_ring.c $(MAIN)/crash_collect.c $(MAIN)/crash_seal.c \
	$(MAIN)/buffer.c

HDRS = $(wildcard shim/*.h) $(wildcard shim/freertos/*.h) \
	$(MAIN)/crash_ring.h $(MAIN)/crash_collect.h $(MAIN)/crash_seal.h

all: test_crash

test_crash: $(SRCS) $(HDRS)
	$(CC) $(CFLAGS) $(SAN) $(INCLUDES) -o $@ $(SRCS) $(LIBS)

run: test_crash
	./test_crash

clean:
	rm -f test_crash

.PHONY: all run clean
