Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
Improve metadata hepler test functions usage
Browse files Browse the repository at this point in the history
This change provides tiny updates of two metalada helper functions
to giarantee more stable testing over time

Signed-off-by: Ivana Atanasova <[email protected]>
  • Loading branch information
ivanayov committed Aug 18, 2023
1 parent 8834c5c commit b30c860
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
12 changes: 5 additions & 7 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,7 @@ func (role *SuccinctRoles) GetRolesForTarget(targetFilepath string) map[string]b
// GetRoles returns the names of all different delegated roles
func (role *SuccinctRoles) GetRoles() []string {
res := []string{}
numberOfBins := int(math.Pow(2, float64(role.BitLength)))
suffixLen := len(strconv.FormatInt(int64(numberOfBins-1), 16))
suffixLen, numberOfBins := role.GetSuffixLen()

for binNumber := 0; binNumber < numberOfBins; binNumber++ {
suffix := fmt.Sprintf("%0*x", suffixLen, binNumber)
Expand All @@ -584,16 +583,15 @@ func (role *SuccinctRoles) GetRoles() []string {
return res
}

func (role *SuccinctRoles) GetSuffixLen() int {
func (role *SuccinctRoles) GetSuffixLen() (int, int) {
numberOfBins := int(math.Pow(2, float64(role.BitLength)))
return len(strconv.FormatInt(int64(numberOfBins-1), 16))
return len(strconv.FormatInt(int64(numberOfBins-1), 16)), numberOfBins
}

// IsDelegatedRole returns whether the given roleName is in one of
// the delegated roles that “SuccinctRoles“ represents
func (role *SuccinctRoles) IsDelegatedRole(roleName string) bool {
numberOfBins := int64(math.Pow(2, float64(role.BitLength)))
suffixLen := len(strconv.FormatInt(int64(numberOfBins-1), 16))
suffixLen, numberOfBins := role.GetSuffixLen()

expectedPrefix := fmt.Sprintf("%s-", role.NamePrefix)

Expand All @@ -615,7 +613,7 @@ func (role *SuccinctRoles) IsDelegatedRole(roleName string) bool {
}

// check if the bin we calculated is indeed within the range of what we support
return (value >= 0) && (value < numberOfBins)
return (value >= 0) && (value < int64(numberOfBins))
}

// AddKey adds new signing key for delegated role "role"
Expand Down
5 changes: 3 additions & 2 deletions metadata/metadata_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ func TestMetadataVerifyDelegate(t *testing.T) {
// (in this case signature is malformed)
keyID := root.Signed.Roles[SNAPSHOT].KeyIDs[0]
goodSig, idx := getSignatureByKeyID(snapshot.Signatures, keyID)
assert.NotEqual(t, -1, idx)
assert.NotEmpty(t, goodSig)
snapshot.Signatures[idx].Signature = []byte("foo")
err = root.VerifyDelegate(SNAPSHOT, snapshot)
assert.ErrorIs(t, err, ErrUnsignedMetadata{"Verifying snapshot failed, not enough signatures, got 0, want 1"})
Expand Down Expand Up @@ -945,7 +945,8 @@ func TestGetRolesInSuccinctRoles(t *testing.T) {
// bin names are in hex format and 4 hex digits are enough to represent
// all bins between 0 and 2^16 - 1 meaning suffix_len must be 4
expectedSuffixLength := 4
assert.Equal(t, expectedSuffixLength, succinctRoles.GetSuffixLen())
suffixLen, _ := succinctRoles.GetSuffixLen()
assert.Equal(t, expectedSuffixLength, suffixLen)

allRoles := succinctRoles.GetRoles()
for binNumer, roleName := range allRoles {
Expand Down
2 changes: 1 addition & 1 deletion metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func getSignatureByKeyID(signatures []Signature, keyID string) (HexBytes, int) {
return sig.Signature, i
}
}
return []byte{}, -1
return []byte{}, 0
}

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

0 comments on commit b30c860

Please sign in to comment.