Skip to content

Commit

Permalink
Add new interface methods needed for ClassID
Browse files Browse the repository at this point in the history
Fix the failing CI Tests

Signed-off-by: Yogesh Deshpande <[email protected]>
  • Loading branch information
yogeshbdeshpande committed May 2, 2024
1 parent 822962c commit 89d797b
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 21 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ name: ci
on: [push, pull_request]
jobs:

# Test on various OS with default Go version.
# Test on various OS with specified Go version.
tests:
name: Test on ${{matrix.os}}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
steps:
- uses: actions/setup-go@v3
with:
go-version: "1.19"
- name: Checkout code
uses: actions/checkout@v2
with:
Expand All @@ -24,4 +27,4 @@ jobs:
- name: Run tests
run: |
go version
make test
make test
37 changes: 32 additions & 5 deletions comid/classid.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,44 @@ func (o *ClassID) SetUUID(uuid UUID) *ClassID {
return o
}

// SetOID sets the value of the targed ClassID to the supplied OID.
func (o ClassID) GetUUID() (UUID, error) {
switch t := o.Value.(type) {
case *TaggedUUID:
return UUID(*t), nil
case TaggedUUID:
return UUID(t), nil
default:
return UUID{}, fmt.Errorf("class-id type is: %T", t)
}
}

