2022-04-11 06:30:22 +00:00
|
|
|
package user_test
|
|
|
|
|
|
|
|
import (
|
2024-07-12 12:42:55 +00:00
|
|
|
"bytes"
|
2023-09-08 14:15:08 +00:00
|
|
|
"crypto/rand"
|
2022-04-11 06:30:22 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-10-07 14:20:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
2023-03-07 11:20:03 +00:00
|
|
|
. "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
usertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user/test"
|
2022-04-11 06:30:22 +00:00
|
|
|
"github.com/mr-tron/base58"
|
2024-01-26 11:41:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2022-04-11 06:30:22 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestID_WalletBytes(t *testing.T) {
|
|
|
|
var scriptHash util.Uint160
|
|
|
|
rand.Read(scriptHash[:])
|
|
|
|
|
|
|
|
var id ID
|
|
|
|
id.SetScriptHash(scriptHash)
|
|
|
|
|
|
|
|
w := id.WalletBytes()
|
|
|
|
|
|
|
|
var m refs.OwnerID
|
|
|
|
m.SetValue(w)
|
|
|
|
|
|
|
|
err := id.ReadFromV2(m)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestID_SetScriptHash(t *testing.T) {
|
|
|
|
var scriptHash util.Uint160
|
|
|
|
rand.Read(scriptHash[:])
|
|
|
|
|
|
|
|
var id ID
|
|
|
|
id.SetScriptHash(scriptHash)
|
|
|
|
|
|
|
|
var m refs.OwnerID
|
|
|
|
id.WriteToV2(&m)
|
|
|
|
|
|
|
|
var id2 ID
|
|
|
|
|
|
|
|
err := id2.ReadFromV2(m)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, id2.Equals(id))
|
|
|
|
}
|
|
|
|
|
2024-01-26 11:41:55 +00:00
|
|
|
func TestID_ScriptHash(t *testing.T) {
|
|
|
|
userID := usertest.ID()
|
|
|
|
|
|
|
|
scriptHash, err := userID.ScriptHash()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ownerAddress := userID.EncodeToString()
|
|
|
|
decodedScriptHash, err := address.StringToUint160(ownerAddress)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, scriptHash.Equals(decodedScriptHash))
|
|
|
|
}
|
|
|
|
|
2022-04-11 06:30:22 +00:00
|
|
|
func TestV2_ID(t *testing.T) {
|
2023-11-21 08:35:10 +00:00
|
|
|
id := usertest.ID()
|
2022-04-11 06:30:22 +00:00
|
|
|
var m refs.OwnerID
|
|
|
|
var id2 ID
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
id.WriteToV2(&m)
|
|
|
|
|
|
|
|
err := id2.ReadFromV2(m)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, id2.Equals(id))
|
|
|
|
})
|
|
|
|
|
|
|
|
val := m.GetValue()
|
|
|
|
|
|
|
|
t.Run("invalid size", func(t *testing.T) {
|
|
|
|
m.SetValue(val[:24])
|
|
|
|
|
|
|
|
err := id2.ReadFromV2(m)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid prefix", func(t *testing.T) {
|
2024-07-12 12:42:55 +00:00
|
|
|
val := bytes.Clone(val)
|
2022-04-11 06:30:22 +00:00
|
|
|
val[0]++
|
|
|
|
|
|
|
|
m.SetValue(val)
|
|
|
|
|
|
|
|
err := id2.ReadFromV2(m)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid checksum", func(t *testing.T) {
|
2024-07-12 12:42:55 +00:00
|
|
|
val := bytes.Clone(val)
|
2022-04-11 06:30:22 +00:00
|
|
|
val[21]++
|
|
|
|
|
|
|
|
m.SetValue(val)
|
|
|
|
|
|
|
|
err := id2.ReadFromV2(m)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestID_EncodeToString(t *testing.T) {
|
2023-11-21 08:35:10 +00:00
|
|
|
id := usertest.ID()
|
2022-04-11 06:30:22 +00:00
|
|
|
|
|
|
|
s := id.EncodeToString()
|
|
|
|
|
|
|
|
_, err := base58.Decode(s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var id2 ID
|
|
|
|
|
|
|
|
err = id2.DecodeString(s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, id, id2)
|
|
|
|
|
|
|
|
err = id2.DecodeString("_") // any invalid bas58 string
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestID_Equal(t *testing.T) {
|
2023-11-21 08:35:10 +00:00
|
|
|
id1 := usertest.ID()
|
|
|
|
id2 := usertest.ID()
|
2022-04-11 06:30:22 +00:00
|
|
|
id3 := id1
|
|
|
|
|
|
|
|
require.True(t, id1.Equals(id1)) // self-equality
|
|
|
|
require.True(t, id1.Equals(id3))
|
|
|
|
require.True(t, id3.Equals(id1)) // commutativity
|
|
|
|
require.False(t, id1.Equals(id2))
|
|
|
|
}
|