def test_get_socket_addresses(self): server = PendingConnectionsServer(None, Network()) node = Node() port = 100 res = server.get_socket_addresses(node, port, None) self.assertEqual(res, []) node.pub_addr = "10.10.10.10" res = server.get_socket_addresses(node, port, None) self.assertEqual(len(res), 1) self.assertEqual(res[0].address, node.pub_addr) self.assertEqual(res[0].port, port) node.pub_port = 1023 res = server.get_socket_addresses(node, port, None) self.assertEqual(len(res), 1) self.assertEqual(res[0].address, node.pub_addr) self.assertEqual(res[0].port, 1023) node.prv_addresses = [ "10.10.10.1", "10.10.10.2", "10.10.10.3", "10.10.10.4" ] res = server.get_socket_addresses(node, port, None) self.assertEqual(len(res), 5) self.assertEqual(res[4].address, node.pub_addr) self.assertEqual(res[4].port, 1023) for i in range(4): self.assertEqual(res[i].address, node.prv_addresses[i]) self.assertEqual(res[i].port, port) node.pub_addr = None res = server.get_socket_addresses(node, port, None) self.assertEqual(len(res), 4) for i in range(4): self.assertEqual(res[i].address, node.prv_addresses[i]) self.assertEqual(res[i].port, port)
def test_add_known_peer(self): key_id = EllipticalKeysAuth(self.path, "TESTPRIV", "TESTPUB").get_key_id() nominal_seeds = len(self.service.seeds) node = Node( 'super_node', key_id, pub_addr='1.2.3.4', prv_addr='1.2.3.4', pub_port=10000, prv_port=10000 ) node.prv_addresses = [node.prv_addr, '172.1.2.3'] assert Node.is_super_node(node) KnownHosts.delete().execute() len_start = len(KnownHosts.select()) # insert one self.service.add_known_peer(node, node.pub_addr, node.pub_port) select_1 = KnownHosts.select() len_1 = len(select_1) last_conn_1 = select_1[0].last_connected assert len_1 > len_start # advance time time.sleep(0.1) # insert duplicate self.service.add_known_peer(node, node.pub_addr, node.pub_port) select_2 = KnownHosts.select() len_2 = len(select_2) assert len_2 == len_1 assert select_2[0].last_connected > last_conn_1 assert len(self.service.seeds) > nominal_seeds # try to add more than max, we already have at least 1 pub_prefix = '2.2.3.' prv_prefix = '172.1.2.' for i in xrange(1, MAX_STORED_HOSTS + 6): i_str = str(i) pub = pub_prefix + i_str prv = prv_prefix + i_str n = Node( i_str, key_id + i_str, pub_addr=pub, prv_addr=prv, pub_port=10000, prv_port=10000 ) self.service.add_known_peer(n, pub, n.prv_port) assert len(KnownHosts.select()) == MAX_STORED_HOSTS assert len(self.service.seeds) == nominal_seeds
def test_add_known_peer(self): key_id = encode_hex(urandom(64))[2:] nominal_seeds = len(self.service.seeds) node = Node(node_name='super_node', key=str(key_id), pub_addr='1.2.3.4', prv_addr='1.2.3.4', pub_port=10000, prv_port=10000) node.prv_addresses = [node.prv_addr, '172.1.2.3'] assert Node.is_super_node(node) KnownHosts.delete().execute() len_start = len(KnownHosts.select()) # insert one self.service.add_known_peer(node, node.pub_addr, node.pub_port) select_1 = KnownHosts.select() len_1 = len(select_1) last_conn_1 = select_1[0].last_connected assert len_1 > len_start # advance time time.sleep(0.1) # insert duplicate self.service.add_known_peer(node, node.pub_addr, node.pub_port) select_2 = KnownHosts.select() len_2 = len(select_2) assert len_2 == len_1 assert select_2[0].last_connected > last_conn_1 assert len(self.service.seeds) > nominal_seeds # try to add more than max, we already have at least 1 pub_prefix = '2.2.3.' prv_prefix = '172.1.2.' key_id_str = key_id for i in range(1, MAX_STORED_HOSTS + 6): i_str = str(i) pub = pub_prefix + i_str prv = prv_prefix + i_str n = Node(node_name=i_str, key=key_id_str + i_str, pub_addr=pub, prv_addr=prv, pub_port=10000, prv_port=10000) self.service.add_known_peer(n, pub, n.prv_port) assert len(KnownHosts.select()) == MAX_STORED_HOSTS assert len(self.service.seeds) == nominal_seeds