2020-09-16 11:50:31 +00:00
|
|
|
package transaction
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/random"
|
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2020-11-18 11:00:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-09-16 11:50:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAttribute_EncodeBinary(t *testing.T) {
|
|
|
|
t.Run("HighPriority", func(t *testing.T) {
|
|
|
|
attr := &Attribute{
|
|
|
|
Type: HighPriority,
|
|
|
|
}
|
|
|
|
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
|
|
|
|
})
|
|
|
|
t.Run("OracleResponse", func(t *testing.T) {
|
|
|
|
attr := &Attribute{
|
|
|
|
Type: OracleResponseT,
|
|
|
|
Value: &OracleResponse{
|
|
|
|
ID: 0x1122334455,
|
|
|
|
Code: Success,
|
|
|
|
Result: []byte{1, 2, 3},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
|
2021-04-06 18:35:56 +00:00
|
|
|
for _, code := range []OracleResponseCode{ProtocolNotSupported, ConsensusUnreachable,
|
|
|
|
NotFound, Timeout, Forbidden, ResponseTooLarge, InsufficientFunds, Error} {
|
|
|
|
attr = &Attribute{
|
|
|
|
Type: OracleResponseT,
|
|
|
|
Value: &OracleResponse{
|
|
|
|
ID: 42,
|
|
|
|
Code: code,
|
|
|
|
Result: []byte{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
|
|
|
|
}
|
2020-09-16 11:50:31 +00:00
|
|
|
})
|
2020-10-14 16:07:16 +00:00
|
|
|
t.Run("NotValidBefore", func(t *testing.T) {
|
2020-11-18 11:13:09 +00:00
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
attr := &Attribute{
|
|
|
|
Type: NotValidBeforeT,
|
|
|
|
Value: &NotValidBefore{
|
|
|
|
Height: 123,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
|
|
|
|
})
|
|
|
|
t.Run("bad format: too short", func(t *testing.T) {
|
|
|
|
bw := io.NewBufBinWriter()
|
2022-06-01 11:25:30 +00:00
|
|
|
bw.WriteBytes([]byte{1, 2, 3})
|
2020-11-18 11:13:09 +00:00
|
|
|
require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(NotValidBefore)))
|
|
|
|
})
|
2020-10-14 16:07:16 +00:00
|
|
|
})
|
2020-10-15 10:06:22 +00:00
|
|
|
t.Run("Reserved", func(t *testing.T) {
|
|
|
|
getReservedAttribute := func(t AttrType) *Attribute {
|
|
|
|
return &Attribute{
|
|
|
|
Type: t,
|
|
|
|
Value: &Reserved{
|
|
|
|
Value: []byte{1, 2, 3, 4, 5},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Run("lower bound", func(t *testing.T) {
|
2020-11-18 11:26:13 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, getReservedAttribute(ReservedLowerBound+3), new(Attribute))
|
2020-10-15 10:06:22 +00:00
|
|
|
})
|
|
|
|
t.Run("upper bound", func(t *testing.T) {
|
|
|
|
testserdes.EncodeDecodeBinary(t, getReservedAttribute(ReservedUpperBound), new(Attribute))
|
|
|
|
})
|
|
|
|
t.Run("inside bounds", func(t *testing.T) {
|
|
|
|
testserdes.EncodeDecodeBinary(t, getReservedAttribute(ReservedLowerBound+5), new(Attribute))
|
|
|
|
})
|
|
|
|
t.Run("not reserved", func(t *testing.T) {
|
|
|
|
_, err := testserdes.EncodeBinary(getReservedAttribute(ReservedLowerBound - 1))
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
})
|
2020-10-15 11:45:29 +00:00
|
|
|
t.Run("Conflicts", func(t *testing.T) {
|
2020-11-18 11:00:24 +00:00
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
attr := &Attribute{
|
|
|
|
Type: ConflictsT,
|
|
|
|
Value: &Conflicts{
|
|
|
|
Hash: random.Uint256(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
|
|
|
|
})
|
|
|
|
t.Run("negative: bad uint256", func(t *testing.T) {
|
|
|
|
bw := io.NewBufBinWriter()
|
2022-06-01 11:25:30 +00:00
|
|
|
bw.WriteBytes(make([]byte, util.Uint256Size-1))
|
2020-11-18 11:00:24 +00:00
|
|
|
require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(Conflicts)))
|
|
|
|
})
|
2020-10-15 11:45:29 +00:00
|
|
|
})
|
2020-11-18 11:26:13 +00:00
|
|
|
t.Run("NotaryAssisted", func(t *testing.T) {
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
attr := &Attribute{
|
|
|
|
Type: NotaryAssistedT,
|
|
|
|
Value: &NotaryAssisted{
|
|
|
|
NKeys: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
|
|
|
|
})
|
|
|
|
t.Run("bad format: too short", func(t *testing.T) {
|
|
|
|
bw := io.NewBufBinWriter()
|
2022-06-01 11:25:30 +00:00
|
|
|
bw.WriteBytes([]byte{})
|
2020-11-18 11:26:13 +00:00
|
|
|
require.Error(t, testserdes.DecodeBinary(bw.Bytes(), new(NotaryAssisted)))
|
|
|
|
})
|
|
|
|
})
|
2020-09-16 11:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttribute_MarshalJSON(t *testing.T) {
|
|
|
|
t.Run("HighPriority", func(t *testing.T) {
|
|
|
|
attr := &Attribute{Type: HighPriority}
|
|
|
|
data, err := json.Marshal(attr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.JSONEq(t, `{"type":"HighPriority"}`, string(data))
|
|
|
|
|
|
|
|
actual := new(Attribute)
|
|
|
|
require.NoError(t, json.Unmarshal(data, actual))
|
|
|
|
require.Equal(t, attr, actual)
|
|
|
|
})
|
|
|
|
t.Run("OracleResponse", func(t *testing.T) {
|
|
|
|
res := []byte{1, 2, 3}
|
|
|
|
attr := &Attribute{
|
|
|
|
Type: OracleResponseT,
|
|
|
|
Value: &OracleResponse{
|
|
|
|
ID: 123,
|
|
|
|
Code: Success,
|
|
|
|
Result: res,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(attr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.JSONEq(t, `{
|
|
|
|
"type":"OracleResponse",
|
|
|
|
"id": 123,
|
2021-04-06 18:35:56 +00:00
|
|
|
"code": "Success",
|
2020-09-16 11:50:31 +00:00
|
|
|
"result": "`+base64.StdEncoding.EncodeToString(res)+`"}`, string(data))
|
|
|
|
|
|
|
|
actual := new(Attribute)
|
|
|
|
require.NoError(t, json.Unmarshal(data, actual))
|
|
|
|
require.Equal(t, attr, actual)
|
|
|
|
testserdes.EncodeDecodeBinary(t, attr, new(Attribute))
|
|
|
|
})
|
2020-10-14 16:07:16 +00:00
|
|
|
t.Run("NotValidBefore", func(t *testing.T) {
|
|
|
|
attr := &Attribute{
|
|
|
|
Type: NotValidBeforeT,
|
|
|
|
Value: &NotValidBefore{
|
|
|
|
Height: 123,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testserdes.MarshalUnmarshalJSON(t, attr, new(Attribute))
|
|
|
|
})
|
2020-10-15 11:45:29 +00:00
|
|
|
t.Run("Conflicts", func(t *testing.T) {
|
|
|
|
attr := &Attribute{
|
|
|
|
Type: ConflictsT,
|
|
|
|
Value: &Conflicts{
|
|
|
|
Hash: random.Uint256(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testserdes.MarshalUnmarshalJSON(t, attr, new(Attribute))
|
|
|
|
})
|
2020-11-18 11:26:13 +00:00
|
|
|
t.Run("NotaryAssisted", func(t *testing.T) {
|
|
|
|
attr := &Attribute{
|
|
|
|
Type: NotaryAssistedT,
|
|
|
|
Value: &NotaryAssisted{
|
|
|
|
NKeys: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testserdes.MarshalUnmarshalJSON(t, attr, new(Attribute))
|
|
|
|
})
|
2020-09-16 11:50:31 +00:00
|
|
|
}
|