|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Fetches coverage data for each fuzz test, combines them, and produces a |
| 4 | +# coverage profile that can be analyzed. |
| 5 | +if [[ "$#" -ne 1 ]]; then |
| 6 | + echo "Usage: $0 LND_DIR" |
| 7 | + exit 1 |
| 8 | +fi |
| 9 | + |
| 10 | +readonly LND_DIR=$1 |
| 11 | +readonly CACHE_DIR=$(mktemp -d) |
| 12 | + |
| 13 | +# Get the directory of cov_profiles.sh in case it is being called not in |
| 14 | +# lnd-fuzz. |
| 15 | +readonly BASE_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 16 | + |
| 17 | +readonly packages=("lnwire" "brontide" "htlcswitch/hop" "tlv" "watchtower/wtwire" "watchtower/wtclient" "zpay32") |
| 18 | + |
| 19 | +# collect_combine_cov_profiles collects coverage for each fuzzing package and |
| 20 | +# then combines them. |
| 21 | +function collect_combine_cov_profiles { |
| 22 | + local coverage_dirs=() |
| 23 | + |
| 24 | + # Collect coverage profiles. |
| 25 | + for p in ${packages[@]}; do |
| 26 | + cd "${BASE_DIR}/${p}/testdata/fuzz" |
| 27 | + |
| 28 | + for f in $(ls $PWD); do |
| 29 | + # Move corpus to CACHE_DIR. |
| 30 | + mkdir -p "${CACHE_DIR}/${f}" |
| 31 | + cp -a "${BASE_DIR}/${p}/testdata/fuzz/${f}"/ "${CACHE_DIR}/${f}/" |
| 32 | + num_inputs=$(ls "${CACHE_DIR}/${f}/" | wc -l | xargs) |
| 33 | + mkdir -p "${BASE_DIR}/coverage/${f}" |
| 34 | + |
| 35 | + cd "${LND_DIR}/${p}/" |
| 36 | + |
| 37 | + go test -v -cover -run="^${f}$" -fuzz="^${f}$" \ |
| 38 | + -fuzztime="${num_inputs}x" \ |
| 39 | + -test.gocoverdir="${BASE_DIR}/coverage/${f}" \ |
| 40 | + -test.fuzzcachedir="${CACHE_DIR}" |
| 41 | + |
| 42 | + coverage_dirs+=("coverage/${f}") |
| 43 | + done |
| 44 | + |
| 45 | + cd $BASE_DIR |
| 46 | + done |
| 47 | + |
| 48 | + # Combine coverage profiles. |
| 49 | + local profile_str="" |
| 50 | + local coverage_dirs_len=${#coverage_dirs[@]} |
| 51 | + |
| 52 | + for (( i = 1; i <= $coverage_dirs_len; i++ )) |
| 53 | + do |
| 54 | + let "index = $i - 1" |
| 55 | + if [[ $i -eq $coverage_dirs_len ]] |
| 56 | + then |
| 57 | + profile_str+="./${coverage_dirs[$index]}" |
| 58 | + break |
| 59 | + fi |
| 60 | + |
| 61 | + profile_str+="./${coverage_dirs[$index]}," |
| 62 | + done |
| 63 | + |
| 64 | + go tool covdata textfmt -i=$profile_str -o coverage/profile |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +collect_combine_cov_profiles |
0 commit comments