#!/usr/bin/python3
#
# This test suite runner runs some integration tests for uwsgi, that is
# each test launches a test server with a specific configuration and
# verifies (usually using a HTTP request) that this test server behaves as
# expected.
#
# buildconf/integration-tests.ini holds the build configuration for this
# to run fine.


import gzip
import os
import requests
import signal
import socket
import subprocess
import sys
import time
import unittest


TESTS_DIR = os.path.dirname(__file__)
UWSGI_BINARY = os.getenv("UWSGI_BINARY", os.path.join(TESTS_DIR, "..", "uwsgi"))
UWSGI_PLUGINS = os.getenv("UWSGI_PLUGINS_TEST", "all").split(" ")
UWSGI_ADDR = "127.0.0.1"
UWSGI_PORT = 8000
UWSGI_HTTP = f"{UWSGI_ADDR}:{UWSGI_PORT}"


def plugins_available(plugins):
    available = False
    if "all" in UWSGI_PLUGINS:
        available = True
    else:
        available = all([plugin in UWSGI_PLUGINS for plugin in plugins])
    return available, f"{plugins} plugins not available but required for this test case"


class UwsgiTest(unittest.TestCase):

    def start_server(self, args):
        self.testserver = subprocess.Popen(
            [UWSGI_BINARY] + args,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            text=True,
        )

    def uwsgi_ready(self):
        try:
            s = socket.socket()
            s.connect(
                (
                    UWSGI_ADDR,
                    UWSGI_PORT,
                )
            )
        except socket.error:
            return False
        else:
            return True
        finally:
            s.close()

    def start_listen_server(self, args):
        self.start_server(["--http-socket", UWSGI_HTTP] + args)

        # ensure server is ready
        retries = 10
        while not self.uwsgi_ready() and retries > 0:
            time.sleep(0.1)
            retries = retries - 1
            if retries == 0:
                raise RuntimeError("uwsgi test server is not available")

    def tearDown(self):
        if hasattr(self._outcome, "errors"):
            # Python 3.4 - 3.10  (These two methods have no side effects)
            result = self.defaultTestResult()
            self._feedErrorsToResult(result, self._outcome.errors)
        else:
            # Python 3.11+
            result = self._outcome.result
        ok = not (result.errors + result.failures)

        if hasattr(self, "testserver"):
            self.testserver.send_signal(signal.SIGTERM)
            if not ok:
                print(self.testserver.stdout.read(), file=sys.stderr)

            self.testserver.wait()
            self.testserver.stdout.close()

    def assert_GET_body(self, url_path, body_expected, session=None):
        if session is None:
            session = requests

        with session.get(f"http://{UWSGI_HTTP}{url_path}") as r:
            self.assertEqual(r.text, body_expected)

    def test_static_expires(self):
        self.start_listen_server(
            [
                "--plugin",
                "notfound",
                os.path.join(TESTS_DIR, "static", "config.ini"),
            ]
        )

        with requests.get(f"http://{UWSGI_HTTP}/foobar/config.ini") as r:
            self.assertTrue("Expires" in r.headers)

    @unittest.skipUnless(*plugins_available(["python3"]))
    def test_mountpoints(self):
        self.start_listen_server(
            [
                "--plugin",
                "python3",
                os.path.join(
                    TESTS_DIR,
                    "python",
                    "manage_script_name",
                    "manage_script_name_test.ini",
                ),
            ]
        )

        mps = {"/foo", "/foobis/", "/footris/"}

        for mp in mps:
            # Requests to /foo should kick-in the managed script name.
            self.assert_GET_body(mp, mp)

            ends = mp.endswith("/")

            # And equally requests to /foo/
            self.assert_GET_body(f"{mp}/" if not ends else f"{mp}"[:-1], mp)

            # Or correct requests (/foo/resource)
            self.assert_GET_body(f"{mp}/resource" if not ends else f"{mp}resource", mp)

        mps = {
            "/fooanything",
            "/foobisis/",
            "/foofighters",
        }

        for mp in mps:
            self.assert_GET_body(mp, "")

    @unittest.skipUnless(*plugins_available(["python3"]))
    def test_python3_helloworld(self):
        self.start_listen_server(
            [
                "--plugin",
                "python3",
                "--wsgi-file",
                os.path.join(TESTS_DIR, "python", "helloapp.py"),
            ]
        )

        self.assert_GET_body("/", "Hello World")

    GZIP_PAYLOAD = b"0123456789abcdef" * 4096

    def start_gzip_server(self, config="config.ini", extra_args=()):
        # the request log would fill the stdout pipe long before tearDown() reads it
        self.start_listen_server(
            [
                "--plugin",
                "python",
                "--disable-logging",
                *extra_args,
                os.path.join(TESTS_DIR, "python", "gzip", config),
            ]
        )

    def get_gzip(self, url_path):
        # the body as sent: requests would silently decode even a truncated gzip
        # stream
        with requests.get(
            f"http://{UWSGI_HTTP}{url_path}",
            headers={"Accept-Encoding": "gzip"},
            stream=True,
            timeout=10,
        ) as r:
            return (
                r.status_code,
                r.headers.get("Content-Encoding"),
                r.raw.read(decode_content=False),
            )

    def _server_rss_kib(self):
        return int(
            subprocess.check_output(
                ["ps", "-o", "rss=", "-p", str(self.testserver.pid)]
            )
        )

    @unittest.skipUnless(*plugins_available(["python"]))
    def test_gzip_transformation_wire(self):
        self.start_gzip_server()

        status, encoding, body = self.get_gzip("/body")
        self.assertEqual((status, encoding), (200, "gzip"))
        self.assertEqual(gzip.decompress(body), self.GZIP_PAYLOAD)
        self.assertEqual(self.get_gzip("/no-body"), (204, None, b""))
        self.assertEqual(self.get_gzip("/empty-chunk"), (200, None, b""))
        # the empty chunk sends the headers, so the data behind it cannot be labelled
        # any more
        self.assertEqual(
            self.get_gzip("/leading-empty-chunk"), (200, None, self.GZIP_PAYLOAD)
        )

    @unittest.skipUnless(*plugins_available(["python"]))
    def test_gzip_transformation_headers_sent_early(self):
        # --start_response-nodelay sends the headers before the first chunk; the
        # gzip-only route keeps chunked: (which has the same problem) out of the way
        self.start_gzip_server("gzip_only.ini", ["--start_response-nodelay"])

        self.assertEqual(self.get_gzip("/body"), (200, None, self.GZIP_PAYLOAD))

    @unittest.skipUnless(*plugins_available(["python"]))
    def test_gzip_transformation_announced_early(self):
        # ... unless they already announce gzip
        self.start_gzip_server(
            "gzip_only.ini",
            ["--start_response-nodelay", "--add-header", "Content-Encoding: gzip"],
        )

        status, encoding, body = self.get_gzip("/body")
        self.assertEqual((status, encoding), (200, "gzip"))
        self.assertEqual(gzip.decompress(body), self.GZIP_PAYLOAD)

    @unittest.skipUnless(*plugins_available(["python"]))
    def test_gzip_transformation_other_coding_early(self):
        # a coding that merely contains "gzip" does not count
        self.start_gzip_server(
            "gzip_only.ini",
            ["--start_response-nodelay", "--add-header", "Content-Encoding: notgzip"],
        )

        self.assertEqual(self.get_gzip("/body"), (200, "notgzip", self.GZIP_PAYLOAD))

    @unittest.skipUnless(*plugins_available(["python"]))
    def test_gzip_transformation_memory(self):
        # the gzip route used to allocate the deflate state when the headers were
        # written and free it only in the final transformation, which does not run
        # when the application yields no chunk at all (#1749)
        self.start_gzip_server()

        for url_path in ("/no-body", "/empty-chunk", "/leading-empty-chunk", "/body"):
            for _ in range(50):
                self.get_gzip(url_path)
            rss_before = self._server_rss_kib()
            for _ in range(1000):
                self.get_gzip(url_path)
            growth = self._server_rss_kib() - rss_before
            # 1000 leaked deflate states (about 400 KB each) would add far more
            self.assertLess(
                growth,
                32 * 1024,
                f"{url_path}: rss grew by {growth} KiB over 1000 responses",
            )

    @unittest.skipUnless(*plugins_available(["pypy3"]))
    def test_pypy3_helloworld(self):
        self.start_listen_server(
            [
                os.path.join(TESTS_DIR, "pypy", "config.ini"),
            ]
        )

        self.assert_GET_body("/", "Hello World")

    @unittest.skipUnless(*plugins_available(["php"]))
    def test_php_session(self):
        self.start_listen_server(
            [
                os.path.join(TESTS_DIR, "php", "config.ini"),
            ]
        )

        self.assert_GET_body("/test.php", "PASS\n")

        s = requests.Session()
        for i in range(1, 3):
            self.assert_GET_body("/testvar.php", f"t={i}\n", session=s)

    @unittest.skipUnless(*plugins_available(["jvm"]))
    def test_jvm_hellworld(self):
        classpath = ":".join(
            [
                "/usr/share/java/uwsgi.jar",
                os.path.join(TESTS_DIR, "java"),
                os.path.join(TESTS_DIR, "..", "plugins", "jvm"),
            ]
        )

        subprocess.call(
            [
                "javac",
                "-classpath",
                classpath,
                os.path.join(TESTS_DIR, "java", "rpc.java"),
            ]
        )

        self.start_listen_server(
            [
                "--need-app=0",
                "--plugins",
                "0:jvm,jwsgi",
                "--jvm-classpath",
                classpath,
                "--jwsgi",
                "rpc:application",
            ]
        )

        self.assert_GET_body("/", "<h1>null</h1>")

    @unittest.skipUnless(*plugins_available(["psgi"]))
    def test_psgi_helloworld(self):
        self.start_listen_server(
            [
                "--plugins",
                "psgi",
                "--psgi",
                os.path.join(TESTS_DIR, "perl", "test_hello.psgi"),
            ]
        )

        self.assert_GET_body("/", "Hello, world!")

    @unittest.skipUnless(*plugins_available(["cgi"]))
    def test_cgi_helloworld(self):
        self.start_listen_server(
            [
                "--need-app=0",
                "--plugins",
                "0:cgi",
                "--cgi",
                os.path.join(TESTS_DIR, "cgi", "hello.cgi"),
            ]
        )

        self.assert_GET_body(
            "/foobar/say_hello", "Hello world!\nPATH_INFO=/foobar/say_hello\n"
        )

    @unittest.skipUnless(*plugins_available(["rack"]))
    def test_rack_helloworld(self):
        self.start_listen_server(
            [
                "--plugins",
                "0:rack",
                "--rack",
                os.path.join(TESTS_DIR, "rack", "app.ru"),
            ]
        )

        with requests.get(f"http://{UWSGI_HTTP}/") as r:
            self.assertEqual(r.text, "Hello")
            self.assertEqual(r.headers["Content-type"], "text/plain")
            self.assertEqual(
                r.headers["Set-Cookie"],
                "country=UK; path=/, autologin=yes; path=/; secure",
            )


if __name__ == "__main__":
    unittest.main(verbosity=2)
