Example #1
0
    def test_cancel_message_when_nothing_to_cancel(self):
        # Setup
        window = Window(name='Alice', type='contact', uid='*****@*****.**')
        user_input = UserInput('cm')
        window.window_contacts = [create_contact('Alice')]
        settings = Settings()
        queues = {MESSAGE_PACKET_QUEUE: Queue(), FILE_PACKET_QUEUE: Queue()}

        # Test
        self.assertIsNone(cancel_packet(user_input, window, settings, queues))
        time.sleep(0.2)
        self.assertEqual(queues[FILE_PACKET_QUEUE].qsize(), 0)
Example #2
0
    def test_file_queue_short_trickle(self):
        # Setup
        input_data = os.urandom(5)
        with open('testfile.txt', 'wb+') as f:
            f.write(input_data)

        settings = Settings(session_trickle=True, disable_gui_dialog=True)
        window = Window(name='Alice', type='contact', uid='*****@*****.**')
        gateway = Gateway(delay=0.02)
        f_queue = Queue()
        input_list = ['./testfile.txt', 'Yes']
        gen = iter(input_list)

        def mock_input(_):
            return str(next(gen))

        builtins.input = mock_input

        # Test
        self.assertIsNone(queue_file(window, settings, f_queue, gateway))
        time.sleep(0.5)
        self.assertEqual(f_queue.qsize(), 1)
        q_data, l_d = f_queue.get()

        self.assertIsInstance(q_data, bytes)
        self.assertIsInstance(l_d, dict)

        # Teardown
        os.remove('testfile.txt')
        time.sleep(0.2)
        f_queue.close()
Example #3
0
    def test_aborted_file(self):
        # Setup
        input_data = os.urandom(5)
        with open('testfile.txt', 'wb+') as f:
            f.write(input_data)

        settings = Settings(session_trickle=True, disable_gui_dialog=True)
        window = Window(name='Alice', type='contact', uid='*****@*****.**')
        gateway = Gateway(delay=0.02)
        f_queue = Queue()
        input_list = ['./testfile.txt', 'No']
        gen = iter(input_list)

        def mock_input(_):
            return str(next(gen))

        builtins.input = mock_input

        # Test
        self.assertFR("File selection aborted.", queue_file, window, settings,
                      f_queue, gateway)

        # Teardown
        os.remove('testfile.txt')
        time.sleep(0.2)
        f_queue.close()
Example #4
0
    def test_active_group_raises_fr(self):
        # Setup
        window = Window(type='group')

        # Test
        self.assertFR("Error: Group is selected.", change_nick, None, window,
                      None, None, None, None)
Example #5
0
    def test_disable_file_reception_for_all_users(self):
        # Setup
        user_input = UserInput('store off all')
        contact_list = ContactList(nicks=['Alice', 'Bob'])
        group_list = GroupList()
        contact = contact_list.get_contact('*****@*****.**')
        settings = Settings()
        c_queue = Queue()
        window = Window(uid='*****@*****.**',
                        type='contact',
                        contact=contact,
                        window_contacts=[contact])
        for c in contact_list:
            c.file_reception = True
        for g in group_list:
            g.file_reception = True

        # Test
        for c in contact_list:
            self.assertTrue(c.file_reception)
        for g in group_list:
            self.assertTrue(g.log_messages)
        self.assertIsNone(
            contact_setting(user_input, window, contact_list, group_list,
                            settings, c_queue))
        time.sleep(0.2)
        for c in contact_list:
            self.assertFalse(c.file_reception)
        for g in group_list:
            self.assertFalse(g.log_messages)
