Skip to content

Conversation

jnewbery
Copy link
Contributor

@jnewbery jnewbery commented Feb 7, 2017

RPC interface testing should include failure test cases (ie where we call an RPC with parameters that we expect to fail). The failure test cases should follow this pattern:

try:
    nodes.rpcfunction(paramters,...) 
except JSONRPCException as exp: #(1) must only catch JSONRPCExceptions
    assert_equal(exp.error["code"], EXPECTED_ERROR_CODE)  #(2) must verify error code
    assert_equal(exp.error["message"], "Expected error message")  #(3) must verify error message
else:
    assert(False) #(4) must fail the test if no JSONRPCException raised

Unfortunately, many of the test cases in qa/rpc-tests get this pattern wrong and don't actually test what they're supposed to.

Exhibit A:

        try:
            self.nodes[0].generatetoaddress(1, 'mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ')
        except JSONRPCException as e:
            assert("Invalid address" not in e.error['message'])
            assert("ProcessNewBlock, block not accepted" not in e.error['message'])
            assert("Couldn't create new block" not in e.error['message'])

The call to generatetoaddress() actually succeeds here, but there isn't an else: clause so the test continues (and none of the code in the except: branch is executing)

Exhibit B:

        try:
            tmpl = self.nodes[0].getblocktemplate({})
            assert(len(tmpl['transactions']) == 1)  # Doesn't include witness tx
            assert(tmpl['sigoplimit'] == 20000)
            assert(tmpl['transactions'][0]['hash'] == txid)
            assert(tmpl['transactions'][0]['sigops'] == 2)
            assert(('!segwit' in tmpl['rules']) or ('segwit' not in tmpl['rules']))
        except JSONRPCException:
            # This is an acceptable outcome
            pass

The call to getblocktemplate() is raising an exception. Apparently This is an acceptable outcome, but there's no explanation why. None of the asserts in the try: branch are being executed. There's also no checking of the error code/message of the JSONRPCException.

Exhibit C:

assert_raises(JSONRPCException, self.nodes[0].gettransaction, [txid3])
#there must be a expection because the unconfirmed wallettx0 must be gone by now

This is using the helper function assert_raises() and the comment explains that the call should fail because the unconfirmed transaction is gone. There's no testing of the error message or code. In fact, gettransaction() is failing because it's being called with an array [txid3] instead of a string txid3.

This PR improves the assert_raises_jsonrpc() to verify error cause and error message and improves the commenting of that function. Future PRs will go over all the qa tests, remove the various broken implementations of this pattern and replace them with calls to assert_raises_jsonrpc().

This should also prevent the same bugs from being introduced in future because it should now be easier to get this right (by calling assert_raises_jsonrpc()) than get it wrong (by trying to reimplement the pattern and failing)

@fanquake fanquake added the Tests label Feb 7, 2017
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 argumemts for the function.
kwargs**: named arguments for the function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kwds

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 `kwargs`. Catches a JSONRPCException
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kwds?

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 argumemts for the function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arguments

@paveljanik
Copy link
Contributor

Concept ACK

@maflcko
Copy link
Member

maflcko commented Feb 7, 2017 via email

@jnewbery
Copy link
Contributor Author

jnewbery commented Feb 7, 2017

it would be still easy to get it wrong by ignoring (or not knowing) that assert_raises* exists

Indeed. There's no way to stop people from doing this again in future. I think the best we can hope for is to remove all existing cases where try/except has been used explicitly in individual testcases for JSON RPC error testing. When people come to write new testcases and copy the existing ones, they see the assert_raises* functions being used.

@jnewbery
Copy link
Contributor Author

jnewbery commented Feb 7, 2017

Thanks @paveljanik - all typos fixed

@maflcko
Copy link
Member

maflcko commented Feb 7, 2017 via email

@jnewbery
Copy link
Contributor Author

jnewbery commented Feb 7, 2017

@MarcoFalke done. It now explicitly tests code and message for not being None.

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.
@jnewbery
Copy link
Contributor Author

jnewbery commented Feb 8, 2017

On @TheBlueMatt's suggestion in #9713, I've added a new commit that removes the assert_raises() and assert_raises_message() functions.

@jnewbery
Copy link
Contributor Author

