Ejemplo n.º 1
0
    def test_add_contact(self):
        # Setup
        contact1 = Contact('*****@*****.**', '*****@*****.**', 'Alice',
                           32 * b'\x01', 32 * b'\x02', True, True, True)
        contact2 = Contact('*****@*****.**', '*****@*****.**', 'Charlie',
                           32 * b'\x01', 32 * b'\x02', True, True, True)
        settings = Settings(software_operation='ut', m_number_of_accnts=20)
        master_k = MasterKey()
        contact_l = ContactList(master_k, settings)
        contact_l.contacts = [contact1, contact2]

        contact_l.add_contact('*****@*****.**', '*****@*****.**', 'Alice',
                              32 * b'\x03', 32 * b'\x04', True, True, True)
        contact_l.add_contact('*****@*****.**', '*****@*****.**', 'David',
                              32 * b'\x03', 32 * b'\x04', True, True, True)

        contact_l2 = ContactList(master_k, settings)
        c_alice = contact_l2.get_contact('Alice')
        c_david = contact_l2.get_contact('David')

        # Test
        self.assertIsInstance(c_alice, Contact)
        self.assertIsInstance(c_david, Contact)
        self.assertEqual(c_alice.tx_fingerprint, 32 * b'\x03')
        self.assertEqual(c_david.tx_fingerprint, 32 * b'\x03')
Ejemplo n.º 2
0
 def test_contact_printing(self):
     # Setup
     contact_list = ContactList(MasterKey(), Settings())
     contact_list.contacts = [
         create_contact(n) for n in ['Alice', 'Bob', 'Charlie', 'David']
     ]
     # Teardown
     self.assertIsNone(contact_list.print_contacts())
Ejemplo n.º 3
0
    def test_len_returns_number_of_contacts(self):
        # Setup
        contact = Contact('*****@*****.**', '*****@*****.**', 'Alice',
                          32 * b'\x01', 32 * b'\x02', True, True, True)
        contact_l = ContactList(MasterKey(), Settings())
        contact_l.contacts = 5 * [contact]

        # Test
        self.assertEqual(len(contact_l), 5)
Ejemplo n.º 4
0
    def test_iterate_over_contacts(self):
        # Setup
        contact = Contact('*****@*****.**', '*****@*****.**', 'Alice',
                          32 * b'\x01', 32 * b'\x02', True, True, True)
        contact_l = ContactList(MasterKey(), Settings())
        contact_l.contacts = 5 * [contact]

        # Test
        for c in contact_l:
            self.assertIsInstance(c, Contact)
Ejemplo n.º 5
0
    def test_database_encryption_with_another_key(self, _: Any) -> None:
        # Setup
        window = RxWindow(type=WIN_TYPE_CONTACT,
                          uid=nick_to_pub_key('Alice'),
                          name='Alice',
                          type_print='contact')
        contact_list = ContactList(self.old_master_key, self.settings)
        contact_list.contacts = [create_contact('Alice')]
        group_list = GroupList()

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

        # Add a message from contact Alice to user (Bob).
        for p in assembly_packet_creator(MESSAGE, 'This is a short message'):
            write_log_entry(p,
                            nick_to_pub_key('Alice'),
                            self.message_log,
                            origin=ORIGIN_CONTACT_HEADER)

        # Add a message from user (Bob) to Alice.
        for p in assembly_packet_creator(MESSAGE, 'This is a short message'):
            write_log_entry(p, nick_to_pub_key('Alice'), self.message_log)

        # Check logfile content.
        message = (CLEAR_ENTIRE_SCREEN + CURSOR_LEFT_UP_CORNER + f"""\
Log file of message(s) sent to contact Alice
════════════════════════════════════════════════════════════════════════════════
{self.time} Alice: This is a short message
{self.time}    Me: This is a short message
<End of log file>

""")
        self.assertIsNone(
            change_log_db_key(self.old_master_key.master_key,
                              self.new_master_key.master_key, self.settings))

        with open(self.tmp_file_name, 'rb') as f:
            purp_temp_data = f.read()
        self.assertNotEqual(purp_temp_data, temp_file_data)

        # Test that decryption with new key is identical.
        replace_log_db(self.settings)
        self.assert_prints(message, access_logs, window, contact_list,
                           group_list, self.settings, self.new_master_key)

        # Test that temp file is removed.
        self.assertFalse(os.path.isfile(self.tmp_file_name))
