Exemple #1
0
        def queue_packet(mk, hk, tx_harac, packet, onion_pub_key=None):
            """Create encrypted datagram."""
            if onion_pub_key is None:
                header = b''
                queue = queues[COMMAND_DATAGRAM_HEADER]
                packet = split_to_assembly_packets(packet, COMMAND)[0]
            else:
                header = onion_pub_key + ORIGIN_CONTACT_HEADER
                queue = queues[MESSAGE_DATAGRAM_HEADER]
                packet = split_to_assembly_packets(packet, MESSAGE)[0]

            encrypted_harac = encrypt_and_sign(int_to_bytes(tx_harac), hk)
            encrypted_message = encrypt_and_sign(packet, mk)
            encrypted_packet = header + encrypted_harac + encrypted_message
            queue.put((datetime.datetime.now(), encrypted_packet))
Exemple #2
0
 def test_long_command(self):
     packet_list = split_to_assembly_packets(os.urandom(800), COMMAND)
     self.assertEqual(len(packet_list), 4)
     self.assertTrue(packet_list[0].startswith(C_L_HEADER))
     self.assertTrue(packet_list[1].startswith(C_A_HEADER))
     self.assertTrue(packet_list[2].startswith(C_A_HEADER))
     self.assertTrue(packet_list[3].startswith(C_E_HEADER))
Exemple #3
0
 def test_long_file(self):
     packet_list = split_to_assembly_packets(os.urandom(800), FILE)
     self.assertEqual(len(packet_list), 4)
     self.assertTrue(packet_list[0].startswith(F_L_HEADER + b'\x00\x00\x00\x00\x00\x00\x00\x04'))
     self.assertTrue(packet_list[1].startswith(F_A_HEADER))
     self.assertTrue(packet_list[2].startswith(F_A_HEADER))
     self.assertTrue(packet_list[3].startswith(F_E_HEADER))
Exemple #4
0
 def test_long_message(self):
     packet_list = split_to_assembly_packets(os.urandom(800), MESSAGE)
     self.assertEqual(len(packet_list), 4)
     self.assertTrue(packet_list[0].startswith(M_L_HEADER))
     self.assertTrue(packet_list[1].startswith(M_A_HEADER))
     self.assertTrue(packet_list[2].startswith(M_A_HEADER))
     self.assertTrue(packet_list[3].startswith(M_E_HEADER))
Exemple #5
0
    def test_short_file(self):
        # Setup
        packets = split_to_assembly_packets(self.short_f_data, FILE)

        # Test
        self.assertFalse(os.path.isfile(f'{DIR_RECV_FILES}Alice/testfile.txt'))
        self.assertFalse(
            os.path.isfile(f'{DIR_RECV_FILES}Alice/testfile.txt.1'))

        packet = Packet(self.onion_pub_key, ORIGIN_CONTACT_HEADER, FILE,
                        self.contact, self.settings)
        packet.long_active = True

        for p in packets:
            packet.add_packet(p)
        self.assertIsNone(
            packet.assemble_and_store_file(self.ts, self.onion_pub_key,
                                           self.window_list))
        self.assertTrue(os.path.isfile(f'{DIR_RECV_FILES}Alice/testfile.txt'))

        for p in packets:
            packet.add_packet(p)
        self.assertIsNone(
            packet.assemble_and_store_file(self.ts, self.onion_pub_key,
                                           self.window_list))
        self.assertTrue(
            os.path.isfile(f'{DIR_RECV_FILES}Alice/testfile.txt.1'))
Exemple #6
0
    def test_queue_command_normal(self):
        # Setup
        packet_list = split_to_assembly_packets(os.urandom(200), COMMAND)

        # Test
        self.assertIsNone(queue_assembly_packets(packet_list, COMMAND, *self.args))
        self.assertEqual(self.queues[COMMAND_PACKET_QUEUE].qsize(), 1)
        packet = self.queues[COMMAND_PACKET_QUEUE].get()
        self.assertIsInstance(packet, bytes)
Exemple #7
0
    def test_short_file_from_user_raises_fr(self):
        # Setup
        packet  = Packet(self.onion_pub_key, ORIGIN_USER_HEADER, FILE, self.contact, self.settings)
        packets = split_to_assembly_packets(self.short_f_data, FILE)

        # Test
        for p in packets:
            self.assert_fr("Ignored file from the user.", packet.add_packet, p)
        self.assertEqual(packet.log_masking_ctr, 1)
