[#242] object: Require only 1 field to be set for SplitInfo

Also write more tests.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-05-17 17:18:49 +03:00 committed by LeL
parent 12ea1e8d74
commit c976332e20
2 changed files with 75 additions and 31 deletions

View file

@ -110,31 +110,30 @@ func (s *SplitInfo) UnmarshalJSON(data []byte) error {
return formatCheckSI((*object.SplitInfo)(s))
}
var errLinkNotSet = errors.New("link object ID is not set")
var errLastPartNotSet = errors.New("last part object ID is not set")
var errSplitInfoMissingFields = errors.New("neither link object ID nor last part object ID is set")
func formatCheckSI(v2 *object.SplitInfo) error {
var oID oid.ID
link := v2.GetLink()
if link == nil {
return errLinkNotSet
lastPart := v2.GetLastPart()
if link == nil && lastPart == nil {
return errSplitInfoMissingFields
}
var oID oid.ID
if link != nil {
err := oID.ReadFromV2(*link)
if err != nil {
return fmt.Errorf("could not convert link object ID: %w", err)
}
lastPart := v2.GetLastPart()
if lastPart == nil {
return errLastPartNotSet
}
err = oID.ReadFromV2(*lastPart)
if lastPart != nil {
err := oID.ReadFromV2(*lastPart)
if err != nil {
return fmt.Errorf("could not convert last part object ID: %w", err)
}
}
return nil
}

View file

@ -29,15 +29,16 @@ func TestSplitInfo(t *testing.T) {
l, set := s.Link()
require.True(t, set)
require.Equal(t, link, l)
}
t.Run("to and from v2", func(t *testing.T) {
func TestSplitInfoMarshal(t *testing.T) {
testToV2 := func(t *testing.T, s *object.SplitInfo) {
v2 := s.ToV2()
newS := object.NewSplitInfoFromV2(v2)
require.Equal(t, s, newS)
})
t.Run("marshal and unmarshal", func(t *testing.T) {
}
testMarshal := func(t *testing.T, s *object.SplitInfo) {
data, err := s.Marshal()
require.NoError(t, err)
@ -46,6 +47,40 @@ func TestSplitInfo(t *testing.T) {
err = newS.Unmarshal(data)
require.NoError(t, err)
require.Equal(t, s, newS)
}
t.Run("good, both fields are set", func(t *testing.T) {
s := object.NewSplitInfo()
s.SetSplitID(object.NewSplitID())
s.SetLink(generateID())
s.SetLastPart(generateID())
testToV2(t, s)
testMarshal(t, s)
})
t.Run("good, only link is set", func(t *testing.T) {
s := object.NewSplitInfo()
s.SetSplitID(object.NewSplitID())
s.SetLink(generateID())
testToV2(t, s)
testMarshal(t, s)
})
t.Run("good, only last part is set", func(t *testing.T) {
s := object.NewSplitInfo()
s.SetSplitID(object.NewSplitID())
s.SetLastPart(generateID())
testToV2(t, s)
testMarshal(t, s)
})
t.Run("bad, no fields are set", func(t *testing.T) {
s := object.NewSplitInfo()
s.SetSplitID(object.NewSplitID())
data, err := s.Marshal()
require.NoError(t, err)
require.Error(t, object.NewSplitInfo().Unmarshal(data))
})
}
@ -96,6 +131,7 @@ func TestNewSplitInfo(t *testing.T) {
}
func TestSplitInfoMarshalJSON(t *testing.T) {
t.Run("good", func(t *testing.T) {
s := object.NewSplitInfo()
s.SetSplitID(object.NewSplitID())
s.SetLastPart(generateID())
@ -107,4 +143,13 @@ func TestSplitInfoMarshalJSON(t *testing.T) {
actual := object.NewSplitInfo()
require.NoError(t, json.Unmarshal(data, actual))
require.Equal(t, s, actual)
})
t.Run("bad link", func(t *testing.T) {
data := `{"splitId":"Sn707289RrqDyJOrZMbMoQ==","lastPart":{"value":"Y7baWE0UdUOBr1ELKX3Q5v1LKRubQUbI81Q5UxCVeow="},"link":{"value":"bad"}}`
require.Error(t, json.Unmarshal([]byte(data), object.NewSplitInfo()))
})
t.Run("bad last part", func(t *testing.T) {
data := `{"splitId":"Sn707289RrqDyJOrZMbMoQ==","lastPart":{"value":"bad"},"link":{"value":"eRyPNCNNxHfxPcjijlv05HEcdoep/b7eHNLRSmDlnts="}}`
require.Error(t, json.Unmarshal([]byte(data), object.NewSplitInfo()))
})
}