omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / x86 / test_mode_labels.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Host-only regression for the freestanding OM-2 diagnostic labels. |
| 3 | |
| 4 | Evaluate the real check-name expressions and use Suite.check to print them. |
| 5 | No acceptance predicate, compiler, caller or emulator is executed here. This |
| 6 | tests reporting and command selection, not native ABI or hardware behavior. |
| 7 | """ |
| 8 | |
| 9 | import ast |
| 10 | import contextlib |
| 11 | import inspect |
| 12 | import io |
| 13 | import os |
| 14 | import textwrap |
| 15 | import unittest |
| 16 | from unittest.mock import patch |
| 17 | |
| 18 | from suite import Suite |
| 19 | |
| 20 | |
| 21 | PREFIXES = ( |
| 22 | "freestanding x86 OM-2 FIPS 180-4 digests, bad-arg and trap leave out untouched, four threads ", |
| 23 | "freestanding x86 OM-2 fapic wrapper traps (SIGILL) on a nonzero status ", |
| 24 | "freestanding x86 OM-2 copy-back-dropped mutant leaves the pre-call fill in every digest ", |
| 25 | ) |
| 26 | |
| 27 | |
| 28 | class ModeLabelTests(unittest.TestCase): |
| 29 | @classmethod |
| 30 | def setUpClass(cls): |
| 31 | source = textwrap.dedent(inspect.getsource(Suite.freestanding_om2_x86)) |
| 32 | cls.labels = [] |
| 33 | for node in ast.walk(ast.parse(source)): |
| 34 | if (isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) |
| 35 | and isinstance(node.func.value, ast.Name) |
| 36 | and node.func.value.id == "self" and node.func.attr == "check"): |
| 37 | label = node.args[0] |
| 38 | if any(isinstance(part, ast.Constant) and isinstance(part.value, str) |
| 39 | and part.value.startswith("freestanding x86 OM-2 ") |
| 40 | for part in ast.walk(label)): |
| 41 | cls.labels.append(compile(ast.Expression(label), "<check-name>", "eval")) |
| 42 | if len(cls.labels) != len(PREFIXES): |
| 43 | raise AssertionError("expected all three freestanding OM-2 diagnostic expressions") |
| 44 | |
| 45 | def assert_labels(self, how, ok, expressions=None): |
| 46 | runner = Suite.__new__(Suite) |
| 47 | runner.how, runner.failed = how, [] |
| 48 | output = io.StringIO() |
| 49 | with contextlib.redirect_stdout(output): |
| 50 | for label in self.labels if expressions is None else expressions: |
| 51 | runner.check(eval(label, {"self": runner}), ok) |
| 52 | verdict = "PASS " if ok else "FAIL " |
| 53 | self.assertEqual(output.getvalue().splitlines(), |
| 54 | [verdict + prefix + how for prefix in PREFIXES]) |
| 55 | self.assertEqual(runner.failed, [] if ok else [prefix + how for prefix in PREFIXES]) |
| 56 | |
| 57 | def test_native_labels(self): |
| 58 | for ok in (True, False): |
| 59 | with self.subTest(ok=ok): |
| 60 | self.assert_labels("NATIVE", ok) |
| 61 | |
| 62 | def test_emulated_labels_unchanged(self): |
| 63 | for ok in (True, False): |
| 64 | with self.subTest(ok=ok): |
| 65 | self.assert_labels("EMULATED", ok) |
| 66 | |
| 67 | def test_each_old_label_mutant_is_detected(self): |
| 68 | for i, prefix in enumerate(PREFIXES): |
| 69 | with self.subTest(label=prefix): |
| 70 | mutant = list(self.labels) |
| 71 | mutant[i] = compile(repr(prefix + "EMULATED"), "<old-label-mutant>", "eval") |
| 72 | with self.assertRaises(AssertionError): |
| 73 | self.assert_labels("NATIVE", True, mutant) |
| 74 | |
| 75 | def test_guest_selection(self): |
| 76 | runner = Suite.__new__(Suite) |
| 77 | runner.qemu, runner.sysroot = ["qemu-x86_64"], "/guest-root" |
| 78 | with patch.dict(os.environ, {}, clear=True): |
| 79 | runner.native = True |
| 80 | self.assertEqual(runner.guest("caller", "trap"), ["caller", "trap"]) |
| 81 | runner.native = False |
| 82 | self.assertEqual(runner.guest("caller", "trap"), |
| 83 | ["qemu-x86_64", "-L", "/guest-root", "caller", "trap"]) |
| 84 | |
| 85 | |
| 86 | if __name__ == "__main__": |
| 87 | unittest.main(verbosity=2) |