def setUp(self): self.settings = Settings() self.contact_list = ContactList( nicks=['Alice', 'Bob', 'Charlie', LOCAL_ID]) self.group_list = GroupList(groups=['test_group', 'test_group2']) self.packet_list = PacketList() group = self.group_list.get_group('test_group') group.members = list( map(self.contact_list.get_contact_by_address_or_nick, ['Alice', 'Bob', 'Charlie'])) self.window_list = WindowList(self.settings, self.contact_list, self.group_list, self.packet_list)
def setUp(self): self.unittest_dir = cd_unittest() 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.") self.ts = datetime.now() self.master_key = MasterKey() self.settings = Settings(log_file_masking=True) self.file_name = f'{DIR_USER_DATA}{self.settings.software_operation}_logs' 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=['test_group']) 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_id = group_name_to_group_id('test_group') self.file_keys = dict() self.group_list.get_group('test_group').log_messages = True self.args = (self.window_list, self.packet_list, self.contact_list, self.key_list, self.group_list, self.settings, self.master_key, self.file_keys) ensure_dir(DIR_USER_DATA)
def test_active_win_is_none_if_local_key_is_not_present(self): # Setup self.contact_list.contacts = [] # Test window_list = WindowList(self.settings, self.contact_list, self.group_list, self.packet_list) self.assertEqual(window_list.active_win, None)
class TestWindowList(TFCTestCase): def setUp(self): """Pre-test actions.""" self.settings = Settings() self.contact_list = ContactList( nicks=['Alice', 'Bob', 'Charlie', LOCAL_ID]) self.group_list = GroupList(groups=['test_group', 'test_group2']) self.packet_list = PacketList() group = self.group_list.get_group('test_group') group.members = list( map(self.contact_list.get_contact_by_address_or_nick, ['Alice', 'Bob', 'Charlie'])) self.window_list = WindowList(self.settings, self.contact_list, self.group_list, self.packet_list) def create_window(self, uid): """Create new RxWindow object.""" return RxWindow(uid, self.contact_list, self.group_list, self.settings, self.packet_list) def test_active_win_is_none_if_local_key_is_not_present(self): # Setup self.contact_list.contacts = [] # Test window_list = WindowList(self.settings, self.contact_list, self.group_list, self.packet_list) self.assertEqual(window_list.active_win, None) def test_active_win_is_command_win_if_local_key_is_present(self): # Setup self.contact_list.contacts = [create_contact(LOCAL_ID)] # Test self.assertEqual(self.window_list.active_win.uid, WIN_UID_LOCAL) def test_window_list_iterates_over_windows(self): for w in self.window_list: self.assertIsInstance(w, RxWindow) def test_len_returns_number_of_windows(self): self.assertEqual(len(self.window_list), 7) def test_group_windows(self): # Setup self.window_list.windows = [ self.create_window(group_name_to_group_id(g)) for g in ['test_group', 'test_group2'] ] # Test for g in self.window_list.get_group_windows(): self.assertEqual(g.type, WIN_TYPE_GROUP) def test_has_window(self): # Setup self.window_list.windows = [ self.create_window(group_name_to_group_id(g)) for g in ['test_group', 'test_group2'] ] # Test self.assertTrue( self.window_list.has_window(group_name_to_group_id('test_group'))) self.assertTrue( self.window_list.has_window(group_name_to_group_id('test_group2'))) self.assertFalse( self.window_list.has_window(group_name_to_group_id('test_group3'))) def test_remove_window(self): # Setup self.window_list.windows = [ self.create_window(group_name_to_group_id(g)) for g in ['test_group', 'test_group2'] ] # Test self.assertEqual(len(self.window_list), 2) self.assertIsNone( self.window_list.remove_window( group_name_to_group_id('test_group3'))) self.assertEqual(len(self.window_list), 2) self.assertIsNone( self.window_list.remove_window( group_name_to_group_id('test_group2'))) self.assertEqual(len(self.window_list), 1) def test_select_rx_window(self): # Setup self.window_list.windows = [ self.create_window(group_name_to_group_id(g)) for g in ['test_group', 'test_group2'] ] tg_win = self.window_list.windows[0] tg2_win = self.window_list.windows[1] tg_win.is_active = True self.window_list.active_win = tg_win # Test self.assert_prints( f"""{CLEAR_ENTIRE_SCREEN}{CURSOR_LEFT_UP_CORNER} {BOLD_ON} This window for test_group2 is currently empty. {NORMAL_TEXT} """, self.window_list.set_active_rx_window, group_name_to_group_id('test_group2')) self.assertFalse(tg_win.is_active) self.assertTrue(tg2_win.is_active) @mock.patch('time.sleep', return_value=None) def test_select_rx_file_window(self, _): # Setup self.window_list.windows = [self.create_window(WIN_UID_FILE)] self.window_list.windows += [ self.create_window(group_name_to_group_id(g)) for g in ['test_group', 'test_group2'] ] tg_win = self.window_list.get_window( group_name_to_group_id('test_group')) tg_win.is_active = True self.window_list.active_win = tg_win self.packet_list.packets = [ Packet(type=FILE, name='testfile.txt', assembly_pt_list=5 * [b'a'], packets=10, size="100.0KB", contact=create_contact('Bob')) ] # Test self.assert_prints( f"""\ File name Size Sender Complete ──────────────────────────────────────────────────────────────────────────────── testfile.txt 100.0KB Bob 50.00% {5*(CURSOR_UP_ONE_LINE+CLEAR_ENTIRE_LINE)}""", self.window_list.set_active_rx_window, WIN_UID_FILE) self.assertFalse(tg_win.is_active) self.assertTrue(self.window_list.get_window(WIN_UID_FILE).is_active) def test_get_local_window(self): # Setup self.window_list.windows = [ self.create_window(uid) for uid in [ group_name_to_group_id('test_group'), group_name_to_group_id('test_group2'), WIN_UID_FILE, WIN_UID_LOCAL ] ] # Test self.assertEqual(self.window_list.get_local_window().uid, WIN_UID_LOCAL) def test_get_non_existing_window(self): # Setup self.window_list.windows = [ self.create_window(uid) for uid in [ group_name_to_group_id('test_group'), WIN_UID_FILE, WIN_UID_LOCAL ] ] # Test existing window self.assertTrue( self.window_list.has_window(group_name_to_group_id('test_group'))) window = self.window_list.get_window( group_name_to_group_id('test_group')) self.assertEqual(window.uid, group_name_to_group_id('test_group')) # Test non-existing window self.assertFalse( self.window_list.has_window(group_name_to_group_id('test_group2'))) window2 = self.window_list.get_window( group_name_to_group_id('test_group2')) self.assertEqual(window2.uid, group_name_to_group_id('test_group2')) self.assertTrue( self.window_list.has_window(group_name_to_group_id('test_group2')))
def output_loop(queues: Dict[bytes, 'Queue[Any]'], gateway: 'Gateway', settings: 'Settings', contact_list: 'ContactList', key_list: 'KeyList', group_list: 'GroupList', master_key: 'MasterKey', message_log: 'MessageLog', stdin_fd: int, unit_test: bool = False) -> None: """Process packets in message queues according to their priority.""" sys.stdin = os.fdopen(stdin_fd) packet_buffer = dict() # type: packet_buffer_type file_buffer = dict() # type: file_buffer_type file_keys = dict() # type: file_keys_type kdk_hashes = [] # type: List[bytes] packet_hashes = [] # type: List[bytes] packet_list = PacketList(settings, contact_list) window_list = WindowList(settings, contact_list, group_list, packet_list) clear_screen() while True: try: # Local key packets process_local_key_queue(queues, window_list, contact_list, key_list, settings, kdk_hashes, packet_hashes) # Commands process_command_queue(queues, window_list, contact_list, group_list, settings, key_list, packet_list, master_key, gateway) # File window refresh window_list.refresh_file_window_check() # Cached messages process_cached_messages(window_list, contact_list, group_list, key_list, settings, packet_list, message_log, file_keys, packet_buffer) # New messages process_message_queue(queues, window_list, contact_list, group_list, key_list, settings, packet_list, message_log, file_keys, packet_buffer) # Cached files process_cached_files(window_list, contact_list, settings, file_keys, file_buffer) # New files process_file_queue(queues, window_list, contact_list, settings, file_keys, file_buffer) time.sleep(0.01) if unit_test and queues[UNIT_TEST_QUEUE].qsize() != 0: break except (KeyError, KeyboardInterrupt, SoftError): pass
def output_loop(queues: Dict[bytes, 'Queue[Any]'], gateway: 'Gateway', settings: 'Settings', contact_list: 'ContactList', key_list: 'KeyList', group_list: 'GroupList', master_key: 'MasterKey', stdin_fd: int, unit_test: bool = False) -> None: """Process packets in message queues according to their priority.""" local_key_queue = queues[LOCAL_KEY_DATAGRAM_HEADER] message_queue = queues[MESSAGE_DATAGRAM_HEADER] file_queue = queues[FILE_DATAGRAM_HEADER] command_queue = queues[COMMAND_DATAGRAM_HEADER] exit_queue = queues[EXIT_QUEUE] sys.stdin = os.fdopen(stdin_fd) packet_buffer = dict() # type: Dict[bytes, List[Tuple[datetime, bytes]]] file_buffer = dict() # type: Dict[bytes, Tuple[datetime, bytes]] file_keys = dict() # type: Dict[bytes, bytes] kdk_hashes = [] # type: List[bytes] packet_hashes = [] # type: List[bytes] packet_list = PacketList(settings, contact_list) window_list = WindowList(settings, contact_list, group_list, packet_list) clear_screen() while True: try: if local_key_queue.qsize() != 0: ts, packet = local_key_queue.get() process_local_key(ts, packet, window_list, contact_list, key_list, settings, kdk_hashes, packet_hashes, local_key_queue) continue if not contact_list.has_local_contact(): time.sleep(0.1) continue # Commands if command_queue.qsize() != 0: ts, packet = command_queue.get() process_command(ts, packet, window_list, packet_list, contact_list, key_list, group_list, settings, master_key, gateway, exit_queue) continue # File window refresh if window_list.active_win is not None and window_list.active_win.uid == WIN_UID_FILE: window_list.active_win.redraw_file_win() # Cached message packets for onion_pub_key in packet_buffer: if (contact_list.has_pub_key(onion_pub_key) and key_list.has_rx_mk(onion_pub_key) and packet_buffer[onion_pub_key]): ts, packet = packet_buffer[onion_pub_key].pop(0) process_message(ts, packet, window_list, packet_list, contact_list, key_list, group_list, settings, master_key, file_keys) continue # New messages if message_queue.qsize() != 0: ts, packet = message_queue.get() onion_pub_key = packet[:ONION_SERVICE_PUBLIC_KEY_LENGTH] if contact_list.has_pub_key( onion_pub_key) and key_list.has_rx_mk(onion_pub_key): process_message(ts, packet, window_list, packet_list, contact_list, key_list, group_list, settings, master_key, file_keys) else: packet_buffer.setdefault(onion_pub_key, []).append( (ts, packet)) continue # Cached files if file_buffer: for k in file_buffer: key_to_remove = b'' try: if k in file_keys: key_to_remove = k ts_, file_ct = file_buffer[k] dec_key = file_keys[k] onion_pub_key = k[:ONION_SERVICE_PUBLIC_KEY_LENGTH] process_file(ts_, onion_pub_key, file_ct, dec_key, contact_list, window_list, settings) finally: if key_to_remove: file_buffer.pop(k) file_keys.pop(k) break # New files if file_queue.qsize() != 0: ts, packet = file_queue.get() new_file(ts, packet, file_keys, file_buffer, contact_list, window_list, settings) time.sleep(0.01) if unit_test and queues[UNIT_TEST_QUEUE].qsize() != 0: break except (FunctionReturn, KeyError, KeyboardInterrupt): pass