2021-11-08 10:04:45 +00:00
|
|
|
package object_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2022-03-14 13:52:45 +00:00
|
|
|
"encoding/json"
|
2021-11-08 10:04:45 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-10-07 14:20:25 +00:00
|
|
|
objv2 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/object"
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-11-08 10:04:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSplitInfo(t *testing.T) {
|
|
|
|
s := object.NewSplitInfo()
|
|
|
|
splitID := object.NewSplitID()
|
|
|
|
lastPart := generateID()
|
|
|
|
link := generateID()
|
|
|
|
|
|
|
|
s.SetSplitID(splitID)
|
|
|
|
require.Equal(t, splitID, s.SplitID())
|
|
|
|
|
|
|
|
s.SetLastPart(lastPart)
|
2022-04-12 16:23:16 +00:00
|
|
|
lp, set := s.LastPart()
|
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, lastPart, lp)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
s.SetLink(link)
|
2022-04-12 16:23:16 +00:00
|
|
|
l, set := s.Link()
|
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, link, l)
|
2022-05-17 14:18:49 +00:00
|
|
|
}
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-05-17 14:18:49 +00:00
|
|
|
func TestSplitInfoMarshal(t *testing.T) {
|
|
|
|
testToV2 := func(t *testing.T, s *object.SplitInfo) {
|
2021-11-08 10:04:45 +00:00
|
|
|
v2 := s.ToV2()
|
|
|
|
newS := object.NewSplitInfoFromV2(v2)
|
|
|
|
|
|
|
|
require.Equal(t, s, newS)
|
2022-05-17 14:18:49 +00:00
|
|
|
}
|
|
|
|
testMarshal := func(t *testing.T, s *object.SplitInfo) {
|
2021-11-08 10:04:45 +00:00
|
|
|
data, err := s.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
newS := object.NewSplitInfo()
|
|
|
|
|
|
|
|
err = newS.Unmarshal(data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, s, newS)
|
2022-05-17 14:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2021-11-08 10:04:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
func generateID() oid.ID {
|
2021-11-08 10:04:45 +00:00
|
|
|
var buf [32]byte
|
|
|
|
_, _ = rand.Read(buf[:])
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var id oid.ID
|
2021-11-08 10:04:45 +00:00
|
|
|
id.SetSHA256(buf)
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewSplitInfoFromV2(t *testing.T) {
|
|
|
|
t.Run("from nil", func(t *testing.T) {
|
|
|
|
var x *objv2.SplitInfo
|
|
|
|
|
|
|
|
require.Nil(t, object.NewSplitInfoFromV2(x))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitInfo_ToV2(t *testing.T) {
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
var x *object.SplitInfo
|
|
|
|
|
|
|
|
require.Nil(t, x.ToV2())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewSplitInfo(t *testing.T) {
|
|
|
|
t.Run("default values", func(t *testing.T) {
|
|
|
|
si := object.NewSplitInfo()
|
|
|
|
|
|
|
|
// check initial values
|
|
|
|
require.Nil(t, si.SplitID())
|
2022-04-12 16:23:16 +00:00
|
|
|
_, set := si.LastPart()
|
|
|
|
require.False(t, set)
|
|
|
|
_, set = si.Link()
|
|
|
|
require.False(t, set)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
// convert to v2 message
|
|
|
|
siV2 := si.ToV2()
|
|
|
|
|
|
|
|
require.Nil(t, siV2.GetSplitID())
|
|
|
|
require.Nil(t, siV2.GetLastPart())
|
|
|
|
require.Nil(t, siV2.GetLink())
|
|
|
|
})
|
|
|
|
}
|
2022-03-14 13:52:45 +00:00
|
|
|
|
|
|
|
func TestSplitInfoMarshalJSON(t *testing.T) {
|
2022-05-17 14:18:49 +00:00
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
s := object.NewSplitInfo()
|
|
|
|
s.SetSplitID(object.NewSplitID())
|
|
|
|
s.SetLastPart(generateID())
|
|
|
|
s.SetLink(generateID())
|
2022-03-14 13:52:45 +00:00
|
|
|
|
2022-05-17 14:18:49 +00:00
|
|
|
data, err := s.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
2022-03-14 13:52:45 +00:00
|
|
|
|
2022-05-17 14:18:49 +00:00
|
|
|
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()))
|
|
|
|
})
|
2022-03-14 13:52:45 +00:00
|
|
|
}
|