forked from ChelseaKR/habitable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_sync_file.py
More file actions
178 lines (151 loc) · 5.72 KB
/
Copy pathtest_cli_sync_file.py
File metadata and controls
178 lines (151 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright 2026 Chelsea Kelly-Reif
"""File-based sneakernet sync via the CLI (`sync-export` / `sync-import`, E-09).
Two vaults are built with the `test_sync.py` fixture pattern, then the real CLI
`main()` is driven (as in `test_cli_demo.py`) to export A's encrypted delta to a
file, hand it over, and import it into B — with no relay and no network at all.
"""
from __future__ import annotations
from collections.abc import Callable
from pathlib import Path
import pytest
from habitable.capture import capture
from habitable.cli import main
from habitable.sync import suggested_delta_filename
from habitable.tsa import LocalRfc3161TSA
from habitable.vault import Vault
SENTINEL = "PLAINTEXT-SENTINEL-mold-on-bathroom-ceiling"
def _seed(vault: Vault, make_jpeg: Callable[..., Path], tsa: LocalRfc3161TSA) -> str:
"""Add one issue + one timestamped capture; capture() persists to disk."""
issue = vault.document.add_issue(category="mold", room="bath", title=SENTINEL, issue_id="i1")
capture(vault, make_jpeg(with_location=True), issue_id=issue, tsa=tsa)
return issue
def test_sneakernet_roundtrip_and_idempotent_reimport(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
a = make_vault("A")
b = make_vault("B", passphrase="pw-b")
_seed(a, make_jpeg, local_tsa)
usb = tmp_path / "usb"
usb.mkdir()
delta = usb / "delta.hsync"
peer_b = b.identity.public().encode()
# A exports a delta sealed to B onto the "stick".
assert (
main(
[
"sync-export",
"--vault",
str(a.path),
"--passphrase",
"test-passphrase",
"--peer",
peer_b,
"--out",
str(delta),
]
)
== 0
)
assert delta.exists() and delta.stat().st_size > 0
out = capsys.readouterr().out
assert "sealed to peer" in out
assert b.identity.public().fingerprint in out
assert "leaks nothing" in out
# B imports it: the output is not silent — it names merges and captures.
assert main(["sync-import", "--vault", str(b.path), "--passphrase", "pw-b", str(delta)]) == 0
out = capsys.readouterr().out
assert "merged 1 message" in out
assert "imported 1 capture" in out
# B now holds the issue, the sealed original, the token, and intact custody.
b_reopened = Vault.open(b.path, "pw-b")
assert [i.issue_id for i in b_reopened.document.issues()] == ["i1"]
record = b_reopened.document.captures()[0]
assert b_reopened.read_original(record.capture_id, record.content_hash)
assert b_reopened.get_token(record.capture_id) is not None
assert b_reopened.custody.verify().ok
# Re-importing the same file is idempotent: still succeeds, 0 new captures.
assert main(["sync-import", "--vault", str(b.path), "--passphrase", "pw-b", str(delta)]) == 0
assert "imported 0 captures" in capsys.readouterr().out
def test_sneakernet_import_wrong_recipient_errors_cleanly(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
a = make_vault("A")
b = make_vault("B", passphrase="pw-b")
c = make_vault("C", passphrase="pw-c")
_seed(a, make_jpeg, local_tsa)
delta = tmp_path / "delta.hsync"
assert (
main(
[
"sync-export",
"--vault",
str(a.path),
"--passphrase",
"test-passphrase",
"--peer",
b.identity.public().encode(), # sealed to B, not C
"--out",
str(delta),
]
)
== 0
)
capsys.readouterr()
# C is not the recipient: a clean, non-zero error, and nothing is imported.
assert main(["sync-import", "--vault", str(c.path), "--passphrase", "pw-c", str(delta)]) == 1
assert "sealed to this device" in capsys.readouterr().err
assert Vault.open(c.path, "pw-c").document.issues() == []
def test_sneakernet_import_rejects_forged_blob(
make_vault: Callable[..., Vault],
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
b = make_vault("B", passphrase="pw-b")
forged = tmp_path / "forged.hsync"
forged.write_bytes(b"not a sealed habitable delta at all")
assert main(["sync-import", "--vault", str(b.path), "--passphrase", "pw-b", str(forged)]) == 1
assert "sealed to this device" in capsys.readouterr().err
def test_sneakernet_export_default_filename_uses_peer_fp8(
make_vault: Callable[..., Vault],
make_jpeg: Callable[..., Path],
local_tsa: LocalRfc3161TSA,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
a = make_vault("A")
b = make_vault("B", passphrase="pw-b")
_seed(a, make_jpeg, local_tsa)
peer = b.identity.public()
monkeypatch.chdir(tmp_path)
assert (
main(
[
"sync-export",
"--vault",
str(a.path),
"--passphrase",
"test-passphrase",
"--peer",
peer.encode(),
]
)
== 0
)
expected = tmp_path / suggested_delta_filename(peer)
assert expected.exists() and expected.stat().st_size > 0
def test_suggested_delta_filename_is_stable_and_fp8(
make_vault: Callable[..., Vault],
) -> None:
peer = make_vault("B").identity.public()
fp8 = peer.fingerprint.replace("-", "")[:8]
assert len(fp8) == 8
assert suggested_delta_filename(peer) == f"habitable-delta-{fp8}.hsync"