github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/translator/hierarchical_translator_test.go (about)

     1  package translator_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/libp2p/go-libp2p/core/peer"
     7  	"github.com/stretchr/testify/require"
     8  	"github.com/stretchr/testify/suite"
     9  	"gotest.tools/assert"
    10  
    11  	"github.com/onflow/flow-go/network/p2p/translator"
    12  
    13  	"github.com/onflow/flow-go/model/flow"
    14  	"github.com/onflow/flow-go/network/internal/p2pfixtures"
    15  	"github.com/onflow/flow-go/network/p2p/keyutils"
    16  	"github.com/onflow/flow-go/utils/unittest"
    17  )
    18  
    19  type HierarchicalTranslatorTestSuite struct {
    20  	suite.Suite
    21  	translator *translator.HierarchicalIDTranslator
    22  	ids        flow.IdentityList
    23  }
    24  
    25  func (suite *HierarchicalTranslatorTestSuite) SetupTest() {
    26  	suite.ids = unittest.IdentityListFixture(2, unittest.WithKeys)
    27  	t1, err := translator.NewFixedTableIdentityTranslator(suite.ids[:1])
    28  	require.NoError(suite.T(), err)
    29  	t2, err := translator.NewFixedTableIdentityTranslator(suite.ids[1:])
    30  	require.NoError(suite.T(), err)
    31  
    32  	suite.translator = translator.NewHierarchicalIDTranslator(t1, t2)
    33  }
    34  
    35  func TestHierarchicalTranslator(t *testing.T) {
    36  	suite.Run(t, new(HierarchicalTranslatorTestSuite))
    37  }
    38  
    39  func (suite *HierarchicalTranslatorTestSuite) TestFirstTranslatorSuccess() {
    40  	pid, err := suite.translator.GetPeerID(suite.ids[0].NodeID)
    41  	require.NoError(suite.T(), err)
    42  	fid, err := suite.translator.GetFlowID(pid)
    43  	require.NoError(suite.T(), err)
    44  	assert.Equal(suite.T(), fid, suite.ids[0].NodeID)
    45  }
    46  
    47  func (suite *HierarchicalTranslatorTestSuite) TestSecondTranslatorSuccess() {
    48  	pid, err := suite.translator.GetPeerID(suite.ids[1].NodeID)
    49  	require.NoError(suite.T(), err)
    50  	fid, err := suite.translator.GetFlowID(pid)
    51  	require.NoError(suite.T(), err)
    52  	assert.Equal(suite.T(), fid, suite.ids[1].NodeID)
    53  }
    54  
    55  func (suite *HierarchicalTranslatorTestSuite) TestTranslationFailure() {
    56  	fid := unittest.IdentifierFixture()
    57  	_, err := suite.translator.GetPeerID(fid)
    58  	require.Error(suite.T(), err)
    59  
    60  	key, err := keyutils.LibP2PPrivKeyFromFlow(p2pfixtures.NetworkingKeyFixtures(suite.T()))
    61  	require.NoError(suite.T(), err)
    62  	pid, err := peer.IDFromPrivateKey(key)
    63  	require.NoError(suite.T(), err)
    64  	_, err = suite.translator.GetFlowID(pid)
    65  	require.Error(suite.T(), err)
    66  }