Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
setrofim committed Sep 28, 2023
1 parent c77a95c commit 78fb6ee
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 120 deletions.
2 changes: 1 addition & 1 deletion cocli/cmd/corimDisplay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Test_CorimDisplayCmd_invalid_signed_corim(t *testing.T) {
require.NoError(t, err)

err = cmd.Execute()
assert.EqualError(t, err, "error decoding signed CoRIM from invalid.cbor: failed validation of unsigned CoRIM: empty id")
assert.EqualError(t, err, `error decoding signed CoRIM from invalid.cbor: failed CBOR decoding of unsigned CoRIM: missing mandatory field "ID" (0)`)
}

func Test_CorimDisplayCmd_ok_top_level_view(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cocli/cmd/corimExtract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Test_CorimExtractCmd_invalid_signed_corim(t *testing.T) {
require.NoError(t, err)

err = cmd.Execute()
assert.EqualError(t, err, "error decoding signed CoRIM from invalid.cbor: failed validation of unsigned CoRIM: empty id")
assert.EqualError(t, err, `error decoding signed CoRIM from invalid.cbor: failed CBOR decoding of unsigned CoRIM: missing mandatory field "ID" (0)`)
}

func Test_CorimExtractCmd_ok_save_to_default_dir(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cocli/cmd/corimSign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func Test_CorimSignCmd_invalid_unsigned_corim(t *testing.T) {
require.NoError(t, err)

err = cmd.Execute()
assert.EqualError(t, err, "error validating CoRIM: tags validation failed: no tags")
assert.EqualError(t, err, `error decoding unsigned CoRIM from invalid.cbor: missing mandatory field "Tags" (1)`)
}

func Test_CorimSignCmd_non_existent_meta_file(t *testing.T) {
Expand Down
9 changes: 7 additions & 2 deletions corim/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/veraison/corim/comid"
"github.com/veraison/corim/encoding"
"github.com/veraison/corim/extensions"
)

// Entity stores an entity-map capable of CBOR and JSON serializations.
Expand All @@ -23,11 +24,13 @@ func NewEntity() *Entity {
return &Entity{}
}

func (o *Entity) RegisterExtensions(exts IExtensionsValue) {
// RegisterExtensions registers a struct as a collections of extensions
func (o *Entity) RegisterExtensions(exts extensions.IExtensionsValue) {
o.Extensions.IExtensionsValue = exts
}

func (o *Entity) GetExtensions() IExtensionsValue {
// GetExtensions returns pervisouosly registered extension
func (o *Entity) GetExtensions() extensions.IExtensionsValue {
return o.Extensions.IExtensionsValue
}

Expand Down Expand Up @@ -86,10 +89,12 @@ func (o Entity) Valid() error {
return o.Extensions.ValidEntity(&o)
}

// UnarshalCBOR deserializes from CBOR
func (o *Entity) UnmarshalCBOR(data []byte) error {
return encoding.PopulateStructFromCBOR(dm, data, o)
}

// MarshalCBOR serializes to CBOR
func (o *Entity) MarshalCBOR() ([]byte, error) {
return encoding.SerializeStructToCBOR(em, o)
}
Expand Down
134 changes: 26 additions & 108 deletions corim/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
package corim

import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/veraison/corim/extensions"
)

var ErrExtensionNotFound = errors.New("extension not found")

type IExtensionsValue interface{}

type IEntityValidator interface {
ValidEntity(*Entity) error
}

type ICorimValidator interface {
ValidCorim(*UnsignedCorim) error
}

type ISignerValidator interface {
ValidSigner(*Signer) error
}

type Extensions struct {
IExtensionsValue
extensions.Extensions
}

func (o *Extensions) ValidEntity(entity *Entity) error {
Expand All @@ -36,115 +37,32 @@ func (o *Extensions) ValidEntity(entity *Entity) error {
return nil
}

func (o *Extensions) HaveExtensions() bool {
return o.IExtensionsValue != nil
}

func (o *Extensions) Get(name string) (any, error) {
if o.IExtensionsValue == nil {
return nil, fmt.Errorf("%w: %s", ErrExtensionNotFound, name)
}

extType := reflect.TypeOf(o.IExtensionsValue)
extVal := reflect.ValueOf(o.IExtensionsValue)
if extType.Kind() == reflect.Pointer {
extType = extType.Elem()
extVal = extVal.Elem()
func (o *Extensions) ValidCorim(c *UnsignedCorim) error {
if !o.HaveExtensions() {
return nil
}

var fieldName, fieldJSONTag, fieldCBORTag string
for i := 0; i < extVal.NumField(); i++ {
typeField := extType.Field(i)
fieldName = typeField.Name

tag, ok := typeField.Tag.Lookup("json")
if ok {
fieldJSONTag = strings.Split(tag, ",")[0]
}

tag, ok = typeField.Tag.Lookup("cbor")
if ok {
fieldCBORTag = strings.Split(tag, ",")[0]
}

if fieldName == name || fieldJSONTag == name || fieldCBORTag == name {
return extVal.Field(i).Interface(), nil
ev, ok := o.IExtensionsValue.(ICorimValidator)
if ok {
if err := ev.ValidCorim(c); err != nil {
return err
}
}

return nil, fmt.Errorf("%w: %s", ErrExtensionNotFound, name)
}

func (o *Extensions) GetString(name string) (string, error) {
v, err := o.Get(name)
if err != nil {
return "", err
}

switch t := v.(type) {
case string:
return t, nil
default:
return fmt.Sprintf("%v", t), nil
}
}

func (o *Extensions) GetInt(name string) (int64, error) {
v, err := o.Get(name)
if err != nil {
return 0, err
}

val := reflect.ValueOf(v)
if val.CanInt() {
return val.Int(), nil
}

return 0, fmt.Errorf("%s is not an integer: %v (%T)", name, v, v)
return nil
}

func (o *Extensions) Set(name string, value any) error {
if o.IExtensionsValue == nil {
return fmt.Errorf("%w: %s", ErrExtensionNotFound, name)
}

extType := reflect.TypeOf(o.IExtensionsValue)
extVal := reflect.ValueOf(o.IExtensionsValue)
if extType.Kind() == reflect.Pointer {
extType = extType.Elem()
extVal = extVal.Elem()
func (o *Extensions) ValidSigner(signer *Signer) error {
if !o.HaveExtensions() {
return nil
}

var fieldName, fieldJSONTag, fieldCBORTag string
for i := 0; i < extVal.NumField(); i++ {
typeField := extType.Field(i)
valField := extVal.Field(i)
fieldName = typeField.Name

tag, ok := typeField.Tag.Lookup("json")
if ok {
fieldJSONTag = strings.Split(tag, ",")[0]
}

tag, ok = typeField.Tag.Lookup("cbor")
if ok {
fieldCBORTag = strings.Split(tag, ",")[0]
}

if fieldName == name || fieldJSONTag == name || fieldCBORTag == name {
newVal := reflect.ValueOf(value)
if newVal.CanConvert(valField.Type()) {
valField.Set(newVal.Convert(valField.Type()))
return nil
}

return fmt.Errorf(
"cannot set field %q (of type %s) to %v (%T)",
name, typeField.Type.Name(),
value, value,
)
ev, ok := o.IExtensionsValue.(ISignerValidator)
if ok {
if err := ev.ValidSigner(signer); err != nil {
return err
}
}

return fmt.Errorf("%w: %s", ErrExtensionNotFound, name)
return nil
}
3 changes: 2 additions & 1 deletion corim/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/fxamacker/cbor/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/veraison/corim/extensions"
)

type TestExtensions struct {
Expand All @@ -29,7 +30,7 @@ func TestEntityExtensions_GetSet(t *testing.T) {
Address: "742 Evergreen Terrace",
Size: 6,
}
exts := &Extensions{&extsVal}
exts := &Extensions{Extensions: extensions.Extensions{IExtensionsValue: &extsVal}}

v, err := exts.GetInt("size")
assert.NoError(t, err)
Expand Down
26 changes: 25 additions & 1 deletion corim/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ import (
"time"

"github.com/veraison/corim/comid"
"github.com/veraison/corim/encoding"
"github.com/veraison/corim/extensions"
)

type Signer struct {
Name string `cbor:"0,keyasint" json:"name"`
URI *comid.TaggedURI `cbor:"1,keyasint,omitempty" json:"uri,omitempty"`

Extensions
}

func NewSigner() *Signer {
return &Signer{}
}

// RegisterExtensions registers a struct as a collections of extensions
func (o *Signer) RegisterExtensions(exts extensions.IExtensionsValue) {
o.Extensions.IExtensionsValue = exts
}

// GetExtensions returns pervisouosly registered extension
func (o *Signer) GetExtensions() extensions.IExtensionsValue {
return o.Extensions.IExtensionsValue
}

// SetName sets the target Signer's name to the supplied value
func (o *Signer) SetName(name string) *Signer {
if o != nil {
Expand Down Expand Up @@ -61,7 +75,17 @@ func (o Signer) Valid() error {
}
}

return nil
return o.Extensions.ValidSigner(&o)
}

// UnarshalCBOR deserializes from CBOR
func (o *Signer) UnmarshalCBOR(data []byte) error {
return encoding.PopulateStructFromCBOR(dm, data, o)
}

// MarshalCBOR serializes to CBOR
func (o *Signer) MarshalCBOR() ([]byte, error) {
return encoding.SerializeStructToCBOR(em, o)
}

// Meta stores a corim-meta-map with JSON and CBOR serializations. It carries
Expand Down
2 changes: 1 addition & 1 deletion corim/signedcorim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func TestSignedCorim_FromCOSE_fail_invalid_corim(t *testing.T) {
var actual SignedCorim
err := actual.FromCOSE(tv)

assert.EqualError(t, err, "failed validation of unsigned CoRIM: tags validation failed: no tags")
assert.EqualError(t, err, `failed CBOR decoding of unsigned CoRIM: missing mandatory field "Tags" (1)`)
}

func TestSignedCorim_FromCOSE_fail_no_content_type(t *testing.T) {
Expand Down
22 changes: 18 additions & 4 deletions corim/unsignedcorim.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"

"github.com/veraison/corim/cots"
"github.com/veraison/corim/encoding"
"github.com/veraison/corim/extensions"

"github.com/veraison/corim/comid"
"github.com/veraison/eat"
Expand All @@ -25,13 +27,25 @@ type UnsignedCorim struct {
Profiles *[]eat.Profile `cbor:"3,keyasint,omitempty" json:"profiles,omitempty"`
RimValidity *Validity `cbor:"4,keyasint,omitempty" json:"validity,omitempty"`
Entities *Entities `cbor:"5,keyasint,omitempty" json:"entities,omitempty"`

Extensions
}

// NewUnsignedCorim instantiates an empty UnsignedCorim
func NewUnsignedCorim() *UnsignedCorim {
return &UnsignedCorim{}
}

// RegisterExtensions registers a struct as a collections of extensions
func (o *UnsignedCorim) RegisterExtensions(exts extensions.IExtensionsValue) {
o.Extensions.IExtensionsValue = exts
}

// GetExtensions returns pervisouosly registered extension
func (o *UnsignedCorim) GetExtensions() extensions.IExtensionsValue {
return o.Extensions.IExtensionsValue
}

// SetID sets the corim-id in the unsigned-corim-map to the supplied value. The
// corim-id can be passed as UUID in string or binary form (i.e., byte array),
// or as a (non-empty) string
Expand Down Expand Up @@ -239,17 +253,17 @@ func (o UnsignedCorim) Valid() error {
}
}

return nil
return o.Extensions.ValidCorim(&o)
}

// ToCBOR serializes the target unsigned CoRIM to CBOR
func (o UnsignedCorim) ToCBOR() ([]byte, error) {
return em.Marshal(&o)
func (o *UnsignedCorim) ToCBOR() ([]byte, error) {
return encoding.SerializeStructToCBOR(em, o)
}

// FromCBOR deserializes a CBOR-encoded unsigned CoRIM into the target UnsignedCorim
func (o *UnsignedCorim) FromCBOR(data []byte) error {
return dm.Unmarshal(data, o)
return encoding.PopulateStructFromCBOR(dm, data, o)
}

// FromJSON deserializes a JSON-encoded unsigned CoRIM into the target UnsignedCorim
Expand Down
Loading

0 comments on commit 78fb6ee

Please sign in to comment.