class TestPacketList(unittest.TestCase): def setUp(self): self.contact_list = ContactList(nicks=['Alice', 'Bob']) self.settings = Settings() packet = Packet('*****@*****.**', self.contact_list.get_contact('Alice'), ORIGIN_CONTACT_HEADER, MESSAGE, self.settings) self.packet_list = PacketList(self.settings, self.contact_list) self.packet_list.packets = [packet] def test_packet_list_iterates_over_contact_objects(self): for p in self.packet_list: self.assertIsInstance(p, Packet) def test_len_returns_number_of_contacts(self): self.assertEqual(len(self.packet_list), 1) def test_has_packet(self): self.assertTrue(self.packet_list.has_packet('*****@*****.**', ORIGIN_CONTACT_HEADER, MESSAGE)) self.assertFalse(self.packet_list.has_packet('*****@*****.**', ORIGIN_USER_HEADER, MESSAGE)) def test_get_packet(self): packet = self.packet_list.get_packet('*****@*****.**', ORIGIN_CONTACT_HEADER, MESSAGE) self.assertEqual(packet.account, '*****@*****.**') self.assertEqual(packet.origin, ORIGIN_CONTACT_HEADER) self.assertEqual(packet.type, MESSAGE)
def setUp(self): self.contact_list = ContactList(nicks=['Alice', 'Bob']) self.settings = Settings() packet = Packet('*****@*****.**', self.contact_list.get_contact('Alice'), ORIGIN_CONTACT_HEADER, MESSAGE, self.settings) self.packet_list = PacketList(self.settings, self.contact_list) self.packet_list.packets = [packet]
def test_class(self): # Setup contact_list = ContactList(nicks=['Alice', 'Bob']) settings = Settings() packet_list = PacketList(contact_list, settings) packet = packet_list.get_packet('*****@*****.**', ORIGIN_CONTACT_HEADER, 'message') # Test self.assertEqual(packet.account, '*****@*****.**') self.assertTrue(packet_list.has_packet('*****@*****.**', ORIGIN_CONTACT_HEADER, 'message')) self.assertFalse(packet_list.has_packet('*****@*****.**', ORIGIN_USER_HEADER, 'message')) self.assertEqual(len(packet_list), 1) for p in packet_list: self.assertIsInstance(p, Packet)
def test_file(self): # Setup ts = datetime.datetime.now() apct_list = self.create_file_apct() contact_list = ContactList(nicks=['Alice', 'Bob', 'local']) key_list = KeyList(nicks=['Alice', 'Bob', 'local']) keyset = key_list.get_keyset('*****@*****.**') keyset.rx_harac = 1 keyset.rx_key = 32 * b'\x01' keyset.rx_hek = 32 * b'\x01' group_list = GroupList(groups=['testgroup']) settings = Settings() packet_list = PacketList(contact_list=contact_list, settings=settings) window_list = WindowList(contact_list=contact_list, group_list=group_list, packet_list=packet_list, settings=settings) master_key = MasterKey() # Test for p in apct_list: self.assertIsNone( process_message(ts, p, window_list, packet_list, contact_list, key_list, group_list, settings, master_key)) # Teardown shutil.rmtree('received_files/')
def test_group_msg(self): # Setup ts = datetime.datetime.now() apct_list = self.create_message_apct(ORIGIN_CONTACT_HEADER, b'testmessage', group_name=b'testgroup') contact_list = ContactList(nicks=['Alice', 'Bob', 'local']) key_list = KeyList(nicks=['Alice', 'Bob', 'local']) keyset = key_list.get_keyset('*****@*****.**') keyset.rx_harac = 1 keyset.rx_key = 32 * b'\x01' keyset.rx_hek = 32 * b'\x01' group_list = GroupList(groups=['testgroup']) group = group_list.get_group('testgroup') group.log_messages = True settings = Settings() packet_list = PacketList(contact_list=contact_list, settings=settings) window_list = WindowList(contact_list=contact_list, group_list=group_list, packet_list=packet_list, settings=settings) master_key = MasterKey() # Test for p in apct_list: self.assertIsNone( process_message(ts, p, window_list, packet_list, contact_list, key_list, group_list, settings, master_key)) # Teardown cleanup()
def test_invalid_window_raises_fr(self): # Setup message = b'testgroup' ts = datetime.datetime.now() timestamp = double_to_bytes(time.time() * 1000) header = GROUP_MESSAGE_HEADER + timestamp + b'test_group' + US_BYTE apct_list = self.create_message_apct(ORIGIN_CONTACT_HEADER, message, header=header) contact_list = ContactList(nicks=['Alice', 'Bob', 'local']) key_list = KeyList(nicks=['Alice', 'Bob', 'local']) keyset = key_list.get_keyset('*****@*****.**') keyset.rx_harac = 1 keyset.rx_key = 32 * b'\x01' keyset.rx_hek = 32 * b'\x01' group_list = GroupList(groups=['testgroup']) settings = Settings() packet_list = PacketList(contact_list=contact_list, settings=settings) window_list = WindowList(contact_list=contact_list, group_list=group_list, packet_list=packet_list, settings=settings) master_key = MasterKey() # Test for p in apct_list: self.assertFR("Received message to unknown group.", process_message, ts, p, window_list, packet_list, contact_list, key_list, group_list, settings, master_key) # Teardown cleanup()
def test_invalid_group_message_header(self): # Setup message = b'testgroup' ts = datetime.datetime.now() apct_list = self.create_message_apct(ORIGIN_CONTACT_HEADER, message, header=GROUP_MESSAGE_HEADER) contact_list = ContactList(nicks=['Alice', 'Bob', 'local']) key_list = KeyList(nicks=['Alice', 'Bob', 'local']) keyset = key_list.get_keyset('*****@*****.**') keyset.rx_harac = 1 keyset.rx_key = 32 * b'\x01' keyset.rx_hek = 32 * b'\x01' group_list = GroupList(groups=['testgroup']) settings = Settings() packet_list = PacketList(contact_list=contact_list, settings=settings) window_list = WindowList(contact_list=contact_list, group_list=group_list, packet_list=packet_list, settings=settings) master_key = MasterKey() # Test for p in apct_list: self.assertFR("Received an invalid group message.", process_message, ts, p, window_list, packet_list, contact_list, key_list, group_list, settings, master_key)
def test_group_remove_member_msg(self): # Setup message = b'testgroup' + US_BYTE + b'*****@*****.**' + US_BYTE + b'*****@*****.**' ts = datetime.datetime.now() apct_list = self.create_message_apct(ORIGIN_CONTACT_HEADER, message, header=GROUP_MSG_MEMBER_RM_HEADER) contact_list = ContactList(nicks=['Alice', 'Bob', 'local']) key_list = KeyList(nicks=['Alice', 'Bob', 'local']) keyset = key_list.get_keyset('*****@*****.**') keyset.rx_harac = 1 keyset.rx_key = 32 * b'\x01' keyset.rx_hek = 32 * b'\x01' group_list = GroupList(groups=['testgroup']) settings = Settings() packet_list = PacketList(contact_list=contact_list, settings=settings) window_list = WindowList(contact_list=contact_list, group_list=group_list, packet_list=packet_list, settings=settings) master_key = MasterKey() # Test for p in apct_list: self.assertIsNone( process_message(ts, p, window_list, packet_list, contact_list, key_list, group_list, settings, master_key)) # Teardown cleanup()
def setUp(self): self.msg = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean condimentum consectetur purus quis" " dapibus. Fusce venenatis lacus ut rhoncus faucibus. Cras sollicitudin commodo sapien, sed bibendu" "m velit maximus in. Aliquam ac metus risus. Sed cursus ornare luctus. Integer aliquet lectus id ma" "ssa blandit imperdiet. Ut sed massa eget quam facilisis rutrum. Mauris eget luctus nisl. Sed ut el" "it iaculis, faucibus lacus eget, sodales magna. Nunc sed commodo arcu. In hac habitasse platea dic" "tumst. Integer luctus aliquam justo, at vestibulum dolor iaculis ac. Etiam laoreet est eget odio r" "utrum, vel malesuada lorem rhoncus. Cras finibus in neque eu euismod. Nulla facilisi. Nunc nec ali" "quam quam, quis ullamcorper leo. Nunc egestas lectus eget est porttitor, in iaculis felis sceleris" "que. In sem elit, fringilla id viverra commodo, sagittis varius purus. Pellentesque rutrum loborti" "s neque a facilisis. Mauris id tortor placerat, aliquam dolor ac, venenatis arcu.").encode() self.ts = datetime.now() self.master_key = MasterKey() self.settings = Settings(logfile_masking=True) self.contact_list = ContactList(nicks=['Alice', 'Bob', 'Charlie', LOCAL_ID]) self.key_list = KeyList( nicks=['Alice', 'Bob', 'Charlie', LOCAL_ID]) self.group_list = GroupList( groups=['testgroup']) self.packet_list = PacketList(contact_list=self.contact_list, settings=self.settings) self.window_list = WindowList(contact_list=self.contact_list, settings=self.settings, group_list=self.group_list, packet_list=self.packet_list) self.group_list.get_group('testgroup').log_messages = True for account in self.contact_list.get_list_of_accounts(): keyset = self.key_list.get_keyset(account) keyset.tx_harac = 1 keyset.rx_harac = 1 keyset.tx_hek = KEY_LENGTH * b'\x01' keyset.rx_hek = KEY_LENGTH * b'\x01' keyset.tx_key = KEY_LENGTH * b'\x01' keyset.rx_key = KEY_LENGTH * b'\x01' self.message = b'testgroup' + US_BYTE + b'*****@*****.**' + US_BYTE + b'*****@*****.**'
def setUp(self): self.ts = datetime.now() self.settings = Settings() self.master_key = MasterKey() self.group_list = GroupList() self.exit_queue = Queue() self.pubkey_buf = dict() self.window_list = WindowList(nicks=[LOCAL_ID]) self.contact_list = ContactList(nicks=[LOCAL_ID]) self.packet_list = PacketList(self.settings, self.contact_list) self.key_list = KeyList(nicks=[LOCAL_ID]) self.key_set = self.key_list.get_keyset(LOCAL_ID) self.key_set.rx_key = bytes(KEY_LENGTH) self.key_set.rx_hek = bytes(KEY_LENGTH) self.key_set.tx_harac = 1 self.key_set.rx_harac = 1
def test_normal_msg(self): # Setup message = ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean condimentum consectetur purus quis" " dapibus. Fusce venenatis lacus ut rhoncus faucibus. Cras sollicitudin commodo sapien, sed bibendu" "m velit maximus in. Aliquam ac metus risus. Sed cursus ornare luctus. Integer aliquet lectus id ma" "ssa blandit imperdiet. Ut sed massa eget quam facilisis rutrum. Mauris eget luctus nisl. Sed ut el" "it iaculis, faucibus lacus eget, sodales magna. Nunc sed commodo arcu. In hac habitasse platea dic" "tumst. Integer luctus aliquam justo, at vestibulum dolor iaculis ac. Etiam laoreet est eget odio r" "utrum, vel malesuada lorem rhoncus. Cras finibus in neque eu euismod. Nulla facilisi. Nunc nec ali" "quam quam, quis ullamcorper leo. Nunc egestas lectus eget est porttitor, in iaculis felis sceleris" "que. In sem elit, fringilla id viverra commodo, sagittis varius purus. Pellentesque rutrum loborti" "s neque a facilisis. Mauris id tortor placerat, aliquam dolor ac, venenatis arcu." .encode()) ts = datetime.datetime.now() apct_list = self.create_message_apct(ORIGIN_CONTACT_HEADER, message) contact_list = ContactList(nicks=['Alice', 'Bob', 'local']) key_list = KeyList(nicks=['Alice', 'Bob', 'local']) keyset = key_list.get_keyset('*****@*****.**') keyset.rx_harac = 1 keyset.rx_key = 32 * b'\x01' keyset.rx_hek = 32 * b'\x01' group_list = GroupList(groups=['testgroup']) settings = Settings() packet_list = PacketList(contact_list=contact_list, settings=settings) window_list = WindowList(contact_list=contact_list, group_list=group_list, packet_list=packet_list, settings=settings) master_key = MasterKey() # Test for p in apct_list: self.assertIsNone( process_message(ts, p, window_list, packet_list, contact_list, key_list, group_list, settings, master_key)) # Teardown cleanup()
def output_loop(queues: Dict[bytes, 'Queue'], settings: 'Settings', contact_list: 'ContactList', key_list: 'KeyList', group_list: 'GroupList', master_key: 'MasterKey', stdin_fd: int, unittest: bool = False) -> None: """Process received packets according to their priority.""" l_queue = queues[LOCAL_KEY_PACKET_HEADER] p_queue = queues[PUBLIC_KEY_PACKET_HEADER] m_queue = queues[MESSAGE_PACKET_HEADER] c_queue = queues[COMMAND_PACKET_HEADER] i_queue = queues[IMPORTED_FILE_HEADER] e_queue = queues[EXIT_QUEUE] sys.stdin = os.fdopen(stdin_fd) packet_buf = dict() # type: Dict[str, List[Tuple[datetime, bytes]]] pubkey_buf = dict() # type: Dict[str, bytes] packet_list = PacketList(settings, contact_list) window_list = WindowList(settings, contact_list, group_list, packet_list) clear_screen() while True: try: if l_queue.qsize() != 0: ts, packet = l_queue.get() process_local_key(ts, packet, window_list, contact_list, key_list, settings) if not contact_list.has_local_contact(): time.sleep(0.01) continue if c_queue.qsize() != 0: ts, packet = c_queue.get() process_command(ts, packet, window_list, packet_list, contact_list, key_list, group_list, settings, master_key, pubkey_buf, e_queue) continue if p_queue.qsize() != 0: ts, packet = p_queue.get() process_public_key(ts, packet, window_list, settings, pubkey_buf) continue if window_list.active_win is not None and window_list.active_win.uid == WIN_TYPE_FILE: window_list.active_win.redraw_file_win() # Prioritize buffered messages for rx_account in packet_buf: if contact_list.has_contact(rx_account) and key_list.has_rx_key(rx_account) and packet_buf[rx_account]: ts, packet = packet_buf[rx_account].pop(0) process_message(ts, packet, window_list, packet_list, contact_list, key_list, group_list, settings, master_key) continue if m_queue.qsize() != 0: ts, packet = m_queue.get() rx_account = packet[PACKET_LENGTH:].decode() if contact_list.has_contact(rx_account) and key_list.has_rx_key(rx_account): process_message(ts, packet, window_list, packet_list, contact_list, key_list, group_list, settings, master_key) else: packet_buf.setdefault(rx_account, []).append((ts, packet)) continue if i_queue.qsize() != 0: ts, packet = i_queue.get() process_imported_file(ts, packet, window_list, settings) continue time.sleep(0.01) if unittest and queues[UNITTEST_QUEUE].qsize() != 0: break except (FunctionReturn, KeyboardInterrupt): pass