Repository navigation
Expand file tree
/
Copy pathWebCryptMCPFullCoverage.test.js
More file actions
299 lines (246 loc) · 8.97 KB
/
Copy pathWebCryptMCPFullCoverage.test.js
File metadata and controls
299 lines (246 loc) · 8.97 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
import { EventEmitter } from "events";
import { WebCryptMCPServer, startMCPServer } from "../src/mcp/server.js";
import { handleToolCall } from "../src/mcp/handlers.js";
describe("WebCrypt MCP Server & Handlers Deep Coverage", () => {
let server;
beforeEach(() => {
server = new WebCryptMCPServer();
});
test("handles ping, notifications, and method not found", async () => {
const pingRes = await server.handleMessage({
jsonrpc: "2.0",
id: 101,
method: "ping",
});
expect(pingRes.result).toEqual({});
const notifRes = await server.handleMessage({
jsonrpc: "2.0",
method: "notifications/initialized",
});
expect(notifRes).toBeNull();
const otherNotifRes = await server.handleMessage({
jsonrpc: "2.0",
method: "custom/notification",
});
expect(otherNotifRes).toBeNull();
const nullMsg = await server.handleMessage(null);
expect(nullMsg).toBeNull();
const unknownRes = await server.handleMessage({
jsonrpc: "2.0",
id: 102,
method: "unknown_method",
});
expect(unknownRes.error.code).toBe(-32601);
});
test("runs stdio stream reader and line buffering in start()", done => {
const mockStdin = new EventEmitter();
const mockStdout = {
data: "",
write(chunk) {
this.data += chunk;
},
};
const stdioServer = new WebCryptMCPServer({
stdin: mockStdin,
stdout: mockStdout,
});
stdioServer.start();
// 1. Partial chunk followed by rest of line
mockStdin.emit("data", '{"jsonrpc":"2.0","id":1,"method"');
mockStdin.emit("data", ':"ping"}\n');
// 2. Empty line & malformed line
mockStdin.emit("data", "\n");
mockStdin.emit("data", "{ invalid json }\n");
// 3. Close stream
mockStdin.emit("end");
setImmediate(() => {
expect(mockStdout.data).toContain('"result":{}');
expect(mockStdout.data).toContain('"code":-32700');
done();
});
});
test("startMCPServer factory function initializes and returns server", () => {
const s = startMCPServer();
expect(s).toBeInstanceOf(WebCryptMCPServer);
expect(s.name).toBe("webcrypt");
});
test("send() ignores falsy response", () => {
let called = false;
const mockStdout = {
write() {
called = true;
},
};
const s = new WebCryptMCPServer({ stdout: mockStdout });
s.send(null);
expect(called).toBe(false);
});
// -------------------------------------------------------------
// Tool Handler Error Cases & Comprehensive Branches
// -------------------------------------------------------------
test("encrypt_payload error branches", async () => {
await expect(handleToolCall("encrypt_payload", { mode: "symmetric" })).rejects.toThrow(
"Missing 'password'"
);
await expect(handleToolCall("encrypt_payload", { mode: "data" })).rejects.toThrow(
"Missing 'password'"
);
await expect(
handleToolCall("encrypt_payload", { mode: "asymmetric", data: "hi" })
).rejects.toThrow("Missing 'public_key_jwk'");
await expect(
handleToolCall("encrypt_payload", { mode: "invalid_mode", data: "hi" })
).rejects.toThrow("Unsupported mode");
});
test("decrypt_payload error branches", async () => {
await expect(handleToolCall("decrypt_payload", {})).rejects.toThrow("Missing 'ciphertext'");
await expect(
handleToolCall("decrypt_payload", { mode: "symmetric", ciphertext: "abc" })
).rejects.toThrow("Missing 'password'");
await expect(
handleToolCall("decrypt_payload", { mode: "data", ciphertext: "abc" })
).rejects.toThrow("Missing 'password'");
await expect(
handleToolCall("decrypt_payload", { mode: "asymmetric", ciphertext: "abc" })
).rejects.toThrow("Missing 'private_key_jwk'");
await expect(
handleToolCall("decrypt_payload", { mode: "invalid_mode", ciphertext: "abc" })
).rejects.toThrow("Unsupported mode");
});
test("manage_keys branches & error handling", async () => {
// Random password
const pwdRes = await handleToolCall("manage_keys", { action: "generate_random_password" });
expect(pwdRes.password).toBeDefined();
// ECDH Keygen
const ecdhRes = await handleToolCall("manage_keys", {
action: "generate",
type: "ecdh",
namedCurve: "P-384",
});
expect(ecdhRes.type).toBe("ecdh");
expect(ecdhRes.publicKey).toBeDefined();
// HMAC Keygen
const hmacRes = await handleToolCall("manage_keys", { action: "generate", type: "hmac" });
expect(hmacRes.type).toBe("hmac");
expect(hmacRes.key_b64).toBeDefined();
// Invalid type & action
await expect(
handleToolCall("manage_keys", { action: "generate", type: "invalid" })
).rejects.toThrow("Unsupported key type");
await expect(handleToolCall("manage_keys", { action: "invalid_action" })).rejects.toThrow(
"Unsupported action"
);
});
test("crypto_hash base64 encoding and SHA-512 / SHA3-512", async () => {
await expect(handleToolCall("crypto_hash", {})).rejects.toThrow("Missing 'data'");
const sha512 = await handleToolCall("crypto_hash", {
algorithm: "SHA-512",
data: "hello world",
encoding: "base64",
});
expect(sha512.digest).toBeDefined();
expect(sha512.encoding).toBe("base64");
const sha3_512 = await handleToolCall("crypto_hash", {
algorithm: "SHA3-512",
data: "hello world",
encoding: "base64",
});
expect(sha3_512.digest).toBeDefined();
});
test("sign_verify RSA-PSS, HMAC-SHA3 and error branches", async () => {
await expect(handleToolCall("sign_verify", { action: "sign" })).rejects.toThrow(
"Missing 'data'"
);
// Missing password for HMAC
await expect(
handleToolCall("sign_verify", { action: "sign", algorithm: "HMAC", data: "test" })
).rejects.toThrow("Missing 'password'");
await expect(
handleToolCall("sign_verify", { action: "sign", algorithm: "HMAC-SHA3", data: "test" })
).rejects.toThrow("Missing 'password'");
await expect(
handleToolCall("sign_verify", { action: "sign", algorithm: "ECDSA", data: "test" })
).rejects.toThrow("Missing 'key_jwk'");
await expect(
handleToolCall("sign_verify", { action: "sign", algorithm: "unsupported", data: "test" })
).rejects.toThrow("Unsupported sign algorithm");
// Verify error branches
await expect(handleToolCall("sign_verify", { action: "verify", data: "test" })).rejects.toThrow(
"Missing 'signature'"
);
await expect(
handleToolCall("sign_verify", {
action: "verify",
algorithm: "HMAC",
data: "test",
signature: "sig",
})
).rejects.toThrow("Missing 'password'");
await expect(
handleToolCall("sign_verify", {
action: "verify",
algorithm: "HMAC-SHA3",
data: "test",
signature: "sig",
})
).rejects.toThrow("Missing 'password'");
await expect(
handleToolCall("sign_verify", {
action: "verify",
algorithm: "ECDSA",
data: "test",
signature: "sig",
})
).rejects.toThrow("Missing 'key_jwk'");
await expect(
handleToolCall("sign_verify", {
action: "verify",
algorithm: "unsupported",
data: "test",
signature: "sig",
})
).rejects.toThrow("Unsupported verify algorithm");
await expect(
handleToolCall("sign_verify", { action: "unsupported_action", data: "test" })
).rejects.toThrow("Unsupported action");
// HMAC-SHA3 sign & verify roundtrip
const hmacSign = await handleToolCall("sign_verify", {
action: "sign",
algorithm: "HMAC-SHA3",
data: "hello hmac sha3",
password: "secret-pass",
});
expect(hmacSign.signature).toBeDefined();
const hmacVerify = await handleToolCall("sign_verify", {
action: "verify",
algorithm: "HMAC-SHA3",
data: "hello hmac sha3",
signature: hmacSign.signature,
password: "secret-pass",
});
expect(hmacVerify.valid).toBe(true);
});
test("pqc_kem_sign validation and error branches", async () => {
await expect(handleToolCall("pqc_kem_sign", { action: "kyber_encapsulate" })).rejects.toThrow(
"Missing 'public_key_b64'"
);
await expect(handleToolCall("pqc_kem_sign", { action: "kyber_decapsulate" })).rejects.toThrow(
"Missing 'ciphertext_b64'"
);
await expect(handleToolCall("pqc_kem_sign", { action: "hybrid_encapsulate" })).rejects.toThrow(
"Missing 'rsa_public_key_jwk'"
);
await expect(handleToolCall("pqc_kem_sign", { action: "hybrid_decapsulate" })).rejects.toThrow(
"Missing required parameters"
);
await expect(handleToolCall("pqc_kem_sign", { action: "dilithium_sign" })).rejects.toThrow(
"Missing 'data'"
);
await expect(handleToolCall("pqc_kem_sign", { action: "dilithium_verify" })).rejects.toThrow(
"Missing 'data'"
);
await expect(handleToolCall("pqc_kem_sign", { action: "invalid_pqc_action" })).rejects.toThrow(
"Unsupported PQC action"
);
});
});