github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/hexutil/hexutil_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package hexutil 19 20 import ( 21 "bytes" 22 "math/big" 23 "testing" 24 ) 25 26 type marshalTest struct { 27 input interface{} 28 want string 29 } 30 31 type unmarshalTest struct { 32 input string 33 want interface{} 34 wantErr error // if set, decoding must fail on any platform 35 wantErr32bit error // if set, decoding must fail on 32bit platforms (used for Uint tests) 36 } 37 38 var ( 39 encodeBytesTests = []marshalTest{ 40 {[]byte{}, "0x"}, 41 {[]byte{0}, "0x00"}, 42 {[]byte{0, 0, 1, 2}, "0x00000102"}, 43 } 44 45 encodeBigTests = []marshalTest{ 46 {referenceBig("0"), "0x0"}, 47 {referenceBig("1"), "0x1"}, 48 {referenceBig("ff"), "0xff"}, 49 {referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"}, 50 {referenceBig("80a7f2c1bcc396c00"), "0x80a7f2c1bcc396c00"}, 51 {referenceBig("-80a7f2c1bcc396c00"), "-0x80a7f2c1bcc396c00"}, 52 } 53 54 encodeUint64Tests = []marshalTest{ 55 {uint64(0), "0x0"}, 56 {uint64(1), "0x1"}, 57 {uint64(0xff), "0xff"}, 58 {uint64(0x1122334455667788), "0x1122334455667788"}, 59 } 60 61 encodeUintTests = []marshalTest{ 62 {uint(0), "0x0"}, 63 {uint(1), "0x1"}, 64 {uint(0xff), "0xff"}, 65 {uint(0x11223344), "0x11223344"}, 66 } 67 68 decodeBytesTests = []unmarshalTest{ 69 // invalid 70 {input: ``, wantErr: ErrEmptyString}, 71 {input: `0`, wantErr: ErrMissingPrefix}, 72 {input: `0x0`, wantErr: ErrOddLength}, 73 {input: `0x023`, wantErr: ErrOddLength}, 74 {input: `0xxx`, wantErr: ErrSyntax}, 75 {input: `0x01zz01`, wantErr: ErrSyntax}, 76 // valid 77 {input: `0x`, want: []byte{}}, 78 {input: `0X`, want: []byte{}}, 79 {input: `0x02`, want: []byte{0x02}}, 80 {input: `0X02`, want: []byte{0x02}}, 81 {input: `0xffffffffff`, want: []byte{0xff, 0xff, 0xff, 0xff, 0xff}}, 82 { 83 input: `0xffffffffffffffffffffffffffffffffffff`, 84 want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 85 }, 86 } 87 88 decodeBigTests = []unmarshalTest{ 89 // invalid 90 {input: `0`, wantErr: ErrMissingPrefix}, 91 {input: `0x`, wantErr: ErrEmptyNumber}, 92 {input: `0x01`, wantErr: ErrLeadingZero}, 93 {input: `0xx`, wantErr: ErrSyntax}, 94 {input: `0x1zz01`, wantErr: ErrSyntax}, 95 { 96 input: `0x10000000000000000000000000000000000000000000000000000000000000000`, 97 wantErr: ErrBig256Range, 98 }, 99 // valid 100 {input: `0x0`, want: big.NewInt(0)}, 101 {input: `0x2`, want: big.NewInt(0x2)}, 102 {input: `0x2F2`, want: big.NewInt(0x2f2)}, 103 {input: `0X2F2`, want: big.NewInt(0x2f2)}, 104 {input: `0x1122aaff`, want: big.NewInt(0x1122aaff)}, 105 {input: `0xbBb`, want: big.NewInt(0xbbb)}, 106 {input: `0xfffffffff`, want: big.NewInt(0xfffffffff)}, 107 { 108 input: `0x112233445566778899aabbccddeeff`, 109 want: referenceBig("112233445566778899aabbccddeeff"), 110 }, 111 { 112 input: `0xffffffffffffffffffffffffffffffffffff`, 113 want: referenceBig("ffffffffffffffffffffffffffffffffffff"), 114 }, 115 { 116 input: `0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff`, 117 want: referenceBig("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 118 }, 119 } 120 121 decodeUint64Tests = []unmarshalTest{ 122 // invalid 123 {input: `0`, wantErr: ErrMissingPrefix}, 124 {input: `0x`, wantErr: ErrEmptyNumber}, 125 {input: `0x01`, wantErr: ErrLeadingZero}, 126 {input: `0xfffffffffffffffff`, wantErr: ErrUint64Range}, 127 {input: `0xx`, wantErr: ErrSyntax}, 128 {input: `0x1zz01`, wantErr: ErrSyntax}, 129 // valid 130 {input: `0x0`, want: uint64(0)}, 131 {input: `0x2`, want: uint64(0x2)}, 132 {input: `0x2F2`, want: uint64(0x2f2)}, 133 {input: `0X2F2`, want: uint64(0x2f2)}, 134 {input: `0x1122aaff`, want: uint64(0x1122aaff)}, 135 {input: `0xbbb`, want: uint64(0xbbb)}, 136 {input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)}, 137 } 138 ) 139 140 func TestEncode(t *testing.T) { 141 for _, test := range encodeBytesTests { 142 enc := Encode(test.input.([]byte)) 143 if enc != test.want { 144 t.Errorf("input %x: wrong encoding %s", test.input, enc) 145 } 146 } 147 } 148 149 func TestDecode(t *testing.T) { 150 for _, test := range decodeBytesTests { 151 dec, err := Decode(test.input) 152 if !checkError(t, test.input, err, test.wantErr) { 153 continue 154 } 155 if !bytes.Equal(test.want.([]byte), dec) { 156 t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want) 157 continue 158 } 159 } 160 } 161 162 func TestEncodeBig(t *testing.T) { 163 for _, test := range encodeBigTests { 164 enc := EncodeBig(test.input.(*big.Int)) 165 if enc != test.want { 166 t.Errorf("input %x: wrong encoding %s", test.input, enc) 167 } 168 } 169 } 170 171 func TestDecodeBig(t *testing.T) { 172 for _, test := range decodeBigTests { 173 dec, err := DecodeBig(test.input) 174 if !checkError(t, test.input, err, test.wantErr) { 175 continue 176 } 177 if dec.Cmp(test.want.(*big.Int)) != 0 { 178 t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want) 179 continue 180 } 181 } 182 } 183 184 func TestEncodeUint64(t *testing.T) { 185 for _, test := range encodeUint64Tests { 186 enc := EncodeUint64(test.input.(uint64)) 187 if enc != test.want { 188 t.Errorf("input %x: wrong encoding %s", test.input, enc) 189 } 190 } 191 } 192 193 func TestDecodeUint64(t *testing.T) { 194 for _, test := range decodeUint64Tests { 195 dec, err := DecodeUint64(test.input) 196 if !checkError(t, test.input, err, test.wantErr) { 197 continue 198 } 199 if dec != test.want.(uint64) { 200 t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want) 201 continue 202 } 203 } 204 }