Exemplo n.º 1
0
    def test_short_group_message(self, _):
        # Setup
        self.window = RxWindow(type=WIN_TYPE_GROUP,
                               uid=group_name_to_group_id('test_group'),
                               name='test_group',
                               group=self.group,
                               type_print='group',
                               group_list=self.group_list)

        # Add messages to Alice and Charlie. Add duplicate of outgoing message that should be skipped by access_logs.
        for p in assembly_packet_creator(MESSAGE, 'This is a short message', group_id=self.window.uid):
            write_log_entry(p, nick_to_pub_key('Alice'),   self.settings, self.master_key)
            write_log_entry(p, nick_to_pub_key('Alice'),   self.settings, self.master_key, origin=ORIGIN_CONTACT_HEADER)
            write_log_entry(p, nick_to_pub_key('Charlie'), self.settings, self.master_key)
            write_log_entry(p, nick_to_pub_key('Charlie'), self.settings, self.master_key, origin=ORIGIN_CONTACT_HEADER)

        # Test
        self.assert_prints((CLEAR_ENTIRE_SCREEN + CURSOR_LEFT_UP_CORNER + f"""\
Log file of message(s) sent to group test_group
════════════════════════════════════════════════════════════════════════════════
{self.time}      Me: This is a short message
{self.time}   Alice: This is a short message
{self.time} Charlie: This is a short message
<End of log file>

"""), access_logs, self.window, self.contact_list, self.group_list, self.settings, self.master_key)
Exemplo n.º 2
0
    def test_get_group_contact_handle(self) -> None:
        # Setup
        window = self.create_window(group_name_to_group_id('test_group'))
        window.is_active = True
        window.handle_dict = {
            nick_to_pub_key("Alice"): 'Alice',
            nick_to_pub_key("Charlie"): 'Charlie',
            nick_to_pub_key("David"): nick_to_short_address("David"),
            nick_to_pub_key("Eric"): nick_to_short_address("Eric")
        }

        # Test
        self.assertEqual(
            window.get_handle(self.ts, nick_to_pub_key("Alice"),
                              ORIGIN_USER_HEADER), f"{self.time}      Me: ")
        self.assertEqual(
            window.get_handle(self.ts, nick_to_pub_key("Charlie"),
                              ORIGIN_CONTACT_HEADER), f"{self.time} Charlie: ")

        window.is_active = False
        self.assertEqual(
            window.get_handle(self.ts, nick_to_pub_key("Alice"),
                              ORIGIN_USER_HEADER),
            f"{self.time} Me (group test_group): ")
        self.assertEqual(
            window.get_handle(self.ts, nick_to_pub_key("Charlie"),
                              ORIGIN_CONTACT_HEADER),
            f"{self.time} Charlie (group test_group): ")
Exemplo n.º 3
0
 def test_successful_member_removal(self):
     self.cmd_data = group_name_to_group_id('test_group') + b''.join(
         [nick_to_pub_key('contact_18'),
          nick_to_pub_key('contact_20')])
     self.assertIsNone(
         group_remove(self.cmd_data, self.ts, self.window_list,
                      self.contact_list, self.group_list))
Exemplo n.º 4
0
def create_group(name: str, nick_list: List[str] = None) -> Group:
    """Create a mock group object."""
    if nick_list is None:
        nick_list = ['Alice', 'Bob']
    settings = Settings()
    members  = [create_contact(n) for n in nick_list]
    return Group(name, group_name_to_group_id(name), False, False, members, settings, lambda: None)
