github.com/core-coin/go-core/v2@v2.1.9/xcb/tracers/tracer_test.go (about) 1 // Copyright 2017 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-core library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package tracers 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "errors" 23 "math/big" 24 "testing" 25 "time" 26 27 "github.com/core-coin/go-core/v2/common" 28 "github.com/core-coin/go-core/v2/core/state" 29 "github.com/core-coin/go-core/v2/core/vm" 30 "github.com/core-coin/go-core/v2/params" 31 ) 32 33 type account struct{} 34 35 func (account) SubBalance(amount *big.Int) {} 36 func (account) AddBalance(amount *big.Int) {} 37 func (account) SetAddress(common.Address) {} 38 func (account) Value() *big.Int { return nil } 39 func (account) SetBalance(*big.Int) {} 40 func (account) SetNonce(uint64) {} 41 func (account) Balance() *big.Int { return nil } 42 func (account) Address() common.Address { return common.Address{} } 43 func (account) ReturnEnergy(*big.Int) {} 44 func (account) SetCode(common.Hash, []byte) {} 45 func (account) ForEachStorage(cb func(key, value common.Hash) bool) {} 46 47 type dummyStatedb struct { 48 state.StateDB 49 } 50 51 func (*dummyStatedb) GetRefund() uint64 { return 1337 } 52 53 func runTrace(tracer *Tracer) (json.RawMessage, error) { 54 env := vm.NewCVM(vm.BlockContext{BlockNumber: big.NewInt(1)}, vm.TxContext{}, &dummyStatedb{}, params.MainnetChainConfig, vm.Config{Debug: true, Tracer: tracer}) 55 56 contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000) 57 contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} 58 59 _, err := env.Interpreter().Run(contract, []byte{}, false) 60 if err != nil { 61 return nil, err 62 } 63 return tracer.GetResult() 64 } 65 66 // TestRegressionPanicSlice tests that we don't panic on bad arguments to memory access 67 func TestRegressionPanicSlice(t *testing.T) { 68 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.memory.slice(-1,-2)); }, fault: function() {}, result: function() { return this.depths; }}") 69 if err != nil { 70 t.Fatal(err) 71 } 72 if _, err = runTrace(tracer); err != nil { 73 t.Fatal(err) 74 } 75 } 76 77 // TestRegressionPanicSlice tests that we don't panic on bad arguments to stack peeks 78 func TestRegressionPanicPeek(t *testing.T) { 79 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.peek(-1)); }, fault: function() {}, result: function() { return this.depths; }}") 80 if err != nil { 81 t.Fatal(err) 82 } 83 if _, err = runTrace(tracer); err != nil { 84 t.Fatal(err) 85 } 86 } 87 88 // TestRegressionPanicSlice tests that we don't panic on bad arguments to memory getUint 89 func TestRegressionPanicGetUint(t *testing.T) { 90 tracer, err := New("{ depths: [], step: function(log, db) { this.depths.push(log.memory.getUint(-64));}, fault: function() {}, result: function() { return this.depths; }}") 91 if err != nil { 92 t.Fatal(err) 93 } 94 if _, err = runTrace(tracer); err != nil { 95 t.Fatal(err) 96 } 97 } 98 99 func TestTracing(t *testing.T) { 100 tracer, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}") 101 if err != nil { 102 t.Fatal(err) 103 } 104 105 ret, err := runTrace(tracer) 106 if err != nil { 107 t.Fatal(err) 108 } 109 if !bytes.Equal(ret, []byte("3")) { 110 t.Errorf("Expected return value to be 3, got %s", string(ret)) 111 } 112 } 113 114 func TestStack(t *testing.T) { 115 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}") 116 if err != nil { 117 t.Fatal(err) 118 } 119 120 ret, err := runTrace(tracer) 121 if err != nil { 122 t.Fatal(err) 123 } 124 if !bytes.Equal(ret, []byte("[0,1,2]")) { 125 t.Errorf("Expected return value to be [0,1,2], got %s", string(ret)) 126 } 127 } 128 129 func TestOpcodes(t *testing.T) { 130 tracer, err := New("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}") 131 if err != nil { 132 t.Fatal(err) 133 } 134 135 ret, err := runTrace(tracer) 136 if err != nil { 137 t.Fatal(err) 138 } 139 if !bytes.Equal(ret, []byte("[\"PUSH1\",\"PUSH1\",\"STOP\"]")) { 140 t.Errorf("Expected return value to be [\"PUSH1\",\"PUSH1\",\"STOP\"], got %s", string(ret)) 141 } 142 } 143 144 func TestHalt(t *testing.T) { 145 t.Skip("duktape doesn't support abortion") 146 147 timeout := errors.New("stahp") 148 tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}") 149 if err != nil { 150 t.Fatal(err) 151 } 152 153 go func() { 154 time.Sleep(1 * time.Second) 155 tracer.Stop(timeout) 156 }() 157 158 if _, err = runTrace(tracer); err.Error() != "stahp in server-side tracer function 'step'" { 159 t.Errorf("Expected timeout error, got %v", err) 160 } 161 } 162 163 func TestHaltBetweenSteps(t *testing.T) { 164 tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}") 165 if err != nil { 166 t.Fatal(err) 167 } 168 169 env := vm.NewCVM(vm.BlockContext{BlockNumber: big.NewInt(1)}, vm.TxContext{}, &dummyStatedb{}, params.MainnetChainConfig, vm.Config{Debug: true, Tracer: tracer}) 170 contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0) 171 172 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, nil, nil, contract, 0, nil) 173 timeout := errors.New("stahp") 174 tracer.Stop(timeout) 175 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, nil, nil, contract, 0, nil) 176 177 if _, err := tracer.GetResult(); err.Error() != timeout.Error() { 178 t.Errorf("Expected timeout error, got %v", err) 179 } 180 }