From d1079c0d56985cafd340077b722a7856e13f1051 Mon Sep 17 00:00:00 2001 From: Ji Hwan KIM <125336262+jhkimqd@users.noreply.github.com> Date: Tue, 5 Nov 2024 01:49:03 +0900 Subject: [PATCH] fix: change return value of ListContentAtACL and write test for it (#1373) * fix: change return value of ListContentAtACL and write test for it Signed-off-by: Ji Hwan * chore: cleanup + fix test cases Signed-off-by: Ji Hwan --------- Signed-off-by: Ji Hwan --- zk/txpool/policy.go | 28 ++++++++++++++++++---- zk/txpool/policy_test.go | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/zk/txpool/policy.go b/zk/txpool/policy.go index 5755928eb37..920003b94cb 100644 --- a/zk/txpool/policy.go +++ b/zk/txpool/policy.go @@ -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")) @@ -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 }) @@ -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 { @@ -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 diff --git a/zk/txpool/policy_test.go b/zk/txpool/policy_test.go index b639cc4b1eb..d12d59eea39 100644 --- a/zk/txpool/policy_test.go +++ b/zk/txpool/policy_test.go @@ -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) + } + }) + } +}