about summary refs log tree commit diff
path: root/test/python/server_test_case.py
blob: 7561792d84e9d4b0096844f5e886a354ed6205a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""Shared helper for tests that exercise a running genenetwork4 server over
real HTTP (Python can only reach the server this way, unlike the Rust
integration tests under tests/, which call the router in-process).
"""

import hashlib
import json
import os
import socket
import struct
import subprocess
import time
import unittest
from urllib.parse import urlparse

BINARY = os.environ.get("GENENETWORK4_BINARY", "genenetwork4")
HOST = "127.0.0.1"
PORT = os.environ.get("GENENETWORK4_PORT", "3000")


def envelope_data(body):
    """The `data` of a success response body, after checking that the
    body is the standard envelope: `meta` (state, took_ms, request_id),
    `notes` and `next` next to `data`."""
    envelope = json.loads(body)
    assert isinstance(envelope["meta"]["state"], str), envelope
    assert isinstance(envelope["meta"]["took_ms"], int), envelope
    assert isinstance(envelope["notes"], list), envelope
    assert isinstance(envelope["next"], list), envelope
    return envelope["data"]


def wait_for_port(host, port, timeout=10):
    deadline = time.time() + timeout
    while time.time() < deadline:
        try:
            with socket.create_connection((host, int(port)), timeout=0.5):
                return True
        except OSError:
            time.sleep(0.1)
    return False


def database_reachable():
    """True only when the configured MariaDB accepts the SQL_URI
    credentials. A server may be listening on the port yet still reject
    the login (the common local case), which a bare port check cannot
    see — so this performs the real mysql_native_password handshake.
    Any protocol surprise is treated as "not reachable" (skip), which is
    the safe direction: these tests must not fail where there is simply
    no usable database (e.g. the Guix build sandbox)."""
    uri = os.environ.get(
        "SQL_URI", "mysql://webqtlout:webqtlout@127.0.0.1/db_webqtl"
    )
    try:
        parsed = urlparse(uri)
        sock = socket.create_connection(
            (parsed.hostname or "127.0.0.1", parsed.port or 3306), timeout=1
        )
        with sock:
            scramble = _read_scramble(_read_packet(sock))
            if scramble is None:
                return False
            sock.sendall(_login_packet(parsed, scramble))
            # OK starts with 0x00, ERR with 0xff (access denied etc.).
            return _read_packet(sock)[4] == 0
    except (OSError, struct.error, IndexError):
        return False


def _read_packet(sock):
    header = _read_exact(sock, 4)
    length = header[0] | (header[1] << 8) | (header[2] << 16)
    return header + _read_exact(sock, length)


def _read_exact(sock, count):
    data = b""
    while len(data) < count:
        chunk = sock.recv(count - len(data))
        if not chunk:
            raise ConnectionError("connection closed mid-packet")
        data += chunk
    return data


def _read_scramble(handshake):
    """Extract the 20-byte auth plugin data from the server handshake."""
    pos = 5  # 4-byte packet header, then protocol version
    pos = handshake.index(b"\0", pos) + 1  # server version string
    pos += 4  # connection id
    scramble = handshake[pos : pos + 8]
    pos += 8 + 1  # auth data part 1, filler
    capabilities = struct.unpack_from("<H", handshake, pos)[0]
    pos += 2
    if len(handshake) <= pos:
        return scramble  # pre-4.1 server; part 1 is all we get
    pos += 1 + 2  # charset, status
    capabilities |= struct.unpack_from("<H", handshake, pos)[0] << 16
    pos += 2
    if capabilities & 0x00080000:  # CLIENT_PLUGIN_AUTH
        auth_len = handshake[pos]
        pos += 1 + 10  # auth data length, reserved
        part2_len = max(13, auth_len - 8)
    else:
        part2_len = 13
    scramble += handshake[pos : pos + part2_len]
    return scramble.rstrip(b"\0")[:20]


def _login_packet(parsed, scramble):
    """A minimal mysql_native_password login attempt."""
    password = (parsed.password or "").encode()
    stage1 = hashlib.sha1(password).digest()
    token = bytes(
        a ^ b
        for a, b in zip(
            stage1,
            hashlib.sha1(scramble + hashlib.sha1(stage1).digest()).digest(),
        )
    )
    user = (parsed.username or "").encode()
    db = (parsed.path or "/").lstrip("/").encode()
    payload = (
        struct.pack("<IIB", 0x00008200, 0, 8)  # PROTOCOL_41 | SECURE_CONN
        + b"\0" * 23
        + user
        + b"\0"
        + bytes([len(token)])
        + token
        + db
        + b"\0"
    )
    return struct.pack("<I", len(payload))[:3] + b"\x01" + payload


class ServerTestCase(unittest.TestCase):
    """Spawns the genenetwork4 binary for the duration of the test class.

    Subclasses that need the server started with extra/overridden
    environment variables (e.g. GN3_URL) can set EXTRA_ENV.
    """

    EXTRA_ENV = {}

    @classmethod
    def setUpClass(cls):
        env = dict(os.environ, GENENETWORK4_PORT=PORT, **cls.EXTRA_ENV)
        cls.proc = subprocess.Popen([BINARY], env=env)
        if not wait_for_port(HOST, PORT):
            cls.proc.terminate()
            raise RuntimeError(f"server did not start listening on {HOST}:{PORT}")

    @classmethod
    def tearDownClass(cls):
        cls.proc.terminate()
        cls.proc.wait(timeout=5)