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

89 lines
3.6 KiB
Python
Raw Normal View History

2015-08-20 17:27:15 +02:00
import hashlib
import struct
from twisted.trial import unittest
2018-05-24 01:33:16 +02:00
from twisted.internet import defer
from lbrynet.dht.node import Node
from lbrynet.dht import constants
2018-11-07 21:15:05 +01:00
from lbrynet.utils import generate_id
2015-08-20 17:27:15 +02:00
2017-10-10 19:31:18 +02:00
2015-08-20 17:27:15 +02:00
class NodeIDTest(unittest.TestCase):
2015-08-20 17:27:15 +02:00
def setUp(self):
self.node = Node()
2015-08-20 17:27:15 +02:00
def test_new_node_has_auto_created_id(self):
self.assertEqual(type(self.node.node_id), bytes)
self.assertEqual(len(self.node.node_id), 48)
2015-08-20 17:27:15 +02:00
def test_uniqueness_and_length_of_generated_ids(self):
previous_ids = []
2015-08-20 17:27:15 +02:00
for i in range(100):
new_id = self.node._generateID()
self.assertNotIn(new_id, previous_ids, f'id at index {i} not unique')
self.assertEqual(len(new_id), 48, 'id at index {} wrong length: {}'.format(i, len(new_id)))
previous_ids.append(new_id)
2015-08-20 17:27:15 +02:00
class NodeDataTest(unittest.TestCase):
""" Test case for the Node class's data-related functions """
2015-08-20 17:27:15 +02:00
def setUp(self):
2017-10-10 19:31:18 +02:00
h = hashlib.sha384()
2018-07-18 02:57:02 +02:00
h.update(b'test')
self.node = Node()
self.contact = self.node.contact_manager.make_contact(
h.digest(), '127.0.0.1', 12345, self.node._protocol)
2015-08-20 17:27:15 +02:00
self.token = self.node.make_token(self.contact.compact_ip())
self.cases = []
2018-07-18 02:57:02 +02:00
for i in range(5):
h.update(str(i).encode())
2015-08-20 17:27:15 +02:00
self.cases.append((h.digest(), 5000+2*i))
self.cases.append((h.digest(), 5001+2*i))
2017-10-25 02:05:38 +02:00
@defer.inlineCallbacks
def test_store(self):
2015-08-20 17:27:15 +02:00
""" Tests if the node can store (and privately retrieve) some data """
for key, port in self.cases:
yield self.node.store(
2018-06-07 18:18:07 +02:00
self.contact, key, self.token, port, self.contact.id, 0
)
2015-08-20 17:27:15 +02:00
for key, value in self.cases:
expected_result = self.contact.compact_ip() + struct.pack('>H', value) + self.contact.id
2018-07-21 22:55:43 +02:00
self.assertTrue(self.node._dataStore.hasPeersForBlob(key),
"Stored key not found in node's DataStore: '%s'" % key)
self.assertIn(expected_result, self.node._dataStore.getPeersForBlob(key),
"Stored val not found in node's DataStore: key:'%s' port:'%s' %s"
2017-10-10 19:31:18 +02:00
% (key, value, self.node._dataStore.getPeersForBlob(key)))
2015-08-20 17:27:15 +02:00
class NodeContactTest(unittest.TestCase):
""" Test case for the Node class's contact management-related functions """
def setUp(self):
self.node = Node()
2017-10-10 19:31:18 +02:00
@defer.inlineCallbacks
def test_add_contact(self):
2015-08-20 17:27:15 +02:00
""" Tests if a contact can be added and retrieved correctly """
# Create the contact
contact_id = generate_id(b'node1')
contact = self.node.contact_manager.make_contact(contact_id, '127.0.0.1', 9182, self.node._protocol)
2015-08-20 17:27:15 +02:00
# Now add it...
yield self.node.addContact(contact)
2015-08-20 17:27:15 +02:00
# ...and request the closest nodes to it using FIND_NODE
closest_nodes = self.node._routingTable.findCloseNodes(contact_id, constants.k)
self.assertEqual(len(closest_nodes), 1)
self.assertIn(contact, closest_nodes)
2017-10-10 19:31:18 +02:00
@defer.inlineCallbacks
def test_add_self_as_contact(self):
2015-08-20 17:27:15 +02:00
""" Tests the node's behaviour when attempting to add itself as a contact """
# Create a contact with the same ID as the local node's ID
2018-05-29 22:22:30 +02:00
contact = self.node.contact_manager.make_contact(self.node.node_id, '127.0.0.1', 9182, None)
2015-08-20 17:27:15 +02:00
# Now try to add it
yield self.node.addContact(contact)
2015-08-20 17:27:15 +02:00
# ...and request the closest nodes to it using FIND_NODE
closest_nodes = self.node._routingTable.findCloseNodes(self.node.node_id, constants.k)
self.assertNotIn(contact, closest_nodes, 'Node added itself as a contact.')