Exemplo n.º 5
0
    def test_create_handle_dict(self) -> None:
        # Setup
        window = self.create_window(group_name_to_group_id('test_group'))
        message_log = [
            (datetime.now(), "Lorem ipsum", nick_to_pub_key("Alice"),
             ORIGIN_CONTACT_HEADER, False, False),
            (datetime.now(), "Lorem ipsum", nick_to_pub_key("Bob"),
             ORIGIN_USER_HEADER, False, False),
            (datetime.now(), "Lorem ipsum", nick_to_pub_key("Charlie"),
             ORIGIN_CONTACT_HEADER, False, False),
            (datetime.now(), "Lorem ipsum", nick_to_pub_key("Charlie"),
             ORIGIN_CONTACT_HEADER, True, False),
            (datetime.now(), "Lorem ipsum", nick_to_pub_key("Charlie"),
             ORIGIN_CONTACT_HEADER, False, False),
            (datetime.now(), "Lorem ipsum", nick_to_pub_key("David"),
             ORIGIN_CONTACT_HEADER, False, False),
            (datetime.now(), "Lorem ipsum", nick_to_pub_key("Eric"),
             ORIGIN_CONTACT_HEADER, False, False)
        ]

        # Test
        self.assertIsNone(window.create_handle_dict(message_log))
        self.assertEqual(
            window.handle_dict, {
                nick_to_pub_key("Alice"): 'Alice',
                nick_to_pub_key("Bob"): 'Bob',
                nick_to_pub_key("Charlie"): 'Charlie',
                nick_to_pub_key("David"): nick_to_short_address("David"),
                nick_to_pub_key("Eric"): nick_to_short_address("Eric")
            })
Exemplo n.º 6
0
    def test_missing_group_id_raises_fr(self):
        # Setup
        cmd_data = group_name_to_group_id('test_group2') + b'new_name'

        # Test
        self.assert_fr("Error: No group with ID '2e7mHQznTMsP6' found.",
                       group_rename, cmd_data, *self.args)
Exemplo n.º 7
0
    def test_invalid_group_name_raises_fr(self):
        # Setup
        cmd_data = group_name_to_group_id('test_group') + b'new_name\x1f'

        # Test
        self.assert_fr("Error: Group name must be printable.", group_rename,
                       cmd_data, *self.args)
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    def test_removal_of_contact_logs(self) -> None:
        # Setup
        short_msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."

        # Create temp file that must be removed.
        with open(self.tmp_file_name, 'wb+') as f:
            f.write(os.urandom(LOG_ENTRY_LENGTH))

        # Add a long message sent to both Alice and Bob.
        for p in assembly_packet_creator(MESSAGE, self.msg):
            write_log_entry(p, nick_to_pub_key('Alice'),   self.tfc_log_database)
            write_log_entry(p, nick_to_pub_key('Charlie'), self.tfc_log_database)

        # Add a short message sent to both Alice and Bob.
        for p in assembly_packet_creator(MESSAGE, short_msg):
            write_log_entry(p, nick_to_pub_key('Alice'),   self.tfc_log_database)
            write_log_entry(p, nick_to_pub_key('Charlie'), self.tfc_log_database)

        # Test
        self.assert_se(f"Removed log entries for contact '{nick_to_short_address('Alice')}'.",
                       remove_logs, *self.args, selector=nick_to_pub_key('Alice'))

        self.assert_se(f"Removed log entries for contact '{nick_to_short_address('Charlie')}'.",
                       remove_logs, *self.args, selector=nick_to_pub_key('Charlie'))

        self.assert_se(f"Found no log entries for contact '{nick_to_short_address('Alice')}'.",
                       remove_logs, *self.args, selector=nick_to_pub_key('Alice'))

        self.contact_list.contacts = [create_contact('Alice')]

        self.assert_se(f"Found no log entries for contact 'Alice'.",
                       remove_logs, *self.args, selector=nick_to_pub_key('Alice'))

        self.assert_se(f"Found no log entries for group '2e8b2Wns7dWjB'.",
                       remove_logs, *self.args, selector=group_name_to_group_id('searched_group'))
Exemplo n.º 10
0
    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)
Exemplo n.º 11
0
    def test_unknown_group_id_raises_soft_error(self) -> None:
        # Setup
        group_list = GroupList(groups=['test_group'])
        cmd_data   = group_name_to_group_id('test_group2')

        # Test
        self.assert_se("Error: No group with ID '2e7mHQznTMsP6' found.",
                       group_delete, cmd_data, self.ts, self.window_list, group_list)
