From 93a816cb2cfbd5903f483976f9cb1a4a407daf0d Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 23 Jun 2024 14:46:38 +0200 Subject: [PATCH] Adding internal/types/blacklist.go --- internal/types/blacklist.go | 21 ++++++++++++++++ internal/types/blacklist_test.go | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 internal/types/blacklist.go create mode 100644 internal/types/blacklist_test.go diff --git a/internal/types/blacklist.go b/internal/types/blacklist.go new file mode 100644 index 0000000..cbe28b5 --- /dev/null +++ b/internal/types/blacklist.go @@ -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 +} diff --git a/internal/types/blacklist_test.go b/internal/types/blacklist_test.go new file mode 100644 index 0000000..a897802 --- /dev/null +++ b/internal/types/blacklist_test.go @@ -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")) +}