@7h3/protocol 0.4.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 (248) hide show
  1. package/README.md +6 -323
  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 -79
  23. package/policyEnforcer.d.ts +50 -0
  24. package/policyTelemetryFeedback.d.ts +11 -0
  25. package/protocol.d.ts +66 -0
  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 -92
  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 -385
  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.png +0 -0
  115. package/eslint.config.js +0 -15
  116. package/fuzz/ts/harness-decode.ts +0 -136
  117. package/fuzz/ts/harness-verify.ts +0 -121
  118. package/fuzz/ts/run.ts +0 -35
  119. package/mcp-server/README.md +0 -38
  120. package/mcp-server/package-lock.json +0 -1187
  121. package/mcp-server/package.json +0 -35
  122. package/mcp-server/src/index.ts +0 -236
  123. package/mcp-server/tsconfig.json +0 -14
  124. package/scripts/aip-framework-quickstart.ts +0 -110
  125. package/scripts/aip-mcp-gateway.ts +0 -38
  126. package/scripts/aip-mcp-wrap-demo.ts +0 -72
  127. package/scripts/aip-quickstart.ts +0 -60
  128. package/scripts/bench-diff.ts +0 -118
  129. package/scripts/bench-protocol-e2e.ts +0 -937
  130. package/scripts/bench-protocol-openloop.ts +0 -1397
  131. package/scripts/bench-replay-cache.ts +0 -76
  132. package/scripts/bench-signature-profiles.ts +0 -180
  133. package/scripts/bench-wire-codecs.ts +0 -161
  134. package/scripts/build-binary-conformance.ts +0 -36
  135. package/scripts/build-release-dashboard.ts +0 -175
  136. package/scripts/canary-rollout.ts +0 -38
  137. package/scripts/mcpGatewayCli.test.ts +0 -116
  138. package/scripts/prepare-aip-package.ts +0 -88
  139. package/scripts/regen-conformance-sigs.ts +0 -18
  140. package/scripts/release-gate.ts +0 -19
  141. package/scripts/validate-runtime-policy.ts +0 -18
  142. package/sdk/browser/index.test.ts +0 -162
  143. package/sdk/browser/index.ts +0 -257
  144. package/sdk/browser/package.json +0 -13
  145. package/sdk/go/go.mod +0 -3
  146. package/sdk/go/http.go +0 -135
  147. package/sdk/go/protocol.go +0 -324
  148. package/sdk/go/protocol_test.go +0 -334
  149. package/sdk/go/webhook.go +0 -136
  150. package/sdk/python/README.md +0 -18
  151. package/sdk/python/protocol_7h3/__init__.py +0 -46
  152. package/sdk/python/protocol_7h3/http.py +0 -212
  153. package/sdk/python/protocol_7h3/keys.py +0 -149
  154. package/sdk/python/protocol_7h3/protocol.py +0 -525
  155. package/sdk/python/protocol_7h3/queue.py +0 -118
  156. package/sdk/python/protocol_7h3/webhook.py +0 -116
  157. package/sdk/python/pyproject.toml +0 -40
  158. package/sdk/python/tests/test_conformance.py +0 -110
  159. package/sdk/python/tests/test_http.py +0 -305
  160. package/sdk/python/tests/test_keys.py +0 -417
  161. package/sdk/python/tests/test_queue.py +0 -120
  162. package/sdk/python/tests/test_webhook.py +0 -345
  163. package/sdk/rust/Cargo.lock +0 -371
  164. package/sdk/rust/Cargo.toml +0 -25
  165. package/sdk/rust/README.md +0 -31
  166. package/sdk/rust/fuzz/Cargo.toml +0 -29
  167. package/sdk/rust/fuzz/fuzz_targets/fuzz_canonicalize.rs +0 -46
  168. package/sdk/rust/fuzz/fuzz_targets/fuzz_decode.rs +0 -11
  169. package/sdk/rust/src/bin/aip_mcp_gateway.rs +0 -59
  170. package/sdk/rust/src/http.rs +0 -145
  171. package/sdk/rust/src/keys.rs +0 -161
  172. package/sdk/rust/src/lib.rs +0 -688
  173. package/sdk/rust/src/queue.rs +0 -79
  174. package/sdk/rust/src/webhook.rs +0 -86
  175. package/sdk/rust/tests/conformance.rs +0 -148
  176. package/sdk/rust/tests/gateway.rs +0 -130
  177. package/sdk/rust/tests/http_webhook_queue.rs +0 -201
  178. package/sdk/rust/tests/keys.rs +0 -189
  179. package/src/agentAdapter.test.ts +0 -48
  180. package/src/agentAdapter.ts +0 -56
  181. package/src/auditLog.test.ts +0 -145
  182. package/src/auditLog.ts +0 -147
  183. package/src/conformance.test.ts +0 -136
  184. package/src/conformanceVectors.ts +0 -99
  185. package/src/frameworkAdapters.test.ts +0 -290
  186. package/src/frameworkAdapters.ts +0 -261
  187. package/src/gateway.test.ts +0 -343
  188. package/src/gateway.ts +0 -171
  189. package/src/grpcBinding.test.ts +0 -211
  190. package/src/grpcBinding.ts +0 -103
  191. package/src/httpBinding.test.ts +0 -376
  192. package/src/httpBinding.ts +0 -163
  193. package/src/index.ts +0 -32
  194. package/src/keyInfra.test.ts +0 -278
  195. package/src/keyInfra.ts +0 -228
  196. package/src/keyRegistry.ts +0 -59
  197. package/src/keyRotation.test.ts +0 -78
  198. package/src/keyRotation.ts +0 -72
  199. package/src/mcpGateway.test.ts +0 -129
  200. package/src/mcpGateway.ts +0 -250
  201. package/src/mcpTransports.test.ts +0 -92
  202. package/src/mcpTransports.ts +0 -169
  203. package/src/mcpWrapper.test.ts +0 -179
  204. package/src/mcpWrapper.ts +0 -206
  205. package/src/policyEnforcer.test.ts +0 -99
  206. package/src/policyEnforcer.ts +0 -169
  207. package/src/policyTelemetryFeedback.test.ts +0 -25
  208. package/src/policyTelemetryFeedback.ts +0 -38
  209. package/src/protocol.bench.ts +0 -37
  210. package/src/protocol.test.ts +0 -155
  211. package/src/protocol.ts +0 -413
  212. package/src/protocolAgent.test.ts +0 -105
  213. package/src/protocolAgent.ts +0 -169
  214. package/src/protocolBinary.test.ts +0 -165
  215. package/src/protocolBinary.ts +0 -312
  216. package/src/protocolCapabilities.ts +0 -70
  217. package/src/protocolFuzz.advanced.test.ts +0 -235
  218. package/src/protocolFuzz.test.ts +0 -111
  219. package/src/protocolNegative.test.ts +0 -97
  220. package/src/protocolReplay.test.ts +0 -71
  221. package/src/protocolReplay.ts +0 -194
  222. package/src/protocolTransport.test.ts +0 -556
  223. package/src/protocolTransport.ts +0 -483
  224. package/src/queueBinding.test.ts +0 -130
  225. package/src/queueBinding.ts +0 -102
  226. package/src/rateLimiter.test.ts +0 -96
  227. package/src/rateLimiter.ts +0 -46
  228. package/src/redisIntegration.test.ts +0 -134
  229. package/src/replayStores.test.ts +0 -141
  230. package/src/replayStores.ts +0 -82
  231. package/src/revocation.test.ts +0 -98
  232. package/src/revocation.ts +0 -0
  233. package/src/routePolicy.test.ts +0 -87
  234. package/src/routePolicy.ts +0 -72
  235. package/src/runtimePolicy.test.ts +0 -49
  236. package/src/runtimePolicy.ts +0 -81
  237. package/src/runtimePolicyManager.test.ts +0 -29
  238. package/src/runtimePolicyManager.ts +0 -50
  239. package/src/runtimePolicyPresets.ts +0 -43
  240. package/src/signedResponse.test.ts +0 -111
  241. package/src/signedResponse.ts +0 -83
  242. package/src/webhookBinding.test.ts +0 -144
  243. package/src/webhookBinding.ts +0 -115
  244. package/src/wsBinding.test.ts +0 -221
  245. package/src/wsBinding.ts +0 -100
  246. package/tsconfig.json +0 -15
  247. package/tsconfig.lib.json +0 -23
  248. package/vite.lib.config.ts +0 -16
@@ -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()