aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Newbery <john@johnnewbery.com>2017-02-07 12:57:37 -0500
committerJohn Newbery <john@johnnewbery.com>2017-03-07 16:45:09 -0500
commita012087667edb35a36f25ae06b42b1644d80e649 (patch)
tree38f3fb5b4d2a65acbc926c3661cbf59b7feda8be
parent960bc7f778d8dd618e65f1e37ec734e2d4734051 (diff)
downloadbitcoin-a012087667edb35a36f25ae06b42b1644d80e649.tar.xz
Return correct error codes in setban().
The setban() RPC was returning misleading or incorrect error codes (for example RPC_CLIENT_NODE_ALREADY_ADDED when an invalid IP address was entered). This commit fixes those error codes: - RPC_CLIENT_INVALID_IP_OR_SUBNET should be returned if the client enters an invalid IP address or subnet. This commit also updates the test cases to explicitly test the error code. This commit also adds a testcase for trying to setban on an invalid subnet.
-rwxr-xr-xqa/rpc-tests/nodehandling.py14
-rw-r--r--src/rpc/net.cpp4
2 files changed, 8 insertions, 10 deletions
diff --git a/qa/rpc-tests/nodehandling.py b/qa/rpc-tests/nodehandling.py
index 89059d2760..a6b10a0d83 100755
--- a/qa/rpc-tests/nodehandling.py
+++ b/qa/rpc-tests/nodehandling.py
@@ -29,15 +29,13 @@ class NodeHandlingTest (BitcoinTestFramework):
assert_equal(len(self.nodes[2].listbanned()), 0)
self.nodes[2].setban("127.0.0.0/24", "add")
assert_equal(len(self.nodes[2].listbanned()), 1)
- try:
- self.nodes[2].setban("127.0.0.1", "add") #throws exception because 127.0.0.1 is within range 127.0.0.0/24
- except:
- pass
+ # This will throw an exception because 127.0.0.1 is within range 127.0.0.0/24
+ assert_raises_jsonrpc(-23, "IP/Subnet already banned", self.nodes[2].setban, "127.0.0.1", "add")
+ # This will throw an exception because 127.0.0.1/42 is not a real subnet
+ assert_raises_jsonrpc(-30, "Error: Invalid IP/Subnet", self.nodes[2].setban, "127.0.0.1/42", "add")
assert_equal(len(self.nodes[2].listbanned()), 1) #still only one banned ip because 127.0.0.1 is within the range of 127.0.0.0/24
- try:
- self.nodes[2].setban("127.0.0.1", "remove")
- except:
- pass
+ # This will throw an exception because 127.0.0.1 was not added above
+ assert_raises_jsonrpc(-30, "Error: Unban failed", self.nodes[2].setban, "127.0.0.1", "remove")
assert_equal(len(self.nodes[2].listbanned()), 1)
self.nodes[2].setban("127.0.0.0/24", "remove")
assert_equal(len(self.nodes[2].listbanned()), 0)
diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp
index a7169749c3..63836f1684 100644
--- a/src/rpc/net.cpp
+++ b/src/rpc/net.cpp
@@ -506,7 +506,7 @@ UniValue setban(const JSONRPCRequest& request)
LookupSubNet(request.params[0].get_str().c_str(), subNet);
if (! (isSubnet ? subNet.IsValid() : netAddr.IsValid()) )
- throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Invalid IP/Subnet");
+ throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Invalid IP/Subnet");
if (strCommand == "add")
{
@@ -526,7 +526,7 @@ UniValue setban(const JSONRPCRequest& request)
else if(strCommand == "remove")
{
if (!( isSubnet ? g_connman->Unban(subNet) : g_connman->Unban(netAddr) ))
- throw JSONRPCError(RPC_MISC_ERROR, "Error: Unban failed");
+ throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Unban failed. Requested address/subnet was not previously banned.");
}
return NullUniValue;
}