2021-09-16 02:51:51 +00:00
|
|
|
package apimiddleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
2021-09-16 02:51:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnmarshalEpochParticipation(t *testing.T) {
|
|
|
|
t.Run("ok", func(t *testing.T) {
|
|
|
|
b := []byte{3, 3, 0}
|
|
|
|
b64 := []byte("\"" + base64.StdEncoding.EncodeToString(b) + "\"")
|
|
|
|
ep := EpochParticipation{}
|
|
|
|
require.NoError(t, ep.UnmarshalJSON(b64))
|
|
|
|
require.Equal(t, 3, len(ep))
|
|
|
|
assert.Equal(t, "3", ep[0])
|
|
|
|
assert.Equal(t, "3", ep[1])
|
|
|
|
assert.Equal(t, "0", ep[2])
|
|
|
|
})
|
|
|
|
t.Run("incorrect value", func(t *testing.T) {
|
|
|
|
ep := EpochParticipation{}
|
|
|
|
err := ep.UnmarshalJSON([]byte(":illegal:"))
|
|
|
|
require.NotNil(t, err)
|
2023-06-16 09:43:20 +00:00
|
|
|
assert.ErrorContains(t, "provided epoch participation json string is malformed", err)
|
2021-09-16 02:51:51 +00:00
|
|
|
})
|
|
|
|
t.Run("length too small", func(t *testing.T) {
|
|
|
|
ep := EpochParticipation{}
|
|
|
|
err := ep.UnmarshalJSON([]byte("x"))
|
|
|
|
require.NotNil(t, err)
|
|
|
|
assert.ErrorContains(t, "epoch participation length must be at least 2", err)
|
|
|
|
})
|
|
|
|
t.Run("null value", func(t *testing.T) {
|
|
|
|
ep := EpochParticipation{}
|
|
|
|
require.NoError(t, ep.UnmarshalJSON([]byte("null")))
|
|
|
|
assert.DeepEqual(t, EpochParticipation([]string{}), ep)
|
|
|
|
})
|
2023-06-15 15:12:39 +00:00
|
|
|
t.Run("invalid value", func(t *testing.T) {
|
|
|
|
ep := EpochParticipation{}
|
2023-06-16 09:43:20 +00:00
|
|
|
require.ErrorContains(t, "provided epoch participation json string is malformed", ep.UnmarshalJSON([]byte("XdHJ1ZQ==X")))
|
2023-06-15 15:12:39 +00:00
|
|
|
})
|
2021-09-16 02:51:51 +00:00
|
|
|
}
|