Skip to content

Commit

Permalink
Add Integrity registers
Browse files Browse the repository at this point in the history
Fixes #108

Signed-off-by: Yogesh Deshpande <[email protected]>
  • Loading branch information
yogeshbdeshpande committed Feb 3, 2024
1 parent 171ae95 commit 8836897
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
16 changes: 8 additions & 8 deletions comid/integregisters.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (i *IntegReg) AddDigests(index IRegisterIndex, digests Digests) error {
}
for _, digest := range digests {
if err := i.AddDigest(index, digest); err != nil {
return fmt.Errorf("unable to add Digest %v", err)
return fmt.Errorf("unable to add Digest: %w", err)
}
}
return nil
Expand Down Expand Up @@ -114,15 +114,15 @@ func (i *IntegReg) FromJSON(data []byte) error {
for key, val := range jmap {
index, err := unMarshalIndex(key)
if err != nil {
return fmt.Errorf("unable to UnMarshal Index: %v", err)
return fmt.Errorf("unable to unMarshal index: %w", err)
}
var d Digests

if err = json.Unmarshal(val, &d); err != nil {
return fmt.Errorf("unable to UnMarshal Digests: %v", err)
return fmt.Errorf("unable to UnMarshal Digests: %w", err)
}
if err = i.AddDigests(index, d); err != nil {
return fmt.Errorf("unable to insert register into register set %v", err)
return fmt.Errorf("unable to insert register into register set: %w", err)
}
}
return nil
Expand All @@ -131,22 +131,22 @@ func (i *IntegReg) FromJSON(data []byte) error {
func unMarshalIndex(key string) (IRegisterIndex, error) {
var v tnv
if err := json.Unmarshal([]byte(key), &v); err != nil {
return nil, fmt.Errorf("unable to UnMarshal key: %v", err)
return nil, fmt.Errorf("unable to unMarshal key: %w", err)
}
switch v.Type {
case "uint":
var key uint
if err := json.Unmarshal(v.Value, &key); err != nil {
return nil, fmt.Errorf("unable to UnMarshal uint key %v", err)
return nil, fmt.Errorf("unable to unMarshal uint key: %w", err)
}
return key, nil
case "text":
var key string
if err := json.Unmarshal(v.Value, &key); err != nil {
return nil, fmt.Errorf("unable to UnMarshal uint key %v", err)
return nil, fmt.Errorf("unable to unMarshal uint key: %w", err)
}
return key, nil
default:
return nil, fmt.Errorf("invalid type %s", v.Type)
return nil, fmt.Errorf("invalid type: %s", v.Type)
}
}
62 changes: 33 additions & 29 deletions comid/integregisters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestIntegRegister_AddDigests_NOK(t *testing.T) {
assert.EqualError(t, err, expectedErr)
}

func TestIntegRegister_UInt_MarshalCBOR_OK(t *testing.T) {
func TestIntegRegister_MarshalCBOR_UIntIndex_OK(t *testing.T) {
reg, err := prepareRegister("uint")
require.NoError(t, err)
bytes, err := reg.MarshalCBOR()
Expand All @@ -80,7 +80,7 @@ func TestIntegRegister_UInt_MarshalCBOR_OK(t *testing.T) {

}

func TestIntegRegister_UInt_UnMarshalCBOR_OK(t *testing.T) {
func TestIntegRegister_UnMarshalCBOR_UIntIndex_OK(t *testing.T) {
bstr := MustHexDecode(nil, `a302818207583031323433353436353836393739303830313234333534363538363937393038303132343335343635383639373930383000828207583031323433353436353836393739303830313234333534363538363937393038303132343335343635383639373930383082075830313234333534363538363937393038303132343335343635383639373930383031323433353436353836393739303930018182075830313234333534363538363937393038303132343335343635383639373930383031323433353436353836393739303830`)
reg, err := NewIntegRegister()
require.NoError(t, err)
Expand All @@ -94,15 +94,15 @@ func TestIntegRegister_UInt_UnMarshalCBOR_OK(t *testing.T) {
}
}

func TestIntegRegister_UInt_ToJSON_OK(t *testing.T) {
func TestIntegRegister_ToJSON_UIntIndex_OK(t *testing.T) {
reg, err := prepareRegister("uint")
require.NoError(t, err)
bytes, err := reg.ToJSON()
require.NoError(t, err)
fmt.Printf("JSON Payload = %s", bytes)
}

func TestIntegRegister_Text_MarshalCBOR_OK(t *testing.T) {
func TestIntegRegister_MarshalCBOR_TextIndex_OK(t *testing.T) {
reg, err := prepareRegister("text")
require.NoError(t, err)
bytes, err := reg.MarshalCBOR()
Expand All @@ -111,7 +111,7 @@ func TestIntegRegister_Text_MarshalCBOR_OK(t *testing.T) {

}

func TestIntegRegister_Text_UnMarshalCBOR_OK(t *testing.T) {
func TestIntegRegister_UnMarshalCBOR_TextIndex_OK(t *testing.T) {
bstr := MustHexDecode(nil, `a3613281820758303132343335343635383639373930383031323433353436353836393739303830313234333534363538363937393038306133818207583031323433353436353836393739303830313234333534363538363937393038303132343335343635383639373930383061318182075830313234333534363538363937393038303132343335343635383639373930383031323433353436353836393739303830`)
reg, err := NewIntegRegister()
require.NoError(t, err)
Expand All @@ -125,15 +125,15 @@ func TestIntegRegister_Text_UnMarshalCBOR_OK(t *testing.T) {
}
}

func TestIntegRegister_Text_ToJSON_OK(t *testing.T) {
func TestIntegRegister_ToJSON_TextIndex_OK(t *testing.T) {
reg, err := prepareRegister("text")
require.NoError(t, err)
bytes, err := reg.ToJSON()
require.NoError(t, err)
fmt.Printf("JSON Payload = %s", bytes)
}

func TestIntegRegister_Text_FromJSON_OK(t *testing.T) {
func TestIntegRegister_FromJSON_TextIndex_OK(t *testing.T) {
j := `{"{\"type\":\"text\",\"value\":\"a\"}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDgw"],"{\"type\":\"text\",\"value\":\"b\"}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDgw"],"{\"type\":\"text\",\"value\":\"c\"}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDgw"],"{\"type\":\"text\",\"value\":\"d\"}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDkw"]}`
reg, err := NewIntegRegister()
require.NoError(t, err)
Expand All @@ -147,7 +147,7 @@ func TestIntegRegister_Text_FromJSON_OK(t *testing.T) {
}
}

func TestIntegRegister_UInt_FromJSON_OK(t *testing.T) {
func TestIntegRegister_FromJSON_UIntIndex_OK(t *testing.T) {
j := `{
"{\"type\":\"uint\",\"value\":0}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDgw"],
"{\"type\":\"uint\",\"value\":1}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDgw"],
Expand All @@ -166,7 +166,7 @@ func TestIntegRegister_UInt_FromJSON_OK(t *testing.T) {
}
}

func TestIntegRegister_UInt_Text_FromJSON_OK(t *testing.T) {
func TestIntegRegister_FromJSON_TextUInt_Index_OK(t *testing.T) {
j := `{
"{\"type\":\"uint\",\"value\":0}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDgw"],
"{\"type\":\"uint\",\"value\":1}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDgw"],
Expand All @@ -186,26 +186,30 @@ func TestIntegRegister_UInt_Text_FromJSON_OK(t *testing.T) {
}
}

func TestIntegRegister_UInt_FromJSON_NOK(t *testing.T) {
expectedErr := `unable to UnMarshal Index: invalid type int`
j := `{
"{\"type\":\"int\",\"value\":0}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDgw"]
}`
reg, err := NewIntegRegister()
require.NoError(t, err)
err = reg.FromJSON([]byte(j))
assert.EqualError(t, err, expectedErr)
}

func TestIntegRegister_UIntFromJSON_NOK1(t *testing.T) {
expectedErr := `unable to UnMarshal Index: unable to UnMarshal key: invalid character 'a' looking for beginning of object key string`
j := `{
"{abcd}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDgw"]
}`
reg, err := NewIntegRegister()
require.NoError(t, err)
err = reg.FromJSON([]byte(j))
assert.EqualError(t, err, expectedErr)
func TestIntegRegister_FromJSON_InvalidIndex(t *testing.T) {
for _, tv := range []struct {
Name string
Input string
Err string
}{
{
Name: "invalid input integer",
Input: `{"{\"type\":\"int\",\"value\":0}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDgw"]}`,
Err: "unable to unMarshal index: invalid type: int",
},
{
Name: "invalid type",
Input: `{"{abcd}":["sha-384;MTI0MzU0NjU4Njk3OTA4MDEyNDM1NDY1ODY5NzkwODAxMjQzNTQ2NTg2OTc5MDgw"]}`,
Err: `unable to unMarshal index: unable to unMarshal key: invalid character 'a' looking for beginning of object key string`,
},
} {
t.Run(tv.Name, func(t *testing.T) {
reg, err := NewIntegRegister()
require.NoError(t, err)
err = reg.FromJSON([]byte(tv.Input))
assert.EqualError(t, err, tv.Err)
})
}
}

func TestIntegRegister_RemoveIndex_OK(t *testing.T) {
Expand Down

0 comments on commit 8836897

Please sign in to comment.