Пример #1
0
class TestWindowList(TFCTestCase):
    def setUp(self) -> None:
        """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: bytes) -> RxWindow:
        """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) -> None:
        # 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) -> None:
        # Setup
        self.contact_list.contacts = [create_contact(LOCAL_ID)]

        # Test
        self.assertEqual(self.window_list.active_win.uid, WIN_UID_COMMAND)

    def test_window_list_iterates_over_windows(self) -> None:
        for w in self.window_list:
            self.assertIsInstance(w, RxWindow)

    def test_len_returns_number_of_windows(self) -> None:
        self.assertEqual(len(self.window_list), 7)

    def test_group_windows(self) -> None:
        # 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) -> None:
        # 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) -> None:
        # 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) -> None:
        # 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, _: Any) -> None:
        # 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_refresh_file_window_check(self) -> None:
        # Setup
        self.window_list.active_win.uid = WIN_UID_FILE

        # Test
        self.assertIsNone(self.window_list.refresh_file_window_check())

    def test_get_command_window(self) -> None:
        # 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_COMMAND
            ]
        ]

        # Test
        self.assertEqual(self.window_list.get_command_window().uid,
                         WIN_UID_COMMAND)

    def test_get_non_existing_window(self) -> None:
        # Setup
        self.window_list.windows = [
            self.create_window(uid) for uid in [
                group_name_to_group_id('test_group'), WIN_UID_FILE,
                WIN_UID_COMMAND
            ]
        ]

        # 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')))
Пример #2
0
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