github.com/aergoio/aergo@v1.3.1/p2p/list/listmanager_test.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package list 7 8 import ( 9 "github.com/aergoio/aergo-lib/log" 10 "github.com/aergoio/aergo/config" 11 "github.com/aergoio/aergo/contract/enterprise" 12 "github.com/aergoio/aergo/p2p/p2pcommon" 13 "github.com/aergoio/aergo/p2p/p2pmock" 14 "github.com/aergoio/aergo/p2p/p2putil" 15 "github.com/aergoio/aergo/types" 16 "github.com/golang/mock/gomock" 17 "testing" 18 ) 19 20 func TestListManagerImpl_Start(t *testing.T) { 21 conf := config.NewServerContext("", "").GetDefaultAuthConfig() 22 logger := log.NewLogger("p2p.list.test") 23 24 ctrl := gomock.NewController(t) 25 defer ctrl.Finish() 26 27 IDOnly := `{"peerid":"16Uiu2HAmPZE7gT1hF2bjpg1UVH65xyNUbBVRf3mBFBJpz3tgLGGt"}` 28 AddrOnly := `{"address":"122.1.3.4"}` 29 AddrRange := `{"peerid":"", "cidr":"122.1.3.4/24"}` 30 IDAddr := `{"peerid":"16Uiu2HAmPZE7gT1hF2bjpg1UVH65xyNUbBVRf3mBFBJpz3tgLGGt", "address":"122.1.3.4"}` 31 32 tests := []struct { 33 name string 34 35 confs []string 36 wantPanic bool 37 }{ 38 {"TEmpty", nil, false}, 39 {"TSingle", []string{IDOnly}, false}, 40 {"TMulti", []string{IDOnly, AddrOnly, IDAddr}, false}, 41 {"TMulti", []string{IDOnly, AddrOnly, IDAddr, AddrRange}, false}, 42 {"TWrong", []string{IDOnly, ":e23dgvsdvz.32@", IDAddr}, true}, 43 {"TWrong2", []string{IDOnly, "e23dgvsd!v32@:", IDAddr}, true}, 44 // TODO: Add test cases. 45 } 46 for _, tt := range tests { 47 t.Run(tt.name, func(t *testing.T) { 48 ecfg := &types.EnterpriseConfig{Key: enterprise.P2PWhite, On: true, Values: tt.confs} 49 mockCA := p2pmock.NewMockChainAccessor(ctrl) 50 mockCA.EXPECT().GetEnterpriseConfig(enterprise.P2PWhite).Return(ecfg, nil) 51 got := NewListManager(conf, "", mockCA, nil, logger, false).(*listManagerImpl) 52 func() { 53 defer checkPanic(t, tt.wantPanic) 54 got.Start() 55 }() 56 if tt.wantPanic { 57 return 58 } 59 if got.entries == nil { 60 t.Errorf("NewListManager() fields not initialized %v", "addrMap") 61 } 62 wantSize := len(tt.confs) 63 if len(got.entries) != wantSize { 64 t.Errorf("NewListManager() len(ListManager.entries) = %v, want %v", len(got.entries), wantSize) 65 } 66 }) 67 } 68 } 69 70 func checkPanic(t *testing.T, wantPanic bool) { 71 if r := recover(); (r != nil) != wantPanic { 72 t.Errorf("panic of NewListManager() %v, want %v", r != nil, wantPanic) 73 } 74 } 75 76 func Test_blacklistManagerImpl_IsBanned(t *testing.T) { 77 conf := config.NewServerContext("", "").GetDefaultAuthConfig() 78 addr1 := "123.45.67.89" 79 id1 := p2putil.RandomPeerID() 80 addrother := "8.8.8.8" 81 idother := p2putil.RandomPeerID() 82 thirdAddr := "222.8.8.8" 83 thirdID := p2putil.RandomPeerID() 84 85 IDOnly := `{"peerid":"`+id1.Pretty()+`"}` 86 AddrOnly := `{"address":"`+addr1+`"}` 87 IDAddr := `{"peerid":"`+idother.Pretty()+`", "address":"`+addrother+`"}` 88 89 logger := log.NewLogger("p2p.list.test") 90 listCfg := &types.EnterpriseConfig{Key: enterprise.P2PWhite, On: true, Values: []string{IDOnly, AddrOnly, IDAddr}} 91 emptyCfg := &types.EnterpriseConfig{Key: enterprise.P2PWhite, On: true, Values: nil} 92 93 ctrl := gomock.NewController(t) 94 defer ctrl.Finish() 95 96 type args struct { 97 addr string 98 pid types.PeerID 99 } 100 tests := []struct { 101 name string 102 cfg *types.EnterpriseConfig 103 args args 104 want bool 105 }{ 106 {"TFoundBoth", listCfg, args{addr1, id1}, false}, 107 {"TIDOnly", listCfg, args{addrother, id1}, false}, 108 {"TIDOnly2", listCfg, args{thirdAddr, id1}, false}, 109 {"TIDOnlyFail", listCfg, args{thirdAddr, idother}, true}, 110 {"TAddrOnly1", listCfg, args{addr1, idother}, false}, 111 {"TAddrOnly2", listCfg, args{addr1, thirdID}, false}, 112 {"TIDAddrSucc", listCfg, args{addrother, idother}, false}, 113 {"TIDAddrFail", listCfg, args{addrother, thirdID}, true}, 114 {"TIDAddrFail2", listCfg, args{thirdAddr, idother}, true}, 115 116 // if config have nothing. everything is allowed 117 {"TEmpFoundBoth", emptyCfg, args{addr1, id1}, false}, 118 {"TEmpIDOnly", emptyCfg, args{addrother, id1}, false}, 119 {"TEmpIDOnly2", emptyCfg, args{thirdAddr, id1}, false}, 120 {"TEmpIDOnly2", emptyCfg, args{thirdAddr, id1}, false}, 121 {"TEmpAddrOnly1", emptyCfg, args{addr1, idother}, false}, 122 {"TEmpAddrOnly2", emptyCfg, args{addr1, thirdID}, false}, 123 {"TEmpIDAddrSucc", emptyCfg, args{addrother, idother}, false}, 124 {"TEmpIDAddrFail", emptyCfg, args{addrother, id1}, false}, 125 {"TEmpIDAddrFail2", emptyCfg, args{thirdAddr, idother}, false}, 126 } 127 128 for _, tt := range tests { 129 t.Run(tt.name, func(t *testing.T) { 130 mockCA := p2pmock.NewMockChainAccessor(ctrl) 131 mockCA.EXPECT().GetEnterpriseConfig(enterprise.P2PWhite).Return(tt.cfg, nil) 132 mockPRM := p2pmock.NewMockPeerRoleManager(ctrl) 133 mockPRM.EXPECT().GetRole(gomock.Any()).Return(p2pcommon.Watcher).AnyTimes() 134 135 b := NewListManager(conf, "", mockCA, mockPRM, logger, false).(*listManagerImpl) 136 b.Start() 137 if got, _ := b.IsBanned(tt.args.addr, tt.args.pid); got != tt.want { 138 t.Errorf("listManagerImpl.IsBanned() = %v, want %v", got, tt.want) 139 } 140 }) 141 } 142 } 143 144 func Test_blacklistManagerImpl_IsBanned2(t *testing.T) { 145 conf := config.NewServerContext("", "").GetDefaultAuthConfig() 146 ent := []string{ 147 `{"address":"192.168.1.14"}`, 148 `{"peerid":"16Uiu2HAkvbHmK1Ke1hqAHmahwTGE4ndkdMdXJeXFE3kgBs17k2oQ"}`, 149 `{"peerid":"16Uiu2HAmNxKsrFQ4Wez4DYHW6o72y2Jpy6RMv5TuqAvjcQ5QPZWw", "address":"192.168.1.13"}`, 150 `{"peerid":"16Uiu2HAmDFV41vku39rsMtXBaFT1MFUDyHxXiDJrUDt7gJycSKnX", "address":"192.168.1.12"}`, 151 } 152 153 addr1 := "192.168.1.13" 154 addr2 := "192.168.1.12" 155 id1, _ := types.IDB58Decode("16Uiu2HAmQn3nFBGhJM7TnZRguLhgUx1HnpNL2easdt2JrxdbFjtb") 156 id2, _ := types.IDB58Decode("16Uiu2HAmAnQ5jjk7huhepfFtDFFCreuJ21nHYBApVpg8G7EBdwme") 157 id3, _ := types.IDB58Decode("16Uiu2HAkvbHmK1Ke1hqAHmahwTGE4ndkdMdXJeXFE3kgBs17k2oQ") 158 id4, _ := types.IDB58Decode("16Uiu2HAkw9ZZ61iq8uWbrQrmNEXFbrbkWupdqiHSKkCuCFLTM6gF") 159 id5, _ := types.IDB58Decode("16Uiu2HAmUkoPDPHrYYC8J4sVvaVRho8UxfWPLDgZS8gu5bsGSRSA") 160 id6, _ := types.IDB58Decode("16Uiu2HAmNxKsrFQ4Wez4DYHW6o72y2Jpy6RMv5TuqAvjcQ5QPZWw") 161 id7, _ := types.IDB58Decode("16Uiu2HAmDFV41vku39rsMtXBaFT1MFUDyHxXiDJrUDt7gJycSKnX") 162 163 logger := log.NewLogger("p2p.list.test") 164 listCfg := &types.EnterpriseConfig{Key: enterprise.P2PWhite, On: true, Values: ent} 165 disabledCfg := &types.EnterpriseConfig{Key: enterprise.P2PWhite, On: false, Values: ent} 166 167 ctrl := gomock.NewController(t) 168 defer ctrl.Finish() 169 170 type args struct { 171 addr string 172 pid types.PeerID 173 } 174 tests := []struct { 175 name string 176 cfg *types.EnterpriseConfig 177 role p2pcommon.PeerRole 178 179 args args 180 want bool 181 }{ 182 {"T1", listCfg, p2pcommon.Watcher, args{addr1, id1}, true}, 183 {"T2", listCfg, p2pcommon.Watcher, args{addr1, id2}, true}, 184 {"T3", listCfg, p2pcommon.Watcher, args{addr1, id3}, false}, 185 {"T4", listCfg, p2pcommon.Watcher, args{addr2, id4}, true}, 186 {"T5", listCfg, p2pcommon.Watcher, args{addr2, id5}, true}, 187 {"T6", listCfg, p2pcommon.Watcher, args{addr2, id6}, true}, 188 {"T7", listCfg, p2pcommon.Watcher, args{addr2, id7}, false}, 189 190 // bp is always allowed 191 {"T1", listCfg, p2pcommon.BlockProducer, args{addr1, id1}, false}, 192 {"T2", listCfg, p2pcommon.BlockProducer, args{addr1, id2}, false}, 193 {"T3", listCfg, p2pcommon.BlockProducer, args{addr1, id3}, false}, 194 {"T4", listCfg, p2pcommon.BlockProducer, args{addr2, id4}, false}, 195 {"T5", listCfg, p2pcommon.BlockProducer, args{addr2, id5}, false}, 196 {"T6", listCfg, p2pcommon.BlockProducer, args{addr2, id6}, false}, 197 {"T7", listCfg, p2pcommon.BlockProducer, args{addr2, id7}, false}, 198 199 // disabling conf will allow all connection 200 {"TDis1", disabledCfg, p2pcommon.Watcher, args{addr1, id1}, false}, 201 {"TDis2", disabledCfg, p2pcommon.Watcher, args{addr1, id2}, false}, 202 {"TDis3", disabledCfg, p2pcommon.Watcher, args{addr1, id3}, false}, 203 {"TDis4", disabledCfg, p2pcommon.Watcher, args{addr2, id4}, false}, 204 {"TDis5", disabledCfg, p2pcommon.Watcher, args{addr2, id5}, false}, 205 {"TDis6", disabledCfg, p2pcommon.Watcher, args{addr2, id6}, false}, 206 {"TDis7", disabledCfg, p2pcommon.Watcher, args{addr2, id7}, false}, 207 } 208 209 for _, tt := range tests { 210 t.Run(tt.name, func(t *testing.T) { 211 mockCA := p2pmock.NewMockChainAccessor(ctrl) 212 mockCA.EXPECT().GetEnterpriseConfig(enterprise.P2PWhite).Return(tt.cfg, nil) 213 mockPRM := p2pmock.NewMockPeerRoleManager(ctrl) 214 mockPRM.EXPECT().GetRole(gomock.Any()).Return(tt.role).AnyTimes() 215 216 b := NewListManager(conf, "", mockCA, mockPRM, logger, false).(*listManagerImpl) 217 b.Start() 218 if got, _ := b.IsBanned(tt.args.addr, tt.args.pid); got != tt.want { 219 t.Errorf("listManagerImpl.IsBanned() = %v, want %v", got, tt.want) 220 } 221 }) 222 } 223 }