github.com/flashbots/go-ethereum@v1.9.7/mobile/interface_test.go (about)

     1  // Copyright 2019 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 geth
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  )
    27  
    28  func TestInterfaceGetSet(t *testing.T) {
    29  	var tests = []struct {
    30  		method string
    31  		input  interface{}
    32  		expect interface{}
    33  	}{
    34  		{"Bool", true, true},
    35  		{"Bool", false, false},
    36  		{"Bools", &Bools{[]bool{false, true}}, &Bools{[]bool{false, true}}},
    37  		{"String", "go-ethereum", "go-ethereum"},
    38  		{"Strings", &Strings{strs: []string{"hello", "world"}}, &Strings{strs: []string{"hello", "world"}}},
    39  		{"Binary", []byte{0x01, 0x02}, []byte{0x01, 0x02}},
    40  		{"Binaries", &Binaries{[][]byte{{0x01, 0x02}, {0x03, 0x04}}}, &Binaries{[][]byte{{0x01, 0x02}, {0x03, 0x04}}}},
    41  		{"Address", &Address{common.HexToAddress("deadbeef")}, &Address{common.HexToAddress("deadbeef")}},
    42  		{"Addresses", &Addresses{[]common.Address{common.HexToAddress("deadbeef"), common.HexToAddress("cafebabe")}}, &Addresses{[]common.Address{common.HexToAddress("deadbeef"), common.HexToAddress("cafebabe")}}},
    43  		{"Hash", &Hash{common.HexToHash("deadbeef")}, &Hash{common.HexToHash("deadbeef")}},
    44  		{"Hashes", &Hashes{[]common.Hash{common.HexToHash("deadbeef"), common.HexToHash("cafebabe")}}, &Hashes{[]common.Hash{common.HexToHash("deadbeef"), common.HexToHash("cafebabe")}}},
    45  		{"Int8", int8(1), int8(1)},
    46  		{"Int16", int16(1), int16(1)},
    47  		{"Int32", int32(1), int32(1)},
    48  		{"Int64", int64(1), int64(1)},
    49  		{"Int8s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
    50  		{"Int16s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
    51  		{"Int32s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
    52  		{"Int64s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
    53  		{"Uint8", NewBigInt(1), NewBigInt(1)},
    54  		{"Uint16", NewBigInt(1), NewBigInt(1)},
    55  		{"Uint32", NewBigInt(1), NewBigInt(1)},
    56  		{"Uint64", NewBigInt(1), NewBigInt(1)},
    57  		{"Uint8s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
    58  		{"Uint16s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
    59  		{"Uint32s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
    60  		{"Uint64s", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
    61  		{"BigInt", NewBigInt(1), NewBigInt(1)},
    62  		{"BigInts", &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}, &BigInts{[]*big.Int{big.NewInt(1), big.NewInt(2)}}},
    63  	}
    64  
    65  	args := NewInterfaces(len(tests))
    66  
    67  	callFn := func(receiver interface{}, method string, arg interface{}) interface{} {
    68  		rval := reflect.ValueOf(receiver)
    69  		rval.MethodByName(fmt.Sprintf("Set%s", method)).Call([]reflect.Value{reflect.ValueOf(arg)})
    70  		res := rval.MethodByName(fmt.Sprintf("Get%s", method)).Call(nil)
    71  		if len(res) > 0 {
    72  			return res[0].Interface()
    73  		}
    74  		return nil
    75  	}
    76  
    77  	for index, c := range tests {
    78  		// In theory the change of iface shouldn't effect the args value
    79  		iface, _ := args.Get(index)
    80  		result := callFn(iface, c.method, c.input)
    81  		if !reflect.DeepEqual(result, c.expect) {
    82  			t.Errorf("Interface get/set mismatch, want %v, got %v", c.expect, result)
    83  		}
    84  		// Check whether the underlying value in args is still zero
    85  		iface, _ = args.Get(index)
    86  		if iface.object != nil {
    87  			t.Error("Get operation is not write safe")
    88  		}
    89  	}
    90  }