github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/network/payload/extensible_test.go (about)

     1  package payload
     2  
     3  import (
     4  	gio "io"
     5  	"testing"
     6  
     7  	"github.com/nspcc-dev/neo-go/internal/random"
     8  	"github.com/nspcc-dev/neo-go/internal/testserdes"
     9  	"github.com/nspcc-dev/neo-go/pkg/core/transaction"
    10  	"github.com/nspcc-dev/neo-go/pkg/io"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestExtensible_Serializable(t *testing.T) {
    15  	expected := &Extensible{
    16  		Category:        "test",
    17  		ValidBlockStart: 12,
    18  		ValidBlockEnd:   1234,
    19  		Sender:          random.Uint160(),
    20  		Data:            random.Bytes(4),
    21  		Witness: transaction.Witness{
    22  			InvocationScript:   random.Bytes(3),
    23  			VerificationScript: random.Bytes(3),
    24  		},
    25  	}
    26  
    27  	testserdes.EncodeDecodeBinary(t, expected, new(Extensible))
    28  
    29  	t.Run("invalid", func(t *testing.T) {
    30  		w := io.NewBufBinWriter()
    31  		expected.encodeBinaryUnsigned(w.BinWriter)
    32  		unsigned := w.Bytes()
    33  
    34  		t.Run("unexpected EOF", func(t *testing.T) {
    35  			err := testserdes.DecodeBinary(unsigned, new(Extensible))
    36  			require.ErrorIs(t, err, gio.EOF)
    37  		})
    38  		t.Run("invalid padding", func(t *testing.T) {
    39  			err := testserdes.DecodeBinary(append(unsigned, 42), new(Extensible))
    40  			require.ErrorIs(t, err, errInvalidPadding)
    41  		})
    42  		t.Run("too large data size", func(t *testing.T) {
    43  			expected.Data = make([]byte, MaxSize+1)
    44  			w := io.NewBufBinWriter()
    45  			expected.encodeBinaryUnsigned(w.BinWriter)
    46  			unsigned = w.Bytes()
    47  			err := testserdes.DecodeBinary(unsigned, new(Extensible))
    48  			require.NotNil(t, err)
    49  		})
    50  	})
    51  }
    52  
    53  func TestExtensible_Hashes(t *testing.T) {
    54  	getExtensiblePair := func() (*Extensible, *Extensible) {
    55  		p1 := NewExtensible()
    56  		p1.Data = []byte{1, 2, 3}
    57  		p2 := NewExtensible()
    58  		p2.Data = []byte{3, 2, 1}
    59  		return p1, p2
    60  	}
    61  
    62  	t.Run("Hash", func(t *testing.T) {
    63  		p1, p2 := getExtensiblePair()
    64  		require.NotEqual(t, p1.Hash(), p2.Hash())
    65  		require.NotEqual(t, p1.Hash(), p2.Hash())
    66  	})
    67  }