2020-01-28 14:18:38 +00:00
|
|
|
package compiler_test
|
|
|
|
|
|
|
|
import (
|
2020-07-28 13:38:00 +00:00
|
|
|
"errors"
|
2020-01-28 14:18:38 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-13 07:41:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-01-28 14:18:38 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPanic(t *testing.T) {
|
|
|
|
t.Run("no panic", func(t *testing.T) {
|
|
|
|
src := getPanicSource(false, `"execution fault"`)
|
|
|
|
eval(t, src, big.NewInt(7))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("panic with message", func(t *testing.T) {
|
|
|
|
var logs []string
|
|
|
|
src := getPanicSource(true, `"execution fault"`)
|
|
|
|
v := vmAndCompile(t, src)
|
2020-07-28 13:38:00 +00:00
|
|
|
v.SyscallHandler = getLogHandler(&logs)
|
2020-01-28 14:18:38 +00:00
|
|
|
|
|
|
|
require.Error(t, v.Run())
|
|
|
|
require.True(t, v.HasFailed())
|
|
|
|
require.Equal(t, 1, len(logs))
|
|
|
|
require.Equal(t, "execution fault", logs[0])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("panic with nil", func(t *testing.T) {
|
|
|
|
var logs []string
|
|
|
|
src := getPanicSource(true, `nil`)
|
|
|
|
v := vmAndCompile(t, src)
|
2020-07-28 13:38:00 +00:00
|
|
|
v.SyscallHandler = getLogHandler(&logs)
|
2020-01-28 14:18:38 +00:00
|
|
|
|
|
|
|
require.Error(t, v.Run())
|
|
|
|
require.True(t, v.HasFailed())
|
|
|
|
require.Equal(t, 0, len(logs))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPanicSource(need bool, message string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
package main
|
|
|
|
func Main() int {
|
|
|
|
needPanic := %#v
|
|
|
|
if needPanic {
|
|
|
|
panic(%s)
|
|
|
|
return 5
|
|
|
|
}
|
|
|
|
return 7
|
|
|
|
}
|
|
|
|
`, need, message)
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:38:00 +00:00
|
|
|
func getLogHandler(logs *[]string) vm.SyscallHandler {
|
2020-08-13 07:41:33 +00:00
|
|
|
logID := interopnames.ToID([]byte("System.Runtime.Log"))
|
2020-07-28 13:38:00 +00:00
|
|
|
return func(v *vm.VM, id uint32) error {
|
2020-01-28 14:18:38 +00:00
|
|
|
if id != logID {
|
2020-07-28 13:38:00 +00:00
|
|
|
return errors.New("syscall not found")
|
2020-01-28 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 08:18:51 +00:00
|
|
|
msg := v.Estack().Pop().String()
|
2020-07-28 13:38:00 +00:00
|
|
|
*logs = append(*logs, msg)
|
|
|
|
return nil
|
2020-01-28 14:18:38 +00:00
|
|
|
}
|
|
|
|
}
|