Skip to content

Commit 195bf05

Browse files
projectgusdpgeorge
authored andcommitted
tests: Add a test for SSL socket memory leaks.
Test is for an issue reported on the micropython-lib Discord as effecting the rp2 port umqtt.simple interface when reconnecting with TLS, however it's a more generic problem. Currently this test fails on RPI_PICO_W and ESP32_GENERIC_C3 (and no doubt others). Fixes are in the subsequent commits. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent bfb1bee commit 195bf05

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/extmod/ssl_noleak.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Ensure that SSLSockets can be allocated sequentially
2+
# without running out of available memory.
3+
try:
4+
import io
5+
import tls
6+
except ImportError:
7+
print("SKIP")
8+
raise SystemExit
9+
10+
import unittest
11+
12+
13+
class TestSocket(io.IOBase):
14+
def write(self, buf):
15+
return len(buf)
16+
17+
def readinto(self, buf):
18+
return 0
19+
20+
def ioctl(self, cmd, arg):
21+
return 0
22+
23+
def setblocking(self, value):
24+
pass
25+
26+
27+
ITERS = 128
28+
29+
30+
class TLSNoLeaks(unittest.TestCase):
31+
def test_unique_context(self):
32+
for n in range(ITERS):
33+
print(n)
34+
s = TestSocket()
35+
ctx = tls.SSLContext(tls.PROTOCOL_TLS_CLIENT)
36+
ctx.verify_mode = tls.CERT_NONE
37+
s = ctx.wrap_socket(s, do_handshake_on_connect=False)
38+
39+
def test_shared_context(self):
40+
# Single SSLContext, multiple sockets
41+
ctx = tls.SSLContext(tls.PROTOCOL_TLS_CLIENT)
42+
ctx.verify_mode = tls.CERT_NONE
43+
for n in range(ITERS):
44+
print(n)
45+
s = TestSocket()
46+
s = ctx.wrap_socket(s, do_handshake_on_connect=False)
47+
48+
49+
if __name__ == "__main__":
50+
unittest.main()

0 commit comments

Comments
 (0)