Skip to content

Commit d42350c

Browse files
authored
Enhance tests for HMAC streaming sign and verify
Added tests for streaming sign and verify with various secret types including empty buffer, empty string, undefined, and null.
1 parent 5cb007c commit d42350c

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

test/jws.test.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,119 @@ test('jws.isValid', function (t) {
329329
t.same(jws.isValid(valid), true);
330330
t.end();
331331
});
332+
333+
test('Streaming sign: HMAC with empty secret buffer', function (t) {
334+
const dataStream = readstream('data.txt');
335+
const secret = Buffer.alloc(0);
336+
const sig = jws.createSign({
337+
header: { alg: 'HS256' },
338+
secret: secret
339+
});
340+
dataStream.pipe(sig.payload);
341+
sig.on('done', function (signature) {
342+
t.ok(jws.verify(signature, 'HS256', secret), 'should verify');
343+
t.end();
344+
}).sign();
345+
});
346+
347+
test('Streaming sign: HMAC with empty secret string', function (t) {
348+
const dataStream = readstream('data.txt');
349+
const secret = '';
350+
const sig = jws.createSign({
351+
header: { alg: 'HS256' },
352+
secret: secret
353+
});
354+
dataStream.pipe(sig.payload);
355+
sig.on('done', function (signature) {
356+
t.ok(jws.verify(signature, 'HS256', secret), 'should verify');
357+
t.end();
358+
}).sign();
359+
});
360+
361+
test('Streaming sign: HMAC with undefined secret', function (t) {
362+
try {
363+
jws.createSign({
364+
header: { alg: 'HS256' },
365+
secret: undefined
366+
});
367+
t.fail('should have errored');
368+
t.end();
369+
} catch (error) {
370+
t.equal(error.name, 'TypeError');
371+
t.equal(error.message, 'secret must be a string or buffer or a KeyObject');
372+
t.end();
373+
}
374+
});
375+
376+
test('Streaming sign: HMAC with null secret', function (t) {
377+
try {
378+
jws.createSign({
379+
header: { alg: 'HS256' },
380+
secret: null
381+
});
382+
t.fail('should have errored');
383+
t.end();
384+
} catch (error) {
385+
t.equal(error.name, 'TypeError');
386+
t.equal(error.message, 'secret must be a string or buffer or a KeyObject');
387+
t.end();
388+
}
389+
});
390+
391+
test('Streaming verify: HMAC with empty secret buffer', function (t) {
392+
const secret = Buffer.alloc(0);
393+
jws.createVerify({
394+
signature: 'eyJhbGciOiJIUzI1NiJ9.b25lLCB0d28sIHRocmVlCg.V1oQ0aq6FgAoe7C2TORtYpQAbYzJoFNFZlJkTlF1e60',
395+
algorithm: 'HS256',
396+
secret: secret
397+
}).on('done', function (valid, decoded) {
398+
t.true(valid);
399+
t.same(decoded.payload, readfile('data.txt'));
400+
t.end();
401+
}).verify();
402+
});
403+
404+
test('Streaming verify: HMAC with empty secret string', function (t) {
405+
const secret = '';
406+
jws.createVerify({
407+
signature: 'eyJhbGciOiJIUzI1NiJ9.b25lLCB0d28sIHRocmVlCg.V1oQ0aq6FgAoe7C2TORtYpQAbYzJoFNFZlJkTlF1e60',
408+
algorithm: 'HS256',
409+
secret: secret
410+
}).on('done', function (valid, decoded) {
411+
t.true(valid);
412+
t.same(decoded.payload, readfile('data.txt'));
413+
t.end();
414+
}).verify();
415+
});
416+
417+
test('Streaming verify: HMAC with undefined secret', function (t) {
418+
try {
419+
jws.createVerify({
420+
signature: 'eyJhbGciOiJIUzI1NiJ9.b25lLCB0d28sIHRocmVlCg.V1oQ0aq6FgAoe7C2TORtYpQAbYzJoFNFZlJkTlF1e60',
421+
algorithm: 'HS256',
422+
secret: undefined
423+
});
424+
t.fail('should have errored');
425+
t.end();
426+
} catch (error) {
427+
t.equal(error.name, 'TypeError');
428+
t.equal(error.message, 'secret must be a string or buffer or a KeyObject');
429+
t.end();
430+
}
431+
});
432+
433+
test('Streaming verify: HMAC with null secret', function (t) {
434+
try {
435+
jws.createVerify({
436+
signature: 'eyJhbGciOiJIUzI1NiJ9.b25lLCB0d28sIHRocmVlCg.V1oQ0aq6FgAoe7C2TORtYpQAbYzJoFNFZlJkTlF1e60',
437+
algorithm: 'HS256',
438+
secret: null
439+
});
440+
t.fail('should have errored');
441+
t.end();
442+
} catch (error) {
443+
t.equal(error.name, 'TypeError');
444+
t.equal(error.message, 'secret must be a string or buffer or a KeyObject');
445+
t.end();
446+
}
447+
});

0 commit comments

Comments
 (0)