github.com/ethersphere/bee/v2@v2.2.0/pkg/p2p/libp2p/internal/blocklist/blocklist_test.go (about) 1 // Copyright 2020 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package blocklist_test 6 7 import ( 8 "testing" 9 "time" 10 11 "github.com/ethersphere/bee/v2/pkg/p2p" 12 "github.com/ethersphere/bee/v2/pkg/p2p/libp2p/internal/blocklist" 13 "github.com/ethersphere/bee/v2/pkg/statestore/mock" 14 "github.com/ethersphere/bee/v2/pkg/swarm" 15 ) 16 17 func TestExist(t *testing.T) { 18 t.Parallel() 19 20 addr1 := swarm.NewAddress([]byte{0, 1, 2, 3}) 21 addr2 := swarm.NewAddress([]byte{4, 5, 6, 7}) 22 ctMock := ¤tTimeMock{} 23 24 bl := blocklist.NewBlocklistWithCurrentTimeFn(mock.NewStateStore(), ctMock.Time) 25 26 exists, err := bl.Exists(addr1) 27 if err != nil { 28 t.Fatal(err) 29 } 30 31 if exists { 32 t.Fatal("got exists, expected not exists") 33 } 34 35 // add forever 36 if err := bl.Add(addr1, 0, "", false); err != nil { 37 t.Fatal(err) 38 } 39 40 // add for 50 milliseconds 41 if err := bl.Add(addr2, time.Millisecond*50, "", false); err != nil { 42 t.Fatal(err) 43 } 44 45 ctMock.SetTime(time.Now().Add(100 * time.Millisecond)) 46 47 exists, err = bl.Exists(addr1) 48 if err != nil { 49 t.Fatal(err) 50 } 51 if !exists { 52 t.Fatal("got not exists, expected exists") 53 } 54 55 exists, err = bl.Exists(addr2) 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 if exists { 61 t.Fatal("got exists, expected not exists") 62 } 63 } 64 65 func TestPeers(t *testing.T) { 66 t.Parallel() 67 68 addr1 := swarm.NewAddress([]byte{0, 1, 2, 3}) 69 addr2 := swarm.NewAddress([]byte{4, 5, 6, 7}) 70 ctMock := ¤tTimeMock{} 71 72 bl := blocklist.NewBlocklistWithCurrentTimeFn(mock.NewStateStore(), ctMock.Time) 73 74 // add forever 75 if err := bl.Add(addr1, 0, "r1", true); err != nil { 76 t.Fatal(err) 77 } 78 79 // add for 50 milliseconds 80 if err := bl.Add(addr2, time.Millisecond*50, "r2", true); err != nil { 81 t.Fatal(err) 82 } 83 84 peers, err := bl.Peers() 85 if err != nil { 86 t.Fatal(err) 87 } 88 if !isIn(addr1, peers, "r1", true) { 89 t.Fatalf("expected addr1 to exist in peers: %v", addr1) 90 } 91 92 if !isIn(addr2, peers, "r2", true) { 93 t.Fatalf("expected addr2 to exist in peers: %v", addr2) 94 } 95 96 ctMock.SetTime(time.Now().Add(100 * time.Millisecond)) 97 98 // now expect just one 99 peers, err = bl.Peers() 100 if err != nil { 101 t.Fatal(err) 102 } 103 if !isIn(addr1, peers, "r1", true) { 104 t.Fatalf("expected addr1 to exist in peers: %v", peers) 105 } 106 107 if isIn(addr2, peers, "r2", true) { 108 t.Fatalf("expected addr2 to not exist in peers: %v", peers) 109 } 110 } 111 112 func isIn(p swarm.Address, peers []p2p.BlockListedPeer, reason string, f bool) bool { 113 for _, v := range peers { 114 if v.Address.Equal(p) && v.Reason == reason && v.Peer.FullNode == f { 115 return true 116 } 117 } 118 return false 119 } 120 121 type currentTimeMock struct { 122 time time.Time 123 } 124 125 func (c *currentTimeMock) Time() time.Time { 126 return c.time 127 } 128 129 func (c *currentTimeMock) SetTime(t time.Time) { 130 c.time = t 131 }