forked from ChelseaKR/habitable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_social_recovery.py
More file actions
198 lines (181 loc) · 5.13 KB
/
Copy pathtest_cli_social_recovery.py
File metadata and controls
198 lines (181 loc) · 5.13 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright 2026 Chelsea Kelly-Reif
"""CLI: threshold (M-of-N) social custody of recovery keys (EXP-11)."""
from __future__ import annotations
from pathlib import Path
import pytest
from habitable.cli import main
def _init_case(vault: Path) -> None:
assert main(["init", str(vault), "--case", "bldg-12", "--unit", "4B"]) == 0
assert main(["issue", "--vault", str(vault), "--category", "mold", "--title", "M"]) == 0
def test_share_and_recover_2_of_3(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
vault = tmp_path / "vault"
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
_init_case(vault)
out_dir = tmp_path / "custody"
assert (
main(
[
"key",
"share",
"--vault",
str(vault),
"--threshold",
"2",
"--steward",
"Ana",
"--steward",
"Bo",
"--steward",
"Cy",
"--out-dir",
str(out_dir),
]
)
== 0
)
bundle = out_dir / "recovery-bundle.json"
shares = sorted(out_dir.glob("share-*.json"))
assert bundle.exists()
assert len(shares) == 3
# Lose the keyfile; the vault no longer opens.
(vault / "keyfile.json").unlink()
assert main(["status", "--vault", str(vault)]) == 1
# Any two stewards' shares reconstruct the key under a new passphrase.
assert (
main(
[
"key",
"recover",
str(vault),
"--bundle",
str(bundle),
"--share",
str(shares[0]),
"--share",
str(shares[2]),
"--new-passphrase",
"pw-recovered",
]
)
== 0
)
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw-recovered")
assert main(["status", "--vault", str(vault)]) == 0
def test_single_share_cannot_recover(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
vault = tmp_path / "vault"
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
_init_case(vault)
out_dir = tmp_path / "custody"
assert (
main(
[
"key",
"share",
"--vault",
str(vault),
"--threshold",
"2",
"--steward",
"Ana",
"--steward",
"Bo",
"--steward",
"Cy",
"--out-dir",
str(out_dir),
]
)
== 0
)
shares = sorted(out_dir.glob("share-*.json"))
bundle = out_dir / "recovery-bundle.json"
# One share is below quorum: the CLI refuses before touching crypto.
assert (
main(
[
"key",
"recover",
str(vault),
"--bundle",
str(bundle),
"--share",
str(shares[0]),
"--new-passphrase",
"x",
]
)
== 1
)
def test_share_rejects_threshold_larger_than_stewards(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
vault = tmp_path / "vault"
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
_init_case(vault)
assert (
main(
[
"key",
"share",
"--vault",
str(vault),
"--threshold",
"3",
"--steward",
"Ana",
"--steward",
"Bo",
"--out-dir",
str(tmp_path / "c"),
]
)
== 1
)
def test_recover_rejects_mismatched_shares(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
vault = tmp_path / "vault"
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
_init_case(vault)
dir_a = tmp_path / "a"
dir_b = tmp_path / "b"
for d in (dir_a, dir_b):
assert (
main(
[
"key",
"share",
"--vault",
str(vault),
"--threshold",
"2",
"--steward",
"Ana",
"--steward",
"Bo",
"--out-dir",
str(d),
]
)
== 0
)
shares_a = sorted(dir_a.glob("share-*.json"))
shares_b = sorted(dir_b.glob("share-*.json"))
# Mixing a share from bundle A with bundle B fails (bundle_id mismatch).
assert (
main(
[
"key",
"recover",
str(vault),
"--bundle",
str(dir_a / "recovery-bundle.json"),
"--share",
str(shares_a[0]),
"--share",
str(shares_b[1]),
"--new-passphrase",
"x",
]
)
== 1
)