From a94ff1efd3161981b50a8329390109e4923acfe0 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Tue, 20 Jul 2021 21:18:47 +0300 Subject: [PATCH] New test case for packets data, so far here are the tests for: varint, protocol.encodeHost(), protocol.CreatePacket() - only checks magic --- src/tests/samples.py | 3 ++ src/tests/test_packets.py | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/tests/test_packets.py diff --git a/src/tests/samples.py b/src/tests/samples.py index ff2dfa93..17ec03bd 100644 --- a/src/tests/samples.py +++ b/src/tests/samples.py @@ -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' diff --git a/src/tests/test_packets.py b/src/tests/test_packets.py new file mode 100644 index 00000000..65ee0d44 --- /dev/null +++ b/src/tests/test_packets.py @@ -0,0 +1,68 @@ + +import unittest +from binascii import unhexlify +from struct import pack + +from pybitmessage import addresses, protocol + +from .samples import magic + + +class TestSerialize(unittest.TestCase): + """Test serializing and deserializing packet data""" + + def test_varint(self): + """Test varint encoding and decoding""" + data = addresses.encodeVarint(0) + self.assertEqual(data, b'\x00') + data = addresses.encodeVarint(42) + self.assertEqual(data, b'*') + data = addresses.encodeVarint(252) + self.assertEqual(data, unhexlify('fc')) + data = addresses.encodeVarint(253) + self.assertEqual(data, unhexlify('fd00fd')) + data = addresses.encodeVarint(100500) + self.assertEqual(data, unhexlify('fe00018894')) + data = addresses.encodeVarint(65535) + self.assertEqual(data, unhexlify('fdffff')) + data = addresses.encodeVarint(4294967295) + self.assertEqual(data, unhexlify('feffffffff')) + data = addresses.encodeVarint(4294967296) + self.assertEqual(data, unhexlify('ff0000000100000000')) + data = addresses.encodeVarint(18446744073709551615) + self.assertEqual(data, unhexlify('ffffffffffffffffff')) + + with self.assertRaises(addresses.varintEncodeError): + addresses.encodeVarint(18446744073709551616) + + value, length = addresses.decodeVarint(b'\xfeaddr') + self.assertEqual(value, protocol.OBJECT_ADDR) + self.assertEqual(length, 5) + value, length = addresses.decodeVarint(b'\xfe\x00tor') + self.assertEqual(value, protocol.OBJECT_ONIONPEER) + self.assertEqual(length, 5) + + def test_packet(self): + """Check the packet created by protocol.CreatePacket()""" + head = unhexlify(b'%x' % magic) + self.assertEqual( + protocol.CreatePacket(b'ping')[:len(head)], head) + + def test_encodehost(self): + """Check the result of protocol.encodeHost()""" + self.assertEqual( + protocol.encodeHost('127.0.0.1'), + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + + pack('>L', 2130706433)) + self.assertEqual( + protocol.encodeHost('191.168.1.1'), + unhexlify('00000000000000000000ffffbfa80101')) + self.assertEqual( + protocol.encodeHost('1.1.1.1'), + unhexlify('00000000000000000000ffff01010101')) + self.assertEqual( + protocol.encodeHost('0102:0304:0506:0708:090A:0B0C:0D0E:0F10'), + unhexlify('0102030405060708090a0b0c0d0e0f10')) + self.assertEqual( + protocol.encodeHost('quzwelsuziwqgpt2.onion'), + unhexlify('fd87d87eeb438533622e54ca2d033e7a'))