Skip to content

Commit 1086ffb

Browse files
committed
[QA] add setban/listbanned/clearbanned tests
1 parent d930b26 commit 1086ffb

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

qa/rpc-tests/httpbasics.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,83 +20,94 @@
2020
except ImportError:
2121
import urlparse
2222

23-
class HTTPBasicsTest (BitcoinTestFramework):
23+
class HTTPBasicsTest (BitcoinTestFramework):
2424
def setup_nodes(self):
2525
return start_nodes(4, self.options.tmpdir, extra_args=[['-rpckeepalive=1'], ['-rpckeepalive=0'], [], []])
2626

27-
def run_test(self):
28-
27+
def run_test(self):
28+
2929
#################################################
3030
# lowlevel check for http persistent connection #
3131
#################################################
3232
url = urlparse.urlparse(self.nodes[0].url)
3333
authpair = url.username + ':' + url.password
3434
headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
35-
35+
3636
conn = httplib.HTTPConnection(url.hostname, url.port)
3737
conn.connect()
3838
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
3939
out1 = conn.getresponse().read();
4040
assert_equal('"error":null' in out1, True)
4141
assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
42-
42+
4343
#send 2nd request without closing connection
4444
conn.request('POST', '/', '{"method": "getchaintips"}', headers)
4545
out2 = conn.getresponse().read();
4646
assert_equal('"error":null' in out1, True) #must also response with a correct json-rpc message
4747
assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
4848
conn.close()
49-
49+
5050
#same should be if we add keep-alive because this should be the std. behaviour
5151
headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection": "keep-alive"}
52-
52+
5353
conn = httplib.HTTPConnection(url.hostname, url.port)
5454
conn.connect()
5555
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
5656
out1 = conn.getresponse().read();
5757
assert_equal('"error":null' in out1, True)
5858
assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
59-
59+
6060
#send 2nd request without closing connection
6161
conn.request('POST', '/', '{"method": "getchaintips"}', headers)
6262
out2 = conn.getresponse().read();
6363
assert_equal('"error":null' in out1, True) #must also response with a correct json-rpc message
6464
assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
6565
conn.close()
66-
66+
6767
#now do the same with "Connection: close"
6868
headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection":"close"}
69-
69+
7070
conn = httplib.HTTPConnection(url.hostname, url.port)
7171
conn.connect()
7272
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
7373
out1 = conn.getresponse().read();
7474
assert_equal('"error":null' in out1, True)
75-
assert_equal(conn.sock!=None, False) #now the connection must be closed after the response
76-
75+
assert_equal(conn.sock!=None, False) #now the connection must be closed after the response
76+
7777
#node1 (2nd node) is running with disabled keep-alive option
7878
urlNode1 = urlparse.urlparse(self.nodes[1].url)
7979
authpair = urlNode1.username + ':' + urlNode1.password
8080
headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
81-
81+
8282
conn = httplib.HTTPConnection(urlNode1.hostname, urlNode1.port)
8383
conn.connect()
8484
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
8585
out1 = conn.getresponse().read();
8686
assert_equal('"error":null' in out1, True)
8787
assert_equal(conn.sock!=None, False) #connection must be closed because keep-alive was set to false
88-
88+
8989
#node2 (third node) is running with standard keep-alive parameters which means keep-alive is off
9090
urlNode2 = urlparse.urlparse(self.nodes[2].url)
9191
authpair = urlNode2.username + ':' + urlNode2.password
9292
headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
93-
93+
9494
conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
9595
conn.connect()
9696
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
9797
out1 = conn.getresponse().read();
9898
assert_equal('"error":null' in out1, True)
9999
assert_equal(conn.sock!=None, True) #connection must be closed because bitcoind should use keep-alive by default
100-
100+
101+
###########################
102+
# setban/listbanned tests #
103+
###########################
104+
assert_equal(len(self.nodes[2].getpeerinfo()), 4); #we should have 4 nodes at this point
105+
self.nodes[2].setban("127.0.0.1", "add")
106+
time.sleep(3) #wait till the nodes are disconected
107+
assert_equal(len(self.nodes[2].getpeerinfo()), 0); #all nodes must be disconnected at this point
108+
assert_equal(len(self.nodes[2].listbanned()), 1);
109+
self.nodes[2].clearbanned()
110+
assert_equal(len(self.nodes[2].listbanned()), 0);
111+
101112
if __name__ == '__main__':
102113
HTTPBasicsTest ().main ()

src/test/rpc_tests.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,13 @@ BOOST_AUTO_TEST_CASE(rpc_boostasiotocnetaddr)
177177
BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("::ffff:127.0.0.1")).ToString(), "127.0.0.1");
178178
}
179179

180+
BOOST_AUTO_TEST_CASE(rpc_ban)
181+
{
182+
BOOST_CHECK_NO_THROW(CallRPC(string("setban 127.0.0.1 add")));
183+
BOOST_CHECK_THROW(CallRPC(string("setban 127.0.0.1:8334")), runtime_error); //portnumber for setban not allowed
184+
BOOST_CHECK_NO_THROW(CallRPC(string("listbanned")));
185+
BOOST_CHECK_NO_THROW(CallRPC(string("setban 127.0.0.1 remove")));
186+
BOOST_CHECK_NO_THROW(CallRPC(string("clearbanned")));
187+
}
188+
180189
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)