Skip to content

Commit

Permalink
Further correction of InstanceID names
Browse files Browse the repository at this point in the history
Signed-off-by: Yogesh Deshpande <[email protected]>
  • Loading branch information
yogeshbdeshpande committed Jan 28, 2025
1 parent 5f38f15 commit d596f09
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
14 changes: 7 additions & 7 deletions comid/tdx-profile/example_pce_refval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,20 @@ func decodePCEMValExtensions(m *comid.Measurement) error {
fmt.Printf("val was not pointer to teeInstanceID")
}

if i.IsBytesInstanceID() {
val, err = i.GetBytesInstanceID()
if i.IsBytesTeeInstanceID() {
val, err = i.GetBytesTeeInstanceID()
if err != nil {
return fmt.Errorf("failed to decode instanceid: %w", err)
return fmt.Errorf("failed to decode teeinstanceid: %w", err)
}
fmt.Printf("\nInstanceID: %x", val)
} else if i.IsUintInstanceID() {
val, err = i.GetUintInstanceID()
} else if i.IsUintTeeInstanceID() {
val, err = i.GetUintTeeInstanceID()
if err != nil {
return fmt.Errorf("failed to decode instanceid: %w", err)
return fmt.Errorf("failed to decode teeinstanceid: %w", err)
}
fmt.Printf("\nInstanceID: %d", val)
} else {
return fmt.Errorf("instanceid is neither integer or byte string")
return fmt.Errorf("teeinstanceid is neither integer or byte string")
}

val, err = m.Val.Extensions.Get("tcbcompsvn")
Expand Down
2 changes: 1 addition & 1 deletion comid/tdx-profile/teeadvisoryids.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (o *TeeAdvisoryIDs) AddTeeAdvisoryIDs(val []any) error {
return nil
}

// Valid checks for validity of AdvisoryIDs and
// Valid checks for validity of TeeAdvisoryIDs and
// returns an error, if invalid
func (o TeeAdvisoryIDs) Valid() error {
if len(o) == 0 {
Expand Down
40 changes: 23 additions & 17 deletions comid/tdx-profile/teeinstanceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type TeeInstanceID struct {
val interface{}
}

// NewteeInstanceID creates a new InstanceID from the
// supplied interface. The supported types are positive integers and
// byte array
func NewTeeInstanceID(val interface{}) *TeeInstanceID {
switch t := val.(type) {
case uint, uint64:
Expand All @@ -30,6 +33,7 @@ func NewTeeInstanceID(val interface{}) *TeeInstanceID {
}
}

// SetTeeInstanceID sets the supplied value of Instance ID
func (o *TeeInstanceID) SetTeeInstanceID(val interface{}) error {
switch t := val.(type) {
case uint, uint64:
Expand All @@ -38,59 +42,61 @@ func (o *TeeInstanceID) SetTeeInstanceID(val interface{}) error {
o.val = val
case int:
if t < 0 {
return fmt.Errorf("unsupported negative InstanceID: %d", t)
return fmt.Errorf("unsupported negative TeeInstanceID: %d", t)
}
o.val = val
default:
return fmt.Errorf("unsupported InstanceID type: %T", t)
return fmt.Errorf("unsupported TeeInstanceID type: %T", t)
}
return nil
}

// valid checks for validity of TeeInstanceID and
// returns an error if Invalid
func (o TeeInstanceID) Valid() error {
if o.val == nil {
return fmt.Errorf("empty InstanceID")
return fmt.Errorf("empty TeeInstanceID")
}
switch t := o.val.(type) {
case uint, uint64:
return nil
case []byte:
if len(t) == 0 {
return fmt.Errorf("empty InstanceID")
return fmt.Errorf("empty TeeInstanceID")
}
case int:
if t < 0 {
return fmt.Errorf("unsupported negative InstanceID: %d", t)
return fmt.Errorf("unsupported negative TeeInstanceID: %d", t)
}
default:
return fmt.Errorf("unsupported InstanceID type: %T", t)
return fmt.Errorf("unsupported TeeInstanceID type: %T", t)
}
return nil
}

func (o TeeInstanceID) GetUintInstanceID() (uint, error) {
func (o TeeInstanceID) GetUintTeeInstanceID() (uint, error) {
switch t := o.val.(type) {
case uint64:
return uint(t), nil
case uint:
return t, nil
default:
return 0, fmt.Errorf("InstanceID type is: %T", t)
return 0, fmt.Errorf("TeeInstanceID type is: %T", t)
}
}

func (o TeeInstanceID) GetBytesInstanceID() ([]byte, error) {
func (o TeeInstanceID) GetBytesTeeInstanceID() ([]byte, error) {
switch t := o.val.(type) {
case []byte:
if len(t) == 0 {
return nil, fmt.Errorf("InstanceID type is of zero length")
return nil, fmt.Errorf("TeeInstanceID type is of zero length")
}
return t, nil
default:
return nil, fmt.Errorf("InstanceID type is: %T", t)
return nil, fmt.Errorf("TeeInstanceID type is: %T", t)
}
}
func (o TeeInstanceID) IsBytesInstanceID() bool {
func (o TeeInstanceID) IsBytesTeeInstanceID() bool {
switch o.val.(type) {
case []byte:
return true
Expand All @@ -99,7 +105,7 @@ func (o TeeInstanceID) IsBytesInstanceID() bool {
}
}

func (o TeeInstanceID) IsUintInstanceID() bool {
func (o TeeInstanceID) IsUintTeeInstanceID() bool {
switch o.val.(type) {
case uint64, uint:
return true
Expand All @@ -111,7 +117,7 @@ func (o TeeInstanceID) IsUintInstanceID() bool {
func (o TeeInstanceID) MarshalJSON() ([]byte, error) {

if o.Valid() != nil {
return nil, fmt.Errorf("invalid InstanceID")
return nil, fmt.Errorf("invalid TeeInstanceID")
}
var (
v encoding.TypeAndValue
Expand All @@ -132,7 +138,7 @@ func (o TeeInstanceID) MarshalJSON() ([]byte, error) {
}
v = encoding.TypeAndValue{Type: "bytes", Value: b}
default:
return nil, fmt.Errorf("unknown type %T for InstanceID", t)
return nil, fmt.Errorf("unknown type %T for TeeInstanceID", t)
}
return json.Marshal(v)
}
Expand All @@ -149,14 +155,14 @@ func (o *TeeInstanceID) UnmarshalJSON(data []byte) error {
var x uint
if err := json.Unmarshal(v.Value, &x); err != nil {
return fmt.Errorf(
"cannot unmarshal InstanceID of type uint: %w", err)
"cannot unmarshal TeeInstanceID of type uint: %w", err)
}
o.val = x
case "bytes":
var x []byte
if err := json.Unmarshal(v.Value, &x); err != nil {
return fmt.Errorf(
"cannot unmarshal InstanceID of type bytes: %w", err)
"cannot unmarshal TeeInstanceID of type bytes: %w", err)
}
o.val = x
}
Expand Down
16 changes: 8 additions & 8 deletions comid/tdx-profile/teeinstanceid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func TestTeeInstanceID_SetTeeInstanceID_OK(t *testing.T) {

func TestTeeInstanceID_SetTeeInstanceID_NOK(t *testing.T) {
inst := &TeeInstanceID{}
expectedErr := "unsupported negative InstanceID: -1"
expectedErr := "unsupported negative TeeInstanceID: -1"
err := inst.SetTeeInstanceID(-1)
assert.EqualError(t, err, expectedErr)
expectedErr = "unsupported InstanceID type: float64"
expectedErr = "unsupported TeeInstanceID type: float64"
err = inst.SetTeeInstanceID(-1.234)
assert.EqualError(t, err, expectedErr)
}
Expand All @@ -69,22 +69,22 @@ func TestTeeInstanceID_Valid_NOK(t *testing.T) {
{
desc: "unsupported type negative integer",
input: -1,
expectedErr: "unsupported negative InstanceID: -1",
expectedErr: "unsupported negative TeeInstanceID: -1",
},
{
desc: "non existent InstanceID",
desc: "non existent TeeInstanceID",
input: nil,
expectedErr: "empty InstanceID",
expectedErr: "empty TeeInstanceID",
},
{
desc: "non existent InstanceID",
desc: "non existent TeeInstanceID",
input: []byte{},
expectedErr: "empty InstanceID",
expectedErr: "empty TeeInstanceID",
},
{
desc: "unsupported type float64",
input: 1.234,
expectedErr: "unsupported InstanceID type: float64",
expectedErr: "unsupported TeeInstanceID type: float64",
},
}

Expand Down

0 comments on commit d596f09

Please sign in to comment.