smtp-test-server/tests/net/test_hosts.py
Juergen Edelbluth 08943cd8d5
Initial Commit
This commit consolidates the SMTP mock code from some of my testing projects into a re-usable form.

This package will be used later for realizing the upcoming pytest plugin.
2023-11-29 20:33:27 +01:00

84 lines
2.3 KiB
Python

"""
Test the hosts shared module
"""
import socket
import sys
import pytest
from hamcrest import equal_to
from hamcrest.core import assert_that
from smtp_test_server.net import is_bind_host_is_local
@pytest.mark.parametrize(
["bind_host", "expectation"],
[
("127.0.0.1", True),
("localhost", True),
("127.0.0.2", False),
("does-not-exist.badtld", False),
("google.com", False),
("microsoft.com", False),
("0.0.0.0", True),
],
)
def test_host_is_local(bind_host, expectation):
"""
Test if the host answer is correct
"""
assert_that(is_bind_host_is_local(bind_host), equal_to(expectation))
@pytest.mark.skipif(not socket.has_ipv6, reason="Host has no IPv6 support")
@pytest.mark.parametrize(
["bind_host", "expectation"],
[
("0:0:0:0:0:0:0:1", True),
("::0", True),
("::1", True),
("2a01:d0c1:200:0:6c38:7aff:feb0:15cb", False),
("2a01:d0c1:200:0:6c38:7aff:feb0:15fa", False),
],
)
def test_ipv6_host_is_local(bind_host, expectation): # pragma: no cover
"""
Test IPv6 addresses, when IPv6 support is available
"""
assert_that(is_bind_host_is_local(bind_host), equal_to(expectation))
@pytest.mark.skipif(socket.has_ipv6, reason="Host has IPv6 support")
@pytest.mark.parametrize(
["bind_host", "expectation"],
[
("0:0:0:0:0:0:0:1", False),
("::0", False),
("::1", False),
("2a01:d0c1:200:0:6c38:7aff:feb0:15cb", False),
("2a01:d0c1:200:0:6c38:7aff:feb0:15fa", False),
],
)
def test_ipv6_host_is_local_without_ipv6_support(bind_host, expectation): # pragma: no cover
"""
Test IPv6 addresses, when IPv6 support is not available
"""
assert_that(is_bind_host_is_local(bind_host), equal_to(expectation))
@pytest.mark.skipif(not hasattr(sys, "getwindowsversion"), reason="on windows")
def test_with_existing_host():
"""
Test own hostname
"""
assert_that(is_bind_host_is_local(socket.gethostname())) # pragma: no cover
@pytest.mark.skipif(hasattr(sys, "getwindowsversion"), reason="not on windows")
@pytest.mark.parametrize("hostname", (socket.gethostname(), socket.getfqdn()))
def test_with_existing_host_with_fqdn(hostname):
"""
Test own hostname
"""
assert_that(is_bind_host_is_local(hostname)) # pragma: no cover