| 1 | #!/usr/bin/env python3 |
| 2 | import importlib.util |
| 3 | import io |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import plistlib |
| 7 | import shutil |
| 8 | import tempfile |
| 9 | import threading |
| 10 | import unittest |
| 11 | import urllib.error |
| 12 | import urllib.request |
| 13 | import zipfile |
| 14 | |
| 15 | REPO = Path(__file__).resolve().parents[1] |
| 16 | root = tempfile.TemporaryDirectory() |
| 17 | os.environ.update(OTA_ROOT=root.name, OTA_ORIGIN="https://ota.example", OTA_UPLOAD_TOKEN="secret") |
| 18 | |
| 19 | |
| 20 | def load(name, path): |
| 21 | spec = importlib.util.spec_from_file_location(name, path) |
| 22 | module = importlib.util.module_from_spec(spec) |
| 23 | spec.loader.exec_module(module) |
| 24 | return module |
| 25 | |
| 26 | |
| 27 | service = load("ota_service", REPO / "service/ota/build/ota.py") |
| 28 | client = load("ota_client", REPO / "tools/ota.py") |
| 29 | |
| 30 | |
| 31 | def ipa(devices=None): |
| 32 | buffer = io.BytesIO() |
| 33 | with zipfile.ZipFile(buffer, "w") as archive: |
| 34 | archive.writestr("Payload/App.app/Info.plist", plistlib.dumps({ |
| 35 | "CFBundleIdentifier": "net.example.app", "CFBundleName": "App", "CFBundleDisplayName": "Fancy <App>", "CFBundleShortVersionString": "1.2", |
| 36 | })) |
| 37 | if devices is not None: |
| 38 | archive.writestr("Payload/App.app/embedded.mobileprovision", b"\x30\x82cms" + plistlib.dumps({"ProvisionedDevices": devices}) + b"\x00sig") |
| 39 | return buffer.getvalue() |
| 40 | |
| 41 | |
| 42 | class Service(unittest.TestCase): |
| 43 | @classmethod |
| 44 | def setUpClass(cls): |
| 45 | cls.server = service.http.server.ThreadingHTTPServer(("127.0.0.1", 0), service.Handler) |
| 46 | threading.Thread(target=cls.server.serve_forever, daemon=True).start() |
| 47 | cls.base = f"http://127.0.0.1:{cls.server.server_port}" |
| 48 | |
| 49 | @classmethod |
| 50 | def tearDownClass(cls): |
| 51 | cls.server.shutdown() |
| 52 | |
| 53 | def setUp(self): |
| 54 | for path in Path(root.name).iterdir(): |
| 55 | shutil.rmtree(path) |
| 56 | |
| 57 | def request(self, path, data=None, token="secret", method=None): |
| 58 | request = urllib.request.Request(self.base + path, data=data, method=method or ("PUT" if data is not None else "GET")) |
| 59 | if token: |
| 60 | request.add_header("Authorization", f"Bearer {token}") |
| 61 | return urllib.request.urlopen(request) |
| 62 | |
| 63 | def test_upload_is_authenticated_and_validated(self): |
| 64 | for token, body, status in ((None, ipa(), 401), ("wrong", ipa(), 401), ("secret", b"not a zip", 400)): |
| 65 | with self.subTest(token=token, status=status), self.assertRaises(urllib.error.HTTPError) as error: |
| 66 | self.request("/builds", body, token) |
| 67 | self.assertEqual(error.exception.code, status) |
| 68 | self.assertEqual([path for path in Path(root.name).iterdir()], []) |
| 69 | |
| 70 | def test_install_link_serves_page_manifest_and_ipa_then_prunes(self): |
| 71 | body = ipa() |
| 72 | link = self.request("/builds", body).read().decode().strip() |
| 73 | build = link.rsplit("/", 1)[1] |
| 74 | page = self.request(f"/{build}").read().decode() |
| 75 | self.assertIn("Fancy &lt;App&gt;", page) |
| 76 | self.assertIn(f"itms-services://?action=download-manifest&amp;url=https://ota.example/{build}/manifest.plist", page) |
| 77 | manifest = plistlib.loads(self.request(f"/{build}/manifest.plist").read())["items"][0] |
| 78 | self.assertEqual(manifest["assets"][0]["url"], f"https://ota.example/{build}/app.ipa") |
| 79 | self.assertEqual(manifest["metadata"]["bundle-identifier"], "net.example.app") |
| 80 | self.assertEqual(self.request(f"/{build}/app.ipa").read(), body) |
| 81 | for _ in range(service.KEEP): |
| 82 | self.request("/builds", body) |
| 83 | with self.assertRaises(urllib.error.HTTPError) as error: |
| 84 | self.request(f"/{build}") |
| 85 | self.assertEqual(error.exception.code, 404) |
| 86 | self.assertEqual(len(list(Path(root.name).iterdir())), service.KEEP) |
| 87 | |
| 88 | |
| 89 | class Profile(unittest.TestCase): |
| 90 | def read(self, data): |
| 91 | with tempfile.NamedTemporaryFile(suffix=".ipa") as file: |
| 92 | file.write(data) |
| 93 | file.flush() |
| 94 | return client.provisioned(file.name) |
| 95 | |
| 96 | def test_reads_devices_from_the_signed_profile_and_reports_none_when_unsigned(self): |
| 97 | self.assertEqual(self.read(ipa(["00008110-001234567890401E"]))["ProvisionedDevices"], ["00008110-001234567890401E"]) |
| 98 | self.assertIsNone(self.read(ipa())) |
| 99 | |
| 100 | |
| 101 | if __name__ == "__main__": |
| 102 | unittest.main() |