Skip to content

Commit

Permalink
add tests that codify the cases where encoded output can't be decoded…
Browse files Browse the repository at this point in the history
… without error
  • Loading branch information
roberto-bayardo committed Nov 1, 2022
1 parent b0d4433 commit 0b714eb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
35 changes: 35 additions & 0 deletions rlp/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,16 @@ type optionalPtrField struct {
B *[3]byte `rlp:"optional"`
}

type nonOptionalPtrField struct {
A uint
B *[3]byte
}

type multipleOptionalFields struct {
A *[3]byte `rlp:"optional"`
B *[3]byte `rlp:"optional"`
}

type optionalPtrFieldNil struct {
A uint
B *[3]byte `rlp:"optional,nil"`
Expand Down Expand Up @@ -729,6 +739,13 @@ var decodeTests = []decodeTest{
ptr: new(optionalPtrField),
value: optionalPtrField{A: 1},
},
{
// try to decode a nil ptr into a ptr that is not nil or not optional
input: "C20180",
ptr: new(nonOptionalPtrField),
//value: optionalPtrField{A: 1},
error: "rlp: input string too short for [3]uint8, decoding into (rlp.nonOptionalPtrField).B",
},
{
input: "C20180", // not accepted because "optional" doesn't enable "nil"
ptr: new(optionalPtrField),
Expand All @@ -744,6 +761,24 @@ var decodeTests = []decodeTest{
ptr: new(optionalPtrField),
value: optionalPtrField{A: 1, B: &[3]byte{1, 2, 3}},
},
{
input: "C88301020383010203",
ptr: new(multipleOptionalFields),
value: multipleOptionalFields{A: &[3]byte{1, 2, 3}, B: &[3]byte{1, 2, 3}},
},
{
// test the case where a nil optional field appears *before* a non-nil one.
input: "C58083010203",
ptr: new(multipleOptionalFields),
//value: multipleOptionalFields{A: nil, B: &[3]byte{1, 2, 3}},
error: "rlp: input string too short for [3]uint8, decoding into (rlp.multipleOptionalFields).A",
},
{
// all nil
input: "C0",
ptr: new(multipleOptionalFields),
value: multipleOptionalFields{A: nil, B: nil},
},
{
input: "C101",
ptr: new(optionalPtrFieldNil),
Expand Down
4 changes: 4 additions & 0 deletions rlp/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ var encTests = []encTest{
{val: &optionalBigIntField{A: 1}, output: "C101"},
{val: &optionalPtrField{A: 1}, output: "C101"},
{val: &optionalPtrFieldNil{A: 1}, output: "C101"},
{val: &nonOptionalPtrField{A: 1}, output: "C20180"}, // encodes without error but decode fails -- should encode return error?
{val: &multipleOptionalFields{A: &[3]byte{1, 2, 3}, B: &[3]byte{1, 2, 3}}, output: "C88301020383010203"},
{val: &multipleOptionalFields{A: nil, B: &[3]byte{1, 2, 3}}, output: "C58083010203"}, // encodes without error but decode fails -- should encode return error?
{val: &multipleOptionalFields{A: nil, B: nil}, output: "C0"},

// nil
{val: (*uint)(nil), output: "80"},
Expand Down

0 comments on commit 0b714eb

Please sign in to comment.