diff --git a/tests/reciprocate.js b/tests/reciprocate.js index f6b68bc..b412883 100644 --- a/tests/reciprocate.js +++ b/tests/reciprocate.js @@ -12,18 +12,80 @@ var refs = { , privJwk: RSA.exportPrivateJwk({ privateKeyPem: privkeyPemRef }) }; +var hasUrsa; +var imported; + +try { + hasUrsa = require('ursa') && true; +} catch(e) { + hasUrsa = false; +} + + + +// +// +// PEM tests +// +// console.log(''); -console.log('valid pem', privkeyPemRef === refs.privPem); +console.log('JWK -> PEM ?', privkeyPemRef === refs.privPem); if (privkeyPemRef !== refs.privPem) { - console.log(privkeyPemRef); - console.log(refs.privPem); + // Watch out for tricky whitespaces (\n instead of \r\n, trailing \r\n, etc) + console.log('REF:'); + console.log(JSON.stringify(privkeyPemRef)); + console.log('GEN:'); + console.log(JSON.stringify(refs.privPem)); + throw new Error("Failed to validate importedJwk against referencePem"); } console.log(''); -console.log('valid jwk', privkeyJwkRef.n === refs.privJwk.n); +console.log('PEM -> _ -> PEM ?', privkeyPemRef === refs.privPem); +if (hasUrsa) { + imported = RSA.import({ privateKeyPem: privkeyPemRef }); + refs.privPem2 = RSA.exportPrivatePem({ _ursa: imported._ursa }); +} +else { + imported = RSA.import({ privateKeyPem: privkeyPemRef }); + refs.privPem2 = RSA.exportPrivatePem({ _forge: imported._forge }); +} +if (privkeyPemRef !== refs.privPem2) { + console.log('REF:'); + console.log(JSON.stringify(privkeyPemRef)); + console.log('GEN:'); + console.log(JSON.stringify(refs.privPem2)); + throw new Error("Failed to validate importedPem against referencePem"); +} + +console.log(''); + + + +// +// +// JWK tests +// +// +console.log(''); +console.log('PEM -> JWK', privkeyJwkRef.n === refs.privJwk.n); if (privkeyJwkRef.n !== refs.privJwk.n) { + console.log('REF:'); console.log(privkeyJwkRef); + console.log('GEN:'); console.log(refs.privJwk); + throw new Error("Failed to validate importedPem against referenceJwk"); +} + +console.log(''); +console.log('JWK -> _ -> JWK', privkeyJwkRef.n === refs.privJwk2.n); +imported = RSA.import({ privateKeyJwk: privkeyJwkRef }); +refs.privJwk2 = RSA.exportPrivateJwk({ _forge: imported._forge }); +if (privkeyJwkRef.n !== refs.privJwk2.n) { + console.log('REF:'); + console.log(privkeyJwkRef); + console.log('GEN:'); + console.log(refs.privJwk2); + throw new Error("Failed to validate importedJwk against referenceJwk"); } console.log('');