def test_contact_canceled_file(self): # Setup account = '*****@*****.**' contact = create_contact('Alice') origin = ORIGIN_CONTACT_HEADER type_ = 'file' settings = Settings() packet = Packet(account, contact, origin, type_, settings) file_data = os.urandom(10000) compressed = zlib.compress(file_data, level=9) file_key = os.urandom(32) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = US_BYTE.join([b'testfile.txt', b'11.0B', b'00d 00h 00m 00s', encoded]) packets = self.mock_file_preprocessor(file_data) packets = packets[:20] packets.append(byte_padding(F_C_HEADER)) # Add cancel packet # Test for p in packets: packet.add_packet(p) self.assertEqual(len(packet.assembly_pt_list), 0) # Cancel packet empties packet list self.assertFalse(packet.lt_active) self.assertFalse(packet.is_complete)
def test_disabled_file_reception_raises_fr_with_append_packet(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_CONTACT_HEADER, FILE, self.settings) packet.long_active = True compressed = zlib.compress(os.urandom(10000), level=COMPRESSION_LEVEL) file_key = os.urandom(KEY_LENGTH) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = int_to_bytes(1000) + int_to_bytes(10000)+ b'testfile.txt' + US_BYTE + encoded packets = split_to_assembly_packets(file_data, FILE) for p in packets[:2]: self.assertIsNone(packet.add_packet(p)) packet.contact.file_reception = False # Test self.assertFR("Alert! File reception disabled mid-transfer.", packet.add_packet, packets[2]) for p in packets[3:]: self.assertFR("Missing start packet.", packet.add_packet, p) self.assertEqual(packet.log_masking_ctr, len(packets))
def test_long_message(self): # Setup account = '*****@*****.**' contact = create_contact('Alice') origin = ORIGIN_USER_HEADER type_ = 'message' settings = Settings() packet = Packet(account, contact, origin, type_, settings) 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.") packets = self.mock_message_preprocessor(long_msg) # Test for p in packets: packet.add_packet(p) message = packet.assemble_message_packet() message = message[1:].decode() self.assertEqual(message, long_msg)
def test_long_file(self): # Setup account = '*****@*****.**' contact = create_contact('Alice') origin = ORIGIN_CONTACT_HEADER type_ = 'file' settings = Settings() packet = Packet(account, contact, origin, type_, settings) file_data = os.urandom(10000) compressed = zlib.compress(file_data, level=9) file_key = os.urandom(32) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = US_BYTE.join([b'testfile.txt', b'11.0B', b'00d 00h 00m 00s', encoded]) packets = self.mock_file_preprocessor(file_data) # Test for p in packets: packet.add_packet(p) self.assertIsNone(packet.assemble_and_store_file()) self.assertTrue(os.path.isfile('received_files/Alice/testfile.txt')) self.assertEqual(os.path.getsize('received_files/Alice/testfile.txt'), 10000) # Teardown shutil.rmtree('received_files/')
def test_compression_error_raises_fr(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_USER_HEADER, MESSAGE, self.settings) payload = zlib.compress(b"Lorem ipsum", level=COMPRESSION_LEVEL)[::-1] packet_list = [M_S_HEADER + byte_padding(payload)] for p in packet_list: packet.add_packet(p) # Test self.assertFR("Error: Decompression of message failed.", packet.assemble_message_packet)
def test_short_command(self): # Setup packet = Packet(LOCAL_ID, self.contact, ORIGIN_CONTACT_HEADER, COMMAND, self.settings) packets = split_to_assembly_packets(b'testcommand', COMMAND) for p in packets: packet.add_packet(p) # Test self.assertEqual(packet.assemble_command_packet(), b'testcommand') self.assertEqual(packet.log_masking_ctr, 0)
def test_short_message(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_USER_HEADER, MESSAGE, self.settings) plaintext = "Lorem ipsum dolor sit amet, consectetur adipiscing elit".encode() packets = split_to_assembly_packets(plaintext, MESSAGE) for p in packets: packet.add_packet(p) # Test self.assertEqual(packet.assemble_message_packet(), plaintext)
def test_long_message(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_USER_HEADER, MESSAGE, self.settings) packets = split_to_assembly_packets(self.msg.encode(), MESSAGE) for p in packets: packet.add_packet(p) # Test message = packet.assemble_message_packet() self.assertEqual(message.decode(), self.msg)
def test_long_command(self): # Setup packet = Packet(LOCAL_ID, self.contact, ORIGIN_CONTACT_HEADER, COMMAND, self.settings) command = os.urandom(500) packets = split_to_assembly_packets(command, COMMAND) for p in packets: packet.add_packet(p) # Test self.assertEqual(packet.assemble_command_packet(), command) self.assertEqual(packet.log_masking_ctr, 0)
def test_long_command_hash_mismatch_raises_fr(self): # Setup packet = Packet(LOCAL_ID, self.contact, ORIGIN_CONTACT_HEADER, COMMAND, self.settings) command = os.urandom(500) + b'a' packets = split_to_assembly_packets(command, COMMAND) packets = [p.replace(b'a', b'c') for p in packets] for p in packets: packet.add_packet(p) # Test self.assertFR("Error: Received an invalid command.", packet.assemble_command_packet) self.assertEqual(packet.log_masking_ctr, 0)
def test_long_command_hash_mismatch_raises_fr(self): # Setup account = 'local' contact = create_contact('local') origin = ORIGIN_CONTACT_HEADER type_ = 'command' settings = Settings() packet = Packet(account, contact, origin, type_, settings) command = os.urandom(500) + b'a' packets = self.mock_command_preprocessor(command) packets = [p.replace(b'a', b'c') for p in packets] # Test for p in packets: packet.add_packet(p) self.assertFR('Received an invalid command.', packet.assemble_command_packet)
def test_long_command(self): # Setup account = 'local' contact = create_contact('local') origin = ORIGIN_CONTACT_HEADER type_ = 'command' settings = Settings() packet = Packet(account, contact, origin, type_, settings) command = os.urandom(500) packets = self.mock_command_preprocessor(command) # Test for p in packets: packet.add_packet(p) purp_cmd = packet.assemble_command_packet() self.assertEqual(purp_cmd, command)
def setUp(self): self.contact_list = ContactList(nicks=['Alice', 'Bob']) self.settings = Settings() packet = Packet('*****@*****.**', self.contact_list.get_contact('Alice'), ORIGIN_CONTACT_HEADER, MESSAGE, self.settings) self.packet_list = PacketList(self.settings, self.contact_list) self.packet_list.packets = [packet]
def test_long_command_compression_error_raises_fr(self): # Setup packet = Packet(LOCAL_ID, self.contact, ORIGIN_CONTACT_HEADER, COMMAND, self.settings) command = os.urandom(500) + b'a' payload = zlib.compress(command, level=COMPRESSION_LEVEL)[::-1] payload += hash_chain(payload) padded = byte_padding(payload) p_list = split_byte_string(padded, item_len=PADDING_LEN) packet_list = ([C_L_HEADER + p_list[0]] + [C_A_HEADER + p for p in p_list[1:-1]] + [C_E_HEADER + p_list[-1]]) for p in packet_list: packet.add_packet(p) # Test self.assertFR("Error: Decompression of command failed.", packet.assemble_command_packet) self.assertEqual(packet.log_masking_ctr, 0)
def test_decryption_error_raises_fr(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_USER_HEADER, MESSAGE, self.settings) payload = zlib.compress(self.msg.encode(), level=COMPRESSION_LEVEL) msg_key = bytes(KEY_LENGTH) payload = encrypt_and_sign(payload, msg_key)[::-1] payload += msg_key padded = byte_padding(payload) p_list = split_byte_string(padded, item_len=PADDING_LEN) packet_list = ([M_L_HEADER + p_list[0]] + [M_A_HEADER + p for p in p_list[1:-1]] + [M_E_HEADER + p_list[-1]]) for p in packet_list: packet.add_packet(p) # Test self.assertFR("Error: Decryption of message failed.", packet.assemble_message_packet)
def test_short_file_from_user_raises_fr(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_USER_HEADER, FILE, self.settings) packets = split_to_assembly_packets(self.short_f_data, FILE) # Test for p in packets: self.assertFR("Ignored file from user.", packet.add_packet, p) self.assertEqual(packet.log_masking_ctr, 1)
def test_short_message(self): # Setup account = '*****@*****.**' contact = create_contact('Alice') origin = ORIGIN_USER_HEADER type_ = 'message' settings = Settings() packet = Packet(account, contact, origin, type_, settings) message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit" packets = self.mock_message_preprocessor(message) # Test for p in packets: packet.add_packet(p) message = packet.assemble_message_packet() message = message[1:].decode() self.assertEqual(message, message)
def test_missing_start_packet_raises_fr(self): account = '*****@*****.**' contact = create_contact('Alice') origin = ORIGIN_USER_HEADER type_ = 'message' settings = Settings() packet = Packet(account, contact, origin, type_, settings) self.assertFR('Missing start packet.', packet.add_packet, (M_A_HEADER + bytes(254))) self.assertFR('Missing start packet.', packet.add_packet, (M_E_HEADER + bytes(254)))
def test_invalid_assembly_packet_header_raises_fr(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_USER_HEADER, MESSAGE, self.settings) plaintext = "Lorem ipsum dolor sit amet, consectetur adipiscing elit".encode() packets = split_to_assembly_packets(plaintext, MESSAGE) # Test self.assertFR("Error: Received packet had an invalid assembly packet header.", packet.add_packet, b'i' + packets[0][1:]) self.assertEqual(packet.log_masking_ctr, 1)
def test_unauthorized_file_from_contact_raises_fr(self): # Setup self.contact.file_reception = False packet = Packet('*****@*****.**', self.contact, ORIGIN_CONTACT_HEADER, FILE, self.settings) packets = split_to_assembly_packets(self.short_f_data, FILE) # Test for p in packets: self.assertFR("Alert! File transmission from Alice but reception is disabled.", packet.add_packet, p) self.assertEqual(packet.log_masking_ctr, 1)
def test_decryption_error_raises_fr(self): # Setup account = '*****@*****.**' contact = create_contact('Alice') origin = ORIGIN_USER_HEADER type_ = 'message' settings = Settings() packet = Packet(account, contact, origin, type_, settings) 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.") plaintext = long_msg.encode() payload = zlib.compress(plaintext, level=9) msg_key = bytes(32) payload = encrypt_and_sign(payload, msg_key) if payload[:-1] == b'a': # Remove false positives payload = payload[-1:] + b'c' else: payload = payload[-1:] + b'a' payload += msg_key padded = byte_padding(payload) p_list = split_byte_string(padded, item_len=255) packet_list = ([M_L_HEADER + p_list[0]] + [M_A_HEADER + p for p in p_list[1:-1]] + [M_E_HEADER + p_list[-1]]) # Test for p in packet_list: packet.add_packet(p) self.assertFR('Decryption of long message failed.', packet.assemble_message_packet)
def test_noise_packet_interrupts_file(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_CONTACT_HEADER, FILE, self.settings) compressed = zlib.compress(os.urandom(10000), level=COMPRESSION_LEVEL) file_key = os.urandom(KEY_LENGTH) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = int_to_bytes(1000) + int_to_bytes(10000) + b'testfile.txt' + US_BYTE + encoded packets = split_to_assembly_packets(file_data, FILE) packets = packets[:20] packets.append(byte_padding(P_N_HEADER)) # Add cancel packet for p in packets: packet.add_packet(p) # Test self.assertEqual(len(packet.assembly_pt_list), 0) # Cancel packet empties packet list self.assertFalse(packet.long_active) self.assertFalse(packet.is_complete) self.assertEqual(packet.log_masking_ctr, len(packets))
def test_long_file_from_user_raises_fr(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_USER_HEADER, FILE, self.settings) compressed = zlib.compress(os.urandom(10000), level=COMPRESSION_LEVEL) file_key = os.urandom(KEY_LENGTH) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = int_to_bytes(1000) + int_to_bytes(10000) + b'testfile.txt' + US_BYTE + encoded packets = split_to_assembly_packets(file_data, FILE) # Test self.assertFR("Ignored file from user.", packet.add_packet, packets[0]) self.assertEqual(packet.log_masking_ctr, 1)
def test_empty_file_raises_fr(self): # Setup account = '*****@*****.**' contact = create_contact('Alice') origin = ORIGIN_CONTACT_HEADER type_ = 'file' settings = Settings() packet = Packet(account, contact, origin, type_, settings) file_data = b'' compressed = zlib.compress(file_data, level=9) file_key = os.urandom(32) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = US_BYTE.join([b'testfile.txt', b'11.0B', b'00d 00h 00m 00s', encoded]) packets = self.mock_file_preprocessor(file_data) # Test for p in packets: packet.add_packet(p) self.assertFR('Received file did not contain data.', packet.assemble_and_store_file)
def test_invalid_long_file_header_raises_fr(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_CONTACT_HEADER, FILE, self.settings) compressed = zlib.compress(os.urandom(10000), level=COMPRESSION_LEVEL) file_key = os.urandom(KEY_LENGTH) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = int_to_bytes(1000) + int_to_bytes(10000) + binascii.unhexlify('3f264d4189d7a091') + US_BYTE + encoded packets = split_to_assembly_packets(file_data, FILE) # Test self.assertFR("Error: Received file packet had an invalid header.", packet.add_packet, packets[0]) self.assertEqual(packet.log_masking_ctr, 1)
def test_compression_error_raises_fr(self): # Setup account = '*****@*****.**' contact = create_contact('Alice') origin = ORIGIN_USER_HEADER type_ = 'message' settings = Settings() packet = Packet(account, contact, origin, type_, settings) message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit" plaintext = message.encode() payload = zlib.compress(plaintext, level=9) if payload[:-1] == b'a': # Remove false positives payload = payload[-1:] + b'c' else: payload = payload[-1:] + b'a' padded = byte_padding(payload) packet_list = [M_S_HEADER + padded] # Test for p in packet_list: packet.add_packet(p) self.assertFR('Decompression of long message failed.', packet.assemble_message_packet)
def test_unauthorized_long_file_raises_fr(self): # Setup self.contact.file_reception = False packet = Packet('*****@*****.**', self.contact, ORIGIN_CONTACT_HEADER, FILE, self.settings) compressed = zlib.compress(os.urandom(10000), level=COMPRESSION_LEVEL) file_key = os.urandom(KEY_LENGTH) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = int_to_bytes(1000) + int_to_bytes(10000) + b'testfile.txt' + US_BYTE + encoded packets = split_to_assembly_packets(file_data, FILE) # Test self.assertFR("Alert! File transmission from Alice but reception is disabled.", packet.add_packet, packets[0]) self.assertEqual(packet.log_masking_ctr, 1)
def test_short_file(self): # Setup packets = split_to_assembly_packets(self.short_f_data, FILE) # Test self.assertFalse(os.path.isfile(f'{DIR_RX_FILES}Alice/testfile.txt')) self.assertFalse(os.path.isfile(f'{DIR_RX_FILES}Alice/testfile.txt.1')) packet = Packet('*****@*****.**', self.contact, ORIGIN_CONTACT_HEADER, FILE, self.settings) packet.long_active = True for p in packets: packet.add_packet(p) self.assertIsNone(packet.assemble_and_store_file()) self.assertTrue(os.path.isfile(f'{DIR_RX_FILES}Alice/testfile.txt')) for p in packets: packet.add_packet(p) self.assertIsNone(packet.assemble_and_store_file()) self.assertTrue(os.path.isfile(f'{DIR_RX_FILES}Alice/testfile.txt.1')) # Teardown shutil.rmtree(DIR_RX_FILES)
def test_invalid_long_file_header_raises_fr(self): # Setup account = '*****@*****.**' contact = create_contact('Alice') origin = ORIGIN_CONTACT_HEADER type_ = 'file' settings = Settings() packet = Packet(account, contact, origin, type_, settings) file_data = os.urandom(10000) compressed = zlib.compress(file_data, level=9) file_key = os.urandom(32) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = US_BYTE.join([b'testfile.txt', b'11.0B', encoded]) packets = self.mock_file_preprocessor(file_data) # Test self.assertFR("Received packet had an invalid header.", packet.add_packet, packets[0])
def test_unauthorized_long_file_raises_fr(self): # Setup account = '*****@*****.**' contact = create_contact('Alice') contact.file_reception = False origin = ORIGIN_CONTACT_HEADER type_ = 'file' settings = Settings() packet = Packet(account, contact, origin, type_, settings) file_data = os.urandom(10000) compressed = zlib.compress(file_data, level=9) file_key = os.urandom(32) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = US_BYTE.join([b'testfile.txt', b'11.0B', b'00d 00h 00m 00s', encoded]) packets = self.mock_file_preprocessor(file_data) # Test self.assertFR("Unauthorized long file from contact.", packet.add_packet, packets[0])