Skip to content

Commit

Permalink
Merge pull request #83 from xenowits/xenowits/fix-out-of-bounds
Browse files Browse the repository at this point in the history
add out-of-bounds check
  • Loading branch information
mcdee authored Oct 21, 2023
2 parents f9b2dfb + 681ff2b commit b12f127
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/v1/validatorstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (v *ValidatorState) UnmarshalJSON(input []byte) error {
}

func (v ValidatorState) String() string {
if v < 0 || int(v) >= len(validatorStateStrings) {
return validatorStateStrings[0] // unknown
}

return validatorStateStrings[v]
}

Expand Down
36 changes: 36 additions & 0 deletions api/v1/validatorstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,39 @@ func TestValidatorToState(t *testing.T) {
})
}
}

func TestString(t *testing.T) {
tests := []struct {
name string
state api.ValidatorState
expected string
}{
{
name: "valid state",
state: api.ValidatorStateActiveOngoing,
expected: "active_ongoing",
},
{
name: "negative index",
state: -1,
expected: "unknown",
},
{
name: "edge bound index",
state: 10,
expected: "unknown",
},
{
name: "high out of bound index",
state: 250,
expected: "unknown",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
resp := test.state.String()
require.Equal(t, test.expected, resp)
})
}
}

0 comments on commit b12f127

Please sign in to comment.