github.com/aergoio/aergo@v1.3.1/polaris/client/polarisconnect_test.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package client 7 8 import ( 9 "errors" 10 "github.com/aergoio/aergo/p2p/p2putil" 11 "github.com/aergoio/aergo/polaris/common" 12 "testing" 13 14 "github.com/aergoio/aergo/config" 15 "github.com/aergoio/aergo/p2p/p2pcommon" 16 "github.com/aergoio/aergo/p2p/p2pmock" 17 "github.com/aergoio/aergo/pkg/component" 18 "github.com/aergoio/aergo/types" 19 "github.com/golang/mock/gomock" 20 ) 21 22 23 type dummyNTC struct { 24 nt p2pcommon.NetworkTransport 25 chainID *types.ChainID 26 selfMeta p2pcommon.PeerMeta 27 } 28 29 func (dntc *dummyNTC) SelfMeta() p2pcommon.PeerMeta { 30 return dntc.selfMeta 31 } 32 33 func (dntc *dummyNTC) GetNetworkTransport() p2pcommon.NetworkTransport { 34 return dntc.nt 35 } 36 func (dntc *dummyNTC) ChainID() *types.ChainID { 37 return dntc.chainID 38 } 39 40 var ( 41 pmapDummyCfg = &config.Config{P2P: &config.P2PConfig{}, Polaris: &config.PolarisConfig{GenesisFile: "../../examples/genesis.json"}} 42 pmapDummyNTC = &dummyNTC{chainID: &types.ChainID{}} 43 ) 44 45 // initSvc select Polarises to connect, or disable polaris 46 func TestPolarisConnectSvc_initSvc(t *testing.T) { 47 polarisIDMain, _ := types.IDB58Decode("16Uiu2HAkuxyDkMTQTGFpmnex2SdfTVzYfPztTyK339rqUdsv3ZUa") 48 polarisIDTest, _ := types.IDB58Decode("16Uiu2HAkvJTHFuJXxr15rFEHsJWnyn1QvGatW2E9ED9Mvy4HWjVF") 49 dummyPeerID2, _ := types.IDB58Decode("16Uiu2HAmFqptXPfcdaCdwipB2fhHATgKGVFVPehDAPZsDKSU7jRm") 50 polar2 := "/ip4/172.21.1.2/tcp/8915/p2p/16Uiu2HAmFqptXPfcdaCdwipB2fhHATgKGVFVPehDAPZsDKSU7jRm" 51 dummyPeerID3, _ := types.IDB58Decode("16Uiu2HAmU8Wc925gZ5QokM4sGDKjysdPwRCQFoYobvoVnyutccCD") 52 polar3 := "/ip4/172.22.2.3/tcp/8915/p2p/16Uiu2HAmU8Wc925gZ5QokM4sGDKjysdPwRCQFoYobvoVnyutccCD" 53 54 customChainID := types.ChainID{Magic: "unittest.blocko.io"} 55 type args struct { 56 use bool 57 polarises []string 58 59 chainID *types.ChainID 60 } 61 tests := []struct { 62 name string 63 args args 64 65 wantCnt int 66 peerIDs []types.PeerID 67 }{ 68 // 69 {"TAergoNoPolaris", args{false, nil, &common.ONEMainNet}, 0, []types.PeerID{}}, 70 {"TAergoMainDefault", args{true, nil, &common.ONEMainNet}, 1, []types.PeerID{polarisIDMain}}, 71 {"TAergoMainPlusCfg", args{true, []string{polar2, polar3}, &common.ONEMainNet}, 3, []types.PeerID{polarisIDMain, dummyPeerID2, dummyPeerID3}}, 72 {"TAergoTestDefault", args{true, nil, &common.ONETestNet}, 1, []types.PeerID{polarisIDTest}}, 73 {"TAergoTestPlusCfg", args{true, []string{polar2, polar3}, &common.ONETestNet}, 3, []types.PeerID{polarisIDTest, dummyPeerID2, dummyPeerID3}}, 74 {"TCustom", args{true, nil, &customChainID}, 0, []types.PeerID{}}, 75 {"TCustomPlusCfg", args{true, []string{polar2, polar3}, &customChainID}, 2, []types.PeerID{dummyPeerID2, dummyPeerID3}}, 76 {"TWrongPolarisAddr", args{true, []string{"/ip4/256.256.1.1/tcp/8915/p2p/16Uiu2HAmU8Wc925gZ5QokM4sGDKjysdPwRCQFoYobvoVnyutccCD"}, &customChainID}, 0, []types.PeerID{}}, 77 {"TWrongPolarisAddr2", args{true, []string{"/egwgew5/p2p/16Uiu2HAmU8Wc925gZ5QokM4sGDKjysdPwRCQFoYobvoVnyutccCD"}, &customChainID}, 0, []types.PeerID{}}, 78 {"TWrongPolarisAddr3", args{true, []string{"/dns/nowhere1234.io/tcp/8915/p2p/16Uiu2HAmU8Wc925gZ5QokM4sGDKjysdPwRCQFoYobvoVnyutccCD"}, &customChainID}, 0, []types.PeerID{}}, 79 } 80 for _, tt := range tests { 81 t.Run(tt.name, func(t *testing.T) { 82 ctrl := gomock.NewController(t) 83 mockNT := p2pmock.NewMockNetworkTransport(ctrl) 84 pmapDummyNTC.nt = mockNT 85 pmapDummyNTC.chainID = tt.args.chainID 86 87 cfg := config.NewServerContext("", "").GetDefaultP2PConfig() 88 cfg.NPUsePolaris = tt.args.use 89 cfg.NPAddPolarises = tt.args.polarises 90 91 pcs := NewPolarisConnectSvc(cfg, pmapDummyNTC) 92 93 if len(pcs.mapServers) != tt.wantCnt { 94 t.Errorf("NewPolarisConnectSvc() = %v, want %v", len(pcs.mapServers), tt.wantCnt) 95 } 96 for _, wantPeerID := range tt.peerIDs { 97 found := false 98 for _, polarisMeta := range pcs.mapServers { 99 if wantPeerID == polarisMeta.ID { 100 found = true 101 break 102 } 103 } 104 if !found { 105 t.Errorf("initSvc() want exist %v but not ", wantPeerID) 106 } 107 } 108 }) 109 } 110 } 111 112 func TestPolarisConnectSvc_BeforeStop(t *testing.T) { 113 114 type fields struct { 115 BaseComponent *component.BaseComponent 116 } 117 tests := []struct { 118 name string 119 fields fields 120 121 calledStreamHandler bool 122 }{ 123 {"TNot", fields{}, false}, 124 // TODO: Add test cases. 125 } 126 for _, tt := range tests { 127 t.Run(tt.name, func(t *testing.T) { 128 ctrl := gomock.NewController(t) 129 130 mockNT := p2pmock.NewMockNetworkTransport(ctrl) 131 pmapDummyNTC.nt = mockNT 132 pms := NewPolarisConnectSvc(pmapDummyCfg.P2P, pmapDummyNTC) 133 134 mockNT.EXPECT().AddStreamHandler(common.PolarisPingSub, gomock.Any()).Times(1) 135 mockNT.EXPECT().RemoveStreamHandler(common.PolarisPingSub).Times(1) 136 137 pms.AfterStart() 138 139 pms.BeforeStop() 140 141 ctrl.Finish() 142 }) 143 } 144 } 145 146 func TestPolarisConnectSvc_queryToPolaris(t *testing.T) { 147 sampleCahinID := &types.ChainID{Consensus:"dpos"} 148 sm := p2pcommon.PeerMeta{ID:p2putil.RandomPeerID()} 149 ss := &types.Status{} 150 151 sErr := errors.New("send erroor") 152 rErr := errors.New("send erroor") 153 154 succR := &types.MapResponse{Status:types.ResultStatus_OK, Addresses:[]*types.PeerAddress{&types.PeerAddress{}}} 155 oldVerR := &types.MapResponse{Status:types.ResultStatus_FAILED_PRECONDITION, Message:common.TooOldVersionMsg} 156 otherR := &types.MapResponse{Status:types.ResultStatus_INVALID_ARGUMENT,Message:"arg is wrong"} 157 158 type args struct { 159 mapServerMeta p2pcommon.PeerMeta 160 peerStatus *types.Status 161 } 162 tests := []struct { 163 name string 164 args args 165 166 sendErr error 167 readErr error 168 mapResp *types.MapResponse 169 170 wantCnt int 171 wantErr bool 172 }{ 173 {"TSingle", args{sm, ss}, nil, nil, succR, 1, false}, 174 {"TSendFail", args{sm, ss}, sErr, nil, succR,0, true}, 175 {"TRecvFail", args{sm, ss}, nil, rErr, succR,0, true}, 176 {"TOldVersion", args{sm, ss}, nil, nil, oldVerR, 0, true}, 177 {"TFailResp", args{sm, ss}, nil, nil, otherR, 0, true}, 178 179 // TODO: Add test cases. 180 } 181 for _, tt := range tests { 182 t.Run(tt.name, func(t *testing.T) { 183 ctrl := gomock.NewController(t) 184 defer ctrl.Finish() 185 186 cfg := config.NewServerContext("", "").GetDefaultP2PConfig() 187 cfg.NPUsePolaris = true 188 189 mockNTC := p2pmock.NewMockNTContainer(ctrl) 190 mockNTC.EXPECT().ChainID().Return(sampleCahinID).AnyTimes() 191 192 mockRW := p2pmock.NewMockMsgReadWriter(ctrl) 193 194 payload, _ := p2putil.MarshalMessageBody(tt.mapResp) 195 dummyData := common.NewPolarisMessage(p2pcommon.NewMsgID(), common.MapResponse, payload) 196 197 mockRW.EXPECT().WriteMsg(gomock.AssignableToTypeOf(&common.PolarisMessage{})).Return(tt.sendErr) 198 if tt.sendErr == nil { 199 mockRW.EXPECT().ReadMsg().Return(dummyData, tt.readErr) 200 } 201 pcs := NewPolarisConnectSvc(cfg, mockNTC) 202 203 204 got, err := pcs.queryToPolaris(tt.args.mapServerMeta, mockRW, tt.args.peerStatus) 205 if (err != nil) != tt.wantErr { 206 t.Errorf("connectAndQuery() error = %v, wantErr %v", err, tt.wantErr) 207 return 208 } 209 if !tt.wantErr && len(got) != tt.wantCnt { 210 t.Errorf("connectAndQuery() len(got) = %v, want %v", len(got), tt.wantCnt) 211 } 212 }) 213 } 214 }