Esempio n. 1
0
    def test_sending_messages(self):
        """
        SocketLayer receives a message from higher layer so it sends it
        """
        self.socket_layer.start_server("127.0.0.1", 8080)

        sender = Peer(33, "33.22.11.22", 9992, False)
        sender2 = Peer(21, "23.45.67.86", 9393, False)
        receiver = Peer(11, '127.0.0.1', 8080, False)
        mess = putils.create_ping_message(sender, receiver)
        mess2 = putils.create_ping_message(sender2, receiver)
        address = putils.get_receiver_address(mess)
        address2 = putils.get_receiver_address(mess2)
        _run(self.higher[0].put((mess.SerializeToString(), address)))
        _run(self.higher[0].put((mess2.SerializeToString(), address2)))

        # Get the message
        msg = _run(self.higher[1].get())
        msg2 = _run(self.higher[1].get())
        log.warning("Stop the server")
        status = _run(self.socket_layer.stop_server())
        self.assertIs(status, StatusMessage.SUCCESS)

        self.assertEqual(msg, mess.SerializeToString())
        self.assertEqual(msg2, mess2.SerializeToString())
Esempio n. 2
0
    def test_that_server_receives_messages(self):
        """
        We tell SocketLayer to start the server.
        It should start it in another thread and listen for incoming connections at given socket.
        We test that by sending some message to the server and if everything works it should handle it and put it into the queue.
        """
        self.socket_layer.start_server("127.0.0.1", 8080)
        log.debug("Create socket")
        sock = socket.socket()
        sock2 = socket.socket()
        try:
            sock.connect(("127.0.0.1", 8080))
            sock2.connect(("127.0.0.1", 8080))
        except socket.error as msg:
            log.warning("Could not connect to the server")
            return

        mess = putils.create_ping_message(self.sender, self.receiver)

        sender2 = Peer(555, '1.123.1.123', 333, True)
        receiver2 = Peer(5434, '64.88.66.22', 444, True)
        mess2 = putils.create_ping_message(sender2, receiver2)

        serialized = putils.serialize_message(mess)
        framed = self.socket_layer._frame_message(serialized)
        serialized2 = putils.serialize_message(mess2)
        framed2 = self.socket_layer._frame_message(serialized2)

        try:
            log.debug("Try to send the messages")
            sock.send(framed)
            log.debug("Try to send next messages")
            sock2.send(framed2)
        except socket.error as msg:
            log.warning("Could not send the message: {}".format(msg))
            sock.close()
            sock2.close()
            return

        sock.close()
        sock2.close()

        #Get the message
        msg = _run(self.higher[1].get())
        msg2 = _run(self.higher[1].get())
        log.warning("Stop the server")
        status = _run(self.socket_layer.stop_server())
        self.assertIs(status, StatusMessage.SUCCESS)
        # And now we check if the message is correct
        self.assertIn(serialized, [msg, msg2])
        self.assertIn(serialized2, [msg, msg2])
Esempio n. 3
0
    def test_responding_to_ping(self):
        another_peer = Peer(5, "127.33.21.22", 3233, False)
        this_peer = self.business_layer.get_myself()
        message = putils.create_ping_message(sender=another_peer,
                                             receiver=this_peer)
        message2 = putils.create_ping_message(sender=another_peer,
                                              receiver=this_peer)
        message3 = putils.create_ping_message(sender=another_peer,
                                              receiver=this_peer)

        # Put three messages in queue
        _run(self.lower[0].put(message))
        _run(self.lower[0].put(message2))
        _run(self.lower[0].put(message3))

        # Three response messages should be created and passed to the lower layer
        respond_message1 = _run(self.lower[1].get())
        respond_message2 = _run(self.lower[1].get())
        respond_message3 = _run(self.lower[1].get())

        # Their uuid should be different
        self.assertNotEqual(respond_message1.uuid, respond_message2.uuid)
        self.assertNotEqual(respond_message2.uuid, respond_message3.uuid)
        self.assertNotEqual(respond_message1.uuid, respond_message3.uuid)

        # But after chaanging it to be the same for all three messages they should be the same
        respond_message2.uuid = respond_message1.uuid
        respond_message3.uuid = respond_message2.uuid
        self.assertEqual(respond_message1, respond_message2)
        self.assertEqual(respond_message1, respond_message3)
        self.assertEqual(respond_message3, respond_message2)

        inputed_peer = _run(self.business_layer.lower_layer.get_peer_by_id(5))
        self.assertEqual(respond_message1.type, Message.PING_RESPONSE)

        self.assertEqual(int(respond_message1.receiver.guid), 5)
        self.assertEqual(respond_message1.receiver.IP, "127.33.21.22")
        self.assertEqual(respond_message1.receiver.port, 3233)
        self.assertEqual(respond_message1.receiver.isNAT, False)

        self.assertEqual(int(respond_message1.sender.guid), this_peer.id)
        self.assertEqual(respond_message1.sender.IP, this_peer.ip)
        self.assertEqual(respond_message1.sender.port, this_peer.port)
        self.assertEqual(respond_message1.sender.isNAT, this_peer.is_NAT)

        self.assertEqual(inputed_peer, another_peer)
