This commit introduces basic tests for the bg_setenv/bg_printenv command-line interface.
Rationale: The bg_setenv/bg_printenv command-line interface is currently untested, which makes it error-prone to extend the current code. Even though there are existing unit tests (based on fff [1]), they are unable to catch certain kinds of issues, e.g. linking related, see our discussion here [2]. For that reason, a new test dependency is introduced: bats-core (MIT, [3]). [1] https://github.com/meekrosoft/fff [2] https://www.mail-archive.com/[email protected]/msg01351.html [3] https://github.com/bats-core/bats-core Signed-off-by: Michael Adler <[email protected]> --- .github/workflows/main.yaml | 7 ++- docs/COMPILE.md | 3 +- tests/bg_setenv.bats | 115 ++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 3 deletions(-) create mode 100755 tests/bg_setenv.bats diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 6bb9f66..10b2a2d 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -40,7 +40,7 @@ jobs: if: ${{ matrix.target == 'amd64' || matrix.target == 'cppcheck' }} run: | sudo apt-get install --no-install-recommends \ - gcc-multilib gnu-efi libz-dev libpci-dev check + gcc-multilib gnu-efi libz-dev libpci-dev check bats - name: Install i386 dependencies if: ${{ matrix.target == 'i386' }} run: | @@ -86,9 +86,12 @@ jobs: - name: Build amd64 if: ${{ matrix.target == 'amd64' }} run: | - cd build + pushd build >/dev/null ../configure make check -j $(nproc) + sudo make install + popd >/dev/null + time bats --tap tests - name: Build i386 if: ${{ matrix.target == 'i386' }} run: | diff --git a/docs/COMPILE.md b/docs/COMPILE.md index bda19d4..0d6c09c 100644 --- a/docs/COMPILE.md +++ b/docs/COMPILE.md @@ -50,4 +50,5 @@ where `<sys-root-dir>` points to the wanted sysroot for cross-compilation. ## Testing ## -`make check` will run all unit tests. +* `make check` will run all unit tests. +* `bats tests` will run all integration tests (you need [bats-core](https://github.com/bats-core/bats-core) for that; packaged as `bash-bats` for Arch Linux and `bats` for Debian). diff --git a/tests/bg_setenv.bats b/tests/bg_setenv.bats new file mode 100755 index 0000000..dffcb5f --- /dev/null +++ b/tests/bg_setenv.bats @@ -0,0 +1,115 @@ +#!/usr/bin/env bats +# Copyright (c) Siemens AG, 2021 +# +# Authors: +# Michael Adler <[email protected]> +# +# This work is licensed under the terms of the GNU GPL, version 2. See +# the COPYING file in the top-level directory. +# +# SPDX-License-Identifier: GPL-2.0 +# + +setup() { + # get the containing directory of this file + # use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0, + # as those will point to the bats executable's location or the preprocessed file respectively + DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" + PATH="$DIR/..:$PATH" +} + +create_sample_bgenv() { + bg_setenv -f "$1" \ + --kernel=C:BOOT:kernel.efi \ + --args=root=/dev/sda \ + --uservar=foo=bar \ + --revision=1 +} + +@test "ensure BGENV.DAT backwards compatbility" { + local envfile + envfile="$(mktemp -d)/BGENV.DAT" + create_sample_bgenv "$envfile" + + run bg_printenv -f "$envfile" + [[ "$output" = "Values: +in_progress: no +revision: 1 +kernel: C:BOOT:kernel.efi +kernelargs: root=/dev/sda +watchdog timeout: 0 seconds +ustate: 0 (OK) + +user variables: +foo = bar" ]] + + run md5sum "$envfile" + [[ "$output" =~ ^6ad1dd1d98209a03d7b4fc2d2f16f9ec\s*.* ]] +} + +@test "create an empty BGENV.DAT" { + local envfile + envfile="$(mktemp -d)/BGENV.DAT" + + run bg_setenv -f "$envfile" + [[ "$output" = "Output written to $envfile." ]] + + run md5sum "$envfile" + [[ "$output" =~ ^441b49e907a117d2fe1dc1d69d8ea1b0\s*.* ]] + + run bg_printenv -f "$envfile" + [[ "$output" = "Values: +in_progress: no +revision: 0 +kernel: +kernelargs: +watchdog timeout: 0 seconds +ustate: 0 (OK) + +user variables:" ]] +} + +@test "modify BGENV, discard existing values" { + local envfile + envfile="$(mktemp -d)/BGENV.DAT" + + create_sample_bgenv "$envfile" + run bg_setenv -f "$envfile" -k C:BOOTNEW:kernel.efi + + run bg_printenv -f "$envfile" + [[ "$output" = "Values: +in_progress: no +revision: 0 +kernel: C:BOOTNEW:kernel.efi +kernelargs: +watchdog timeout: 0 seconds +ustate: 0 (OK) + +user variables:" ]] + + run md5sum "$envfile" + [[ "$output" =~ ^15bc40c9feae99cc879cfc55e0132caa\s*.* ]] +} + +@test "modify BGENV, preserve existing values" { + local envfile + envfile="$(mktemp -d)/BGENV.DAT" + + create_sample_bgenv "$envfile" + run bg_setenv -f "$envfile" -k C:BOOTNEW:kernel.efi -P + + run bg_printenv -f "$envfile" + [[ "$output" = "Values: +in_progress: no +revision: 1 +kernel: C:BOOTNEW:kernel.efi +kernelargs: root=/dev/sda +watchdog timeout: 0 seconds +ustate: 0 (OK) + +user variables: +foo = bar" ]] + + run md5sum "$envfile" + [[ "$output" =~ ^a24b154a48e1f33b79b87e0fa5eff8a1\s*.* ]] +} -- 2.33.0 -- You received this message because you are subscribed to the Google Groups "EFI Boot Guard" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/efibootguard-dev/20211029094526.278940-2-michael.adler%40siemens.com.
