Skip to content

Commit b5f951d

Browse files
committed
Add unit tests for inheritBootloaderKindBySHA function
1 parent a0eb8c9 commit b5f951d

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

internal/image/imageinspect/imageinspect_core_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,3 +807,108 @@ func TestInspectCore_PropagatesFilesystemError_WhenCalled(t *testing.T) {
807807
t.Fatalf("expected GetFilesystem to be called at least once")
808808
}
809809
}
810+
811+
func TestInheritBootloaderKindBySHA_InheritsFromKnown(t *testing.T) {
812+
evs := []EFIBinaryEvidence{
813+
{Path: "/EFI/ubuntu/shimx64.efi", SHA256: "abc123def456", Kind: BootloaderShim},
814+
{Path: "/EFI/BOOT/BOOTX64.EFI", SHA256: "abc123def456", Kind: BootloaderUnknown},
815+
}
816+
817+
inheritBootloaderKindBySHA(evs)
818+
819+
if evs[1].Kind != BootloaderShim {
820+
t.Errorf("expected BOOTX64.EFI to inherit kind=shim, got %q", evs[1].Kind)
821+
}
822+
if len(evs[1].Notes) == 0 || !strings.Contains(evs[1].Notes[0], "sha256 match") {
823+
t.Errorf("expected note about sha256 inheritance, got %v", evs[1].Notes)
824+
}
825+
// Original should remain unchanged
826+
if evs[0].Kind != BootloaderShim {
827+
t.Errorf("original should stay shim, got %q", evs[0].Kind)
828+
}
829+
}
830+
831+
func TestInheritBootloaderKindBySHA_NoMatchLeavesUnknown(t *testing.T) {
832+
evs := []EFIBinaryEvidence{
833+
{Path: "/EFI/ubuntu/shimx64.efi", SHA256: "abc123", Kind: BootloaderShim},
834+
{Path: "/EFI/BOOT/BOOTX64.EFI", SHA256: "different456", Kind: BootloaderUnknown},
835+
}
836+
837+
inheritBootloaderKindBySHA(evs)
838+
839+
if evs[1].Kind != BootloaderUnknown {
840+
t.Errorf("expected BOOTX64.EFI to remain unknown when hash differs, got %q", evs[1].Kind)
841+
}
842+
if len(evs[1].Notes) != 0 {
843+
t.Errorf("expected no notes when no match, got %v", evs[1].Notes)
844+
}
845+
}
846+
847+
func TestInheritBootloaderKindBySHA_EmptySHA256Ignored(t *testing.T) {
848+
evs := []EFIBinaryEvidence{
849+
{Path: "/EFI/ubuntu/shimx64.efi", SHA256: "", Kind: BootloaderShim},
850+
{Path: "/EFI/BOOT/BOOTX64.EFI", SHA256: "", Kind: BootloaderUnknown},
851+
}
852+
853+
inheritBootloaderKindBySHA(evs)
854+
855+
// Both should remain unchanged - empty SHA256 entries are skipped
856+
if evs[0].Kind != BootloaderShim {
857+
t.Errorf("expected shim to remain shim, got %q", evs[0].Kind)
858+
}
859+
if evs[1].Kind != BootloaderUnknown {
860+
t.Errorf("expected unknown to remain unknown when SHA256 empty, got %q", evs[1].Kind)
861+
}
862+
}
863+
864+
func TestInheritBootloaderKindBySHA_MultipleInheritances(t *testing.T) {
865+
evs := []EFIBinaryEvidence{
866+
{Path: "/EFI/ubuntu/shimx64.efi", SHA256: "shimhash", Kind: BootloaderShim},
867+
{Path: "/EFI/fedora/grubx64.efi", SHA256: "grubhash", Kind: BootloaderGrub},
868+
{Path: "/EFI/BOOT/BOOTX64.EFI", SHA256: "shimhash", Kind: BootloaderUnknown},
869+
{Path: "/EFI/BOOT/grubx64.efi", SHA256: "grubhash", Kind: BootloaderUnknown},
870+
{Path: "/EFI/unknown/mystery.efi", SHA256: "otherhash", Kind: BootloaderUnknown},
871+
}
872+
873+
inheritBootloaderKindBySHA(evs)
874+
875+
if evs[2].Kind != BootloaderShim {
876+
t.Errorf("BOOTX64.EFI should inherit shim, got %q", evs[2].Kind)
877+
}
878+
if evs[3].Kind != BootloaderGrub {
879+
t.Errorf("grubx64.efi copy should inherit grub, got %q", evs[3].Kind)
880+
}
881+
if evs[4].Kind != BootloaderUnknown {
882+
t.Errorf("mystery.efi should remain unknown, got %q", evs[4].Kind)
883+
}
884+
}
885+
886+
func TestInheritBootloaderKindBySHA_FirstKnownWins(t *testing.T) {
887+
// If the same hash appears with different kinds, first one wins
888+
evs := []EFIBinaryEvidence{
889+
{Path: "/EFI/first/shimx64.efi", SHA256: "samehash", Kind: BootloaderShim},
890+
{Path: "/EFI/second/grubx64.efi", SHA256: "samehash", Kind: BootloaderGrub},
891+
{Path: "/EFI/BOOT/BOOTX64.EFI", SHA256: "samehash", Kind: BootloaderUnknown},
892+
}
893+
894+
inheritBootloaderKindBySHA(evs)
895+
896+
// The unknown should inherit from the first known (shim)
897+
if evs[2].Kind != BootloaderShim {
898+
t.Errorf("expected first known kind (shim) to win, got %q", evs[2].Kind)
899+
}
900+
}
901+
902+
func TestInheritBootloaderKindBySHA_AlreadyClassifiedNotOverwritten(t *testing.T) {
903+
evs := []EFIBinaryEvidence{
904+
{Path: "/EFI/ubuntu/shimx64.efi", SHA256: "abc123", Kind: BootloaderShim},
905+
{Path: "/EFI/BOOT/BOOTX64.EFI", SHA256: "abc123", Kind: BootloaderGrub}, // Already classified differently
906+
}
907+
908+
inheritBootloaderKindBySHA(evs)
909+
910+
// Should NOT overwrite an already-classified binary
911+
if evs[1].Kind != BootloaderGrub {
912+
t.Errorf("already classified binary should not be overwritten, got %q", evs[1].Kind)
913+
}
914+
}

0 commit comments

Comments
 (0)