Example #6
0
    def test_disable_notifications_for_all_users(self):
        # Setup
        user_input = UserInput('notify off all')
        contact_list = ContactList(nicks=['Alice', 'Bob'])
        group_list = GroupList(groups=['testgroup'])
        contact = contact_list.get_contact('*****@*****.**')
        settings = Settings()
        c_queue = Queue()
        window = Window(uid='*****@*****.**',
                        type='contact',
                        contact=contact,
                        window_contacts=[contact])
        for c in contact_list:
            c.notifications = True
        for g in group_list:
            g.notifications = True

        # Test
        for c in contact_list:
            self.assertTrue(c.notifications)
        for g in group_list:
            self.assertTrue(g.notifications)
        self.assertIsNone(
            contact_setting(user_input, window, contact_list, group_list,
                            settings, c_queue))
        time.sleep(0.2)
        for c in contact_list:
            self.assertFalse(c.notifications)
        for g in group_list:
            self.assertFalse(g.notifications)
Example #7
0
 def test_fingerprint_print_command(self):
     # Setup
     window = Window(name='Alice',
                     type='contact',
                     contact=create_contact('Alice'))
     # Test
     self.assertIsNone(fingerprints(window))
Example #8
0
    def test_successful_removal_of_contact(self):
        # Setup
        o_input = builtins.input
        builtins.input = lambda x: 'Yes'
        user_input = UserInput('rm Alice')
        contact_list = ContactList(nicks=['Alice'])
        window = Window(window_contacts=[contact_list.get_contact('Alice')],
                        type='contact')
        group_list = GroupList(groups=['testgroup'])
        settings = Settings()
        queues = {KEY_MANAGEMENT_QUEUE: Queue(), COMMAND_PACKET_QUEUE: Queue()}

        # Test
        for g in group_list:
            self.assertIsInstance(g, Group)
            self.assertTrue(g.has_member('*****@*****.**'))

        self.assertIsNone(
            remove_contact(user_input, window, contact_list, group_list,
                           settings, queues))
        self.assertEqual(queues[COMMAND_PACKET_QUEUE].qsize(), 1)

        km_data = queues[KEY_MANAGEMENT_QUEUE].get()
        self.assertEqual(km_data, ('REM', '*****@*****.**'))
        self.assertFalse(contact_list.has_contact('*****@*****.**'))

        for g in group_list:
            self.assertIsInstance(g, Group)
            self.assertFalse(g.has_member('*****@*****.**'))

        # Teardown
        builtins.input = o_input
Example #9
0
    def test_contact_not_present_on_txm(self):
        # Setup
        o_input = builtins.input
        builtins.input = lambda x: 'Yes'
        user_input = UserInput('rm [email protected]')
        contact_list = ContactList(nicks=['Bob'])
        window = Window(window_contact=[contact_list.get_contact('Bob')],
                        type='group')
        group_list = GroupList(groups=[])
        settings = Settings()
        queues = {KEY_MANAGEMENT_QUEUE: Queue(), COMMAND_PACKET_QUEUE: Queue()}

        # Test
        self.assertIsNone(
            remove_contact(user_input, window, contact_list, group_list,
                           settings, queues))
        self.assertEqual(queues[COMMAND_PACKET_QUEUE].qsize(), 1)

        command_packet, settings_ = queues[COMMAND_PACKET_QUEUE].get()
        self.assertIsInstance(command_packet, bytes)
        self.assertIsInstance(settings_, Settings)

        # Teardown
        builtins.input = o_input

        queues[KEY_MANAGEMENT_QUEUE].close()
        queues[COMMAND_PACKET_QUEUE].close()
Example #10
0
    def test_active_group_raises_fr(self):
        # Setup
        settings = Settings()
        window = Window(type='group')

        # Test
        self.assertFR("Group is selected.", rxm_load_psk, window, None,
                      settings, None)
Example #11
0
    def test_missing_nick_raises_fr(self):
        # Setup
        user_input = UserInput("nick ")
        window = Window(type='contact')

        # Test
        self.assertFR("Error: No nick specified.", change_nick, user_input,
                      window, None, None, None, None)
Example #12
0
    def test_x25519_key_raises_fr(self):
        # Setup
        settings = Settings()
        window = Window(type='contact', uid='*****@*****.**')
        contact_list = ContactList(nicks=['Alice'])

        # Test
        self.assertFR("Current key was exchanged with X25519.", rxm_load_psk,
                      window, contact_list, settings, None)
