Skip to content

Commit

Permalink
testutil: stop using deprecated CommandPacket and ResponsePacket
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisccoulson committed Jan 12, 2024
1 parent d499036 commit 1e2d75c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 63 deletions.
3 changes: 3 additions & 0 deletions testutil/suites.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,21 @@ type CommandRecordC struct {
*CommandRecord
}

// Deprecated: use [CommandRecord.CmdCode].
func (r *CommandRecordC) GetCommandCode(c *C) tpm2.CommandCode {
code, err := r.CommandRecord.GetCommandCode()
c.Assert(err, IsNil)
return code
}

// Deprecated: use [CommandRecord.CmdHandles], [CommandRecord.CmdAuthArea] and [CommandRecord.CpBytes].
func (r *CommandRecordC) UnmarshalCommand(c *C) (handles tpm2.HandleList, authArea []tpm2.AuthCommand, parameters []byte) {
handles, authArea, parameters, err := r.CommandRecord.UnmarshalCommand()
c.Assert(err, IsNil)
return handles, authArea, parameters
}

// Deprecated: use [CommandRecord.RspCode], [CommandRecord.RspHandle], [CommandRecord.RpBytes] and [CommandRecord.RspAuthArea].
func (r *CommandRecordC) UnmarshalResponse(c *C) (rc tpm2.ResponseCode, handle tpm2.Handle, parameters []byte, authArea []tpm2.AuthResponse) {
rc, handle, parameters, authArea, err := r.CommandRecord.UnmarshalResponse()
c.Assert(err, IsNil)
Expand Down
40 changes: 19 additions & 21 deletions testutil/suites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,36 +367,34 @@ func (s *tpmTestSuiteProper) TestCommandLog(c *C) {

c.Assert(s.CommandLog(), internal_testutil.LenEquals, 2)

c.Check(s.CommandLog()[0].GetCommandCode(c), Equals, tpm2.CommandSelfTest)
cHandles, cAuthArea, cpBytes := s.CommandLog()[0].UnmarshalCommand(c)
c.Check(cHandles, internal_testutil.LenEquals, 0)
c.Check(cAuthArea, internal_testutil.LenEquals, 0)
cmd := s.CommandLog()[0]
c.Check(cmd.CmdCode, Equals, tpm2.CommandSelfTest)
c.Check(cmd.CmdHandles, internal_testutil.LenEquals, 0)
c.Check(cmd.CmdAuthArea, internal_testutil.LenEquals, 0)

var fullTest bool
_, err = mu.UnmarshalFromBytes(cpBytes, &fullTest)
_, err = mu.UnmarshalFromBytes(cmd.CpBytes, &fullTest)
c.Check(err, IsNil)
c.Check(fullTest, internal_testutil.IsTrue)

rc, rHandle, rpBytes, rAuthArea := s.CommandLog()[0].UnmarshalResponse(c)
c.Check(rc, Equals, tpm2.ResponseSuccess)
c.Check(rHandle, Equals, tpm2.HandleUnassigned)
c.Check(rpBytes, internal_testutil.LenEquals, 0)
c.Check(rAuthArea, internal_testutil.LenEquals, 0)
c.Check(cmd.RspCode, Equals, tpm2.ResponseSuccess)
c.Check(cmd.RspHandle, Equals, tpm2.HandleUnassigned)
c.Check(cmd.RpBytes, internal_testutil.LenEquals, 0)
c.Check(cmd.RspAuthArea, internal_testutil.LenEquals, 0)

c.Check(s.CommandLog()[1].GetCommandCode(c), Equals, tpm2.CommandGetTestResult)
cHandles, cAuthArea, cpBytes = s.CommandLog()[1].UnmarshalCommand(c)
c.Check(cHandles, internal_testutil.LenEquals, 0)
c.Check(cAuthArea, internal_testutil.LenEquals, 0)
c.Check(cpBytes, internal_testutil.LenEquals, 0)
cmd = s.CommandLog()[1]
c.Check(cmd.CmdCode, Equals, tpm2.CommandGetTestResult)
c.Check(cmd.CmdHandles, internal_testutil.LenEquals, 0)
c.Check(cmd.CmdAuthArea, internal_testutil.LenEquals, 0)
c.Check(cmd.CpBytes, internal_testutil.LenEquals, 0)

rc, rHandle, rpBytes, rAuthArea = s.CommandLog()[1].UnmarshalResponse(c)
c.Check(rc, Equals, tpm2.ResponseSuccess)
c.Check(rHandle, Equals, tpm2.HandleUnassigned)
c.Check(rAuthArea, internal_testutil.LenEquals, 0)
c.Check(cmd.RspCode, Equals, tpm2.ResponseSuccess)
c.Check(cmd.RspHandle, Equals, tpm2.HandleUnassigned)
c.Check(cmd.RspAuthArea, internal_testutil.LenEquals, 0)

var outData2 tpm2.MaxBuffer
var testResult2 tpm2.ResponseCode
_, err = mu.UnmarshalFromBytes(rpBytes, &outData2, &testResult2)
_, err = mu.UnmarshalFromBytes(cmd.RpBytes, &outData2, &testResult2)
c.Check(err, IsNil)
c.Check(outData2, DeepEquals, outData)
c.Check(testResult2, Equals, testResult)
Expand All @@ -407,7 +405,7 @@ func (s *tpmTestSuiteProper) TestLastCommand(c *C) {
_, _, err := s.TPM.GetTestResult()
c.Check(err, IsNil)

c.Check(s.LastCommand(c).GetCommandCode(c), Equals, tpm2.CommandGetTestResult)
c.Check(s.LastCommand(c).CmdCode, Equals, tpm2.CommandGetTestResult)
}

func (s *tpmTestSuiteProper) TestForgetCommands(c *C) {
Expand Down
50 changes: 30 additions & 20 deletions testutil/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ type daParams struct {

type cmdContext struct {
info *commandInfo
command []byte
code tpm2.CommandCode
handles tpm2.HandleList
authArea []tpm2.AuthCommand
Expand All @@ -218,39 +217,42 @@ var savedObjects []*savedObject
// CommandRecord provides information about a command executed via
// the Transport interface.
type CommandRecord struct {
cmdInfo *commandInfo
commandPacket tpm2.CommandPacket
responsePacket tpm2.ResponsePacket
CmdCode tpm2.CommandCode
CmdHandles tpm2.HandleList
CmdAuthArea []tpm2.AuthCommand
CpBytes []byte

RspCode tpm2.ResponseCode
RspHandle tpm2.Handle // Will be tpm2.HandleUnassigned if no handle was returned
RpBytes []byte
RspAuthArea []tpm2.AuthResponse
}

// GetCommandCode returns the command code associated with this record.
//
// Deprecated: use the CmdCode field.
func (r *CommandRecord) GetCommandCode() (tpm2.CommandCode, error) {
return r.commandPacket.GetCommandCode()
return r.CmdCode, nil
}

// UnmarshalCommand unmarshals the command packet associated with this
// record, returning the handles, auth area and parameters. The parameters
// will still be in the TPM wire format.
//
// Deprecated: use the CmdHandles, CmdAuthArea and CpBytes fields.
func (r *CommandRecord) UnmarshalCommand() (handles tpm2.HandleList, authArea []tpm2.AuthCommand, parameters []byte, err error) {
return r.commandPacket.Unmarshal(r.cmdInfo.cmdHandles)
return r.CmdHandles, r.CmdAuthArea, r.CpBytes, nil
}

// UnmarshalResponse unmarshals the response packet associated with this
// record, returning the response code, handle, parameters and auth area.
// The parameters will still be in the TPM wire format. For commands that
// don't respond with a handle, the returned handle will be
// tpm2.HandleUnassigned.
//
// Deprecated: use the RspCode, RspHandle, RpBytes and RspAuthArea fields.
func (r *CommandRecord) UnmarshalResponse() (rc tpm2.ResponseCode, handle tpm2.Handle, parameters []byte, authArea []tpm2.AuthResponse, err error) {
handle = tpm2.HandleUnassigned
var pHandle *tpm2.Handle
if r.cmdInfo.rspHandle {
pHandle = &handle
}
rc, parameters, authArea, err = r.responsePacket.Unmarshal(pHandle)
if err != nil {
return 0, handle, nil, nil, err
}
return rc, handle, parameters, authArea, nil
return r.RspCode, r.RspHandle, r.RpBytes, r.RspAuthArea, nil
}

// TCTI is a special proxy inteface used for testing, which wraps a real interface.
Expand Down Expand Up @@ -310,22 +312,31 @@ func (t *Transport) processCommandDoneIfPossible() error {
return nil
}

var rHandle tpm2.Handle
rHandle := tpm2.HandleUnassigned
var pHandle *tpm2.Handle

// Try to unpack the response packet
if cmd.info.rspHandle {
pHandle = &rHandle
}
rc, rpBytes, _, err := tpm2.ReadResponsePacket(bytes.NewReader(cmd.response.Bytes()), pHandle)
rc, rpBytes, rAuthArea, err := tpm2.ReadResponsePacket(bytes.NewReader(cmd.response.Bytes()), pHandle)
if err != nil {
return fmt.Errorf("cannot unmarshal response: %w", err)
}

t.currentCmd = nil

if !t.disableCommandLogging {
t.CommandLog = append(t.CommandLog, &CommandRecord{cmd.info, cmd.command, cmd.response.Bytes()})
t.CommandLog = append(t.CommandLog, &CommandRecord{
CmdCode: cmd.code,
CmdHandles: cmd.handles,
CmdAuthArea: cmd.authArea,
CpBytes: cmd.pBytes,
RspCode: rc,
RspHandle: rHandle,
RpBytes: rpBytes,
RspAuthArea: rAuthArea,
})
}

if rc != tpm2.ResponseSuccess {
Expand Down Expand Up @@ -697,7 +708,6 @@ func (t *Transport) sendCommand(data []byte) (int, error) {

t.currentCmd = &cmdContext{
info: &cmdInfo,
command: data,
code: hdr.CommandCode,
handles: handles,
authArea: authArea,
Expand Down
40 changes: 18 additions & 22 deletions testutil/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,15 @@ func (s *transportSuite) TestCommandLog(c *C) {

c.Check(s.CommandLog(), internal_testutil.LenEquals, 2)

cmd := s.CommandLog()[0].GetCommandCode(c)
c.Check(cmd, Equals, tpm2.CommandLoadExternal)
cmdHandles, cmdAuthArea, cpBytes := s.CommandLog()[0].UnmarshalCommand(c)
c.Check(cmdHandles, internal_testutil.LenEquals, 0)
c.Check(cmdAuthArea, internal_testutil.LenEquals, 0)
cmd := s.CommandLog()[0]
c.Check(cmd.CmdCode, Equals, tpm2.CommandLoadExternal)
c.Check(cmd.CmdHandles, internal_testutil.LenEquals, 0)
c.Check(cmd.CmdAuthArea, internal_testutil.LenEquals, 0)

var inSensitiveBytes []byte
var inPublicBytes []byte
var hierarchy tpm2.Handle
_, err = mu.UnmarshalFromBytes(cpBytes, &inSensitiveBytes, &inPublicBytes, &hierarchy)
_, err = mu.UnmarshalFromBytes(cmd.CpBytes, &inSensitiveBytes, &inPublicBytes, &hierarchy)
c.Check(err, IsNil)
var inPublic *tpm2.Public
_, err = mu.UnmarshalFromBytes(inPublicBytes, &inPublic)
Expand All @@ -148,39 +147,36 @@ func (s *transportSuite) TestCommandLog(c *C) {
c.Check(inPublic, TPMValueDeepEquals, &public)
c.Check(hierarchy, Equals, tpm2.HandleOwner)

rc, rHandle, rpBytes, rspAuthArea := s.CommandLog()[0].UnmarshalResponse(c)
c.Check(rHandle, Equals, object.Handle())
c.Check(rc, Equals, tpm2.ResponseSuccess)
c.Check(rspAuthArea, internal_testutil.LenEquals, 0)
c.Check(cmd.RspHandle, Equals, object.Handle())
c.Check(cmd.RspCode, Equals, tpm2.ResponseSuccess)
c.Check(cmd.RspAuthArea, internal_testutil.LenEquals, 0)

var name tpm2.Name
_, err = mu.UnmarshalFromBytes(rpBytes, &name)
_, err = mu.UnmarshalFromBytes(cmd.RpBytes, &name)
c.Check(err, IsNil)
c.Check(name, DeepEquals, object.Name())

cmd = s.CommandLog()[1].GetCommandCode(c)
c.Check(cmd, Equals, tpm2.CommandGetCapability)
cmdHandles, cmdAuthArea, cpBytes = s.CommandLog()[1].UnmarshalCommand(c)
c.Check(cmdHandles, internal_testutil.LenEquals, 0)
c.Check(cmdAuthArea, internal_testutil.LenEquals, 0)
cmd = s.CommandLog()[1]
c.Check(cmd.CmdCode, Equals, tpm2.CommandGetCapability)
c.Check(cmd.CmdHandles, internal_testutil.LenEquals, 0)
c.Check(cmd.CmdAuthArea, internal_testutil.LenEquals, 0)

var capability tpm2.Capability
var property uint32
var propertyCount uint32
_, err = mu.UnmarshalFromBytes(cpBytes, &capability, &property, &propertyCount)
_, err = mu.UnmarshalFromBytes(cmd.CpBytes, &capability, &property, &propertyCount)
c.Check(err, IsNil)
c.Check(capability, Equals, tpm2.CapabilityHandles)
c.Check(property, Equals, uint32(object.Handle()))
c.Check(propertyCount, Equals, uint32(1))

rc, rHandle, rpBytes, rspAuthArea = s.CommandLog()[1].UnmarshalResponse(c)
c.Check(rc, Equals, tpm2.ResponseSuccess)
c.Check(rHandle, Equals, tpm2.HandleUnassigned)
c.Check(rspAuthArea, internal_testutil.LenEquals, 0)
c.Check(cmd.RspCode, Equals, tpm2.ResponseSuccess)
c.Check(cmd.RspHandle, Equals, tpm2.HandleUnassigned)
c.Check(cmd.RspAuthArea, internal_testutil.LenEquals, 0)

var moreData bool
var capabilityData tpm2.CapabilityData
_, err = mu.UnmarshalFromBytes(rpBytes, &moreData, &capabilityData)
_, err = mu.UnmarshalFromBytes(cmd.RpBytes, &moreData, &capabilityData)
c.Check(err, IsNil)
c.Check(moreData, Equals, false)
c.Check(capabilityData, DeepEquals, tpm2.CapabilityData{
Expand Down

0 comments on commit 1e2d75c

Please sign in to comment.