Skip to content

Commit 1722c92

Browse files
committed
tests/multi_net: Update DTLS multi-net test.
The original version of this test had to exchange a 1 byte UDP packet before the DTLS handshake. This is no longer needed due to MSG_PEEK support. The test also doesn't work with HelloVerify enabled, as the first connection attempt always fails with an MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED result. Anticipate this by listening for the client twice on the server side. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 305700e commit 1722c92

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

tests/multi_net/tls_dtls_server_client.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,36 @@ def instance0():
3434

3535
multitest.next()
3636

37-
# Wait for the client to connect.
38-
data, client_addr = s.recvfrom(1)
39-
print("incoming connection", data)
40-
41-
# Connect back to the client, so the UDP socket can be used like a stream.
42-
s.connect(client_addr)
43-
44-
# Create the DTLS context and load the certificate.
4537
ctx = tls.SSLContext(tls.PROTOCOL_DTLS_SERVER)
4638
ctx.load_cert_chain(cert, key)
4739

48-
# Wrap the UDP socket in server mode.
49-
print("wrap socket")
50-
s = ctx.wrap_socket(s, server_side=1)
51-
52-
# Transfer some data.
53-
for _ in range(4):
54-
print(s.recv(16))
55-
s.send(b"server to client")
56-
57-
# Close the DTLS and UDP connection.
58-
s.close()
40+
# Because of "hello verify required", we expect the peer
41+
# to connect twice: once to set the cookie, then second time
42+
# successfully.
43+
#
44+
# As this isn't a real server, we hard-code two connection attempts
45+
for _ in range(2):
46+
print("waiting")
47+
# Wait for the client to connect so we know their address
48+
_, client_addr = s.recvfrom(1, socket.MSG_PEEK)
49+
print("incoming connection")
50+
s.connect(client_addr) # Connect back to the client
51+
52+
# Wrap the UDP socket in server mode.
53+
try:
54+
s = ctx.wrap_socket(s, server_side=1)
55+
except OSError as e:
56+
print(e)
57+
continue # wait for second connection
58+
59+
# Transfer some data.
60+
for i in range(4):
61+
print(s.recv(32))
62+
s.send(b"server to client " + str(i).encode())
63+
64+
# Close the DTLS and UDP connection.
65+
s.close()
66+
break
5967

6068

6169
# DTLS client.
@@ -68,9 +76,6 @@ def instance1():
6876
print("connect")
6977
s.connect(addr)
7078

71-
# Send one byte to indicate a connection, and so the server can obtain our address.
72-
s.write("X")
73-
7479
# Create a DTLS context and load the certificate.
7580
ctx = tls.SSLContext(tls.PROTOCOL_DTLS_CLIENT)
7681
ctx.verify_mode = tls.CERT_REQUIRED
@@ -81,9 +86,9 @@ def instance1():
8186
s = ctx.wrap_socket(s, server_hostname="micropython.local")
8287

8388
# Transfer some data.
84-
for _ in range(4):
85-
s.send(b"client to server")
86-
print(s.recv(16))
89+
for i in range(4):
90+
s.send(b"client to server " + str(i).encode())
91+
print(s.recv(32))
8792

8893
# Close the DTLS and UDP connection.
8994
s.close()
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
--- instance0 ---
2-
incoming connection b'X'
3-
wrap socket
4-
b'client to server'
5-
b'client to server'
6-
b'client to server'
7-
b'client to server'
2+
waiting
3+
incoming connection
4+
(-27264, 'MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED')
5+
waiting
6+
incoming connection
7+
b'client to server 0'
8+
b'client to server 1'
9+
b'client to server 2'
10+
b'client to server 3'
811
--- instance1 ---
912
connect
1013
wrap socket
11-
b'server to client'
12-
b'server to client'
13-
b'server to client'
14-
b'server to client'
14+
b'server to client 0'
15+
b'server to client 1'
16+
b'server to client 2'
17+
b'server to client 3'

0 commit comments

Comments
 (0)