Example #13
0
 def test_psk_raises_fr(self):
     # Setup
     contact = create_contact('Alice')
     contact.tx_fingerprint = bytes(32)
     window = Window(name='Alice', type='contact', contact=contact)
     # Test
     self.assertFR(
         "Key have been pre-shared with Alice and thus have no fingerprints.",
         fingerprints, window)
Example #14
0
    def test_missing_file_raises_fr(self):
        # Setup
        settings = Settings()
        window = Window()
        gateway = Gateway(delay=0.02)

        # Test
        self.assertFR("Error: File not found.", File, './testfile.txt', window,
                      settings, gateway)
Example #15
0
    def test_class(self):
        # Setup
        o_input = builtins.input
        input_list = [
            '/', '', 'testmessage', '/file', '/nick Alice', 'testmessage',
            '/nick Alice', '  '
        ]
        gen = iter(input_list)

        def mock_input(_):
            return str(next(gen))

        builtins.input = mock_input

        window = Window(name='Alice',
                        type='contact',
                        window_contacts=[create_contact('Alice')])
        settings = Settings()

        # Test
        user_input = UserInput(window, settings)
        self.assertEqual(user_input.plaintext, 'testmessage')
        self.assertEqual(user_input.type, 'message')

        user_input = UserInput(window, settings)
        self.assertEqual(user_input.plaintext, '/file')
        self.assertEqual(user_input.type, 'file')

        user_input = UserInput(window, settings)
        self.assertEqual(user_input.plaintext, 'nick Alice')
        self.assertEqual(user_input.type, 'command')

        window = Window(name='Testgroup', type='group', window_contacts=[])

        user_input = UserInput(window, settings)
        self.assertEqual(user_input.plaintext, 'nick Alice')
        self.assertEqual(user_input.type, 'command')

        user_input = UserInput(window, settings)
        self.assertEqual(user_input.plaintext, 'clear')
        self.assertEqual(user_input.type, 'command')

        # Teardown
        builtins.input = o_input
Example #16
0
    def test_cancel_group_message_during_normal(self):
        # Setup
        window = Window(name='testgroup', type='group', uid='testgroup')
        user_input = UserInput('cm')
        window.window_contacts = [create_contact('Alice')]
        settings = Settings()
        queues = {MESSAGE_PACKET_QUEUE: Queue(), FILE_PACKET_QUEUE: Queue()}

        # Test
        queues[MESSAGE_PACKET_QUEUE].put(
            ('testmessage1', settings, '*****@*****.**', '*****@*****.**',
             False, 'testgroup'))
        queues[MESSAGE_PACKET_QUEUE].put(
            ('testmessage2', settings, '*****@*****.**', '*****@*****.**',
             False, 'testgroup'))
        time.sleep(0.2)
        self.assertIsNone(cancel_packet(user_input, window, settings, queues))
        time.sleep(0.2)
        self.assertEqual(queues[MESSAGE_PACKET_QUEUE].qsize(), 1)
Example #17
0
    def test_cancel_message_during_trickle(self):
        # Setup
        user_input = UserInput('cm')
        window = Window()
        window.window_contacts = [create_contact('Alice')]
        settings = Settings(session_trickle=True)
        queues = {MESSAGE_PACKET_QUEUE: Queue(), FILE_PACKET_QUEUE: Queue()}

        # Test
        queues[MESSAGE_PACKET_QUEUE].put(('testmessage1', {
            '*****@*****.**': False
        }))
        queues[MESSAGE_PACKET_QUEUE].put(('testmessage2', {
            '*****@*****.**': False
        }))
        time.sleep(0.2)
        self.assertIsNone(cancel_packet(user_input, window, settings, queues))
        time.sleep(0.2)
        self.assertEqual(queues[MESSAGE_PACKET_QUEUE].qsize(), 1)
Example #18
0
    def test_invalid_nick_raises_fr(self):
        # Setup
        user_input = UserInput("nick Alice\x01")
        window = Window(type='contact', contact=create_contact('Alice'))
        contact_list = ContactList(nicks=['Alice'])
        group_list = GroupList()

        # Test
        self.assertFR("Nick must be printable.", change_nick, user_input,
                      window, contact_list, group_list, None, None)
