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        | 105 ++++++++++++++++++++++++++++++++++++
 tests/files/BGENV.DAT       | Bin 0 -> 132104 bytes
 4 files changed, 112 insertions(+), 3 deletions(-)
 create mode 100755 tests/bg_setenv.bats
 create mode 100644 tests/files/BGENV.DAT

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..df52c07 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 (`bats` is a git submodule)
diff --git a/tests/bg_setenv.bats b/tests/bg_setenv.bats
new file mode 100755
index 0000000..bebd914
--- /dev/null
+++ b/tests/bg_setenv.bats
@@ -0,0 +1,105 @@
+#!/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"
+}
+
+@test "print existing BGENV" {
+    envfile="$DIR/files/BGENV.DAT"
+
+    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" =~ ^6613ca4da6bcaad37362f4096a60f0e3\s*.* ]]
+}
+
+@test "create an empty BGENV" {
+    envfile="$(mktemp -d)/BGENV.DAT"
+
+    run bg_setenv -f "$envfile"
+
+    [[ "$output" = "Output written to $envfile." ]]
+
+    run stat --format=%s "$envfile"
+    [[ "$output" = 132104 ]]
+
+    run bg_printenv -f "$envfile"
+    [[ "$output" =  "Values:
+in_progress:      no
+revision:         0
+kernel:           
+kernelargs:       
+watchdog timeout: 0 seconds
+ustate:           0 (OK)
+
+user variables:" ]]
+    run md5sum "$envfile"
+    [[ "$output" =~ ^441b49e907a117d2fe1dc1d69d8ea1b0\s*.* ]]
+}
+
+@test "modify BGENV, discard existing values" {
+    envfile="$(mktemp -d)/BGENV.DAT"
+
+    cp "$DIR/files/BGENV.DAT" "$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" {
+    envfile="$(mktemp -d)/BGENV.DAT"
+
+    cp "$DIR/files/BGENV.DAT" "$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" =~ ^56f5733996e25fa1ea9b053d8f197658\s*.* ]]
+}
diff --git a/tests/files/BGENV.DAT b/tests/files/BGENV.DAT
new file mode 100644
index 
0000000000000000000000000000000000000000..1dbbfe51dbbeddd43c2ed228d713a47d43e5dda5
GIT binary patch
literal 132104
zcmeIw!3{xC5CzaH8c>1?h{Q@_Ljg7#_<}@;FP~^ZgKDuh3srpQO)_)0bLOqH$SRwv
zZKZP#>2j@p$)x%*r+EDp=+ag_vdlF5y7#l@y;4&vU+Oo^a?8BG>)R}YGB~zvMm5Jj
zk6qLKl0kp~0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+
z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly
zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF
z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk
z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs
z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ
zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U
zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7
z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N
z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+
z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly
NK!Cu%34C6|_ZORb5l;XB

literal 0
HcmV?d00001

-- 
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/20211026085748.388326-1-michael.adler%40siemens.com.

Reply via email to