2017-03-09 02:42:34 +00:00
|
|
|
# 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")
|
2017-06-10 18:14:16 +01:00
|
|
|
raise SystemExit
|
2017-03-09 02:42:34 +00:00
|
|
|
|
2022-07-16 15:48:41 +01:00
|
|
|
|
|
|
|
class TestSocket(io.IOBase):
|
|
|
|
def write(self, buf):
|
|
|
|
return len(buf)
|
|
|
|
|
|
|
|
def readinto(self, buf):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def ioctl(self, cmd, arg):
|
|
|
|
print("TestSocket.ioctl", cmd, arg)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def setblocking(self, value):
|
|
|
|
print("TestSocket.setblocking({})".format(value))
|
|
|
|
|
|
|
|
|
2017-03-09 02:42:34 +00:00
|
|
|
# create in client mode
|
|
|
|
try:
|
2022-07-16 15:48:41 +01:00
|
|
|
ss = ssl.wrap_socket(TestSocket(), server_hostname="test.example.com")
|
2017-03-09 02:42:34 +00:00
|
|
|
except OSError as er:
|
2022-07-16 15:48:41 +01:00
|
|
|
print("OSError: client")
|
2017-03-09 02:42:34 +00:00
|
|
|
|
|
|
|
# create in server mode (can use this object for further tests)
|
2022-07-16 15:48:41 +01:00
|
|
|
ss = ssl.wrap_socket(TestSocket(), server_side=1, do_handshake=0)
|
2017-03-09 02:42:34 +00:00
|
|
|
|
|
|
|
# print
|
|
|
|
print(repr(ss)[:12])
|
|
|
|
|
2022-07-16 15:48:41 +01:00
|
|
|
# setblocking() propagates call to the underlying stream object
|
|
|
|
ss.setblocking(False)
|
|
|
|
ss.setblocking(True)
|
2017-03-09 02:42:34 +00:00
|
|
|
|
|
|
|
# write
|
2022-07-16 15:48:41 +01:00
|
|
|
try:
|
|
|
|
ss.write(b"aaaa")
|
|
|
|
except OSError:
|
|
|
|
pass
|
2017-03-09 02:42:34 +00:00
|
|
|
|
|
|
|
# read (underlying socket has no data)
|
|
|
|
try:
|
|
|
|
ss.read(8)
|
2022-07-16 15:48:41 +01:00
|
|
|
except OSError:
|
|
|
|
pass
|
2017-03-09 02:42:34 +00:00
|
|
|
|
|
|
|
# close
|
|
|
|
ss.close()
|
2017-07-19 22:20:53 +01:00
|
|
|
# close 2nd time
|
|
|
|
ss.close()
|
|
|
|
|
|
|
|
# read on closed socket
|
|
|
|
try:
|
|
|
|
ss.read(10)
|
|
|
|
except OSError as er:
|
2022-07-16 15:48:41 +01:00
|
|
|
print("OSError: read")
|
2017-03-09 02:42:34 +00:00
|
|
|
|
|
|
|
# write on closed socket
|
|
|
|
try:
|
|
|
|
ss.write(b"aaaa")
|
|
|
|
except OSError as er:
|
2022-07-16 15:48:41 +01:00
|
|
|
print("OSError: write")
|