github.com/nnlgsakib/mind-dpos@v0.0.0-20230606105614-f3c8ca06f808/common/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 common 18 19 import ( 20 "encoding/json" 21 "github.com/TTCECO/gttc/common/hexutil" 22 23 "math/big" 24 "strings" 25 "testing" 26 ) 27 28 func TestBytesConversion(t *testing.T) { 29 bytes := []byte{5} 30 hash := BytesToHash(bytes) 31 32 var exp Hash 33 exp[31] = 5 34 35 if hash != exp { 36 t.Errorf("expected %x got %x", exp, hash) 37 } 38 } 39 40 func TestIsHexAddress(t *testing.T) { 41 tests := []struct { 42 str string 43 exp bool 44 }{ 45 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 46 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 47 {"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 48 {"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 49 {"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 50 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false}, 51 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false}, 52 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false}, 53 {"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false}, 54 } 55 56 for _, test := range tests { 57 if result := IsHexAddress(test.str); result != test.exp { 58 t.Errorf("IsHexAddress(%s) == %v; expected %v", 59 test.str, result, test.exp) 60 } 61 } 62 } 63 64 func TestHashJsonValidation(t *testing.T) { 65 var tests = []struct { 66 Prefix string 67 Size int 68 Error string 69 }{ 70 {"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"}, 71 {"0x", 66, "hex string has length 66, want 64 for common.Hash"}, 72 {"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"}, 73 {"0x", 0, "hex string has length 0, want 64 for common.Hash"}, 74 {"0x", 64, ""}, 75 {"0X", 64, ""}, 76 } 77 for _, test := range tests { 78 input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"` 79 var v Hash 80 err := json.Unmarshal([]byte(input), &v) 81 if err == nil { 82 if test.Error != "" { 83 t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error) 84 } 85 } else { 86 if err.Error() != test.Error { 87 t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error) 88 } 89 } 90 } 91 } 92 93 func TestAddressUnmarshalJSON(t *testing.T) { 94 var tests = []struct { 95 Input string 96 ShouldErr bool 97 Output *big.Int 98 }{ 99 {"", true, nil}, 100 {`""`, true, nil}, 101 {`"0x"`, true, nil}, 102 {`"0x00"`, true, nil}, 103 {`"0xG000000000000000000000000000000000000000"`, true, nil}, 104 {`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)}, 105 {`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)}, 106 } 107 for i, test := range tests { 108 var v Address 109 err := json.Unmarshal([]byte(test.Input), &v) 110 if err != nil && !test.ShouldErr { 111 t.Errorf("test #%d: unexpected error: %v", i, err) 112 } 113 if err == nil { 114 if test.ShouldErr { 115 t.Errorf("test #%d: expected error, got none", i) 116 } 117 if v.Big().Cmp(test.Output) != 0 { 118 t.Errorf("test #%d: address mismatch: have %v, want %v", i, v.Big(), test.Output) 119 } 120 } 121 } 122 } 123 124 func TestAddressHexChecksum(t *testing.T) { 125 // PM want to change hex prefix 0x to anything they want , so ... 126 127 var tests = []struct { 128 Input string 129 Output string 130 }{ 131 // Test cases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification 132 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"}, 133 {"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"}, 134 {"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"}, 135 {"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"}, 136 // Ensure that non-standard length input values are handled correctly 137 {"0xa", "0x000000000000000000000000000000000000000A"}, 138 {"0x0a", "0x000000000000000000000000000000000000000A"}, 139 {"0x00a", "0x000000000000000000000000000000000000000A"}, 140 {"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"}, 141 } 142 for i, test := range tests { 143 output := HexToAddress(hexutil.HexToCP(test.Input)).Hex() 144 if output != hexutil.HexToCP(test.Output) { 145 t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, hexutil.HexToCP(test.Output)) 146 } 147 } 148 } 149 150 func BenchmarkAddressHex(b *testing.B) { 151 testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") 152 for n := 0; n < b.N; n++ { 153 testAddr.Hex() 154 } 155 } 156 157 func TestMixedcaseAccount_Address(t *testing.T) { 158 159 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md 160 // Note: 0X{checksum_addr} is not valid according to spec above 161 162 var res []struct { 163 A MixedcaseAddress 164 Valid bool 165 } 166 if err := json.Unmarshal([]byte(`[ 167 {"A" : "0xae967917c465db8578ca9024c205720b1a3651A9", "Valid": false}, 168 {"A" : "0xAe967917c465db8578ca9024c205720b1a3651A9", "Valid": true}, 169 {"A" : "0XAe967917c465db8578ca9024c205720b1a3651A9", "Valid": false}, 170 {"A" : "0x1111111111111111111112222222222223333323", "Valid": true} 171 ]`), &res); err != nil { 172 t.Fatal(err) 173 } 174 175 for _, r := range res { 176 if got := r.A.ValidChecksum(); got != r.Valid { 177 t.Errorf("Expected checksum %v, got checksum %v, input %v", r.Valid, got, r.A.String()) 178 } 179 } 180 181 //These should throw exceptions: 182 var r2 []MixedcaseAddress 183 for _, r := range []string{ 184 `["0x11111111111111111111122222222222233333"]`, // Too short 185 `["0x111111111111111111111222222222222333332"]`, // Too short 186 `["0x11111111111111111111122222222222233333234"]`, // Too long 187 `["0x111111111111111111111222222222222333332344"]`, // Too long 188 `["1111111111111111111112222222222223333323"]`, // Missing 0x 189 `["x1111111111111111111112222222222223333323"]`, // Missing 0 190 `["0xG111111111111111111112222222222223333323"]`, //Non-hex 191 } { 192 if err := json.Unmarshal([]byte(r), &r2); err == nil { 193 t.Errorf("Expected failure, input %v", r) 194 } 195 196 } 197 198 }