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_persona_attachment_selection.py (5361B)


      1 import json
      2 import unittest
      3 from pathlib import Path
      4 from unittest import mock
      5 
      6 from scripts import test_local_social_fixture as support
      7 
      8 fixture = support.fixture
      9 
     10 
     11 class PersonaAttachmentExtractionTests(unittest.TestCase):
     12     def setUp(self) -> None:
     13         self.suite, self.attempts = (
     14             support.LocalSocialFixtureTests().persona_attempt_attachments()
     15         )
     16 
     17     def export(self, destination: Path) -> list[dict]:
     18         rows = []
     19         for index, (name, (raw, _)) in enumerate(
     20             zip(fixture.PERSONA_ATTACHMENT_NAMES, self.attempts, strict=True)
     21         ):
     22             exported = f"attempt-{index}.json"
     23             (destination / exported).write_bytes(raw)
     24             rows.append(
     25                 self.row(
     26                     exported,
     27                     f"{name[:-5]}_0_{index:08X}-0000-0000-0000-000000000000.json",
     28                 )
     29             )
     30         for index in range(3):
     31             for label, suffix in [
     32                 ("Screen before audit", "png"),
     33                 ("Accessibility tree before audit", "txt"),
     34             ]:
     35                 exported = f"audit-{index}.{suffix}"
     36                 (destination / exported).write_bytes(b"native audit diagnostic")
     37                 rows.append(
     38                     self.row(
     39                         exported,
     40                         f"{label}_0_{index:08X}-0000-0000-0000-000000000000.{suffix}",
     41                     )
     42                 )
     43         return rows
     44 
     45     @staticmethod
     46     def row(exported: str, name: str) -> dict:
     47         return {
     48             "exportedFileName": exported,
     49             "suggestedHumanReadableName": name,
     50             "isAssociatedWithFailure": False,
     51             "configurationName": "Test Scheme Action",
     52             "deviceName": "iPhone 17 Pro",
     53             "deviceId": "11111111-2222-3333-4444-555555555555",
     54         }
     55 
     56     def extract(self, mutation=None):
     57         def run(command, **_):
     58             destination = Path(command[command.index("--output-path") + 1])
     59             rows = self.export(destination)
     60             if mutation is not None:
     61                 mutation(destination, rows)
     62             (destination / "manifest.json").write_text(
     63                 json.dumps(
     64                     [
     65                         {
     66                             "testIdentifier": fixture.PERSONA_XCRESULT_NODE_IDENTIFIER,
     67                             "testIdentifierURL": fixture.PERSONA_XCRESULT_NODE_URL,
     68                             "attachments": rows,
     69                         }
     70                     ]
     71                 )
     72             )
     73 
     74         tests = {
     75             "devices": [],
     76             "testPlanConfigurations": [],
     77             "testNodes": [
     78                 {
     79                     "name": "testLocalSocialDeterministicPersonas()",
     80                     "nodeIdentifier": fixture.PERSONA_XCRESULT_NODE_IDENTIFIER,
     81                     "nodeIdentifierURL": fixture.PERSONA_XCRESULT_NODE_URL,
     82                     "nodeType": "Test Case",
     83                     "result": "Passed",
     84                 }
     85             ],
     86         }
     87         with (
     88             mock.patch.object(fixture, "run_json_command_bounded", return_value=tests),
     89             mock.patch.object(fixture, "run_selector_command", side_effect=run),
     90         ):
     91             return fixture.extract_persona_attempt_attachments(
     92                 Path("synthetic.xcresult"), self.suite, require_measured_network=True
     93             )
     94 
     95     def test_native_audit_diagnostics_do_not_replace_any_measured_attempt(self) -> None:
     96         actual = self.extract()
     97         self.assertEqual(actual, self.attempts)
     98         self.assertEqual(len(actual), 15)
     99 
    100     def test_unknown_unsafe_or_foreign_diagnostics_are_rejected(self) -> None:
    101         def unknown(_, rows):
    102             rows[-1]["suggestedHumanReadableName"] = "unknown.json"
    103 
    104         def unrelated_file(destination, _):
    105             (destination / "unreferenced.txt").write_text("unreferenced")
    106 
    107         def failure(_, rows):
    108             rows[-1]["isAssociatedWithFailure"] = True
    109 
    110         def foreign(_, rows):
    111             rows[-1]["deviceId"] = "22222222-2222-3333-4444-555555555555"
    112 
    113         def traversal(_, rows):
    114             rows[-1]["exportedFileName"] = "../outside.txt"
    115 
    116         def duplicate(_, rows):
    117             rows[0]["suggestedHumanReadableName"] = rows[1][
    118                 "suggestedHumanReadableName"
    119             ]
    120 
    121         def symbolic_link(destination, rows):
    122             path = destination / rows[-1]["exportedFileName"]
    123             path.unlink()
    124             path.symlink_to(destination / rows[0]["exportedFileName"])
    125 
    126         for mutation in [
    127             unknown,
    128             unrelated_file,
    129             failure,
    130             foreign,
    131             traversal,
    132             duplicate,
    133             symbolic_link,
    134         ]:
    135             with (
    136                 self.subTest(mutation=mutation.__name__),
    137                 self.assertRaises(ValueError),
    138             ):
    139                 self.extract(mutation)
    140 
    141     def test_raw_diagnostics_retain_the_existing_result_file_byte_bound(self) -> None:
    142         def oversized(destination, rows):
    143             with (destination / rows[-1]["exportedFileName"]).open("wb") as stream:
    144                 stream.truncate(fixture.MAX_RESULT_BUNDLE_FILE_BYTES + 1)
    145 
    146         with self.assertRaisesRegex(ValueError, "byte bound"):
    147             self.extract(oversized)
    148 
    149 
    150 if __name__ == "__main__":
    151     unittest.main()