github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/transaction/witness_rule_test.go (about) 1 package transaction 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/nspcc-dev/neo-go/internal/testserdes" 8 "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestWitnessRuleSerDes(t *testing.T) { 13 var b bool 14 expected := &WitnessRule{ 15 Action: WitnessAllow, 16 Condition: (*ConditionBoolean)(&b), 17 } 18 actual := &WitnessRule{} 19 testserdes.EncodeDecodeBinary(t, expected, actual) 20 } 21 22 func TestWitnessRuleSerDesBad(t *testing.T) { 23 var b bool 24 bad := &WitnessRule{ 25 Action: 0xff, 26 Condition: (*ConditionBoolean)(&b), 27 } 28 badB, err := testserdes.EncodeBinary(bad) 29 require.NoError(t, err) 30 err = testserdes.DecodeBinary(badB, &WitnessRule{}) 31 require.Error(t, err) 32 } 33 34 func TestWitnessRuleJSON(t *testing.T) { 35 var b bool 36 expected := &WitnessRule{ 37 Action: WitnessDeny, 38 Condition: (*ConditionBoolean)(&b), 39 } 40 actual := &WitnessRule{} 41 testserdes.MarshalUnmarshalJSON(t, expected, actual) 42 } 43 44 func TestWitnessRuleBadJSON(t *testing.T) { 45 var cases = []string{ 46 `{}`, 47 `[]`, 48 `{"action":"Allow"}`, 49 `{"action":"Unknown","condition":{"type":"Boolean", "expression":true}}`, 50 `{"action":"Allow","condition":{"type":"Boolean", "expression":42}}`, 51 } 52 for i := range cases { 53 actual := &WitnessRule{} 54 err := json.Unmarshal([]byte(cases[i]), actual) 55 require.Errorf(t, err, "case %d, json %s", i, cases[i]) 56 } 57 } 58 59 func TestWitnessRule_ToStackItem(t *testing.T) { 60 var b bool 61 for _, act := range []WitnessAction{WitnessDeny, WitnessAllow} { 62 expected := stackitem.NewArray([]stackitem.Item{ 63 stackitem.Make(int64(act)), 64 stackitem.Make([]stackitem.Item{ 65 stackitem.Make(WitnessBoolean), 66 stackitem.Make(b), 67 }), 68 }) 69 actual := (&WitnessRule{ 70 Action: act, 71 Condition: (*ConditionBoolean)(&b), 72 }).ToStackItem() 73 require.Equal(t, expected, actual, act) 74 } 75 } 76 77 func TestWitnessRule_Copy(t *testing.T) { 78 b := true 79 wr := &WitnessRule{ 80 Action: WitnessDeny, 81 Condition: (*ConditionBoolean)(&b), 82 } 83 copied := wr.Copy() 84 require.Equal(t, wr.Action, copied.Action) 85 require.Equal(t, wr.Condition, copied.Condition) 86 require.NotSame(t, wr.Condition, copied.Condition) 87 }