github.com/tuotoo/go-ethereum@v1.7.4-0.20171121184211-049797d40a24/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 "math/big" 22 "strings" 23 "testing" 24 ) 25 26 func TestBytesConversion(t *testing.T) { 27 bytes := []byte{5} 28 hash := BytesToHash(bytes) 29 30 var exp Hash 31 exp[31] = 5 32 33 if hash != exp { 34 t.Errorf("expected %x got %x", exp, hash) 35 } 36 } 37 38 func TestHashJsonValidation(t *testing.T) { 39 var tests = []struct { 40 Prefix string 41 Size int 42 Error string 43 }{ 44 {"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"}, 45 {"0x", 66, "hex string has length 66, want 64 for common.Hash"}, 46 {"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"}, 47 {"0x", 0, "hex string has length 0, want 64 for common.Hash"}, 48 {"0x", 64, ""}, 49 {"0X", 64, ""}, 50 } 51 for _, test := range tests { 52 input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"` 53 var v Hash 54 err := json.Unmarshal([]byte(input), &v) 55 if err == nil { 56 if test.Error != "" { 57 t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error) 58 } 59 } else { 60 if err.Error() != test.Error { 61 t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error) 62 } 63 } 64 } 65 } 66 67 func TestAddressUnmarshalJSON(t *testing.T) { 68 var tests = []struct { 69 Input string 70 ShouldErr bool 71 Output *big.Int 72 }{ 73 {"", true, nil}, 74 {`""`, true, nil}, 75 {`"0x"`, true, nil}, 76 {`"0x00"`, true, nil}, 77 {`"0xG000000000000000000000000000000000000000"`, true, nil}, 78 {`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)}, 79 {`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)}, 80 } 81 for i, test := range tests { 82 var v Address 83 err := json.Unmarshal([]byte(test.Input), &v) 84 if err != nil && !test.ShouldErr { 85 t.Errorf("test #%d: unexpected error: %v", i, err) 86 } 87 if err == nil { 88 if test.ShouldErr { 89 t.Errorf("test #%d: expected error, got none", i) 90 } 91 if v.Big().Cmp(test.Output) != 0 { 92 t.Errorf("test #%d: address mismatch: have %v, want %v", i, v.Big(), test.Output) 93 } 94 } 95 } 96 } 97 98 func TestAddressHexChecksum(t *testing.T) { 99 var tests = []struct { 100 Input string 101 Output string 102 }{ 103 // Test cases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification 104 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"}, 105 {"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"}, 106 {"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"}, 107 {"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"}, 108 // Ensure that non-standard length input values are handled correctly 109 {"0xa", "0x000000000000000000000000000000000000000A"}, 110 {"0x0a", "0x000000000000000000000000000000000000000A"}, 111 {"0x00a", "0x000000000000000000000000000000000000000A"}, 112 {"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"}, 113 } 114 for i, test := range tests { 115 output := HexToAddress(test.Input).Hex() 116 if output != test.Output { 117 t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output) 118 } 119 } 120 } 121 122 func BenchmarkAddressHex(b *testing.B) { 123 testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") 124 for n := 0; n < b.N; n++ { 125 testAddr.Hex() 126 } 127 }