Skip to content

Commit

Permalink
Add tests for eventlog parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mjg59 committed Oct 11, 2019
1 parent cca097f commit 4b93759
Show file tree
Hide file tree
Showing 15 changed files with 14,496 additions and 0 deletions.
102 changes: 102 additions & 0 deletions events/events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

package events

import (
"crypto"
"encoding/json"
"io/ioutil"
"testing"

"github.com/google/go-attestation/attest"
)

func TestParseEvents(t *testing.T) {
var emptyPCRs [24]attest.PCR

for i, _ := range emptyPCRs {
emptyPCRs[i].Index = i
emptyPCRs[i].Digest = make([]byte, 20)
emptyPCRs[i].DigestAlg = crypto.SHA1
}
testParseEvent(t, emptyPCRs[:], "testdata/binary_bios_measurements_15")
testParseEvent(t, emptyPCRs[:], "testdata/binary_bios_measurements_27")
testParseEvent(t, emptyPCRs[:], "testdata/linux_event_log")
testParseEvent(t, emptyPCRs[:], "testdata/tpm12_windows_lenovo_x1carbonv3")
}

func TestParseCryptoAgileEvents(t *testing.T) {
var emptyPCRs [24]attest.PCR
for i, _ := range emptyPCRs {
emptyPCRs[i].Index = i
emptyPCRs[i].Digest = make([]byte, 32)
emptyPCRs[i].DigestAlg = crypto.SHA256
}

testParseEvent(t, emptyPCRs[:], "testdata/crypto_agile_eventlog")
testParseEvent(t, emptyPCRs[:], "testdata/tpm2_windows_lenovo_yogax1v2")
testParseEvent(t, emptyPCRs[:], "testdata/windows_event_log")
}

func testParseEvent(t *testing.T, PCRs []attest.PCR, filename string) {
data, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("reading test data %s: %v", filename, err)
}
el, err := attest.ParseEventLog(data)
if err != nil {
t.Fatalf("parsing event log %s: %v", filename, err)
}
outputEvents, err := el.Verify(PCRs[:])
if err != nil {
if replayErr, isReplayErr := err.(attest.ReplayError); isReplayErr {
outputEvents = replayErr.Events
} else {
t.Fatalf("failed to verify from event log %s: %v", filename, err)
}
}
if len(outputEvents) == 0 {
t.Fatalf("failed to extract any events from %s", filename)
}

parsedEvents, err := ParseEvents(outputEvents)

if err != nil {
t.Fatalf("parsing events %s: %v", filename, err)
}

if len(parsedEvents) == 0 {
t.Fatalf("failed to parse any events from %s", filename)
}

reference := filename + ".json"
referenceData, err := ioutil.ReadFile(reference)
if err != nil {
t.Fatalf("failed to read json reference %s: %v", reference, err)
}

var referenceEvents []TPMEvent

err = json.Unmarshal(referenceData, &referenceEvents)
if err != nil {
t.Fatalf("failed to unmarshal json reference %s: %v", reference, err)
}

for i, _ := range parsedEvents {
if parsedEvents[i] != referenceEvents[i] {
t.Fatalf("mismatch in event %d from %s: %+v %+v", i, filename, referenceEvents[i], parsedEvents[i])
}
}
}
Binary file added events/testdata/binary_bios_measurements_15
Binary file not shown.
Loading

0 comments on commit 4b93759

Please sign in to comment.