Skip to content

Instantly share code, notes, and snippets.

@ashtewari
Last active December 20, 2015 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashtewari/6194358 to your computer and use it in GitHub Desktop.
Save ashtewari/6194358 to your computer and use it in GitHub Desktop.
nodeunit unit test for validating xml-crypto generated signed xml.
var testrunner = require('nodeunit').reporters.default;
process.chdir(__dirname);
testrunner.run(['.']);
exports.testSignedXml = function(test){
var SignedXml = require('xml-crypto').SignedXml
, fs = require('fs')
, dom = require('xmldom').DOMParser;
var xml = "<library>" +
"<book>" +
"<name>Harry Potter</name>" +
"</book>" +
"</library>"
var sig = new SignedXml()
sig.addReference("//*[local-name(.)='book']")
sig.signingKey = fs.readFileSync("private.pem")
sig.computeSignature(xml)
var signed = sig.getSignedXml();
console.log(signed);
var doc = new dom().parseFromString(signed);
/*
Expecting this structure:
<library>
<book Id="_0">
<name>Harry Potter</name>
</book>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="#_0">
<Transforms><Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>cdiS43aFDQMnb3X8yaIUej3+z9Q=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>J79hiSUrKdLOuX....Mthy1M=</SignatureValue>
</Signature>
</library>
*/
test.ok(doc.documentElement.nodeName == "library", "root node = <library>.");
test.ok(doc.childNodes.length == 1, "only one root node is expected.");
test.ok(doc.documentElement.childNodes.length == 2, "<library> should have two child nodes : <book> and <Signature>");
test.done();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment