summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2023-06-25 14:16:20 +1000
committerDamien George <damien@micropython.org>2023-06-26 16:34:41 +1000
commitb50da3dbd7e98dfe5360684a55b342666c7683ca (patch)
treeadde0bd22d8473fdea00ebddca57e6294719e800
parente8a4c1dd537c7ccfba92936c6eee9d5cf529be9e (diff)
tests/extmod: Add tests for ssl.SSLContext.
Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--tests/extmod/ssl_sslcontext.py25
-rw-r--r--tests/extmod/ssl_sslcontext_micropython.py29
-rw-r--r--tests/extmod/ssl_sslcontext_micropython.py.exp4
-rw-r--r--tests/extmod/ssl_sslcontext_verify_mode.py24
4 files changed, 82 insertions, 0 deletions
diff --git a/tests/extmod/ssl_sslcontext.py b/tests/extmod/ssl_sslcontext.py
new file mode 100644
index 000000000..23ff9c296
--- /dev/null
+++ b/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)
diff --git a/tests/extmod/ssl_sslcontext_micropython.py b/tests/extmod/ssl_sslcontext_micropython.py
new file mode 100644
index 000000000..136fb8a05
--- /dev/null
+++ b/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")
diff --git a/tests/extmod/ssl_sslcontext_micropython.py.exp b/tests/extmod/ssl_sslcontext_micropython.py.exp
new file mode 100644
index 000000000..21e65258f
--- /dev/null
+++ b/tests/extmod/ssl_sslcontext_micropython.py.exp
@@ -0,0 +1,4 @@
+TypeError
+AttributeError on load
+AttributeError on store
+AttributeError on delete
diff --git a/tests/extmod/ssl_sslcontext_verify_mode.py b/tests/extmod/ssl_sslcontext_verify_mode.py
new file mode 100644
index 000000000..daccc2f4a
--- /dev/null
+++ b/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)