github.com/braveheart12/insolar-09-08-19@v0.8.7/network/transport/packet/packet_test.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package packet
    19  
    20  import (
    21  	"bytes"
    22  	"crypto/rand"
    23  	"encoding/gob"
    24  	"testing"
    25  
    26  	"github.com/insolar/insolar/network/transport/host"
    27  	"github.com/insolar/insolar/testutils"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func init() {
    32  	gob.Register(&RequestTest{})
    33  }
    34  
    35  func TestSerializePacket(t *testing.T) {
    36  	sender, _ := host.NewHostN("127.0.0.1:31337", testutils.RandomRef())
    37  	receiver, _ := host.NewHostN("127.0.0.2:31338", testutils.RandomRef())
    38  	builder := NewBuilder(sender)
    39  	msg := builder.Receiver(receiver).Type(TestPacket).Request(&RequestTest{[]byte{0, 1, 2, 3}}).Build()
    40  
    41  	_, err := SerializePacket(msg)
    42  
    43  	require.NoError(t, err)
    44  }
    45  
    46  func TestDeserializePacket(t *testing.T) {
    47  	sender, _ := host.NewHostN("127.0.0.1:31337", testutils.RandomRef())
    48  	receiver, _ := host.NewHostN("127.0.0.2:31338", testutils.RandomRef())
    49  	builder := NewBuilder(sender)
    50  	msg := builder.Receiver(receiver).Type(TestPacket).Request(&RequestTest{[]byte{0, 1, 2, 3}}).Build()
    51  
    52  	serialized, _ := SerializePacket(msg)
    53  
    54  	var buffer bytes.Buffer
    55  
    56  	buffer.Write(serialized)
    57  
    58  	deserialized, err := DeserializePacket(&buffer)
    59  
    60  	require.NoError(t, err)
    61  	require.Equal(t, deserialized, msg)
    62  }
    63  
    64  func TestDeserializeBigPacket(t *testing.T) {
    65  	hostOne, _ := host.NewHost("127.0.0.1:31337")
    66  
    67  	data := make([]byte, 1024*1024*10)
    68  	rand.Read(data)
    69  
    70  	builder := NewBuilder(hostOne)
    71  	msg := builder.Receiver(hostOne).Type(TestPacket).Request(&RequestTest{data}).Build()
    72  
    73  	serialized, err := SerializePacket(msg)
    74  	require.NoError(t, err)
    75  
    76  	var buffer bytes.Buffer
    77  	buffer.Write(serialized)
    78  
    79  	deserializedMsg, err := DeserializePacket(&buffer)
    80  	require.NoError(t, err)
    81  
    82  	deserializedData := deserializedMsg.Data.(*RequestTest).Data
    83  	require.Equal(t, data, deserializedData)
    84  }