github.com/aergoio/aergo@v1.3.1/polaris/server/listmanager_test.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package server 7 8 import ( 9 "github.com/aergoio/aergo-lib/log" 10 "github.com/aergoio/aergo/config" 11 "github.com/aergoio/aergo/p2p/p2putil" 12 "github.com/aergoio/aergo/types" 13 "github.com/golang/mock/gomock" 14 "os" 15 "path/filepath" 16 "reflect" 17 "testing" 18 ) 19 20 const testAuthDir = "/tmp" 21 22 var sampleEntries []types.WhiteListEntry 23 func init() { 24 eIDIP, _ := types.ParseListEntry(`{"peerid":"16Uiu2HAmPZE7gT1hF2bjpg1UVH65xyNUbBVRf3mBFBJpz3tgLGGt", "address":"172.21.3.35" }`) 25 eIDIR, _ := types.ParseListEntry(`{"peerid":"16Uiu2HAmN5YU8V2LnTy9neuuJCLNsxLnd5xVSRZqkjvZUHS3mLoD", "cidr":"172.21.3.35/16" }`) 26 eID, _ := types.ParseListEntry(`{"peerid":"16Uiu2HAkvvhjxVm2WE9yFBDdPQ9qx6pX9taF6TTwDNHs8VPi1EeR" }`) 27 eIR, _ := types.ParseListEntry(`{"cidr":"211.5.3.123/16" }`) 28 eIP6, _ := types.ParseListEntry(`{"address":"2001:0db8:0123:4567:89ab:cdef:1234:5678" }`) 29 eIR6, _ := types.ParseListEntry(`{"cidr":"2001:0db8:0123:4567:89ab:cdef:1234:5678/96" }`) 30 sampleEntries = []types.WhiteListEntry{eIDIP, eIDIR, eID, eIR, eIP6, eIR6} 31 } 32 func Test_polarisListManager_saveListFile(t *testing.T) { 33 34 logger := log.NewLogger("polaris.test") 35 conf := config.PolarisConfig{EnableBlacklist: true} 36 lm := NewPolarisListManager(&conf, testAuthDir, logger) 37 lm.entries = sampleEntries 38 lm.saveListFile() 39 defer func() { 40 os.Remove(filepath.Join(testAuthDir, localListFile)) 41 }() 42 43 lm2 := NewPolarisListManager(&conf, testAuthDir, logger) 44 lm2.loadListFile() 45 if len(lm2.entries) != len(lm.entries) { 46 t.Errorf("polarisListManager.loadListFile() entry count %v, want %v", len(lm2.entries), len(lm.entries)) 47 } 48 49 for i, e := range lm.entries { 50 e2 := lm.entries[i] 51 52 if !reflect.DeepEqual(e, e2) { 53 t.Errorf("polarisListManager.loadListFile() entry %v, %v", e, e2) 54 } 55 } 56 } 57 58 func Test_polarisListManager_RemoveEntry(t *testing.T) { 59 logger := log.NewLogger("polaris.test") 60 conf := &config.PolarisConfig{EnableBlacklist: true} 61 62 tests := []struct { 63 name string 64 idx int 65 want bool 66 }{ 67 {"TFirst",0, true}, 68 {"TMid",1, true}, 69 {"TLast",5, true}, 70 {"TOverflow",6, false}, 71 {"TNegative",-1, false}, 72 } 73 for _, tt := range tests { 74 t.Run(tt.name, func(t *testing.T) { 75 lm := NewPolarisListManager(conf, "temp", logger) 76 lm.entries = sampleEntries 77 if got := lm.RemoveEntry(tt.idx); got != tt.want { 78 t.Errorf("RemoveEntry() = %v, want %v", got, tt.want) 79 } else if got != (len(lm.entries) < 6) { 80 t.Errorf("RemoveEntry() remain size = %v, want not", len(lm.entries)) 81 } 82 83 }) 84 } 85 } 86 87 func Test_polarisListManager_AddEntry(t *testing.T) { 88 logger := log.NewLogger("polaris.test") 89 conf := &config.PolarisConfig{EnableBlacklist: true} 90 91 tests := []struct { 92 name string 93 args []types.WhiteListEntry 94 wantSize int 95 }{ 96 {"TFirst",sampleEntries[:0], 0}, 97 {"T1",sampleEntries[:1], 1}, 98 {"TAll",sampleEntries, 6}, 99 } 100 for _, tt := range tests { 101 t.Run(tt.name, func(t *testing.T) { 102 lm := NewPolarisListManager(conf, "temp", logger) 103 for _, e := range tt.args { 104 lm.AddEntry(e) 105 } 106 107 if len(lm.ListEntries()) != tt.wantSize { 108 t.Errorf("AddEntry() entries size = %v, want %v", len(lm.ListEntries()), tt.wantSize) 109 } 110 }) 111 } 112 } 113 114 115 116 func Test_polarisListManager_IsBanned(t *testing.T) { 117 conf := config.NewServerContext("", "").GetDefaultPolarisConfig() 118 conf.EnableBlacklist = true 119 logger := log.NewLogger("polaris.test") 120 121 addr1 := "123.45.67.89" 122 id1 := p2putil.RandomPeerID() 123 addrother := "8.8.8.8" 124 idother := p2putil.RandomPeerID() 125 thirdAddr := "222.8.8.8" 126 thirdID := p2putil.RandomPeerID() 127 128 IDOnly, e1 := types.ParseListEntry(`{"peerid":"`+id1.Pretty()+`"}`) 129 AddrOnly, e2 := types.ParseListEntry(`{"address":"`+addr1+`"}`) 130 IDAddr, e3 := types.ParseListEntry(`{"peerid":"`+idother.Pretty()+`", "address":"`+addrother+`"}`) 131 if e1 !=nil || e2 != nil || e3 != nil { 132 t.Fatalf("Inital entry value failure %v , %v , %v",e1,e2,e3) 133 } 134 listCfg := []types.WhiteListEntry{IDOnly, AddrOnly, IDAddr} 135 emptyCfg := []types.WhiteListEntry{} 136 137 138 ctrl := gomock.NewController(t) 139 defer ctrl.Finish() 140 141 type args struct { 142 addr string 143 pid types.PeerID 144 } 145 tests := []struct { 146 name string 147 preset []types.WhiteListEntry 148 args args 149 want bool 150 }{ 151 {"TFoundBoth", listCfg, args{addr1, id1}, true}, 152 {"TIDOnly", listCfg, args{addrother, id1}, true}, 153 {"TIDOnly2", listCfg, args{thirdAddr, id1}, true}, 154 {"TIDOnlyFail", listCfg, args{thirdAddr, idother}, false}, 155 {"TAddrOnly1", listCfg, args{addr1, idother}, true}, 156 {"TAddrOnly2", listCfg, args{addr1, thirdID}, true}, 157 {"TIDAddrSucc", listCfg, args{addrother, idother}, true}, 158 {"TIDAddrFail", listCfg, args{addrother, thirdID}, false}, 159 {"TIDAddrFail2", listCfg, args{thirdAddr, idother}, false}, 160 161 // if config have nothing. everything is allowed 162 {"TEmpFoundBoth", emptyCfg, args{addr1, id1}, false}, 163 {"TEmpIDOnly", emptyCfg, args{addrother, id1}, false}, 164 {"TEmpIDOnly2", emptyCfg, args{thirdAddr, id1}, false}, 165 {"TEmpIDOnly2", emptyCfg, args{thirdAddr, id1}, false}, 166 {"TEmpAddrOnly1", emptyCfg, args{addr1, idother}, false}, 167 {"TEmpAddrOnly2", emptyCfg, args{addr1, thirdID}, false}, 168 {"TEmpIDAddrSucc", emptyCfg, args{addrother, idother}, false}, 169 {"TEmpIDAddrFail", emptyCfg, args{addrother, id1}, false}, 170 {"TEmpIDAddrFail2", emptyCfg, args{thirdAddr, idother}, false}, 171 } 172 173 for _, tt := range tests { 174 t.Run(tt.name, func(t *testing.T) { 175 b := NewPolarisListManager(conf, "/tmp", logger) 176 b.entries = tt.preset 177 178 if got, _ := b.IsBanned(tt.args.addr, tt.args.pid); got != tt.want { 179 t.Errorf("listManagerImpl.IsBanned() = %v, want %v", got, tt.want) 180 } 181 }) 182 } 183 }