def test_successful_storage_of_file(self, _): compressed = zlib.compress(str_to_bytes("test_file.txt") + b'file_data', level=COMPRESSION_LEVEL) file_data = encrypt_and_sign(compressed, self.file_key) self.assertIsNone( process_file(self.ts, self.account, file_data, *self.args))
def process_cached_files(window_list: 'WindowList', contact_list: 'ContactList', settings: 'Settings', file_keys: 'file_keys_type', file_buffer: 'file_buffer_type') -> None: """Check if file key has been received for cached file packet.""" 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) raise SoftError("Cached file processing complete.", output=False)
def test_successful_storage_during_traffic_masking(self, _: Any) -> None: # Setup self.settings.traffic_masking = True self.window_list.active_win = self.window_list.get_window(nick_to_pub_key('Bob')) compressed = zlib.compress(str_to_bytes("testfile.txt") + b'file_data', level=COMPRESSION_LEVEL) file_data = encrypt_and_sign(compressed, self.file_key) self.assertIsNone(process_file(self.ts, self.account, file_data, *self.args)) self.assertEqual(self.window_list.get_window(nick_to_pub_key('Bob')).message_log[0][1], "Stored file from Alice as 'testfile.txt'.") self.assertTrue(os.path.isfile(f'{DIR_RECV_FILES}Alice/testfile.txt'))
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