mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
rlp: fix off by one-flaw, make tests more idiomatic
This commit is contained in:
parent
ab6ad71b4b
commit
f59bd61282
2 changed files with 15 additions and 14 deletions
|
|
@ -336,8 +336,8 @@ func EndList(buf []byte, offset int) []byte {
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
headerSize := intsize(uint64(contentSize)) + 1
|
headerSize := intsize(uint64(contentSize)) + 1
|
||||||
// shift the content for room of list header
|
// shift the content for room of list header (minus the one byte already written)
|
||||||
buf = append(buf[:offset+headerSize], buf[offset:]...)
|
buf = append(buf[:offset+headerSize-1], buf[offset:]...)
|
||||||
// write list header. OBS! This call ignores the return value,
|
// write list header. OBS! This call ignores the return value,
|
||||||
// since the append operation is performed on buf[:offset], and we
|
// since the append operation is performed on buf[:offset], and we
|
||||||
// already just moved the content, we know that the append operation
|
// already just moved the content, we know that the append operation
|
||||||
|
|
|
||||||
|
|
@ -340,22 +340,21 @@ func TestAppendString(t *testing.T) {
|
||||||
func TestEndList(t *testing.T) {
|
func TestEndList(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
input interface{}
|
input interface{}
|
||||||
slice []byte
|
|
||||||
output string
|
output string
|
||||||
}{
|
}{
|
||||||
{[]interface{}{}, nil, "C0"},
|
{[]interface{}{}, "C0"},
|
||||||
{[]interface{}{1, 2, 3}, nil, "C3010203"},
|
{[]interface{}{1, 2, 3}, "C3010203"},
|
||||||
{
|
{
|
||||||
// [ [], [[]], [ [], [[]] ] ]
|
// [ [], [[]], [ [], [[]] ] ]
|
||||||
[]interface{}{[]interface{}{}, []interface{}{[]interface{}{}}, []interface{}{[]interface{}{}, []interface{}{[]interface{}{}}}}, nil,
|
[]interface{}{[]interface{}{}, []interface{}{[]interface{}{}}, []interface{}{[]interface{}{}, []interface{}{[]interface{}{}}}},
|
||||||
"C7C0C1C0C3C0C1C0",
|
"C7C0C1C0C3C0C1C0",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
[]interface{}{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo"}, nil,
|
[]interface{}{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo"},
|
||||||
"F83C836161618362626283636363836464648365656583666666836767678368686883696969836A6A6A836B6B6B836C6C6C836D6D6D836E6E6E836F6F6F",
|
"F83C836161618362626283636363836464648365656583666666836767678368686883696969836A6A6A836B6B6B836C6C6C836D6D6D836E6E6E836F6F6F",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
[]interface{}{1, 0xFFFFFF, []interface{}{[]interface{}{4, 5, 5}}, "abc"}, nil,
|
[]interface{}{1, 0xFFFFFF, []interface{}{[]interface{}{4, 5, 5}}, "abc"},
|
||||||
"CE0183FFFFFFC4C304050583616263",
|
"CE0183FFFFFFC4C304050583616263",
|
||||||
},
|
},
|
||||||
{[]interface{}{
|
{[]interface{}{
|
||||||
|
|
@ -390,7 +389,7 @@ func TestEndList(t *testing.T) {
|
||||||
[]interface{}{"asdf", "qwer", "zxcv"},
|
[]interface{}{"asdf", "qwer", "zxcv"},
|
||||||
[]interface{}{"asdf", "qwer", "zxcv"},
|
[]interface{}{"asdf", "qwer", "zxcv"},
|
||||||
[]interface{}{"asdf", "qwer", "zxcv"},
|
[]interface{}{"asdf", "qwer", "zxcv"},
|
||||||
[]interface{}{"asdf", "qwer", "zxcv"}}, nil,
|
[]interface{}{"asdf", "qwer", "zxcv"}},
|
||||||
"F90200CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376"},
|
"F90200CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -398,7 +397,8 @@ func TestEndList(t *testing.T) {
|
||||||
encode = func(buf []byte, v interface{}) []byte {
|
encode = func(buf []byte, v interface{}) []byte {
|
||||||
switch v := v.(type) {
|
switch v := v.(type) {
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
offset := len(buf)
|
var offset int
|
||||||
|
buf, offset = StartList(buf)
|
||||||
for _, c := range v {
|
for _, c := range v {
|
||||||
buf = encode(buf, c)
|
buf = encode(buf, c)
|
||||||
}
|
}
|
||||||
|
|
@ -416,10 +416,11 @@ func TestEndList(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for i, test := range tests {
|
||||||
x := encode(test.slice, test.input)
|
have := encode(nil, test.input)
|
||||||
if !bytes.Equal(x, unhex(test.output)) {
|
want := unhex(test.output)
|
||||||
t.Errorf("TestEndList(%v, %d): got %x, want %s", test.slice, test.input, x, test.output)
|
if !bytes.Equal(have, want) {
|
||||||
|
t.Errorf("test %d: input %v \n\thave %x\n\twant %x", i, test.input, have, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue