github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/common/hexutil/hexutil_test.go (about) 1 package hexutil 2 3 import ( 4 "bytes" 5 "math/big" 6 "testing" 7 ) 8 9 type marshalTest struct { 10 input interface{} 11 want string 12 } 13 14 type unmarshalTest struct { 15 input string 16 want interface{} 17 wantErr error 18 wantErr32bit error 19 } 20 21 var ( 22 encodeBytesTests = []marshalTest{ 23 {[]byte{}, "0x"}, 24 {[]byte{0}, "0x00"}, 25 {[]byte{0, 0, 1, 2}, "0x00000102"}, 26 } 27 28 encodeBigTests = []marshalTest{ 29 {referenceBig("0"), "0x0"}, 30 {referenceBig("1"), "0x1"}, 31 {referenceBig("ff"), "0xff"}, 32 {referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"}, 33 {referenceBig("80a7f2c1bcc396c00"), "0x80a7f2c1bcc396c00"}, 34 {referenceBig("-80a7f2c1bcc396c00"), "-0x80a7f2c1bcc396c00"}, 35 } 36 37 encodeUint64Tests = []marshalTest{ 38 {uint64(0), "0x0"}, 39 {uint64(1), "0x1"}, 40 {uint64(0xff), "0xff"}, 41 {uint64(0x1122334455667788), "0x1122334455667788"}, 42 } 43 44 encodeUintTests = []marshalTest{ 45 {uint(0), "0x0"}, 46 {uint(1), "0x1"}, 47 {uint(0xff), "0xff"}, 48 {uint(0x11223344), "0x11223344"}, 49 } 50 51 decodeBytesTests = []unmarshalTest{ 52 53 {input: ``, wantErr: ErrEmptyString}, 54 {input: `0`, wantErr: ErrMissingPrefix}, 55 {input: `0x0`, wantErr: ErrOddLength}, 56 {input: `0x023`, wantErr: ErrOddLength}, 57 {input: `0xxx`, wantErr: ErrSyntax}, 58 {input: `0x01zz01`, wantErr: ErrSyntax}, 59 60 {input: `0x`, want: []byte{}}, 61 {input: `0X`, want: []byte{}}, 62 {input: `0x02`, want: []byte{0x02}}, 63 {input: `0X02`, want: []byte{0x02}}, 64 {input: `0xffffffffff`, want: []byte{0xff, 0xff, 0xff, 0xff, 0xff}}, 65 { 66 input: `0xffffffffffffffffffffffffffffffffffff`, 67 want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 68 }, 69 } 70 71 decodeBigTests = []unmarshalTest{ 72 73 {input: `0`, wantErr: ErrMissingPrefix}, 74 {input: `0x`, wantErr: ErrEmptyNumber}, 75 {input: `0x01`, wantErr: ErrLeadingZero}, 76 {input: `0xx`, wantErr: ErrSyntax}, 77 {input: `0x1zz01`, wantErr: ErrSyntax}, 78 { 79 input: `0x10000000000000000000000000000000000000000000000000000000000000000`, 80 wantErr: ErrBig256Range, 81 }, 82 83 {input: `0x0`, want: big.NewInt(0)}, 84 {input: `0x2`, want: big.NewInt(0x2)}, 85 {input: `0x2F2`, want: big.NewInt(0x2f2)}, 86 {input: `0X2F2`, want: big.NewInt(0x2f2)}, 87 {input: `0x1122aaff`, want: big.NewInt(0x1122aaff)}, 88 {input: `0xbBb`, want: big.NewInt(0xbbb)}, 89 {input: `0xfffffffff`, want: big.NewInt(0xfffffffff)}, 90 { 91 input: `0x112233445566778899aabbccddeeff`, 92 want: referenceBig("112233445566778899aabbccddeeff"), 93 }, 94 { 95 input: `0xffffffffffffffffffffffffffffffffffff`, 96 want: referenceBig("ffffffffffffffffffffffffffffffffffff"), 97 }, 98 { 99 input: `0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff`, 100 want: referenceBig("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 101 }, 102 } 103 104 decodeUint64Tests = []unmarshalTest{ 105 106 {input: `0`, wantErr: ErrMissingPrefix}, 107 {input: `0x`, wantErr: ErrEmptyNumber}, 108 {input: `0x01`, wantErr: ErrLeadingZero}, 109 {input: `0xfffffffffffffffff`, wantErr: ErrUint64Range}, 110 {input: `0xx`, wantErr: ErrSyntax}, 111 {input: `0x1zz01`, wantErr: ErrSyntax}, 112 113 {input: `0x0`, want: uint64(0)}, 114 {input: `0x2`, want: uint64(0x2)}, 115 {input: `0x2F2`, want: uint64(0x2f2)}, 116 {input: `0X2F2`, want: uint64(0x2f2)}, 117 {input: `0x1122aaff`, want: uint64(0x1122aaff)}, 118 {input: `0xbbb`, want: uint64(0xbbb)}, 119 {input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)}, 120 } 121 ) 122 123 func TestEncode(t *testing.T) { 124 for _, test := range encodeBytesTests { 125 enc := Encode(test.input.([]byte)) 126 if enc != test.want { 127 t.Errorf("input %x: wrong encoding %s", test.input, enc) 128 } 129 } 130 } 131 132 func TestDecode(t *testing.T) { 133 for _, test := range decodeBytesTests { 134 dec, err := Decode(test.input) 135 if !checkError(t, test.input, err, test.wantErr) { 136 continue 137 } 138 if !bytes.Equal(test.want.([]byte), dec) { 139 t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want) 140 continue 141 } 142 } 143 } 144 145 func TestEncodeBig(t *testing.T) { 146 for _, test := range encodeBigTests { 147 enc := EncodeBig(test.input.(*big.Int)) 148 if enc != test.want { 149 t.Errorf("input %x: wrong encoding %s", test.input, enc) 150 } 151 } 152 } 153 154 func TestDecodeBig(t *testing.T) { 155 for _, test := range decodeBigTests { 156 dec, err := DecodeBig(test.input) 157 if !checkError(t, test.input, err, test.wantErr) { 158 continue 159 } 160 if dec.Cmp(test.want.(*big.Int)) != 0 { 161 t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want) 162 continue 163 } 164 } 165 } 166 167 func TestEncodeUint64(t *testing.T) { 168 for _, test := range encodeUint64Tests { 169 enc := EncodeUint64(test.input.(uint64)) 170 if enc != test.want { 171 t.Errorf("input %x: wrong encoding %s", test.input, enc) 172 } 173 } 174 } 175 176 func TestDecodeUint64(t *testing.T) { 177 for _, test := range decodeUint64Tests { 178 dec, err := DecodeUint64(test.input) 179 if !checkError(t, test.input, err, test.wantErr) { 180 continue 181 } 182 if dec != test.want.(uint64) { 183 t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want) 184 continue 185 } 186 } 187 }