1# test that modtls produces a numerical error message when out of heap
2
3try:
4    import usocket as socket, ussl as ssl, sys
5except:
6    import socket, ssl, sys
7try:
8    from micropython import alloc_emergency_exception_buf, heap_lock, heap_unlock
9except:
10    print("SKIP")
11    raise SystemExit
12
13
14# test with heap locked to see it switch to number-only error message
15def test(addr):
16    alloc_emergency_exception_buf(256)
17    s = socket.socket()
18    s.connect(addr)
19    try:
20        s.setblocking(False)
21        s = ssl.wrap_socket(s, do_handshake=False)
22        heap_lock()
23        print("heap is locked")
24        while True:
25            ret = s.write("foo")
26            if ret:
27                break
28        heap_unlock()
29        print("wrap: no exception")
30    except OSError as e:
31        heap_unlock()
32        # mbedtls produces "-29184"
33        # axtls produces "RECORD_OVERFLOW"
34        ok = "-29184" in str(e) or "RECORD_OVERFLOW" in str(e)
35        print("wrap:", ok)
36        if not ok:
37            print("got exception:", e)
38    s.close()
39
40
41if __name__ == "__main__":
42    # connect to plain HTTP port, oops!
43    addr = socket.getaddrinfo("micropython.org", 80)[0][-1]
44    test(addr)
45