circuitpython/tests/net_inet/getaddrinfo.py
Thorsten von Eicken ab4e197707 esp32/modsocket: Fix getaddrinfo to raise on error.
This commit fixes the behaviour of socket.getaddrinfo on the ESP32 so it
raises an OSError when the name resolution fails instead of returning a []
or a resolution for 0.0.0.0.

Tests are added (generic and ESP32-specific) to verify behaviour consistent
with CPython, modulo the different types of exceptions per MicroPython
documentation.
2020-05-09 16:43:48 +10:00

53 lines
1.3 KiB
Python

try:
import usocket as socket, sys
except:
import socket, sys
def test_non_existent():
try:
res = socket.getaddrinfo("nonexistent.example.com", 80)
print("getaddrinfo returned", res)
except OSError as e:
print("getaddrinfo raised")
def test_bogus():
try:
res = socket.getaddrinfo("hey.!!$$", 80)
print("getaddrinfo returned", res)
except OSError as e:
print("getaddrinfo raised")
except Exception as e:
print("getaddrinfo raised") # CPython raises UnicodeError!?
def test_ip_addr():
try:
res = socket.getaddrinfo("10.10.10.10", 80)
print("getaddrinfo returned resolutions")
except Exception as e:
print("getaddrinfo raised", e)
def test_0_0_0_0():
try:
res = socket.getaddrinfo("0.0.0.0", 80)
print("getaddrinfo returned resolutions")
except Exception as e:
print("getaddrinfo raised", e)
def test_valid():
try:
res = socket.getaddrinfo("micropython.org", 80)
print("getaddrinfo returned resolutions")
except Exception as e:
print("getaddrinfo raised", e)
test_funs = [n for n in dir() if n.startswith("test_")]
for f in sorted(test_funs):
print("--", f, end=": ")
eval(f + "()")