Ejemplo n.º 6
0
    def test_get_contact(self):
        # Setup
        contact1 = Contact('*****@*****.**', '*****@*****.**', 'Alice',
                           32 * b'\x01', 32 * b'\x02', True, True, True)
        contact2 = Contact('*****@*****.**', '*****@*****.**', 'Charlie',
                           32 * b'\x01', 32 * b'\x02', True, True, True)
        settings = Settings()
        master_k = MasterKey()
        contact_l = ContactList(master_k, settings)
        contact_l.contacts = [contact1, contact2]

        # Test
        co1 = contact_l.get_contact('*****@*****.**')
        self.assertIsInstance(co1, Contact)
        self.assertEqual(co1.rx_account, '*****@*****.**')

        co2 = contact_l.get_contact('Alice')
        self.assertIsInstance(co2, Contact)
        self.assertEqual(co2.rx_account, '*****@*****.**')
Ejemplo n.º 7
0
    def test_getters(self):
        # Setup
        contact1 = Contact('*****@*****.**', '*****@*****.**', 'Alice',
                           32 * b'\x01', 32 * b'\x02', True, True, True)
        contact2 = Contact('*****@*****.**', '*****@*****.**', 'Charlie',
                           32 * b'\x01', 32 * b'\x02', True, True, True)
        settings = Settings()
        master_k = MasterKey()
        contact_l = ContactList(master_k, settings)
        contact_l.contacts = [contact1, contact2]

        # Test
        self.assertEqual(
            contact_l.contact_selectors(),
            ['*****@*****.**', '*****@*****.**', 'Alice', 'Charlie'])
        self.assertEqual(contact_l.get_list_of_accounts(),
                         ['*****@*****.**', '*****@*****.**'])
        self.assertEqual(contact_l.get_list_of_nicks(), ['Alice', 'Charlie'])
        self.assertEqual(contact_l.get_list_of_users_accounts(),
                         ['*****@*****.**'])
Ejemplo n.º 8
0
    def test_store_and_load_contacts(self):
        # Setup
        contact = Contact('*****@*****.**', '*****@*****.**', 'Alice',
                          32 * b'\x01', 32 * b'\x02', True, True, True)
        settings = Settings()
        master_k = MasterKey()
        contact_l = ContactList(master_k, settings)
        contact_l.contacts = 5 * [contact]
        contact_l.store_contacts()

        # Test
        contact_l2 = ContactList(master_k, settings)
        self.assertEqual(len(contact_l2), 5)
        for c in contact_l2:
            self.assertIsInstance(c, Contact)

        self.assertTrue(os.path.isfile(f'{DIR_USER_DATA}/ut_contacts'))
        self.assertEqual(
            os.path.getsize(f'{DIR_USER_DATA}/ut_contacts'),
            24 + 20 * (1024 + 1024 + 1024 + 32 + 32 + 1 + 1 + 1) + 16)
        os.remove(f'{DIR_USER_DATA}/ut_contacts')
Ejemplo n.º 9
0
    def test_local_contact(self):
        # Setup
        contact1 = Contact('*****@*****.**', '*****@*****.**', 'Alice',
                           32 * b'\x01', 32 * b'\x02', True, True, True)
        contact_l = ContactList(MasterKey(), Settings())
        contact_l.contacts = [contact1]
        o_get_tty_w = src.common.misc.get_tty_w
        src.common.misc.get_tty_w = lambda x: 1

        # Test
        self.assertFalse(contact_l.has_local_contact())

        contact_l.add_contact('local', 'local', 'local', 32 * b'\x03',
                              32 * b'\x04', True, True, True)

        self.assertTrue(contact_l.has_local_contact())
        self.assertIsNone(contact_l.print_contacts())
        self.assertIsNone(contact_l.print_contacts(spacing=True))

        # Teardown
        src.common.misc.get_tty_w = o_get_tty_w
Ejemplo n.º 10
0
    def test_remove_contact(self):
        # Setup
        contact1 = Contact('*****@*****.**', '*****@*****.**', 'Alice',
                           32 * b'\x01', 32 * b'\x02', True, True, True)
        contact2 = Contact('*****@*****.**', '*****@*****.**', 'Charlie',
                           32 * b'\x01', 32 * b'\x02', True, True, True)
        contact_l = ContactList(MasterKey(), Settings())
        contact_l.contacts = [contact1, contact2]

        # Test
        self.assertTrue(contact_l.has_contacts())
        self.assertTrue(contact_l.has_contact('Alice'))
        self.assertTrue(contact_l.has_contact('*****@*****.**'))

        contact_l.remove_contact('*****@*****.**')
        self.assertFalse(contact_l.has_contact('Alice'))
        self.assertFalse(contact_l.has_contact('*****@*****.**'))

        contact_l.remove_contact('Charlie')
        self.assertEqual(len(contact_l.contacts), 0)
        self.assertFalse(contact_l.has_contacts())
