Skip to content

Commit

Permalink
Fix RPC failure testing
Browse files Browse the repository at this point in the history
Make sure that RPC tests are actually checking failures correctly by:

- Catching JSON RPC exceptions and verifying the error codes and messages.
- Failing the test case if the JSON RPC exception isn't raised.
  • Loading branch information
jnewbery committed Feb 8, 2017
1 parent 729de15 commit 9db8eec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion qa/rpc-tests/rpcnamedargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def run_test(self):
h = node.help(command='getinfo')
assert(h.startswith('getinfo\n'))

assert_raises_jsonrpc(-8, node.help, random='getinfo')
assert_raises_jsonrpc(-8, 'Unknown named parameter', node.help, random='getinfo')

h = node.getblockhash(height=0)
node.getblock(blockhash=h)
Expand Down
23 changes: 20 additions & 3 deletions qa/rpc-tests/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,30 @@ def assert_raises_message(exc, message, fun, *args, **kwds):
else:
raise AssertionError("No exception raised")

def assert_raises_jsonrpc(code, fun, *args, **kwds):
'''Check for specific JSONRPC exception code'''
def assert_raises_jsonrpc(code, message, fun, *args, **kwds):
"""Run an RPC and verify that a specific JSONRPC exception code and message is raised.
Calls function `fun` with arguments `args` and `kwds`. Catches a JSONRPCException
and verifies that the error code and message are as expected. Throws AssertionError if
no JSONRPCException was returned or if the error code/message are not as expected.
Args:
code (int), optional: the error code returned by the RPC call (defined
in src/rpc/protocol.h). Set to None if checking the error code is not required.
message (string), optional: [a substring of] the error string returned by the
RPC call. Set to None if checking the error string is not required
fun (function): the function to call. This should be the name of an RPC.
args*: positional arguments for the function.
kwds**: named arguments for the function.
"""
try:
fun(*args, **kwds)
except JSONRPCException as e:
if e.error["code"] != code:
# JSONRPCException was thrown as expected. Check the code and message values are correct.
if (code is not None) and (code != e.error["code"]):
raise AssertionError("Unexpected JSONRPC error code %i" % e.error["code"])
if (message is not None) and (message not in e.error['message']):
raise AssertionError("Expected substring not found:"+e.error['message'])
except Exception as e:
raise AssertionError("Unexpected exception raised: "+type(e).__name__)
else:
Expand Down

0 comments on commit 9db8eec

Please sign in to comment.