-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yogesh Deshpande <[email protected]>
- Loading branch information
1 parent
5a0612a
commit 8a7028f
Showing
3 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2021 Contributors to the Veraison project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package corim | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type EntityExtension struct { | ||
Param1 string `cbor:"0,keyasint" json:"param1"` | ||
Param2 byte `cbor:"1,keyasint,omitempty" json:"param2,omitempty"` | ||
} | ||
|
||
func (o EntityExtension) Valid() error { | ||
if o.Param1 == "" { | ||
return fmt.Errorf("mandatory Parameter Param1 missing") | ||
} | ||
if o.Param2 == 0x40 { | ||
return fmt.Errorf("mandatory Parameter Param2 cannot be 0x40") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *EntityExtension) FromCBOR(data []byte) error { | ||
if o != nil { | ||
err := dm.Unmarshal(data, o) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (o *EntityExtension) ToCBOR() ([]byte, error) { | ||
if o != nil { | ||
data, err := em.Marshal(o) | ||
if err != nil { | ||
return nil, err | ||
} else { | ||
Check failure on line 41 in corim/entity_extension.go GitHub Actions / Lint
|
||
return data, nil | ||
} | ||
} else { | ||
return nil, fmt.Errorf("no entity to serialize") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2021 Contributors to the Veraison project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package corim | ||
|
||
import ( | ||
"testing" | ||
|
||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEntities_Extensions_Valid_ok(t *testing.T) { | ||
ext := &EntityExtension{"my profile", 0x56} | ||
|
||
e := NewEntity(). | ||
SetEntityName("ACME Ltd."). | ||
SetRegID("http://acme.example"). | ||
SetRoles(RoleManifestCreator). | ||
SetExtension(ext) | ||
require.NotNil(t, e) | ||
|
||
es := NewEntities().AddEntity(*e) | ||
require.NotNil(t, es) | ||
|
||
err := es.Valid() | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestEntities_Extensions_ToCBOR_ok(t *testing.T) { | ||
ext := &EntityExtension{"my profile", 0x56} | ||
|
||
e := NewEntity(). | ||
SetEntityName("ACME Ltd."). | ||
SetRegID("http://acme.example"). | ||
SetRoles(RoleManifestCreator). | ||
SetExtension(ext) | ||
require.NotNil(t, e) | ||
data, err := e.ToCBOR() | ||
|
||
require.Nil(t, err) | ||
fmt.Printf("to CBOR Entity = %x", data) | ||
|
||
} | ||
|
||
func TestEntities_Extensions_FromCBOR_ok(t *testing.T) { | ||
data := []byte{0xa4, 0x00, 0x69, 0x41, 0x43, 0x4d, 0x45, 0x20, 0x4c, 0x74, 0x64, 0x2e, 0x01, 0xd8, 0x20, 0x73, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x61, 0x63, 0x6d, 0x65, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x02, 0x81, 0x01, 0x03, 0xa2, 0x00, 0x6a, 0x6d, 0x79, 0x20, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x01, 0x18, 0x56} | ||
//var ext1 EntityExtension | ||
e := &Entity{Extension: &EntityExtension{}} | ||
|
||
err := e.FromCBOR(data) | ||
require.Nil(t, err) | ||
ext, err := e.GetExtension() | ||
require.Nil(t, err) | ||
require.NotNil(t, ext) | ||
} | ||
|
||
func TestEntities_NoExtensions_ToCBOR_ok(t *testing.T) { | ||
e := NewEntity(). | ||
SetEntityName("ACME Ltd."). | ||
SetRegID("http://acme.example"). | ||
SetRoles(RoleManifestCreator) | ||
require.NotNil(t, e) | ||
data, err := e.ToCBOR() | ||
|
||
require.Nil(t, err) | ||
fmt.Printf("to CBOR Entity = %x", data) | ||
|
||
} | ||
|
||
func TestEntities_NoExtensions_FromCBOR_ok(t *testing.T) { | ||
data := []byte{0xa4, 0x00, 0x69, 0x41, 0x43, 0x4d, 0x45, 0x20, 0x4c, 0x74, 0x64, 0x2e, 0x01, 0xd8, 0x20, 0x73, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x61, 0x63, 0x6d, 0x65, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x02, 0x81, 0x01, 0x03, 0x0F6} | ||
e := &Entity{} | ||
err := e.FromCBOR(data) | ||
require.Nil(t, err) | ||
require.Nil(t, err) | ||
|
||
} |