Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding comments to TestReadPublicKey #299

Merged
merged 2 commits into from
Aug 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions direct/tpm2/read_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ import (
"github.com/google/go-tpm/direct/transport/simulator"
)

// TestReadPublicKey compares the CreatePrimary reponse parameter outPublic with the output of ReadPublic outPublic.
func TestReadPublicKey(t *testing.T) {

// Open simulated TPM for testing.
thetpm, err := simulator.OpenSimulator()
if err != nil {
t.Fatalf("could not connect to TPM simulator: %v", err)
}

// Defer the close of the simulated TPM to after use.
matt-tsai marked this conversation as resolved.
Show resolved Hide resolved
// Without this, other programs/tests may not be able to get a handle to the TPM.
defer thetpm.Close()

// Fill in the CreatePrimary struct.
// See definition in Part 3, Commands, section 24.1.
// See direct/templates/templates.go for more tpmt.Public examples.
createPrimary := CreatePrimary{
matt-tsai marked this conversation as resolved.
Show resolved Hide resolved
PrimaryHandle: tpm.RHOwner,
InPublic: tpm2b.Public{
Expand Down Expand Up @@ -51,26 +58,44 @@ func TestReadPublicKey(t *testing.T) {
},
}

// Executing the command uses reflection to pack the bytes into a
// TPM2_CreatePrimary command, returns a TPM2_CreatePrimary Response.
// This response is also decoded so you are again working with structs
// that can be found in Part 3, Commands, section 24.1.
rspCP, err := createPrimary.Execute(thetpm)
if err != nil {
t.Fatalf("CreatePrimary failed: %v", err)
}

// The TPM can only hold so much in nonvolatile memory, thus we must
// flush the handle after we are done using it to prevent overloading.
// Again we defer the flush to after we are done using the object.
matt-tsai marked this conversation as resolved.
Show resolved Hide resolved
// It is generally good practice to defer the cleanup immediately
// after loading an object or creating an Authorization Session.
// See Part 1, Architecture, section 30.4
flushContext := FlushContext{FlushHandle: rspCP.ObjectHandle}
defer flushContext.Execute(thetpm)

// Fill in the ReadPublic struct.
// See definition in Part 3, Commands, section 12.4.
readPublic := ReadPublic{
ObjectHandle: rspCP.ObjectHandle,
}

// Executing the command uses reflection to pack the bytes into a
// TPM2_ReadPublic command, returns a TPM2_ReadPublic Response.
// This response is also decoded so you are again working with structs
// that can be found in Part 3, Commands, section 12.4.
rspRP, err := readPublic.Execute(thetpm)
if err != nil {
t.Fatalf("ReadPublic failed: %v", err)
}

// PublicArea.Unique represents the unique identifier of the TPMT.Public.
// Notice how this test uses verification of another TPM command that is
// able to produce similar results to validate the response.
rspCPUnique := rspCP.OutPublic.PublicArea.Unique
rspRPUnique := rspRP.OutPublic.PublicArea.Unique

if !cmp.Equal(rspCPUnique, rspRPUnique) {
t.Error("Mismatch between public returned from CreatePrimary & ReadPublic")
}
Expand Down