github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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  	"encoding/hex"
    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
    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  	}
    51  
    52  	encodeUint64Tests = []marshalTest{
    53  		{uint64(0), "0x0"},
    54  		{uint64(1), "0x1"},
    55  		{uint64(0xff), "0xff"},
    56  		{uint64(0x1122334455667788), "0x1122334455667788"},
    57  	}
    58  
    59  	decodeBytesTests = []unmarshalTest{
    60  		// invalid
    61  		{input: ``, wantErr: ErrEmptyString},
    62  		{input: `0`, wantErr: ErrMissingPrefix},
    63  		{input: `0x0`, wantErr: hex.ErrLength},
    64  		{input: `0x023`, wantErr: hex.ErrLength},
    65  		{input: `0xxx`, wantErr: hex.InvalidByteError('x')},
    66  		{input: `0x01zz01`, wantErr: hex.InvalidByteError('z')},
    67  		// valid
    68  		{input: `0x`, want: []byte{}},
    69  		{input: `0X`, want: []byte{}},
    70  		{input: `0x02`, want: []byte{0x02}},
    71  		{input: `0X02`, want: []byte{0x02}},
    72  		{input: `0xffffffffff`, want: []byte{0xff, 0xff, 0xff, 0xff, 0xff}},
    73  		{
    74  			input: `0xffffffffffffffffffffffffffffffffffff`,
    75  			want:  []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
    76  		},
    77  	}
    78  
    79  	decodeBigTests = []unmarshalTest{
    80  		// invalid
    81  		{input: `0`, wantErr: ErrMissingPrefix},
    82  		{input: `0x`, wantErr: ErrEmptyNumber},
    83  		{input: `0x01`, wantErr: ErrLeadingZero},
    84  		{input: `0xx`, wantErr: ErrSyntax},
    85  		{input: `0x1zz01`, wantErr: ErrSyntax},
    86  		// valid
    87  		{input: `0x0`, want: big.NewInt(0)},
    88  		{input: `0x2`, want: big.NewInt(0x2)},
    89  		{input: `0x2F2`, want: big.NewInt(0x2f2)},
    90  		{input: `0X2F2`, want: big.NewInt(0x2f2)},
    91  		{input: `0x1122aaff`, want: big.NewInt(0x1122aaff)},
    92  		{input: `0xbBb`, want: big.NewInt(0xbbb)},
    93  		{input: `0xfffffffff`, want: big.NewInt(0xfffffffff)},
    94  		{
    95  			input: `0x112233445566778899aabbccddeeff`,
    96  			want:  referenceBig("112233445566778899aabbccddeeff"),
    97  		},
    98  		{
    99  			input: `0xffffffffffffffffffffffffffffffffffff`,
   100  			want:  referenceBig("ffffffffffffffffffffffffffffffffffff"),
   101  		},
   102  	}
   103  
   104  	decodeUint64Tests = []unmarshalTest{
   105  		// invalid
   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  		// valid
   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  }