-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
789 lines (614 loc) · 28 KB
/
test_cli.py
File metadata and controls
789 lines (614 loc) · 28 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
"""
Tests for the capsule CLI.
Tests cover the CLI entry point, verification logic, inspection,
key management, and the hash utility.
"""
import json
import tempfile
from io import StringIO
from pathlib import Path
import pytest
from nacl.signing import SigningKey
from qp_capsule.capsule import Capsule, TriggerSection
from qp_capsule.cli import (
VerifyError,
VerifyResult,
_build_parser,
_capsule_from_full_dict,
_capsule_to_full_dict,
_load_capsules_from_json,
_supports_color,
cmd_hash,
cmd_inspect,
cmd_keys,
cmd_verify,
main,
verify_chain,
)
from qp_capsule.keyring import Keyring
from qp_capsule.seal import Seal
@pytest.fixture
def temp_dir():
with tempfile.TemporaryDirectory() as d:
yield Path(d)
@pytest.fixture
def key_path(temp_dir):
return temp_dir / "key"
@pytest.fixture
def keyring_path(temp_dir):
return temp_dir / "keyring.json"
@pytest.fixture
def seal(key_path):
return Seal(key_path=key_path)
def _make_sealed_chain(seal: Seal, count: int) -> list[Capsule]:
"""Create a sealed chain of capsules."""
capsules: list[Capsule] = []
for i in range(count):
c = Capsule(trigger=TriggerSection(type="test", source="test-cli", request=f"req_{i}"))
c.sequence = i
c.previous_hash = capsules[-1].hash if capsules else None
seal.seal(c)
capsules.append(c)
return capsules
def _write_chain_json(capsules: list[Capsule], path: Path) -> Path:
"""Write a sealed chain to a JSON file."""
data = [_capsule_to_full_dict(c) for c in capsules]
path.write_text(json.dumps(data, indent=2))
return path
# ---------------------------------------------------------------------------
# VerifyResult / VerifyError serialization
# ---------------------------------------------------------------------------
class TestVerifyResult:
def test_to_dict(self):
r = VerifyResult(valid=True, level="structural", capsules_verified=3, total_capsules=3)
d = r.to_dict()
assert d["valid"] is True
assert d["capsules_verified"] == 3
def test_with_errors(self):
err = VerifyError(sequence=2, capsule_id="abc", error="broken")
r = VerifyResult(
valid=False, level="full", capsules_verified=2, total_capsules=5, errors=[err]
)
d = r.to_dict()
assert d["valid"] is False
assert len(d["errors"]) == 1
assert d["errors"][0]["sequence"] == 2
# ---------------------------------------------------------------------------
# Capsule dict helpers
# ---------------------------------------------------------------------------
class TestCapsuleDictHelpers:
def test_roundtrip(self, seal):
c = Capsule(trigger=TriggerSection(type="test", source="s", request="r"))
c.sequence = 0
c.previous_hash = None
seal.seal(c)
d = _capsule_to_full_dict(c)
restored = _capsule_from_full_dict(d)
assert str(restored.id) == str(c.id)
assert restored.hash == c.hash
assert restored.signature == c.signature
assert restored.signed_by == c.signed_by
def test_from_dict_without_seal_fields(self):
d = Capsule().to_dict()
c = _capsule_from_full_dict(d)
assert c.hash == ""
assert c.signature == ""
assert c.signed_by == ""
# ---------------------------------------------------------------------------
# JSON loading
# ---------------------------------------------------------------------------
class TestLoadFromJson:
def test_load_array(self, seal, temp_dir):
chain = _make_sealed_chain(seal, 3)
path = _write_chain_json(chain, temp_dir / "chain.json")
loaded = _load_capsules_from_json(path)
assert len(loaded) == 3
assert loaded[0].hash == chain[0].hash
def test_load_single_object(self, seal, temp_dir):
chain = _make_sealed_chain(seal, 1)
d = _capsule_to_full_dict(chain[0])
path = temp_dir / "single.json"
path.write_text(json.dumps(d))
loaded = _load_capsules_from_json(path)
assert len(loaded) == 1
def test_load_invalid_json_type(self, temp_dir):
path = temp_dir / "bad.json"
path.write_text('"just a string"')
with pytest.raises(ValueError, match="Expected JSON array"):
_load_capsules_from_json(path)
# ---------------------------------------------------------------------------
# Core verification logic
# ---------------------------------------------------------------------------
class TestVerifyChain:
def test_empty_chain(self):
r = verify_chain([])
assert r.valid is True
assert r.capsules_verified == 0
def test_valid_structural(self, seal):
chain = _make_sealed_chain(seal, 5)
r = verify_chain(chain, level="structural")
assert r.valid is True
assert r.capsules_verified == 5
def test_valid_full(self, seal):
chain = _make_sealed_chain(seal, 3)
r = verify_chain(chain, level="full")
assert r.valid is True
def test_valid_signatures(self, seal):
chain = _make_sealed_chain(seal, 3)
r = verify_chain(chain, level="signatures", seal=seal)
assert r.valid is True
def test_sequence_gap(self, seal):
chain = _make_sealed_chain(seal, 3)
chain[1].sequence = 5
r = verify_chain(chain, level="structural")
assert r.valid is False
assert r.capsules_verified == 1
assert "Sequence gap" in r.errors[0].error
def test_genesis_with_previous_hash(self, seal):
chain = _make_sealed_chain(seal, 1)
chain[0].previous_hash = "should_be_none"
r = verify_chain(chain, level="structural")
assert r.valid is False
assert "Genesis" in r.errors[0].error
def test_broken_previous_hash(self, seal):
chain = _make_sealed_chain(seal, 3)
chain[2].previous_hash = "wrong"
r = verify_chain(chain, level="structural")
assert r.valid is False
assert r.capsules_verified == 2
def test_content_hash_mismatch(self, seal):
chain = _make_sealed_chain(seal, 3)
chain[1].trigger.request = "tampered"
r = verify_chain(chain, level="full")
assert r.valid is False
assert "hash mismatch" in r.errors[0].error
def test_signature_failure(self, seal, key_path):
chain = _make_sealed_chain(seal, 2)
chain[0].signature = "00" * 64
r = verify_chain(chain, level="signatures", seal=seal)
assert r.valid is False
assert "Signature" in r.errors[0].error
# ---------------------------------------------------------------------------
# cmd_verify
# ---------------------------------------------------------------------------
class TestCmdVerify:
def test_verify_json_structural(self, seal, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 3)
path = _write_chain_json(chain, temp_dir / "c.json")
args = _build_parser().parse_args(["verify", str(path)])
assert cmd_verify(args) == 0
def test_verify_json_full(self, seal, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 3)
path = _write_chain_json(chain, temp_dir / "c.json")
args = _build_parser().parse_args(["verify", "--full", str(path)])
assert cmd_verify(args) == 0
def test_verify_json_output(self, seal, temp_dir, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 2)
path = _write_chain_json(chain, temp_dir / "c.json")
args = _build_parser().parse_args(["verify", "--json", str(path)])
assert cmd_verify(args) == 0
out = json.loads(capsys.readouterr().out)
assert out["valid"] is True
def test_verify_quiet_pass(self, seal, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 2)
path = _write_chain_json(chain, temp_dir / "c.json")
args = _build_parser().parse_args(["verify", "--quiet", str(path)])
assert cmd_verify(args) == 0
def test_verify_quiet_fail(self, seal, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 2)
chain[1].previous_hash = "bad"
path = _write_chain_json(chain, temp_dir / "c.json")
args = _build_parser().parse_args(["verify", "--quiet", str(path)])
assert cmd_verify(args) == 1
def test_verify_no_source(self, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
args = _build_parser().parse_args(["verify"])
assert cmd_verify(args) == 2
def test_verify_multiple_sources(self, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
path = temp_dir / "c.json"
path.write_text("[]")
args = _build_parser().parse_args(["verify", str(path), "--db", "other.db"])
assert cmd_verify(args) == 2
def test_verify_bad_file(self, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
args = _build_parser().parse_args(["verify", str(temp_dir / "nonexistent.json")])
assert cmd_verify(args) == 2
def test_verify_broken_chain_colored(self, seal, temp_dir, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 3)
chain[1].previous_hash = "wrong"
path = _write_chain_json(chain, temp_dir / "c.json")
args = _build_parser().parse_args(["verify", str(path)])
result = cmd_verify(args)
assert result == 1
out = capsys.readouterr().out
assert "FAIL" in out
def test_verify_db_structural(self, seal, temp_dir, monkeypatch):
"""Verify from SQLite database."""
monkeypatch.setenv("NO_COLOR", "1")
async def _setup_db():
from qp_capsule.storage import CapsuleStorage
db_path = temp_dir / "test.db"
storage = CapsuleStorage(db_path=db_path)
for i in range(3):
c = Capsule(
trigger=TriggerSection(type="test", source="db-test", request=f"r{i}")
)
c.sequence = i
c.previous_hash = None
if i > 0:
latest = await storage.get_latest()
c.previous_hash = latest.hash if latest else None
seal.seal(c)
await storage.store(c)
await storage.close()
return db_path
import asyncio
db_path = asyncio.run(_setup_db())
args = _build_parser().parse_args(["verify", "--db", str(db_path)])
assert cmd_verify(args) == 0
def test_verify_full_json_broken(self, seal, temp_dir, capsys, monkeypatch):
"""--full --json on a broken chain produces structured error output."""
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 3)
chain[1].trigger.request = "tampered"
path = _write_chain_json(chain, temp_dir / "c.json")
args = _build_parser().parse_args(["verify", "--full", "--json", str(path)])
assert cmd_verify(args) == 1
out = json.loads(capsys.readouterr().out)
assert out["valid"] is False
assert len(out["errors"]) == 1
assert "hash mismatch" in out["errors"][0]["error"]
def test_verify_colored_shows_skipped_capsules(self, seal, temp_dir, capsys, monkeypatch):
"""Colored output shows skipped capsules beyond the break point."""
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 5)
chain[2].previous_hash = "broken"
path = _write_chain_json(chain, temp_dir / "c.json")
args = _build_parser().parse_args(["verify", str(path)])
cmd_verify(args)
out = capsys.readouterr().out
assert "skipped" in out
def test_verify_signatures_with_keyring(self, temp_dir, monkeypatch):
"""Signature verification using keyring via QUANTUMPIPES_DATA_DIR."""
monkeypatch.setenv("NO_COLOR", "1")
monkeypatch.setenv("QUANTUMPIPES_DATA_DIR", str(temp_dir))
key_path = temp_dir / "key"
kr = Keyring(keyring_path=temp_dir / "keyring.json", key_path=key_path)
seal_obj = Seal(key_path=key_path, keyring=kr)
chain = _make_sealed_chain(seal_obj, 3)
path = _write_chain_json(chain, temp_dir / "c.json")
args = _build_parser().parse_args(["verify", "--signatures", str(path)])
assert cmd_verify(args) == 0
# ---------------------------------------------------------------------------
# cmd_inspect
# ---------------------------------------------------------------------------
class TestCmdInspect:
def test_inspect_single_json(self, seal, temp_dir, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 1)
d = _capsule_to_full_dict(chain[0])
path = temp_dir / "single.json"
path.write_text(json.dumps(d))
args = _build_parser().parse_args(["inspect", str(path)])
assert cmd_inspect(args) == 0
out = capsys.readouterr().out
assert "Capsule Inspection" in out
assert "Trigger" in out
assert "Outcome" in out
def test_inspect_by_seq(self, seal, temp_dir, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 5)
path = _write_chain_json(chain, temp_dir / "chain.json")
args = _build_parser().parse_args(["inspect", str(path), "--seq", "2"])
assert cmd_inspect(args) == 0
out = capsys.readouterr().out
assert "Sequence: 2" in out
def test_inspect_by_id(self, seal, temp_dir, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 3)
target_id = str(chain[1].id)
path = _write_chain_json(chain, temp_dir / "chain.json")
args = _build_parser().parse_args(["inspect", str(path), "--id", target_id])
assert cmd_inspect(args) == 0
out = capsys.readouterr().out
assert target_id in out
def test_inspect_seq_not_found(self, seal, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 2)
path = _write_chain_json(chain, temp_dir / "chain.json")
args = _build_parser().parse_args(["inspect", str(path), "--seq", "99"])
assert cmd_inspect(args) == 2
def test_inspect_id_not_found(self, seal, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 2)
path = _write_chain_json(chain, temp_dir / "chain.json")
args = _build_parser().parse_args(["inspect", str(path), "--id", "nonexistent"])
assert cmd_inspect(args) == 2
def test_inspect_ambiguous_no_selector(self, seal, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 3)
path = _write_chain_json(chain, temp_dir / "chain.json")
args = _build_parser().parse_args(["inspect", str(path)])
assert cmd_inspect(args) == 2
def test_inspect_no_source(self, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
args = _build_parser().parse_args(["inspect"])
assert cmd_inspect(args) == 2
def test_inspect_bad_file(self, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
args = _build_parser().parse_args(["inspect", str(temp_dir / "gone.json")])
assert cmd_inspect(args) == 2
def test_inspect_with_outcome_error(self, seal, temp_dir, capsys, monkeypatch):
"""Inspect prints the error field when outcome has an error."""
monkeypatch.setenv("NO_COLOR", "1")
c = Capsule(trigger=TriggerSection(type="test", source="s", request="r"))
c.sequence = 0
c.previous_hash = None
c.outcome.status = "failure"
c.outcome.error = "something went wrong"
seal.seal(c)
d = _capsule_to_full_dict(c)
path = temp_dir / "err.json"
path.write_text(json.dumps(d))
args = _build_parser().parse_args(["inspect", str(path)])
assert cmd_inspect(args) == 0
out = capsys.readouterr().out
assert "something went wrong" in out
def test_inspect_from_db_by_id(self, seal, temp_dir, capsys, monkeypatch):
"""Inspect a capsule from SQLite by ID."""
monkeypatch.setenv("NO_COLOR", "1")
async def _setup_db():
from qp_capsule.storage import CapsuleStorage
db_path = temp_dir / "inspect.db"
storage = CapsuleStorage(db_path=db_path)
c = Capsule(trigger=TriggerSection(type="test", source="db", request="check"))
c.sequence = 0
c.previous_hash = None
seal.seal(c)
await storage.store(c)
await storage.close()
return db_path, str(c.id)
import asyncio
db_path, cid = asyncio.run(_setup_db())
args = _build_parser().parse_args(["inspect", "--db", str(db_path), "--id", cid])
assert cmd_inspect(args) == 0
out = capsys.readouterr().out
assert "Capsule Inspection" in out
def test_inspect_from_db_by_seq(self, seal, temp_dir, capsys, monkeypatch):
"""Inspect a capsule from SQLite by sequence number."""
monkeypatch.setenv("NO_COLOR", "1")
async def _setup_db():
from qp_capsule.storage import CapsuleStorage
db_path = temp_dir / "inspect_seq.db"
storage = CapsuleStorage(db_path=db_path)
for i in range(3):
c = Capsule(
trigger=TriggerSection(type="test", source="db", request=f"seq{i}")
)
c.sequence = i
c.previous_hash = None
if i > 0:
latest = await storage.get_latest()
c.previous_hash = latest.hash if latest else None
seal.seal(c)
await storage.store(c)
await storage.close()
return db_path
import asyncio
db_path = asyncio.run(_setup_db())
args = _build_parser().parse_args(["inspect", "--db", str(db_path), "--seq", "1"])
assert cmd_inspect(args) == 0
out = capsys.readouterr().out
assert "Sequence: 1" in out
# ---------------------------------------------------------------------------
# cmd_keys
# ---------------------------------------------------------------------------
class TestCmdKeys:
def test_keys_info_empty(self, keyring_path, key_path, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
monkeypatch.setenv("QUANTUMPIPES_DATA_DIR", str(keyring_path.parent))
args = _build_parser().parse_args(["keys", "info"])
assert cmd_keys(args) == 0
out = capsys.readouterr().out
assert "No keys" in out
def test_keys_info_with_epochs(self, key_path, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
monkeypatch.setenv("QUANTUMPIPES_DATA_DIR", str(key_path.parent))
sk = SigningKey.generate()
key_path.write_bytes(bytes(sk))
args = _build_parser().parse_args(["keys", "info"])
assert cmd_keys(args) == 0
out = capsys.readouterr().out
assert "Epoch 0" in out
assert "active" in out
def test_keys_rotate(self, key_path, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
monkeypatch.setenv("QUANTUMPIPES_DATA_DIR", str(key_path.parent))
args = _build_parser().parse_args(["keys", "rotate"])
assert cmd_keys(args) == 0
out = capsys.readouterr().out
assert "Rotation" in out
assert "active" in out
def test_keys_rotate_with_existing(self, key_path, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
monkeypatch.setenv("QUANTUMPIPES_DATA_DIR", str(key_path.parent))
sk = SigningKey.generate()
key_path.write_bytes(bytes(sk))
args = _build_parser().parse_args(["keys", "rotate"])
assert cmd_keys(args) == 0
out = capsys.readouterr().out
assert "retired" in out
def test_keys_export(self, key_path, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
monkeypatch.setenv("QUANTUMPIPES_DATA_DIR", str(key_path.parent))
sk = SigningKey.generate()
key_path.write_bytes(bytes(sk))
args = _build_parser().parse_args(["keys", "export-public"])
assert cmd_keys(args) == 0
out = capsys.readouterr().out.strip()
assert len(out) == 64
def test_keys_export_no_key(self, keyring_path, key_path, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
monkeypatch.setenv("QUANTUMPIPES_DATA_DIR", str(keyring_path.parent))
args = _build_parser().parse_args(["keys", "export-public"])
assert cmd_keys(args) == 2
def test_keys_no_subcommand(self, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
args = _build_parser().parse_args(["keys"])
assert cmd_keys(args) == 2
# ---------------------------------------------------------------------------
# cmd_hash
# ---------------------------------------------------------------------------
class TestCmdHash:
def test_hash_file(self, temp_dir, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
path = temp_dir / "doc.txt"
path.write_text("hello world")
import hashlib
expected = hashlib.sha3_256(b"hello world").hexdigest()
args = _build_parser().parse_args(["hash", str(path)])
assert cmd_hash(args) == 0
out = capsys.readouterr().out.strip()
assert out == expected
def test_hash_missing_file(self, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
args = _build_parser().parse_args(["hash", str(temp_dir / "gone.txt")])
assert cmd_hash(args) == 2
# ---------------------------------------------------------------------------
# main() integration
# ---------------------------------------------------------------------------
class TestMain:
def test_no_command(self, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
assert main([]) == 2
def test_version_flag(self, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
with pytest.raises(SystemExit, match="0"):
main(["--version"])
out = capsys.readouterr().out
assert "capsule" in out
assert "1.5.1" in out
def test_verify_via_main(self, seal, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 2)
path = _write_chain_json(chain, temp_dir / "c.json")
assert main(["verify", "--quiet", str(path)]) == 0
def test_hash_via_main(self, temp_dir, capsys, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
path = temp_dir / "f.bin"
path.write_bytes(b"\x00\x01\x02")
assert main(["hash", str(path)]) == 0
# ---------------------------------------------------------------------------
# ANSI color support detection
# ---------------------------------------------------------------------------
class TestDefaultKeyringPath:
def test_default_keyring_path_without_env(self, monkeypatch):
monkeypatch.delenv("QUANTUMPIPES_DATA_DIR", raising=False)
from qp_capsule.paths import default_keyring_path
p = default_keyring_path()
assert str(p).endswith("keyring.json")
assert ".quantumpipes" in str(p)
class TestColorSupport:
def test_no_color_env(self, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
assert _supports_color(StringIO()) is False
def test_force_color_env(self, monkeypatch):
monkeypatch.delenv("NO_COLOR", raising=False)
monkeypatch.setenv("FORCE_COLOR", "1")
assert _supports_color(StringIO()) is True
def test_non_tty(self, monkeypatch):
monkeypatch.delenv("NO_COLOR", raising=False)
monkeypatch.delenv("FORCE_COLOR", raising=False)
assert _supports_color(StringIO()) is False
def test_ansi_no_color_passthrough(self):
"""_c() returns plain text when _NO_COLOR is True."""
import qp_capsule.cli as cli_mod
old = cli_mod._NO_COLOR
cli_mod._NO_COLOR = True
try:
assert cli_mod._c("32", "hello") == "hello"
assert cli_mod._green("ok") == "ok"
assert cli_mod._red("err") == "err"
assert cli_mod._bold("b") == "b"
assert cli_mod._dim("d") == "d"
assert cli_mod._yellow("y") == "y"
finally:
cli_mod._NO_COLOR = old
# ---------------------------------------------------------------------------
# Seal + Keyring integration via CLI
# ---------------------------------------------------------------------------
class TestVerifyOutput:
"""Test verify output formatting details."""
def test_level_description_in_output(self, seal, temp_dir, capsys, monkeypatch):
"""Verify output includes the human-readable level description."""
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 2)
path = _write_chain_json(chain, temp_dir / "c.json")
args = _build_parser().parse_args(["verify", "--full", str(path)])
cmd_verify(args)
out = capsys.readouterr().out
assert "SHA3-256 recomputation" in out
def test_structural_description_in_output(self, seal, temp_dir, capsys, monkeypatch):
"""Default structural level shows its description."""
monkeypatch.setenv("NO_COLOR", "1")
chain = _make_sealed_chain(seal, 1)
path = _write_chain_json(chain, temp_dir / "c.json")
args = _build_parser().parse_args(["verify", str(path)])
cmd_verify(args)
out = capsys.readouterr().out
assert "sequence + hash linkage" in out
def test_empty_chain_json(self, temp_dir, capsys, monkeypatch):
"""--json on an empty chain produces valid JSON with 0 capsules."""
monkeypatch.setenv("NO_COLOR", "1")
path = temp_dir / "empty.json"
path.write_text("[]")
args = _build_parser().parse_args(["verify", "--json", str(path)])
assert cmd_verify(args) == 0
out = json.loads(capsys.readouterr().out)
assert out["valid"] is True
assert out["capsules_verified"] == 0
assert out["total_capsules"] == 0
def test_signatures_without_seal_skips_sigs(self, seal):
"""verify_chain(level='signatures') without seal= skips signature checks gracefully."""
chain = _make_sealed_chain(seal, 3)
r = verify_chain(chain, level="signatures", seal=None)
assert r.valid is True
assert r.capsules_verified == 3
class TestKeyringVerification:
"""Test that signature verification works across key rotations."""
def test_verify_with_keyring(self, temp_dir, monkeypatch):
monkeypatch.setenv("NO_COLOR", "1")
key_path = temp_dir / "key"
kr_path = temp_dir / "keyring.json"
kr = Keyring(keyring_path=kr_path, key_path=key_path)
seal_obj = Seal(key_path=key_path, keyring=kr)
chain = _make_sealed_chain(seal_obj, 3)
r = verify_chain(chain, level="signatures", seal=seal_obj)
assert r.valid is True
def test_verify_across_rotation(self, temp_dir, monkeypatch):
"""Capsules signed with old key verify after rotation."""
monkeypatch.setenv("NO_COLOR", "1")
key_path = temp_dir / "key"
kr_path = temp_dir / "keyring.json"
kr = Keyring(keyring_path=kr_path, key_path=key_path)
seal_obj = Seal(key_path=key_path, keyring=kr)
c0 = Capsule(trigger=TriggerSection(type="test", source="s", request="before rotation"))
c0.sequence = 0
c0.previous_hash = None
seal_obj.seal(c0)
kr.rotate()
seal_obj2 = Seal(key_path=key_path, keyring=kr)
c1 = Capsule(trigger=TriggerSection(type="test", source="s", request="after rotation"))
c1.sequence = 1
c1.previous_hash = c0.hash
seal_obj2.seal(c1)
r = verify_chain([c0, c1], level="signatures", seal=seal_obj2)
assert r.valid is True
assert r.capsules_verified == 2