2020-08-10 16:17:14 +00:00
|
|
|
package standard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func fooMethodBarEvent() *manifest.Manifest {
|
|
|
|
return &manifest.Manifest{
|
|
|
|
ABI: manifest.ABI{
|
|
|
|
Methods: []manifest.Method{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
{Type: smartcontract.ByteArrayType},
|
|
|
|
{Type: smartcontract.PublicKeyType},
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
2020-12-08 10:47:05 +00:00
|
|
|
Safe: true,
|
2020-08-10 16:17:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Events: []manifest.Event{
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
{Type: smartcontract.StringType},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestComplyMissingMethod(t *testing.T) {
|
|
|
|
m := fooMethodBarEvent()
|
2021-01-26 14:37:34 +00:00
|
|
|
m.ABI.GetMethod("foo", -1).Name = "notafoo"
|
2021-02-08 15:17:18 +00:00
|
|
|
err := Comply(m, &Standard{Manifest: *fooMethodBarEvent()})
|
2020-08-10 16:17:14 +00:00
|
|
|
require.True(t, errors.Is(err, ErrMethodMissing))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestComplyInvalidReturnType(t *testing.T) {
|
|
|
|
m := fooMethodBarEvent()
|
2021-01-26 14:37:34 +00:00
|
|
|
m.ABI.GetMethod("foo", -1).ReturnType = smartcontract.VoidType
|
2021-02-08 15:17:18 +00:00
|
|
|
err := Comply(m, &Standard{Manifest: *fooMethodBarEvent()})
|
2020-08-10 16:17:14 +00:00
|
|
|
require.True(t, errors.Is(err, ErrInvalidReturnType))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestComplyMethodParameterCount(t *testing.T) {
|
|
|
|
t.Run("Method", func(t *testing.T) {
|
|
|
|
m := fooMethodBarEvent()
|
2021-01-26 14:37:34 +00:00
|
|
|
f := m.ABI.GetMethod("foo", -1)
|
2020-08-10 16:17:14 +00:00
|
|
|
f.Parameters = append(f.Parameters, manifest.Parameter{Type: smartcontract.BoolType})
|
2021-02-08 15:17:18 +00:00
|
|
|
err := Comply(m, &Standard{Manifest: *fooMethodBarEvent()})
|
2021-01-26 14:37:34 +00:00
|
|
|
require.True(t, errors.Is(err, ErrMethodMissing))
|
2020-08-10 16:17:14 +00:00
|
|
|
})
|
|
|
|
t.Run("Event", func(t *testing.T) {
|
|
|
|
m := fooMethodBarEvent()
|
|
|
|
ev := m.ABI.GetEvent("bar")
|
2021-05-12 16:12:09 +00:00
|
|
|
ev.Parameters = ev.Parameters[:0]
|
2021-02-08 15:17:18 +00:00
|
|
|
err := Comply(m, &Standard{Manifest: *fooMethodBarEvent()})
|
2020-08-10 16:17:14 +00:00
|
|
|
require.True(t, errors.Is(err, ErrInvalidParameterCount))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestComplyParameterType(t *testing.T) {
|
|
|
|
t.Run("Method", func(t *testing.T) {
|
|
|
|
m := fooMethodBarEvent()
|
2021-01-26 14:37:34 +00:00
|
|
|
m.ABI.GetMethod("foo", -1).Parameters[0].Type = smartcontract.InteropInterfaceType
|
2021-02-08 15:17:18 +00:00
|
|
|
err := Comply(m, &Standard{Manifest: *fooMethodBarEvent()})
|
2020-08-10 16:17:14 +00:00
|
|
|
require.True(t, errors.Is(err, ErrInvalidParameterType))
|
|
|
|
})
|
|
|
|
t.Run("Event", func(t *testing.T) {
|
|
|
|
m := fooMethodBarEvent()
|
|
|
|
m.ABI.GetEvent("bar").Parameters[0].Type = smartcontract.InteropInterfaceType
|
2021-02-08 15:17:18 +00:00
|
|
|
err := Comply(m, &Standard{Manifest: *fooMethodBarEvent()})
|
2020-08-10 16:17:14 +00:00
|
|
|
require.True(t, errors.Is(err, ErrInvalidParameterType))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-19 10:13:36 +00:00
|
|
|
func TestComplyParameterName(t *testing.T) {
|
|
|
|
t.Run("Method", func(t *testing.T) {
|
|
|
|
m := fooMethodBarEvent()
|
|
|
|
m.ABI.GetMethod("foo", -1).Parameters[0].Name = "hehe"
|
|
|
|
s := &Standard{Manifest: *fooMethodBarEvent()}
|
|
|
|
err := Comply(m, s)
|
|
|
|
require.True(t, errors.Is(err, ErrInvalidParameterName))
|
|
|
|
require.NoError(t, ComplyABI(m, s))
|
|
|
|
})
|
|
|
|
t.Run("Event", func(t *testing.T) {
|
|
|
|
m := fooMethodBarEvent()
|
|
|
|
m.ABI.GetEvent("bar").Parameters[0].Name = "hehe"
|
|
|
|
s := &Standard{Manifest: *fooMethodBarEvent()}
|
|
|
|
err := Comply(m, s)
|
|
|
|
require.True(t, errors.Is(err, ErrInvalidParameterName))
|
|
|
|
require.NoError(t, ComplyABI(m, s))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-10 16:17:14 +00:00
|
|
|
func TestMissingEvent(t *testing.T) {
|
|
|
|
m := fooMethodBarEvent()
|
|
|
|
m.ABI.GetEvent("bar").Name = "notabar"
|
2021-02-08 15:17:18 +00:00
|
|
|
err := Comply(m, &Standard{Manifest: *fooMethodBarEvent()})
|
2020-08-10 16:17:14 +00:00
|
|
|
require.True(t, errors.Is(err, ErrEventMissing))
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:47:05 +00:00
|
|
|
func TestSafeFlag(t *testing.T) {
|
|
|
|
m := fooMethodBarEvent()
|
2021-01-26 14:37:34 +00:00
|
|
|
m.ABI.GetMethod("foo", -1).Safe = false
|
2021-02-08 15:17:18 +00:00
|
|
|
err := Comply(m, &Standard{Manifest: *fooMethodBarEvent()})
|
2020-12-08 10:47:05 +00:00
|
|
|
require.True(t, errors.Is(err, ErrSafeMethodMismatch))
|
|
|
|
}
|
|
|
|
|
2020-08-10 16:17:14 +00:00
|
|
|
func TestComplyValid(t *testing.T) {
|
|
|
|
m := fooMethodBarEvent()
|
|
|
|
m.ABI.Methods = append(m.ABI.Methods, manifest.Method{
|
|
|
|
Name: "newmethod",
|
|
|
|
Offset: 123,
|
|
|
|
ReturnType: smartcontract.ByteArrayType,
|
|
|
|
})
|
|
|
|
m.ABI.Events = append(m.ABI.Events, manifest.Event{
|
|
|
|
Name: "otherevent",
|
|
|
|
Parameters: []manifest.Parameter{{
|
|
|
|
Name: "names do not matter",
|
|
|
|
Type: smartcontract.IntegerType,
|
|
|
|
}},
|
|
|
|
})
|
2021-02-08 15:17:18 +00:00
|
|
|
require.NoError(t, Comply(m, &Standard{Manifest: *fooMethodBarEvent()}))
|
2020-08-10 16:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheck(t *testing.T) {
|
2020-11-18 09:43:51 +00:00
|
|
|
m := manifest.NewManifest("Test")
|
2020-08-10 16:17:14 +00:00
|
|
|
require.Error(t, Check(m, manifest.NEP17StandardName))
|
|
|
|
|
2021-02-08 15:17:18 +00:00
|
|
|
m.ABI.Methods = append(m.ABI.Methods, decimalTokenBase.ABI.Methods...)
|
|
|
|
m.ABI.Methods = append(m.ABI.Methods, nep17.ABI.Methods...)
|
|
|
|
m.ABI.Events = append(m.ABI.Events, nep17.ABI.Events...)
|
|
|
|
require.NoError(t, Check(m, manifest.NEP17StandardName))
|
2021-02-19 10:13:36 +00:00
|
|
|
require.NoError(t, CheckABI(m, manifest.NEP17StandardName))
|
2020-08-10 16:17:14 +00:00
|
|
|
}
|
2021-02-19 09:05:30 +00:00
|
|
|
|
|
|
|
func TestOptional(t *testing.T) {
|
|
|
|
var m Standard
|
|
|
|
m.Optional = []manifest.Method{{
|
|
|
|
Name: "optMet",
|
|
|
|
Parameters: []manifest.Parameter{{Type: smartcontract.ByteArrayType}},
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
|
|
|
}}
|
|
|
|
|
|
|
|
t.Run("wrong parameter count, do not check", func(t *testing.T) {
|
|
|
|
var actual manifest.Manifest
|
|
|
|
actual.ABI.Methods = []manifest.Method{{
|
|
|
|
Name: "optMet",
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
|
|
|
}}
|
|
|
|
require.NoError(t, Comply(&actual, &m))
|
|
|
|
})
|
|
|
|
t.Run("good parameter count, bad return", func(t *testing.T) {
|
|
|
|
var actual manifest.Manifest
|
|
|
|
actual.ABI.Methods = []manifest.Method{{
|
|
|
|
Name: "optMet",
|
|
|
|
Parameters: []manifest.Parameter{{Type: smartcontract.ArrayType}},
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
|
|
|
}}
|
|
|
|
require.Error(t, Comply(&actual, &m))
|
|
|
|
})
|
|
|
|
t.Run("good parameter count, good return", func(t *testing.T) {
|
|
|
|
var actual manifest.Manifest
|
|
|
|
actual.ABI.Methods = []manifest.Method{{
|
|
|
|
Name: "optMet",
|
|
|
|
Parameters: []manifest.Parameter{{Type: smartcontract.ByteArrayType}},
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
|
|
|
}}
|
|
|
|
require.NoError(t, Comply(&actual, &m))
|
|
|
|
})
|
|
|
|
}
|