github.com/XinFinOrg/xdcchain@v1.1.0/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 "database/sql/driver" 21 "encoding/json" 22 "math/big" 23 "reflect" 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 {"xdc5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 46 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 47 {"XDC5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 48 {"XdcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 49 {"xdcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 50 {"xdc5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false}, 51 {"xdc5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false}, 52 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false}, 53 {"xdcxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", 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 {`"xdc"`, true, nil}, 107 {`"xdc00"`, true, nil}, 108 {`"xdcG000000000000000000000000000000000000000"`, true, nil}, 109 {`"xdc0000000000000000000000000000000000000000"`, false, big.NewInt(0)}, 110 {`"xdc0000000000000000000000000000000000000010"`, false, big.NewInt(16)}, 111 } 112 for i, test := range tests { 113 var v Address 114 err := json.Unmarshal([]byte(test.Input), &v) 115 if err != nil && !test.ShouldErr { 116 t.Errorf("test #%d: unexpected error: %v", i, err) 117 } 118 if err == nil { 119 if test.ShouldErr { 120 t.Errorf("test #%d: expected error, got none", i) 121 } 122 if v.Big().Cmp(test.Output) != 0 { 123 t.Errorf("test #%d: address mismatch: have %v, want %v", i, v.Big(), test.Output) 124 } 125 } 126 } 127 } 128 129 func TestAddressHexChecksum(t *testing.T) { 130 var tests = []struct { 131 Input string 132 Output string 133 }{ 134 // Test cases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification 135 {"xdc5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "xdc5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"}, 136 {"xdcfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "xdcfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"}, 137 {"xdcdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "xdcdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"}, 138 {"xdcd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "xdcD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"}, 139 // Ensure that non-standard length input values are handled correctly 140 {"0xa", "xdc000000000000000000000000000000000000000A"}, 141 {"0x0a", "xdc000000000000000000000000000000000000000A"}, 142 {"0x00a", "xdc000000000000000000000000000000000000000A"}, 143 {"0x000000000000000000000000000000000000000a", "xdc000000000000000000000000000000000000000A"}, 144 } 145 for i, test := range tests { 146 output := HexToAddress(test.Input).Hex() 147 if output != test.Output { 148 t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output) 149 } 150 } 151 } 152 153 func BenchmarkAddressHex(b *testing.B) { 154 testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") 155 for n := 0; n < b.N; n++ { 156 testAddr.Hex() 157 } 158 } 159 160 func TestMixedcaseAccount_Address(t *testing.T) { 161 162 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md 163 // Note: 0X{checksum_addr} is not valid according to spec above 164 165 var res []struct { 166 A MixedcaseAddress 167 Valid bool 168 } 169 if err := json.Unmarshal([]byte(`[ 170 {"A" : "Xdcae967917c465db8578ca9024c205720b1a3651A9", "Valid": false}, 171 {"A" : "xdcAe967917c465db8578ca9024c205720b1a3651A9", "Valid": true}, 172 {"A" : "xDcAe967917c465db8578ca9024c205720b1a3651A9", "Valid": false}, 173 {"A" : "xdc1111111111111111111112222222222223333323", "Valid": true} 174 ]`), &res); err != nil { 175 t.Fatal(err) 176 } 177 178 for _, r := range res { 179 if got := r.A.ValidChecksum(); got != r.Valid { 180 t.Errorf("Expected checksum %v, got checksum %v, input %v", r.Valid, got, r.A.String()) 181 } 182 } 183 184 //These should throw exceptions: 185 var r2 []MixedcaseAddress 186 for _, r := range []string{ 187 `["0x11111111111111111111122222222222233333"]`, // Too short 188 `["0x111111111111111111111222222222222333332"]`, // Too short 189 `["0x11111111111111111111122222222222233333234"]`, // Too long 190 `["0x111111111111111111111222222222222333332344"]`, // Too long 191 `["1111111111111111111112222222222223333323"]`, // Missing 0x 192 `["x1111111111111111111112222222222223333323"]`, // Missing 0 193 `["0xG111111111111111111112222222222223333323"]`, //Non-hex 194 } { 195 if err := json.Unmarshal([]byte(r), &r2); err == nil { 196 t.Errorf("Expected failure, input %v", r) 197 } 198 199 } 200 201 } 202 203 func TestHash_Scan(t *testing.T) { 204 type args struct { 205 src interface{} 206 } 207 tests := []struct { 208 name string 209 args args 210 wantErr bool 211 }{ 212 { 213 name: "working scan", 214 args: args{src: []byte{ 215 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 216 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 217 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 218 0x10, 0x00, 219 }}, 220 wantErr: false, 221 }, 222 { 223 name: "non working scan", 224 args: args{src: int64(1234567890)}, 225 wantErr: true, 226 }, 227 { 228 name: "invalid length scan", 229 args: args{src: []byte{ 230 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 231 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 232 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 233 }}, 234 wantErr: true, 235 }, 236 } 237 for _, tt := range tests { 238 t.Run(tt.name, func(t *testing.T) { 239 h := &Hash{} 240 if err := h.Scan(tt.args.src); (err != nil) != tt.wantErr { 241 t.Errorf("Hash.Scan() error = %v, wantErr %v", err, tt.wantErr) 242 } 243 244 if !tt.wantErr { 245 for i := range h { 246 if h[i] != tt.args.src.([]byte)[i] { 247 t.Errorf( 248 "Hash.Scan() didn't scan the %d src correctly (have %X, want %X)", 249 i, h[i], tt.args.src.([]byte)[i], 250 ) 251 } 252 } 253 } 254 }) 255 } 256 } 257 258 func TestHash_Value(t *testing.T) { 259 b := []byte{ 260 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 261 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 262 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 263 0x10, 0x00, 264 } 265 var usedH Hash 266 usedH.SetBytes(b) 267 tests := []struct { 268 name string 269 h Hash 270 want driver.Value 271 wantErr bool 272 }{ 273 { 274 name: "Working value", 275 h: usedH, 276 want: b, 277 wantErr: false, 278 }, 279 } 280 for _, tt := range tests { 281 t.Run(tt.name, func(t *testing.T) { 282 got, err := tt.h.Value() 283 if (err != nil) != tt.wantErr { 284 t.Errorf("Hash.Value() error = %v, wantErr %v", err, tt.wantErr) 285 return 286 } 287 if !reflect.DeepEqual(got, tt.want) { 288 t.Errorf("Hash.Value() = %v, want %v", got, tt.want) 289 } 290 }) 291 } 292 } 293 294 func TestAddress_Scan(t *testing.T) { 295 type args struct { 296 src interface{} 297 } 298 tests := []struct { 299 name string 300 args args 301 wantErr bool 302 }{ 303 { 304 name: "working scan", 305 args: args{src: []byte{ 306 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 307 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 308 }}, 309 wantErr: false, 310 }, 311 { 312 name: "non working scan", 313 args: args{src: int64(1234567890)}, 314 wantErr: true, 315 }, 316 { 317 name: "invalid length scan", 318 args: args{src: []byte{ 319 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 320 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 321 }}, 322 wantErr: true, 323 }, 324 } 325 for _, tt := range tests { 326 t.Run(tt.name, func(t *testing.T) { 327 a := &Address{} 328 if err := a.Scan(tt.args.src); (err != nil) != tt.wantErr { 329 t.Errorf("Address.Scan() error = %v, wantErr %v", err, tt.wantErr) 330 } 331 332 if !tt.wantErr { 333 for i := range a { 334 if a[i] != tt.args.src.([]byte)[i] { 335 t.Errorf( 336 "Address.Scan() didn't scan the %d src correctly (have %X, want %X)", 337 i, a[i], tt.args.src.([]byte)[i], 338 ) 339 } 340 } 341 } 342 }) 343 } 344 } 345 346 func TestAddress_Value(t *testing.T) { 347 b := []byte{ 348 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 349 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 350 } 351 var usedA Address 352 usedA.SetBytes(b) 353 tests := []struct { 354 name string 355 a Address 356 want driver.Value 357 wantErr bool 358 }{ 359 { 360 name: "Working value", 361 a: usedA, 362 want: b, 363 wantErr: false, 364 }, 365 } 366 for _, tt := range tests { 367 t.Run(tt.name, func(t *testing.T) { 368 got, err := tt.a.Value() 369 if (err != nil) != tt.wantErr { 370 t.Errorf("Address.Value() error = %v, wantErr %v", err, tt.wantErr) 371 return 372 } 373 if !reflect.DeepEqual(got, tt.want) { 374 t.Errorf("Address.Value() = %v, want %v", got, tt.want) 375 } 376 }) 377 } 378 } 379 380 func TestRemoveItemInArray(t *testing.T) { 381 array := []Address{HexToAddress("0x0000003"), HexToAddress("0x0000001"), HexToAddress("0x0000002"), HexToAddress("0x0000003")} 382 remove := []Address{HexToAddress("0x0000002"), HexToAddress("0x0000004"), HexToAddress("0x0000003")} 383 array = RemoveItemFromArray(array, remove) 384 if len(array) != 1 { 385 t.Error("fail remove item from array address") 386 } 387 }