|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""``__repr__`` output for the sessions bindings must be parseable Python. |
| 5 | +
|
| 6 | +These reprs were built with ``format!("{:?}")``, which is not a Python |
| 7 | +literal writer. Rust's ``Debug`` for ``str`` agrees with Python on the |
| 8 | +quote, the backslash and the C0 controls, but renders anything else |
| 9 | +non-printable as ``\\u{a0}`` -- and CPython wants exactly four hex digits |
| 10 | +after ``\\u``, so the literal does not parse. ``Debug`` for ``Option`` |
| 11 | +likewise emits ``Some(0)``, which is a ``NameError``. |
| 12 | +
|
| 13 | +That made a repr corruptible by its own data. ``ActionResult.stdout`` is |
| 14 | +captured process output, so a non-ASCII byte in a job's stdout is ordinary |
| 15 | +rather than adversarial, and ``PosixSessionUser`` carries a user name that |
| 16 | +arrives from outside. Both land in log lines and exception messages. |
| 17 | +
|
| 18 | +Every string field now goes through CPython's own ``repr()``, so the |
| 19 | +escaping is by construction whatever the running interpreter produces. |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import annotations |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | +from openjd._openjd_rs import ( |
| 27 | + ActionResult, |
| 28 | + ActionState, |
| 29 | + ActionStatus, |
| 30 | + PosixSessionUser, |
| 31 | +) |
| 32 | + |
| 33 | +# The characters Rust's Debug renders as \u{...}: non-printable outside |
| 34 | +# ASCII (U+00A0, U+3000, U+0085) plus non-printable ASCII (U+007F). Then |
| 35 | +# the ones Debug does escape correctly, kept as controls against a |
| 36 | +# hand-rolled replacement getting them wrong, and printable non-ASCII |
| 37 | +# that must survive verbatim. |
| 38 | +HOSTILE_STRINGS = [ |
| 39 | + "a\xa0b", |
| 40 | + "a\u3000b", |
| 41 | + "a\x85b", |
| 42 | + "a\x7fb", |
| 43 | + "a\u200bb", |
| 44 | + "a\U00100000b", |
| 45 | + 'a"b', |
| 46 | + "it's", |
| 47 | + "a\\b", |
| 48 | + "a\nb", |
| 49 | + "a\r\nb", |
| 50 | + "a\tb", |
| 51 | + "a\x00b", |
| 52 | + "café", |
| 53 | + "a\U0001f600b", |
| 54 | + "", |
| 55 | +] |
| 56 | + |
| 57 | +ALL_ACTION_STATES = [ |
| 58 | + ActionState.RUNNING, |
| 59 | + ActionState.SUCCESS, |
| 60 | + ActionState.FAILED, |
| 61 | + ActionState.CANCELED, |
| 62 | + ActionState.TIMEOUT, |
| 63 | +] |
| 64 | + |
| 65 | +EVAL_NS = { |
| 66 | + "ActionResult": ActionResult, |
| 67 | + "ActionState": ActionState, |
| 68 | + "ActionStatus": ActionStatus, |
| 69 | + "PosixSessionUser": PosixSessionUser, |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +def assert_parses(text: str) -> None: |
| 74 | + """The repr must at least be syntactically valid Python.""" |
| 75 | + compile(text, "<repr>", "eval") |
| 76 | + |
| 77 | + |
| 78 | +class TestActionResultRepr: |
| 79 | + """``ActionResult.stdout`` is captured process output.""" |
| 80 | + |
| 81 | + @pytest.mark.parametrize("stdout", HOSTILE_STRINGS) |
| 82 | + def test_repr_parses(self, stdout: str) -> None: |
| 83 | + assert_parses(repr(ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout=stdout))) |
| 84 | + |
| 85 | + @pytest.mark.parametrize("stdout", HOSTILE_STRINGS) |
| 86 | + def test_repr_embeds_cpython_repr_of_stdout(self, stdout: str) -> None: |
| 87 | + result = ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout=stdout) |
| 88 | + assert repr(result) == ( |
| 89 | + f"ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout={stdout!r})" |
| 90 | + ) |
| 91 | + |
| 92 | + @pytest.mark.parametrize("stdout", HOSTILE_STRINGS) |
| 93 | + def test_repr_round_trips(self, stdout: str) -> None: |
| 94 | + result = ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout=stdout) |
| 95 | + assert eval(repr(result), dict(EVAL_NS)) == result |
| 96 | + |
| 97 | + @pytest.mark.parametrize( |
| 98 | + "exit_code,expected", |
| 99 | + [(0, "exit_code=0"), (1, "exit_code=1"), (-9, "exit_code=-9"), (None, "exit_code=None")], |
| 100 | + ) |
| 101 | + def test_repr_renders_exit_code_as_python(self, exit_code: int | None, expected: str) -> None: |
| 102 | + # Debug would emit `Some(0)`, which is a NameError. |
| 103 | + assert expected in repr( |
| 104 | + ActionResult(state=ActionState.SUCCESS, exit_code=exit_code, stdout="") |
| 105 | + ) |
| 106 | + |
| 107 | + @pytest.mark.parametrize("state", ALL_ACTION_STATES) |
| 108 | + def test_repr_names_the_state_as_python(self, state: ActionState) -> None: |
| 109 | + result = ActionResult(state=state, exit_code=0, stdout="") |
| 110 | + assert f"state={state!r}" in repr(result) |
| 111 | + assert eval(repr(result), dict(EVAL_NS)) == result |
| 112 | + |
| 113 | + |
| 114 | +class TestActionStatusRepr: |
| 115 | + """No string field, but the same ``Option`` and enum defects.""" |
| 116 | + |
| 117 | + @pytest.mark.parametrize("exit_code", [0, 1, -9, None]) |
| 118 | + def test_repr_is_evaluable(self, exit_code: int | None) -> None: |
| 119 | + status = ActionStatus(state=ActionState.SUCCESS, exit_code=exit_code) |
| 120 | + assert_parses(repr(status)) |
| 121 | + # Evaluates without NameError; ActionStatus has no __eq__ against a |
| 122 | + # rebuilt instance's other fields, so this asserts evaluability only. |
| 123 | + eval(repr(status), dict(EVAL_NS)) |
| 124 | + |
| 125 | + def test_repr_renders_none_exit_code(self) -> None: |
| 126 | + assert repr(ActionStatus(state=ActionState.FAILED, exit_code=None)) == ( |
| 127 | + "ActionStatus(state=ActionState.FAILED, exit_code=None)" |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +class TestPosixSessionUserRepr: |
| 132 | + """``user`` and ``group`` arrive from outside.""" |
| 133 | + |
| 134 | + @pytest.mark.parametrize("value", HOSTILE_STRINGS) |
| 135 | + def test_repr_parses_for_user(self, value: str) -> None: |
| 136 | + assert_parses(repr(PosixSessionUser(user=value, group="g"))) |
| 137 | + |
| 138 | + @pytest.mark.parametrize("value", HOSTILE_STRINGS) |
| 139 | + def test_repr_matches_cpython_for_user(self, value: str) -> None: |
| 140 | + assert repr(PosixSessionUser(user=value, group="g")) == ( |
| 141 | + f"PosixSessionUser(user={value!r}, group={'g'!r})" |
| 142 | + ) |
| 143 | + |
| 144 | + @pytest.mark.parametrize("value", HOSTILE_STRINGS) |
| 145 | + def test_repr_matches_cpython_for_group(self, value: str) -> None: |
| 146 | + # `group` is a separate argument to the same writer; a fix applied |
| 147 | + # to only the first would pass every `user` case above. |
| 148 | + assert repr(PosixSessionUser(user="u", group=value)) == ( |
| 149 | + f"PosixSessionUser(user={'u'!r}, group={value!r})" |
| 150 | + ) |
| 151 | + |
| 152 | + |
| 153 | +class TestReprNegativeControls: |
| 154 | + """Text needing no escaping must pass through unaltered.""" |
| 155 | + |
| 156 | + def test_plain_action_result(self) -> None: |
| 157 | + assert repr(ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout="ok")) == ( |
| 158 | + "ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout='ok')" |
| 159 | + ) |
| 160 | + |
| 161 | + def test_plain_posix_session_user(self) -> None: |
| 162 | + assert repr(PosixSessionUser(user="alice", group="staff")) == ( |
| 163 | + "PosixSessionUser(user='alice', group='staff')" |
| 164 | + ) |
| 165 | + |
| 166 | + def test_state_enum_repr_unchanged(self) -> None: |
| 167 | + # The spelling the reprs above embed. |
| 168 | + assert repr(ActionState.SUCCESS) == "ActionState.SUCCESS" |
0 commit comments