github.com/devintegral3/go-ethereum@v1.9.7-0.20191031044858-21506be82b68/rpc/types_test.go (about) 1 // Copyright 2015 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 rpc 18 19 import ( 20 "encoding/json" 21 "testing" 22 23 "github.com/Fantom-foundation/go-ethereum/common" 24 "github.com/Fantom-foundation/go-ethereum/common/math" 25 ) 26 27 func TestBlockNumberJSONUnmarshal(t *testing.T) { 28 tests := []struct { 29 input string 30 mustFail bool 31 expected BlockNumber 32 }{ 33 0: {`"0x"`, true, BlockNumber(0)}, 34 1: {`"0x0"`, false, BlockNumber(0)}, 35 2: {`"0X1"`, false, BlockNumber(1)}, 36 3: {`"0x00"`, true, BlockNumber(0)}, 37 4: {`"0x01"`, true, BlockNumber(0)}, 38 5: {`"0x1"`, false, BlockNumber(1)}, 39 6: {`"0x12"`, false, BlockNumber(18)}, 40 7: {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)}, 41 8: {`"0x8000000000000000"`, true, BlockNumber(0)}, 42 9: {"0", true, BlockNumber(0)}, 43 10: {`"ff"`, true, BlockNumber(0)}, 44 11: {`"pending"`, false, PendingBlockNumber}, 45 12: {`"latest"`, false, LatestBlockNumber}, 46 13: {`"earliest"`, false, EarliestBlockNumber}, 47 14: {`someString`, true, BlockNumber(0)}, 48 15: {`""`, true, BlockNumber(0)}, 49 16: {``, true, BlockNumber(0)}, 50 } 51 52 for i, test := range tests { 53 var num BlockNumber 54 err := json.Unmarshal([]byte(test.input), &num) 55 if test.mustFail && err == nil { 56 t.Errorf("Test %d should fail", i) 57 continue 58 } 59 if !test.mustFail && err != nil { 60 t.Errorf("Test %d should pass but got err: %v", i, err) 61 continue 62 } 63 if num != test.expected { 64 t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num) 65 } 66 } 67 } 68 69 func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) { 70 tests := []struct { 71 input string 72 mustFail bool 73 expected BlockNumberOrHash 74 }{ 75 0: {`"0x"`, true, BlockNumberOrHash{}}, 76 1: {`"0x0"`, false, BlockNumberOrHashWithNumber(0)}, 77 2: {`"0X1"`, false, BlockNumberOrHashWithNumber(1)}, 78 3: {`"0x00"`, true, BlockNumberOrHash{}}, 79 4: {`"0x01"`, true, BlockNumberOrHash{}}, 80 5: {`"0x1"`, false, BlockNumberOrHashWithNumber(1)}, 81 6: {`"0x12"`, false, BlockNumberOrHashWithNumber(18)}, 82 7: {`"0x7fffffffffffffff"`, false, BlockNumberOrHashWithNumber(math.MaxInt64)}, 83 8: {`"0x8000000000000000"`, true, BlockNumberOrHash{}}, 84 9: {"0", true, BlockNumberOrHash{}}, 85 10: {`"ff"`, true, BlockNumberOrHash{}}, 86 11: {`"pending"`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)}, 87 12: {`"latest"`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)}, 88 13: {`"earliest"`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)}, 89 14: {`someString`, true, BlockNumberOrHash{}}, 90 15: {`""`, true, BlockNumberOrHash{}}, 91 16: {``, true, BlockNumberOrHash{}}, 92 17: {`"0x0000000000000000000000000000000000000000000000000000000000000000"`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)}, 93 18: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)}, 94 19: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":false}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)}, 95 20: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":true}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), true)}, 96 21: {`{"blockNumber":"0x1"}`, false, BlockNumberOrHashWithNumber(1)}, 97 22: {`{"blockNumber":"pending"}`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)}, 98 23: {`{"blockNumber":"latest"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)}, 99 24: {`{"blockNumber":"earliest"}`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)}, 100 25: {`{"blockNumber":"0x1", "blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, true, BlockNumberOrHash{}}, 101 } 102 103 for i, test := range tests { 104 var bnh BlockNumberOrHash 105 err := json.Unmarshal([]byte(test.input), &bnh) 106 if test.mustFail && err == nil { 107 t.Errorf("Test %d should fail", i) 108 continue 109 } 110 if !test.mustFail && err != nil { 111 t.Errorf("Test %d should pass but got err: %v", i, err) 112 continue 113 } 114 hash, hashOk := bnh.Hash() 115 expectedHash, expectedHashOk := test.expected.Hash() 116 num, numOk := bnh.Number() 117 expectedNum, expectedNumOk := test.expected.Number() 118 if bnh.RequireCanonical != test.expected.RequireCanonical || 119 hash != expectedHash || hashOk != expectedHashOk || 120 num != expectedNum || numOk != expectedNumOk { 121 t.Errorf("Test %d got unexpected value, want %v, got %v", i, test.expected, bnh) 122 } 123 } 124 }