2020-06-24 13:47:57 +00:00
|
|
|
package nef
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2020-06-24 13:47:57 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncodeDecodeBinary(t *testing.T) {
|
|
|
|
script := []byte{12, 32, 84, 35, 14}
|
|
|
|
expected := &File{
|
|
|
|
Header: Header{
|
|
|
|
Magic: Magic,
|
|
|
|
Compiler: "the best compiler ever",
|
2020-11-27 18:42:54 +00:00
|
|
|
Version: "1.2.3.4",
|
2020-06-24 13:47:57 +00:00
|
|
|
},
|
|
|
|
Script: script,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("invalid Magic", func(t *testing.T) {
|
|
|
|
expected.Header.Magic = 123
|
|
|
|
checkDecodeError(t, expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid checksum", func(t *testing.T) {
|
|
|
|
expected.Header.Magic = Magic
|
|
|
|
expected.Checksum = 123
|
|
|
|
checkDecodeError(t, expected)
|
|
|
|
})
|
|
|
|
|
2020-11-18 09:57:44 +00:00
|
|
|
t.Run("zero-length script", func(t *testing.T) {
|
|
|
|
expected.Script = make([]byte, 0)
|
2020-11-27 16:33:36 +00:00
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
2020-11-18 09:57:44 +00:00
|
|
|
checkDecodeError(t, expected)
|
|
|
|
})
|
|
|
|
|
2020-06-24 13:47:57 +00:00
|
|
|
t.Run("invalid script length", func(t *testing.T) {
|
|
|
|
newScript := make([]byte, MaxScriptLength+1)
|
|
|
|
expected.Script = newScript
|
2020-11-27 16:33:36 +00:00
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
2020-06-24 13:47:57 +00:00
|
|
|
checkDecodeError(t, expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
expected.Script = script
|
2020-11-27 16:33:36 +00:00
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
2020-06-24 13:47:57 +00:00
|
|
|
expected.Header.Magic = Magic
|
|
|
|
testserdes.EncodeDecodeBinary(t, expected, &File{})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDecodeError(t *testing.T, expected *File) {
|
|
|
|
bytes, err := testserdes.EncodeBinary(expected)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Error(t, testserdes.DecodeBinary(bytes, &File{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesFromBytes(t *testing.T) {
|
|
|
|
script := []byte{12, 32, 84, 35, 14}
|
|
|
|
expected := File{
|
|
|
|
Header: Header{
|
|
|
|
Magic: Magic,
|
|
|
|
Compiler: "the best compiler ever",
|
2020-11-27 18:42:54 +00:00
|
|
|
Version: "1.2.3.4",
|
2020-06-24 13:47:57 +00:00
|
|
|
},
|
|
|
|
Script: script,
|
|
|
|
}
|
2020-11-27 16:33:36 +00:00
|
|
|
expected.Checksum = expected.CalculateChecksum()
|
2020-06-24 13:47:57 +00:00
|
|
|
|
|
|
|
bytes, err := expected.Bytes()
|
|
|
|
require.NoError(t, err)
|
|
|
|
actual, err := FileFromBytes(bytes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|