summaryrefslogtreecommitdiff
path: root/debian/tests/python/ucspi_test/__init__.py
blob: 6aac8a2046b85e18d29027b0add21715404f4c4d (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
"""Run a couple of UCSPI client and server tests."""

from __future__ import annotations

import abc
import dataclasses
import pathlib
import shlex
import subprocess  # noqa: S404
import sys
import typing


if typing.TYPE_CHECKING:
    import socket
    from collections.abc import Callable
    from typing import Any, Final


VERSION: Final = "0.2.0"

MSG_RESP_HELLO: Final = "a01 hello\n"
MSG_RESP_BYE: Final = "a02 bye\n"


@dataclasses.dataclass(frozen=True)
class Config:
    """Runtime configuration for the UCSPI test runner."""

    bindir: pathlib.Path
    proto: str
    utf8_env: dict[str, str]


@dataclasses.dataclass
class RunnerError(Exception):
    """An error that occurred while preparing for or running the tests."""


@dataclasses.dataclass
class HandlerMismatchError(RunnerError):
    """The test framework attempted to add a different handler."""

    proto: str
    current: type[Runner]
    runner: type[Runner]

    def __str__(self) -> str:
        """Provide a human-readable error message."""
        return (
            f"Handler mismatch for the {self.proto!r} protocol: "
            "had {self.current!r}, now {self.runner!r}"
        )


@dataclasses.dataclass
class SocketAddressLengthError(RunnerError):
    """An address with an unexpected length was specified."""

    proto: str
    addr: Any

    def __str__(self) -> str:
        """Provide a human-readable error message."""
        return f"{self.proto}.get_connected_socket(): unexpected address length for {self.addr!r}"


class Runner(abc.ABC):
    """A helper class for running tests for a single UCSPI protocol."""

    _cfg: Config
    _proto: str

    def __init__(self, cfg: Config, proto: str) -> None:
        """Store the configuration object."""
        self._cfg = cfg
        self._proto = proto

    @property
    def cfg(self) -> Config:
        """Get the configuration for this runner."""
        return self._cfg

    @property
    def proto(self) -> str:
        """Get the name of the UCSPI protocol to test."""
        return self._proto

    @property
    def supports_remote_info(self) -> bool:
        """Whether the client and server support the -R command-line option."""
        return True

    @property
    def logs_to_stdout(self) -> bool:
        """Whether verbose output goes to the standard output stream (argh)."""
        return False

    @abc.abstractmethod
    def find_listening_address(self) -> list[str]:
        """Find an available protocol-specific address to listen on."""
        raise NotImplementedError

    @abc.abstractmethod
    def get_listening_socket(self, addr: list[str]) -> socket.socket:
        """Start listening on the specified address."""
        raise NotImplementedError(repr(addr))

    @abc.abstractmethod
    def get_connected_socket(self, addr: list[str]) -> socket.socket:
        """Connect to the specified address."""
        raise NotImplementedError(repr(addr))

    @abc.abstractmethod
    def format_local_addr(self, addr: list[str]) -> str:
        """Format an address returned by accept(), etc."""
        raise NotImplementedError(repr(addr))

    @abc.abstractmethod
    def format_remote_addr(self, addr: Any) -> str:  # noqa: ANN401
        """Format an address returned by accept(), etc."""
        raise NotImplementedError(repr(addr))

    def __repr__(self) -> str:
        """Provide a Python-esque representation of the helper."""
        return f"{type(self).__name__}(cfg={self._cfg!r}, proto={self._proto!r})"


HANDLERS: dict[str, type[Runner]] = {}


def with_listener(
    runner: Runner,
    addr: list[str],
    tag: str,
    callback: Callable[[socket.socket], None],
) -> None:
    """Get a listener socket from the runner, invoke the callback."""
    proto: Final = runner.proto
    saddr: Final = runner.format_local_addr(addr)
    print(f"- {tag}: setting up a listening socket at {saddr}")
    try:
        listener: Final = runner.get_listening_socket(addr)
    except RunnerError as err:
        sys.exit(f"{tag}: could not get a {proto} listening socket: {err}")

    try:
        callback(listener)
    finally:
        try:
            listener.close()
        except OSError as err:
            print(f"{tag}: could not close the listening socket at {saddr}: {err}", file=sys.stderr)


def with_connect(
    runner: Runner,
    addr: list[str],
    tag: str,
    callback: Callable[[socket.socket], None],
) -> None:
    """Ask the runner to connect to the specified address, invoke the callback."""
    proto: Final = runner.proto
    saddr: Final = runner.format_local_addr(addr)
    print(f"- {tag}: setting up a socket connected to {saddr}")
    try:
        conn: Final = runner.get_connected_socket(addr)
    except RunnerError as err:
        sys.exit(f"{tag}: could not get a {proto} connected socket: {err}")

    try:
        callback(conn)
    finally:
        try:
            conn.close()
        except OSError as err:
            print(f"{tag}: could not close the socket connected to {saddr}: {err}", file=sys.stderr)


def with_child(
    runner: Runner,
    tag: str,
    cmd: list[str],
    callback: Callable[[subprocess.Popen[str]], None],
    stderr: int | None = None,
) -> None:
    """Spawn a child process, invoke the callback."""
    print(f"- {tag}: starting {shlex.join(cmd)}")
    try:
        with subprocess.Popen(
            cmd,  # noqa: S603
            bufsize=0,
            encoding="UTF-8",
            env=runner.cfg.utf8_env,
            stdout=subprocess.PIPE,
            stderr=stderr,
        ) as child:
            try:
                callback(child)
            finally:
                if child.poll() is None:
                    ptag: Final = f"the {child.pid} ({cmd[0]}) process"
                    print(f"- {tag}: {ptag} is still active, killing it")
                    try:
                        child.kill()
                    except OSError as err:
                        print(
                            f"{tag}: could not send a kill signal to {ptag}: {err}",
                            file=sys.stderr,
                        )
                    print(f"- {tag}: waiting for {ptag} to go away")
                    try:
                        child.wait()
                    except OSError as err:
                        print(f"{tag}: could not wait for {ptag} to end: {err}", file=sys.stderr)
    except (OSError, subprocess.CalledProcessError) as err:
        sys.exit(f"Could not spawn {shlex.join(cmd)}: {err}")


def test_local_spew(runner: Runner, addr: list[str], tag: str, cmd: list[str]) -> None:
    """Test a client program against our listening socket, unidirectional transfer."""

    def run(listener: socket.socket, catproc: subprocess.Popen[str]) -> None:  # noqa: PLR0912
        """Run the spew test itself."""
        saddr: Final = runner.format_local_addr(addr)
        print(f"- started the client as pid {catproc.pid}")
        print(f"- waiting for a connection at {saddr}")
        try:
            conn, rem_addr = listener.accept()
        except OSError as err:
            sys.exit(f"Could not accept an incoming connection at {saddr}: {err}")
        print(
            f"- accepted a connection at fd {conn.fileno()} from "
            f"{runner.format_remote_addr(rem_addr)}",
        )

        msg: Final = MSG_RESP_HELLO + MSG_RESP_BYE
        try:
            conn.sendall(msg.encode("UTF-8"))
        except OSError as err:
            sys.exit(f"Could not send a message on the {conn} connection at {saddr}: {err}")

        print("- closing the incoming connection")
        try:
            conn.close()
        except OSError as err:
            sys.exit(f"Could not close the {conn} connection at {saddr}: {err}")

        try:
            output, _ = catproc.communicate()
        except OSError as err:
            sys.exit(f"Could not read the output of the {cmd[0]} process: {err}")
        if not isinstance(output, str):
            raise TypeError(repr(output))

        try:
            res: Final = catproc.wait()
        except OSError as err:
            sys.exit(f"Could not wait for the {cmd[0]} process to end: {err}")
        print(f"- client exit code: {res}; output: {output!r}")
        if res != 0:
            sys.exit(f"The {cmd[0]} program exited with non-zero code {res}")

        if output != msg:
            sys.exit(f"Expected {msg!r} as {cmd[0]} output, got {catproc.stdout!r}")

    with_listener(
        runner,
        addr,
        tag,
        lambda listener: with_child(runner, tag, cmd, lambda catproc: run(listener, catproc)),
    )
    print(f"- {cmd[0]} seems fine")


def test_local_cat(runner: Runner, addr: list[str]) -> None:
    """Test the {proto}cat program."""
    proto: Final = runner.proto
    print(f"\n=== Testing {proto}cat")
    if runner.cfg.bindir != pathlib.Path("/usr/bin"):
        print(
            f"- test skipped, {proto}cat will probably not find {runner.cfg.bindir}/{proto}client",
        )
        return

    catpath: Final = runner.cfg.bindir / f"{proto}cat"
    print(f"- will spawn {proto}cat at {catpath} in a while")
    test_local_spew(runner, addr, "test_local_cat", [str(catpath), *addr])


def remote_opt(runner: Runner) -> list[str]:
    """Add the -R command-line option if the client and server support it."""
    return ["-R"] if runner.supports_remote_info else []


def test_local_client_spew(runner: Runner, addr: list[str]) -> None:
    """Test {proto}client against our own listening socket."""
    proto: Final = runner.proto
    print(f"\n=== Testing {proto}client against our own listening socket")

    clipath: Final = runner.cfg.bindir / f"{proto}client"
    print(f"- will spawn {proto}client at {clipath} in a while")
    test_local_spew(
        runner,
        addr,
        "test_local_client_spew",
        [str(clipath), *remote_opt(runner), *addr, "sh", "-c", "set -e; exec <&6; exec cat"],
    )


def test_server_local(runner: Runner, addr: list[str]) -> None:  # noqa: C901,PLR0915
    """Test {proto}server against our own client socket."""

    def run(srvproc: subprocess.Popen[str], conn: socket.socket) -> None:  # noqa: PLR0912
        """Run the test itself."""
        try:
            rem_addr: Final = conn.getpeername()
        except OSError as err:
            sys.exit(f"getpeername() failed for {conn!r}: {err}")
        raddr: Final = runner.format_remote_addr(rem_addr)
        print(f"- got a connection at fd {conn.fileno()} to {raddr}")

        print("- reading all the data we can")
        data = b""
        while True:
            try:
                chunk = conn.recv(4096)
            except OSError as err:
                sys.exit(f"Could not read from the {conn!r} socket: {err}")
            print(f"- read {len(chunk)} bytes from the socket")
            if not chunk:
                break
            data += chunk
        print(f"- read a total of {len(data)} bytes from the socket")

        try:
            output: Final = data.decode("UTF-8")
        except ValueError as err:
            sys.exit(f"Could not decode {data!r} as valid UTF-8: {err}")
        if output != message:
            sys.exit(f"Expected {message!r}, got {output!r}")

        if srvproc.poll() is not None:
            sys.exit(
                f"Did not expect the {proto}server process to have exited: "
                f"code {srvproc.returncode}",
            )
        print(f"- sending a SIGTERM signal to the {proto}server process")
        try:
            srvproc.terminate()
        except OSError as err:
            sys.exit(
                f"Could not send a SIGTERM signal to the {proto}server process at "
                f"{srvproc.pid}: {err}",
            )
        print(f"- waiting for the {proto}server process to exit")
        res: Final = srvproc.wait()
        if res != 0:
            sys.exit(f"The {proto}server process exited with a non-zero code {res}")

    def wait_and_connect(srvproc: subprocess.Popen[str]) -> None:
        """Wait for the "ready" message from tcpserver."""
        stream: Final = srvproc.stdout if runner.logs_to_stdout else srvproc.stderr
        assert stream is not None, repr(srvproc)  # noqa: S101  # mypy needs this
        print(f"- awaiting the first 'status' line from {proto}server")
        line: Final = stream.readline()
        if "server: status: 0/" not in line:
            sys.exit(
                f"Unexpected first line from {proto}server: expected 'status: 0/N', got {line!r}",
            )
        with_connect(runner, addr, "test_server_local", lambda conn: run(srvproc, conn))

    proto: Final = runner.proto
    print(f"\n=== Testing {proto}server against our own listening socket")

    srvpath: Final = runner.cfg.bindir / f"{proto}server"
    print(f"- will spawn {proto}server at {srvpath} in a while")
    message: Final = MSG_RESP_HELLO + MSG_RESP_BYE
    with_child(
        runner,
        "test_server_local",
        [
            "stdbuf",
            "-oL",
            "-eL",
            "--",
            str(srvpath),
            "-v",
            *remote_opt(runner),
            *addr,
            "printf",
            "--",
            message.replace("\n", "\\n"),
        ],
        wait_and_connect,
        stderr=None if runner.logs_to_stdout else subprocess.PIPE,
    )

    print(f"- test_server_local: {srvpath} seems fine")


def test_server_client_spew(runner: Runner, addr: list[str]) -> None:  # noqa: C901
    """Test {proto}server against {proto}client, unidirectional data transfer."""

    def run(srvproc: subprocess.Popen[str], cliproc: subprocess.Popen[str]) -> None:
        """Read the data received by the client."""
        try:
            output, _ = cliproc.communicate()
        except OSError as err:
            sys.exit(f"Could not read the output of the {proto}client process: {err}")
        res_cli: Final = cliproc.poll()
        print(f"- client exit code {res_cli}; output {output!r}")
        if res_cli is None:
            sys.exit(f"Expected the {proto}client process to be done by now")
        if res_cli != 0:
            sys.exit(f"The {proto}client process exited with a non-zero code {res_cli}")
        if output != message:
            sys.exit(f"Expected {message!r}, got {output!r}")

        if srvproc.poll() is not None:
            sys.exit(
                f"Did not expect the {proto}server process to have exited: "
                f"code {srvproc.returncode}",
            )
        print(f"- sending a SIGTERM signal to the {proto}server process")
        try:
            srvproc.terminate()
        except OSError as err:
            sys.exit(
                f"Could not send a SIGTERM signal to the {proto}server process at "
                f"{srvproc.pid}: {err}",
            )
        print(f"- waiting for the {proto}server process to exit")
        res_srv: Final = srvproc.wait()
        if res_srv != 0:
            sys.exit(f"The {proto}server process exited with a non-zero code {res_srv}")

    def wait_and_connect(srvproc: subprocess.Popen[str]) -> None:
        """Wait for the "ready" message from tcpserver."""
        stream: Final = srvproc.stdout if runner.logs_to_stdout else srvproc.stderr
        assert stream is not None, repr(srvproc)  # noqa: S101  # mypy needs this
        print(f"- awaiting the first 'status' line from {proto}server")
        line: Final = stream.readline()
        if "server: status: 0/" not in line:
            sys.exit(
                f"Unexpected first line from {proto}server: expected 'status: 0/N', got {line!r}",
            )
        with_child(
            runner,
            "test_server_client_spew",
            [str(clipath), *remote_opt(runner), *addr, "sh", "-c", "set -e; exec <&6; exec cat"],
            lambda cliproc: run(srvproc, cliproc),
        )

    proto: Final = runner.proto
    print(f"\n=== Testing {proto}server against {proto}client")

    srvpath: Final = runner.cfg.bindir / f"{proto}server"
    print(f"- will spawn {proto}server at {srvpath} in a while")
    clipath: Final = runner.cfg.bindir / f"{proto}client"
    print(f"- will spawn {proto}client at {clipath} in a while")
    message: Final = MSG_RESP_HELLO + MSG_RESP_BYE
    with_child(
        runner,
        "test_server_client_spew",
        [
            "stdbuf",
            "-oL",
            "-eL",
            "--",
            str(srvpath),
            "-v",
            *remote_opt(runner),
            *addr,
            "printf",
            "--",
            message.replace("\n", "\\n"),
        ],
        wait_and_connect,
        stderr=None if runner.logs_to_stdout else subprocess.PIPE,
    )

    print(f"- {srvpath} seems fine")


def run_test(runner: Runner) -> None:
    """Run a couple of UCSPI tests."""
    addr: Final = runner.find_listening_address()

    test_local_cat(runner, addr)
    test_local_client_spew(runner, addr)

    test_server_local(runner, addr)
    test_server_client_spew(runner, addr)

    print(f"\n=== The tests for {runner.cfg.proto} passed")


def add_handler(proto: str, runner: type[Runner]) -> None:
    """Add a UCSPI protocol test runner."""
    current: Final = HANDLERS.get(proto)
    if current is None:
        HANDLERS[proto] = runner
    elif current != runner:
        raise HandlerMismatchError(proto, current, runner)


def run_test_handler(cfg: Config) -> None:
    """Parse command-line arguments, run the tests."""
    hprot: Final = HANDLERS.get(cfg.proto)
    if hprot is None:
        sys.exit(f"Don't know how to test the {cfg.proto!r} UCSPI protocol")

    run_test(hprot(cfg, cfg.proto))