mosquitto/test/broker/08-ssl-connect-no-auth.py
Olivier Gayot b7fb911428 tests: replace use of ssl.wrap_socket that throws warnings in Python 3.10
The function ssl.wrap_socket() is deprecated starting Python 3.7 because
it does not support hostname matching (which is considered insecure). In
Python 3.10, the function now throws warnings at runtime, which makes
Ubuntu / Debian autopkgtest fail.

The function ssl.SSLContext.wrap_socket comes in as the replacement and
has support for SNI and hostname matching.

Replaced all uses of ssl.wrap_socket() by equivalent using
ssl.SSLContext.wrap_socket().

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
2022-02-15 09:23:06 +01:00

57 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
# Test whether a valid CONNECT results in the correct CONNACK packet using an SSL connection.
from mosq_test_helper import *
if sys.version < '2.7':
print("WARNING: SSL not supported on Python 2.6")
exit(0)
def write_config(filename, port1, port2):
with open(filename, 'w') as f:
f.write("port %d\n" % (port2))
f.write("allow_anonymous true\n")
f.write("\n")
f.write("listener %d\n" % (port1))
f.write("allow_anonymous true\n")
f.write("cafile ../ssl/all-ca.crt\n")
f.write("certfile ../ssl/server.crt\n")
f.write("keyfile ../ssl/server.key\n")
(port1, port2) = mosq_test.get_port(2)
conf_file = os.path.basename(__file__).replace('.py', '.conf')
write_config(conf_file, port1, port2)
rc = 1
keepalive = 10
connect_packet = mosq_test.gen_connect("connect-success-test", keepalive=keepalive)
connack_packet = mosq_test.gen_connack(rc=0)
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, use_conf=True)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile="../ssl/test-root-ca.crt")
ssock = context.wrap_socket(sock, server_hostname="localhost")
ssock.settimeout(20)
ssock.connect(("localhost", port1))
mosq_test.do_send_receive(ssock, connect_packet, connack_packet, "connack")
rc = 0
ssock.close()
except mosq_test.TestError:
pass
finally:
os.remove(conf_file)
broker.terminate()
broker.wait()
(stdo, stde) = broker.communicate()
if rc:
print(stde.decode('utf-8'))
exit(rc)