Ejemplo n.º 11
0
    def test_database_encryption_with_another_key(self):
        # Setup
        window                = RxWindow(type=WIN_TYPE_CONTACT, uid='*****@*****.**', name='Alice')
        contact_list          = ContactList(self.old_key, self.settings)
        contact_list.contacts = [create_contact()]
        group_list            = GroupList()

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

        for p in assembly_packet_creator(MESSAGE, b'This is a short message'):
            write_log_entry(p, '*****@*****.**', self.settings, self.old_key, origin=ORIGIN_CONTACT_HEADER)
        for p in assembly_packet_creator(MESSAGE, b'This is a short message'):
            write_log_entry(p, '*****@*****.**', self.settings, self.old_key)

        # Test
        self.assertPrints((CLEAR_ENTIRE_SCREEN + CURSOR_LEFT_UP_CORNER + f"""\
Logfile of messages to/from Alice
════════════════════════════════════════════════════════════════════════════════
{self.time} Alice: This is a short message
{self.time}    Me: This is a short message
<End of logfile>

"""), access_logs, window, contact_list, group_list, self.settings, self.old_key)

        self.assertIsNone(re_encrypt(self.old_key.master_key, self.new_key.master_key, self.settings))

        # Test that decryption works with new key
        self.assertPrints((CLEAR_ENTIRE_SCREEN + CURSOR_LEFT_UP_CORNER + f"""\
Logfile of messages to/from Alice
════════════════════════════════════════════════════════════════════════════════
{self.time} Alice: This is a short message
{self.time}    Me: This is a short message
<End of logfile>

"""), access_logs, window, contact_list, group_list, self.settings, self.new_key)

        # Test that temp file is removed
        self.assertFalse(os.path.isfile("user_data/ut_logs_temp"))
Ejemplo n.º 12
0
    def test_class(self):
        # Setup
        master_key = MasterKey()
        settings = Settings()
        contact_list = ContactList(master_key, settings)
        group_list = GroupList(master_key, settings, contact_list)
        members = [
            create_contact(n) for n in [
                'Alice', 'Bob', 'Charlie', 'David', 'Eric', 'Fido', 'Gunter',
                'Heidi', 'Ivan', 'Joana', 'Karol'
            ]
        ]
        contact_list.contacts = members
        groups = [
            Group(n, False, False, members, settings,
                  group_list.store_groups())
            for n in [
                'testgroup_1', 'testgroup_2', 'testgroup3', 'testgroup_4',
                'testgroup_5', 'testgroup_6', 'testgroup_7', 'testgroup8',
                'testgroup_9', 'testgroup_10', 'testgroup_11'
            ]
        ]
        group_list.groups = groups
        group_list.store_groups()

        # Test
        for g in group_list:
            self.assertIsInstance(g, Group)
        self.assertEqual(len(group_list), 11)

        self.assertTrue(os.path.isfile(f'{DIR_USER_DATA}/ut_groups'))
        self.assertEqual(os.path.getsize(f'{DIR_USER_DATA}/ut_groups'),
                         24 + 32 + 20 * (1024 + 2 + (20 * 1024)) + 16)

        settings.m_number_of_groups = 10
        settings.m_members_in_group = 10

        group_list2 = GroupList(master_key, settings, contact_list)

        self.assertEqual(len(group_list2), 11)

        self.assertEqual(settings.m_number_of_groups, 20)
        self.assertEqual(settings.m_members_in_group, 20)

        bytestring = group_list2.generate_header()
        self.assertEqual(len(bytestring), 32)
        self.assertIsInstance(bytestring, bytes)

        dg_bytestring = group_list2.generate_dummy_group()
        self.assertEqual(len(dg_bytestring), (1024 + 2 + (20 * 1024)))
        self.assertIsInstance(dg_bytestring, bytes)

        members.append(create_contact('Laura'))
        group_list2.add_group('testgroup_12', False, False, members)
        group_list2.add_group('testgroup_12', False, True, members)
        self.assertTrue(group_list2.get_group('testgroup_12').notifications)
        self.assertEqual(len(group_list2), 12)
        self.assertEqual(group_list2.largest_group(), 12)

        g_names = [
            'testgroup_1', 'testgroup_2', 'testgroup3', 'testgroup_4',
            'testgroup_5', 'testgroup_6', 'testgroup_7', 'testgroup8',
            'testgroup_9', 'testgroup_10', 'testgroup_11', 'testgroup_12'
        ]
        self.assertEqual(group_list2.get_list_of_group_names(), g_names)

        g_o = group_list2.get_group('testgroup_1')
        self.assertIsInstance(g_o, Group)
        self.assertEqual(g_o.name, 'testgroup_1')
        self.assertTrue(group_list2.has_group('testgroup_12'))
        self.assertFalse(group_list2.has_group('testgroup_13'))
        self.assertTrue(group_list2.has_groups(), True)

        members = group_list2.get_group_members('testgroup_1')
        for c in members:
            self.assertIsInstance(c, Contact)

        self.assertEqual(len(group_list2), 12)
        group_list2.remove_group('testgroup_13')
        self.assertEqual(len(group_list2), 12)
        group_list2.remove_group('testgroup_12')
        self.assertEqual(len(group_list2), 11)
        self.assertIsNone(group_list2.print_groups())

        # Teardown
        cleanup()
