github.com/Ethersocial/go-esn@v0.3.7/common/hexutil/hexutil_test.go (about)

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