jnewbery commented Feb 8, 2017

Actually, on second thoughts I'll save removing assert_raises() and assert_raises_message() for a future PR.

@TheBlueMatt
Copy link
Contributor

OK, sounds good. utACK 9db8eec

@jnewbery
Copy link
Contributor Author

Thanks @TheBlueMatt.

@MarcoFalke any chance of merging this small PR? I have a lot of fixes that build off this so it'd be good to have this merged into master.

@maflcko
Copy link
Member

maflcko commented Feb 10, 2017

utACK 9db8eec. This is refactoring, so can go into 0.14.

@maflcko maflcko merged commit 9db8eec into bitcoin:master Feb 10, 2017
maflcko pushed a commit that referenced this pull request Feb 10, 2017
9db8eec Fix RPC failure testing (John Newbery)
@jnewbery
Copy link
Contributor Author

awsome. Thanks @MarcoFalke

@jnewbery jnewbery deleted the rpctestassert branch February 10, 2017 17:48
@jtimon
Copy link
Contributor

jtimon commented Mar 7, 2017

late utACK 9db8eec

codablock pushed a commit to codablock/dash that referenced this pull request Jan 23, 2018
9db8eec Fix RPC failure testing (John Newbery)
andvgal pushed a commit to energicryptocurrency/gen2-energi that referenced this pull request Jan 6, 2019
9db8eec Fix RPC failure testing (John Newbery)
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 21, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 29, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9
CryptoCentric pushed a commit to absolute-community/absolute that referenced this pull request Feb 27, 2019
9db8eec Fix RPC failure testing (John Newbery)
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 10, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 10, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 11, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 11, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 12, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 13, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Mar 13, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9

s/serialize_with_witness/serialize/

miss a tx_submit -> sendrawtransaction

dropped dip4

fix prioritise_transaction.py and nulldummy.py
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 13, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9

s/serialize_with_witness/serialize/

miss a tx_submit -> sendrawtransaction

dropped dip4
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 14, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9

s/serialize_with_witness/serialize/

miss a tx_submit -> sendrawtransaction

dropped dip4
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 14, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9

s/serialize_with_witness/serialize/

miss a tx_submit -> sendrawtransaction

dropped dip4

Fix tests

Signed-off-by: Pasta <Pasta@dash.org>
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 15, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9

s/serialize_with_witness/serialize/

miss a tx_submit -> sendrawtransaction

dropped dip4

Fix tests

Signed-off-by: Pasta <Pasta@dash.org>
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 16, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9

s/serialize_with_witness/serialize/

miss a tx_submit -> sendrawtransaction

dropped dip4

Fix tests

Signed-off-by: Pasta <Pasta@dash.org>

Fix file permissions
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 3, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9

s/serialize_with_witness/serialize/

miss a tx_submit -> sendrawtransaction

dropped dip4

Fix tests

Signed-off-by: Pasta <Pasta@dash.org>

Fix file permissions
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 3, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9

s/serialize_with_witness/serialize/

miss a tx_submit -> sendrawtransaction

dropped dip4

Fix tests

Signed-off-by: Pasta <Pasta@dash.org>
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 5, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9

s/serialize_with_witness/serialize/

miss a tx_submit -> sendrawtransaction

dropped dip4

Fix tests

Signed-off-by: Pasta <Pasta@dash.org>

Fix file permissions
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request May 6, 2019
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9

s/serialize_with_witness/serialize/

miss a tx_submit -> sendrawtransaction

dropped dip4

Fix tests

Signed-off-by: Pasta <Pasta@dash.org>

Fix file permissions
barrystyle pushed a commit to PACGlobalOfficial/PAC that referenced this pull request Jan 22, 2020
…9707)

c9bd0f6 Fix RPC failure testing (2 of 2) (John Newbery)

Tree-SHA512: df30e6e85abe8c4e12910dc60699f1201e9c243457abd738c1fdeac45f0ff05c674f68619ad9a47c847ec557954007d672cd89d3a9a3b2398dd188d9ffa6dcc9

s/serialize_with_witness/serialize/

miss a tx_submit -> sendrawtransaction

dropped dip4

Fix tests

Signed-off-by: Pasta <Pasta@dash.org>

Fix file permissions
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants