github.com/ethereum/go-ethereum@v1.16.1/core/vm/memory_test.go (about)

     1  // Copyright 2023 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vm
    18  
    19  import (
    20  	"bytes"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  )
    26  
    27  func TestMemoryCopy(t *testing.T) {
    28  	// Test cases from https://eips.ethereum.org/EIPS/eip-5656#test-cases
    29  	for i, tc := range []struct {
    30  		dst, src, len uint64
    31  		pre           string
    32  		want          string
    33  	}{
    34  		{ // MCOPY 0 32 32 - copy 32 bytes from offset 32 to offset 0.
    35  			0, 32, 32,
    36  			"0000000000000000000000000000000000000000000000000000000000000000 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
    37  			"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
    38  		},
    39  
    40  		{ // MCOPY 0 0 32 - copy 32 bytes from offset 0 to offset 0.
    41  			0, 0, 32,
    42  			"0101010101010101010101010101010101010101010101010101010101010101",
    43  			"0101010101010101010101010101010101010101010101010101010101010101",
    44  		},
    45  		{ // MCOPY 0 1 8 - copy 8 bytes from offset 1 to offset 0 (overlapping).
    46  			0, 1, 8,
    47  			"000102030405060708 000000000000000000000000000000000000000000000000",
    48  			"010203040506070808 000000000000000000000000000000000000000000000000",
    49  		},
    50  		{ // MCOPY 1 0 8 - copy 8 bytes from offset 0 to offset 1 (overlapping).
    51  			1, 0, 8,
    52  			"000102030405060708 000000000000000000000000000000000000000000000000",
    53  			"000001020304050607 000000000000000000000000000000000000000000000000",
    54  		},
    55  		// Tests below are not in the EIP, but maybe should be added
    56  		{ // MCOPY 0xFFFFFFFFFFFF 0xFFFFFFFFFFFF 0 - copy zero bytes from out-of-bounds index(overlapping).
    57  			0xFFFFFFFFFFFF, 0xFFFFFFFFFFFF, 0,
    58  			"11",
    59  			"11",
    60  		},
    61  		{ // MCOPY 0xFFFFFFFFFFFF 0 0 - copy zero bytes from start of mem to out-of-bounds.
    62  			0xFFFFFFFFFFFF, 0, 0,
    63  			"11",
    64  			"11",
    65  		},
    66  		{ // MCOPY 0 0xFFFFFFFFFFFF 0 - copy zero bytes from out-of-bounds to start of mem
    67  			0, 0xFFFFFFFFFFFF, 0,
    68  			"11",
    69  			"11",
    70  		},
    71  	} {
    72  		m := NewMemory()
    73  		// Clean spaces
    74  		data := common.FromHex(strings.ReplaceAll(tc.pre, " ", ""))
    75  		// Set pre
    76  		m.Resize(uint64(len(data)))
    77  		m.Set(0, uint64(len(data)), data)
    78  		// Do the copy
    79  		m.Copy(tc.dst, tc.src, tc.len)
    80  		want := common.FromHex(strings.ReplaceAll(tc.want, " ", ""))
    81  		if have := m.store; !bytes.Equal(want, have) {
    82  			t.Errorf("case %d: want: %#x\nhave: %#x\n", i, want, have)
    83  		}
    84  	}
    85  }