github.com/klaytn/klaytn@v1.12.1/common/hexutil/hexutil_test.go (about)

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