github.com/braveheart12/insolar-09-08-19@v0.8.7/network/transport/transport_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 transport
    19  
    20  import (
    21  	"context"
    22  	"crypto/rand"
    23  	"encoding/gob"
    24  	"testing"
    25  
    26  	"github.com/insolar/insolar/configuration"
    27  	"github.com/insolar/insolar/network/transport/host"
    28  	"github.com/insolar/insolar/network/transport/packet"
    29  	"github.com/insolar/insolar/network/transport/packet/types"
    30  	"github.com/insolar/insolar/network/transport/relay"
    31  	"github.com/insolar/insolar/network/transport/resolver"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  	"github.com/stretchr/testify/suite"
    35  )
    36  
    37  type node struct {
    38  	config    configuration.Transport
    39  	transport Transport
    40  	host      *host.Host
    41  }
    42  
    43  type transportSuite struct {
    44  	suite.Suite
    45  	node1 node
    46  	node2 node
    47  }
    48  
    49  func NewSuite(cfg1 configuration.Transport, cfg2 configuration.Transport) *transportSuite {
    50  	return &transportSuite{
    51  		Suite: suite.Suite{},
    52  		node1: node{config: cfg1},
    53  		node2: node{config: cfg2},
    54  	}
    55  }
    56  
    57  func setupNode(t *transportSuite, n *node) {
    58  	var err error
    59  	n.host, err = host.NewHost(n.config.Address)
    60  	t.Assert().NoError(err)
    61  
    62  	n.transport, err = NewTransport(n.config, relay.NewProxy())
    63  	t.Require().NoError(err)
    64  	t.Require().NotNil(n.transport)
    65  	t.Require().Implements((*Transport)(nil), n.transport)
    66  }
    67  
    68  func (t *transportSuite) SetupTest() {
    69  	gob.Register(&packet.RequestTest{})
    70  	setupNode(t, &t.node1)
    71  	setupNode(t, &t.node2)
    72  }
    73  
    74  func (t *transportSuite) BeforeTest(suiteName, testName string) {
    75  	ctx := context.Background()
    76  	t.node1.transport.Listen(ctx)
    77  	t.node2.transport.Listen(ctx)
    78  }
    79  
    80  func (t *transportSuite) AfterTest(suiteName, testName string) {
    81  	go t.node1.transport.Stop()
    82  	<-t.node1.transport.Stopped()
    83  	t.node1.transport.Close()
    84  
    85  	go t.node2.transport.Stop()
    86  	<-t.node2.transport.Stopped()
    87  	t.node2.transport.Close()
    88  }
    89  
    90  func generateRandomBytes(n int) ([]byte, error) {
    91  	b := make([]byte, n)
    92  	_, err := rand.Read(b)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	return b, nil
    97  }
    98  
    99  func (t *transportSuite) TestPingPong() {
   100  	if t.node1.config.Protocol == "PURE_UDP" {
   101  		t.T().Skip("Skipping TestPingPong for PURE_UDP")
   102  	}
   103  	ctx := context.Background()
   104  	p := packet.NewBuilder(t.node1.host).Type(types.Ping).Receiver(t.node2.host).Build()
   105  	future, err := t.node1.transport.SendRequest(ctx, p)
   106  	t.Assert().NoError(err)
   107  
   108  	requestMsg := <-t.node2.transport.Packets()
   109  	t.Assert().Equal(types.Ping, requestMsg.Type)
   110  	t.Assert().Equal(t.node2.host, future.Actor())
   111  	t.Assert().False(requestMsg.IsResponse)
   112  
   113  	builder := packet.NewBuilder(t.node2.host).Receiver(requestMsg.Sender).Type(types.Ping)
   114  	err = t.node2.transport.SendResponse(ctx, requestMsg.RequestID, builder.Response(nil).Build())
   115  	t.Assert().NoError(err)
   116  
   117  	responseMsg := <-future.Result()
   118  	t.Assert().Equal(types.Ping, responseMsg.Type)
   119  	t.Assert().True(responseMsg.IsResponse)
   120  }
   121  
   122  func (t *transportSuite) TestSendBigPacket() {
   123  	if testing.Short() {
   124  		t.T().Skip("Skipping TestSendBigPacket in short mode")
   125  	}
   126  	if t.node1.config.Protocol == "PURE_UDP" {
   127  		t.T().Skip("Skipping TestSendBigPacket for PURE_UDP")
   128  	}
   129  	ctx := context.Background()
   130  	data, _ := generateRandomBytes(1024 * 1024 * 2)
   131  	builder := packet.NewBuilder(t.node1.host).Receiver(t.node2.host).Type(packet.TestPacket)
   132  	requestMsg := builder.Request(&packet.RequestTest{Data: data}).Build()
   133  
   134  	_, err := t.node1.transport.SendRequest(ctx, requestMsg)
   135  	t.Assert().NoError(err)
   136  
   137  	msg := <-t.node2.transport.Packets()
   138  	t.Assert().Equal(packet.TestPacket, msg.Type)
   139  	receivedData := msg.Data.(*packet.RequestTest).Data
   140  	t.Assert().Equal(data, receivedData)
   141  }
   142  
   143  // func (t *consensusSuite) TestSendPacketConsensus() {
   144  // 	t.T().Skip("fix tests for consensus udp transport")
   145  // 	ctx := context.Background()
   146  // 	builder := packet.NewBuilder(t.node1.host).Receiver(t.node2.host).Type(types.Phase1)
   147  // 	requestMsg := builder.Request(consensus.NewPhase1Packet()).Build()
   148  // 	_, err := t.node1.transport.SendRequest(ctx, requestMsg)
   149  // 	t.Assert().NoError(err)
   150  //
   151  // 	<-t.node2.transport.Packets()
   152  // }
   153  
   154  // func TestUDPTransport(t *testing.T) {
   155  // 	cfg1 := configuration.Transport{Protocol: "PURE_UDP", Address: "127.0.0.1:17014", BehindNAT: false}
   156  // 	cfg2 := configuration.Transport{Protocol: "PURE_UDP", Address: "127.0.0.1:17015", BehindNAT: false}
   157  //
   158  // 	suite.Run(t, NewConsensusSuite(cfg1, cfg2))
   159  // }
   160  
   161  func TestTCPTransport(t *testing.T) {
   162  	cfg1 := configuration.Transport{Protocol: "TCP", Address: "127.0.0.1:17016", BehindNAT: false}
   163  	cfg2 := configuration.Transport{Protocol: "TCP", Address: "127.0.0.1:17017", BehindNAT: false}
   164  
   165  	suite.Run(t, NewSuite(cfg1, cfg2))
   166  }
   167  
   168  func TestQuicTransport(t *testing.T) {
   169  	t.Skip("QUIC internals racing atm. Skip until we want to use it in production")
   170  
   171  	cfg1 := configuration.Transport{Protocol: "QUIC", Address: "127.0.0.1:17018", BehindNAT: false}
   172  	cfg2 := configuration.Transport{Protocol: "QUIC", Address: "127.0.0.1:17019", BehindNAT: false}
   173  
   174  	suite.Run(t, NewSuite(cfg1, cfg2))
   175  }
   176  
   177  func Test_createResolver(t *testing.T) {
   178  	a := assert.New(t)
   179  
   180  	cfg1 := configuration.Transport{Protocol: "TCP", Address: "127.0.0.1:17018", BehindNAT: false, FixedPublicAddress: "192.168.0.1"}
   181  	r, err := createResolver(cfg1)
   182  	a.NoError(err)
   183  	a.IsType(resolver.NewFixedAddressResolver(""), r)
   184  
   185  	cfg3 := configuration.Transport{Protocol: "TCP", Address: "127.0.0.1:17018", BehindNAT: false, FixedPublicAddress: ""}
   186  	r, err = createResolver(cfg3)
   187  	a.NoError(err)
   188  	a.IsType(resolver.NewExactResolver(), r)
   189  }