github.com/ethereum/go-ethereum@v1.14.3/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 "reflect" 22 "testing" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/common/math" 26 ) 27 28 func TestBlockNumberJSONUnmarshal(t *testing.T) { 29 tests := []struct { 30 input string 31 mustFail bool 32 expected BlockNumber 33 }{ 34 0: {`"0x"`, true, BlockNumber(0)}, 35 1: {`"0x0"`, false, BlockNumber(0)}, 36 2: {`"0X1"`, false, BlockNumber(1)}, 37 3: {`"0x00"`, true, BlockNumber(0)}, 38 4: {`"0x01"`, true, BlockNumber(0)}, 39 5: {`"0x1"`, false, BlockNumber(1)}, 40 6: {`"0x12"`, false, BlockNumber(18)}, 41 7: {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)}, 42 8: {`"0x8000000000000000"`, true, BlockNumber(0)}, 43 9: {"0", true, BlockNumber(0)}, 44 10: {`"ff"`, true, BlockNumber(0)}, 45 11: {`"pending"`, false, PendingBlockNumber}, 46 12: {`"latest"`, false, LatestBlockNumber}, 47 13: {`"earliest"`, false, EarliestBlockNumber}, 48 14: {`"safe"`, false, SafeBlockNumber}, 49 15: {`"finalized"`, false, FinalizedBlockNumber}, 50 16: {`someString`, true, BlockNumber(0)}, 51 17: {`""`, true, BlockNumber(0)}, 52 18: {``, true, BlockNumber(0)}, 53 } 54 55 for i, test := range tests { 56 var num BlockNumber 57 err := json.Unmarshal([]byte(test.input), &num) 58 if test.mustFail && err == nil { 59 t.Errorf("Test %d should fail", i) 60 continue 61 } 62 if !test.mustFail && err != nil { 63 t.Errorf("Test %d should pass but got err: %v", i, err) 64 continue 65 } 66 if num != test.expected { 67 t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num) 68 } 69 } 70 } 71 72 func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) { 73 tests := []struct { 74 input string 75 mustFail bool 76 expected BlockNumberOrHash 77 }{ 78 0: {`"0x"`, true, BlockNumberOrHash{}}, 79 1: {`"0x0"`, false, BlockNumberOrHashWithNumber(0)}, 80 2: {`"0X1"`, false, BlockNumberOrHashWithNumber(1)}, 81 3: {`"0x00"`, true, BlockNumberOrHash{}}, 82 4: {`"0x01"`, true, BlockNumberOrHash{}}, 83 5: {`"0x1"`, false, BlockNumberOrHashWithNumber(1)}, 84 6: {`"0x12"`, false, BlockNumberOrHashWithNumber(18)}, 85 7: {`"0x7fffffffffffffff"`, false, BlockNumberOrHashWithNumber(math.MaxInt64)}, 86 8: {`"0x8000000000000000"`, true, BlockNumberOrHash{}}, 87 9: {"0", true, BlockNumberOrHash{}}, 88 10: {`"ff"`, true, BlockNumberOrHash{}}, 89 11: {`"pending"`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)}, 90 12: {`"latest"`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)}, 91 13: {`"earliest"`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)}, 92 14: {`"safe"`, false, BlockNumberOrHashWithNumber(SafeBlockNumber)}, 93 15: {`"finalized"`, false, BlockNumberOrHashWithNumber(FinalizedBlockNumber)}, 94 16: {`someString`, true, BlockNumberOrHash{}}, 95 17: {`""`, true, BlockNumberOrHash{}}, 96 18: {``, true, BlockNumberOrHash{}}, 97 19: {`"0x0000000000000000000000000000000000000000000000000000000000000000"`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)}, 98 20: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)}, 99 21: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":false}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)}, 100 22: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":true}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), true)}, 101 23: {`{"blockNumber":"0x1"}`, false, BlockNumberOrHashWithNumber(1)}, 102 24: {`{"blockNumber":"pending"}`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)}, 103 25: {`{"blockNumber":"latest"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)}, 104 26: {`{"blockNumber":"earliest"}`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)}, 105 27: {`{"blockNumber":"safe"}`, false, BlockNumberOrHashWithNumber(SafeBlockNumber)}, 106 28: {`{"blockNumber":"finalized"}`, false, BlockNumberOrHashWithNumber(FinalizedBlockNumber)}, 107 29: {`{"blockNumber":"0x1", "blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, true, BlockNumberOrHash{}}, 108 } 109 110 for i, test := range tests { 111 var bnh BlockNumberOrHash 112 err := json.Unmarshal([]byte(test.input), &bnh) 113 if test.mustFail && err == nil { 114 t.Errorf("Test %d should fail", i) 115 continue 116 } 117 if !test.mustFail && err != nil { 118 t.Errorf("Test %d should pass but got err: %v", i, err) 119 continue 120 } 121 hash, hashOk := bnh.Hash() 122 expectedHash, expectedHashOk := test.expected.Hash() 123 num, numOk := bnh.Number() 124 expectedNum, expectedNumOk := test.expected.Number() 125 if bnh.RequireCanonical != test.expected.RequireCanonical || 126 hash != expectedHash || hashOk != expectedHashOk || 127 num != expectedNum || numOk != expectedNumOk { 128 t.Errorf("Test %d got unexpected value, want %v, got %v", i, test.expected, bnh) 129 } 130 } 131 } 132 133 func TestBlockNumberOrHash_WithNumber_MarshalAndUnmarshal(t *testing.T) { 134 tests := []struct { 135 name string 136 number int64 137 }{ 138 {"max", math.MaxInt64}, 139 {"pending", int64(PendingBlockNumber)}, 140 {"latest", int64(LatestBlockNumber)}, 141 {"earliest", int64(EarliestBlockNumber)}, 142 {"safe", int64(SafeBlockNumber)}, 143 {"finalized", int64(FinalizedBlockNumber)}, 144 } 145 for _, test := range tests { 146 test := test 147 t.Run(test.name, func(t *testing.T) { 148 bnh := BlockNumberOrHashWithNumber(BlockNumber(test.number)) 149 marshalled, err := json.Marshal(bnh) 150 if err != nil { 151 t.Fatal("cannot marshal:", err) 152 } 153 var unmarshalled BlockNumberOrHash 154 err = json.Unmarshal(marshalled, &unmarshalled) 155 if err != nil { 156 t.Fatal("cannot unmarshal:", err) 157 } 158 if !reflect.DeepEqual(bnh, unmarshalled) { 159 t.Fatalf("wrong result: expected %v, got %v", bnh, unmarshalled) 160 } 161 }) 162 } 163 } 164 165 func TestBlockNumberOrHash_StringAndUnmarshal(t *testing.T) { 166 tests := []BlockNumberOrHash{ 167 BlockNumberOrHashWithNumber(math.MaxInt64), 168 BlockNumberOrHashWithNumber(PendingBlockNumber), 169 BlockNumberOrHashWithNumber(LatestBlockNumber), 170 BlockNumberOrHashWithNumber(EarliestBlockNumber), 171 BlockNumberOrHashWithNumber(SafeBlockNumber), 172 BlockNumberOrHashWithNumber(FinalizedBlockNumber), 173 BlockNumberOrHashWithNumber(32), 174 BlockNumberOrHashWithHash(common.Hash{0xaa}, false), 175 } 176 for _, want := range tests { 177 marshalled, _ := json.Marshal(want.String()) 178 var have BlockNumberOrHash 179 if err := json.Unmarshal(marshalled, &have); err != nil { 180 t.Fatalf("cannot unmarshal (%v): %v", string(marshalled), err) 181 } 182 if !reflect.DeepEqual(want, have) { 183 t.Fatalf("wrong result: have %v, want %v", have, want) 184 } 185 } 186 }