Skip to content

Commit 8007f84

Browse files
committed
tests/net_hosted: Convert connect-nonblock-xfer test to use unittest.
This allows it to run parts of the test on esp8266 (or any target using axTLS). Signed-off-by: Damien George <damien@micropython.org>
1 parent 1d9ec23 commit 8007f84

File tree

2 files changed

+62
-82
lines changed

2 files changed

+62
-82
lines changed
Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
# test that socket.connect() on a non-blocking socket raises EINPROGRESS
22
# and that an immediate write/send/read/recv does the right thing
33

4+
import unittest
45
import errno
56
import select
67
import socket
78
import ssl
89

910
# only mbedTLS supports non-blocking mode
10-
if not hasattr(ssl, "MBEDTLS_VERSION"):
11-
print("SKIP")
12-
raise SystemExit
11+
ssl_supports_nonblocking = hasattr(ssl, "MBEDTLS_VERSION")
1312

1413

1514
# get the name of an errno error code
@@ -24,34 +23,43 @@ def errno_name(er):
2423
# do_connect establishes the socket and wraps it if tls is True.
2524
# If handshake is true, the initial connect (and TLS handshake) is
2625
# allowed to be performed before returning.
27-
def do_connect(peer_addr, tls, handshake):
26+
def do_connect(self, peer_addr, tls, handshake):
2827
s = socket.socket()
2928
s.setblocking(False)
3029
try:
31-
# print("Connecting to", peer_addr)
30+
print("Connecting to", peer_addr)
3231
s.connect(peer_addr)
32+
self.fail()
3333
except OSError as er:
3434
print("connect:", errno_name(er.errno))
35+
self.assertEqual(er.errno, errno.EINPROGRESS)
36+
3537
# wrap with ssl/tls if desired
3638
if tls:
39+
print("wrap socket")
3740
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
38-
try:
39-
s = ssl_context.wrap_socket(s, do_handshake_on_connect=handshake)
40-
print("wrap ok: True")
41-
except Exception as e:
42-
print("wrap er:", e)
41+
s = ssl_context.wrap_socket(s, do_handshake_on_connect=handshake)
42+
4343
return s
4444

4545

46-
# poll a socket and print out the result
47-
def poll(s):
46+
# poll a socket and check the result
47+
def poll(self, s, expect_writable):
4848
poller = select.poll()
4949
poller.register(s)
50-
print("poll: ", poller.poll(0))
50+
result = poller.poll(0)
51+
print("poll:", result)
52+
if expect_writable:
53+
self.assertEqual(len(result), 1)
54+
self.assertEqual(result[0][1], select.POLLOUT)
55+
else:
56+
self.assertEqual(result, [])
5157

5258

53-
# test runs the test against a specific peer address.
54-
def test(peer_addr, tls, handshake):
59+
# do_test runs the test against a specific peer address.
60+
def do_test(self, peer_addr, tls, handshake):
61+
print()
62+
5563
# MicroPython plain and TLS sockets have read/write
5664
hasRW = True
5765

@@ -62,54 +70,70 @@ def test(peer_addr, tls, handshake):
6270
# connect + send
6371
# non-blocking send should raise EAGAIN
6472
if hasSR:
65-
s = do_connect(peer_addr, tls, handshake)
66-
poll(s)
73+
s = do_connect(self, peer_addr, tls, handshake)
74+
poll(self, s, False)
6775
try:
6876
ret = s.send(b"1234")
69-
print("send ok:", ret) # shouldn't get here
77+
self.fail()
7078
except OSError as er:
71-
print("send er:", errno_name(er.errno))
79+
print("send error:", errno_name(er.errno))
80+
self.assertEqual(er.errno, errno.EAGAIN)
7281
s.close()
7382

7483
# connect + write
7584
# non-blocking write should return None
7685
if hasRW:
77-
s = do_connect(peer_addr, tls, handshake)
78-
poll(s)
86+
s = do_connect(self, peer_addr, tls, handshake)
87+
poll(self, s, tls and handshake)
7988
ret = s.write(b"1234")
80-
print("write: ", ret)
89+
print("write:", ret)
90+
if tls and handshake:
91+
self.assertEqual(ret, 4)
92+
else:
93+
self.assertIsNone(ret)
8194
s.close()
8295

8396
# connect + recv
8497
# non-blocking recv should raise EAGAIN
8598
if hasSR:
86-
s = do_connect(peer_addr, tls, handshake)
87-
poll(s)
99+
s = do_connect(self, peer_addr, tls, handshake)
100+
poll(self, s, False)
88101
try:
89102
ret = s.recv(10)
90-
print("recv ok:", ret) # shouldn't get here
103+
self.fail()
91104
except OSError as er:
92-
print("recv er:", errno_name(er.errno))
105+
print("recv error:", errno_name(er.errno))
106+
self.assertEqual(er.errno, errno.EAGAIN)
93107
s.close()
94108

95109
# connect + read
96110
# non-blocking read should return None
97111
if hasRW:
98-
s = do_connect(peer_addr, tls, handshake)
99-
poll(s)
112+
s = do_connect(self, peer_addr, tls, handshake)
113+
poll(self, s, tls and handshake)
100114
ret = s.read(10)
101-
print("read: ", ret)
115+
print("read:", ret)
116+
self.assertIsNone(ret)
102117
s.close()
103118

104119

105-
if __name__ == "__main__":
120+
class Test(unittest.TestCase):
106121
# these tests use a non-existent test IP address, this way the connect takes forever and
107122
# we can see EAGAIN/None (https://tools.ietf.org/html/rfc5737)
108-
print("--- Plain sockets to nowhere ---")
109-
test(socket.getaddrinfo("192.0.2.1", 80)[0][-1], False, False)
110-
print("--- SSL sockets to nowhere ---")
111-
test(socket.getaddrinfo("192.0.2.1", 443)[0][-1], True, False)
112-
print("--- Plain sockets ---")
113-
test(socket.getaddrinfo("micropython.org", 80)[0][-1], False, False)
114-
print("--- SSL sockets ---")
115-
test(socket.getaddrinfo("micropython.org", 443)[0][-1], True, True)
123+
def test_plain_sockets_to_nowhere(self):
124+
do_test(self, socket.getaddrinfo("192.0.2.1", 80)[0][-1], False, False)
125+
126+
@unittest.skipIf(not ssl_supports_nonblocking, "SSL doesn't support non-blocking")
127+
def test_ssl_sockets_to_nowhere(self):
128+
do_test(self, socket.getaddrinfo("192.0.2.1", 443)[0][-1], True, False)
129+
130+
def test_plain_sockets(self):
131+
do_test(self, socket.getaddrinfo("micropython.org", 80)[0][-1], False, False)
132+
133+
@unittest.skipIf(not ssl_supports_nonblocking, "SSL doesn't support non-blocking")
134+
def test_ssl_sockets(self):
135+
do_test(self, socket.getaddrinfo("micropython.org", 443)[0][-1], True, True)
136+
137+
138+
if __name__ == "__main__":
139+
unittest.main()

tests/net_hosted/connect_nonblock_xfer.py.exp

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)