Ejemplo n.º 1
0
 def test_hex_to_long(self):
     """
     Ensure a valid hex value produces the correct result.
     """
     raw = 'abcdef0123456789'
     result = hex_to_long(raw)
     self.assertEqual(raw, long_to_hex(result))
Ejemplo n.º 2
0
 def test_long_to_hex(self):
     """
     Ensure a long number produces the correct result.
     """
     raw = 123456789L
     result = long_to_hex(raw)
     self.assertEqual(raw, long(result.encode('hex'), 16))
Ejemplo n.º 3
0
 def test_sort_peer_nodes_no_longer_than_k(self):
     """
     Ensure that no more than constants.K contacts are returned from the
     sort_peer_nodes function despite a longer list being passed in.
     """
     contacts = []
     for i in range(512):
         contact = PeerNode(2 ** i, "192.168.0.%d" % i, 9999, self.version,
                            0)
         contacts.append(contact)
     target_key = long_to_hex(2 ** 256)
     result = sort_peer_nodes(contacts, target_key)
     self.assertEqual(constants.K, len(result))
Ejemplo n.º 4
0
 def test_find_close_nodes_multiple_buckets(self):
     """
     Ensures that nodes are returned from neighbouring k-buckets if the
     k-bucket containing the referenced ID doesn't contain K entries.
     """
     parent_node_id = 'abc'
     r = RoutingTable(parent_node_id)
     # Fill up the bucket and replacement cache
     for i in range(512):
         contact = PeerNode(2 ** i, "192.168.0.%d" % i, 9999, self.version,
                            0)
         r.add_contact(contact)
     result = r.find_close_nodes(long_to_hex(2 ** 256))
     self.assertEqual(constants.K, len(result))
Ejemplo n.º 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):
            contact = PeerNode(2 ** i, "192.168.0.%d" % i, 9999, self.version,
                               0)
            contacts.append(contact)
        target_key = long_to_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)
Ejemplo n.º 6
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 = 'abc'
        r = RoutingTable(parent_node_id)
        # Fill up the bucket and replacement cache
        for i in range(512):
            contact = PeerNode(2 ** i, "192.168.0.%d" % i, 9999, self.version,
                               0)
            r.add_contact(contact)
        target_key = long_to_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)