def test_roundtrip_message_list_request(self): """Test message list request creation / parsing by a round trip""" msg = Protocol.create_message_list_request() res = Protocol.parse_message_list_request(msg) self.assertEqual(res, None) msg = Protocol.create_message_list_request([b'1', b'2']) res = Protocol.parse_message_list_request(msg) self.assertEqual(res, [b'1', b'2'])
def test_roundtrip_message_list_request(self): """Test message list request creation / parsing by a round trip""" msg = Protocol.create_message_list_request() res = Protocol.parse_message_list_request(msg) self.assertEqual(res, None) msg = Protocol.create_message_list_request([b'1', b'2']) res = Protocol.parse_message_list_request(msg) self.assertEqual(res, [b'1', b'2'])
def test_parse_message_list_request(self): """Test parsing the message list request string""" msgs = Protocol.parse_message_list_request('GETMESSAGES\n') self.assertEqual(msgs, None) m1 = b'42' m2 = b'\x01\x23\x245' m3 = b'\x42\x42\x42' m3_str = dandelion.util.encode_bytes(m3).decode() m2_str = dandelion.util.encode_bytes(m2).decode() m1_str = dandelion.util.encode_bytes(m1).decode() msgs_ret = Protocol.parse_message_list_request('GETMESSAGES {0}\n'.format(';'.join([m1_str, m2_str, m3_str]))) self.assertEquals(len(msgs_ret), 3) self.assertTrue(m1 in msgs_ret) self.assertTrue(m2 in msgs_ret) self.assertTrue(m3 in msgs_ret) """Testing bad input""" self.assertRaises(ValueError, Protocol.parse_message_list_request, None) self.assertRaises(TypeError, Protocol.parse_message_list_request, 1337) self.assertRaises(TypeError, Protocol.parse_message_list_request, []) self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, '') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'XXX\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'FF\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'GETMESSAGESXX\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'GETMESSAGES ???;???\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'GETMESSAGES FF;;FF\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'GETMESSAGES FF;FF;\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'GETMESSAGES ;FF;FF\n')
def test_parse_message_list_request(self): """Test parsing the message list request string""" msgs = Protocol.parse_message_list_request('GETMESSAGES\n') self.assertEqual(msgs, None) m1 = b'42' m2 = b'\x01\x23\x245' m3 = b'\x42\x42\x42' m3_str = dandelion.util.encode_bytes(m3).decode() m2_str = dandelion.util.encode_bytes(m2).decode() m1_str = dandelion.util.encode_bytes(m1).decode() msgs_ret = Protocol.parse_message_list_request( 'GETMESSAGES {0}\n'.format(';'.join([m1_str, m2_str, m3_str]))) self.assertEquals(len(msgs_ret), 3) self.assertTrue(m1 in msgs_ret) self.assertTrue(m2 in msgs_ret) self.assertTrue(m3 in msgs_ret) """Testing bad input""" self.assertRaises(ValueError, Protocol.parse_message_list_request, None) self.assertRaises(TypeError, Protocol.parse_message_list_request, 1337) self.assertRaises(TypeError, Protocol.parse_message_list_request, []) self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, '') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'XXX\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'FF\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'GETMESSAGESXX\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'GETMESSAGES ???;???\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'GETMESSAGES FF;;FF\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'GETMESSAGES FF;FF;\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_list_request, 'GETMESSAGES ;FF;FF\n')
def _process_data(self, bdata): """Internal helper function that processes what should be a server request.""" try: data = bdata.decode() if Protocol.is_message_id_list_request(data): tc = Protocol.parse_message_id_list_request(data) tc, msgs = self._db.messages_since(tc) response_str = Protocol.create_message_id_list(tc, msgs) self._write(response_str.encode()) elif Protocol.is_message_list_request(data): msgids = Protocol.parse_message_list_request(data) msgs = self._db.get_messages(msgids) response_str = Protocol.create_message_list(msgs) self._write(response_str.encode()) else: raise ProtocolParseError except (ProtocolParseError, ValueError, TypeError): #print("SERVER TRANSACTION: Error processing data from client") raise ServerTransaction._AbortTransactionException