A working subset of #1788 #1875
|
@ -3,6 +3,8 @@
|
|||
from binascii import unhexlify
|
||||
|
||||
|
||||
magic = 0xE9BEB4D9
|
||||
|
||||
# These keys are from addresses test script
|
||||
sample_pubsigningkey = unhexlify(
|
||||
'044a367f049ec16cb6b6118eb734a9962d10b8db59c890cd08f210c43ff08bdf09d'
|
||||
|
@ -14,6 +16,7 @@ sample_privsigningkey = \
|
|||
b'93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665'
|
||||
sample_privencryptionkey = \
|
||||
b'4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a'
|
||||
|
||||
sample_ripe = b'003cd097eb7f35c87b5dc8b4538c22cb55312a9f'
|
||||
# stream: 1, version: 2
|
||||
sample_address = 'BM-onkVu1KKL2UaUss5Upg9vXmqd3esTmV79'
|
||||
|
|
68
src/tests/test_packets.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
|
||||
|
||||
`length`, not `lenght`
|
||||
import unittest
|
||||
`length`, not `lenght`
|
||||
from binascii import unhexlify
|
||||
`length`, not `lenght`
|
||||
from struct import pack
|
||||
`length`, not `lenght`
|
||||
|
||||
`length`, not `lenght`
|
||||
from pybitmessage import addresses, protocol
|
||||
`length`, not `lenght`
|
||||
|
||||
`length`, not `lenght`
|
||||
from .samples import magic
|
||||
`length`, not `lenght`
|
||||
|
||||
`length`, not `lenght`
|
||||
|
||||
`length`, not `lenght`
|
||||
class TestSerialize(unittest.TestCase):
|
||||
`length`, not `lenght`
|
||||
"""Test serializing and deserializing packet data"""
|
||||
`length`, not `lenght`
|
||||
|
||||
`length`, not `lenght`
|
||||
def test_varint(self):
|
||||
`length`, not `lenght`
|
||||
"""Test varint encoding and decoding"""
|
||||
`length`, not `lenght`
|
||||
data = addresses.encodeVarint(0)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(data, b'\x00')
|
||||
`length`, not `lenght`
|
||||
data = addresses.encodeVarint(42)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(data, b'*')
|
||||
`length`, not `lenght`
|
||||
data = addresses.encodeVarint(252)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(data, unhexlify('fc'))
|
||||
`length`, not `lenght`
|
||||
data = addresses.encodeVarint(253)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(data, unhexlify('fd00fd'))
|
||||
`length`, not `lenght`
|
||||
data = addresses.encodeVarint(100500)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(data, unhexlify('fe00018894'))
|
||||
`length`, not `lenght`
|
||||
data = addresses.encodeVarint(65535)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(data, unhexlify('fdffff'))
|
||||
`length`, not `lenght`
|
||||
data = addresses.encodeVarint(4294967295)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(data, unhexlify('feffffffff'))
|
||||
`length`, not `lenght`
|
||||
data = addresses.encodeVarint(4294967296)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(data, unhexlify('ff0000000100000000'))
|
||||
`length`, not `lenght`
|
||||
data = addresses.encodeVarint(18446744073709551615)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(data, unhexlify('ffffffffffffffffff'))
|
||||
`length`, not `lenght`
|
||||
|
||||
`length`, not `lenght`
|
||||
with self.assertRaises(addresses.varintEncodeError):
|
||||
`length`, not `lenght`
|
||||
addresses.encodeVarint(18446744073709551616)
|
||||
`length`, not `lenght`
|
||||
|
||||
`length`, not `lenght`
|
||||
value, length = addresses.decodeVarint(b'\xfeaddr')
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(value, protocol.OBJECT_ADDR)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(length, 5)
|
||||
`length`, not `lenght`
|
||||
value, length = addresses.decodeVarint(b'\xfe\x00tor')
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(value, protocol.OBJECT_ONIONPEER)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(length, 5)
|
||||
`length`, not `lenght`
|
||||
|
||||
`length`, not `lenght`
|
||||
def test_packet(self):
|
||||
`length`, not `lenght`
|
||||
"""Check the packet created by protocol.CreatePacket()"""
|
||||
`length`, not `lenght`
|
||||
head = unhexlify(b'%x' % magic)
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(
|
||||
`length`, not `lenght`
|
||||
protocol.CreatePacket(b'ping')[:len(head)], head)
|
||||
`length`, not `lenght`
|
||||
|
||||
`length`, not `lenght`
|
||||
def test_encodehost(self):
|
||||
`length`, not `lenght`
|
||||
"""Check the result of protocol.encodeHost()"""
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(
|
||||
`length`, not `lenght`
|
||||
protocol.encodeHost('127.0.0.1'),
|
||||
`length`, not `lenght`
|
||||
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF'
|
||||
`length`, not `lenght`
|
||||
+ pack('>L', 2130706433))
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(
|
||||
`length`, not `lenght`
|
||||
protocol.encodeHost('191.168.1.1'),
|
||||
`length`, not `lenght`
|
||||
unhexlify('00000000000000000000ffffbfa80101'))
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(
|
||||
`length`, not `lenght`
|
||||
protocol.encodeHost('1.1.1.1'),
|
||||
`length`, not `lenght`
|
||||
unhexlify('00000000000000000000ffff01010101'))
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(
|
||||
`length`, not `lenght`
|
||||
protocol.encodeHost('0102:0304:0506:0708:090A:0B0C:0D0E:0F10'),
|
||||
`length`, not `lenght`
|
||||
unhexlify('0102030405060708090a0b0c0d0e0f10'))
|
||||
`length`, not `lenght`
|
||||
self.assertEqual(
|
||||
`length`, not `lenght`
|
||||
protocol.encodeHost('quzwelsuziwqgpt2.onion'),
|
||||
`length`, not `lenght`
|
||||
unhexlify('fd87d87eeb438533622e54ca2d033e7a'))
|
||||
`length`, not `lenght`
|
length
, notlenght
length
, notlenght