tests/extmod: Add tests for ssl.SSLContext.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2023-06-25 14:16:20 +10:00
parent e8a4c1dd53
commit b50da3dbd7
4 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,25 @@
# Very basic test of ssl.SSLContext class.
try:
import socket, ssl
except ImportError:
print("SKIP")
raise SystemExit
# Test constructing with arguments.
ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
# Test printing object.
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
print("SSLContext" in str(ctx))
# Coverage test for destructor, and calling it twice.
if hasattr(ctx, "__del__"):
ctx.__del__()
ctx.__del__()
# Test calling .wrap_socket() method, multiple times.
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.wrap_socket(socket.socket(), do_handshake_on_connect=False)
ctx.wrap_socket(socket.socket(), do_handshake_on_connect=False)

View File

@ -0,0 +1,29 @@
# Test MicroPython-specific behaviour of ssl.SSLContext.
try:
import ssl
except ImportError:
print("SKIP")
raise SystemExit
# Test constructing without any arguments (in CPython it's a DeprecationWarning).
try:
ssl.SSLContext()
except TypeError:
print("TypeError")
# Test attributes that don't exist (in CPython new attributes can be added).
# This test is needed for coverage because SSLContext implements a custom attr handler.
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
try:
ctx.does_not_exist
except AttributeError:
print("AttributeError on load")
try:
ctx.does_not_exist = None
except AttributeError:
print("AttributeError on store")
try:
del ctx.does_not_exist
except AttributeError:
print("AttributeError on delete")

View File

@ -0,0 +1,4 @@
TypeError
AttributeError on load
AttributeError on store
AttributeError on delete

View File

@ -0,0 +1,24 @@
# Test ssl.SSLContext.verify_mode attribute.
# It's not available in the axtls implementation, so has an independent test.
try:
import ssl
except ImportError:
print("SKIP")
raise SystemExit
if not hasattr(ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT), "verify_mode"):
print("SKIP")
raise SystemExit
# Test default verify_mode for server (client default is different in MicroPython).
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
print(ctx.verify_mode == ssl.CERT_NONE)
# Test setting and getting verify_mode.
ctx.verify_mode = ssl.CERT_NONE
print(ctx.verify_mode == ssl.CERT_NONE)
ctx.verify_mode = ssl.CERT_OPTIONAL
print(ctx.verify_mode == ssl.CERT_OPTIONAL)
ctx.verify_mode = ssl.CERT_REQUIRED
print(ctx.verify_mode == ssl.CERT_REQUIRED)