Skip to content

Commit

Permalink
Minor tidy up!
Browse files Browse the repository at this point in the history
Signed-off-by: Yogesh Deshpande <[email protected]>
  • Loading branch information
yogeshbdeshpande committed Feb 7, 2024
1 parent ce6f11b commit 437bb89
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
11 changes: 8 additions & 3 deletions comid/integrityregisters.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
const TextType = "text"

// IRegisterIndex is the interface to hold register index
// Supported index types are uint and text
type IRegisterIndex interface{}

// IntegrityRegisters holds the Integrity Registers
Expand All @@ -24,7 +25,8 @@ func NewIntegrityRegisters() *IntegrityRegisters {
return &IntegrityRegisters{m: make(map[IRegisterIndex]Digests)}
}

// AddDigests allows inserting a series of Digests at a specific RegisterIndex
// AddDigests allows inserting an array of digests at a specific index
// Supported index types are uint and text
func (i *IntegrityRegisters) AddDigests(index IRegisterIndex, digests Digests) error {
if len(digests) == 0 {
return fmt.Errorf("no digests to add")
Expand All @@ -37,9 +39,9 @@ func (i *IntegrityRegisters) AddDigests(index IRegisterIndex, digests Digests) e
return nil
}

// AddDigest allows inserting a Digest at a specific registerIndex
// 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 {
return fmt.Errorf("no register to add digest")
}
Expand Down Expand Up @@ -96,6 +98,9 @@ func (i IntegrityRegisters) MarshalJSON() ([]byte, error) {
}

func (i *IntegrityRegisters) UnmarshalJSON(data []byte) error {
if i.m == nil {
i.m = make(map[IRegisterIndex]Digests)
}
jmap := make(map[string]json.RawMessage)
var index IRegisterIndex
if err := json.Unmarshal(data, &jmap); err != nil {
Expand Down
15 changes: 7 additions & 8 deletions comid/integrityregisters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ func TestIntegrityRegisters_MarshalCBOR_TextIndex_OK(t *testing.T) {
}

func TestIntegrityRegisters_UnmarshalCBOR_TextIndex_OK(t *testing.T) {

expected := IntegrityRegisters{map[IRegisterIndex]Digests{
"0": []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
"1": []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
Expand Down Expand Up @@ -211,10 +210,10 @@ func TestIntegrityRegisters_MarshalJSON_TextIndex_OK(t *testing.T) {

func TestIntegrityRegisters_UnmarshalJSON_TextIndex_OK(t *testing.T) {
j := `{"abcd":{"key_type":"text","Value":["sha-256;5Fty9cDAtXLbTY06t+l/No/3TmI0eoJN7LZ6hOUiTXU="]}}`
expected := &IntegrityRegisters{map[IRegisterIndex]Digests{
expected := IntegrityRegisters{map[IRegisterIndex]Digests{
"abcd": []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
}}
actual := NewIntegrityRegisters()
actual := IntegrityRegisters{}
err := actual.UnmarshalJSON([]byte(j))
require.NoError(t, err)
require.True(t, reflect.DeepEqual(expected, actual))
Expand All @@ -228,14 +227,14 @@ func TestIntegrityRegisters_UnmarshalJSON_UIntIndex_OK(t *testing.T) {
"3":{"key_type":"uint","Value":["sha-256;5Fty9cDAtXLbTY06t+l/No/3TmI0eoJN7LZ6hOUiTXU="]},
"4":{"key_type":"uint","Value":["sha-256;5Fty9cDAtXLbTY06t+l/No/3TmI0eoJN7LZ6hOUiTXU="]}
}`
expected := &IntegrityRegisters{map[IRegisterIndex]Digests{
expected := IntegrityRegisters{map[IRegisterIndex]Digests{
uint(0): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint(1): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint(2): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint(3): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint(4): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
}}
actual := NewIntegrityRegisters()
actual := IntegrityRegisters{}
err := actual.UnmarshalJSON([]byte(j))
require.NoError(t, err)
require.True(t, reflect.DeepEqual(expected, actual))
Expand All @@ -249,15 +248,15 @@ func TestIntegrityRegisters_UnmarshalJSON_TextUInt_Index_OK(t *testing.T) {
"3":{"key_type":"text","Value":["sha-256;5Fty9cDAtXLbTY06t+l/No/3TmI0eoJN7LZ6hOUiTXU="]},
"4":{"key_type":"text","Value":["sha-256;5Fty9cDAtXLbTY06t+l/No/3TmI0eoJN7LZ6hOUiTXU="]}
}`
expected := &IntegrityRegisters{
expected := IntegrityRegisters{
map[IRegisterIndex]Digests{
uint(0): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint(1): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
uint(2): []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
"3": []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
"4": []swid.HashEntry{{HashAlgID: swid.Sha256, HashValue: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75")}},
}}
actual := NewIntegrityRegisters()
actual := IntegrityRegisters{}
err := actual.UnmarshalJSON([]byte(j))
require.NoError(t, err)
require.True(t, reflect.DeepEqual(expected, actual))
Expand Down Expand Up @@ -291,7 +290,7 @@ func TestIntegrityRegisters_UnmarshalJSON_NOK(t *testing.T) {
},
} {
t.Run(tv.Name, func(t *testing.T) {
reg := NewIntegrityRegisters()
reg := IntegrityRegisters{}
err := reg.UnmarshalJSON([]byte(tv.Input))
assert.EqualError(t, err, tv.Err)
})
Expand Down

0 comments on commit 437bb89

Please sign in to comment.