Exemple #8
0
    def test_queue_command_traffic_masking(self):
        # Setup
        packet_list                   = split_to_assembly_packets(os.urandom(200), COMMAND)
        self.settings.traffic_masking = True

        # Test
        self.assertIsNone(queue_assembly_packets(packet_list, COMMAND, *self.args))
        self.assertEqual(self.queues[TM_COMMAND_PACKET_QUEUE].qsize(), 1)
        data = self.queues[TM_COMMAND_PACKET_QUEUE].get()
        self.assertIsInstance(data, bytes)
Exemple #9
0
    def test_unauthorized_file_from_contact_raises_fr(self):
        # Setup
        self.contact.file_reception = False

        packet  = Packet(self.onion_pub_key, ORIGIN_CONTACT_HEADER, FILE, self.contact, self.settings)
        packets = split_to_assembly_packets(self.short_f_data, FILE)

        # Test
        for p in packets:
            self.assert_fr("Alert! File transmission from Alice but reception is disabled.", packet.add_packet, p)
        self.assertEqual(packet.log_masking_ctr, 1)
Exemple #10
0
    def test_queue_file_traffic_masking(self):
        # Setup
        packet_list                   = split_to_assembly_packets(os.urandom(200), FILE)
        self.settings.traffic_masking = True

        # Test
        self.assertIsNone(queue_assembly_packets(packet_list, FILE, *self.args))
        self.assertEqual(self.queues[TM_FILE_PACKET_QUEUE].qsize(), 1)
        packet, log_messages, log_as_ph = self.queues[TM_FILE_PACKET_QUEUE].get()
        self.assertIsInstance(packet, bytes)
        self.assertTrue(log_messages)
        self.assertFalse(log_as_ph)
Exemple #11
0
    def test_queue_message_normal(self):
        # Setup
        packet_list = split_to_assembly_packets(os.urandom(200), MESSAGE)

        # Test
        self.assertIsNone(queue_assembly_packets(packet_list, MESSAGE, *self.args))
        self.assertEqual(self.queues[MESSAGE_PACKET_QUEUE].qsize(), 1)

        packet, pub_key, log_setting, log_as_ph, win_uid = self.queues[MESSAGE_PACKET_QUEUE].get()
        self.assertIsInstance(packet, bytes)
        self.assertEqual(pub_key, nick_to_pub_key("Alice"))
        self.assertEqual(win_uid, nick_to_pub_key("Alice"))
        self.assertTrue(log_setting)
        self.assertFalse(log_as_ph)
Exemple #12
0
    def test_successful_export_command(self, *_):
        # Setup
        self.window.type = 'contact'
        self.window.uid = nick_to_pub_key('Alice')
        whisper_header = b'\x00'
        packet = split_to_assembly_packets(
            whisper_header + PRIVATE_MESSAGE_HEADER + b'test', MESSAGE)[0]
        write_log_entry(packet, nick_to_pub_key('Alice'), self.settings,
                        self.master_key)

        # Test
        for command in ['export', 'export 1']:
            self.assert_fr(f"Exported log file of contact 'Alice'.",
                           log_command, UserInput(command), self.window,
                           ContactList(nicks=['Alice']), self.group_list,
                           self.settings, self.queues, self.master_key)
Exemple #13
0
    def test_successful_export_command(self, *_):
        # Setup
        from src.common.db_masterkey import MasterKey
        self.master_key = MasterKey(operation=TX, local_test=True)
        self.window.type = WIN_TYPE_CONTACT
        self.window.uid = nick_to_pub_key('Alice')
        whisper_header = bool_to_bytes(False)
        packet = split_to_assembly_packets(
            whisper_header + PRIVATE_MESSAGE_HEADER + b'test', MESSAGE)[0]
        write_log_entry(packet, nick_to_pub_key('Alice'), self.settings,
                        self.master_key)

        # Test
        for command in ['export', 'export 1']:
            self.assert_fr(f"Exported log file of contact 'Alice'.",
                           log_command, UserInput(command), self.window,
                           ContactList(nicks=['Alice']), self.group_list,
                           self.settings, self.queues, self.master_key)
Exemple #14
0
 def test_short_command(self):
     packet_list = split_to_assembly_packets(os.urandom(50), COMMAND)
     self.assertEqual(len(packet_list), 1)
     self.assertTrue(packet_list[0].startswith(C_S_HEADER))
Exemple #15
0
 def test_short_file(self):
     packet_list = split_to_assembly_packets(os.urandom(50), FILE)
     self.assertEqual(len(packet_list), 1)
     self.assertTrue(packet_list[0].startswith(F_S_HEADER))
Exemple #16
0
 def test_short_message(self):
     packet_list = split_to_assembly_packets(b'Short message', MESSAGE)
     self.assertEqual(len(packet_list), 1)
     self.assertTrue(packet_list[0].startswith(M_S_HEADER))