def process_file_queue(queues: 'queue_dict', window_list: 'WindowList', contact_list: 'ContactList', settings: 'Settings', file_keys: 'file_keys_type', file_buffer: 'file_buffer_type') -> None: """Check file queue for packets.""" file_queue = queues[FILE_DATAGRAM_HEADER] if file_queue.qsize(): ts, packet = file_queue.get() new_file(ts, packet, file_keys, file_buffer, contact_list, window_list, settings) raise SoftError("File processing complete.", output=False)
def test_valid_file_without_key_is_cached(self) -> None: # Setup file_ct = encrypt_and_sign(self.compressed, self.file_key) file_hash = blake2b(file_ct) packet = nick_to_pub_key('Alice') + ORIGIN_CONTACT_HEADER + file_ct # Test self.assertIsNone(new_file(self.ts, packet, *self.args)) self.assertEqual(self.file_buf[nick_to_pub_key('Alice') + file_hash], (self.ts, file_ct))
def test_valid_file_with_key_is_processed(self, _: Any) -> None: # Setup file_ct = encrypt_and_sign(self.compressed, self.file_key) file_hash = blake2b(file_ct) packet = nick_to_pub_key('Alice') + ORIGIN_CONTACT_HEADER + file_ct self.file_keys = {(nick_to_pub_key('Alice') + file_hash): self.file_key} self.args = self.file_keys, self.file_buf, self.contact_list, self.window_list, self.settings # Test self.assertIsNone(new_file(self.ts, packet, *self.args))
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