Ejemplo n.º 1
0
def test_bucket_ordering():
    first = kademlia.KBucket(0, 50)
    second = kademlia.KBucket(51, 100)
    third = random_node()
    assert first < second
    with pytest.raises(TypeError):
        assert first > third
Ejemplo n.º 2
0
def test_routingtable_edgecase_neighbours():
    # tests the edge cases where sorting by midpoint distance is not enough
    table = kademlia.RoutingTable(random_node(0))
    table.buckets = [kademlia.KBucket(0, 127), kademlia.KBucket(128, 1024)]
    table.add_node(random_node(126))
    table.add_node(random_node(129))
    assert [(b.start, b.end) for b in table.buckets_by_distance_to(128)] == [
        (0, 127),
        (128, 1024),
    ]
    assert [n.id for n in table.neighbours(128)] == [129, 126]
Ejemplo n.º 3
0
def test_kbucket_split():
    bucket = kademlia.KBucket(0, 100)
    for i in range(1, bucket.k + 1):
        node = random_node()
        # Set the IDs of half the nodes below the midpoint, so when we split we should end up with
        # two buckets containing k/2 nodes.
        if i % 2 == 0:
            node.id = bucket.midpoint + i
        else:
            node.id = bucket.midpoint - i
        bucket.add(node)
    assert bucket.is_full
    bucket1, bucket2 = bucket.split()
    assert bucket1.start == 0
    assert bucket1.end == 50
    assert bucket2.start == 51
    assert bucket2.end == 100
    assert len(bucket1) == bucket.k / 2
    assert len(bucket2) == bucket.k / 2
Ejemplo n.º 4
0
def test_kbucket_add():
    bucket = kademlia.KBucket(0, 100)
    node = random_node()
    assert bucket.add(node) is None
    assert bucket.nodes == [node]

    node2 = random_node()
    assert bucket.add(node2) is None
    assert bucket.nodes == [node, node2]
    assert bucket.head == node

    assert bucket.add(node) is None
    assert bucket.nodes == [node2, node]
    assert bucket.head == node2

    bucket.k = 2
    node3 = random_node()
    assert bucket.add(node3) == node2
    assert bucket.nodes == [node2, node]
    assert bucket.head == node2
Ejemplo n.º 5
0
def test_bucket_ordering():
    first = kademlia.KBucket(0, 50)
    second = kademlia.KBucket(51, 100)
    third = random_node()
    assert first < second
    with pytest.raises(TypeError):
        assert first > third


@pytest.mark.parametrize(
    "bucket_list, node_id",
    (
        (list([]), 5),
        # test for node.id < bucket.end
        (list([kademlia.KBucket(0, 4)]), 5),
        # test for node.id > bucket.start
        (list([kademlia.KBucket(6, 10)]), 5),
        # test multiple buckets that don't contain node.id
        (
            list([
                kademlia.KBucket(1, 5),
                kademlia.KBucket(6, 49),
                kademlia.KBucket(50, 100),
            ]),
            0,
        ),
    ),
)
def test_binary_get_bucket_for_node_error(bucket_list, node_id):
    node = random_node(nodeid=node_id)