github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/execution/evm/code_test.go (about)

     1  package evm
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hyperledger/burrow/execution/evm/asm"
     9  	"github.com/hyperledger/burrow/execution/evm/asm/bc"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/tmthrgd/go-bitset"
    12  )
    13  
    14  func TestOpcodeBitset(t *testing.T) {
    15  	tests := []struct {
    16  		name   string
    17  		code   []byte
    18  		bitset bitset.Bitset
    19  	}{
    20  		{
    21  			name:   "Only one real JUMPDEST",
    22  			code:   bc.MustSplice(asm.PUSH2, 1, asm.JUMPDEST, asm.ADD, asm.JUMPDEST),
    23  			bitset: mkBitset("10011"),
    24  		},
    25  		{
    26  			name:   "Two JUMPDESTs",
    27  			code:   bc.MustSplice(asm.PUSH1, 1, asm.JUMPDEST, asm.ADD, asm.JUMPDEST),
    28  			bitset: mkBitset("10111"),
    29  		},
    30  		{
    31  			name:   "One PUSH",
    32  			code:   bc.MustSplice(asm.PUSH4, asm.JUMPDEST, asm.ADD, asm.JUMPDEST, asm.PUSH32, asm.BALANCE),
    33  			bitset: mkBitset("100001"),
    34  		},
    35  		{
    36  			name:   "Two PUSHes",
    37  			code:   bc.MustSplice(asm.PUSH3, asm.JUMPDEST, asm.ADD, asm.JUMPDEST, asm.PUSH32, asm.BALANCE),
    38  			bitset: mkBitset("100010"),
    39  		},
    40  		{
    41  			name:   "Three PUSHes",
    42  			code:   bc.MustSplice(asm.PUSH3, asm.JUMPDEST, asm.ADD, asm.PUSH2, asm.PUSH32, asm.BALANCE),
    43  			bitset: mkBitset("100010"),
    44  		},
    45  		{
    46  			name:   "No PUSH",
    47  			code:   bc.MustSplice(asm.JUMPDEST, asm.ADD, asm.BALANCE),
    48  			bitset: mkBitset("111"),
    49  		},
    50  		{
    51  			name:   "End PUSH",
    52  			code:   bc.MustSplice(asm.JUMPDEST, asm.ADD, asm.PUSH6),
    53  			bitset: mkBitset("111"),
    54  		},
    55  		{
    56  			name:   "Middle PUSH",
    57  			code:   bc.MustSplice(asm.JUMPDEST, asm.PUSH2, asm.PUSH1, asm.PUSH2, asm.BLOCKHASH),
    58  			bitset: mkBitset("11001"),
    59  		},
    60  	}
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			if got := opcodeBitset(tt.code); !reflect.DeepEqual(got, tt.bitset) {
    64  				t.Errorf("opcodeBitset() = %v, want %v", got, tt.bitset)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func TestCode_IsOpcodeAt(t *testing.T) {
    71  	code := NewCode(bc.MustSplice(asm.PUSH2, 2, 3))
    72  
    73  	assert.True(t, code.IsOpcode(0))
    74  	assert.False(t, code.IsOpcode(1))
    75  	assert.False(t, code.IsOpcode(2))
    76  	assert.False(t, code.IsOpcode(3))
    77  }
    78  
    79  func mkBitset(binaryString string) bitset.Bitset {
    80  	length := uint(len(binaryString))
    81  	bs := bitset.New(length)
    82  	for i := uint(0); i < length; i++ {
    83  		switch binaryString[i] {
    84  		case '1':
    85  			bs.Set(i)
    86  		case '0':
    87  		case ' ':
    88  			i++
    89  		default:
    90  			panic(fmt.Errorf("mkBitset() expects a string containing only 1s, 0s, and spaces"))
    91  		}
    92  	}
    93  	return bs
    94  }