Skip to content

Commit 9d79afe

Browse files
committed
add RPC tests for setban & disconnectnode
1 parent 1f02b80 commit 9d79afe

File tree

3 files changed

+71
-27
lines changed

3 files changed

+71
-27
lines changed

qa/pull-tester/rpc-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ testScripts=(
3232
'merkle_blocks.py'
3333
'signrawtransactions.py'
3434
'walletbackup.py'
35+
'nodehandling.py'
3536
);
3637
testScriptsExt=(
3738
'bipdersig-p2p.py'

qa/rpc-tests/httpbasics.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#
7-
# Test REST interface
7+
# Test rpc http basics
88
#
99

1010
from test_framework.test_framework import BitcoinTestFramework
@@ -98,31 +98,5 @@ def run_test(self):
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
100100

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-
self.nodes[2].setban("127.0.0.0/24", "add")
112-
assert_equal(len(self.nodes[2].listbanned()), 1)
113-
try:
114-
self.nodes[2].setban("127.0.0.1", "add") #throws exception because 127.0.0.1 is within range 127.0.0.0/24
115-
except:
116-
pass
117-
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
118-
try:
119-
self.nodes[2].setban("127.0.0.1", "remove")
120-
except:
121-
pass
122-
assert_equal(len(self.nodes[2].listbanned()), 1)
123-
self.nodes[2].setban("127.0.0.0/24", "remove")
124-
assert_equal(len(self.nodes[2].listbanned()), 0)
125-
self.nodes[2].clearbanned()
126-
assert_equal(len(self.nodes[2].listbanned()), 0)
127101
if __name__ == '__main__':
128102
HTTPBasicsTest ().main ()

qa/rpc-tests/nodehandling.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python2
2+
# Copyright (c) 2014 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#
7+
# Test node handling
8+
#
9+
10+
from test_framework.test_framework import BitcoinTestFramework
11+
from test_framework.util import *
12+
import base64
13+
14+
try:
15+
import http.client as httplib
16+
except ImportError:
17+
import httplib
18+
try:
19+
import urllib.parse as urlparse
20+
except ImportError:
21+
import urlparse
22+
23+
class NodeHandlingTest (BitcoinTestFramework):
24+
def run_test(self):
25+
###########################
26+
# setban/listbanned tests #
27+
###########################
28+
assert_equal(len(self.nodes[2].getpeerinfo()), 4) #we should have 4 nodes at this point
29+
self.nodes[2].setban("127.0.0.1", "add")
30+
time.sleep(3) #wait till the nodes are disconected
31+
assert_equal(len(self.nodes[2].getpeerinfo()), 0) #all nodes must be disconnected at this point
32+
assert_equal(len(self.nodes[2].listbanned()), 1)
33+
self.nodes[2].clearbanned()
34+
assert_equal(len(self.nodes[2].listbanned()), 0)
35+
self.nodes[2].setban("127.0.0.0/24", "add")
36+
assert_equal(len(self.nodes[2].listbanned()), 1)
37+
try:
38+
self.nodes[2].setban("127.0.0.1", "add") #throws exception because 127.0.0.1 is within range 127.0.0.0/24
39+
except:
40+
pass
41+
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
42+
try:
43+
self.nodes[2].setban("127.0.0.1", "remove")
44+
except:
45+
pass
46+
assert_equal(len(self.nodes[2].listbanned()), 1)
47+
self.nodes[2].setban("127.0.0.0/24", "remove")
48+
assert_equal(len(self.nodes[2].listbanned()), 0)
49+
self.nodes[2].clearbanned()
50+
assert_equal(len(self.nodes[2].listbanned()), 0)
51+
52+
###########################
53+
# RPC disconnectnode test #
54+
###########################
55+
url = urlparse.urlparse(self.nodes[1].url)
56+
self.nodes[0].disconnectnode(url.hostname+":"+str(p2p_port(1)))
57+
time.sleep(2) #disconnecting a node needs a little bit of time
58+
for node in self.nodes[0].getpeerinfo():
59+
assert(node['addr'] != url.hostname+":"+str(p2p_port(1)))
60+
61+
connect_nodes_bi(self.nodes,0,1) #reconnect the node
62+
found = False
63+
for node in self.nodes[0].getpeerinfo():
64+
if node['addr'] == url.hostname+":"+str(p2p_port(1)):
65+
found = True
66+
assert(found)
67+
68+
if __name__ == '__main__':
69+
NodeHandlingTest ().main ()

0 commit comments

Comments
 (0)