interface_test.go 4.2 KB

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