|
| 1 | +import socket, sys |
| 2 | + |
| 3 | + |
| 4 | +def check_ipv6_support(): |
| 5 | + try: |
| 6 | + socket.AF_INET6 |
| 7 | + return True |
| 8 | + except AttributeError: |
| 9 | + return False |
| 10 | + |
| 11 | + |
| 12 | +if not check_ipv6_support(): |
| 13 | + print("SKIP") |
| 14 | + raise SystemExit |
| 15 | + |
| 16 | + |
| 17 | +def test_ipv6_localhost(): |
| 18 | + try: |
| 19 | + res = socket.getaddrinfo("::1", 80, socket.AF_INET6) |
| 20 | + print("IPv6 localhost getaddrinfo returned resolutions") |
| 21 | + except Exception as e: |
| 22 | + print("IPv6 localhost getaddrinfo raised", e) |
| 23 | + |
| 24 | + |
| 25 | +def test_ipv6_wildcard(): |
| 26 | + try: |
| 27 | + res = socket.getaddrinfo("::", 80, socket.AF_INET6) |
| 28 | + print("IPv6 wildcard getaddrinfo returned resolutions") |
| 29 | + except Exception as e: |
| 30 | + print("IPv6 wildcard getaddrinfo raised", e) |
| 31 | + |
| 32 | + |
| 33 | +def test_ipv6_address(): |
| 34 | + try: |
| 35 | + res = socket.getaddrinfo("2001:db8::1", 80, socket.AF_INET6) |
| 36 | + print("IPv6 address getaddrinfo returned resolutions") |
| 37 | + except Exception as e: |
| 38 | + print("IPv6 address getaddrinfo raised", e) |
| 39 | + |
| 40 | + |
| 41 | +def test_dual_stack(): |
| 42 | + try: |
| 43 | + # Test that both IPv4 and IPv6 can be resolved |
| 44 | + res4 = socket.getaddrinfo("127.0.0.1", 80, socket.AF_INET) |
| 45 | + res6 = socket.getaddrinfo("::1", 80, socket.AF_INET6) |
| 46 | + print("Dual stack getaddrinfo returned resolutions") |
| 47 | + except Exception as e: |
| 48 | + print("Dual stack getaddrinfo raised", e) |
| 49 | + |
| 50 | + |
| 51 | +def test_family_unspec(): |
| 52 | + try: |
| 53 | + # Test unspecified family (should return both IPv4 and IPv6 if available) |
| 54 | + res = socket.getaddrinfo("localhost", 80) |
| 55 | + has_ipv4 = any(addr[0] == socket.AF_INET for addr in res) |
| 56 | + has_ipv6 = any(addr[0] == socket.AF_INET6 for addr in res) |
| 57 | + if has_ipv4 or has_ipv6: |
| 58 | + print("Unspecified family getaddrinfo returned resolutions") |
| 59 | + else: |
| 60 | + print("Unspecified family getaddrinfo returned empty") |
| 61 | + except Exception as e: |
| 62 | + print("Unspecified family getaddrinfo raised", e) |
| 63 | + |
| 64 | + |
| 65 | +def test_ipv6_domain(): |
| 66 | + try: |
| 67 | + # Try to resolve a domain name with IPv6 |
| 68 | + res = socket.getaddrinfo("google.com", 80, socket.AF_INET6) |
| 69 | + print("IPv6 domain getaddrinfo returned resolutions") |
| 70 | + except Exception as e: |
| 71 | + print("IPv6 domain getaddrinfo raised", e) |
| 72 | + |
| 73 | + |
| 74 | +test_funs = [n for n in dir() if n.startswith("test_")] |
| 75 | +for f in sorted(test_funs): |
| 76 | + print("--", f, end=": ") |
| 77 | + eval(f + "()") |
0 commit comments