Skip to content

Commit

Permalink
Test ParseGCENonHostInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmwu committed Nov 2, 2021
1 parent c4c214f commit e6736ae
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions server/policy_constants_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package server

import (
"testing"

pb "github.com/google/go-tpm-tools/proto/attest"
)

func getGceMemoryEncryptionNonhostEvent(memoryEncrypted bool) []byte {
event := make([]byte, 32)
copy(event[:], []byte(GCENonHostInfoSignature))
// event[15] is a null byte.
if memoryEncrypted {
event[16] = 0x01
}
// Last 15 bytes are reserved.
return event
}

func TestParseGCENonHostInfo(t *testing.T) {
nonconfidentialEvent := getGceMemoryEncryptionNonhostEvent( /*memoryEncrypted=*/ false)

// Empty events should return NONCONFIDENTIAL.
confTech, err := ParseGCENonHostInfo([]byte{})
if err == nil {
t.Error("expected error on incorrect size!")
}
if confTech != pb.GCEConfidentialTechnology_NONE {
t.Errorf("expected ConfidentialTechnology %v, received %v", pb.GCEConfidentialTechnology_NONE, confTech)
}

confTech, err = ParseGCENonHostInfo(nonconfidentialEvent)
if err != nil {
t.Errorf("failed to parse GCE confidential tech: %v", err)
}
if confTech != pb.GCEConfidentialTechnology_NONE {
t.Errorf("expected ConfidentialTechnology %v, received %v", pb.GCEConfidentialTechnology_NONE, confTech)
}

sevEvent := getGceMemoryEncryptionNonhostEvent( /*memoryEncrypted=*/ true)
confTech, err = ParseGCENonHostInfo(sevEvent)
if err != nil {
t.Errorf("failed to parse GCE confidential tech: %v", err)
}
if confTech != pb.GCEConfidentialTechnology_AMD_SEV {
t.Errorf("expected ConfidentialTechnology %v, received %v", pb.GCEConfidentialTechnology_AMD_SEV, confTech)
}
}

func TestParseGCENonHostInfoUnknownType(t *testing.T) {
nonconfidentialEvent := getGceMemoryEncryptionNonhostEvent( /*memoryEncrypted=*/ false)
nonconfidentialEvent[16] = 0x99
if _, err := ParseGCENonHostInfo(nonconfidentialEvent); err == nil {
t.Errorf("expected error parsing GCE confidential nonhost event")
}
}

0 comments on commit e6736ae

Please sign in to comment.