2022-01-25 16:20:32 +00:00
|
|
|
package oid
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2024-10-07 14:20:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
2021-11-08 10:04:45 +00:00
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
const emptyID = "11111111111111111111111111111111"
|
|
|
|
|
|
|
|
func randID(t *testing.T) ID {
|
|
|
|
var id ID
|
2022-01-25 16:20:32 +00:00
|
|
|
id.SetSHA256(randSHA256Checksum(t))
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func randSHA256Checksum(t *testing.T) (cs [sha256.Size]byte) {
|
|
|
|
_, err := rand.Read(cs[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-08 10:04:45 +00:00
|
|
|
func TestIDV2(t *testing.T) {
|
2022-04-11 16:25:14 +00:00
|
|
|
var id ID
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
checksum := [sha256.Size]byte{}
|
|
|
|
|
|
|
|
_, err := rand.Read(checksum[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
id.SetSHA256(checksum)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var idV2 refs.ObjectID
|
|
|
|
id.WriteToV2(&idV2)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
require.Equal(t, checksum[:], idV2.GetValue())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestID_Equal(t *testing.T) {
|
|
|
|
cs := randSHA256Checksum(t)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var id1 ID
|
2021-11-08 10:04:45 +00:00
|
|
|
id1.SetSHA256(cs)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var id2 ID
|
2021-11-08 10:04:45 +00:00
|
|
|
id2.SetSHA256(cs)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var id3 ID
|
2021-11-08 10:04:45 +00:00
|
|
|
id3.SetSHA256(randSHA256Checksum(t))
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.True(t, id1.Equals(id2))
|
|
|
|
require.False(t, id1.Equals(id3))
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestID_Parse(t *testing.T) {
|
|
|
|
t.Run("should parse successful", func(t *testing.T) {
|
2024-09-03 10:20:30 +00:00
|
|
|
for i := range 10 {
|
2021-11-08 10:04:45 +00:00
|
|
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
|
|
|
cs := randSHA256Checksum(t)
|
|
|
|
str := base58.Encode(cs[:])
|
2022-04-11 16:25:14 +00:00
|
|
|
var oid ID
|
|
|
|
|
|
|
|
require.NoError(t, oid.DecodeString(str))
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var oidV2 refs.ObjectID
|
|
|
|
oid.WriteToV2(&oidV2)
|
|
|
|
|
|
|
|
require.Equal(t, cs[:], oidV2.GetValue())
|
2021-11-08 10:04:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should failure on parse", func(t *testing.T) {
|
2024-09-03 10:20:30 +00:00
|
|
|
for i := range 10 {
|
2021-11-08 10:04:45 +00:00
|
|
|
j := i
|
|
|
|
t.Run(strconv.Itoa(j), func(t *testing.T) {
|
|
|
|
cs := []byte{1, 2, 3, 4, 5, byte(j)}
|
|
|
|
str := base58.Encode(cs)
|
2022-04-11 16:25:14 +00:00
|
|
|
var oid ID
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.Error(t, oid.DecodeString(str))
|
2021-11-08 10:04:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestID_String(t *testing.T) {
|
2022-04-11 16:25:14 +00:00
|
|
|
t.Run("zero", func(t *testing.T) {
|
|
|
|
var id ID
|
|
|
|
require.Equal(t, emptyID, id.EncodeToString())
|
2021-11-08 10:04:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should be equal", func(t *testing.T) {
|
2024-09-03 10:20:30 +00:00
|
|
|
for i := range 10 {
|
2021-11-08 10:04:45 +00:00
|
|
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
|
|
|
cs := randSHA256Checksum(t)
|
|
|
|
str := base58.Encode(cs[:])
|
2022-04-11 16:25:14 +00:00
|
|
|
var oid ID
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.NoError(t, oid.DecodeString(str))
|
2022-05-31 06:55:08 +00:00
|
|
|
require.Equal(t, str, oid.EncodeToString())
|
2021-11-08 10:04:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObjectIDEncoding(t *testing.T) {
|
|
|
|
id := randID(t)
|
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data, err := id.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var id2 ID
|
2021-11-08 10:04:45 +00:00
|
|
|
require.NoError(t, id2.Unmarshal(data))
|
|
|
|
|
|
|
|
require.Equal(t, id, id2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := id.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var id2 ID
|
|
|
|
require.NoError(t, id2.UnmarshalJSON(data))
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.Equal(t, id, id2)
|
2021-11-08 10:04:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewIDFromV2(t *testing.T) {
|
2022-04-11 16:25:14 +00:00
|
|
|
t.Run("from zero", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
x ID
|
|
|
|
v2 refs.ObjectID
|
|
|
|
)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.Error(t, x.ReadFromV2(v2))
|
2021-11-08 10:04:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestID_ToV2(t *testing.T) {
|
2022-04-11 16:25:14 +00:00
|
|
|
t.Run("zero to v2", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
x ID
|
|
|
|
v2 refs.ObjectID
|
|
|
|
)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
x.WriteToV2(&v2)
|
|
|
|
|
|
|
|
require.Equal(t, sha256.Size, len(v2.GetValue()))
|
|
|
|
require.Equal(t, emptyID, base58.Encode(v2.GetValue()))
|
2021-11-08 10:04:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
func TestID_Encode(t *testing.T) {
|
|
|
|
var id ID
|
|
|
|
|
|
|
|
t.Run("panic", func(t *testing.T) {
|
|
|
|
dst := make([]byte, sha256.Size-1)
|
|
|
|
|
|
|
|
require.Panics(t, func() {
|
|
|
|
id.Encode(dst)
|
|
|
|
})
|
|
|
|
})
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
t.Run("correct", func(t *testing.T) {
|
|
|
|
dst := make([]byte, sha256.Size)
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
require.NotPanics(t, func() {
|
|
|
|
id.Encode(dst)
|
|
|
|
})
|
|
|
|
require.Equal(t, emptyID, id.EncodeToString())
|
2021-11-08 10:04:45 +00:00
|
|
|
})
|
|
|
|
}
|