Example #19
0
    def test_empty_file_raises_fr(self):
        # Setup
        with open('testfile.txt', 'wb+') as f:
            f.write(b'')

        settings = Settings()
        window = Window()
        gateway = Gateway(delay=0.02)

        # Test
        self.assertFR("Error: Target file is empty. No file was sent.", File,
                      './testfile.txt', window, settings, gateway)
Example #20
0
    def test_invalid_export(self):
        # Test
        user_input = UserInput("history a")
        window = Window()
        contact_list = ContactList()
        settings = Settings()
        c_queue = Queue()
        master_key = MasterKey()

        # Test
        self.assertFR("Specified invalid number of messages to print.",
                      print_logs, user_input, window, contact_list, settings,
                      c_queue, master_key)
Example #21
0
    def test_user_abort_raises_fr(self):
        # Setup
        user_input = UserInput('export')
        window = Window(uid='*****@*****.**', name='Alice')
        o_input = builtins.input
        builtins.input = lambda x: 'No'

        # Test
        self.assertFR("Logfile export aborted.", export_logs, user_input,
                      window, None, None, None, None)

        # Teardown
        builtins.input = o_input
Example #22
0
    def test_no_nh_cmd_during_trickle(self):
        # Setup
        window = Window(imc_name='*****@*****.**')
        settings = Settings(session_trickle=True)
        c_queue = Queue()
        gateway = Gateway()

        # Test
        self.assertIsNone(clear_screens(window, settings, c_queue, gateway))
        self.assertEqual(len(gateway.packets), 0)
        self.assertEqual(c_queue.qsize(), 1)

        # Teardown
        while not c_queue.empty():
            c_queue.get()
        time.sleep(0.2)
Example #23
0
    def test_successful_command(self):
        # Setup
        settings = Settings()
        window = Window(type='contact', uid='*****@*****.**')
        contact = create_contact('Alice', txfp=bytes(32))
        contact_list = ContactList(contacts=[contact])
        queue = Queue()

        # Test
        self.assertIsNone(rxm_load_psk(window, contact_list, settings, queue))
        self.assertEqual(queue.qsize(), 1)

        # Teardown
        while not queue.empty():
            queue.get()
        time.sleep(0.2)
Example #24
0
    def test_oversize_filename_raises_fr(self):
        # Setup
        f_name = 250 * 'a' + '.txt'
        with open(f_name, 'wb+') as f:
            f.write(b'a')

        settings = Settings()
        window = Window()
        gateway = Gateway(delay=0.02)

        # Test
        self.assertFR("Error: File name is too long. No file was sent.", File,
                      f'./{f_name}', window, settings, gateway)

        # Teardown
        os.remove(f_name)
Example #25
0
    def test_clear_screens(self):
        # Setup
        window = Window(imc_name='*****@*****.**')
        settings = Settings()
        c_queue = Queue()
        gateway = Gateway()

        # Test
        self.assertIsNone(clear_screens(window, settings, c_queue, gateway))
        self.assertEqual(len(gateway.packets), 1)
        self.assertTrue(gateway.packets[0].startswith(b'*****@*****.**'))
        self.assertEqual(c_queue.qsize(), 1)

        # Teardown
        while not c_queue.empty():
            c_queue.get()
        time.sleep(0.2)
Example #26
0
    def test_command(self):
        # Setup
        window = Window(name='*****@*****.**', uid='*****@*****.**')
        settings = Settings()
        c_queue = Queue()
        o_input = builtins.input
        builtins.input = lambda x: ''

        # Test
        self.assertIsNone(rxm_display_f_win(window, settings, c_queue))
        self.assertEqual(c_queue.qsize(), 2)

        # Teardown
        builtins.input = o_input
        while not c_queue.empty():
            c_queue.get()
        time.sleep(0.2)
