-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_webauthn_integration.html
More file actions
698 lines (595 loc) · 31.8 KB
/
test_webauthn_integration.html
File metadata and controls
698 lines (595 loc) · 31.8 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebAuthn Hardware Key Tests - Meow Decoder</title>
<style>
* { box-sizing: border-box; }
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 900px;
margin: 0 auto;
padding: 20px;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
color: #e0e0e0;
min-height: 100vh;
}
h1 { color: #00ff88; text-align: center; }
h2 { color: #00ccff; margin-top: 30px; }
.test-section {
background: rgba(255, 255, 255, 0.08);
border-radius: 12px;
padding: 20px;
margin: 20px 0;
border-left: 4px solid #00ccff;
}
.test-case {
background: rgba(0, 0, 0, 0.3);
padding: 15px;
margin: 10px 0;
border-radius: 8px;
border-left: 3px solid #666;
}
.test-case.pass { border-left-color: #4caf50; }
.test-case.fail { border-left-color: #f44336; }
.test-case.pending { border-left-color: #ff9800; }
button {
background: linear-gradient(135deg, #00ff88 0%, #00ccff 100%);
color: #1a1a2e;
border: none;
padding: 12px 24px;
border-radius: 6px;
cursor: pointer;
font-weight: bold;
margin: 5px;
transition: transform 0.2s;
}
button:hover { transform: scale(1.05); }
button:disabled { background: #444; color: #777; cursor: not-allowed; }
.status-badge {
display: inline-block;
padding: 4px 12px;
border-radius: 4px;
font-weight: bold;
font-size: 0.9em;
margin-right: 10px;
}
.status-badge.pass { background: #4caf50; color: white; }
.status-badge.fail { background: #f44336; color: white; }
.status-badge.pending { background: #ff9800; color: white; }
pre {
background: #000;
padding: 10px;
border-radius: 6px;
overflow-x: auto;
font-size: 0.85em;
color: #0f0;
}
.summary {
text-align: center;
font-size: 1.2em;
margin: 20px 0;
padding: 20px;
background: rgba(0, 204, 255, 0.1);
border-radius: 10px;
}
.log {
max-height: 200px;
overflow-y: auto;
background: #000;
padding: 10px;
border-radius: 6px;
font-family: monospace;
font-size: 0.85em;
color: #0f0;
}
</style>
</head>
<body>
<h1>🔐 WebAuthn Hardware Key Tests</h1>
<p style="text-align: center; color: #888;">
Unit tests for <code>webauthn-hardware.js</code> - Hybrid key derivation and hardware integration
</p>
<div class="test-section">
<h2>Test Controls</h2>
<button id="runAllTests">▶️ Run All Tests</button>
<button id="resetTests">🔄 Reset Tests</button>
<button id="clearStorage">🗑️ Clear Storage</button>
</div>
<div id="summary" class="summary" style="display: none;">
<div id="summaryText"></div>
</div>
<div class="test-section">
<h2>1️⃣ Utility Function Tests (Pure Crypto)</h2>
<div id="utilityTests"></div>
</div>
<div class="test-section">
<h2>2️⃣ HKDF Key Derivation Tests</h2>
<div id="hkdfTests"></div>
</div>
<div class="test-section">
<h2>3️⃣ Hybrid Key Mixing Tests</h2>
<div id="hybridTests"></div>
</div>
<div class="test-section">
<h2>4️⃣ WebAuthn Integration Tests (Requires Hardware Key)</h2>
<p style="color: #ff9800;">⚠️ These tests require a physical FIDO2 security key and user interaction.</p>
<div id="webauthnTests"></div>
</div>
<div class="test-section">
<h2>📋 Test Log</h2>
<div id="testLog" class="log"></div>
</div>
<script type="module">
import { HardwareKeyManager, createHardwareUI, initHardwareUI } from './webauthn-hardware.js';
// Test framework
let testResults = [];
let testLog = [];
function log(message) {
testLog.push(`[${new Date().toLocaleTimeString()}] ${message}`);
const logDiv = document.getElementById('testLog');
logDiv.innerHTML = testLog.slice(-20).join('\n');
logDiv.scrollTop = logDiv.scrollHeight;
}
function createTestCase(container, name, testFn) {
const div = document.createElement('div');
div.className = 'test-case pending';
div.innerHTML = `
<span class="status-badge pending">PENDING</span>
<strong>${name}</strong>
<div class="test-details" style="margin-top: 10px; display: none;"></div>
`;
container.appendChild(div);
return {
element: div,
async run() {
const badge = div.querySelector('.status-badge');
const details = div.querySelector('.test-details');
try {
log(`Running: ${name}`);
const result = await testFn();
if (result.pass) {
badge.textContent = '✅ PASS';
badge.className = 'status-badge pass';
div.className = 'test-case pass';
log(`✅ ${name} - PASS`);
} else {
badge.textContent = '❌ FAIL';
badge.className = 'status-badge fail';
div.className = 'test-case fail';
log(`❌ ${name} - FAIL: ${result.error}`);
}
if (result.details) {
details.innerHTML = `<pre>${result.details}</pre>`;
details.style.display = 'block';
}
testResults.push({ name, pass: result.pass, error: result.error });
return result.pass;
} catch (err) {
badge.textContent = '❌ ERROR';
badge.className = 'status-badge fail';
div.className = 'test-case fail';
details.innerHTML = `<pre style="color: #f44;">Error: ${err.message}\n${err.stack}</pre>`;
details.style.display = 'block';
log(`❌ ${name} - ERROR: ${err.message}`);
testResults.push({ name, pass: false, error: err.message });
return false;
}
}
};
}
// Helper: Compare arrays
function arraysEqual(a, b) {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
// Helper: Get test crypto functions from webauthn-hardware.js
// We need to expose internal functions for testing
async function sha256(data) {
const hash = await crypto.subtle.digest('SHA-256', data);
return new Uint8Array(hash);
}
async function hmacSha256(key, data) {
const cryptoKey = await crypto.subtle.importKey(
'raw', key,
{ name: 'HMAC', hash: 'SHA-256' },
false, ['sign']
);
const signature = await crypto.subtle.sign('HMAC', cryptoKey, data);
return new Uint8Array(signature);
}
function concatBuffers(...buffers) {
const totalLength = buffers.reduce((acc, buf) => acc + buf.byteLength, 0);
const result = new Uint8Array(totalLength);
let offset = 0;
for (const buf of buffers) {
result.set(new Uint8Array(buf), offset);
offset += buf.byteLength;
}
return result;
}
async function hkdf(ikm, salt, info, length = 32) {
const prk = await hmacSha256(salt, ikm);
const n = Math.ceil(length / 32);
const blocks = [];
let t = new Uint8Array(0);
for (let i = 1; i <= n; i++) {
const input = concatBuffers(t, info, new Uint8Array([i]));
t = await hmacSha256(prk, input);
blocks.push(t);
}
const okm = concatBuffers(...blocks);
return okm.slice(0, length);
}
// ====================================================================
// 1. Utility Function Tests
// ====================================================================
const utilityTests = [
createTestCase(document.getElementById('utilityTests'), 'SHA-256 produces 32 bytes', async () => {
const input = new TextEncoder().encode('test input');
const hash = await sha256(input);
return {
pass: hash.length === 32,
error: hash.length !== 32 ? `Expected 32 bytes, got ${hash.length}` : null,
details: `Hash: ${Array.from(hash.slice(0, 16)).map(b => b.toString(16).padStart(2, '0')).join('')}...`
};
}),
createTestCase(document.getElementById('utilityTests'), 'SHA-256 is deterministic', async () => {
const input = new TextEncoder().encode('deterministic test');
const hash1 = await sha256(input);
const hash2 = await sha256(input);
return {
pass: arraysEqual(hash1, hash2),
error: !arraysEqual(hash1, hash2) ? 'Hashes do not match' : null,
details: `Hash1: ${Array.from(hash1.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nHash2: ${Array.from(hash2.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
}),
createTestCase(document.getElementById('utilityTests'), 'HMAC-SHA256 produces 32 bytes', async () => {
const key = new Uint8Array(32).fill(0x42);
const data = new TextEncoder().encode('test data');
const hmac = await hmacSha256(key, data);
return {
pass: hmac.length === 32,
error: hmac.length !== 32 ? `Expected 32 bytes, got ${hmac.length}` : null,
details: `HMAC: ${Array.from(hmac.slice(0, 16)).map(b => b.toString(16).padStart(2, '0')).join('')}...`
};
}),
createTestCase(document.getElementById('utilityTests'), 'HMAC-SHA256 is keyed', async () => {
const key1 = new Uint8Array(32).fill(0x01);
const key2 = new Uint8Array(32).fill(0x02);
const data = new TextEncoder().encode('same data');
const hmac1 = await hmacSha256(key1, data);
const hmac2 = await hmacSha256(key2, data);
return {
pass: !arraysEqual(hmac1, hmac2),
error: arraysEqual(hmac1, hmac2) ? 'Different keys produced same HMAC' : null,
details: `HMAC1: ${Array.from(hmac1.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nHMAC2: ${Array.from(hmac2.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
})
];
// ====================================================================
// 2. HKDF Tests
// ====================================================================
const hkdfTests = [
createTestCase(document.getElementById('hkdfTests'), 'HKDF derives 32 bytes by default', async () => {
const ikm = crypto.getRandomValues(new Uint8Array(32));
const salt = crypto.getRandomValues(new Uint8Array(16));
const info = new TextEncoder().encode('test-context');
const key = await hkdf(ikm, salt, info);
return {
pass: key.length === 32,
error: key.length !== 32 ? `Expected 32 bytes, got ${key.length}` : null,
details: `Derived key: ${Array.from(key.slice(0, 16)).map(b => b.toString(16).padStart(2, '0')).join('')}...`
};
}),
createTestCase(document.getElementById('hkdfTests'), 'HKDF is deterministic', async () => {
const ikm = new Uint8Array(32).fill(0xAA);
const salt = new Uint8Array(16).fill(0xBB);
const info = new TextEncoder().encode('deterministic');
const key1 = await hkdf(ikm, salt, info, 32);
const key2 = await hkdf(ikm, salt, info, 32);
return {
pass: arraysEqual(key1, key2),
error: !arraysEqual(key1, key2) ? 'HKDF produced different outputs' : null,
details: `Key1: ${Array.from(key1.slice(0, 16)).map(b => b.toString(16).padStart(2, '0')).join('')}\nKey2: ${Array.from(key2.slice(0, 16)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
}),
createTestCase(document.getElementById('hkdfTests'), 'HKDF responds to IKM changes', async () => {
const ikm1 = new Uint8Array(32).fill(0x01);
const ikm2 = new Uint8Array(32).fill(0x02);
const salt = new Uint8Array(16).fill(0x00);
const info = new TextEncoder().encode('test');
const key1 = await hkdf(ikm1, salt, info, 32);
const key2 = await hkdf(ikm2, salt, info, 32);
return {
pass: !arraysEqual(key1, key2),
error: arraysEqual(key1, key2) ? 'Different IKMs produced same key' : null,
details: `Key1: ${Array.from(key1.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nKey2: ${Array.from(key2.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
}),
createTestCase(document.getElementById('hkdfTests'), 'HKDF responds to salt changes', async () => {
const ikm = new Uint8Array(32).fill(0xAA);
const salt1 = new Uint8Array(16).fill(0x11);
const salt2 = new Uint8Array(16).fill(0x22);
const info = new TextEncoder().encode('test');
const key1 = await hkdf(ikm, salt1, info, 32);
const key2 = await hkdf(ikm, salt2, info, 32);
return {
pass: !arraysEqual(key1, key2),
error: arraysEqual(key1, key2) ? 'Different salts produced same key' : null,
details: `Key1: ${Array.from(key1.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nKey2: ${Array.from(key2.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
}),
createTestCase(document.getElementById('hkdfTests'), 'HKDF responds to context changes', async () => {
const ikm = new Uint8Array(32).fill(0xAA);
const salt = new Uint8Array(16).fill(0xBB);
const info1 = new TextEncoder().encode('context-1');
const info2 = new TextEncoder().encode('context-2');
const key1 = await hkdf(ikm, salt, info1, 32);
const key2 = await hkdf(ikm, salt, info2, 32);
return {
pass: !arraysEqual(key1, key2),
error: arraysEqual(key1, key2) ? 'Different contexts produced same key' : null,
details: `Key1: ${Array.from(key1.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nKey2: ${Array.from(key2.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
})
];
// ====================================================================
// 3. Hybrid Key Mixing Tests
// ====================================================================
const hybridTests = [
createTestCase(document.getElementById('hybridTests'), 'XOR mixing produces correct length', async () => {
const key1 = crypto.getRandomValues(new Uint8Array(32));
const key2 = crypto.getRandomValues(new Uint8Array(32));
const mixed = new Uint8Array(32);
for (let i = 0; i < 32; i++) {
mixed[i] = key1[i] ^ key2[i];
}
return {
pass: mixed.length === 32,
error: mixed.length !== 32 ? `Expected 32 bytes, got ${mixed.length}` : null,
details: `Mixed key: ${Array.from(mixed.slice(0, 16)).map(b => b.toString(16).padStart(2, '0')).join('')}...`
};
}),
createTestCase(document.getElementById('hybridTests'), 'XOR is reversible (key1 XOR key2 XOR key2 = key1)', async () => {
const key1 = crypto.getRandomValues(new Uint8Array(32));
const key2 = crypto.getRandomValues(new Uint8Array(32));
const mixed = new Uint8Array(32);
const recovered = new Uint8Array(32);
for (let i = 0; i < 32; i++) {
mixed[i] = key1[i] ^ key2[i];
recovered[i] = mixed[i] ^ key2[i];
}
return {
pass: arraysEqual(key1, recovered),
error: !arraysEqual(key1, recovered) ? 'XOR did not reverse properly' : null,
details: `Original: ${Array.from(key1.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nRecovered: ${Array.from(recovered.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
}),
createTestCase(document.getElementById('hybridTests'), 'XOR with zero preserves key', async () => {
const key = crypto.getRandomValues(new Uint8Array(32));
const zero = new Uint8Array(32).fill(0);
const result = new Uint8Array(32);
for (let i = 0; i < 32; i++) {
result[i] = key[i] ^ zero[i];
}
return {
pass: arraysEqual(key, result),
error: !arraysEqual(key, result) ? 'XOR with zero changed the key' : null,
details: `Original: ${Array.from(key.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nResult: ${Array.from(result.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
}),
createTestCase(document.getElementById('hybridTests'), 'Hybrid key requires both factors', async () => {
const passwordKey = crypto.getRandomValues(new Uint8Array(32));
const hardwareKey = crypto.getRandomValues(new Uint8Array(32));
const hybridKey = new Uint8Array(32);
for (let i = 0; i < 32; i++) {
hybridKey[i] = passwordKey[i] ^ hardwareKey[i];
}
// Verify hybrid key is different from both inputs
const diffFromPassword = !arraysEqual(hybridKey, passwordKey);
const diffFromHardware = !arraysEqual(hybridKey, hardwareKey);
return {
pass: diffFromPassword && diffFromHardware,
error: !(diffFromPassword && diffFromHardware) ? 'Hybrid key equals one of the inputs' : null,
details: `Password: ${Array.from(passwordKey.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nHardware: ${Array.from(hardwareKey.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nHybrid: ${Array.from(hybridKey.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
}),
createTestCase(document.getElementById('hybridTests'), 'Password change affects hybrid key', async () => {
const password1Key = new Uint8Array(32).fill(0xAA);
const password2Key = new Uint8Array(32).fill(0xBB);
const hardwareKey = new Uint8Array(32).fill(0xCC);
const hybrid1 = new Uint8Array(32);
const hybrid2 = new Uint8Array(32);
for (let i = 0; i < 32; i++) {
hybrid1[i] = password1Key[i] ^ hardwareKey[i];
hybrid2[i] = password2Key[i] ^ hardwareKey[i];
}
return {
pass: !arraysEqual(hybrid1, hybrid2),
error: arraysEqual(hybrid1, hybrid2) ? 'Password change did not affect hybrid key' : null,
details: `Hybrid1: ${Array.from(hybrid1.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nHybrid2: ${Array.from(hybrid2.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
}),
createTestCase(document.getElementById('hybridTests'), 'Hardware change affects hybrid key', async () => {
const passwordKey = new Uint8Array(32).fill(0xAA);
const hardware1Key = new Uint8Array(32).fill(0xBB);
const hardware2Key = new Uint8Array(32).fill(0xCC);
const hybrid1 = new Uint8Array(32);
const hybrid2 = new Uint8Array(32);
for (let i = 0; i < 32; i++) {
hybrid1[i] = passwordKey[i] ^ hardware1Key[i];
hybrid2[i] = passwordKey[i] ^ hardware2Key[i];
}
return {
pass: !arraysEqual(hybrid1, hybrid2),
error: arraysEqual(hybrid1, hybrid2) ? 'Hardware change did not affect hybrid key' : null,
details: `Hybrid1: ${Array.from(hybrid1.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nHybrid2: ${Array.from(hybrid2.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
})
];
// ====================================================================
// 4. WebAuthn Integration Tests (Requires Hardware)
// ====================================================================
const hwManager = new HardwareKeyManager();
const webauthnTests = [
createTestCase(document.getElementById('webauthnTests'), 'WebAuthn API is supported', async () => {
const supported = await hwManager.isSupported();
return {
pass: supported,
error: !supported ? 'WebAuthn not supported in this browser' : null,
details: `PublicKeyCredential: ${typeof window.PublicKeyCredential}\nSupported: ${supported}`
};
}),
createTestCase(document.getElementById('webauthnTests'), 'Can check registration status', async () => {
const isRegistered = hwManager.isRegistered();
return {
pass: true, // Always passes, just reports status
details: `Registered: ${isRegistered}\nCredential: ${isRegistered ? hwManager.getCredential().credentialId.slice(0, 32) + '...' : 'None'}`
};
}),
createTestCase(document.getElementById('webauthnTests'), '[MANUAL] Register hardware key', async () => {
// This test requires user interaction
if (!await hwManager.isSupported()) {
return { pass: false, error: 'WebAuthn not supported' };
}
const alreadyRegistered = hwManager.isRegistered();
if (alreadyRegistered) {
return {
pass: true,
details: 'Already registered (skipping). Use "Clear Storage" button to re-test registration.'
};
}
// Prompt user
const proceed = confirm('This test will prompt you to register your security key.\n\nInsert your key and click OK to continue.');
if (!proceed) {
return { pass: false, error: 'User canceled' };
}
try {
const result = await hwManager.register('test-user');
return {
pass: true,
details: `Registered!\nCredential ID: ${result.credentialId.slice(0, 32)}...\nUsername: ${result.username}`
};
} catch (err) {
return { pass: false, error: err.message };
}
}),
createTestCase(document.getElementById('webauthnTests'), '[MANUAL] Get hardware signature', async () => {
if (!hwManager.isRegistered()) {
return { pass: false, error: 'No hardware key registered (run registration test first)' };
}
const proceed = confirm('This test will prompt you to touch your security key.\n\nClick OK to continue.');
if (!proceed) {
return { pass: false, error: 'User canceled' };
}
try {
const challenge = crypto.getRandomValues(new Uint8Array(32));
const signature = await hwManager.getSignature(challenge);
return {
pass: signature.length > 0,
error: signature.length === 0 ? 'Empty signature returned' : null,
details: `Signature: ${signature.length} bytes\n${Array.from(signature.slice(0, 16)).map(b => b.toString(16).padStart(2, '0')).join('')}...`
};
} catch (err) {
return { pass: false, error: err.message };
}
}),
createTestCase(document.getElementById('webauthnTests'), '[MANUAL] Hardware signatures are different', async () => {
if (!hwManager.isRegistered()) {
return { pass: false, error: 'No hardware key registered' };
}
const proceed = confirm('This test will prompt you to touch your key TWICE.\n\nClick OK to continue.');
if (!proceed) {
return { pass: false, error: 'User canceled' };
}
try {
const challenge1 = crypto.getRandomValues(new Uint8Array(32));
const challenge2 = crypto.getRandomValues(new Uint8Array(32));
const sig1 = await hwManager.getSignature(challenge1);
const sig2 = await hwManager.getSignature(challenge2);
return {
pass: !arraysEqual(sig1, sig2),
error: arraysEqual(sig1, sig2) ? 'Different challenges produced same signature' : null,
details: `Sig1: ${Array.from(sig1.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}\nSig2: ${Array.from(sig2.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join('')}`
};
} catch (err) {
return { pass: false, error: err.message };
}
})
];
// ====================================================================
// Test Runner
// ====================================================================
async function runAllTests() {
testResults = [];
testLog = [];
document.getElementById('runAllTests').disabled = true;
document.getElementById('summary').style.display = 'none';
log('=== Starting Test Suite ===');
const allTests = [
...utilityTests,
...hkdfTests,
...hybridTests,
...webauthnTests
];
let passed = 0;
let failed = 0;
for (const test of allTests) {
const result = await test.run();
if (result) passed++;
else failed++;
}
const total = passed + failed;
const percent = Math.round((passed / total) * 100);
log(`=== Test Suite Complete ===`);
log(`Passed: ${passed}/${total} (${percent}%)`);
// Show summary
const summaryDiv = document.getElementById('summary');
const summaryText = document.getElementById('summaryText');
summaryDiv.style.display = 'block';
if (failed === 0) {
summaryText.innerHTML = `✅ All tests passed! (${passed}/${total})`;
summaryDiv.style.borderLeft = '4px solid #4caf50';
} else {
summaryText.innerHTML = `⚠️ ${failed} test(s) failed (${passed}/${total} passed)`;
summaryDiv.style.borderLeft = '4px solid #f44336';
}
document.getElementById('runAllTests').disabled = false;
}
function resetTests() {
testResults = [];
testLog = [];
document.getElementById('testLog').innerHTML = '';
document.getElementById('summary').style.display = 'none';
// Reset all test cases
document.querySelectorAll('.test-case').forEach(tc => {
tc.className = 'test-case pending';
const badge = tc.querySelector('.status-badge');
badge.textContent = 'PENDING';
badge.className = 'status-badge pending';
tc.querySelector('.test-details').style.display = 'none';
});
log('Tests reset');
}
function clearStorage() {
if (confirm('⚠️ Clear all WebAuthn credentials?\n\nThis will unregister your hardware key.')) {
hwManager.unregister();
localStorage.clear();
alert('Storage cleared');
resetTests();
}
}
// Event listeners
document.getElementById('runAllTests').addEventListener('click', runAllTests);
document.getElementById('resetTests').addEventListener('click', resetTests);
document.getElementById('clearStorage').addEventListener('click', clearStorage);
// Auto-run on load (optional)
// runAllTests();
</script>
</body>
</html>