Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rlp: add more tests for nil pointer / optional field encoding #26077

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 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 @@ -744,6 +754,30 @@ var decodeTests = []decodeTest{
ptr: new(optionalPtrField),
value: optionalPtrField{A: 1, B: &[3]byte{1, 2, 3}},
},
{
// all optional fields nil
input: "C0",
ptr: new(multipleOptionalFields),
value: multipleOptionalFields{A: nil, B: nil},
},
{
// all optional fields set
input: "C88301020383010203",
ptr: new(multipleOptionalFields),
value: multipleOptionalFields{A: &[3]byte{1, 2, 3}, B: &[3]byte{1, 2, 3}},
},
{
// nil optional field appears before a non-nil one
input: "C58083010203",
ptr: new(multipleOptionalFields),
error: "rlp: input string too short for [3]uint8, decoding into (rlp.multipleOptionalFields).A",
},
{
// decode a nil ptr into a ptr that is not nil or not optional
input: "C20180",
ptr: new(nonOptionalPtrField),
error: "rlp: input string too short for [3]uint8, decoding into (rlp.nonOptionalPtrField).B",
},
{
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: &multipleOptionalFields{A: nil, B: nil}, output: "C0"},
{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 will fail
{val: &nonOptionalPtrField{A: 1}, output: "C20180"}, // encodes without error but decode will fail

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