Example #27
0
    def test_successful_nick_change(self):
        # Setup
        user_input = UserInput("nick Alice_")
        contact_list = ContactList(nicks=['Alice'])
        window = Window(name='Alice',
                        type='contact',
                        contact=contact_list.get_contact('Alice'))
        group_list = GroupList()
        settings = Settings()
        c_queue = Queue()

        # Test
        self.assertIsNone(
            change_nick(user_input, window, contact_list, group_list, settings,
                        c_queue))
        contact = contact_list.get_contact('*****@*****.**')
        self.assertEqual(contact.nick, 'Alice_')
Example #28
0
    def test_normal_contact(self):
        # Setup
        settings = Settings()
        m_queue = Queue()
        window = Window(type='contact',
                        window_contacts=[create_contact('Alice')],
                        uid='*****@*****.**')

        # Short messages
        user_input = UserInput(
            plaintext=binascii.hexlify(os.urandom(125)).decode())
        self.assertIsNone(queue_message(user_input, window, settings, m_queue))

        p, s, ra, ta, ls, wu = m_queue.get()
        self.assertIsInstance(p, bytes)
        self.assertEqual(len(p), 256)

        self.assertIsInstance(s, Settings)
        self.assertEqual(ra, '*****@*****.**')
        self.assertEqual(ta, '*****@*****.**')
        self.assertEqual(ls, True)
        self.assertEqual(wu, '*****@*****.**')

        # Long messages
        user_input = UserInput(
            plaintext=binascii.hexlify(os.urandom(250)).decode())
        self.assertIsNone(queue_message(user_input, window, settings, m_queue))
        self.assertEqual(m_queue.qsize(), 2)

        while not m_queue.empty():
            p, s, ra, ta, ls, wu = m_queue.get()
            self.assertIsInstance(p, bytes)
            self.assertEqual(len(p), 256)

            self.assertIsInstance(s, Settings)
            self.assertEqual(ra, '*****@*****.**')
            self.assertEqual(ta, '*****@*****.**')
            self.assertEqual(ls, True)
            self.assertEqual(wu, '*****@*****.**')

        # Teardown
        time.sleep(0.2)
        m_queue.close()
Example #29
0
 def test_enable_notifications_for_user(self):
     # Setup
     user_input = UserInput('notify on')
     contact_list = ContactList(nicks=['Alice'])
     group_list = GroupList()
     settings = Settings()
     c_queue = Queue()
     contact = contact_list.get_contact('Alice')
     contact.notifications = False
     window = Window(uid='*****@*****.**',
                     type='contact',
                     contact=contact)
     # Test
     self.assertFalse(contact.notifications)
     self.assertIsNone(
         contact_setting(user_input, window, contact_list, group_list,
                         settings, c_queue))
     time.sleep(0.2)
     self.assertTrue(contact.notifications)
Example #30
0
    def test_file_queue_long_normal(self):
        # Setup
        input_data = os.urandom(200000)
        with open('testfile.txt', 'wb+') as f:
            f.write(input_data)

        settings = Settings(session_trickle=False,
                            disable_gui_dialog=True,
                            confirm_sent_files=True)
        window = Window(name='Alice',
                        type='contact',
                        uid='*****@*****.**',
                        window_contacts=[create_contact('Alice')])
        gateway = Gateway(delay=0.02)
        f_queue = Queue()

        input_list = ['./testfile.txt', 'Yes']
        gen = iter(input_list)

        def mock_input(_):
            return str(next(gen))

        builtins.input = mock_input

        # Test
        self.assertIsNone(queue_file(window, settings, f_queue, gateway))
        time.sleep(1)
        self.assertEqual(f_queue.qsize(), 982)

        while not f_queue.empty():
            p, s, ra, ta, lm, wu = f_queue.get()
            self.assertIsInstance(p, bytes)
            self.assertIsInstance(s, Settings)
            self.assertEqual(ra, '*****@*****.**')
            self.assertEqual(ta, '*****@*****.**')
            self.assertEqual(wu, '*****@*****.**')
            self.assertTrue(lm)

        # Teardown
        os.remove('testfile.txt')
        time.sleep(0.5)
        f_queue.close()
        time.sleep(0.5)