Skip to content

Commit

Permalink
Initial middleware + firmware code coverage workflow
Browse files Browse the repository at this point in the history
- Instrumented firmware unit tests and tcpsigner tests to run with optional coverage
- Added lcov to middleware docker image (used to also run firmware tests)
- Added firmware code coverage generation script
- TCPSigner now catches termination signals so that it can write gcda files when compiled with coverage
- Code coverage github workflow is now a single one for both middleware and firmware
  • Loading branch information
amendelzon committed Nov 28, 2023
1 parent e2f7f3c commit 9bcea93
Show file tree
Hide file tree
Showing 25 changed files with 192 additions and 82 deletions.
26 changes: 0 additions & 26 deletions .github/workflows/coverage-mware.yml

This file was deleted.

34 changes: 34 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "Code coverage"

on:
push:

jobs:
coverage:
name: Run tests and generate coverage reports
runs-on: ubuntu-20.04

steps:
- name: Checkout this repo
uses: actions/checkout@v3

- name: Build the middleware docker image
run: docker/mware/build

- name: Run middleware coverage script
run: middleware/test-all-coverage

- name: "Upload middleware coverage report"
uses: actions/upload-artifact@v3
with:
name: middleware_coverage_report
path: middleware/coverage

- name: Run firmware coverage script
run: ledger/coverage/gen-coverage

- name: "Upload firmware coverage report"
uses: actions/upload-artifact@v3
with:
name: firmware_coverage_report
path: ledger/coverage/output
3 changes: 2 additions & 1 deletion docker/mware/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ WORKDIR /hsm2
RUN apt-get update && \
apt-get install -y apt-utils vim && \
apt-get install -y build-essential=12.9 && \
apt-get install -y git
apt-get install -y git && \
apt-get install -y lcov

# Python package prerequisites
RUN apt-get install -y \
Expand Down
2 changes: 2 additions & 0 deletions ledger/coverage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage.info
output
6 changes: 6 additions & 0 deletions ledger/coverage/coverage.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ifeq ($(COVERAGE),y)
COVFLAGS = --coverage
else
COVFLAGS =
endif
COVFILES = *.gcda *.gcno
40 changes: 40 additions & 0 deletions ledger/coverage/gen-coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

if [[ $1 == "exec" ]]; then
BASEDIR=$(realpath $(dirname $0))
SRCDIR=$(realpath $BASEDIR/../src)
REPOROOT=$(realpath $BASEDIR/../..)

# Remove any existing coverage data
rm -rf $BASEDIR/coverage.info $BASEDIR/output
find $REPOROOT/ledger -name "*.gcno" -o -name "*.gcda" | xargs rm -f

# Run firmware unit tests with coverage generation
COVERAGE=y $REPOROOT/ledger/src/signer/test/run-all.sh
COVERAGE=y $REPOROOT/ledger/src/ui/test/run-all.sh

# Run tcpsigner test suite
pushd $REPOROOT/ledger/src/tcpsigner > /dev/null
COVERAGE=y make clean all
./tcpsigner --checkpoint 0xbdcb3c17c7aee714cec8ad900341bfd987b452280220dcbd6e7191f67ea4209b --difficulty 0x32 --network regtest > /dev/null &
popd > /dev/null

pushd $REPOROOT/ledger/test > /dev/null
python run.py
err_code=$?
popd > /dev/null

lcov --capture --directory $SRCDIR --list-full-path --output-file $BASEDIR/coverage.info
genhtml $BASEDIR/coverage.info --output $BASEDIR/output -p $SRCDIR
mv $BASEDIR/coverage.info $BASEDIR/output
else
# Script directory
REPOROOT=$(realpath $(dirname $0)/../..)
SCRIPT=$(realpath $0 --relative-to=$REPOROOT)

# Generate coverage report
$REPOROOT/docker/mware/do-notty-nousb /hsm2 "./$SCRIPT exec"
err_code=$?
fi

exit $err_code
4 changes: 4 additions & 0 deletions ledger/src/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ tcpsigner/**/*.json
# Icons hex
signer/icon.hex
ui/icon.hex

# Code coverage artifacts
*.gcda
*.gcno
8 changes: 5 additions & 3 deletions ledger/src/signer/test/btcscript/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ SRCDIR = ../../src
TCPSIGNERSRCDIR = ../../../tcpsigner
CFLAGS = -I $(SRCDIR) -I $(TCPSIGNERSRCDIR) -I $(TESTCOMMONDIR)

include ../../../../coverage/coverage.mk

PROG = test.out
OBJS = test_fwk.o btcscript.o hex_reader.o test_btcscript.o

all: $(PROG)

$(PROG): $(OBJS)
$(CC) -o $@ $^
$(CC) $(COVFLAGS) -o $@ $^

test_fwk.o: $(TESTCOMMONDIR)/test_fwk.c
$(CC) $(CFLAGS) -c -o $@ $^