Esempio n. 4
0
 async def ping_all(self):
     null_peer = Peer(-1, "127.0.0.1", 22, False)
     message = putils.create_ping_message(sender=self.get_myself(), receiver=null_peer)
     try:
         status = await self._propagate_message(message)
         return status
     except asyncio.CancelledError:
         return StatusMessage.FAILURE
Esempio n. 5
0
    def test_connecting_to_wrong_server(self):
        self.socket_layer.start_server("127.0.0.1", 8080)

        sender = Peer(33, "33.22.11.22", 9992, False)
        sender2 = Peer(21, "23.45.67.86", 9393, False)
        receiver = Peer(11, '127.0.0.1', 88, False)

        mess = putils.create_ping_message(sender, receiver)
        mess2 = putils.create_ping_message(sender2, receiver)

        address = putils.get_receiver_address(mess)
        address2 = putils.get_receiver_address(mess2)
        _run(self.higher[0].put((mess.SerializeToString(), address)))
        _run(self.higher[0].put((mess2.SerializeToString(), address2)))

        self.assertEqual(self.higher[1].qsize(), 0)
        # Get the message
        log.warning("Stop the server")
        status = _run(self.socket_layer.stop_server())
        self.assertIs(status, StatusMessage.SUCCESS)
Esempio n. 6
0
 def test_passing_message(self):
     """
     P2P layer got some message from higher layer.
     It passes it on to the lower layer using queue.
     """
     sender = Peer(123, '123.123.123.123', 8022, True)
     receiver = Peer(11, '66.22.66.22', 9090, False)
     mess = putils.create_ping_message(sender, receiver)
     _run(self.higher[0].put(mess))
     # wait a while for message to propagate through the layer
     _run(asyncio.sleep(0.1))
     self.assertEqual(_run(self.lower[1].get()), mess)
Esempio n. 7
0
 def test_passing_message(self):
     """
     Message layer got some message from higher layer.
     It serializes it.
     Then it passes it on to the lower layer using queue.
     """
     mess = putils.create_ping_message(self.sender, self.receiver)
     address = putils.get_receiver_address(mess)
     _run(self.higher[0].put(mess))
     # wait a while for message to propagate through the layer
     _run(asyncio.sleep(0.1))
     self.assertEqual(_run(self.lower[1].get()),
                      (putils.serialize_message(mess), address))
Esempio n. 8
0
 async def ping(self, target_id):
     """
     Sends ping message to peer with given target_id
     :param target_id: id of target peer
     :return: SUCCESS or FAILURE
     """
     peer = await self.lower_layer.get_peer_by_id(target_id)
     if peer is None:
         return StatusMessage.FAILURE
     message = putils.create_ping_message(sender=self.get_myself(), receiver=peer)
     try:
         status = await self._put_message_on_lower(message)
         self._pinged_peers.append((peer, asyncio.ensure_future(self._wait_for_ping_response(peer=peer, timeout=10))))
         return status
     except asyncio.CancelledError:
         return StatusMessage.FAILURE
Esempio n. 9
0
    def test_ping(self):
        """
        Business layer creates ping message using protobuf_utils
        and passes it on to the lower layer
        """
        peer = Peer(2, "127.33.21.22", 3233, False)
        _run(self.business_layer.lower_layer._routing_table.insert(peer))
        status = _run(self.business_layer.ping(2))
        self.assertIs(status, StatusMessage.SUCCESS)
        message = _run(self.lower[1].get())
        self.assertIsInstance(message, Message)
        self.assertEqual(message.type, Message.PING)

        ping_message = putils.create_ping_message(
            self.business_layer.get_myself(), peer)
        ping_message.uuid = message.uuid
        self.assertEqual(message, ping_message)

        status = _run(self.business_layer.ping(266))
        self.assertIs(status, StatusMessage.FAILURE)
Esempio n. 10
0
 def test_get_receiver_address(self):
     mess = putils.create_ping_message(self.sender, self.receiver)
     self.assertEqual((self.receiver.ip, self.receiver.port),
                      putils.get_receiver_address(mess))
Esempio n. 11
0
 def test_encoding_and_decoding(self):
     mess = putils.create_ping_message(self.sender, self.receiver)
     self.assertEqual(
         putils.deserialize_message(putils.serialize_message(mess)), mess)
Esempio n. 12
0
 def test_creation_of_ping_message(self):
     mess = putils.create_ping_message(self.sender, self.receiver)
     self.assertEqual(mess.type, Message.PING)