github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/common/types_test.go (about) 1 package common 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "math/big" 7 "strings" 8 "testing" 9 ) 10 11 func TestBytesConversion(t *testing.T) { 12 bytes := []byte{5} 13 hash := BytesToHash(bytes) 14 15 var exp Hash 16 exp[31] = 5 17 18 if hash != exp { 19 t.Errorf("expected %x got %x", exp, hash) 20 } 21 } 22 23 func TestIsHexAddress(t *testing.T) { 24 tests := []struct { 25 str string 26 exp bool 27 }{ 28 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 29 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 30 {"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true}, 31 {"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 32 {"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true}, 33 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false}, 34 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false}, 35 {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false}, 36 {"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false}, 37 } 38 39 for _, test := range tests { 40 if result := IsHexAddress(test.str); result != test.exp { 41 t.Errorf("IsHexAddress(%s) == %v; expected %v", 42 test.str, result, test.exp) 43 } 44 } 45 } 46 47 func TestHashJsonValidation(t *testing.T) { 48 var tests = []struct { 49 Prefix string 50 Size int 51 Error string 52 }{ 53 {"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"}, 54 {"0x", 66, "hex string has length 66, want 64 for common.Hash"}, 55 {"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"}, 56 {"0x", 0, "hex string has length 0, want 64 for common.Hash"}, 57 {"0x", 64, ""}, 58 {"0X", 64, ""}, 59 } 60 for _, test := range tests { 61 input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"` 62 var v Hash 63 err := json.Unmarshal([]byte(input), &v) 64 if err == nil { 65 if test.Error != "" { 66 t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error) 67 } 68 } else { 69 if err.Error() != test.Error { 70 t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error) 71 } 72 } 73 } 74 } 75 76 func TestAddressUnmarshalJSON(t *testing.T) { 77 var tests = []struct { 78 Input string 79 ShouldErr bool 80 Output *big.Int 81 }{ 82 {"", true, nil}, 83 {`""`, true, nil}, 84 {`"0x"`, true, nil}, 85 {`"0x00"`, true, nil}, 86 {`"0xG000000000000000000000000000000000000000"`, true, nil}, 87 {`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)}, 88 {`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)}, 89 } 90 for i, test := range tests { 91 var v Address 92 err := json.Unmarshal([]byte(test.Input), &v) 93 if err != nil && !test.ShouldErr { 94 t.Errorf("test #%d: unexpected error: %v", i, err) 95 } 96 if err == nil { 97 if test.ShouldErr { 98 t.Errorf("test #%d: expected error, got none", i) 99 } 100 if v.Big().Cmp(test.Output) != 0 { 101 t.Errorf("test #%d: address mismatch: have %v, want %v", i, v.Big(), test.Output) 102 } 103 } 104 } 105 } 106 107 func TestAddressHexChecksum(t *testing.T) { 108 var tests = []struct { 109 Input string 110 Output string 111 }{ 112 113 {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"}, 114 {"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"}, 115 {"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"}, 116 {"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"}, 117 118 {"0xa", "0x000000000000000000000000000000000000000A"}, 119 {"0x0a", "0x000000000000000000000000000000000000000A"}, 120 {"0x00a", "0x000000000000000000000000000000000000000A"}, 121 {"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"}, 122 } 123 for i, test := range tests { 124 output := HexToAddress(test.Input).Hex() 125 if output != test.Output { 126 t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output) 127 } 128 } 129 } 130 131 func TestAddress_UnmarshalJSON(t *testing.T) { 132 addr1 := HexToAddress("0x0e0ffd4c684b325be82f120a7938c7d938ff3dca") 133 134 addr2 := Address{} 135 addrB := []byte(`"`) 136 137 fmt.Printf("addrB=%v\n", addrB) 138 139 addrC := append(addrB, addr1[:]...) 140 addrC = append(addrC, addrB...) 141 err := addr2.UnmarshalJSON(addrC[:]) 142 if err != nil { 143 t.Error(err) 144 } 145 fmt.Printf("addr2=%v\n", addr2) 146 } 147 148 func BenchmarkAddressHex(b *testing.B) { 149 testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") 150 for n := 0; n < b.N; n++ { 151 testAddr.Hex() 152 } 153 }