@7h3/protocol 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (294) hide show
  1. package/README.md +6 -1317
  2. package/agentAdapter.d.ts +26 -0
  3. package/auditLog.d.ts +54 -0
  4. package/capability.d.ts +67 -0
  5. package/cborCodec.d.ts +34 -0
  6. package/conformanceVectors.d.ts +20 -0
  7. package/encryption.d.ts +85 -0
  8. package/envelopeCbor.d.ts +33 -0
  9. package/frameworkAdapters.d.ts +72 -0
  10. package/gateway.d.ts +53 -0
  11. package/grpcBinding.d.ts +29 -0
  12. package/httpBinding.d.ts +50 -0
  13. package/index.d.ts +39 -0
  14. package/index.js +3662 -0
  15. package/keyInfra.d.ts +64 -0
  16. package/keyRegistry.d.ts +9 -0
  17. package/keyRotation.d.ts +20 -0
  18. package/mcpGateway.d.ts +37 -0
  19. package/mcpTransports.d.ts +62 -0
  20. package/mcpWrapper.d.ts +83 -0
  21. package/otel.d.ts +65 -0
  22. package/package.json +13 -121
  23. package/policyEnforcer.d.ts +50 -0
  24. package/policyTelemetryFeedback.d.ts +11 -0
  25. package/{src/protocol.d.ts → protocol.d.ts} +1 -2
  26. package/protocolAgent.d.ts +58 -0
  27. package/protocolBinary.d.ts +8 -0
  28. package/protocolCapabilities.d.ts +24 -0
  29. package/protocolReplay.d.ts +35 -0
  30. package/protocolTransport.d.ts +73 -0
  31. package/queueBinding.d.ts +43 -0
  32. package/rateLimiter.d.ts +18 -0
  33. package/{src/redisClient.ts → redisClient.d.ts} +25 -75
  34. package/replayStores.d.ts +98 -0
  35. package/revocation.d.ts +71 -0
  36. package/routePolicy.d.ts +30 -0
  37. package/runtimePolicy.d.ts +24 -0
  38. package/runtimePolicyManager.d.ts +15 -0
  39. package/runtimePolicyPresets.d.ts +11 -0
  40. package/signedResponse.d.ts +21 -0
  41. package/stream.d.ts +59 -0
  42. package/telemetry.d.ts +79 -0
  43. package/webhookBinding.d.ts +30 -0
  44. package/wsBinding.d.ts +52 -0
  45. package/.dockerignore +0 -19
  46. package/.github/dependabot.yml +0 -32
  47. package/.github/workflows/ci.yml +0 -31
  48. package/.github/workflows/publish.yml +0 -59
  49. package/.github/workflows/scorecard.yml +0 -37
  50. package/7h3.example.yaml +0 -125
  51. package/CHANGELOG.md +0 -152
  52. package/CONTRIBUTING.md +0 -82
  53. package/Dockerfile +0 -73
  54. package/GOVERNANCE.md +0 -62
  55. package/SECURITY.md +0 -70
  56. package/bench-results/replay-cache-full-1777891033256.json +0 -10
  57. package/bench-results/replay-cache-full-1777896317488.json +0 -10
  58. package/bench-results/replay-cache-full-1777900993184.json +0 -10
  59. package/bench-results/replay-cache-full-1777901019285.json +0 -10
  60. package/bench-results/replay-cache-quick-1777870170126.json +0 -10
  61. package/bench-results/signature-profiles-quick-1775875160079.json +0 -85
  62. package/bench-results/signature-profiles-quick-1775983539716.json +0 -85
  63. package/bench-results/signature-profiles-quick-1776237913190.json +0 -85
  64. package/bench-results/wire-codecs-full-1777891019803.json +0 -93
  65. package/bench-results/wire-codecs-full-1777896260964.json +0 -93
  66. package/bench-results/wire-codecs-full-1777901004247.json +0 -93
  67. package/bench-results/wire-codecs-quick-1775972879056.json +0 -93
  68. package/bench-results/wire-codecs-quick-1775983541111.json +0 -93
  69. package/bench-results/wire-codecs-quick-1776237914299.json +0 -93
  70. package/bench-results/wire-codecs-quick-1777841285236.json +0 -93
  71. package/bench-results/wire-codecs-quick-1777841321772.json +0 -93
  72. package/bench-results/wire-codecs-quick-1777841330408.json +0 -93
  73. package/bench-results/wire-codecs-quick-1777852886082.json +0 -93
  74. package/bench-results/wire-codecs-quick-1777852988773.json +0 -93
  75. package/bench-results/wire-codecs-quick-1777870188095.json +0 -93
  76. package/bench-results/wire-codecs-quick-1777870263918.json +0 -93
  77. package/bench-results/wire-codecs-quick-1777870455034.json +0 -93
  78. package/bench-results/wire-codecs-quick-1778816163081.json +0 -93
  79. package/bench-results/wire-codecs-quick-1778843936130.json +0 -93
  80. package/bin/7h3.ts +0 -406
  81. package/conformance/7h3_v0_1.json +0 -77
  82. package/conformance/7h3_v0_1_binary.json +0 -20
  83. package/conformance/aip_v0_1_binary.json +0 -20
  84. package/docker-compose.yaml +0 -77
  85. package/docs/ADOPTION_PLAN.md +0 -120
  86. package/docs/AGENTS.md +0 -77
  87. package/docs/AIP_RFC_v0.1.md +0 -97
  88. package/docs/AI_DECISION_CARD.md +0 -122
  89. package/docs/AI_RUNTIME_POLICY.json +0 -126
  90. package/docs/AI_RUNTIME_POLICY.yaml +0 -110
  91. package/docs/BACKPRESSURE_TUNING.md +0 -65
  92. package/docs/BENCHMARK_CLAIM_MATRIX.md +0 -42
  93. package/docs/BENCHMARK_REPORT_TEMPLATE.md +0 -169
  94. package/docs/BINARY_CODEC_BENCH.md +0 -23
  95. package/docs/CLEAN_CLONE_RUNBOOK.md +0 -36
  96. package/docs/CLOCK_SKEW_POLICY.md +0 -30
  97. package/docs/DISTRIBUTED_REPLAY.md +0 -142
  98. package/docs/FUZZ_CAMPAIGN.md +0 -121
  99. package/docs/GATEWAY.md +0 -195
  100. package/docs/KEY_MANAGEMENT_POLICY.md +0 -53
  101. package/docs/KEY_REVOCATION.md +0 -69
  102. package/docs/MCP_WRAPPER.md +0 -159
  103. package/docs/MIGRATION_GUIDE.md +0 -40
  104. package/docs/OPERATORS.md +0 -184
  105. package/docs/PERF_REGRESSION_POLICY.md +0 -34
  106. package/docs/PROJECT_EXAMINATION_2026-05-31.md +0 -219
  107. package/docs/RELEASE_BENCHMARK_REPORT_2026-05-15.md +0 -135
  108. package/docs/RELEASE_GATE.md +0 -25
  109. package/docs/RELEASE_NOTES_v0.1.0.md +0 -54
  110. package/docs/SECURITY_REVIEW_2026-06-05.md +0 -165
  111. package/docs/TELEMETRY.md +0 -41
  112. package/docs/THREAT_MODEL.md +0 -89
  113. package/docs/VERSIONING_POLICY.md +0 -30
  114. package/docs/assets/banner-github.png +0 -0
  115. package/docs/assets/banner.png +0 -0
  116. package/docs/assets/banner.svg +0 -123
  117. package/eslint.config.js +0 -15
  118. package/fuzz/ts/harness-decode.ts +0 -136
  119. package/fuzz/ts/harness-verify.ts +0 -121
  120. package/fuzz/ts/run.ts +0 -35
  121. package/mcp-server/README.md +0 -38
  122. package/mcp-server/package-lock.json +0 -1187
  123. package/mcp-server/package.json +0 -35
  124. package/mcp-server/src/index.ts +0 -236
  125. package/mcp-server/tsconfig.json +0 -14
  126. package/scripts/aip-framework-quickstart.ts +0 -110
  127. package/scripts/aip-mcp-gateway.ts +0 -38
  128. package/scripts/aip-mcp-wrap-demo.ts +0 -72
  129. package/scripts/aip-quickstart.ts +0 -60
  130. package/scripts/bench-diff.ts +0 -118
  131. package/scripts/bench-protocol-e2e.ts +0 -937
  132. package/scripts/bench-protocol-openloop.ts +0 -1397
  133. package/scripts/bench-replay-cache.ts +0 -76
  134. package/scripts/bench-signature-profiles.ts +0 -180
  135. package/scripts/bench-wire-codecs.ts +0 -161
  136. package/scripts/build-binary-conformance.ts +0 -36
  137. package/scripts/build-release-dashboard.ts +0 -175
  138. package/scripts/canary-rollout.ts +0 -38
  139. package/scripts/mcpGatewayCli.test.ts +0 -116
  140. package/scripts/prepare-aip-package.ts +0 -88
  141. package/scripts/regen-conformance-sigs.ts +0 -18
  142. package/scripts/release-gate.ts +0 -19
  143. package/scripts/validate-runtime-policy.ts +0 -18
  144. package/sdk/browser/index.test.ts +0 -162
  145. package/sdk/browser/index.ts +0 -257
  146. package/sdk/browser/package.json +0 -13
  147. package/sdk/go/cbor.go +0 -551
  148. package/sdk/go/cbor_test.go +0 -232
  149. package/sdk/go/encryption.go +0 -280
  150. package/sdk/go/encryption_test.go +0 -318
  151. package/sdk/go/go.mod +0 -7
  152. package/sdk/go/go.sum +0 -4
  153. package/sdk/go/http.go +0 -135
  154. package/sdk/go/protocol.go +0 -324
  155. package/sdk/go/protocol_test.go +0 -334
  156. package/sdk/go/replay.go +0 -121
  157. package/sdk/go/replay_test.go +0 -149
  158. package/sdk/go/webhook.go +0 -136
  159. package/sdk/pq/package-lock.json +0 -1358
  160. package/sdk/pq/package.json +0 -42
  161. package/sdk/pq/src/index.test.ts +0 -143
  162. package/sdk/pq/src/index.ts +0 -166
  163. package/sdk/pq/tsconfig.json +0 -14
  164. package/sdk/pq/vitest.config.ts +0 -7
  165. package/sdk/python/README.md +0 -18
  166. package/sdk/python/protocol_7h3/__init__.py +0 -46
  167. package/sdk/python/protocol_7h3/encryption.py +0 -252
  168. package/sdk/python/protocol_7h3/http.py +0 -212
  169. package/sdk/python/protocol_7h3/keys.py +0 -149
  170. package/sdk/python/protocol_7h3/pq.py +0 -244
  171. package/sdk/python/protocol_7h3/protocol.py +0 -525
  172. package/sdk/python/protocol_7h3/queue.py +0 -118
  173. package/sdk/python/protocol_7h3/replay.py +0 -98
  174. package/sdk/python/protocol_7h3/webhook.py +0 -116
  175. package/sdk/python/pyproject.toml +0 -40
  176. package/sdk/python/tests/test_conformance.py +0 -110
  177. package/sdk/python/tests/test_encryption.py +0 -206
  178. package/sdk/python/tests/test_http.py +0 -305
  179. package/sdk/python/tests/test_keys.py +0 -417
  180. package/sdk/python/tests/test_queue.py +0 -120
  181. package/sdk/python/tests/test_webhook.py +0 -345
  182. package/sdk/rust/Cargo.lock +0 -371
  183. package/sdk/rust/Cargo.toml +0 -25
  184. package/sdk/rust/README.md +0 -31
  185. package/sdk/rust/fuzz/Cargo.toml +0 -29
  186. package/sdk/rust/fuzz/fuzz_targets/fuzz_canonicalize.rs +0 -46
  187. package/sdk/rust/fuzz/fuzz_targets/fuzz_decode.rs +0 -11
  188. package/sdk/rust/src/bin/aip_mcp_gateway.rs +0 -59
  189. package/sdk/rust/src/http.rs +0 -145
  190. package/sdk/rust/src/keys.rs +0 -161
  191. package/sdk/rust/src/lib.rs +0 -688
  192. package/sdk/rust/src/queue.rs +0 -79
  193. package/sdk/rust/src/webhook.rs +0 -86
  194. package/sdk/rust/tests/conformance.rs +0 -148
  195. package/sdk/rust/tests/gateway.rs +0 -130
  196. package/sdk/rust/tests/http_webhook_queue.rs +0 -201
  197. package/sdk/rust/tests/keys.rs +0 -189
  198. package/sdk/threshold/index.d.ts +0 -68
  199. package/sdk/threshold/index.d.ts.map +0 -1
  200. package/sdk/threshold/index.js +0 -254
  201. package/sdk/threshold/package-lock.json +0 -1361
  202. package/sdk/threshold/package.json +0 -39
  203. package/sdk/threshold/src/index.d.ts +0 -68
  204. package/sdk/threshold/src/index.d.ts.map +0 -1
  205. package/sdk/threshold/src/index.js +0 -254
  206. package/sdk/threshold/src/index.test.ts +0 -238
  207. package/sdk/threshold/src/index.ts +0 -355
  208. package/sdk/threshold/tsconfig.json +0 -19
  209. package/sdk/threshold/vitest.config.ts +0 -12
  210. package/src/agentAdapter.test.ts +0 -48
  211. package/src/agentAdapter.ts +0 -56
  212. package/src/auditLog.test.ts +0 -145
  213. package/src/auditLog.ts +0 -147
  214. package/src/capability.test.ts +0 -504
  215. package/src/capability.ts +0 -380
  216. package/src/cborCodec.test.ts +0 -263
  217. package/src/cborCodec.ts +0 -339
  218. package/src/conformance.test.ts +0 -136
  219. package/src/conformanceVectors.ts +0 -99
  220. package/src/encryption.test.ts +0 -206
  221. package/src/encryption.ts +0 -245
  222. package/src/envelopeCbor.ts +0 -140
  223. package/src/frameworkAdapters.test.ts +0 -290
  224. package/src/frameworkAdapters.ts +0 -261
  225. package/src/gateway.test.ts +0 -343
  226. package/src/gateway.ts +0 -246
  227. package/src/grpcBinding.test.ts +0 -211
  228. package/src/grpcBinding.ts +0 -103
  229. package/src/httpBinding.test.ts +0 -376
  230. package/src/httpBinding.ts +0 -189
  231. package/src/index.ts +0 -39
  232. package/src/keyInfra.test.ts +0 -278
  233. package/src/keyInfra.ts +0 -228
  234. package/src/keyRegistry.ts +0 -59
  235. package/src/keyRotation.test.ts +0 -78
  236. package/src/keyRotation.ts +0 -72
  237. package/src/mcpGateway.test.ts +0 -129
  238. package/src/mcpGateway.ts +0 -250
  239. package/src/mcpTransports.test.ts +0 -92
  240. package/src/mcpTransports.ts +0 -169
  241. package/src/mcpWrapper.test.ts +0 -179
  242. package/src/mcpWrapper.ts +0 -206
  243. package/src/otel.ts +0 -136
  244. package/src/policyEnforcer.test.ts +0 -99
  245. package/src/policyEnforcer.ts +0 -169
  246. package/src/policyTelemetryFeedback.test.ts +0 -25
  247. package/src/policyTelemetryFeedback.ts +0 -38
  248. package/src/protocol.bench.ts +0 -37
  249. package/src/protocol.d.ts.map +0 -1
  250. package/src/protocol.js +0 -294
  251. package/src/protocol.test.ts +0 -155
  252. package/src/protocol.ts +0 -414
  253. package/src/protocolAgent.test.ts +0 -105
  254. package/src/protocolAgent.ts +0 -169
  255. package/src/protocolBinary.test.ts +0 -165
  256. package/src/protocolBinary.ts +0 -312
  257. package/src/protocolCapabilities.ts +0 -70
  258. package/src/protocolFuzz.advanced.test.ts +0 -235
  259. package/src/protocolFuzz.test.ts +0 -111
  260. package/src/protocolNegative.test.ts +0 -97
  261. package/src/protocolReplay.test.ts +0 -71
  262. package/src/protocolReplay.ts +0 -194
  263. package/src/protocolTransport.test.ts +0 -556
  264. package/src/protocolTransport.ts +0 -483
  265. package/src/queueBinding.test.ts +0 -130
  266. package/src/queueBinding.ts +0 -102
  267. package/src/rateLimiter.test.ts +0 -96
  268. package/src/rateLimiter.ts +0 -46
  269. package/src/redisIntegration.test.ts +0 -134
  270. package/src/replayStores.test.ts +0 -273
  271. package/src/replayStores.ts +0 -215
  272. package/src/revocation.test.ts +0 -98
  273. package/src/revocation.ts +0 -0
  274. package/src/routePolicy.test.ts +0 -87
  275. package/src/routePolicy.ts +0 -72
  276. package/src/runtimePolicy.test.ts +0 -49
  277. package/src/runtimePolicy.ts +0 -81
  278. package/src/runtimePolicyManager.test.ts +0 -29
  279. package/src/runtimePolicyManager.ts +0 -50
  280. package/src/runtimePolicyPresets.ts +0 -43
  281. package/src/signedResponse.test.ts +0 -111
  282. package/src/signedResponse.ts +0 -83
  283. package/src/stream.test.ts +0 -254
  284. package/src/stream.ts +0 -417
  285. package/src/telemetry.test.ts +0 -251
  286. package/src/telemetry.ts +0 -299
  287. package/src/webhookBinding.test.ts +0 -144
  288. package/src/webhookBinding.ts +0 -115
  289. package/src/wsBinding.test.ts +0 -221
  290. package/src/wsBinding.ts +0 -200
  291. package/tsconfig.json +0 -15
  292. package/tsconfig.lib.json +0 -23
  293. package/vite.lib.config.ts +0 -16
  294. package/vitest.config.ts +0 -11
