| 1 | import os |
| 2 | from pathlib import Path |
| 3 | from types import SimpleNamespace |
| 4 | from unittest.mock import patch |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from lib import private_files |
| 9 | |
| 10 | |
| 11 | @pytest.mark.skipif(os.name != "posix", reason="POSIX file permissions") |
| 12 | def test_new_file_is_private_at_creation(tmp_path): |
| 13 | path = tmp_path / "state.json" |
| 14 | modes_at_creation = [] |
| 15 | real_open = os.open |
| 16 | |
| 17 | def observe_open(*args, **kwargs): |
| 18 | fd = real_open(*args, **kwargs) |
| 19 | modes_at_creation.append(os.fstat(fd).st_mode & 0o777) |
| 20 | return fd |
| 21 | |
| 22 | old_umask = os.umask(0o022) |
| 23 | try: |
| 24 | with patch.object(private_files.os, "open", side_effect=observe_open): |
| 25 | private_files.write_private_text(path, "private evidence") |
| 26 | finally: |
| 27 | os.umask(old_umask) |
| 28 | |
| 29 | assert modes_at_creation == [0o600] |
| 30 | assert path.read_text() == "private evidence" |
| 31 | |
| 32 | |
| 33 | def test_windows_write_keeps_existing_file_mode(tmp_path, monkeypatch): |
| 34 | path = tmp_path / "state.json" |
| 35 | path.write_text("old content" * 20, encoding="utf-8") |
| 36 | original_mode = path.stat().st_mode |
| 37 | monkeypatch.setattr(private_files, "os", SimpleNamespace(name="nt")) |
| 38 | |
| 39 | private_files.write_private_text(path, "new café", encoding="utf-8") |
| 40 | |
| 41 | assert path.read_text(encoding="utf-8") == "new café" |
| 42 | assert path.stat().st_mode == original_mode |
| 43 | |
| 44 | |
| 45 | @pytest.mark.skipif(os.name != "posix", reason="POSIX file permissions") |
| 46 | def test_permission_failure_closes_descriptor_without_writing(tmp_path): |
| 47 | path = tmp_path / "state.json" |
| 48 | path.write_text("old content", encoding="utf-8") |
| 49 | descriptor = os.open(path, os.O_WRONLY) |
| 50 | with patch.object(private_files.os, "open", return_value=descriptor), \ |
| 51 | patch.object(private_files.os, "fchmod", side_effect=PermissionError("denied")): |
| 52 | with pytest.raises(PermissionError, match="denied"): |
| 53 | private_files.write_private_text(path, "new content") |
| 54 | |
| 55 | with pytest.raises(OSError): |
| 56 | os.fstat(descriptor) |
| 57 | assert path.read_text(encoding="utf-8") == "old content" |
| 58 | |
| 59 | |
| 60 | @pytest.mark.parametrize( |
| 61 | ("platform", "winerror", "failures", "delays", "published"), |
| 62 | [ |
| 63 | ("nt", 32, 1, [0.05], True), |
| 64 | ("nt", 32, 4, [0.05, 0.05, 0.05], False), |
| 65 | ("nt", 5, 1, [], False), |
| 66 | ("posix", 32, 1, [], False), |
| 67 | ], |
| 68 | ) |
| 69 | def test_atomic_private_text_retries_only_windows_sharing_violations( |
| 70 | tmp_path, monkeypatch, platform, winerror, failures, delays, published, |
| 71 | ): |
| 72 | target = tmp_path / "cache.json" |
| 73 | target.write_text("old", encoding="utf-8") |
| 74 | staged_paths = [] |
| 75 | waits = [] |
| 76 | failure = PermissionError("simulated replacement failure") |
| 77 | failure.winerror = winerror |
| 78 | |
| 79 | def replace(source, destination): |
| 80 | stage = Path(source) |
| 81 | staged_paths.append(stage) |
| 82 | assert stage.read_text(encoding="utf-8") == "new" |
| 83 | assert destination == target |
| 84 | assert target.read_text(encoding="utf-8") == "old" |
| 85 | if len(staged_paths) <= failures: |
| 86 | raise failure |
| 87 | os.replace(source, destination) |
| 88 | |
| 89 | monkeypatch.setattr(private_files, "os", SimpleNamespace(name=platform, replace=replace)) |
| 90 | monkeypatch.setattr("time.sleep", waits.append) |
| 91 | |
| 92 | if published: |
| 93 | private_files.replace_private_text(target, "new") |
| 94 | else: |
| 95 | with pytest.raises(PermissionError) as caught: |
| 96 | private_files.replace_private_text(target, "new") |
| 97 | assert caught.value is failure |
| 98 | |
| 99 | assert len(staged_paths) == len(delays) + 1 |
| 100 | assert len(set(staged_paths)) == 1 |
| 101 | assert waits == delays |
| 102 | assert target.read_text(encoding="utf-8") == ("new" if published else "old") |
| 103 | assert list(tmp_path.iterdir()) == [target] |
| 104 |