Exemplo n.º 1
0
 def setUp(self):
     self.cases = ((RequestMessage('node1', 'rpcMethod', {'arg1': 'a string', 'arg2': 123}, 'rpc1'),
                    {DefaultFormat.headerType: DefaultFormat.typeRequest,
                     DefaultFormat.headerNodeID: 'node1',
                     DefaultFormat.headerMsgID: 'rpc1',
                     DefaultFormat.headerPayload: 'rpcMethod',
                     DefaultFormat.headerArgs: {'arg1': 'a string', 'arg2': 123}}),
                     
                   (ResponseMessage('rpc2', 'node2', 'response'),
                    {DefaultFormat.headerType: DefaultFormat.typeResponse,
                     DefaultFormat.headerNodeID: 'node2',
                     DefaultFormat.headerMsgID: 'rpc2',
                     DefaultFormat.headerPayload: 'response'}),
                     
                   (ErrorMessage('rpc3', 'node3', "<type 'exceptions.ValueError'>", 'this is a test exception'),
                    {DefaultFormat.headerType: DefaultFormat.typeError,
                     DefaultFormat.headerNodeID: 'node3',
                     DefaultFormat.headerMsgID: 'rpc3',
                     DefaultFormat.headerPayload: "<type 'exceptions.ValueError'>",
                     DefaultFormat.headerArgs: 'this is a test exception'}),
                   
                   (ResponseMessage('rpc4', 'node4', [('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)]),
                    {DefaultFormat.headerType: DefaultFormat.typeResponse,
                     DefaultFormat.headerNodeID: 'node4',
                     DefaultFormat.headerMsgID: 'rpc4',
                     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)]})
                   )
     self.translator = DefaultFormat()
     self.failUnless(isinstance(self.translator, MessageTranslator), 'Translator class must inherit from entangled.kademlia.msgformat.MessageTranslator!')
Exemplo n.º 2
0
class DefaultFormatTranslatorTest(unittest.TestCase):
    """ Test case for the default message translator """
    def setUp(self):
        self.cases = ((RequestMessage('node1', 'rpcMethod', {'arg1': 'a string', 'arg2': 123}, 'rpc1'),
                       {DefaultFormat.headerType: DefaultFormat.typeRequest,
                        DefaultFormat.headerNodeID: 'node1',
                        DefaultFormat.headerMsgID: 'rpc1',
                        DefaultFormat.headerPayload: 'rpcMethod',
                        DefaultFormat.headerArgs: {'arg1': 'a string', 'arg2': 123}}),
                        
                      (ResponseMessage('rpc2', 'node2', 'response'),
                       {DefaultFormat.headerType: DefaultFormat.typeResponse,
                        DefaultFormat.headerNodeID: 'node2',
                        DefaultFormat.headerMsgID: 'rpc2',
                        DefaultFormat.headerPayload: 'response'}),
                        
                      (ErrorMessage('rpc3', 'node3', "<type 'exceptions.ValueError'>", 'this is a test exception'),
                       {DefaultFormat.headerType: DefaultFormat.typeError,
                        DefaultFormat.headerNodeID: 'node3',
                        DefaultFormat.headerMsgID: 'rpc3',
                        DefaultFormat.headerPayload: "<type 'exceptions.ValueError'>",
                        DefaultFormat.headerArgs: 'this is a test exception'}),
                      
                      (ResponseMessage('rpc4', 'node4', [('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)]),
                       {DefaultFormat.headerType: DefaultFormat.typeResponse,
                        DefaultFormat.headerNodeID: 'node4',
                        DefaultFormat.headerMsgID: 'rpc4',
                        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)]})
                      )
        self.translator = DefaultFormat()
        self.failUnless(isinstance(self.translator, MessageTranslator), 'Translator class must inherit from entangled.kademlia.msgformat.MessageTranslator!')

    def testToPrimitive(self):
        """ Tests translation from a Message object to a primitive """
        for msg, msgPrimitive in self.cases:
            translatedObj = self.translator.toPrimitive(msg)
            self.failUnlessEqual(len(translatedObj), len(msgPrimitive), "Translated object does not match example object's size")
            for key in msgPrimitive:
                self.failUnlessEqual(translatedObj[key], msgPrimitive[key], 'Message object type %s not translated correctly into primitive on key "%s"; expected "%s", got "%s"' % (msg.__class__.__name__, key, msgPrimitive[key], translatedObj[key]))
            
    def testFromPrimitive(self):
        """ Tests translation from a primitive to a Message object """
        for msg, msgPrimitive in self.cases:
            translatedObj = self.translator.fromPrimitive(msgPrimitive)
            self.failUnlessEqual(type(translatedObj), type(msg), 'Message type incorrectly translated; expected "%s", got "%s"' % (type(msg), type(translatedObj))) 
            for key in msg.__dict__:
                self.failUnlessEqual(msg.__dict__[key], translatedObj.__dict__[key], 'Message instance variable "%s" not translated correctly; expected "%s", got "%s"' % (key, msg.__dict__[key], translatedObj.__dict__[key]))
Exemplo n.º 3
0
import struct
import hashlib
import logging
from binascii import unhexlify

from twisted.internet import defer, error
from lbrynet.dht import encoding
from lbrynet.dht.error import DecodeError
from lbrynet.dht.msgformat import DefaultFormat
from lbrynet.dht.msgtypes import ResponseMessage, RequestMessage, ErrorMessage

_datagram_formatter = DefaultFormat()

log = logging.getLogger()

MOCK_DHT_NODES = [
    unhexlify(
        "cc8db9d0dd9b65b103594b5f992adf09f18b310958fa451d61ce8d06f3ee97a91461777c2b7dea1a89d02d2f23eb0e4f"
    ),
    unhexlify(
        "83a3a398eead3f162fbbe1afb3d63482bb5b6d3cdd8f9b0825c1dfa58dffd3f6f6026d6e64d6d4ae4c3dfe2262e734ba"
    ),
    unhexlify(
        "b6928ff25778a7bbb5d258d3b3a06e26db1654f3d2efce8c26681d43f7237cdf2e359a4d309c4473d5d89ec99fb4f573"
    ),
]

MOCK_DHT_SEED_DNS = {  # these map to mock nodes 0, 1, and 2
    "lbrynet1.lbry.io": "10.42.42.1",
    "lbrynet2.lbry.io": "10.42.42.2",
    "lbrynet3.lbry.io": "10.42.42.3",