function __construct() { parent::__construct('Kademlia HTTP Test'); return; ############################# $this->node = KademliaTestFactory::constructNode(['protocols' => [Kademlia\Http::protocol_id => ['protocol' => 'http', 'host' => '10.0.0.1', 'port' => 8080, 'path' => '/kad/kademlia']]]); }
public function testAddingInvalidNodeIdDoesNothing() { $settings = $this->settings; $bucket = new Kademlia\Bucket($settings); $first_node = KademliaTestFactory::constructNode(); $bucket->addNode($first_node); $invalid_node = KademliaTestFactory::constructNode(['id' => 'foobar']); $bucket->addNode($invalid_node); $this->assertEqual($bucket->positionOfNodeId($first_node), 0); $this->assertFalse($bucket->positionOfNodeId($invalid_node)); }
public function testDistance() { $data_a = ['id' => str_repeat('FF', N / 8)]; $node_a = KademliaTestFactory::constructNode($data_a); $zeros = Kademlia\Node::hexId2bin(str_repeat('00', N / 8)); $this->assertEqual($node_a->distanceTo($node_a), $zeros); $data_b = ['id' => str_repeat('FF', N / 8)]; $data_b['id'][2 * N / 8 - 1] = 'E'; $node_b = KademliaTestFactory::constructNode($data_b); $one = Kademlia\Node::hexid2bin(str_repeat('0', 2 * N / 8 - 1) . '1'); $this->assertEqual($node_a->distanceTo($node_b), $one); }
public function testEmitSuccessWhenValueFound() { $settings = new Kademlia\Settings(); $settings->own_node_id = Kademlia\Node::randomNodeId(); $callback_done =& new TestCallback(); $callback_success =& new TestCallback(); $node = KademliaTestFactory::constructNode(); $task = new Kademlia\FindValue($settings, $node->idBin(), new Kademlia\NodeList([])); $task->done([$callback_done, 'callme']); $task->done([$callback_success, 'callme']); $task->perform([[['node_list' => new Kademlia\NodeList([$node]), 'values' => ['abc' => 'd']]]]); $callback_success->expectOnce('callme'); $callback_done->expectOnce('callme'); }
public function testClosestNodes() { $zeros = str_repeat('00', N / 8); $node_array = []; for ($i = 0; $i < N / 8; $i++) { $id = str_repeat('00', N / 8); $id[2 * $i + 1] = '1'; array_push($node_array, KademliaTestFactory::constructNode(['id' => $id])); } $expected_nearest_node_id = str_repeat('00', N / 8 - 1) . '01'; $expected_second_nearest_node_id = str_repeat('00', N / 8 - 2) . '0100'; shuffle($node_array); $node_list = new Kademlia\NodeList($node_array); $two_closest_nodes = $node_list->closestNodes($zeros, 2)->toArray(); $nearest_node = $two_closest_nodes[0]; $second_nearest_node = $two_closest_nodes[1]; $this->assertNotEqual($nearest_node->idStr(), $second_nearest_node->idStr()); $this->assertEqual($nearest_node->idStr(), $expected_nearest_node_id); $this->assertEqual($second_nearest_node->idStr(), $expected_second_nearest_node_id); }
public function testRawStoring() { $settings = new \Kademlia\Settings(); $protocol = new \Kademlia\Http\Protocol($settings); $expire = 10; $sender_node = KademliaTestFactory::constructNode(); $key_id_A = \Kademlia\Node::randomNodeId(); $value = 'foobar'; $protocol->createStoreResponse($sender_node, $key_id_A, $value, $expire); $key_id_B = \Kademlia\Node::randomNodeId(); $protocol->createStoreResponse($sender_node, $key_id_B, $value, $expire); $sender_node = KademliaTestFactory::constructNode(); $value = 'barfoo'; $protocol->createStoreResponse($sender_node, $key_id_B, $value, $expire); $this->assertEqual(count($settings->value_storage), 2); $response = $protocol->createFindValueResponse($key_id_B, $sender_node); $this->assertEqual($response['values'], ['foobar', 'barfoo']); $response = $protocol->createFindValueResponse($key_id_A, $sender_node); $this->assertEqual($response['values'], ['foobar']); }
public function testClosestNodes() { $zeros = str_repeat('00', N / 8); $settings = new Kademlia\Settings(); $settings->own_node_id = $zeros; $kbuckets = new Kademlia\KBuckets($settings); for ($i = 0; $i < N / 8; $i++) { $id = str_repeat('00', N / 8); $id[2 * $i + 1] = '1'; $node = KademliaTestFactory::constructNode(['id' => $id]); $kbuckets->nodeOnline($node); } $expected_nearest_node_id = str_repeat('00', N / 8 - 1) . '01'; $expected_second_nearest_node_id = str_repeat('00', N / 8 - 2) . '0100'; $two_closest_nodes = $kbuckets->closestNodes($zeros, 2)->toArray(); $nearest_node = $two_closest_nodes[0]; $second_nearest_node = $two_closest_nodes[1]; $this->assertNotEqual($nearest_node->idStr(), $second_nearest_node->idStr()); $this->assertEqual($nearest_node->idStr(), $expected_nearest_node_id); $this->assertEqual($second_nearest_node->idStr(), $expected_second_nearest_node_id); }