# Host tests for the script container parser.
#
# The parser is compiled straight from the firmware source, not copied, so the
# test cannot drift from what ships. It has no firmware dependencies, which is
# the point: untrusted-input parsing belongs where it can be fuzzed and run
# under sanitizers.

CFLAGS += -O1 -g -std=c11 -Wall -Wextra -Wconversion -Wsign-conversion
SAN ?= -fsanitize=address,undefined -fno-omit-frame-pointer

LUA_DIR ?= ../../lua
# Same configuration the firmware builds Lua with, so the engine under test is
# the engine that ships. LUA_32BITS is set in the vendored luaconf.h rather
# than here, because Lua defines it itself and a command-line define collides.
GFX_DIR ?= ../../lispBM/utils
DISP_DIR ?= ../../display
# ttf_backend.h lives in the lispBM tree but depends on nothing from the
# interpreter: it is the glyph format the prepared fonts are in, which both
# engines read.
TTF_DIR ?= ../../lispBM/src/extensions
LUA_CFLAGS = -I$(LUA_DIR) -I$(GFX_DIR) -I$(DISP_DIR) -I$(TTF_DIR) -DLUA_USE_APICHECK
LUA_SRCS = $(wildcard $(LUA_DIR)/*.c)

.PHONY: all run clean fuzz
all: run

test_script_pack: test_script_pack.c ../script_pack.c ../script_pack.h
	$(CC) $(CFLAGS) $(SAN) -o $@ test_script_pack.c ../script_pack.c

test_pkg_table: test_pkg_table.c ../pkg_table.c ../pkg_table.h
	$(CC) $(CFLAGS) $(SAN) -I.. -o $@ test_pkg_table.c ../pkg_table.c

# Lua itself is compiled without the warning flags used for our own code:
# it is vendored third-party source and we do not edit it.
# The headers are a dependency, not just the sources. LUA_32BITS lives in the
# vendored luaconf.h, so an archive built before that was set has 64-bit
# integers and 8-byte floats while our own translation units have 32-bit ones.
# Nothing fails to link: integer arguments still work and every float read
# through luaL_checknumber comes back as zero. That cost an afternoon once.
LUA_HDRS = $(wildcard $(LUA_DIR)/*.h)

lua_objs.a: $(LUA_SRCS) $(LUA_HDRS)
	@mkdir -p .luaobj
	@for f in $(LUA_SRCS); do \
		$(CC) -O1 -g -std=c11 $(LUA_CFLAGS) $(SAN) -c $$f \
			-o .luaobj/`basename $$f .c`.o || exit 1; \
	done
	@$(AR) rcs $@ .luaobj/*.o

# tinygfx and tjpgd come straight from the tree: the drawing under test is the
# drawing that ships, and it needs nothing from either interpreter.
test_script_lua: test_script_lua.c ../script_lua.c ../script_pack.c \
		../lua_vesc_disp.c ../lua_vesc_color.c \
		$(DISP_DIR)/disp_backend.c $(DISP_DIR)/color_core.c \
		$(GFX_DIR)/tinygfx.c $(GFX_DIR)/tjpgd.c lua_objs.a
	$(CC) $(CFLAGS) -Wno-conversion -Wno-sign-conversion $(LUA_CFLAGS) $(SAN) \
		-o $@ test_script_lua.c ../script_lua.c ../script_pack.c \
		../lua_vesc_disp.c ../lua_vesc_color.c \
		$(DISP_DIR)/disp_backend.c $(DISP_DIR)/color_core.c \
		$(GFX_DIR)/tinygfx.c $(GFX_DIR)/tjpgd.c lua_objs.a -lm

# The fixture is packed by the real tool, so the Python writer and the C
# reader are checked against each other on every run.
packed.bin: fixtures/main.lua fixtures/mod.lua fixtures/pkg/sub.lua ../../../tools/luapack.py
	python3 ../../../tools/luapack.py fixtures/main.lua -o $@

run: test_script_pack test_pkg_table test_script_lua packed.bin check-table-fixture
	./test_script_pack
	./test_pkg_table fixtures/pkg_table.bin
	python3 ../../../tools/luapack.py --selftest
	./test_script_lua packed.bin

# Property fuzzing: any byte string at all must either parse or be refused,
# and every import it reports must be in bounds. Needs clang.
# Renders a packed Lua script to a PPM with the real drawing code, so a view
# can be compared against the goldens in dash_common/test without a board.
render_host: render_host.c ../script_lua.c ../script_pack.c \
		../lua_vesc_disp.c ../lua_vesc_color.c ../lua_vesc_font.c \
		$(DISP_DIR)/disp_backend.c $(DISP_DIR)/color_core.c \
		$(DISP_DIR)/ttf_font.c ../../buffer.c \
		$(GFX_DIR)/tinygfx.c $(GFX_DIR)/tjpgd.c lua_objs.a
	$(CC) $(CFLAGS) -Wno-conversion -Wno-sign-conversion $(LUA_CFLAGS) $(SAN) \
		-o $@ render_host.c ../script_lua.c ../script_pack.c \
		../lua_vesc_disp.c ../lua_vesc_color.c ../lua_vesc_font.c \
		$(DISP_DIR)/disp_backend.c $(DISP_DIR)/color_core.c \
		$(DISP_DIR)/ttf_font.c ../../buffer.c \
		$(GFX_DIR)/tinygfx.c $(GFX_DIR)/tjpgd.c lua_objs.a -lm

fuzz_script_pack: fuzz_script_pack.c ../script_pack.c ../script_pack.h
	clang $(CFLAGS) -fsanitize=fuzzer,address,undefined \
		-o $@ fuzz_script_pack.c ../script_pack.c

fuzz: fuzz_script_pack
	./fuzz_script_pack -max_total_time=$(or $(FUZZ_SECONDS),30) -print_final_stats=1

clean:
	rm -f test_pkg_table pkg_table_fixture.bin test_script_pack test_script_lua fuzz_script_pack render_host lua_objs.a packed.bin
	rm -rf crash-* leak-* timeout-* .luaobj

# The package table fixture, built by luapack.py --table and parsed by
# test_pkg_table here and by bldc/tests/pkg_table, which carries a copy. The
# rule rebuilds it into a temporary file and compares, so a change to either
# side of the format that the other did not get fails here.
TABLE_SRC = fixtures/table
pkg_table_fixture.bin: $(TABLE_SRC)/legacy.lisp $(TABLE_SRC)/tone_seq.lua $(TABLE_SRC)/logui.lisp ../../../tools/luapack.py
	python3 ../../../tools/luapack.py --lisp $(TABLE_SRC)/legacy.lisp -o .legacy.pkg >/dev/null
	python3 ../../../tools/luapack.py $(TABLE_SRC)/tone_seq.lua -o .tone.pkg >/dev/null
	python3 ../../../tools/luapack.py --lisp $(TABLE_SRC)/logui.lisp -o .logui.pkg >/dev/null
	python3 ../../../tools/luapack.py --table $@ --legacy .legacy.pkg \
		--entry tone_seq:1.2:512:512=.tone.pkg --entry logui:1:0:0:removed=.logui.pkg >/dev/null
	rm -f .legacy.pkg .tone.pkg .logui.pkg

check-table-fixture: pkg_table_fixture.bin
	cmp pkg_table_fixture.bin fixtures/pkg_table.bin
