def test_parse_message_id_list(self): """Test parsing the message ID list request string""" tc = b'13\x01\x07' tc_str = dandelion.util.encode_bytes(tc).decode() 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() parsed_tc, msgidlist = Protocol.parse_message_id_list(''.join([';'.join([tc_str, m1_str, m2_str, m3_str]), '\n'])) self.assertEqual(parsed_tc, tc) self.assertEqual(len(msgidlist), 3) self.assertTrue(m1 in msgidlist) self.assertTrue(m2 in msgidlist) self.assertTrue(m3 in msgidlist) parsed_tc, msgidlist = Protocol.parse_message_id_list(''.join([tc_str, '\n'])) self.assertEqual(parsed_tc, tc) self.assertEqual(len(msgidlist), 0) """Testing bad input""" self.assertRaises(ValueError, Protocol.parse_message_id_list, None) self.assertRaises(TypeError, Protocol.parse_message_id_list, 1337) self.assertRaises(TypeError, Protocol.parse_message_id_list, []) self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, '') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, '\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, '???\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, 'FF FF\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, '???;???\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, 'FF;;FF\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, 'FF;FF;\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, ';FF;FF\n')
def test_parse_message_id_list(self): """Test parsing the message ID list request string""" tc = b'13\x01\x07' tc_str = dandelion.util.encode_bytes(tc).decode() 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() parsed_tc, msgidlist = Protocol.parse_message_id_list(''.join( [';'.join([tc_str, m1_str, m2_str, m3_str]), '\n'])) self.assertEqual(parsed_tc, tc) self.assertEqual(len(msgidlist), 3) self.assertTrue(m1 in msgidlist) self.assertTrue(m2 in msgidlist) self.assertTrue(m3 in msgidlist) parsed_tc, msgidlist = Protocol.parse_message_id_list(''.join( [tc_str, '\n'])) self.assertEqual(parsed_tc, tc) self.assertEqual(len(msgidlist), 0) """Testing bad input""" self.assertRaises(ValueError, Protocol.parse_message_id_list, None) self.assertRaises(TypeError, Protocol.parse_message_id_list, 1337) self.assertRaises(TypeError, Protocol.parse_message_id_list, []) self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, '') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, '\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, '???\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, 'FF FF\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, '???;???\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, 'FF;;FF\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, 'FF;FF;\n') self.assertRaises(ProtocolParseError, Protocol.parse_message_id_list, ';FF;FF\n')
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_roundtrip_message_id_list(self): """Test message ID list response creation / parsing by a round trip""" msg1 = Message('M1') msg2 = Message('M2') msg3 = Message('M3') tc, msgids = Protocol.parse_message_id_list(Protocol.create_message_id_list(b'24', [msg1, msg2, msg3])) self.assertEqual(tc, b'24') self.assertEqual(len(msgids), 3) self.assertTrue(msg1.id in msgids) self.assertTrue(msg2.id in msgids) self.assertTrue(msg3.id in msgids)
def test_roundtrip_message_id_list(self): """Test message ID list response creation / parsing by a round trip""" msg1 = Message('M1') msg2 = Message('M2') msg3 = Message('M3') tc, msgids = Protocol.parse_message_id_list( Protocol.create_message_id_list(b'24', [msg1, msg2, msg3])) self.assertEqual(tc, b'24') self.assertEqual(len(msgids), 3) self.assertTrue(msg1.id in msgids) self.assertTrue(msg2.id in msgids) self.assertTrue(msg3.id in msgids)