lbry-sdk/tests/unit/dht/test_messages.py

85 lines
4.5 KiB
Python
Raw Normal View History

2015-08-20 17:27:15 +02:00
#!/usr/bin/env python
#
# This library is free software, distributed under the terms of
# the GNU Lesser General Public License Version 3, or any later version.
# See the COPYING file included in this archive
from twisted.trial import unittest
2015-08-20 17:27:15 +02:00
2017-09-29 12:44:22 +02:00
from lbrynet.dht.msgtypes import RequestMessage, ResponseMessage, ErrorMessage
2015-08-20 17:27:15 +02:00
from lbrynet.dht.msgformat import MessageTranslator, DefaultFormat
2017-10-25 02:05:38 +02:00
2015-08-20 17:27:15 +02:00
class DefaultFormatTranslatorTest(unittest.TestCase):
""" Test case for the default message translator """
def setUp(self):
2017-10-25 02:05:38 +02:00
self.cases = ((RequestMessage('1' * 48, 'rpcMethod',
{'arg1': 'a string', 'arg2': 123}, '1' * 20),
2015-08-20 17:27:15 +02:00
{DefaultFormat.headerType: DefaultFormat.typeRequest,
2017-10-25 02:05:38 +02:00
DefaultFormat.headerNodeID: '1' * 48,
DefaultFormat.headerMsgID: '1' * 20,
2015-08-20 17:27:15 +02:00
DefaultFormat.headerPayload: 'rpcMethod',
DefaultFormat.headerArgs: {'arg1': 'a string', 'arg2': 123}}),
2017-09-29 12:44:22 +02:00
2017-10-25 02:05:38 +02:00
(ResponseMessage('2' * 20, '2' * 48, 'response'),
2015-08-20 17:27:15 +02:00
{DefaultFormat.headerType: DefaultFormat.typeResponse,
2017-10-25 02:05:38 +02:00
DefaultFormat.headerNodeID: '2' * 48,
DefaultFormat.headerMsgID: '2' * 20,
2015-08-20 17:27:15 +02:00
DefaultFormat.headerPayload: 'response'}),
2017-09-29 12:44:22 +02:00
2017-10-25 02:05:38 +02:00
(ErrorMessage('3' * 20, '3' * 48,
2017-09-29 12:44:22 +02:00
"<type 'exceptions.ValueError'>", 'this is a test exception'),
2015-08-20 17:27:15 +02:00
{DefaultFormat.headerType: DefaultFormat.typeError,
2017-10-25 02:05:38 +02:00
DefaultFormat.headerNodeID: '3' * 48,
DefaultFormat.headerMsgID: '3' * 20,
2015-08-20 17:27:15 +02:00
DefaultFormat.headerPayload: "<type 'exceptions.ValueError'>",
DefaultFormat.headerArgs: 'this is a test exception'}),
2017-09-29 12:44:22 +02:00
(ResponseMessage(
2017-10-25 02:05:38 +02:00
'4' * 20, '4' * 48,
2017-09-29 12:44:22 +02:00
[('H\x89\xb0\xf4\xc9\xe6\xc5`H>\xd5\xc2\xc5\xe8Od\xf1\xca\xfa\x82',
'127.0.0.1', 1919),
('\xae\x9ey\x93\xdd\xeb\xf1^\xff\xc5\x0f\xf8\xac!\x0e\x03\x9fY@{',
'127.0.0.1', 1921)]),
2015-08-20 17:27:15 +02:00
{DefaultFormat.headerType: DefaultFormat.typeResponse,
2017-10-25 02:05:38 +02:00
DefaultFormat.headerNodeID: '4' * 48,
DefaultFormat.headerMsgID: '4' * 20,
2017-09-29 12:44:22 +02:00
DefaultFormat.headerPayload:
[('H\x89\xb0\xf4\xc9\xe6\xc5`H>\xd5\xc2\xc5\xe8Od\xf1\xca\xfa\x82',
'127.0.0.1', 1919),
('\xae\x9ey\x93\xdd\xeb\xf1^\xff\xc5\x0f\xf8\xac!\x0e\x03\x9fY@{',
'127.0.0.1', 1921)]})
2015-08-20 17:27:15 +02:00
)
self.translator = DefaultFormat()
2018-07-21 22:55:43 +02:00
self.assertTrue(
2017-09-29 12:44:22 +02:00
isinstance(self.translator, MessageTranslator),
'Translator class must inherit from entangled.kademlia.msgformat.MessageTranslator!')
2015-08-20 17:27:15 +02:00
def testToPrimitive(self):
""" Tests translation from a Message object to a primitive """
for msg, msgPrimitive in self.cases:
translatedObj = self.translator.toPrimitive(msg)
2018-07-21 22:55:43 +02:00
self.assertEqual(len(translatedObj), len(msgPrimitive),
2017-09-29 12:44:22 +02:00
"Translated object does not match example object's size")
2015-08-20 17:27:15 +02:00
for key in msgPrimitive:
2018-07-21 22:55:43 +02:00
self.assertEqual(
2017-09-29 12:44:22 +02:00
translatedObj[key], msgPrimitive[key],
'Message object type %s not translated correctly into primitive on '
'key "%s"; expected "%s", got "%s"' %
2017-09-29 12:44:22 +02:00
(msg.__class__.__name__, key, msgPrimitive[key], translatedObj[key]))
2015-08-20 17:27:15 +02:00
def testFromPrimitive(self):
""" Tests translation from a primitive to a Message object """
for msg, msgPrimitive in self.cases:
translatedObj = self.translator.fromPrimitive(msgPrimitive)
2018-07-21 22:55:43 +02:00
self.assertEqual(
2017-09-29 12:44:22 +02:00
type(translatedObj), type(msg),
'Message type incorrectly translated; expected "%s", got "%s"' %
(type(msg), type(translatedObj)))
2015-08-20 17:27:15 +02:00
for key in msg.__dict__:
2018-07-21 22:55:43 +02:00
self.assertEqual(
2017-09-29 12:44:22 +02:00
msg.__dict__[key], translatedObj.__dict__[key],
'Message instance variable "%s" not translated correctly; '
'expected "%s", got "%s"' %
2017-09-29 12:44:22 +02:00
(key, msg.__dict__[key], translatedObj.__dict__[key]))