github.com/klaytn/klaytn@v1.12.1/networks/rpc/types_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from rpc/types_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package rpc 22 23 import ( 24 "encoding/json" 25 "testing" 26 27 "github.com/klaytn/klaytn/common" 28 "github.com/klaytn/klaytn/common/math" 29 ) 30 31 func TestBlockNumberJSONUnmarshal(t *testing.T) { 32 tests := []struct { 33 input string 34 mustFail bool 35 expected BlockNumber 36 }{ 37 0: {`"0x"`, true, BlockNumber(0)}, 38 1: {`"0x0"`, false, BlockNumber(0)}, 39 2: {`"0X1"`, false, BlockNumber(1)}, 40 3: {`"0x00"`, true, BlockNumber(0)}, 41 4: {`"0x01"`, true, BlockNumber(0)}, 42 5: {`"0x1"`, false, BlockNumber(1)}, 43 6: {`"0x12"`, false, BlockNumber(18)}, 44 7: {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)}, 45 8: {`"0x8000000000000000"`, true, BlockNumber(0)}, 46 9: {`"0"`, true, BlockNumber(0)}, 47 10: {`"ff"`, true, BlockNumber(0)}, 48 11: {`"pending"`, false, PendingBlockNumber}, 49 12: {`"latest"`, false, LatestBlockNumber}, 50 13: {`"earliest"`, false, EarliestBlockNumber}, 51 14: {`someString`, true, BlockNumber(0)}, 52 15: {`""`, true, BlockNumber(0)}, 53 16: {``, true, BlockNumber(0)}, 54 17: {"0", false, BlockNumber(0)}, 55 18: {"1", false, BlockNumber(1)}, 56 19: {"10", false, BlockNumber(10)}, 57 20: {"80000000", false, BlockNumber(80000000)}, 58 21: {"-1", true, BlockNumber(0)}, 59 } 60 61 for i, test := range tests { 62 var num BlockNumber 63 err := json.Unmarshal([]byte(test.input), &num) 64 if test.mustFail && err == nil { 65 t.Errorf("Test %d should fail", i) 66 continue 67 } 68 if !test.mustFail && err != nil { 69 t.Errorf("Test %d should pass but got err: %v", i, err) 70 continue 71 } 72 if num != test.expected { 73 t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num) 74 } 75 } 76 } 77 78 func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) { 79 tests := []struct { 80 input string 81 mustFail bool 82 expected BlockNumberOrHash 83 }{ 84 0: {`"0x"`, true, BlockNumberOrHash{}}, 85 1: {`"0x0"`, false, NewBlockNumberOrHashWithNumber(0)}, 86 2: {`"0X1"`, false, NewBlockNumberOrHashWithNumber(1)}, 87 3: {`"0x00"`, true, BlockNumberOrHash{}}, 88 4: {`"0x01"`, true, BlockNumberOrHash{}}, 89 5: {`"0x1"`, false, NewBlockNumberOrHashWithNumber(1)}, 90 6: {`"0x12"`, false, NewBlockNumberOrHashWithNumber(18)}, 91 7: {`"0x7fffffffffffffff"`, false, NewBlockNumberOrHashWithNumber(math.MaxInt64)}, 92 8: {`"0x8000000000000000"`, true, BlockNumberOrHash{}}, 93 9: {`"0"`, true, BlockNumberOrHash{}}, 94 10: {`"ff"`, true, BlockNumberOrHash{}}, 95 11: {`"pending"`, false, NewBlockNumberOrHashWithNumber(PendingBlockNumber)}, 96 12: {`"latest"`, false, NewBlockNumberOrHashWithNumber(LatestBlockNumber)}, 97 13: {`"earliest"`, false, NewBlockNumberOrHashWithNumber(EarliestBlockNumber)}, 98 14: {`someString`, true, BlockNumberOrHash{}}, 99 15: {`""`, true, BlockNumberOrHash{}}, 100 16: {``, true, BlockNumberOrHash{}}, 101 17: {`"0x0000000000000000000000000000000000000000000000000000000000000000"`, false, NewBlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)}, 102 18: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, false, NewBlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)}, 103 19: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":false}`, false, NewBlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)}, 104 20: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":true}`, false, NewBlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), true)}, 105 21: {`{"blockNumber":"0x1"}`, false, NewBlockNumberOrHashWithNumber(1)}, 106 22: {`{"blockNumber":"pending"}`, false, NewBlockNumberOrHashWithNumber(PendingBlockNumber)}, 107 23: {`{"blockNumber":"latest"}`, false, NewBlockNumberOrHashWithNumber(LatestBlockNumber)}, 108 24: {`{"blockNumber":"earliest"}`, false, NewBlockNumberOrHashWithNumber(EarliestBlockNumber)}, 109 25: {`{"blockNumber":"0x1", "blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, true, BlockNumberOrHash{}}, 110 } 111 112 for i, test := range tests { 113 var bnh BlockNumberOrHash 114 err := json.Unmarshal([]byte(test.input), &bnh) 115 if test.mustFail && err == nil { 116 t.Errorf("Test %d should fail", i) 117 continue 118 } 119 if !test.mustFail && err != nil { 120 t.Errorf("Test %d should pass but got err: %v", i, err) 121 continue 122 } 123 hash, hashOk := bnh.Hash() 124 expectedHash, expectedHashOk := test.expected.Hash() 125 num, numOk := bnh.Number() 126 expectedNum, expectedNumOk := test.expected.Number() 127 if bnh.RequireCanonical != test.expected.RequireCanonical || 128 hash != expectedHash || hashOk != expectedHashOk || 129 num != expectedNum || numOk != expectedNumOk { 130 t.Errorf("Test %d got unexpected value, want %v, got %v", i, test.expected, bnh) 131 } 132 } 133 }