aboutsummaryrefslogtreecommitdiff
path: root/tests/net_inet/ssl_errors.py
blob: fd281b1c499c3e388bf0b14b9fc1af2be4ee71ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# test that socket.connect() on a non-blocking socket raises EINPROGRESS
# and that an immediate write/send/read/recv does the right thing

import sys

try:
    import uerrno as errno, usocket as socket, ussl as ssl
except:
    import errno, socket, ssl


def test(addr, hostname, block=True):
    print("---", hostname or addr)
    s = socket.socket()
    s.setblocking(block)
    try:
        s.connect(addr)
        print("connected")
    except OSError as e:
        if e.args[0] != errno.EINPROGRESS:
            raise
        print("EINPROGRESS")

    try:
        if sys.implementation.name == "micropython":
            s = ssl.wrap_socket(s, do_handshake=block)
        else:
            s = ssl.wrap_socket(s, do_handshake_on_connect=block)
        print("wrap: True")
    except OSError:
        print("wrap: error")

    if not block:
        try:
            while s.write(b"0") is None:
                pass
        except (ValueError, OSError):  # CPython raises ValueError, MicroPython raises OSError
            print("write: error")
    s.close()


if __name__ == "__main__":
    # connect to plain HTTP port, oops!
    addr = socket.getaddrinfo("micropython.org", 80)[0][-1]
    test(addr, None)
    # connect to plain HTTP port, oops!
    addr = socket.getaddrinfo("micropython.org", 80)[0][-1]
    test(addr, None, False)
    # connect to server with self-signed cert, oops!
    addr = socket.getaddrinfo("test.mosquitto.org", 8883)[0][-1]
    test(addr, "test.mosquitto.org")