2015-08-20 17:27:15 +02:00
|
|
|
import time
|
|
|
|
import unittest
|
|
|
|
import twisted.internet.selectreactor
|
|
|
|
|
|
|
|
import lbrynet.dht.protocol
|
|
|
|
import lbrynet.dht.contact
|
|
|
|
import lbrynet.dht.constants
|
|
|
|
import lbrynet.dht.msgtypes
|
2017-10-10 19:31:18 +02:00
|
|
|
from lbrynet.dht.error import TimeoutError
|
|
|
|
from lbrynet.dht.node import Node, rpcmethod
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
class KademliaProtocolTest(unittest.TestCase):
|
|
|
|
""" Test case for the Protocol class """
|
2017-10-10 19:31:18 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def setUp(self):
|
|
|
|
del lbrynet.dht.protocol.reactor
|
|
|
|
lbrynet.dht.protocol.reactor = twisted.internet.selectreactor.SelectReactor()
|
2017-10-25 02:05:38 +02:00
|
|
|
self.node = Node(node_id='1' * 48, udpPort=9182, externalIP="127.0.0.1")
|
2015-08-20 17:27:15 +02:00
|
|
|
self.protocol = lbrynet.dht.protocol.KademliaProtocol(self.node)
|
|
|
|
|
|
|
|
def testReactor(self):
|
|
|
|
""" Tests if the reactor can start/stop the protocol correctly """
|
|
|
|
lbrynet.dht.protocol.reactor.listenUDP(0, self.protocol)
|
|
|
|
lbrynet.dht.protocol.reactor.callLater(0, lbrynet.dht.protocol.reactor.stop)
|
|
|
|
lbrynet.dht.protocol.reactor.run()
|
|
|
|
|
|
|
|
def testRPCTimeout(self):
|
|
|
|
""" Tests if a RPC message sent to a dead remote node times out correctly """
|
2017-10-10 19:31:18 +02:00
|
|
|
|
|
|
|
@rpcmethod
|
|
|
|
def fake_ping(*args, **kwargs):
|
|
|
|
time.sleep(lbrynet.dht.constants.rpcTimeout + 1)
|
|
|
|
return 'pong'
|
|
|
|
|
|
|
|
real_ping = self.node.ping
|
|
|
|
real_timeout = lbrynet.dht.constants.rpcTimeout
|
|
|
|
real_attempts = lbrynet.dht.constants.rpcAttempts
|
|
|
|
lbrynet.dht.constants.rpcAttempts = 1
|
|
|
|
lbrynet.dht.constants.rpcTimeout = 1
|
|
|
|
self.node.ping = fake_ping
|
2017-10-25 02:05:38 +02:00
|
|
|
deadContact = lbrynet.dht.contact.Contact('2' * 48, '127.0.0.1', 9182, self.protocol)
|
2015-08-20 17:27:15 +02:00
|
|
|
self.node.addContact(deadContact)
|
|
|
|
# Make sure the contact was added
|
2017-10-10 19:31:18 +02:00
|
|
|
self.failIf(deadContact not in self.node.contacts,
|
|
|
|
'Contact not added to fake node (error in test code)')
|
|
|
|
lbrynet.dht.protocol.reactor.listenUDP(9182, self.protocol)
|
|
|
|
|
|
|
|
# Run the PING RPC (which should raise a timeout error)
|
|
|
|
df = self.protocol.sendRPC(deadContact, 'ping', {})
|
|
|
|
|
|
|
|
def check_timeout(err):
|
|
|
|
self.assertEqual(type(err), TimeoutError)
|
|
|
|
|
|
|
|
df.addErrback(check_timeout)
|
|
|
|
|
|
|
|
def reset_values():
|
|
|
|
self.node.ping = real_ping
|
|
|
|
lbrynet.dht.constants.rpcTimeout = real_timeout
|
|
|
|
lbrynet.dht.constants.rpcAttempts = real_attempts
|
|
|
|
|
|
|
|
# See if the contact was removed due to the timeout
|
|
|
|
def check_removed_contact():
|
|
|
|
self.failIf(deadContact in self.node.contacts,
|
|
|
|
'Contact was not removed after RPC timeout; check exception types.')
|
|
|
|
|
|
|
|
df.addCallback(lambda _: reset_values())
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
# Stop the reactor if a result arrives (timeout or not)
|
|
|
|
df.addBoth(lambda _: lbrynet.dht.protocol.reactor.stop())
|
2017-10-10 19:31:18 +02:00
|
|
|
df.addCallback(lambda _: check_removed_contact())
|
2015-08-20 17:27:15 +02:00
|
|
|
lbrynet.dht.protocol.reactor.run()
|
2017-10-10 19:31:18 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def testRPCRequest(self):
|
|
|
|
""" Tests if a valid RPC request is executed and responded to correctly """
|
2017-10-25 02:05:38 +02:00
|
|
|
remoteContact = lbrynet.dht.contact.Contact('2' * 48, '127.0.0.1', 9182, self.protocol)
|
2015-08-20 17:27:15 +02:00
|
|
|
self.node.addContact(remoteContact)
|
|
|
|
self.error = None
|
2017-10-10 19:31:18 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def handleError(f):
|
|
|
|
self.error = 'An RPC error occurred: %s' % f.getErrorMessage()
|
2017-10-10 19:31:18 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def handleResult(result):
|
|
|
|
expectedResult = 'pong'
|
|
|
|
if result != expectedResult:
|
2017-10-10 19:31:18 +02:00
|
|
|
self.error = 'Result from RPC is incorrect; expected "%s", got "%s"' \
|
|
|
|
% (expectedResult, result)
|
|
|
|
|
|
|
|
# Publish the "local" node on the network
|
2015-08-20 17:27:15 +02:00
|
|
|
lbrynet.dht.protocol.reactor.listenUDP(9182, self.protocol)
|
|
|
|
# Simulate the RPC
|
|
|
|
df = remoteContact.ping()
|
|
|
|
df.addCallback(handleResult)
|
|
|
|
df.addErrback(handleError)
|
|
|
|
df.addBoth(lambda _: lbrynet.dht.protocol.reactor.stop())
|
|
|
|
lbrynet.dht.protocol.reactor.run()
|
|
|
|
self.failIf(self.error, self.error)
|
|
|
|
# The list of sent RPC messages should be empty at this stage
|
2017-10-10 19:31:18 +02:00
|
|
|
self.failUnlessEqual(len(self.protocol._sentMessages), 0,
|
|
|
|
'The protocol is still waiting for a RPC result, '
|
|
|
|
'but the transaction is already done!')
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def testRPCAccess(self):
|
|
|
|
""" Tests invalid RPC requests
|
|
|
|
Verifies that a RPC request for an existing but unpublished
|
|
|
|
method is denied, and that the associated (remote) exception gets
|
|
|
|
raised locally """
|
2017-10-25 02:05:38 +02:00
|
|
|
remoteContact = lbrynet.dht.contact.Contact('2' * 48, '127.0.0.1', 9182, self.protocol)
|
2015-08-20 17:27:15 +02:00
|
|
|
self.node.addContact(remoteContact)
|
|
|
|
self.error = None
|
2017-10-10 19:31:18 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def handleError(f):
|
|
|
|
try:
|
|
|
|
f.raiseException()
|
|
|
|
except AttributeError, e:
|
|
|
|
# This is the expected outcome since the remote node did not publish the method
|
|
|
|
self.error = None
|
|
|
|
except Exception, e:
|
2017-10-10 19:31:18 +02:00
|
|
|
self.error = 'The remote method failed, but the wrong exception was raised; ' \
|
|
|
|
'expected AttributeError, got %s' % type(e)
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def handleResult(result):
|
2017-10-10 19:31:18 +02:00
|
|
|
self.error = 'The remote method executed successfully, returning: "%s"; ' \
|
|
|
|
'this RPC should not have been allowed.' % result
|
|
|
|
|
|
|
|
# Publish the "local" node on the network
|
2015-08-20 17:27:15 +02:00
|
|
|
lbrynet.dht.protocol.reactor.listenUDP(9182, self.protocol)
|
|
|
|
# Simulate the RPC
|
2017-10-25 02:05:38 +02:00
|
|
|
df = remoteContact.not_a_rpc_function()
|
2015-08-20 17:27:15 +02:00
|
|
|
df.addCallback(handleResult)
|
|
|
|
df.addErrback(handleError)
|
|
|
|
df.addBoth(lambda _: lbrynet.dht.protocol.reactor.stop())
|
|
|
|
lbrynet.dht.protocol.reactor.run()
|
|
|
|
self.failIf(self.error, self.error)
|
|
|
|
# The list of sent RPC messages should be empty at this stage
|
2017-10-10 19:31:18 +02:00
|
|
|
self.failUnlessEqual(len(self.protocol._sentMessages), 0,
|
|
|
|
'The protocol is still waiting for a RPC result, '
|
|
|
|
'but the transaction is already done!')
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def testRPCRequestArgs(self):
|
|
|
|
""" Tests if an RPC requiring arguments is executed correctly """
|
2017-10-25 02:05:38 +02:00
|
|
|
remoteContact = lbrynet.dht.contact.Contact('2' * 48, '127.0.0.1', 9182, self.protocol)
|
2015-08-20 17:27:15 +02:00
|
|
|
self.node.addContact(remoteContact)
|
|
|
|
self.error = None
|
2017-10-10 19:31:18 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def handleError(f):
|
|
|
|
self.error = 'An RPC error occurred: %s' % f.getErrorMessage()
|
2017-10-10 19:31:18 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def handleResult(result):
|
2017-10-10 19:31:18 +02:00
|
|
|
expectedResult = 'pong'
|
|
|
|
if result != expectedResult:
|
|
|
|
self.error = 'Result from RPC is incorrect; expected "%s", got "%s"' % \
|
|
|
|
(expectedResult, result)
|
|
|
|
|
|
|
|
# Publish the "local" node on the network
|
2015-08-20 17:27:15 +02:00
|
|
|
lbrynet.dht.protocol.reactor.listenUDP(9182, self.protocol)
|
|
|
|
# Simulate the RPC
|
2017-10-10 19:31:18 +02:00
|
|
|
df = remoteContact.ping()
|
2015-08-20 17:27:15 +02:00
|
|
|
df.addCallback(handleResult)
|
|
|
|
df.addErrback(handleError)
|
|
|
|
df.addBoth(lambda _: lbrynet.dht.protocol.reactor.stop())
|
|
|
|
lbrynet.dht.protocol.reactor.run()
|
|
|
|
self.failIf(self.error, self.error)
|
|
|
|
# The list of sent RPC messages should be empty at this stage
|
2017-10-10 19:31:18 +02:00
|
|
|
self.failUnlessEqual(len(self.protocol._sentMessages), 0,
|
|
|
|
'The protocol is still waiting for a RPC result, '
|
|
|
|
'but the transaction is already done!')
|