LifeMate — Personal, Family & Learning Companion

feat/local-signing-preflight
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
py 145 lines 7.43 KB
Raw
1 """Real JDK integration, using only disposable fictional keystores."""
2
3 import hashlib
4 import json
5 import shutil
6 import struct
7 import unittest
8
9 from scripts.tests.signing_test_support import request_frame, run_inspector, synthetic_keystores
10
11
12 @unittest.skipUnless(shutil.which("java") and shutil.which("keytool"), "JDK required")
13 class SigningJavaTests(unittest.TestCase):
14 @classmethod
15 def setUpClass(cls):
16 cls.fixture_context = synthetic_keystores()
17 cls.fixtures = cls.fixture_context.__enter__()
18 cls.addClassCleanup(cls.fixture_context.__exit__, None, None, None)
19
20 def inspect(self, path=None, *, alias=None, store_password=None, key_password=None):
21 fixture = self.fixtures
22 path = path or fixture.jks_path
23 frame = request_frame(
24 path.read_bytes(), alias if alias is not None else fixture.alias,
25 store_password if store_password is not None else fixture.store_password,
26 key_password if key_password is not None else (
27 fixture.store_password if path.suffix == ".p12" else fixture.key_password),
28 )
29 return self.parse(run_inspector(frame))
30
31 def parse(self, result):
32 self.assertIn(result.returncode, (0, 2), "helper must return its fixed protocol status")
33 self.assertEqual(result.stderr, b"", "helper must suppress raw diagnostic output")
34 try:
35 report = json.loads(result.stdout)
36 except (ValueError, UnicodeDecodeError):
37 self.fail("helper must return a JSON report")
38 rendered = result.stdout.decode("utf-8")
39 for sensitive in (self.fixtures.alias, self.fixtures.store_password,
40 self.fixtures.key_password, str(self.fixtures.directory)):
41 self.assertTrue(sensitive not in rendered, "helper output leaked fixture input")
42 return result.returncode, report
43
44 def assert_refused(self, result, reason):
45 self.assertEqual(result, (2, {"schemaVersion": 1, "status": "REFUSED", "reason": reason}))
46
47 def test_rsa_jks_proof_repeats_and_source_bytes_stay_unchanged(self):
48 path = self.fixtures.jks_path
49 before = hashlib.sha256(path.read_bytes()).digest()
50 expected = {"schemaVersion": 1, "status": "INSPECTED",
51 "certificateSha256": self.fixtures.fingerprints[path],
52 "debugCertificate": False, "privateKeyProof": True,
53 "certificateCurrentlyValid": True}
54 self.assertEqual(self.inspect(path), (0, expected))
55 self.assertEqual(self.inspect(path), (0, expected))
56 self.assertEqual(hashlib.sha256(path.read_bytes()).digest(), before)
57
58 def test_ec_pkcs12_and_encrypted_copy_have_same_public_facts(self):
59 source = self.fixtures.pkcs12_path
60 original = source.read_bytes()
61 copied = self.fixtures.directory / "encrypted-copy.p12"
62 copied.write_bytes(original)
63 expected = {"schemaVersion": 1, "status": "INSPECTED",
64 "certificateSha256": self.fixtures.fingerprints[source],
65 "debugCertificate": False, "privateKeyProof": True,
66 "certificateCurrentlyValid": True}
67 self.assertEqual(self.inspect(source), (0, expected))
68 self.assertEqual(self.inspect(copied), (0, expected))
69 self.assertEqual(source.read_bytes(), original)
70 self.assertEqual(copied.read_bytes(), original)
71
72 def test_debug_certificate_is_reported(self):
73 code, report = self.inspect(self.fixtures.debug_path)
74 self.assertEqual(code, 0)
75 self.assertIs(report["debugCertificate"], True)
76
77 def test_debug_cn_in_multivalued_rdn_and_non_cn_value(self):
78 cases = (("CN=Android Debug+C=US,O=Android", True),
79 ("CN=android debug+OU=QA,O=Example", True),
80 ("CN=Fictional+OU=Android Debug,O=Example", False))
81 fixture = self.fixtures
82 for index, (subject, expected) in enumerate(cases):
83 with self.subTest(case=index):
84 path = fixture.directory / ("multivalue-" + str(index) + ".jks")
85 fixture.keytool("-genkeypair", "-keystore", path, "-storetype", "JKS",
86 "-alias", fixture.alias, "-keyalg", "EC", "-dname", subject,
87 "-validity", "3650", "-noprompt",
88 "-storepass:env", "LG_TEST_STORE_PASSWORD",
89 "-keypass:env", "LG_TEST_KEY_PASSWORD")
90 before = path.read_bytes()
91 code, report = self.inspect(path)
92 self.assertEqual(code, 0)
93 self.assertIs(report["debugCertificate"], expected)
94 self.assertEqual(path.read_bytes(), before)
95
96 def test_expired_certificate_is_reported_without_blocking_proof(self):
97 path = self.fixtures.directory / "expired.jks"
98 self.fixtures.create_key(path, "RSA", expired=True)
99 code, report = self.inspect(path)
100 self.assertEqual(code, 0)
101 self.assertIs(report["certificateCurrentlyValid"], False)
102 self.assertIs(report["privateKeyProof"], True)
103
104 def test_wrong_store_password_is_sanitized(self):
105 self.assert_refused(self.inspect(store_password="canary-wrong-store"), "keystore_unreadable")
106
107 def test_wrong_key_password_is_sanitized(self):
108 self.assert_refused(self.inspect(key_password="canary-wrong-key"), "private_key_unavailable")
109
110 def test_missing_alias_and_certificate_only_entry_refused(self):
111 self.assert_refused(self.inspect(alias="canary-missing-alias"), "private_key_unavailable")
112 self.assert_refused(self.inspect(self.fixtures.cert_only_path), "private_key_unavailable")
113
114 def test_unsupported_dsa_refused(self):
115 self.assert_refused(self.inspect(self.fixtures.dsa_path), "unsupported_key_algorithm")
116
117 def test_unrelated_rsa_certificate_fails_actual_private_key_proof(self):
118 path = self.fixtures.mismatched_certificate_store()
119 self.assert_refused(self.inspect(path), "key_proof_failed")
120
121 def test_corrupt_keystore_refused_without_echo(self):
122 frame = request_frame(b"canary-corrupt-file", self.fixtures.alias,
123 self.fixtures.store_password, self.fixtures.key_password)
124 self.assert_refused(self.parse(run_inspector(frame)), "keystore_unreadable")
125
126 def test_malformed_frames_refused_before_loading(self):
127 valid = request_frame(self.fixtures.jks_path.read_bytes(), self.fixtures.alias,
128 self.fixtures.store_password, self.fixtures.key_password)
129 malformed = [b"", b"BAD!", b"LGKI" + struct.pack(">I", 2 * 1024 * 1024 + 1),
130 valid[:-1], valid + b"trailing-canary",
131 request_frame(b"file", "", "pass", "pass"),
132 request_frame(b"file", "a" * 257, "pass", "pass"),
133 request_frame(b"file", "alias", "", "pass"),
134 request_frame(b"file", "alias", "p" * 4097, "pass"),
135 request_frame(b"file", "alias", "pass", "line\ncanary"),
136 request_frame(b"file", "alias", "nul\0canary", "pass"),
137 b"LGKI" + struct.pack(">I", 1) + b"x" + struct.pack(">I", 1) + b"\xff"
138 + struct.pack(">I", 4) + b"pass" + struct.pack(">I", 4) + b"pass"]
139 for frame in malformed:
140 with self.subTest(case=malformed.index(frame)):
141 self.assert_refused(self.parse(run_inspector(frame)), "invalid_request")
142
143
144 if __name__ == "__main__":
145 unittest.main()