Exemplo n.º 12
0
    def test_invalid_group_name_encoding_raises_se(self) -> None:
        # Setup
        cmd_data = group_name_to_group_id(
            'test_group') + b'new_name' + UNDECODABLE_UNICODE

        # Test
        self.assert_se("Error: New name for group 'test_group' was invalid.",
                       group_rename, cmd_data, *self.args)
Exemplo n.º 13
0
    def test_group_window_selection_from_command(self):
        # Setup
        self.window.uid = None

        self.assertIsNone(
            self.window.select_tx_window(*self.args,
                                         selection='test_group',
                                         cmd=True))
        self.assertEqual(self.window.uid, group_name_to_group_id('test_group'))
Exemplo n.º 14
0
    def test_unknown_group_id_raises_fr(self):
        # Setup
        group_list   = GroupList(groups=['test_group'])
        contact_list = ContactList(nicks=[str(n) for n in range(21)])
        cmd_data     = group_name_to_group_id('test_group2') + nick_to_pub_key('20')

        # Test
        self.assert_fr("Error: No group with ID '2e7mHQznTMsP6' found.",
                       group_remove, cmd_data, self.ts, self.window_list, contact_list, group_list)
Exemplo n.º 15
0
    def test_unknown_group_id_raises_soft_error(self) -> None:
        # Setup
        group_list   = GroupList(groups=['test_group'])
        contact_list = ContactList(nicks=[str(n) for n in range(21)])
        cmd_data     = group_name_to_group_id('test_group2') + nick_to_pub_key('50')

        # Test
        self.assert_se("Error: No group with ID '2e7mHQznTMsP6' found.",
                       group_add, cmd_data, self.ts, self.window_list, contact_list, group_list, self.settings)
Exemplo n.º 16
0
    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)
Exemplo n.º 17
0
    def test_reset_window(self):
        # Setup
        window = self.create_window(group_name_to_group_id('test_group'))
        window.message_log = \
            [(datetime.now(), "Hi everybody", nick_to_pub_key("Alice"), ORIGIN_USER_HEADER,    False, False),
             (datetime.now(), "Hi David",     nick_to_pub_key("Alice"), ORIGIN_CONTACT_HEADER, False, False),
             (datetime.now(), "Hi David",     nick_to_pub_key("Bob"),   ORIGIN_CONTACT_HEADER, False, False)]

        # Test
        self.assertIsNone(window.reset_window())
        self.assertEqual(len(window), 0)
Exemplo n.º 18
0
    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)
Exemplo n.º 19
0
    def test_too_large_final_member_list_raises_soft_error(self) -> None:
        # Setup
        group_list    = GroupList(groups=['test_group'])
        contact_list  = ContactList(nicks=[str(n) for n in range(51)])
        group         = group_list.get_group('test_group')
        group.members = contact_list.contacts[:50]
        cmd_data      = group_name_to_group_id('test_group') + nick_to_pub_key('50')

        # Test
        self.assert_se("Error: TFC settings only allow 50 members per group.",
                       group_add, cmd_data, self.ts, self.window_list, contact_list, group_list, self.settings)
Exemplo n.º 20
0
    def test_disable_notifications_for_group(self) -> None:
        # Setup
        group               = self.group_list.get_group('test_group')
        group.notifications = True
        window              = TxWindow(uid=group_name_to_group_id('test_group'),
                                       type=WIN_TYPE_GROUP,
                                       group=group,
                                       window_contacts=group.members)

        # Test
        self.assertTrue(group.notifications)
        self.assertIsNone(contact_setting(UserInput('notify off'), window, *self.args))
        self.assertFalse(group.notifications)
