frostfs-api-go/refs/types_test.go
Leonard Lyubich dc292864aa [#356] refs: Handle uint32 overflow in SubnetID text format
Clarify the bit size limit in `SubnetID.UnmarshalText` method. Cover
overflow case in unit test.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2021-11-24 17:13:18 +03:00

60 lines
1.1 KiB
Go

package refs_test
import (
"math"
"strconv"
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/stretchr/testify/require"
)
func TestZeroSubnet(t *testing.T) {
id := new(refs.SubnetID)
require.True(t, refs.IsZeroSubnet(id))
id.SetValue(1)
require.False(t, refs.IsZeroSubnet(id))
refs.MakeZeroSubnet(id)
require.True(t, refs.IsZeroSubnet(id))
}
func TestSubnetID_MarshalText(t *testing.T) {
var id refs.SubnetID
const val = 15
id.SetValue(val)
txt, err := id.MarshalText()
require.NoError(t, err)
res, err := strconv.ParseUint(string(txt), 10, 32)
require.NoError(t, err)
require.EqualValues(t, val, res)
}
func TestSubnetID_UnmarshalText(t *testing.T) {
const val = 15
str := strconv.FormatUint(val, 10)
var id refs.SubnetID
err := id.UnmarshalText([]byte(str))
require.NoError(t, err)
require.EqualValues(t, val, id.GetValue())
t.Run("uint32 overflow", func(t *testing.T) {
txt := strconv.FormatUint(math.MaxUint32+1, 10)
var id refs.SubnetID
err := id.UnmarshalText([]byte(txt))
require.ErrorIs(t, err, strconv.ErrRange)
})
}