refactor: modularize audit and harden build validation
This commit is contained in:
@@ -1,11 +1,66 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRunTPMValidationPackSkipsWhenNoTPMDevice(t *testing.T) {
|
||||
old := tpmDeviceGlob
|
||||
tpmDeviceGlob = func() []string { return nil }
|
||||
t.Cleanup(func() { tpmDeviceGlob = old })
|
||||
|
||||
dir := t.TempDir()
|
||||
runDir, err := (&System{}).RunTPMValidationPack(nil, dir, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("RunTPMValidationPack: %v", err)
|
||||
}
|
||||
for _, name := range []string{"01-properties-fixed.log", "02-pcr-banks.log", "03-pcr-values.log", "04-test-result.log"} {
|
||||
if _, err := os.Stat(filepath.Join(runDir, name)); err == nil {
|
||||
t.Fatalf("tpm2 job %q ran despite no TPM device", name)
|
||||
}
|
||||
}
|
||||
summary, err := os.ReadFile(filepath.Join(runDir, "summary.txt"))
|
||||
if err != nil {
|
||||
t.Fatalf("read summary: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(summary), "overall_status=UNSUPPORTED") ||
|
||||
!strings.Contains(string(summary), "tpm_present=false") {
|
||||
t.Fatalf("summary missing UNSUPPORTED/tpm_present markers:\n%s", summary)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTPMPresentRequiresVersion2(t *testing.T) {
|
||||
oldGlob, oldRead := tpmDeviceGlob, tpmReadFile
|
||||
t.Cleanup(func() {
|
||||
tpmDeviceGlob = oldGlob
|
||||
tpmReadFile = oldRead
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
version string
|
||||
readErr error
|
||||
want bool
|
||||
}{
|
||||
{name: "TPM 2", version: "2\n", want: true},
|
||||
{name: "TPM 1.2", version: "1\n", want: false},
|
||||
{name: "missing version attribute", readErr: os.ErrNotExist, want: false},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
tpmDeviceGlob = func() []string { return []string{"/sys/class/tpm/tpm0"} }
|
||||
tpmReadFile = func(string) ([]byte, error) { return []byte(test.version), test.readErr }
|
||||
if got := (&System{}).TPMPresent(); got != test.want {
|
||||
t.Fatalf("TPMPresent()=%v want %v", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTPMValidationJobsAreReadOnly(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user