gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/types_test.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain 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 TestIsHexAddress(t *testing.T) { 39 tests := []struct { 40 str string 41 exp bool 42 }{ 43 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 44 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 45 {"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 46 {"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 47 {"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 48 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false}, 49 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false}, 50 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false}, 51 {"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false}, 52 } 53 54 for _, test := range tests { 55 if result := IsHexAddress(test.str); result != test.exp { 56 t.Errorf("IsHexAddress(%s) == %v; expected %v", 57 test.str, result, test.exp) 58 } 59 } 60 } 61 62 func TestHashJsonValidation(t *testing.T) { 63 var tests = []struct { 64 Prefix string 65 Size int 66 Error string 67 }{ 68 {"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"}, 69 {"0x", 66, "hex string has length 66, want 64 for common.Hash"}, 70 {"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"}, 71 {"0x", 0, "hex string has length 0, want 64 for common.Hash"}, 72 {"0x", 64, ""}, 73 {"0X", 64, ""}, 74 } 75 for _, test := range tests { 76 input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"` 77 var v Hash 78 err := json.Unmarshal([]byte(input), &v) 79 if err == nil { 80 if test.Error != "" { 81 t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error) 82 } 83 } else { 84 if err.Error() != test.Error { 85 t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error) 86 } 87 } 88 } 89 } 90 91 func TestAddressUnmarshalJSON(t *testing.T) { 92 var tests = []struct { 93 Input string 94 ShouldErr bool 95 Output *big.Int 96 }{ 97 {"", true, nil}, 98 {`""`, true, nil}, 99 {`"0x"`, true, nil}, 100 {`"0x00"`, true, nil}, 101 {`"0xG000000000000000000000000000000000000000"`, true, nil}, 102 {`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)}, 103 {`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)}, 104 } 105 for i, test := range tests { 106 var v Address 107 err := json.Unmarshal([]byte(test.Input), &v) 108 if err != nil && !test.ShouldErr { 109 t.Errorf("test #%d: unexpected error: %v", i, err) 110 } 111 if err == nil { 112 if test.ShouldErr { 113 t.Errorf("test #%d: expected error, got none", i) 114 } 115 if v.Big().Cmp(test.Output) != 0 { 116 t.Errorf("test #%d: address mismatch: have %v, want %v", i, v.Big(), test.Output) 117 } 118 } 119 } 120 } 121 122 func TestAddressHexChecksum(t *testing.T) { 123 var tests = []struct { 124 Input string 125 Output string 126 }{ 127 // Test cases from https://github.com/aquanetwork/EIPs/blob/master/EIPS/eip-55.md#specification 128 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"}, 129 {"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"}, 130 {"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"}, 131 {"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"}, 132 // Ensure that non-standard length input values are handled correctly 133 {"0xa", "0x000000000000000000000000000000000000000A"}, 134 {"0x0a", "0x000000000000000000000000000000000000000A"}, 135 {"0x00a", "0x000000000000000000000000000000000000000A"}, 136 {"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"}, 137 } 138 for i, test := range tests { 139 output := HexToAddress(test.Input).Hex() 140 if output != test.Output { 141 t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output) 142 } 143 } 144 } 145 146 func BenchmarkAddressHex(b *testing.B) { 147 testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") 148 for n := 0; n < b.N; n++ { 149 testAddr.Hex() 150 } 151 }