github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/consensus/abci/peers_filter_test.go (about) 1 package abci 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hyperledger/burrow/logging" 8 9 "github.com/hyperledger/burrow/consensus/tendermint/codes" 10 "github.com/stretchr/testify/assert" 11 "github.com/tendermint/tendermint/abci/types" 12 abciTypes "github.com/tendermint/tendermint/abci/types" 13 ) 14 15 const ( 16 aNodeId = "836AB8674A33416718E5A19557A25ED826B2BDD3" 17 aNodeAddress = "127.0.0.1:26656" 18 ) 19 20 func TestApp_QueryAuthorizedPeers(t *testing.T) { 21 var panicked bool 22 app := &App{ 23 panicFunc: func(e error) { 24 panicked = true 25 }, 26 logger: logging.NewNoopLogger(), 27 // Given no authorized node defined 28 authorizedPeers: &PeerLists{}, 29 } 30 31 // When authorized node query is raised with any node id 32 resp := app.Query(*makeTestFilterQuery("id", aNodeId)) 33 34 // Then we should authorized any node 35 assert.NotNil(t, resp) 36 assert.Equal(t, codes.PeerFilterAuthorizedCode, resp.Code) 37 38 // Given authorized nodes defined 39 app.authorizedPeers = &PeerLists{ 40 IDs: map[string]struct{}{aNodeId: {}}, 41 Addresses: map[string]struct{}{aNodeAddress: {}}, 42 } 43 44 // When authorized node query is raised for an authorized node by id 45 resp = app.Query(*makeTestFilterQuery("id", aNodeId)) 46 47 // Then we should authorize it 48 assert.NotNil(t, resp) 49 assert.Equal(t, codes.PeerFilterAuthorizedCode, resp.Code) 50 51 // When authorized node query is raised for another node by id 52 resp = app.Query(*makeTestFilterQuery("id", "forbiddenId")) 53 54 // Then we should forbid this node to sync 55 assert.NotNil(t, resp) 56 assert.Equal(t, codes.PeerFilterForbiddenCode, resp.Code) 57 58 // When authorized node query is raised for an authorized node by address 59 resp = app.Query(*makeTestFilterQuery("addr", aNodeAddress)) 60 61 // Then we should authorize it 62 assert.NotNil(t, resp) 63 assert.Equal(t, codes.PeerFilterAuthorizedCode, resp.Code) 64 65 // When authorized node query is raised for another node 66 resp = app.Query(*makeTestFilterQuery("addr", "forbiddenAddress")) 67 68 // Then we should forbid this node to sync 69 assert.NotNil(t, resp) 70 assert.Equal(t, codes.PeerFilterForbiddenCode, resp.Code) 71 72 // Given a provider which panics 73 assert.False(t, panicked) 74 app.authorizedPeers = new(panicker) 75 76 // When authorized node query is raised 77 resp = app.Query(*makeTestFilterQuery("addr", "hackMe")) 78 79 // The we should recover and mark the query as unsupported, so the node cannot sync 80 assert.True(t, panicked) 81 assert.NotNil(t, resp) 82 assert.Equal(t, codes.UnsupportedRequestCode, resp.Code) 83 } 84 85 type panicker struct { 86 } 87 88 func (p panicker) QueryPeerByID(id string) bool { 89 panic("id") 90 } 91 92 func (p panicker) QueryPeerByAddress(id string) bool { 93 panic("address") 94 } 95 96 func (p panicker) NumPeers() int { 97 panic("peers") 98 } 99 100 func TestIsPeersFilterQuery(t *testing.T) { 101 assert.True(t, isPeersFilterQuery(makeTestFilterQuery("id", aNodeId))) 102 assert.True(t, isPeersFilterQuery(makeTestFilterQuery("addr", aNodeAddress))) 103 assert.False(t, isPeersFilterQuery(makeTestQuery("/another/query"))) 104 } 105 106 func makeTestFilterQuery(filterType string, peer string) *abciTypes.RequestQuery { 107 return makeTestQuery(fmt.Sprintf("%v%v/%v", peersFilterQueryPath, filterType, peer)) 108 } 109 110 func makeTestQuery(path string) *types.RequestQuery { 111 return &abciTypes.RequestQuery{ 112 Path: path, 113 } 114 }