Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/extmod: Add tests for ssl.SSLContext.
Signed-off-by: Damien George <damien@micropython.org>
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
TypeError | ||
AttributeError on load | ||
AttributeError on store | ||
AttributeError on delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |