|
| 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