107 lines
3.6 KiB
JavaScript
107 lines
3.6 KiB
JavaScript
var Keypairs = require('./');
|
|
|
|
/* global Promise*/
|
|
Keypairs.parseOrGenerate({ key: '' }).then(function (pair) {
|
|
// should NOT have any warning output
|
|
if (!pair.private || !pair.public) {
|
|
throw new Error("missing key pairs");
|
|
}
|
|
|
|
return Promise.all([
|
|
// Testing Public Part of key
|
|
Keypairs.export({ jwk: pair.public }).then(function (pem) {
|
|
if (!/--BEGIN PUBLIC/.test(pem)) {
|
|
throw new Error("did not export public pem");
|
|
}
|
|
return Promise.all([
|
|
Keypairs.parse({ key: pem }).then(function (pair) {
|
|
if (pair.private) {
|
|
throw new Error("shouldn't have private part");
|
|
}
|
|
return true;
|
|
})
|
|
, Keypairs.parse({ key: pem, private: true }).then(function () {
|
|
var err = new Error("should have thrown an error when private key was required and public pem was given");
|
|
err.code = 'NOERR';
|
|
throw err;
|
|
}).catch(function (e) {
|
|
if ('NOERR' === e.code) { throw e; }
|
|
return true;
|
|
})
|
|
]).then(function () {
|
|
return true;
|
|
});
|
|
})
|
|
// Testing Private Part of Key
|
|
, Keypairs.export({ jwk: pair.private }).then(function (pem) {
|
|
if (!/--BEGIN .*PRIVATE KEY--/.test(pem)) {
|
|
throw new Error("did not export private pem: " + pem);
|
|
}
|
|
return Promise.all([
|
|
Keypairs.parse({ key: pem }).then(function (pair) {
|
|
if (!pair.private) {
|
|
throw new Error("should have private part");
|
|
}
|
|
if (!pair.public) {
|
|
throw new Error("should have public part also");
|
|
}
|
|
return true;
|
|
})
|
|
, Keypairs.parse({ key: pem, public: true }).then(function (pair) {
|
|
if (pair.private) {
|
|
throw new Error("should NOT have private part");
|
|
}
|
|
if (!pair.public) {
|
|
throw new Error("should have the public part though");
|
|
}
|
|
return true;
|
|
})
|
|
]).then(function () {
|
|
return true;
|
|
});
|
|
})
|
|
, Keypairs.parseOrGenerate({ key: 'not a key', public: true }).then(function (pair) {
|
|
// SHOULD have warning output
|
|
if (!pair.private || !pair.public) {
|
|
throw new Error("missing key pairs (should ignore 'public')");
|
|
}
|
|
return true;
|
|
})
|
|
, Keypairs.parse({ key: JSON.stringify(pair.private) }).then(function (pair) {
|
|
if (!pair.private || !pair.public) {
|
|
throw new Error("missing key pairs (stringified jwt)");
|
|
}
|
|
return true;
|
|
})
|
|
, Keypairs.parse({ key: JSON.stringify(pair.private), public: true }).then(function (pair) {
|
|
if (pair.private) {
|
|
throw new Error("has private key when it shouldn't");
|
|
}
|
|
if (!pair.public) {
|
|
throw new Error("doesn't have public key when it should");
|
|
}
|
|
return true;
|
|
})
|
|
, Keypairs.parse({ key: JSON.stringify(pair.public), private: true }).then(function () {
|
|
var err = new Error("should have thrown an error when private key was required and public jwk was given");
|
|
err.code = 'NOERR';
|
|
throw err;
|
|
}).catch(function (e) {
|
|
if ('NOERR' === e.code) { throw e; }
|
|
return true;
|
|
})
|
|
]).then(function (results) {
|
|
if (results.length && results.every(function (v) { return true === v; })) {
|
|
console.info("If a warning prints right above this, it's a pass");
|
|
console.log("PASS");
|
|
process.exit(0);
|
|
} else {
|
|
throw new Error("didn't get all passes (but no errors either)");
|
|
}
|
|
});
|
|
}).catch(function (e) {
|
|
console.error("Caught an unexpected (failing) error:");
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|