def test_message_feedback(self):
     obj = self._construct_protocol_msg()
     obj.type = FeedbackMessage.MessageType
     self._msg_parser_raises(obj)
     obj.feedback = "test"
     self._msg_parser_raises(obj)
     obj.status = 31337 # invalid status
     self._msg_parser_raises(obj)
     obj.status = FeedbackMessage.INFO
     msg = MessageParser.parse(obj.to_json())
     self.assertIsInstance(msg, FeedbackMessage)
Exemple #2
0
    def __conn_handler(self, kill_ev):
        self._logger.warning("backend connection handler started")
        while not kill_ev.is_set():
            # Try to connect to the backend
            try:
                self._connect()
            except BastioBackendError as ex:
                self.close()
                self._logger.critical(ex.message)
                # TODO: Implement a more decent reconnection strategy
                time.sleep(5) # Sleep 5 seconds before retrial
                continue

            # Read a message from the wire, parse it, and push it to ingress queue(s)
            try:
                json_string = self._read_message()
                message = MessageParser.parse(json_string)
                self._put_ingress(message)
            except socket.timeout:
                pass # No messages are ready to be read
            except BastioNetstringError as ex:
                self._logger.critical(
                        "error parsing a Netstring message: {}".format(ex.message))
                self.close()
                continue
            except BastioMessageError as ex:
                self._logger.critical(
                        "error parsing a protocol message: {}".format(ex.message))
                self.close()
                continue
            except BastioEOFError:
                self._logger.critical("received EOF on channel")
                self.close()
                continue

            # Get an item from the egress queue(s) and send it to the backend
            try:
                message = self._get_egress(timeout=0.01) # 10ms
                if message == None:
                    # No message is available to send
                    continue
                self._write_message(message.to_json())
            except socket.timeout:
                # Too many un-ACK'd packets? Sliding window shut on our fingers?
                # We don't really know what happened, lets reschedule the last
                # message for retransmission anyway
                self._push_queue(self._tx, message)
            except BastioEOFError:
                # Message was not sent because channel was closed
                # re-push the message to the TX queue again and retry connection
                self._push_queue(self._tx, message)
                self.close()
                continue
 def test_message_reply(self):
     obj = self._construct_action_msg(RemoveUserMessage)
     msg = MessageParser.parse(obj.to_json())
     fb = msg.reply("test success", FeedbackMessage.SUCCESS)
     self.assertIsInstance(fb, FeedbackMessage)
     self.assertEqual(fb.feedback, "test success")
     self.assertEqual(fb.status, FeedbackMessage.SUCCESS)
     fb = msg.reply("test warning", FeedbackMessage.WARNING)
     self.assertIsInstance(fb, FeedbackMessage)
     self.assertEqual(fb.feedback, "test warning")
     self.assertEqual(fb.status, FeedbackMessage.WARNING)
     self.assertEqual(fb.mid, msg.mid)
     fb.to_json()
 def start_subsystem(self, name, transport, chan):
     while True:
         try:
             json_string = self._read_message(chan)
             msg = MessageParser.parse(json_string)
             reply = msg.reply("message received successfully",
                     FeedbackMessage.SUCCESS)
             self._write_message(chan, reply.to_json())
             return
         except BastioNetstringError as ex:
             msg = FeedbackMessage(ex.message, FeedbackMessage.ERROR)
             self._write_message(chan, msg.to_json())
             return
         except BastioMessageError as ex:
             msg = FeedbackMessage(ex.message, FeedbackMessage.ERROR)
             self._write_message(chan, msg.to_json())
             return
         except BastioEOFError:
             return
 def test_message_remove_key(self):
     obj = self._construct_action_msg(RemoveKeyMessage)
     self._msg_parser_raises(obj)
     obj.public_key = self.pubkey
     msg = MessageParser.parse(obj.to_json())
     self.assertIsInstance(msg, RemoveKeyMessage)
 def test_message_update_user(self):
     obj = self._construct_action_msg(UpdateUserMessage)
     self._msg_parser_raises(obj)
     obj.sudo = False
     msg = MessageParser.parse(obj.to_json())
     self.assertIsInstance(msg, UpdateUserMessage)
 def test_message_remove_user(self):
     obj = self._construct_action_msg(RemoveUserMessage)
     msg = MessageParser.parse(obj.to_json())
     self.assertIsInstance(msg, RemoveUserMessage)
 def _msg_parser_raises(self, obj):
     with self.assertRaises(BastioMessageError):
         MessageParser.parse(obj.to_json())