From 4ca843408bbeb9b1730e07e9ea394314604f12cc Mon Sep 17 00:00:00 2001 From: David Maze Date: Sat, 16 Jul 2016 15:02:59 -0400 Subject: [PATCH] Fix Go decoding of (successful) CBOR-RPC responses. This doesn't affect much; Go code will usually use a direct connection or the restclient interface. --- cborrpc/cborrpc.go | 6 ++++-- cborrpc/cborrpc_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ doc/changes.md | 5 +++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/cborrpc/cborrpc.go b/cborrpc/cborrpc.go index adae85c..f47a35b 100644 --- a/cborrpc/cborrpc.go +++ b/cborrpc/cborrpc.go @@ -128,8 +128,10 @@ func (x respExt) UpdateExt(dest interface{}, v interface{}) { response := dest.(*Response) response.ID = uint(wire["id"].(uint64)) response.Result = wire["result"] - errorDict := wire["error"].(map[string]interface{}) - response.Error = string(errorDict["message"].([]byte)) + if wire["error"] != nil { + errorDict := wire["error"].(map[string]interface{}) + response.Error = string(errorDict["message"].([]byte)) + } } // PythonTuple is a simple Go wrapper representing a Python tuple. diff --git a/cborrpc/cborrpc_test.go b/cborrpc/cborrpc_test.go index 9b7b190..3db27aa 100644 --- a/cborrpc/cborrpc_test.go +++ b/cborrpc/cborrpc_test.go @@ -377,6 +377,20 @@ func TestDecodeRegressions(t *testing.T) { Params: tests["params"].Value.([]interface{}), }, } + tests["responseMap"] = DeTest{ + Data: bytes.Join([][]byte{ + []byte{0xA2}, + DeTestByteString("id").Data, + []byte{0x02}, + DeTestByteString("result").Data, + []byte{0x81}, + DeTestByteString("x").Data, + }, []byte{}), + Value: map[interface{}]interface{}{ + "id": uint64(2), + "result": []interface{}{[]byte("x")}, + }, + } for name, test := range tests { decoder := codec.NewDecoderBytes(test.Data, cbor) @@ -387,3 +401,29 @@ func TestDecodeRegressions(t *testing.T) { } } } + +func TestDecodeResponse(t *testing.T) { + inner := bytes.Join([][]byte{ + []byte{0xA2}, + DeTestByteString("id").Data, + []byte{0x02}, + DeTestByteString("result").Data, + []byte{0x81}, + DeTestByteString("x").Data, + }, []byte{}) + outer := bytes.Join([][]byte{ + []byte{0xD8, 0x18, 0x58, byte(len(inner))}, + inner, + }, []byte{}) + decoder := codec.NewDecoderBytes(outer, cbor) + var actual Response + expected := Response{ + ID: 2, + Result: []interface{}{[]byte("x")}, + Error: "", + } + err := decoder.Decode(&actual) + if assert.NoError(t, err) { + assert.Equal(t, expected, actual) + } +} diff --git a/doc/changes.md b/doc/changes.md index 9fb68c7..5d1f9fe 100644 --- a/doc/changes.md +++ b/doc/changes.md @@ -1,6 +1,11 @@ Major Changes ============= +0.4.1 (TBD) +----------- + +* Bug fix in Go decoding of CBOR-RPC responses. + 0.4.0 (29 Jun 2016) -------------------