github.com/amazechain/amc@v0.1.3/internal/avm/rlp/raw_test.go (about)

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