github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/common/hexutil/hexutil_test.go (about)

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