Exemplo n.º 21
0
    def test_enable_logging_for_group(self) -> None:
        # Setup
        group              = self.group_list.get_group('test_group')
        group.log_messages = False
        window             = TxWindow(uid=group_name_to_group_id('test_group'),
                                      type=WIN_TYPE_GROUP,
                                      group=group,
                                      window_contacts=group.members)

        # Test
        self.assertFalse(group.log_messages)
        self.assertIsNone(contact_setting(UserInput('logging on'), window, *self.args))
        self.assertTrue(group.log_messages)
Exemplo n.º 22
0
 def test_successful_group_creation(self) -> None:
     # Setup
     group_list   = GroupList(groups=['test_group'])
     cmd_data     = group_name_to_group_id('test_group') + b'test_group2' + US_BYTE + nick_to_pub_key('Bob')
     contact_list = ContactList(nicks=['Alice', 'Bob'])
     window_list  = WindowList(nicks       =['Alice', 'Bob'],
                               contact_list=contact_list,
                               group_lis   =group_list,
                               packet_list =None,
                               settings    =Settings)
     # Test
     self.assertIsNone(group_create(cmd_data, self.ts, window_list, contact_list, group_list, self.settings))
     self.assertEqual(len(group_list.get_group('test_group')), 2)
Exemplo n.º 23
0
    def test_remove_contacts(self):
        # Setup
        window = self.create_window(group_name_to_group_id('test_group'))

        # Test
        self.assertEqual(len(window.window_contacts), 3)
        self.assertIsNone(
            window.remove_contacts([
                nick_to_pub_key("Alice"),
                nick_to_pub_key("Bob"),
                nick_to_pub_key("DoesNotExist")
            ]))
        self.assertEqual(len(window.window_contacts), 1)
Exemplo n.º 24
0
    def test_update_group_win_members_if_group_is_available(self):
        # Setup
        self.window.window_contacts = []
        self.window.group = None
        self.window.group_id = group_name_to_group_id('test_group')
        self.window.name = 'test_group'
        self.window.type = WIN_TYPE_GROUP

        # Test
        self.assertIsNone(self.window.update_window(self.group_list))
        self.assertEqual(self.window.group,
                         self.group_list.get_group('test_group'))
        self.assertEqual(self.window.window_contacts,
                         self.window.group.members)
Exemplo n.º 25
0
    def test_removal_of_group_logs(self):
        # Setup
        short_msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."

        # Add long message from user (Bob) to Alice and Charlie. These should be removed.
        for p in assembly_packet_creator(MESSAGE, self.msg, group_id=group_name_to_group_id('test_group')):
            write_log_entry(p, nick_to_pub_key('Alice'),   self.settings, self.master_key)
            write_log_entry(p, nick_to_pub_key('Charlie'), self.settings, self.master_key)

        # Add short message from user (Bob) to Alice and Charlie. These should be removed.
        for p in assembly_packet_creator(MESSAGE, short_msg, group_id=group_name_to_group_id('test_group')):
            write_log_entry(p, nick_to_pub_key('Alice'),   self.settings, self.master_key)
            write_log_entry(p, nick_to_pub_key('Charlie'), self.settings, self.master_key)

        # Add short message from user (Bob) to David. This should be kept.
        for p in assembly_packet_creator(MESSAGE, short_msg):
            write_log_entry(p, nick_to_pub_key('David'), self.settings, self.master_key)

        # Add long message from user (Bob) to David. These should be kept.
        for p in assembly_packet_creator(MESSAGE, self.msg):
            write_log_entry(p, nick_to_pub_key('David'), self.settings, self.master_key)

        # Add short message from user (Bob) to David in a group. This should be kept as group is different.
        for p in assembly_packet_creator(MESSAGE, short_msg, group_id=group_name_to_group_id('different_group')):
            write_log_entry(p, nick_to_pub_key('David'), self.settings, self.master_key)

        # Add an orphaned 'append' assembly packet. This should be removed as it's corrupted.
        write_log_entry(M_A_HEADER + bytes(PADDING_LENGTH), nick_to_pub_key('Alice'), self.settings, self.master_key)

        # Add long message to group member David, canceled half-way. This should be removed as unviewable.
        packets = assembly_packet_creator(MESSAGE, self.msg, group_id=group_name_to_group_id('test_group'))
        packets = packets[2:] + [M_C_HEADER + bytes(PADDING_LENGTH)]
        for p in packets:
            write_log_entry(p, nick_to_pub_key('David'), self.settings, self.master_key)

        # Add long message to group member David, remove_logs should keep these as group is different.
        for p in assembly_packet_creator(MESSAGE, self.msg, group_id=group_name_to_group_id('different_group')):
            write_log_entry(p, nick_to_pub_key('David'), self.settings, self.master_key)

        # Test
        self.assertEqual(os.path.getsize(self.file_name), 19 * LOG_ENTRY_LENGTH)

        # Test log entries were found.
        self.assert_fr("Removed log entries for group 'test_group'.",
                       remove_logs, *self.args, selector=group_name_to_group_id('test_group'))
        self.assertEqual(os.path.getsize(self.file_name), 8 * LOG_ENTRY_LENGTH)

        # Test log entries were not found when removing group again.
        self.assert_fr("Found no log entries for group 'test_group'.",
                       remove_logs, *self.args, selector=group_name_to_group_id('test_group'))
        self.assertEqual(os.path.getsize(self.file_name), 8 * LOG_ENTRY_LENGTH)
