08-ssl-connect-cert-auth-pw.py 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#!/usr/bin/env python3

# Test whether a client produces a correct connect and subsequent disconnect when using SSL.
# Client must provide a certificate - the private key is encrypted with a password.

# The client should connect to port 1888 with keepalive=60, clean session set,
# and client id 08-ssl-connect-crt-auth
# It should use the CA certificate ssl/all-ca.crt for verifying the server.
# The test will send a CONNACK message to the client with rc=0. Upon receiving
# the CONNACK and verifying that rc=0, the client should send a DISCONNECT
# message. If rc!=0, the client should exit with an error.

import context
import paho_test
from paho_test import ssl

context.check_ssl()

rc = 1
keepalive = 60
connect_packet = paho_test.gen_connect("08-ssl-connect-crt-auth-pw", keepalive=keepalive)
connack_packet = paho_test.gen_connack(rc=0)
disconnect_packet = paho_test.gen_disconnect()

ssock = paho_test.create_server_socket_ssl(cert_reqs=ssl.CERT_REQUIRED)

client = context.start_client()

try:
    (conn, address) = ssock.accept()
    conn.settimeout(10)

R
Roger A. Light 已提交
33
    paho_test.expect_packet(conn, "connect", connect_packet)
R
Roger A. Light 已提交
34
    conn.send(connack_packet)
35

R
Roger A. Light 已提交
36
    paho_test.expect_packet(conn, "disconnect", disconnect_packet)
R
Roger A. Light 已提交
37
    rc = 0
38 39 40 41 42 43 44 45

    conn.close()
finally:
    client.terminate()
    client.wait()
    ssock.close()

exit(rc)