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.
This commit is contained in:
John Newbery 2017-02-07 12:57:37 -05:00
parent 960bc7f778
commit a012087667
2 changed files with 8 additions and 10 deletions

View file

@ -29,15 +29,13 @@ class NodeHandlingTest (BitcoinTestFramework):
assert_equal(len(self.nodes[2].listbanned()), 0) assert_equal(len(self.nodes[2].listbanned()), 0)
self.nodes[2].setban("127.0.0.0/24", "add") self.nodes[2].setban("127.0.0.0/24", "add")
assert_equal(len(self.nodes[2].listbanned()), 1) assert_equal(len(self.nodes[2].listbanned()), 1)
try: # This will throw an exception because 127.0.0.1 is within range 127.0.0.0/24
self.nodes[2].setban("127.0.0.1", "add") #throws 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")
except: # This will throw an exception because 127.0.0.1/42 is not a real subnet
pass 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 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: # This will throw an exception because 127.0.0.1 was not added above
self.nodes[2].setban("127.0.0.1", "remove") assert_raises_jsonrpc(-30, "Error: Unban failed", self.nodes[2].setban, "127.0.0.1", "remove")
except:
pass
assert_equal(len(self.nodes[2].listbanned()), 1) assert_equal(len(self.nodes[2].listbanned()), 1)
self.nodes[2].setban("127.0.0.0/24", "remove") self.nodes[2].setban("127.0.0.0/24", "remove")
assert_equal(len(self.nodes[2].listbanned()), 0) assert_equal(len(self.nodes[2].listbanned()), 0)

View file

@ -506,7 +506,7 @@ UniValue setban(const JSONRPCRequest& request)
LookupSubNet(request.params[0].get_str().c_str(), subNet); LookupSubNet(request.params[0].get_str().c_str(), subNet);
if (! (isSubnet ? subNet.IsValid() : netAddr.IsValid()) ) 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") if (strCommand == "add")
{ {
@ -526,7 +526,7 @@ UniValue setban(const JSONRPCRequest& request)
else if(strCommand == "remove") else if(strCommand == "remove")
{ {
if (!( isSubnet ? g_connman->Unban(subNet) : g_connman->Unban(netAddr) )) 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; return NullUniValue;
} }