github.com/aergoio/aergo@v1.3.1/p2p/p2p_test.go (about) 1 /** 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package p2p 7 8 import ( 9 "github.com/aergoio/aergo/p2p/list" 10 "github.com/aergoio/aergo/p2p/p2putil" 11 "reflect" 12 "testing" 13 14 "github.com/aergoio/aergo-lib/log" 15 "github.com/aergoio/aergo/message" 16 "github.com/aergoio/aergo/p2p/p2pcommon" 17 "github.com/aergoio/aergo/p2p/p2pmock" 18 "github.com/aergoio/aergo/pkg/component" 19 "github.com/aergoio/aergo/types" 20 "github.com/golang/mock/gomock" 21 ) 22 23 func TestP2P_CreateHSHandler(t *testing.T) { 24 ctrl := gomock.NewController(t) 25 defer ctrl.Finish() 26 27 type args struct { 28 legacy bool 29 outbound bool 30 } 31 tests := []struct { 32 name string 33 34 args args 35 wantType reflect.Type 36 }{ 37 {"TNewIn", args{false, false}, reflect.TypeOf(&InboundWireHandshaker{})}, 38 {"TNewOut", args{false, true}, reflect.TypeOf(&OutboundWireHandshaker{})}, 39 {"TLegacyIn", args{true, false}, reflect.TypeOf(&LegacyInboundHSHandler{})}, 40 {"TLegacyOut", args{true, true}, reflect.TypeOf(&LegacyOutboundHSHandler{})}, 41 // TODO: Add test cases. 42 } 43 for _, tt := range tests { 44 t.Run(tt.name, func(t *testing.T) { 45 mockPM := p2pmock.NewMockPeerManager(ctrl) 46 sampleChainID := types.ChainID{} 47 //mockMF := p2pmock.NewMockMoFactory(ctrl) 48 //mockPeer := (*p2pmock.MockRemotePeer)(nil) 49 //if tt.hasPeer { 50 // mockPeer = p2pmock.NewMockRemotePeer(ctrl) 51 // mockPeer.EXPECT().SendMessage(gomock.Any()).Times(1) 52 //} 53 //p2pmock.NewMockRemotePeer(ctrl) 54 //mockPM.EXPECT().GetPeer(dummyPeerID).Return(mockPeer, tt.hasPeer).Times(1) 55 //mockPM.EXPECT().SelfMeta().Return(dummyPeerMeta).Times(tt.wantSend).MaxTimes(tt.wantSend) 56 //mockMF.EXPECT().NewMsgRequestOrder(true, subproto.AddressesRequest, gomock.AssignableToTypeOf(&types.AddressesRequest{})).Times(tt.wantSend) 57 58 p2ps := &P2P{ 59 pm: mockPM, chainID: &sampleChainID, 60 } 61 p2ps.BaseComponent = component.NewBaseComponent(message.P2PSvc, p2ps, log.NewLogger("p2p.test")) 62 63 got := p2ps.CreateHSHandler(tt.args.legacy, tt.args.outbound, dummyPeerID) 64 if !reflect.TypeOf(got).AssignableTo(tt.wantType) { 65 t.Errorf("P2P.CreateHSHandler() type = %v, want %v", reflect.TypeOf(got), tt.wantType) 66 } 67 }) 68 } 69 } 70 71 func TestP2P_InsertHandlers(t *testing.T) { 72 tests := []struct { 73 name string 74 }{ 75 {"T1"}, 76 } 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 ctrl := gomock.NewController(t) 80 defer ctrl.Finish() 81 82 mockPM := p2pmock.NewMockPeerManager(ctrl) 83 mockPeer := p2pmock.NewMockRemotePeer(ctrl) 84 mockPeer.EXPECT().AddMessageHandler(gomock.AssignableToTypeOf(p2pcommon.PingResponse), gomock.Any()).MinTimes(1) 85 86 p2ps := &P2P{ 87 pm: mockPM, 88 } 89 p2ps.BaseComponent = component.NewBaseComponent(message.P2PSvc, p2ps, log.NewLogger("p2p.test")) 90 91 p2ps.insertHandlers(mockPeer) 92 }) 93 } 94 } 95 96 func TestP2P_banIfFound(t *testing.T) { 97 sampleCnt := 5 98 addr := "172.21.11.3" 99 pids := make([]types.PeerID,sampleCnt) 100 for i:=0; i<sampleCnt; i++ { 101 pids[i] = p2putil.RandomPeerID() 102 } 103 tests := []struct { 104 name string 105 106 inWhite []int 107 wantStopCnt int 108 }{ 109 {"TAllWhite", []int{1,1,1,1,1},0 }, 110 {"TAllBan", []int{0,0,0,0,0},5 }, 111 {"TMix", []int{0,1,1,0,1},2 }, 112 } 113 for _, tt := range tests { 114 t.Run(tt.name, func(t *testing.T) { 115 ctrl := gomock.NewController(t) 116 defer ctrl.Finish() 117 118 mockPM := p2pmock.NewMockPeerManager(ctrl) 119 mockLM := p2pmock.NewMockListManager(ctrl) 120 peers := make([]p2pcommon.RemotePeer,sampleCnt) 121 for i:=0; i<sampleCnt; i++ { 122 mPeer := p2pmock.NewMockRemotePeer(ctrl) 123 mPeer.EXPECT().ID().Return(pids[i]) 124 mPeer.EXPECT().Meta().Return(p2pcommon.PeerMeta{IPAddress:addr, Outbound:false}).MinTimes(1) 125 mPeer.EXPECT().Name().Return("peer "+pids[i].ShortString()).AnyTimes() 126 if tt.inWhite[i] == 0 { 127 mPeer.EXPECT().Stop() 128 } 129 130 peers[i] = mPeer 131 mockLM.EXPECT().IsBanned(addr, pids[i]).Return(tt.inWhite[i]==0, list.FarawayFuture) 132 } 133 mockPM.EXPECT().GetPeers().Return(peers) 134 p2ps := &P2P{ 135 pm: mockPM, 136 lm: mockLM, 137 } 138 p2ps.BaseComponent = component.NewBaseComponent(message.P2PSvc, p2ps, log.NewLogger("p2p")) 139 140 p2ps.checkAndBanInboundPeers() 141 }) 142 } 143 }