Ejemplo n.º 13
0
    def test_read_private_message(self):
        # Setup
        masterkey = MasterKey()
        settings  = Settings()
        window    = Window(type='contact', uid='*****@*****.**', name='Alice')

        contact_list          = ContactList(masterkey, settings)
        contact_list.contacts = [create_contact('Alice')]

        with self.assertRaises(FunctionReturn):
            self.assertIsNone(access_history(window, contact_list, settings, masterkey))

        for p in self.mock_entry_preprocessor('This is a short message'):
            write_log_entry(p, '*****@*****.**', settings, masterkey)

        long_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.")

        for p in self.mock_entry_preprocessor(long_msg):
            write_log_entry(p, '*****@*****.**', settings, masterkey)

        # Add packet cancelled half-way
        packets = self.mock_entry_preprocessor(long_msg)
        packets = packets[2:] + [M_C_HEADER + bytes(255)]
        for p in packets:
            write_log_entry(p, '*****@*****.**', settings, masterkey)

        # Test
        self.assertIsNone(access_history(window, contact_list, settings, masterkey))

        # Test window UID mismatch
        window.uid = '*****@*****.**'
        self.assertIsNone(access_history(window, contact_list, settings, masterkey))

        # Test window type mismatch
        window.uid  = '*****@*****.**'
        window.type = 'group'
        self.assertIsNone(access_history(window, contact_list, settings, masterkey))

        # Group messages

        window = Window(type='group', uid='testgroup', name='testgroup')

        contact_list          = ContactList(masterkey, settings)
        contact_list.contacts = [create_contact(n) for n in ['Alice', 'Charlie']]

        for p in self.mock_entry_preprocessor('This is a short message', group=True):
            write_log_entry(p, '*****@*****.**',   settings, masterkey)
            write_log_entry(p, '*****@*****.**', settings, masterkey)

        long_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.")

        for p in self.mock_entry_preprocessor(long_msg, group=True):
            write_log_entry(p, '*****@*****.**',   settings, masterkey)
            write_log_entry(p, '*****@*****.**', settings, masterkey)

        # Test
        self.assertIsNone(access_history(window, contact_list, settings, masterkey))

        # Test window name mismatch
        window.name = '*****@*****.**'
        self.assertIsNone(access_history(window, contact_list, settings, masterkey))

        # Test window type mismatch
        window.name = 'testgroup'
        window.type = 'contact'
        self.assertIsNone(access_history(window, contact_list, settings, masterkey))

        # Re-encrypt log database

        # Create garbage file to remove
        with open(f'{DIR_USER_DATA}/{settings.software_operation}_logs_temp', 'wb+') as f:
            f.write(b'will screw decryption')

        self.assertIsNone(re_encrypt(masterkey.master_key, 32 * b'\x01', settings))
        masterkey.master_key = 32 * b'\x01'
        self.assertIsNone(access_history(window, contact_list, settings, masterkey))
        self.assertIsNone(access_history(window, contact_list, settings, masterkey, export=True))

        cleanup()
        with self.assertRaises(FunctionReturn):
            re_encrypt(masterkey.master_key, 32 * b'\x01', settings)

        # Teardown
        os.remove("Unittest - Plaintext log (testgroup)")
        cleanup()