Skip to content

Commit

Permalink
tests/extmod: Add tests for ssl.SSLContext.
Browse files Browse the repository at this point in the history
Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Jun 26, 2023
1 parent e8a4c1d commit b50da3d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/extmod/ssl_sslcontext.py
@@ -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)
29 changes: 29 additions & 0 deletions tests/extmod/ssl_sslcontext_micropython.py
@@ -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")
4 changes: 4 additions & 0 deletions tests/extmod/ssl_sslcontext_micropython.py.exp
@@ -0,0 +1,4 @@
TypeError
AttributeError on load
AttributeError on store
AttributeError on delete
24 changes: 24 additions & 0 deletions tests/extmod/ssl_sslcontext_verify_mode.py
@@ -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)

0 comments on commit b50da3d

Please sign in to comment.