field_ios

In-the-field app for Radroots on iOS
git clone https://radroots.dev/git/field_ios.git
Log | Files | Refs | README | LICENSE

test_legacy_upgrade_fixture.py (13543B)


      1 from __future__ import annotations
      2 
      3 import copy
      4 import io
      5 import tempfile
      6 import unittest
      7 import sys
      8 from pathlib import Path
      9 from unittest.mock import patch
     10 import tarfile
     11 
     12 SCRIPTS = Path(__file__).resolve().parent
     13 if str(SCRIPTS) not in sys.path:
     14     sys.path.insert(0, str(SCRIPTS))
     15 
     16 import legacy_upgrade_fixture_producer as producer  # noqa: E402
     17 import legacy_upgrade_fixture_admission as admission  # noqa: E402
     18 
     19 
     20 class HistoricalProducerBoundaryTests(unittest.TestCase):
     21     def test_freeze_refuses_unadmitted_changed_or_escaped_bytes_before_creating_destination(
     22         self,
     23     ):
     24         with tempfile.TemporaryDirectory() as temporary:
     25             root = Path(temporary)
     26             fixture_root = root / "test-fixtures"
     27             fixture_root.mkdir()
     28             work = root / "managed"
     29             (work / "output").mkdir(parents=True)
     30             content = b"synthetic guard unit bytes, not a historical owner fixture"
     31             (work / "output" / "guard.txt").write_bytes(content)
     32             (work / "output" / "link.txt").symlink_to(work / "output" / "guard.txt")
     33             (root / "outside").mkdir()
     34             (root / "outside" / "guard.txt").write_bytes(content)
     35             (work / "output" / "parent_link").symlink_to(
     36                 root / "outside", target_is_directory=True
     37             )
     38             destination = fixture_root / "legacy_upgrade_v1"
     39             record = {
     40                 "state": "HISTORICAL_WRITER_ADMITTED_CURRENT_READERS_PENDING",
     41                 "files": {"guard.txt": producer.digest(content)},
     42             }
     43             with patch.object(
     44                 producer, "__file__", str(root / "scripts" / "producer.py")
     45             ):
     46                 for changed in [
     47                     {**record, "state": "UNADMITTED"},
     48                     {**record, "files": {"guard.txt": "0" * 64}},
     49                     {**record, "files": {"../outside": "0" * 64}},
     50                     {**record, "files": {"link.txt": producer.digest(content)}},
     51                     {
     52                         **record,
     53                         "files": {"parent_link/guard.txt": producer.digest(content)},
     54                     },
     55                 ]:
     56                     with self.assertRaises(ValueError):
     57                         producer.freeze_output(work, changed, destination)
     58                     self.assertFalse(destination.exists())
     59                 producer.freeze_output(work, record, destination)
     60                 self.assertEqual((destination / "guard.txt").read_bytes(), content)
     61                 with self.assertRaisesRegex(ValueError, "fresh canonical"):
     62                     producer.freeze_output(work, record, destination)
     63 
     64     def test_source_root_must_be_its_exact_public_git_identity(self):
     65         with tempfile.TemporaryDirectory() as temporary:
     66             root = Path(temporary).resolve()
     67             replies = [str(root).encode(), b"https://github.com/unrelated/lib.git"]
     68             with patch.object(producer, "git", side_effect=replies):
     69                 with self.assertRaisesRegex(ValueError, "identity"):
     70                     producer.validate_root(root, "lib")
     71             with patch.object(producer, "git", return_value=str(root.parent).encode()):
     72                 with self.assertRaisesRegex(ValueError, "Git root"):
     73                     producer.validate_root(root, "lib")
     74 
     75     def test_historical_commit_must_be_exact_and_published(self):
     76         with tempfile.TemporaryDirectory() as temporary:
     77             root = Path(temporary).resolve()
     78             replies = [
     79                 str(root).encode(),
     80                 b"https://github.com/radrootslabs/lib.git",
     81                 b"0" * 40,
     82             ]
     83             with patch.object(producer, "git", side_effect=replies):
     84                 with self.assertRaisesRegex(ValueError, "unavailable"):
     85                     producer.validate_root(root, "lib")
     86             replies[-1] = producer.SOURCES["lib"][0].encode()
     87             with patch.object(producer, "git", side_effect=replies):
     88                 with patch.object(
     89                     producer.subprocess,
     90                     "run",
     91                     side_effect=RuntimeError("not published"),
     92                 ):
     93                     with self.assertRaisesRegex(RuntimeError, "published"):
     94                         producer.validate_root(root, "lib")
     95             with patch.object(
     96                 producer.subprocess, "check_output", return_value=b"record"
     97             ) as command:
     98                 producer.git(root, "rev-parse", "HEAD")
     99                 command.assert_called_once_with(
    100                     [
    101                         "git",
    102                         "--no-replace-objects",
    103                         "-C",
    104                         str(root),
    105                         "rev-parse",
    106                         "HEAD",
    107                     ]
    108                 )
    109 
    110     def test_historical_archive_refuses_traversal_links_and_devices(self):
    111         for name, kind in [
    112             ("../escaped", tarfile.REGTYPE),
    113             ("/absolute", tarfile.REGTYPE),
    114             ("link", tarfile.SYMTYPE),
    115             ("hard", tarfile.LNKTYPE),
    116             ("device", tarfile.CHRTYPE),
    117         ]:
    118             with self.subTest(name=name), tempfile.TemporaryDirectory() as temporary:
    119                 buffer = io.BytesIO()
    120                 with tarfile.open(fileobj=buffer, mode="w") as archive:
    121                     entry = tarfile.TarInfo(name)
    122                     entry.type = kind
    123                     archive.addfile(entry)
    124                 with patch.object(producer, "git", return_value=buffer.getvalue()):
    125                     with self.assertRaisesRegex(ValueError, "unsupported"):
    126                         producer.archive(Path(temporary), "lib", Path(temporary))
    127 
    128     def test_work_must_be_a_fresh_child_of_managed_output(self):
    129         with tempfile.TemporaryDirectory() as temporary:
    130             managed = Path(temporary).resolve()
    131             with patch.dict(
    132                 producer.os.environ, {"CARGO_TARGET_DIR": str(managed / "target")}
    133             ):
    134                 for destination in [managed, managed.parent / "outside"]:
    135                     with (
    136                         self.subTest(destination=destination),
    137                         self.assertRaisesRegex(ValueError, "fresh child"),
    138                     ):
    139                         producer.produce(
    140                             {}, destination, Path("unused.swift"), Path("unused.rs")
    141                         )
    142                 used = managed / "already_used"
    143                 used.mkdir()
    144                 with self.assertRaises(FileExistsError):
    145                     producer.produce({}, used, Path("unused.swift"), Path("unused.rs"))
    146 
    147 
    148 class HistoricalAdmissionBoundaryTests(unittest.TestCase):
    149     def test_pending_wal_and_nonregular_owner_refuse_before_sqlite_open(self):
    150         with tempfile.TemporaryDirectory() as temporary:
    151             root = Path(temporary)
    152             database = root / "runtime.sqlite"
    153             database.write_bytes(b"negative unopened guard input")
    154             database.with_name(database.name + "-wal").write_bytes(b"pending")
    155             with patch.object(admission.sqlite3, "connect") as connect:
    156                 with self.assertRaisesRegex(ValueError, "uncheckpointed WAL"):
    157                     admission.read_database(database)
    158                 link = root / "linked.sqlite"
    159                 link.symlink_to(database)
    160                 with self.assertRaisesRegex(ValueError, "regular closed file"):
    161                     admission.read_database(link)
    162                 with self.assertRaisesRegex(ValueError, "regular closed file"):
    163                     admission.read_database(root / "missing.sqlite")
    164                 connect.assert_not_called()
    165 
    166     def test_zero_duplicate_missing_and_skipped_case_logs_cannot_be_admitted(self):
    167         templates = {
    168             "historical-host": "Test Case '-[TeraHistoricalFixtureWriterTests testProduceHistoricalOwnerBytes]' passed (0.1 seconds).\nExecuted 1 test, with 0 failures\n",
    169             "historical-rust": "test produce_pre_refactor_owner_state ... ok\ntest result: ok. 1 passed; 0 failed; 0 ignored;\n",
    170             "historical-transfer": "Test Case '-[TeraHistoricalFixtureWriterTests testProduceHistoricalOwnerBytes]' passed (0.1 seconds).\nExecuted 1 test, with 0 failures\n",
    171         }
    172         with tempfile.TemporaryDirectory() as temporary:
    173             work = Path(temporary)
    174             for name, raw in templates.items():
    175                 (work / (name + ".txt")).write_text(raw)
    176             self.assertEqual(len(admission.admit_case_logs(work)), 3)
    177             for name, raw in templates.items():
    178                 for malformed in [
    179                     "",
    180                     raw + raw,
    181                     raw.replace("passed", "skipped").replace("... ok", "... ignored"),
    182                     raw.replace("1 passed", "0 passed").replace(
    183                         "Executed 1", "Executed 0"
    184                     ),
    185                 ]:
    186                     with self.subTest(name=name, malformed=malformed):
    187                         (work / (name + ".txt")).write_text(malformed)
    188                         with self.assertRaises(ValueError):
    189                             admission.admit_case_logs(work)
    190                 (work / (name + ".txt")).write_text(raw)
    191 
    192     def test_changed_public_generation_and_installed_identity_refuse(self):
    193         host = {
    194             "source_generation": "fixture_generation",
    195             "bundle_identifier": "dev.local.radroots",
    196             "keychain_service_prefix": "org.radroots.field_ios.local",
    197         }
    198         rust = {"public_key": "ab" * 32}
    199         native = {
    200             "public_key": rust["public_key"],
    201             "source_generation": host["source_generation"],
    202         }
    203         self.assertEqual(
    204             admission.admit_identities(host, rust, native), rust["public_key"]
    205         )
    206         for field in ["source_generation", "public_key"]:
    207             value = copy.deepcopy(native)
    208             value[field] = "different"
    209             with self.subTest(field=field), self.assertRaises(ValueError):
    210                 admission.admit_identities(host, rust, value)
    211         for field in ["bundle_identifier", "keychain_service_prefix"]:
    212             value = copy.deepcopy(host)
    213             value[field] = "different"
    214             with self.subTest(field=field), self.assertRaises(ValueError):
    215                 admission.admit_identities(value, rust, native)
    216         for public in ["ab" * 31, "AB" * 32, "z" * 64]:
    217             with self.subTest(public=public), self.assertRaises(ValueError):
    218                 admission.admit_identities(host, {"public_key": public}, native)
    219 
    220     def test_media_escape_hash_size_and_receipt_state_refuse(self):
    221         with tempfile.TemporaryDirectory() as temporary:
    222             root = Path(temporary)
    223             output = root / "output"
    224             output.mkdir()
    225             content = (
    226                 b"synthetic byte-boundary input, not a historical positive fixture"
    227             )
    228             (output / "blob").write_bytes(content)
    229             (root / "outside").write_bytes(content)
    230             host = {
    231                 "staged_relative_path": "blob",
    232                 "media_sha256": admission.sha256(content),
    233                 "media_bytes": len(content),
    234             }
    235             native = {
    236                 "media_sha256": host["media_sha256"],
    237                 "state": "awaitingVerification",
    238             }
    239             admission.admit_media(output, host, native)
    240             for field, value in [
    241                 ("staged_relative_path", "../outside"),
    242                 ("media_sha256", "0" * 64),
    243                 ("media_bytes", len(content) + 1),
    244             ]:
    245                 changed = {**host, field: value}
    246                 with self.subTest(field=field), self.assertRaises(ValueError):
    247                     admission.admit_media(output, changed, native)
    248             with self.assertRaises(ValueError):
    249                 admission.admit_media(output, host, {**native, "state": "completed"})
    250             (output / "link").symlink_to(output / "blob")
    251             with self.assertRaises(ValueError):
    252                 admission.admit_media(
    253                     output, {**host, "staged_relative_path": "link"}, native
    254                 )
    255 
    256     def test_wrong_inventory_head_or_missing_credential_scan_refuses(self):
    257         rows = [
    258             {
    259                 "draft_id": str(index),
    260                 "revision": 1,
    261                 "author_public_key": "public",
    262                 "operation_id": None,
    263             }
    264             for index in range(106)
    265         ]
    266         rust = {
    267             "drafts": [
    268                 {"draft_id": row["draft_id"], "revision": 1, "operation_id": None}
    269                 for row in rows
    270             ],
    271             "actual_first_page_count": 100,
    272             "pending_media_outside_first_page": True,
    273             "secret_scan": True,
    274             "authorization_scan": True,
    275         }
    276         admission.admit_statuses(rust, rows, "public")
    277         for field, value in [
    278             ("actual_first_page_count", 99),
    279             ("pending_media_outside_first_page", False),
    280             ("secret_scan", False),
    281             ("authorization_scan", False),
    282         ]:
    283             with self.subTest(field=field), self.assertRaises(ValueError):
    284                 admission.admit_statuses({**rust, field: value}, rows, "public")
    285         damaged = copy.deepcopy(rows)
    286         damaged[0]["revision"] = 2
    287         with self.assertRaises(ValueError):
    288             admission.admit_statuses(rust, damaged, "public")
    289 
    290 
    291 if __name__ == "__main__":
    292     unittest.main()