github.com/braveheart12/insolar-09-08-19@v0.8.7/network/transport/host/host_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 host
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/insolar/insolar/core"
    24  	"github.com/insolar/insolar/testutils"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestNewHost(t *testing.T) {
    29  	actualHost, _ := NewHost("127.0.0.1:31337")
    30  	expectedHost, _ := NewHost("127.0.0.1:31337")
    31  
    32  	require.Equal(t, expectedHost, actualHost)
    33  }
    34  
    35  func TestHost_String(t *testing.T) {
    36  	nd, _ := NewHost("127.0.0.1:31337")
    37  	nd.NodeID = testutils.RandomRef()
    38  	string := nd.NodeID.String() + " (" + nd.Address.String() + ")"
    39  
    40  	require.Equal(t, string, nd.String())
    41  }
    42  
    43  func TestHost_Equal(t *testing.T) {
    44  	id1 := testutils.RandomRef()
    45  	id2 := testutils.RandomRef()
    46  	idNil := core.RecordRef{}
    47  	addr1, _ := NewAddress("127.0.0.1:31337")
    48  	addr2, _ := NewAddress("10.10.11.11:12345")
    49  
    50  	tests := []struct {
    51  		id1   core.RecordRef
    52  		addr1 *Address
    53  		id2   core.RecordRef
    54  		addr2 *Address
    55  		equal bool
    56  		name  string
    57  	}{
    58  		{id1, addr1, id1, addr1, true, "same id and address"},
    59  		{id1, addr1, id1, addr2, false, "different addresses"},
    60  		{id1, addr1, id2, addr1, false, "different ids"},
    61  		{id1, addr1, id2, addr2, false, "different id and address"},
    62  		{id1, addr1, id2, addr2, false, "different id and address"},
    63  		{id1, nil, id1, nil, false, "nil addresses"},
    64  		{idNil, addr1, idNil, addr1, true, "nil ids"},
    65  	}
    66  	for _, test := range tests {
    67  		t.Run(test.name, func(t *testing.T) {
    68  			require.Equal(t, test.equal, Host{NodeID: test.id1, Address: test.addr1}.Equal(Host{NodeID: test.id2, Address: test.addr2}))
    69  		})
    70  	}
    71  }