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
| 1 | """Failure-path tests for write-once synthetic QA artifacts.""" |
| 2 | |
| 3 | import errno |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import stat |
| 7 | import subprocess |
| 8 | import sys |
| 9 | import tempfile |
| 10 | import unittest |
| 11 | from unittest.mock import patch |
| 12 | |
| 13 | from scripts.personal_recovery.common import RecoveryError |
| 14 | from scripts.personal_recovery.files import publish_new, read_bounded |
| 15 | |
| 16 | |
| 17 | class RecoveryFilesTests(unittest.TestCase): |
| 18 | def setUp(self): |
| 19 | self.tmp = tempfile.TemporaryDirectory() |
| 20 | self.addCleanup(self.tmp.cleanup) |
| 21 | self.root = Path(self.tmp.name) |
| 22 | |
| 23 | def test_complete_file_private_and_no_temporary_residue(self): |
| 24 | dest = self.root / "fictional.lgrf" |
| 25 | publish_new(dest, b"synthetic ciphertext") |
| 26 | self.assertEqual(dest.read_bytes(), b"synthetic ciphertext") |
| 27 | self.assertEqual(sorted(p.name for p in self.root.iterdir()), [dest.name]) |
| 28 | if os.name == "posix": |
| 29 | self.assertEqual(stat.S_IMODE(dest.stat().st_mode), 0o600) |
| 30 | |
| 31 | def test_existing_file_never_overwritten(self): |
| 32 | dest = self.root / "existing" |
| 33 | dest.write_bytes(b"keep") |
| 34 | with self.assertRaises(RecoveryError) as failure: |
| 35 | publish_new(dest, b"replacement") |
| 36 | self.assertEqual(failure.exception.code, "destination_exists") |
| 37 | self.assertEqual(dest.read_bytes(), b"keep") |
| 38 | self.assertEqual(list(self.root.iterdir()), [dest]) |
| 39 | |
| 40 | def test_symlink_never_replaces_or_modifies_target(self): |
| 41 | target = self.root / "target" |
| 42 | target.write_bytes(b"keep") |
| 43 | dest = self.root / "link" |
| 44 | dest.symlink_to(target) |
| 45 | with self.assertRaises(RecoveryError): |
| 46 | publish_new(dest, b"replacement") |
| 47 | self.assertTrue(dest.is_symlink()) |
| 48 | self.assertEqual(target.read_bytes(), b"keep") |
| 49 | |
| 50 | def test_space_exhaustion_leaves_no_output(self): |
| 51 | with patch("scripts.personal_recovery.files.os.fsync", side_effect=OSError(errno.ENOSPC, "synthetic")): |
| 52 | with self.assertRaises(RecoveryError) as failure: |
| 53 | publish_new(self.root / "new", b"fictional") |
| 54 | self.assertEqual(failure.exception.code, "file_io_error") |
| 55 | self.assertEqual(list(self.root.iterdir()), []) |
| 56 | |
| 57 | def test_interruption_before_publish_leaves_no_output(self): |
| 58 | with patch("scripts.personal_recovery.files.os.fsync", side_effect=KeyboardInterrupt): |
| 59 | with self.assertRaises(KeyboardInterrupt): |
| 60 | publish_new(self.root / "new", b"fictional") |
| 61 | self.assertEqual(list(self.root.iterdir()), []) |
| 62 | |
| 63 | def test_racing_destination_creation_is_preserved(self): |
| 64 | dest = self.root / "new" |
| 65 | real_link = os.link |
| 66 | |
| 67 | def raced(source, destination): |
| 68 | Path(destination).write_bytes(b"other writer") |
| 69 | return real_link(source, destination) |
| 70 | |
| 71 | with patch("scripts.personal_recovery.files.os.link", side_effect=raced): |
| 72 | with self.assertRaises(RecoveryError) as failure: |
| 73 | publish_new(dest, b"replacement") |
| 74 | self.assertEqual(failure.exception.code, "destination_exists") |
| 75 | self.assertEqual(dest.read_bytes(), b"other writer") |
| 76 | self.assertEqual(list(self.root.iterdir()), [dest]) |
| 77 | |
| 78 | def test_bounded_read_rejects_oversize_and_symlink(self): |
| 79 | path = self.root / "input" |
| 80 | path.write_bytes(b"12345") |
| 81 | self.assertEqual(read_bounded(path, 5), b"12345") |
| 82 | with self.assertRaises(RecoveryError): |
| 83 | read_bounded(path, 4) |
| 84 | link = self.root / "link" |
| 85 | link.symlink_to(path) |
| 86 | with self.assertRaises(RecoveryError): |
| 87 | read_bounded(link, 5) |
| 88 | |
| 89 | @unittest.skipUnless(hasattr(os, "mkfifo"), "POSIX named-pipe test") |
| 90 | def test_named_pipe_is_refused_without_waiting_for_writer(self): |
| 91 | fifo = self.root / "pipe" |
| 92 | os.mkfifo(fifo) |
| 93 | code = ("from pathlib import Path; from scripts.personal_recovery.files import read_bounded; " |
| 94 | "read_bounded(Path(__import__('sys').argv[1]), 10)") |
| 95 | try: |
| 96 | result = subprocess.run([sys.executable, "-c", code, str(fifo)], capture_output=True, timeout=2) |
| 97 | except subprocess.TimeoutExpired: |
| 98 | self.fail("nonregular input blocked instead of being refused") |
| 99 | self.assertNotEqual(result.returncode, 0) |
| 100 | self.assertIn(b"invalid_input_file", result.stderr) |
| 101 | |
| 102 | def test_swap_after_initial_lstat_cannot_bypass_identity_or_size_checks(self): |
| 103 | source = self.root / "source" |
| 104 | source.write_bytes(b"small") |
| 105 | replacement = self.root / "replacement" |
| 106 | replacement.write_bytes(b"oversized fictional replacement") |
| 107 | real_open = os.open |
| 108 | |
| 109 | def swapped(path, flags): |
| 110 | source.unlink() |
| 111 | source.symlink_to(replacement) |
| 112 | return real_open(path, flags) |
| 113 | |
| 114 | with patch("scripts.personal_recovery.files.os.open", side_effect=swapped): |
| 115 | with self.assertRaises(RecoveryError): |
| 116 | read_bounded(source, 10) |
| 117 | |
| 118 | def test_regular_file_swap_with_same_size_is_refused(self): |
| 119 | source = self.root / "source" |
| 120 | source.write_bytes(b"first") |
| 121 | replacement = self.root / "replacement" |
| 122 | replacement.write_bytes(b"other") |
| 123 | real_open = os.open |
| 124 | |
| 125 | def swapped(path, flags): |
| 126 | os.replace(replacement, source) |
| 127 | return real_open(path, flags) |
| 128 | |
| 129 | with patch("scripts.personal_recovery.files.os.open", side_effect=swapped): |
| 130 | with self.assertRaises(RecoveryError): |
| 131 | read_bounded(source, 10) |
| 132 | |
| 133 | def test_prefix_refusal_precedes_any_body_read(self): |
| 134 | source = self.root / "source" |
| 135 | source.write_bytes(b"f" * 200) |
| 136 | observed = [] |
| 137 | |
| 138 | def reject(prefix): |
| 139 | observed.append(prefix) |
| 140 | raise RecoveryError("synthetic_marker_required") |
| 141 | |
| 142 | with self.assertRaises(RecoveryError) as failure: |
| 143 | read_bounded(source, 300, prefix_validator=reject) |
| 144 | self.assertEqual(failure.exception.code, "synthetic_marker_required") |
| 145 | self.assertEqual(observed, [b"f" * 100]) |
| 146 | |
| 147 | def test_prefix_gate_uses_unbuffered_stream_without_read_ahead(self): |
| 148 | source = self.root / "source" |
| 149 | source.write_bytes(b"f" * 200) |
| 150 | real_fdopen = os.fdopen |
| 151 | with patch("scripts.personal_recovery.files.os.fdopen", wraps=real_fdopen) as opening: |
| 152 | read_bounded(source, 300, prefix_validator=lambda prefix: None) |
| 153 | self.assertEqual(opening.call_args.kwargs.get("buffering"), 0) |
| 154 | |
| 155 | |
| 156 | if __name__ == "__main__": |
| 157 | unittest.main() |