Skip to content

Commit

Permalink
add JSON encoders for Comid
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-fossati committed Oct 5, 2021
1 parent c8c45a7 commit f73a11a
Show file tree
Hide file tree
Showing 26 changed files with 495 additions and 194 deletions.
9 changes: 9 additions & 0 deletions comid/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,12 @@ func (o *Class) FromJSON(data []byte) error {

return o.Valid()
}

// ToJSON serializes the target Class to JSON (if the Class is "valid")
func (o Class) ToJSON() ([]byte, error) {
if err := o.Valid(); err != nil {
return nil, err
}

return json.Marshal(&o)
}
78 changes: 65 additions & 13 deletions comid/classid.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ func (o *ClassID) SetUUID(uuid UUID) *ClassID {

type ImplID [32]byte
type TaggedImplID ImplID

func (o ImplID) MarshalJSON() ([]byte, error) {
return json.Marshal(o[:])
}

func (o *ImplID) UnmarshalJSON(data []byte) error {
var b []byte

if err := json.Unmarshal(data, &b); err != nil {
return fmt.Errorf("bad ImplID: %w", err)
}

if nb := len(b); nb != 32 {
return fmt.Errorf("bad ImplID format: got %d bytes, want 32", nb)
}

copy(o[:], b)

return nil
}

type TaggedOID OID

// SetImplID sets the value of the targed ClassID to the supplied PSA
Expand Down Expand Up @@ -122,41 +143,72 @@ func (o *ClassID) UnmarshalCBOR(data []byte) error {
// "value": "YWNtZS1pbXBsZW1lbnRhdGlvbi1pZC0wMDAwMDAwMDE="
// }
func (o *ClassID) UnmarshalJSON(data []byte) error {
v := struct {
Type string `json:"type"`
Value interface{} `json:"value"`
}{}
var v tnv

if err := json.Unmarshal(data, &v); err != nil {
return err
}

switch v.Type {
case "uuid": // nolint: goconst
var uuid UUID
if err := jsonDecodeUUID(v.Value, &uuid); err != nil {
var x UUID
if err := x.UnmarshalJSON(v.Value); err != nil {
return err
}
o.SetUUID(uuid)
o.val = TaggedUUID(x)
case "oid":
var berOID OID
if err := jsonDecodeOID(v.Value, &berOID); err != nil {
var x OID
if err := x.UnmarshalJSON(v.Value); err != nil {
return err
}
o.val = TaggedOID(berOID)
o.val = TaggedOID(x)
case "psa.impl-id":
var implID ImplID
if err := jsonDecodeImplID(v.Value, &implID); err != nil {
var x ImplID
if err := x.UnmarshalJSON(v.Value); err != nil {
return err
}
o.SetImplID(implID)
o.val = TaggedImplID(x)
default:
return fmt.Errorf("unknown type '%s' for class id", v.Type)
}

return nil
}

// MarshalJSON serializes the target ClassID to JSON
func (o ClassID) MarshalJSON() ([]byte, error) {
var (
v tnv
b []byte
err error
)

switch t := o.val.(type) {
case TaggedUUID:
b, err = UUID(t).MarshalJSON()
if err != nil {
return nil, err
}
v = tnv{Type: "uuid", Value: b}
case TaggedOID:
b, err = OID(t).MarshalJSON()
if err != nil {
return nil, err
}
v = tnv{Type: "oid", Value: b}
case TaggedImplID:
b, err = ImplID(t).MarshalJSON()
if err != nil {
return nil, err
}
v = tnv{Type: "psa.impl-id", Value: b}
default:
return nil, fmt.Errorf("unknown type %T for class-id", t)
}

return json.Marshal(v)
}

// Type returns the type of the target ClassID, i.e., one of UUID, OID or PSA
// Implementation ID
func (o ClassID) Type() ClassIDType {
Expand Down
2 changes: 1 addition & 1 deletion comid/classid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestClassID_UnmarshalJSON_badInput_missing_value(t *testing.T) {
var actual ClassID
err := actual.UnmarshalJSON([]byte(tv))

assert.EqualError(t, err, "ImplID must be string")
assert.EqualError(t, err, "bad ImplID: unexpected end of JSON input")
assert.Equal(t, ClassIDTypeUnknown, actual.Type())
}

Expand Down
5 changes: 5 additions & 0 deletions comid/comid.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,8 @@ func (o *Comid) FromCBOR(data []byte) error {
func (o *Comid) FromJSON(data []byte) error {
return json.Unmarshal(data, o)
}

// ToJSON serializes the target Comid to JSON
func (o Comid) ToJSON() ([]byte, error) {
return json.Marshal(&o)
}
114 changes: 0 additions & 114 deletions comid/common.go

This file was deleted.

23 changes: 23 additions & 0 deletions comid/endorsedvalue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package comid

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestEndorsedValue_Valid_empty_environment(t *testing.T) {
tv := EndorsedValue{}

assert.EqualError(t, tv.Valid(), "environment validation failed: environment must not be empty")
}

func TestEndorsedValue_Valid_empty_measurements(t *testing.T) {
tv := EndorsedValue{
Environment: Environment{
Class: NewClassUUID(TestUUID),
},
}

assert.EqualError(t, tv.Valid(), "measurements validation failed: no measurement entries")
}
15 changes: 12 additions & 3 deletions comid/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ func (o Environment) Valid() error {

if o.Class != nil {
if err := o.Class.Valid(); err != nil {
return fmt.Errorf("environment validation failed: %w", err)
return fmt.Errorf("class validation failed: %w", err)
}
}

if o.Instance != nil {
if err := o.Instance.Valid(); err != nil {
return fmt.Errorf("environment validation failed: %w", err)
return fmt.Errorf("instance validation failed: %w", err)
}
}

if o.Group != nil {
if err := o.Group.Valid(); err != nil {
return fmt.Errorf("environment validation failed: %w", err)
return fmt.Errorf("group validation failed: %w", err)
}
}

Expand Down Expand Up @@ -71,3 +71,12 @@ func (o *Environment) FromJSON(data []byte) error {

return o.Valid()
}

// ToJSON serializes the target Environment to JSON (if the Environment is "valid")
func (o Environment) ToJSON() ([]byte, error) {
if err := o.Valid(); err != nil {
return nil, err
}

return json.Marshal(&o)
}
6 changes: 3 additions & 3 deletions comid/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestEnvironment_Valid_empty_class(t *testing.T) {

err := tv.Valid()

assert.EqualError(t, err, "environment validation failed: class must not be empty")
assert.EqualError(t, err, "class validation failed: class must not be empty")
}

func TestEnvironment_Valid_empty_instance(t *testing.T) {
Expand All @@ -36,7 +36,7 @@ func TestEnvironment_Valid_empty_instance(t *testing.T) {

err := tv.Valid()

assert.EqualError(t, err, "environment validation failed: invalid instance id")
assert.EqualError(t, err, "instance validation failed: invalid instance id")
}

func TestEnvironment_Valid_empty_group(t *testing.T) {
Expand All @@ -46,7 +46,7 @@ func TestEnvironment_Valid_empty_group(t *testing.T) {

err := tv.Valid()

assert.EqualError(t, err, "environment validation failed: invalid group id")
assert.EqualError(t, err, "group validation failed: invalid group id")
}
func TestEnvironment_Valid_ok_with_class(t *testing.T) {
tv := Environment{
Expand Down
Loading

0 comments on commit f73a11a

Please sign in to comment.