Add backends and simulated TPM device

Signed-off-by: Ettore Di Giacinto <edigiacinto@suse.com>
This commit is contained in:
Ettore Di Giacinto
2022-02-16 12:52:30 +01:00
parent c2203f40dd
commit 1ab3e10e4d
11 changed files with 269 additions and 16 deletions

View File

@@ -0,0 +1,13 @@
package backend_test
import (
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestBackend(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Backend Suite")
}

17
backend/fake.go Normal file
View File

@@ -0,0 +1,17 @@
package backend
import (
"io"
)
type FakeTPM struct {
io.ReadWriteCloser
}
var fixedLog = []byte{0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x21, 0x0, 0x0, 0x0, 0x53, 0x70, 0x65, 0x63, 0x20, 0x49, 0x44,
0x20, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x30, 0x33, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x2, 0x0, 0x2, 0x1, 0x0, 0x0, 0x0, 0xb, 0x0, 0x20, 0x0, 0x0}
func (*FakeTPM) MeasurementLog() ([]byte, error) { return fixedLog, nil }

13
backend/socket.go Normal file
View File

@@ -0,0 +1,13 @@
package backend
import "net"
func Socket(f string) (*FakeTPM, error) {
conn, err := net.Dial("unix", f)
if err != nil {
return nil, err
}
return &FakeTPM{
ReadWriteCloser: conn,
}, nil
}

29
backend/socket_test.go Normal file
View File

@@ -0,0 +1,29 @@
package backend_test
import (
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/rancher-sandbox/go-tpm/backend"
)
var _ = Describe("SWTPM", func() {
socket := os.Getenv("SWTPM_SOCKET")
Context("opening socket connection", func() {
It("fails on invalid files", func() {
_, err := Socket("foobar")
Expect(err).To(HaveOccurred())
})
It("dials in just fine", func() {
if socket == "" {
Skip("No socket file specified")
}
_, err := Socket(socket)
Expect(err).ToNot(HaveOccurred())
})
})
})