diff options
author | Thorsten von Eicken <tve@voneicken.com> | 2020-04-28 10:58:43 -0700 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2020-05-09 16:43:48 +1000 |
commit | ab4e197707cd456499d680091fdc9daeff18c62a (patch) | |
tree | a8af94aeda01b21aa417cd1aa35f37653e79808d /tests/esp32/resolve_on_connect.py | |
parent | adb673302238bb1129ad62e7783dc650aaa27277 (diff) |
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.
Diffstat (limited to 'tests/esp32/resolve_on_connect.py')
-rw-r--r-- | tests/esp32/resolve_on_connect.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/esp32/resolve_on_connect.py b/tests/esp32/resolve_on_connect.py new file mode 100644 index 000000000..068757ab2 --- /dev/null +++ b/tests/esp32/resolve_on_connect.py @@ -0,0 +1,59 @@ +# Test that the esp32's socket module performs DNS resolutions on bind and connect +import sys + +if sys.implementation.name == "micropython" and sys.platform != "esp32": + print("SKIP") + raise SystemExit + +try: + import usocket as socket, sys +except: + import socket, sys + + +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 + "()") |