circuitpython/tests/extmod/ussl_basic.py
Thorsten von Eicken 9aa214077e extmod/modussl: Improve exception error messages.
This commit adds human readable error messages when mbedtls or axtls raise
an exception.  Currently often just an EIO error is raised so the user is
lost and can't tell whether it's a cert error, buffer overrun, connecting
to a non-ssl port, etc.  The axtls and mbedtls error raising in the ussl
module is modified to raise:

    OSError(-err_num, "error string")

For axtls a small error table of strings is added and used for the second
argument of the OSErrer.  For mbedtls the code uses mbedtls' built-in
strerror function, and if there is an out of memory condition it just
produces OSError(-err_num).  Producing the error string for mbedtls is
conditional on them being included in the mbedtls build, via
MBEDTLS_ERROR_C.
2020-07-20 23:41:45 +10:00

61 lines
1.2 KiB
Python

# very basic test of ssl module, just to test the methods exist
try:
import uio as io
import ussl as ssl
except ImportError:
print("SKIP")
raise SystemExit
# create in client mode
try:
ss = ssl.wrap_socket(io.BytesIO(), server_hostname="test.example.com")
except OSError as er:
print("wrap_socket:", repr(er))
# create in server mode (can use this object for further tests)
socket = io.BytesIO()
ss = ssl.wrap_socket(socket, server_side=1)
# print
print(repr(ss)[:12])
# setblocking() propagates call to the underlying stream object, and
# io.BytesIO doesn't have setblocking() (in CPython too).
# try:
# ss.setblocking(False)
# except NotImplementedError:
# print('setblocking: NotImplementedError')
# ss.setblocking(True)
# write
print(ss.write(b"aaaa"))
# read (underlying socket has no data)
print(ss.read(8))
# read (underlying socket has data, but it's bad data)
socket.write(b"aaaaaaaaaaaaaaaa")
socket.seek(0)
try:
ss.read(8)
except OSError as er:
print("read:", repr(er))
# close
ss.close()
# close 2nd time
ss.close()
# read on closed socket
try:
ss.read(10)
except OSError as er:
print("read:", repr(er))
# write on closed socket
try:
ss.write(b"aaaa")
except OSError as er:
print("write:", repr(er))