/ src / tests / test_protocol.py
test_protocol.py
  1  """
  2  Tests for common protocol functions
  3  """
  4  
  5  import sys
  6  import unittest
  7  
  8  from pybitmessage import protocol, state
  9  from pybitmessage.helper_startup import fixSocket
 10  
 11  
 12  class TestSocketInet(unittest.TestCase):
 13      """Base class for test cases using protocol.encodeHost()"""
 14  
 15      @classmethod
 16      def setUpClass(cls):
 17          """Execute fixSocket() before start. Only for Windows?"""
 18          fixSocket()
 19  
 20  
 21  class TestProtocol(TestSocketInet):
 22      """Main protocol test case"""
 23  
 24      def test_checkIPv4Address(self):
 25          """Check the results of protocol.checkIPv4Address()"""
 26          token = 'HELLO'
 27          # checking protocol.encodeHost()[12:]
 28          self.assertEqual(  # 127.0.0.1
 29              token, protocol.checkIPv4Address(b'\x7f\x00\x00\x01', token, True))
 30          self.assertFalse(
 31              protocol.checkIPv4Address(b'\x7f\x00\x00\x01', token))
 32          self.assertEqual(  # 10.42.43.1
 33              token, protocol.checkIPv4Address(b'\n*+\x01', token, True))
 34          self.assertFalse(
 35              protocol.checkIPv4Address(b'\n*+\x01', token, False))
 36          self.assertEqual(  # 192.168.0.254
 37              token, protocol.checkIPv4Address(b'\xc0\xa8\x00\xfe', token, True))
 38          self.assertEqual(  # 172.31.255.254
 39              token, protocol.checkIPv4Address(b'\xac\x1f\xff\xfe', token, True))
 40          # self.assertEqual(  # 169.254.1.1
 41          #     token, protocol.checkIPv4Address(b'\xa9\xfe\x01\x01', token, True))
 42          # self.assertEqual(  # 254.128.1.1
 43          #     token, protocol.checkIPv4Address(b'\xfe\x80\x01\x01', token, True))
 44          self.assertFalse(  # 8.8.8.8
 45              protocol.checkIPv4Address(b'\x08\x08\x08\x08', token, True))
 46  
 47      def test_checkIPv6Address(self):
 48          """Check the results of protocol.checkIPv6Address()"""
 49          test_ip = '2001:db8::ff00:42:8329'
 50          self.assertEqual(
 51              'test', protocol.checkIPv6Address(
 52                  protocol.encodeHost(test_ip), 'test'))
 53          self.assertFalse(
 54              protocol.checkIPv6Address(
 55                  protocol.encodeHost(test_ip), 'test', True))
 56          for test_ip in ('fe80::200:5aee:feaa:20a2', 'fdf8:f53b:82e4::53'):
 57              self.assertEqual(
 58                  'test', protocol.checkIPv6Address(
 59                      protocol.encodeHost(test_ip), 'test', True))
 60              self.assertFalse(
 61                  protocol.checkIPv6Address(
 62                      protocol.encodeHost(test_ip), 'test'))
 63  
 64      def test_check_local(self):
 65          """Check the logic of TCPConnection.local"""
 66          self.assertFalse(
 67              protocol.checkIPAddress(protocol.encodeHost('127.0.0.1')))
 68          self.assertTrue(
 69              protocol.checkIPAddress(protocol.encodeHost('127.0.0.1'), True))
 70          self.assertTrue(
 71              protocol.checkIPAddress(protocol.encodeHost('192.168.0.1'), True))
 72          self.assertTrue(
 73              protocol.checkIPAddress(protocol.encodeHost('10.42.43.1'), True))
 74          self.assertTrue(
 75              protocol.checkIPAddress(protocol.encodeHost('172.31.255.2'), True))
 76          self.assertFalse(protocol.checkIPAddress(
 77              protocol.encodeHost('2001:db8::ff00:42:8329'), True))
 78  
 79          globalhost = protocol.encodeHost('8.8.8.8')
 80          self.assertFalse(protocol.checkIPAddress(globalhost, True))
 81          self.assertEqual(protocol.checkIPAddress(globalhost), '8.8.8.8')
 82  
 83      @unittest.skipIf(
 84          sys.hexversion >= 0x3000000, 'this is still not working with python3')
 85      def test_check_local_socks(self):
 86          """The SOCKS part of the local check"""
 87          self.assertTrue(
 88              not protocol.checkSocksIP('127.0.0.1')
 89              or state.socksIP)
 90  
 91      def test_network_group(self):
 92          """Test various types of network groups"""
 93  
 94          test_ip = '1.2.3.4'
 95          self.assertEqual(b'\x01\x02', protocol.network_group(test_ip))
 96  
 97          test_ip = '127.0.0.1'
 98          self.assertEqual('IPv4', protocol.network_group(test_ip))
 99  
100          self.assertEqual(
101              protocol.network_group('8.8.8.8'),
102              protocol.network_group('8.8.4.4'))
103          self.assertNotEqual(
104              protocol.network_group('1.1.1.1'),
105              protocol.network_group('8.8.8.8'))
106  
107          test_ip = '0102:0304:0506:0708:090A:0B0C:0D0E:0F10'
108          self.assertEqual(
109              b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C',
110              protocol.network_group(test_ip))
111  
112          for test_ip in (
113                  'bootstrap8444.bitmessage.org', 'quzwelsuziwqgpt2.onion', None):
114              self.assertEqual(
115                  test_ip, protocol.network_group(test_ip))