Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config test suites #557

Merged
merged 11 commits into from
Apr 4, 2023
10 changes: 7 additions & 3 deletions precompile/allowlist/allowlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ var (
)

type dummyConfig struct {
*AllowListConfig
AllowListConfig
}

func (d *dummyConfig) Key() string { return "dummy" }
func (d *dummyConfig) Timestamp() *big.Int { return common.Big0 }
func (d *dummyConfig) IsDisabled() bool { return false }
func (d *dummyConfig) Equal(other precompileconfig.Config) bool {
return d.AllowListConfig.Equal(other.(*dummyConfig).AllowListConfig)
func (d *dummyConfig) Equal(cfg precompileconfig.Config) bool {
other, ok := (cfg).(*dummyConfig)
if !ok {
return false
}
return d.AllowListConfig.Equal(&other.AllowListConfig)
}

type dummyConfigurator struct{}
Expand Down
6 changes: 3 additions & 3 deletions precompile/allowlist/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *AllowListConfig) Verify() error {
// check for duplicates in enabled list
for _, enabledAddr := range c.EnabledAddresses {
if _, ok := addressMap[enabledAddr]; ok {
return fmt.Errorf("duplicate address %s in enabled list", enabledAddr)
return fmt.Errorf("duplicate address in enabled list: %s", enabledAddr)
}
addressMap[enabledAddr] = EnabledRole
}
Expand All @@ -67,9 +67,9 @@ func (c *AllowListConfig) Verify() error {
for _, adminAddr := range c.AdminAddresses {
if role, ok := addressMap[adminAddr]; ok {
if role == AdminRole {
return fmt.Errorf("duplicate address %s in admin list", adminAddr)
return fmt.Errorf("duplicate address in admin list: %s", adminAddr)
} else {
return fmt.Errorf("cannot set address %s as both admin and enabled", adminAddr)
return fmt.Errorf("cannot set address as both admin and enabled: %s", adminAddr)
}
}
addressMap[adminAddr] = AdminRole
Expand Down
93 changes: 12 additions & 81 deletions precompile/allowlist/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,23 @@ package allowlist
import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"github.com/ava-labs/subnet-evm/precompile/modules"
)

func TestVerifyAllowlistAllowList(t *testing.T) {
admins := []common.Address{{1}}
enableds := []common.Address{{2}}
tests := []struct {
name string
config AllowListConfig
expectedError string
}{
{
name: "invalid allow list config in allowlist",
config: AllowListConfig{admins, admins},
expectedError: "cannot set address",
},
{
name: "nil member allow list config in allowlist",
config: AllowListConfig{nil, nil},
expectedError: "",
},
{
name: "empty member allow list config in allowlist",
config: AllowListConfig{[]common.Address{}, []common.Address{}},
expectedError: "",
},
{
name: "valid allow list config in allowlist",
config: AllowListConfig{admins, enableds},
expectedError: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)

err := tt.config.Verify()
if tt.expectedError == "" {
require.NoError(err)
} else {
require.ErrorContains(err, tt.expectedError)
}
})
dummyModule := modules.Module{
aaronbuchwald marked this conversation as resolved.
Show resolved Hide resolved
aaronbuchwald marked this conversation as resolved.
Show resolved Hide resolved
Address: dummyAddr,
Contract: CreateAllowListPrecompile(dummyAddr),
Configurator: &dummyConfigurator{},
}
VerifyPrecompileWithAllowListTests(t, dummyModule, nil)
}

func TestEqualAllowListAllowList(t *testing.T) {
admins := []common.Address{{1}}
enableds := []common.Address{{2}}
tests := []struct {
name string
config *AllowListConfig
other *AllowListConfig
expected bool
}{
{
name: "non-nil config and nil other",
config: &AllowListConfig{admins, enableds},
other: nil,
expected: false,
},
{
name: "different admin",
config: &AllowListConfig{admins, enableds},
other: &AllowListConfig{[]common.Address{{3}}, enableds},
expected: false,
},
{
name: "different enabled",
config: &AllowListConfig{admins, enableds},
other: &AllowListConfig{admins, []common.Address{{3}}},
expected: false,
},
{
name: "same config",
config: &AllowListConfig{admins, enableds},
other: &AllowListConfig{admins, enableds},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)

require.Equal(tt.expected, tt.config.Equal(tt.other))
})
func TestEqualAllowList(t *testing.T) {
dummyModule := modules.Module{
Address: dummyAddr,
Contract: CreateAllowListPrecompile(dummyAddr),
Configurator: &dummyConfigurator{},
}
EqualPrecompileWithAllowListTests(t, dummyModule, nil)
}
19 changes: 0 additions & 19 deletions precompile/allowlist/test_allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
package allowlist

import (
"encoding/json"
"testing"

"github.com/ava-labs/subnet-evm/precompile/contract"
"github.com/ava-labs/subnet-evm/precompile/modules"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ava-labs/subnet-evm/precompile/testutils"
"github.com/ava-labs/subnet-evm/vmerrs"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -22,23 +20,6 @@ var (
TestNoRoleAddr = common.HexToAddress("0x0000000000000000000000000000000000000033")
)

// mkConfigWithAllowList creates a new config with the correct type for [module]
// by marshalling [cfg] to JSON and then unmarshalling it into the config.
func mkConfigWithAllowList(module modules.Module, cfg *AllowListConfig) precompileconfig.Config {
jsonBytes, err := json.Marshal(cfg)
if err != nil {
panic(err)
}

moduleCfg := module.MakeConfig()
err = json.Unmarshal(jsonBytes, moduleCfg)
if err != nil {
panic(err)
}

return moduleCfg
}

func AllowListTests(module modules.Module) map[string]testutils.PrecompileTest {
contractAddress := module.Address
return map[string]testutils.PrecompileTest{
Expand Down
133 changes: 133 additions & 0 deletions precompile/allowlist/test_allowlist_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// (c) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package allowlist

import (
"encoding/json"
"testing"

"github.com/ava-labs/subnet-evm/precompile/modules"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ava-labs/subnet-evm/precompile/testutils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

// mkConfigWithAllowList creates a new config with the correct type for [module]
// by marshalling [cfg] to JSON and then unmarshalling it into the config.
func mkConfigWithAllowList(module modules.Module, cfg *AllowListConfig) precompileconfig.Config {
jsonBytes, err := json.Marshal(cfg)
if err != nil {
panic(err)
}

moduleCfg := module.MakeConfig()
err = json.Unmarshal(jsonBytes, moduleCfg)
if err != nil {
panic(err)
}

return moduleCfg
}

func AllowListConfigVerifyTests(module modules.Module) map[string]testutils.ConfigVerifyTest {
return map[string]testutils.ConfigVerifyTest{
"invalid allow list config with duplicate admins in allowlist": {
Config: mkConfigWithAllowList(module, &AllowListConfig{[]common.Address{TestAdminAddr, TestAdminAddr}, nil}),
ExpectedError: "duplicate address in admin list",
},
"invalid allow list config with duplicate enableds in allowlist": {
Config: mkConfigWithAllowList(module, &AllowListConfig{nil, []common.Address{TestEnabledAddr, TestEnabledAddr}}),
ExpectedError: "duplicate address in enabled list",
},
"invalid allow list config with same admin and enabled in allowlist": {
Config: mkConfigWithAllowList(module, &AllowListConfig{[]common.Address{TestAdminAddr}, []common.Address{TestAdminAddr}}),
ExpectedError: "cannot set address as both admin and enabled",
},
"nil member allow list config in allowlist": {
Config: mkConfigWithAllowList(module, &AllowListConfig{nil, nil}),
ExpectedError: "",
},
"empty member allow list config in allowlist": {
Config: mkConfigWithAllowList(module, &AllowListConfig{[]common.Address{}, []common.Address{}}),
ExpectedError: "",
},
"valid allow list config in allowlist": {
Config: mkConfigWithAllowList(module, &AllowListConfig{[]common.Address{TestAdminAddr}, []common.Address{TestEnabledAddr}}),
ExpectedError: "",
},
}
}

func AllowListConfigEqualTests(module modules.Module) map[string]testutils.ConfigEqualTest {
return map[string]testutils.ConfigEqualTest{
"allowlist non-nil config and nil other": {
Config: mkConfigWithAllowList(module, &AllowListConfig{[]common.Address{TestAdminAddr}, []common.Address{TestEnabledAddr}}),
Other: nil,
Expected: false,
},
"allowlist different admin": {
Config: mkConfigWithAllowList(module, &AllowListConfig{[]common.Address{TestAdminAddr}, []common.Address{TestEnabledAddr}}),
Other: mkConfigWithAllowList(module, &AllowListConfig{[]common.Address{{3}}, []common.Address{TestEnabledAddr}}),
Expected: false,
},
"allowlist different enabled": {
Config: mkConfigWithAllowList(module, &AllowListConfig{[]common.Address{TestAdminAddr}, []common.Address{TestEnabledAddr}}),
Other: mkConfigWithAllowList(module, &AllowListConfig{[]common.Address{module.Address}, []common.Address{{3}}}),
Expected: false,
},
"allowlist same config": {
Config: mkConfigWithAllowList(module, &AllowListConfig{[]common.Address{TestAdminAddr}, []common.Address{TestEnabledAddr}}),
Other: mkConfigWithAllowList(module, &AllowListConfig{[]common.Address{TestAdminAddr}, []common.Address{TestEnabledAddr}}),
Expected: true,
},
}
}

func VerifyPrecompileWithAllowListTests(t *testing.T, module modules.Module, verifyTests map[string]testutils.ConfigVerifyTest) {
t.Helper()
tests := AllowListConfigVerifyTests(module)
// Add the contract specific tests to the map of tests to run.
for name, test := range verifyTests {
if _, exists := tests[name]; exists {
t.Fatalf("duplicate test name: %s", name)
}
tests[name] = test
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
t.Helper()
require := require.New(t)

err := test.Config.Verify()
if test.ExpectedError == "" {
require.NoError(err)
} else {
require.ErrorContains(err, test.ExpectedError)
}
})
}
}

func EqualPrecompileWithAllowListTests(t *testing.T, module modules.Module, equalTests map[string]testutils.ConfigEqualTest) {
t.Helper()
tests := AllowListConfigEqualTests(module)
// Add the contract specific tests to the map of tests to run.
for name, test := range equalTests {
if _, exists := tests[name]; exists {
t.Fatalf("duplicate test name: %s", name)
}
tests[name] = test
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
t.Helper()
require := require.New(t)

require.Equal(test.Expected, test.Config.Equal(test.Other))
})
}
}
Loading