def test_parse_greeting_message(self): """Test parsing greeting message""" ex_database_id_bin = b'\x01\x03\x03\x07' ex_database_id_str = dandelion.util.encode_bytes(ex_database_id_bin).decode() dbid = Protocol.parse_greeting_message('DMS;{0};{1}\n'.format(Protocol.PROTOCOL_VERSION, ex_database_id_str)) self.assertEqual(dbid, ex_database_id_bin) self.assertRaises(ProtocolParseError, Protocol.parse_greeting_message, '') self.assertRaises(ValueError, Protocol.parse_greeting_message, None) self.assertRaises(TypeError, Protocol.parse_greeting_message, 1337) self.assertRaises(ProtocolParseError, Protocol.parse_greeting_message, 'XXX;{0};{1}\n'.format(Protocol.PROTOCOL_VERSION, ex_database_id_str)) self.assertRaises(ProtocolParseError, Protocol.parse_greeting_message, 'XXX;XXX;{0};{1}\n'.format(Protocol.PROTOCOL_VERSION, ex_database_id_str)) self.assertRaises(ProtocolParseError, Protocol.parse_greeting_message, 'DMS;10;{0}\n'.format(ex_database_id_str)) self.assertRaises(ProtocolParseError, Protocol.parse_greeting_message, 'DMS;{0};???\n'.format(Protocol.PROTOCOL_VERSION)) self.assertRaises(ProtocolParseError, Protocol.parse_greeting_message, 'DMS;{0};\n'.format(Protocol.PROTOCOL_VERSION)) self.assertRaises(ProtocolVersionError, Protocol.parse_greeting_message, 'DMS;2.0;{0}\n'.format(ex_database_id_str))
def process(self): # print("CLIENT TRANSACTION: starting") try: """Read greeting from server""" dbid = Protocol.parse_greeting_message(self._read().decode()) """TODO: We should use the remote tc from the last sync...""" """Request and read message id's""" self._write(Protocol.create_message_id_list_request().encode()) _, msgids = Protocol.parse_message_id_list(self._read().decode()) req_msgids = [mid for mid in msgids if not self._db.contains_message(mid)] if len(req_msgids) == 0: # Nothing to fetch # print("CLIENT TRANSACTION: hanging up - 0 sync") return """Request and read messages""" self._write(Protocol.create_message_list_request(req_msgids).encode()) msgs = Protocol.parse_message_list(self._read().decode()) """Store the new messages""" self._db.add_messages(msgs) except (socket.timeout, ProtocolParseError, ValueError, TypeError): """Do nothing on error, just hang up""" #print("CLIENT TRANSACTION: Error processing data from server") print("CLIENT TRANSACTION: hanging up")
def test_parse_greeting_message(self): """Test parsing greeting message""" ex_database_id_bin = b'\x01\x03\x03\x07' ex_database_id_str = dandelion.util.encode_bytes( ex_database_id_bin).decode() dbid = Protocol.parse_greeting_message('DMS;{0};{1}\n'.format( Protocol.PROTOCOL_VERSION, ex_database_id_str)) self.assertEqual(dbid, ex_database_id_bin) self.assertRaises(ProtocolParseError, Protocol.parse_greeting_message, '') self.assertRaises(ValueError, Protocol.parse_greeting_message, None) self.assertRaises(TypeError, Protocol.parse_greeting_message, 1337) self.assertRaises( ProtocolParseError, Protocol.parse_greeting_message, 'XXX;{0};{1}\n'.format(Protocol.PROTOCOL_VERSION, ex_database_id_str)) self.assertRaises( ProtocolParseError, Protocol.parse_greeting_message, 'XXX;XXX;{0};{1}\n'.format(Protocol.PROTOCOL_VERSION, ex_database_id_str)) self.assertRaises(ProtocolParseError, Protocol.parse_greeting_message, 'DMS;10;{0}\n'.format(ex_database_id_str)) self.assertRaises(ProtocolParseError, Protocol.parse_greeting_message, 'DMS;{0};???\n'.format(Protocol.PROTOCOL_VERSION)) self.assertRaises(ProtocolParseError, Protocol.parse_greeting_message, 'DMS;{0};\n'.format(Protocol.PROTOCOL_VERSION)) self.assertRaises(ProtocolVersionError, Protocol.parse_greeting_message, 'DMS;2.0;{0}\n'.format(ex_database_id_str))
def test_roundtrip_greeting_message(self): """Test the greeting message creation / parsing by a round trip""" ex_database_id_bin = b'\x01\x03\x03\x07' self.assertEqual( Protocol.parse_greeting_message( Protocol.create_greeting_message(ex_database_id_bin)), ex_database_id_bin)
def test_roundtrip_greeting_message(self): """Test the greeting message creation / parsing by a round trip""" ex_database_id_bin = b'\x01\x03\x03\x07' self.assertEqual(Protocol.parse_greeting_message(Protocol.create_greeting_message(ex_database_id_bin)), ex_database_id_bin)