From 758dad5410c4ece13757e2b990a6bfdc5c982a70 Mon Sep 17 00:00:00 2001 From: Adrian Stobbe Date: Mon, 16 Oct 2023 09:01:31 +0200 Subject: [PATCH] add test for allEqual --- .../api/attestationconfigapi/cli/BUILD.bazel | 6 ++++- internal/api/attestationconfigapi/cli/main.go | 2 +- .../api/attestationconfigapi/cli/main_test.go | 27 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 internal/api/attestationconfigapi/cli/main_test.go diff --git a/internal/api/attestationconfigapi/cli/BUILD.bazel b/internal/api/attestationconfigapi/cli/BUILD.bazel index 80017fa45a..a64c3fdbd2 100644 --- a/internal/api/attestationconfigapi/cli/BUILD.bazel +++ b/internal/api/attestationconfigapi/cli/BUILD.bazel @@ -34,9 +34,13 @@ go_library( go_test( name = "cli_test", - srcs = ["delete_test.go"], + srcs = [ + "delete_test.go", + "main_test.go", + ], embed = [":cli_lib"], deps = [ + "//internal/verify", "@com_github_stretchr_testify//assert", "@com_github_stretchr_testify//require", ], diff --git a/internal/api/attestationconfigapi/cli/main.go b/internal/api/attestationconfigapi/cli/main.go index 1fd7d49d17..defab1cd9f 100644 --- a/internal/api/attestationconfigapi/cli/main.go +++ b/internal/api/attestationconfigapi/cli/main.go @@ -152,7 +152,7 @@ func runCmd(cmd *cobra.Command, _ []string) (retErr error) { return nil } -func allEqual[T comparable](args ...T) bool { +func allEqual(args ...verify.TCBVersion) bool { if len(args) < 2 { return true } diff --git a/internal/api/attestationconfigapi/cli/main_test.go b/internal/api/attestationconfigapi/cli/main_test.go new file mode 100644 index 0000000000..6e30600a97 --- /dev/null +++ b/internal/api/attestationconfigapi/cli/main_test.go @@ -0,0 +1,27 @@ +package main + +import ( + "testing" + + "github.com/edgelesssys/constellation/v2/internal/verify" + "github.com/stretchr/testify/assert" +) + +func TestAllEqual(t *testing.T) { + // Test case 1: One input arg + assert.True(t, allEqual(verify.TCBVersion{Bootloader: 1, Microcode: 2, SNP: 3, TEE: 4}), "Expected allEqual to return true for one input arg, but got false") + + // Test case 2: Three input args that are equal + assert.True(t, allEqual( + verify.TCBVersion{Bootloader: 1, Microcode: 2, SNP: 3, TEE: 4}, + verify.TCBVersion{Bootloader: 1, Microcode: 2, SNP: 3, TEE: 4}, + verify.TCBVersion{Bootloader: 1, Microcode: 2, SNP: 3, TEE: 4}, + ), "Expected allEqual to return true for three equal input args, but got false") + + // Test case 3: Three input args where second and third element are different + assert.False(t, allEqual( + verify.TCBVersion{Bootloader: 1, Microcode: 2, SNP: 3, TEE: 4}, + verify.TCBVersion{Bootloader: 2, Microcode: 2, SNP: 3, TEE: 4}, + verify.TCBVersion{Bootloader: 2, Microcode: 3, SNP: 3, TEE: 4}, + ), "Expected allEqual to return false for three input args with different second and third elements, but got true") +}