def test_udp_socket_send_encrypted_messages_when_encryption_in_transit_set_to_true( ): # Given key = generate_key_32() msg = b"tests" socket_ip = "127.0.0.1" socket_port = 50017 test_socket_port = 50018 n_msg = 2 test_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) test_socket.bind((socket_ip, test_socket_port)) test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) udp_socket = UDPSocket(key=key, socket_ip=socket_ip, socket_port=socket_port, max_queue_size=n_msg, encryption_in_transit=True).start() # When udp_socket.sendto(msg, (socket_ip, test_socket_port)) rcv_msg = test_socket.recv(100) time.sleep(.1) udp_socket.stop() test_socket.close() time.sleep(.5) # Then assert rcv_msg != msg assert msg == udp_socket._decrypt(rcv_msg)
def test_change_key_correctly_change_the_fernet_encoder(): # Given key_old = generate_key_32() key_new = generate_key_32() msg = b"tests" # When udp_socket = UDPSocket(key=key_old, socket_port=50016).start() msg_crypt = udp_socket._encrypt(msg) msg_decrypt_old = udp_socket._decrypt(msg_crypt) udp_socket.change_key(key_new) # Then assert msg_decrypt_old == msg udp_socket.stop() time.sleep(.5)