github.com/theQRL/go-zond@v0.1.1/rlp/raw_test.go (about)

     1  // Copyright 2015 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 rlp
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"io"
    23  	"testing"
    24  	"testing/quick"
    25  )
    26  
    27  func TestCountValues(t *testing.T) {
    28  	tests := []struct {
    29  		input string // note: spaces in input are stripped by unhex
    30  		count int
    31  		err   error
    32  	}{
    33  		// simple cases
    34  		{"", 0, nil},
    35  		{"00", 1, nil},
    36  		{"80", 1, nil},
    37  		{"C0", 1, nil},
    38  		{"01 02 03", 3, nil},
    39  		{"01 C406070809 02", 3, nil},
    40  		{"820101 820202 8403030303 04", 4, nil},
    41  
    42  		// size errors
    43  		{"8142", 0, ErrCanonSize},
    44  		{"01 01 8142", 0, ErrCanonSize},
    45  		{"02 84020202", 0, ErrValueTooLarge},
    46  
    47  		{
    48  			input: "A12000BF49F440A1CD0527E4D06E2765654C0F56452257516D793A9B8D604DCFDF2AB853F851808D10000000000000000000000000A056E81F171BCC55A6FF8345E692C0F86E5B48E01B996CADC001622FB5E363B421A0C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470",
    49  			count: 2,
    50  		},
    51  	}
    52  	for i, test := range tests {
    53  		count, err := CountValues(unhex(test.input))
    54  		if count != test.count {
    55  			t.Errorf("test %d: count mismatch, got %d want %d\ninput: %s", i, count, test.count, test.input)
    56  		}
    57  		if !errors.Is(err, test.err) {
    58  			t.Errorf("test %d: err mismatch, got %q want %q\ninput: %s", i, err, test.err, test.input)
    59  		}
    60  	}
    61  }
    62  
    63  func TestSplitString(t *testing.T) {
    64  	for i, test := range []string{
    65  		"C0",
    66  		"C100",
    67  		"C3010203",
    68  		"C88363617483646F67",
    69  		"F8384C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E7365637465747572206164697069736963696E6720656C6974",
    70  	} {
    71  		if _, _, err := SplitString(unhex(test)); !errors.Is(err, ErrExpectedString) {
    72  			t.Errorf("test %d: error mismatch: have %q, want %q", i, err, ErrExpectedString)
    73  		}
    74  	}
    75  }
    76  
    77  func TestSplitList(t *testing.T) {
    78  	for i, test := range []string{
    79  		"80",
    80  		"00",
    81  		"01",
    82  		"8180",
    83  		"81FF",
    84  		"820400",
    85  		"83636174",
    86  		"83646F67",
    87  		"B8384C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E7365637465747572206164697069736963696E6720656C6974",
    88  	} {
    89  		if _, _, err := SplitList(unhex(test)); !errors.Is(err, ErrExpectedList) {
    90  			t.Errorf("test %d: error mismatch: have %q, want %q", i, err, ErrExpectedList)
    91  		}
    92  	}
    93  }
    94  
    95  func TestSplitUint64(t *testing.T) {
    96  	tests := []struct {
    97  		input string
    98  		val   uint64
    99  		rest  string
   100  		err   error
   101  	}{
   102  		{"01", 1, "", nil},
   103  		{"7FFF", 0x7F, "FF", nil},
   104  		{"80FF", 0, "FF", nil},
   105  		{"81FAFF", 0xFA, "FF", nil},
   106  		{"82FAFAFF", 0xFAFA, "FF", nil},
   107  		{"83FAFAFAFF", 0xFAFAFA, "FF", nil},
   108  		{"84FAFAFAFAFF", 0xFAFAFAFA, "FF", nil},
   109  		{"85FAFAFAFAFAFF", 0xFAFAFAFAFA, "FF", nil},
   110  		{"86FAFAFAFAFAFAFF", 0xFAFAFAFAFAFA, "FF", nil},
   111  		{"87FAFAFAFAFAFAFAFF", 0xFAFAFAFAFAFAFA, "FF", nil},
   112  		{"88FAFAFAFAFAFAFAFAFF", 0xFAFAFAFAFAFAFAFA, "FF", nil},
   113  
   114  		// errors
   115  		{"", 0, "", io.ErrUnexpectedEOF},
   116  		{"00", 0, "00", ErrCanonInt},
   117  		{"81", 0, "81", ErrValueTooLarge},
   118  		{"8100", 0, "8100", ErrCanonSize},
   119  		{"8200FF", 0, "8200FF", ErrCanonInt},
   120  		{"8103FF", 0, "8103FF", ErrCanonSize},
   121  		{"89FAFAFAFAFAFAFAFAFAFF", 0, "89FAFAFAFAFAFAFAFAFAFF", errUintOverflow},
   122  	}
   123  
   124  	for i, test := range tests {
   125  		val, rest, err := SplitUint64(unhex(test.input))
   126  		if val != test.val {
   127  			t.Errorf("test %d: val mismatch: got %x, want %x (input %q)", i, val, test.val, test.input)
   128  		}
   129  		if !bytes.Equal(rest, unhex(test.rest)) {
   130  			t.Errorf("test %d: rest mismatch: got %x, want %s (input %q)", i, rest, test.rest, test.input)
   131  		}
   132  		if err != test.err {
   133  			t.Errorf("test %d: error mismatch: got %q, want %q", i, err, test.err)
   134  		}
   135  	}
   136  }
   137  
   138  func TestSplit(t *testing.T) {
   139  	tests := []struct {
   140  		input     string
   141  		kind      Kind
   142  		val, rest string
   143  		err       error
   144  	}{
   145  		{input: "00FFFF", kind: Byte, val: "00", rest: "FFFF"},
   146  		{input: "01FFFF", kind: Byte, val: "01", rest: "FFFF"},
   147  		{input: "7FFFFF", kind: Byte, val: "7F", rest: "FFFF"},
   148  		{input: "80FFFF", kind: String, val: "", rest: "FFFF"},
   149  		{input: "C3010203", kind: List, val: "010203"},
   150  
   151  		// errors
   152  		{input: "", err: io.ErrUnexpectedEOF},
   153  
   154  		{input: "8141", err: ErrCanonSize, rest: "8141"},
   155  		{input: "B800", err: ErrCanonSize, rest: "B800"},
   156  		{input: "B802FFFF", err: ErrCanonSize, rest: "B802FFFF"},
   157  		{input: "B90000", err: ErrCanonSize, rest: "B90000"},
   158  		{input: "B90055", err: ErrCanonSize, rest: "B90055"},
   159  		{input: "BA0002FFFF", err: ErrCanonSize, rest: "BA0002FFFF"},
   160  		{input: "F800", err: ErrCanonSize, rest: "F800"},
   161  		{input: "F90000", err: ErrCanonSize, rest: "F90000"},
   162  		{input: "F90055", err: ErrCanonSize, rest: "F90055"},
   163  		{input: "FA0002FFFF", err: ErrCanonSize, rest: "FA0002FFFF"},
   164  
   165  		{input: "81", err: ErrValueTooLarge, rest: "81"},
   166  		{input: "8501010101", err: ErrValueTooLarge, rest: "8501010101"},
   167  		{input: "C60607080902", err: ErrValueTooLarge, rest: "C60607080902"},
   168  
   169  		// size check overflow
   170  		{input: "BFFFFFFFFFFFFFFFFF", err: ErrValueTooLarge, rest: "BFFFFFFFFFFFFFFFFF"},
   171  		{input: "FFFFFFFFFFFFFFFFFF", err: ErrValueTooLarge, rest: "FFFFFFFFFFFFFFFFFF"},
   172  
   173  		{
   174  			input: "B838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   175  			err:   ErrValueTooLarge,
   176  			rest:  "B838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   177  		},
   178  		{
   179  			input: "F838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   180  			err:   ErrValueTooLarge,
   181  			rest:  "F838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   182  		},
   183  
   184  		// a few bigger values, just for kicks
   185  		{
   186  			input: "F839FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   187  			kind:  List,
   188  			val:   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   189  			rest:  "",
   190  		},
   191  		{
   192  			input: "F90211A060EF29F20CC1007AE6E9530AEE16F4B31F8F1769A2D1264EC995C6D1241868D6A07C62AB8AC9838F5F5877B20BB37B387BC2106E97A3D52172CBEDB5EE17C36008A00EAB6B7324AADC0F6047C6AFC8229F09F7CF451B51D67C8DFB08D49BA8C3C626A04453343B2F3A6E42FCF87948F88AF7C8FC16D0C2735CBA7F026836239AB2C15FA024635C7291C882CE4C0763760C1A362DFC3FFCD802A55722236DE058D74202ACA0A220C808DE10F55E40AB25255201CFF009EA181D3906638E944EE2BF34049984A08D325AB26796F1CCB470F69C0F842501DC35D368A0C2575B2D243CFD1E8AB0FDA0B5298FF60DA5069463D610513C9F04F24051348391A143AFFAB7197DFACDEA72A02D2A7058A4463F8FB69378369E11EF33AE3252E2DB86CB545B36D3C26DDECE5AA0888F97BCA8E0BD83DC5B3B91CFF5FAF2F66F9501010682D67EF4A3B4E66115FBA0E8175A60C93BE9ED02921958F0EA55DA0FB5E4802AF5846147BAD92BC2D8AF26A08B3376FF433F3A4250FA64B7F804004CAC5807877D91C4427BD1CD05CF912ED8A09B32EF0F03BD13C37FF950C0CCCEFCCDD6669F2E7F2AA5CB859928E84E29763EA09BBA5E46610C8C8B1F8E921E5691BF8C7E40D75825D5EA3217AA9C3A8A355F39A0EEB95BC78251CCCEC54A97F19755C4A59A293544EEE6119AFA50531211E53C4FA00B6E86FE150BF4A9E0FEEE9C90F5465E617A861BB5E357F942881EE762212E2580",
   193  			kind:  List,
   194  			val:   "A060EF29F20CC1007AE6E9530AEE16F4B31F8F1769A2D1264EC995C6D1241868D6A07C62AB8AC9838F5F5877B20BB37B387BC2106E97A3D52172CBEDB5EE17C36008A00EAB6B7324AADC0F6047C6AFC8229F09F7CF451B51D67C8DFB08D49BA8C3C626A04453343B2F3A6E42FCF87948F88AF7C8FC16D0C2735CBA7F026836239AB2C15FA024635C7291C882CE4C0763760C1A362DFC3FFCD802A55722236DE058D74202ACA0A220C808DE10F55E40AB25255201CFF009EA181D3906638E944EE2BF34049984A08D325AB26796F1CCB470F69C0F842501DC35D368A0C2575B2D243CFD1E8AB0FDA0B5298FF60DA5069463D610513C9F04F24051348391A143AFFAB7197DFACDEA72A02D2A7058A4463F8FB69378369E11EF33AE3252E2DB86CB545B36D3C26DDECE5AA0888F97BCA8E0BD83DC5B3B91CFF5FAF2F66F9501010682D67EF4A3B4E66115FBA0E8175A60C93BE9ED02921958F0EA55DA0FB5E4802AF5846147BAD92BC2D8AF26A08B3376FF433F3A4250FA64B7F804004CAC5807877D91C4427BD1CD05CF912ED8A09B32EF0F03BD13C37FF950C0CCCEFCCDD6669F2E7F2AA5CB859928E84E29763EA09BBA5E46610C8C8B1F8E921E5691BF8C7E40D75825D5EA3217AA9C3A8A355F39A0EEB95BC78251CCCEC54A97F19755C4A59A293544EEE6119AFA50531211E53C4FA00B6E86FE150BF4A9E0FEEE9C90F5465E617A861BB5E357F942881EE762212E2580",
   195  			rest:  "",
   196  		},
   197  		{
   198  			input: "F877A12000BF49F440A1CD0527E4D06E2765654C0F56452257516D793A9B8D604DCFDF2AB853F851808D10000000000000000000000000A056E81F171BCC55A6FF8345E692C0F86E5B48E01B996CADC001622FB5E363B421A0C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470",
   199  			kind:  List,
   200  			val:   "A12000BF49F440A1CD0527E4D06E2765654C0F56452257516D793A9B8D604DCFDF2AB853F851808D10000000000000000000000000A056E81F171BCC55A6FF8345E692C0F86E5B48E01B996CADC001622FB5E363B421A0C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470",
   201  			rest:  "",
   202  		},
   203  	}
   204  
   205  	for i, test := range tests {
   206  		kind, val, rest, err := Split(unhex(test.input))
   207  		if kind != test.kind {
   208  			t.Errorf("test %d: kind mismatch: got %v, want %v", i, kind, test.kind)
   209  		}
   210  		if !bytes.Equal(val, unhex(test.val)) {
   211  			t.Errorf("test %d: val mismatch: got %x, want %s", i, val, test.val)
   212  		}
   213  		if !bytes.Equal(rest, unhex(test.rest)) {
   214  			t.Errorf("test %d: rest mismatch: got %x, want %s", i, rest, test.rest)
   215  		}
   216  		if err != test.err {
   217  			t.Errorf("test %d: error mismatch: got %q, want %q", i, err, test.err)
   218  		}
   219  	}
   220  }
   221  
   222  func TestReadSize(t *testing.T) {
   223  	tests := []struct {
   224  		input string
   225  		slen  byte
   226  		size  uint64
   227  		err   error
   228  	}{
   229  		{input: "", slen: 1, err: io.ErrUnexpectedEOF},
   230  		{input: "FF", slen: 2, err: io.ErrUnexpectedEOF},
   231  		{input: "00", slen: 1, err: ErrCanonSize},
   232  		{input: "36", slen: 1, err: ErrCanonSize},
   233  		{input: "37", slen: 1, err: ErrCanonSize},
   234  		{input: "38", slen: 1, size: 0x38},
   235  		{input: "FF", slen: 1, size: 0xFF},
   236  		{input: "FFFF", slen: 2, size: 0xFFFF},
   237  		{input: "FFFFFF", slen: 3, size: 0xFFFFFF},
   238  		{input: "FFFFFFFF", slen: 4, size: 0xFFFFFFFF},
   239  		{input: "FFFFFFFFFF", slen: 5, size: 0xFFFFFFFFFF},
   240  		{input: "FFFFFFFFFFFF", slen: 6, size: 0xFFFFFFFFFFFF},
   241  		{input: "FFFFFFFFFFFFFF", slen: 7, size: 0xFFFFFFFFFFFFFF},
   242  		{input: "FFFFFFFFFFFFFFFF", slen: 8, size: 0xFFFFFFFFFFFFFFFF},
   243  		{input: "0102", slen: 2, size: 0x0102},
   244  		{input: "010203", slen: 3, size: 0x010203},
   245  		{input: "01020304", slen: 4, size: 0x01020304},
   246  		{input: "0102030405", slen: 5, size: 0x0102030405},
   247  		{input: "010203040506", slen: 6, size: 0x010203040506},
   248  		{input: "01020304050607", slen: 7, size: 0x01020304050607},
   249  		{input: "0102030405060708", slen: 8, size: 0x0102030405060708},
   250  	}
   251  
   252  	for _, test := range tests {
   253  		size, err := readSize(unhex(test.input), test.slen)
   254  		if err != test.err {
   255  			t.Errorf("readSize(%s, %d): error mismatch: got %q, want %q", test.input, test.slen, err, test.err)
   256  			continue
   257  		}
   258  		if size != test.size {
   259  			t.Errorf("readSize(%s, %d): size mismatch: got %#x, want %#x", test.input, test.slen, size, test.size)
   260  		}
   261  	}
   262  }
   263  
   264  func TestAppendUint64(t *testing.T) {
   265  	tests := []struct {
   266  		input  uint64
   267  		slice  []byte
   268  		output string
   269  	}{
   270  		{0, nil, "80"},
   271  		{1, nil, "01"},
   272  		{2, nil, "02"},
   273  		{127, nil, "7F"},
   274  		{128, nil, "8180"},
   275  		{129, nil, "8181"},
   276  		{0xFFFFFF, nil, "83FFFFFF"},
   277  		{127, []byte{1, 2, 3}, "0102037F"},
   278  		{0xFFFFFF, []byte{1, 2, 3}, "01020383FFFFFF"},
   279  	}
   280  
   281  	for _, test := range tests {
   282  		x := AppendUint64(test.slice, test.input)
   283  		if !bytes.Equal(x, unhex(test.output)) {
   284  			t.Errorf("AppendUint64(%v, %d): got %x, want %s", test.slice, test.input, x, test.output)
   285  		}
   286  
   287  		// Check that IntSize returns the appended size.
   288  		length := len(x) - len(test.slice)
   289  		if s := IntSize(test.input); s != length {
   290  			t.Errorf("IntSize(%d): got %d, want %d", test.input, s, length)
   291  		}
   292  	}
   293  }
   294  
   295  func TestAppendUint64Random(t *testing.T) {
   296  	fn := func(i uint64) bool {
   297  		enc, _ := EncodeToBytes(i)
   298  		encAppend := AppendUint64(nil, i)
   299  		return bytes.Equal(enc, encAppend)
   300  	}
   301  	config := quick.Config{MaxCountScale: 50}
   302  	if err := quick.Check(fn, &config); err != nil {
   303  		t.Fatal(err)
   304  	}
   305  }
   306  
   307  func TestBytesSize(t *testing.T) {
   308  	tests := []struct {
   309  		v    []byte
   310  		size uint64
   311  	}{
   312  		{v: []byte{}, size: 1},
   313  		{v: []byte{0x1}, size: 1},
   314  		{v: []byte{0x7E}, size: 1},
   315  		{v: []byte{0x7F}, size: 1},
   316  		{v: []byte{0x80}, size: 2},
   317  		{v: []byte{0xFF}, size: 2},
   318  		{v: []byte{0xFF, 0xF0}, size: 3},
   319  		{v: make([]byte, 55), size: 56},
   320  		{v: make([]byte, 56), size: 58},
   321  	}
   322  
   323  	for _, test := range tests {
   324  		s := BytesSize(test.v)
   325  		if s != test.size {
   326  			t.Errorf("BytesSize(%#x) -> %d, want %d", test.v, s, test.size)
   327  		}
   328  		s = StringSize(string(test.v))
   329  		if s != test.size {
   330  			t.Errorf("StringSize(%#x) -> %d, want %d", test.v, s, test.size)
   331  		}
   332  		// Sanity check:
   333  		enc, _ := EncodeToBytes(test.v)
   334  		if uint64(len(enc)) != test.size {
   335  			t.Errorf("len(EncodeToBytes(%#x)) -> %d, test says %d", test.v, len(enc), test.size)
   336  		}
   337  	}
   338  }