1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-06-26 16:45:52 +00:00
Radicale/radicale/tests/test_server.py

244 lines
10 KiB
Python
Raw Permalink Normal View History

2021-12-08 21:45:42 +01:00
# This file is part of Radicale - CalDAV and CardDAV server
2019-06-17 04:13:25 +02:00
# Copyright © 2018-2019 Unrud <unrud@outlook.com>
2018-09-04 03:33:45 +02:00
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
"""
Test the internal server.
"""
import errno
2018-09-06 09:12:53 +02:00
import os
2018-09-04 03:33:45 +02:00
import socket
import ssl
2018-09-09 14:58:44 +02:00
import subprocess
import sys
2018-09-04 03:33:45 +02:00
import threading
import time
2019-06-17 04:13:25 +02:00
from configparser import RawConfigParser
2022-03-22 17:32:51 +01:00
from http.client import HTTPMessage
from typing import IO, Callable, Dict, Optional, Tuple, cast
2018-09-04 03:33:45 +02:00
from urllib import request
from urllib.error import HTTPError, URLError
2019-06-15 09:01:55 +02:00
import pytest
2018-09-04 03:33:45 +02:00
from radicale import config, server
from radicale.tests import BaseTest
2020-01-15 18:44:00 +01:00
from radicale.tests.helpers import configuration_to_dict, get_file_path
2018-09-04 03:33:45 +02:00
class DisabledRedirectHandler(request.HTTPRedirectHandler):
2022-03-22 17:32:51 +01:00
def redirect_request(
self, req: request.Request, fp: IO[bytes], code: int, msg: str,
headers: HTTPMessage, newurl: str) -> None:
return None
2018-09-04 03:33:45 +02:00
class TestBaseServerRequests(BaseTest):
2018-09-04 03:33:45 +02:00
"""Test the internal server."""
2021-07-26 20:56:47 +02:00
shutdown_socket: socket.socket
thread: threading.Thread
opener: request.OpenerDirector
def setup_method(self) -> None:
super().setup_method()
2018-09-04 03:33:45 +02:00
self.shutdown_socket, shutdown_socket_out = socket.socketpair()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Find available port
2018-09-06 10:50:54 +02:00
sock.bind(("127.0.0.1", 0))
self.sockfamily = socket.AF_INET
2018-09-04 03:33:45 +02:00
self.sockname = sock.getsockname()
self.configure({"server": {"hosts": "%s:%d" % self.sockname},
2021-12-10 20:54:04 +01:00
# Enable debugging for new processes
"logging": {"level": "debug"}})
2018-09-04 03:33:45 +02:00
self.thread = threading.Thread(target=server.serve, args=(
self.configuration, shutdown_socket_out))
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
self.opener = request.build_opener(
request.HTTPSHandler(context=ssl_context),
DisabledRedirectHandler)
def teardown_method(self) -> None:
2020-02-19 10:01:39 +01:00
self.shutdown_socket.close()
2018-09-06 09:12:53 +02:00
try:
self.thread.join()
except RuntimeError: # Thread never started
pass
super().teardown_method()
2018-09-04 03:33:45 +02:00
2021-07-26 20:56:47 +02:00
def request(self, method: str, path: str, data: Optional[str] = None,
2022-01-16 13:07:56 +01:00
check: Optional[int] = None, **kwargs
) -> Tuple[int, Dict[str, str], str]:
2018-09-04 03:33:45 +02:00
"""Send a request."""
2021-07-26 20:56:47 +02:00
login = kwargs.pop("login", None)
if login is not None and not isinstance(login, str):
raise TypeError("login argument must be %r, not %r" %
(str, type(login)))
if login:
raise NotImplementedError
is_alive_fn: Optional[Callable[[], bool]] = kwargs.pop(
"is_alive_fn", None)
headers: Dict[str, str] = kwargs
for k, v in headers.items():
if not isinstance(v, str):
raise TypeError("type of %r is %r, expected %r" %
(k, type(v), str))
2018-09-09 14:58:44 +02:00
if is_alive_fn is None:
is_alive_fn = self.thread.is_alive
2021-07-26 20:56:47 +02:00
encoding: str = self.configuration.get("encoding", "request")
scheme = "https" if self.configuration.get("server", "ssl") else "http"
data_bytes = None
if data:
data_bytes = data.encode(encoding)
if self.sockfamily == socket.AF_INET6:
req_host = ("[%s]" % self.sockname[0])
else:
req_host = self.sockname[0]
2018-09-04 03:33:45 +02:00
req = request.Request(
"%s://%s:%d%s" % (scheme, req_host, self.sockname[1], path),
2021-07-26 20:56:47 +02:00
data=data_bytes, headers=headers, method=method)
2018-09-04 03:33:45 +02:00
while True:
2018-09-09 14:58:44 +02:00
assert is_alive_fn()
2018-09-04 03:33:45 +02:00
try:
with self.opener.open(req) as f:
2021-07-26 20:56:47 +02:00
return f.getcode(), dict(f.info()), f.read().decode()
2018-09-04 03:33:45 +02:00
except HTTPError as e:
2022-01-16 13:07:56 +01:00
assert check is None or e.code == check, "%d != %d" % (e.code,
check)
2021-07-26 20:56:47 +02:00
return e.code, dict(e.headers), e.read().decode()
2018-09-04 03:33:45 +02:00
except URLError as e:
if not isinstance(e.reason, ConnectionRefusedError):
raise
time.sleep(0.1)
2021-07-26 20:56:47 +02:00
def test_root(self) -> None:
2018-09-04 03:33:45 +02:00
self.thread.start()
self.get("/", check=302)
2018-09-04 03:33:45 +02:00
2021-07-26 20:56:47 +02:00
def test_ssl(self) -> None:
2021-12-10 20:54:04 +01:00
self.configure({"server": {"ssl": "True",
"certificate": get_file_path("cert.pem"),
"key": get_file_path("key.pem")}})
2018-09-04 03:33:45 +02:00
self.thread.start()
self.get("/", check=302)
2018-09-06 09:12:53 +02:00
2021-07-26 20:56:47 +02:00
def test_bind_fail(self) -> None:
2020-02-20 10:55:00 +01:00
for address_family, address in [(socket.AF_INET, "::1"),
(socket.AF_INET6, "127.0.0.1")]:
try:
with socket.socket(address_family, socket.SOCK_STREAM) as sock:
if address_family == socket.AF_INET6:
# Only allow IPv6 connections to the IPv6 socket
sock.setsockopt(server.COMPAT_IPPROTO_IPV6,
socket.IPV6_V6ONLY, 1)
with pytest.raises(OSError) as exc_info:
sock.bind((address, 0))
except OSError as e:
if e.errno in (errno.EADDRNOTAVAIL, errno.EAFNOSUPPORT,
errno.EPROTONOSUPPORT):
continue
raise
2020-02-20 11:27:26 +01:00
# See ``radicale.server.serve``
assert (isinstance(exc_info.value, socket.gaierror) and
2020-04-09 22:01:55 +02:00
exc_info.value.errno in (
socket.EAI_NONAME, server.COMPAT_EAI_ADDRFAMILY,
server.COMPAT_EAI_NODATA) or
2020-02-20 11:27:26 +01:00
str(exc_info.value) == "address family mismatched" or
2020-08-18 22:43:59 +02:00
exc_info.value.errno in (
errno.EADDRNOTAVAIL, errno.EAFNOSUPPORT,
errno.EPROTONOSUPPORT))
2021-07-26 20:56:47 +02:00
def test_ipv6(self) -> None:
try:
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as sock:
# Only allow IPv6 connections to the IPv6 socket
sock.setsockopt(
server.COMPAT_IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
2018-09-08 09:24:46 +02:00
# Find available port
sock.bind(("::1", 0))
self.sockfamily = socket.AF_INET6
self.sockname = sock.getsockname()[:2]
except OSError as e:
2020-08-18 22:43:59 +02:00
if e.errno in (errno.EADDRNOTAVAIL, errno.EAFNOSUPPORT,
errno.EPROTONOSUPPORT):
pytest.skip("IPv6 not supported")
raise
2021-12-10 20:54:04 +01:00
self.configure({"server": {"hosts": "[%s]:%d" % self.sockname}})
self.thread.start()
self.get("/", check=302)
2018-09-09 14:58:44 +02:00
def test_command_line_interface(self, with_bool_options=False) -> None:
2021-12-10 20:54:04 +01:00
self.configure({"headers": {"Test-Server": "test"}})
2018-09-09 14:58:44 +02:00
config_args = []
for section in self.configuration.sections():
if section.startswith("_"):
2019-06-17 04:13:25 +02:00
continue
for option in self.configuration.options(section):
2019-06-17 04:13:25 +02:00
if option.startswith("_"):
continue
2020-01-19 18:13:05 +01:00
long_name = "--%s-%s" % (section, option.replace("_", "-"))
if with_bool_options and config.DEFAULT_CONFIG_SCHEMA.get(
section, {}).get(option, {}).get("type") == bool:
2021-07-26 20:56:47 +02:00
if not cast(bool, self.configuration.get(section, option)):
2020-01-19 18:13:05 +01:00
long_name = "--no%s" % long_name[1:]
2018-09-09 14:58:44 +02:00
config_args.append(long_name)
else:
config_args.append(long_name)
2021-07-26 20:56:47 +02:00
raw_value = self.configuration.get_raw(section, option)
assert isinstance(raw_value, str)
config_args.append(raw_value)
config_args.append("--headers-Test-Header=test")
2018-09-09 14:58:44 +02:00
p = subprocess.Popen(
[sys.executable, "-m", "radicale"] + config_args,
env={**os.environ, "PYTHONPATH": os.pathsep.join(sys.path)})
2018-09-09 14:58:44 +02:00
try:
status, headers, _ = self.request(
2022-01-16 13:07:56 +01:00
"GET", "/", check=302, is_alive_fn=lambda: p.poll() is None)
for key in self.configuration.options("headers"):
assert headers.get(key) == self.configuration.get(
"headers", key)
2018-09-09 14:58:44 +02:00
finally:
p.terminate()
p.wait()
if sys.platform != "win32":
assert p.returncode == 0
2018-09-09 14:58:44 +02:00
def test_command_line_interface_with_bool_options(self) -> None:
self.test_command_line_interface(with_bool_options=True)
2021-07-26 20:56:47 +02:00
def test_wsgi_server(self) -> None:
2018-09-09 14:58:44 +02:00
config_path = os.path.join(self.colpath, "config")
2019-06-17 04:13:25 +02:00
parser = RawConfigParser()
parser.read_dict(configuration_to_dict(self.configuration))
2018-09-09 14:58:44 +02:00
with open(config_path, "w") as f:
2019-06-17 04:13:25 +02:00
parser.write(f)
2018-09-09 14:58:44 +02:00
env = os.environ.copy()
env["PYTHONPATH"] = os.pathsep.join(sys.path)
env["RADICALE_CONFIG"] = config_path
2021-07-26 20:56:47 +02:00
raw_server_hosts = self.configuration.get_raw("server", "hosts")
assert isinstance(raw_server_hosts, str)
2018-09-09 14:58:44 +02:00
p = subprocess.Popen([
2021-07-26 20:56:47 +02:00
sys.executable, "-m", "waitress", "--listen", raw_server_hosts,
"radicale:application"], env=env)
2018-09-09 14:58:44 +02:00
try:
self.get("/", is_alive_fn=lambda: p.poll() is None, check=302)
2018-09-09 14:58:44 +02:00
finally:
p.terminate()
p.wait()