2020-07-24 13:54:03 +00:00
|
|
|
package client
|
2020-07-10 14:17:51 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-28 12:59:40 +00:00
|
|
|
"math"
|
2020-08-27 10:54:26 +00:00
|
|
|
"math/big"
|
2020-07-10 14:17:51 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-10-28 12:59:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-08-27 10:54:26 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-07-10 14:17:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-10-28 12:59:40 +00:00
|
|
|
bigIntValue = new(big.Int).Mul(big.NewInt(math.MaxInt64), big.NewInt(10))
|
|
|
|
|
2020-08-27 10:54:26 +00:00
|
|
|
stringByteItem = stackitem.NewByteArray([]byte("Hello World"))
|
|
|
|
intItem = stackitem.NewBigInteger(new(big.Int).SetInt64(1))
|
2020-10-28 12:59:40 +00:00
|
|
|
bigIntItem = stackitem.NewBigInteger(bigIntValue)
|
2020-08-27 10:54:26 +00:00
|
|
|
byteWithIntItem = stackitem.NewByteArray([]byte{0x0a})
|
2020-10-28 12:59:40 +00:00
|
|
|
byteWithBigIntItem = stackitem.NewByteArray(bigint.ToBytes(bigIntValue))
|
2020-08-27 10:54:26 +00:00
|
|
|
emptyByteArrayItem = stackitem.NewByteArray([]byte{})
|
|
|
|
trueBoolItem = stackitem.NewBool(true)
|
|
|
|
falseBoolItem = stackitem.NewBool(false)
|
|
|
|
arrayItem = stackitem.NewArray([]stackitem.Item{intItem, stringByteItem})
|
|
|
|
anyTypeItem = stackitem.Null{}
|
2020-07-10 14:17:51 +00:00
|
|
|
)
|
|
|
|
|
2020-08-27 10:54:26 +00:00
|
|
|
func TestBoolFromStackItem(t *testing.T) {
|
|
|
|
t.Run("true assert", func(t *testing.T) {
|
|
|
|
val, err := BoolFromStackItem(trueBoolItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, val)
|
|
|
|
|
|
|
|
val, err = BoolFromStackItem(intItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, val)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("false assert", func(t *testing.T) {
|
|
|
|
val, err := BoolFromStackItem(falseBoolItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, val)
|
|
|
|
|
|
|
|
val, err = BoolFromStackItem(emptyByteArrayItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, val)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("incorrect assert", func(t *testing.T) {
|
|
|
|
_, err := BoolFromStackItem(arrayItem)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArrayFromStackItem(t *testing.T) {
|
|
|
|
t.Run("correct assert", func(t *testing.T) {
|
|
|
|
val, err := ArrayFromStackItem(arrayItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, val, len(arrayItem.Value().([]stackitem.Item)))
|
|
|
|
})
|
|
|
|
t.Run("incorrect assert", func(t *testing.T) {
|
|
|
|
_, err := ArrayFromStackItem(stringByteItem)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("nil array case", func(t *testing.T) {
|
|
|
|
val, err := ArrayFromStackItem(anyTypeItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, val)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesFromStackItem(t *testing.T) {
|
|
|
|
t.Run("correct assert", func(t *testing.T) {
|
|
|
|
val, err := BytesFromStackItem(stringByteItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, stringByteItem.Value().([]byte), val)
|
2021-04-15 13:23:12 +00:00
|
|
|
|
|
|
|
val, err = BytesFromStackItem(intItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, intItem.Value().(*big.Int).Bytes(), val)
|
2020-08-27 10:54:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("incorrect assert", func(t *testing.T) {
|
|
|
|
_, err := BytesFromStackItem(arrayItem)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntFromStackItem(t *testing.T) {
|
|
|
|
t.Run("correct assert", func(t *testing.T) {
|
|
|
|
val, err := IntFromStackItem(intItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, intItem.Value().(*big.Int).Int64(), val)
|
|
|
|
|
|
|
|
val, err = IntFromStackItem(byteWithIntItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(0x0a), val)
|
|
|
|
|
|
|
|
val, err = IntFromStackItem(emptyByteArrayItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(0), val)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("incorrect assert", func(t *testing.T) {
|
|
|
|
_, err := IntFromStackItem(arrayItem)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-28 12:59:40 +00:00
|
|
|
func TestBigIntFromStackItem(t *testing.T) {
|
|
|
|
t.Run("correct assert", func(t *testing.T) {
|
|
|
|
val, err := BigIntFromStackItem(bigIntItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, bigIntValue, val)
|
|
|
|
|
|
|
|
val, err = BigIntFromStackItem(byteWithBigIntItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, bigIntValue, val)
|
|
|
|
|
|
|
|
val, err = BigIntFromStackItem(emptyByteArrayItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, big.NewInt(0), val)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("incorrect assert", func(t *testing.T) {
|
|
|
|
_, err := BigIntFromStackItem(arrayItem)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-27 10:54:26 +00:00
|
|
|
func TestStringFromStackItem(t *testing.T) {
|
|
|
|
t.Run("correct assert", func(t *testing.T) {
|
|
|
|
val, err := StringFromStackItem(stringByteItem)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(stringByteItem.Value().([]byte)), val)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("incorrect assert", func(t *testing.T) {
|
|
|
|
_, err := StringFromStackItem(intItem)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|