github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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 "fmt" 22 "math/big" 23 "strings" 24 "testing" 25 ) 26 27 func TestBytesConversion(t *testing.T) { 28 bytes := []byte{5} 29 hash := BytesToHash(bytes) 30 31 var exp Hash 32 exp[31] = 5 33 34 if hash != exp { 35 t.Errorf("expected %x got %x", exp, hash) 36 } 37 } 38 39 func TestIsHexAddress(t *testing.T) { 40 tests := []struct { 41 str string 42 exp bool 43 }{ 44 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 45 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 46 {"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 47 {"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 48 {"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 49 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false}, 50 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false}, 51 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false}, 52 {"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false}, 53 } 54 55 for _, test := range tests { 56 if result := IsHexAddress(test.str); result != test.exp { 57 t.Errorf("IsHexAddress(%s) == %v; expected %v", 58 test.str, result, test.exp) 59 } 60 } 61 } 62 63 func TestHashJsonValidation(t *testing.T) { 64 var tests = []struct { 65 Prefix string 66 Size int 67 Error string 68 }{ 69 {"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"}, 70 {"0x", 66, "hex string has length 66, want 64 for common.Hash"}, 71 {"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"}, 72 {"0x", 0, "hex string has length 0, want 64 for common.Hash"}, 73 {"0x", 64, ""}, 74 {"0X", 64, ""}, 75 } 76 for _, test := range tests { 77 input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"` 78 var v Hash 79 err := json.Unmarshal([]byte(input), &v) 80 if err == nil { 81 if test.Error != "" { 82 t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error) 83 } 84 } else { 85 if err.Error() != test.Error { 86 t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error) 87 } 88 } 89 } 90 } 91 92 func TestAddressUnmarshalJSON(t *testing.T) { 93 var tests = []struct { 94 Input string 95 ShouldErr bool 96 Output *big.Int 97 }{ 98 {"", true, nil}, 99 {`""`, true, nil}, 100 {`"0x"`, true, nil}, 101 {`"0x00"`, true, nil}, 102 {`"0xG000000000000000000000000000000000000000"`, true, nil}, 103 {`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)}, 104 {`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)}, 105 } 106 for i, test := range tests { 107 var v Address 108 err := json.Unmarshal([]byte(test.Input), &v) 109 if err != nil && !test.ShouldErr { 110 t.Errorf("test #%d: unexpected error: %v", i, err) 111 } 112 if err == nil { 113 if test.ShouldErr { 114 t.Errorf("test #%d: expected error, got none", i) 115 } 116 if v.Big().Cmp(test.Output) != 0 { 117 t.Errorf("test #%d: address mismatch: have %v, want %v", i, v.Big(), test.Output) 118 } 119 } 120 } 121 } 122 123 func TestAddressHexChecksum(t *testing.T) { 124 var tests = []struct { 125 Input string 126 Output string 127 }{ 128 // Test cases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification 129 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"}, 130 {"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"}, 131 {"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"}, 132 {"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"}, 133 // Ensure that non-standard length input values are handled correctly 134 {"0xa", "0x000000000000000000000000000000000000000A"}, 135 {"0x0a", "0x000000000000000000000000000000000000000A"}, 136 {"0x00a", "0x000000000000000000000000000000000000000A"}, 137 {"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"}, 138 } 139 for i, test := range tests { 140 output := HexToAddress(test.Input).Hex() 141 if output != test.Output { 142 t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output) 143 } 144 } 145 } 146 147 func TestAddress_UnmarshalJSON(t *testing.T) { 148 addr1 := StringToAddress("34Sjxs9AwRj5mCyx7AfxJuZx5f91BqJ533") 149 //addr1 := []byte("0x970e8128ab834e8eac17ab8e3812f010678cf791") 150 addr2 := Address{} 151 addrB := []byte(`"`) 152 //addr0x := []byte(`0x`) 153 fmt.Printf("addrB=%v\n", addrB) 154 //addrC := append(addrB, addr0x...) 155 addrC := append(addrB, addr1[:]...) 156 addrC = append(addrC, addrB...) 157 err := addr2.UnmarshalJSON(addrC[:]) 158 if err != nil { 159 t.Error(err) 160 } 161 fmt.Printf("addr2=%v\n", addr2) 162 } 163 164 func BenchmarkAddressHex(b *testing.B) { 165 testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") 166 for n := 0; n < b.N; n++ { 167 testAddr.Hex() 168 } 169 }