Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion comid/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ type Mval struct {
UUID *UUID `cbor:"10,keyasint,omitempty" json:"uuid,omitempty"`
Name *string `cbor:"11,keyasint,omitempty" json:"name,omitempty"`
IntegrityRegisters *IntegrityRegisters `cbor:"14,keyasint,omitempty" json:"integrity-registers,omitempty"`
RawInt *RawInt `cbor:"15,keyasint,omitempty" json:"raw-int,omitempty"`
Extensions
}

Expand Down Expand Up @@ -442,8 +443,8 @@ func (o Mval) Valid() error {
o.UUID == nil &&
o.Name == nil &&
o.IntegrityRegisters == nil &&
o.RawInt == nil &&
o.Extensions.IsEmpty() {

return fmt.Errorf("no measurement value set")
}

Expand Down
31 changes: 3 additions & 28 deletions comid/svn.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ type SVN struct {
}

// NewSVN creates a new SVN of the specified and value. The type must be one of
// the strings defined by the spec ("exact-value", "min-value"), or has been
// registered with RegisterSVNType().
// the strings defined by the spec ("exact-value", "min-value").
func NewSVN(val any, typ string) (*SVN, error) {
factory, ok := svnValueRegister[typ]
if !ok {
Expand Down Expand Up @@ -279,9 +278,8 @@ func convertToSVNUint64(val any) (uint64, error) {
}
}

// ISVNFactory defines the signature for the factory functions that may be
// registred using RegisterSVNType to provide a new implementation of the
// corresponding type choice. The factory function should create a new *SVN
// ISVNFactory defines the signature for factory functions to create SVN types
// supported by svn-type-choice. The factory function should create a new *SVN
// with the underlying value created based on the provided input. The range of
// valid inputs is up to the specific type choice implementation, however it
// _must_ accept nil as one of the inputs, and return the Zero value for
Expand All @@ -293,26 +291,3 @@ var svnValueRegister = map[string]ISVNFactory{
ExactValueType: NewTaggedSVN,
MinValueType: NewTaggedMinSVN,
}

// RegisterSVNType registers a new ISVNValue implementation
// (created by the provided ISVNFactory) under the specified CBOR tag.
func RegisterSVNType(tag uint64, factory ISVNFactory) error {

nilVal, err := factory(nil)
if err != nil {
return err
}

typ := nilVal.Value.Type()
if _, exists := svnValueRegister[typ]; exists {
return fmt.Errorf("SVN type with name %q already exists", typ)
}

if err := registerCOMIDTag(tag, nilVal.Value); err != nil {
return err
}

svnValueRegister[typ] = factory

return nil
}
49 changes: 2 additions & 47 deletions comid/svn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

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

func Test_NewSVN(t *testing.T) {
Expand Down Expand Up @@ -83,8 +82,8 @@ func Test_NewSVN(t *testing.T) {
}

retMin, err := NewSVN(tv.Input, "min-value")
min := TaggedMinSVN(tv.Expected)
expected = SVN{&min}
minSVN := TaggedMinSVN(tv.Expected)
expected = SVN{&minSVN}

if tv.Err != "" {
assert.EqualError(t, err, tv.Err)
Expand Down Expand Up @@ -139,50 +138,6 @@ func TestSVN_JSON(t *testing.T) {

}

type testSVN uint64

func newTestSVN(_ any) (*SVN, error) {
v := testSVN(7)
return &SVN{&v}, nil
}

func (o testSVN) Type() string {
return "test-value"
}

func (o testSVN) String() string {
return "test"
}

func (o testSVN) Valid() error {
return nil
}

type testSVNBadType struct {
testSVN
}

func newTestSVNBadType(_ any) (*SVN, error) {
v := testSVNBadType{testSVN(7)}
return &SVN{&v}, nil
}

func (o testSVNBadType) Type() string {
return "min-value"
}

func Test_RegisterSVNType(t *testing.T) {
err := RegisterSVNType(32, newTestSVN)
assert.EqualError(t, err, "tag 32 is already registered")

err = RegisterSVNType(99995, newTestSVNBadType)
assert.EqualError(t, err, `SVN type with name "min-value" already exists`)

err = RegisterSVNType(99995, newTestSVN)
require.NoError(t, err)

}

func Test_TaggedSVN_Equal_True(t *testing.T) {
claim := TaggedSVN(7)
ref := TaggedSVN(7)
Expand Down