1
1
# test that socket.connect() on a non-blocking socket raises EINPROGRESS
2
2
# and that an immediate write/send/read/recv does the right thing
3
3
4
- import sys , time , socket , errno , ssl
4
+ import errno
5
+ import select
6
+ import socket
7
+ import ssl
5
8
6
- isMP = sys .implementation .name == "micropython"
9
+ # only mbedTLS supports non-blocking mode
10
+ if not hasattr (ssl , "MBEDTLS_VERSION" ):
11
+ print ("SKIP" )
12
+ raise SystemExit
7
13
8
14
9
- def dp (e ):
10
- # uncomment next line for development and testing, to print the actual exceptions
11
- # print(repr(e))
12
- pass
15
+ # get the name of an errno error code
16
+ def errno_name (er ):
17
+ if er == errno .EAGAIN :
18
+ return "EAGAIN"
19
+ if er == errno .EINPROGRESS :
20
+ return "EINPROGRESS"
21
+ return er
13
22
14
23
15
24
# do_connect establishes the socket and wraps it if tls is True.
@@ -22,112 +31,75 @@ def do_connect(peer_addr, tls, handshake):
22
31
# print("Connecting to", peer_addr)
23
32
s .connect (peer_addr )
24
33
except OSError as er :
25
- print ("connect:" , er .errno == errno .EINPROGRESS )
26
- if er .errno != errno .EINPROGRESS :
27
- print (" got" , er .errno )
34
+ print ("connect:" , errno_name (er .errno ))
28
35
# wrap with ssl/tls if desired
29
36
if tls :
30
37
ssl_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
31
- if hasattr (ssl_context , "check_hostname" ):
32
- ssl_context .check_hostname = False
33
-
34
38
try :
35
39
s = ssl_context .wrap_socket (s , do_handshake_on_connect = handshake )
36
- print ("wrap: True" )
40
+ print ("wrap ok : True" )
37
41
except Exception as e :
38
- dp (e )
39
- print ("wrap:" , e )
40
- elif handshake :
41
- # just sleep a little bit, this allows any connect() errors to happen
42
- time .sleep (0.2 )
42
+ print ("wrap er:" , e )
43
43
return s
44
44
45
45
46
+ # poll a socket and print out the result
47
+ def poll (s ):
48
+ poller = select .poll ()
49
+ poller .register (s )
50
+ print ("poll: " , poller .poll (0 ))
51
+
52
+
46
53
# test runs the test against a specific peer address.
47
- def test (peer_addr , tls = False , handshake = False ):
48
- # MicroPython plain sockets have read/write, but CPython's don't
49
- # MicroPython TLS sockets and CPython's have read/write
50
- # hasRW captures this wonderful state of affairs
51
- hasRW = isMP or tls
54
+ def test (peer_addr , tls , handshake ):
55
+ # MicroPython plain and TLS sockets have read/write
56
+ hasRW = True
52
57
53
- # MicroPython plain sockets and CPython's have send/recv
54
- # MicroPython TLS sockets don't have send/recv, but CPython's do
55
- # hasSR captures this wonderful state of affairs
56
- hasSR = not (isMP and tls )
58
+ # MicroPython plain sockets have send/recv
59
+ # MicroPython TLS sockets don't have send/recv
60
+ hasSR = not tls
57
61
58
62
# connect + send
63
+ # non-blocking send should raise EAGAIN
59
64
if hasSR :
60
65
s = do_connect (peer_addr , tls , handshake )
61
- # send -> 4 or EAGAIN
66
+ poll ( s )
62
67
try :
63
68
ret = s .send (b"1234" )
64
- print ("send:" , handshake and ret == 4 )
69
+ print ("send ok :" , ret ) # shouldn't get here
65
70
except OSError as er :
66
- #
67
- dp (er )
68
- print ("send:" , er .errno in (errno .EAGAIN , errno .EINPROGRESS ))
71
+ print ("send er:" , errno_name (er .errno ))
69
72
s .close ()
70
- else : # fake it...
71
- print ("connect:" , True )
72
- if tls :
73
- print ("wrap:" , True )
74
- print ("send:" , True )
75
73
76
74
# connect + write
75
+ # non-blocking write should return None
77
76
if hasRW :
78
77
s = do_connect (peer_addr , tls , handshake )
79
- # write -> None
80
- try :
81
- ret = s .write (b"1234" )
82
- print ("write:" , ret in (4 , None )) # SSL may accept 4 into buffer
83
- except OSError as er :
84
- dp (er )
85
- print ("write:" , False ) # should not raise
86
- except ValueError as er : # CPython
87
- dp (er )
88
- print ("write:" , er .args [0 ] == "Write on closed or unwrapped SSL socket." )
78
+ poll (s )
79
+ ret = s .write (b"1234" )
80
+ print ("write: " , ret )
89
81
s .close ()
90
- else : # fake it...
91
- print ("connect:" , True )
92
- if tls :
93
- print ("wrap:" , True )
94
- print ("write:" , True )
95
82
83
+ # connect + recv
84
+ # non-blocking recv should raise EAGAIN
96
85
if hasSR :
97
- # connect + recv
98
86
s = do_connect (peer_addr , tls , handshake )
99
- # recv -> EAGAIN
87
+ poll ( s )
100
88
try :
101
- print ("recv:" , s .recv (10 ))
89
+ ret = s .recv (10 )
90
+ print ("recv ok:" , ret ) # shouldn't get here
102
91
except OSError as er :
103
- dp (er )
104
- print ("recv:" , er .errno == errno .EAGAIN )
92
+ print ("recv er:" , errno_name (er .errno ))
105
93
s .close ()
106
- else : # fake it...
107
- print ("connect:" , True )
108
- if tls :
109
- print ("wrap:" , True )
110
- print ("recv:" , True )
111
94
112
95
# connect + read
96
+ # non-blocking read should return None
113
97
if hasRW :
114
98
s = do_connect (peer_addr , tls , handshake )
115
- # read -> None
116
- try :
117
- ret = s .read (10 )
118
- print ("read:" , ret is None )
119
- except OSError as er :
120
- dp (er )
121
- print ("read:" , False ) # should not raise
122
- except ValueError as er : # CPython
123
- dp (er )
124
- print ("read:" , er .args [0 ] == "Read on closed or unwrapped SSL socket." )
99
+ poll (s )
100
+ ret = s .read (10 )
101
+ print ("read: " , ret )
125
102
s .close ()
126
- else : # fake it...
127
- print ("connect:" , True )
128
- if tls :
129
- print ("wrap:" , True )
130
- print ("read:" , True )
131
103
132
104
133
105
if __name__ == "__main__" :
@@ -136,10 +108,8 @@ def test(peer_addr, tls=False, handshake=False):
136
108
print ("--- Plain sockets to nowhere ---" )
137
109
test (socket .getaddrinfo ("192.0.2.1" , 80 )[0 ][- 1 ], False , False )
138
110
print ("--- SSL sockets to nowhere ---" )
139
- # this test fails with AXTLS because do_handshake=False blocks on first read/write and
140
- # there it times out until the connect is aborted
141
111
test (socket .getaddrinfo ("192.0.2.1" , 443 )[0 ][- 1 ], True , False )
142
112
print ("--- Plain sockets ---" )
143
- test (socket .getaddrinfo ("micropython.org" , 80 )[0 ][- 1 ], False , True )
113
+ test (socket .getaddrinfo ("micropython.org" , 80 )[0 ][- 1 ], False , False )
144
114
print ("--- SSL sockets ---" )
145
115
test (socket .getaddrinfo ("micropython.org" , 443 )[0 ][- 1 ], True , True )
0 commit comments