Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_delete_pid_exit():
pfile = create_file("test.pid", 123)
with mock.patch("os.getpid", return_value=123), mock.patch(
"atexit.register"
):
daemon = Daemon(pfile)
assert daemon.pid == 123
daemon._exit()
assert daemon.pid is None
def test_run_test():
cfile = create_config(("",))
with mock.patch("sys.argv", ["blackhole", "-t", "-c", cfile]), mock.patch(
"blackhole.config.Config.test_port", return_value=True
), mock.patch(
"blackhole.config.Config.test_pidfile", return_value=True
), pytest.raises(
SystemExit
) as exc:
run()
assert exc.value.code == 0
def test_get_version_invalid_version_split():
version_file = create_file("version.py", "__version__")
with mock.patch("os.path.join", return_value=version_file), pytest.raises(
AssertionError
) as err:
get_version()
assert str(err.value) == "Cannot extract version from __version__"
def test_test(self):
parser = parse_cmd_args(["-t"])
assert parser.test is True
parser = parse_cmd_args(["--test"])
assert parser.test is True
def test_ipv4_and_ipv6_diff_port(self):
cfile = create_config(("tls_listen=127.0.0.1:9000,:::9001",))
conf = Config(cfile).load()
assert conf.tls_listen == [
("127.0.0.1", 9000, socket.AF_INET, {}),
("::", 9001, socket.AF_INET6, {}),
]
"mode=bounce #default accept",
)
)
conf = Config(cfile).load()
assert conf.listen == [("10.0.0.1", 1025, socket.AF_INET, {})]
assert conf.tls_listen == []
cfile = create_config(
(
"#not=thisline",
"listen=10.0.0.1:1025",
"""this won't be added""",
"mode=bounce #default accept",
)
)
with pytest.raises(ConfigException):
Config(cfile).load()
def test_invalid_options():
cfile = create_config(("workers=2", "delay=10", "test=option"))
with pytest.raises(ConfigException):
Config(cfile).load()
def test_port_under_1024_no_perms(self):
cfile = create_config(("listen=127.0.0.1:1023",))
conf = Config(cfile).load()
with mock.patch(
"os.getuid", return_value=9000
) as mock_getuid, pytest.raises(ConfigException):
conf.test_port()
assert mock_getuid.called is True
assert mock_getuid.call_count == 1
def test_setgid_invalid_group(_):
cfile = create_config(("group=testgroup",))
with pytest.raises(SystemExit) as exc:
Config(cfile).load()
setgid()
assert exc.value.code == 64
def test_mode_and_delay_range_flag(self):
key = create_file("key.key")
cert = create_file("crt.crt")
settings = (
"tls_listen=:123 mode=bounce delay=15-20",
"tls_cert={}".format(cert),
"tls_key={}".format(key),
)
cfile = create_config(settings)
conf = Config(cfile).load()
assert conf.tls_listen == [
(
"",
123,
socket.AF_INET,
{"delay": ("15", "20"), "mode": "bounce"},
)