btcscript.o: $(SRCDIR)/btcscript.c
$(CC) $(CFLAGS) -c -o $@ $^
$(CC) $(CFLAGS) $(COVFLAGS) -c -o $@ $^

hex_reader.o: $(TCPSIGNERSRCDIR)/hex_reader.c
$(CC) $(CFLAGS) -c -o $@ $^
Expand All @@ -49,7 +51,7 @@ $(TCPSIGNERSRCDIR)/hex_reader.c: $(TCPSIGNERSRCDIR)/hex_reader.h

.PHONY: clean test
clean:
rm -f $(PROG) ./*.o
rm -f $(PROG) *.o $(COVFILES)

test: all
./$(PROG)
10 changes: 6 additions & 4 deletions ledger/src/signer/test/btctx/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ TCPSIGNERSRCDIR = ../../../tcpsigner
COMMONPATH = ../../../common/src
CFLAGS = -I $(SRCDIR) -I $(TCPSIGNERSRCDIR) -I $(COMMONPATH) -I $(TESTCOMMONDIR)

include ../../../../coverage/coverage.mk

PROG = test.out
OBJS = test_fwk.o btctx.o svarint.o hex_reader.o os.o test_btctx.o

all: $(PROG)

$(PROG): $(OBJS)
$(CC) -o $@ $^
$(CC) $(COVFLAGS) -o $@ $^

test_fwk.o: $(TESTCOMMONDIR)/test_fwk.c
$(CC) $(CFLAGS) -c -o $@ $^

btctx.o: $(SRCDIR)/btctx.c
$(CC) $(CFLAGS) -c -o $@ $^
$(CC) $(CFLAGS) $(COVFLAGS) -c -o $@ $^

svarint.o: $(SRCDIR)/svarint.c
$(CC) $(CFLAGS) -c -o $@ $^
$(CC) $(CFLAGS) $(COVFLAGS) -c -o $@ $^

hex_reader.o: $(TCPSIGNERSRCDIR)/hex_reader.c
$(CC) $(CFLAGS) -c -o $@ $^
Expand All @@ -57,7 +59,7 @@ $(TCPSIGNERSRCDIR)/hex_reader.c: $(TCPSIGNERSRCDIR)/hex_reader.h

.PHONY: clean test
clean:
rm -f $(PROG) ./*.o
rm -f $(PROG) *.o $(COVFILES)

test: all
./$(PROG)
10 changes: 6 additions & 4 deletions ledger/src/signer/test/difficulty/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ SRCDIR = ../../src
COMMONPATH = ../../../common/src
TCPSIGNERPATH = ../../../tcpsigner

include ../../../../coverage/coverage.mk

CFLAGS = -I $(SRCDIR) -I $(COMMONPATH) -I $(TCPSIGNERPATH) -I $(TESTCOMMONDIR) -DFEDHM_EMULATOR -DDEBUG_DIFF

PROG = test.out
Expand All @@ -33,19 +35,19 @@ OBJS = test_fwk.o bigdigits.o bc_diff.o os.o test_difficulty.o
all: $(PROG)

$(PROG): $(OBJS)
$(CC) -o $@ $^
$(CC) $(COVFLAGS) -o $@ $^

test_fwk.o: $(TESTCOMMONDIR)/test_fwk.c
$(CC) $(CFLAGS) -c -o $@ $^

bigdigits.o: $(SRCDIR)/bigdigits.c
$(CC) $(CFLAGS) -c -o $@ $^
$(CC) $(CFLAGS) $(COVFLAGS) -c -o $@ $^

os.o: $(TCPSIGNERPATH)/os.c
$(CC) $(CFLAGS) -c -o $@ $^

bc_diff.o: $(SRCDIR)/bc_diff.c
$(CC) $(CFLAGS) -c -o $@ $^
$(CC) $(CFLAGS) $(COVFLAGS) -c -o $@ $^

test_difficulty.o: test_difficulty.c test_fwk.o bigdigits.o bc_diff.o os.o

Expand All @@ -54,7 +56,7 @@ $(SRCDIR)/bc_diff.c: $(SRCDIR)/bc_diff.h $(SRCDIR)/bigdigits.h $(SRCDIR)/bigdtyp

.PHONY: clean test
clean:
rm -f $(PROG) ./*.o
rm -f $(PROG) *.o $(COVFILES)

test: all
./$(PROG)
8 changes: 5 additions & 3 deletions ledger/src/signer/test/sha256/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,29 @@ TESTCOMMONDIR = ../common
SRCDIR = ../../src
CFLAGS = -I $(SRCDIR) -I $(TESTCOMMONDIR)

include ../../../../coverage/coverage.mk

PROG = test.out
OBJS = test_fwk.o sha256.o test_sha256.o

all: $(PROG)

$(PROG): $(OBJS)
$(CC) -o $@ $^
$(CC) $(COVFLAGS) -o $@ $^

test_fwk.o: $(TESTCOMMONDIR)/test_fwk.c
$(CC) $(CFLAGS) -c -o $@ $^

sha256.o: $(SRCDIR)/sha256.c
$(CC) $(CFLAGS) -c -o $@ $^
$(CC) $(CFLAGS) $(COVFLAGS) -c -o $@ $^

test_sha256.o: test_sha256.c test_fwk.o sha256.o

$(SRCDIR)/sha256.c: $(SRCDIR)/sha256.h

.PHONY: clean test
clean:
rm -f $(PROG) ./*.o
rm -f $(PROG) *.o $(COVFILES)

test: all
./$(PROG)
8 changes: 5 additions & 3 deletions ledger/src/signer/test/srlp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,29 @@ TESTCOMMONDIR = ../common
SRCDIR = ../../src
CFLAGS = -I $(SRCDIR) -I $(TESTCOMMONDIR) -Imocks/ -DMAX_RLP_CTX_DEPTH=10

include ../../../../coverage/coverage.mk

PROG = test.out
OBJS = test_fwk.o srlp.o test_srlp.o

all: $(PROG)

$(PROG): $(OBJS)
$(CC) -o $@ $^
$(CC) $(COVFLAGS) -o $@ $^

test_fwk.o: $(TESTCOMMONDIR)/test_fwk.c
$(CC) $(CFLAGS) -c -o $@ $^

srlp.o: $(SRCDIR)/srlp.c
$(CC) $(CFLAGS) -c -o $@ $^
$(CC) $(CFLAGS) $(COVFLAGS) -c -o $@ $^

test_srlp.o: test_srlp.c test_fwk.o srlp.o

$(SRCDIR)/srlp.c: $(SRCDIR)/srlp.h

.PHONY: clean test
clean:
rm -f $(PROG) ./*.o
rm -f $(PROG) *.o $(COVFILES)

test: all
./$(PROG)
8 changes: 5 additions & 3 deletions ledger/src/signer/test/svarint/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,29 @@ TESTCOMMONDIR = ../common
SRCDIR = ../../src
CFLAGS = -I $(SRCDIR) -I $(TESTCOMMONDIR)

include ../../../../coverage/coverage.mk

PROG = test.out
OBJS = test_fwk.o svarint.o test_svarint.o

all: $(PROG)

$(PROG): $(OBJS)
$(CC) -o $@ $^
$(CC) $(COVFLAGS) -o $@ $^

test_fwk.o: $(TESTCOMMONDIR)/test_fwk.c
$(CC) $(CFLAGS) -c -o $@ $^

svarint.o: $(SRCDIR)/svarint.c
$(CC) $(CFLAGS) -c -o $@ $^
$(CC) $(CFLAGS) $(COVFLAGS) -c -o $@ $^

test_svarint.o: test_svarint.c test_fwk.o svarint.o

$(SRCDIR)/svarint.c: $(SRCDIR)/svarint.h

.PHONY: clean test
clean:
rm -f $(PROG) ./*.o
rm -f $(PROG) *.o $(COVFILES)

test: all
./$(PROG)
10 changes: 6 additions & 4 deletions ledger/src/signer/test/trie/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ TCPSIGNERSRCDIR = ../../../tcpsigner
COMMONPATH = ../../../common/src
CFLAGS = -I $(SRCDIR) -I $(TCPSIGNERSRCDIR) -I $(COMMONPATH) -I $(TESTCOMMONDIR)

include ../../../../coverage/coverage.mk

PROG = test.out
OBJS = test_fwk.o trie.o svarint.o hex_reader.o os.o test_trie.o

all: $(PROG)

$(PROG): $(OBJS)
$(CC) -o $@ $^
$(CC) $(COVFLAGS) -o $@ $^

test_fwk.o: $(TESTCOMMONDIR)/test_fwk.c
$(CC) $(CFLAGS) -c -o $@ $^

trie.o: $(SRCDIR)/trie.c
$(CC) $(CFLAGS) -c -o $@ $^
$(CC) $(CFLAGS) $(COVFLAGS) -c -o $@ $^

svarint.o: $(SRCDIR)/svarint.c
$(CC) $(CFLAGS) -c -o $@ $^
$(CC) $(CFLAGS) $(COVFLAGS) -c -o $@ $^

hex_reader.o: $(TCPSIGNERSRCDIR)/hex_reader.c
$(CC) $(CFLAGS) -c -o $@ $^
Expand All @@ -57,7 +59,7 @@ $(TCPSIGNERSRCDIR)/hex_reader.c: $(TCPSIGNERSRCDIR)/hex_reader.h

.PHONY: clean test
clean:
rm -f $(PROG) ./*.o
rm -f $(PROG) *.o $(COVFILES)

test: all
./$(PROG)
Loading

0 comments on commit 9bcea93

Please sign in to comment.