Complete test_packet()

with parsing a prepared message and assertion of validation,
including message.Header; use magic_bytes from shared
assuming the value from the Spec.

Cover message.Message except for repr
This commit is contained in:
Lee Miller 2023-08-12 03:29:22 +03:00
parent c6d8bd64b2
commit 3788b12a28
Signed by: lee.miller
GPG Key ID: 4F97A5EA88F4AB63

View File

@ -3,10 +3,9 @@ import unittest
from binascii import unhexlify
from minode import message
from minode.shared import magic_bytes
MAGIC = 0xE9BEB4D9
# 500 identical peers:
# import ipaddress
# from hyperbit import net, packet
@ -21,15 +20,30 @@ sample_data = unhexlify(
) * 500
)
# protocol.CreatePacket(b'ping', b'test')
sample_ping_msg = unhexlify(
'e9beb4d970696e67000000000000000000000004ee26b0dd74657374')
class TestMessage(unittest.TestCase):
"""Test assembling and disassembling of network mesages"""
def test_packet(self):
"""Check the packet created by message.Message()"""
head = unhexlify(b'%x' % MAGIC)
self.assertEqual(
message.Message(b'ping', b'').to_bytes()[:len(head)], head)
"""Check packet creation and parsing by message.Message"""
msg = message.Message(b'ping', b'test').to_bytes()
self.assertEqual(msg[:len(magic_bytes)], magic_bytes)
with self.assertRaises(ValueError):
# wrong magic
message.Message.from_bytes(msg[1:])
with self.assertRaises(ValueError):
# wrong length
message.Message.from_bytes(msg[:-1])
with self.assertRaises(ValueError):
# wrong checksum
message.Message.from_bytes(msg[:-1] + b'\x00')
msg = message.Message.from_bytes(sample_ping_msg)
self.assertEqual(msg.command, b'ping')
self.assertEqual(msg.payload, b'test')
def test_addr(self):
"""Test addr messages"""