Skip to content

Commit

Permalink
Merge pull request erdtman#16 from hildjj/coverage
Browse files Browse the repository at this point in the history
Coverage
  • Loading branch information
erdtman committed Aug 30, 2016
2 parents 700c783 + 4adab97 commit 1319b4d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
13 changes: 5 additions & 8 deletions lib/mac.js
Expand Up @@ -57,18 +57,15 @@ exports.read = function (data, key, externalAAD) {

return cbor.decodeFirst(data)
.then((obj) => {
const hProtected = obj[0];
// const unprotected = obj[1];
const payload = obj[2];
const tag = obj[3];
if (!Array.isArray(obj) || (obj.length < 4)) {
throw new Error('Expecting Array of length 4');
}
const [hProtected, unprotected, payload, tag] = obj;
unprotected; // prevent unused variable warning

// TODO validate protected header
return doMac('MAC0', hProtected, externalAAD, payload, 'sha256', key)
.then((calcTag) => {
// TODO: why was this here? Make sure it's not needed anymore and
// delete, along with unprotected above.
// const encoded = cbor.encode([hProtected, unprotected, payload, tag]);

if (tag.toString('hex') !== calcTag.toString('hex')) {
throw new Error('Tag mismatch');
}
Expand Down
13 changes: 10 additions & 3 deletions package.json
Expand Up @@ -4,12 +4,16 @@
"description": "JavaScript COSE implementation",
"main": "lib/index.js",
"scripts": {
"clean": "rm -rf coverage/ .nyc_output/",
"pretest": "semistandard",
"test": "ava test",
"coverage": "nyc npm test",
"coveragehtml": "nyc report -r html",
"precoveragehtml": "npm run coverage",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"live": "live-server -q --port=4003 --ignorePattern='(js|css|png)$' coverage",
"watch": "watch 'npm run coveragehtml' test lib",
"dev": "npm-run-all -p --silent watch live"
},
"repository": {
"type": "git",
Expand All @@ -30,7 +34,7 @@
"email": "joe-github@cursive.net"
}
],
"license": "Apache 2.0",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/erdtman/cose-js/issues"
},
Expand All @@ -44,8 +48,11 @@
},
"devDependencies": {
"ava": "*",
"live-server": "*",
"npm-run-all": "*",
"nyc": "*",
"semistandard": "*"
"semistandard": "*",
"watch": "*"
},
"engines": {
"node": ">=6.0"
Expand Down
21 changes: 21 additions & 0 deletions test/common.js
@@ -0,0 +1,21 @@
/* jshint esversion: 6 */
/* jslint node: true */
'use strict';
const test = require('ava');
const cose = require('../');

test('translate headers', (t) => {
let h = cose.common.TranslateHeaders({});
t.is(h.constructor.name, 'Map');
h = cose.common.TranslateHeaders({alg: 'SHA-256', crit: 2});
t.is(h.constructor.name, 'Map');
t.is(h.size, 2);
t.is(h.get(cose.common.HeaderParameters.alg), 'SHA-256');
t.is(h.get(cose.common.HeaderParameters.crit), 2);
});

test('invalid', (t) => {
t.throws(() => {
cose.common.TranslateHeaders({'fizzle stomp': 12});
});
});
14 changes: 11 additions & 3 deletions test/mac.js
Expand Up @@ -12,9 +12,9 @@ var hUnprotected = {};

test('basic mac', t => {
return cose.mac.create(hProtected,
hUnprotected,
cbor.encode(payload),
key)
hUnprotected,
cbor.encode(payload),
key)
.then((buf) => {
t.true(Buffer.isBuffer(buf));
t.true(buf.length > 0);
Expand All @@ -29,3 +29,11 @@ test('basic mac', t => {
t.deepEqual(obj, payload);
});
});

test('errors', t => {
t.throws(() => {
cose.mac.create({});
});
t.throws(() => cose.mac.create({'alg': 'fizzle blorp'}));
t.throws(cose.mac.read(cbor.encode('foo'), key));
});

0 comments on commit 1319b4d

Please sign in to comment.