81 lines
3.7 KiB
Python
81 lines
3.7 KiB
Python
import contextlib
|
|
import io
|
|
import importlib.util
|
|
import json
|
|
import runpy
|
|
import shutil
|
|
import tempfile
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
from pathlib import Path
|
|
|
|
|
|
TASK = Path(__file__).resolve().parents[1] / "tasks" / "frame-decoder"
|
|
SPEC = importlib.util.spec_from_file_location("frame_verifier", TASK / "verify.py")
|
|
VERIFIER = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(VERIFIER)
|
|
|
|
|
|
@unittest.skipUnless(shutil.which("cc"), "C compiler required")
|
|
class FrameDecoderTests(unittest.TestCase):
|
|
def test_gold_passes_actual_stream_cases(self):
|
|
result = VERIFIER.verify(TASK / "gold")
|
|
self.assertEqual(result, {"status": "PASS", "checks": 87})
|
|
|
|
def test_wrong_implementations_are_rejected(self):
|
|
for fixture in ("template", "noop", "cheat"):
|
|
with self.subTest(fixture=fixture):
|
|
result = VERIFIER.verify(TASK / fixture)
|
|
self.assertEqual(result["status"], "FAIL")
|
|
self.assertGreater(result["checks"], 0)
|
|
|
|
def test_private_grader_requires_linux_root_supervisor(self):
|
|
with patch.dict(VERIFIER.os.environ, {"ROLLOUT_GRADE_ISOLATION": "uid65534"}):
|
|
with patch.object(VERIFIER.os, "geteuid", return_value=1000):
|
|
with self.assertRaises(VERIFIER.UnsupportedEnvironment):
|
|
VERIFIER.verify(TASK / "gold")
|
|
|
|
def test_child_drops_groups_before_uid(self):
|
|
operations = []
|
|
with patch.object(VERIFIER.os, "setgroups", side_effect=lambda groups: operations.append(("groups", groups))):
|
|
with patch.object(VERIFIER.os, "setgid", side_effect=lambda gid: operations.append(("gid", gid))):
|
|
with patch.object(VERIFIER.os, "setuid", side_effect=lambda uid: operations.append(("uid", uid))):
|
|
VERIFIER.candidate_user()
|
|
self.assertEqual(operations, [("groups", []), ("gid", 65534), ("uid", 65534)])
|
|
|
|
def test_build_permissions_work_without_fowner_capability(self):
|
|
owned = [True]
|
|
|
|
def chmod_without_fowner(path, mode):
|
|
if not owned[0]:
|
|
raise PermissionError("chmod requires ownership or CAP_FOWNER")
|
|
|
|
with patch.object(VERIFIER, "isolation", return_value=lambda: None), \
|
|
patch.object(VERIFIER.os, "chmod", side_effect=chmod_without_fowner), \
|
|
patch.object(VERIFIER.os, "chown", side_effect=lambda *args: owned.__setitem__(0, False)), \
|
|
patch.object(VERIFIER.subprocess, "run", return_value=SimpleNamespace(returncode=1, stderr="compile fixture")):
|
|
result = VERIFIER.verify(TASK / "gold")
|
|
self.assertEqual(result["reason"], "compile")
|
|
|
|
def test_only_old_example_lengths_is_not_a_valid_decoder(self):
|
|
source = (TASK / "gold" / "decoder.c").read_text().replace(
|
|
"if (byte > 16)", "if (byte > 16 || (byte != 0 && byte != 1 && byte != 2 && byte != 16))")
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
candidate = Path(directory)
|
|
(candidate / "decoder.c").write_text(source)
|
|
self.assertEqual(VERIFIER.verify(candidate)["status"], "FAIL")
|
|
|
|
def test_missing_compiler_is_unverified(self):
|
|
with patch("subprocess.run", side_effect=FileNotFoundError("cc")):
|
|
with patch("sys.argv", [str(TASK / "verify.py"), str(TASK / "gold")]):
|
|
with contextlib.redirect_stdout(io.StringIO()) as output:
|
|
with self.assertRaises(SystemExit) as error:
|
|
runpy.run_path(str(TASK / "verify.py"), run_name="__main__")
|
|
self.assertEqual(error.exception.code, 2)
|
|
self.assertEqual(json.loads(output.getvalue())["status"], "UNVERIFIED")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|