1# Test that the esp32's socket module performs DNS resolutions on bind and connect
2import sys
3
4if sys.implementation.name == "micropython" and sys.platform != "esp32":
5    print("SKIP")
6    raise SystemExit
7
8try:
9    import usocket as socket, sys
10except:
11    import socket, sys
12
13
14def test_bind_resolves_0_0_0_0():
15    s = socket.socket()
16    try:
17        s.bind(("0.0.0.0", 31245))
18        print("bind actually bound")
19        s.close()
20    except Exception as e:
21        print("bind raised", e)
22
23
24def test_bind_resolves_localhost():
25    s = socket.socket()
26    try:
27        s.bind(("localhost", 31245))
28        print("bind actually bound")
29        s.close()
30    except Exception as e:
31        print("bind raised", e)
32
33
34def test_connect_resolves():
35    s = socket.socket()
36    try:
37        s.connect(("micropython.org", 80))
38        print("connect actually connected")
39        s.close()
40    except Exception as e:
41        print("connect raised", e)
42
43
44def test_connect_non_existent():
45    s = socket.socket()
46    try:
47        s.connect(("nonexistent.example.com", 80))
48        print("connect actually connected")
49        s.close()
50    except OSError as e:
51        print("connect raised OSError")
52    except Exception as e:
53        print("connect raised", e)
54
55
56test_funs = [n for n in dir() if n.startswith("test_")]
57for f in sorted(test_funs):
58    print("--", f, end=": ")
59    eval(f + "()")
60