From 89d797b3a919e8eca688b99c12d774c110d2701d Mon Sep 17 00:00:00 2001 From: Yogesh Deshpande Date: Tue, 23 Apr 2024 07:06:21 -0400 Subject: [PATCH] Add new interface methods needed for ClassID Fix the failing CI Tests Signed-off-by: Yogesh Deshpande --- .github/workflows/ci.yml | 7 +- comid/classid.go | 37 ++++++++-- comid/classid_test.go | 96 ++++++++++++++++++++++++++ comid/example_cca_realm_refval_test.go | 8 +-- comid/integrityregisters.go | 18 ++--- go.mod | 2 +- 6 files changed, 147 insertions(+), 21 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 322b5d77..4d9b9133 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,7 @@ 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 }} @@ -12,6 +12,9 @@ jobs: 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: @@ -24,4 +27,4 @@ jobs: - name: Run tests run: | go version - make test + make test \ No newline at end of file diff --git a/comid/classid.go b/comid/classid.go index 4cf6f631..feb375e9 100644 --- a/comid/classid.go +++ b/comid/classid.go @@ -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" diff --git a/comid/classid_test.go b/comid/classid_test.go index 28a9ce38..f1fe7abc 100644 --- a/comid/classid_test.go +++ b/comid/classid_test.go @@ -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 @@ -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{} diff --git a/comid/example_cca_realm_refval_test.go b/comid/example_cca_realm_refval_test.go index 1ef644ef..c4b83b8d 100644 --- a/comid/example_cca_realm_refval_test.go +++ b/comid/example_cca_realm_refval_test.go @@ -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 { + if err := extractMeasurement(meas); err != nil { return fmt.Errorf("extracting measurement at index %d: %w", i, err) } } @@ -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) } @@ -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) diff --git a/comid/integrityregisters.go b/comid/integrityregisters.go index e0aec76f..ab953396 100644 --- a/comid/integrityregisters.go +++ b/comid/integrityregisters.go @@ -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 @@ -42,12 +42,12 @@ 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) } @@ -55,11 +55,11 @@ func (i *IntegrityRegisters) AddDigest(index IRegisterIndex, digest swid.HashEnt } 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 { @@ -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: @@ -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 diff --git a/go.mod b/go.mod index 96a5aa49..6533eb88 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/veraison/corim -go 1.18 +go 1.19 require ( github.com/fxamacker/cbor/v2 v2.5.0