mirror of
https://github.com/eosswedenorg/thalos
synced 2026-07-03 11:53:41 +02:00
Adding internal/types/blacklist.go
This commit is contained in:
parent
a79bf8f2f3
commit
93a816cb2c
2 changed files with 64 additions and 0 deletions
21
internal/types/blacklist.go
Normal file
21
internal/types/blacklist.go
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
package types
|
||||||
|
|
||||||
|
type Blacklist map[string][]string
|
||||||
|
|
||||||
|
func (bl Blacklist) Add(contract string, action string) {
|
||||||
|
if len(bl[contract]) < 1 {
|
||||||
|
bl[contract] = []string{}
|
||||||
|
}
|
||||||
|
bl[contract] = append(bl[contract], action)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bl Blacklist) Lookup(contract string, action string) bool {
|
||||||
|
if v, ok := bl[contract]; ok {
|
||||||
|
for _, act := range v {
|
||||||
|
if act == action || act == "*" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
43
internal/types/blacklist_test.go
Normal file
43
internal/types/blacklist_test.go
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBlacklist_Add(t *testing.T) {
|
||||||
|
bl := Blacklist{}
|
||||||
|
bl.Add("contract", "action1")
|
||||||
|
bl.Add("contract", "action2")
|
||||||
|
bl.Add("contract2", "action1")
|
||||||
|
|
||||||
|
expected := Blacklist{
|
||||||
|
"contract": {"action1", "action2"},
|
||||||
|
"contract2": {"action1"},
|
||||||
|
}
|
||||||
|
|
||||||
|
require.Equal(t, expected, bl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBlacklist_Lookup(t *testing.T) {
|
||||||
|
bl := Blacklist{
|
||||||
|
"mycontract": {"myaction", "noop"},
|
||||||
|
}
|
||||||
|
|
||||||
|
require.True(t, bl.Lookup("mycontract", "myaction"))
|
||||||
|
require.True(t, bl.Lookup("mycontract", "noop"))
|
||||||
|
require.False(t, bl.Lookup("mycontract", "xxx"))
|
||||||
|
require.False(t, bl.Lookup("xxx", "yyy"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBlacklist_LookupWildcard(t *testing.T) {
|
||||||
|
bl := Blacklist{
|
||||||
|
"mycontract": {"*"},
|
||||||
|
}
|
||||||
|
|
||||||
|
require.True(t, bl.Lookup("mycontract", "myaction"))
|
||||||
|
require.True(t, bl.Lookup("mycontract", "noop"))
|
||||||
|
require.True(t, bl.Lookup("mycontract", "xxx"))
|
||||||
|
require.False(t, bl.Lookup("xxx", "yyy"))
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue