Debian Codegraph @

server/policy_constants_test.go

raw
1 package server
2
3 import (
4 "testing"
5
6 pb "github.com/google/go-tpm-tools/proto/attest"
7 )
8
9 func getGceMemoryEncryptionNonhostEvent(memoryEncrypted bool) []byte {
10 event := make([]byte, 32)
11 copy(event[:], []byte(GCENonHostInfoSignature))
12 // event[15] is a null byte.
13 if memoryEncrypted {
14 event[16] = 0x01
15 }
16 // Last 15 bytes are reserved.
17 return event
18 }
19
20 func TestParseGCENonHostInfo(t *testing.T) {
21 nonconfidentialEvent := getGceMemoryEncryptionNonhostEvent( /*memoryEncrypted=*/ false)
22
23 // Empty events should return NONCONFIDENTIAL.
24 confTech, err := ParseGCENonHostInfo([]byte{})
25 if err == nil {
26 t.Error("expected error on incorrect size!")
27 }
28 if confTech != pb.GCEConfidentialTechnology_NONE {
29 t.Errorf("expected ConfidentialTechnology %v, received %v", pb.GCEConfidentialTechnology_NONE, confTech)
30 }
31
32 confTech, err = ParseGCENonHostInfo(nonconfidentialEvent)
33 if err != nil {
34 t.Errorf("failed to parse GCE confidential tech: %v", err)
35 }
36 if confTech != pb.GCEConfidentialTechnology_NONE {
37 t.Errorf("expected ConfidentialTechnology %v, received %v", pb.GCEConfidentialTechnology_NONE, confTech)
38 }
39
40 sevEvent := getGceMemoryEncryptionNonhostEvent( /*memoryEncrypted=*/ true)
41 confTech, err = ParseGCENonHostInfo(sevEvent)
42 if err != nil {
43 t.Errorf("failed to parse GCE confidential tech: %v", err)
44 }
45 if confTech != pb.GCEConfidentialTechnology_AMD_SEV {
46 t.Errorf("expected ConfidentialTechnology %v, received %v", pb.GCEConfidentialTechnology_AMD_SEV, confTech)
47 }
48 }
49
50 func TestParseGCENonHostInfoUnknownType(t *testing.T) {
51 nonconfidentialEvent := getGceMemoryEncryptionNonhostEvent( /*memoryEncrypted=*/ false)
52 nonconfidentialEvent[16] = 0x99
53 if _, err := ParseGCENonHostInfo(nonconfidentialEvent); err == nil {
54 t.Errorf("expected error parsing GCE confidential nonhost event")
55 }
56 }