summaryrefslogtreecommitdiff
path: root/tests/net_inet/resolve_on_connect.py
blob: cd8bb9a1d1d9d13fe29455a204333b3782c10153 (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
52
53
54
55
56
57
58
# Test that the socket module performs DNS resolutions on bind and connect.
# Currenty only the esp32 port does this, so the test is restricted to that port.

import sys

if sys.implementation.name == "micropython" and sys.platform != "esp32":
    print("SKIP")
    raise SystemExit

import socket


def test_bind_resolves_0_0_0_0():
    s = socket.socket()
    try:
        s.bind(("0.0.0.0", 31245))
        print("bind actually bound")
        s.close()
    except Exception as e:
        print("bind raised", e)


def test_bind_resolves_localhost():
    s = socket.socket()
    try:
        s.bind(("localhost", 31245))
        print("bind actually bound")
        s.close()
    except Exception as e:
        print("bind raised", e)


def test_connect_resolves():
    s = socket.socket()
    try:
        s.connect(("micropython.org", 80))
        print("connect actually connected")
        s.close()
    except Exception as e:
        print("connect raised", e)


def test_connect_non_existent():
    s = socket.socket()
    try:
        s.connect(("nonexistent.example.com", 80))
        print("connect actually connected")
        s.close()
    except OSError as e:
        print("connect raised OSError")
    except Exception as e:
        print("connect raised", e)


test_funs = [n for n in dir() if n.startswith("test_")]
for f in sorted(test_funs):
    print("--", f, end=": ")
    eval(f + "()")