Skip to content

Commit

Permalink
Fix Go decoding of (successful) CBOR-RPC responses.
Browse files Browse the repository at this point in the history
This doesn't affect much; Go code will usually use a direct connection or
the restclient interface.
  • Loading branch information
David Maze committed Jul 16, 2016
1 parent d5f977c commit 4ca8434
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cborrpc/cborrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions cborrpc/cborrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
5 changes: 5 additions & 0 deletions doc/changes.md
Original file line number Diff line number Diff line change
@@ -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)
-------------------

Expand Down

0 comments on commit 4ca8434

Please sign in to comment.