From b50da3dbd7e98dfe5360684a55b342666c7683ca Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 25 Jun 2023 14:16:20 +1000 Subject: [PATCH] tests/extmod: Add tests for ssl.SSLContext. Signed-off-by: Damien George --- tests/extmod/ssl_sslcontext.py | 25 ++++++++++++++++ tests/extmod/ssl_sslcontext_micropython.py | 29 +++++++++++++++++++ .../extmod/ssl_sslcontext_micropython.py.exp | 4 +++ tests/extmod/ssl_sslcontext_verify_mode.py | 24 +++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 tests/extmod/ssl_sslcontext.py create mode 100644 tests/extmod/ssl_sslcontext_micropython.py create mode 100644 tests/extmod/ssl_sslcontext_micropython.py.exp create mode 100644 tests/extmod/ssl_sslcontext_verify_mode.py diff --git a/tests/extmod/ssl_sslcontext.py b/tests/extmod/ssl_sslcontext.py new file mode 100644 index 0000000000..23ff9c2964 --- /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 0000000000..136fb8a054 --- /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 0000000000..21e65258ff --- /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 0000000000..daccc2f4a9 --- /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)