mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-30 15:01:35 +00:00
9875799893
See neo-project/neo#2622. The implementation is somewhat asymmetric (and not very efficient) for binary/JSON encoding/decoding, but it should be sufficient.
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package transaction
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWitnessRuleSerDes(t *testing.T) {
|
|
var b bool
|
|
expected := &WitnessRule{
|
|
Action: WitnessAllow,
|
|
Condition: (*ConditionBoolean)(&b),
|
|
}
|
|
actual := &WitnessRule{}
|
|
testserdes.EncodeDecodeBinary(t, expected, actual)
|
|
}
|
|
|
|
func TestWitnessRuleSerDesBad(t *testing.T) {
|
|
var b bool
|
|
bad := &WitnessRule{
|
|
Action: 0xff,
|
|
Condition: (*ConditionBoolean)(&b),
|
|
}
|
|
badB, err := testserdes.EncodeBinary(bad)
|
|
require.NoError(t, err)
|
|
err = testserdes.DecodeBinary(badB, &WitnessRule{})
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestWitnessRuleJSON(t *testing.T) {
|
|
var b bool
|
|
expected := &WitnessRule{
|
|
Action: WitnessDeny,
|
|
Condition: (*ConditionBoolean)(&b),
|
|
}
|
|
actual := &WitnessRule{}
|
|
testserdes.MarshalUnmarshalJSON(t, expected, actual)
|
|
}
|
|
|
|
func TestWitnessRuleBadJSON(t *testing.T) {
|
|
var cases = []string{
|
|
`{}`,
|
|
`[]`,
|
|
`{"action":"Allow"}`,
|
|
`{"action":"Unknown","condition":{"type":"Boolean", "expression":true}}`,
|
|
`{"action":"Allow","condition":{"type":"Boolean", "expression":42}}`,
|
|
}
|
|
for i := range cases {
|
|
actual := &WitnessRule{}
|
|
err := json.Unmarshal([]byte(cases[i]), actual)
|
|
require.Errorf(t, err, "case %d, json %s", i, cases[i])
|
|
}
|
|
}
|