@@ -1,417 +0,0 @@
1
- """Tests for protocol_7h3.keys — key infrastructure."""
2
- from __future__ import annotations
3
-
4
- import json
5
- import time
6
- import unittest
7
-
8
- from protocol_7h3.keys import (
9
- KeyEntry,
10
- KeyRotationManager,
11
- ManagedKeyPair,
12
- RevocationRegistry,
13
- WellKnownKeysDocument,
14
- )
15
- from protocol_7h3.http import StaticKeyRegistry
16
-
17
-
18
- class TestWellKnownKeysDocumentRoundTrip(unittest.TestCase):
19
- """Test 1: WellKnownKeysDocument.to_json / from_json round trip."""
20
-
21
- def test_round_trip_basic(self):
22
- now = int(time.time() * 1000)
23
- original = WellKnownKeysDocument(
24
- version="7h3/0.1",
25
- updated=now,
26
- keys=[
27
- KeyEntry(
28
- id="key-abc",
29
- algorithm="Ed25519",
30
- public_key="MCowBQYDK2VwAyEAAABBBBCCDDEEFF==",
31
- created=now - 1000,
32
- ),
33
- KeyEntry(
34
- id="key-def",
35
- algorithm="Ed25519",
36
- public_key="MCowBQYDK2VwAyEA11223344==",
37
- created=now - 500,
38
- expires=now + 3600000,
39
- ),
40
- ],
41
- )
42
-
43
- serialized = original.to_json()
44
- parsed = json.loads(serialized)
45
-
46
- # Check top-level structure
47
- self.assertEqual(parsed["version"], "7h3/0.1")
48
- self.assertEqual(parsed["updated"], now)
49
- self.assertEqual(len(parsed["keys"]), 2)
50
-
51
- # Deserialize
52
- restored = WellKnownKeysDocument.from_json(serialized)
53
- self.assertEqual(restored.version, original.version)
54
- self.assertEqual(restored.updated, original.updated)
55
- self.assertEqual(len(restored.keys), 2)
56
-
57
- k0 = restored.keys[0]
58
- self.assertEqual(k0.id, "key-abc")
59
- self.assertEqual(k0.algorithm, "Ed25519")
60
- self.assertEqual(k0.public_key, "MCowBQYDK2VwAyEAAABBBBCCDDEEFF==")
61
- self.assertEqual(k0.created, now - 1000)
62
- self.assertIsNone(k0.expires)
63
- self.assertFalse(k0.revoked)
64
-
65
- k1 = restored.keys[1]
66
- self.assertEqual(k1.id, "key-def")
67
- self.assertEqual(k1.expires, now + 3600000)
68
-
69
- def test_round_trip_with_revoked_key(self):
70
- now = int(time.time() * 1000)
71
- original = WellKnownKeysDocument(
72
- version="7h3/0.1",
73
- updated=now,
74
- keys=[
75
- KeyEntry(
76
- id="key-revoked",
77
- algorithm="Ed25519",
78
- public_key="MCowBQYDK2VwAyEA==",
79
- created=now - 10000,
80
- revoked=True,
81
- revoked_at=now - 5000,
82
- ),
83
- ],
84
- )
85
-
86
- serialized = original.to_json()
87
- restored = WellKnownKeysDocument.from_json(serialized)
88
-
89
- k = restored.keys[0]
90
- self.assertTrue(k.revoked)
91
- self.assertEqual(k.revoked_at, now - 5000)
92
-
93
- def test_to_json_omits_optional_fields_when_absent(self):
94
- now = int(time.time() * 1000)
95
- doc = WellKnownKeysDocument(
96
- version="7h3/0.1",
97
- updated=now,
98
- keys=[
99
- KeyEntry(
100
- id="key-minimal",
101
- algorithm="Ed25519",
102
- public_key="abc123==",
103
- created=now,
104
- ),
105
- ],
106
- )
107
- serialized = doc.to_json()
108
- parsed = json.loads(serialized)
109
- k = parsed["keys"][0]
110
- self.assertNotIn("expires", k)
111
- self.assertNotIn("revoked", k)
112
- self.assertNotIn("revokedAt", k)
113
-
114
-
115
- class TestStaticKeyRegistry(unittest.TestCase):
116
- """Test 2: StaticKeyRegistry (from http.py) get_public_key."""
117
-
118
- def test_get_public_key_returns_registered_key(self):
119
- registry = StaticKeyRegistry({"agent-alice": "MCowBQYDK2VwAyEAAlice=="})
120
- result = registry.get_public_key("agent-alice")
121
- self.assertEqual(result, "MCowBQYDK2VwAyEAAlice==")
122
-
123
- def test_get_public_key_returns_none_for_unknown(self):
124
- registry = StaticKeyRegistry({"agent-alice": "MCowBQYDK2VwAyEAAlice=="})
125
- result = registry.get_public_key("agent-unknown")
126
- self.assertIsNone(result)
127
-
128
- def test_get_public_key_empty_registry(self):
129
- registry = StaticKeyRegistry({})
130
- self.assertIsNone(registry.get_public_key("any-id"))
131
-
132
- def test_get_public_key_multiple_keys(self):
133
- keys = {
134
- "alice": "pub-alice",
135
- "bob": "pub-bob",
136
- "carol": "pub-carol",
137
- }
138
- registry = StaticKeyRegistry(keys)
139
- self.assertEqual(registry.get_public_key("alice"), "pub-alice")
140
- self.assertEqual(registry.get_public_key("bob"), "pub-bob")
141
- self.assertEqual(registry.get_public_key("carol"), "pub-carol")
142
-
143
-
144
- class TestRevocationRegistryRevokeAndIsRevoked(unittest.TestCase):
145
- """Test 3: RevocationRegistry: revoke + is_revoked."""
146
-
147
- def test_revoke_marks_key_as_revoked(self):
148
- reg = RevocationRegistry()
149
- reg.revoke("key-abc")
150
- self.assertTrue(reg.is_revoked("key-abc"))
151
-
152
- def test_is_revoked_returns_false_for_unrevo_key(self):
153
- reg = RevocationRegistry()
154
- reg.revoke("key-abc")
155
- self.assertFalse(reg.is_revoked("key-xyz"))
156
-
157
- def test_revoke_with_reason(self):
158
- reg = RevocationRegistry()
159
- reg.revoke("key-compromised", reason="private key leak")
160
- self.assertTrue(reg.is_revoked("key-compromised"))
161
-
162
- def test_revoke_multiple_keys(self):
163
- reg = RevocationRegistry()
164
- reg.revoke("key-1")
165
- reg.revoke("key-2")
166
- self.assertTrue(reg.is_revoked("key-1"))
167
- self.assertTrue(reg.is_revoked("key-2"))
168
- self.assertFalse(reg.is_revoked("key-3"))
169
-
170
- def test_empty_registry_returns_false(self):
171
- reg = RevocationRegistry()
172
- self.assertFalse(reg.is_revoked("key-anything"))
173
-
174
-
175
- class TestRevocationRegistryGetList(unittest.TestCase):
176
- """Test 4: RevocationRegistry: get_list returns correct structure."""
177
-
178
- def test_get_list_structure_when_empty(self):
179
- reg = RevocationRegistry()
180
- result = reg.get_list()
181
- self.assertEqual(result["version"], "7h3/0.1")
182
- self.assertIn("updated", result)
183
- self.assertIsInstance(result["updated"], int)
184
- self.assertEqual(result["revokedKeys"], [])
185
-
186
- def test_get_list_contains_revoked_key(self):
187
- reg = RevocationRegistry()
188
- before = int(time.time() * 1000)
189
- reg.revoke("key-foo", reason="expired")
190
- after = int(time.time() * 1000)
191
-
192
- result = reg.get_list()
193
- self.assertEqual(result["version"], "7h3/0.1")
194
- self.assertEqual(len(result["revokedKeys"]), 1)
195
-
196
- entry = result["revokedKeys"][0]
197
- self.assertEqual(entry["id"], "key-foo")
198
- self.assertGreaterEqual(entry["revokedAt"], before)
199
- self.assertLessEqual(entry["revokedAt"], after)
200
- self.assertEqual(entry["reason"], "expired")
201
-
202
- def test_get_list_omits_reason_when_none(self):
203
- reg = RevocationRegistry()
204
- reg.revoke("key-noreaso")
205
- result = reg.get_list()
206
- entry = result["revokedKeys"][0]
207
- self.assertNotIn("reason", entry)
208
-
209
- def test_get_list_multiple_revoked(self):
210
- reg = RevocationRegistry()
211
- reg.revoke("key-a")
212
- reg.revoke("key-b")
213
- result = reg.get_list()
214
- ids = {e["id"] for e in result["revokedKeys"]}
215
- self.assertIn("key-a", ids)
216
- self.assertIn("key-b", ids)
217
-
218
-
219
- class TestRevocationRegistryImportList(unittest.TestCase):
220
- """Test 5: RevocationRegistry: import_list merges entries."""
221
-
222
- def test_import_list_adds_entries(self):
223
- reg = RevocationRegistry()
224
- revocation_list = {
225
- "version": "7h3/0.1",
226
- "updated": int(time.time() * 1000),
227
- "revokedKeys": [
228
- {"id": "key-remote-1", "revokedAt": 1700000000000},
229
- {"id": "key-remote-2", "revokedAt": 1700000001000, "reason": "compromised"},
230
- ],
231
- }
232
- reg.import_list(revocation_list)
233
-
234
- self.assertTrue(reg.is_revoked("key-remote-1"))
235
- self.assertTrue(reg.is_revoked("key-remote-2"))
236
-
237
- def test_import_list_does_not_overwrite_existing(self):
238
- reg = RevocationRegistry()
239
- local_revoked_at = int(time.time() * 1000)
240
- reg.revoke("key-shared")
241
-
242
- revocation_list = {
243
- "revokedKeys": [
244
- {"id": "key-shared", "revokedAt": 1000000000000, "reason": "remote"},
245
- ]
246
- }
247
- reg.import_list(revocation_list)
248
-
249
- # Should still be revoked; original entry should be kept (not overwritten)
250
- self.assertTrue(reg.is_revoked("key-shared"))
251
- # Check that the local timestamp is preserved (not overwritten by remote)
252
- result = reg.get_list()
253
- entry = next(e for e in result["revokedKeys"] if e["id"] == "key-shared")
254
- self.assertGreaterEqual(entry["revokedAt"], local_revoked_at - 100)
255
-
256
- def test_import_list_merges_new_and_existing(self):
257
- reg = RevocationRegistry()
258
- reg.revoke("key-existing")
259
-
260
- revocation_list = {
261
- "revokedKeys": [
262
- {"id": "key-new-from-remote", "revokedAt": 1700000000000},
263
- ]
264
- }
265
- reg.import_list(revocation_list)
266
-
267
- self.assertTrue(reg.is_revoked("key-existing"))
268
- self.assertTrue(reg.is_revoked("key-new-from-remote"))
269
-
270
- def test_import_empty_list(self):
271
- reg = RevocationRegistry()
272
- reg.import_list({"revokedKeys": []})
273
- result = reg.get_list()
274
- self.assertEqual(result["revokedKeys"], [])
275
-
276
-
277
- class TestKeyRotationManagerAddKeyGetCurrentKey(unittest.TestCase):
278
- """Test 6: KeyRotationManager: add_key + get_current_key returns newest."""
279
-
280
- def test_get_current_key_returns_none_when_empty(self):
281
- mgr = KeyRotationManager()
282
- self.assertIsNone(mgr.get_current_key())
283
-
284
- def test_get_current_key_returns_added_key(self):
285
- mgr = KeyRotationManager()
286
- now = int(time.time() * 1000)
287
- pair = ManagedKeyPair(
288
- id="key-1", public_key="pub-1", private_key="priv-1", created=now
289
- )
290
- mgr.add_key(pair)
291
- result = mgr.get_current_key()
292
- self.assertIsNotNone(result)
293
- self.assertEqual(result.id, "key-1")
294
-
295
- def test_get_current_key_returns_newest_of_multiple(self):
296
- mgr = KeyRotationManager()
297
- now = int(time.time() * 1000)
298
- old_pair = ManagedKeyPair(
299
- id="key-old", public_key="pub-old", private_key="priv-old",
300
- created=now - 10000
301
- )
302
- new_pair = ManagedKeyPair(
303
- id="key-new", public_key="pub-new", private_key="priv-new",
304
- created=now
305
- )
306
- mgr.add_key(old_pair)
307
- mgr.add_key(new_pair)
308
-
309
- result = mgr.get_current_key()
310
- self.assertEqual(result.id, "key-new")
311
-
312
- def test_get_current_key_excludes_expired(self):
313
- mgr = KeyRotationManager()
314
- now = int(time.time() * 1000)
315
- expired_pair = ManagedKeyPair(
316
- id="key-expired", public_key="pub-exp", private_key="priv-exp",
317
- created=now - 10000,
318
- expires_at=now - 1000, # already expired
319
- )
320
- active_pair = ManagedKeyPair(
321
- id="key-active", public_key="pub-act", private_key="priv-act",
322
- created=now - 5000,
323
- expires_at=now + 3600000,
324
- )
325
- mgr.add_key(expired_pair)
326
- mgr.add_key(active_pair)
327
-
328
- result = mgr.get_current_key()
329
- self.assertEqual(result.id, "key-active")
330
-
331
- def test_get_current_key_none_when_all_expired(self):
332
- mgr = KeyRotationManager()
333
- now = int(time.time() * 1000)
334
- expired = ManagedKeyPair(
335
- id="key-exp", public_key="pub", private_key="priv",
336
- created=now - 10000,
337
- expires_at=now - 1000,
338
- )
339
- mgr.add_key(expired)
340
- self.assertIsNone(mgr.get_current_key())
341
-
342
-
343
- class TestKeyRotationManagerGetWellKnownDocument(unittest.TestCase):
344
- """Test 7: KeyRotationManager: get_well_known_document returns correct format."""
345
-
346
- def test_well_known_document_empty(self):
347
- mgr = KeyRotationManager()
348
- doc = mgr.get_well_known_document()
349
- self.assertEqual(doc.version, "7h3/0.1")
350
- self.assertIsInstance(doc.updated, int)
351
- self.assertEqual(doc.keys, [])
352
-
353
- def test_well_known_document_with_keys(self):
354
- mgr = KeyRotationManager()
355
- now = int(time.time() * 1000)
356
- pair1 = ManagedKeyPair(
357
- id="key-1", public_key="pub-spki-1==", private_key="priv-pkcs8-1==",
358
- created=now - 1000,
359
- )
360
- pair2 = ManagedKeyPair(
361
- id="key-2", public_key="pub-spki-2==", private_key="priv-pkcs8-2==",
362
- created=now,
363
- expires_at=now + 3600000,
364
- )
365
- mgr.add_key(pair1)
366
- mgr.add_key(pair2)
367
-
368
- doc = mgr.get_well_known_document()
369
- self.assertEqual(doc.version, "7h3/0.1")
370
- self.assertEqual(len(doc.keys), 2)
371
-
372
- ids = {k.id for k in doc.keys}
373
- self.assertIn("key-1", ids)
374
- self.assertIn("key-2", ids)
375
-
376
- for k in doc.keys:
377
- self.assertEqual(k.algorithm, "Ed25519")
378
- self.assertIsNotNone(k.public_key)
379
-
380
- def test_well_known_document_is_serializable(self):
381
- mgr = KeyRotationManager()
382
- now = int(time.time() * 1000)
383
- mgr.add_key(ManagedKeyPair(
384
- id="key-serial", public_key="pub-spki==", private_key="priv==",
385
- created=now,
386
- ))
387
-
388
- doc = mgr.get_well_known_document()
389
- serialized = doc.to_json()
390
- parsed = json.loads(serialized)
391
-
392
- self.assertEqual(parsed["version"], "7h3/0.1")
393
- self.assertIn("updated", parsed)
394
- self.assertEqual(len(parsed["keys"]), 1)
395
- self.assertEqual(parsed["keys"][0]["id"], "key-serial")
396
- self.assertEqual(parsed["keys"][0]["algorithm"], "Ed25519")
397
- self.assertEqual(parsed["keys"][0]["publicKey"], "pub-spki==")
398
-
399
- def test_expired_key_marked_revoked_in_document(self):
400
- mgr = KeyRotationManager()
401
- now = int(time.time() * 1000)
402
- # A key that has already expired
403
- expired_pair = ManagedKeyPair(
404
- id="key-expired", public_key="pub==", private_key="priv==",
405
- created=now - 10000,
406
- expires_at=now - 1000,
407
- )
408
- mgr.add_key(expired_pair)
409
-
410
- doc = mgr.get_well_known_document()
411
- self.assertEqual(len(doc.keys), 1)
412
- entry = doc.keys[0]
413
- self.assertTrue(entry.revoked)
414
-
415
-
416
- if __name__ == "__main__":
417
- unittest.main()
@@ -1,120 +0,0 @@
1
- """Tests for protocol_7h3.queue — Queue message binding."""
2
- from __future__ import annotations
3
-
4
- import json
5
- import unittest
6
-
7
- from protocol_7h3.queue import (
8
- sign_queue_message,
9
- verify_queue_message,
10
- verify_queue_batch,
11
- )
12
-
13
- # Keys from conformance vectors (PKCS8/SPKI DER, base64url-encoded)
14
- PRIVATE_KEY = "MC4CAQAwBQYDK2VwBCIEICheZbQGuDVb6hezIlcs0QnCHGxz6IhiLkC9M0qr8OOZ"
15
- PUBLIC_KEY = "MCowBQYDK2VwAyEA-mUFiTQtcKN4nnD19V_-Wyy4q19OivnAutRUPhOcC78"
16
-
17
- # A different public key to test wrong-key rejection (second key from test vectors or generated)
18
- OTHER_PUBLIC_KEY = "MCowBQYDK2VwAyEAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
19
-
20
-
21
- def _make_signed(payload: object) -> str:
22
- return sign_queue_message(payload, PRIVATE_KEY, sender="test.sender")
23
-
24
-
25
- class TestSignVerifyRoundTrip(unittest.TestCase):
26
- def test_string_payload_roundtrip(self):
27
- """sign_queue_message + verify_queue_message round trip (string payload)."""
28
- payload = "hello queue"
29
- signed = _make_signed(payload)
30
- result = verify_queue_message(signed, PUBLIC_KEY)
31
- self.assertEqual(result["payload"], payload)
32
- self.assertIn("envelope", result)
33
- self.assertIn("header", result["envelope"])
34
- self.assertIn("signature", result["envelope"])
35
-
36
- def test_dict_payload_roundtrip(self):
37
- """Round trip with dict payload."""
38
- payload = {"task": "process", "items": [1, 2, 3], "meta": {"retry": True}}
39
- signed = _make_signed(payload)
40
- result = verify_queue_message(signed, PUBLIC_KEY)
41
- self.assertEqual(result["payload"], payload)
42
- self.assertIn("signature", result["envelope"])
43
-
44
- def test_verify_raises_on_tampered_envelope(self):
45
- """verify_queue_message raises on tampered envelope."""
46
- signed = _make_signed("important data")
47
- wrapper = json.loads(signed)
48
- # Tamper with the sender field inside the envelope
49
- wrapper["envelope"]["header"]["sender"] = "evil.actor"
50
- tampered = json.dumps(wrapper)
51
- with self.assertRaises(ValueError) as ctx:
52
- verify_queue_message(tampered, PUBLIC_KEY)
53
- self.assertIn("7h3:", str(ctx.exception))
54
-
55
- def test_verify_raises_on_wrong_public_key(self):
56
- """verify_queue_message raises on wrong public key."""
57
- # Generate a different key pair by using a known different key from a second vector
58
- # We use a second private/public pair
59
- alt_private = "MC4CAQAwBQYDK2VwBCIEIBcJSnDN3_g3o_0K5VJHOO_HqFcBCUt1tnGbqiVoaWyb"
60
- alt_public = "MCowBQYDK2VwAyEAiFt0vBZq_X5sMCbR5_LhkRKU2ueD80_bqFMhqfYP5TA"
61
- signed = sign_queue_message("payload", alt_private, sender="test.sender")
62
- with self.assertRaises(ValueError) as ctx:
63
- verify_queue_message(signed, PUBLIC_KEY)
64
- self.assertIn("7h3:", str(ctx.exception))
65
-
66
- def test_verify_raises_on_malformed_json(self):
67
- """verify_queue_message raises on malformed JSON."""
68
- with self.assertRaises(ValueError) as ctx:
69
- verify_queue_message("{not valid json}", PUBLIC_KEY)
70
- self.assertIn("7h3: malformed queue message", str(ctx.exception))
71
-
72
- def test_verify_raises_on_missing_envelope(self):
73
- """verify_queue_message raises when envelope key is missing."""
74
- msg = json.dumps({"payload": "data"})
75
- with self.assertRaises(ValueError) as ctx:
76
- verify_queue_message(msg, PUBLIC_KEY)
77
- self.assertIn("7h3: missing envelope", str(ctx.exception))
78
-
79
-
80
- class TestVerifyQueueBatch(unittest.TestCase):
81
- def test_batch_mixed_ok_and_fail(self):
82
- """verify_queue_batch: returns correct ok/false mix."""
83
- valid_msg = _make_signed("valid payload")
84
- invalid_msg = "not json at all"
85
- results = verify_queue_batch([valid_msg, invalid_msg], PUBLIC_KEY)
86
- self.assertEqual(len(results), 2)
87
- self.assertTrue(results[0]["ok"])
88
- self.assertEqual(results[0]["payload"], "valid payload")
89
- self.assertFalse(results[1]["ok"])
90
- self.assertIn("error", results[1])
91
- self.assertEqual(results[1]["raw"], invalid_msg)
92
-
93
- def test_batch_all_valid(self):
94
- """verify_queue_batch: all valid messages."""
95
- messages = [_make_signed(f"msg-{i}") for i in range(3)]
96
- results = verify_queue_batch(messages, PUBLIC_KEY)
97
- self.assertEqual(len(results), 3)
98
- for i, r in enumerate(results):
99
- self.assertTrue(r["ok"], f"message {i} should be ok")
100
- self.assertEqual(r["payload"], f"msg-{i}")
101
- self.assertIn("envelope", r)
102
-
103
- def test_batch_all_invalid_never_throws(self):
104
- """verify_queue_batch: all invalid messages (never throws)."""
105
- bad_messages = [
106
- "not json",
107
- json.dumps({"no": "envelope"}),
108
- "",
109
- "{}",
110
- ]
111
- # Must not raise
112
- results = verify_queue_batch(bad_messages, PUBLIC_KEY)
113
- self.assertEqual(len(results), len(bad_messages))
114
- for r in results:
115
- self.assertFalse(r["ok"])
116
- self.assertIn("error", r)
117
-
118
-
119
- if __name__ == "__main__":
120
- unittest.main()