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

     1  package subproto
     2  
     3  import (
     4  	"github.com/aergoio/aergo/internal/enc"
     5  	"github.com/aergoio/aergo/types"
     6  	"github.com/libp2p/go-libp2p-core/network"
     7  	"testing"
     8  
     9  	"github.com/aergoio/aergo-lib/log"
    10  	"github.com/aergoio/aergo/p2p/p2pcommon"
    11  	"github.com/aergoio/aergo/p2p/p2pmock"
    12  	"github.com/golang/mock/gomock"
    13  )
    14  
    15  func TestPingProtocol_onStatusRequest(t *testing.T) {
    16  	//// TODO this test should be moved to handshake later.
    17  	//t.SkipNow()
    18  	ctrl := gomock.NewController(t)
    19  	defer ctrl.Finish()
    20  
    21  	mockP2PS := p2pmock.NewMockPeerManager(ctrl)
    22  	mockIStream := p2pmock.NewMockStream(ctrl)
    23  	//mockConn := p2pmock.NewMockConn(ctrl)
    24  
    25  	//samplePeerID, _ := types.IDB58Decode("16Uiu2HAkvvhjxVm2WE9yFBDdPQ9qx6pX9taF6TTwDNHs8VPi1EeR")
    26  	// dummyPeer := AergoPeer{}
    27  
    28  	//mockIStream.EXPECT().Conn().Return(mockConn)
    29  	//mockIStream.EXPECT().Protocol().Return(protocol.ID(StatusRequest))
    30  	//	mockIStream.EXPECT().Close().Return(nil)
    31  	//mockConn.EXPECT().("MessageImpl").Return(samplePeerID)
    32  	//mockP2PS.EXPECT().LookupPeer(, samplePeerID).Return(nil, false)
    33  
    34  	type fields struct {
    35  		actor  p2pcommon.ActorService
    36  		ps     p2pcommon.PeerManager
    37  		logger *log.Logger
    38  	}
    39  	type args struct {
    40  		s network.Stream
    41  	}
    42  	tests := []struct {
    43  		name   string
    44  		fields *fields
    45  		args   args
    46  		expect func()
    47  	}{
    48  		{
    49  			"normal",
    50  			&fields{
    51  				actor:  p2pmock.NewMockActorService(ctrl),
    52  				logger: log.NewLogger("test.p2p"),
    53  				ps:     mockP2PS,
    54  			},
    55  			args{s: mockIStream},
    56  			func() {
    57  				// dummy
    58  			},
    59  		},
    60  		// TODO: Add test cases.
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			// 	p := &PingProtocol{
    65  			// 		actorService: tt.fields.actorService,
    66  			// 		ps:        tt.fields.ps,
    67  			// 		log:       tt.fields.logger,
    68  			// 	}
    69  
    70  			tt.expect()
    71  		})
    72  	}
    73  }
    74  
    75  func Test_pingRequestHandler_handle(t *testing.T) {
    76  	ctrl := gomock.NewController(t)
    77  	defer ctrl.Finish()
    78  
    79  	logger := log.NewLogger("test.subproto")
    80  	dummyBlockHash, _ := enc.ToBytes("v6zbuQ4aVSdbTwQhaiZGp5pcL5uL55X3kt2wfxor5W6")
    81  	var dummyPeerID, _ = types.IDB58Decode("16Uiu2HAmN5YU8V2LnTy9neuuJCLNsxLnd5xVSRZqkjvZUHS3mLoD")
    82  
    83  	type args struct {
    84  		hash   []byte
    85  		height uint64
    86  	}
    87  	tests := []struct {
    88  		name string
    89  		args args
    90  
    91  		sendRespCnt int
    92  	}{
    93  		{"TSucc", args{dummyBlockHash, 10}, 1},
    94  		{"TWrongHash", args{[]byte{}, 10}, 0},
    95  	}
    96  	for _, tt := range tests {
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			mockPM := p2pmock.NewMockPeerManager(ctrl)
    99  			mockPeer := p2pmock.NewMockRemotePeer(ctrl)
   100  			mockActor := p2pmock.NewMockActorService(ctrl)
   101  			dummyMF := p2pmock.NewMockMoFactory(ctrl)
   102  			mockPeer.EXPECT().ID().Return(dummyPeerID).AnyTimes()
   103  			mockPeer.EXPECT().Name().Return("16..aadecf@1").AnyTimes()
   104  			mockPeer.EXPECT().MF().Return(dummyMF).MinTimes(tt.sendRespCnt)
   105  			mockPeer.EXPECT().SendMessage(gomock.Any()).Times(tt.sendRespCnt)
   106  			mockPeer.EXPECT().UpdateLastNotice(tt.args.hash, tt.args.height).Times(tt.sendRespCnt)
   107  			mockCA := p2pmock.NewMockChainAccessor(ctrl)
   108  			mockActor.EXPECT().GetChainAccessor().Return(mockCA).MaxTimes(1)
   109  
   110  			reqID := p2pcommon.NewMsgID()
   111  			dummyMF.EXPECT().NewMsgResponseOrder(reqID, p2pcommon.PingResponse, gomock.AssignableToTypeOf(&types.Pong{})).Return(nil).Times(tt.sendRespCnt)
   112  
   113  			msg := p2pmock.NewMockMessage(ctrl)
   114  			msg.EXPECT().ID().Return(reqID).AnyTimes()
   115  			msg.EXPECT().Subprotocol().Return(p2pcommon.PingRequest).AnyTimes()
   116  			body := &types.Ping{BestBlockHash: tt.args.hash, BestHeight: tt.args.height}
   117  
   118  			ph := NewPingReqHandler(mockPM, mockPeer, logger, mockActor)
   119  
   120  			ph.Handle(msg, body)
   121  
   122  		})
   123  	}
   124  }
   125