aboutsummaryrefslogtreecommitdiffstats
path: root/rlp/decode_test.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-04-17 07:16:46 +0800
committerFelix Lange <fjl@twurst.com>2015-04-17 20:45:09 +0800
commitcad64fb911e7029bef876f16e0956b3b0b4bb4d0 (patch)
tree89c0005bc28fe08cb54cab9c49800a21c2f1bf94 /rlp/decode_test.go
parent1e2c93aa2da453ef9548b9957b5ed453f60ce5ca (diff)
downloaddexon-cad64fb911e7029bef876f16e0956b3b0b4bb4d0.tar.gz
dexon-cad64fb911e7029bef876f16e0956b3b0b4bb4d0.tar.zst
dexon-cad64fb911e7029bef876f16e0956b3b0b4bb4d0.zip
rlp: stricter rules for structs and pointers
The rules have changed as follows: * When decoding into pointers, empty values no longer produce a nil pointer. This can be overriden for struct fields using the struct tag "nil". * When decoding into structs, the input list must contain an element for each field.
Diffstat (limited to 'rlp/decode_test.go')
-rw-r--r--rlp/decode_test.go65
1 files changed, 53 insertions, 12 deletions
diff --git a/rlp/decode_test.go b/rlp/decode_test.go
index 7e2ea2041..fd52bd1be 100644
--- a/rlp/decode_test.go
+++ b/rlp/decode_test.go
@@ -280,7 +280,7 @@ type simplestruct struct {
type recstruct struct {
I uint
- Child *recstruct
+ Child *recstruct `rlp:"nil"`
}
var (
@@ -390,15 +390,33 @@ var decodeTests = []decodeTest{
{input: "8105", ptr: new(big.Int), error: "rlp: non-canonical size information for *big.Int"},
// structs
- {input: "C0", ptr: new(simplestruct), value: simplestruct{0, ""}},
- {input: "C105", ptr: new(simplestruct), value: simplestruct{5, ""}},
- {input: "C50583343434", ptr: new(simplestruct), value: simplestruct{5, "444"}},
{
- input: "C501C302C103",
+ input: "C50583343434",
+ ptr: new(simplestruct),
+ value: simplestruct{5, "444"},
+ },
+ {
+ input: "C601C402C203C0",
ptr: new(recstruct),
value: recstruct{1, &recstruct{2, &recstruct{3, nil}}},
},
+ // struct errors
+ {
+ input: "C0",
+ ptr: new(simplestruct),
+ error: "rlp: too few elements for rlp.simplestruct",
+ },
+ {
+ input: "C105",
+ ptr: new(simplestruct),
+ error: "rlp: too few elements for rlp.simplestruct",
+ },
+ {
+ input: "C7C50583343434C0",
+ ptr: new([]*simplestruct),
+ error: "rlp: too few elements for rlp.simplestruct, decoding into ([]*rlp.simplestruct)[1]",
+ },
{
input: "83222222",
ptr: new(simplestruct),
@@ -417,19 +435,15 @@ var decodeTests = []decodeTest{
// pointers
{input: "00", ptr: new(*[]byte), value: &[]byte{0}},
- {input: "80", ptr: new(*uint), value: (*uint)(nil)},
- {input: "C0", ptr: new(*uint), value: (*uint)(nil)},
+ {input: "80", ptr: new(*uint), value: uintp(0)},
+ {input: "C0", ptr: new(*uint), error: "rlp: expected input string or byte for uint"},
{input: "07", ptr: new(*uint), value: uintp(7)},
{input: "8158", ptr: new(*uint), value: uintp(0x58)},
{input: "C109", ptr: new(*[]uint), value: &[]uint{9}},
{input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}},
// check that input position is advanced also for empty values.
- {input: "C3808005", ptr: new([]*uint), value: []*uint{nil, nil, uintp(5)}},
-
- // pointer should be reset to nil
- {input: "05", ptr: sharedPtr, value: uintp(5)},
- {input: "80", ptr: sharedPtr, value: (*uint)(nil)},
+ {input: "C3808005", ptr: new([]*uint), value: []*uint{uintp(0), uintp(0), uintp(5)}},
// interface{}
{input: "00", ptr: new(interface{}), value: []byte{0}},
@@ -599,6 +613,33 @@ func ExampleDecode() {
// Decoded value: rlp.example{A:0xa, B:0x14, private:0x0, String:"foobar"}
}
+func ExampleDecode_structTagNil() {
+ // In this example, we'll use the "nil" struct tag to change
+ // how a pointer-typed field is decoded. The input contains an RLP
+ // list of one element, an empty string.
+ input := []byte{0xC1, 0x80}
+
+ // This type uses the normal rules.
+ // The empty input string is decoded as a pointer to an empty Go string.
+ var normalRules struct {
+ String *string
+ }
+ Decode(bytes.NewReader(input), &normalRules)
+ fmt.Printf("normal: String = %q\n", *normalRules.String)
+
+ // This type uses the struct tag.
+ // The empty input string is decoded as a nil pointer.
+ var withEmptyOK struct {
+ String *string `rlp:"nil"`
+ }
+ Decode(bytes.NewReader(input), &withEmptyOK)
+ fmt.Printf("with nil tag: String = %v\n", withEmptyOK.String)
+
+ // Output:
+ // normal: String = ""
+ // with nil tag: String = <nil>
+}
+
func ExampleStream() {
input, _ := hex.DecodeString("C90A1486666F6F626172")
s := NewStream(bytes.NewReader(input), 0)