Skip to content

Commit

Permalink
fix: change return value of ListContentAtACL and write test for it (#…
Browse files Browse the repository at this point in the history
…1373)

* fix: change return value of ListContentAtACL and write test for it

Signed-off-by: Ji Hwan <jkim@polygon.technology>

* chore: cleanup + fix test cases

Signed-off-by: Ji Hwan <jkim@polygon.technology>

---------

Signed-off-by: Ji Hwan <jkim@polygon.technology>
  • Loading branch information
jhkimqd authored Nov 4, 2024
1 parent f7f7615 commit d1079c0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
28 changes: 23 additions & 5 deletions zk/txpool/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,12 +561,15 @@ func RemovePolicy(ctx context.Context, aclDB kv.RwDB, aclType string, addr commo
return err
}

func ListContentAtACL(ctx context.Context, db kv.RwDB) (string, error) {

func ListContentAtACL(ctx context.Context, db kv.RwDB) ([]string, error) {
var combinedBuffers []string
var buffer bytes.Buffer
var bufferConfig bytes.Buffer
var bufferBlockList bytes.Buffer
var bufferAllowlist bytes.Buffer

tables := db.AllTables()
buffer.WriteString("ListContentAtACL\n")
buffer.WriteString(" \n")
buffer.WriteString("Tables\nTable - { Flags, AutoDupSortKeysConversion, IsDeprecated, DBI, DupFromLen, DupToLen }\n")
for key, config := range tables {
buffer.WriteString(fmt.Sprint(key, config, "\n"))
Expand All @@ -577,6 +580,7 @@ func ListContentAtACL(ctx context.Context, db kv.RwDB) (string, error) {
buffer.WriteString("\nConfig\n")
err := tx.ForEach(Config, nil, func(k, v []byte) error {
buffer.WriteString(fmt.Sprintf("Key: %s, Value: %s\n", string(k), string(v)))
bufferConfig.WriteString(fmt.Sprintf("Key: %s, Value: %s\n", string(k), string(v)))
return nil
})

Expand All @@ -598,10 +602,14 @@ func ListContentAtACL(ctx context.Context, db kv.RwDB) (string, error) {
"\nBlocklist\n%s",
BlockListContent.String(),
))
bufferBlockList.WriteString(fmt.Sprintf(
"\nBlocklist\n%s",
BlockListContent.String(),
))
} else {
buffer.WriteString("\nBlocklist is empty")
bufferBlockList.WriteString("\nBlocklist is empty")
}

// Allowlist table
var AllowlistContent strings.Builder
err = tx.ForEach(Allowlist, nil, func(k, v []byte) error {
Expand All @@ -620,14 +628,24 @@ func ListContentAtACL(ctx context.Context, db kv.RwDB) (string, error) {
"\nAllowlist\n%s",
AllowlistContent.String(),
))
bufferAllowlist.WriteString(fmt.Sprintf(
"\nAllowlist\n%s",
AllowlistContent.String(),
))
} else {
buffer.WriteString("\nAllowlist is empty")
bufferAllowlist.WriteString("\nAllowlist is empty")
}

return err
})

return buffer.String(), err
combinedBuffers = append(combinedBuffers, buffer.String())
combinedBuffers = append(combinedBuffers, bufferConfig.String())
combinedBuffers = append(combinedBuffers, bufferBlockList.String())
combinedBuffers = append(combinedBuffers, bufferAllowlist.String())

return combinedBuffers, err
}

// SetMode sets the mode of the ACL
Expand Down
50 changes: 50 additions & 0 deletions zk/txpool/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,53 @@ func TestIsActionAllowed(t *testing.T) {
require.True(t, allowed) // In disabled mode, all actions are allowed
})
}

func TestListContentAtACL(t *testing.T) {
db := newTestACLDB(t, "")
ctx := context.Background()

// Populate different tables in ACL
// Create a test address and policy for allowlist table
addrAllowlist := common.HexToAddress("0x1234567890abcdef")
policyAllowlist := SendTx

err := AddPolicy(ctx, db, "allowlist", addrAllowlist, policyAllowlist)
require.NoError(t, err)

// Create a test address and policy for blocklist table
addrBlocklist := common.HexToAddress("0x1234567890abcdef")
policyBlocklist := SendTx

err = AddPolicy(ctx, db, "blocklist", addrBlocklist, policyBlocklist)
require.NoError(t, err)

var tests = []struct {
wantAllowlist string
wantBlockList string
}{
{"\nAllowlist\nKey: 0000000000000000000000001234567890abcdef, Value: {\n\tdeploy: false\n\tsendTx: true\n}\n", "\nBlocklist\nKey: 0000000000000000000000001234567890abcdef, Value: {\n\tsendTx: true\n\tdeploy: false\n}\n"},
}
// ListContentAtACL will return []string in the following order:
// [buffer.String(), bufferConfig.String(), bufferBlockList.String(), bufferAllowlist.String()]
ans, err := ListContentAtACL(ctx, db)
for _, tt := range tests {
t.Run("ListContentAtACL", func(t *testing.T) {
switch {
case err != nil:
t.Errorf("ListContentAtACL did not execute successfully: %v", err)
case !strings.Contains(ans[3], "\nAllowlist\nKey: 0000000000000000000000001234567890abcdef"):
t.Errorf("got %v, want %v", ans, tt.wantAllowlist)
case !strings.Contains(ans[3], "sendTx: true"):
t.Errorf("got %v, want %v", ans, tt.wantAllowlist)
case !strings.Contains(ans[3], "deploy: false"):
t.Errorf("got %v, want %v", ans, tt.wantAllowlist)
case !strings.Contains(ans[2], "\nBlocklist\nKey: 0000000000000000000000001234567890abcdef"):
t.Errorf("got %v, want %v", ans, tt.wantBlockList)
case !strings.Contains(ans[2], "sendTx: true"):
t.Errorf("got %v, want %v", ans, tt.wantBlockList)
case !strings.Contains(ans[2], "deploy: false"):
t.Errorf("got %v, want %v", ans, tt.wantBlockList)
}
})
}
}

0 comments on commit d1079c0

Please sign in to comment.