github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/rlp/raw_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 rlp
    13  
    14  import (
    15  	"bytes"
    16  	"io"
    17  	"reflect"
    18  	"testing"
    19  )
    20  
    21  func TestCountValues(t *testing.T) {
    22  	tests := []struct {
    23  		input string // note: spaces in input are stripped by unhex
    24  		count int
    25  		err   error
    26  	}{
    27  		// simple cases
    28  		{"", 0, nil},
    29  		{"00", 1, nil},
    30  		{"80", 1, nil},
    31  		{"C0", 1, nil},
    32  		{"01 02 03", 3, nil},
    33  		{"01 C406070809 02", 3, nil},
    34  		{"820101 820202 8403030303 04", 4, nil},
    35  
    36  		// size errors
    37  		{"8142", 0, ErrCanonSize},
    38  		{"01 01 8142", 0, ErrCanonSize},
    39  		{"02 84020202", 0, ErrValueTooLarge},
    40  
    41  		{
    42  			input: "A12000BF49F440A1CD0527E4D06E2765654C0F56452257516D793A9B8D604DCFDF2AB853F851808D10000000000000000000000000A056E81F171BCC55A6FF8345E692C0F86E5B48E01B996CADC001622FB5E363B421A0C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470",
    43  			count: 2,
    44  		},
    45  	}
    46  	for i, test := range tests {
    47  		count, err := CountValues(unhex(test.input))
    48  		if count != test.count {
    49  			t.Errorf("test %d: count mismatch, got %d want %d\ninput: %s", i, count, test.count, test.input)
    50  		}
    51  		if !reflect.DeepEqual(err, test.err) {
    52  			t.Errorf("test %d: err mismatch, got %q want %q\ninput: %s", i, err, test.err, test.input)
    53  		}
    54  	}
    55  }
    56  
    57  func TestSplitTypes(t *testing.T) {
    58  	if _, _, err := SplitString(unhex("C100")); err != ErrExpectedString {
    59  		t.Errorf("SplitString returned %q, want %q", err, ErrExpectedString)
    60  	}
    61  	if _, _, err := SplitList(unhex("01")); err != ErrExpectedList {
    62  		t.Errorf("SplitString returned %q, want %q", err, ErrExpectedList)
    63  	}
    64  	if _, _, err := SplitList(unhex("81FF")); err != ErrExpectedList {
    65  		t.Errorf("SplitString returned %q, want %q", err, ErrExpectedList)
    66  	}
    67  }
    68  
    69  func TestSplit(t *testing.T) {
    70  	tests := []struct {
    71  		input     string
    72  		kind      Kind
    73  		val, rest string
    74  		err       error
    75  	}{
    76  		{input: "01FFFF", kind: Byte, val: "01", rest: "FFFF"},
    77  		{input: "80FFFF", kind: String, val: "", rest: "FFFF"},
    78  		{input: "C3010203", kind: List, val: "010203"},
    79  
    80  		// errors
    81  		{input: "", err: io.ErrUnexpectedEOF},
    82  
    83  		{input: "8141", err: ErrCanonSize, rest: "8141"},
    84  		{input: "B800", err: ErrCanonSize, rest: "B800"},
    85  		{input: "B802FFFF", err: ErrCanonSize, rest: "B802FFFF"},
    86  		{input: "B90000", err: ErrCanonSize, rest: "B90000"},
    87  		{input: "B90055", err: ErrCanonSize, rest: "B90055"},
    88  		{input: "BA0002FFFF", err: ErrCanonSize, rest: "BA0002FFFF"},
    89  		{input: "F800", err: ErrCanonSize, rest: "F800"},
    90  		{input: "F90000", err: ErrCanonSize, rest: "F90000"},
    91  		{input: "F90055", err: ErrCanonSize, rest: "F90055"},
    92  		{input: "FA0002FFFF", err: ErrCanonSize, rest: "FA0002FFFF"},
    93  
    94  		{input: "81", err: ErrValueTooLarge, rest: "81"},
    95  		{input: "8501010101", err: ErrValueTooLarge, rest: "8501010101"},
    96  		{input: "C60607080902", err: ErrValueTooLarge, rest: "C60607080902"},
    97  
    98  		// size check overflow
    99  		{input: "BFFFFFFFFFFFFFFFFF", err: ErrValueTooLarge, rest: "BFFFFFFFFFFFFFFFFF"},
   100  		{input: "FFFFFFFFFFFFFFFFFF", err: ErrValueTooLarge, rest: "FFFFFFFFFFFFFFFFFF"},
   101  
   102  		{
   103  			input: "B838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   104  			err:   ErrValueTooLarge,
   105  			rest:  "B838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   106  		},
   107  		{
   108  			input: "F838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   109  			err:   ErrValueTooLarge,
   110  			rest:  "F838FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   111  		},
   112  
   113  		// a few bigger values, just for kicks
   114  		{
   115  			input: "F839FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   116  			kind:  List,
   117  			val:   "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
   118  			rest:  "",
   119  		},
   120  		{
   121  			input: "F90211A060EF29F20CC1007AE6E9530AEE16F4B31F8F1769A2D1264EC995C6D1241868D6A07C62AB8AC9838F5F5877B20BB37B387BC2106E97A3D52172CBEDB5EE17C36008A00EAB6B7324AADC0F6047C6AFC8229F09F7CF451B51D67C8DFB08D49BA8C3C626A04453343B2F3A6E42FCF87948F88AF7C8FC16D0C2735CBA7F026836239AB2C15FA024635C7291C882CE4C0763760C1A362DFC3FFCD802A55722236DE058D74202ACA0A220C808DE10F55E40AB25255201CFF009EA181D3906638E944EE2BF34049984A08D325AB26796F1CCB470F69C0F842501DC35D368A0C2575B2D243CFD1E8AB0FDA0B5298FF60DA5069463D610513C9F04F24051348391A143AFFAB7197DFACDEA72A02D2A7058A4463F8FB69378369E11EF33AE3252E2DB86CB545B36D3C26DDECE5AA0888F97BCA8E0BD83DC5B3B91CFF5FAF2F66F9501010682D67EF4A3B4E66115FBA0E8175A60C93BE9ED02921958F0EA55DA0FB5E4802AF5846147BAD92BC2D8AF26A08B3376FF433F3A4250FA64B7F804004CAC5807877D91C4427BD1CD05CF912ED8A09B32EF0F03BD13C37FF950C0CCCEFCCDD6669F2E7F2AA5CB859928E84E29763EA09BBA5E46610C8C8B1F8E921E5691BF8C7E40D75825D5EA3217AA9C3A8A355F39A0EEB95BC78251CCCEC54A97F19755C4A59A293544EEE6119AFA50531211E53C4FA00B6E86FE150BF4A9E0FEEE9C90F5465E617A861BB5E357F942881EE762212E2580",
   122  			kind:  List,
   123  			val:   "A060EF29F20CC1007AE6E9530AEE16F4B31F8F1769A2D1264EC995C6D1241868D6A07C62AB8AC9838F5F5877B20BB37B387BC2106E97A3D52172CBEDB5EE17C36008A00EAB6B7324AADC0F6047C6AFC8229F09F7CF451B51D67C8DFB08D49BA8C3C626A04453343B2F3A6E42FCF87948F88AF7C8FC16D0C2735CBA7F026836239AB2C15FA024635C7291C882CE4C0763760C1A362DFC3FFCD802A55722236DE058D74202ACA0A220C808DE10F55E40AB25255201CFF009EA181D3906638E944EE2BF34049984A08D325AB26796F1CCB470F69C0F842501DC35D368A0C2575B2D243CFD1E8AB0FDA0B5298FF60DA5069463D610513C9F04F24051348391A143AFFAB7197DFACDEA72A02D2A7058A4463F8FB69378369E11EF33AE3252E2DB86CB545B36D3C26DDECE5AA0888F97BCA8E0BD83DC5B3B91CFF5FAF2F66F9501010682D67EF4A3B4E66115FBA0E8175A60C93BE9ED02921958F0EA55DA0FB5E4802AF5846147BAD92BC2D8AF26A08B3376FF433F3A4250FA64B7F804004CAC5807877D91C4427BD1CD05CF912ED8A09B32EF0F03BD13C37FF950C0CCCEFCCDD6669F2E7F2AA5CB859928E84E29763EA09BBA5E46610C8C8B1F8E921E5691BF8C7E40D75825D5EA3217AA9C3A8A355F39A0EEB95BC78251CCCEC54A97F19755C4A59A293544EEE6119AFA50531211E53C4FA00B6E86FE150BF4A9E0FEEE9C90F5465E617A861BB5E357F942881EE762212E2580",
   124  			rest:  "",
   125  		},
   126  		{
   127  			input: "F877A12000BF49F440A1CD0527E4D06E2765654C0F56452257516D793A9B8D604DCFDF2AB853F851808D10000000000000000000000000A056E81F171BCC55A6FF8345E692C0F86E5B48E01B996CADC001622FB5E363B421A0C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470",
   128  			kind:  List,
   129  			val:   "A12000BF49F440A1CD0527E4D06E2765654C0F56452257516D793A9B8D604DCFDF2AB853F851808D10000000000000000000000000A056E81F171BCC55A6FF8345E692C0F86E5B48E01B996CADC001622FB5E363B421A0C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470",
   130  			rest:  "",
   131  		},
   132  	}
   133  
   134  	for i, test := range tests {
   135  		kind, val, rest, err := Split(unhex(test.input))
   136  		if kind != test.kind {
   137  			t.Errorf("test %d: kind mismatch: got %v, want %v", i, kind, test.kind)
   138  		}
   139  		if !bytes.Equal(val, unhex(test.val)) {
   140  			t.Errorf("test %d: val mismatch: got %x, want %s", i, val, test.val)
   141  		}
   142  		if !bytes.Equal(rest, unhex(test.rest)) {
   143  			t.Errorf("test %d: rest mismatch: got %x, want %s", i, rest, test.rest)
   144  		}
   145  		if err != test.err {
   146  			t.Errorf("test %d: error mismatch: got %q, want %q", i, err, test.err)
   147  		}
   148  	}
   149  }
   150  
   151  func TestReadSize(t *testing.T) {
   152  	tests := []struct {
   153  		input string
   154  		slen  byte
   155  		size  uint64
   156  		err   error
   157  	}{
   158  		{input: "", slen: 1, err: io.ErrUnexpectedEOF},
   159  		{input: "FF", slen: 2, err: io.ErrUnexpectedEOF},
   160  		{input: "00", slen: 1, err: ErrCanonSize},
   161  		{input: "36", slen: 1, err: ErrCanonSize},
   162  		{input: "37", slen: 1, err: ErrCanonSize},
   163  		{input: "38", slen: 1, size: 0x38},
   164  		{input: "FF", slen: 1, size: 0xFF},
   165  		{input: "FFFF", slen: 2, size: 0xFFFF},
   166  		{input: "FFFFFF", slen: 3, size: 0xFFFFFF},
   167  		{input: "FFFFFFFF", slen: 4, size: 0xFFFFFFFF},
   168  		{input: "FFFFFFFFFF", slen: 5, size: 0xFFFFFFFFFF},
   169  		{input: "FFFFFFFFFFFF", slen: 6, size: 0xFFFFFFFFFFFF},
   170  		{input: "FFFFFFFFFFFFFF", slen: 7, size: 0xFFFFFFFFFFFFFF},
   171  		{input: "FFFFFFFFFFFFFFFF", slen: 8, size: 0xFFFFFFFFFFFFFFFF},
   172  		{input: "0102", slen: 2, size: 0x0102},
   173  		{input: "010203", slen: 3, size: 0x010203},
   174  		{input: "01020304", slen: 4, size: 0x01020304},
   175  		{input: "0102030405", slen: 5, size: 0x0102030405},
   176  		{input: "010203040506", slen: 6, size: 0x010203040506},
   177  		{input: "01020304050607", slen: 7, size: 0x01020304050607},
   178  		{input: "0102030405060708", slen: 8, size: 0x0102030405060708},
   179  	}
   180  
   181  	for _, test := range tests {
   182  		size, err := readSize(unhex(test.input), test.slen)
   183  		if err != test.err {
   184  			t.Errorf("readSize(%s, %d): error mismatch: got %q, want %q", test.input, test.slen, err, test.err)
   185  			continue
   186  		}
   187  		if size != test.size {
   188  			t.Errorf("readSize(%s, %d): size mismatch: got %#x, want %#x", test.input, test.slen, size, test.size)
   189  		}
   190  	}
   191  }