github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/common/types_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2015 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package common 26 27 import ( 28 "database/sql/driver" 29 "encoding/json" 30 "math/big" 31 "reflect" 32 "strings" 33 "testing" 34 ) 35 36 func TestBytesConversion(t *testing.T) { 37 bytes := []byte{5} 38 hash := BytesToHash(bytes) 39 40 var exp Hash 41 exp[31] = 5 42 43 if hash != exp { 44 t.Errorf("expected %x got %x", exp, hash) 45 } 46 } 47 48 func TestIsHexAddress(t *testing.T) { 49 tests := []struct { 50 str string 51 exp bool 52 }{ 53 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 54 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 55 {"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 56 {"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 57 {"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 58 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false}, 59 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false}, 60 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false}, 61 {"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false}, 62 } 63 64 for _, test := range tests { 65 if result := IsHexAddress(test.str); result != test.exp { 66 t.Errorf("IsHexAddress(%s) == %v; expected %v", 67 test.str, result, test.exp) 68 } 69 } 70 } 71 72 func TestHashJsonValidation(t *testing.T) { 73 var tests = []struct { 74 Prefix string 75 Size int 76 Error string 77 }{ 78 {"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"}, 79 {"0x", 66, "hex string has length 66, want 64 for common.Hash"}, 80 {"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"}, 81 {"0x", 0, "hex string has length 0, want 64 for common.Hash"}, 82 {"0x", 64, ""}, 83 {"0X", 64, ""}, 84 } 85 for _, test := range tests { 86 input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"` 87 var v Hash 88 err := json.Unmarshal([]byte(input), &v) 89 if err == nil { 90 if test.Error != "" { 91 t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error) 92 } 93 } else { 94 if err.Error() != test.Error { 95 t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error) 96 } 97 } 98 } 99 } 100 101 func TestAddressUnmarshalJSON(t *testing.T) { 102 var tests = []struct { 103 Input string 104 ShouldErr bool 105 Output *big.Int 106 }{ 107 {"", true, nil}, 108 {`""`, true, nil}, 109 {`"0x"`, true, nil}, 110 {`"0x00"`, true, nil}, 111 {`"0xG000000000000000000000000000000000000000"`, true, nil}, 112 {`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)}, 113 {`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)}, 114 } 115 for i, test := range tests { 116 var v Address 117 err := json.Unmarshal([]byte(test.Input), &v) 118 if err != nil && !test.ShouldErr { 119 t.Errorf("test #%d: unexpected error: %v", i, err) 120 } 121 if err == nil { 122 if test.ShouldErr { 123 t.Errorf("test #%d: expected error, got none", i) 124 } 125 if v.Big().Cmp(test.Output) != 0 { 126 t.Errorf("test #%d: address mismatch: have %v, want %v", i, v.Big(), test.Output) 127 } 128 } 129 } 130 } 131 132 func TestAddressHexChecksum(t *testing.T) { 133 var tests = []struct { 134 Input string 135 Output string 136 }{ 137 //测试用例来自https://github.com/ethereum/eips/blob/master/eips/eip-55.md规范 138 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"}, 139 {"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"}, 140 {"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"}, 141 {"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"}, 142 //确保正确处理非标准长度输入值 143 {"0xa", "0x000000000000000000000000000000000000000A"}, 144 {"0x0a", "0x000000000000000000000000000000000000000A"}, 145 {"0x00a", "0x000000000000000000000000000000000000000A"}, 146 {"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"}, 147 } 148 for i, test := range tests { 149 output := HexToAddress(test.Input).Hex() 150 if output != test.Output { 151 t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output) 152 } 153 } 154 } 155 156 func BenchmarkAddressHex(b *testing.B) { 157 testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") 158 for n := 0; n < b.N; n++ { 159 testAddr.Hex() 160 } 161 } 162 163 func TestMixedcaseAccount_Address(t *testing.T) { 164 165 //https://github.com/ethereum/eips/blob/master/eips/eip-55.md网站 166 //注:根据上述规范,0x校验和地址无效 167 168 var res []struct { 169 A MixedcaseAddress 170 Valid bool 171 } 172 if err := json.Unmarshal([]byte(`[ 173 {"A" : "0xae967917c465db8578ca9024c205720b1a3651A9", "Valid": false}, 174 {"A" : "0xAe967917c465db8578ca9024c205720b1a3651A9", "Valid": true}, 175 {"A" : "0XAe967917c465db8578ca9024c205720b1a3651A9", "Valid": false}, 176 {"A" : "0x1111111111111111111112222222222223333323", "Valid": true} 177 ]`), &res); err != nil { 178 t.Fatal(err) 179 } 180 181 for _, r := range res { 182 if got := r.A.ValidChecksum(); got != r.Valid { 183 t.Errorf("Expected checksum %v, got checksum %v, input %v", r.Valid, got, r.A.String()) 184 } 185 } 186 187 //这些应该抛出异常: 188 var r2 []MixedcaseAddress 189 for _, r := range []string{ 190 `["0x11111111111111111111122222222222233333"]`, //太短 191 `["0x111111111111111111111222222222222333332"]`, //太短 192 `["0x11111111111111111111122222222222233333234"]`, //太长 193 `["0x111111111111111111111222222222222333332344"]`, //太长 194 `["1111111111111111111112222222222223333323"]`, //遗失0X 195 `["x1111111111111111111112222222222223333323"]`, //失踪0 196 `["0xG111111111111111111112222222222223333323"]`, //非十六进制 197 } { 198 if err := json.Unmarshal([]byte(r), &r2); err == nil { 199 t.Errorf("Expected failure, input %v", r) 200 } 201 202 } 203 204 } 205 206 func TestHash_Scan(t *testing.T) { 207 type args struct { 208 src interface{} 209 } 210 tests := []struct { 211 name string 212 args args 213 wantErr bool 214 }{ 215 { 216 name: "working scan", 217 args: args{src: []byte{ 218 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 219 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 220 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 221 0x10, 0x00, 222 }}, 223 wantErr: false, 224 }, 225 { 226 name: "non working scan", 227 args: args{src: int64(1234567890)}, 228 wantErr: true, 229 }, 230 { 231 name: "invalid length scan", 232 args: args{src: []byte{ 233 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 234 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 235 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 236 }}, 237 wantErr: true, 238 }, 239 } 240 for _, tt := range tests { 241 t.Run(tt.name, func(t *testing.T) { 242 h := &Hash{} 243 if err := h.Scan(tt.args.src); (err != nil) != tt.wantErr { 244 t.Errorf("Hash.Scan() error = %v, wantErr %v", err, tt.wantErr) 245 } 246 247 if !tt.wantErr { 248 for i := range h { 249 if h[i] != tt.args.src.([]byte)[i] { 250 t.Errorf( 251 "Hash.Scan() didn't scan the %d src correctly (have %X, want %X)", 252 i, h[i], tt.args.src.([]byte)[i], 253 ) 254 } 255 } 256 } 257 }) 258 } 259 } 260 261 func TestHash_Value(t *testing.T) { 262 b := []byte{ 263 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 264 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 265 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 266 0x10, 0x00, 267 } 268 var usedH Hash 269 usedH.SetBytes(b) 270 tests := []struct { 271 name string 272 h Hash 273 want driver.Value 274 wantErr bool 275 }{ 276 { 277 name: "Working value", 278 h: usedH, 279 want: b, 280 wantErr: false, 281 }, 282 } 283 for _, tt := range tests { 284 t.Run(tt.name, func(t *testing.T) { 285 got, err := tt.h.Value() 286 if (err != nil) != tt.wantErr { 287 t.Errorf("Hash.Value() error = %v, wantErr %v", err, tt.wantErr) 288 return 289 } 290 if !reflect.DeepEqual(got, tt.want) { 291 t.Errorf("Hash.Value() = %v, want %v", got, tt.want) 292 } 293 }) 294 } 295 } 296 297 func TestAddress_Scan(t *testing.T) { 298 type args struct { 299 src interface{} 300 } 301 tests := []struct { 302 name string 303 args args 304 wantErr bool 305 }{ 306 { 307 name: "working scan", 308 args: args{src: []byte{ 309 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 310 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 311 }}, 312 wantErr: false, 313 }, 314 { 315 name: "non working scan", 316 args: args{src: int64(1234567890)}, 317 wantErr: true, 318 }, 319 { 320 name: "invalid length scan", 321 args: args{src: []byte{ 322 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 323 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 324 }}, 325 wantErr: true, 326 }, 327 } 328 for _, tt := range tests { 329 t.Run(tt.name, func(t *testing.T) { 330 a := &Address{} 331 if err := a.Scan(tt.args.src); (err != nil) != tt.wantErr { 332 t.Errorf("Address.Scan() error = %v, wantErr %v", err, tt.wantErr) 333 } 334 335 if !tt.wantErr { 336 for i := range a { 337 if a[i] != tt.args.src.([]byte)[i] { 338 t.Errorf( 339 "Address.Scan() didn't scan the %d src correctly (have %X, want %X)", 340 i, a[i], tt.args.src.([]byte)[i], 341 ) 342 } 343 } 344 } 345 }) 346 } 347 } 348 349 func TestAddress_Value(t *testing.T) { 350 b := []byte{ 351 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 352 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 353 } 354 var usedA Address 355 usedA.SetBytes(b) 356 tests := []struct { 357 name string 358 a Address 359 want driver.Value 360 wantErr bool 361 }{ 362 { 363 name: "Working value", 364 a: usedA, 365 want: b, 366 wantErr: false, 367 }, 368 } 369 for _, tt := range tests { 370 t.Run(tt.name, func(t *testing.T) { 371 got, err := tt.a.Value() 372 if (err != nil) != tt.wantErr { 373 t.Errorf("Address.Value() error = %v, wantErr %v", err, tt.wantErr) 374 return 375 } 376 if !reflect.DeepEqual(got, tt.want) { 377 t.Errorf("Address.Value() = %v, want %v", got, tt.want) 378 } 379 }) 380 } 381 }