github.com/aergoio/aergo@v1.3.1/p2p/subproto/addrs_test.go (about)

     1  /**
     2   *  @file
     3   *  @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package subproto
     7  
     8  import (
     9  	"fmt"
    10  	"github.com/aergoio/aergo-lib/log"
    11  	"github.com/aergoio/aergo/p2p/p2pmock"
    12  	"github.com/golang/mock/gomock"
    13  	"github.com/libp2p/go-libp2p-core/crypto"
    14  	"testing"
    15  
    16  	"github.com/aergoio/aergo/p2p/p2pcommon"
    17  
    18  	"github.com/aergoio/aergo/types"
    19  	"github.com/golang/protobuf/proto"
    20  )
    21  
    22  var samplePeers []p2pcommon.RemotePeer
    23  
    24  func MakeSenderSlice(slis ...[]p2pcommon.RemotePeer) []p2pcommon.RemotePeer {
    25  	result := make([]p2pcommon.RemotePeer, 0, 10)
    26  	for _, sli := range slis {
    27  		result = append(result, sli...)
    28  	}
    29  	return result
    30  }
    31  
    32  func Test_addressesRequestHandler_handle(t *testing.T) {
    33  	ctrl := gomock.NewController(t)
    34  	defer ctrl.Finish()
    35  
    36  	logger := log.NewLogger("test.subproto")
    37  
    38  	var samplePeers = make([]p2pcommon.RemotePeer, 20)
    39  	for i := 0; i < 20; i++ {
    40  		_, pub, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
    41  		peerid, _ := types.IDFromPublicKey(pub)
    42  		// first 10 are visible, others are hidden
    43  		meta := p2pcommon.PeerMeta{ID: peerid, Hidden: i >= 10}
    44  		samplePeer := p2pmock.NewMockRemotePeer(ctrl)
    45  		samplePeer.EXPECT().ID().Return(meta.ID).AnyTimes()
    46  		samplePeer.EXPECT().Meta().Return(meta).AnyTimes()
    47  		samplePeers[i] = samplePeer
    48  	}
    49  	var dummyPeerID, _ = types.IDB58Decode("16Uiu2HAmN5YU8V2LnTy9neuuJCLNsxLnd5xVSRZqkjvZUHS3mLoD")
    50  	var dummyPeerID2, _ = types.IDB58Decode("16Uiu2HAmFqptXPfcdaCdwipB2fhHATgKGVFVPehDAPZsDKSU7jRm")
    51  
    52  	dummySender := &types.PeerAddress{PeerID: []byte(dummyPeerID), Port: 7845}
    53  	senderPeer := p2pmock.NewMockRemotePeer(ctrl)
    54  	senderPeer.EXPECT().ID().Return(dummyPeerID2).AnyTimes()
    55  	senderPeer.EXPECT().Meta().Return(p2pcommon.PeerMeta{ID:dummyPeerID2}).AnyTimes()
    56  
    57  	tests := []struct {
    58  		name     string
    59  		gotPeers []p2pcommon.RemotePeer
    60  		wantSize int
    61  	}{
    62  		{"TVisible", samplePeers[:10], 10},
    63  		{"THidden", samplePeers[10:], 0},
    64  		{"TMix", samplePeers[5:15], 5},
    65  		// get peers contains sender peer itself. it will be skipped in response
    66  		{"TVisibleWithSender", MakeSenderSlice(samplePeers[:10], []p2pcommon.RemotePeer{senderPeer}), 10},
    67  		{"THiddenWithSender", MakeSenderSlice(samplePeers[10:], []p2pcommon.RemotePeer{senderPeer}), 0},
    68  		{"TMixWithSender", MakeSenderSlice(samplePeers[5:15], []p2pcommon.RemotePeer{senderPeer}), 5},
    69  	}
    70  	for _, tt := range tests {
    71  		t.Run(tt.name, func(t *testing.T) {
    72  			mockPM := p2pmock.NewMockPeerManager(ctrl)
    73  			mockPM.EXPECT().SelfNodeID().Return(dummyPeerID2).AnyTimes()
    74  			mockPM.EXPECT().GetPeers().Return(tt.gotPeers).AnyTimes()
    75  			mockMF := p2pmock.NewMockMoFactory(ctrl)
    76  			mockPeer := p2pmock.NewMockRemotePeer(ctrl)
    77  			mockActor := p2pmock.NewMockActorService(ctrl)
    78  			mockPeer.EXPECT().ID().Return(dummyPeerID2).AnyTimes()
    79  			mockPeer.EXPECT().Name().Return("16..aadecf@1").AnyTimes()
    80  			mockPeer.EXPECT().MF().Return(mockMF).MinTimes(1)
    81  			mockPeer.EXPECT().Meta().Return(p2pcommon.PeerMeta{ID:"16..aadecf@1"}).AnyTimes()
    82  			mockPeer.EXPECT().SendMessage(gomock.Any()).Times(1)
    83  			dummyMo := &testMo{}
    84  			mockMF.EXPECT().NewMsgResponseOrder(gomock.Any(), p2pcommon.AddressesResponse, &addrRespSizeMatcher{tt.wantSize}).Return(dummyMo)
    85  
    86  			ph := NewAddressesReqHandler(mockPM, mockPeer, logger, mockActor)
    87  			dummyMsg :=&testMessage{id:p2pcommon.NewMsgID()}
    88  			msgBody := &types.AddressesRequest{Sender: dummySender, MaxSize: 50}
    89  			ph.Handle(dummyMsg, msgBody)
    90  
    91  		})
    92  	}
    93  }
    94  
    95  type addrRespSizeMatcher struct {
    96  	wantSize int
    97  }
    98  
    99  func (rsm addrRespSizeMatcher) Matches(x interface{}) bool {
   100  	m, ok := x.(*types.AddressesResponse)
   101  	if !ok {
   102  		return false
   103  	}
   104  
   105  	return rsm.wantSize == len(m.Peers)
   106  }
   107  
   108  func (rsm addrRespSizeMatcher) String() string {
   109  	return fmt.Sprintf("len(Peers) = %d",rsm.wantSize)
   110  }
   111  
   112  func Test_addressesResponseHandler_checkAndAddPeerAddresses(t *testing.T) {
   113  	type args struct {
   114  		peers []*types.PeerAddress
   115  	}
   116  	tests := []struct {
   117  		name string
   118  		args args
   119  	}{
   120  		// TODO: Add test cases.
   121  	}
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  		})
   125  	}
   126  }
   127  
   128  func Test_addressesResponseHandler_handle(t *testing.T) {
   129  	type fields struct {
   130  		BaseMsgHandler BaseMsgHandler
   131  	}
   132  	type args struct {
   133  		msg     p2pcommon.Message
   134  		msgBody proto.Message
   135  	}
   136  	tests := []struct {
   137  		name   string
   138  		fields fields
   139  		args   args
   140  	}{
   141  		// TODO: Add test cases.
   142  	}
   143  	for _, tt := range tests {
   144  		t.Run(tt.name, func(t *testing.T) {
   145  			ph := &addressesResponseHandler{
   146  				BaseMsgHandler: tt.fields.BaseMsgHandler,
   147  			}
   148  			ph.Handle(tt.args.msg, tt.args.msgBody)
   149  		})
   150  	}
   151  }