# Host tests for the OTA rollback timeout.
#
#   nix-shell -p gcc gnumake --run 'cd tests/ota_rollback && make && ./test_ota_rollback'
#
# main/ota_rollback.c is compiled straight from the firmware source, not
# copied. What is replaced is the SDK underneath: the OTA state machine, a
# restart that returns instead of rebooting, and a scheduler that runs the
# task inline with a clock that costs nothing -- which is what turns a
# three-minute timeout into a test that runs instantly.
#
# No mbedTLS and no NVS here, so this needs nothing but a compiler.

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)

SRCS = test_ota_rollback.c shim/shim.c $(MAIN)/ota_rollback.c

HDRS = $(wildcard shim/*.h) $(wildcard shim/freertos/*.h) \
	$(MAIN)/ota_rollback.h

all: test_ota_rollback

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

run: test_ota_rollback
	./test_ota_rollback

clean:
	rm -f test_ota_rollback

.PHONY: all run clean
