You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nodehandling.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014-2015 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. # Test node handling
  7. #
  8. from test_framework.test_framework import BitcoinTestFramework
  9. from test_framework.util import *
  10. import base64
  11. try:
  12. import http.client as httplib
  13. except ImportError:
  14. import httplib
  15. try:
  16. import urllib.parse as urlparse
  17. except ImportError:
  18. import urlparse
  19. class NodeHandlingTest (BitcoinTestFramework):
  20. def run_test(self):
  21. ###########################
  22. # setban/listbanned tests #
  23. ###########################
  24. assert_equal(len(self.nodes[2].getpeerinfo()), 4) #we should have 4 nodes at this point
  25. self.nodes[2].setban("127.0.0.1", "add")
  26. time.sleep(3) #wait till the nodes are disconected
  27. assert_equal(len(self.nodes[2].getpeerinfo()), 0) #all nodes must be disconnected at this point
  28. assert_equal(len(self.nodes[2].listbanned()), 1)
  29. self.nodes[2].clearbanned()
  30. assert_equal(len(self.nodes[2].listbanned()), 0)
  31. self.nodes[2].setban("127.0.0.0/24", "add")
  32. assert_equal(len(self.nodes[2].listbanned()), 1)
  33. try:
  34. self.nodes[2].setban("127.0.0.1", "add") #throws exception because 127.0.0.1 is within range 127.0.0.0/24
  35. except:
  36. pass
  37. 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
  38. try:
  39. self.nodes[2].setban("127.0.0.1", "remove")
  40. except:
  41. pass
  42. assert_equal(len(self.nodes[2].listbanned()), 1)
  43. self.nodes[2].setban("127.0.0.0/24", "remove")
  44. assert_equal(len(self.nodes[2].listbanned()), 0)
  45. self.nodes[2].clearbanned()
  46. assert_equal(len(self.nodes[2].listbanned()), 0)
  47. ##test persisted banlist
  48. self.nodes[2].setban("127.0.0.0/32", "add")
  49. self.nodes[2].setban("127.0.0.0/24", "add")
  50. self.nodes[2].setban("192.168.0.1", "add", 1) #ban for 1 seconds
  51. self.nodes[2].setban("2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19", "add", 1000) #ban for 1000 seconds
  52. listBeforeShutdown = self.nodes[2].listbanned();
  53. assert_equal("192.168.0.1/32", listBeforeShutdown[2]['address']) #must be here
  54. time.sleep(2) #make 100% sure we expired 192.168.0.1 node time
  55. #stop node
  56. stop_node(self.nodes[2], 2)
  57. self.nodes[2] = start_node(2, self.options.tmpdir)
  58. listAfterShutdown = self.nodes[2].listbanned();
  59. assert_equal("127.0.0.0/24", listAfterShutdown[0]['address'])
  60. assert_equal("127.0.0.0/32", listAfterShutdown[1]['address'])
  61. assert_equal("/19" in listAfterShutdown[2]['address'], True)
  62. ###########################
  63. # RPC disconnectnode test #
  64. ###########################
  65. url = urlparse.urlparse(self.nodes[1].url)
  66. self.nodes[0].disconnectnode(url.hostname+":"+str(p2p_port(1)))
  67. time.sleep(2) #disconnecting a node needs a little bit of time
  68. for node in self.nodes[0].getpeerinfo():
  69. assert(node['addr'] != url.hostname+":"+str(p2p_port(1)))
  70. connect_nodes_bi(self.nodes,0,1) #reconnect the node
  71. found = False
  72. for node in self.nodes[0].getpeerinfo():
  73. if node['addr'] == url.hostname+":"+str(p2p_port(1)):
  74. found = True
  75. assert(found)
  76. if __name__ == '__main__':
  77. NodeHandlingTest ().main ()