// SetOID sets the value of the target ClassID to the supplied OID.
// The OID is a string in dotted-decimal notation
func (o *ClassID) SetOID(s string) *ClassID {
func (o *ClassID) SetOID(s string) error {
if o != nil {
var berOID OID
if berOID.FromString(s) != nil {
return nil
if err := berOID.FromString(s); err != nil {
return err
}
o.Value = TaggedOID(berOID)
}
return o
return nil
}

// GetOID gets the value of the OID in a string dotted-decimal notation
func (o ClassID) GetOID() (string, error) {
switch t := o.Value.(type) {
case *TaggedOID:
oid := OID(*t)
stringOID := oid.String()
return stringOID, nil
case TaggedOID:
oid := OID(t)
stringOID := oid.String()
return stringOID, nil
default:
return "", fmt.Errorf("class-id type is: %T", t)
}
}

const ImplIDType = "psa.impl-id"
Expand Down
96 changes: 96 additions & 0 deletions comid/classid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,93 @@ func TestClassID_SetOID_ok(t *testing.T) {
}
}

func TestClassID_SetOID_GetOID_ok(t *testing.T) {
tvs := []string{
"1.2.3",
"1.2.3.4",
"1.2.3.4.5",
"1.2.3.4.5.6",
"1.2.3.4.5.6.7",
"1.2.3.4.5.6.7.8",
"1.2.3.4.5.6.7.8.9",
"1.2.3.4.5.6.7.8.9.10",
"1.2.3.4.5.6.7.8.9.10.11",
"1.2.3.4.5.6.7.8.9.10.11.12",
"1.2.3.4.5.6.7.8.9.10.11.12.13",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20",
}

for _, tv := range tvs {
c := &ClassID{}
err := c.SetOID(tv)
require.Nil(t, err)
out, err := c.GetOID()
require.NoError(t, err)
assert.Equal(t, tv, out)
}
}

func TestClassID_SetOID_NOK(t *testing.T) {
tvs := []struct {
desc string
input string
expectedErr string
}{
{
desc: "empyt OID",
input: "",
expectedErr: "empty OID",
},
{
desc: "too little OID",
input: "1",
expectedErr: "invalid OID: got 1 arcs, expecting at least 3",
},
{
desc: "still too little OID",
input: "1.2",
expectedErr: "invalid OID: got 2 arcs, expecting at least 3",
},
{
desc: "negative arc",
input: "1.2.-3",
expectedErr: "invalid OID: negative arc -3 not allowed",
},
{
desc: "not absolute",
input: ".1.2.3",
expectedErr: "OID must be absolute",
},
{
desc: "not dotted decimal",
input: "iso(1) org(3) dod(6) iana(1)",
expectedErr: `invalid OID: strconv.Atoi: parsing "iso(1) org(3) dod(6) iana(1)": invalid syntax`,
},
{
desc: "invalid format",
input: "1...",
expectedErr: `invalid OID: strconv.Atoi: parsing "": invalid syntax`,
},
{
desc: "invalid format, no digits",
input: "1...",
expectedErr: `invalid OID: strconv.Atoi: parsing "": invalid syntax`,
},
}
for _, tv := range tvs {
c := &ClassID{}
err := c.SetOID(tv.input)
fmt.Printf("Error = %s", err.Error())
assert.NotNil(t, err)
assert.EqualError(t, err, tv.expectedErr)
}
}
func TestClassID_SetOID_bad(t *testing.T) {
tvs := []string{
"", // empty
Expand All @@ -220,6 +307,15 @@ func TestClassID_SetOID_bad(t *testing.T) {
}
}

func TestClassID_SetUUID_GetUUID_OK(t *testing.T) {
class := &ClassID{}
class = class.SetUUID(TestUUID)
require.NotNil(t, class)
uuid, err := class.GetUUID()
require.NoError(t, err)
assert.Equal(t, TestUUID, uuid)
}

func Test_NewImplIDClassID(t *testing.T) {
classID, err := NewImplIDClassID(nil)
expected := [32]byte{}
Expand Down
8 changes: 4 additions & 4 deletions comid/example_cca_realm_refval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func extractMeasurements(m Measurements) error {
return fmt.Errorf("no measurements")
}

for i, m := range m {
if err := extractMeasurement(m); err != nil {
for i, meas := range m {

Check failure on line 87 in comid/example_cca_realm_refval_test.go

View workflow job for this annotation

GitHub Actions / Lint

rangeValCopy: each iteration copies 128 bytes (consider pointers or indexing) (gocritic)

Check failure on line 87 in comid/example_cca_realm_refval_test.go

View workflow job for this annotation

GitHub Actions / Lint

rangeValCopy: each iteration copies 128 bytes (consider pointers or indexing) (gocritic)
if err := extractMeasurement(meas); err != nil {
return fmt.Errorf("extracting measurement at index %d: %w", i, err)
}
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func extractIntegrityRegisters(r *IntegrityRegisters) error {
}

for _, k := range keys {
d, ok := r.m[k]
d, ok := r.M[k]
if !ok {
return fmt.Errorf("unable to locate register index for: %s", k)
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func extractRealmDigests(digests Digests) error {

func extractRegisterIndexes(r *IntegrityRegisters) ([]string, error) {
var keys [5]string
for k := range r.m {
for k := range r.M {
switch t := k.(type) {
case string:
key := strings.ToLower(t)
Expand Down
18 changes: 9 additions & 9 deletions comid/integrityregisters.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type IRegisterIndex interface{}

// IntegrityRegisters holds the Integrity Registers
type IntegrityRegisters struct {
m map[IRegisterIndex]Digests
M map[IRegisterIndex]Digests
}

func NewIntegrityRegisters() *IntegrityRegisters {
return &IntegrityRegisters{m: make(map[IRegisterIndex]Digests)}
return &IntegrityRegisters{M: make(map[IRegisterIndex]Digests)}
}

// AddDigests allows inserting an array of digests at a specific index
Expand All @@ -42,24 +42,24 @@ func (i *IntegrityRegisters) AddDigests(index IRegisterIndex, digests Digests) e
// AddDigest allows inserting a digest at a specific index
// Supported index types are uint and text
func (i *IntegrityRegisters) AddDigest(index IRegisterIndex, digest swid.HashEntry) error {
if i.m == nil {
if i.M == nil {
return fmt.Errorf("no register to add digest")
}
switch t := index.(type) {
case string, uint, uint64:
i.m[t] = append(i.m[t], digest)
i.M[t] = append(i.M[t], digest)
default:
return fmt.Errorf("unexpected type for index: %T", t)
}
return nil
}

func (i IntegrityRegisters) MarshalCBOR() ([]byte, error) {
return em.Marshal(i.m)
return em.Marshal(i.M)
}

func (i *IntegrityRegisters) UnmarshalCBOR(data []byte) error {
return dm.Unmarshal(data, &i.m)
return dm.Unmarshal(data, &i.M)
}

type keyTypeandVal struct {
Expand All @@ -70,7 +70,7 @@ type keyTypeandVal struct {
func (i IntegrityRegisters) MarshalJSON() ([]byte, error) {
jmap := make(map[string]json.RawMessage)
var newkey string
for key, val := range i.m {
for key, val := range i.M {
var ktv keyTypeandVal
switch t := key.(type) {
case uint, uint64:
Expand Down Expand Up @@ -98,8 +98,8 @@ func (i IntegrityRegisters) MarshalJSON() ([]byte, error) {
}

func (i *IntegrityRegisters) UnmarshalJSON(data []byte) error {
if i.m == nil {
i.m = make(map[IRegisterIndex]Digests)
if i.M == nil {
i.M = make(map[IRegisterIndex]Digests)
}
jmap := make(map[string]json.RawMessage)
var index IRegisterIndex
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/veraison/corim

go 1.18
go 1.19

require (
github.com/fxamacker/cbor/v2 v2.5.0
Expand Down

0 comments on commit 89d797b

Please sign in to comment.