Exemplo n.º 26
0
    def test_add_contacts(self):
        # Setup
        window = self.create_window(group_name_to_group_id('test_group'))
        window.window_contacts = [
            self.contact_list.get_contact_by_address_or_nick('Alice')
        ]

        # Test
        self.assertIsNone(
            window.add_contacts([
                nick_to_pub_key("Alice"),
                nick_to_pub_key("Bob"),
                nick_to_pub_key("DoesNotExist")
            ]))
        self.assertEqual(len(window.window_contacts), 2)
Exemplo n.º 27
0
    def test_log_remove_with_group_name(self, _):
        # Setup
        for p in assembly_packet_creator(
                MESSAGE,
                'This is a short group message',
                group_id=group_name_to_group_id('test_group')):
            write_log_entry(p, nick_to_pub_key('Alice'), self.settings,
                            self.master_key)
        self.assertEqual(os.path.getsize(self.file_name), LOG_ENTRY_LENGTH)

        # Test
        self.assert_fr("Removed log entries for group 'test_group'.",
                       remove_log, UserInput(f'/rmlogs test_group'),
                       *self.args)
        self.assertEqual(os.path.getsize(self.file_name), 0)
        self.assertEqual(self.queues[COMMAND_PACKET_QUEUE].qsize(), 1)
Exemplo n.º 28
0
    def test_successful_group_add(self) -> None:
        # Setup
        contact_list  = ContactList(nicks=[str(n) for n in range(21)])
        group_lst     = GroupList(groups=['test_group'])
        group         = group_lst.get_group('test_group')
        group.members = contact_list.contacts[:19]
        cmd_data      = group_name_to_group_id('test_group') + nick_to_pub_key('20')

        # Test
        self.assertIsNone(group_add(cmd_data, self.ts, self.window_list, contact_list, group_lst, self.settings))

        group2 = group_lst.get_group('test_group')
        self.assertEqual(len(group2), 20)

        for c in group2:
            self.assertIsInstance(c, Contact)
Exemplo n.º 29
0
    def test_enable_file_reception_for_group(self) -> None:
        # Setup
        group  = self.group_list.get_group('test_group')
        window = TxWindow(uid=group_name_to_group_id('test_group'),
                          type=WIN_TYPE_GROUP,
                          group=group,
                          window_contacts=group.members)

        for m in group:
            m.file_reception = False

        # Test
        for m in group:
            self.assertFalse(m.file_reception)
        self.assertIsNone(contact_setting(UserInput('store on'), window, *self.args))
        for m in group:
            self.assertTrue(m.file_reception)
Exemplo n.º 30
0
    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')))