2018-08-21 12:51:16 +00:00
|
|
|
package compiler_test
|
|
|
|
|
|
|
|
import (
|
2021-06-04 08:47:52 +00:00
|
|
|
"fmt"
|
2022-08-17 09:19:37 +00:00
|
|
|
"math/big"
|
2018-08-21 12:51:16 +00:00
|
|
|
"os"
|
2021-11-17 11:14:22 +00:00
|
|
|
"path/filepath"
|
2021-02-19 10:48:45 +00:00
|
|
|
"strings"
|
2018-08-21 12:51:16 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-06-25 07:54:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/random"
|
2023-08-29 17:39:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/versionutil"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
2021-06-24 15:36:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/neo"
|
2021-06-02 08:46:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2021-06-24 15:36:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-01-14 14:33:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-21 12:51:16 +00:00
|
|
|
)
|
|
|
|
|
2019-12-03 15:18:14 +00:00
|
|
|
const examplePath = "../../examples"
|
2020-01-14 14:33:04 +00:00
|
|
|
const exampleCompilePath = "testdata/compile"
|
|
|
|
const exampleSavePath = exampleCompilePath + "/save"
|
2018-08-21 12:51:16 +00:00
|
|
|
|
2023-08-29 17:39:27 +00:00
|
|
|
// Keep contract NEFs consistent between runs.
|
|
|
|
const _ = versionutil.TestVersion
|
|
|
|
|
2020-01-14 14:33:04 +00:00
|
|
|
type compilerTestCase struct {
|
|
|
|
name string
|
2020-02-29 15:55:16 +00:00
|
|
|
function func(*testing.T)
|
2020-01-14 14:33:04 +00:00
|
|
|
}
|
2018-08-21 12:51:16 +00:00
|
|
|
|
2020-01-14 14:33:04 +00:00
|
|
|
func TestCompiler(t *testing.T) {
|
|
|
|
testCases := []compilerTestCase{
|
2020-08-10 15:23:45 +00:00
|
|
|
{
|
|
|
|
name: "TestCompileDirectory",
|
|
|
|
function: func(t *testing.T) {
|
|
|
|
const multiMainDir = "testdata/multi"
|
2021-07-26 12:34:07 +00:00
|
|
|
_, di, err := compiler.CompileWithOptions(multiMainDir, nil, nil)
|
2020-08-10 15:23:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
m := map[string]bool{}
|
|
|
|
for i := range di.Methods {
|
2020-08-13 07:29:08 +00:00
|
|
|
m[di.Methods[i].ID] = true
|
2020-08-10 15:23:45 +00:00
|
|
|
}
|
|
|
|
require.Contains(t, m, "Func1")
|
|
|
|
require.Contains(t, m, "Func2")
|
|
|
|
},
|
|
|
|
},
|
2020-01-14 14:33:04 +00:00
|
|
|
{
|
|
|
|
name: "TestCompile",
|
2020-02-29 15:55:16 +00:00
|
|
|
function: func(t *testing.T) {
|
2022-02-22 16:27:32 +00:00
|
|
|
infos, err := os.ReadDir(examplePath)
|
2020-01-14 14:33:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
for _, info := range infos {
|
2021-03-22 18:13:29 +00:00
|
|
|
if !info.IsDir() {
|
|
|
|
// example smart contracts are located in the `examplePath` subdirectories, but
|
2022-04-20 18:30:09 +00:00
|
|
|
// there is also a couple of files inside the `examplePath` which don't need to be compiled
|
2021-03-22 18:13:29 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-07-29 11:02:33 +00:00
|
|
|
if info.Name() == "zkp" {
|
|
|
|
// A set of special ZKP-related examples, they have their own tests.
|
|
|
|
continue
|
|
|
|
}
|
2018-08-22 17:07:36 +00:00
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
targetPath := filepath.Join(examplePath, info.Name())
|
2021-12-02 14:44:53 +00:00
|
|
|
require.NoError(t, compileFile(targetPath), info.Name())
|
2020-01-14 14:33:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "TestCompileAndSave",
|
2020-02-29 15:55:16 +00:00
|
|
|
function: func(t *testing.T) {
|
2022-02-22 16:27:32 +00:00
|
|
|
infos, err := os.ReadDir(exampleCompilePath)
|
2020-01-14 14:33:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = os.MkdirAll(exampleSavePath, os.ModePerm)
|
|
|
|
require.NoError(t, err)
|
2021-03-01 11:14:15 +00:00
|
|
|
t.Cleanup(func() {
|
2020-01-14 14:33:04 +00:00
|
|
|
err := os.RemoveAll(exampleSavePath)
|
|
|
|
require.NoError(t, err)
|
2021-03-01 11:14:15 +00:00
|
|
|
})
|
2020-12-10 10:40:27 +00:00
|
|
|
outfile := exampleSavePath + "/test.nef"
|
|
|
|
_, err = compiler.CompileAndSave(exampleCompilePath+"/"+infos[0].Name(), &compiler.Options{Outfile: outfile})
|
|
|
|
require.NoError(t, err)
|
2020-01-14 14:33:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tcase := range testCases {
|
2020-02-29 15:55:16 +00:00
|
|
|
t.Run(tcase.name, tcase.function)
|
2018-08-21 12:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func compileFile(src string) error {
|
2020-08-10 15:23:45 +00:00
|
|
|
_, err := compiler.Compile(src, nil)
|
2018-08-21 12:51:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-19 10:48:45 +00:00
|
|
|
|
|
|
|
func TestOnPayableChecks(t *testing.T) {
|
|
|
|
compileAndCheck := func(t *testing.T, src string) error {
|
2021-12-02 13:36:29 +00:00
|
|
|
_, di, err := compiler.CompileWithOptions("payable.go", strings.NewReader(src), nil)
|
2021-02-19 10:48:45 +00:00
|
|
|
require.NoError(t, err)
|
2022-07-14 12:36:21 +00:00
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{Name: "payable"})
|
2021-02-19 10:48:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("NEP-11, good", func(t *testing.T) {
|
|
|
|
src := `package payable
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
2023-04-03 10:34:24 +00:00
|
|
|
func OnNEP11Payment(from interop.Hash160, amount int, tokenID []byte, data any) {}`
|
2021-02-19 10:48:45 +00:00
|
|
|
require.NoError(t, compileAndCheck(t, src))
|
|
|
|
})
|
|
|
|
t.Run("NEP-11, bad", func(t *testing.T) {
|
|
|
|
src := `package payable
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
2023-04-03 10:34:24 +00:00
|
|
|
func OnNEP11Payment(from interop.Hash160, amount int, oldParam string, tokenID []byte, data any) {}`
|
2021-02-19 10:48:45 +00:00
|
|
|
require.Error(t, compileAndCheck(t, src))
|
|
|
|
})
|
|
|
|
t.Run("NEP-17, good", func(t *testing.T) {
|
|
|
|
src := `package payable
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
2023-04-03 10:34:24 +00:00
|
|
|
func OnNEP17Payment(from interop.Hash160, amount int, data any) {}`
|
2021-02-19 10:48:45 +00:00
|
|
|
require.NoError(t, compileAndCheck(t, src))
|
|
|
|
})
|
|
|
|
t.Run("NEP-17, bad", func(t *testing.T) {
|
|
|
|
src := `package payable
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
2023-04-03 10:34:24 +00:00
|
|
|
func OnNEP17Payment(from interop.Hash160, amount int, data any, extra int) {}`
|
2021-02-19 10:48:45 +00:00
|
|
|
require.Error(t, compileAndCheck(t, src))
|
|
|
|
})
|
|
|
|
}
|
2021-06-02 08:46:08 +00:00
|
|
|
|
2021-10-06 12:18:57 +00:00
|
|
|
func TestSafeMethodWarnings(t *testing.T) {
|
|
|
|
src := `package payable
|
|
|
|
func Main() int { return 1 }`
|
|
|
|
|
2021-12-02 13:36:29 +00:00
|
|
|
_, di, err := compiler.CompileWithOptions("eventTest.go", strings.NewReader(src),
|
|
|
|
&compiler.Options{Name: "eventTest"})
|
2021-10-06 12:18:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-14 12:36:21 +00:00
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{SafeMethods: []string{"main"}, Name: "eventTest"})
|
2021-10-06 12:18:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-14 12:36:21 +00:00
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{SafeMethods: []string{"main", "mississippi"}, Name: "eventTest"})
|
2021-10-06 12:18:57 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2021-06-02 08:46:08 +00:00
|
|
|
func TestEventWarnings(t *testing.T) {
|
|
|
|
src := `package payable
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
func Main() { runtime.Notify("Event", 1) }`
|
|
|
|
|
2021-12-02 13:36:29 +00:00
|
|
|
_, di, err := compiler.CompileWithOptions("eventTest.go", strings.NewReader(src), nil)
|
2021-06-02 08:46:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("event it missing from config", func(t *testing.T) {
|
2022-07-14 12:36:21 +00:00
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{Name: "payable"})
|
2021-06-02 08:46:08 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
t.Run("suppress", func(t *testing.T) {
|
2022-07-14 12:36:21 +00:00
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{NoEventsCheck: true, Name: "payable"})
|
2021-06-02 08:46:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("wrong parameter number", func(t *testing.T) {
|
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{
|
2023-05-08 18:58:28 +00:00
|
|
|
ContractEvents: []compiler.HybridEvent{{Name: "Event"}},
|
2022-07-14 12:36:21 +00:00
|
|
|
Name: "payable",
|
2021-06-02 08:46:08 +00:00
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("wrong parameter type", func(t *testing.T) {
|
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{
|
2023-05-08 18:58:28 +00:00
|
|
|
ContractEvents: []compiler.HybridEvent{{
|
2021-06-02 08:46:08 +00:00
|
|
|
Name: "Event",
|
2023-05-08 18:58:28 +00:00
|
|
|
Parameters: []compiler.HybridParameter{{Parameter: manifest.NewParameter("number", smartcontract.StringType)}},
|
2021-06-02 08:46:08 +00:00
|
|
|
}},
|
2022-07-14 12:36:21 +00:00
|
|
|
Name: "payable",
|
2021-06-02 08:46:08 +00:00
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2022-09-29 12:13:29 +00:00
|
|
|
t.Run("any parameter type", func(t *testing.T) {
|
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{
|
2023-05-08 18:58:28 +00:00
|
|
|
ContractEvents: []compiler.HybridEvent{{
|
2022-09-29 12:13:29 +00:00
|
|
|
Name: "Event",
|
2023-05-08 18:58:28 +00:00
|
|
|
Parameters: []compiler.HybridParameter{{Parameter: manifest.NewParameter("number", smartcontract.AnyType)}},
|
2022-09-29 12:13:29 +00:00
|
|
|
}},
|
|
|
|
Name: "payable",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2021-06-02 08:46:08 +00:00
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{
|
2023-05-08 18:58:28 +00:00
|
|
|
ContractEvents: []compiler.HybridEvent{{
|
2021-06-02 08:46:08 +00:00
|
|
|
Name: "Event",
|
2023-05-08 18:58:28 +00:00
|
|
|
Parameters: []compiler.HybridParameter{{Parameter: manifest.NewParameter("number", smartcontract.IntegerType)}},
|
2021-06-02 08:46:08 +00:00
|
|
|
}},
|
2022-07-14 12:36:21 +00:00
|
|
|
Name: "payable",
|
2021-06-02 08:46:08 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2021-09-30 08:48:18 +00:00
|
|
|
t.Run("event in imported package", func(t *testing.T) {
|
|
|
|
t.Run("unused", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/notify"
|
|
|
|
func Main() int {
|
|
|
|
return notify.Value
|
|
|
|
}`
|
|
|
|
|
2021-12-02 13:36:29 +00:00
|
|
|
_, di, err := compiler.CompileWithOptions("eventTest.go", strings.NewReader(src), &compiler.Options{Name: "eventTest"})
|
2021-09-30 08:48:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-14 12:36:21 +00:00
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{NoEventsCheck: true, Name: "eventTest"})
|
2021-09-30 08:48:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("used", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/notify"
|
|
|
|
func Main() int {
|
|
|
|
notify.EmitEvent()
|
|
|
|
return 42
|
|
|
|
}`
|
|
|
|
|
2021-12-02 13:36:29 +00:00
|
|
|
_, di, err := compiler.CompileWithOptions("eventTest.go",
|
|
|
|
strings.NewReader(src), &compiler.Options{Name: "eventTest"})
|
2021-09-30 08:48:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-14 12:36:21 +00:00
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{Name: "eventTest"})
|
2021-09-30 08:48:18 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{
|
2023-05-08 18:58:28 +00:00
|
|
|
ContractEvents: []compiler.HybridEvent{{Name: "Event"}},
|
2022-07-14 12:36:21 +00:00
|
|
|
Name: "eventTest",
|
2021-09-30 08:48:18 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
})
|
2022-09-29 12:41:53 +00:00
|
|
|
t.Run("variadic event args via ellipsis", func(t *testing.T) {
|
|
|
|
src := `package payable
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
func Main() {
|
2023-04-03 10:34:24 +00:00
|
|
|
runtime.Notify("Event", []any{1}...)
|
2022-09-29 12:41:53 +00:00
|
|
|
}`
|
|
|
|
|
|
|
|
_, di, err := compiler.CompileWithOptions("eventTest.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = compiler.CreateManifest(di, &compiler.Options{
|
|
|
|
Name: "eventTest",
|
2023-05-08 18:58:28 +00:00
|
|
|
ContractEvents: []compiler.HybridEvent{{
|
2022-09-29 12:41:53 +00:00
|
|
|
Name: "Event",
|
2023-05-08 18:58:28 +00:00
|
|
|
Parameters: []compiler.HybridParameter{{Parameter: manifest.NewParameter("number", smartcontract.IntegerType)}},
|
2022-09-29 12:41:53 +00:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2021-06-02 08:46:08 +00:00
|
|
|
}
|
2021-06-04 08:47:52 +00:00
|
|
|
|
|
|
|
func TestNotifyInVerify(t *testing.T) {
|
|
|
|
srcTmpl := `package payable
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
func Verify() bool { runtime.%s("Event"); return true }`
|
|
|
|
|
|
|
|
for _, name := range []string{"Notify", "Log"} {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
src := fmt.Sprintf(srcTmpl, name)
|
2021-12-02 13:36:29 +00:00
|
|
|
_, _, err := compiler.CompileWithOptions("eventTest.go", strings.NewReader(src),
|
2023-05-08 18:58:28 +00:00
|
|
|
&compiler.Options{ContractEvents: []compiler.HybridEvent{{Name: "Event"}}})
|
2021-06-04 08:47:52 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
t.Run("suppress", func(t *testing.T) {
|
2021-12-02 13:36:29 +00:00
|
|
|
_, _, err := compiler.CompileWithOptions("eventTest.go", strings.NewReader(src),
|
2021-06-04 08:47:52 +00:00
|
|
|
&compiler.Options{NoEventsCheck: true})
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 15:36:40 +00:00
|
|
|
|
|
|
|
func TestInvokedContractsPermissons(t *testing.T) {
|
|
|
|
testCompile := func(t *testing.T, di *compiler.DebugInfo, disable bool, ps ...manifest.Permission) error {
|
|
|
|
o := &compiler.Options{
|
|
|
|
NoPermissionsCheck: disable,
|
|
|
|
Permissions: ps,
|
2022-07-14 12:36:21 +00:00
|
|
|
Name: "test",
|
2021-06-24 15:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := compiler.CreateManifest(di, o)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("native", func(t *testing.T) {
|
|
|
|
src := `package test
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/native/neo"
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
|
|
|
func Main() int {
|
|
|
|
neo.Transfer(nil, nil, 10, nil)
|
|
|
|
management.GetContract(nil) // skip read-only
|
|
|
|
return 0
|
|
|
|
}`
|
|
|
|
|
2021-12-02 13:36:29 +00:00
|
|
|
_, di, err := compiler.CompileWithOptions("permissionTest.go", strings.NewReader(src), nil)
|
2021-06-24 15:36:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var nh util.Uint160
|
|
|
|
|
|
|
|
p := manifest.NewPermission(manifest.PermissionHash, nh)
|
|
|
|
require.Error(t, testCompile(t, di, false, *p))
|
|
|
|
require.NoError(t, testCompile(t, di, true, *p))
|
|
|
|
|
|
|
|
copy(nh[:], neo.Hash)
|
|
|
|
p.Contract.Value = nh
|
|
|
|
require.NoError(t, testCompile(t, di, false, *p))
|
|
|
|
|
|
|
|
p.Methods.Restrict()
|
|
|
|
require.Error(t, testCompile(t, di, false, *p))
|
|
|
|
require.NoError(t, testCompile(t, di, true, *p))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("custom", func(t *testing.T) {
|
|
|
|
hashStr := "aaaaaaaaaaaaaaaaaaaa"
|
|
|
|
src := fmt.Sprintf(`package test
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/runh"
|
|
|
|
|
|
|
|
const hash = "%s"
|
|
|
|
var runtimeHash interop.Hash160
|
|
|
|
var runtimeMethod string
|
|
|
|
func invoke(h string) interop.Hash160 { return nil }
|
|
|
|
func Main() {
|
|
|
|
contract.Call(interop.Hash160(hash), "method1", contract.All)
|
|
|
|
contract.Call(interop.Hash160(hash), "method2", contract.All)
|
|
|
|
contract.Call(interop.Hash160(hash), "method2", contract.All)
|
|
|
|
|
|
|
|
// skip read-only
|
|
|
|
contract.Call(interop.Hash160(hash), "method3", contract.ReadStates)
|
|
|
|
|
|
|
|
// skip this
|
|
|
|
contract.Call(interop.Hash160(hash), runtimeMethod, contract.All)
|
|
|
|
contract.Call(runtimeHash, "someMethod", contract.All)
|
|
|
|
contract.Call(interop.Hash160(runtimeHash), "someMethod", contract.All)
|
2021-06-25 07:54:00 +00:00
|
|
|
contract.Call(runh.RuntimeHash(), "method4", contract.All)
|
2021-06-24 15:36:40 +00:00
|
|
|
}`, hashStr)
|
|
|
|
|
2021-12-02 13:36:29 +00:00
|
|
|
_, di, err := compiler.CompileWithOptions("permissionTest.go", strings.NewReader(src), nil)
|
2021-06-24 15:36:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var h util.Uint160
|
|
|
|
copy(h[:], hashStr)
|
|
|
|
|
|
|
|
p := manifest.NewPermission(manifest.PermissionHash, h)
|
|
|
|
require.NoError(t, testCompile(t, di, false, *p))
|
|
|
|
|
|
|
|
p.Methods.Add("method1")
|
|
|
|
require.Error(t, testCompile(t, di, false, *p))
|
|
|
|
require.NoError(t, testCompile(t, di, true, *p))
|
|
|
|
|
2021-06-25 07:54:00 +00:00
|
|
|
pr := manifest.NewPermission(manifest.PermissionHash, random.Uint160())
|
|
|
|
pr.Methods.Add("someMethod")
|
|
|
|
pr.Methods.Add("method4")
|
|
|
|
|
2021-06-24 15:36:40 +00:00
|
|
|
t.Run("wildcard", func(t *testing.T) {
|
|
|
|
pw := manifest.NewPermission(manifest.PermissionWildcard)
|
|
|
|
require.NoError(t, testCompile(t, di, false, *p, *pw))
|
|
|
|
|
|
|
|
pw.Methods.Add("method2")
|
2021-06-25 07:54:00 +00:00
|
|
|
require.Error(t, testCompile(t, di, false, *p, *pw))
|
|
|
|
require.NoError(t, testCompile(t, di, false, *p, *pw, *pr))
|
2021-06-24 15:36:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("group", func(t *testing.T) {
|
|
|
|
priv, _ := keys.NewPrivateKey()
|
|
|
|
pw := manifest.NewPermission(manifest.PermissionGroup, priv.PublicKey())
|
|
|
|
require.NoError(t, testCompile(t, di, false, *p, *pw))
|
|
|
|
|
|
|
|
pw.Methods.Add("invalid")
|
2021-06-25 07:54:00 +00:00
|
|
|
require.Error(t, testCompile(t, di, false, *p, *pw, *pr))
|
2021-06-24 15:36:40 +00:00
|
|
|
|
|
|
|
pw.Methods.Add("method2")
|
2021-06-25 07:54:00 +00:00
|
|
|
require.Error(t, testCompile(t, di, false, *p, *pw))
|
|
|
|
require.NoError(t, testCompile(t, di, false, *p, *pw, *pr))
|
2021-06-24 15:36:40 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2022-07-14 12:34:57 +00:00
|
|
|
|
|
|
|
func TestUnnamedParameterCheck(t *testing.T) {
|
|
|
|
t.Run("single argument", func(t *testing.T) {
|
|
|
|
src := `
|
|
|
|
package testcase
|
|
|
|
func Main(_ int) int {
|
|
|
|
x := 10
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.ErrorIs(t, err, compiler.ErrMissingExportedParamName)
|
|
|
|
})
|
|
|
|
t.Run("several arguments", func(t *testing.T) {
|
|
|
|
src := `
|
|
|
|
package testcase
|
|
|
|
func Main(a int, b string, _ int) int {
|
|
|
|
x := 10
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.ErrorIs(t, err, compiler.ErrMissingExportedParamName)
|
|
|
|
})
|
|
|
|
t.Run("interface", func(t *testing.T) {
|
|
|
|
src := `
|
|
|
|
package testcase
|
2023-04-03 10:34:24 +00:00
|
|
|
func OnNEP17Payment(h string, i int, _ any){}
|
2022-07-14 12:34:57 +00:00
|
|
|
`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.ErrorIs(t, err, compiler.ErrMissingExportedParamName)
|
|
|
|
})
|
|
|
|
t.Run("a set of unnamed params", func(t *testing.T) {
|
|
|
|
src := `
|
|
|
|
package testcase
|
2023-04-03 10:34:24 +00:00
|
|
|
func OnNEP17Payment(_ string, _ int, _ any){}
|
2022-07-14 12:34:57 +00:00
|
|
|
`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.ErrorIs(t, err, compiler.ErrMissingExportedParamName)
|
|
|
|
})
|
|
|
|
t.Run("mixed named and unnamed params", func(t *testing.T) {
|
|
|
|
src := `
|
|
|
|
package testcase
|
|
|
|
func OnNEP17Payment(s0, _, s2 string){}
|
|
|
|
`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.ErrorIs(t, err, compiler.ErrMissingExportedParamName)
|
|
|
|
})
|
|
|
|
t.Run("empty args", func(t *testing.T) {
|
|
|
|
src := `
|
|
|
|
package testcase
|
|
|
|
func OnNEP17Payment(){}
|
|
|
|
`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
src := `
|
|
|
|
package testcase
|
|
|
|
func OnNEP17Payment(s string, i int, iface interface{}){}
|
|
|
|
`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2023-04-03 10:34:24 +00:00
|
|
|
t.Run("good, use any keyword", func(t *testing.T) {
|
|
|
|
src := `
|
|
|
|
package testcase
|
|
|
|
func OnNEP17Payment(s string, i int, iface any){}
|
|
|
|
`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2022-08-17 09:23:31 +00:00
|
|
|
t.Run("method with unnamed params", func(t *testing.T) {
|
|
|
|
src := `
|
|
|
|
package testcase
|
|
|
|
type A int
|
2023-04-03 10:34:24 +00:00
|
|
|
func (rsv A) OnNEP17Payment(_ string, _ int, iface any){}
|
2022-08-17 09:23:31 +00:00
|
|
|
`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err) // it's OK for exported method to have unnamed params as it won't be included into manifest
|
|
|
|
})
|
2022-07-14 12:34:57 +00:00
|
|
|
}
|
2022-08-17 09:19:37 +00:00
|
|
|
|
|
|
|
func TestReturnValuesCountCheck(t *testing.T) {
|
|
|
|
t.Run("void", func(t *testing.T) {
|
|
|
|
t.Run("exported", func(t *testing.T) {
|
|
|
|
t.Run("func", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
var a int
|
|
|
|
func Main() {
|
|
|
|
a = 5
|
|
|
|
}`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("method", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
type A int
|
|
|
|
var a int
|
|
|
|
func (rcv A) Main() {
|
|
|
|
a = 5
|
|
|
|
}`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("unexported", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
var a int
|
|
|
|
func main() {
|
|
|
|
a = 5
|
|
|
|
}`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("single return", func(t *testing.T) {
|
|
|
|
t.Run("exported", func(t *testing.T) {
|
|
|
|
t.Run("func", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
var a int
|
|
|
|
func Main() int {
|
|
|
|
a = 5
|
|
|
|
return a
|
|
|
|
}`
|
|
|
|
eval(t, src, big.NewInt(5))
|
|
|
|
})
|
|
|
|
t.Run("method", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
type A int
|
|
|
|
var a int
|
|
|
|
func (rcv A) Main() int {
|
|
|
|
a = 5
|
|
|
|
return a
|
|
|
|
}`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("unexported", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
var a int
|
|
|
|
func main() int {
|
|
|
|
a = 5
|
|
|
|
return a
|
|
|
|
}`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("multiple unnamed return vals", func(t *testing.T) {
|
|
|
|
t.Run("exported", func(t *testing.T) {
|
|
|
|
t.Run("func", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
var a int
|
|
|
|
func Main() (int, int) {
|
|
|
|
a = 5
|
|
|
|
return a, a
|
|
|
|
}`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.ErrorIs(t, err, compiler.ErrInvalidExportedRetCount)
|
|
|
|
})
|
|
|
|
t.Run("method", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
type A int
|
|
|
|
var a int
|
|
|
|
func (rcv A) Main() (int, int) {
|
|
|
|
a = 5
|
|
|
|
return a, a
|
|
|
|
}`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err) // OK for method to have multiple return values as it won't be included into manifest
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("unexported", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
var a int
|
|
|
|
func main() (int, int) {
|
|
|
|
a = 5
|
|
|
|
return a, a
|
|
|
|
}`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err) // OK for unexported function to have multiple return values as it won't be included into manifest
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("multiple named return vals", func(t *testing.T) {
|
|
|
|
t.Run("exported", func(t *testing.T) {
|
|
|
|
t.Run("func", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
var a int
|
|
|
|
func Main() (a int, b int) {
|
|
|
|
a = 5
|
|
|
|
b = 2
|
|
|
|
return
|
|
|
|
}`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.ErrorIs(t, err, compiler.ErrInvalidExportedRetCount)
|
|
|
|
})
|
|
|
|
t.Run("method", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
type A int
|
|
|
|
var a int
|
|
|
|
func (rcv A) Main() (a int, b int) {
|
|
|
|
a = 5
|
|
|
|
b = 2
|
|
|
|
return
|
|
|
|
}`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err) // OK for method to have multiple return values as it won't be included into manifest
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("unexported", func(t *testing.T) {
|
|
|
|
src := `package testcase
|
|
|
|
var a int
|
|
|
|
func main() (a int, b int) {
|
|
|
|
a = 5
|
|
|
|
b = 2
|
|
|
|
return
|
|
|
|
}`
|
|
|
|
_, _, err := compiler.CompileWithOptions("test.go", strings.NewReader(src), nil)
|
|
|
|
require.NoError(t, err) // OK for unexported function to have multiple return values as it won't be included into manifest
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|