github.com/braveheart12/insolar-09-08-19@v0.8.7/network/controller/bootstrap/helper_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 bootstrap
    19  
    20  import (
    21  	"crypto"
    22  	"testing"
    23  
    24  	"github.com/insolar/insolar/core"
    25  	"github.com/insolar/insolar/network/nodenetwork"
    26  	"github.com/insolar/insolar/testutils"
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func newTestNode() core.Node {
    32  	return nodenetwork.NewNode(testutils.RandomRef(), core.StaticRoleUnknown, nil, "127.0.0.1:5432", "")
    33  }
    34  
    35  func newTestNodeWithShortID(id core.ShortNodeID) core.Node {
    36  	node := newTestNode()
    37  	node.(nodenetwork.MutableNode).SetShortID(id)
    38  	return node
    39  }
    40  
    41  func TestCorrectShortIDCollision(t *testing.T) {
    42  	keeper := nodenetwork.NewNodeKeeper(newTestNode())
    43  	keeper.AddActiveNodes([]core.Node{
    44  		newTestNodeWithShortID(0),
    45  		newTestNodeWithShortID(1),
    46  		newTestNodeWithShortID(30),
    47  		newTestNodeWithShortID(32),
    48  		newTestNodeWithShortID(33),
    49  		newTestNodeWithShortID(34),
    50  		newTestNodeWithShortID(64),
    51  		newTestNodeWithShortID(1<<32 - 2),
    52  		newTestNodeWithShortID(1<<32 - 1),
    53  	})
    54  
    55  	require.False(t, CheckShortIDCollision(keeper, core.ShortNodeID(2)))
    56  	require.False(t, CheckShortIDCollision(keeper, core.ShortNodeID(31)))
    57  	require.False(t, CheckShortIDCollision(keeper, core.ShortNodeID(35)))
    58  	require.False(t, CheckShortIDCollision(keeper, core.ShortNodeID(65)))
    59  
    60  	require.True(t, CheckShortIDCollision(keeper, core.ShortNodeID(30)))
    61  	require.Equal(t, core.ShortNodeID(31), regenerateShortID(keeper, core.ShortNodeID(30)))
    62  
    63  	require.True(t, CheckShortIDCollision(keeper, core.ShortNodeID(32)))
    64  	require.Equal(t, core.ShortNodeID(35), regenerateShortID(keeper, core.ShortNodeID(32)))
    65  
    66  	require.True(t, CheckShortIDCollision(keeper, core.ShortNodeID(64)))
    67  	require.Equal(t, core.ShortNodeID(65), regenerateShortID(keeper, core.ShortNodeID(64)))
    68  
    69  	require.True(t, CheckShortIDCollision(keeper, core.ShortNodeID(1<<32-2)))
    70  	require.Equal(t, core.ShortNodeID(2), regenerateShortID(keeper, core.ShortNodeID(1<<32-2)))
    71  }
    72  
    73  type testNode struct {
    74  	ref core.RecordRef
    75  }
    76  
    77  func (t *testNode) GetNodeRef() *core.RecordRef {
    78  	return &t.ref
    79  }
    80  
    81  func (t *testNode) GetPublicKey() crypto.PublicKey {
    82  	return nil
    83  }
    84  
    85  func (t *testNode) GetHost() string {
    86  	return ""
    87  }
    88  
    89  func TestRemoveOrigin(t *testing.T) {
    90  	origin := testutils.RandomRef()
    91  	originNode := &testNode{origin}
    92  	first := &testNode{testutils.RandomRef()}
    93  	second := &testNode{testutils.RandomRef()}
    94  
    95  	discoveryNodes := []core.DiscoveryNode{first, originNode, second}
    96  	result := RemoveOrigin(discoveryNodes, origin)
    97  	assert.Equal(t, []core.DiscoveryNode{first, second}, result)
    98  
    99  	discoveryNodes = []core.DiscoveryNode{first, second}
   100  	result = RemoveOrigin(discoveryNodes, origin)
   101  	assert.Equal(t, discoveryNodes, result)
   102  
   103  	discoveryNodes = []core.DiscoveryNode{first, originNode}
   104  	result = RemoveOrigin(discoveryNodes, origin)
   105  	assert.Equal(t, []core.DiscoveryNode{first}, result)
   106  
   107  	discoveryNodes = []core.DiscoveryNode{originNode, first}
   108  	result = RemoveOrigin(discoveryNodes, origin)
   109  	assert.Equal(t, []core.DiscoveryNode{first}, result)
   110  
   111  	discoveryNodes = []core.DiscoveryNode{originNode}
   112  	result = RemoveOrigin(discoveryNodes, origin)
   113  	assert.Empty(t, result)
   114  }