github.com/cgcardona/r-subnet-evm@v0.1.5/accounts/abi/bind/precompilebind/precompile_contract_test_template.go (about) 1 // (c) 2019-2022, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 package precompilebind 4 5 // tmplSourcePrecompileConfigGo is the Go precompiled config source template. 6 const tmplSourcePrecompileContractTestGo = ` 7 // Code generated 8 // This file is a generated precompile contract test with the skeleton of test functions. 9 // The file is generated by a template. Please inspect every code and comment in this file before use. 10 11 package {{.Package}} 12 13 import ( 14 "testing" 15 16 "github.com/cgcardona/r-subnet-evm/core/state" 17 {{- if .Contract.AllowList}} 18 "github.com/cgcardona/r-subnet-evm/precompile/allowlist" 19 {{- end}} 20 "github.com/cgcardona/r-subnet-evm/precompile/testutils" 21 "github.com/cgcardona/r-subnet-evm/vmerrs" 22 "github.com/ethereum/go-ethereum/common" 23 "github.com/stretchr/testify/require" 24 ) 25 26 // These tests are run against the precompile contract directly with 27 // the given input and expected output. They're just a guide to 28 // help you write your own tests. These tests are for general cases like 29 // allowlist, readOnly behaviour, and gas cost. You should write your own 30 // tests for specific cases. 31 var( 32 tests = map[string]testutils.PrecompileTest{ 33 {{- $contract := .Contract}} 34 {{- $structs := .Structs}} 35 {{- range .Contract.Funcs}} 36 {{- $func := .}} 37 {{- if $contract.AllowList}} 38 {{- $roles := mkList "NoRole" "Enabled" "Admin"}} 39 {{- range $role := $roles}} 40 {{- $fail := and (not $func.Original.IsConstant) (eq $role "NoRole")}} 41 "calling {{decapitalise $func.Normalized.Name}} from {{$role}} should {{- if $fail}} fail {{- else}} succeed{{- end}}": { 42 Caller: allowlist.Test{{$role}}Addr, 43 BeforeHook: allowlist.SetDefaultRoles(Module.Address), 44 InputFn: func(t testing.TB) []byte { 45 {{- if len $func.Normalized.Inputs | lt 1}} 46 // CUSTOM CODE STARTS HERE 47 // populate test input here 48 testInput := {{capitalise $func.Normalized.Name}}Input{} 49 input, err := Pack{{$func.Normalized.Name}}(testInput) 50 {{- else if len $func.Normalized.Inputs | eq 1 }} 51 {{- $input := index $func.Normalized.Inputs 0}} 52 // CUSTOM CODE STARTS HERE 53 // set test input to a value here 54 var testInput {{bindtype $input.Type $structs}} 55 input, err := Pack{{$func.Normalized.Name}}(testInput) 56 {{- else}} 57 input, err := Pack{{$func.Normalized.Name}}() 58 {{- end}} 59 require.NoError(t, err) 60 return input 61 }, 62 {{- if not $fail}} 63 // This test is for a successful call. You can set the expected output here. 64 // CUSTOM CODE STARTS HERE 65 ExpectedRes: []byte{}, 66 {{- end}} 67 SuppliedGas: {{$func.Normalized.Name}}GasCost, 68 ReadOnly: false, 69 ExpectedErr: {{if $fail}} ErrCannot{{$func.Normalized.Name}}.Error() {{- else}} "" {{- end}}, 70 }, 71 {{- end}} 72 {{- end}} 73 {{- if not $func.Original.IsConstant}} 74 "readOnly {{decapitalise $func.Normalized.Name}} should fail": { 75 Caller: common.Address{1}, 76 InputFn: func(t testing.TB) []byte { 77 {{- if len $func.Normalized.Inputs | lt 1}} 78 // CUSTOM CODE STARTS HERE 79 // populate test input here 80 testInput := {{capitalise $func.Normalized.Name}}Input{} 81 input, err := Pack{{$func.Normalized.Name}}(testInput) 82 {{- else if len $func.Normalized.Inputs | eq 1 }} 83 {{- $input := index $func.Normalized.Inputs 0}} 84 // CUSTOM CODE STARTS HERE 85 // set test input to a value here 86 var testInput {{bindtype $input.Type $structs}} 87 input, err := Pack{{$func.Normalized.Name}}(testInput) 88 {{- else}} 89 input, err := Pack{{$func.Normalized.Name}}() 90 {{- end}} 91 require.NoError(t, err) 92 return input 93 }, 94 SuppliedGas: {{$func.Normalized.Name}}GasCost, 95 ReadOnly: true, 96 ExpectedErr: vmerrs.ErrWriteProtection.Error(), 97 }, 98 {{- end}} 99 "insufficient gas for {{decapitalise $func.Normalized.Name}} should fail": { 100 Caller: common.Address{1}, 101 InputFn: func(t testing.TB) []byte { 102 {{- if len $func.Normalized.Inputs | lt 1}} 103 // CUSTOM CODE STARTS HERE 104 // populate test input here 105 testInput := {{capitalise $func.Normalized.Name}}Input{} 106 input, err := Pack{{$func.Normalized.Name}}(testInput) 107 {{- else if len $func.Normalized.Inputs | eq 1 }} 108 {{- $input := index $func.Normalized.Inputs 0}} 109 // CUSTOM CODE STARTS HERE 110 // set test input to a value here 111 var testInput {{bindtype $input.Type $structs}} 112 input, err := Pack{{$func.Normalized.Name}}(testInput) 113 {{- else}} 114 input, err := Pack{{$func.Normalized.Name}}() 115 {{- end}} 116 require.NoError(t, err) 117 return input 118 }, 119 SuppliedGas: {{$func.Normalized.Name}}GasCost - 1, 120 ReadOnly: false, 121 ExpectedErr: vmerrs.ErrOutOfGas.Error(), 122 }, 123 {{- end}} 124 } 125 ) 126 127 // Test{{.Contract.Type}}Run tests the Run function of the precompile contract. 128 func Test{{.Contract.Type}}Run(t *testing.T) { 129 {{- if .Contract.AllowList}} 130 // Run tests with allowlist tests. 131 // This adds allowlist tests to your custom tests 132 // and runs them all together. 133 // Even if you don't add any custom tests, keep this. This will still 134 // run the default allowlist tests. 135 allowlist.RunPrecompileWithAllowListTests(t, Module, state.NewTestStateDB, tests) 136 {{- else}} 137 // Run tests. 138 for name, test := range tests { 139 t.Run(name, func(t *testing.T) { 140 test.Run(t, Module, state.NewTestStateDB(t)) 141 }) 142 } 143 {{- end}} 144 } 145 146 func Benchmark{{.Contract.Type}}(b *testing.B) { 147 {{- if .Contract.AllowList}} 148 // Benchmark tests with allowlist tests. 149 // This adds allowlist tests to your custom tests 150 // and benchmarks them all together. 151 // Even if you don't add any custom tests, keep this. This will still 152 // run the default allowlist tests. 153 allowlist.BenchPrecompileWithAllowList(b, Module, state.NewTestStateDB, tests) 154 {{- else}} 155 // Benchmark tests. 156 for name, test := range tests { 157 b.Run(name, func(b *testing.B) { 158 test.Bench(b, module, newStateDB(b)) 159 }) 160 } 161 {{- end}} 162 } 163 164 `