Example #1
0
    def test_find_close_nodes_in_correct_order(self):
        """
        Ensures that the nearest nodes are returned in the correct order: from
        the node closest to the target key to the node furthest away.
        """
        parent_node_id = 'deadbeef'
        r = RoutingTable(parent_node_id)
        # Fill up the bucket and replacement cache
        for i in range(512):
            uri = 'netstring://192.168.0.%d:9999/' % i
            contact = PeerNode(PUBLIC_KEY, self.version, uri, 0)
            contact.network_id = hex(2 ** i)
            r.add_contact(contact)
        target_key = hex(2 ** 256)
        result = r.find_close_nodes(target_key)
        self.assertEqual(constants.K, len(result))

        # Ensure results are in the correct order.
        def key(node):
            return distance(node.network_id, target_key)
        sorted_nodes = sorted(result, key=key)
        self.assertEqual(sorted_nodes, result)
        # Ensure the order is from lowest to highest in terms of distance
        distances = [distance(x.network_id, target_key) for x in result]
        self.assertEqual(sorted(distances), distances)
Example #2
0
 def test_distance(self):
     """
     Sanity check to ensure the XOR'd values return the correct distance.
     """
     key1 = 'deadbeef'
     key2 = 'beefdead'
     expected = int(key1, 16) ^ int(key2, 16)
     actual = distance(key1, key2)
     self.assertEqual(expected, actual)
Example #3
0
 def test_distance(self):
     """
     Sanity check to ensure the XOR'd values return the correct distance.
     """
     key1 = 'deadbeef'
     key2 = 'beefdead'
     expected = int(key1, 16) ^ int(key2, 16)
     actual = distance(key1, key2)
     self.assertEqual(expected, actual)
Example #4
0
    def test_sort_peer_nodes(self):
        """
        Ensures that the sort_peer_nodes function returns the list ordered in
        such a way that the contacts closest to the target key are at the head
        of the list.
        """
        contacts = []
        for i in range(512):
            uri = 'netstring://192.168.0.%d:9999' % i
            contact = PeerNode(str(i), self.version, uri, 0)
            contact.network_id = hex(2 ** i)
            contacts.append(contact)
        target_key = hex(2 ** 256)
        result = sort_peer_nodes(contacts, target_key)

        # Ensure results are in the correct order.
        def key(node):
            return distance(node.network_id, target_key)
        sorted_nodes = sorted(result, key=key)
        self.assertEqual(sorted_nodes, result)
        # Ensure the order is from lowest to highest in terms of distance
        distances = [distance(x.network_id, target_key) for x in result]
        self.assertEqual(sorted(distances), distances)
Example #5
0
    def test_sort_peer_nodes(self):
        """
        Ensures that the sort_peer_nodes function returns the list ordered in
        such a way that the contacts closest to the target key are at the head
        of the list.
        """
        contacts = []
        for i in range(512):
            uri = 'netstring://192.168.0.%d:9999' % i
            contact = PeerNode(str(i), self.version, uri, 0)
            contact.network_id = hex(2**i)
            contacts.append(contact)
        target_key = hex(2**256)
        result = sort_peer_nodes(contacts, target_key)

        # Ensure results are in the correct order.
        def key(node):
            return distance(node.network_id, target_key)

        sorted_nodes = sorted(result, key=key)
        self.assertEqual(sorted_nodes, result)
        # Ensure the order is from lowest to highest in terms of distance
        distances = [distance(x.network_id, target_key) for x in result]
        self.assertEqual(sorted(distances), distances)
Example #6
0
import asyncio
import unittest
import time


# Create a tuple containing source values ordered by associated sha512 has
# values. These are to be used in place of public_key values to help test
# remote nodes reported back to the Lookup instance.
HASH_TUPLES = []
for i in range(2000):
    s = str(i)
    h = sha512(s.encode('utf-8')).hexdigest()
    HASH_TUPLES.append((s, h))
ORDERED_HASHES = tuple([val[0] for val in
                       sorted(HASH_TUPLES,
                       key=lambda x: distance('0', x[1]))])
TARGET = sha512(ORDERED_HASHES[1000].encode('utf-8')).hexdigest()
CLOSEST_TO_TARGET = tuple([val[0] for val in
                          sorted(HASH_TUPLES,
                          key=lambda x: distance(TARGET, x[1]))])


@asyncio.coroutine
def blip(wait=0.01):
    """
    A coroutine that ends after some period of time to allow tasks scheduled
    in the tests to run.

    THIS IS A QUICK HACK AND SHOULD BE CHANGED TO SOMETHING MORE ELEGANT.
    """
    yield from asyncio.sleep(wait)
Example #7
0
 def key(node):
     return distance(node.network_id, target_key)
Example #8
0
import asyncio
import unittest
import time


# Create a tuple containing source values ordered by associated sha512 has
# values. These are to be used in place of public_key values to help test
# remote nodes reported back to the Lookup instance.
HASH_TUPLES = []
for i in range(2000):
    s = str(i)
    h = sha512(s.encode('utf-8')).hexdigest()
    HASH_TUPLES.append((s, h))
ORDERED_HASHES = tuple([val[0] for val in
                       sorted(HASH_TUPLES,
                       key=lambda x: distance('0', x[1]))])
TARGET = sha512(ORDERED_HASHES[1000].encode('utf-8')).hexdigest()
CLOSEST_TO_TARGET = tuple([val[0] for val in
                          sorted(HASH_TUPLES,
                          key=lambda x: distance(TARGET, x[1]))])


@asyncio.coroutine
def blip(wait=0.01):
    """
    A coroutine that ends after some period of time to allow tasks scheduled
    in the tests to run.

    THIS IS A QUICK HACK AND SHOULD BE CHANGED TO SOMETHING MORE ELEGANT.
    """
    yield from asyncio.sleep(wait)