Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyonur committed May 18, 2023
1 parent 528f77c commit 55d0ed0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 84 deletions.
99 changes: 33 additions & 66 deletions helloworld/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,50 @@ import (
"math/big"
"testing"

"github.com/ava-labs/subnet-evm/precompile/allowlist"
"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"
)

func TestVerifyHelloWorldConfig(t *testing.T) {
admins := []common.Address{{1}}
tests := []struct {
name string
config precompileconfig.Config
ExpectedError string
}{
{
name: "invalid allow list config in hello world allowlist",
config: NewConfig(big.NewInt(3), admins, admins),
ExpectedError: "cannot set address",
},
}
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)
}
})
}
func TestVerify(t *testing.T) {
// We don't have a custom verification logic for HelloWorld
// so we just test the allowlist verification logic with a nil custom verifyTests input.
// VerifyPrecompileWithAllowListTests will add the allowlist verification logic tests for us.
allowlist.VerifyPrecompileWithAllowListTests(t, Module, nil)
}

func TestEqualHelloWorldConfig(t *testing.T) {
admins := []common.Address{{1}}
enableds := []common.Address{{2}}
tests := []struct {
name string
config precompileconfig.Config
other precompileconfig.Config
expected bool
}{
{
name: "non-nil config and nil other",
config: NewConfig(big.NewInt(3), admins, enableds),
other: nil,
expected: false,
admins := []common.Address{allowlist.TestAdminAddr}
enableds := []common.Address{allowlist.TestEnabledAddr}
tests := map[string]testutils.ConfigEqualTest{
"non-nil config and nil other": {
Config: NewConfig(big.NewInt(3), admins, enableds),
Other: nil,
Expected: false,
},
{
name: "different type",
config: NewConfig(big.NewInt(3), admins, enableds),
other: precompileconfig.NewNoopStatefulPrecompileConfig(),
expected: false,
"different type": {
Config: NewConfig(big.NewInt(3), admins, enableds),
Other: precompileconfig.NewNoopStatefulPrecompileConfig(),
Expected: false,
},
{
name: "different timestamp",
config: NewConfig(big.NewInt(3), admins, nil),
other: NewConfig(big.NewInt(4), admins, nil),
expected: false,
"different timestamp": {
Config: NewConfig(big.NewInt(3), admins, nil),
Other: NewConfig(big.NewInt(4), admins, nil),
Expected: false,
},
{
name: "different enabled",
config: NewConfig(big.NewInt(3), admins, nil),
other: NewConfig(big.NewInt(3), admins, enableds),
expected: false,
"different enabled": {
Config: NewConfig(big.NewInt(3), admins, nil),
Other: NewConfig(big.NewInt(3), admins, enableds),
Expected: false,
},
{
name: "same config",
config: NewConfig(big.NewInt(3), admins, nil),
other: NewConfig(big.NewInt(3), admins, nil),
expected: true,
"same config": {
Config: NewConfig(big.NewInt(3), admins, nil),
Other: NewConfig(big.NewInt(3), admins, nil),
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))
})
}
// EqualPrecompileWithAllowListTests will add the allowlist verification logic tests for us.
// We also add the custom tests defined above as [tests] input to the function.
allowlist.EqualPrecompileWithAllowListTests(t, Module, tests)
}
50 changes: 32 additions & 18 deletions helloworld/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
"github.com/stretchr/testify/require"
)

func TestHelloWorld(t *testing.T) {
testGreeting := "test"
tests := map[string]testutils.PrecompileTest{
var (
testGreeting = "test"
tests = map[string]testutils.PrecompileTest{
"set greeting from no role fails": {
Caller: allowlist.TestNoRoleAddr,
BeforeHook: allowlist.SetDefaultRoles(Module.Address),
InputFn: func(t *testing.T) []byte {
InputFn: func(t testing.TB) []byte {
input, err := PackSetGreeting("test")
require.NoError(t, err)

Expand All @@ -34,7 +34,7 @@ func TestHelloWorld(t *testing.T) {
"set greeting from enabled address": {
Caller: allowlist.TestEnabledAddr,
BeforeHook: allowlist.SetDefaultRoles(Module.Address),
InputFn: func(t *testing.T) []byte {
InputFn: func(t testing.TB) []byte {
input, err := PackSetGreeting(testGreeting)
require.NoError(t, err)

Expand All @@ -43,15 +43,15 @@ func TestHelloWorld(t *testing.T) {
SuppliedGas: SetGreetingGasCost,
ReadOnly: false,
ExpectedRes: []byte{},
AfterHook: func(t *testing.T, state contract.StateDB) {
AfterHook: func(t testing.TB, state contract.StateDB) {
greeting := GetGreeting(state)
require.Equal(t, greeting, testGreeting)
},
},
"set greeting from admin address": {
Caller: allowlist.TestAdminAddr,
BeforeHook: allowlist.SetDefaultRoles(Module.Address),
InputFn: func(t *testing.T) []byte {
InputFn: func(t testing.TB) []byte {
input, err := PackSetGreeting(testGreeting)
require.NoError(t, err)

Expand All @@ -60,15 +60,15 @@ func TestHelloWorld(t *testing.T) {
SuppliedGas: SetGreetingGasCost,
ReadOnly: false,
ExpectedRes: []byte{},
AfterHook: func(t *testing.T, state contract.StateDB) {
AfterHook: func(t testing.TB, state contract.StateDB) {
greeting := GetGreeting(state)
require.Equal(t, greeting, testGreeting)
},
},
"get default hello from non-enabled address": {
Caller: allowlist.TestNoRoleAddr,
BeforeHook: allowlist.SetDefaultRoles(Module.Address),
InputFn: func(t *testing.T) []byte {
InputFn: func(t testing.TB) []byte {
input, err := PackSayHello()
require.NoError(t, err)

Expand All @@ -79,17 +79,19 @@ func TestHelloWorld(t *testing.T) {
ReadOnly: true,
ExpectedRes: func() []byte {
res, err := PackSayHelloOutput(defaultGreeting)
require.NoError(t, err)
if err != nil {
panic(err)
}
return res
}(),
},
"store greeting then say hello from non-enabled address": {
Caller: allowlist.TestNoRoleAddr,
BeforeHook: func(t *testing.T, state contract.StateDB) {
BeforeHook: func(t testing.TB, state contract.StateDB) {
allowlist.SetDefaultRoles(Module.Address)(t, state)
StoreGreeting(state, testGreeting)
},
InputFn: func(t *testing.T) []byte {
InputFn: func(t testing.TB) []byte {
input, err := PackSayHello()
require.NoError(t, err)

Expand All @@ -99,14 +101,16 @@ func TestHelloWorld(t *testing.T) {
ReadOnly: true,
ExpectedRes: func() []byte {
res, err := PackSayHelloOutput(testGreeting)
require.NoError(t, err)
if err != nil {
panic(err)
}
return res
}(),
},
"set a very long greeting from enabled address": {
Caller: allowlist.TestEnabledAddr,
BeforeHook: allowlist.SetDefaultRoles(Module.Address),
InputFn: func(t *testing.T) []byte {
InputFn: func(t testing.TB) []byte {
longString := "a very long string that is longer than 32 bytes and will cause an error"
input, err := PackSetGreeting(longString)
require.NoError(t, err)
Expand All @@ -120,7 +124,7 @@ func TestHelloWorld(t *testing.T) {
"readOnly setFeeConfig with noRole fails": {
Caller: allowlist.TestNoRoleAddr,
BeforeHook: allowlist.SetDefaultRoles(Module.Address),
InputFn: func(t *testing.T) []byte {
InputFn: func(t testing.TB) []byte {
input, err := PackSetGreeting(testGreeting)
require.NoError(t, err)

Expand All @@ -133,7 +137,7 @@ func TestHelloWorld(t *testing.T) {
"readOnly setFeeConfig with enabled role fails": {
Caller: allowlist.TestEnabledAddr,
BeforeHook: allowlist.SetDefaultRoles(Module.Address),
InputFn: func(t *testing.T) []byte {
InputFn: func(t testing.TB) []byte {
input, err := PackSetGreeting(testGreeting)
require.NoError(t, err)

Expand All @@ -146,7 +150,7 @@ func TestHelloWorld(t *testing.T) {
"readOnly setFeeConfig with admin role fails": {
Caller: allowlist.TestAdminAddr,
BeforeHook: allowlist.SetDefaultRoles(Module.Address),
InputFn: func(t *testing.T) []byte {
InputFn: func(t testing.TB) []byte {
input, err := PackSetGreeting(testGreeting)
require.NoError(t, err)

Expand All @@ -159,7 +163,7 @@ func TestHelloWorld(t *testing.T) {
"insufficient gas setFeeConfig from admin": {
Caller: allowlist.TestAdminAddr,
BeforeHook: allowlist.SetDefaultRoles(Module.Address),
InputFn: func(t *testing.T) []byte {
InputFn: func(t testing.TB) []byte {
input, err := PackSetGreeting(testGreeting)
require.NoError(t, err)

Expand All @@ -170,6 +174,16 @@ func TestHelloWorld(t *testing.T) {
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
},
}
)

func TestHelloWorld(t *testing.T) {
// RunPrecompileWithAllowListTests will add the allowlist verification logic tests for us.
// We also add the custom tests defined above as [tests] input to the function.
allowlist.RunPrecompileWithAllowListTests(t, Module, state.NewTestStateDB, tests)
}

func BenchmarkHelloWorld(b *testing.B) {
// BenchPrecompileWithAllowList will add the allowlist benchmarks for us.
// We also add the custom tests defined above as [tests] input to the function.
allowlist.BenchPrecompileWithAllowList(b, Module, state.NewTestStateDB, tests)
}

0 comments on commit 55d0ed0

Please sign in to comment.