-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add verify allow list tests * use new verify allow list suite in precompiles * add equal test suite * nits * add cnfig test template * add contract unit test generation * Remove abis * nits * fix testing input in template * Update accounts/abi/bind/precompilebind/precompile_contract_test_template.go Co-authored-by: Darioush Jalali <darioush.jalali@avalabs.org> * Update accounts/abi/bind/precompilebind/precompile_contract_test_template.go Co-authored-by: Darioush Jalali <darioush.jalali@avalabs.org> * Update accounts/abi/bind/precompilebind/precompile_contract_test_template.go Co-authored-by: Darioush Jalali <darioush.jalali@avalabs.org> * fix non-allowlist tests
- Loading branch information
Showing
15 changed files
with
367 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
accounts/abi/bind/precompilebind/precompile_config_test_template.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// (c) 2019-2022, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
package precompilebind | ||
|
||
// tmplSourcePrecompileConfigGo is the Go precompiled config source template. | ||
const tmplSourcePrecompileConfigTestGo = ` | ||
// Code generated | ||
// This file is a generated precompile config test with the skeleton of test functions. | ||
// The file is generated by a template. Please inspect every code and comment in this file before use. | ||
package {{.Package}} | ||
import ( | ||
"math/big" | ||
"testing" | ||
"github.com/ava-labs/subnet-evm/precompile/precompileconfig" | ||
"github.com/ava-labs/subnet-evm/precompile/testutils" | ||
{{- if .Contract.AllowList}} | ||
"github.com/ava-labs/subnet-evm/precompile/allowlist" | ||
"github.com/ethereum/go-ethereum/common" | ||
{{- end}} | ||
) | ||
// TestVerify tests the verification of Config. | ||
func TestVerify(t *testing.T) { | ||
{{- if .Contract.AllowList}} | ||
admins := []common.Address{allowlist.TestAdminAddr} | ||
enableds := []common.Address{allowlist.TestEnabledAddr} | ||
{{- end}} | ||
tests := map[string]testutils.ConfigVerifyTest{ | ||
"valid config": { | ||
Config: NewConfig(big.NewInt(3){{- if .Contract.AllowList}}, admins, enableds{{- end}}), | ||
ExpectedError: "", | ||
}, | ||
// CUSTOM CODE STARTS HERE | ||
// Add your own Verify tests here, e.g.: | ||
// "your custom test name": { | ||
// Config: NewConfig(big.NewInt(3), {{- if .Contract.AllowList}} admins, enableds{{- end}}), | ||
// ExpectedError: ErrYourCustomError.Error(), | ||
// }, | ||
} | ||
{{- if .Contract.AllowList}} | ||
// Verify the precompile with the allowlist. | ||
// This adds allowlist verify tests to your custom tests | ||
// and runs them all together. | ||
// Even if you don't add any custom tests, keep this. This will still | ||
// run the default allowlist verify tests. | ||
allowlist.VerifyPrecompileWithAllowListTests(t, Module, tests) | ||
{{- else}} | ||
// Run verify tests. | ||
testutils.RunVerifyTests(t, tests) | ||
{{- end}} | ||
} | ||
// TestEqual tests the equality of Config with other precompile configs. | ||
func TestEqual(t *testing.T) { | ||
{{- if .Contract.AllowList}} | ||
admins := []common.Address{allowlist.TestAdminAddr} | ||
enableds := []common.Address{allowlist.TestEnabledAddr} | ||
{{- end}} | ||
tests := map[string]testutils.ConfigEqualTest{ | ||
"non-nil config and nil other": { | ||
Config: NewConfig(big.NewInt(3){{- if .Contract.AllowList}}, admins, enableds{{- end}}), | ||
Other: nil, | ||
Expected: false, | ||
}, | ||
"different type": { | ||
Config: NewConfig(big.NewInt(3){{- if .Contract.AllowList}}, admins, enableds{{- end}}), | ||
Other: precompileconfig.NewNoopStatefulPrecompileConfig(), | ||
Expected: false, | ||
}, | ||
"different timestamp": { | ||
Config: NewConfig(big.NewInt(3){{- if .Contract.AllowList}}, admins, enableds{{- end}}), | ||
Other: NewConfig(big.NewInt(4){{- if .Contract.AllowList}}, admins, enableds{{- end}}), | ||
Expected: false, | ||
}, | ||
"same config": { | ||
Config: NewConfig(big.NewInt(3){{- if .Contract.AllowList}}, admins, enableds{{- end}}), | ||
Other: NewConfig(big.NewInt(3){{- if .Contract.AllowList}}, admins, enableds{{- end}}), | ||
Expected: true, | ||
}, | ||
// CUSTOM CODE STARTS HERE | ||
// Add your own Equal tests here | ||
} | ||
{{- if .Contract.AllowList}} | ||
// Run allow list equal tests. | ||
// This adds allowlist equal tests to your custom tests | ||
// and runs them all together. | ||
// Even if you don't add any custom tests, keep this. This will still | ||
// run the default allowlist equal tests. | ||
allowlist.EqualPrecompileWithAllowListTests(t, Module, tests) | ||
{{- else}} | ||
// Run equal tests. | ||
testutils.RunEqualTests(t, tests) | ||
{{- end}} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
accounts/abi/bind/precompilebind/precompile_contract_test_template.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// (c) 2019-2022, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
package precompilebind | ||
|
||
// tmplSourcePrecompileConfigGo is the Go precompiled config source template. | ||
const tmplSourcePrecompileContractTestGo = ` | ||
// Code generated | ||
// This file is a generated precompile contract test with the skeleton of test functions. | ||
// The file is generated by a template. Please inspect every code and comment in this file before use. | ||
package {{.Package}} | ||
import ( | ||
"testing" | ||
"github.com/ava-labs/subnet-evm/core/state" | ||
{{- if .Contract.AllowList}} | ||
"github.com/ava-labs/subnet-evm/precompile/allowlist" | ||
{{- end}} | ||
"github.com/ava-labs/subnet-evm/precompile/testutils" | ||
"github.com/ava-labs/subnet-evm/vmerrs" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/require" | ||
) | ||
// TestRun tests the Run function of the precompile contract. | ||
// These tests are run against the precompile contract directly with | ||
// the given input and expected output. They're just a guide to | ||
// help you write your own tests. These tests are for general cases like | ||
// allowlist, readOnly behaviour, and gas cost. You should write your own | ||
// tests for specific cases. | ||
func TestRun(t *testing.T) { | ||
tests := map[string]testutils.PrecompileTest{ | ||
{{- $contract := .Contract}} | ||
{{- $structs := .Structs}} | ||
{{- range .Contract.Funcs}} | ||
{{- $func := .}} | ||
{{- if $contract.AllowList}} | ||
{{- $roles := mkList "NoRole" "Enabled" "Admin"}} | ||
{{- range $role := $roles}} | ||
{{- $fail := and (not $func.Original.IsConstant) (eq $role "NoRole")}} | ||
"calling {{decapitalise $func.Normalized.Name}} from {{$role}} should {{- if $fail}} fail {{- else}} succeed{{- end}}": { | ||
Caller: allowlist.Test{{$role}}Addr, | ||
BeforeHook: allowlist.SetDefaultRoles(Module.Address), | ||
InputFn: func(t testing.TB) []byte { | ||
{{- if len $func.Normalized.Inputs | lt 1}} | ||
// CUSTOM CODE STARTS HERE | ||
// populate test input here | ||
testInput := {{capitalise $func.Normalized.Name}}Input{} | ||
input, err := Pack{{$func.Normalized.Name}}(testInput) | ||
{{- else if len $func.Normalized.Inputs | eq 1 }} | ||
{{- $input := index $func.Normalized.Inputs 0}} | ||
// CUSTOM CODE STARTS HERE | ||
// set test input to a value here | ||
var testInput {{bindtype $input.Type $structs}} | ||
input, err := Pack{{$func.Normalized.Name}}(testInput) | ||
{{- else}} | ||
input, err := Pack{{$func.Normalized.Name}}() | ||
{{- end}} | ||
require.NoError(t, err) | ||
return input | ||
}, | ||
{{- if not $fail}} | ||
// This test is for a successful call. You can set the expected output here. | ||
// CUSTOM CODE STARTS HERE | ||
ExpectedRes: []byte{}, | ||
{{- end}} | ||
SuppliedGas: {{$func.Normalized.Name}}GasCost, | ||
ReadOnly: false, | ||
ExpectedErr: {{if $fail}} ErrCannot{{$func.Normalized.Name}}.Error() {{- else}} "" {{- end}}, | ||
}, | ||
{{- end}} | ||
{{- end}} | ||
{{- if not $func.Original.IsConstant}} | ||
"readOnly {{decapitalise $func.Normalized.Name}} should fail": { | ||
Caller: common.Address{1}, | ||
InputFn: func(t testing.TB) []byte { | ||
{{- if len $func.Normalized.Inputs | lt 1}} | ||
// CUSTOM CODE STARTS HERE | ||
// populate test input here | ||
testInput := {{capitalise $func.Normalized.Name}}Input{} | ||
input, err := Pack{{$func.Normalized.Name}}(testInput) | ||
{{- else if len $func.Normalized.Inputs | eq 1 }} | ||
{{- $input := index $func.Normalized.Inputs 0}} | ||
// CUSTOM CODE STARTS HERE | ||
// set test input to a value here | ||
var testInput {{bindtype $input.Type $structs}} | ||
input, err := Pack{{$func.Normalized.Name}}(testInput) | ||
{{- else}} | ||
input, err := Pack{{$func.Normalized.Name}}() | ||
{{- end}} | ||
require.NoError(t, err) | ||
return input | ||
}, | ||
SuppliedGas: {{$func.Normalized.Name}}GasCost, | ||
ReadOnly: true, | ||
ExpectedErr: vmerrs.ErrWriteProtection.Error(), | ||
}, | ||
{{- end}} | ||
"insufficient gas for {{decapitalise $func.Normalized.Name}} should fail": { | ||
Caller: common.Address{1}, | ||
InputFn: func(t testing.TB) []byte { | ||
{{- if len $func.Normalized.Inputs | lt 1}} | ||
// CUSTOM CODE STARTS HERE | ||
// populate test input here | ||
testInput := {{capitalise $func.Normalized.Name}}Input{} | ||
input, err := Pack{{$func.Normalized.Name}}(testInput) | ||
{{- else if len $func.Normalized.Inputs | eq 1 }} | ||
{{- $input := index $func.Normalized.Inputs 0}} | ||
// CUSTOM CODE STARTS HERE | ||
// set test input to a value here | ||
var testInput {{bindtype $input.Type $structs}} | ||
input, err := Pack{{$func.Normalized.Name}}(testInput) | ||
{{- else}} | ||
input, err := Pack{{$func.Normalized.Name}}() | ||
{{- end}} | ||
require.NoError(t, err) | ||
return input | ||
}, | ||
SuppliedGas: {{$func.Normalized.Name}}GasCost - 1, | ||
ReadOnly: false, | ||
ExpectedErr: vmerrs.ErrOutOfGas.Error(), | ||
}, | ||
{{- end}} | ||
} | ||
{{- if .Contract.AllowList}} | ||
// Run tests with allowlist tests. | ||
// This adds allowlist run tests to your custom tests | ||
// and runs them all together. | ||
// Even if you don't add any custom tests, keep this. This will still | ||
// run the default allowlist tests. | ||
allowlist.RunPrecompileWithAllowListTests(t, Module, state.NewTestStateDB, tests) | ||
{{- else}} | ||
// Run tests. | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
test.Run(t, Module, state.NewTestStateDB(t)) | ||
}) | ||
} | ||
{{- end}} | ||
} | ||
` |
Oops, something went wrong.