You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2011/01/18 14:41:00 UTC
svn commit: r1060367 -
/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/
Author: coheigea
Date: Tue Jan 18 13:40:59 2011
New Revision: 1060367
URL: http://svn.apache.org/viewvc?rev=1060367&view=rev
Log:
[SANTUARIO-257] - More work on the tests.
Removed:
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/AllTests.java
Modified:
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/CreateSignatureTest.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/ECDSASignatureTest.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/HMACOutputLengthTest.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/IndexedDocument.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/InvalidKeyTest.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/KeyValueTest.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/NoKeyInfoTest.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XPointerResourceResolver.java
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/CreateSignatureTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/CreateSignatureTest.java?rev=1060367&r1=1060366&r2=1060367&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/CreateSignatureTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/CreateSignatureTest.java Tue Jan 18 13:40:59 2011
@@ -39,50 +39,41 @@ import org.apache.xml.security.utils.XML
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
/**
* Tests that create signatures.
*
* @author Sean Mullan
*/
-public class CreateSignatureTest extends TestCase {
+public class CreateSignatureTest extends org.junit.Assert {
/** {@link org.apache.commons.logging} logging facility */
static org.apache.commons.logging.Log log =
- org.apache.commons.logging.LogFactory.getLog
- (CreateSignatureTest.class.getName());
+ org.apache.commons.logging.LogFactory.getLog(CreateSignatureTest.class.getName());
private static final String BASEDIR = System.getProperty("basedir");
private static final String SEP = System.getProperty("file.separator");
- public static Test suite() {
- return new TestSuite(CreateSignatureTest.class);
- }
-
- public CreateSignatureTest(String name) {
- super(name);
- }
-
- public static void main(String[] args) {
- String[] testCaseName = { "-noloading",
- CreateSignatureTest.class.getName() };
-
- junit.textui.TestRunner.main(testCaseName);
+ private KeyPair kp = null;
+ private javax.xml.parsers.DocumentBuilder db;
+
+ public CreateSignatureTest() throws Exception {
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+ dbf.setNamespaceAware(true);
+ db = dbf.newDocumentBuilder();
+ org.apache.xml.security.Init.init();
+ kp = KeyPairGenerator.getInstance("RSA").genKeyPair();
}
-
/**
* Test for bug 36044 - Canonicalizing an empty node-set throws an
* ArrayIndexOutOfBoundsException.
*/
+ @org.junit.Test
public void testEmptyNodeSet() throws Exception {
-
Document doc = db.newDocument();
- Element envelope = doc.createElementNS("http://www.usps.gov/",
- "Envelope");
+ Element envelope = doc.createElementNS("http://www.usps.gov/", "Envelope");
envelope.appendChild(doc.createTextNode("\n"));
doc.appendChild(envelope);
@@ -106,48 +97,41 @@ public class CreateSignatureTest extends
XPathContainer xpathC = new XPathContainer(doc);
xpathC.setXPath("self::text()");
- transforms.addTransform(Transforms.TRANSFORM_XPATH,
- xpathC.getElementPlusReturns());
- sig.addDocument("#object-1", transforms,
- Constants.ALGO_ID_DIGEST_SHA1, null,
- "http://www.w3.org/2000/09/xmldsig#Object");
+ transforms.addTransform(Transforms.TRANSFORM_XPATH, xpathC.getElementPlusReturns());
+ sig.addDocument(
+ "#object-1", transforms, Constants.ALGO_ID_DIGEST_SHA1, null,
+ "http://www.w3.org/2000/09/xmldsig#Object"
+ );
KeyStore ks = KeyStore.getInstance("JKS");
FileInputStream fis = null;
if (BASEDIR != null && !"".equals(BASEDIR)) {
- fis = new FileInputStream(BASEDIR + SEP +
- "data/org/apache/xml/security/samples/input/keystore.jks");
+ fis =
+ new FileInputStream(BASEDIR + SEP
+ + "data/org/apache/xml/security/samples/input/keystore.jks"
+ );
} else {
- fis = new FileInputStream(
- "data/org/apache/xml/security/samples/input/keystore.jks");
+ fis =
+ new FileInputStream("data/org/apache/xml/security/samples/input/keystore.jks");
}
ks.load(fis, "xmlsecurity".toCharArray());
- PrivateKey privateKey = (PrivateKey) ks.getKey("test",
- "xmlsecurity".toCharArray());
+ PrivateKey privateKey = (PrivateKey) ks.getKey("test", "xmlsecurity".toCharArray());
sig.sign(privateKey);
}
-
- KeyPair kp = null;
- javax.xml.parsers.DocumentBuilder db;
-
- protected void setUp() throws Exception {
- javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory
- .newInstance();
- dbf.setNamespaceAware(true);
- db = dbf.newDocumentBuilder();
- org.apache.xml.security.Init.init();
- kp=KeyPairGenerator.getInstance("RSA").genKeyPair();
- }
+
+ @org.junit.Test
public void testOne() throws Exception {
doVerify(doSign());
doVerify(doSign());
}
+ @org.junit.Test
public void testTwo() throws Exception {
doSignWithCert();
}
+ @org.junit.Test
public void testWithNSPrefixDisabled() throws Exception {
String prefix = Constants.getSignatureSpecNSprefix();
try {
@@ -160,25 +144,25 @@ public class CreateSignatureTest extends
}
}
- String doSign() throws Exception {
+ private String doSign() throws Exception {
PrivateKey privateKey = kp.getPrivate();
org.w3c.dom.Document doc = db.newDocument();
doc.appendChild(doc.createComment(" Comment before "));
- Element root = doc.createElementNS("",
- "RootElement");
+ Element root = doc.createElementNS("", "RootElement");
doc.appendChild(root);
root.appendChild(doc.createTextNode("Some simple text\n"));
- Element canonElem = XMLUtils.createElementInSignatureSpace(doc,
- Constants._TAG_CANONICALIZATIONMETHOD);
- canonElem.setAttributeNS(null, Constants._ATT_ALGORITHM,
- Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
-
- SignatureAlgorithm signatureAlgorithm = new SignatureAlgorithm(doc,
- XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);
- XMLSignature sig = new XMLSignature(doc, null, signatureAlgorithm
- .getElement(), canonElem);
+ Element canonElem =
+ XMLUtils.createElementInSignatureSpace(doc, Constants._TAG_CANONICALIZATIONMETHOD);
+ canonElem.setAttributeNS(
+ null, Constants._ATT_ALGORITHM, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS
+ );
+
+ SignatureAlgorithm signatureAlgorithm =
+ new SignatureAlgorithm(doc, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);
+ XMLSignature sig =
+ new XMLSignature(doc, null, signatureAlgorithm.getElement(), canonElem);
root.appendChild(sig.getElement());
doc.appendChild(doc.createComment(" Comment after "));
@@ -196,36 +180,35 @@ public class CreateSignatureTest extends
return new String(bos.toByteArray());
}
- String doSignWithCert() throws Exception {
+ private String doSignWithCert() throws Exception {
KeyStore ks = KeyStore.getInstance("JKS");
FileInputStream fis = null;
if (BASEDIR != null && !"".equals(BASEDIR)) {
fis = new FileInputStream(BASEDIR + SEP +
- "data/test.jks");
+ "data/test.jks");
} else {
fis = new FileInputStream("data/test.jks");
}
ks.load(fis, "changeit".toCharArray());
- PrivateKey privateKey = (PrivateKey) ks.getKey("mullan",
- "changeit".toCharArray());
+ PrivateKey privateKey = (PrivateKey) ks.getKey("mullan", "changeit".toCharArray());
org.w3c.dom.Document doc = db.newDocument();
X509Certificate signingCert = (X509Certificate) ks.getCertificate("mullan");
doc.appendChild(doc.createComment(" Comment before "));
- Element root = doc.createElementNS("",
- "RootElement");
+ Element root = doc.createElementNS("", "RootElement");
doc.appendChild(root);
root.appendChild(doc.createTextNode("Some simple text\n"));
- Element canonElem = XMLUtils.createElementInSignatureSpace(doc,
- Constants._TAG_CANONICALIZATIONMETHOD);
- canonElem.setAttributeNS(null, Constants._ATT_ALGORITHM,
- Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
-
- SignatureAlgorithm signatureAlgorithm = new SignatureAlgorithm(doc,
- XMLSignature.ALGO_ID_SIGNATURE_DSA);
- XMLSignature sig = new XMLSignature(doc, null, signatureAlgorithm
- .getElement(), canonElem);
+ Element canonElem =
+ XMLUtils.createElementInSignatureSpace(doc, Constants._TAG_CANONICALIZATIONMETHOD);
+ canonElem.setAttributeNS(
+ null, Constants._ATT_ALGORITHM, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS
+ );
+
+ SignatureAlgorithm signatureAlgorithm =
+ new SignatureAlgorithm(doc, XMLSignature.ALGO_ID_SIGNATURE_DSA);
+ XMLSignature sig =
+ new XMLSignature(doc, null, signatureAlgorithm.getElement(), canonElem);
root.appendChild(sig.getElement());
doc.appendChild(doc.createComment(" Comment after "));
@@ -244,21 +227,23 @@ public class CreateSignatureTest extends
return new String(bos.toByteArray());
}
- void doVerify(String signedXML) throws Exception {
+ private void doVerify(String signedXML) throws Exception {
org.w3c.dom.Document doc = db.parse(new ByteArrayInputStream(signedXML.getBytes()));
Element nscontext = TestUtils.createDSctx(doc, "ds",Constants.SignatureSpecNS);
- Element sigElement = (Element) XPathAPI.selectSingleNode(doc,"//ds:Signature[1]", nscontext);
+ Element sigElement =
+ (Element) XPathAPI.selectSingleNode(doc,"//ds:Signature[1]", nscontext);
XMLSignature signature = new XMLSignature(sigElement, "");
KeyInfo ki = signature.getKeyInfo();
if (ki == null) {
- throw new RuntimeException("No keyinfo");
+ throw new RuntimeException("No keyinfo");
}
PublicKey pk = signature.getKeyInfo().getPublicKey();
if (pk == null) {
- throw new RuntimeException("No public key");
+ throw new RuntimeException("No public key");
}
- assertTrue(signature.checkSignatureValue(pk) );
+ assertTrue(signature.checkSignatureValue(pk));
}
+
}
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/ECDSASignatureTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/ECDSASignatureTest.java?rev=1060367&r1=1060366&r2=1060367&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/ECDSASignatureTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/ECDSASignatureTest.java Tue Jan 18 13:40:59 2011
@@ -28,10 +28,6 @@ import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.X509Certificate;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
import org.apache.xml.security.algorithms.SignatureAlgorithm;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.security.keys.KeyInfo;
@@ -47,7 +43,7 @@ import org.w3c.dom.Element;
*
* @author Wolfgang Glas
*/
-public class ECDSASignatureTest extends TestCase {
+public class ECDSASignatureTest extends org.junit.Assert {
/** {@link org.apache.commons.logging} logging facility */
private static org.apache.commons.logging.Log log =
@@ -63,37 +59,16 @@ public class ECDSASignatureTest extends
private javax.xml.parsers.DocumentBuilder db;
- private File makeDataFile(String relPath) {
- if (BASEDIR != null && !"".equals(BASEDIR)) {
- return new File(BASEDIR + SEP + relPath);
- } else {
- return new File(relPath);
- }
- }
-
- public static Test suite() {
- return new TestSuite(ECDSASignatureTest.class);
- }
-
- public ECDSASignatureTest(String name) {
- super(name);
- }
-
- public static void main(String[] args) {
- String[] testCaseName = {"-noloading", ECDSASignatureTest.class.getName()};
- junit.textui.TestRunner.main(testCaseName);
- }
-
- protected void setUp() throws Exception {
+ public ECDSASignatureTest() throws Exception {
//
// If the BouncyCastle provider is not installed, then try to load it
// via reflection. If it is not available, then skip this test as it is
// required for elliptic curves
//
if (Security.getProvider("BC") == null) {
- Constructor cons = null;
+ Constructor<?> cons = null;
try {
- Class c = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
+ Class<?> c = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
cons = c.getConstructor(new Class[] {});
} catch (Exception e) {
//ignore
@@ -109,17 +84,20 @@ public class ECDSASignatureTest extends
javax.xml.parsers.DocumentBuilderFactory dbf =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
- log.info("dbf.isIgnoringComments()=" + dbf.isIgnoringComments());
- log.info("dbf.isIgnoringElementContentWhitespace()=" + dbf.isIgnoringElementContentWhitespace());
+ log.info("dbf.isIgnoringComments() = " + dbf.isIgnoringComments());
+ log.info(
+ "dbf.isIgnoringElementContentWhitespace() = "
+ + dbf.isIgnoringElementContentWhitespace()
+ );
String id = "http://apache.org/xml/properties/dom/document-class-name";
-
dbf.setAttribute(id, IndexedDocument.class.getName());
db = dbf.newDocumentBuilder();
org.apache.xml.security.Init.init();
}
+ @org.junit.Test
public void testOne() throws Exception {
if (Security.getProvider("BC") == null) {
return;
@@ -135,6 +113,7 @@ public class ECDSASignatureTest extends
doVerify(doSign());
}
+ @org.junit.Test
public void testTwo() throws Exception {
if (Security.getProvider("BC") == null) {
return;
@@ -147,6 +126,7 @@ public class ECDSASignatureTest extends
doVerify(is);
}
+ @org.junit.Test
public void testThree() throws Exception {
if (Security.getProvider("BC") == null) {
return;
@@ -168,15 +148,16 @@ public class ECDSASignatureTest extends
doc.appendChild(root);
root.appendChild(doc.createTextNode("Some simple text\n"));
- Element canonElem = XMLUtils.createElementInSignatureSpace(doc,
- Constants._TAG_CANONICALIZATIONMETHOD);
- canonElem.setAttributeNS(null, Constants._ATT_ALGORITHM,
- Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
-
- SignatureAlgorithm signatureAlgorithm = new SignatureAlgorithm(doc,
- XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA1);
- XMLSignature sig = new XMLSignature(doc, null, signatureAlgorithm
- .getElement(), canonElem);
+ Element canonElem =
+ XMLUtils.createElementInSignatureSpace(doc, Constants._TAG_CANONICALIZATIONMETHOD);
+ canonElem.setAttributeNS(
+ null, Constants._ATT_ALGORITHM, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS
+ );
+
+ SignatureAlgorithm signatureAlgorithm =
+ new SignatureAlgorithm(doc, XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA1);
+ XMLSignature sig =
+ new XMLSignature(doc, null, signatureAlgorithm.getElement(), canonElem);
root.appendChild(sig.getElement());
doc.appendChild(doc.createComment(" Comment after "));
@@ -202,23 +183,31 @@ public class ECDSASignatureTest extends
private void doVerify(InputStream is) throws Exception {
org.w3c.dom.Document doc = this.db.parse(is);
Element nscontext = XMLUtils.createDSctx(doc, "ds",Constants.SignatureSpecNS);
- Element sigElement = (Element) XPathAPI.selectSingleNode(doc,"//ds:Signature[1]", nscontext);
+ Element sigElement =
+ (Element) XPathAPI.selectSingleNode(doc,"//ds:Signature[1]", nscontext);
XMLSignature signature = new XMLSignature(sigElement, "");
signature.addResourceResolver(new XPointerResourceResolver(sigElement));
KeyInfo ki = signature.getKeyInfo();
-
if (ki == null) {
- throw new RuntimeException("No keyinfo");
+ throw new RuntimeException("No keyinfo");
}
X509Certificate cert = signature.getKeyInfo().getX509Certificate();
if (cert == null) {
- throw new RuntimeException("No certificate");
+ throw new RuntimeException("No certificate");
}
assertTrue(signature.checkSignatureValue(cert) );
}
+
+ private File makeDataFile(String relPath) {
+ if (BASEDIR != null && !"".equals(BASEDIR)) {
+ return new File(BASEDIR + SEP + relPath);
+ } else {
+ return new File(relPath);
+ }
+ }
/**
* DO NOT DELETE THIS COMMENTED OUT METHOD!
@@ -231,7 +220,6 @@ public class ECDSASignatureTest extends
* keystore used for this test ("data/org/apache/xml/security/samples/input/ecdsa.jks").
*
private static void setUpKeyAndCertificate() throws Exception {
-
java.security.KeyPairGenerator kpg =
java.security.KeyPairGenerator.getInstance("ECDSA");
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/HMACOutputLengthTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/HMACOutputLengthTest.java?rev=1060367&r1=1060366&r2=1060367&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/HMACOutputLengthTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/HMACOutputLengthTest.java Tue Jan 18 13:40:59 2011
@@ -22,9 +22,6 @@ import javax.xml.parsers.DocumentBuilder
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
import org.apache.xml.security.Init;
import org.apache.xml.security.c14n.Canonicalizer;
@@ -32,41 +29,27 @@ import org.apache.xml.security.signature
import org.apache.xml.security.signature.XMLSignatureException;
import org.apache.xml.security.utils.Constants;
-public class HMACOutputLengthTest extends TestCase {
+public class HMACOutputLengthTest extends org.junit.Assert {
- private static DocumentBuilderFactory dbf = null;
-
- protected void setUp() throws Exception {
- Init.init();
- dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- dbf.setValidating(false);
- }
-
/** {@link org.apache.commons.logging} logging facility */
static org.apache.commons.logging.Log log =
org.apache.commons.logging.LogFactory.getLog
(HMACOutputLengthTest.class.getName());
+
+ private static DocumentBuilderFactory dbf = null;
private static final String BASEDIR =
System.getProperty("basedir") == null ? "./": System.getProperty("basedir");
private static final String SEP = System.getProperty("file.separator");
- public static Test suite() {
- return new TestSuite(HMACOutputLengthTest.class);
- }
-
- public HMACOutputLengthTest(String name) {
- super(name);
- }
-
- public static void main(String[] args) {
- String[] testCaseName = { "-noloading",
- HMACOutputLengthTest.class.getName() };
-
- junit.textui.TestRunner.main(testCaseName);
+ public HMACOutputLengthTest() throws Exception {
+ Init.init();
+ dbf = DocumentBuilderFactory.newInstance();
+ dbf.setNamespaceAware(true);
+ dbf.setValidating(false);
}
-
+
+ @org.junit.Test
public void test_signature_enveloping_hmac_sha1_trunclen_0() throws Exception {
try {
validate("signature-enveloping-hmac-sha1-trunclen-0-attack.xml");
@@ -80,6 +63,8 @@ public class HMACOutputLengthTest extend
}
}
}
+
+ @org.junit.Test
public void test_signature_enveloping_hmac_sha1_trunclen_8() throws Exception {
try {
validate("signature-enveloping-hmac-sha1-trunclen-8-attack.xml");
@@ -92,32 +77,15 @@ public class HMACOutputLengthTest extend
}
}
}
-
- private static void validate(String data) throws Exception {
- // System.out.println("Validating " + data);
- File file = new File(BASEDIR + SEP + "data" + SEP + "javax" + SEP + "xml" + SEP + "crypto" + SEP + "dsig" + SEP, data);
-
- Document doc = dbf.newDocumentBuilder().parse(file);
- NodeList nl =
- doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature");
- if (nl.getLength() == 0) {
- throw new Exception("Couldn't find signature Element");
- }
- Element sigElement = (Element) nl.item(0);
- XMLSignature signature = new XMLSignature
- (sigElement, file.toURI().toString());
- SecretKey sk = signature.createSecretKey("secret".getBytes("ASCII"));
- System.out.println
- ("Validation status: " + signature.checkSignatureValue(sk));
- }
-
+
+ @org.junit.Test
public void test_generate_hmac_sha1_40() throws Exception {
- // System.out.println("Generating ");
-
Document doc = dbf.newDocumentBuilder().newDocument();
- XMLSignature sig = new XMLSignature
- (doc, null, XMLSignature.ALGO_ID_MAC_HMAC_SHA1, 40,
- Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
+ XMLSignature sig =
+ new XMLSignature(
+ doc, null, XMLSignature.ALGO_ID_MAC_HMAC_SHA1,
+ 40, Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS
+ );
try {
sig.sign(getSecretKey("secret".getBytes("ASCII")));
fail("Expected HMACOutputLength Exception");
@@ -131,11 +99,29 @@ public class HMACOutputLengthTest extend
}
}
- private static SecretKey getSecretKey(final byte[] secret) {
+ private boolean validate(String data) throws Exception {
+ File file =
+ new File(BASEDIR + SEP + "data" + SEP + "javax" + SEP + "xml"
+ + SEP + "crypto" + SEP + "dsig" + SEP, data);
+
+ Document doc = dbf.newDocumentBuilder().parse(file);
+ NodeList nl =
+ doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature");
+ if (nl.getLength() == 0) {
+ throw new Exception("Couldn't find signature Element");
+ }
+ Element sigElement = (Element) nl.item(0);
+ XMLSignature signature = new XMLSignature(sigElement, file.toURI().toString());
+ SecretKey sk = signature.createSecretKey("secret".getBytes("ASCII"));
+ return signature.checkSignatureValue(sk);
+ }
+
+ private SecretKey getSecretKey(final byte[] secret) {
return new SecretKey() {
public String getFormat() { return "RAW"; }
public byte[] getEncoded() { return secret; }
public String getAlgorithm(){ return "SECRET"; }
};
}
+
}
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/IndexedDocument.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/IndexedDocument.java?rev=1060367&r1=1060366&r2=1060367&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/IndexedDocument.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/IndexedDocument.java Tue Jan 18 13:40:59 2011
@@ -30,43 +30,35 @@ import org.w3c.dom.Node;
*
* @author wglas
*/
-public class IndexedDocument extends DocumentImpl
-{
+public class IndexedDocument extends DocumentImpl {
private static final long serialVersionUID = -1342041999864449753L;
private static final String ID_ATTR = "Id";
-
- private Map idMap;
-
- private void addToIdMap(Element element)
- {
+
+ private Map<String, Element> idMap;
+
+ private void addToIdMap(Element element) {
String id = element.getAttribute(ID_ATTR);
-
- if (id != null)
+
+ if (id != null) {
this.idMap.put(id,element);
-
- for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling())
- {
- if (node instanceof Element)
- {
+ }
+
+ for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
+ if (node instanceof Element) {
Element e = (Element) node;
this.addToIdMap(e);
}
}
}
-
- /* (non-Javadoc)
- * @see org.apache.xerces.dom.CoreDocumentImpl#getElementById(java.lang.String)
- */
- public Element getElementById(String id)
- {
- if (this.idMap == null)
- {
- this.idMap = new HashMap();
+
+ public Element getElementById(String id) {
+ if (this.idMap == null) {
+ this.idMap = new HashMap<String, Element>();
this.addToIdMap(this.getDocumentElement());
}
-
+
return (Element)this.idMap.get(id);
}
-
+
}
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/InvalidKeyTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/InvalidKeyTest.java?rev=1060367&r1=1060366&r2=1060367&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/InvalidKeyTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/InvalidKeyTest.java Tue Jan 18 13:40:59 2011
@@ -28,29 +28,22 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
/**
* Test case contributed by Matthias Germann for testing that bug 43239 is
* fixed: "No installed provider supports this key" when checking a RSA
* signature against a DSA key before RSA key.
*/
-public class InvalidKeyTest extends TestCase {
-
- static {
- Init.init();
- }
-
- public static Test suite() {
- return new TestSuite(InvalidKeyTest.class);
- }
+public class InvalidKeyTest extends org.junit.Assert {
private static final String BASEDIR =
System.getProperty("basedir") == null ? "./": System.getProperty("basedir");
private static final String SEP = System.getProperty("file.separator");
+
+ static {
+ Init.init();
+ }
+ @org.junit.Test
public void test() throws Exception {
FileInputStream input = new FileInputStream(BASEDIR + SEP +
"data/org/apache/xml/security/samples/input/truststore.jks");
@@ -66,7 +59,7 @@ public class InvalidKeyTest extends Test
validate(trustStore.getCertificate("a70-garaio-frontend-u").getPublicKey());
}
- public void validate(PublicKey pk) throws Exception {
+ private void validate(PublicKey pk) throws Exception {
FileInputStream is = new FileInputStream(BASEDIR + SEP +
"data/org/apache/xml/security/samples/input/test-assertion.xml");
@@ -82,4 +75,5 @@ public class InvalidKeyTest extends Test
// System.out.println("VALIDATION OK" );
}
+
}
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/KeyValueTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/KeyValueTest.java?rev=1060367&r1=1060366&r2=1060367&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/KeyValueTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/KeyValueTest.java Tue Jan 18 13:40:59 2011
@@ -28,51 +28,38 @@ import org.apache.xml.security.keys.KeyI
import org.apache.xml.security.keys.content.KeyValue;
import org.apache.xml.security.signature.XMLSignature;
import org.apache.xml.security.utils.Constants;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-public class KeyValueTest extends TestCase {
+public class KeyValueTest extends org.junit.Assert {
private static final String BASEDIR = System.getProperty("basedir");
private static final String SEP = System.getProperty("file.separator");
private DocumentBuilder db;
- public KeyValueTest(String name) {
- super(name);
- }
-
- public static Test suite() {
- TestSuite suite = new TestSuite(KeyValueTest.class);
- return suite;
- }
-
- public void setUp() throws Exception {
+ public KeyValueTest() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
db = dbf.newDocumentBuilder();
}
-
+ @org.junit.Test
public void testDSAPublicKey() throws Exception {
-
File f = null;
+ String filename =
+ "data/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/signature-enveloping-dsa.xml";
if (BASEDIR != null && !"".equals(BASEDIR)) {
- f = new File(BASEDIR + SEP +
- "data/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/signature-enveloping-dsa.xml");
+ f = new File(BASEDIR + SEP + filename);
} else {
- f = new File(
- "data/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/signature-enveloping-dsa.xml");
+ f = new File(filename);
}
Document doc = db.parse(new FileInputStream(f));
- NodeList nl = doc.getElementsByTagNameNS
- (Constants.SignatureSpecNS, "Signature");
+ NodeList nl = doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature");
XMLSignature sig = new XMLSignature
- ((Element) nl.item(0), f.toURL().toString());
+ ((Element) nl.item(0), f.toURI().toURL().toString());
KeyInfo ki = sig.getKeyInfo();
KeyValue kv = ki.itemKeyValue(0);
PublicKey pk = kv.getPublicKey();
assertNotNull(pk);
}
+
}
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/NoKeyInfoTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/NoKeyInfoTest.java?rev=1060367&r1=1060366&r2=1060367&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/NoKeyInfoTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/NoKeyInfoTest.java Tue Jan 18 13:40:59 2011
@@ -26,48 +26,36 @@ import org.w3c.dom.NodeList;
import org.apache.xml.security.keys.KeyInfo;
import org.apache.xml.security.signature.XMLSignature;
import org.apache.xml.security.utils.Constants;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-public class NoKeyInfoTest extends TestCase {
+public class NoKeyInfoTest extends org.junit.Assert {
private static final String BASEDIR = System.getProperty("basedir");
private static final String SEP = System.getProperty("file.separator");
private DocumentBuilder db;
- public NoKeyInfoTest(String name) {
- super(name);
- }
-
- public static Test suite() {
- TestSuite suite = new TestSuite(NoKeyInfoTest.class);
- return suite;
- }
-
- public void setUp() throws Exception {
+ public NoKeyInfoTest() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
db = dbf.newDocumentBuilder();
}
-
+ @org.junit.Test
public void testNullKeyInfo() throws Exception {
File f = null;
+ String filename =
+ "data/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/signature-enveloping-hmac-sha1.xml";
if (BASEDIR != null && !"".equals(BASEDIR)) {
- f = new File(BASEDIR + SEP +
- "data/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/signature-enveloping-hmac-sha1.xml");
+ f = new File(BASEDIR + SEP + filename);
} else {
- f = new File(
- "data/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/signature-enveloping-hmac-sha1.xml");
+ f = new File(filename);
}
Document doc = db.parse(new FileInputStream(f));
- NodeList nl = doc.getElementsByTagNameNS
- (Constants.SignatureSpecNS, "Signature");
+ NodeList nl = doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature");
XMLSignature sig = new XMLSignature
- ((Element) nl.item(0), f.toURL().toString());
+ ((Element) nl.item(0), f.toURI().toURL().toString());
KeyInfo ki = sig.getKeyInfo();
assertNull(ki);
}
+
}
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XPointerResourceResolver.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XPointerResourceResolver.java?rev=1060367&r1=1060366&r2=1060367&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XPointerResourceResolver.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XPointerResourceResolver.java Tue Jan 18 13:40:59 2011
@@ -44,198 +44,190 @@ import org.w3c.dom.NodeList;
*
* @author wglas
*/
-public class XPointerResourceResolver extends ResourceResolverSpi
-{
+public class XPointerResourceResolver extends ResourceResolverSpi {
private static Log log = LogFactory.getLog(XPointerResourceResolver.class);
-
+
private static final String XP_OPEN = "xpointer(";
private static final String XNS_OPEN = "xmlns(";
-
+
private Node baseNode;
-
- public XPointerResourceResolver(Node baseNode)
- {
+
+ public XPointerResourceResolver(Node baseNode) {
this.baseNode = baseNode;
}
-
- private static class XPointerPrefixResolver extends PrefixResolverDefault
- {
- private Map extraPrefixes;
-
- public XPointerPrefixResolver(Node node)
- {
+
+ private static class XPointerPrefixResolver extends PrefixResolverDefault {
+ private Map<String, String> extraPrefixes;
+
+ public XPointerPrefixResolver(Node node) {
super(node);
- this.extraPrefixes = new HashMap();
+ this.extraPrefixes = new HashMap<String, String>();
+ }
+
+ public void addExtraPrefix (String pfx, String nsURI) {
+ this.extraPrefixes.put(pfx, nsURI);
}
- public void addExtraPrefix (String pfx, String nsURI)
- {
- this.extraPrefixes.put(pfx,nsURI);
- }
-
- public String getNamespaceForPrefix(String pfx)
- {
- String nsURI = (String)this.extraPrefixes.get(pfx);
-
- if (nsURI != null) return nsURI;
-
+ public String getNamespaceForPrefix(String pfx) {
+ String nsURI = extraPrefixes.get(pfx);
+
+ if (nsURI != null) {
+ return nsURI;
+ }
+
return super.getNamespaceForPrefix(pfx);
}
}
-
- /* (non-Javadoc)
- * @see org.apache.xml.security.utils.resolver.ResourceResolverSpi#engineCanResolve(org.w3c.dom.Attr, java.lang.String)
- */
- public boolean engineCanResolve(Attr uri, String BaseURI)
- {
+
+ public boolean engineCanResolve(Attr uri, String BaseURI) {
String v = uri.getNodeValue();
-
- if (v==null || v.length() <= 0) return false;
-
- if (v.charAt(0) != '#')
+
+ if (v==null || v.length() <= 0) {
return false;
-
+ }
+
+ if (v.charAt(0) != '#') {
+ return false;
+ }
+
String xpURI;
- try
- {
+ try {
xpURI = URLDecoder.decode(v, "utf-8");
- } catch (UnsupportedEncodingException e)
- {
+ } catch (UnsupportedEncodingException e) {
log.warn("utf-8 not a valid encoding",e);
return false;
}
-
+
String parts[] = xpURI.substring(1).split("\\s");
-
+
// plain ID reference.
- if (parts.length == 1 && !parts[0].startsWith(XNS_OPEN))
+ if (parts.length == 1 && !parts[0].startsWith(XNS_OPEN)) {
return true;
-
- int i=0;
-
- for (;i<parts.length-1;++i)
- if (!parts[i].endsWith(")") || !parts[i].startsWith(XNS_OPEN))
- return false;
-
- if (!parts[i].endsWith(")") || !parts[i].startsWith(XP_OPEN))
+ }
+
+ int i = 0;
+ for (; i < parts.length - 1; ++i) {
+ if (!parts[i].endsWith(")") || !parts[i].startsWith(XNS_OPEN)) {
return false;
-
- log.debug("xpURI="+xpURI);
- log.debug("BaseURI="+BaseURI);
+ }
+ }
+
+ if (!parts[i].endsWith(")") || !parts[i].startsWith(XP_OPEN)) {
+ return false;
+ }
+
+ log.debug("xpURI = " + xpURI);
+ log.debug("BaseURI = " + BaseURI);
return true;
}
- /* (non-Javadoc)
- * @see org.apache.xml.security.utils.resolver.ResourceResolverSpi#engineResolve(org.w3c.dom.Attr, java.lang.String)
- */
- public XMLSignatureInput engineResolve(Attr uri, String BaseURI) throws ResourceResolverException
- {
+ public XMLSignatureInput engineResolve(Attr uri, String BaseURI)
+ throws ResourceResolverException {
String v = uri.getNodeValue();
-
- if (v.charAt(0) != '#')
+
+ if (v.charAt(0) != '#') {
return null;
-
+ }
+
String xpURI;
- try
- {
+ try {
xpURI = URLDecoder.decode(v, "utf-8");
- } catch (UnsupportedEncodingException e)
- {
- log.warn("utf-8 not a valid encoding",e);
+ } catch (UnsupportedEncodingException e) {
+ log.warn("utf-8 not a valid encoding ", e);
return null;
}
-
+
String parts[] = xpURI.substring(1).split("\\s");
-
- int i=0;
- XPointerPrefixResolver nsContext=null;
-
- if (parts.length > 1)
- {
- nsContext= new XPointerPrefixResolver(this.baseNode);
-
- for (;i<parts.length-1;++i)
- {
- if (!parts[i].endsWith(")") || !parts[i].startsWith(XNS_OPEN))
+ int i = 0;
+ XPointerPrefixResolver nsContext = null;
+
+ if (parts.length > 1) {
+ nsContext = new XPointerPrefixResolver(this.baseNode);
+
+ for (; i < parts.length - 1; ++i) {
+ if (!parts[i].endsWith(")") || !parts[i].startsWith(XNS_OPEN)) {
return null;
-
- String mapping = parts[i].substring(XNS_OPEN.length(),parts[i].length()-1);
-
+ }
+
+ String mapping = parts[i].substring(XNS_OPEN.length(), parts[i].length() - 1);
+
int pos = mapping.indexOf('=');
-
- if (pos <= 0 || pos >= mapping.length()-1)
- throw new ResourceResolverException("malformed namespace part of XPointer expression",uri,BaseURI);
-
- nsContext.addExtraPrefix(mapping.substring(0,pos),
- mapping.substring(pos+1));
+
+ if (pos <= 0 || pos >= mapping.length() - 1) {
+ throw new ResourceResolverException(
+ "malformed namespace part of XPointer expression", uri, BaseURI
+ );
+ }
+
+ nsContext.addExtraPrefix(
+ mapping.substring(0, pos), mapping.substring(pos + 1)
+ );
}
}
-
- try
- {
+
+ try {
Node node = null;
- NodeList nodes=null;
-
+ NodeList nodes = null;
+
// plain ID reference.
- if (i==0 && !parts[i].startsWith(XP_OPEN))
- {
+ if (i == 0 && !parts[i].startsWith(XP_OPEN)) {
node = this.baseNode.getOwnerDocument().getElementById(parts[i]);
- }
- else
- {
- if (!parts[i].endsWith(")") || !parts[i].startsWith(XP_OPEN))
+ } else {
+ if (!parts[i].endsWith(")") || !parts[i].startsWith(XP_OPEN)) {
return null;
-
- String xpathExpr = parts[i].substring(XP_OPEN.length(),parts[i].length()-1);
-
+ }
+
+ String xpathExpr = parts[i].substring(XP_OPEN.length(), parts[i].length() - 1);
+
XObject xo;
-
- if (nsContext != null)
- xo = XPathAPI.eval(this.baseNode, xpathExpr,nsContext);
- else
+ if (nsContext != null) {
+ xo = XPathAPI.eval(this.baseNode, xpathExpr, nsContext);
+ } else {
xo = XPathAPI.eval(this.baseNode, xpathExpr);
-
-
- if (!(xo instanceof NodeSequence)) return null;
-
+ }
+
+
+ if (!(xo instanceof NodeSequence)) {
+ return null;
+ }
+
nodes = ((NodeSequence)xo).nodelist();
-
- if (nodes.getLength() == 0) return null;
- if (nodes.getLength() == 1)
+
+ if (nodes.getLength() == 0) {
+ return null;
+ }
+ if (nodes.getLength() == 1) {
node = nodes.item(0);
+ }
}
-
+
XMLSignatureInput result = null;
-
- if (node != null)
- {
- result = new XMLSignatureInput(node);
- }
- else if (nodes !=null)
- {
- Set nodeSet = new HashSet(nodes.getLength());
-
- for (int j=0 ; j<nodes.getLength(); ++j)
- {
+
+ if (node != null) {
+ result = new XMLSignatureInput(node);
+ } else if (nodes != null) {
+ Set<Node> nodeSet = new HashSet<Node>(nodes.getLength());
+
+ for (int j = 0; j < nodes.getLength(); ++j) {
nodeSet.add(nodes.item(j));
}
-
+
result = new XMLSignatureInput(nodeSet);
- }
- else
+ } else {
return null;
-
+ }
+
result.setMIMEType("text/xml");
result.setExcludeComments(true);
- result.setSourceURI((BaseURI != null) ? BaseURI.concat(v):v);
+ result.setSourceURI((BaseURI != null) ? BaseURI.concat(v) : v);
return result;
-
- } catch (TransformerException e)
- {
- throw new ResourceResolverException("TransformerException inside XPointer expression",e,uri,BaseURI);
+ } catch (TransformerException e) {
+ throw new ResourceResolverException(
+ "TransformerException inside XPointer expression", e, uri, BaseURI
+ );
}
}