neoneo-go/pkg/core/transaction/witness_rule_test.go
Roman Khimov 9875799893 transaction: add new Rules witness scope
See neo-project/neo#2622. The implementation is somewhat asymmetric (and not
very efficient) for binary/JSON encoding/decoding, but it should be
sufficient.
2021-11-12 15:29:28 +03:00

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])
}
}